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

Annotation of mandoc/mdoc_term.c, Revision 1.6

1.6     ! kristaps    1: /*     $Id: mdoc_term.c,v 1.5 2009/04/12 19:19:57 kristaps Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
1.6     ! 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.6     ! 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:  */
                     17: #include <sys/types.h>
                     18:
                     19: #include <assert.h>
                     20: #include <ctype.h>
                     21: #include <err.h>
                     22: #include <stdio.h>
                     23: #include <stdlib.h>
                     24: #include <string.h>
                     25:
                     26: #include "term.h"
                     27: #include "mdoc.h"
                     28:
                     29: /* FIXME: macro arguments can be escaped. */
                     30: /* FIXME: support more offset/width tokens. */
                     31:
                     32: #define        TTYPE_PROG        0
                     33: #define        TTYPE_CMD_FLAG    1
                     34: #define        TTYPE_CMD_ARG     2
                     35: #define        TTYPE_SECTION     3
                     36: #define        TTYPE_FUNC_DECL   4
                     37: #define        TTYPE_VAR_DECL    5
                     38: #define        TTYPE_FUNC_TYPE   6
                     39: #define        TTYPE_FUNC_NAME   7
                     40: #define        TTYPE_FUNC_ARG    8
                     41: #define        TTYPE_LINK        9
                     42: #define        TTYPE_SSECTION    10
                     43: #define        TTYPE_FILE        11
                     44: #define        TTYPE_EMPH        12
                     45: #define        TTYPE_CONFIG      13
                     46: #define        TTYPE_CMD         14
                     47: #define        TTYPE_INCLUDE     15
                     48: #define        TTYPE_SYMB        16
                     49: #define        TTYPE_SYMBOL      17
                     50: #define        TTYPE_DIAG        18
                     51: #define        TTYPE_LINK_ANCHOR 19
                     52: #define        TTYPE_LINK_TEXT   20
                     53: #define        TTYPE_REF_JOURNAL 21
                     54: #define        TTYPE_LIST        22
                     55: #define        TTYPE_NMAX        23
                     56:
                     57: const  int ttypes[TTYPE_NMAX] = {
                     58:        TERMP_BOLD,             /* TTYPE_PROG */
                     59:        TERMP_BOLD,             /* TTYPE_CMD_FLAG */
                     60:        TERMP_UNDER,            /* TTYPE_CMD_ARG */
                     61:        TERMP_BOLD,             /* TTYPE_SECTION */
                     62:        TERMP_BOLD,             /* TTYPE_FUNC_DECL */
                     63:        TERMP_UNDER,            /* TTYPE_VAR_DECL */
                     64:        TERMP_UNDER,            /* TTYPE_FUNC_TYPE */
                     65:        TERMP_BOLD,             /* TTYPE_FUNC_NAME */
                     66:        TERMP_UNDER,            /* TTYPE_FUNC_ARG */
                     67:        TERMP_UNDER,            /* TTYPE_LINK */
                     68:        TERMP_BOLD,             /* TTYPE_SSECTION */
                     69:        TERMP_UNDER,            /* TTYPE_FILE */
                     70:        TERMP_UNDER,            /* TTYPE_EMPH */
                     71:        TERMP_BOLD,             /* TTYPE_CONFIG */
                     72:        TERMP_BOLD,             /* TTYPE_CMD */
                     73:        TERMP_BOLD,             /* TTYPE_INCLUDE */
                     74:        TERMP_BOLD,             /* TTYPE_SYMB */
                     75:        TERMP_BOLD,             /* TTYPE_SYMBOL */
                     76:        TERMP_BOLD,             /* TTYPE_DIAG */
                     77:        TERMP_UNDER,            /* TTYPE_LINK_ANCHOR */
                     78:        TERMP_BOLD,             /* TTYPE_LINK_TEXT */
                     79:        TERMP_UNDER,            /* TTYPE_REF_JOURNAL */
                     80:        TERMP_BOLD              /* TTYPE_LIST */
                     81: };
                     82:
                     83: /* XXX - clean this up. */
                     84:
                     85: struct termpair {
                     86:        struct termpair  *ppair;
                     87:        int               type;
                     88: #define        TERMPAIR_FLAG    (1 << 0)
                     89:        int               flag;
                     90:        size_t            offset;
                     91:        size_t            rmargin;
                     92:        int               count;
                     93: };
                     94:
                     95: #define        TERMPAIR_SETFLAG(termp, p, fl) \
                     96:        do { \
                     97:                assert(! (TERMPAIR_FLAG & (p)->type)); \
                     98:                (termp)->flags |= (fl); \
                     99:                (p)->flag = (fl); \
                    100:                (p)->type |= TERMPAIR_FLAG; \
                    101:        } while ( /* CONSTCOND */ 0)
                    102:
                    103: #define        DECL_ARGS \
                    104:        struct termp *p, struct termpair *pair, \
                    105:        const struct mdoc_meta *meta, \
                    106:        const struct mdoc_node *node
                    107:
                    108: #define        DECL_PRE(name) \
                    109: static int               name##_pre(DECL_ARGS)
                    110: #define        DECL_POST(name) \
                    111: static void              name##_post(DECL_ARGS)
                    112: #define        DECL_PREPOST(name) \
                    113: DECL_PRE(name); \
                    114: DECL_POST(name);
                    115:
                    116: DECL_PREPOST(termp__t);
                    117: DECL_PREPOST(termp_aq);
                    118: DECL_PREPOST(termp_bd);
                    119: DECL_PREPOST(termp_bq);
                    120: DECL_PREPOST(termp_brq);
                    121: DECL_PREPOST(termp_d1);
                    122: DECL_PREPOST(termp_dq);
                    123: DECL_PREPOST(termp_fd);
                    124: DECL_PREPOST(termp_fn);
                    125: DECL_PREPOST(termp_fo);
                    126: DECL_PREPOST(termp_ft);
                    127: DECL_PREPOST(termp_in);
                    128: DECL_PREPOST(termp_it);
                    129: DECL_PREPOST(termp_lb);
                    130: DECL_PREPOST(termp_op);
                    131: DECL_PREPOST(termp_pf);
                    132: DECL_PREPOST(termp_pq);
                    133: DECL_PREPOST(termp_qq);
                    134: DECL_PREPOST(termp_sh);
                    135: DECL_PREPOST(termp_ss);
                    136: DECL_PREPOST(termp_sq);
                    137: DECL_PREPOST(termp_vt);
                    138:
                    139: DECL_PRE(termp__j);
                    140: DECL_PRE(termp_ap);
                    141: DECL_PRE(termp_ar);
                    142: DECL_PRE(termp_at);
                    143: DECL_PRE(termp_bf);
                    144: DECL_PRE(termp_bsx);
                    145: DECL_PRE(termp_bt);
                    146: DECL_PRE(termp_cd);
                    147: DECL_PRE(termp_cm);
                    148: DECL_PRE(termp_dx);
                    149: DECL_PRE(termp_em);
                    150: DECL_PRE(termp_ex);
                    151: DECL_PRE(termp_fa);
                    152: DECL_PRE(termp_fl);
                    153: DECL_PRE(termp_fx);
                    154: DECL_PRE(termp_ic);
                    155: DECL_PRE(termp_lk);
                    156: DECL_PRE(termp_ms);
                    157: DECL_PRE(termp_mt);
                    158: DECL_PRE(termp_nd);
                    159: DECL_PRE(termp_nm);
                    160: DECL_PRE(termp_ns);
                    161: DECL_PRE(termp_nx);
                    162: DECL_PRE(termp_ox);
                    163: DECL_PRE(termp_pa);
                    164: DECL_PRE(termp_pp);
                    165: DECL_PRE(termp_rs);
                    166: DECL_PRE(termp_rv);
                    167: DECL_PRE(termp_sm);
                    168: DECL_PRE(termp_st);
                    169: DECL_PRE(termp_sx);
                    170: DECL_PRE(termp_sy);
                    171: DECL_PRE(termp_ud);
                    172: DECL_PRE(termp_ux);
                    173: DECL_PRE(termp_va);
                    174: DECL_PRE(termp_xr);
                    175:
                    176: DECL_POST(termp___);
                    177: DECL_POST(termp_bl);
                    178: DECL_POST(termp_bx);
                    179:
                    180: struct termact {
                    181:        int     (*pre)(DECL_ARGS);
                    182:        void    (*post)(DECL_ARGS);
                    183: };
                    184:
                    185: static const struct termact termacts[MDOC_MAX] = {
                    186:        { NULL, NULL }, /* \" */
                    187:        { NULL, NULL }, /* Dd */
                    188:        { NULL, NULL }, /* Dt */
                    189:        { NULL, NULL }, /* Os */
                    190:        { termp_sh_pre, termp_sh_post }, /* Sh */
                    191:        { termp_ss_pre, termp_ss_post }, /* Ss */
                    192:        { termp_pp_pre, NULL }, /* Pp */
                    193:        { termp_d1_pre, termp_d1_post }, /* D1 */
                    194:        { termp_d1_pre, termp_d1_post }, /* Dl */
                    195:        { termp_bd_pre, termp_bd_post }, /* Bd */
                    196:        { NULL, NULL }, /* Ed */
                    197:        { NULL, termp_bl_post }, /* Bl */
                    198:        { NULL, NULL }, /* El */
                    199:        { termp_it_pre, termp_it_post }, /* It */
                    200:        { NULL, NULL }, /* Ad */
                    201:        { NULL, NULL }, /* An */
                    202:        { termp_ar_pre, NULL }, /* Ar */
                    203:        { termp_cd_pre, NULL }, /* Cd */
                    204:        { termp_cm_pre, NULL }, /* Cm */
                    205:        { NULL, NULL }, /* Dv */
                    206:        { NULL, NULL }, /* Er */
                    207:        { NULL, NULL }, /* Ev */
                    208:        { termp_ex_pre, NULL }, /* Ex */
                    209:        { termp_fa_pre, NULL }, /* Fa */
                    210:        { termp_fd_pre, termp_fd_post }, /* Fd */
                    211:        { termp_fl_pre, NULL }, /* Fl */
                    212:        { termp_fn_pre, termp_fn_post }, /* Fn */
                    213:        { termp_ft_pre, termp_ft_post }, /* Ft */
                    214:        { termp_ic_pre, NULL }, /* Ic */
                    215:        { termp_in_pre, termp_in_post }, /* In */
                    216:        { NULL, NULL }, /* Li */
                    217:        { termp_nd_pre, NULL }, /* Nd */
                    218:        { termp_nm_pre, NULL }, /* Nm */
                    219:        { termp_op_pre, termp_op_post }, /* Op */
                    220:        { NULL, NULL }, /* Ot */
                    221:        { termp_pa_pre, NULL }, /* Pa */
                    222:        { termp_rv_pre, NULL }, /* Rv */
                    223:        { termp_st_pre, NULL }, /* St */
                    224:        { termp_va_pre, NULL }, /* Va */
                    225:        { termp_vt_pre, termp_vt_post }, /* Vt */
                    226:        { termp_xr_pre, NULL }, /* Xr */
                    227:        { NULL, termp____post }, /* %A */
                    228:        { NULL, termp____post }, /* %B */
                    229:        { NULL, termp____post }, /* %D */
                    230:        { NULL, termp____post }, /* %I */
                    231:        { termp__j_pre, termp____post }, /* %J */
                    232:        { NULL, termp____post }, /* %N */
                    233:        { NULL, termp____post }, /* %O */
                    234:        { NULL, termp____post }, /* %P */
                    235:        { NULL, termp____post }, /* %R */
                    236:        { termp__t_pre, termp__t_post }, /* %T */
                    237:        { NULL, termp____post }, /* %V */
                    238:        { NULL, NULL }, /* Ac */
                    239:        { termp_aq_pre, termp_aq_post }, /* Ao */
                    240:        { termp_aq_pre, termp_aq_post }, /* Aq */
                    241:        { termp_at_pre, NULL }, /* At */
                    242:        { NULL, NULL }, /* Bc */
                    243:        { termp_bf_pre, NULL }, /* Bf */
                    244:        { termp_bq_pre, termp_bq_post }, /* Bo */
                    245:        { termp_bq_pre, termp_bq_post }, /* Bq */
                    246:        { termp_bsx_pre, NULL }, /* Bsx */
                    247:        { NULL, termp_bx_post }, /* Bx */
                    248:        { NULL, NULL }, /* Db */
                    249:        { NULL, NULL }, /* Dc */
                    250:        { termp_dq_pre, termp_dq_post }, /* Do */
                    251:        { termp_dq_pre, termp_dq_post }, /* Dq */
                    252:        { NULL, NULL }, /* Ec */
                    253:        { NULL, NULL }, /* Ef */
                    254:        { termp_em_pre, NULL }, /* Em */
                    255:        { NULL, NULL }, /* Eo */
                    256:        { termp_fx_pre, NULL }, /* Fx */
                    257:        { termp_ms_pre, NULL }, /* Ms */
                    258:        { NULL, NULL }, /* No */
                    259:        { termp_ns_pre, NULL }, /* Ns */
                    260:        { termp_nx_pre, NULL }, /* Nx */
                    261:        { termp_ox_pre, NULL }, /* Ox */
                    262:        { NULL, NULL }, /* Pc */
                    263:        { termp_pf_pre, termp_pf_post }, /* Pf */
                    264:        { termp_pq_pre, termp_pq_post }, /* Po */
                    265:        { termp_pq_pre, termp_pq_post }, /* Pq */
                    266:        { NULL, NULL }, /* Qc */
                    267:        { termp_sq_pre, termp_sq_post }, /* Ql */
                    268:        { termp_qq_pre, termp_qq_post }, /* Qo */
                    269:        { termp_qq_pre, termp_qq_post }, /* Qq */
                    270:        { NULL, NULL }, /* Re */
                    271:        { termp_rs_pre, NULL }, /* Rs */
                    272:        { NULL, NULL }, /* Sc */
                    273:        { termp_sq_pre, termp_sq_post }, /* So */
                    274:        { termp_sq_pre, termp_sq_post }, /* Sq */
                    275:        { termp_sm_pre, NULL }, /* Sm */
                    276:        { termp_sx_pre, NULL }, /* Sx */
                    277:        { termp_sy_pre, NULL }, /* Sy */
                    278:        { NULL, NULL }, /* Tn */
                    279:        { termp_ux_pre, NULL }, /* Ux */
                    280:        { NULL, NULL }, /* Xc */
                    281:        { NULL, NULL }, /* Xo */
                    282:        { termp_fo_pre, termp_fo_post }, /* Fo */
                    283:        { NULL, NULL }, /* Fc */
                    284:        { termp_op_pre, termp_op_post }, /* Oo */
                    285:        { NULL, NULL }, /* Oc */
                    286:        { NULL, NULL }, /* Bk */
                    287:        { NULL, NULL }, /* Ek */
                    288:        { termp_bt_pre, NULL }, /* Bt */
                    289:        { NULL, NULL }, /* Hf */
                    290:        { NULL, NULL }, /* Fr */
                    291:        { termp_ud_pre, NULL }, /* Ud */
                    292:        { termp_lb_pre, termp_lb_post }, /* Lb */
                    293:        { termp_ap_pre, NULL }, /* Lb */
                    294:        { termp_pp_pre, NULL }, /* Pp */
                    295:        { termp_lk_pre, NULL }, /* Lk */
                    296:        { termp_mt_pre, NULL }, /* Mt */
                    297:        { termp_brq_pre, termp_brq_post }, /* Brq */
                    298:        { termp_brq_pre, termp_brq_post }, /* Bro */
                    299:        { NULL, NULL }, /* Brc */
                    300:        { NULL, NULL }, /* %C */
                    301:        { NULL, NULL }, /* Es */
                    302:        { NULL, NULL }, /* En */
                    303:        { termp_dx_pre, NULL }, /* Dx */
                    304:        { NULL, NULL }, /* %Q */
                    305: };
                    306:
