[BACK]Return to man_term.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/man_term.c, Revision 1.107

1.107   ! kristaps    1: /*     $Id: man_term.c,v 1.106 2011/04/29 22:18:12 kristaps Exp $ */
1.1       kristaps    2: /*
1.98      schwarze    3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.94      schwarze    4:  * Copyright (c) 2010, 2011 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
1.8       kristaps    7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    9:  *
1.8       kristaps   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   17:  */
1.55      kristaps   18: #ifdef HAVE_CONFIG_H
                     19: #include "config.h"
                     20: #endif
                     21:
1.28      kristaps   22: #include <sys/types.h>
                     23:
1.1       kristaps   24: #include <assert.h>
1.18      kristaps   25: #include <ctype.h>
1.1       kristaps   26: #include <stdio.h>
                     27: #include <stdlib.h>
                     28: #include <string.h>
                     29:
1.71      kristaps   30: #include "mandoc.h"
1.40      kristaps   31: #include "out.h"
1.36      kristaps   32: #include "man.h"
1.1       kristaps   33: #include "term.h"
1.36      kristaps   34: #include "main.h"
1.1       kristaps   35:
1.18      kristaps   36: #define        INDENT            7
                     37: #define        HALFINDENT        3
                     38:
1.44      kristaps   39: /* FIXME: have PD set the default vspace width. */
                     40:
1.24      kristaps   41: struct mtermp {
                     42:        int               fl;
1.19      kristaps   43: #define        MANT_LITERAL     (1 << 0)
1.26      kristaps   44:        /*
                     45:         * Default amount to indent the left margin after leading text
                     46:         * has been printed (e.g., `HP' left-indent, `TP' and `IP' body
                     47:         * indent).  This needs to be saved because `HP' and so on, if
                     48:         * not having a specified value, must default.
                     49:         *
                     50:         * Note that this is the indentation AFTER the left offset, so
                     51:         * the total offset is usually offset + lmargin.
                     52:         */
                     53:        size_t            lmargin;
                     54:        /*
                     55:         * The default offset, i.e., the amount between any text and the
                     56:         * page boundary.
                     57:         */
                     58:        size_t            offset;
1.24      kristaps   59: };
1.19      kristaps   60:
1.1       kristaps   61: #define        DECL_ARGS         struct termp *p, \
1.24      kristaps   62:                          struct mtermp *mt, \
1.1       kristaps   63:                          const struct man_node *n, \
                     64:                          const struct man_meta *m
                     65:
                     66: struct termact {
                     67:        int             (*pre)(DECL_ARGS);
                     68:        void            (*post)(DECL_ARGS);
1.56      kristaps   69:        int               flags;
                     70: #define        MAN_NOTEXT       (1 << 0) /* Never has text children. */
1.1       kristaps   71: };
                     72:
1.77      kristaps   73: static int               a2width(const struct termp *, const char *);
                     74: static size_t            a2height(const struct termp *, const char *);
1.39      kristaps   75:
1.52      kristaps   76: static void              print_man_nodelist(DECL_ARGS);
1.45      kristaps   77: static void              print_man_node(DECL_ARGS);
1.74      kristaps   78: static void              print_man_head(struct termp *, const void *);
1.73      kristaps   79: static void              print_man_foot(struct termp *, const void *);
1.39      kristaps   80: static void              print_bvspace(struct termp *,
                     81:                                const struct man_node *);
                     82:
1.88      kristaps   83: static int               pre_alternate(DECL_ARGS);
1.1       kristaps   84: static int               pre_B(DECL_ARGS);
1.19      kristaps   85: static int               pre_HP(DECL_ARGS);
1.1       kristaps   86: static int               pre_I(DECL_ARGS);
1.4       kristaps   87: static int               pre_IP(DECL_ARGS);
1.1       kristaps   88: static int               pre_PP(DECL_ARGS);
1.26      kristaps   89: static int               pre_RS(DECL_ARGS);
1.1       kristaps   90: static int               pre_SH(DECL_ARGS);
                     91: static int               pre_SS(DECL_ARGS);
                     92: static int               pre_TP(DECL_ARGS);
1.29      kristaps   93: static int               pre_ign(DECL_ARGS);
1.83      kristaps   94: static int               pre_in(DECL_ARGS);
1.84      kristaps   95: static int               pre_literal(DECL_ARGS);
1.19      kristaps   96: static int               pre_sp(DECL_ARGS);
1.89      kristaps   97: static int               pre_ft(DECL_ARGS);
1.1       kristaps   98:
1.22      kristaps   99: static void              post_IP(DECL_ARGS);
1.20      kristaps  100: static void              post_HP(DECL_ARGS);
1.26      kristaps  101: static void              post_RS(DECL_ARGS);
1.1       kristaps  102: static void              post_SH(DECL_ARGS);
                    103: static void              post_SS(DECL_ARGS);
