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

Annotation of mandoc/man_term.c, Revision 1.155

1.155   ! schwarze    1: /*     $Id: man_term.c,v 1.154 2014/10/20 15:50:24 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 */
                    126:        { pre_ign, NULL, 0 }, /* 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.77      kristaps  204:        if ( ! a2roffsu(cp, &su, SCALE_BU))
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: {
                    291:
                    292:        n = n->child;
                    293:        if (0 == n) {
                    294:                mt->pardist = 1;
                    295:                return(0);
                    296:        }
                    297:        assert(MAN_TEXT == n->type);
                    298:        mt->pardist = atoi(n->string);
                    299:        return(0);
                    300: }
                    301:
                    302: static int
1.88      kristaps  303: pre_alternate(DECL_ARGS)
1.3       kristaps  304: {
1.88      kristaps  305:        enum termfont            font[2];
                    306:        const struct man_node   *nn;
                    307:        int                      savelit, i;
1.3       kristaps  308:
1.88      kristaps  309:        switch (n->tok) {
1.146     schwarze  310:        case MAN_RB:
1.88      kristaps  311:                font[0] = TERMFONT_NONE;
                    312:                font[1] = TERMFONT_BOLD;
                    313:                break;
1.146     schwarze  314:        case MAN_RI:
1.88      kristaps  315:                font[0] = TERMFONT_NONE;
                    316:                font[1] = TERMFONT_UNDER;
                    317:                break;
1.146     schwarze  318:        case MAN_BR:
1.88      kristaps  319:                font[0] = TERMFONT_BOLD;
                    320:                font[1] = TERMFONT_NONE;
                    321:                break;
1.146     schwarze  322:        case MAN_BI:
1.88      kristaps  323:                font[0] = TERMFONT_BOLD;
                    324:                font[1] = TERMFONT_UNDER;
                    325:                break;
1.146     schwarze  326:        case MAN_IR:
1.88      kristaps  327:                font[0] = TERMFONT_UNDER;
                    328:                font[1] = TERMFONT_NONE;
                    329:                break;
1.146     schwarze  330:        case MAN_IB:
1.88      kristaps  331:                font[0] = TERMFONT_UNDER;
                    332:                font[1] = TERMFONT_BOLD;
                    333:                break;
                    334:        default:
                    335:                abort();
                    336:        }
1.35      kristaps  337:
1.88      kristaps  338:        savelit = MANT_LITERAL & mt->fl;
                    339:        mt->fl &= ~MANT_LITERAL;
1.35      kristaps  340:
1.88      kristaps  341:        for (i = 0, nn = n->child; nn; nn = nn->next, i = 1 - i) {
                    342:                term_fontrepl(p, font[i]);
                    343:                if (savelit && NULL == nn->next)
                    344:                        mt->fl |= MANT_LITERAL;
1.135     schwarze  345:                print_man_node(p, mt, nn, meta);
1.88      kristaps  346:                if (nn->next)
1.4       kristaps  347:                        p->flags |= TERMP_NOSPACE;
1.3       kristaps  348:        }
                    349:
                    350:        return(0);
                    351: }
                    352:
1.1       kristaps  353: static int
                    354: pre_B(DECL_ARGS)
                    355: {
                    356:
1.52      kristaps  357:        term_fontrepl(p, TERMFONT_BOLD);
1.1       kristaps  358:        return(1);
1.127     kristaps  359: }
                    360:
                    361: static int
                    362: pre_OP(DECL_ARGS)
                    363: {
                    364:
                    365:        term_word(p, "[");
                    366:        p->flags |= TERMP_NOSPACE;
                    367:
                    368:        if (NULL != (n = n->child)) {
                    369:                term_fontrepl(p, TERMFONT_BOLD);
                    370:                term_word(p, n->string);
                    371:        }
                    372:        if (NULL != n && NULL != n->next) {
                    373:                term_fontrepl(p, TERMFONT_UNDER);
                    374:                term_word(p, n->next->string);
                    375:        }
                    376:
                    377:        term_fontrepl(p, TERMFONT_NONE);
                    378:        p->flags |= TERMP_NOSPACE;
                    379:        term_word(p, "]");
                    380:        return(0);
1.89      kristaps  381: }
                    382:
                    383: static int
                    384: pre_ft(DECL_ARGS)
                    385: {
                    386:        const char      *cp;
                    387:
                    388:        if (NULL == n->child) {
                    389:                term_fontlast(p);
                    390:                return(0);
                    391:        }
                    392:
                    393:        cp = n->child->string;
                    394:        switch (*cp) {
1.146     schwarze  395:        case '4':
1.89      kristaps  396:                /* FALLTHROUGH */
1.146     schwarze  397:        case '3':
1.89      kristaps  398:                /* FALLTHROUGH */
1.146     schwarze  399:        case 'B':
1.89      kristaps  400:                term_fontrepl(p, TERMFONT_BOLD);
                    401:                break;
1.146     schwarze  402:        case '2':
1.89      kristaps  403:                /* FALLTHROUGH */
1.146     schwarze  404:        case 'I':
1.89      kristaps  405:                term_fontrepl(p, TERMFONT_UNDER);
                    406:                break;
