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

Annotation of mandoc/man_term.c, Revision 1.214

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

CVSweb