[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.43

1.43    ! schwarze    1: /*     $Id: man_term.c,v 1.42 2010/06/26 19:08:00 schwarze Exp $ */
1.1       kristaps    2: /*
1.43    ! schwarze    3:  * Copyright (c) 2008, 2009 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"
1.43    ! schwarze   27: #include "regs.h"
1.18      schwarze   28: #include "man.h"
1.1       kristaps   29: #include "term.h"
1.18      schwarze   30: #include "chars.h"
                     31: #include "main.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.11      schwarze   92: static int               pre_br(DECL_ARGS);
                     93: static int               pre_fi(DECL_ARGS);
1.14      schwarze   94: static int               pre_ign(DECL_ARGS);
1.11      schwarze   95: static int               pre_nf(DECL_ARGS);
                     96: static int               pre_sp(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.26      schwarze  106:        { pre_br, NULL, MAN_NOTEXT }, /* br */
                    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 */
                    130:        { pre_nf, NULL, 0 }, /* nf */
                    131:        { pre_fi, NULL, 0 }, /* fi */
                    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 */
                    139:        { pre_nf, NULL, 0 }, /* Vb */
                    140:        { pre_fi, NULL, 0 }, /* Ve */
1.36      schwarze  141:        { pre_ign, NULL, 0 }, /* AT */
1.1       kristaps  142: };
1.11      schwarze  143:
1.1       kristaps  144:
                    145:
1.16      schwarze  146: void
1.18      schwarze  147: terminal_man(void *arg, const struct man *man)
1.1       kristaps  148: {
1.18      schwarze  149:        struct termp            *p;
                    150:        const struct man_node   *n;
                    151:        const struct man_meta   *m;
                    152:        struct mtermp            mt;
                    153:
                    154:        p = (struct termp *)arg;
                    155:
1.39      schwarze  156:        p->overstep = 0;
1.32      schwarze  157:        p->maxrmargin = p->defrmargin;
1.42      schwarze  158:        p->tabwidth = term_len(p, 5);
1.27      schwarze  159:
1.18      schwarze  160:        if (NULL == p->symtab)
                    161:                switch (p->enc) {
                    162:                case (TERMENC_ASCII):
                    163:                        p->symtab = chars_init(CHARS_ASCII);
                    164:                        break;
                    165:                default:
                    166:                        abort();
                    167:                        /* NOTREACHED */
                    168:                }
                    169:
                    170:        n = man_node(man);
                    171:        m = man_meta(man);
1.1       kristaps  172:
1.41      schwarze  173:        term_begin(p, print_man_head, print_man_foot, m);
1.1       kristaps  174:        p->flags |= TERMP_NOSPACE;
1.11      schwarze  175:
                    176:        mt.fl = 0;
1.42      schwarze  177:        mt.lmargin = term_len(p, INDENT);
                    178:        mt.offset = term_len(p, INDENT);
1.11      schwarze  179:
1.18      schwarze  180:        if (n->child)
1.21      schwarze  181:                print_man_nodelist(p, &mt, n->child, m);
1.41      schwarze  182:
                    183:        term_end(p);
1.1       kristaps  184: }
                    185:
                    186:
1.42      schwarze  187: static size_t
                    188: a2height(const struct termp *p, const char *cp)
1.11      schwarze  189: {
1.18      schwarze  190:        struct roffsu    su;
1.11      schwarze  191:
1.42      schwarze  192:        if ( ! a2roffsu(cp, &su, SCALE_VS))
                    193:                SCALE_VS_INIT(&su, term_strlen(p, cp));
1.11      schwarze  194:
1.42      schwarze  195:        return(term_vspan(p, &su));
1.11      schwarze  196: }
                    197:
                    198:
                    199: static int
1.42      schwarze  200: a2width(const struct termp *p, const char *cp)
1.11      schwarze  201: {
1.18      schwarze  202:        struct roffsu    su;
1.11      schwarze  203:
1.42      schwarze  204:        if ( ! a2roffsu(cp, &su, SCALE_BU))
1.18      schwarze  205:                return(-1);
                    206:
1.42      schwarze  207:        return((int)term_hspan(p, &su));
1.18      schwarze  208: }
1.11      schwarze  209:
                    210:
