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

Annotation of mandoc/man_term.c, Revision 1.224

1.224   ! schwarze    1: /*     $Id: man_term.c,v 1.223 2018/12/30 00:49:55 schwarze Exp $ */
1.1       kristaps    2: /*
1.128     schwarze    3:  * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.210     schwarze    4:  * Copyright (c) 2010-2015, 2017, 2018 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
1.8       kristaps    7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    9:  *
1.171     schwarze   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.8       kristaps   11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.171     schwarze   12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.8       kristaps   13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   17:  */
1.55      kristaps   18: #include "config.h"
                     19:
1.28      kristaps   20: #include <sys/types.h>
                     21:
1.1       kristaps   22: #include <assert.h>
1.18      kristaps   23: #include <ctype.h>
1.164     schwarze   24: #include <limits.h>
1.1       kristaps   25: #include <stdio.h>
                     26: #include <stdlib.h>
                     27: #include <string.h>
                     28:
1.171     schwarze   29: #include "mandoc_aux.h"
                     30: #include "roff.h"
                     31: #include "man.h"
1.40      kristaps   32: #include "out.h"
1.1       kristaps   33: #include "term.h"
1.36      kristaps   34: #include "main.h"
1.1       kristaps   35:
1.116     schwarze   36: #define        MAXMARGINS        64 /* maximum number of indented scopes */
1.18      kristaps   37:
1.24      kristaps   38: struct mtermp {
                     39:        int               fl;
1.19      kristaps   40: #define        MANT_LITERAL     (1 << 0)
1.163     schwarze   41:        int               lmargin[MAXMARGINS]; /* margins (incl. vis. page) */
1.116     schwarze   42:        int               lmargincur; /* index of current margin */
                     43:        int               lmarginsz; /* actual number of nested margins */
                     44:        size_t            offset; /* default offset to visible page */
1.134     schwarze   45:        int               pardist; /* vert. space before par., unit: [v] */
1.24      kristaps   46: };
1.19      kristaps   47:
1.146     schwarze   48: #define        DECL_ARGS         struct termp *p, \
1.24      kristaps   49:                          struct mtermp *mt, \
1.172     schwarze   50:                          struct roff_node *n, \
1.173     schwarze   51:                          const struct roff_meta *meta
1.1       kristaps   52:
1.214     schwarze   53: struct man_term_act {
1.1       kristaps   54:        int             (*pre)(DECL_ARGS);
                     55:        void            (*post)(DECL_ARGS);
1.56      kristaps   56:        int               flags;
                     57: #define        MAN_NOTEXT       (1 << 0) /* Never has text children. */
1.1       kristaps   58: };
                     59:
1.52      kristaps   60: static void              print_man_nodelist(DECL_ARGS);
1.45      kristaps   61: static void              print_man_node(DECL_ARGS);
1.173     schwarze   62: static void              print_man_head(struct termp *,
                     63:                                const struct roff_meta *);
                     64: static void              print_man_foot(struct termp *,
                     65:                                const struct roff_meta *);
1.146     schwarze   66: static void              print_bvspace(struct termp *,
1.172     schwarze   67:                                const struct roff_node *, int);
1.39      kristaps   68:
1.1       kristaps   69: static int               pre_B(DECL_ARGS);
1.199     schwarze   70: static int               pre_DT(DECL_ARGS);
1.19      kristaps   71: static int               pre_HP(DECL_ARGS);
1.1       kristaps   72: static int               pre_I(DECL_ARGS);
1.4       kristaps   73: static int               pre_IP(DECL_ARGS);
1.127     kristaps   74: static int               pre_OP(DECL_ARGS);
1.134     schwarze   75: static int               pre_PD(DECL_ARGS);
1.1       kristaps   76: static int               pre_PP(DECL_ARGS);
1.26      kristaps   77: static int               pre_RS(DECL_ARGS);
1.1       kristaps   78: static int               pre_SH(DECL_ARGS);
                     79: static int               pre_SS(DECL_ARGS);
1.215     schwarze   80: static int               pre_SY(DECL_ARGS);
1.1       kristaps   81: static int               pre_TP(DECL_ARGS);
1.137     schwarze   82: static int               pre_UR(DECL_ARGS);
1.221     schwarze   83: static int               pre_abort(DECL_ARGS);
1.127     kristaps   84: static int               pre_alternate(DECL_ARGS);
1.29      kristaps   85: static int               pre_ign(DECL_ARGS);
1.83      kristaps   86: static int               pre_in(DECL_ARGS);
1.84      kristaps   87: static int               pre_literal(DECL_ARGS);
1.1       kristaps   88:
1.22      kristaps   89: static void              post_IP(DECL_ARGS);
1.20      kristaps   90: static void              post_HP(DECL_ARGS);
1.26      kristaps   91: static void              post_RS(DECL_ARGS);
1.1       kristaps   92: static void              post_SH(DECL_ARGS);
                     93: static void              post_SS(DECL_ARGS);
1.215     schwarze   94: static void              post_SY(DECL_ARGS);
1.21      kristaps   95: static void              post_TP(DECL_ARGS);
1.137     schwarze   96: static void              post_UR(DECL_ARGS);
1.1       kristaps   97:
1.214     schwarze   98: static const struct man_term_act man_term_acts[MAN_MAX - MAN_TH] = {
1.56      kristaps   99:        { NULL, NULL, 0 }, /* TH */
                    100:        { pre_SH, post_SH, 0 }, /* SH */
                    101:        { pre_SS, post_SS, 0 }, /* SS */
                    102:        { pre_TP, post_TP, 0 }, /* TP */
1.213     schwarze  103:        { pre_TP, post_TP, 0 }, /* TQ */
1.221     schwarze  104:        { pre_abort, NULL, 0 }, /* LP */
1.56      kristaps  105:        { pre_PP, NULL, 0 }, /* PP */
1.221     schwarze  106:        { pre_abort, NULL, 0 }, /* P */
1.56      kristaps  107:        { pre_IP, post_IP, 0 }, /* IP */
1.146     schwarze  108:        { pre_HP, post_HP, 0 }, /* HP */
1.56      kristaps  109:        { NULL, NULL, 0 }, /* SM */
                    110:        { pre_B, NULL, 0 }, /* SB */
1.88      kristaps  111:        { pre_alternate, NULL, 0 }, /* BI */
                    112:        { pre_alternate, NULL, 0 }, /* IB */
                    113:        { pre_alternate, NULL, 0 }, /* BR */
                    114:        { pre_alternate, NULL, 0 }, /* RB */
1.56      kristaps  115:        { NULL, NULL, 0 }, /* R */
                    116:        { pre_B, NULL, 0 }, /* B */
                    117:        { pre_I, NULL, 0 }, /* I */
1.88      kristaps  118:        { pre_alternate, NULL, 0 }, /* IR */
                    119:        { pre_alternate, NULL, 0 }, /* RI */
1.56      kristaps  120:        { NULL, NULL, 0 }, /* RE */
                    121:        { pre_RS, post_RS, 0 }, /* RS */
1.199     schwarze  122:        { pre_DT, NULL, 0 }, /* DT */
1.158     schwarze  123:        { pre_ign, NULL, MAN_NOTEXT }, /* UC */
1.134     schwarze  124:        { pre_PD, NULL, MAN_NOTEXT }, /* PD */
1.70      joerg     125:        { pre_ign, NULL, 0 }, /* AT */
1.83      kristaps  126:        { pre_in, NULL, MAN_NOTEXT }, /* in */
1.215     schwarze  127:        { pre_SY, post_SY, 0 }, /* SY */
                    128:        { NULL, NULL, 0 }, /* YS */
1.127     kristaps  129:        { pre_OP, NULL, 0 }, /* OP */
1.129     schwarze  130:        { pre_literal, NULL, 0 }, /* EX */
                    131:        { pre_literal, NULL, 0 }, /* EE */
1.137     schwarze  132:        { pre_UR, post_UR, 0 }, /* UR */
                    133:        { NULL, NULL, 0 }, /* UE */
1.208     schwarze  134:        { pre_UR, post_UR, 0 }, /* MT */
                    135:        { NULL, NULL, 0 }, /* ME */
1.1       kristaps  136: };
1.214     schwarze  137: static const struct man_term_act *man_term_act(enum roff_tok);
1.1       kristaps  138:
                    139:
1.214     schwarze  140: static const struct man_term_act *
                    141: man_term_act(enum roff_tok tok)
                    142: {
                    143:        assert(tok >= MAN_TH && tok <= MAN_MAX);
                    144:        return man_term_acts + (tok - MAN_TH);
                    145: }
                    146:
1.31      kristaps  147: void
1.223     schwarze  148: terminal_man(void *arg, const struct roff_meta *man)
1.1       kristaps  149: {
1.36      kristaps  150:        struct termp            *p;
1.172     schwarze  151:        struct roff_node        *n;
1.36      kristaps  152:        struct mtermp            mt;
1.189     schwarze  153:        size_t                   save_defindent;
1.36      kristaps  154:
                    155:        p = (struct termp *)arg;
1.206     schwarze  156:        save_defindent = p->defindent;
                    157:        if (p->synopsisonly == 0 && p->defindent == 0)
                    158:                p->defindent = 7;
1.203     schwarze  159:        p->tcol->rmargin = p->maxrmargin = p->defrmargin;
1.198     schwarze  160:        term_tab_set(p, NULL);
                    161:        term_tab_set(p, "T");
                    162:        term_tab_set(p, ".5i");
1.36      kristaps  163:
1.116     schwarze  164:        memset(&mt, 0, sizeof(struct mtermp));
1.122     schwarze  165:        mt.lmargin[mt.lmargincur] = term_len(p, p->defindent);
                    166:        mt.offset = term_len(p, p->defindent);
1.134     schwarze  167:        mt.pardist = 1;
1.24      kristaps  168:
1.181     schwarze  169:        n = man->first->child;
1.151     schwarze  170:        if (p->synopsisonly) {
                    171:                while (n != NULL) {
                    172:                        if (n->tok == MAN_SH &&
1.171     schwarze  173:                            n->child->child->type == ROFFT_TEXT &&
1.151     schwarze  174:                            !strcmp(n->child->child->string, "SYNOPSIS")) {
                    175:                                if (n->child->next->child != NULL)
                    176:                                        print_man_nodelist(p, &mt,
1.223     schwarze  177:                                            n->child->next->child, man);
1.151     schwarze  178:                                term_newln(p);
                    179:                                break;
                    180:                        }
                    181:                        n = n->next;
                    182:                }
                    183:        } else {
1.223     schwarze  184:                term_begin(p, print_man_head, print_man_foot, man);
1.151     schwarze  185:                p->flags |= TERMP_NOSPACE;
                    186:                if (n != NULL)
1.223     schwarze  187:                        print_man_nodelist(p, &mt, n, man);
1.151     schwarze  188:                term_end(p);
                    189:        }
1.206     schwarze  190:        p->defindent = save_defindent;
1.1       kristaps  191: }
                    192:
1.111     kristaps  193: /*
                    194:  * Printing leading vertical space before a block.
                    195:  * This is used for the paragraph macros.
                    196:  * The rules are pretty simple, since there's very little nesting going
                    197:  * on here.  Basically, if we're the first within another block (SS/SH),
                    198:  * then don't emit vertical space.  If we are (RS), then do.  If not the
                    199:  * first, print it.
                    200:  */
1.39      kristaps  201: static void
1.172     schwarze  202: print_bvspace(struct termp *p, const struct roff_node *n, int pardist)
1.18      kristaps  203: {
1.134     schwarze  204:        int      i;
1.111     kristaps  205:
1.39      kristaps  206:        term_newln(p);
1.101     schwarze  207:
1.111     kristaps  208:        if (n->body && n->body->child)
1.171     schwarze  209:                if (n->body->child->type == ROFFT_TBL)
1.111     kristaps  210:                        return;
                    211:
1.171     schwarze  212:        if (n->parent->type == ROFFT_ROOT || n->parent->tok != MAN_RS)
1.111     kristaps  213:                if (NULL == n->prev)
                    214:                        return;
1.18      kristaps  215:
1.134     schwarze  216:        for (i = 0; i < pardist; i++)
                    217:                term_vspace(p);
1.18      kristaps  218: }
                    219:
1.221     schwarze  220:
                    221: static int
                    222: pre_abort(DECL_ARGS)
                    223: {
                    224:        abort();
                    225: }
1.146     schwarze  226:
1.1       kristaps  227: static int
1.29      kristaps  228: pre_ign(DECL_ARGS)
                    229: {
                    230:
1.185     schwarze  231:        return 0;
1.29      kristaps  232: }
                    233:
                    234: static int
1.1       kristaps  235: pre_I(DECL_ARGS)
                    236: {
                    237:
1.52      kristaps  238:        term_fontrepl(p, TERMFONT_UNDER);
1.185     schwarze  239:        return 1;
1.19      kristaps  240: }
                    241:
1.3       kristaps  242: static int
1.84      kristaps  243: pre_literal(DECL_ARGS)
1.19      kristaps  244: {
                    245:
1.81      kristaps  246:        term_newln(p);
1.88      kristaps  247:
1.224   ! schwarze  248:        if (n->tok == MAN_EX)
1.84      kristaps  249:                mt->fl |= MANT_LITERAL;
1.88      kristaps  250:        else
1.84      kristaps  251:                mt->fl &= ~MANT_LITERAL;
                    252:
1.117     schwarze  253:        /*
                    254:         * Unlike .IP and .TP, .HP does not have a HEAD.
                    255:         * So in case a second call to term_flushln() is needed,
                    256:         * indentation has to be set up explicitly.
                    257:         */
1.203     schwarze  258:        if (n->parent->tok == MAN_HP && p->tcol->rmargin < p->maxrmargin) {
                    259:                p->tcol->offset = p->tcol->rmargin;
                    260:                p->tcol->rmargin = p->maxrmargin;
1.139     schwarze  261:                p->trailspace = 0;
1.145     schwarze  262:                p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
1.117     schwarze  263:                p->flags |= TERMP_NOSPACE;
                    264:        }
                    265:
1.185     schwarze  266:        return 0;
1.19      kristaps  267: }
                    268:
                    269: static int
