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

Annotation of mandoc/mdoc_term.c, Revision 1.63

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

CVSweb