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

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