1.134     schwarze  270: pre_PD(DECL_ARGS)
                    271: {
1.160     schwarze  272:        struct roffsu    su;
1.134     schwarze  273:
                    274:        n = n->child;
1.160     schwarze  275:        if (n == NULL) {
1.134     schwarze  276:                mt->pardist = 1;
1.185     schwarze  277:                return 0;
1.134     schwarze  278:        }
1.171     schwarze  279:        assert(n->type == ROFFT_TEXT);
1.204     schwarze  280:        if (a2roffsu(n->string, &su, SCALE_VS) != NULL)
1.160     schwarze  281:                mt->pardist = term_vspan(p, &su);
1.185     schwarze  282:        return 0;
1.134     schwarze  283: }
                    284:
                    285: static int
1.88      kristaps  286: pre_alternate(DECL_ARGS)
1.3       kristaps  287: {
1.88      kristaps  288:        enum termfont            font[2];
1.172     schwarze  289:        struct roff_node        *nn;
1.88      kristaps  290:        int                      savelit, i;
1.3       kristaps  291:
1.88      kristaps  292:        switch (n->tok) {
1.146     schwarze  293:        case MAN_RB:
1.88      kristaps  294:                font[0] = TERMFONT_NONE;
                    295:                font[1] = TERMFONT_BOLD;
                    296:                break;
1.146     schwarze  297:        case MAN_RI:
1.88      kristaps  298:                font[0] = TERMFONT_NONE;
                    299:                font[1] = TERMFONT_UNDER;
                    300:                break;
1.146     schwarze  301:        case MAN_BR:
1.88      kristaps  302:                font[0] = TERMFONT_BOLD;
                    303:                font[1] = TERMFONT_NONE;
                    304:                break;
1.146     schwarze  305:        case MAN_BI:
1.88      kristaps  306:                font[0] = TERMFONT_BOLD;
                    307:                font[1] = TERMFONT_UNDER;
                    308:                break;
1.146     schwarze  309:        case MAN_IR:
1.88      kristaps  310:                font[0] = TERMFONT_UNDER;
                    311:                font[1] = TERMFONT_NONE;
                    312:                break;
1.146     schwarze  313:        case MAN_IB:
1.88      kristaps  314:                font[0] = TERMFONT_UNDER;
                    315:                font[1] = TERMFONT_BOLD;
                    316:                break;
                    317:        default:
                    318:                abort();
                    319:        }
1.35      kristaps  320:
1.88      kristaps  321:        savelit = MANT_LITERAL & mt->fl;
                    322:        mt->fl &= ~MANT_LITERAL;
1.35      kristaps  323:
1.88      kristaps  324:        for (i = 0, nn = n->child; nn; nn = nn->next, i = 1 - i) {
                    325:                term_fontrepl(p, font[i]);
                    326:                if (savelit && NULL == nn->next)
                    327:                        mt->fl |= MANT_LITERAL;
1.179     schwarze  328:                assert(nn->type == ROFFT_TEXT);
                    329:                term_word(p, nn->string);
1.188     schwarze  330:                if (nn->flags & NODE_EOS)
1.179     schwarze  331:                        p->flags |= TERMP_SENTENCE;
1.88      kristaps  332:                if (nn->next)
1.4       kristaps  333:                        p->flags |= TERMP_NOSPACE;
1.3       kristaps  334:        }
                    335:
1.185     schwarze  336:        return 0;
1.3       kristaps  337: }
                    338:
1.1       kristaps  339: static int
                    340: pre_B(DECL_ARGS)
                    341: {
                    342:
1.52      kristaps  343:        term_fontrepl(p, TERMFONT_BOLD);
1.185     schwarze  344:        return 1;
1.127     kristaps  345: }
                    346:
                    347: static int
                    348: pre_OP(DECL_ARGS)
                    349: {
                    350:
                    351:        term_word(p, "[");
1.216     schwarze  352:        p->flags |= TERMP_KEEP | TERMP_NOSPACE;
1.127     kristaps  353:
                    354:        if (NULL != (n = n->child)) {
                    355:                term_fontrepl(p, TERMFONT_BOLD);
                    356:                term_word(p, n->string);
                    357:        }
                    358:        if (NULL != n && NULL != n->next) {
                    359:                term_fontrepl(p, TERMFONT_UNDER);
                    360:                term_word(p, n->next->string);
                    361:        }
                    362:
                    363:        term_fontrepl(p, TERMFONT_NONE);
1.216     schwarze  364:        p->flags &= ~TERMP_KEEP;
1.127     kristaps  365:        p->flags |= TERMP_NOSPACE;
                    366:        term_word(p, "]");
1.185     schwarze  367:        return 0;
1.83      kristaps  368: }
                    369:
                    370: static int
                    371: pre_in(DECL_ARGS)
                    372: {
1.163     schwarze  373:        struct roffsu    su;
                    374:        const char      *cp;
1.83      kristaps  375:        size_t           v;
1.163     schwarze  376:        int              less;
1.83      kristaps  377:
                    378:        term_newln(p);
                    379:
1.203     schwarze  380:        if (n->child == NULL) {
                    381:                p->tcol->offset = mt->offset;
1.185     schwarze  382:                return 0;
1.83      kristaps  383:        }
                    384:
                    385:        cp = n->child->string;
                    386:        less = 0;
                    387:
                    388:        if ('-' == *cp)
                    389:                less = -1;
                    390:        else if ('+' == *cp)
                    391:                less = 1;
                    392:        else
                    393:                cp--;
                    394:
1.204     schwarze  395:        if (a2roffsu(++cp, &su, SCALE_EN) == NULL)
1.185     schwarze  396:                return 0;
1.83      kristaps  397:
1.205     schwarze  398:        v = term_hen(p, &su);
1.83      kristaps  399:
                    400:        if (less < 0)
1.203     schwarze  401:                p->tcol->offset -= p->tcol->offset > v ? v : p->tcol->offset;
1.83      kristaps  402:        else if (less > 0)
1.203     schwarze  403:                p->tcol->offset += v;
1.146     schwarze  404:        else
1.203     schwarze  405:                p->tcol->offset = v;
                    406:        if (p->tcol->offset > SHRT_MAX)
                    407:                p->tcol->offset = term_len(p, p->defindent);
1.19      kristaps  408:
1.199     schwarze  409:        return 0;
                    410: }
                    411:
                    412: static int
                    413: pre_DT(DECL_ARGS)
                    414: {
                    415:        term_tab_set(p, NULL);
                    416:        term_tab_set(p, "T");
                    417:        term_tab_set(p, ".5i");
1.185     schwarze  418:        return 0;
1.15      kristaps  419: }
                    420:
                    421: static int
