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

Annotation of mandoc/man_term.c, Revision 1.114

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

CVSweb