1.146     schwarze  407:        case 'P':
1.89      kristaps  408:                term_fontlast(p);
                    409:                break;
1.146     schwarze  410:        case '1':
1.89      kristaps  411:                /* FALLTHROUGH */
1.146     schwarze  412:        case 'C':
1.89      kristaps  413:                /* FALLTHROUGH */
1.146     schwarze  414:        case 'R':
1.89      kristaps  415:                term_fontrepl(p, TERMFONT_NONE);
                    416:                break;
                    417:        default:
                    418:                break;
                    419:        }
                    420:        return(0);
1.83      kristaps  421: }
                    422:
                    423: static int
                    424: pre_in(DECL_ARGS)
                    425: {
                    426:        int              len, less;
                    427:        size_t           v;
                    428:        const char      *cp;
                    429:
                    430:        term_newln(p);
                    431:
                    432:        if (NULL == n->child) {
                    433:                p->offset = mt->offset;
                    434:                return(0);
                    435:        }
                    436:
                    437:        cp = n->child->string;
                    438:        less = 0;
                    439:
                    440:        if ('-' == *cp)
                    441:                less = -1;
                    442:        else if ('+' == *cp)
                    443:                less = 1;
                    444:        else
                    445:                cp--;
                    446:
                    447:        if ((len = a2width(p, ++cp)) < 0)
                    448:                return(0);
                    449:
                    450:        v = (size_t)len;
                    451:
                    452:        if (less < 0)
                    453:                p->offset -= p->offset > v ? v : p->offset;
                    454:        else if (less > 0)
                    455:                p->offset += v;
1.146     schwarze  456:        else
1.83      kristaps  457:                p->offset = v;
1.95      kristaps  458:
                    459:        /* Don't let this creep beyond the right margin. */
                    460:
                    461:        if (p->offset > p->rmargin)
                    462:                p->offset = p->rmargin;
1.83      kristaps  463:
                    464:        return(0);
1.1       kristaps  465: }
                    466:
                    467: static int
1.19      kristaps  468: pre_sp(DECL_ARGS)
                    469: {
1.131     schwarze  470:        char            *s;
1.77      kristaps  471:        size_t           i, len;
1.131     schwarze  472:        int              neg;
1.112     kristaps  473:
                    474:        if ((NULL == n->prev && n->parent)) {
1.132     schwarze  475:                switch (n->parent->tok) {
1.146     schwarze  476:                case MAN_SH:
1.132     schwarze  477:                        /* FALLTHROUGH */
1.146     schwarze  478:                case MAN_SS:
1.132     schwarze  479:                        /* FALLTHROUGH */
1.146     schwarze  480:                case MAN_PP:
1.132     schwarze  481:                        /* FALLTHROUGH */
1.146     schwarze  482:                case MAN_LP:
1.132     schwarze  483:                        /* FALLTHROUGH */
1.146     schwarze  484:                case MAN_P:
1.132     schwarze  485:                        /* FALLTHROUGH */
1.112     kristaps  486:                        return(0);
1.132     schwarze  487:                default:
                    488:                        break;
                    489:                }
1.112     kristaps  490:        }
1.19      kristaps  491:
1.131     schwarze  492:        neg = 0;
1.82      kristaps  493:        switch (n->tok) {
1.146     schwarze  494:        case MAN_br:
1.82      kristaps  495:                len = 0;
                    496:                break;
                    497:        default:
1.131     schwarze  498:                if (NULL == n->child) {
                    499:                        len = 1;
                    500:                        break;
                    501:                }
                    502:                s = n->child->string;
                    503:                if ('-' == *s) {
                    504:                        neg = 1;
                    505:                        s++;
                    506:                }
                    507:                len = a2height(p, s);
1.82      kristaps  508:                break;
                    509:        }
1.38      kristaps  510:
1.19      kristaps  511:        if (0 == len)
                    512:                term_newln(p);
1.131     schwarze  513:        else if (neg)
                    514:                p->skipvsp += len;
                    515:        else
                    516:                for (i = 0; i < len; i++)
                    517:                        term_vspace(p);
1.19      kristaps  518:
1.15      kristaps  519:        return(0);
                    520: }
                    521:
                    522: static int
