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

Annotation of mandoc/man_term.c, Revision 1.162

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

CVSweb