[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.141

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