1.21      kristaps  104: static void              post_TP(DECL_ARGS);
1.1       kristaps  105:
1.32      kristaps  106: static const struct termact termacts[MAN_MAX] = {
1.82      kristaps  107:        { pre_sp, NULL, MAN_NOTEXT }, /* br */
1.56      kristaps  108:        { NULL, NULL, 0 }, /* TH */
                    109:        { pre_SH, post_SH, 0 }, /* SH */
                    110:        { pre_SS, post_SS, 0 }, /* SS */
                    111:        { pre_TP, post_TP, 0 }, /* TP */
                    112:        { pre_PP, NULL, 0 }, /* LP */
                    113:        { pre_PP, NULL, 0 }, /* PP */
                    114:        { pre_PP, NULL, 0 }, /* P */
                    115:        { pre_IP, post_IP, 0 }, /* IP */
                    116:        { pre_HP, post_HP, 0 }, /* HP */
                    117:        { NULL, NULL, 0 }, /* SM */
                    118:        { pre_B, NULL, 0 }, /* SB */
1.88      kristaps  119:        { pre_alternate, NULL, 0 }, /* BI */
                    120:        { pre_alternate, NULL, 0 }, /* IB */
                    121:        { pre_alternate, NULL, 0 }, /* BR */
                    122:        { pre_alternate, NULL, 0 }, /* RB */
1.56      kristaps  123:        { NULL, NULL, 0 }, /* R */
                    124:        { pre_B, NULL, 0 }, /* B */
                    125:        { pre_I, NULL, 0 }, /* I */
1.88      kristaps  126:        { pre_alternate, NULL, 0 }, /* IR */
                    127:        { pre_alternate, NULL, 0 }, /* RI */
1.99      schwarze  128:        { pre_ign, NULL, MAN_NOTEXT }, /* na */
1.56      kristaps  129:        { pre_sp, NULL, MAN_NOTEXT }, /* sp */
1.84      kristaps  130:        { pre_literal, NULL, 0 }, /* nf */
                    131:        { pre_literal, NULL, 0 }, /* fi */
1.56      kristaps  132:        { NULL, NULL, 0 }, /* RE */
                    133:        { pre_RS, post_RS, 0 }, /* RS */
                    134:        { pre_ign, NULL, 0 }, /* DT */
                    135:        { pre_ign, NULL, 0 }, /* UC */
                    136:        { pre_ign, NULL, 0 }, /* PD */
1.70      joerg     137:        { pre_ign, NULL, 0 }, /* AT */
1.83      kristaps  138:        { pre_in, NULL, MAN_NOTEXT }, /* in */
1.89      kristaps  139:        { pre_ft, NULL, MAN_NOTEXT }, /* ft */
1.1       kristaps  140: };
                    141:
                    142:
                    143:
1.31      kristaps  144: void
1.36      kristaps  145: terminal_man(void *arg, const struct man *man)
1.1       kristaps  146: {
1.36      kristaps  147:        struct termp            *p;
                    148:        const struct man_node   *n;
                    149:        const struct man_meta   *m;
                    150:        struct mtermp            mt;
                    151:
                    152:        p = (struct termp *)arg;
                    153:
1.58      kristaps  154:        p->overstep = 0;
1.65      joerg     155:        p->maxrmargin = p->defrmargin;
1.77      kristaps  156:        p->tabwidth = term_len(p, 5);
1.73      kristaps  157:
1.36      kristaps  158:        if (NULL == p->symtab)
                    159:                switch (p->enc) {
                    160:                case (TERMENC_ASCII):
1.107   ! kristaps  161:                        p->symtab = mchars_init();
1.36      kristaps  162:                        break;
                    163:                default:
                    164:                        abort();
                    165:                        /* NOTREACHED */
                    166:                }
                    167:
                    168:        n = man_node(man);
                    169:        m = man_meta(man);
1.1       kristaps  170:
1.75      schwarze  171:        term_begin(p, print_man_head, print_man_foot, m);
1.1       kristaps  172:        p->flags |= TERMP_NOSPACE;
1.19      kristaps  173:
1.24      kristaps  174:        mt.fl = 0;
1.77      kristaps  175:        mt.lmargin = term_len(p, INDENT);
                    176:        mt.offset = term_len(p, INDENT);
1.24      kristaps  177:
1.36      kristaps  178:        if (n->child)
1.52      kristaps  179:                print_man_nodelist(p, &mt, n->child, m);
1.73      kristaps  180:
                    181:        term_end(p);
1.1       kristaps  182: }
                    183:
                    184:
1.77      kristaps  185: static size_t
                    186: a2height(const struct termp *p, const char *cp)
1.18      kristaps  187: {
1.40      kristaps  188:        struct roffsu    su;
1.18      kristaps  189:
1.77      kristaps  190:        if ( ! a2roffsu(cp, &su, SCALE_VS))
                    191:                SCALE_VS_INIT(&su, term_strlen(p, cp));
1.18      kristaps  192:
1.77      kristaps  193:        return(term_vspan(p, &su));
1.18      kristaps  194: }
                    195:
                    196:
                    197: static int
1.77      kristaps  198: a2width(const struct termp *p, const char *cp)
1.38      kristaps  199: {
1.40      kristaps  200:        struct roffsu    su;
1.38      kristaps  201:
1.77      kristaps  202:        if ( ! a2roffsu(cp, &su, SCALE_BU))
1.41      kristaps  203:                return(-1);
1.40      kristaps  204:
1.77      kristaps  205:        return((int)term_hspan(p, &su));
1.38      kristaps  206: }
                    207:
                    208:
1.39      kristaps  209: static void
                    210: print_bvspace(struct termp *p, const struct man_node *n)
1.18      kristaps  211: {
1.39      kristaps  212:        term_newln(p);
1.101     schwarze  213:
                    214:        if (n->body && n->body->child && MAN_TBL == n->body->child->type)
                    215:                return;
1.18      kristaps  216:
1.39      kristaps  217:        if (NULL == n->prev)
                    218:                return;
1.18      kristaps  219:
1.39      kristaps  220:        if (MAN_SS == n->prev->tok)
                    221:                return;
                    222:        if (MAN_SH == n->prev->tok)
                    223:                return;
1.18      kristaps  224:
1.39      kristaps  225:        term_vspace(p);
1.18      kristaps  226: }
                    227:
                    228:
1.3       kristaps  229: /* ARGSUSED */
1.1       kristaps  230: static int
1.29      kristaps  231: pre_ign(DECL_ARGS)
                    232: {
                    233:
                    234:        return(0);
                    235: }
                    236:
                    237:
                    238: /* ARGSUSED */
                    239: static int
1.1       kristaps  240: pre_I(DECL_ARGS)
                    241: {
                    242:
1.52      kristaps  243:        term_fontrepl(p, TERMFONT_UNDER);
1.19      kristaps  244:        return(1);
                    245: }
                    246:
                    247:
                    248: /* ARGSUSED */
1.3       kristaps  249: static int
1.84      kristaps  250: pre_literal(DECL_ARGS)
1.19      kristaps  251: {
                    252:
1.81      kristaps  253:        term_newln(p);
1.88      kristaps  254:
                    255:        if (MAN_nf == n->tok)
1.84      kristaps  256:                mt->fl |= MANT_LITERAL;
1.88      kristaps  257:        else
1.84      kristaps  258:                mt->fl &= ~MANT_LITERAL;
                    259:
1.99      schwarze  260:        return(0);
1.19      kristaps  261: }
                    262:
                    263: /* ARGSUSED */
                    264: static int
1.88      kristaps  265: pre_alternate(DECL_ARGS)
1.3       kristaps  266: {
1.88      kristaps  267:        enum termfont            font[2];
                    268:        const struct man_node   *nn;
                    269:        int                      savelit, i;
1.3       kristaps  270:
1.88      kristaps  271:        switch (n->tok) {
                    272:        case (MAN_RB):
                    273:                font[0] = TERMFONT_NONE;
                    274:                font[1] = TERMFONT_BOLD;
                    275:                break;
                    276:        case (MAN_RI):
                    277:                font[0] = TERMFONT_NONE;
                    278:                font[1] = TERMFONT_UNDER;
                    279:                break;
                    280:        case (MAN_BR):
                    281:                font[0] = TERMFONT_BOLD;
                    282:                font[1] = TERMFONT_NONE;
                    283:                break;
                    284:        case (MAN_BI):
                    285:                font[0] = TERMFONT_BOLD;
                    286:                font[1] = TERMFONT_UNDER;
                    287:                break;
                    288:        case (MAN_IR):
                    289:                font[0] = TERMFONT_UNDER;
                    290:                font[1] = TERMFONT_NONE;
                    291:                break;
                    292:        case (MAN_IB):
                    293:                font[0] = TERMFONT_UNDER;
                    294:                font[1] = TERMFONT_BOLD;
                    295:                break;
                    296:        default:
                    297:                abort();
                    298:        }
1.35      kristaps  299:
1.88      kristaps  300:        savelit = MANT_LITERAL & mt->fl;
                    301:        mt->fl &= ~MANT_LITERAL;
1.35      kristaps  302:
1.88      kristaps  303:        for (i = 0, nn = n->child; nn; nn = nn->next, i = 1 - i) {
                    304:                term_fontrepl(p, font[i]);
                    305:                if (savelit && NULL == nn->next)
                    306:                        mt->fl |= MANT_LITERAL;
1.45      kristaps  307:                print_man_node(p, mt, nn, m);
1.88      kristaps  308:                if (nn->next)
1.4       kristaps  309:                        p->flags |= TERMP_NOSPACE;
1.3       kristaps  310:        }
                    311:
                    312:        return(0);
                    313: }
                    314:
                    315: /* ARGSUSED */
1.1       kristaps  316: static int
                    317: pre_B(DECL_ARGS)
                    318: {
                    319:
1.52      kristaps  320:        term_fontrepl(p, TERMFONT_BOLD);
1.1       kristaps  321:        return(1);
1.89      kristaps  322: }
                    323:
                    324: /* ARGSUSED */
                    325: static int
                    326: pre_ft(DECL_ARGS)
                    327: {
                    328:        const char      *cp;
                    329:
                    330:        if (NULL == n->child) {
                    331:                term_fontlast(p);
                    332:                return(0);
                    333:        }
                    334:
                    335:        cp = n->child->string;
                    336:        switch (*cp) {
                    337:        case ('4'):
                    338:                /* FALLTHROUGH */
                    339:        case ('3'):
                    340:                /* FALLTHROUGH */
                    341:        case ('B'):
                    342:                term_fontrepl(p, TERMFONT_BOLD);
                    343:                break;
                    344:        case ('2'):
                    345:                /* FALLTHROUGH */
                    346:        case ('I'):
                    347:                term_fontrepl(p, TERMFONT_UNDER);
                    348:                break;
                    349:        case ('P'):
                    350:                term_fontlast(p);
                    351:                break;
                    352:        case ('1'):
                    353:                /* FALLTHROUGH */
                    354:        case ('C'):
                    355:                /* FALLTHROUGH */
                    356:        case ('R'):
                    357:                term_fontrepl(p, TERMFONT_NONE);
                    358:                break;
                    359:        default:
                    360:                break;
                    361:        }
                    362:        return(0);
1.83      kristaps  363: }
                    364:
                    365: /* ARGSUSED */
                    366: static int
                    367: pre_in(DECL_ARGS)
                    368: {
                    369:        int              len, less;
                    370:        size_t           v;
                    371:        const char      *cp;
                    372:
                    373:        term_newln(p);
                    374:
                    375:        if (NULL == n->child) {
                    376:                p->offset = mt->offset;
                    377:                return(0);
                    378:        }
                    379:
                    380:        cp = n->child->string;
                    381:        less = 0;
                    382:
                    383:        if ('-' == *cp)
                    384:                less = -1;
                    385:        else if ('+' == *cp)
                    386:                less = 1;
                    387:        else
                    388:                cp--;
                    389:
                    390:        if ((len = a2width(p, ++cp)) < 0)
                    391:                return(0);
                    392:
                    393:        v = (size_t)len;
                    394:
                    395:        if (less < 0)
                    396:                p->offset -= p->offset > v ? v : p->offset;
                    397:        else if (less > 0)
                    398:                p->offset += v;
                    399:        else
                    400:                p->offset = v;
1.95      kristaps  401:
                    402:        /* Don't let this creep beyond the right margin. */
                    403:
                    404:        if (p->offset > p->rmargin)
                    405:                p->offset = p->rmargin;
1.83      kristaps  406:
                    407:        return(0);
1.1       kristaps  408: }
                    409:
                    410:
1.3       kristaps  411: /* ARGSUSED */
1.1       kristaps  412: static int
1.19      kristaps  413: pre_sp(DECL_ARGS)
                    414: {
1.77      kristaps  415:        size_t           i, len;
1.19      kristaps  416:
1.82      kristaps  417:        switch (n->tok) {
                    418:        case (MAN_br):
                    419:                len = 0;
                    420:                break;
                    421:        default:
                    422:                len = n->child ? a2height(p, n->child->string) : 1;
                    423:                break;
                    424:        }
1.38      kristaps  425:
1.19      kristaps  426:        if (0 == len)
                    427:                term_newln(p);
1.82      kristaps  428:        for (i = 0; i < len; i++)
1.19      kristaps  429:                term_vspace(p);
                    430:
1.15      kristaps  431:        return(0);
                    432: }
                    433:
                    434:
                    435: /* ARGSUSED */
                    436: static int
1.19      kristaps  437: pre_HP(DECL_ARGS)
                    438: {
1.24      kristaps  439:        size_t                   len;
                    440:        int                      ival;
                    441:        const struct man_node   *nn;
1.19      kristaps  442:
1.20      kristaps  443:        switch (n->type) {
                    444:        case (MAN_BLOCK):
1.39      kristaps  445:                print_bvspace(p, n);
1.24      kristaps  446:                return(1);
1.20      kristaps  447:        case (MAN_BODY):
                    448:                p->flags |= TERMP_NOBREAK;
                    449:                p->flags |= TERMP_TWOSPACE;
                    450:                break;
                    451:        default:
                    452:                return(0);
                    453:        }
                    454:
1.26      kristaps  455:        len = mt->lmargin;
1.24      kristaps  456:        ival = -1;
                    457:
                    458:        /* Calculate offset. */
                    459:
1.39      kristaps  460:        if (NULL != (nn = n->parent->head->child))
1.77      kristaps  461:                if ((ival = a2width(p, nn->string)) >= 0)
1.24      kristaps  462:                        len = (size_t)ival;
                    463:
                    464:        if (0 == len)
1.77      kristaps  465:                len = term_len(p, 1);
1.24      kristaps  466:
1.26      kristaps  467:        p->offset = mt->offset;
                    468:        p->rmargin = mt->offset + len;
1.24      kristaps  469:
                    470:        if (ival >= 0)
1.26      kristaps  471:                mt->lmargin = (size_t)ival;
1.24      kristaps  472:
1.19      kristaps  473:        return(1);
                    474: }
                    475:
                    476:
                    477: /* ARGSUSED */
1.20      kristaps  478: static void
                    479: post_HP(DECL_ARGS)
                    480: {
                    481:
                    482:        switch (n->type) {
1.24      kristaps  483:        case (MAN_BLOCK):
                    484:                term_flushln(p);
                    485:                break;
1.20      kristaps  486:        case (MAN_BODY):
                    487:                term_flushln(p);
                    488:                p->flags &= ~TERMP_NOBREAK;
                    489:                p->flags &= ~TERMP_TWOSPACE;
1.26      kristaps  490:                p->offset = mt->offset;
1.20      kristaps  491:                p->rmargin = p->maxrmargin;
                    492:                break;
                    493:        default:
                    494:                break;
                    495:        }
                    496: }
                    497:
                    498:
                    499: /* ARGSUSED */
1.19      kristaps  500: static int
1.1       kristaps  501: pre_PP(DECL_ARGS)
                    502: {
                    503:
1.19      kristaps  504:        switch (n->type) {
                    505:        case (MAN_BLOCK):
1.77      kristaps  506:                mt->lmargin = term_len(p, INDENT);
1.39      kristaps  507:                print_bvspace(p, n);
1.19      kristaps  508:                break;
                    509:        default:
1.26      kristaps  510:                p->offset = mt->offset;
1.19      kristaps  511:                break;
                    512:        }
                    513:
1.87      kristaps  514:        return(MAN_HEAD != n->type);
1.1       kristaps  515: }
                    516:
                    517:
1.3       kristaps  518: /* ARGSUSED */
1.1       kristaps  519: static int
1.4       kristaps  520: pre_IP(DECL_ARGS)
                    521: {
1.22      kristaps  522:        const struct man_node   *nn;
                    523:        size_t                   len;
1.94      schwarze  524:        int                      savelit, ival;
1.18      kristaps  525:
1.22      kristaps  526:        switch (n->type) {
                    527:        case (MAN_BODY):
                    528:                p->flags |= TERMP_NOLPAD;
                    529:                p->flags |= TERMP_NOSPACE;
                    530:                break;
                    531:        case (MAN_HEAD):
                    532:                p->flags |= TERMP_NOBREAK;
                    533:                break;
1.23      kristaps  534:        case (MAN_BLOCK):
1.39      kristaps  535:                print_bvspace(p, n);
1.23      kristaps  536:                /* FALLTHROUGH */
1.22      kristaps  537:        default:
                    538:                return(1);
                    539:        }
1.18      kristaps  540:
1.26      kristaps  541:        len = mt->lmargin;
1.22      kristaps  542:        ival = -1;
1.4       kristaps  543:
1.94      schwarze  544:        /* Calculate the offset from the optional second argument. */
1.22      kristaps  545:        if (NULL != (nn = n->parent->head->child))
1.94      schwarze  546:                if (NULL != (nn = nn->next))
1.77      kristaps  547:                        if ((ival = a2width(p, nn->string)) >= 0)
1.22      kristaps  548:                                len = (size_t)ival;
1.4       kristaps  549:
1.22      kristaps  550:        switch (n->type) {
                    551:        case (MAN_HEAD):
1.23      kristaps  552:                /* Handle zero-width lengths. */
                    553:                if (0 == len)
1.77      kristaps  554:                        len = term_len(p, 1);
1.23      kristaps  555:
1.26      kristaps  556:                p->offset = mt->offset;
                    557:                p->rmargin = mt->offset + len;
1.22      kristaps  558:                if (ival < 0)
                    559:                        break;
1.18      kristaps  560:
1.24      kristaps  561:                /* Set the saved left-margin. */
1.26      kristaps  562:                mt->lmargin = (size_t)ival;
1.24      kristaps  563:
1.94      schwarze  564:                savelit = MANT_LITERAL & mt->fl;
                    565:                mt->fl &= ~MANT_LITERAL;
                    566:
                    567:                if (n->child)
                    568:                        print_man_node(p, mt, n->child, m);
                    569:
                    570:                if (savelit)
                    571:                        mt->fl |= MANT_LITERAL;
                    572:
1.22      kristaps  573:                return(0);
1.23      kristaps  574:        case (MAN_BODY):
1.26      kristaps  575:                p->offset = mt->offset + len;
1.23      kristaps  576:                p->rmargin = p->maxrmargin;
                    577:                break;
1.22      kristaps  578:        default:
                    579:                break;
1.18      kristaps  580:        }
                    581:
1.22      kristaps  582:        return(1);
                    583: }
1.18      kristaps  584:
                    585:
1.22      kristaps  586: /* ARGSUSED */
                    587: static void
                    588: post_IP(DECL_ARGS)
                    589: {
1.4       kristaps  590:
1.22      kristaps  591:        switch (n->type) {
                    592:        case (MAN_HEAD):
                    593:                term_flushln(p);
                    594:                p->flags &= ~TERMP_NOBREAK;
                    595:                p->rmargin = p->maxrmargin;
                    596:                break;
                    597:        case (MAN_BODY):
1.94      schwarze  598:                term_newln(p);
1.22      kristaps  599:                p->flags &= ~TERMP_NOLPAD;
                    600:                break;
                    601:        default:
                    602:                break;
                    603:        }
1.4       kristaps  604: }
                    605:
                    606:
                    607: /* ARGSUSED */
                    608: static int
1.1       kristaps  609: pre_TP(DECL_ARGS)
                    610: {
1.23      kristaps  611:        const struct man_node   *nn;
                    612:        size_t                   len;
1.94      schwarze  613:        int                      savelit, ival;
1.1       kristaps  614:
1.21      kristaps  615:        switch (n->type) {
                    616:        case (MAN_HEAD):
                    617:                p->flags |= TERMP_NOBREAK;
                    618:                break;
                    619:        case (MAN_BODY):
                    620:                p->flags |= TERMP_NOLPAD;
                    621:                p->flags |= TERMP_NOSPACE;
1.23      kristaps  622:                break;
                    623:        case (MAN_BLOCK):
1.39      kristaps  624:                print_bvspace(p, n);
1.23      kristaps  625:                /* FALLTHROUGH */
                    626:        default:
                    627:                return(1);
                    628:        }
                    629:
1.24      kristaps  630:        len = (size_t)mt->lmargin;
1.23      kristaps  631:        ival = -1;
                    632:
                    633:        /* Calculate offset. */
                    634:
1.56      kristaps  635:        if (NULL != (nn = n->parent->head->child)) {
                    636:                while (nn && MAN_TEXT != nn->type)
                    637:                        nn = nn->next;
                    638:                if (nn && nn->next)
1.77      kristaps  639:                        if ((ival = a2width(p, nn->string)) >= 0)
1.23      kristaps  640:                                len = (size_t)ival;
1.56      kristaps  641:        }
1.23      kristaps  642:
                    643:        switch (n->type) {
                    644:        case (MAN_HEAD):
                    645:                /* Handle zero-length properly. */
                    646:                if (0 == len)
1.77      kristaps  647:                        len = term_len(p, 1);
1.23      kristaps  648:
1.26      kristaps  649:                p->offset = mt->offset;
                    650:                p->rmargin = mt->offset + len;
1.23      kristaps  651:
1.94      schwarze  652:                savelit = MANT_LITERAL & mt->fl;
                    653:                mt->fl &= ~MANT_LITERAL;
                    654:
1.23      kristaps  655:                /* Don't print same-line elements. */
1.94      schwarze  656:                for (nn = n->child; nn; nn = nn->next)
1.23      kristaps  657:                        if (nn->line > n->line)
1.45      kristaps  658:                                print_man_node(p, mt, nn, m);
1.24      kristaps  659:
1.94      schwarze  660:                if (savelit)
                    661:                        mt->fl |= MANT_LITERAL;
                    662:
1.24      kristaps  663:                if (ival >= 0)
1.26      kristaps  664:                        mt->lmargin = (size_t)ival;
1.24      kristaps  665:
1.23      kristaps  666:                return(0);
                    667:        case (MAN_BODY):
1.26      kristaps  668:                p->offset = mt->offset + len;
1.23      kristaps  669:                p->rmargin = p->maxrmargin;
1.21      kristaps  670:                break;
                    671:        default:
                    672:                break;
                    673:        }
1.16      kristaps  674:
1.21      kristaps  675:        return(1);
                    676: }