1.18      schwarze  211: static void
                    212: print_bvspace(struct termp *p, const struct man_node *n)
                    213: {
                    214:        term_newln(p);
1.11      schwarze  215:
1.18      schwarze  216:        if (NULL == n->prev)
                    217:                return;
1.11      schwarze  218:
1.18      schwarze  219:        if (MAN_SS == n->prev->tok)
                    220:                return;
                    221:        if (MAN_SH == n->prev->tok)
                    222:                return;
1.11      schwarze  223:
1.18      schwarze  224:        term_vspace(p);
1.14      schwarze  225: }
                    226:
                    227:
                    228: /* ARGSUSED */
                    229: static int
                    230: pre_ign(DECL_ARGS)
                    231: {
                    232:
                    233:        return(0);
1.11      schwarze  234: }
                    235:
                    236:
1.1       kristaps  237: /* ARGSUSED */
                    238: static int
                    239: pre_I(DECL_ARGS)
                    240: {
                    241:
1.21      schwarze  242:        term_fontrepl(p, TERMFONT_UNDER);
1.1       kristaps  243:        return(1);
                    244: }
                    245:
                    246:
                    247: /* ARGSUSED */
1.11      schwarze  248: static int
                    249: pre_fi(DECL_ARGS)
                    250: {
                    251:
                    252:        mt->fl &= ~MANT_LITERAL;
                    253:        return(1);
                    254: }
                    255:
                    256:
                    257: /* ARGSUSED */
                    258: static int
                    259: pre_nf(DECL_ARGS)
                    260: {
                    261:
                    262:        mt->fl |= MANT_LITERAL;
1.27      schwarze  263:        return(MAN_Vb != n->tok);
1.11      schwarze  264: }
                    265:
                    266:
                    267: /* ARGSUSED */
                    268: static int
1.17      schwarze  269: pre_RB(DECL_ARGS)
1.1       kristaps  270: {
                    271:        const struct man_node *nn;
                    272:        int              i;
                    273:
                    274:        for (i = 0, nn = n->child; nn; nn = nn->next, i++) {
1.17      schwarze  275:                if (i % 2 && MAN_RB == n->tok)
1.21      schwarze  276:                        term_fontrepl(p, TERMFONT_BOLD);
1.17      schwarze  277:                else if ( ! (i % 2) && MAN_RB != n->tok)
1.21      schwarze  278:                        term_fontrepl(p, TERMFONT_BOLD);
                    279:                else
                    280:                        term_fontrepl(p, TERMFONT_NONE);
1.17      schwarze  281:
1.1       kristaps  282:                if (i > 0)
                    283:                        p->flags |= TERMP_NOSPACE;
1.17      schwarze  284:
1.19      schwarze  285:                print_man_node(p, mt, nn, m);
1.1       kristaps  286:        }
                    287:        return(0);
                    288: }
                    289:
                    290:
                    291: /* ARGSUSED */
                    292: static int
                    293: pre_RI(DECL_ARGS)
                    294: {
                    295:        const struct man_node *nn;
                    296:        int              i;
                    297:
                    298:        for (i = 0, nn = n->child; nn; nn = nn->next, i++) {
1.21      schwarze  299:                if (i % 2 && MAN_RI == n->tok)
                    300:                        term_fontrepl(p, TERMFONT_UNDER);
                    301:                else if ( ! (i % 2) && MAN_RI != n->tok)
                    302:                        term_fontrepl(p, TERMFONT_UNDER);
                    303:                else
                    304:                        term_fontrepl(p, TERMFONT_NONE);
                    305:
1.1       kristaps  306:                if (i > 0)
                    307:                        p->flags |= TERMP_NOSPACE;
1.21      schwarze  308:
1.19      schwarze  309:                print_man_node(p, mt, nn, m);
1.1       kristaps  310:        }
                    311:        return(0);
                    312: }
                    313:
                    314:
                    315: /* ARGSUSED */
                    316: static int
                    317: pre_BI(DECL_ARGS)
                    318: {
1.17      schwarze  319:        const struct man_node   *nn;
                    320:        int                      i;
1.1       kristaps  321:
                    322:        for (i = 0, nn = n->child; nn; nn = nn->next, i++) {
1.17      schwarze  323:                if (i % 2 && MAN_BI == n->tok)
1.21      schwarze  324:                        term_fontrepl(p, TERMFONT_UNDER);
1.17      schwarze  325:                else if (i % 2)
1.21      schwarze  326:                        term_fontrepl(p, TERMFONT_BOLD);
1.17      schwarze  327:                else if (MAN_BI == n->tok)
1.21      schwarze  328:                        term_fontrepl(p, TERMFONT_BOLD);
1.15      schwarze  329:                else
1.21      schwarze  330:                        term_fontrepl(p, TERMFONT_UNDER);
1.17      schwarze  331:
                    332:                if (i)
1.1       kristaps  333:                        p->flags |= TERMP_NOSPACE;
1.21      schwarze  334:
1.19      schwarze  335:                print_man_node(p, mt, nn, m);
1.1       kristaps  336:        }
                    337:        return(0);
                    338: }
                    339:
                    340:
                    341: /* ARGSUSED */
                    342: static int
                    343: pre_B(DECL_ARGS)
                    344: {
                    345:
1.21      schwarze  346:        term_fontrepl(p, TERMFONT_BOLD);
1.1       kristaps  347:        return(1);
                    348: }
                    349:
                    350:
                    351: /* ARGSUSED */
                    352: static int
