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

Annotation of mandoc/man_term.c, Revision 1.160

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

CVSweb