1.19      kristaps  422: pre_HP(DECL_ARGS)
                    423: {
1.163     schwarze  424:        struct roffsu            su;
1.172     schwarze  425:        const struct roff_node  *nn;
1.163     schwarze  426:        int                      len;
1.19      kristaps  427:
1.20      kristaps  428:        switch (n->type) {
1.171     schwarze  429:        case ROFFT_BLOCK:
1.134     schwarze  430:                print_bvspace(p, n, mt->pardist);
1.185     schwarze  431:                return 1;
1.171     schwarze  432:        case ROFFT_BODY:
1.20      kristaps  433:                break;
                    434:        default:
1.185     schwarze  435:                return 0;
1.20      kristaps  436:        }
                    437:
1.130     schwarze  438:        if ( ! (MANT_LITERAL & mt->fl)) {
1.145     schwarze  439:                p->flags |= TERMP_NOBREAK | TERMP_BRIND;
1.139     schwarze  440:                p->trailspace = 2;
1.130     schwarze  441:        }
                    442:
1.24      kristaps  443:        /* Calculate offset. */
                    444:
1.163     schwarze  445:        if ((nn = n->parent->head->child) != NULL &&
1.204     schwarze  446:            a2roffsu(nn->string, &su, SCALE_EN) != NULL) {
1.205     schwarze  447:                len = term_hen(p, &su);
1.164     schwarze  448:                if (len < 0 && (size_t)(-len) > mt->offset)
                    449:                        len = -mt->offset;
                    450:                else if (len > SHRT_MAX)
                    451:                        len = term_len(p, p->defindent);
1.163     schwarze  452:                mt->lmargin[mt->lmargincur] = len;
                    453:        } else
                    454:                len = mt->lmargin[mt->lmargincur];
1.24      kristaps  455:
1.203     schwarze  456:        p->tcol->offset = mt->offset;
                    457:        p->tcol->rmargin = mt->offset + len;
1.185     schwarze  458:        return 1;
1.19      kristaps  459: }
                    460:
1.20      kristaps  461: static void
                    462: post_HP(DECL_ARGS)
                    463: {
                    464:
                    465:        switch (n->type) {
1.171     schwarze  466:        case ROFFT_BODY:
1.136     schwarze  467:                term_newln(p);
1.174     schwarze  468:
                    469:                /*
                    470:                 * Compatibility with a groff bug.
                    471:                 * The .HP macro uses the undocumented .tag request
                    472:                 * which causes a line break and cancels no-space
                    473:                 * mode even if there isn't any output.
                    474:                 */
                    475:
                    476:                if (n->child == NULL)
                    477:                        term_vspace(p);
                    478:
1.145     schwarze  479:                p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
1.139     schwarze  480:                p->trailspace = 0;
1.203     schwarze  481:                p->tcol->offset = mt->offset;
                    482:                p->tcol->rmargin = p->maxrmargin;
1.20      kristaps  483:                break;
                    484:        default:
                    485:                break;
                    486:        }
                    487: }
                    488:
1.19      kristaps  489: static int
1.1       kristaps  490: pre_PP(DECL_ARGS)
                    491: {
                    492:
1.19      kristaps  493:        switch (n->type) {
1.171     schwarze  494:        case ROFFT_BLOCK:
1.122     schwarze  495:                mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
1.134     schwarze  496:                print_bvspace(p, n, mt->pardist);
1.19      kristaps  497:                break;
                    498:        default:
1.203     schwarze  499:                p->tcol->offset = mt->offset;
1.19      kristaps  500:                break;
                    501:        }
                    502:
1.185     schwarze  503:        return n->type != ROFFT_HEAD;
1.1       kristaps  504: }
                    505:
                    506: static int
1.4       kristaps  507: pre_IP(DECL_ARGS)
                    508: {
1.163     schwarze  509:        struct roffsu            su;
1.172     schwarze  510:        const struct roff_node  *nn;
1.163     schwarze  511:        int                      len, savelit;
1.18      kristaps  512:
1.22      kristaps  513:        switch (n->type) {
1.171     schwarze  514:        case ROFFT_BODY:
1.22      kristaps  515:                p->flags |= TERMP_NOSPACE;
                    516:                break;
1.171     schwarze  517:        case ROFFT_HEAD:
1.22      kristaps  518:                p->flags |= TERMP_NOBREAK;
1.139     schwarze  519:                p->trailspace = 1;
1.22      kristaps  520:                break;
1.171     schwarze  521:        case ROFFT_BLOCK:
1.134     schwarze  522:                print_bvspace(p, n, mt->pardist);
1.23      kristaps  523:                /* FALLTHROUGH */
1.22      kristaps  524:        default:
1.185     schwarze  525:                return 1;
1.22      kristaps  526:        }
1.18      kristaps  527:
1.94      schwarze  528:        /* Calculate the offset from the optional second argument. */
1.163     schwarze  529:        if ((nn = n->parent->head->child) != NULL &&
                    530:            (nn = nn->next) != NULL &&
1.204     schwarze  531:            a2roffsu(nn->string, &su, SCALE_EN) != NULL) {
1.205     schwarze  532:                len = term_hen(p, &su);
1.163     schwarze  533:                if (len < 0 && (size_t)(-len) > mt->offset)
                    534:                        len = -mt->offset;
1.164     schwarze  535:                else if (len > SHRT_MAX)
                    536:                        len = term_len(p, p->defindent);
                    537:                mt->lmargin[mt->lmargincur] = len;
1.163     schwarze  538:        } else
                    539:                len = mt->lmargin[mt->lmargincur];
1.4       kristaps  540:
1.22      kristaps  541:        switch (n->type) {
1.171     schwarze  542:        case ROFFT_HEAD:
1.203     schwarze  543:                p->tcol->offset = mt->offset;
                    544:                p->tcol->rmargin = mt->offset + len;
1.18      kristaps  545:
1.94      schwarze  546:                savelit = MANT_LITERAL & mt->fl;
                    547:                mt->fl &= ~MANT_LITERAL;
                    548:
                    549:                if (n->child)
1.135     schwarze  550:                        print_man_node(p, mt, n->child, meta);
1.94      schwarze  551:
                    552:                if (savelit)
                    553:                        mt->fl |= MANT_LITERAL;
                    554:
1.185     schwarze  555:                return 0;
1.171     schwarze  556:        case ROFFT_BODY:
1.203     schwarze  557:                p->tcol->offset = mt->offset + len;
                    558:                p->tcol->rmargin = p->maxrmargin;
1.23      kristaps  559:                break;
1.22      kristaps  560:        default:
                    561:                break;
1.18      kristaps  562:        }
                    563:
1.185     schwarze  564:        return 1;
1.22      kristaps  565: }
1.18      kristaps  566:
1.22      kristaps  567: static void
                    568: post_IP(DECL_ARGS)
                    569: {
1.4       kristaps  570:
1.22      kristaps  571:        switch (n->type) {
1.171     schwarze  572:        case ROFFT_HEAD:
1.22      kristaps  573:                term_flushln(p);
                    574:                p->flags &= ~TERMP_NOBREAK;
1.139     schwarze  575:                p->trailspace = 0;
1.203     schwarze  576:                p->tcol->rmargin = p->maxrmargin;
1.22      kristaps  577:                break;
1.171     schwarze  578:        case ROFFT_BODY:
1.94      schwarze  579:                term_newln(p);
1.203     schwarze  580:                p->tcol->offset = mt->offset;
1.22      kristaps  581:                break;
                    582:        default:
                    583:                break;
                    584:        }
1.4       kristaps  585: }
                    586:
                    587: static int