1.11      schwarze  353: pre_sp(DECL_ARGS)
                    354: {
1.42      schwarze  355:        size_t           i, len;
1.11      schwarze  356:
1.42      schwarze  357:        len = n->child ?
                    358:                a2height(p, n->child->string) : term_len(p, 1);
1.11      schwarze  359:
                    360:        if (0 == len)
                    361:                term_newln(p);
1.29      schwarze  362:        for (i = 0; i <= len; i++)
1.11      schwarze  363:                term_vspace(p);
                    364:
                    365:        return(0);
                    366: }
                    367:
                    368:
                    369: /* ARGSUSED */
                    370: static int
1.8       schwarze  371: pre_br(DECL_ARGS)
                    372: {
                    373:
                    374:        term_newln(p);
                    375:        return(0);
                    376: }
                    377:
                    378:
                    379: /* ARGSUSED */
                    380: static int
1.11      schwarze  381: pre_HP(DECL_ARGS)
                    382: {
                    383:        size_t                   len;
                    384:        int                      ival;
                    385:        const struct man_node   *nn;
                    386:
                    387:        switch (n->type) {
                    388:        case (MAN_BLOCK):
1.18      schwarze  389:                print_bvspace(p, n);
1.11      schwarze  390:                return(1);
                    391:        case (MAN_BODY):
                    392:                p->flags |= TERMP_NOBREAK;
                    393:                p->flags |= TERMP_TWOSPACE;
                    394:                break;
                    395:        default:
                    396:                return(0);
                    397:        }
                    398:
1.13      schwarze  399:        len = mt->lmargin;
1.11      schwarze  400:        ival = -1;
                    401:
                    402:        /* Calculate offset. */
                    403:
                    404:        if (NULL != (nn = n->parent->head->child))
1.42      schwarze  405:                if ((ival = a2width(p, nn->string)) >= 0)
1.11      schwarze  406:                        len = (size_t)ival;
                    407:
                    408:        if (0 == len)
1.42      schwarze  409:                len = term_len(p, 1);
1.11      schwarze  410:
1.13      schwarze  411:        p->offset = mt->offset;
                    412:        p->rmargin = mt->offset + len;
1.11      schwarze  413:
                    414:        if (ival >= 0)
1.13      schwarze  415:                mt->lmargin = (size_t)ival;
1.11      schwarze  416:
                    417:        return(1);
                    418: }
                    419:
                    420:
                    421: /* ARGSUSED */
                    422: static void
                    423: post_HP(DECL_ARGS)
                    424: {
                    425:
                    426:        switch (n->type) {
                    427:        case (MAN_BLOCK):
                    428:                term_flushln(p);
                    429:                break;
                    430:        case (MAN_BODY):
                    431:                term_flushln(p);
                    432:                p->flags &= ~TERMP_NOBREAK;
                    433:                p->flags &= ~TERMP_TWOSPACE;
1.13      schwarze  434:                p->offset = mt->offset;
1.11      schwarze  435:                p->rmargin = p->maxrmargin;
                    436:                break;
                    437:        default:
                    438:                break;
                    439:        }
                    440: }
                    441:
                    442:
                    443: /* ARGSUSED */
                    444: static int
