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

Annotation of mandoc/man_term.c, Revision 1.235

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

CVSweb