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

Annotation of mandoc/mdoc_term.c, Revision 1.65

1.65    ! kristaps    1: /*     $Id: mdoc_term.c,v 1.64 2009/07/29 08:46:06 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:        /*
1.65    ! kristaps 1402:         * XXX: undocumented: using two `Sh' macros in sequence has no
        !          1403:         * vspace between calls, only a newline.
1.64      kristaps 1404:         */
1.1       kristaps 1405:        switch (node->type) {
1.65    ! kristaps 1406:        case (MDOC_BLOCK):
        !          1407:                if (node->prev && MDOC_Sh == node->prev->tok)
        !          1408:                        if (NULL == node->prev->body->child)
        !          1409:                                break;
        !          1410:                term_vspace(p);
        !          1411:                break;
1.1       kristaps 1412:        case (MDOC_HEAD):
1.28      kristaps 1413:                pair->flag |= ttypes[TTYPE_SECTION];
1.1       kristaps 1414:                break;
                   1415:        case (MDOC_BODY):
                   1416:                p->offset = INDENT;
                   1417:                break;
                   1418:        default:
                   1419:                break;
                   1420:        }
                   1421:        return(1);
                   1422: }
                   1423:
                   1424:
                   1425: /* ARGSUSED */
                   1426: static void
                   1427: termp_sh_post(DECL_ARGS)
                   1428: {
                   1429:
                   1430:        switch (node->type) {
                   1431:        case (MDOC_HEAD):
                   1432:                term_newln(p);
                   1433:                break;
                   1434:        case (MDOC_BODY):
                   1435:                term_newln(p);
                   1436:                p->offset = 0;
                   1437:                break;
                   1438:        default:
                   1439:                break;
                   1440:        }
                   1441: }
                   1442:
                   1443:
                   1444: /* ARGSUSED */
                   1445: static int
                   1446: termp_op_pre(DECL_ARGS)
                   1447: {
                   1448:
                   1449:        switch (node->type) {
                   1450:        case (MDOC_BODY):
                   1451:                term_word(p, "\\(lB");
                   1452:                p->flags |= TERMP_NOSPACE;
                   1453:                break;
                   1454:        default:
                   1455:                break;
                   1456:        }
                   1457:        return(1);
                   1458: }
                   1459:
                   1460:
                   1461: /* ARGSUSED */
                   1462: static int
                   1463: termp_bt_pre(DECL_ARGS)
                   1464: {
                   1465:
                   1466:        term_word(p, "is currently in beta test.");
                   1467:        return(1);
                   1468: }
                   1469:
                   1470:
                   1471: /* ARGSUSED */
                   1472: static void
                   1473: termp_lb_post(DECL_ARGS)
                   1474: {
                   1475:
                   1476:        term_newln(p);
                   1477: }
                   1478:
                   1479:
                   1480: /* ARGSUSED */
                   1481: static int
                   1482: termp_ud_pre(DECL_ARGS)
                   1483: {
                   1484:
                   1485:        term_word(p, "currently under development.");
                   1486:        return(1);
                   1487: }
                   1488:
                   1489:
                   1490: /* ARGSUSED */
                   1491: static int
                   1492: termp_d1_pre(DECL_ARGS)
                   1493: {
                   1494:
1.4       kristaps 1495:        if (MDOC_BLOCK != node->type)
1.1       kristaps 1496:                return(1);
                   1497:        term_newln(p);
1.29      kristaps 1498:        p->offset += (INDENT + 1);
1.1       kristaps 1499:        return(1);
                   1500: }
                   1501:
                   1502:
                   1503: /* ARGSUSED */
                   1504: static void
                   1505: termp_d1_post(DECL_ARGS)
                   1506: {
                   1507:
1.4       kristaps 1508:        if (MDOC_BLOCK != node->type)
1.1       kristaps 1509:                return;
                   1510:        term_newln(p);
                   1511: }
                   1512:
                   1513:
                   1514: /* ARGSUSED */
                   1515: static int
                   1516: termp_aq_pre(DECL_ARGS)
                   1517: {
                   1518:
                   1519:        if (MDOC_BODY != node->type)
                   1520:                return(1);
                   1521:        term_word(p, "\\(la");
                   1522:        p->flags |= TERMP_NOSPACE;
                   1523:        return(1);
                   1524: }
                   1525:
                   1526:
                   1527: /* ARGSUSED */
                   1528: static void
                   1529: termp_aq_post(DECL_ARGS)
                   1530: {
                   1531:
                   1532:        if (MDOC_BODY != node->type)
                   1533:                return;
                   1534:        p->flags |= TERMP_NOSPACE;
                   1535:        term_word(p, "\\(ra");
                   1536: }
                   1537:
                   1538:
                   1539: /* ARGSUSED */
                   1540: static int
                   1541: termp_ft_pre(DECL_ARGS)
                   1542: {
                   1543:
                   1544:        if (SEC_SYNOPSIS == node->sec)
                   1545:                if (node->prev && MDOC_Fo == node->prev->tok)
                   1546:                        term_vspace(p);
1.28      kristaps 1547:        pair->flag |= ttypes[TTYPE_FUNC_TYPE];
1.1       kristaps 1548:        return(1);
                   1549: }
                   1550:
                   1551:
                   1552: /* ARGSUSED */
                   1553: static void
                   1554: termp_ft_post(DECL_ARGS)
                   1555: {
                   1556:
                   1557:        if (SEC_SYNOPSIS == node->sec)
                   1558:                term_newln(p);
                   1559: }
                   1560:
                   1561:
                   1562: /* ARGSUSED */
                   1563: static int
                   1564: termp_fn_pre(DECL_ARGS)
                   1565: {
                   1566:        const struct mdoc_node *n;
                   1567:
1.11      kristaps 1568:        assert(node->child && MDOC_TEXT == node->child->type);
1.1       kristaps 1569:
                   1570:        /* FIXME: can be "type funcname" "type varname"... */
                   1571:
                   1572:        p->flags |= ttypes[TTYPE_FUNC_NAME];
                   1573:        term_word(p, node->child->string);
                   1574:        p->flags &= ~ttypes[TTYPE_FUNC_NAME];
                   1575:
                   1576:        p->flags |= TERMP_NOSPACE;
                   1577:        term_word(p, "(");
                   1578:
                   1579:        for (n = node->child->next; n; n = n->next) {
                   1580:                p->flags |= ttypes[TTYPE_FUNC_ARG];
                   1581:                term_word(p, n->string);
                   1582:                p->flags &= ~ttypes[TTYPE_FUNC_ARG];
                   1583:                if (n->next)
                   1584:                        term_word(p, ",");
                   1585:        }
                   1586:
                   1587:        term_word(p, ")");
                   1588:
                   1589:        if (SEC_SYNOPSIS == node->sec)
                   1590:                term_word(p, ";");
                   1591:
                   1592:        return(0);
                   1593: }
                   1594:
                   1595:
                   1596: /* ARGSUSED */
                   1597: static void
                   1598: termp_fn_post(DECL_ARGS)
                   1599: {
                   1600:
                   1601:        if (node->sec == SEC_SYNOPSIS && node->next)
                   1602:                term_vspace(p);
                   1603: }
                   1604:
                   1605:
                   1606: /* ARGSUSED */
                   1607: static int
                   1608: termp_sx_pre(DECL_ARGS)
                   1609: {
                   1610:
1.28      kristaps 1611:        pair->flag |= ttypes[TTYPE_LINK];
1.1       kristaps 1612:        return(1);
                   1613: }
                   1614:
                   1615:
                   1616: /* ARGSUSED */
                   1617: static int
                   1618: termp_fa_pre(DECL_ARGS)
                   1619: {
                   1620:        struct mdoc_node *n;
                   1621:
                   1622:        if (node->parent->tok != MDOC_Fo) {
1.28      kristaps 1623:                pair->flag |= ttypes[TTYPE_FUNC_ARG];
1.1       kristaps 1624:                return(1);
                   1625:        }
                   1626:
                   1627:        for (n = node->child; n; n = n->next) {
                   1628:                p->flags |= ttypes[TTYPE_FUNC_ARG];
                   1629:                term_word(p, n->string);
                   1630:                p->flags &= ~ttypes[TTYPE_FUNC_ARG];
                   1631:                if (n->next)
                   1632:                        term_word(p, ",");
                   1633:        }
                   1634:
                   1635:        if (node->child && node->next && node->next->tok == MDOC_Fa)
                   1636:                term_word(p, ",");
                   1637:
                   1638:        return(0);
                   1639: }
                   1640:
                   1641:
                   1642: /* ARGSUSED */
                   1643: static int
                   1644: termp_va_pre(DECL_ARGS)
                   1645: {
                   1646:
1.28      kristaps 1647:        pair->flag |= ttypes[TTYPE_VAR_DECL];
1.1       kristaps 1648:        return(1);
                   1649: }
                   1650:
                   1651:
                   1652: /* ARGSUSED */
                   1653: static int
                   1654: termp_bd_pre(DECL_ARGS)
                   1655: {
1.60      kristaps 1656:        int              i, type;
1.1       kristaps 1657:
                   1658:        /*
                   1659:         * This is fairly tricky due primarily to crappy documentation.
                   1660:         * If -ragged or -filled are specified, the block does nothing
                   1661:         * but change the indentation.
                   1662:         *
                   1663:         * If, on the other hand, -unfilled or -literal are specified,
                   1664:         * then the game changes.  Text is printed exactly as entered in
                   1665:         * the display: if a macro line, a newline is appended to the
                   1666:         * line.  Blank lines are allowed.
                   1667:         */
                   1668:
1.54      kristaps 1669:        if (MDOC_BLOCK == node->type) {
                   1670:                fmt_block_vspace(p, node, node);
                   1671:                return(1);
                   1672:        } else if (MDOC_BODY != node->type)
1.1       kristaps 1673:                return(1);
                   1674:
1.60      kristaps 1675:        assert(node->parent->args);
1.11      kristaps 1676:
1.60      kristaps 1677:        for (type = -1, i = 0; -1 == type &&
1.1       kristaps 1678:                        i < (int)node->parent->args->argc; i++) {
                   1679:                switch (node->parent->args->argv[i].arg) {
                   1680:                case (MDOC_Ragged):
                   1681:                        /* FALLTHROUGH */
                   1682:                case (MDOC_Filled):
                   1683:                        /* FALLTHROUGH */
                   1684:                case (MDOC_Unfilled):
                   1685:                        /* FALLTHROUGH */
                   1686:                case (MDOC_Literal):
                   1687:                        type = node->parent->args->argv[i].arg;
                   1688:                        break;
                   1689:                default:
                   1690:                        break;
                   1691:                }
                   1692:        }
1.60      kristaps 1693:
                   1694:        assert(type > -1);
1.1       kristaps 1695:
                   1696:        i = arg_getattr(MDOC_Offset, node->parent);
1.60      kristaps 1697:        if (-1 != i)
1.1       kristaps 1698:                p->offset += arg_offset(&node->parent->args->argv[i]);
                   1699:
                   1700:        switch (type) {
                   1701:        case (MDOC_Literal):
                   1702:                /* FALLTHROUGH */
                   1703:        case (MDOC_Unfilled):
                   1704:                break;
                   1705:        default:
                   1706:                return(1);
                   1707:        }
                   1708:
                   1709:        for (node = node->child; node; node = node->next) {
1.60      kristaps 1710:                p->flags |= TERMP_NOSPACE;
                   1711:                print_node(p, pair, meta, node);
                   1712:                if (node->next)
1.1       kristaps 1713:                        term_flushln(p);
                   1714:        }
                   1715:
                   1716:        return(0);
                   1717: }
                   1718:
                   1719:
                   1720: /* ARGSUSED */
                   1721: static void
                   1722: termp_bd_post(DECL_ARGS)
                   1723: {
                   1724:
                   1725:        if (MDOC_BODY != node->type)
                   1726:                return;
1.60      kristaps 1727:        p->flags |= TERMP_NOSPACE;
1.1       kristaps 1728:        term_flushln(p);
                   1729: }
                   1730:
                   1731:
                   1732: /* ARGSUSED */
                   1733: static int
                   1734: termp_qq_pre(DECL_ARGS)
                   1735: {
                   1736:
                   1737:        if (MDOC_BODY != node->type)
                   1738:                return(1);
                   1739:        term_word(p, "\"");
                   1740:        p->flags |= TERMP_NOSPACE;
                   1741:        return(1);
                   1742: }
                   1743:
                   1744:
                   1745: /* ARGSUSED */
                   1746: static void
                   1747: termp_qq_post(DECL_ARGS)
                   1748: {
                   1749:
                   1750:        if (MDOC_BODY != node->type)
                   1751:                return;
                   1752:        p->flags |= TERMP_NOSPACE;
                   1753:        term_word(p, "\"");
                   1754: }
                   1755:
                   1756:
                   1757: /* ARGSUSED */
                   1758: static void
                   1759: termp_bx_post(DECL_ARGS)
                   1760: {
                   1761:
                   1762:        if (node->child)
                   1763:                p->flags |= TERMP_NOSPACE;
                   1764:        term_word(p, "BSD");
                   1765: }
                   1766:
                   1767:
                   1768: /* ARGSUSED */
                   1769: static int
