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

Annotation of mandoc/man_term.c, Revision 1.192

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

CVSweb