[BACK]Return to man_term.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/man_term.c, Revision 1.147

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