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

Annotation of mandoc/man_term.c, Revision 1.228

1.228   ! schwarze    1: /*     $Id: man_term.c,v 1.227 2019/01/05 01:29:32 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;
1.228   ! schwarze  836:                if (n->next->child == NULL ||
        !           837:                    (n->next->child->flags & NODE_NOFILL) == 0)
        !           838:                        p->flags |= TERMP_NOBREAK;
1.215     schwarze  839:                term_fontrepl(p, TERMFONT_BOLD);
                    840:                break;
                    841:        case ROFFT_BODY:
                    842:                mt->lmargin[mt->lmargincur] = len;
                    843:                p->tcol->offset = mt->offset + len;
                    844:                p->tcol->rmargin = p->maxrmargin;
                    845:                p->flags |= TERMP_NOSPACE;
                    846:                break;
                    847:        default:
                    848:                abort();
                    849:        }
                    850:        return 1;
                    851: }
                    852:
                    853: static void
                    854: post_SY(DECL_ARGS)
                    855: {
                    856:        switch (n->type) {
1.227     schwarze  857:        case ROFFT_BLOCK:
                    858:                break;
1.215     schwarze  859:        case ROFFT_HEAD:
                    860:                term_flushln(p);
                    861:                p->flags &= ~TERMP_NOBREAK;
                    862:                break;
                    863:        case ROFFT_BODY:
                    864:                term_newln(p);
                    865:                p->tcol->offset = mt->offset;
                    866:                break;
                    867:        default:
1.227     schwarze  868:                abort();
1.215     schwarze  869:        }
1.137     schwarze  870: }
                    871:
                    872: static int
                    873: pre_UR(DECL_ARGS)
                    874: {
1.185     schwarze  875:        return n->type != ROFFT_HEAD;
1.137     schwarze  876: }
                    877:
                    878: static void
                    879: post_UR(DECL_ARGS)
                    880: {
1.171     schwarze  881:        if (n->type != ROFFT_BLOCK)
1.137     schwarze  882:                return;
                    883:
                    884:        term_word(p, "<");
                    885:        p->flags |= TERMP_NOSPACE;
                    886:
1.227     schwarze  887:        if (n->child->child != NULL)
1.137     schwarze  888:                print_man_node(p, mt, n->child->child, meta);
                    889:
                    890:        p->flags |= TERMP_NOSPACE;
                    891:        term_word(p, ">");
1.26      kristaps  892: }
                    893:
1.1       kristaps  894: static void
1.45      kristaps  895: print_man_node(DECL_ARGS)
1.1       kristaps  896: {
1.214     schwarze  897:        const struct man_term_act *act;
                    898:        int c;
1.1       kristaps  899:
                    900:        switch (n->type) {
1.171     schwarze  901:        case ROFFT_TEXT:
1.97      kristaps  902:                /*
                    903:                 * If we have a blank line, output a vertical space.
                    904:                 * If we have a space as the first character, break
                    905:                 * before printing the line's data.
                    906:                 */
1.200     schwarze  907:                if (*n->string == '\0') {
1.207     schwarze  908:                        if (p->flags & TERMP_NONEWLINE)
                    909:                                term_newln(p);
                    910:                        else
                    911:                                term_vspace(p);
1.98      schwarze  912:                        return;
1.200     schwarze  913:                } else if (*n->string == ' ' && n->flags & NODE_LINE &&
                    914:                    (p->flags & TERMP_NONEWLINE) == 0)
1.96      kristaps  915:                        term_newln(p);
1.212     schwarze  916:                else if (n->flags & NODE_DELIMC)
                    917:                        p->flags |= TERMP_NOSPACE;
1.54      kristaps  918:
1.4       kristaps  919:                term_word(p, n->string);
1.130     schwarze  920:                goto out;
1.210     schwarze  921:        case ROFFT_COMMENT:
                    922:                return;