1.2       kristaps  307: #ifdef __linux__
                    308: extern size_t            strlcpy(char *, const char *, size_t);
                    309: extern size_t            strlcat(char *, const char *, size_t);
                    310: #endif
                    311:
1.1       kristaps  312: static int       arg_hasattr(int, const struct mdoc_node *);
                    313: static int       arg_getattrs(const int *, int *, size_t,
                    314:                        const struct mdoc_node *);
                    315: static int       arg_getattr(int, const struct mdoc_node *);
                    316: static size_t    arg_offset(const struct mdoc_argv *);
                    317: static size_t    arg_width(const struct mdoc_argv *, int);
                    318: static int       arg_listtype(const struct mdoc_node *);
                    319: static int       fmt_block_vspace(struct termp *,
                    320:                        const struct mdoc_node *,
                    321:                        const struct mdoc_node *);
1.3       kristaps  322: static void      print_node(DECL_ARGS);
                    323: static void      print_head(struct termp *,
1.1       kristaps  324:                        const struct mdoc_meta *);
1.3       kristaps  325: static void      print_body(DECL_ARGS);
                    326: static void      print_foot(struct termp *,
1.1       kristaps  327:                        const struct mdoc_meta *);
                    328: static void      sanity(const struct mdoc_node *);
                    329:
                    330:
                    331: int
                    332: mdoc_run(struct termp *p, const struct mdoc *m)
                    333: {
                    334:
1.3       kristaps  335:        print_head(p, mdoc_meta(m));
                    336:        print_body(p, NULL, mdoc_meta(m), mdoc_node(m));
                    337:        print_foot(p, mdoc_meta(m));
                    338:        return(1);
1.1       kristaps  339: }
                    340:
                    341:
1.3       kristaps  342: static void
1.1       kristaps  343: print_body(DECL_ARGS)
                    344: {
                    345:
1.3       kristaps  346:        print_node(p, pair, meta, node);
1.1       kristaps  347:        if ( ! node->next)
1.3       kristaps  348:                return;
                    349:        print_body(p, pair, meta, node->next);
1.1       kristaps  350: }
                    351:
                    352:
1.3       kristaps  353: static void
1.1       kristaps  354: print_node(DECL_ARGS)
                    355: {
                    356:        int              dochild;
                    357:        struct termpair  npair;
                    358:
                    359:        /* Some quick sanity-checking. */
                    360:
                    361:        sanity(node);
                    362:
                    363:        /* Pre-processing. */
                    364:
                    365:        dochild = 1;
                    366:        npair.ppair = pair;
                    367:        npair.type = 0;
                    368:        npair.offset = npair.rmargin = 0;
                    369:        npair.flag = 0;
                    370:        npair.count = 0;
                    371:
                    372:        if (MDOC_TEXT != node->type) {
                    373:                if (termacts[node->tok].pre)
                    374:                        if ( ! (*termacts[node->tok].pre)(p, &npair, meta, node))
                    375:                                dochild = 0;
                    376:        } else /* MDOC_TEXT == node->type */
                    377:                term_word(p, node->string);
                    378:
                    379:        /* Children. */
                    380:
                    381:        if (TERMPAIR_FLAG & npair.type)
                    382:                p->flags |= npair.flag;
                    383:
                    384:        if (dochild && node->child)
                    385:                print_body(p, &npair, meta, node->child);
                    386:
                    387:        if (TERMPAIR_FLAG & npair.type)
                    388:                p->flags &= ~npair.flag;
                    389:
                    390:        /* Post-processing. */
                    391:
                    392:        if (MDOC_TEXT != node->type)
                    393:                if (termacts[node->tok].post)
                    394:                        (*termacts[node->tok].post)(p, &npair, meta, node);
                    395: }
                    396:
                    397:
1.3       kristaps  398: static void
1.1       kristaps  399: print_foot(struct termp *p, const struct mdoc_meta *meta)
                    400: {
                    401:        struct tm       *tm;
                    402:        char            *buf, *os;
                    403:
                    404:        if (NULL == (buf = malloc(p->rmargin)))
                    405:                err(1, "malloc");
                    406:        if (NULL == (os = malloc(p->rmargin)))
                    407:                err(1, "malloc");
                    408:
                    409:        tm = localtime(&meta->date);
                    410:
                    411: #ifdef __OpenBSD__
                    412:        if (NULL == strftime(buf, p->rmargin, "%B %d, %Y", tm))
                    413: #else
                    414:        if (0 == strftime(buf, p->rmargin, "%B %d, %Y", tm))
                    415: #endif
                    416:                err(1, "strftime");
                    417:
                    418:        (void)strlcpy(os, meta->os, p->rmargin);
                    419:
                    420:        /*
                    421:         * This is /slightly/ different from regular groff output
                    422:         * because we don't have page numbers.  Print the following:
                    423:         *
                    424:         * OS                                            MDOCDATE
                    425:         */
                    426:
                    427:        term_vspace(p);
                    428:
                    429:        p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
                    430:        p->rmargin = p->maxrmargin - strlen(buf);
                    431:        p->offset = 0;
                    432:
                    433:        term_word(p, os);
                    434:        term_flushln(p);
                    435:
                    436:        p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
                    437:        p->offset = p->rmargin;
                    438:        p->rmargin = p->maxrmargin;
                    439:        p->flags &= ~TERMP_NOBREAK;
                    440:
                    441:        term_word(p, buf);
                    442:        term_flushln(p);
                    443:
                    444:        free(buf);
                    445:        free(os);
                    446: }
                    447:
                    448:
1.3       kristaps  449: static void
1.1       kristaps  450: print_head(struct termp *p, const struct mdoc_meta *meta)
                    451: {
                    452:        char            *buf, *title;
                    453:
                    454:        p->rmargin = p->maxrmargin;
                    455:        p->offset = 0;
                    456:
                    457:        if (NULL == (buf = malloc(p->rmargin)))
                    458:                err(1, "malloc");
                    459:        if (NULL == (title = malloc(p->rmargin)))
                    460:                err(1, "malloc");
                    461:
                    462:        /*
                    463:         * The header is strange.  It has three components, which are
                    464:         * really two with the first duplicated.  It goes like this:
                    465:         *
                    466:         * IDENTIFIER              TITLE                   IDENTIFIER
                    467:         *
                    468:         * The IDENTIFIER is NAME(SECTION), which is the command-name
                    469:         * (if given, or "unknown" if not) followed by the manual page
                    470:         * section.  These are given in `Dt'.  The TITLE is a free-form
                    471:         * string depending on the manual volume.  If not specified, it
                    472:         * switches on the manual section.
                    473:         */
                    474:
                    475:        assert(meta->vol);
                    476:        (void)strlcpy(buf, meta->vol, p->rmargin);
                    477:
                    478:        if (meta->arch) {
                    479:                (void)strlcat(buf, " (", p->rmargin);
                    480:                (void)strlcat(buf, meta->arch, p->rmargin);
                    481:                (void)strlcat(buf, ")", p->rmargin);
                    482:        }
                    483:
                    484:        (void)snprintf(title, p->rmargin, "%s(%d)",
                    485:                        meta->title, meta->msec);
                    486:
                    487:        p->offset = 0;
                    488:        p->rmargin = (p->maxrmargin - strlen(buf)) / 2;
                    489:        p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
                    490:
                    491:        term_word(p, title);
                    492:        term_flushln(p);
                    493:
                    494:        p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
                    495:        p->offset = p->rmargin;
                    496:        p->rmargin = p->maxrmargin - strlen(title);
                    497:
                    498:        term_word(p, buf);
                    499:        term_flushln(p);
                    500:
                    501:        p->offset = p->rmargin;
                    502:        p->rmargin = p->maxrmargin;
                    503:        p->flags &= ~TERMP_NOBREAK;
                    504:        p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
                    505:
                    506:        term_word(p, title);
                    507:        term_flushln(p);
                    508:
                    509:        p->rmargin = p->maxrmargin;
                    510:        p->offset = 0;
                    511:        p->flags &= ~TERMP_NOSPACE;
                    512:
                    513:        free(title);
                    514:        free(buf);
                    515: }
                    516:
                    517:
                    518: static void
                    519: sanity(const struct mdoc_node *n)
                    520: {
                    521:        char            *p;
                    522:
                    523:        p = "regular form violated";
                    524:
                    525:        switch (n->type) {
                    526:        case (MDOC_TEXT):
                    527:                if (n->child)
                    528:                        errx(1, p);
                    529:                if (NULL == n->parent)
                    530:                        errx(1, p);
                    531:                if (NULL == n->string)
                    532:                        errx(1, p);
                    533:                switch (n->parent->type) {
                    534:                case (MDOC_TEXT):
                    535:                        /* FALLTHROUGH */
                    536:                case (MDOC_ROOT):
                    537:                        errx(1, p);
                    538:                        /* NOTREACHED */
                    539:                default:
                    540:                        break;
                    541:                }
                    542:                break;
                    543:        case (MDOC_ELEM):
                    544:                if (NULL == n->parent)
                    545:                        errx(1, p);
                    546:                switch (n->parent->type) {
                    547:                case (MDOC_TAIL):
                    548:                        /* FALLTHROUGH */
                    549:                case (MDOC_BODY):
                    550:                        /* FALLTHROUGH */
                    551:                case (MDOC_HEAD):
                    552:                        break;
                    553:                default:
                    554:                        errx(1, p);
                    555:                        /* NOTREACHED */
                    556:                }
                    557:                if (n->child) switch (n->child->type) {
                    558:                case (MDOC_TEXT):
                    559:                        break;
                    560:                default:
                    561:                        errx(1, p);
                    562:                        /* NOTREACHED */
                    563:                }
                    564:                break;
                    565:        case (MDOC_HEAD):
                    566:                /* FALLTHROUGH */
                    567:        case (MDOC_BODY):
                    568:                /* FALLTHROUGH */
                    569:        case (MDOC_TAIL):
                    570:                if (NULL == n->parent)
                    571:                        errx(1, p);
                    572:                if (MDOC_BLOCK != n->parent->type)
                    573:                        errx(1, p);
                    574:                if (n->child) switch (n->child->type) {
                    575:                case (MDOC_BLOCK):
                    576:                        /* FALLTHROUGH */
                    577:                case (MDOC_ELEM):
                    578:                        /* FALLTHROUGH */
                    579:                case (MDOC_TEXT):
                    580:                        break;
                    581:                default:
                    582:                        errx(1, p);
                    583:                        /* NOTREACHED */
                    584:                }
                    585:                break;
                    586:        case (MDOC_BLOCK):
                    587:                if (NULL == n->parent)
                    588:                        errx(1, p);
                    589:                if (NULL == n->child)
                    590:                        errx(1, p);
                    591:                switch (n->parent->type) {
                    592:                case (MDOC_ROOT):
                    593:                        /* FALLTHROUGH */
                    594:                case (MDOC_HEAD):
                    595:                        /* FALLTHROUGH */
                    596:                case (MDOC_BODY):
                    597:                        /* FALLTHROUGH */
                    598:                case (MDOC_TAIL):
                    599:                        break;
                    600:                default:
                    601:                        errx(1, p);
                    602:                        /* NOTREACHED */
                    603:                }
                    604:                switch (n->child->type) {
                    605:                case (MDOC_ROOT):
                    606:                        /* FALLTHROUGH */
                    607:                case (MDOC_ELEM):
                    608:                        errx(1, p);
                    609:                        /* NOTREACHED */
                    610:                default:
                    611:                        break;
                    612:                }
                    613:                break;
                    614:        case (MDOC_ROOT):
                    615:                if (n->parent)
                    616:                        errx(1, p);
                    617:                if (NULL == n->child)
                    618:                        errx(1, p);
                    619:                switch (n->child->type) {
                    620:                case (MDOC_BLOCK):
                    621:                        break;
                    622:                default:
                    623:                        errx(1, p);
                    624:                        /* NOTREACHED */
                    625:                }
                    626:                break;
                    627:        }
                    628: }
                    629:
                    630:
                    631: static size_t
                    632: arg_width(const struct mdoc_argv *arg, int pos)
                    633: {
                    634:        size_t           v;
                    635:        int              i, len;
                    636:
                    637:        assert(pos < (int)arg->sz && pos >= 0);
                    638:        assert(arg->value[pos]);
                    639:        if (0 == strcmp(arg->value[pos], "indent"))
                    640:                return(INDENT);
                    641:        if (0 == strcmp(arg->value[pos], "indent-two"))
                    642:                return(INDENT * 2);
                    643:
                    644:        if (0 == (len = (int)strlen(arg->value[pos])))
                    645:                return(0);
                    646:
                    647:        for (i = 0; i < len - 1; i++)
                    648:                if ( ! isdigit((u_char)arg->value[pos][i]))
                    649:                        break;
                    650:
                    651:        if (i == len - 1) {
                    652:                if ('n' == arg->value[pos][len - 1]) {
                    653:                        v = (size_t)atoi(arg->value[pos]);
                    654:                        return(v);
                    655:                }
                    656:
                    657:        }
                    658:        return(strlen(arg->value[pos]) + 1);
                    659: }
                    660:
                    661:
                    662: static int
                    663: arg_listtype(const struct mdoc_node *n)
                    664: {
                    665:        int              i, len;
                    666:
                    667:        assert(MDOC_BLOCK == n->type);
                    668:
                    669:        len = (int)(n->args ? n->args->argc : 0);
                    670:
                    671:        for (i = 0; i < len; i++)
                    672:                switch (n->args->argv[i].arg) {
                    673:                case (MDOC_Bullet):
                    674:                        /* FALLTHROUGH */
                    675:                case (MDOC_Dash):
                    676:                        /* FALLTHROUGH */
                    677:                case (MDOC_Enum):
                    678:                        /* FALLTHROUGH */
                    679:                case (MDOC_Hyphen):
                    680:                        /* FALLTHROUGH */
                    681:                case (MDOC_Tag):
                    682:                        /* FALLTHROUGH */
                    683:                case (MDOC_Inset):
                    684:                        /* FALLTHROUGH */
                    685:                case (MDOC_Diag):
                    686:                        /* FALLTHROUGH */
                    687:                case (MDOC_Item):
                    688:                        /* FALLTHROUGH */
                    689:                case (MDOC_Column):
                    690:                        /* FALLTHROUGH */
                    691:                case (MDOC_Ohang):
                    692:                        return(n->args->argv[i].arg);
                    693:                default:
                    694:                        break;
                    695:                }
                    696:
                    697:        errx(1, "list type not supported");
                    698:        /* NOTREACHED */
                    699: }
                    700:
                    701:
                    702: static size_t
                    703: arg_offset(const struct mdoc_argv *arg)
                    704: {
                    705:
                    706:        assert(*arg->value);
                    707:        if (0 == strcmp(*arg->value, "indent"))
                    708:                return(INDENT);
                    709:        if (0 == strcmp(*arg->value, "indent-two"))
                    710:                return(INDENT * 2);
                    711:        return(strlen(*arg->value));
                    712: }
                    713:
                    714:
                    715: static int
                    716: arg_hasattr(int arg, const struct mdoc_node *n)
                    717: {
                    718:
                    719:        return(-1 != arg_getattr(arg, n));
                    720: }
                    721:
                    722:
                    723: static int
                    724: arg_getattr(int v, const struct mdoc_node *n)
                    725: {
                    726:        int              val;
                    727:
                    728:        return(arg_getattrs(&v, &val, 1, n) ? val : -1);
                    729: }
                    730:
                    731:
                    732: static int
                    733: arg_getattrs(const int *keys, int *vals,
                    734:                size_t sz, const struct mdoc_node *n)
                    735: {
                    736:        int              i, j, k;
                    737:
                    738:        if (NULL == n->args)
                    739:                return(0);
                    740:
                    741:        for (k = i = 0; i < (int)n->args->argc; i++)
                    742:                for (j = 0; j < (int)sz; j++)
                    743:                        if (n->args->argv[i].arg == keys[j]) {
                    744:                                vals[j] = i;
                    745:                                k++;
                    746:                        }
                    747:        return(k);
                    748: }
                    749:
                    750:
                    751: /* ARGSUSED */
                    752: static int
                    753: fmt_block_vspace(struct termp *p,
                    754:                const struct mdoc_node *bl,
                    755:                const struct mdoc_node *node)
                    756: {
                    757:        const struct mdoc_node *n;
                    758:
                    759:        term_newln(p);
                    760:
                    761:        if (arg_hasattr(MDOC_Compact, bl))
                    762:                return(1);
                    763:
                    764:        for (n = node; n; n = n->parent) {
                    765:                if (MDOC_BLOCK != n->type)
                    766:                        continue;
                    767:                if (MDOC_Ss == n->tok)
                    768:                        break;
                    769:                if (MDOC_Sh == n->tok)
                    770:                        break;
                    771:                if (NULL == n->prev)
                    772:                        continue;
                    773:                term_vspace(p);
                    774:                break;
                    775:        }
                    776:
                    777:        return(1);
                    778: }
                    779:
                    780:
                    781: /* ARGSUSED */
                    782: static int
                    783: termp_dq_pre(DECL_ARGS)
                    784: {
                    785:
                    786:        if (MDOC_BODY != node->type)
                    787:                return(1);
                    788:
                    789:        term_word(p, "\\(lq");
                    790:        p->flags |= TERMP_NOSPACE;
                    791:        return(1);
                    792: }
                    793:
                    794:
                    795: /* ARGSUSED */
                    796: static void
                    797: termp_dq_post(DECL_ARGS)
                    798: {
                    799:
                    800:        if (MDOC_BODY != node->type)
                    801:                return;
                    802:
                    803:        p->flags |= TERMP_NOSPACE;
                    804:        term_word(p, "\\(rq");
                    805: }
                    806:
                    807:
                    808: /* ARGSUSED */
                    809: static int
                    810: termp_it_pre(DECL_ARGS)
                    811: {
                    812:        const struct mdoc_node *bl, *n;
                    813:        char                    buf[7];
                    814:        int                     i, type, keys[3], vals[3];
                    815:        size_t                  width, offset;
                    816:
                    817:        if (MDOC_BLOCK == node->type)
                    818:                return(fmt_block_vspace(p, node->parent->parent, node));
                    819:
                    820:        bl = node->parent->parent->parent;
                    821:
                    822:        /* Save parent attributes. */
                    823:
                    824:        pair->offset = p->offset;
                    825:        pair->rmargin = p->rmargin;
                    826:        pair->flag = p->flags;
                    827:
                    828:        /* Get list width and offset. */
                    829:
                    830:        keys[0] = MDOC_Width;
                    831:        keys[1] = MDOC_Offset;
                    832:        keys[2] = MDOC_Column;
                    833:
                    834:        vals[0] = vals[1] = vals[2] = -1;
                    835:
                    836:        width = offset = 0;
                    837:
                    838:        (void)arg_getattrs(keys, vals, 3, bl);
                    839:
                    840:        type = arg_listtype(bl);
                    841:
                    842:        /* Calculate real width and offset. */
                    843:
                    844:        switch (type) {
                    845:        case (MDOC_Column):
                    846:                if (MDOC_BODY == node->type)
                    847:                        break;
                    848:                for (i = 0, n = node->prev; n; n = n->prev, i++)
                    849:                        offset += arg_width
                    850:                                (&bl->args->argv[vals[2]], i);
                    851:                assert(i < (int)bl->args->argv[vals[2]].sz);
                    852:                width = arg_width(&bl->args->argv[vals[2]], i);
                    853:                if (vals[1] >= 0)
                    854:                        offset += arg_offset(&bl->args->argv[vals[1]]);
                    855:                break;
                    856:        default:
                    857:                if (vals[0] >= 0)
                    858:                        width = arg_width(&bl->args->argv[vals[0]], 0);
                    859:                if (vals[1] >= 0)
                    860:                        offset = arg_offset(&bl->args->argv[vals[1]]);
                    861:                break;
                    862:        }
                    863:
                    864:        /*
                    865:         * List-type can override the width in the case of fixed-head
                    866:         * values (bullet, dash/hyphen, enum).  Tags need a non-zero
                    867:         * offset.
                    868:         */
                    869:
                    870:        switch (type) {
                    871:        case (MDOC_Bullet):
                    872:                /* FALLTHROUGH */
                    873:        case (MDOC_Dash):
                    874:                /* FALLTHROUGH */
                    875:        case (MDOC_Enum):
                    876:                /* FALLTHROUGH */
                    877:        case (MDOC_Hyphen):
                    878:                if (width < 4)
                    879:                        width = 4;
                    880:                break;
                    881:        case (MDOC_Tag):
                    882:                if (0 == width)
                    883:                        width = 10;
                    884:                break;
                    885:        default:
                    886:                break;
                    887:        }
                    888:
                    889:        /*
                    890:         * Whitespace control.  Inset bodies need an initial space.
                    891:         */
                    892:
                    893:        switch (type) {
                    894:        case (MDOC_Diag):
                    895:                /* FALLTHROUGH */
                    896:        case (MDOC_Inset):
                    897:                if (MDOC_BODY == node->type)
                    898:                        p->flags &= ~TERMP_NOSPACE;
                    899:                else
                    900:                        p->flags |= TERMP_NOSPACE;
                    901:                break;
                    902:        default:
                    903:                p->flags |= TERMP_NOSPACE;
                    904:                break;
                    905:        }
                    906:
                    907:        /*
                    908:         * Style flags.  Diagnostic heads need TTYPE_DIAG.
                    909:         */
                    910:
                    911:        switch (type) {
                    912:        case (MDOC_Diag):
                    913:                if (MDOC_HEAD == node->type)
                    914:                        p->flags |= ttypes[TTYPE_DIAG];
                    915:                break;
                    916:        default:
                    917:                break;
                    918:        }
                    919:
                    920:        /*
                    921:         * Pad and break control.  This is the tricker part.  Lists with
                    922:         * set right-margins for the head get TERMP_NOBREAK because, if
                    923:         * they overrun the margin, they wrap to the new margin.
                    924:         * Correspondingly, the body for these types don't left-pad, as
                    925:         * the head will pad out to to the right.
                    926:         */
                    927:
                    928:        switch (type) {
                    929:        case (MDOC_Bullet):
                    930:                /* FALLTHROUGH */
                    931:        case (MDOC_Dash):
                    932:                /* FALLTHROUGH */
                    933:        case (MDOC_Enum):
                    934:                /* FALLTHROUGH */
                    935:        case (MDOC_Hyphen):
                    936:                /* FALLTHROUGH */
                    937:        case (MDOC_Tag):
                    938:                if (MDOC_HEAD == node->type)
                    939:                        p->flags |= TERMP_NOBREAK;
                    940:                else
                    941:                        p->flags |= TERMP_NOLPAD;
                    942:                if (MDOC_HEAD == node->type && MDOC_Tag == type)
                    943:                        if (NULL == node->next ||
                    944:                                        NULL == node->next->child)
                    945:                                p->flags |= TERMP_NONOBREAK;
                    946:                break;
                    947:        case (MDOC_Column):
                    948:                if (MDOC_HEAD == node->type) {
                    949:                        assert(node->next);
                    950:                        if (MDOC_BODY == node->next->type)
                    951:                                p->flags &= ~TERMP_NOBREAK;
                    952:                        else
                    953:                                p->flags |= TERMP_NOBREAK;
                    954:                        if (node->prev)
                    955:                                p->flags |= TERMP_NOLPAD;
                    956:                }
                    957:                break;
                    958:        case (MDOC_Diag):
                    959:                if (MDOC_HEAD == node->type)
                    960:                        p->flags |= TERMP_NOBREAK;
                    961:                break;
                    962:        default:
                    963:                break;
                    964:        }
                    965:
                    966:        /*
                    967:         * Margin control.  Set-head-width lists have their right
                    968:         * margins shortened.  The body for these lists has the offset
                    969:         * necessarily lengthened.  Everybody gets the offset.
                    970:         */
                    971:
                    972:        p->offset += offset;
                    973:
                    974:        switch (type) {
                    975:        case (MDOC_Bullet):
                    976:                /* FALLTHROUGH */
                    977:        case (MDOC_Dash):
                    978:                /* FALLTHROUGH */
                    979:        case (MDOC_Enum):
                    980:                /* FALLTHROUGH */
                    981:        case (MDOC_Hyphen):
                    982:                /* FALLTHROUGH */
                    983:        case (MDOC_Tag):
                    984:                if (MDOC_HEAD == node->type)
                    985:                        p->rmargin = p->offset + width;
                    986:                else
                    987:                        p->offset += width;
                    988:                break;
                    989:        case (MDOC_Column):
                    990:                p->rmargin = p->offset + width;
                    991:                break;
                    992:        default:
                    993:                break;
                    994:        }
                    995:
                    996:        /*
                    997:         * The dash, hyphen, bullet and enum lists all have a special
                    998:         * HEAD character.  Print it now.
                    999:         */
                   1000:
                   1001:        if (MDOC_HEAD == node->type)
                   1002:                switch (type) {
                   1003:                case (MDOC_Bullet):
                   1004:                        term_word(p, "\\[bu]");
                   1005:                        break;
                   1006:                case (MDOC_Dash):
                   1007:                        /* FALLTHROUGH */
                   1008:                case (MDOC_Hyphen):
                   1009:                        term_word(p, "\\-");
                   1010:                        break;
                   1011:                case (MDOC_Enum):
                   1012:                        (pair->ppair->ppair->count)++;
                   1013:                        (void)snprintf(buf, sizeof(buf), "%d.",
                   1014:                                        pair->ppair->ppair->count);
                   1015:                        term_word(p, buf);
                   1016:                        break;
                   1017:                default:
                   1018:                        break;
                   1019:                }
                   1020:
                   1021:        /*
                   1022:         * If we're not going to process our children, indicate so here.
                   1023:         */
                   1024:
                   1025:        switch (type) {
                   1026:        case (MDOC_Bullet):
                   1027:                /* FALLTHROUGH */
                   1028:        case (MDOC_Item):
                   1029:                /* FALLTHROUGH */
                   1030:        case (MDOC_Dash):
                   1031:                /* FALLTHROUGH */
                   1032:        case (MDOC_Hyphen):
                   1033:                /* FALLTHROUGH */
                   1034:        case (MDOC_Enum):
                   1035:                if (MDOC_HEAD == node->type)
                   1036:                        return(0);
                   1037:                break;
                   1038:        case (MDOC_Column):
                   1039:                if (MDOC_BODY == node->type)
                   1040:                        return(0);
                   1041:                break;
                   1042:        default:
                   1043:                break;
                   1044:        }
                   1045:
                   1046:        return(1);
                   1047: }
                   1048:
                   1049:
                   1050: /* ARGSUSED */
                   1051: static void
                   1052: termp_it_post(DECL_ARGS)
                   1053: {
                   1054:        int                type;
                   1055:
                   1056:        if (MDOC_BODY != node->type && MDOC_HEAD != node->type)
                   1057:                return;
                   1058:
                   1059:        type = arg_listtype(node->parent->parent->parent);
                   1060:
                   1061:        switch (type) {
                   1062:        case (MDOC_Diag):
                   1063:                /* FALLTHROUGH */
                   1064:        case (MDOC_Item):
                   1065:                /* FALLTHROUGH */
                   1066:        case (MDOC_Inset):
                   1067:                if (MDOC_BODY == node->type)
                   1068:                        term_flushln(p);
                   1069:                break;
                   1070:        case (MDOC_Column):
                   1071:                if (MDOC_HEAD == node->type)
                   1072:                        term_flushln(p);
                   1073:                break;
                   1074:        default:
                   1075:                term_flushln(p);
                   1076:                break;
                   1077:        }
                   1078:
                   1079:        p->offset = pair->offset;
                   1080:        p->rmargin = pair->rmargin;
                   1081:        p->flags = pair->flag;
                   1082: }
                   1083:
                   1084:
                   1085: /* ARGSUSED */
                   1086: static int
                   1087: termp_nm_pre(DECL_ARGS)
                   1088: {
                   1089:
                   1090:        if (SEC_SYNOPSIS == node->sec)
                   1091:                term_newln(p);
                   1092:
                   1093:        TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_PROG]);
                   1094:        if (NULL == node->child)
                   1095:                term_word(p, meta->name);
                   1096:
                   1097:        return(1);
                   1098: }
                   1099:
                   1100:
                   1101: /* ARGSUSED */
                   1102: static int
                   1103: termp_fl_pre(DECL_ARGS)
                   1104: {
                   1105:
                   1106:        TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_CMD_FLAG]);
                   1107:        term_word(p, "\\-");
                   1108:        p->flags |= TERMP_NOSPACE;
                   1109:        return(1);
                   1110: }
                   1111:
                   1112:
                   1113: /* ARGSUSED */
                   1114: static int
                   1115: termp_ar_pre(DECL_ARGS)
                   1116: {
                   1117:
                   1118:        TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_CMD_ARG]);
                   1119:        return(1);
                   1120: }
                   1121:
                   1122:
                   1123: /* ARGSUSED */
                   1124: static int
                   1125: termp_ns_pre(DECL_ARGS)
                   1126: {
                   1127:
                   1128:        p->flags |= TERMP_NOSPACE;
                   1129:        return(1);
                   1130: }
                   1131:
                   1132:
                   1133: /* ARGSUSED */
                   1134: static int
                   1135: termp_pp_pre(DECL_ARGS)
                   1136: {
                   1137:
                   1138:        term_vspace(p);
                   1139:        return(1);
                   1140: }
                   1141:
                   1142:
                   1143: /* ARGSUSED */
                   1144: static int
                   1145: termp_st_pre(DECL_ARGS)
                   1146: {
                   1147:        const char      *cp;
                   1148:
                   1149:        if (node->child && (cp = mdoc_a2st(node->child->string)))
                   1150:                term_word(p, cp);
                   1151:        return(0);
                   1152: }
                   1153:
                   1154:
                   1155: /* ARGSUSED */
                   1156: static int
                   1157: termp_rs_pre(DECL_ARGS)
                   1158: {
                   1159:
                   1160:        if (MDOC_BLOCK == node->type && node->prev)
                   1161:                term_vspace(p);
                   1162:        return(1);
                   1163: }
                   1164:
                   1165:
                   1166: /* ARGSUSED */
                   1167: static int
                   1168: termp_rv_pre(DECL_ARGS)
                   1169: {
                   1170:        int              i;
                   1171:
                   1172:        if (-1 == (i = arg_getattr(MDOC_Std, node)))
                   1173:                errx(1, "expected -std argument");
                   1174:        if (1 != node->args->argv[i].sz)
                   1175:                errx(1, "expected -std argument");
                   1176:
                   1177:        term_newln(p);
                   1178:        term_word(p, "The");
                   1179:
                   1180:        p->flags |= ttypes[TTYPE_FUNC_NAME];
                   1181:        term_word(p, *node->args->argv[i].value);
                   1182:        p->flags &= ~ttypes[TTYPE_FUNC_NAME];
                   1183:        p->flags |= TERMP_NOSPACE;
                   1184:
                   1185:                term_word(p, "() function returns the value 0 if successful;");
                   1186:                term_word(p, "otherwise the value -1 is returned and the");
                   1187:                term_word(p, "global variable");
                   1188:
                   1189:        p->flags |= ttypes[TTYPE_VAR_DECL];
                   1190:        term_word(p, "errno");
                   1191:        p->flags &= ~ttypes[TTYPE_VAR_DECL];
                   1192:
                   1193:                term_word(p, "is set to indicate the error.");
                   1194:
                   1195:        return(1);
                   1196: }
                   1197:
                   1198:
                   1199: /* ARGSUSED */
                   1200: static int
                   1201: termp_ex_pre(DECL_ARGS)
                   1202: {
                   1203:        int              i;
                   1204:
                   1205:        if (-1 == (i = arg_getattr(MDOC_Std, node)))
                   1206:                errx(1, "expected -std argument");
                   1207:        if (1 != node->args->argv[i].sz)
                   1208:                errx(1, "expected -std argument");
                   1209:
                   1210:        term_word(p, "The");
                   1211:        p->flags |= ttypes[TTYPE_PROG];
                   1212:        term_word(p, *node->args->argv[i].value);
                   1213:        p->flags &= ~ttypes[TTYPE_PROG];
                   1214:                term_word(p, "utility exits 0 on success, and >0 if an error occurs.");
                   1215:
                   1216:        return(1);
                   1217: }
                   1218:
                   1219:
                   1220: /* ARGSUSED */
                   1221: static int
                   1222: termp_nd_pre(DECL_ARGS)
                   1223: {
                   1224:
                   1225:        term_word(p, "\\-");
                   1226:        return(1);
                   1227: }
                   1228:
                   1229:
                   1230: /* ARGSUSED */
                   1231: static void
                   1232: termp_bl_post(DECL_ARGS)
                   1233: {
                   1234:
                   1235:        if (MDOC_BLOCK == node->type)
                   1236:                term_newln(p);
                   1237: }
                   1238:
                   1239:
                   1240: /* ARGSUSED */
                   1241: static void
                   1242: termp_op_post(DECL_ARGS)
                   1243: {
                   1244:
                   1245:        if (MDOC_BODY != node->type)
                   1246:                return;
                   1247:        p->flags |= TERMP_NOSPACE;
                   1248:        term_word(p, "\\(rB");
                   1249: }
                   1250:
                   1251:
                   1252: /* ARGSUSED */
                   1253: static int
                   1254: termp_xr_pre(DECL_ARGS)
                   1255: {
                   1256:        const struct mdoc_node *n;
                   1257:
                   1258:        if (NULL == (n = node->child))
                   1259:                errx(1, "expected text line argument");
                   1260:        term_word(p, n->string);
                   1261:        if (NULL == (n = n->next))
                   1262:                return(0);
                   1263:        p->flags |= TERMP_NOSPACE;
                   1264:        term_word(p, "(");
                   1265:        p->flags |= TERMP_NOSPACE;
                   1266:        term_word(p, n->string);
                   1267:        p->flags |= TERMP_NOSPACE;
                   1268:        term_word(p, ")");
                   1269:        return(0);
                   1270: }
                   1271:
                   1272:
                   1273: /* ARGSUSED */
                   1274: static int
                   1275: termp_vt_pre(DECL_ARGS)
                   1276: {
                   1277:
                   1278:        /* FIXME: this can be "type name". */
                   1279:        TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_VAR_DECL]);
                   1280:        return(1);
                   1281: }
                   1282:
                   1283:
                   1284: /* ARGSUSED */
                   1285: static void
                   1286: termp_vt_post(DECL_ARGS)
                   1287: {
                   1288:
                   1289:        if (node->sec == SEC_SYNOPSIS)
                   1290:                term_vspace(p);
                   1291: }
                   1292:
                   1293:
                   1294: /* ARGSUSED */
                   1295: static int
                   1296: termp_fd_pre(DECL_ARGS)
                   1297: {
                   1298:
                   1299:        /*
                   1300:         * FIXME: this naming is bad.  This value is used, in general,
                   1301:         * for the #include header or other preprocessor statement.
                   1302:         */
                   1303:        TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_FUNC_DECL]);
                   1304:        return(1);
                   1305: }
                   1306:
                   1307:
                   1308: /* ARGSUSED */
                   1309: static void
                   1310: termp_fd_post(DECL_ARGS)
                   1311: {
                   1312:
                   1313:        if (node->sec != SEC_SYNOPSIS)
                   1314:                return;
                   1315:        term_newln(p);
                   1316:        if (node->next && MDOC_Fd != node->next->tok)
                   1317:                term_vspace(p);
                   1318: }
                   1319:
                   1320:
                   1321: /* ARGSUSED */
                   1322: static int
                   1323: termp_sh_pre(DECL_ARGS)
                   1324: {
                   1325:
                   1326:        switch (node->type) {
                   1327:        case (MDOC_HEAD):
                   1328:                term_vspace(p);
                   1329:                TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_SECTION]);
                   1330:                break;
                   1331:        case (MDOC_BODY):
                   1332:                p->offset = INDENT;
                   1333:                break;
                   1334:        default:
                   1335:                break;
                   1336:        }
                   1337:        return(1);
                   1338: }
                   1339:
                   1340:
                   1341: /* ARGSUSED */
                   1342: static void
                   1343: termp_sh_post(DECL_ARGS)
                   1344: {
                   1345:
                   1346:        switch (node->type) {
                   1347:        case (MDOC_HEAD):
                   1348:                term_newln(p);
                   1349:                break;
                   1350:        case (MDOC_BODY):
                   1351:                term_newln(p);
                   1352:                p->offset = 0;
                   1353:                break;
                   1354:        default:
                   1355:                break;
                   1356:        }
                   1357: }
                   1358:
                   1359:
                   1360: /* ARGSUSED */
                   1361: static int
                   1362: termp_op_pre(DECL_ARGS)
                   1363: {
                   1364:
                   1365:        switch (node->type) {
                   1366:        case (MDOC_BODY):
                   1367:                term_word(p, "\\(lB");
                   1368:                p->flags |= TERMP_NOSPACE;
                   1369:                break;
                   1370:        default:
                   1371:                break;
                   1372:        }
                   1373:        return(1);
                   1374: }
                   1375:
                   1376:
                   1377: /* ARGSUSED */
                   1378: static int
                   1379: termp_bt_pre(DECL_ARGS)
                   1380: {
                   1381:
                   1382:        term_word(p, "is currently in beta test.");
                   1383:        return(1);
                   1384: }
                   1385:
                   1386:
                   1387: /* ARGSUSED */
                   1388: static int
                   1389: termp_lb_pre(DECL_ARGS)
                   1390: {
                   1391:        const char      *lb;
                   1392:
                   1393:        if (NULL == node->child)
                   1394:                errx(1, "expected text line argument");
                   1395:        if ((lb = mdoc_a2lib(node->child->string))) {
                   1396:                term_word(p, lb);
                   1397:                return(0);
                   1398:        }
                   1399:        term_word(p, "library");
                   1400:        return(1);
                   1401: }
                   1402:
                   1403:
                   1404: /* ARGSUSED */
                   1405: static void
                   1406: termp_lb_post(DECL_ARGS)
                   1407: {
                   1408:
                   1409:        term_newln(p);
                   1410: }
                   1411:
                   1412:
                   1413: /* ARGSUSED */
                   1414: static int
                   1415: termp_ud_pre(DECL_ARGS)
                   1416: {
                   1417:
                   1418:        term_word(p, "currently under development.");
                   1419:        return(1);
                   1420: }
                   1421:
                   1422:
                   1423: /* ARGSUSED */
                   1424: static int
                   1425: termp_d1_pre(DECL_ARGS)
                   1426: {
                   1427:
1.4       kristaps 1428:        if (MDOC_BLOCK != node->type)
1.1       kristaps 1429:                return(1);
                   1430:        term_newln(p);
                   1431:        p->offset += (pair->offset = INDENT);
                   1432:        return(1);
                   1433: }
                   1434:
                   1435:
                   1436: /* ARGSUSED */
                   1437: static void
                   1438: termp_d1_post(DECL_ARGS)
                   1439: {
                   1440:
1.4       kristaps 1441:        if (MDOC_BLOCK != node->type)
1.1       kristaps 1442:                return;
                   1443:        term_newln(p);
                   1444:        p->offset -= pair->offset;
                   1445: }
                   1446:
                   1447:
                   1448: /* ARGSUSED */
                   1449: static int
                   1450: termp_aq_pre(DECL_ARGS)
                   1451: {
                   1452:
                   1453:        if (MDOC_BODY != node->type)
                   1454:                return(1);
                   1455:        term_word(p, "\\(la");
                   1456:        p->flags |= TERMP_NOSPACE;
                   1457:        return(1);
                   1458: }
                   1459:
                   1460:
                   1461: /* ARGSUSED */
                   1462: static void
                   1463: termp_aq_post(DECL_ARGS)
                   1464: {
                   1465:
                   1466:        if (MDOC_BODY != node->type)
                   1467:                return;
                   1468:        p->flags |= TERMP_NOSPACE;
                   1469:        term_word(p, "\\(ra");
                   1470: }
                   1471:
                   1472:
                   1473: /* ARGSUSED */
                   1474: static int
                   1475: termp_ft_pre(DECL_ARGS)
                   1476: {
                   1477:
                   1478:        if (SEC_SYNOPSIS == node->sec)
                   1479:                if (node->prev && MDOC_Fo == node->prev->tok)
                   1480:                        term_vspace(p);
                   1481:        TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_FUNC_TYPE]);
                   1482:        return(1);
                   1483: }
                   1484:
                   1485:
                   1486: /* ARGSUSED */
                   1487: static void
                   1488: termp_ft_post(DECL_ARGS)
                   1489: {
                   1490:
                   1491:        if (SEC_SYNOPSIS == node->sec)
                   1492:                term_newln(p);
                   1493: }
                   1494:
                   1495:
                   1496: /* ARGSUSED */
                   1497: static int
                   1498: termp_fn_pre(DECL_ARGS)
                   1499: {
                   1500:        const struct mdoc_node *n;
                   1501:
                   1502:        if (NULL == node->child)
                   1503:                errx(1, "expected text line arguments");
                   1504:
                   1505:        /* FIXME: can be "type funcname" "type varname"... */
                   1506:
                   1507:        p->flags |= ttypes[TTYPE_FUNC_NAME];
                   1508:        term_word(p, node->child->string);
                   1509:        p->flags &= ~ttypes[TTYPE_FUNC_NAME];
                   1510:
                   1511:        p->flags |= TERMP_NOSPACE;
                   1512:        term_word(p, "(");
                   1513:
                   1514:        for (n = node->child->next; n; n = n->next) {
                   1515:                p->flags |= ttypes[TTYPE_FUNC_ARG];
                   1516:                term_word(p, n->string);
                   1517:                p->flags &= ~ttypes[TTYPE_FUNC_ARG];
                   1518:                if (n->next)
                   1519:                        term_word(p, ",");
                   1520:        }
                   1521:
                   1522:        term_word(p, ")");
                   1523:
                   1524:        if (SEC_SYNOPSIS == node->sec)
                   1525:                term_word(p, ";");
                   1526:
                   1527:        return(0);
                   1528: }
                   1529:
                   1530:
                   1531: /* ARGSUSED */
                   1532: static void
                   1533: termp_fn_post(DECL_ARGS)
                   1534: {
                   1535:
                   1536:        if (node->sec == SEC_SYNOPSIS && node->next)
                   1537:                term_vspace(p);
                   1538:
                   1539: }
                   1540:
                   1541:
                   1542: /* ARGSUSED */
                   1543: static int
                   1544: termp_sx_pre(DECL_ARGS)
                   1545: {
                   1546:
                   1547:        TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_LINK]);
                   1548:        return(1);
                   1549: }
                   1550:
                   1551:
                   1552: /* ARGSUSED */
                   1553: static int
                   1554: termp_fa_pre(DECL_ARGS)
                   1555: {
                   1556:        struct mdoc_node *n;
                   1557:
                   1558:        if (node->parent->tok != MDOC_Fo) {
                   1559:                TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_FUNC_ARG]);
                   1560:                return(1);
                   1561:        }
                   1562:
                   1563:        for (n = node->child; n; n = n->next) {
                   1564:                p->flags |= ttypes[TTYPE_FUNC_ARG];
                   1565:                term_word(p, n->string);
                   1566:                p->flags &= ~ttypes[TTYPE_FUNC_ARG];
                   1567:                if (n->next)
                   1568:                        term_word(p, ",");
                   1569:        }
                   1570:
                   1571:        if (node->child && node->next && node->next->tok == MDOC_Fa)
                   1572:                term_word(p, ",");
                   1573:
                   1574:        return(0);
                   1575: }
                   1576:
                   1577:
                   1578: /* ARGSUSED */
                   1579: static int
                   1580: termp_va_pre(DECL_ARGS)
                   1581: {
                   1582:
                   1583:        TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_VAR_DECL]);
                   1584:        return(1);
                   1585: }
                   1586:
                   1587:
                   1588: /* ARGSUSED */
                   1589: static int
                   1590: termp_bd_pre(DECL_ARGS)
                   1591: {
                   1592:        int              i, type, ln;
                   1593:
                   1594:        /*
                   1595:         * This is fairly tricky due primarily to crappy documentation.
                   1596:         * If -ragged or -filled are specified, the block does nothing
                   1597:         * but change the indentation.
                   1598:         *
                   1599:         * If, on the other hand, -unfilled or -literal are specified,
                   1600:         * then the game changes.  Text is printed exactly as entered in
                   1601:         * the display: if a macro line, a newline is appended to the
                   1602:         * line.  Blank lines are allowed.
                   1603:         */
                   1604:
                   1605:        if (MDOC_BLOCK == node->type)
                   1606:                return(fmt_block_vspace(p, node, node));
                   1607:        else if (MDOC_BODY != node->type)
                   1608:                return(1);
                   1609:
                   1610:        if (NULL == node->parent->args)
                   1611:                errx(1, "missing display type");
                   1612:
                   1613:        pair->offset = p->offset;
                   1614:
                   1615:        for (type = -1, i = 0;
                   1616:                        i < (int)node->parent->args->argc; i++) {
                   1617:                switch (node->parent->args->argv[i].arg) {
                   1618:                case (MDOC_Ragged):
                   1619:                        /* FALLTHROUGH */
                   1620:                case (MDOC_Filled):
                   1621:                        /* FALLTHROUGH */
                   1622:                case (MDOC_Unfilled):
                   1623:                        /* FALLTHROUGH */
                   1624:                case (MDOC_Literal):
                   1625:                        type = node->parent->args->argv[i].arg;
                   1626:                        i = (int)node->parent->args->argc;
                   1627:                        break;
                   1628:                default:
                   1629:                        break;
                   1630:                }
                   1631:        }
                   1632:
                   1633:        if (NULL == node->parent->args)
                   1634:                errx(1, "missing display type");
                   1635:
                   1636:        i = arg_getattr(MDOC_Offset, node->parent);
                   1637:        if (-1 != i) {
                   1638:                if (1 != node->parent->args->argv[i].sz)
                   1639:                        errx(1, "expected single value");
                   1640:                p->offset += arg_offset(&node->parent->args->argv[i]);
                   1641:        }
                   1642:
                   1643:        switch (type) {
                   1644:        case (MDOC_Literal):
                   1645:                /* FALLTHROUGH */
                   1646:        case (MDOC_Unfilled):
                   1647:                break;
                   1648:        default:
                   1649:                return(1);
                   1650:        }
                   1651:
                   1652:        /*
                   1653:         * Tricky.  Iterate through all children.  If we're on a
                   1654:         * different parse line, append a newline and then the contents.
                   1655:         * Ew.
                   1656:         */
                   1657:
                   1658:        p->flags |= TERMP_LITERAL;
                   1659:        ln = node->child ? node->child->line : 0;
                   1660:
                   1661:        for (node = node->child; node; node = node->next) {
                   1662:                if (ln < node->line) {
                   1663:                        term_flushln(p);
                   1664:                        p->flags |= TERMP_NOSPACE;
                   1665:                }
                   1666:                ln = node->line;
                   1667:                print_node(p, pair, meta, node);
                   1668:        }
                   1669:
                   1670:        return(0);
                   1671: }
                   1672:
                   1673:
                   1674: /* ARGSUSED */
                   1675: static void
                   1676: termp_bd_post(DECL_ARGS)
                   1677: {
                   1678:
                   1679:        if (MDOC_BODY != node->type)
                   1680:                return;
                   1681:
                   1682:        term_flushln(p);
                   1683:        p->flags &= ~TERMP_LITERAL;
                   1684:        p->offset = pair->offset;
                   1685:        p->flags |= TERMP_NOSPACE;
                   1686: }
                   1687:
                   1688:
                   1689: /* ARGSUSED */
                   1690: static int
                   1691: termp_qq_pre(DECL_ARGS)
                   1692: {
                   1693:
                   1694:        if (MDOC_BODY != node->type)
                   1695:                return(1);
                   1696:        term_word(p, "\"");
                   1697:        p->flags |= TERMP_NOSPACE;
                   1698:        return(1);
                   1699: }
                   1700:
                   1701:
                   1702: /* ARGSUSED */
                   1703: static void
                   1704: termp_qq_post(DECL_ARGS)
                   1705: {
                   1706:
                   1707:        if (MDOC_BODY != node->type)
                   1708:                return;
                   1709:        p->flags |= TERMP_NOSPACE;
                   1710:        term_word(p, "\"");
                   1711: }
                   1712:
                   1713:
                   1714: /* ARGSUSED */
                   1715: static int
                   1716: termp_bsx_pre(DECL_ARGS)
                   1717: {
                   1718:
                   1719:        term_word(p, "BSDI BSD/OS");
                   1720:        return(1);
                   1721: }
                   1722:
                   1723:
                   1724: /* ARGSUSED */
                   1725: static void
                   1726: termp_bx_post(DECL_ARGS)
                   1727: {
                   1728:
                   1729:        if (node->child)
                   1730:                p->flags |= TERMP_NOSPACE;
                   1731:        term_word(p, "BSD");
                   1732: }
                   1733:
                   1734:
                   1735: /* ARGSUSED */
                   1736: static int
                   1737: termp_ox_pre(DECL_ARGS)
                   1738: {
                   1739:
                   1740:        term_word(p, "OpenBSD");
                   1741:        return(1);
                   1742: }
                   1743:
                   1744:
                   1745: /* ARGSUSED */
                   1746: static int
                   1747: termp_dx_pre(DECL_ARGS)
                   1748: {
                   1749:
                   1750:        term_word(p, "DragonFly");
                   1751:        return(1);
                   1752: }
                   1753:
                   1754:
                   1755: /* ARGSUSED */
                   1756: static int
                   1757: termp_ux_pre(DECL_ARGS)
                   1758: {
                   1759:
                   1760:        term_word(p, "UNIX");
                   1761:        return(1);
                   1762: }
                   1763:
                   1764:
                   1765: /* ARGSUSED */
                   1766: static int
                   1767: termp_fx_pre(DECL_ARGS)
                   1768: {
                   1769:
                   1770:        term_word(p, "FreeBSD");
                   1771:        return(1);
                   1772: }
                   1773:
                   1774:
                   1775: /* ARGSUSED */
                   1776: static int
                   1777: termp_nx_pre(DECL_ARGS)
                   1778: {
                   1779:
                   1780:        term_word(p, "NetBSD");
                   1781:        return(1);
                   1782: }
                   1783:
                   1784:
                   1785: /* ARGSUSED */
                   1786: static int
                   1787: termp_sq_pre(DECL_ARGS)
                   1788: {
                   1789:
                   1790:        if (MDOC_BODY != node->type)
                   1791:                return(1);
                   1792:        term_word(p, "\\(oq");
                   1793:        p->flags |= TERMP_NOSPACE;
                   1794:        return(1);
                   1795: }
                   1796:
                   1797:
                   1798: /* ARGSUSED */
                   1799: static void
                   1800: termp_sq_post(DECL_ARGS)
                   1801: {
                   1802:
                   1803:        if (MDOC_BODY != node->type)
                   1804:                return;
                   1805:        p->flags |= TERMP_NOSPACE;
                   1806:        term_word(p, "\\(aq");
                   1807: }
                   1808:
                   1809:
                   1810: /* ARGSUSED */
                   1811: static int
                   1812: termp_pf_pre(DECL_ARGS)
                   1813: {
                   1814:
                   1815:        p->flags |= TERMP_IGNDELIM;
                   1816:        return(1);
                   1817: }
                   1818:
                   1819:
                   1820: /* ARGSUSED */
                   1821: static void
                   1822: termp_pf_post(DECL_ARGS)
                   1823: {
                   1824:
                   1825:        p->flags &= ~TERMP_IGNDELIM;
                   1826:        p->flags |= TERMP_NOSPACE;
                   1827: }
                   1828:
                   1829:
                   1830: /* ARGSUSED */
                   1831: static int
                   1832: termp_ss_pre(DECL_ARGS)
                   1833: {
                   1834:
                   1835:        switch (node->type) {
                   1836:        case (MDOC_BLOCK):
                   1837:                term_newln(p);
                   1838:                if (node->prev)
                   1839:                        term_vspace(p);
                   1840:                break;
                   1841:        case (MDOC_HEAD):
                   1842:                TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_SSECTION]);