1.1       kristaps  445: pre_PP(DECL_ARGS)
                    446: {
                    447:
1.11      schwarze  448:        switch (n->type) {
                    449:        case (MAN_BLOCK):
1.42      schwarze  450:                mt->lmargin = term_len(p, INDENT);
1.18      schwarze  451:                print_bvspace(p, n);
1.11      schwarze  452:                break;
                    453:        default:
1.13      schwarze  454:                p->offset = mt->offset;
1.11      schwarze  455:                break;
                    456:        }
                    457:
                    458:        return(1);
1.1       kristaps  459: }
                    460:
                    461:
                    462: /* ARGSUSED */
                    463: static int
                    464: pre_IP(DECL_ARGS)
                    465: {
1.11      schwarze  466:        const struct man_node   *nn;
                    467:        size_t                   len;
                    468:        int                      ival;
                    469:
                    470:        switch (n->type) {
                    471:        case (MAN_BODY):
                    472:                p->flags |= TERMP_NOLPAD;
                    473:                p->flags |= TERMP_NOSPACE;
                    474:                break;
                    475:        case (MAN_HEAD):
                    476:                p->flags |= TERMP_NOBREAK;
                    477:                break;
                    478:        case (MAN_BLOCK):
1.18      schwarze  479:                print_bvspace(p, n);
1.11      schwarze  480:                /* FALLTHROUGH */
                    481:        default:
                    482:                return(1);
                    483:        }
                    484:
1.13      schwarze  485:        len = mt->lmargin;
1.11      schwarze  486:        ival = -1;
                    487:
                    488:        /* Calculate offset. */
                    489:
                    490:        if (NULL != (nn = n->parent->head->child))
                    491:                if (NULL != (nn = nn->next)) {
                    492:                        for ( ; nn->next; nn = nn->next)
                    493:                                /* Do nothing. */ ;
1.42      schwarze  494:                        if ((ival = a2width(p, nn->string)) >= 0)
1.11      schwarze  495:                                len = (size_t)ival;
                    496:                }
                    497:
                    498:        switch (n->type) {
                    499:        case (MAN_HEAD):
                    500:                /* Handle zero-width lengths. */
                    501:                if (0 == len)
1.42      schwarze  502:                        len = term_len(p, 1);
1.11      schwarze  503:
1.13      schwarze  504:                p->offset = mt->offset;
                    505:                p->rmargin = mt->offset + len;
1.11      schwarze  506:                if (ival < 0)
                    507:                        break;
                    508:
                    509:                /* Set the saved left-margin. */
1.13      schwarze  510:                mt->lmargin = (size_t)ival;
1.1       kristaps  511:
1.11      schwarze  512:                /* Don't print the length value. */
                    513:                for (nn = n->child; nn->next; nn = nn->next)
1.19      schwarze  514:                        print_man_node(p, mt, nn, m);
1.11      schwarze  515:                return(0);
                    516:        case (MAN_BODY):
1.13      schwarze  517:                p->offset = mt->offset + len;
1.11      schwarze  518:                p->rmargin = p->maxrmargin;
                    519:                break;
                    520:        default:
                    521:                break;
                    522:        }
1.1       kristaps  523:
1.11      schwarze  524:        return(1);
                    525: }
1.1       kristaps  526:
                    527:
1.11      schwarze  528: /* ARGSUSED */
                    529: static void
                    530: post_IP(DECL_ARGS)
                    531: {
1.4       schwarze  532:
1.11      schwarze  533:        switch (n->type) {
                    534:        case (MAN_HEAD):
                    535:                term_flushln(p);
                    536:                p->flags &= ~TERMP_NOBREAK;
                    537:                p->rmargin = p->maxrmargin;
                    538:                break;
                    539:        case (MAN_BODY):
                    540:                term_flushln(p);
                    541:                p->flags &= ~TERMP_NOLPAD;
                    542:                break;
                    543:        default:
                    544:                break;
                    545:        }
1.1       kristaps  546: }
                    547:
                    548:
                    549: /* ARGSUSED */
                    550: static int
                    551: pre_TP(DECL_ARGS)
                    552: {
1.11      schwarze  553:        const struct man_node   *nn;
                    554:        size_t                   len;
                    555:        int                      ival;
                    556:
                    557:        switch (n->type) {
                    558:        case (MAN_HEAD):
                    559:                p->flags |= TERMP_NOBREAK;
                    560:                p->flags |= TERMP_TWOSPACE;
                    561:                break;
                    562:        case (MAN_BODY):
                    563:                p->flags |= TERMP_NOLPAD;
                    564:                p->flags |= TERMP_NOSPACE;
                    565:                break;
                    566:        case (MAN_BLOCK):
1.18      schwarze  567:                print_bvspace(p, n);
1.11      schwarze  568:                /* FALLTHROUGH */
                    569:        default:
                    570:                return(1);
                    571:        }
                    572:
                    573:        len = (size_t)mt->lmargin;
                    574:        ival = -1;
                    575:
                    576:        /* Calculate offset. */
1.1       kristaps  577:
1.22      schwarze  578:        if (NULL != (nn = n->parent->head->child)) {
                    579:                while (nn && MAN_TEXT != nn->type)
                    580:                        nn = nn->next;
                    581:                if (nn && nn->next)
1.42      schwarze  582:                        if ((ival = a2width(p, nn->string)) >= 0)
1.11      schwarze  583:                                len = (size_t)ival;
1.22      schwarze  584:        }
1.8       schwarze  585:
1.11      schwarze  586:        switch (n->type) {
                    587:        case (MAN_HEAD):
                    588:                /* Handle zero-length properly. */
                    589:                if (0 == len)
1.42      schwarze  590:                        len = term_len(p, 1);
1.11      schwarze  591:
1.13      schwarze  592:                p->offset = mt->offset;
                    593:                p->rmargin = mt->offset + len;
1.11      schwarze  594:
                    595:                /* Don't print same-line elements. */
                    596:                for (nn = n->child; nn; nn = nn->next)
                    597:                        if (nn->line > n->line)
1.19      schwarze  598:                                print_man_node(p, mt, nn, m);
1.11      schwarze  599:
                    600:                if (ival >= 0)
1.13      schwarze  601:                        mt->lmargin = (size_t)ival;
1.11      schwarze  602:
                    603:                return(0);
                    604:        case (MAN_BODY):
1.13      schwarze  605:                p->offset = mt->offset + len;
1.11      schwarze  606:                p->rmargin = p->maxrmargin;
                    607:                break;
                    608:        default:
                    609:                break;
                    610:        }
1.1       kristaps  611:
1.11      schwarze  612:        return(1);
                    613: }
1.1       kristaps  614:
                    615:
1.11      schwarze  616: /* ARGSUSED */
                    617: static void
                    618: post_TP(DECL_ARGS)
                    619: {
1.1       kristaps  620:
1.11      schwarze  621:        switch (n->type) {
                    622:        case (MAN_HEAD):
                    623:                term_flushln(p);
                    624:                p->flags &= ~TERMP_NOBREAK;
                    625:                p->flags &= ~TERMP_TWOSPACE;
                    626:                p->rmargin = p->maxrmargin;
                    627:                break;
                    628:        case (MAN_BODY):
                    629:                term_flushln(p);
                    630:                p->flags &= ~TERMP_NOLPAD;
                    631:                break;
                    632:        default:
                    633:                break;
                    634:        }
1.1       kristaps  635: }
                    636:
                    637:
                    638: /* ARGSUSED */
                    639: static int
                    640: pre_SS(DECL_ARGS)
                    641: {
                    642:
1.11      schwarze  643:        switch (n->type) {
                    644:        case (MAN_BLOCK):
1.42      schwarze  645:                mt->lmargin = term_len(p, INDENT);
                    646:                mt->offset = term_len(p, INDENT);
1.11      schwarze  647:                /* If following a prior empty `SS', no vspace. */
                    648:                if (n->prev && MAN_SS == n->prev->tok)
                    649:                        if (NULL == n->prev->body->child)
                    650:                                break;
                    651:                if (NULL == n->prev)
                    652:                        break;
                    653:                term_vspace(p);
                    654:                break;
                    655:        case (MAN_HEAD):
1.21      schwarze  656:                term_fontrepl(p, TERMFONT_BOLD);
1.42      schwarze  657:                p->offset = term_len(p, HALFINDENT);
1.11      schwarze  658:                break;
                    659:        case (MAN_BODY):
1.13      schwarze  660:                p->offset = mt->offset;
1.11      schwarze  661:                break;
                    662:        default:
                    663:                break;
                    664:        }
                    665:
1.1       kristaps  666:        return(1);
                    667: }
                    668:
                    669:
                    670: /* ARGSUSED */
                    671: static void
                    672: post_SS(DECL_ARGS)
                    673: {
                    674:
1.11      schwarze  675:        switch (n->type) {
                    676:        case (MAN_HEAD):
                    677:                term_newln(p);
                    678:                break;
                    679:        case (MAN_BODY):
                    680:                term_newln(p);
                    681:                break;
                    682:        default:
                    683:                break;
                    684:        }
1.1       kristaps  685: }
                    686:
                    687:
                    688: /* ARGSUSED */
                    689: static int
                    690: pre_SH(DECL_ARGS)
                    691: {
                    692:
1.11      schwarze  693:        switch (n->type) {
                    694:        case (MAN_BLOCK):
1.42      schwarze  695:                mt->lmargin = term_len(p, INDENT);
                    696:                mt->offset = term_len(p, INDENT);
1.11      schwarze  697:                /* If following a prior empty `SH', no vspace. */
                    698:                if (n->prev && MAN_SH == n->prev->tok)
                    699:                        if (NULL == n->prev->body->child)
                    700:                                break;
1.29      schwarze  701:                /* If the first macro, no vspae. */
                    702:                if (NULL == n->prev)
                    703:                        break;
1.11      schwarze  704:                term_vspace(p);
                    705:                break;
                    706:        case (MAN_HEAD):
1.21      schwarze  707:                term_fontrepl(p, TERMFONT_BOLD);
1.11      schwarze  708:                p->offset = 0;
                    709:                break;
                    710:        case (MAN_BODY):
1.13      schwarze  711:                p->offset = mt->offset;
1.11      schwarze  712:                break;
                    713:        default:
                    714:                break;
                    715:        }
                    716:
1.1       kristaps  717:        return(1);
                    718: }
                    719:
                    720:
                    721: /* ARGSUSED */
                    722: static void
                    723: post_SH(DECL_ARGS)
                    724: {
                    725:
1.11      schwarze  726:        switch (n->type) {
                    727:        case (MAN_HEAD):
                    728:                term_newln(p);
                    729:                break;
                    730:        case (MAN_BODY):
                    731:                term_newln(p);
                    732:                break;
                    733:        default:
1.13      schwarze  734:                break;
                    735:        }
                    736: }
                    737:
                    738:
                    739: /* ARGSUSED */
                    740: static int
                    741: pre_RS(DECL_ARGS)
                    742: {
                    743:        const struct man_node   *nn;
                    744:        int                      ival;
                    745:
                    746:        switch (n->type) {
                    747:        case (MAN_BLOCK):
                    748:                term_newln(p);
                    749:                return(1);
                    750:        case (MAN_HEAD):
                    751:                return(0);
                    752:        default:
                    753:                break;
                    754:        }
                    755:
                    756:        if (NULL == (nn = n->parent->head->child)) {
1.42      schwarze  757:                mt->offset = mt->lmargin + term_len(p, INDENT);
1.13      schwarze  758:                p->offset = mt->offset;
                    759:                return(1);
                    760:        }
                    761:
1.42      schwarze  762:        if ((ival = a2width(p, nn->string)) < 0)
1.13      schwarze  763:                return(1);
                    764:
1.42      schwarze  765:        mt->offset = term_len(p, INDENT) + (size_t)ival;
1.13      schwarze  766:        p->offset = mt->offset;
                    767:
                    768:        return(1);
                    769: }
                    770:
                    771:
                    772: /* ARGSUSED */
                    773: static void
                    774: post_RS(DECL_ARGS)
                    775: {
                    776:
                    777:        switch (n->type) {
                    778:        case (MAN_BLOCK):
1.42      schwarze  779:                mt->offset = mt->lmargin = term_len(p, INDENT);
1.13      schwarze  780:                break;
1.27      schwarze  781:        case (MAN_HEAD):
                    782:                break;
1.13      schwarze  783:        default:
                    784:                term_newln(p);
1.42      schwarze  785:                p->offset = term_len(p, INDENT);
1.11      schwarze  786:                break;
                    787:        }
1.1       kristaps  788: }
                    789:
                    790:
                    791: static void
