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

Annotation of mandoc/man_term.c, Revision 1.215

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

CVSweb