1.19      kristaps  523: pre_HP(DECL_ARGS)
                    524: {
1.117     schwarze  525:        size_t                   len, one;
1.24      kristaps  526:        int                      ival;
                    527:        const struct man_node   *nn;
1.19      kristaps  528:
1.20      kristaps  529:        switch (n->type) {
1.146     schwarze  530:        case MAN_BLOCK:
1.134     schwarze  531:                print_bvspace(p, n, mt->pardist);
1.24      kristaps  532:                return(1);
1.146     schwarze  533:        case MAN_BODY:
1.20      kristaps  534:                break;
                    535:        default:
                    536:                return(0);
                    537:        }
                    538:
1.130     schwarze  539:        if ( ! (MANT_LITERAL & mt->fl)) {
1.145     schwarze  540:                p->flags |= TERMP_NOBREAK | TERMP_BRIND;
1.139     schwarze  541:                p->trailspace = 2;
1.130     schwarze  542:        }
                    543:
1.116     schwarze  544:        len = mt->lmargin[mt->lmargincur];
1.24      kristaps  545:        ival = -1;
                    546:
                    547:        /* Calculate offset. */
                    548:
1.39      kristaps  549:        if (NULL != (nn = n->parent->head->child))
1.77      kristaps  550:                if ((ival = a2width(p, nn->string)) >= 0)
1.24      kristaps  551:                        len = (size_t)ival;
                    552:
1.117     schwarze  553:        one = term_len(p, 1);
1.121     schwarze  554:        if (len < one)
1.117     schwarze  555:                len = one;
1.24      kristaps  556:
1.26      kristaps  557:        p->offset = mt->offset;
                    558:        p->rmargin = mt->offset + len;
1.24      kristaps  559:
                    560:        if (ival >= 0)
1.116     schwarze  561:                mt->lmargin[mt->lmargincur] = (size_t)ival;
1.24      kristaps  562:
1.19      kristaps  563:        return(1);
                    564: }
                    565:
1.20      kristaps  566: static void
                    567: post_HP(DECL_ARGS)
                    568: {
                    569:
                    570:        switch (n->type) {
1.146     schwarze  571:        case MAN_BODY:
1.136     schwarze  572:                term_newln(p);
1.145     schwarze  573:                p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
1.139     schwarze  574:                p->trailspace = 0;
1.26      kristaps  575:                p->offset = mt->offset;
1.20      kristaps  576:                p->rmargin = p->maxrmargin;
                    577:                break;
                    578:        default:
                    579:                break;
                    580:        }
                    581: }
                    582:
1.19      kristaps  583: static int
1.1       kristaps  584: pre_PP(DECL_ARGS)
                    585: {
                    586:
1.19      kristaps  587:        switch (n->type) {
1.146     schwarze  588:        case MAN_BLOCK:
1.122     schwarze  589:                mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
1.134     schwarze  590:                print_bvspace(p, n, mt->pardist);
1.19      kristaps  591:                break;
                    592:        default:
1.26      kristaps  593:                p->offset = mt->offset;
1.19      kristaps  594:                break;
                    595:        }
                    596:
1.87      kristaps  597:        return(MAN_HEAD != n->type);
1.1       kristaps  598: }
                    599:
                    600: static int
1.4       kristaps  601: pre_IP(DECL_ARGS)
                    602: {
1.22      kristaps  603:        const struct man_node   *nn;
                    604:        size_t                   len;
1.94      schwarze  605:        int                      savelit, ival;
1.18      kristaps  606:
1.22      kristaps  607:        switch (n->type) {
1.146     schwarze  608:        case MAN_BODY:
1.22      kristaps  609:                p->flags |= TERMP_NOSPACE;
                    610:                break;
1.146     schwarze  611:        case MAN_HEAD:
1.22      kristaps  612:                p->flags |= TERMP_NOBREAK;
1.139     schwarze  613:                p->trailspace = 1;
1.22      kristaps  614:                break;
1.146     schwarze  615:        case MAN_BLOCK:
1.134     schwarze  616:                print_bvspace(p, n, mt->pardist);
1.23      kristaps  617:                /* FALLTHROUGH */
1.22      kristaps  618:        default:
                    619:                return(1);
                    620:        }
1.18      kristaps  621:
1.116     schwarze  622:        len = mt->lmargin[mt->lmargincur];
1.22      kristaps  623:        ival = -1;
1.4       kristaps  624:
1.94      schwarze  625:        /* Calculate the offset from the optional second argument. */
1.22      kristaps  626:        if (NULL != (nn = n->parent->head->child))
1.94      schwarze  627:                if (NULL != (nn = nn->next))
1.77      kristaps  628:                        if ((ival = a2width(p, nn->string)) >= 0)
1.22      kristaps  629:                                len = (size_t)ival;
1.4       kristaps  630:
1.22      kristaps  631:        switch (n->type) {
1.146     schwarze  632:        case MAN_HEAD:
1.23      kristaps  633:                /* Handle zero-width lengths. */
                    634:                if (0 == len)
1.77      kristaps  635:                        len = term_len(p, 1);
1.23      kristaps  636:
1.26      kristaps  637:                p->offset = mt->offset;
                    638:                p->rmargin = mt->offset + len;
1.22      kristaps  639:                if (ival < 0)
                    640:                        break;
1.18      kristaps  641:
1.24      kristaps  642:                /* Set the saved left-margin. */
1.116     schwarze  643:                mt->lmargin[mt->lmargincur] = (size_t)ival;
1.24      kristaps  644:
1.94      schwarze  645:                savelit = MANT_LITERAL & mt->fl;
                    646:                mt->fl &= ~MANT_LITERAL;
                    647:
                    648:                if (n->child)
1.135     schwarze  649:                        print_man_node(p, mt, n->child, meta);
1.94      schwarze  650:
                    651:                if (savelit)
                    652:                        mt->fl |= MANT_LITERAL;
                    653:
1.22      kristaps  654:                return(0);
1.146     schwarze  655:        case MAN_BODY:
1.26      kristaps  656:                p->offset = mt->offset + len;
1.140     schwarze  657:                p->rmargin = p->maxrmargin > p->offset ?
1.146     schwarze  658:                    p->maxrmargin : p->offset;
1.23      kristaps  659:                break;
1.22      kristaps  660:        default:
                    661:                break;
1.18      kristaps  662:        }
                    663:
1.22      kristaps  664:        return(1);
                    665: }
