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

Annotation of mandoc/man_term.c, Revision 1.223

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

CVSweb