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

Annotation of mandoc/man_term.c, Revision 1.225

1.225   ! schwarze    1: /*     $Id: man_term.c,v 1.224 2018/12/31 07:08:12 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 {
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.225   ! schwarze  422:        if ((n->flags & NODE_NOFILL) == 0) {
1.145     schwarze  423:                p->flags |= TERMP_NOBREAK | TERMP_BRIND;
1.139     schwarze  424:                p->trailspace = 2;
1.130     schwarze  425:        }
                    426:
1.24      kristaps  427:        /* Calculate offset. */
                    428:
1.163     schwarze  429:        if ((nn = n->parent->head->child) != NULL &&
1.204     schwarze  430:            a2roffsu(nn->string, &su, SCALE_EN) != NULL) {
1.205     schwarze  431:                len = term_hen(p, &su);
1.164     schwarze  432:                if (len < 0 && (size_t)(-len) > mt->offset)
                    433:                        len = -mt->offset;
                    434:                else if (len > SHRT_MAX)
                    435:                        len = term_len(p, p->defindent);
1.163     schwarze  436:                mt->lmargin[mt->lmargincur] = len;
                    437:        } else
                    438:                len = mt->lmargin[mt->lmargincur];
1.24      kristaps  439:
1.203     schwarze  440:        p->tcol->offset = mt->offset;
                    441:        p->tcol->rmargin = mt->offset + len;
1.185     schwarze  442:        return 1;
1.19      kristaps  443: }
                    444:
1.20      kristaps  445: static void
                    446: post_HP(DECL_ARGS)
                    447: {
                    448:
                    449:        switch (n->type) {
1.171     schwarze  450:        case ROFFT_BODY:
1.136     schwarze  451:                term_newln(p);
1.174     schwarze  452:
                    453:                /*
                    454:                 * Compatibility with a groff bug.
                    455:                 * The .HP macro uses the undocumented .tag request
                    456:                 * which causes a line break and cancels no-space
                    457:                 * mode even if there isn't any output.
                    458:                 */
                    459:
                    460:                if (n->child == NULL)
                    461:                        term_vspace(p);
                    462:
1.145     schwarze  463:                p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
1.139     schwarze  464:                p->trailspace = 0;
1.203     schwarze  465:                p->tcol->offset = mt->offset;
                    466:                p->tcol->rmargin = p->maxrmargin;
1.20      kristaps  467:                break;
                    468:        default:
                    469:                break;
                    470:        }
                    471: }
                    472:
1.19      kristaps  473: static int
1.1       kristaps  474: pre_PP(DECL_ARGS)
                    475: {
                    476:
1.19      kristaps  477:        switch (n->type) {
1.171     schwarze  478:        case ROFFT_BLOCK:
1.122     schwarze  479:                mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
1.134     schwarze  480:                print_bvspace(p, n, mt->pardist);
1.19      kristaps  481:                break;
                    482:        default:
1.203     schwarze  483:                p->tcol->offset = mt->offset;
1.19      kristaps  484:                break;
                    485:        }
                    486:
1.185     schwarze  487:        return n->type != ROFFT_HEAD;
1.1       kristaps  488: }
                    489:
                    490: static int
