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

Annotation of mandoc/man_term.c, Revision 1.234

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

CVSweb