1.1       kristaps  588: pre_TP(DECL_ARGS)
                    589: {
1.163     schwarze  590:        struct roffsu            su;
1.172     schwarze  591:        struct roff_node        *nn;
1.163     schwarze  592:        int                      len, savelit;
1.1       kristaps  593:
1.21      kristaps  594:        switch (n->type) {
1.171     schwarze  595:        case ROFFT_HEAD:
1.184     schwarze  596:                p->flags |= TERMP_NOBREAK | TERMP_BRTRSP;
1.139     schwarze  597:                p->trailspace = 1;
1.21      kristaps  598:                break;
1.171     schwarze  599:        case ROFFT_BODY:
1.21      kristaps  600:                p->flags |= TERMP_NOSPACE;
1.23      kristaps  601:                break;
1.171     schwarze  602:        case ROFFT_BLOCK:
1.213     schwarze  603:                if (n->tok == MAN_TP)
                    604:                        print_bvspace(p, n, mt->pardist);
1.23      kristaps  605:                /* FALLTHROUGH */
                    606:        default:
1.185     schwarze  607:                return 1;
1.23      kristaps  608:        }
                    609:
                    610:        /* Calculate offset. */
                    611:
1.163     schwarze  612:        if ((nn = n->parent->head->child) != NULL &&
1.188     schwarze  613:            nn->string != NULL && ! (NODE_LINE & nn->flags) &&
1.204     schwarze  614:            a2roffsu(nn->string, &su, SCALE_EN) != NULL) {
1.205     schwarze  615:                len = term_hen(p, &su);
1.163     schwarze  616:                if (len < 0 && (size_t)(-len) > mt->offset)
                    617:                        len = -mt->offset;
1.164     schwarze  618:                else if (len > SHRT_MAX)
                    619:                        len = term_len(p, p->defindent);
                    620:                mt->lmargin[mt->lmargincur] = len;
1.163     schwarze  621:        } else
                    622:                len = mt->lmargin[mt->lmargincur];
1.23      kristaps  623:
                    624:        switch (n->type) {
1.171     schwarze  625:        case ROFFT_HEAD:
1.203     schwarze  626:                p->tcol->offset = mt->offset;
                    627:                p->tcol->rmargin = mt->offset + len;
1.23      kristaps  628:
1.94      schwarze  629:                savelit = MANT_LITERAL & mt->fl;
                    630:                mt->fl &= ~MANT_LITERAL;
                    631:
1.23      kristaps  632:                /* Don't print same-line elements. */
1.141     schwarze  633:                nn = n->child;
1.188     schwarze  634:                while (NULL != nn && 0 == (NODE_LINE & nn->flags))
1.141     schwarze  635:                        nn = nn->next;
                    636:
                    637:                while (NULL != nn) {
                    638:                        print_man_node(p, mt, nn, meta);
                    639:                        nn = nn->next;
                    640:                }
1.24      kristaps  641:
1.94      schwarze  642:                if (savelit)
                    643:                        mt->fl |= MANT_LITERAL;
1.185     schwarze  644:                return 0;
1.171     schwarze  645:        case ROFFT_BODY:
1.203     schwarze  646:                p->tcol->offset = mt->offset + len;
                    647:                p->tcol->rmargin = p->maxrmargin;
1.139     schwarze  648:                p->trailspace = 0;
1.184     schwarze  649:                p->flags &= ~(TERMP_NOBREAK | TERMP_BRTRSP);
1.21      kristaps  650:                break;
                    651:        default:
                    652:                break;
                    653:        }
1.16      kristaps  654:
1.185     schwarze  655:        return 1;
1.21      kristaps  656: }
1.1       kristaps  657:
1.21      kristaps  658: static void
                    659: post_TP(DECL_ARGS)
                    660: {
1.1       kristaps  661:
1.21      kristaps  662:        switch (n->type) {
1.171     schwarze  663:        case ROFFT_HEAD:
1.21      kristaps  664:                term_flushln(p);
                    665:                break;
1.171     schwarze  666:        case ROFFT_BODY:
1.94      schwarze  667:                term_newln(p);
1.203     schwarze  668:                p->tcol->offset = mt->offset;
1.21      kristaps  669:                break;
                    670:        default:
                    671:                break;
                    672:        }
1.1       kristaps  673: }
                    674:
                    675: static int
                    676: pre_SS(DECL_ARGS)
                    677: {
1.134     schwarze  678:        int      i;
1.1       kristaps  679:
1.19      kristaps  680:        switch (n->type) {
1.171     schwarze  681:        case ROFFT_BLOCK:
1.113     kristaps  682:                mt->fl &= ~MANT_LITERAL;
1.122     schwarze  683:                mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
                    684:                mt->offset = term_len(p, p->defindent);
1.158     schwarze  685:
                    686:                /*
                    687:                 * No vertical space before the first subsection
                    688:                 * and after an empty subsection.
                    689:                 */
                    690:
                    691:                do {
                    692:                        n = n->prev;
1.209     schwarze  693:                } while (n != NULL && n->tok >= MAN_TH &&
1.214     schwarze  694:                    man_term_act(n->tok)->flags & MAN_NOTEXT);
1.210     schwarze  695:                if (n == NULL || n->type == ROFFT_COMMENT ||
                    696:                    (n->tok == MAN_SS && n->body->child == NULL))
1.24      kristaps  697:                        break;
1.158     schwarze  698:
1.134     schwarze  699:                for (i = 0; i < mt->pardist; i++)
                    700:                        term_vspace(p);
1.19      kristaps  701:                break;
1.171     schwarze  702:        case ROFFT_HEAD:
1.52      kristaps  703:                term_fontrepl(p, TERMFONT_BOLD);
1.203     schwarze  704:                p->tcol->offset = term_len(p, 3);
                    705:                p->tcol->rmargin = mt->offset;
1.176     schwarze  706:                p->trailspace = mt->offset;
                    707:                p->flags |= TERMP_NOBREAK | TERMP_BRIND;
1.19      kristaps  708:                break;
1.171     schwarze  709:        case ROFFT_BODY:
1.203     schwarze  710:                p->tcol->offset = mt->offset;
                    711:                p->tcol->rmargin = p->maxrmargin;
1.176     schwarze  712:                p->trailspace = 0;
                    713:                p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
1.24      kristaps  714:                break;
1.19      kristaps  715:        default:
                    716:                break;
                    717:        }
                    718:
1.185     schwarze  719:        return 1;
1.1       kristaps  720: }
                    721:
                    722: static void
                    723: post_SS(DECL_ARGS)
                    724: {
1.146     schwarze  725:
1.19      kristaps  726:        switch (n->type) {
1.171     schwarze  727:        case ROFFT_HEAD:
1.19      kristaps  728:                term_newln(p);
                    729:                break;
1.171     schwarze  730:        case ROFFT_BODY:
1.24      kristaps  731:                term_newln(p);
                    732:                break;
1.19      kristaps  733:        default:
                    734:                break;
                    735:        }
1.1       kristaps  736: }
                    737:
                    738: static int
                    739: pre_SH(DECL_ARGS)
                    740: {
1.134     schwarze  741:        int      i;
1.22      kristaps  742:
1.19      kristaps  743:        switch (n->type) {
1.171     schwarze  744:        case ROFFT_BLOCK:
1.113     kristaps  745:                mt->fl &= ~MANT_LITERAL;
1.122     schwarze  746:                mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
                    747:                mt->offset = term_len(p, p->defindent);
1.158     schwarze  748:
                    749:                /*
                    750:                 * No vertical space before the first section
                    751:                 * and after an empty section.
                    752:                 */
                    753:
                    754:                do {
                    755:                        n = n->prev;
1.209     schwarze  756:                } while (n != NULL && n->tok >= MAN_TH &&
1.214     schwarze  757:                    man_term_act(n->tok)->flags & MAN_NOTEXT);
1.210     schwarze  758:                if (n == NULL || n->type == ROFFT_COMMENT ||
                    759:                    (n->tok == MAN_SH && n->body->child == NULL))
1.61      kristaps  760:                        break;
1.158     schwarze  761:
1.134     schwarze  762:                for (i = 0; i < mt->pardist; i++)
                    763:                        term_vspace(p);
1.19      kristaps  764:                break;
1.171     schwarze  765:        case ROFFT_HEAD:
1.52      kristaps  766:                term_fontrepl(p, TERMFONT_BOLD);
1.203     schwarze  767:                p->tcol->offset = 0;
                    768:                p->tcol->rmargin = mt->offset;
1.176     schwarze  769:                p->trailspace = mt->offset;
                    770:                p->flags |= TERMP_NOBREAK | TERMP_BRIND;
1.19      kristaps  771:                break;
1.171     schwarze  772:        case ROFFT_BODY:
1.203     schwarze  773:                p->tcol->offset = mt->offset;
                    774:                p->tcol->rmargin = p->maxrmargin;
1.176     schwarze  775:                p->trailspace = 0;
                    776:                p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
1.19      kristaps  777:                break;
                    778:        default:
                    779:                break;
                    780:        }
1.1       kristaps  781:
1.185     schwarze  782:        return 1;
1.1       kristaps  783: }
                    784:
                    785: static void
                    786: post_SH(DECL_ARGS)
                    787: {
1.146     schwarze  788:
1.19      kristaps  789:        switch (n->type) {
1.171     schwarze  790:        case ROFFT_HEAD:
1.19      kristaps  791:                term_newln(p);
                    792:                break;
1.171     schwarze  793:        case ROFFT_BODY:
1.19      kristaps  794:                term_newln(p);
                    795:                break;
                    796:        default:
                    797:                break;
                    798:        }
1.1       kristaps  799: }
                    800:
1.26      kristaps  801: static int
                    802: pre_RS(DECL_ARGS)
                    803: {
1.163     schwarze  804:        struct roffsu    su;
1.26      kristaps  805:
                    806:        switch (n->type) {
1.171     schwarze  807:        case ROFFT_BLOCK:
1.26      kristaps  808:                term_newln(p);
1.185     schwarze  809:                return 1;
1.171     schwarze  810:        case ROFFT_HEAD:
1.185     schwarze  811:                return 0;
1.26      kristaps  812:        default:
                    813:                break;
                    814:        }
                    815:
1.165     schwarze  816:        n = n->parent->head;
                    817:        n->aux = SHRT_MAX + 1;
1.177     schwarze  818:        if (n->child == NULL)
                    819:                n->aux = mt->lmargin[mt->lmargincur];
1.204     schwarze  820:        else if (a2roffsu(n->child->string, &su, SCALE_EN) != NULL)
1.205     schwarze  821:                n->aux = term_hen(p, &su);
1.165     schwarze  822:        if (n->aux < 0 && (size_t)(-n->aux) > mt->offset)
                    823:                n->aux = -mt->offset;
                    824:        else if (n->aux > SHRT_MAX)
                    825:                n->aux = term_len(p, p->defindent);
1.26      kristaps  826:
1.165     schwarze  827:        mt->offset += n->aux;
1.203     schwarze  828:        p->tcol->offset = mt->offset;
                    829:        p->tcol->rmargin = p->maxrmargin;
1.26      kristaps  830:
1.116     schwarze  831:        if (++mt->lmarginsz < MAXMARGINS)
                    832:                mt->lmargincur = mt->lmarginsz;
                    833:
1.178     schwarze  834:        mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
1.185     schwarze  835:        return 1;
1.26      kristaps  836: }
                    837:
                    838: static void
                    839: post_RS(DECL_ARGS)
                    840: {
                    841:
                    842:        switch (n->type) {
1.171     schwarze  843:        case ROFFT_BLOCK:
1.110     kristaps  844:                return;
1.171     schwarze  845:        case ROFFT_HEAD:
1.110     kristaps  846:                return;
1.26      kristaps  847:        default:
                    848:                term_newln(p);
                    849:                break;
                    850:        }
1.110     kristaps  851:
1.165     schwarze  852:        mt->offset -= n->parent->head->aux;
1.203     schwarze  853:        p->tcol->offset = mt->offset;
1.116     schwarze  854:
                    855:        if (--mt->lmarginsz < MAXMARGINS)
                    856:                mt->lmargincur = mt->lmarginsz;
1.215     schwarze  857: }
                    858:
                    859: static int
                    860: pre_SY(DECL_ARGS)
                    861: {
                    862:        const struct roff_node  *nn;
                    863:        int                      len;
                    864:
                    865:        switch (n->type) {
                    866:        case ROFFT_BLOCK:
1.217     schwarze  867:                if (n->prev == NULL || n->prev->tok != MAN_SY)
                    868:                        print_bvspace(p, n, mt->pardist);
1.215     schwarze  869:                return 1;
                    870:        case ROFFT_HEAD:
                    871:        case ROFFT_BODY:
                    872:                break;
                    873:        default:
                    874:                abort();
                    875:        }
                    876:
                    877:        nn = n->parent->head->child;
1.219     schwarze  878:        len = nn == NULL ? 1 : term_strlen(p, nn->string) + 1;
1.215     schwarze  879:
                    880:        switch (n->type) {
                    881:        case ROFFT_HEAD:
                    882:                p->tcol->offset = mt->offset;
                    883:                p->tcol->rmargin = mt->offset + len;
                    884:                p->flags |= TERMP_NOBREAK;
                    885:                term_fontrepl(p, TERMFONT_BOLD);
                    886:                break;
                    887:        case ROFFT_BODY:
                    888:                mt->lmargin[mt->lmargincur] = len;
                    889:                p->tcol->offset = mt->offset + len;
                    890:                p->tcol->rmargin = p->maxrmargin;
                    891:                p->flags |= TERMP_NOSPACE;
                    892:                break;
                    893:        default:
                    894:                abort();
                    895:        }
                    896:        return 1;
                    897: }
                    898:
                    899: static void
                    900: post_SY(DECL_ARGS)
                    901: {
                    902:        switch (n->type) {
                    903:        case ROFFT_HEAD:
                    904:                term_flushln(p);
                    905:                p->flags &= ~TERMP_NOBREAK;
                    906:                break;
                    907:        case ROFFT_BODY:
                    908:                term_newln(p);
                    909:                p->tcol->offset = mt->offset;
                    910:                break;
                    911:        default:
                    912:                break;
                    913:        }
1.137     schwarze  914: }
                    915:
                    916: static int
                    917: pre_UR(DECL_ARGS)
                    918: {
                    919:
1.185     schwarze  920:        return n->type != ROFFT_HEAD;
1.137     schwarze  921: }
                    922:
                    923: static void
                    924: post_UR(DECL_ARGS)
                    925: {
                    926:
1.171     schwarze  927:        if (n->type != ROFFT_BLOCK)
1.137     schwarze  928:                return;
                    929:
                    930:        term_word(p, "<");
                    931:        p->flags |= TERMP_NOSPACE;
                    932:
                    933:        if (NULL != n->child->child)
                    934:                print_man_node(p, mt, n->child->child, meta);
                    935:
                    936:        p->flags |= TERMP_NOSPACE;
                    937:        term_word(p, ">");
1.26      kristaps  938: }
                    939:
1.1       kristaps  940: static void
1.45      kristaps  941: print_man_node(DECL_ARGS)
1.1       kristaps  942: {
1.214     schwarze  943:        const struct man_term_act *act;
                    944:        int c;
1.1       kristaps  945:
                    946:        switch (n->type) {
1.171     schwarze  947:        case ROFFT_TEXT:
1.97      kristaps  948:                /*
                    949:                 * If we have a blank line, output a vertical space.
                    950:                 * If we have a space as the first character, break
                    951:                 * before printing the line's data.
                    952:                 */
1.200     schwarze  953:                if (*n->string == '\0') {
1.207     schwarze  954:                        if (p->flags & TERMP_NONEWLINE)
                    955:                                term_newln(p);
                    956:                        else
                    957:                                term_vspace(p);
1.98      schwarze  958:                        return;
1.200     schwarze  959:                } else if (*n->string == ' ' && n->flags & NODE_LINE &&
                    960:                    (p->flags & TERMP_NONEWLINE) == 0)
1.96      kristaps  961:                        term_newln(p);
1.212     schwarze  962:                else if (n->flags & NODE_DELIMC)
                    963:                        p->flags |= TERMP_NOSPACE;
1.54      kristaps  964:
1.4       kristaps  965:                term_word(p, n->string);
1.130     schwarze  966:                goto out;
1.210     schwarze  967:        case ROFFT_COMMENT:
                    968:                return;
1.171     schwarze  969:        case ROFFT_EQN:
1.188     schwarze  970:                if ( ! (n->flags & NODE_LINE))
1.152     schwarze  971:                        p->flags |= TERMP_NOSPACE;
1.115     kristaps  972:                term_eqn(p, n->eqn);
1.188     schwarze  973:                if (n->next != NULL && ! (n->next->flags & NODE_LINE))
1.153     schwarze  974:                        p->flags |= TERMP_NOSPACE;
1.97      kristaps  975:                return;
1.171     schwarze  976:        case ROFFT_TBL:
1.169     schwarze  977:                if (p->tbl.cols == NULL)
                    978:                        term_vspace(p);
1.92      kristaps  979:                term_tbl(p, n->span);
1.97      kristaps  980:                return;
1.1       kristaps  981:        default:
                    982:                break;
                    983:        }
1.224   ! schwarze  984:
        !           985:        if (n->tok == ROFF_nf)
        !           986:                n->tok = MAN_EX;
        !           987:        else if (n->tok == ROFF_fi)
        !           988:                n->tok = MAN_EE;
1.1       kristaps  989:
1.193     schwarze  990:        if (n->tok < ROFF_MAX) {
1.194     schwarze  991:                roff_term_pre(p, n);
1.193     schwarze  992:                return;
                    993:        }
                    994:
1.214     schwarze  995:        act = man_term_act(n->tok);
1.220     schwarze  996:        if ((act->flags & MAN_NOTEXT) == 0 && n->tok != MAN_SM)
1.97      kristaps  997:                term_fontrepl(p, TERMFONT_NONE);
                    998:
                    999:        c = 1;
1.214     schwarze 1000:        if (act->pre != NULL)
                   1001:                c = (*act->pre)(p, mt, n, meta);
1.97      kristaps 1002:
1.1       kristaps 1003:        if (c && n->child)
1.135     schwarze 1004:                print_man_nodelist(p, mt, n->child, meta);
1.1       kristaps 1005:
1.214     schwarze 1006:        if (act->post != NULL)
                   1007:                (*act->post)(p, mt, n, meta);
1.220     schwarze 1008:        if ((act->flags & MAN_NOTEXT) == 0 && n->tok != MAN_SM)
1.97      kristaps 1009:                term_fontrepl(p, TERMFONT_NONE);
1.63      kristaps 1010:
1.130     schwarze 1011: out:
                   1012:        /*
                   1013:         * If we're in a literal context, make sure that words
                   1014:         * together on the same line stay together.  This is a
                   1015:         * POST-printing call, so we check the NEXT word.  Since
                   1016:         * -man doesn't have nested macros, we don't need to be
                   1017:         * more specific than this.
                   1018:         */
1.157     schwarze 1019:        if (mt->fl & MANT_LITERAL &&
                   1020:            ! (p->flags & (TERMP_NOBREAK | TERMP_NONEWLINE)) &&
1.188     schwarze 1021:            (n->next == NULL || n->next->flags & NODE_LINE)) {
1.202     schwarze 1022:                p->flags |= TERMP_BRNEVER | TERMP_NOSPACE;
1.157     schwarze 1023:                if (n->string != NULL && *n->string != '\0')
1.130     schwarze 1024:                        term_flushln(p);
                   1025:                else
                   1026:                        term_newln(p);
1.202     schwarze 1027:                p->flags &= ~TERMP_BRNEVER;
1.203     schwarze 1028:                if (p->tcol->rmargin < p->maxrmargin &&
                   1029:                    n->parent->tok == MAN_HP) {
                   1030:                        p->tcol->offset = p->tcol->rmargin;
                   1031:                        p->tcol->rmargin = p->maxrmargin;
1.202     schwarze 1032:                }
1.130     schwarze 1033:        }
1.188     schwarze 1034:        if (NODE_EOS & n->flags)
1.63      kristaps 1035:                p->flags |= TERMP_SENTENCE;
1.1       kristaps 1036: }
                   1037:
                   1038:
                   1039: static void
1.52      kristaps 1040: print_man_nodelist(DECL_ARGS)
1.1       kristaps 1041: {
1.19      kristaps 1042:
1.168     schwarze 1043:        while (n != NULL) {
                   1044:                print_man_node(p, mt, n, meta);
                   1045:                n = n->next;
                   1046:        }
1.1       kristaps 1047: }
                   1048:
1.31      kristaps 1049: static void
1.173     schwarze 1050: print_man_foot(struct termp *p, const struct roff_meta *meta)
1.1       kristaps 1051: {
1.147     schwarze 1052:        char                    *title;
1.156     schwarze 1053:        size_t                   datelen, titlen;
1.73      kristaps 1054:
1.125     schwarze 1055:        assert(meta->title);
                   1056:        assert(meta->msec);
                   1057:        assert(meta->date);
1.1       kristaps 1058:
1.52      kristaps 1059:        term_fontrepl(p, TERMFONT_NONE);
1.50      kristaps 1060:
1.149     schwarze 1061:        if (meta->hasbody)
                   1062:                term_vspace(p);
1.126     schwarze 1063:
                   1064:        /*
                   1065:         * Temporary, undocumented option to imitate mdoc(7) output.
1.173     schwarze 1066:         * In the bottom right corner, use the operating system
                   1067:         * instead of the title.
1.126     schwarze 1068:         */
                   1069:
                   1070:        if ( ! p->mdocstyle) {
1.149     schwarze 1071:                if (meta->hasbody) {
                   1072:                        term_vspace(p);
                   1073:                        term_vspace(p);
                   1074:                }
1.147     schwarze 1075:                mandoc_asprintf(&title, "%s(%s)",
                   1076:                    meta->title, meta->msec);
1.173     schwarze 1077:        } else if (meta->os) {
                   1078:                title = mandoc_strdup(meta->os);
1.126     schwarze 1079:        } else {
1.147     schwarze 1080:                title = mandoc_strdup("");
1.126     schwarze 1081:        }
1.125     schwarze 1082:        datelen = term_strlen(p, meta->date);
1.1       kristaps 1083:
1.173     schwarze 1084:        /* Bottom left corner: operating system. */
1.126     schwarze 1085:
1.1       kristaps 1086:        p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
1.139     schwarze 1087:        p->trailspace = 1;
1.203     schwarze 1088:        p->tcol->offset = 0;
                   1089:        p->tcol->rmargin = p->maxrmargin > datelen ?
1.156     schwarze 1090:            (p->maxrmargin + term_len(p, 1) - datelen) / 2 : 0;
1.1       kristaps 1091:
1.173     schwarze 1092:        if (meta->os)
                   1093:                term_word(p, meta->os);
1.1       kristaps 1094:        term_flushln(p);
                   1095:
1.126     schwarze 1096:        /* At the bottom in the middle: manual date. */
                   1097:
1.203     schwarze 1098:        p->tcol->offset = p->tcol->rmargin;
1.156     schwarze 1099:        titlen = term_strlen(p, title);
1.203     schwarze 1100:        p->tcol->rmargin = p->maxrmargin > titlen ?
                   1101:            p->maxrmargin - titlen : 0;
1.117     schwarze 1102:        p->flags |= TERMP_NOSPACE;
1.123     schwarze 1103:
1.125     schwarze 1104:        term_word(p, meta->date);
1.123     schwarze 1105:        term_flushln(p);
                   1106:
1.126     schwarze 1107:        /* Bottom right corner: manual title and section. */
                   1108:
1.123     schwarze 1109:        p->flags &= ~TERMP_NOBREAK;
                   1110:        p->flags |= TERMP_NOSPACE;
1.139     schwarze 1111:        p->trailspace = 0;
1.203     schwarze 1112:        p->tcol->offset = p->tcol->rmargin;
                   1113:        p->tcol->rmargin = p->maxrmargin;
1.1       kristaps 1114:
1.123     schwarze 1115:        term_word(p, title);
1.1       kristaps 1116:        term_flushln(p);
1.211     schwarze 1117:
                   1118:        /*
                   1119:         * Reset the terminal state for more output after the footer:
                   1120:         * Some output modes, in particular PostScript and PDF, print
                   1121:         * the header and the footer into a buffer such that it can be
                   1122:         * reused for multiple output pages, then go on to format the
                   1123:         * main text.
                   1124:         */
                   1125:
                   1126:         p->tcol->offset = 0;
                   1127:         p->flags = 0;
                   1128:
1.147     schwarze 1129:        free(title);
1.1       kristaps 1130: }
                   1131:
1.31      kristaps 1132: static void
1.173     schwarze 1133: print_man_head(struct termp *p, const struct roff_meta *meta)
1.1       kristaps 1134: {
1.148     schwarze 1135:        const char              *volume;
1.147     schwarze 1136:        char                    *title;
1.148     schwarze 1137:        size_t                   vollen, titlen;
1.73      kristaps 1138:
1.135     schwarze 1139:        assert(meta->title);
                   1140:        assert(meta->msec);
1.1       kristaps 1141:
1.148     schwarze 1142:        volume = NULL == meta->vol ? "" : meta->vol;
                   1143:        vollen = term_strlen(p, volume);
1.1       kristaps 1144:
1.126     schwarze 1145:        /* Top left corner: manual title and section. */
                   1146:
1.147     schwarze 1147:        mandoc_asprintf(&title, "%s(%s)", meta->title, meta->msec);
1.77      kristaps 1148:        titlen = term_strlen(p, title);
1.1       kristaps 1149:
1.118     schwarze 1150:        p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
1.139     schwarze 1151:        p->trailspace = 1;
1.203     schwarze 1152:        p->tcol->offset = 0;
                   1153:        p->tcol->rmargin = 2 * (titlen+1) + vollen < p->maxrmargin ?
1.148     schwarze 1154:            (p->maxrmargin - vollen + term_len(p, 1)) / 2 :
1.156     schwarze 1155:            vollen < p->maxrmargin ? p->maxrmargin - vollen : 0;
1.1       kristaps 1156:
                   1157:        term_word(p, title);
                   1158:        term_flushln(p);
                   1159:
1.126     schwarze 1160:        /* At the top in the middle: manual volume. */
                   1161:
1.117     schwarze 1162:        p->flags |= TERMP_NOSPACE;
1.203     schwarze 1163:        p->tcol->offset = p->tcol->rmargin;
                   1164:        p->tcol->rmargin = p->tcol->offset + vollen + titlen <
                   1165:            p->maxrmargin ?  p->maxrmargin - titlen : p->maxrmargin;
1.1       kristaps 1166:
1.148     schwarze 1167:        term_word(p, volume);
1.1       kristaps 1168:        term_flushln(p);
                   1169:
1.126     schwarze 1170:        /* Top right corner: title and section, again. */
                   1171:
1.1       kristaps 1172:        p->flags &= ~TERMP_NOBREAK;
1.139     schwarze 1173:        p->trailspace = 0;
1.203     schwarze 1174:        if (p->tcol->rmargin + titlen <= p->maxrmargin) {
1.117     schwarze 1175:                p->flags |= TERMP_NOSPACE;
1.203     schwarze 1176:                p->tcol->offset = p->tcol->rmargin;
                   1177:                p->tcol->rmargin = p->maxrmargin;
1.57      kristaps 1178:                term_word(p, title);
                   1179:                term_flushln(p);
                   1180:        }
1.1       kristaps 1181:
1.118     schwarze 1182:        p->flags &= ~TERMP_NOSPACE;
1.203     schwarze 1183:        p->tcol->offset = 0;
                   1184:        p->tcol->rmargin = p->maxrmargin;
1.61      kristaps 1185:
1.146     schwarze 1186:        /*
1.126     schwarze 1187:         * Groff prints three blank lines before the content.
                   1188:         * Do the same, except in the temporary, undocumented
                   1189:         * mode imitating mdoc(7) output.
1.62      kristaps 1190:         */
                   1191:
1.61      kristaps 1192:        term_vspace(p);
1.126     schwarze 1193:        if ( ! p->mdocstyle) {
                   1194:                term_vspace(p);
                   1195:                term_vspace(p);
                   1196:        }
1.147     schwarze 1197:        free(title);
1.1       kristaps 1198: }

CVSweb