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

Annotation of mandoc/man_term.c, Revision 1.129

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

CVSweb