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

Annotation of mandoc/man_term.c, Revision 1.130

1.130   ! schwarze    1: /*     $Id: man_term.c,v 1.129 2012/06/02 20:16:23 schwarze Exp $ */
1.1       kristaps    2: /*
1.128     schwarze    3:  * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.130   ! schwarze    4:  * Copyright (c) 2010, 2011, 2012 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:                break;
                    484:        default:
                    485:                return(0);
                    486:        }
                    487:
1.130   ! schwarze  488:        if ( ! (MANT_LITERAL & mt->fl)) {
        !           489:                p->flags |= TERMP_NOBREAK;
        !           490:                p->flags |= TERMP_TWOSPACE;
        !           491:        }
        !           492:
1.116     schwarze  493:        len = mt->lmargin[mt->lmargincur];
1.24      kristaps  494:        ival = -1;
                    495:
                    496:        /* Calculate offset. */
                    497:
1.39      kristaps  498:        if (NULL != (nn = n->parent->head->child))
1.77      kristaps  499:                if ((ival = a2width(p, nn->string)) >= 0)
1.24      kristaps  500:                        len = (size_t)ival;
                    501:
1.117     schwarze  502:        one = term_len(p, 1);
1.121     schwarze  503:        if (len < one)
1.117     schwarze  504:                len = one;
1.24      kristaps  505:
1.26      kristaps  506:        p->offset = mt->offset;
                    507:        p->rmargin = mt->offset + len;
1.24      kristaps  508:
                    509:        if (ival >= 0)
1.116     schwarze  510:                mt->lmargin[mt->lmargincur] = (size_t)ival;
1.24      kristaps  511:
1.19      kristaps  512:        return(1);
                    513: }
                    514:
                    515:
                    516: /* ARGSUSED */
1.20      kristaps  517: static void
                    518: post_HP(DECL_ARGS)
                    519: {
                    520:
                    521:        switch (n->type) {
                    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.130   ! schwarze  699:                p->flags &= ~TERMP_NOBREAK;
        !           700:                p->flags &= ~TERMP_TWOSPACE;
1.21      kristaps  701:                break;
                    702:        default:
                    703:                break;
                    704:        }
1.16      kristaps  705:
1.21      kristaps  706:        return(1);
                    707: }
1.1       kristaps  708:
                    709:
1.21      kristaps  710: /* ARGSUSED */
                    711: static void
                    712: post_TP(DECL_ARGS)
                    713: {
1.1       kristaps  714:
1.21      kristaps  715:        switch (n->type) {
                    716:        case (MAN_HEAD):
                    717:                term_flushln(p);
                    718:                break;
                    719:        case (MAN_BODY):
1.94      schwarze  720:                term_newln(p);
1.21      kristaps  721:                break;
                    722:        default:
                    723:                break;
                    724:        }
1.1       kristaps  725: }
                    726:
                    727:
1.3       kristaps  728: /* ARGSUSED */
1.1       kristaps  729: static int
                    730: pre_SS(DECL_ARGS)
                    731: {
                    732:
1.19      kristaps  733:        switch (n->type) {
                    734:        case (MAN_BLOCK):
1.113     kristaps  735:                mt->fl &= ~MANT_LITERAL;
1.122     schwarze  736:                mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
                    737:                mt->offset = term_len(p, p->defindent);
1.24      kristaps  738:                /* If following a prior empty `SS', no vspace. */
                    739:                if (n->prev && MAN_SS == n->prev->tok)
                    740:                        if (NULL == n->prev->body->child)
                    741:                                break;
                    742:                if (NULL == n->prev)
                    743:                        break;
                    744:                term_vspace(p);
1.19      kristaps  745:                break;
                    746:        case (MAN_HEAD):
1.52      kristaps  747:                term_fontrepl(p, TERMFONT_BOLD);
1.122     schwarze  748:                p->offset = term_len(p, p->defindent/2);
1.19      kristaps  749:                break;
1.24      kristaps  750:        case (MAN_BODY):
1.26      kristaps  751:                p->offset = mt->offset;
1.24      kristaps  752:                break;
1.19      kristaps  753:        default:
                    754:                break;
                    755:        }
                    756:
1.1       kristaps  757:        return(1);
                    758: }
                    759:
                    760:
1.3       kristaps  761: /* ARGSUSED */
1.1       kristaps  762: static void
                    763: post_SS(DECL_ARGS)
                    764: {
                    765:
1.19      kristaps  766:        switch (n->type) {
                    767:        case (MAN_HEAD):
                    768:                term_newln(p);
                    769:                break;
1.24      kristaps  770:        case (MAN_BODY):
                    771:                term_newln(p);
                    772:                break;
1.19      kristaps  773:        default:
                    774:                break;
                    775:        }
1.1       kristaps  776: }
                    777:
                    778:
1.3       kristaps  779: /* ARGSUSED */
1.1       kristaps  780: static int
                    781: pre_SH(DECL_ARGS)
                    782: {
1.22      kristaps  783:
1.19      kristaps  784:        switch (n->type) {
                    785:        case (MAN_BLOCK):
1.113     kristaps  786:                mt->fl &= ~MANT_LITERAL;
1.122     schwarze  787:                mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
                    788:                mt->offset = term_len(p, p->defindent);
1.22      kristaps  789:                /* If following a prior empty `SH', no vspace. */
1.19      kristaps  790:                if (n->prev && MAN_SH == n->prev->tok)
                    791:                        if (NULL == n->prev->body->child)
                    792:                                break;
1.61      kristaps  793:                /* If the first macro, no vspae. */
                    794:                if (NULL == n->prev)
                    795:                        break;
1.19      kristaps  796:                term_vspace(p);
                    797:                break;
                    798:        case (MAN_HEAD):
1.52      kristaps  799:                term_fontrepl(p, TERMFONT_BOLD);
1.19      kristaps  800:                p->offset = 0;
                    801:                break;
                    802:        case (MAN_BODY):
1.26      kristaps  803:                p->offset = mt->offset;
1.19      kristaps  804:                break;
                    805:        default:
                    806:                break;
                    807:        }
1.1       kristaps  808:
                    809:        return(1);
                    810: }
                    811:
                    812:
1.3       kristaps  813: /* ARGSUSED */
1.1       kristaps  814: static void
                    815: post_SH(DECL_ARGS)
                    816: {
                    817:
1.19      kristaps  818:        switch (n->type) {
                    819:        case (MAN_HEAD):
                    820:                term_newln(p);
                    821:                break;
                    822:        case (MAN_BODY):
                    823:                term_newln(p);
                    824:                break;
                    825:        default:
                    826:                break;
                    827:        }
1.1       kristaps  828: }
                    829:
1.26      kristaps  830: /* ARGSUSED */
                    831: static int
                    832: pre_RS(DECL_ARGS)
                    833: {
1.110     kristaps  834:        int              ival;
                    835:        size_t           sz;
1.26      kristaps  836:
                    837:        switch (n->type) {
                    838:        case (MAN_BLOCK):
                    839:                term_newln(p);
                    840:                return(1);
                    841:        case (MAN_HEAD):
                    842:                return(0);
                    843:        default:
                    844:                break;
                    845:        }
                    846:
1.122     schwarze  847:        sz = term_len(p, p->defindent);
1.26      kristaps  848:
1.110     kristaps  849:        if (NULL != (n = n->parent->head->child))
                    850:                if ((ival = a2width(p, n->string)) >= 0)
                    851:                        sz = (size_t)ival;
1.26      kristaps  852:
1.110     kristaps  853:        mt->offset += sz;
1.119     schwarze  854:        p->rmargin = p->maxrmargin;
                    855:        p->offset = mt->offset < p->rmargin ? mt->offset : p->rmargin;
1.26      kristaps  856:
1.116     schwarze  857:        if (++mt->lmarginsz < MAXMARGINS)
                    858:                mt->lmargincur = mt->lmarginsz;
                    859:
                    860:        mt->lmargin[mt->lmargincur] = mt->lmargin[mt->lmargincur - 1];
1.26      kristaps  861:        return(1);
                    862: }
                    863:
                    864: /* ARGSUSED */
                    865: static void
                    866: post_RS(DECL_ARGS)
                    867: {
1.110     kristaps  868:        int              ival;
                    869:        size_t           sz;
1.26      kristaps  870:
                    871:        switch (n->type) {
                    872:        case (MAN_BLOCK):
1.110     kristaps  873:                return;
1.59      kristaps  874:        case (MAN_HEAD):
1.110     kristaps  875:                return;
1.26      kristaps  876:        default:
                    877:                term_newln(p);
                    878:                break;
                    879:        }
1.110     kristaps  880:
1.122     schwarze  881:        sz = term_len(p, p->defindent);
1.110     kristaps  882:
                    883:        if (NULL != (n = n->parent->head->child))
                    884:                if ((ival = a2width(p, n->string)) >= 0)
                    885:                        sz = (size_t)ival;
                    886:
                    887:        mt->offset = mt->offset < sz ?  0 : mt->offset - sz;
                    888:        p->offset = mt->offset;
1.116     schwarze  889:
                    890:        if (--mt->lmarginsz < MAXMARGINS)
                    891:                mt->lmargincur = mt->lmarginsz;
1.26      kristaps  892: }
                    893:
1.1       kristaps  894: static void
1.45      kristaps  895: print_man_node(DECL_ARGS)
1.1       kristaps  896: {
1.65      joerg     897:        size_t           rm, rmax;
1.54      kristaps  898:        int              c;
1.1       kristaps  899:
                    900:        switch (n->type) {
                    901:        case(MAN_TEXT):
1.97      kristaps  902:                /*
                    903:                 * If we have a blank line, output a vertical space.
                    904:                 * If we have a space as the first character, break
                    905:                 * before printing the line's data.
                    906:                 */
1.96      kristaps  907:                if ('\0' == *n->string) {
1.4       kristaps  908:                        term_vspace(p);
1.98      schwarze  909:                        return;
1.97      kristaps  910:                } else if (' ' == *n->string && MAN_LINE & n->flags)
1.96      kristaps  911:                        term_newln(p);
1.54      kristaps  912:
1.4       kristaps  913:                term_word(p, n->string);
1.130   ! schwarze  914:                goto out;
1.52      kristaps  915:
1.102     kristaps  916:        case (MAN_EQN):
1.115     kristaps  917:                term_eqn(p, n->eqn);
1.97      kristaps  918:                return;
1.91      kristaps  919:        case (MAN_TBL):
1.97      kristaps  920:                /*
                    921:                 * Tables are preceded by a newline.  Then process a
                    922:                 * table line, which will cause line termination,
                    923:                 */
1.93      kristaps  924:                if (TBL_SPAN_FIRST & n->span->flags)
                    925:                        term_newln(p);
1.92      kristaps  926:                term_tbl(p, n->span);
1.97      kristaps  927:                return;
1.1       kristaps  928:        default:
                    929:                break;
                    930:        }
                    931:
1.97      kristaps  932:        if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
                    933:                term_fontrepl(p, TERMFONT_NONE);
                    934:
                    935:        c = 1;
                    936:        if (termacts[n->tok].pre)
                    937:                c = (*termacts[n->tok].pre)(p, mt, n, m);
                    938:
1.1       kristaps  939:        if (c && n->child)
1.52      kristaps  940:                print_man_nodelist(p, mt, n->child, m);
1.1       kristaps  941:
1.97      kristaps  942:        if (termacts[n->tok].post)
                    943:                (*termacts[n->tok].post)(p, mt, n, m);
                    944:        if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
                    945:                term_fontrepl(p, TERMFONT_NONE);
1.63      kristaps  946:
1.130   ! schwarze  947: out:
        !           948:        /*
        !           949:         * If we're in a literal context, make sure that words
        !           950:         * together on the same line stay together.  This is a
        !           951:         * POST-printing call, so we check the NEXT word.  Since
        !           952:         * -man doesn't have nested macros, we don't need to be
        !           953:         * more specific than this.
        !           954:         */
        !           955:        if (MANT_LITERAL & mt->fl && ! (TERMP_NOBREAK & p->flags) &&
        !           956:            NULL != n->next && n->next->line > n->line) {
        !           957:                rm = p->rmargin;
        !           958:                rmax = p->maxrmargin;
        !           959:                p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
        !           960:                p->flags |= TERMP_NOSPACE;
        !           961:                if (NULL != n->string && '\0' != *n->string)
        !           962:                        term_flushln(p);
        !           963:                else
        !           964:                        term_newln(p);
        !           965:                if (rm < rmax && n->parent->tok == MAN_HP) {
        !           966:                        p->offset = rm;
        !           967:                        p->rmargin = rmax;
        !           968:                } else
        !           969:                        p->rmargin = rm;
        !           970:                p->maxrmargin = rmax;
        !           971:        }
1.63      kristaps  972:        if (MAN_EOS & n->flags)
                    973:                p->flags |= TERMP_SENTENCE;
1.1       kristaps  974: }
                    975:
                    976:
                    977: static void