1.1       kristaps  677:
                    678:
1.21      kristaps  679: /* ARGSUSED */
                    680: static void
                    681: post_TP(DECL_ARGS)
                    682: {
1.1       kristaps  683:
1.21      kristaps  684:        switch (n->type) {
                    685:        case (MAN_HEAD):
                    686:                term_flushln(p);
                    687:                p->flags &= ~TERMP_NOBREAK;
                    688:                p->flags &= ~TERMP_TWOSPACE;
                    689:                p->rmargin = p->maxrmargin;
                    690:                break;
                    691:        case (MAN_BODY):
1.94      schwarze  692:                term_newln(p);
1.21      kristaps  693:                p->flags &= ~TERMP_NOLPAD;
                    694:                break;
                    695:        default:
                    696:                break;
                    697:        }
1.1       kristaps  698: }
                    699:
                    700:
1.3       kristaps  701: /* ARGSUSED */
1.1       kristaps  702: static int
                    703: pre_SS(DECL_ARGS)
                    704: {
                    705:
1.19      kristaps  706:        switch (n->type) {
                    707:        case (MAN_BLOCK):
1.77      kristaps  708:                mt->lmargin = term_len(p, INDENT);
                    709:                mt->offset = term_len(p, INDENT);
1.24      kristaps  710:                /* If following a prior empty `SS', no vspace. */
                    711:                if (n->prev && MAN_SS == n->prev->tok)
                    712:                        if (NULL == n->prev->body->child)
                    713:                                break;
                    714:                if (NULL == n->prev)
                    715:                        break;
                    716:                term_vspace(p);
1.19      kristaps  717:                break;
                    718:        case (MAN_HEAD):
1.52      kristaps  719:                term_fontrepl(p, TERMFONT_BOLD);
1.77      kristaps  720:                p->offset = term_len(p, HALFINDENT);
1.19      kristaps  721:                break;
1.24      kristaps  722:        case (MAN_BODY):
1.26      kristaps  723:                p->offset = mt->offset;
1.24      kristaps  724:                break;
1.19      kristaps  725:        default:
                    726:                break;
                    727:        }
                    728:
1.1       kristaps  729:        return(1);
                    730: }
                    731:
                    732:
1.3       kristaps  733: /* ARGSUSED */
1.1       kristaps  734: static void
                    735: post_SS(DECL_ARGS)
                    736: {
                    737:
1.19      kristaps  738:        switch (n->type) {
                    739:        case (MAN_HEAD):
                    740:                term_newln(p);
                    741:                break;
1.24      kristaps  742:        case (MAN_BODY):
                    743:                term_newln(p);
                    744:                break;
1.19      kristaps  745:        default:
                    746:                break;
                    747:        }
1.1       kristaps  748: }
                    749:
                    750:
1.3       kristaps  751: /* ARGSUSED */
1.1       kristaps  752: static int
                    753: pre_SH(DECL_ARGS)
                    754: {
1.22      kristaps  755:
1.19      kristaps  756:        switch (n->type) {
                    757:        case (MAN_BLOCK):
1.77      kristaps  758:                mt->lmargin = term_len(p, INDENT);
                    759:                mt->offset = term_len(p, INDENT);
1.22      kristaps  760:                /* If following a prior empty `SH', no vspace. */
1.19      kristaps  761:                if (n->prev && MAN_SH == n->prev->tok)
                    762:                        if (NULL == n->prev->body->child)
                    763:                                break;
1.61      kristaps  764:                /* If the first macro, no vspae. */
                    765:                if (NULL == n->prev)
                    766:                        break;
1.19      kristaps  767:                term_vspace(p);
                    768:                break;
                    769:        case (MAN_HEAD):
1.52      kristaps  770:                term_fontrepl(p, TERMFONT_BOLD);
1.19      kristaps  771:                p->offset = 0;
                    772:                break;
                    773:        case (MAN_BODY):
1.26      kristaps  774:                p->offset = mt->offset;
1.19      kristaps  775:                break;
                    776:        default:
                    777:                break;
                    778:        }
1.1       kristaps  779:
                    780:        return(1);
                    781: }
                    782:
                    783:
1.3       kristaps  784: /* ARGSUSED */
1.1       kristaps  785: static void
                    786: post_SH(DECL_ARGS)
                    787: {
                    788:
1.19      kristaps  789:        switch (n->type) {
                    790:        case (MAN_HEAD):
                    791:                term_newln(p);
                    792:                break;
                    793:        case (MAN_BODY):
                    794:                term_newln(p);
                    795:                break;
                    796:        default:
                    797:                break;
                    798:        }
1.1       kristaps  799: }
                    800:
                    801:
1.26      kristaps  802: /* ARGSUSED */
                    803: static int
                    804: pre_RS(DECL_ARGS)
                    805: {
                    806:        const struct man_node   *nn;
                    807:        int                      ival;
                    808:
                    809:        switch (n->type) {
                    810:        case (MAN_BLOCK):
                    811:                term_newln(p);
                    812:                return(1);
                    813:        case (MAN_HEAD):
                    814:                return(0);
                    815:        default:
                    816:                break;
                    817:        }
                    818:
                    819:        if (NULL == (nn = n->parent->head->child)) {
1.77      kristaps  820:                mt->offset = mt->lmargin + term_len(p, INDENT);
1.26      kristaps  821:                p->offset = mt->offset;
                    822:                return(1);
                    823:        }
                    824:
1.77      kristaps  825:        if ((ival = a2width(p, nn->string)) < 0)
1.26      kristaps  826:                return(1);
                    827:
1.77      kristaps  828:        mt->offset = term_len(p, INDENT) + (size_t)ival;
1.26      kristaps  829:        p->offset = mt->offset;
                    830:
                    831:        return(1);
                    832: }
                    833:
                    834:
                    835: /* ARGSUSED */
                    836: static void
                    837: post_RS(DECL_ARGS)
                    838: {
                    839:
                    840:        switch (n->type) {
                    841:        case (MAN_BLOCK):
1.77      kristaps  842:                mt->offset = mt->lmargin = term_len(p, INDENT);
1.59      kristaps  843:                break;
                    844:        case (MAN_HEAD):
1.26      kristaps  845:                break;
                    846:        default:
                    847:                term_newln(p);
1.77      kristaps  848:                p->offset = term_len(p, INDENT);
1.26      kristaps  849:                break;
                    850:        }
                    851: }
                    852:
                    853:
1.1       kristaps  854: static void
1.45      kristaps  855: print_man_node(DECL_ARGS)
1.1       kristaps  856: {
1.65      joerg     857:        size_t           rm, rmax;
1.54      kristaps  858:        int              c;
1.1       kristaps  859:
                    860:        switch (n->type) {
                    861:        case(MAN_TEXT):
1.97      kristaps  862:                /*
                    863:                 * If we have a blank line, output a vertical space.
                    864:                 * If we have a space as the first character, break
                    865:                 * before printing the line's data.
                    866:                 */
1.96      kristaps  867:                if ('\0' == *n->string) {
1.4       kristaps  868:                        term_vspace(p);
1.98      schwarze  869:                        return;
1.97      kristaps  870:                } else if (' ' == *n->string && MAN_LINE & n->flags)
1.96      kristaps  871:                        term_newln(p);
1.54      kristaps  872:
1.4       kristaps  873:                term_word(p, n->string);
1.52      kristaps  874:
1.97      kristaps  875:                /*
                    876:                 * If we're in a literal context, make sure that words
                    877:                 * togehter on the same line stay together.  This is a
                    878:                 * POST-printing call, so we check the NEXT word.  Since
                    879:                 * -man doesn't have nested macros, we don't need to be
                    880:                 * more specific than this.
                    881:                 */
                    882:                if (MANT_LITERAL & mt->fl &&
                    883:                                (NULL == n->next ||
                    884:                                 n->next->line > n->line)) {
1.65      joerg     885:                        rm = p->rmargin;
                    886:                        rmax = p->maxrmargin;
1.60      kristaps  887:                        p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
1.19      kristaps  888:                        p->flags |= TERMP_NOSPACE;
                    889:                        term_flushln(p);
1.94      schwarze  890:                        p->flags &= ~TERMP_NOLPAD;
1.65      joerg     891:                        p->rmargin = rm;
                    892:                        p->maxrmargin = rmax;
1.19      kristaps  893:                }
1.100     schwarze  894:
                    895:                if (MAN_EOS & n->flags)
                    896:                        p->flags |= TERMP_SENTENCE;
1.102     kristaps  897:                return;
                    898:        case (MAN_EQN):
1.103     kristaps  899:                term_word(p, n->eqn->data);
1.97      kristaps  900:                return;
1.91      kristaps  901:        case (MAN_TBL):
1.97      kristaps  902:                /*
                    903:                 * Tables are preceded by a newline.  Then process a
                    904:                 * table line, which will cause line termination,
                    905:                 */
1.93      kristaps  906:                if (TBL_SPAN_FIRST & n->span->flags)
                    907:                        term_newln(p);
1.92      kristaps  908:                term_tbl(p, n->span);
1.97      kristaps  909:                return;
1.1       kristaps  910:        default:
                    911:                break;
                    912:        }
                    913:
1.97      kristaps  914:        if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
                    915:                term_fontrepl(p, TERMFONT_NONE);
                    916:
                    917:        c = 1;
                    918:        if (termacts[n->tok].pre)
                    919:                c = (*termacts[n->tok].pre)(p, mt, n, m);
                    920:
1.1       kristaps  921:        if (c && n->child)
1.52      kristaps  922:                print_man_nodelist(p, mt, n->child, m);
1.1       kristaps  923:
1.97      kristaps  924:        if (termacts[n->tok].post)
                    925:                (*termacts[n->tok].post)(p, mt, n, m);
                    926:        if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
                    927:                term_fontrepl(p, TERMFONT_NONE);
1.63      kristaps  928:
                    929:        if (MAN_EOS & n->flags)
                    930:                p->flags |= TERMP_SENTENCE;
1.1       kristaps  931: }
                    932:
                    933:
                    934: static void