1.4       kristaps  491: pre_IP(DECL_ARGS)
                    492: {
1.163     schwarze  493:        struct roffsu            su;
1.172     schwarze  494:        const struct roff_node  *nn;
1.225   ! schwarze  495:        int                      len;
1.18      kristaps  496:
1.22      kristaps  497:        switch (n->type) {
1.171     schwarze  498:        case ROFFT_BODY:
1.22      kristaps  499:                p->flags |= TERMP_NOSPACE;
                    500:                break;
1.171     schwarze  501:        case ROFFT_HEAD:
1.22      kristaps  502:                p->flags |= TERMP_NOBREAK;
1.139     schwarze  503:                p->trailspace = 1;
1.22      kristaps  504:                break;
1.171     schwarze  505:        case ROFFT_BLOCK:
1.134     schwarze  506:                print_bvspace(p, n, mt->pardist);
1.23      kristaps  507:                /* FALLTHROUGH */
1.22      kristaps  508:        default:
1.185     schwarze  509:                return 1;
1.22      kristaps  510:        }
1.18      kristaps  511:
1.94      schwarze  512:        /* Calculate the offset from the optional second argument. */
1.163     schwarze  513:        if ((nn = n->parent->head->child) != NULL &&
                    514:            (nn = nn->next) != NULL &&
1.204     schwarze  515:            a2roffsu(nn->string, &su, SCALE_EN) != NULL) {
1.205     schwarze  516:                len = term_hen(p, &su);
1.163     schwarze  517:                if (len < 0 && (size_t)(-len) > mt->offset)
                    518:                        len = -mt->offset;
1.164     schwarze  519:                else if (len > SHRT_MAX)
                    520:                        len = term_len(p, p->defindent);
                    521:                mt->lmargin[mt->lmargincur] = len;
1.163     schwarze  522:        } else
                    523:                len = mt->lmargin[mt->lmargincur];
1.4       kristaps  524:
1.22      kristaps  525:        switch (n->type) {
1.171     schwarze  526:        case ROFFT_HEAD:
1.203     schwarze  527:                p->tcol->offset = mt->offset;
                    528:                p->tcol->rmargin = mt->offset + len;
1.94      schwarze  529:                if (n->child)
1.135     schwarze  530:                        print_man_node(p, mt, n->child, meta);
1.185     schwarze  531:                return 0;
1.171     schwarze  532:        case ROFFT_BODY:
1.203     schwarze  533:                p->tcol->offset = mt->offset + len;
                    534:                p->tcol->rmargin = p->maxrmargin;
1.23      kristaps  535:                break;
1.22      kristaps  536:        default:
                    537:                break;
1.18      kristaps  538:        }
1.185     schwarze  539:        return 1;
1.22      kristaps  540: }
1.18      kristaps  541:
1.22      kristaps  542: static void
                    543: post_IP(DECL_ARGS)
                    544: {
                    545:        switch (n->type) {
1.171     schwarze  546:        case ROFFT_HEAD:
1.22      kristaps  547:                term_flushln(p);
                    548:                p->flags &= ~TERMP_NOBREAK;
1.139     schwarze  549:                p->trailspace = 0;
1.203     schwarze  550:                p->tcol->rmargin = p->maxrmargin;
1.22      kristaps  551:                break;
1.171     schwarze  552:        case ROFFT_BODY:
1.94      schwarze  553:                term_newln(p);
1.203     schwarze  554:                p->tcol->offset = mt->offset;
1.22      kristaps  555:                break;
                    556:        default:
                    557:                break;
                    558:        }
1.4       kristaps  559: }
                    560:
                    561: static int
