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

Annotation of mandoc/man_term.c, Revision 1.227

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

CVSweb