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

Annotation of mandoc/mdoc_term.c, Revision 1.64

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

CVSweb