1.5       kristaps 1843:                p->offset = HALFINDENT;
1.1       kristaps 1844:                break;
                   1845:        default:
                   1846:                break;
                   1847:        }
                   1848:
                   1849:        return(1);
                   1850: }
                   1851:
                   1852:
                   1853: /* ARGSUSED */
                   1854: static void
                   1855: termp_ss_post(DECL_ARGS)
                   1856: {
                   1857:
                   1858:        switch (node->type) {
                   1859:        case (MDOC_HEAD):
                   1860:                term_newln(p);
                   1861:                p->offset = INDENT;
                   1862:                break;
                   1863:        default:
                   1864:                break;
                   1865:        }
                   1866: }
                   1867:
                   1868:
                   1869: /* ARGSUSED */
                   1870: static int
                   1871: termp_pa_pre(DECL_ARGS)
                   1872: {
                   1873:
                   1874:        TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_FILE]);
                   1875:        return(1);
                   1876: }
                   1877:
                   1878:
                   1879: /* ARGSUSED */
                   1880: static int
                   1881: termp_em_pre(DECL_ARGS)
                   1882: {
                   1883:
                   1884:        TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_EMPH]);
                   1885:        return(1);
                   1886: }
                   1887:
                   1888:
                   1889: /* ARGSUSED */
                   1890: static int
                   1891: termp_cd_pre(DECL_ARGS)
                   1892: {
                   1893:
                   1894:        TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_CONFIG]);
                   1895:        term_newln(p);
                   1896:        return(1);
                   1897: }
                   1898:
                   1899:
                   1900: /* ARGSUSED */
                   1901: static int
                   1902: termp_cm_pre(DECL_ARGS)
                   1903: {
                   1904:
                   1905:        TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_CMD_FLAG]);
                   1906:        return(1);
                   1907: }
                   1908:
                   1909:
                   1910: /* ARGSUSED */
                   1911: static int
                   1912: termp_ic_pre(DECL_ARGS)
                   1913: {
                   1914:
                   1915:        TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_CMD]);
                   1916:        return(1);
                   1917: }
                   1918:
                   1919:
                   1920: /* ARGSUSED */
                   1921: static int
                   1922: termp_in_pre(DECL_ARGS)
                   1923: {
                   1924:
                   1925:        TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_INCLUDE]);
                   1926:        term_word(p, "#include");
                   1927:        term_word(p, "<");
                   1928:        p->flags |= TERMP_NOSPACE;
                   1929:        return(1);
                   1930: }
                   1931:
                   1932:
                   1933: /* ARGSUSED */
                   1934: static void
                   1935: termp_in_post(DECL_ARGS)
                   1936: {
                   1937:
                   1938:        p->flags |= TERMP_NOSPACE;
                   1939:        term_word(p, ">");
                   1940:
                   1941:        term_newln(p);
                   1942:        if (SEC_SYNOPSIS != node->sec)
                   1943:                return;
                   1944:        if (node->next && MDOC_In != node->next->tok)
                   1945:                term_vspace(p);
                   1946: }
                   1947:
                   1948:
                   1949: /* ARGSUSED */
                   1950: static int
                   1951: termp_at_pre(DECL_ARGS)
                   1952: {
                   1953:        const char      *att;
                   1954:
                   1955:        att = NULL;
                   1956:
                   1957:        if (node->child)
                   1958:                att = mdoc_a2att(node->child->string);
                   1959:        if (NULL == att)
                   1960:                att = "AT&T UNIX";
                   1961:
                   1962:        term_word(p, att);
                   1963:        return(0);
                   1964: }
                   1965:
                   1966:
                   1967: /* ARGSUSED */
                   1968: static int
                   1969: termp_brq_pre(DECL_ARGS)
                   1970: {
                   1971:
                   1972:        if (MDOC_BODY != node->type)
                   1973:                return(1);
                   1974:        term_word(p, "\\(lC");
                   1975:        p->flags |= TERMP_NOSPACE;
                   1976:        return(1);
                   1977: }
                   1978:
                   1979:
                   1980: /* ARGSUSED */
                   1981: static void
                   1982: termp_brq_post(DECL_ARGS)
                   1983: {
                   1984:
                   1985:        if (MDOC_BODY != node->type)
                   1986:                return;
                   1987:        p->flags |= TERMP_NOSPACE;
                   1988:        term_word(p, "\\(rC");
                   1989: }
                   1990:
                   1991:
                   1992: /* ARGSUSED */
                   1993: static int
                   1994: termp_bq_pre(DECL_ARGS)
                   1995: {
                   1996:
                   1997:        if (MDOC_BODY != node->type)
                   1998:                return(1);
                   1999:        term_word(p, "\\(lB");
                   2000:        p->flags |= TERMP_NOSPACE;
                   2001:        return(1);
                   2002: }
                   2003:
                   2004:
                   2005: /* ARGSUSED */
                   2006: static void
                   2007: termp_bq_post(DECL_ARGS)
                   2008: {
                   2009:
                   2010:        if (MDOC_BODY != node->type)
                   2011:                return;
                   2012:        p->flags |= TERMP_NOSPACE;
                   2013:        term_word(p, "\\(rB");
                   2014: }
                   2015:
                   2016:
                   2017: /* ARGSUSED */
                   2018: static int
                   2019: termp_pq_pre(DECL_ARGS)
                   2020: {
                   2021:
                   2022:        if (MDOC_BODY != node->type)
                   2023:                return(1);
                   2024:        term_word(p, "\\&(");
                   2025:        p->flags |= TERMP_NOSPACE;
                   2026:        return(1);
                   2027: }
                   2028:
                   2029:
                   2030: /* ARGSUSED */
                   2031: static void
                   2032: termp_pq_post(DECL_ARGS)
                   2033: {
                   2034:
                   2035:        if (MDOC_BODY != node->type)
                   2036:                return;
                   2037:        term_word(p, ")");
                   2038: }
                   2039:
                   2040:
                   2041: /* ARGSUSED */
                   2042: static int
                   2043: termp_fo_pre(DECL_ARGS)
                   2044: {
                   2045:        const struct mdoc_node *n;
                   2046:
                   2047:        if (MDOC_BODY == node->type) {
                   2048:                term_word(p, "(");
                   2049:                p->flags |= TERMP_NOSPACE;
                   2050:                return(1);
                   2051:        } else if (MDOC_HEAD != node->type)
                   2052:                return(1);
                   2053:
                   2054:        /* XXX - groff shows only first parameter */
                   2055:
                   2056:        p->flags |= ttypes[TTYPE_FUNC_NAME];
                   2057:        for (n = node->child; n; n = n->next) {
                   2058:                if (MDOC_TEXT != n->type)
                   2059:                        errx(1, "expected text line argument");
                   2060:                term_word(p, n->string);
                   2061:        }
                   2062:        p->flags &= ~ttypes[TTYPE_FUNC_NAME];
                   2063:
                   2064:        return(0);
                   2065: }
                   2066:
                   2067:
                   2068: /* ARGSUSED */
                   2069: static void
                   2070: termp_fo_post(DECL_ARGS)
                   2071: {
                   2072:
                   2073:        if (MDOC_BODY != node->type)
                   2074:                return;
                   2075:        p->flags |= TERMP_NOSPACE;
                   2076:        term_word(p, ")");
                   2077:        p->flags |= TERMP_NOSPACE;
                   2078:        term_word(p, ";");
                   2079:        term_newln(p);
                   2080: }
                   2081:
                   2082:
                   2083: /* ARGSUSED */
                   2084: static int
                   2085: termp_bf_pre(DECL_ARGS)
                   2086: {
                   2087:        const struct mdoc_node  *n;
                   2088:
                   2089:        if (MDOC_HEAD == node->type) {
                   2090:                return(0);
                   2091:        } else if (MDOC_BLOCK != node->type)
                   2092:                return(1);
                   2093:
                   2094:        if (NULL == (n = node->head->child)) {
                   2095:                if (arg_hasattr(MDOC_Emphasis, node))
                   2096:                        TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_EMPH]);
                   2097:                else if (arg_hasattr(MDOC_Symbolic, node))
                   2098:                        TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_SYMB]);
                   2099:
                   2100:                return(1);
                   2101:        }
                   2102:
                   2103:        if (MDOC_TEXT != n->type)
                   2104:                errx(1, "expected text line arguments");
                   2105:
                   2106:        if (0 == strcmp("Em", n->string))
                   2107:                TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_EMPH]);
                   2108:        else if (0 == strcmp("Sy", n->string))
                   2109:                TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_EMPH]);
                   2110:
                   2111:        return(1);
                   2112: }
                   2113:
                   2114:
                   2115: /* ARGSUSED */
                   2116: static int
                   2117: termp_sy_pre(DECL_ARGS)
                   2118: {
                   2119:
                   2120:        TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_SYMB]);
                   2121:        return(1);
                   2122: }
                   2123:
                   2124:
                   2125: /* ARGSUSED */
                   2126: static int
                   2127: termp_ms_pre(DECL_ARGS)
                   2128: {
                   2129:
                   2130:        TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_SYMBOL]);
                   2131:        return(1);
                   2132: }
                   2133:
                   2134:
                   2135:
                   2136: /* ARGSUSED */
                   2137: static int
                   2138: termp_sm_pre(DECL_ARGS)
                   2139: {
                   2140:
                   2141:        if (NULL == node->child || MDOC_TEXT != node->child->type)
                   2142:                errx(1, "expected boolean line argument");
                   2143:
                   2144:        if (0 == strcmp("on", node->child->string)) {
                   2145:                p->flags &= ~TERMP_NONOSPACE;
                   2146:                p->flags &= ~TERMP_NOSPACE;
                   2147:        } else
                   2148:                p->flags |= TERMP_NONOSPACE;
                   2149:
                   2150:        return(0);
                   2151: }
                   2152:
                   2153:
                   2154: /* ARGSUSED */
                   2155: static int
                   2156: termp_ap_pre(DECL_ARGS)
                   2157: {
                   2158:
                   2159:        p->flags |= TERMP_NOSPACE;
                   2160:        term_word(p, "\\(aq");
                   2161:        p->flags |= TERMP_NOSPACE;
                   2162:        return(1);
                   2163: }
                   2164:
                   2165:
                   2166: /* ARGSUSED */
                   2167: static int
                   2168: termp__j_pre(DECL_ARGS)
                   2169: {
                   2170:
                   2171:        TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_REF_JOURNAL]);
                   2172:        return(1);
                   2173: }
                   2174:
                   2175:
                   2176: /* ARGSUSED */
                   2177: static int
                   2178: termp__t_pre(DECL_ARGS)
                   2179: {
                   2180:
                   2181:        term_word(p, "\"");
                   2182:        p->flags |= TERMP_NOSPACE;
                   2183:        return(1);
                   2184: }
                   2185:
                   2186:
                   2187: /* ARGSUSED */
                   2188: static void
                   2189: termp__t_post(DECL_ARGS)
                   2190: {
                   2191:
                   2192:        p->flags |= TERMP_NOSPACE;
                   2193:        term_word(p, "\"");
                   2194:        termp____post(p, pair, meta, node);
                   2195: }
                   2196:
                   2197:
                   2198: /* ARGSUSED */
                   2199: static void
                   2200: termp____post(DECL_ARGS)
                   2201: {
                   2202:
                   2203:        p->flags |= TERMP_NOSPACE;
                   2204:        term_word(p, node->next ? "," : ".");
                   2205: }
                   2206:
                   2207:
                   2208: /* ARGSUSED */
                   2209: static int
                   2210: termp_lk_pre(DECL_ARGS)
                   2211: {
                   2212:        const struct mdoc_node *n;
                   2213:
                   2214:        if (NULL == (n = node->child))
                   2215:                errx(1, "expected line argument");
                   2216:
                   2217:        p->flags |= ttypes[TTYPE_LINK_ANCHOR];
                   2218:        term_word(p, n->string);
                   2219:        p->flags &= ~ttypes[TTYPE_LINK_ANCHOR];
                   2220:        p->flags |= TERMP_NOSPACE;
                   2221:        term_word(p, ":");
                   2222:
                   2223:        p->flags |= ttypes[TTYPE_LINK_TEXT];
                   2224:        for ( ; n; n = n->next) {
                   2225:                term_word(p, n->string);
                   2226:        }
                   2227:        p->flags &= ~ttypes[TTYPE_LINK_TEXT];
                   2228:
                   2229:        return(0);
                   2230: }
                   2231:
                   2232:
                   2233: /* ARGSUSED */
                   2234: static int
                   2235: termp_mt_pre(DECL_ARGS)
                   2236: {
                   2237:
                   2238:        TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_LINK_ANCHOR]);
                   2239:        return(1);
                   2240: }
                   2241:
                   2242:

CVSweb