1.171     schwarze  923:        case ROFFT_EQN:
1.188     schwarze  924:                if ( ! (n->flags & NODE_LINE))
1.152     schwarze  925:                        p->flags |= TERMP_NOSPACE;
1.115     kristaps  926:                term_eqn(p, n->eqn);
1.188     schwarze  927:                if (n->next != NULL && ! (n->next->flags & NODE_LINE))
1.153     schwarze  928:                        p->flags |= TERMP_NOSPACE;
1.97      kristaps  929:                return;
1.171     schwarze  930:        case ROFFT_TBL:
1.169     schwarze  931:                if (p->tbl.cols == NULL)
                    932:                        term_vspace(p);
1.92      kristaps  933:                term_tbl(p, n->span);
1.97      kristaps  934:                return;
1.1       kristaps  935:        default:
                    936:                break;
                    937:        }
1.224     schwarze  938:
1.193     schwarze  939:        if (n->tok < ROFF_MAX) {
1.194     schwarze  940:                roff_term_pre(p, n);
1.193     schwarze  941:                return;
                    942:        }
                    943:
1.214     schwarze  944:        act = man_term_act(n->tok);
1.220     schwarze  945:        if ((act->flags & MAN_NOTEXT) == 0 && n->tok != MAN_SM)
1.97      kristaps  946:                term_fontrepl(p, TERMFONT_NONE);
                    947:
                    948:        c = 1;
1.214     schwarze  949:        if (act->pre != NULL)
                    950:                c = (*act->pre)(p, mt, n, meta);
1.97      kristaps  951:
1.227     schwarze  952:        if (c && n->child != NULL)
1.135     schwarze  953:                print_man_nodelist(p, mt, n->child, meta);
1.1       kristaps  954:
1.214     schwarze  955:        if (act->post != NULL)
                    956:                (*act->post)(p, mt, n, meta);
1.220     schwarze  957:        if ((act->flags & MAN_NOTEXT) == 0 && n->tok != MAN_SM)
1.97      kristaps  958:                term_fontrepl(p, TERMFONT_NONE);
1.63      kristaps  959:
1.130     schwarze  960: out:
                    961:        /*
                    962:         * If we're in a literal context, make sure that words
                    963:         * together on the same line stay together.  This is a
                    964:         * POST-printing call, so we check the NEXT word.  Since
                    965:         * -man doesn't have nested macros, we don't need to be
                    966:         * more specific than this.
                    967:         */
1.225     schwarze  968:        if (n->flags & NODE_NOFILL &&
1.157     schwarze  969:            ! (p->flags & (TERMP_NOBREAK | TERMP_NONEWLINE)) &&
1.188     schwarze  970:            (n->next == NULL || n->next->flags & NODE_LINE)) {
1.202     schwarze  971:                p->flags |= TERMP_BRNEVER | TERMP_NOSPACE;
1.157     schwarze  972:                if (n->string != NULL && *n->string != '\0')
1.130     schwarze  973:                        term_flushln(p);
                    974:                else
                    975:                        term_newln(p);
1.202     schwarze  976:                p->flags &= ~TERMP_BRNEVER;
1.203     schwarze  977:                if (p->tcol->rmargin < p->maxrmargin &&
                    978:                    n->parent->tok == MAN_HP) {
                    979:                        p->tcol->offset = p->tcol->rmargin;
                    980:                        p->tcol->rmargin = p->maxrmargin;
1.202     schwarze  981:                }
1.130     schwarze  982:        }
1.227     schwarze  983:        if (n->flags & NODE_EOS)
1.63      kristaps  984:                p->flags |= TERMP_SENTENCE;
1.1       kristaps  985: }
                    986:
                    987: static void
1.52      kristaps  988: print_man_nodelist(DECL_ARGS)
1.1       kristaps  989: {
1.168     schwarze  990:        while (n != NULL) {
                    991:                print_man_node(p, mt, n, meta);
                    992:                n = n->next;
                    993:        }
1.1       kristaps  994: }
                    995:
1.31      kristaps  996: static void
1.173     schwarze  997: print_man_foot(struct termp *p, const struct roff_meta *meta)
1.1       kristaps  998: {
1.147     schwarze  999:        char                    *title;
1.156     schwarze 1000:        size_t                   datelen, titlen;
1.73      kristaps 1001:
1.125     schwarze 1002:        assert(meta->title);
                   1003:        assert(meta->msec);
                   1004:        assert(meta->date);
1.1       kristaps 1005:
1.52      kristaps 1006:        term_fontrepl(p, TERMFONT_NONE);
1.50      kristaps 1007:
1.149     schwarze 1008:        if (meta->hasbody)
                   1009:                term_vspace(p);
1.126     schwarze 1010:
                   1011:        /*
                   1012:         * Temporary, undocumented option to imitate mdoc(7) output.
1.173     schwarze 1013:         * In the bottom right corner, use the operating system
                   1014:         * instead of the title.
1.126     schwarze 1015:         */
                   1016:
                   1017:        if ( ! p->mdocstyle) {
1.149     schwarze 1018:                if (meta->hasbody) {
                   1019:                        term_vspace(p);
                   1020:                        term_vspace(p);
                   1021:                }
1.147     schwarze 1022:                mandoc_asprintf(&title, "%s(%s)",
                   1023:                    meta->title, meta->msec);
1.227     schwarze 1024:        } else if (meta->os != NULL) {
1.173     schwarze 1025:                title = mandoc_strdup(meta->os);
1.126     schwarze 1026:        } else {
1.147     schwarze 1027:                title = mandoc_strdup("");
1.126     schwarze 1028:        }
1.125     schwarze 1029:        datelen = term_strlen(p, meta->date);
1.1       kristaps 1030:
1.173     schwarze 1031:        /* Bottom left corner: operating system. */
1.126     schwarze 1032:
1.1       kristaps 1033:        p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
1.139     schwarze 1034:        p->trailspace = 1;
1.203     schwarze 1035:        p->tcol->offset = 0;
                   1036:        p->tcol->rmargin = p->maxrmargin > datelen ?
1.156     schwarze 1037:            (p->maxrmargin + term_len(p, 1) - datelen) / 2 : 0;
1.1       kristaps 1038:
1.173     schwarze 1039:        if (meta->os)
                   1040:                term_word(p, meta->os);
1.1       kristaps 1041:        term_flushln(p);
                   1042:
1.126     schwarze 1043:        /* At the bottom in the middle: manual date. */
                   1044:
1.203     schwarze 1045:        p->tcol->offset = p->tcol->rmargin;
1.156     schwarze 1046:        titlen = term_strlen(p, title);
1.203     schwarze 1047:        p->tcol->rmargin = p->maxrmargin > titlen ?
                   1048:            p->maxrmargin - titlen : 0;
1.117     schwarze 1049:        p->flags |= TERMP_NOSPACE;
1.123     schwarze 1050:
1.125     schwarze 1051:        term_word(p, meta->date);
1.123     schwarze 1052:        term_flushln(p);
                   1053:
1.126     schwarze 1054:        /* Bottom right corner: manual title and section. */
                   1055:
1.123     schwarze 1056:        p->flags &= ~TERMP_NOBREAK;
                   1057:        p->flags |= TERMP_NOSPACE;
1.139     schwarze 1058:        p->trailspace = 0;
1.203     schwarze 1059:        p->tcol->offset = p->tcol->rmargin;
                   1060:        p->tcol->rmargin = p->maxrmargin;
1.1       kristaps 1061:
1.123     schwarze 1062:        term_word(p, title);
1.1       kristaps 1063:        term_flushln(p);
1.211     schwarze 1064:
                   1065:        /*
                   1066:         * Reset the terminal state for more output after the footer:
                   1067:         * Some output modes, in particular PostScript and PDF, print
                   1068:         * the header and the footer into a buffer such that it can be
                   1069:         * reused for multiple output pages, then go on to format the
                   1070:         * main text.
                   1071:         */
                   1072:
                   1073:         p->tcol->offset = 0;
                   1074:         p->flags = 0;
                   1075:
1.147     schwarze 1076:        free(title);
1.1       kristaps 1077: }
                   1078:
1.31      kristaps 1079: static void
1.173     schwarze 1080: print_man_head(struct termp *p, const struct roff_meta *meta)
1.1       kristaps 1081: {
1.148     schwarze 1082:        const char              *volume;
1.147     schwarze 1083:        char                    *title;
1.148     schwarze 1084:        size_t                   vollen, titlen;
1.73      kristaps 1085:
1.135     schwarze 1086:        assert(meta->title);
                   1087:        assert(meta->msec);
1.1       kristaps 1088:
1.148     schwarze 1089:        volume = NULL == meta->vol ? "" : meta->vol;
                   1090:        vollen = term_strlen(p, volume);
1.1       kristaps 1091:
1.126     schwarze 1092:        /* Top left corner: manual title and section. */
                   1093:
1.147     schwarze 1094:        mandoc_asprintf(&title, "%s(%s)", meta->title, meta->msec);
1.77      kristaps 1095:        titlen = term_strlen(p, title);
1.1       kristaps 1096:
1.118     schwarze 1097:        p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
1.139     schwarze 1098:        p->trailspace = 1;
1.203     schwarze 1099:        p->tcol->offset = 0;
                   1100:        p->tcol->rmargin = 2 * (titlen+1) + vollen < p->maxrmargin ?
1.148     schwarze 1101:            (p->maxrmargin - vollen + term_len(p, 1)) / 2 :
1.156     schwarze 1102:            vollen < p->maxrmargin ? p->maxrmargin - vollen : 0;
1.1       kristaps 1103:
                   1104:        term_word(p, title);
                   1105:        term_flushln(p);
                   1106:
1.126     schwarze 1107:        /* At the top in the middle: manual volume. */
                   1108:
1.117     schwarze 1109:        p->flags |= TERMP_NOSPACE;
1.203     schwarze 1110:        p->tcol->offset = p->tcol->rmargin;
                   1111:        p->tcol->rmargin = p->tcol->offset + vollen + titlen <
                   1112:            p->maxrmargin ?  p->maxrmargin - titlen : p->maxrmargin;
1.1       kristaps 1113:
1.148     schwarze 1114:        term_word(p, volume);
1.1       kristaps 1115:        term_flushln(p);
                   1116:
1.126     schwarze 1117:        /* Top right corner: title and section, again. */
                   1118:
1.1       kristaps 1119:        p->flags &= ~TERMP_NOBREAK;
1.139     schwarze 1120:        p->trailspace = 0;
1.203     schwarze 1121:        if (p->tcol->rmargin + titlen <= p->maxrmargin) {
1.117     schwarze 1122:                p->flags |= TERMP_NOSPACE;
1.203     schwarze 1123:                p->tcol->offset = p->tcol->rmargin;
                   1124:                p->tcol->rmargin = p->maxrmargin;
1.57      kristaps 1125:                term_word(p, title);
                   1126:                term_flushln(p);
                   1127:        }
1.1       kristaps 1128:
1.118     schwarze 1129:        p->flags &= ~TERMP_NOSPACE;
1.203     schwarze 1130:        p->tcol->offset = 0;
                   1131:        p->tcol->rmargin = p->maxrmargin;
1.61      kristaps 1132:
1.146     schwarze 1133:        /*
1.126     schwarze 1134:         * Groff prints three blank lines before the content.
                   1135:         * Do the same, except in the temporary, undocumented
                   1136:         * mode imitating mdoc(7) output.
1.62      kristaps 1137:         */
                   1138:
1.61      kristaps 1139:        term_vspace(p);
1.126     schwarze 1140:        if ( ! p->mdocstyle) {
                   1141:                term_vspace(p);
                   1142:                term_vspace(p);
                   1143:        }
1.147     schwarze 1144:        free(title);
1.1       kristaps 1145: }

CVSweb