1.19      schwarze  792: print_man_node(DECL_ARGS)
1.1       kristaps  793: {
1.32      schwarze  794:        size_t           rm, rmax;
1.21      schwarze  795:        int              c;
1.1       kristaps  796:
                    797:        c = 1;
                    798:
                    799:        switch (n->type) {
                    800:        case(MAN_TEXT):
                    801:                if (0 == *n->string) {
                    802:                        term_vspace(p);
                    803:                        break;
                    804:                }
1.21      schwarze  805:
1.1       kristaps  806:                term_word(p, n->string);
1.21      schwarze  807:
1.11      schwarze  808:                /* FIXME: this means that macro lines are munged!  */
1.21      schwarze  809:
1.11      schwarze  810:                if (MANT_LITERAL & mt->fl) {
1.32      schwarze  811:                        rm = p->rmargin;
                    812:                        rmax = p->maxrmargin;
1.29      schwarze  813:                        p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
1.11      schwarze  814:                        p->flags |= TERMP_NOSPACE;
                    815:                        term_flushln(p);
1.32      schwarze  816:                        p->rmargin = rm;
                    817:                        p->maxrmargin = rmax;
1.11      schwarze  818:                }
1.1       kristaps  819:                break;
                    820:        default:
1.26      schwarze  821:                if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
                    822:                        term_fontrepl(p, TERMFONT_NONE);
1.11      schwarze  823:                if (termacts[n->tok].pre)
                    824:                        c = (*termacts[n->tok].pre)(p, mt, n, m);
1.1       kristaps  825:                break;
                    826:        }
                    827:
                    828:        if (c && n->child)
1.21      schwarze  829:                print_man_nodelist(p, mt, n->child, m);
1.1       kristaps  830:
1.21      schwarze  831:        if (MAN_TEXT != n->type) {
1.1       kristaps  832:                if (termacts[n->tok].post)
1.11      schwarze  833:                        (*termacts[n->tok].post)(p, mt, n, m);
1.26      schwarze  834:                if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
                    835:                        term_fontrepl(p, TERMFONT_NONE);
1.21      schwarze  836:        }
1.30      schwarze  837:
                    838:        if (MAN_EOS & n->flags)
                    839:                p->flags |= TERMP_SENTENCE;
1.1       kristaps  840: }
                    841:
                    842:
                    843: static void
1.21      schwarze  844: print_man_nodelist(DECL_ARGS)
1.1       kristaps  845: {
1.11      schwarze  846:
1.19      schwarze  847:        print_man_node(p, mt, n, m);
1.1       kristaps  848:        if ( ! n->next)
                    849:                return;
1.21      schwarze  850:        print_man_nodelist(p, mt, n->next, m);
1.1       kristaps  851: }
                    852:
                    853:
                    854: static void