1.26      kristaps 1770: termp_xx_pre(DECL_ARGS)
1.1       kristaps 1771: {
1.26      kristaps 1772:        const char      *pp;
1.1       kristaps 1773:
1.26      kristaps 1774:        pp = NULL;
                   1775:        switch (node->tok) {
                   1776:        case (MDOC_Bsx):
                   1777:                pp = "BSDI BSD/OS";
                   1778:                break;
                   1779:        case (MDOC_Dx):
                   1780:                pp = "DragonFlyBSD";
                   1781:                break;
                   1782:        case (MDOC_Fx):
                   1783:                pp = "FreeBSD";
                   1784:                break;
                   1785:        case (MDOC_Nx):
                   1786:                pp = "NetBSD";
                   1787:                break;
                   1788:        case (MDOC_Ox):
                   1789:                pp = "OpenBSD";
                   1790:                break;
                   1791:        case (MDOC_Ux):
                   1792:                pp = "UNIX";
                   1793:                break;
                   1794:        default:
                   1795:                break;
                   1796:        }
1.1       kristaps 1797:
1.26      kristaps 1798:        assert(pp);
                   1799:        term_word(p, pp);
1.1       kristaps 1800:        return(1);
                   1801: }
                   1802:
                   1803:
                   1804: /* ARGSUSED */
                   1805: static int
                   1806: termp_sq_pre(DECL_ARGS)
                   1807: {
                   1808:
                   1809:        if (MDOC_BODY != node->type)
                   1810:                return(1);
                   1811:        term_word(p, "\\(oq");
                   1812:        p->flags |= TERMP_NOSPACE;
                   1813:        return(1);
                   1814: }
                   1815:
                   1816:
                   1817: /* ARGSUSED */
                   1818: static void
                   1819: termp_sq_post(DECL_ARGS)
                   1820: {
                   1821:
                   1822:        if (MDOC_BODY != node->type)
                   1823:                return;
                   1824:        p->flags |= TERMP_NOSPACE;
                   1825:        term_word(p, "\\(aq");
                   1826: }
                   1827:
                   1828:
                   1829: /* ARGSUSED */
                   1830: static int