1.1       kristaps  562: pre_TP(DECL_ARGS)
                    563: {
1.163     schwarze  564:        struct roffsu            su;
1.172     schwarze  565:        struct roff_node        *nn;
1.225   ! schwarze  566:        int                      len;
1.1       kristaps  567:
1.21      kristaps  568:        switch (n->type) {
1.171     schwarze  569:        case ROFFT_HEAD:
1.184     schwarze  570:                p->flags |= TERMP_NOBREAK | TERMP_BRTRSP;
1.139     schwarze  571:                p->trailspace = 1;
1.21      kristaps  572:                break;
1.171     schwarze  573:        case ROFFT_BODY:
1.21      kristaps  574:                p->flags |= TERMP_NOSPACE;
1.23      kristaps  575:                break;
1.171     schwarze  576:        case ROFFT_BLOCK:
1.213     schwarze  577:                if (n->tok == MAN_TP)
                    578:                        print_bvspace(p, n, mt->pardist);
1.23      kristaps  579:                /* FALLTHROUGH */
                    580:        default:
1.185     schwarze  581:                return 1;
1.23      kristaps  582:        }
                    583:
                    584:        /* Calculate offset. */
                    585:
1.163     schwarze  586:        if ((nn = n->parent->head->child) != NULL &&
1.188     schwarze  587:            nn->string != NULL && ! (NODE_LINE & nn->flags) &&
1.204     schwarze  588:            a2roffsu(nn->string, &su, SCALE_EN) != NULL) {
1.205     schwarze  589:                len = term_hen(p, &su);
1.163     schwarze  590:                if (len < 0 && (size_t)(-len) > mt->offset)
                    591:                        len = -mt->offset;
1.164     schwarze  592:                else if (len > SHRT_MAX)
                    593:                        len = term_len(p, p->defindent);
                    594:                mt->lmargin[mt->lmargincur] = len;
1.163     schwarze  595:        } else
                    596:                len = mt->lmargin[mt->lmargincur];
1.23      kristaps  597:
                    598:        switch (n->type) {
1.171     schwarze  599:        case ROFFT_HEAD:
1.203     schwarze  600:                p->tcol->offset = mt->offset;
                    601:                p->tcol->rmargin = mt->offset + len;
1.23      kristaps  602:
                    603:                /* Don't print same-line elements. */
1.141     schwarze  604:                nn = n->child;
1.188     schwarze  605:                while (NULL != nn && 0 == (NODE_LINE & nn->flags))
1.141     schwarze  606:                        nn = nn->next;
                    607:
                    608:                while (NULL != nn) {
                    609:                        print_man_node(p, mt, nn, meta);
                    610:                        nn = nn->next;
                    611:                }
1.185     schwarze  612:                return 0;
1.171     schwarze  613:        case ROFFT_BODY:
1.203     schwarze  614:                p->tcol->offset = mt->offset + len;
                    615:                p->tcol->rmargin = p->maxrmargin;
1.139     schwarze  616:                p->trailspace = 0;
1.184     schwarze  617:                p->flags &= ~(TERMP_NOBREAK | TERMP_BRTRSP);
1.21      kristaps  618:                break;
                    619:        default:
                    620:                break;
                    621:        }
1.185     schwarze  622:        return 1;
1.21      kristaps  623: }
1.1       kristaps  624:
1.21      kristaps  625: static void
                    626: post_TP(DECL_ARGS)
                    627: {
                    628:        switch (n->type) {
1.171     schwarze  629:        case ROFFT_HEAD:
1.21      kristaps  630:                term_flushln(p);
                    631:                break;
1.171     schwarze  632:        case ROFFT_BODY:
1.94      schwarze  633:                term_newln(p);
1.203     schwarze  634:                p->tcol->offset = mt->offset;
1.21      kristaps  635:                break;
                    636:        default:
                    637:                break;
                    638:        }
1.1       kristaps  639: }
                    640:
                    641: static int
                    642: pre_SS(DECL_ARGS)
                    643: {
1.134     schwarze  644:        int      i;
1.1       kristaps  645:
1.19      kristaps  646:        switch (n->type) {
1.171     schwarze  647:        case ROFFT_BLOCK:
1.122     schwarze  648:                mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
                    649:                mt->offset = term_len(p, p->defindent);
1.158     schwarze  650:
                    651:                /*
                    652:                 * No vertical space before the first subsection
                    653:                 * and after an empty subsection.
                    654:                 */
                    655:
                    656:                do {
                    657:                        n = n->prev;
1.209     schwarze  658:                } while (n != NULL && n->tok >= MAN_TH &&
1.214     schwarze  659:                    man_term_act(n->tok)->flags & MAN_NOTEXT);
1.210     schwarze  660:                if (n == NULL || n->type == ROFFT_COMMENT ||
                    661:                    (n->tok == MAN_SS && n->body->child == NULL))
1.24      kristaps  662:                        break;
1.158     schwarze  663:
1.134     schwarze  664:                for (i = 0; i < mt->pardist; i++)
                    665:                        term_vspace(p);
1.19      kristaps  666:                break;
1.171     schwarze  667:        case ROFFT_HEAD:
1.52      kristaps  668:                term_fontrepl(p, TERMFONT_BOLD);
1.203     schwarze  669:                p->tcol->offset = term_len(p, 3);
                    670:                p->tcol->rmargin = mt->offset;
1.176     schwarze  671:                p->trailspace = mt->offset;
                    672:                p->flags |= TERMP_NOBREAK | TERMP_BRIND;
1.19      kristaps  673:                break;
1.171     schwarze  674:        case ROFFT_BODY:
1.203     schwarze  675:                p->tcol->offset = mt->offset;
                    676:                p->tcol->rmargin = p->maxrmargin;
1.176     schwarze  677:                p->trailspace = 0;
                    678:                p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
1.24      kristaps  679:                break;
1.19      kristaps  680:        default:
                    681:                break;
                    682:        }
                    683:
1.185     schwarze  684:        return 1;
1.1       kristaps  685: }
                    686:
                    687: static void
                    688: post_SS(DECL_ARGS)
                    689: {
1.146     schwarze  690:
1.19      kristaps  691:        switch (n->type) {
1.171     schwarze  692:        case ROFFT_HEAD:
1.19      kristaps  693:                term_newln(p);
                    694:                break;
1.171     schwarze  695:        case ROFFT_BODY:
1.24      kristaps  696:                term_newln(p);
                    697:                break;
1.19      kristaps  698:        default:
                    699:                break;
                    700:        }
1.1       kristaps  701: }
                    702:
                    703: static int
                    704: pre_SH(DECL_ARGS)
                    705: {
1.134     schwarze  706:        int      i;
1.22      kristaps  707:
1.19      kristaps  708:        switch (n->type) {
1.171     schwarze  709:        case ROFFT_BLOCK:
1.122     schwarze  710:                mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
                    711:                mt->offset = term_len(p, p->defindent);
1.158     schwarze  712:
                    713:                /*
                    714:                 * No vertical space before the first section
                    715:                 * and after an empty section.
                    716:                 */
                    717:
                    718:                do {
                    719:                        n = n->prev;
1.209     schwarze  720:                } while (n != NULL && n->tok >= MAN_TH &&
1.214     schwarze  721:                    man_term_act(n->tok)->flags & MAN_NOTEXT);
1.210     schwarze  722:                if (n == NULL || n->type == ROFFT_COMMENT ||
                    723:                    (n->tok == MAN_SH && n->body->child == NULL))
1.61      kristaps  724:                        break;
1.158     schwarze  725:
1.134     schwarze  726:                for (i = 0; i < mt->pardist; i++)
                    727:                        term_vspace(p);
1.19      kristaps  728:                break;
1.171     schwarze  729:        case ROFFT_HEAD:
1.52      kristaps  730:                term_fontrepl(p, TERMFONT_BOLD);
1.203     schwarze  731:                p->tcol->offset = 0;
                    732:                p->tcol->rmargin = mt->offset;
1.176     schwarze  733:                p->trailspace = mt->offset;
                    734:                p->flags |= TERMP_NOBREAK | TERMP_BRIND;
1.19      kristaps  735:                break;
1.171     schwarze  736:        case ROFFT_BODY:
1.203     schwarze  737:                p->tcol->offset = mt->offset;
                    738:                p->tcol->rmargin = p->maxrmargin;
1.176     schwarze  739:                p->trailspace = 0;
                    740:                p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
1.19      kristaps  741:                break;
                    742:        default:
                    743:                break;
                    744:        }
1.1       kristaps  745:
1.185     schwarze  746:        return 1;
1.1       kristaps  747: }
                    748:
                    749: static void
                    750: post_SH(DECL_ARGS)
                    751: {
1.146     schwarze  752:
1.19      kristaps  753:        switch (n->type) {
1.171     schwarze  754:        case ROFFT_HEAD:
1.19      kristaps  755:                term_newln(p);
                    756:                break;
1.171     schwarze  757:        case ROFFT_BODY:
1.19      kristaps  758:                term_newln(p);
                    759:                break;
                    760:        default:
                    761:                break;
                    762:        }
1.1       kristaps  763: }
                    764:
1.26      kristaps  765: static int
                    766: pre_RS(DECL_ARGS)
                    767: {
1.163     schwarze  768:        struct roffsu    su;
1.26      kristaps  769:
                    770:        switch (n->type) {
1.171     schwarze  771:        case ROFFT_BLOCK:
1.26      kristaps  772:                term_newln(p);
1.185     schwarze  773:                return 1;
1.171     schwarze  774:        case ROFFT_HEAD:
1.185     schwarze  775:                return 0;
1.26      kristaps  776:        default:
                    777:                break;
                    778:        }
                    779:
1.165     schwarze  780:        n = n->parent->head;
                    781:        n->aux = SHRT_MAX + 1;
1.177     schwarze  782:        if (n->child == NULL)
                    783:                n->aux = mt->lmargin[mt->lmargincur];
1.204     schwarze  784:        else if (a2roffsu(n->child->string, &su, SCALE_EN) != NULL)
1.205     schwarze  785:                n->aux = term_hen(p, &su);
1.165     schwarze  786:        if (n->aux < 0 && (size_t)(-n->aux) > mt->offset)
                    787:                n->aux = -mt->offset;
                    788:        else if (n->aux > SHRT_MAX)
                    789:                n->aux = term_len(p, p->defindent);
1.26      kristaps  790:
1.165     schwarze  791:        mt->offset += n->aux;
1.203     schwarze  792:        p->tcol->offset = mt->offset;
                    793:        p->tcol->rmargin = p->maxrmargin;
1.26      kristaps  794:
1.116     schwarze  795:        if (++mt->lmarginsz < MAXMARGINS)
                    796:                mt->lmargincur = mt->lmarginsz;
                    797:
1.178     schwarze  798:        mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
1.185     schwarze  799:        return 1;
1.26      kristaps  800: }
                    801:
                    802: static void
                    803: post_RS(DECL_ARGS)
                    804: {
                    805:
                    806:        switch (n->type) {
1.171     schwarze  807:        case ROFFT_BLOCK:
1.110     kristaps  808:                return;
1.171     schwarze  809:        case ROFFT_HEAD:
1.110     kristaps  810:                return;
1.26      kristaps  811:        default:
                    812:                term_newln(p);
                    813:                break;
                    814:        }
1.110     kristaps  815:
1.165     schwarze  816:        mt->offset -= n->parent->head->aux;
1.203     schwarze  817:        p->tcol->offset = mt->offset;
1.116     schwarze  818:
                    819:        if (--mt->lmarginsz < MAXMARGINS)
                    820:                mt->lmargincur = mt->lmarginsz;
1.215     schwarze  821: }
                    822:
                    823: static int
                    824: pre_SY(DECL_ARGS)
                    825: {
                    826:        const struct roff_node  *nn;
                    827:        int                      len;
                    828:
                    829:        switch (n->type) {
                    830:        case ROFFT_BLOCK:
1.217     schwarze  831:                if (n->prev == NULL || n->prev->tok != MAN_SY)
                    832:                        print_bvspace(p, n, mt->pardist);
1.215     schwarze  833:                return 1;
                    834:        case ROFFT_HEAD:
                    835:        case ROFFT_BODY:
                    836:                break;
                    837:        default:
                    838:                abort();
                    839:        }
                    840:
                    841:        nn = n->parent->head->child;
1.219     schwarze  842:        len = nn == NULL ? 1 : term_strlen(p, nn->string) + 1;
1.215     schwarze  843:
                    844:        switch (n->type) {
                    845:        case ROFFT_HEAD:
                    846:                p->tcol->offset = mt->offset;
                    847:                p->tcol->rmargin = mt->offset + len;
                    848:                p->flags |= TERMP_NOBREAK;
                    849:                term_fontrepl(p, TERMFONT_BOLD);
                    850:                break;
                    851:        case ROFFT_BODY:
                    852:                mt->lmargin[mt->lmargincur] = len;
                    853:                p->tcol->offset = mt->offset + len;
                    854:                p->tcol->rmargin = p->maxrmargin;
                    855:                p->flags |= TERMP_NOSPACE;
                    856:                break;
                    857:        default:
                    858:                abort();
                    859:        }
                    860:        return 1;
                    861: }
                    862:
                    863: static void
                    864: post_SY(DECL_ARGS)
                    865: {
                    866:        switch (n->type) {
                    867:        case ROFFT_HEAD:
                    868:                term_flushln(p);
                    869:                p->flags &= ~TERMP_NOBREAK;
                    870:                break;
                    871:        case ROFFT_BODY:
                    872:                term_newln(p);
                    873:                p->tcol->offset = mt->offset;
                    874:                break;
                    875:        default:
                    876:                break;
                    877:        }
1.137     schwarze  878: }
                    879:
                    880: static int
                    881: pre_UR(DECL_ARGS)
                    882: {
                    883:
1.185     schwarze  884:        return n->type != ROFFT_HEAD;
1.137     schwarze  885: }
                    886:
                    887: static void
                    888: post_UR(DECL_ARGS)
                    889: {
                    890:
1.171     schwarze  891:        if (n->type != ROFFT_BLOCK)
1.137     schwarze  892:                return;
                    893:
                    894:        term_word(p, "<");
                    895:        p->flags |= TERMP_NOSPACE;
                    896:
                    897:        if (NULL != n->child->child)
                    898:                print_man_node(p, mt, n->child->child, meta);
                    899:
                    900:        p->flags |= TERMP_NOSPACE;
                    901:        term_word(p, ">");
1.26      kristaps  902: }
                    903:
1.1       kristaps  904: static void
1.45      kristaps  905: print_man_node(DECL_ARGS)
1.1       kristaps  906: {
1.214     schwarze  907:        const struct man_term_act *act;
                    908:        int c;
1.1       kristaps  909:
                    910:        switch (n->type) {
1.171     schwarze  911:        case ROFFT_TEXT:
1.97      kristaps  912:                /*
                    913:                 * If we have a blank line, output a vertical space.
                    914:                 * If we have a space as the first character, break
                    915:                 * before printing the line's data.
                    916:                 */
1.200     schwarze  917:                if (*n->string == '\0') {
1.207     schwarze  918:                        if (p->flags & TERMP_NONEWLINE)
                    919:                                term_newln(p);
                    920:                        else
                    921:                                term_vspace(p);
1.98      schwarze  922:                        return;
1.200     schwarze  923:                } else if (*n->string == ' ' && n->flags & NODE_LINE &&
                    924:                    (p->flags & TERMP_NONEWLINE) == 0)
1.96      kristaps  925:                        term_newln(p);
1.212     schwarze  926:                else if (n->flags & NODE_DELIMC)
                    927:                        p->flags |= TERMP_NOSPACE;
1.54      kristaps  928:
1.4       kristaps  929:                term_word(p, n->string);
1.130     schwarze  930:                goto out;
1.210     schwarze  931:        case ROFFT_COMMENT:
                    932:                return;
1.171     schwarze  933:        case ROFFT_EQN:
1.188     schwarze  934:                if ( ! (n->flags & NODE_LINE))
1.152     schwarze  935:                        p->flags |= TERMP_NOSPACE;
1.115     kristaps  936:                term_eqn(p, n->eqn);
1.188     schwarze  937:                if (n->next != NULL && ! (n->next->flags & NODE_LINE))
1.153     schwarze  938:                        p->flags |= TERMP_NOSPACE;
1.97      kristaps  939:                return;
1.171     schwarze  940:        case ROFFT_TBL:
1.169     schwarze  941:                if (p->tbl.cols == NULL)
                    942:                        term_vspace(p);
1.92      kristaps  943:                term_tbl(p, n->span);
1.97      kristaps  944:                return;
1.1       kristaps  945:        default:
                    946:                break;
                    947:        }
1.224     schwarze  948:
1.193     schwarze  949:        if (n->tok < ROFF_MAX) {
1.194     schwarze  950:                roff_term_pre(p, n);
1.193     schwarze  951:                return;
                    952:        }
                    953:
1.214     schwarze  954:        act = man_term_act(n->tok);
1.220     schwarze  955:        if ((act->flags & MAN_NOTEXT) == 0 && n->tok != MAN_SM)
1.97      kristaps  956:                term_fontrepl(p, TERMFONT_NONE);
                    957:
                    958:        c = 1;
1.214     schwarze  959:        if (act->pre != NULL)
                    960:                c = (*act->pre)(p, mt, n, meta);
1.97      kristaps  961:
1.1       kristaps  962:        if (c && n->child)
1.135     schwarze  963:                print_man_nodelist(p, mt, n->child, meta);
1.1       kristaps  964:
1.214     schwarze  965:        if (act->post != NULL)
                    966:                (*act->post)(p, mt, n, meta);
1.220     schwarze  967:        if ((act->flags & MAN_NOTEXT) == 0 && n->tok != MAN_SM)
1.97      kristaps  968:                term_fontrepl(p, TERMFONT_NONE);
1.63      kristaps  969:
1.130     schwarze  970: out:
                    971:        /*
                    972:         * If we're in a literal context, make sure that words
                    973:         * together on the same line stay together.  This is a
                    974:         * POST-printing call, so we check the NEXT word.  Since
                    975:         * -man doesn't have nested macros, we don't need to be
                    976:         * more specific than this.
                    977:         */
1.225   ! schwarze  978:        if (n->flags & NODE_NOFILL &&
1.157     schwarze  979:            ! (p->flags & (TERMP_NOBREAK | TERMP_NONEWLINE)) &&
1.188     schwarze  980:            (n->next == NULL || n->next->flags & NODE_LINE)) {
1.202     schwarze  981:                p->flags |= TERMP_BRNEVER | TERMP_NOSPACE;
1.157     schwarze  982:                if (n->string != NULL && *n->string != '\0')
1.130     schwarze  983:                        term_flushln(p);
                    984:                else
                    985:                        term_newln(p);
1.202     schwarze  986:                p->flags &= ~TERMP_BRNEVER;
1.203     schwarze  987:                if (p->tcol->rmargin < p->maxrmargin &&
                    988:                    n->parent->tok == MAN_HP) {
                    989:                        p->tcol->offset = p->tcol->rmargin;
                    990:                        p->tcol->rmargin = p->maxrmargin;
1.202     schwarze  991:                }
1.130     schwarze  992:        }
1.188     schwarze  993:        if (NODE_EOS & n->flags)
1.63      kristaps  994:                p->flags |= TERMP_SENTENCE;
1.1       kristaps  995: }
                    996:
                    997:
                    998: static void