1.52      kristaps  935: print_man_nodelist(DECL_ARGS)
1.1       kristaps  936: {
1.19      kristaps  937:
1.45      kristaps  938:        print_man_node(p, mt, n, m);
1.1       kristaps  939:        if ( ! n->next)
                    940:                return;
1.52      kristaps  941:        print_man_nodelist(p, mt, n->next, m);
1.1       kristaps  942: }
                    943:
                    944:
1.31      kristaps  945: static void
1.73      kristaps  946: print_man_foot(struct termp *p, const void *arg)
1.1       kristaps  947: {
1.73      kristaps  948:        const struct man_meta *meta;
                    949:
                    950:        meta = (const struct man_meta *)arg;
1.1       kristaps  951:
1.52      kristaps  952:        term_fontrepl(p, TERMFONT_NONE);
1.50      kristaps  953:
1.69      joerg     954:        term_vspace(p);
                    955:        term_vspace(p);
1.1       kristaps  956:        term_vspace(p);
                    957:
                    958:        p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
1.104     schwarze  959:        p->rmargin = p->maxrmargin - term_strlen(p, meta->date);
1.1       kristaps  960:        p->offset = 0;
1.85      kristaps  961:
                    962:        /* term_strlen() can return zero. */
                    963:        if (p->rmargin == p->maxrmargin)
                    964:                p->rmargin--;
1.1       kristaps  965:
                    966:        if (meta->source)
                    967:                term_word(p, meta->source);
                    968:        if (meta->source)
                    969:                term_word(p, "");
                    970:        term_flushln(p);
                    971:
                    972:        p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
                    973:        p->offset = p->rmargin;
                    974:        p->rmargin = p->maxrmargin;
                    975:        p->flags &= ~TERMP_NOBREAK;
                    976:
1.104     schwarze  977:        term_word(p, meta->date);
1.1       kristaps  978:        term_flushln(p);
                    979: }
                    980:
                    981:
1.31      kristaps  982: static void
1.73      kristaps  983: print_man_head(struct termp *p, const void *arg)
1.1       kristaps  984: {
1.46      kristaps  985:        char            buf[BUFSIZ], title[BUFSIZ];
1.57      kristaps  986:        size_t          buflen, titlen;
1.73      kristaps  987:        const struct man_meta *m;
                    988:
                    989:        m = (const struct man_meta *)arg;
1.1       kristaps  990:
1.62      kristaps  991:        /*
                    992:         * Note that old groff would spit out some spaces before the
                    993:         * header.  We discontinue this strange behaviour, but at one
                    994:         * point we did so here.
                    995:         */
                    996:
1.1       kristaps  997:        p->rmargin = p->maxrmargin;
1.58      kristaps  998:
1.1       kristaps  999:        p->offset = 0;
1.46      kristaps 1000:        buf[0] = title[0] = '\0';
1.1       kristaps 1001:
1.46      kristaps 1002:        if (m->vol)
                   1003:                strlcpy(buf, m->vol, BUFSIZ);
1.77      kristaps 1004:        buflen = term_strlen(p, buf);
1.1       kristaps 1005:
1.64      kristaps 1006:        snprintf(title, BUFSIZ, "%s(%s)", m->title, m->msec);
1.77      kristaps 1007:        titlen = term_strlen(p, title);
1.1       kristaps 1008:
                   1009:        p->offset = 0;
1.57      kristaps 1010:        p->rmargin = 2 * (titlen+1) + buflen < p->maxrmargin ?
1.77      kristaps 1011:            (p->maxrmargin -
                   1012:             term_strlen(p, buf) + term_len(p, 1)) / 2 :
1.57      kristaps 1013:            p->maxrmargin - buflen;
1.1       kristaps 1014:        p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
                   1015:
                   1016:        term_word(p, title);
                   1017:        term_flushln(p);
                   1018:
                   1019:        p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
                   1020:        p->offset = p->rmargin;
1.57      kristaps 1021:        p->rmargin = p->offset + buflen + titlen < p->maxrmargin ?
                   1022:            p->maxrmargin - titlen : p->maxrmargin;
1.1       kristaps 1023:
                   1024:        term_word(p, buf);
                   1025:        term_flushln(p);
                   1026:
                   1027:        p->flags &= ~TERMP_NOBREAK;
1.57      kristaps 1028:        if (p->rmargin + titlen <= p->maxrmargin) {
                   1029:                p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
                   1030:                p->offset = p->rmargin;
                   1031:                p->rmargin = p->maxrmargin;
                   1032:                term_word(p, title);
                   1033:                term_flushln(p);
                   1034:        }
1.1       kristaps 1035:
                   1036:        p->rmargin = p->maxrmargin;
                   1037:        p->offset = 0;
                   1038:        p->flags &= ~TERMP_NOSPACE;
1.61      kristaps 1039:
1.62      kristaps 1040:        /*
                   1041:         * Groff likes to have some leading spaces before content.  Well
                   1042:         * that's fine by me.
                   1043:         */
                   1044:
1.61      kristaps 1045:        term_vspace(p);
                   1046:        term_vspace(p);
                   1047:        term_vspace(p);
1.1       kristaps 1048: }

CVSweb