1.41      schwarze  855: print_man_foot(struct termp *p, const void *arg)
1.1       kristaps  856: {
1.19      schwarze  857:        char            buf[DATESIZ];
1.41      schwarze  858:        const struct man_meta *meta;
                    859:
                    860:        meta = (const struct man_meta *)arg;
1.21      schwarze  861:
                    862:        term_fontrepl(p, TERMFONT_NONE);
1.1       kristaps  863:
1.40      schwarze  864:        if (meta->rawdate)
                    865:                strlcpy(buf, meta->rawdate, DATESIZ);
                    866:        else
                    867:                time2a(meta->date, buf, DATESIZ);
1.1       kristaps  868:
1.38      schwarze  869:        term_vspace(p);
                    870:        term_vspace(p);
1.1       kristaps  871:        term_vspace(p);
                    872:
                    873:        p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
1.42      schwarze  874:        p->rmargin = p->maxrmargin - term_strlen(p, buf);
1.1       kristaps  875:        p->offset = 0;
                    876:
                    877:        if (meta->source)
                    878:                term_word(p, meta->source);
                    879:        if (meta->source)
                    880:                term_word(p, "");
                    881:        term_flushln(p);
                    882:
                    883:        p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
                    884:        p->offset = p->rmargin;
                    885:        p->rmargin = p->maxrmargin;
                    886:        p->flags &= ~TERMP_NOBREAK;
                    887:
                    888:        term_word(p, buf);
                    889:        term_flushln(p);
                    890: }
                    891:
                    892:
                    893: static void
1.41      schwarze  894: print_man_head(struct termp *p, const void *arg)
1.1       kristaps  895: {
1.20      schwarze  896:        char            buf[BUFSIZ], title[BUFSIZ];
1.25      schwarze  897:        size_t          buflen, titlen;
1.41      schwarze  898:        const struct man_meta *m;
                    899:
                    900:        m = (const struct man_meta *)arg;
1.1       kristaps  901:
1.29      schwarze  902:        /*
                    903:         * Note that old groff would spit out some spaces before the
                    904:         * header.  We discontinue this strange behaviour, but at one
                    905:         * point we did so here.
                    906:         */
                    907:
1.1       kristaps  908:        p->rmargin = p->maxrmargin;
1.27      schwarze  909:
1.1       kristaps  910:        p->offset = 0;
1.20      schwarze  911:        buf[0] = title[0] = '\0';
1.1       kristaps  912:
1.20      schwarze  913:        if (m->vol)
                    914:                strlcpy(buf, m->vol, BUFSIZ);
1.42      schwarze  915:        buflen = term_strlen(p, buf);
1.1       kristaps  916:
1.31      schwarze  917:        snprintf(title, BUFSIZ, "%s(%s)", m->title, m->msec);
1.42      schwarze  918:        titlen = term_strlen(p, title);
1.1       kristaps  919:
                    920:        p->offset = 0;
1.25      schwarze  921:        p->rmargin = 2 * (titlen+1) + buflen < p->maxrmargin ?
1.42      schwarze  922:            (p->maxrmargin -
                    923:             term_strlen(p, buf) + term_len(p, 1)) / 2 :
1.25      schwarze  924:            p->maxrmargin - buflen;
1.1       kristaps  925:        p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
                    926:
                    927:        term_word(p, title);
                    928:        term_flushln(p);
                    929:
                    930:        p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
                    931:        p->offset = p->rmargin;
1.25      schwarze  932:        p->rmargin = p->offset + buflen + titlen < p->maxrmargin ?
                    933:            p->maxrmargin - titlen : p->maxrmargin;
1.1       kristaps  934:
                    935:        term_word(p, buf);
                    936:        term_flushln(p);
                    937:
                    938:        p->flags &= ~TERMP_NOBREAK;
1.25      schwarze  939:        if (p->rmargin + titlen <= p->maxrmargin) {
                    940:                p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
                    941:                p->offset = p->rmargin;
                    942:                p->rmargin = p->maxrmargin;
                    943:                term_word(p, title);
                    944:                term_flushln(p);
                    945:        }
1.1       kristaps  946:
                    947:        p->rmargin = p->maxrmargin;
                    948:        p->offset = 0;
                    949:        p->flags &= ~TERMP_NOSPACE;
1.29      schwarze  950:
                    951:        /*
                    952:         * Groff likes to have some leading spaces before content.  Well
                    953:         * that's fine by me.
                    954:         */
                    955:
                    956:        term_vspace(p);
                    957:        term_vspace(p);
                    958:        term_vspace(p);
1.1       kristaps  959: }