1.18      kristaps  666:
1.22      kristaps  667: static void
                    668: post_IP(DECL_ARGS)
                    669: {
1.4       kristaps  670:
1.22      kristaps  671:        switch (n->type) {
1.146     schwarze  672:        case MAN_HEAD:
1.22      kristaps  673:                term_flushln(p);
                    674:                p->flags &= ~TERMP_NOBREAK;
1.139     schwarze  675:                p->trailspace = 0;
1.22      kristaps  676:                p->rmargin = p->maxrmargin;
                    677:                break;
1.146     schwarze  678:        case MAN_BODY:
1.94      schwarze  679:                term_newln(p);
1.138     schwarze  680:                p->offset = mt->offset;
1.22      kristaps  681:                break;
                    682:        default:
                    683:                break;
                    684:        }
1.4       kristaps  685: }
                    686:
                    687: static int
1.1       kristaps  688: pre_TP(DECL_ARGS)
                    689: {
1.23      kristaps  690:        const struct man_node   *nn;
                    691:        size_t                   len;
1.94      schwarze  692:        int                      savelit, ival;
1.1       kristaps  693:
1.21      kristaps  694:        switch (n->type) {
1.146     schwarze  695:        case MAN_HEAD:
1.21      kristaps  696:                p->flags |= TERMP_NOBREAK;
1.139     schwarze  697:                p->trailspace = 1;
1.21      kristaps  698:                break;
1.146     schwarze  699:        case MAN_BODY:
1.21      kristaps  700:                p->flags |= TERMP_NOSPACE;
1.23      kristaps  701:                break;
1.146     schwarze  702:        case MAN_BLOCK:
1.134     schwarze  703:                print_bvspace(p, n, mt->pardist);
1.23      kristaps  704:                /* FALLTHROUGH */
                    705:        default:
                    706:                return(1);
                    707:        }
                    708:
1.116     schwarze  709:        len = (size_t)mt->lmargin[mt->lmargincur];
1.23      kristaps  710:        ival = -1;
                    711:
                    712:        /* Calculate offset. */
                    713:
1.116     schwarze  714:        if (NULL != (nn = n->parent->head->child))
1.141     schwarze  715:                if (nn->string && 0 == (MAN_LINE & nn->flags))
1.77      kristaps  716:                        if ((ival = a2width(p, nn->string)) >= 0)
1.23      kristaps  717:                                len = (size_t)ival;
                    718:
                    719:        switch (n->type) {
1.146     schwarze  720:        case MAN_HEAD:
1.23      kristaps  721:                /* Handle zero-length properly. */
                    722:                if (0 == len)
1.77      kristaps  723:                        len = term_len(p, 1);
1.23      kristaps  724:
1.26      kristaps  725:                p->offset = mt->offset;
                    726:                p->rmargin = mt->offset + len;
1.23      kristaps  727:
1.94      schwarze  728:                savelit = MANT_LITERAL & mt->fl;
                    729:                mt->fl &= ~MANT_LITERAL;
                    730:
1.23      kristaps  731:                /* Don't print same-line elements. */
1.141     schwarze  732:                nn = n->child;
                    733:                while (NULL != nn && 0 == (MAN_LINE & nn->flags))
                    734:                        nn = nn->next;
                    735:
                    736:                while (NULL != nn) {
                    737:                        print_man_node(p, mt, nn, meta);
                    738:                        nn = nn->next;
                    739:                }
1.24      kristaps  740:
1.94      schwarze  741:                if (savelit)
                    742:                        mt->fl |= MANT_LITERAL;
1.24      kristaps  743:                if (ival >= 0)
1.116     schwarze  744:                        mt->lmargin[mt->lmargincur] = (size_t)ival;
1.24      kristaps  745:
1.23      kristaps  746:                return(0);
1.146     schwarze  747:        case MAN_BODY:
1.26      kristaps  748:                p->offset = mt->offset + len;
1.140     schwarze  749:                p->rmargin = p->maxrmargin > p->offset ?
1.146     schwarze  750:                    p->maxrmargin : p->offset;
1.139     schwarze  751:                p->trailspace = 0;
1.130     schwarze  752:                p->flags &= ~TERMP_NOBREAK;
1.21      kristaps  753:                break;
                    754:        default:
                    755:                break;
                    756:        }
1.16      kristaps  757:
1.21      kristaps  758:        return(1);
                    759: }