1.52      kristaps  978: print_man_nodelist(DECL_ARGS)
1.1       kristaps  979: {
1.19      kristaps  980:
1.45      kristaps  981:        print_man_node(p, mt, n, m);
1.1       kristaps  982:        if ( ! n->next)
                    983:                return;
1.52      kristaps  984:        print_man_nodelist(p, mt, n->next, m);
1.1       kristaps  985: }
                    986:
                    987:
1.31      kristaps  988: static void
1.73      kristaps  989: print_man_foot(struct termp *p, const void *arg)
1.1       kristaps  990: {
1.123     schwarze  991:        char            title[BUFSIZ];
                    992:        size_t          datelen;
1.73      kristaps  993:        const struct man_meta *meta;
                    994:
                    995:        meta = (const struct man_meta *)arg;
1.125     schwarze  996:        assert(meta->title);
                    997:        assert(meta->msec);
                    998:        assert(meta->date);
1.1       kristaps  999:
1.52      kristaps 1000:        term_fontrepl(p, TERMFONT_NONE);
1.50      kristaps 1001:
1.69      joerg    1002:        term_vspace(p);
1.126     schwarze 1003:
                   1004:        /*
                   1005:         * Temporary, undocumented option to imitate mdoc(7) output.
                   1006:         * In the bottom right corner, use the source instead of
                   1007:         * the title.
                   1008:         */
                   1009:
                   1010:        if ( ! p->mdocstyle) {
                   1011:                term_vspace(p);
                   1012:                term_vspace(p);
                   1013:                snprintf(title, BUFSIZ, "%s(%s)", meta->title, meta->msec);
                   1014:        } else if (meta->source) {
                   1015:                strlcpy(title, meta->source, BUFSIZ);
                   1016:        } else {
                   1017:                title[0] = '\0';
                   1018:        }
1.125     schwarze 1019:        datelen = term_strlen(p, meta->date);
1.1       kristaps 1020:
1.126     schwarze 1021:        /* Bottom left corner: manual source. */
                   1022:
1.1       kristaps 1023:        p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
                   1024:        p->offset = 0;
1.123     schwarze 1025:        p->rmargin = (p->maxrmargin - datelen + term_len(p, 1)) / 2;
1.1       kristaps 1026:
                   1027:        if (meta->source)
                   1028:                term_word(p, meta->source);
                   1029:        term_flushln(p);
                   1030:
1.126     schwarze 1031:        /* At the bottom in the middle: manual date. */
                   1032:
1.117     schwarze 1033:        p->flags |= TERMP_NOSPACE;
1.1       kristaps 1034:        p->offset = p->rmargin;
1.123     schwarze 1035:        p->rmargin = p->maxrmargin - term_strlen(p, title);
                   1036:        if (p->offset + datelen >= p->rmargin)
                   1037:                p->rmargin = p->offset + datelen;
                   1038:
1.125     schwarze 1039:        term_word(p, meta->date);
1.123     schwarze 1040:        term_flushln(p);
                   1041:
1.126     schwarze 1042:        /* Bottom right corner: manual title and section. */
                   1043:
1.123     schwarze 1044:        p->flags &= ~TERMP_NOBREAK;
                   1045:        p->flags |= TERMP_NOSPACE;
                   1046:        p->offset = p->rmargin;
1.1       kristaps 1047:        p->rmargin = p->maxrmargin;
                   1048:
1.123     schwarze 1049:        term_word(p, title);
1.1       kristaps 1050:        term_flushln(p);
                   1051: }
                   1052:
                   1053:
