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

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

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