1.1       kristaps  760:
1.21      kristaps  761: static void
                    762: post_TP(DECL_ARGS)
                    763: {
1.1       kristaps  764:
1.21      kristaps  765:        switch (n->type) {
1.146     schwarze  766:        case MAN_HEAD:
1.21      kristaps  767:                term_flushln(p);
                    768:                break;
1.146     schwarze  769:        case MAN_BODY:
1.94      schwarze  770:                term_newln(p);
1.138     schwarze  771:                p->offset = mt->offset;
1.21      kristaps  772:                break;
                    773:        default:
                    774:                break;
                    775:        }
1.1       kristaps  776: }
                    777:
                    778: static int
                    779: pre_SS(DECL_ARGS)
                    780: {
1.134     schwarze  781:        int      i;
1.1       kristaps  782:
1.19      kristaps  783:        switch (n->type) {
1.146     schwarze  784:        case MAN_BLOCK:
1.113     kristaps  785:                mt->fl &= ~MANT_LITERAL;
1.122     schwarze  786:                mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
                    787:                mt->offset = term_len(p, p->defindent);
1.24      kristaps  788:                /* If following a prior empty `SS', no vspace. */
                    789:                if (n->prev && MAN_SS == n->prev->tok)
                    790:                        if (NULL == n->prev->body->child)
                    791:                                break;
                    792:                if (NULL == n->prev)
                    793:                        break;
1.134     schwarze  794:                for (i = 0; i < mt->pardist; i++)
                    795:                        term_vspace(p);
1.19      kristaps  796:                break;
1.146     schwarze  797:        case MAN_HEAD:
1.52      kristaps  798:                term_fontrepl(p, TERMFONT_BOLD);
1.133     schwarze  799:                p->offset = term_len(p, 3);
1.19      kristaps  800:                break;
1.146     schwarze  801:        case MAN_BODY:
1.26      kristaps  802:                p->offset = mt->offset;
1.24      kristaps  803:                break;
1.19      kristaps  804:        default:
                    805:                break;
                    806:        }
                    807:
1.1       kristaps  808:        return(1);
                    809: }
                    810:
                    811: static void
                    812: post_SS(DECL_ARGS)
                    813: {
1.146     schwarze  814:
1.19      kristaps  815:        switch (n->type) {
1.146     schwarze  816:        case MAN_HEAD:
1.19      kristaps  817:                term_newln(p);
                    818:                break;
1.146     schwarze  819:        case MAN_BODY:
1.24      kristaps  820:                term_newln(p);
                    821:                break;
1.19      kristaps  822:        default:
                    823:                break;
                    824:        }
1.1       kristaps  825: }
                    826:
                    827: static int
                    828: pre_SH(DECL_ARGS)
                    829: {
1.134     schwarze  830:        int      i;
1.22      kristaps  831:
1.19      kristaps  832:        switch (n->type) {
1.146     schwarze  833:        case MAN_BLOCK:
1.113     kristaps  834:                mt->fl &= ~MANT_LITERAL;
1.122     schwarze  835:                mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
                    836:                mt->offset = term_len(p, p->defindent);
1.22      kristaps  837:                /* If following a prior empty `SH', no vspace. */
1.19      kristaps  838:                if (n->prev && MAN_SH == n->prev->tok)
                    839:                        if (NULL == n->prev->body->child)
                    840:                                break;
1.61      kristaps  841:                /* If the first macro, no vspae. */
                    842:                if (NULL == n->prev)
                    843:                        break;
1.134     schwarze  844:                for (i = 0; i < mt->pardist; i++)
                    845:                        term_vspace(p);
1.19      kristaps  846:                break;
1.146     schwarze  847:        case MAN_HEAD:
1.52      kristaps  848:                term_fontrepl(p, TERMFONT_BOLD);
1.19      kristaps  849:                p->offset = 0;
                    850:                break;
1.146     schwarze  851:        case MAN_BODY:
1.26      kristaps  852:                p->offset = mt->offset;
1.19      kristaps  853:                break;
                    854:        default:
                    855:                break;
                    856:        }
1.1       kristaps  857:
                    858:        return(1);
                    859: }
                    860:
                    861: static void
                    862: post_SH(DECL_ARGS)
                    863: {
1.146     schwarze  864:
1.19      kristaps  865:        switch (n->type) {
1.146     schwarze  866:        case MAN_HEAD:
1.19      kristaps  867:                term_newln(p);
                    868:                break;
1.146     schwarze  869:        case MAN_BODY:
1.19      kristaps  870:                term_newln(p);
                    871:                break;
                    872:        default:
                    873:                break;
                    874:        }
1.1       kristaps  875: }
                    876:
1.26      kristaps  877: static int
                    878: pre_RS(DECL_ARGS)
                    879: {
1.110     kristaps  880:        int              ival;
                    881:        size_t           sz;
1.26      kristaps  882:
                    883:        switch (n->type) {
1.146     schwarze  884:        case MAN_BLOCK:
1.26      kristaps  885:                term_newln(p);
                    886:                return(1);
1.146     schwarze  887:        case MAN_HEAD:
1.26      kristaps  888:                return(0);
                    889:        default:
                    890:                break;
                    891:        }
                    892:
1.122     schwarze  893:        sz = term_len(p, p->defindent);
1.26      kristaps  894:
1.110     kristaps  895:        if (NULL != (n = n->parent->head->child))
1.146     schwarze  896:                if ((ival = a2width(p, n->string)) >= 0)
1.110     kristaps  897:                        sz = (size_t)ival;
1.26      kristaps  898:
1.110     kristaps  899:        mt->offset += sz;
1.140     schwarze  900:        p->offset = mt->offset;
                    901:        p->rmargin = p->maxrmargin > p->offset ?
1.146     schwarze  902:            p->maxrmargin : p->offset;
1.26      kristaps  903:
1.116     schwarze  904:        if (++mt->lmarginsz < MAXMARGINS)
                    905:                mt->lmargincur = mt->lmarginsz;
                    906:
                    907:        mt->lmargin[mt->lmargincur] = mt->lmargin[mt->lmargincur - 1];
1.26      kristaps  908:        return(1);
                    909: }
                    910:
                    911: static void
                    912: post_RS(DECL_ARGS)
                    913: {
1.110     kristaps  914:        int              ival;
                    915:        size_t           sz;
1.26      kristaps  916:
                    917:        switch (n->type) {
1.146     schwarze  918:        case MAN_BLOCK:
1.110     kristaps  919:                return;
1.146     schwarze  920:        case MAN_HEAD:
1.110     kristaps  921:                return;
1.26      kristaps  922:        default:
                    923:                term_newln(p);
                    924:                break;
                    925:        }
1.110     kristaps  926:
1.122     schwarze  927:        sz = term_len(p, p->defindent);
1.110     kristaps  928:
1.146     schwarze  929:        if (NULL != (n = n->parent->head->child))
                    930:                if ((ival = a2width(p, n->string)) >= 0)
1.110     kristaps  931:                        sz = (size_t)ival;
                    932:
                    933:        mt->offset = mt->offset < sz ?  0 : mt->offset - sz;
                    934:        p->offset = mt->offset;
1.116     schwarze  935:
                    936:        if (--mt->lmarginsz < MAXMARGINS)
                    937:                mt->lmargincur = mt->lmarginsz;
1.137     schwarze  938: }
                    939:
                    940: static int
                    941: pre_UR(DECL_ARGS)
                    942: {
                    943:
                    944:        return (MAN_HEAD != n->type);
                    945: }
                    946:
                    947: static void
                    948: post_UR(DECL_ARGS)
                    949: {
                    950:
                    951:        if (MAN_BLOCK != n->type)
                    952:                return;
                    953:
                    954:        term_word(p, "<");
                    955:        p->flags |= TERMP_NOSPACE;
                    956:
                    957:        if (NULL != n->child->child)
                    958:                print_man_node(p, mt, n->child->child, meta);
                    959:
                    960:        p->flags |= TERMP_NOSPACE;
                    961:        term_word(p, ">");
1.26      kristaps  962: }
                    963:
1.1       kristaps  964: static void
1.45      kristaps  965: print_man_node(DECL_ARGS)
1.1       kristaps  966: {
1.65      joerg     967:        size_t           rm, rmax;
1.54      kristaps  968:        int              c;
1.1       kristaps  969:
                    970:        switch (n->type) {
1.146     schwarze  971:        case MAN_TEXT:
1.97      kristaps  972:                /*
                    973:                 * If we have a blank line, output a vertical space.
                    974:                 * If we have a space as the first character, break
                    975:                 * before printing the line's data.
                    976:                 */
1.96      kristaps  977:                if ('\0' == *n->string) {
1.4       kristaps  978:                        term_vspace(p);
1.98      schwarze  979:                        return;
1.97      kristaps  980:                } else if (' ' == *n->string && MAN_LINE & n->flags)
1.96      kristaps  981:                        term_newln(p);
1.54      kristaps  982:
1.4       kristaps  983:                term_word(p, n->string);
1.130     schwarze  984:                goto out;
1.52      kristaps  985:
1.146     schwarze  986:        case MAN_EQN:
1.152     schwarze  987:                if ( ! (n->flags & MAN_LINE))
                    988:                        p->flags |= TERMP_NOSPACE;
1.115     kristaps  989:                term_eqn(p, n->eqn);
1.154     schwarze  990:                if (n->next != NULL && ! (n->next->flags & MAN_LINE))
1.153     schwarze  991:                        p->flags |= TERMP_NOSPACE;
1.97      kristaps  992:                return;
1.146     schwarze  993:        case MAN_TBL:
1.97      kristaps  994:                /*
                    995:                 * Tables are preceded by a newline.  Then process a
                    996:                 * table line, which will cause line termination,
                    997:                 */
1.146     schwarze  998:                if (TBL_SPAN_FIRST & n->span->flags)
1.93      kristaps  999:                        term_newln(p);
1.92      kristaps 1000:                term_tbl(p, n->span);
1.97      kristaps 1001:                return;
1.1       kristaps 1002:        default:
                   1003:                break;
                   1004:        }
                   1005:
1.97      kristaps 1006:        if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
                   1007:                term_fontrepl(p, TERMFONT_NONE);
                   1008:
                   1009:        c = 1;
                   1010:        if (termacts[n->tok].pre)
1.135     schwarze 1011:                c = (*termacts[n->tok].pre)(p, mt, n, meta);
1.97      kristaps 1012:
1.1       kristaps 1013:        if (c && n->child)
1.135     schwarze 1014:                print_man_nodelist(p, mt, n->child, meta);
1.1       kristaps 1015:
1.97      kristaps 1016:        if (termacts[n->tok].post)
1.135     schwarze 1017:                (*termacts[n->tok].post)(p, mt, n, meta);
1.97      kristaps 1018:        if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
                   1019:                term_fontrepl(p, TERMFONT_NONE);
1.63      kristaps 1020:
1.130     schwarze 1021: out:
                   1022:        /*
                   1023:         * If we're in a literal context, make sure that words
                   1024:         * together on the same line stay together.  This is a
                   1025:         * POST-printing call, so we check the NEXT word.  Since
                   1026:         * -man doesn't have nested macros, we don't need to be
                   1027:         * more specific than this.
                   1028:         */
                   1029:        if (MANT_LITERAL & mt->fl && ! (TERMP_NOBREAK & p->flags) &&
1.142     schwarze 1030:            (NULL == n->next || MAN_LINE & n->next->flags)) {
1.130     schwarze 1031:                rm = p->rmargin;
                   1032:                rmax = p->maxrmargin;
                   1033:                p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
                   1034:                p->flags |= TERMP_NOSPACE;
                   1035:                if (NULL != n->string && '\0' != *n->string)
                   1036:                        term_flushln(p);
                   1037:                else
                   1038:                        term_newln(p);
                   1039:                if (rm < rmax && n->parent->tok == MAN_HP) {
                   1040:                        p->offset = rm;
                   1041:                        p->rmargin = rmax;
                   1042:                } else
                   1043:                        p->rmargin = rm;
                   1044:                p->maxrmargin = rmax;
                   1045:        }
1.63      kristaps 1046:        if (MAN_EOS & n->flags)
                   1047:                p->flags |= TERMP_SENTENCE;
1.1       kristaps 1048: }
                   1049:
                   1050:
                   1051: static void
1.52      kristaps 1052: print_man_nodelist(DECL_ARGS)
1.1       kristaps 1053: {
1.19      kristaps 1054:
1.135     schwarze 1055:        print_man_node(p, mt, n, meta);
1.1       kristaps 1056:        if ( ! n->next)
                   1057:                return;
1.135     schwarze 1058:        print_man_nodelist(p, mt, n->next, meta);
1.1       kristaps 1059: }
                   1060:
1.31      kristaps 1061: static void
1.73      kristaps 1062: print_man_foot(struct termp *p, const void *arg)
1.1       kristaps 1063: {
1.147     schwarze 1064:        const struct man_meta   *meta;
                   1065:        char                    *title;
                   1066:        size_t                   datelen;
1.73      kristaps 1067:
                   1068:        meta = (const struct man_meta *)arg;
1.125     schwarze 1069:        assert(meta->title);
                   1070:        assert(meta->msec);
                   1071:        assert(meta->date);
1.1       kristaps 1072:
1.52      kristaps 1073:        term_fontrepl(p, TERMFONT_NONE);
1.50      kristaps 1074:
1.149     schwarze 1075:        if (meta->hasbody)
                   1076:                term_vspace(p);
1.126     schwarze 1077:
                   1078:        /*
                   1079:         * Temporary, undocumented option to imitate mdoc(7) output.
                   1080:         * In the bottom right corner, use the source instead of
                   1081:         * the title.
                   1082:         */
                   1083:
                   1084:        if ( ! p->mdocstyle) {
1.149     schwarze 1085:                if (meta->hasbody) {
                   1086:                        term_vspace(p);
                   1087:                        term_vspace(p);
                   1088:                }
1.147     schwarze 1089:                mandoc_asprintf(&title, "%s(%s)",
                   1090:                    meta->title, meta->msec);
1.126     schwarze 1091:        } else if (meta->source) {
1.147     schwarze 1092:                title = mandoc_strdup(meta->source);
1.126     schwarze 1093:        } else {
1.147     schwarze 1094:                title = mandoc_strdup("");
1.126     schwarze 1095:        }
1.125     schwarze 1096:        datelen = term_strlen(p, meta->date);
1.1       kristaps 1097:
1.126     schwarze 1098:        /* Bottom left corner: manual source. */
                   1099:
1.1       kristaps 1100:        p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
1.139     schwarze 1101:        p->trailspace = 1;
1.1       kristaps 1102:        p->offset = 0;
1.123     schwarze 1103:        p->rmargin = (p->maxrmargin - datelen + term_len(p, 1)) / 2;
1.1       kristaps 1104:
                   1105:        if (meta->source)
                   1106:                term_word(p, meta->source);
                   1107:        term_flushln(p);
                   1108:
1.126     schwarze 1109:        /* At the bottom in the middle: manual date. */
                   1110:
1.117     schwarze 1111:        p->flags |= TERMP_NOSPACE;
1.1       kristaps 1112:        p->offset = p->rmargin;
1.123     schwarze 1113:        p->rmargin = p->maxrmargin - term_strlen(p, title);
                   1114:        if (p->offset + datelen >= p->rmargin)
                   1115:                p->rmargin = p->offset + datelen;
                   1116:
1.125     schwarze 1117:        term_word(p, meta->date);
1.123     schwarze 1118:        term_flushln(p);
                   1119:
1.126     schwarze 1120:        /* Bottom right corner: manual title and section. */
                   1121:
1.123     schwarze 1122:        p->flags &= ~TERMP_NOBREAK;
                   1123:        p->flags |= TERMP_NOSPACE;
1.139     schwarze 1124:        p->trailspace = 0;
1.123     schwarze 1125:        p->offset = p->rmargin;
1.1       kristaps 1126:        p->rmargin = p->maxrmargin;
                   1127:
1.123     schwarze 1128:        term_word(p, title);
1.1       kristaps 1129:        term_flushln(p);
1.147     schwarze 1130:        free(title);
1.1       kristaps 1131: }
                   1132:
1.31      kristaps 1133: static void
1.73      kristaps 1134: print_man_head(struct termp *p, const void *arg)
1.1       kristaps 1135: {
1.147     schwarze 1136:        const struct man_meta   *meta;
1.148     schwarze 1137:        const char              *volume;
1.147     schwarze 1138:        char                    *title;
1.148     schwarze 1139:        size_t                   vollen, titlen;
1.73      kristaps 1140:
1.135     schwarze 1141:        meta = (const struct man_meta *)arg;
                   1142:        assert(meta->title);
                   1143:        assert(meta->msec);
1.1       kristaps 1144:
1.148     schwarze 1145:        volume = NULL == meta->vol ? "" : meta->vol;
                   1146:        vollen = term_strlen(p, volume);
1.1       kristaps 1147:
1.126     schwarze 1148:        /* Top left corner: manual title and section. */
                   1149:
1.147     schwarze 1150:        mandoc_asprintf(&title, "%s(%s)", meta->title, meta->msec);
1.77      kristaps 1151:        titlen = term_strlen(p, title);
1.1       kristaps 1152:
1.118     schwarze 1153:        p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
1.139     schwarze 1154:        p->trailspace = 1;
1.1       kristaps 1155:        p->offset = 0;
1.148     schwarze 1156:        p->rmargin = 2 * (titlen+1) + vollen < p->maxrmargin ?
                   1157:            (p->maxrmargin - vollen + term_len(p, 1)) / 2 :
                   1158:            p->maxrmargin - vollen;
1.1       kristaps 1159:
                   1160:        term_word(p, title);
                   1161:        term_flushln(p);
                   1162:
1.126     schwarze 1163:        /* At the top in the middle: manual volume. */
                   1164:
1.117     schwarze 1165:        p->flags |= TERMP_NOSPACE;
1.1       kristaps 1166:        p->offset = p->rmargin;
1.148     schwarze 1167:        p->rmargin = p->offset + vollen + titlen < p->maxrmargin ?
1.57      kristaps 1168:            p->maxrmargin - titlen : p->maxrmargin;
1.1       kristaps 1169:
1.148     schwarze 1170:        term_word(p, volume);
1.1       kristaps 1171:        term_flushln(p);
                   1172:
1.126     schwarze 1173:        /* Top right corner: title and section, again. */
                   1174:
1.1       kristaps 1175:        p->flags &= ~TERMP_NOBREAK;
1.139     schwarze 1176:        p->trailspace = 0;
1.57      kristaps 1177:        if (p->rmargin + titlen <= p->maxrmargin) {
1.117     schwarze 1178:                p->flags |= TERMP_NOSPACE;
1.57      kristaps 1179:                p->offset = p->rmargin;
                   1180:                p->rmargin = p->maxrmargin;
                   1181:                term_word(p, title);
                   1182:                term_flushln(p);
                   1183:        }
1.1       kristaps 1184:
1.118     schwarze 1185:        p->flags &= ~TERMP_NOSPACE;
                   1186:        p->offset = 0;
1.1       kristaps 1187:        p->rmargin = p->maxrmargin;
1.61      kristaps 1188:
1.146     schwarze 1189:        /*
1.126     schwarze 1190:         * Groff prints three blank lines before the content.
                   1191:         * Do the same, except in the temporary, undocumented
                   1192:         * mode imitating mdoc(7) output.
1.62      kristaps 1193:         */
                   1194:
1.61      kristaps 1195:        term_vspace(p);
1.126     schwarze 1196:        if ( ! p->mdocstyle) {
                   1197:                term_vspace(p);
                   1198:                term_vspace(p);
                   1199:        }
1.147     schwarze 1200:        free(title);
1.1       kristaps 1201: }

CVSweb