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

Annotation of mandoc/man_term.c, Revision 1.102

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

CVSweb