[BACK]Return to man_term.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/man_term.c, Revision 1.50

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