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

Annotation of mandoc/man_term.c, Revision 1.68

1.68    ! kristaps    1: /*     $Id: man_term.c,v 1.67 2010/05/15 20:51:40 kristaps Exp $ */
1.1       kristaps    2: /*
1.9       kristaps    3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
1.8       kristaps    6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    8:  *
1.8       kristaps    9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   16:  */
1.55      kristaps   17: #ifdef HAVE_CONFIG_H
                     18: #include "config.h"
                     19: #endif
                     20:
1.28      kristaps   21: #include <sys/types.h>
                     22:
1.1       kristaps   23: #include <assert.h>
1.18      kristaps   24: #include <ctype.h>
1.1       kristaps   25: #include <stdio.h>
                     26: #include <stdlib.h>
                     27: #include <string.h>
                     28:
1.40      kristaps   29: #include "out.h"
1.36      kristaps   30: #include "man.h"
1.1       kristaps   31: #include "term.h"
1.36      kristaps   32: #include "chars.h"
                     33: #include "main.h"
1.1       kristaps   34:
1.18      kristaps   35: #define        INDENT            7
                     36: #define        HALFINDENT        3
                     37:
1.44      kristaps   38: /* FIXME: have PD set the default vspace width. */
                     39:
1.24      kristaps   40: struct mtermp {
                     41:        int               fl;
1.19      kristaps   42: #define        MANT_LITERAL     (1 << 0)
1.26      kristaps   43:        /*
                     44:         * Default amount to indent the left margin after leading text
                     45:         * has been printed (e.g., `HP' left-indent, `TP' and `IP' body
                     46:         * indent).  This needs to be saved because `HP' and so on, if
                     47:         * not having a specified value, must default.
                     48:         *
                     49:         * Note that this is the indentation AFTER the left offset, so
                     50:         * the total offset is usually offset + lmargin.
                     51:         */
                     52:        size_t            lmargin;
                     53:        /*
                     54:         * The default offset, i.e., the amount between any text and the
                     55:         * page boundary.
                     56:         */
                     57:        size_t            offset;
1.24      kristaps   58: };
1.19      kristaps   59:
1.1       kristaps   60: #define        DECL_ARGS         struct termp *p, \
1.24      kristaps   61:                          struct mtermp *mt, \
1.1       kristaps   62:                          const struct man_node *n, \
                     63:                          const struct man_meta *m
                     64:
                     65: struct termact {
                     66:        int             (*pre)(DECL_ARGS);
                     67:        void            (*post)(DECL_ARGS);
1.56      kristaps   68:        int               flags;
                     69: #define        MAN_NOTEXT       (1 << 0) /* Never has text children. */
1.1       kristaps   70: };
                     71:
1.45      kristaps   72: static int               a2width(const struct man_node *);
                     73: static int               a2height(const struct man_node *);
1.39      kristaps   74:
1.45      kristaps   75: static void              print_man_head(struct termp *,
1.39      kristaps   76:                                const struct man_meta *);
1.52      kristaps   77: static void              print_man_nodelist(DECL_ARGS);
1.45      kristaps   78: static void              print_man_node(DECL_ARGS);
                     79: static void              print_man_foot(struct termp *,
1.39      kristaps   80:                                const struct man_meta *);
                     81: static void              print_bvspace(struct termp *,
                     82:                                const struct man_node *);
                     83:
1.1       kristaps   84: static int               pre_B(DECL_ARGS);
1.3       kristaps   85: static int               pre_BI(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.3       kristaps   90: static int               pre_RB(DECL_ARGS);
                     91: static int               pre_RI(DECL_ARGS);
1.26      kristaps   92: static int               pre_RS(DECL_ARGS);
1.1       kristaps   93: static int               pre_SH(DECL_ARGS);
                     94: static int               pre_SS(DECL_ARGS);
                     95: static int               pre_TP(DECL_ARGS);
1.19      kristaps   96: static int               pre_br(DECL_ARGS);
                     97: static int               pre_fi(DECL_ARGS);
1.29      kristaps   98: static int               pre_ign(DECL_ARGS);
1.19      kristaps   99: static int               pre_nf(DECL_ARGS);
                    100: static int               pre_sp(DECL_ARGS);
1.1       kristaps  101:
1.22      kristaps  102: static void              post_IP(DECL_ARGS);
1.20      kristaps  103: static void              post_HP(DECL_ARGS);
1.26      kristaps  104: static void              post_RS(DECL_ARGS);
1.1       kristaps  105: static void              post_SH(DECL_ARGS);
                    106: static void              post_SS(DECL_ARGS);
1.21      kristaps  107: static void              post_TP(DECL_ARGS);
1.1       kristaps  108:
1.32      kristaps  109: static const struct termact termacts[MAN_MAX] = {
1.56      kristaps  110:        { pre_br, NULL, MAN_NOTEXT }, /* br */
                    111:        { NULL, NULL, 0 }, /* TH */
                    112:        { pre_SH, post_SH, 0 }, /* SH */
                    113:        { pre_SS, post_SS, 0 }, /* SS */
                    114:        { pre_TP, post_TP, 0 }, /* TP */
                    115:        { pre_PP, NULL, 0 }, /* LP */
                    116:        { pre_PP, NULL, 0 }, /* PP */
                    117:        { pre_PP, NULL, 0 }, /* P */
                    118:        { pre_IP, post_IP, 0 }, /* IP */
                    119:        { pre_HP, post_HP, 0 }, /* HP */
                    120:        { NULL, NULL, 0 }, /* SM */
                    121:        { pre_B, NULL, 0 }, /* SB */
                    122:        { pre_BI, NULL, 0 }, /* BI */
                    123:        { pre_BI, NULL, 0 }, /* IB */
                    124:        { pre_RB, NULL, 0 }, /* BR */
                    125:        { pre_RB, NULL, 0 }, /* RB */
                    126:        { NULL, NULL, 0 }, /* R */
                    127:        { pre_B, NULL, 0 }, /* B */
                    128:        { pre_I, NULL, 0 }, /* I */
                    129:        { pre_RI, NULL, 0 }, /* IR */
                    130:        { pre_RI, NULL, 0 }, /* RI */
                    131:        { NULL, NULL, MAN_NOTEXT }, /* na */
                    132:        { pre_I, NULL, 0 }, /* i */
                    133:        { pre_sp, NULL, MAN_NOTEXT }, /* sp */
                    134:        { pre_nf, NULL, 0 }, /* nf */
                    135:        { pre_fi, NULL, 0 }, /* fi */
                    136:        { NULL, NULL, 0 }, /* r */
                    137:        { NULL, NULL, 0 }, /* RE */
                    138:        { pre_RS, post_RS, 0 }, /* RS */
                    139:        { pre_ign, NULL, 0 }, /* DT */
                    140:        { pre_ign, NULL, 0 }, /* UC */
                    141:        { pre_ign, NULL, 0 }, /* PD */
1.57      kristaps  142:        { pre_sp, NULL, MAN_NOTEXT }, /* Sp */
                    143:        { pre_nf, NULL, 0 }, /* Vb */
                    144:        { pre_fi, NULL, 0 }, /* Ve */
1.1       kristaps  145: };
                    146:
                    147:
                    148:
1.31      kristaps  149: void
1.36      kristaps  150: terminal_man(void *arg, const struct man *man)
1.1       kristaps  151: {
1.36      kristaps  152:        struct termp            *p;
                    153:        const struct man_node   *n;
                    154:        const struct man_meta   *m;
                    155:        struct mtermp            mt;
                    156:
                    157:        p = (struct termp *)arg;
                    158:
1.58      kristaps  159:        p->overstep = 0;
1.65      joerg     160:        p->maxrmargin = p->defrmargin;
1.58      kristaps  161:
1.36      kristaps  162:        if (NULL == p->symtab)
                    163:                switch (p->enc) {
                    164:                case (TERMENC_ASCII):
                    165:                        p->symtab = chars_init(CHARS_ASCII);
                    166:                        break;
                    167:                default:
                    168:                        abort();
                    169:                        /* NOTREACHED */
                    170:                }
                    171:
                    172:        n = man_node(man);
                    173:        m = man_meta(man);
1.1       kristaps  174:
1.45      kristaps  175:        print_man_head(p, m);
1.1       kristaps  176:        p->flags |= TERMP_NOSPACE;
1.19      kristaps  177:
1.24      kristaps  178:        mt.fl = 0;
                    179:        mt.lmargin = INDENT;
1.26      kristaps  180:        mt.offset = INDENT;
1.24      kristaps  181:
1.36      kristaps  182:        if (n->child)
1.52      kristaps  183:                print_man_nodelist(p, &mt, n->child, m);
1.45      kristaps  184:        print_man_foot(p, m);
1.1       kristaps  185: }
                    186:
                    187:
1.39      kristaps  188: static int
1.45      kristaps  189: a2height(const struct man_node *n)
1.18      kristaps  190: {
1.40      kristaps  191:        struct roffsu    su;
1.18      kristaps  192:
1.39      kristaps  193:        assert(MAN_TEXT == n->type);
                    194:        assert(n->string);
1.40      kristaps  195:        if ( ! a2roffsu(n->string, &su, SCALE_VS))
                    196:                SCALE_VS_INIT(&su, strlen(n->string));
1.18      kristaps  197:
1.41      kristaps  198:        return((int)term_vspan(&su));
1.18      kristaps  199: }
                    200:
                    201:
                    202: static int
1.45      kristaps  203: a2width(const struct man_node *n)
1.38      kristaps  204: {
1.40      kristaps  205:        struct roffsu    su;
1.38      kristaps  206:
1.39      kristaps  207:        assert(MAN_TEXT == n->type);
                    208:        assert(n->string);
1.40      kristaps  209:        if ( ! a2roffsu(n->string, &su, SCALE_BU))
1.41      kristaps  210:                return(-1);
1.40      kristaps  211:
1.41      kristaps  212:        return((int)term_hspan(&su));
1.38      kristaps  213: }
                    214:
                    215:
1.39      kristaps  216: static void
                    217: print_bvspace(struct termp *p, const struct man_node *n)
1.18      kristaps  218: {
1.39      kristaps  219:        term_newln(p);
1.18      kristaps  220:
1.39      kristaps  221:        if (NULL == n->prev)
                    222:                return;
1.18      kristaps  223:
1.39      kristaps  224:        if (MAN_SS == n->prev->tok)
                    225:                return;
                    226:        if (MAN_SH == n->prev->tok)
                    227:                return;
1.18      kristaps  228:
1.39      kristaps  229:        term_vspace(p);
1.18      kristaps  230: }
                    231:
                    232:
1.3       kristaps  233: /* ARGSUSED */
1.1       kristaps  234: static int
1.29      kristaps  235: pre_ign(DECL_ARGS)
                    236: {
                    237:
                    238:        return(0);
                    239: }
                    240:
                    241:
                    242: /* ARGSUSED */
                    243: static int
1.1       kristaps  244: pre_I(DECL_ARGS)
                    245: {
                    246:
1.52      kristaps  247:        term_fontrepl(p, TERMFONT_UNDER);
1.19      kristaps  248:        return(1);
                    249: }
                    250:
                    251:
                    252: /* ARGSUSED */
1.3       kristaps  253: static int
1.19      kristaps  254: pre_fi(DECL_ARGS)
                    255: {
                    256:
1.24      kristaps  257:        mt->fl &= ~MANT_LITERAL;
1.19      kristaps  258:        return(1);
                    259: }
                    260:
                    261:
                    262: /* ARGSUSED */
                    263: static int
                    264: pre_nf(DECL_ARGS)
                    265: {
                    266:
1.24      kristaps  267:        mt->fl |= MANT_LITERAL;
1.57      kristaps  268:        return(MAN_Vb != n->tok);
1.19      kristaps  269: }
                    270:
                    271:
                    272: /* ARGSUSED */
                    273: static int
1.35      kristaps  274: pre_RB(DECL_ARGS)
1.3       kristaps  275: {
                    276:        const struct man_node *nn;
                    277:        int              i;
                    278:
                    279:        for (i = 0, nn = n->child; nn; nn = nn->next, i++) {
1.35      kristaps  280:                if (i % 2 && MAN_RB == n->tok)
1.52      kristaps  281:                        term_fontrepl(p, TERMFONT_BOLD);
1.35      kristaps  282:                else if ( ! (i % 2) && MAN_RB != n->tok)
1.52      kristaps  283:                        term_fontrepl(p, TERMFONT_BOLD);
                    284:                else
                    285:                        term_fontrepl(p, TERMFONT_NONE);
1.35      kristaps  286:
1.4       kristaps  287:                if (i > 0)
                    288:                        p->flags |= TERMP_NOSPACE;
1.35      kristaps  289:
1.45      kristaps  290:                print_man_node(p, mt, nn, m);
1.3       kristaps  291:        }
                    292:        return(0);
                    293: }
                    294:
                    295:
                    296: /* ARGSUSED */
                    297: static int
                    298: pre_RI(DECL_ARGS)
                    299: {
                    300:        const struct man_node *nn;
                    301:        int              i;
                    302:
                    303:        for (i = 0, nn = n->child; nn; nn = nn->next, i++) {
1.49      kristaps  304:                if (i % 2 && MAN_RI == n->tok)
1.52      kristaps  305:                        term_fontrepl(p, TERMFONT_UNDER);
1.49      kristaps  306:                else if ( ! (i % 2) && MAN_RI != n->tok)
1.52      kristaps  307:                        term_fontrepl(p, TERMFONT_UNDER);
                    308:                else
                    309:                        term_fontrepl(p, TERMFONT_NONE);
1.49      kristaps  310:
1.4       kristaps  311:                if (i > 0)
                    312:                        p->flags |= TERMP_NOSPACE;
1.52      kristaps  313:
1.45      kristaps  314:                print_man_node(p, mt, nn, m);
1.3       kristaps  315:        }
                    316:        return(0);
                    317: }
                    318:
                    319:
                    320: /* ARGSUSED */
                    321: static int
                    322: pre_BI(DECL_ARGS)
                    323: {
1.35      kristaps  324:        const struct man_node   *nn;
                    325:        int                      i;
1.3       kristaps  326:
                    327:        for (i = 0, nn = n->child; nn; nn = nn->next, i++) {
1.35      kristaps  328:                if (i % 2 && MAN_BI == n->tok)
1.52      kristaps  329:                        term_fontrepl(p, TERMFONT_UNDER);
1.35      kristaps  330:                else if (i % 2)
1.52      kristaps  331:                        term_fontrepl(p, TERMFONT_BOLD);
1.35      kristaps  332:                else if (MAN_BI == n->tok)
1.52      kristaps  333:                        term_fontrepl(p, TERMFONT_BOLD);
1.30      kristaps  334:                else
1.52      kristaps  335:                        term_fontrepl(p, TERMFONT_UNDER);
1.35      kristaps  336:
                    337:                if (i)
1.4       kristaps  338:                        p->flags |= TERMP_NOSPACE;
1.52      kristaps  339:
1.45      kristaps  340:                print_man_node(p, mt, nn, m);
1.3       kristaps  341:        }
                    342:        return(0);
                    343: }
                    344:
                    345:
                    346: /* ARGSUSED */
1.1       kristaps  347: static int
                    348: pre_B(DECL_ARGS)
                    349: {
                    350:
1.52      kristaps  351:        term_fontrepl(p, TERMFONT_BOLD);
1.1       kristaps  352:        return(1);
                    353: }
                    354:
                    355:
1.3       kristaps  356: /* ARGSUSED */
1.1       kristaps  357: static int
1.19      kristaps  358: pre_sp(DECL_ARGS)
                    359: {
                    360:        int              i, len;
                    361:
1.45      kristaps  362:        len = n->child ? a2height(n->child) : 1;
1.38      kristaps  363:
1.19      kristaps  364:        if (0 == len)
                    365:                term_newln(p);
1.61      kristaps  366:        for (i = 0; i <= len; i++)
1.19      kristaps  367:                term_vspace(p);
                    368:
                    369:        return(0);
                    370: }
                    371:
                    372:
                    373: /* ARGSUSED */
                    374: static int
1.15      kristaps  375: pre_br(DECL_ARGS)
                    376: {
                    377:
                    378:        term_newln(p);
                    379:        return(0);
                    380: }
                    381:
                    382:
                    383: /* ARGSUSED */
                    384: static int
1.19      kristaps  385: pre_HP(DECL_ARGS)
                    386: {
1.24      kristaps  387:        size_t                   len;
                    388:        int                      ival;
                    389:        const struct man_node   *nn;
1.19      kristaps  390:
1.20      kristaps  391:        switch (n->type) {
                    392:        case (MAN_BLOCK):
1.39      kristaps  393:                print_bvspace(p, n);
1.24      kristaps  394:                return(1);
1.20      kristaps  395:        case (MAN_BODY):
                    396:                p->flags |= TERMP_NOBREAK;
                    397:                p->flags |= TERMP_TWOSPACE;
                    398:                break;
                    399:        default:
                    400:                return(0);
                    401:        }
                    402:
1.26      kristaps  403:        len = mt->lmargin;
1.24      kristaps  404:        ival = -1;
                    405:
                    406:        /* Calculate offset. */
                    407:
1.39      kristaps  408:        if (NULL != (nn = n->parent->head->child))
1.45      kristaps  409:                if ((ival = a2width(nn)) >= 0)
1.24      kristaps  410:                        len = (size_t)ival;
                    411:
                    412:        if (0 == len)
                    413:                len = 1;
                    414:
1.26      kristaps  415:        p->offset = mt->offset;
                    416:        p->rmargin = mt->offset + len;
1.24      kristaps  417:
                    418:        if (ival >= 0)
1.26      kristaps  419:                mt->lmargin = (size_t)ival;
1.24      kristaps  420:
1.19      kristaps  421:        return(1);
                    422: }
                    423:
                    424:
                    425: /* ARGSUSED */
1.20      kristaps  426: static void
                    427: post_HP(DECL_ARGS)
                    428: {
                    429:
                    430:        switch (n->type) {
1.24      kristaps  431:        case (MAN_BLOCK):
                    432:                term_flushln(p);
                    433:                break;
1.20      kristaps  434:        case (MAN_BODY):
                    435:                term_flushln(p);
                    436:                p->flags &= ~TERMP_NOBREAK;
                    437:                p->flags &= ~TERMP_TWOSPACE;
1.26      kristaps  438:                p->offset = mt->offset;
1.20      kristaps  439:                p->rmargin = p->maxrmargin;
                    440:                break;
                    441:        default:
                    442:                break;
                    443:        }
                    444: }
                    445:
                    446:
                    447: /* ARGSUSED */
1.19      kristaps  448: static int
1.1       kristaps  449: pre_PP(DECL_ARGS)
                    450: {
                    451:
1.19      kristaps  452:        switch (n->type) {
                    453:        case (MAN_BLOCK):
1.24      kristaps  454:                mt->lmargin = INDENT;
1.39      kristaps  455:                print_bvspace(p, n);
1.19      kristaps  456:                break;
                    457:        default:
1.26      kristaps  458:                p->offset = mt->offset;
1.19      kristaps  459:                break;
                    460:        }
                    461:
                    462:        return(1);
1.1       kristaps  463: }
                    464:
                    465:
1.3       kristaps  466: /* ARGSUSED */
1.1       kristaps  467: static int
1.4       kristaps  468: pre_IP(DECL_ARGS)
                    469: {
1.22      kristaps  470:        const struct man_node   *nn;
                    471:        size_t                   len;
                    472:        int                      ival;
1.18      kristaps  473:
1.22      kristaps  474:        switch (n->type) {
                    475:        case (MAN_BODY):
                    476:                p->flags |= TERMP_NOLPAD;
                    477:                p->flags |= TERMP_NOSPACE;
                    478:                break;
                    479:        case (MAN_HEAD):
                    480:                p->flags |= TERMP_NOBREAK;
                    481:                break;
1.23      kristaps  482:        case (MAN_BLOCK):
1.39      kristaps  483:                print_bvspace(p, n);
1.23      kristaps  484:                /* FALLTHROUGH */
1.22      kristaps  485:        default:
                    486:                return(1);
                    487:        }
1.18      kristaps  488:
1.26      kristaps  489:        len = mt->lmargin;
1.22      kristaps  490:        ival = -1;
1.4       kristaps  491:
1.22      kristaps  492:        /* Calculate offset. */
1.4       kristaps  493:
1.22      kristaps  494:        if (NULL != (nn = n->parent->head->child))
                    495:                if (NULL != (nn = nn->next)) {
                    496:                        for ( ; nn->next; nn = nn->next)
                    497:                                /* Do nothing. */ ;
1.45      kristaps  498:                        if ((ival = a2width(nn)) >= 0)
1.22      kristaps  499:                                len = (size_t)ival;
                    500:                }
1.4       kristaps  501:
1.22      kristaps  502:        switch (n->type) {
                    503:        case (MAN_HEAD):
1.23      kristaps  504:                /* Handle zero-width lengths. */
                    505:                if (0 == len)
                    506:                        len = 1;
                    507:
1.26      kristaps  508:                p->offset = mt->offset;
                    509:                p->rmargin = mt->offset + len;
1.22      kristaps  510:                if (ival < 0)
                    511:                        break;
1.18      kristaps  512:
1.24      kristaps  513:                /* Set the saved left-margin. */
1.26      kristaps  514:                mt->lmargin = (size_t)ival;
1.24      kristaps  515:
1.22      kristaps  516:                /* Don't print the length value. */
                    517:                for (nn = n->child; nn->next; nn = nn->next)
1.45      kristaps  518:                        print_man_node(p, mt, nn, m);
1.22      kristaps  519:                return(0);
1.23      kristaps  520:        case (MAN_BODY):
1.26      kristaps  521:                p->offset = mt->offset + len;
1.23      kristaps  522:                p->rmargin = p->maxrmargin;
                    523:                break;
1.22      kristaps  524:        default:
                    525:                break;
1.18      kristaps  526:        }
                    527:
1.22      kristaps  528:        return(1);
                    529: }
1.18      kristaps  530:
                    531:
1.22      kristaps  532: /* ARGSUSED */
                    533: static void
                    534: post_IP(DECL_ARGS)
                    535: {
1.4       kristaps  536:
1.22      kristaps  537:        switch (n->type) {
                    538:        case (MAN_HEAD):
                    539:                term_flushln(p);
                    540:                p->flags &= ~TERMP_NOBREAK;
                    541:                p->rmargin = p->maxrmargin;
                    542:                break;
                    543:        case (MAN_BODY):
                    544:                term_flushln(p);
                    545:                p->flags &= ~TERMP_NOLPAD;
                    546:                break;
                    547:        default:
                    548:                break;
                    549:        }
1.4       kristaps  550: }
                    551:
                    552:
                    553: /* ARGSUSED */
                    554: static int
1.1       kristaps  555: pre_TP(DECL_ARGS)
                    556: {
1.23      kristaps  557:        const struct man_node   *nn;
                    558:        size_t                   len;
                    559:        int                      ival;
1.1       kristaps  560:
1.21      kristaps  561:        switch (n->type) {
                    562:        case (MAN_HEAD):
                    563:                p->flags |= TERMP_NOBREAK;
                    564:                p->flags |= TERMP_TWOSPACE;
                    565:                break;
                    566:        case (MAN_BODY):
                    567:                p->flags |= TERMP_NOLPAD;
                    568:                p->flags |= TERMP_NOSPACE;
1.23      kristaps  569:                break;
                    570:        case (MAN_BLOCK):
1.39      kristaps  571:                print_bvspace(p, n);
1.23      kristaps  572:                /* FALLTHROUGH */
                    573:        default:
                    574:                return(1);
                    575:        }
                    576:
1.24      kristaps  577:        len = (size_t)mt->lmargin;
1.23      kristaps  578:        ival = -1;
                    579:
                    580:        /* Calculate offset. */
                    581:
1.56      kristaps  582:        if (NULL != (nn = n->parent->head->child)) {
                    583:                while (nn && MAN_TEXT != nn->type)
                    584:                        nn = nn->next;
                    585:                if (nn && nn->next)
1.45      kristaps  586:                        if ((ival = a2width(nn)) >= 0)
1.23      kristaps  587:                                len = (size_t)ival;
1.56      kristaps  588:        }
1.23      kristaps  589:
                    590:        switch (n->type) {
                    591:        case (MAN_HEAD):
                    592:                /* Handle zero-length properly. */
                    593:                if (0 == len)
                    594:                        len = 1;
                    595:
1.26      kristaps  596:                p->offset = mt->offset;
                    597:                p->rmargin = mt->offset + len;
1.23      kristaps  598:
                    599:                /* Don't print same-line elements. */
                    600:                for (nn = n->child; nn; nn = nn->next)
                    601:                        if (nn->line > n->line)
1.45      kristaps  602:                                print_man_node(p, mt, nn, m);
1.24      kristaps  603:
                    604:                if (ival >= 0)
1.26      kristaps  605:                        mt->lmargin = (size_t)ival;
1.24      kristaps  606:
1.23      kristaps  607:                return(0);
                    608:        case (MAN_BODY):
1.26      kristaps  609:                p->offset = mt->offset + len;
1.23      kristaps  610:                p->rmargin = p->maxrmargin;
1.21      kristaps  611:                break;
                    612:        default:
                    613:                break;
                    614:        }
1.16      kristaps  615:
1.21      kristaps  616:        return(1);
                    617: }
1.1       kristaps  618:
                    619:
1.21      kristaps  620: /* ARGSUSED */
                    621: static void
                    622: post_TP(DECL_ARGS)
                    623: {
1.1       kristaps  624:
1.21      kristaps  625:        switch (n->type) {
                    626:        case (MAN_HEAD):
                    627:                term_flushln(p);
                    628:                p->flags &= ~TERMP_NOBREAK;
                    629:                p->flags &= ~TERMP_TWOSPACE;
                    630:                p->rmargin = p->maxrmargin;
                    631:                break;
                    632:        case (MAN_BODY):
                    633:                term_flushln(p);
                    634:                p->flags &= ~TERMP_NOLPAD;
                    635:                break;
                    636:        default:
                    637:                break;
                    638:        }
1.1       kristaps  639: }
                    640:
                    641:
1.3       kristaps  642: /* ARGSUSED */
1.1       kristaps  643: static int
                    644: pre_SS(DECL_ARGS)
                    645: {
                    646:
1.19      kristaps  647:        switch (n->type) {
                    648:        case (MAN_BLOCK):
1.24      kristaps  649:                mt->lmargin = INDENT;
1.26      kristaps  650:                mt->offset = INDENT;
1.24      kristaps  651:                /* If following a prior empty `SS', no vspace. */
                    652:                if (n->prev && MAN_SS == n->prev->tok)
                    653:                        if (NULL == n->prev->body->child)
                    654:                                break;
                    655:                if (NULL == n->prev)
                    656:                        break;
                    657:                term_vspace(p);
1.19      kristaps  658:                break;
                    659:        case (MAN_HEAD):
1.52      kristaps  660:                term_fontrepl(p, TERMFONT_BOLD);
1.19      kristaps  661:                p->offset = HALFINDENT;
                    662:                break;
1.24      kristaps  663:        case (MAN_BODY):
1.26      kristaps  664:                p->offset = mt->offset;
1.24      kristaps  665:                break;
1.19      kristaps  666:        default:
                    667:                break;
                    668:        }
                    669:
1.1       kristaps  670:        return(1);
                    671: }
                    672:
                    673:
1.3       kristaps  674: /* ARGSUSED */
1.1       kristaps  675: static void
                    676: post_SS(DECL_ARGS)
                    677: {
                    678:
1.19      kristaps  679:        switch (n->type) {
                    680:        case (MAN_HEAD):
                    681:                term_newln(p);
                    682:                break;
1.24      kristaps  683:        case (MAN_BODY):
                    684:                term_newln(p);
                    685:                break;
1.19      kristaps  686:        default:
                    687:                break;
                    688:        }
1.1       kristaps  689: }
                    690:
                    691:
1.3       kristaps  692: /* ARGSUSED */
1.1       kristaps  693: static int
                    694: pre_SH(DECL_ARGS)
                    695: {
1.22      kristaps  696:
1.19      kristaps  697:        switch (n->type) {
                    698:        case (MAN_BLOCK):
1.24      kristaps  699:                mt->lmargin = INDENT;
1.26      kristaps  700:                mt->offset = INDENT;
1.22      kristaps  701:                /* If following a prior empty `SH', no vspace. */
1.19      kristaps  702:                if (n->prev && MAN_SH == n->prev->tok)
                    703:                        if (NULL == n->prev->body->child)
                    704:                                break;
1.61      kristaps  705:                /* If the first macro, no vspae. */
                    706:                if (NULL == n->prev)
                    707:                        break;
1.19      kristaps  708:                term_vspace(p);
                    709:                break;
                    710:        case (MAN_HEAD):
1.52      kristaps  711:                term_fontrepl(p, TERMFONT_BOLD);
1.19      kristaps  712:                p->offset = 0;
                    713:                break;
                    714:        case (MAN_BODY):
1.26      kristaps  715:                p->offset = mt->offset;
1.19      kristaps  716:                break;
                    717:        default:
                    718:                break;
                    719:        }
1.1       kristaps  720:
                    721:        return(1);
                    722: }
                    723:
                    724:
1.3       kristaps  725: /* ARGSUSED */
1.1       kristaps  726: static void
                    727: post_SH(DECL_ARGS)
                    728: {
                    729:
1.19      kristaps  730:        switch (n->type) {
                    731:        case (MAN_HEAD):
                    732:                term_newln(p);
                    733:                break;
                    734:        case (MAN_BODY):
                    735:                term_newln(p);
                    736:                break;
                    737:        default:
                    738:                break;
                    739:        }
1.1       kristaps  740: }
                    741:
                    742:
1.26      kristaps  743: /* ARGSUSED */
                    744: static int
                    745: pre_RS(DECL_ARGS)
                    746: {
                    747:        const struct man_node   *nn;
                    748:        int                      ival;
                    749:
                    750:        switch (n->type) {
                    751:        case (MAN_BLOCK):
                    752:                term_newln(p);
                    753:                return(1);
                    754:        case (MAN_HEAD):
                    755:                return(0);
                    756:        default:
                    757:                break;
                    758:        }
                    759:
                    760:        if (NULL == (nn = n->parent->head->child)) {
                    761:                mt->offset = mt->lmargin + INDENT;
                    762:                p->offset = mt->offset;
                    763:                return(1);
                    764:        }
                    765:
1.45      kristaps  766:        if ((ival = a2width(nn)) < 0)
1.26      kristaps  767:                return(1);
                    768:
                    769:        mt->offset = INDENT + (size_t)ival;
                    770:        p->offset = mt->offset;
                    771:
                    772:        return(1);
                    773: }
                    774:
                    775:
                    776: /* ARGSUSED */
                    777: static void
                    778: post_RS(DECL_ARGS)
                    779: {
                    780:
                    781:        switch (n->type) {
                    782:        case (MAN_BLOCK):
                    783:                mt->offset = mt->lmargin = INDENT;
1.59      kristaps  784:                break;
                    785:        case (MAN_HEAD):
1.26      kristaps  786:                break;
                    787:        default:
                    788:                term_newln(p);
                    789:                p->offset = INDENT;
                    790:                break;
                    791:        }
                    792: }
                    793:
                    794:
1.1       kristaps  795: static void
1.45      kristaps  796: print_man_node(DECL_ARGS)
1.1       kristaps  797: {
1.65      joerg     798:        size_t           rm, rmax;
1.54      kristaps  799:        int              c;
1.1       kristaps  800:
                    801:        c = 1;
                    802:
                    803:        switch (n->type) {
                    804:        case(MAN_TEXT):
1.4       kristaps  805:                if (0 == *n->string) {
                    806:                        term_vspace(p);
1.1       kristaps  807:                        break;
                    808:                }
1.54      kristaps  809:
1.4       kristaps  810:                term_word(p, n->string);
1.52      kristaps  811:
1.19      kristaps  812:                /* FIXME: this means that macro lines are munged!  */
1.52      kristaps  813:
1.24      kristaps  814:                if (MANT_LITERAL & mt->fl) {
1.65      joerg     815:                        rm = p->rmargin;
                    816:                        rmax = p->maxrmargin;
1.60      kristaps  817:                        p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
1.19      kristaps  818:                        p->flags |= TERMP_NOSPACE;
                    819:                        term_flushln(p);
1.65      joerg     820:                        p->rmargin = rm;
                    821:                        p->maxrmargin = rmax;
1.19      kristaps  822:                }
1.1       kristaps  823:                break;
                    824:        default:
1.56      kristaps  825:                if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
                    826:                        term_fontrepl(p, TERMFONT_NONE);
1.19      kristaps  827:                if (termacts[n->tok].pre)
1.24      kristaps  828:                        c = (*termacts[n->tok].pre)(p, mt, n, m);
1.1       kristaps  829:                break;
                    830:        }
                    831:
                    832:        if (c && n->child)
1.52      kristaps  833:                print_man_nodelist(p, mt, n->child, m);
1.1       kristaps  834:
1.51      kristaps  835:        if (MAN_TEXT != n->type) {
1.1       kristaps  836:                if (termacts[n->tok].post)
1.24      kristaps  837:                        (*termacts[n->tok].post)(p, mt, n, m);
1.56      kristaps  838:                if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
                    839:                        term_fontrepl(p, TERMFONT_NONE);
1.51      kristaps  840:        }
1.63      kristaps  841:
                    842:        if (MAN_EOS & n->flags)
                    843:                p->flags |= TERMP_SENTENCE;
1.1       kristaps  844: }
                    845:
                    846:
                    847: static void
1.52      kristaps  848: print_man_nodelist(DECL_ARGS)
1.1       kristaps  849: {
1.19      kristaps  850:
1.45      kristaps  851:        print_man_node(p, mt, n, m);
1.1       kristaps  852:        if ( ! n->next)
                    853:                return;
1.52      kristaps  854:        print_man_nodelist(p, mt, n->next, m);
1.1       kristaps  855: }
                    856:
                    857:
1.31      kristaps  858: static void
1.45      kristaps  859: print_man_foot(struct termp *p, const struct man_meta *meta)
1.1       kristaps  860: {
1.43      kristaps  861:        char            buf[DATESIZ];
1.1       kristaps  862:
1.52      kristaps  863:        term_fontrepl(p, TERMFONT_NONE);
1.50      kristaps  864:
1.43      kristaps  865:        time2a(meta->date, buf, DATESIZ);
1.1       kristaps  866:
                    867:        term_vspace(p);
                    868:
                    869:        p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
                    870:        p->rmargin = p->maxrmargin - strlen(buf);
                    871:        p->offset = 0;
                    872:
                    873:        if (meta->source)
                    874:                term_word(p, meta->source);
                    875:        if (meta->source)
                    876:                term_word(p, "");
                    877:        term_flushln(p);
                    878:
                    879:        p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
                    880:        p->offset = p->rmargin;
                    881:        p->rmargin = p->maxrmargin;
                    882:        p->flags &= ~TERMP_NOBREAK;
                    883:
                    884:        term_word(p, buf);
                    885:        term_flushln(p);
                    886: }
                    887:
                    888:
1.31      kristaps  889: static void
1.46      kristaps  890: print_man_head(struct termp *p, const struct man_meta *m)
1.1       kristaps  891: {
1.46      kristaps  892:        char            buf[BUFSIZ], title[BUFSIZ];
1.57      kristaps  893:        size_t          buflen, titlen;
1.1       kristaps  894:
1.62      kristaps  895:        /*
                    896:         * Note that old groff would spit out some spaces before the
                    897:         * header.  We discontinue this strange behaviour, but at one
                    898:         * point we did so here.
                    899:         */
                    900:
1.1       kristaps  901:        p->rmargin = p->maxrmargin;
1.58      kristaps  902:
1.1       kristaps  903:        p->offset = 0;
1.46      kristaps  904:        buf[0] = title[0] = '\0';
1.1       kristaps  905:
1.46      kristaps  906:        if (m->vol)
                    907:                strlcpy(buf, m->vol, BUFSIZ);
1.57      kristaps  908:        buflen = strlen(buf);
1.1       kristaps  909:
1.64      kristaps  910:        snprintf(title, BUFSIZ, "%s(%s)", m->title, m->msec);
1.57      kristaps  911:        titlen = strlen(title);
1.1       kristaps  912:
                    913:        p->offset = 0;
1.57      kristaps  914:        p->rmargin = 2 * (titlen+1) + buflen < p->maxrmargin ?
                    915:            (p->maxrmargin - strlen(buf) + 1) / 2 :
                    916:            p->maxrmargin - buflen;
1.1       kristaps  917:        p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
                    918:
                    919:        term_word(p, title);
                    920:        term_flushln(p);
                    921:
                    922:        p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
                    923:        p->offset = p->rmargin;
1.57      kristaps  924:        p->rmargin = p->offset + buflen + titlen < p->maxrmargin ?
                    925:            p->maxrmargin - titlen : p->maxrmargin;
1.1       kristaps  926:
                    927:        term_word(p, buf);
                    928:        term_flushln(p);
                    929:
                    930:        p->flags &= ~TERMP_NOBREAK;
1.57      kristaps  931:        if (p->rmargin + titlen <= p->maxrmargin) {
                    932:                p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
                    933:                p->offset = p->rmargin;
                    934:                p->rmargin = p->maxrmargin;
                    935:                term_word(p, title);
                    936:                term_flushln(p);
                    937:        }
1.1       kristaps  938:
                    939:        p->rmargin = p->maxrmargin;
                    940:        p->offset = 0;
                    941:        p->flags &= ~TERMP_NOSPACE;
1.61      kristaps  942:
1.62      kristaps  943:        /*
                    944:         * Groff likes to have some leading spaces before content.  Well
                    945:         * that's fine by me.
                    946:         */
                    947:
1.61      kristaps  948:        term_vspace(p);
                    949:        term_vspace(p);
                    950:        term_vspace(p);
1.1       kristaps  951: }

CVSweb