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

Annotation of mandoc/man_term.c, Revision 1.244

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

CVSweb