1.57      kristaps 1831: termp_pa_pre(DECL_ARGS)
                   1832: {
                   1833:
                   1834:        pair->flag |= ttypes[TTYPE_FILE];
                   1835:        return(1);
                   1836: }
                   1837:
                   1838:
                   1839: /* ARGSUSED */
                   1840: static int
1.1       kristaps 1841: termp_pf_pre(DECL_ARGS)
                   1842: {
                   1843:
                   1844:        p->flags |= TERMP_IGNDELIM;
                   1845:        return(1);
                   1846: }
                   1847:
                   1848:
                   1849: /* ARGSUSED */
                   1850: static void
                   1851: termp_pf_post(DECL_ARGS)
                   1852: {
                   1853:
                   1854:        p->flags &= ~TERMP_IGNDELIM;
                   1855:        p->flags |= TERMP_NOSPACE;
                   1856: }
                   1857:
                   1858:
                   1859: /* ARGSUSED */
                   1860: static int
                   1861: termp_ss_pre(DECL_ARGS)
                   1862: {
                   1863:
                   1864:        switch (node->type) {
                   1865:        case (MDOC_BLOCK):
                   1866:                term_newln(p);
                   1867:                if (node->prev)
                   1868:                        term_vspace(p);
                   1869:                break;
                   1870:        case (MDOC_HEAD):
1.28      kristaps 1871:                pair->flag |= ttypes[TTYPE_SSECTION];
1.5       kristaps 1872:                p->offset = HALFINDENT;
1.1       kristaps 1873:                break;
                   1874:        default:
                   1875:                break;
                   1876:        }
                   1877:
                   1878:        return(1);
                   1879: }
                   1880:
                   1881:
                   1882: /* ARGSUSED */
                   1883: static void
                   1884: termp_ss_post(DECL_ARGS)
                   1885: {
                   1886:
1.29      kristaps 1887:        if (MDOC_HEAD == node->type)
1.1       kristaps 1888:                term_newln(p);
                   1889: }
                   1890:
                   1891:
                   1892: /* ARGSUSED */
                   1893: static int
                   1894: termp_em_pre(DECL_ARGS)
                   1895: {
                   1896:
1.28      kristaps 1897:        pair->flag |= ttypes[TTYPE_EMPH];
1.1       kristaps 1898:        return(1);
                   1899: }
                   1900:
                   1901:
                   1902: /* ARGSUSED */
                   1903: static int
                   1904: termp_cd_pre(DECL_ARGS)
                   1905: {
                   1906:
1.28      kristaps 1907:        pair->flag |= ttypes[TTYPE_CONFIG];
1.1       kristaps 1908:        term_newln(p);
                   1909:        return(1);
                   1910: }
                   1911:
                   1912:
                   1913: /* ARGSUSED */
                   1914: static int
                   1915: termp_cm_pre(DECL_ARGS)
                   1916: {
                   1917:
1.28      kristaps 1918:        pair->flag |= ttypes[TTYPE_CMD_FLAG];
1.1       kristaps 1919:        return(1);
                   1920: }
                   1921:
                   1922:
                   1923: /* ARGSUSED */
                   1924: static int
                   1925: termp_ic_pre(DECL_ARGS)
                   1926: {
                   1927:
1.28      kristaps 1928:        pair->flag |= ttypes[TTYPE_CMD];
1.1       kristaps 1929:        return(1);
                   1930: }
                   1931:
                   1932:
                   1933: /* ARGSUSED */
                   1934: static int
                   1935: termp_in_pre(DECL_ARGS)
                   1936: {
                   1937:
1.28      kristaps 1938:        pair->flag |= ttypes[TTYPE_INCLUDE];
                   1939:        p->flags |= ttypes[TTYPE_INCLUDE];
1.22      kristaps 1940:
                   1941:        if (SEC_SYNOPSIS == node->sec)
                   1942:                term_word(p, "#include");
                   1943:
1.1       kristaps 1944:        term_word(p, "<");
                   1945:        p->flags |= TERMP_NOSPACE;
                   1946:        return(1);
                   1947: }
                   1948:
                   1949:
                   1950: /* ARGSUSED */
                   1951: static void
                   1952: termp_in_post(DECL_ARGS)
                   1953: {
                   1954:
1.46      kristaps 1955:        p->flags |= TERMP_NOSPACE | ttypes[TTYPE_INCLUDE];
1.1       kristaps 1956:        term_word(p, ">");
1.46      kristaps 1957:        p->flags &= ~ttypes[TTYPE_INCLUDE];
1.1       kristaps 1958:
                   1959:        if (SEC_SYNOPSIS != node->sec)
                   1960:                return;
1.22      kristaps 1961:
                   1962:        term_newln(p);
                   1963:        /*
                   1964:         * XXX Not entirely correct.  If `.In foo bar' is specified in
                   1965:         * the SYNOPSIS section, then it produces a single break after
                   1966:         * the <foo>; mandoc asserts a vertical space.  Since this
                   1967:         * construction is rarely used, I think it's fine.
                   1968:         */
1.1       kristaps 1969:        if (node->next && MDOC_In != node->next->tok)
                   1970:                term_vspace(p);
                   1971: }
                   1972:
                   1973:
                   1974: /* ARGSUSED */
                   1975: static int
