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

Annotation of mandoc/man_term.c, Revision 1.226

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

CVSweb