1.52      kristaps  999: print_man_nodelist(DECL_ARGS)
1.1       kristaps 1000: {
1.19      kristaps 1001:
1.168     schwarze 1002:        while (n != NULL) {
                   1003:                print_man_node(p, mt, n, meta);
                   1004:                n = n->next;
                   1005:        }
1.1       kristaps 1006: }
                   1007:
1.31      kristaps 1008: static void
1.173     schwarze 1009: print_man_foot(struct termp *p, const struct roff_meta *meta)
1.1       kristaps 1010: {
1.147     schwarze 1011:        char                    *title;
1.156     schwarze 1012:        size_t                   datelen, titlen;
1.73      kristaps 1013:
1.125     schwarze 1014:        assert(meta->title);
                   1015:        assert(meta->msec);
                   1016:        assert(meta->date);
1.1       kristaps 1017:
1.52      kristaps 1018:        term_fontrepl(p, TERMFONT_NONE);
1.50      kristaps 1019:
1.149     schwarze 1020:        if (meta->hasbody)
                   1021:                term_vspace(p);
1.126     schwarze 1022:
                   1023:        /*
                   1024:         * Temporary, undocumented option to imitate mdoc(7) output.
1.173     schwarze 1025:         * In the bottom right corner, use the operating system
                   1026:         * instead of the title.
1.126     schwarze 1027:         */
                   1028:
                   1029:        if ( ! p->mdocstyle) {
1.149     schwarze 1030:                if (meta->hasbody) {
                   1031:                        term_vspace(p);
                   1032:                        term_vspace(p);
                   1033:                }
1.147     schwarze 1034:                mandoc_asprintf(&title, "%s(%s)",
                   1035:                    meta->title, meta->msec);
1.173     schwarze 1036:        } else if (meta->os) {
                   1037:                title = mandoc_strdup(meta->os);
1.126     schwarze 1038:        } else {
1.147     schwarze 1039:                title = mandoc_strdup("");
1.126     schwarze 1040:        }
1.125     schwarze 1041:        datelen = term_strlen(p, meta->date);
1.1       kristaps 1042:
1.173     schwarze 1043:        /* Bottom left corner: operating system. */
1.126     schwarze 1044:
1.1       kristaps 1045:        p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
1.139     schwarze 1046:        p->trailspace = 1;
1.203     schwarze 1047:        p->tcol->offset = 0;
                   1048:        p->tcol->rmargin = p->maxrmargin > datelen ?
1.156     schwarze 1049:            (p->maxrmargin + term_len(p, 1) - datelen) / 2 : 0;
1.1       kristaps 1050:
1.173     schwarze 1051:        if (meta->os)
                   1052:                term_word(p, meta->os);
1.1       kristaps 1053:        term_flushln(p);
                   1054:
1.126     schwarze 1055:        /* At the bottom in the middle: manual date. */
                   1056:
1.203     schwarze 1057:        p->tcol->offset = p->tcol->rmargin;
1.156     schwarze 1058:        titlen = term_strlen(p, title);
1.203     schwarze 1059:        p->tcol->rmargin = p->maxrmargin > titlen ?
                   1060:            p->maxrmargin - titlen : 0;
1.117     schwarze 1061:        p->flags |= TERMP_NOSPACE;
1.123     schwarze 1062:
1.125     schwarze 1063:        term_word(p, meta->date);
1.123     schwarze 1064:        term_flushln(p);
                   1065:
1.126     schwarze 1066:        /* Bottom right corner: manual title and section. */
                   1067:
1.123     schwarze 1068:        p->flags &= ~TERMP_NOBREAK;
                   1069:        p->flags |= TERMP_NOSPACE;
1.139     schwarze 1070:        p->trailspace = 0;
1.203     schwarze 1071:        p->tcol->offset = p->tcol->rmargin;
                   1072:        p->tcol->rmargin = p->maxrmargin;
1.1       kristaps 1073:
1.123     schwarze 1074:        term_word(p, title);
1.1       kristaps 1075:        term_flushln(p);
1.211     schwarze 1076:
                   1077:        /*
                   1078:         * Reset the terminal state for more output after the footer:
                   1079:         * Some output modes, in particular PostScript and PDF, print
                   1080:         * the header and the footer into a buffer such that it can be
                   1081:         * reused for multiple output pages, then go on to format the
                   1082:         * main text.
                   1083:         */
                   1084:
                   1085:         p->tcol->offset = 0;
                   1086:         p->flags = 0;
                   1087:
1.147     schwarze 1088:        free(title);
1.1       kristaps 1089: }
                   1090:
1.31      kristaps 1091: static void
1.173     schwarze 1092: print_man_head(struct termp *p, const struct roff_meta *meta)
1.1       kristaps 1093: {
1.148     schwarze 1094:        const char              *volume;
1.147     schwarze 1095:        char                    *title;
1.148     schwarze 1096:        size_t                   vollen, titlen;
1.73      kristaps 1097:
1.135     schwarze 1098:        assert(meta->title);
                   1099:        assert(meta->msec);
1.1       kristaps 1100:
1.148     schwarze 1101:        volume = NULL == meta->vol ? "" : meta->vol;
                   1102:        vollen = term_strlen(p, volume);
1.1       kristaps 1103:
1.126     schwarze 1104:        /* Top left corner: manual title and section. */
                   1105:
1.147     schwarze 1106:        mandoc_asprintf(&title, "%s(%s)", meta->title, meta->msec);
1.77      kristaps 1107:        titlen = term_strlen(p, title);
1.1       kristaps 1108:
1.118     schwarze 1109:        p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
1.139     schwarze 1110:        p->trailspace = 1;
1.203     schwarze 1111:        p->tcol->offset = 0;
                   1112:        p->tcol->rmargin = 2 * (titlen+1) + vollen < p->maxrmargin ?
1.148     schwarze 1113:            (p->maxrmargin - vollen + term_len(p, 1)) / 2 :
1.156     schwarze 1114:            vollen < p->maxrmargin ? p->maxrmargin - vollen : 0;
1.1       kristaps 1115:
                   1116:        term_word(p, title);
                   1117:        term_flushln(p);
                   1118:
1.126     schwarze 1119:        /* At the top in the middle: manual volume. */
                   1120:
1.117     schwarze 1121:        p->flags |= TERMP_NOSPACE;
1.203     schwarze 1122:        p->tcol->offset = p->tcol->rmargin;
                   1123:        p->tcol->rmargin = p->tcol->offset + vollen + titlen <
                   1124:            p->maxrmargin ?  p->maxrmargin - titlen : p->maxrmargin;
1.1       kristaps 1125:
1.148     schwarze 1126:        term_word(p, volume);
1.1       kristaps 1127:        term_flushln(p);
                   1128:
1.126     schwarze 1129:        /* Top right corner: title and section, again. */
                   1130:
1.1       kristaps 1131:        p->flags &= ~TERMP_NOBREAK;
1.139     schwarze 1132:        p->trailspace = 0;
1.203     schwarze 1133:        if (p->tcol->rmargin + titlen <= p->maxrmargin) {
1.117     schwarze 1134:                p->flags |= TERMP_NOSPACE;
1.203     schwarze 1135:                p->tcol->offset = p->tcol->rmargin;
                   1136:                p->tcol->rmargin = p->maxrmargin;
1.57      kristaps 1137:                term_word(p, title);
                   1138:                term_flushln(p);
                   1139:        }
1.1       kristaps 1140:
1.118     schwarze 1141:        p->flags &= ~TERMP_NOSPACE;
1.203     schwarze 1142:        p->tcol->offset = 0;
                   1143:        p->tcol->rmargin = p->maxrmargin;
1.61      kristaps 1144:
1.146     schwarze 1145:        /*
1.126     schwarze 1146:         * Groff prints three blank lines before the content.
                   1147:         * Do the same, except in the temporary, undocumented
                   1148:         * mode imitating mdoc(7) output.
1.62      kristaps 1149:         */
                   1150:
1.61      kristaps 1151:        term_vspace(p);
1.126     schwarze 1152:        if ( ! p->mdocstyle) {
                   1153:                term_vspace(p);
                   1154:                term_vspace(p);
                   1155:        }
1.147     schwarze 1156:        free(title);
1.1       kristaps 1157: }

CVSweb