1.45      kristaps 1976: termp_sp_pre(DECL_ARGS)
                   1977: {
                   1978:        int              i, len;
                   1979:
                   1980:        if (NULL == node->child) {
                   1981:                term_vspace(p);
                   1982:                return(0);
                   1983:        }
                   1984:
                   1985:        len = atoi(node->child->string);
                   1986:        if (0 == len)
                   1987:                term_newln(p);
                   1988:        for (i = 0; i < len; i++)
                   1989:                term_vspace(p);
                   1990:
                   1991:        return(0);
                   1992: }
                   1993:
                   1994:
                   1995: /* ARGSUSED */
                   1996: static int
1.44      kristaps 1997: termp_br_pre(DECL_ARGS)
                   1998: {
                   1999:
                   2000:        term_newln(p);
                   2001:        return(1);
                   2002: }
                   2003:
                   2004:
                   2005: /* ARGSUSED */
                   2006: static int
1.1       kristaps 2007: termp_brq_pre(DECL_ARGS)
                   2008: {
                   2009:
                   2010:        if (MDOC_BODY != node->type)
                   2011:                return(1);
                   2012:        term_word(p, "\\(lC");
                   2013:        p->flags |= TERMP_NOSPACE;
                   2014:        return(1);
                   2015: }
                   2016:
                   2017:
                   2018: /* ARGSUSED */
                   2019: static void
                   2020: termp_brq_post(DECL_ARGS)
                   2021: {
                   2022:
                   2023:        if (MDOC_BODY != node->type)
                   2024:                return;
                   2025:        p->flags |= TERMP_NOSPACE;
                   2026:        term_word(p, "\\(rC");
                   2027: }
                   2028:
                   2029:
                   2030: /* ARGSUSED */
                   2031: static int
                   2032: termp_bq_pre(DECL_ARGS)
                   2033: {
                   2034:
                   2035:        if (MDOC_BODY != node->type)
                   2036:                return(1);
                   2037:        term_word(p, "\\(lB");
                   2038:        p->flags |= TERMP_NOSPACE;
                   2039:        return(1);
                   2040: }
                   2041:
                   2042:
                   2043: /* ARGSUSED */
                   2044: static void
                   2045: termp_bq_post(DECL_ARGS)
                   2046: {
                   2047:
                   2048:        if (MDOC_BODY != node->type)
                   2049:                return;
                   2050:        p->flags |= TERMP_NOSPACE;
                   2051:        term_word(p, "\\(rB");
                   2052: }
                   2053:
                   2054:
                   2055: /* ARGSUSED */
                   2056: static int
                   2057: termp_pq_pre(DECL_ARGS)
                   2058: {
                   2059:
                   2060:        if (MDOC_BODY != node->type)
                   2061:                return(1);
                   2062:        term_word(p, "\\&(");
                   2063:        p->flags |= TERMP_NOSPACE;
                   2064:        return(1);
                   2065: }
                   2066:
                   2067:
                   2068: /* ARGSUSED */
                   2069: static void
                   2070: termp_pq_post(DECL_ARGS)
                   2071: {
                   2072:
                   2073:        if (MDOC_BODY != node->type)
                   2074:                return;
                   2075:        term_word(p, ")");
                   2076: }
                   2077:
                   2078:
                   2079: /* ARGSUSED */
                   2080: static int
                   2081: termp_fo_pre(DECL_ARGS)
                   2082: {
                   2083:        const struct mdoc_node *n;
                   2084:
                   2085:        if (MDOC_BODY == node->type) {
1.33      kristaps 2086:                p->flags |= TERMP_NOSPACE;
1.1       kristaps 2087:                term_word(p, "(");
                   2088:                p->flags |= TERMP_NOSPACE;
                   2089:                return(1);
                   2090:        } else if (MDOC_HEAD != node->type)
                   2091:                return(1);
                   2092:
                   2093:        p->flags |= ttypes[TTYPE_FUNC_NAME];
                   2094:        for (n = node->child; n; n = n->next) {
1.11      kristaps 2095:                assert(MDOC_TEXT == n->type);
1.1       kristaps 2096:                term_word(p, n->string);
                   2097:        }
                   2098:        p->flags &= ~ttypes[TTYPE_FUNC_NAME];
                   2099:
                   2100:        return(0);
                   2101: }
                   2102:
                   2103:
                   2104: /* ARGSUSED */
                   2105: static void
                   2106: termp_fo_post(DECL_ARGS)
                   2107: {
                   2108:
                   2109:        if (MDOC_BODY != node->type)
                   2110:                return;
                   2111:        p->flags |= TERMP_NOSPACE;
                   2112:        term_word(p, ")");
                   2113:        p->flags |= TERMP_NOSPACE;
                   2114:        term_word(p, ";");
                   2115:        term_newln(p);
                   2116: }
                   2117:
                   2118:
                   2119: /* ARGSUSED */
                   2120: static int
                   2121: termp_bf_pre(DECL_ARGS)
                   2122: {
                   2123:        const struct mdoc_node  *n;
                   2124:
1.28      kristaps 2125:        if (MDOC_HEAD == node->type)
1.1       kristaps 2126:                return(0);
1.28      kristaps 2127:        else if (MDOC_BLOCK != node->type)
1.1       kristaps 2128:                return(1);
                   2129:
                   2130:        if (NULL == (n = node->head->child)) {
                   2131:                if (arg_hasattr(MDOC_Emphasis, node))
1.28      kristaps 2132:                        pair->flag |= ttypes[TTYPE_EMPH];
1.1       kristaps 2133:                else if (arg_hasattr(MDOC_Symbolic, node))
1.28      kristaps 2134:                        pair->flag |= ttypes[TTYPE_SYMB];
1.1       kristaps 2135:
                   2136:                return(1);
                   2137:        }
                   2138:
1.11      kristaps 2139:        assert(MDOC_TEXT == n->type);
1.1       kristaps 2140:        if (0 == strcmp("Em", n->string))
1.28      kristaps 2141:                pair->flag |= ttypes[TTYPE_EMPH];
1.1       kristaps 2142:        else if (0 == strcmp("Sy", n->string))
1.28      kristaps 2143:                pair->flag |= ttypes[TTYPE_SYMB];
1.1       kristaps 2144:
                   2145:        return(1);
                   2146: }
                   2147:
                   2148:
                   2149: /* ARGSUSED */
                   2150: static int
                   2151: termp_sy_pre(DECL_ARGS)
                   2152: {
                   2153:
1.28      kristaps 2154:        pair->flag |= ttypes[TTYPE_SYMB];
1.1       kristaps 2155:        return(1);
                   2156: }
                   2157:
                   2158:
                   2159: /* ARGSUSED */
                   2160: static int
                   2161: termp_ms_pre(DECL_ARGS)
                   2162: {
                   2163:
1.28      kristaps 2164:        pair->flag |= ttypes[TTYPE_SYMBOL];
1.1       kristaps 2165:        return(1);
                   2166: }
                   2167:
                   2168:
                   2169:
                   2170: /* ARGSUSED */
                   2171: static int
                   2172: termp_sm_pre(DECL_ARGS)
                   2173: {
                   2174:
1.11      kristaps 2175:        assert(node->child && MDOC_TEXT == node->child->type);
1.1       kristaps 2176:        if (0 == strcmp("on", node->child->string)) {
                   2177:                p->flags &= ~TERMP_NONOSPACE;
                   2178:                p->flags &= ~TERMP_NOSPACE;
                   2179:        } else
                   2180:                p->flags |= TERMP_NONOSPACE;
                   2181:
                   2182:        return(0);
                   2183: }
                   2184:
                   2185:
                   2186: /* ARGSUSED */
                   2187: static int
                   2188: termp_ap_pre(DECL_ARGS)
                   2189: {
                   2190:
                   2191:        p->flags |= TERMP_NOSPACE;
                   2192:        term_word(p, "\\(aq");
                   2193:        p->flags |= TERMP_NOSPACE;
                   2194:        return(1);
                   2195: }
                   2196:
                   2197:
                   2198: /* ARGSUSED */
                   2199: static int
                   2200: termp__j_pre(DECL_ARGS)
                   2201: {
                   2202:
1.28      kristaps 2203:        pair->flag |= ttypes[TTYPE_REF_JOURNAL];
1.1       kristaps 2204:        return(1);
                   2205: }
                   2206:
                   2207:
                   2208: /* ARGSUSED */
                   2209: static int
                   2210: termp__t_pre(DECL_ARGS)
                   2211: {
                   2212:
1.56      kristaps 2213:        pair->flag |= ttypes[TTYPE_REF_TITLE];
1.1       kristaps 2214:        return(1);
                   2215: }
                   2216:
                   2217:
                   2218: /* ARGSUSED */
                   2219: static void
                   2220: termp____post(DECL_ARGS)
                   2221: {
                   2222:
                   2223:        p->flags |= TERMP_NOSPACE;
                   2224:        term_word(p, node->next ? "," : ".");
                   2225: }
                   2226:
                   2227:
                   2228: /* ARGSUSED */
                   2229: static int
                   2230: termp_lk_pre(DECL_ARGS)
                   2231: {
                   2232:        const struct mdoc_node *n;
                   2233:
1.11      kristaps 2234:        assert(node->child);
                   2235:        n = node->child;
1.1       kristaps 2236:
1.12      kristaps 2237:        if (NULL == n->next) {
1.28      kristaps 2238:                pair->flag |= ttypes[TTYPE_LINK_ANCHOR];
1.12      kristaps 2239:                return(1);
                   2240:        }
                   2241:
1.1       kristaps 2242:        p->flags |= ttypes[TTYPE_LINK_ANCHOR];
                   2243:        term_word(p, n->string);
                   2244:        p->flags |= TERMP_NOSPACE;
                   2245:        term_word(p, ":");
1.12      kristaps 2246:        p->flags &= ~ttypes[TTYPE_LINK_ANCHOR];
1.1       kristaps 2247:
                   2248:        p->flags |= ttypes[TTYPE_LINK_TEXT];
1.12      kristaps 2249:        for (n = n->next; n; n = n->next)
1.1       kristaps 2250:                term_word(p, n->string);
1.12      kristaps 2251:
1.1       kristaps 2252:        p->flags &= ~ttypes[TTYPE_LINK_TEXT];
                   2253:        return(0);
                   2254: }
                   2255:
                   2256:
                   2257: /* ARGSUSED */
                   2258: static int
                   2259: termp_mt_pre(DECL_ARGS)
                   2260: {
                   2261:
1.28      kristaps 2262:        pair->flag |= ttypes[TTYPE_LINK_ANCHOR];
1.1       kristaps 2263:        return(1);
                   2264: }
                   2265:
                   2266:

CVSweb