1.31      kristaps 1054: static void
1.73      kristaps 1055: print_man_head(struct termp *p, const void *arg)
1.1       kristaps 1056: {
1.46      kristaps 1057:        char            buf[BUFSIZ], title[BUFSIZ];
1.57      kristaps 1058:        size_t          buflen, titlen;
1.73      kristaps 1059:        const struct man_meta *m;
                   1060:
                   1061:        m = (const struct man_meta *)arg;
1.125     schwarze 1062:        assert(m->title);
                   1063:        assert(m->msec);
1.1       kristaps 1064:
1.46      kristaps 1065:        if (m->vol)
                   1066:                strlcpy(buf, m->vol, BUFSIZ);
1.126     schwarze 1067:        else
                   1068:                buf[0] = '\0';
1.77      kristaps 1069:        buflen = term_strlen(p, buf);
1.1       kristaps 1070:
1.126     schwarze 1071:        /* Top left corner: manual title and section. */
                   1072:
1.125     schwarze 1073:        snprintf(title, BUFSIZ, "%s(%s)", m->title, m->msec);
1.77      kristaps 1074:        titlen = term_strlen(p, title);
1.1       kristaps 1075:
1.118     schwarze 1076:        p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
1.1       kristaps 1077:        p->offset = 0;
1.57      kristaps 1078:        p->rmargin = 2 * (titlen+1) + buflen < p->maxrmargin ?
1.77      kristaps 1079:            (p->maxrmargin -
                   1080:             term_strlen(p, buf) + term_len(p, 1)) / 2 :
1.57      kristaps 1081:            p->maxrmargin - buflen;
1.1       kristaps 1082:
                   1083:        term_word(p, title);
                   1084:        term_flushln(p);
                   1085:
1.126     schwarze 1086:        /* At the top in the middle: manual volume. */
                   1087:
1.117     schwarze 1088:        p->flags |= TERMP_NOSPACE;
1.1       kristaps 1089:        p->offset = p->rmargin;
1.57      kristaps 1090:        p->rmargin = p->offset + buflen + titlen < p->maxrmargin ?
                   1091:            p->maxrmargin - titlen : p->maxrmargin;
1.1       kristaps 1092:
                   1093:        term_word(p, buf);
                   1094:        term_flushln(p);
                   1095:
1.126     schwarze 1096:        /* Top right corner: title and section, again. */
                   1097:
1.1       kristaps 1098:        p->flags &= ~TERMP_NOBREAK;
1.57      kristaps 1099:        if (p->rmargin + titlen <= p->maxrmargin) {
1.117     schwarze 1100:                p->flags |= TERMP_NOSPACE;
1.57      kristaps 1101:                p->offset = p->rmargin;
                   1102:                p->rmargin = p->maxrmargin;
                   1103:                term_word(p, title);
                   1104:                term_flushln(p);
                   1105:        }
1.1       kristaps 1106:
1.118     schwarze 1107:        p->flags &= ~TERMP_NOSPACE;
                   1108:        p->offset = 0;
1.1       kristaps 1109:        p->rmargin = p->maxrmargin;
1.61      kristaps 1110:
1.62      kristaps 1111:        /*
1.126     schwarze 1112:         * Groff prints three blank lines before the content.
                   1113:         * Do the same, except in the temporary, undocumented
                   1114:         * mode imitating mdoc(7) output.
1.62      kristaps 1115:         */
                   1116:
1.61      kristaps 1117:        term_vspace(p);
1.126     schwarze 1118:        if ( ! p->mdocstyle) {
                   1119:                term_vspace(p);
                   1120:                term_vspace(p);
                   1121:        }
1.1       kristaps 1122: }

CVSweb