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

Annotation of mandoc/mdoc_term.c, Revision 1.61

1.61    ! kristaps    1: /*     $Id: mdoc_term.c,v 1.60 2009/07/24 11:54:54 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;
                    677:
                    678:        /*
                    679:         * Search through our prior nodes.  If we follow a `Ss' or `Sh',
                    680:         * then don't vspace.
                    681:         */
1.1       kristaps  682:
                    683:        for (n = node; n; n = n->parent) {
                    684:                if (MDOC_BLOCK != n->type)
                    685:                        continue;
                    686:                if (MDOC_Ss == n->tok)
1.54      kristaps  687:                        return;
1.1       kristaps  688:                if (MDOC_Sh == n->tok)
1.54      kristaps  689:                        return;
1.1       kristaps  690:                if (NULL == n->prev)
                    691:                        continue;
                    692:                break;
                    693:        }
                    694:
1.54      kristaps  695:        /*
1.55      kristaps  696:         * XXX - not documented: a `-column' does not ever assert vspace
                    697:         * within the list.
1.54      kristaps  698:         */
                    699:
1.59      kristaps  700:        if (MDOC_Bl == bl->tok && arg_hasattr(MDOC_Column, bl))
1.54      kristaps  701:                if (node->prev && MDOC_It == node->prev->tok)
                    702:                        return;
                    703:
1.55      kristaps  704:        /*
                    705:         * XXX - not documented: a `-diag' without a body does not
                    706:         * assert a vspace prior to the next element.
                    707:         */
1.59      kristaps  708:        if (MDOC_Bl == bl->tok && arg_hasattr(MDOC_Diag, bl))
1.55      kristaps  709:                if (node->prev && MDOC_It == node->prev->tok) {
                    710:                        assert(node->prev->body);
                    711:                        if (NULL == node->prev->body->child)
                    712:                                return;
                    713:                }
                    714:
1.54      kristaps  715:        term_vspace(p);
1.1       kristaps  716: }
                    717:
                    718:
                    719: /* ARGSUSED */
                    720: static int
                    721: termp_dq_pre(DECL_ARGS)
                    722: {
                    723:
                    724:        if (MDOC_BODY != node->type)
                    725:                return(1);
                    726:
                    727:        term_word(p, "\\(lq");
                    728:        p->flags |= TERMP_NOSPACE;
                    729:        return(1);
                    730: }
                    731:
                    732:
                    733: /* ARGSUSED */
                    734: static void
                    735: termp_dq_post(DECL_ARGS)
                    736: {
                    737:
                    738:        if (MDOC_BODY != node->type)
                    739:                return;
                    740:
                    741:        p->flags |= TERMP_NOSPACE;
                    742:        term_word(p, "\\(rq");
                    743: }
                    744:
                    745:
                    746: /* ARGSUSED */
                    747: static int
                    748: termp_it_pre(DECL_ARGS)
                    749: {
                    750:        const struct mdoc_node *bl, *n;
                    751:        char                    buf[7];
1.39      kristaps  752:        int                     i, type, keys[3], vals[3];
1.1       kristaps  753:        size_t                  width, offset;
                    754:
1.54      kristaps  755:        if (MDOC_BLOCK == node->type) {
                    756:                fmt_block_vspace(p, node->parent->parent, node);
                    757:                return(1);
                    758:        }
1.1       kristaps  759:
                    760:        bl = node->parent->parent->parent;
                    761:
                    762:        /* Save parent attributes. */
                    763:
                    764:        pair->flag = p->flags;
                    765:
                    766:        /* Get list width and offset. */
                    767:
                    768:        keys[0] = MDOC_Width;
                    769:        keys[1] = MDOC_Offset;
                    770:        keys[2] = MDOC_Column;
                    771:
                    772:        vals[0] = vals[1] = vals[2] = -1;
                    773:
                    774:        width = offset = 0;
                    775:
                    776:        (void)arg_getattrs(keys, vals, 3, bl);
                    777:
                    778:        type = arg_listtype(bl);
1.38      kristaps  779:        assert(-1 != type);
1.1       kristaps  780:
                    781:        /* Calculate real width and offset. */
                    782:
                    783:        switch (type) {
                    784:        case (MDOC_Column):
                    785:                if (MDOC_BODY == node->type)
                    786:                        break;
1.58      kristaps  787:                /*
                    788:                 * Work around groff's column handling.  The offset is
                    789:                 * equal to the sum of all widths leading to the current
                    790:                 * column (plus the -offset value).  If this column
                    791:                 * exceeds the stated number of columns, the width is
                    792:                 * set as 0, else it's the stated column width (later
                    793:                 * the 0 will be adjusted to default 10 or, if in the
                    794:                 * last column case, set to stretch to the margin).
                    795:                 */
                    796:                for (i = 0, n = node->prev; n && n &&
                    797:                                i < (int)bl->args[vals[2]].argv->sz;
                    798:                                n = n->prev, i++)
1.1       kristaps  799:                        offset += arg_width
                    800:                                (&bl->args->argv[vals[2]], i);
1.58      kristaps  801:
                    802:                /* Whether exceeds maximum column. */
                    803:                if (i < (int)bl->args[vals[2]].argv->sz)
                    804:                        width = arg_width(&bl->args->argv[vals[2]], i);
                    805:                else
                    806:                        width = 0;
                    807:
1.1       kristaps  808:                if (vals[1] >= 0)
                    809:                        offset += arg_offset(&bl->args->argv[vals[1]]);
                    810:                break;
                    811:        default:
                    812:                if (vals[0] >= 0)
                    813:                        width = arg_width(&bl->args->argv[vals[0]], 0);
                    814:                if (vals[1] >= 0)
1.19      kristaps  815:                        offset += arg_offset(&bl->args->argv[vals[1]]);
1.1       kristaps  816:                break;
                    817:        }
                    818:
                    819:        /*
                    820:         * List-type can override the width in the case of fixed-head
                    821:         * values (bullet, dash/hyphen, enum).  Tags need a non-zero
1.38      kristaps  822:         * offset.  FIXME: double-check that correct.
1.1       kristaps  823:         */
                    824:
                    825:        switch (type) {
                    826:        case (MDOC_Bullet):
                    827:                /* FALLTHROUGH */
                    828:        case (MDOC_Dash):
                    829:                /* FALLTHROUGH */
                    830:        case (MDOC_Hyphen):
                    831:                if (width < 4)
                    832:                        width = 4;
                    833:                break;
1.17      kristaps  834:        case (MDOC_Enum):
                    835:                if (width < 5)
                    836:                        width = 5;
                    837:                break;
1.38      kristaps  838:        case (MDOC_Hang):
1.39      kristaps  839:                if (0 == width)
                    840:                        width = 8;
                    841:                break;
1.49      kristaps  842:        case (MDOC_Column):
                    843:                /* FALLTHROUGH */
1.1       kristaps  844:        case (MDOC_Tag):
                    845:                if (0 == width)
                    846:                        width = 10;
                    847:                break;
                    848:        default:
                    849:                break;
                    850:        }
                    851:
                    852:        /*
1.17      kristaps  853:         * Whitespace control.  Inset bodies need an initial space,
                    854:         * while diagonal bodies need two.
1.1       kristaps  855:         */
                    856:
1.51      kristaps  857:        p->flags |= TERMP_NOSPACE;
                    858:
1.1       kristaps  859:        switch (type) {
1.51      kristaps  860:        case (MDOC_Diag):
                    861:                term_word(p, "\\ \\ ");
                    862:                break;
1.1       kristaps  863:        case (MDOC_Inset):
                    864:                if (MDOC_BODY == node->type)
1.51      kristaps  865:                        term_word(p, "\\ ");
1.1       kristaps  866:                break;
                    867:        default:
                    868:                break;
                    869:        }
                    870:
1.51      kristaps  871:        p->flags |= TERMP_NOSPACE;
                    872:
1.1       kristaps  873:        /*
                    874:         * Style flags.  Diagnostic heads need TTYPE_DIAG.
                    875:         */
                    876:
                    877:        switch (type) {
                    878:        case (MDOC_Diag):
                    879:                if (MDOC_HEAD == node->type)
                    880:                        p->flags |= ttypes[TTYPE_DIAG];
                    881:                break;
                    882:        default:
                    883:                break;
                    884:        }
                    885:
                    886:        /*
                    887:         * Pad and break control.  This is the tricker part.  Lists with
                    888:         * set right-margins for the head get TERMP_NOBREAK because, if
                    889:         * they overrun the margin, they wrap to the new margin.
                    890:         * Correspondingly, the body for these types don't left-pad, as
                    891:         * the head will pad out to to the right.
                    892:         */
                    893:
                    894:        switch (type) {
                    895:        case (MDOC_Bullet):
                    896:                /* FALLTHROUGH */
                    897:        case (MDOC_Dash):
                    898:                /* FALLTHROUGH */
                    899:        case (MDOC_Enum):
                    900:                /* FALLTHROUGH */
                    901:        case (MDOC_Hyphen):
1.39      kristaps  902:                if (MDOC_HEAD == node->type)
                    903:                        p->flags |= TERMP_NOBREAK;
                    904:                else
                    905:                        p->flags |= TERMP_NOLPAD;
                    906:                break;
1.38      kristaps  907:        case (MDOC_Hang):
1.39      kristaps  908:                if (MDOC_HEAD == node->type)
                    909:                        p->flags |= TERMP_NOBREAK;
                    910:                else
                    911:                        p->flags |= TERMP_NOLPAD;
                    912:
1.59      kristaps  913:                if (MDOC_HEAD != node->type)
                    914:                        break;
                    915:
                    916:                /*
                    917:                 * This is ugly.  If `-hang' is specified and the body
                    918:                 * is a `Bl' or `Bd', then we want basically to nullify
                    919:                 * the "overstep" effect in term_flushln() and treat
                    920:                 * this as a `-ohang' list instead.
                    921:                 */
                    922:                if (node->next->child &&
                    923:                                (MDOC_Bl == node->next->child->tok ||
                    924:                                 MDOC_Bd == node->next->child->tok)) {
                    925:                        p->flags &= ~TERMP_NOBREAK;
                    926:                        p->flags &= ~TERMP_NOLPAD;
                    927:                } else
1.39      kristaps  928:                        p->flags |= TERMP_HANG;
                    929:                break;
1.1       kristaps  930:        case (MDOC_Tag):
                    931:                if (MDOC_HEAD == node->type)
1.51      kristaps  932:                        p->flags |= TERMP_NOBREAK | TERMP_TWOSPACE;
1.1       kristaps  933:                else
                    934:                        p->flags |= TERMP_NOLPAD;
1.38      kristaps  935:
1.39      kristaps  936:                if (MDOC_HEAD != node->type)
                    937:                        break;
                    938:                if (NULL == node->next || NULL == node->next->child)
                    939:                        p->flags |= TERMP_DANGLE;
1.1       kristaps  940:                break;
                    941:        case (MDOC_Column):
                    942:                if (MDOC_HEAD == node->type) {
                    943:                        assert(node->next);
                    944:                        if (MDOC_BODY == node->next->type)
                    945:                                p->flags &= ~TERMP_NOBREAK;
                    946:                        else
                    947:                                p->flags |= TERMP_NOBREAK;
                    948:                        if (node->prev)
                    949:                                p->flags |= TERMP_NOLPAD;
                    950:                }
                    951:                break;
                    952:        case (MDOC_Diag):
                    953:                if (MDOC_HEAD == node->type)
                    954:                        p->flags |= TERMP_NOBREAK;
                    955:                break;
                    956:        default:
                    957:                break;
                    958:        }
                    959:
                    960:        /*
                    961:         * Margin control.  Set-head-width lists have their right
                    962:         * margins shortened.  The body for these lists has the offset
                    963:         * necessarily lengthened.  Everybody gets the offset.
                    964:         */
                    965:
                    966:        p->offset += offset;
                    967:
                    968:        switch (type) {
1.59      kristaps  969:        case (MDOC_Hang):
                    970:                /*
                    971:                 * Same stipulation as above, regarding `-hang'.  We
                    972:                 * don't want to recalculate rmargin and offsets when
                    973:                 * using `Bd' or `Bl' within `-hang' overstep lists.
                    974:                 */
                    975:                if (MDOC_HEAD == node->type && node->next->child &&
                    976:                                (MDOC_Bl == node->next->child->tok ||
                    977:                                 MDOC_Bd == node->next->child->tok))
                    978:                        break;
                    979:                /* FALLTHROUGH */
1.1       kristaps  980:        case (MDOC_Bullet):
                    981:                /* FALLTHROUGH */
                    982:        case (MDOC_Dash):
                    983:                /* FALLTHROUGH */
                    984:        case (MDOC_Enum):
                    985:                /* FALLTHROUGH */
                    986:        case (MDOC_Hyphen):
                    987:                /* FALLTHROUGH */
                    988:        case (MDOC_Tag):
1.49      kristaps  989:                assert(width);
1.1       kristaps  990:                if (MDOC_HEAD == node->type)
                    991:                        p->rmargin = p->offset + width;
                    992:                else
                    993:                        p->offset += width;
                    994:                break;
                    995:        case (MDOC_Column):
1.49      kristaps  996:                assert(width);
1.1       kristaps  997:                p->rmargin = p->offset + width;
1.50      kristaps  998:                /*
                    999:                 * XXX - this behaviour is not documented: the
                   1000:                 * right-most column is filled to the right margin.
                   1001:                 */
                   1002:                if (MDOC_HEAD == node->type &&
                   1003:                                MDOC_BODY == node->next->type)
                   1004:                        p->rmargin = p->maxrmargin;
1.1       kristaps 1005:                break;
                   1006:        default:
                   1007:                break;
                   1008:        }
                   1009:
                   1010:        /*
                   1011:         * The dash, hyphen, bullet and enum lists all have a special
1.15      kristaps 1012:         * HEAD character (temporarily bold, in some cases).
1.1       kristaps 1013:         */
                   1014:
                   1015:        if (MDOC_HEAD == node->type)
                   1016:                switch (type) {
                   1017:                case (MDOC_Bullet):
1.15      kristaps 1018:                        p->flags |= TERMP_BOLD;
1.1       kristaps 1019:                        term_word(p, "\\[bu]");
1.39      kristaps 1020:                        p->flags &= ~TERMP_BOLD;
1.1       kristaps 1021:                        break;
                   1022:                case (MDOC_Dash):
                   1023:                        /* FALLTHROUGH */
                   1024:                case (MDOC_Hyphen):
1.15      kristaps 1025:                        p->flags |= TERMP_BOLD;
1.27      kristaps 1026:                        term_word(p, "\\(hy");
1.39      kristaps 1027:                        p->flags &= ~TERMP_BOLD;
1.1       kristaps 1028:                        break;
                   1029:                case (MDOC_Enum):
                   1030:                        (pair->ppair->ppair->count)++;
                   1031:                        (void)snprintf(buf, sizeof(buf), "%d.",
                   1032:                                        pair->ppair->ppair->count);
                   1033:                        term_word(p, buf);
                   1034:                        break;
                   1035:                default:
                   1036:                        break;
                   1037:                }
                   1038:
                   1039:        /*
                   1040:         * If we're not going to process our children, indicate so here.
                   1041:         */
                   1042:
                   1043:        switch (type) {
                   1044:        case (MDOC_Bullet):
                   1045:                /* FALLTHROUGH */
                   1046:        case (MDOC_Item):
                   1047:                /* FALLTHROUGH */
                   1048:        case (MDOC_Dash):
                   1049:                /* FALLTHROUGH */
                   1050:        case (MDOC_Hyphen):
                   1051:                /* FALLTHROUGH */
                   1052:        case (MDOC_Enum):
                   1053:                if (MDOC_HEAD == node->type)
                   1054:                        return(0);
                   1055:                break;
                   1056:        case (MDOC_Column):
                   1057:                if (MDOC_BODY == node->type)
                   1058:                        return(0);
                   1059:                break;
                   1060:        default:
                   1061:                break;
                   1062:        }
                   1063:
                   1064:        return(1);
                   1065: }
                   1066:
                   1067:
                   1068: /* ARGSUSED */
                   1069: static void
                   1070: termp_it_post(DECL_ARGS)
                   1071: {
                   1072:        int                type;
                   1073:
                   1074:        if (MDOC_BODY != node->type && MDOC_HEAD != node->type)
                   1075:                return;
                   1076:
                   1077:        type = arg_listtype(node->parent->parent->parent);
1.38      kristaps 1078:        assert(-1 != type);
1.1       kristaps 1079:
                   1080:        switch (type) {
                   1081:        case (MDOC_Item):
                   1082:                /* FALLTHROUGH */
1.53      kristaps 1083:        case (MDOC_Diag):
                   1084:                /* FALLTHROUGH */
1.1       kristaps 1085:        case (MDOC_Inset):
                   1086:                if (MDOC_BODY == node->type)
                   1087:                        term_flushln(p);
                   1088:                break;
                   1089:        case (MDOC_Column):
                   1090:                if (MDOC_HEAD == node->type)
                   1091:                        term_flushln(p);
                   1092:                break;
                   1093:        default:
                   1094:                term_flushln(p);
                   1095:                break;
                   1096:        }
                   1097:
                   1098:        p->flags = pair->flag;
                   1099: }
                   1100:
                   1101:
                   1102: /* ARGSUSED */
                   1103: static int
                   1104: termp_nm_pre(DECL_ARGS)
                   1105: {
                   1106:
                   1107:        if (SEC_SYNOPSIS == node->sec)
                   1108:                term_newln(p);
                   1109:
1.28      kristaps 1110:        pair->flag |= ttypes[TTYPE_PROG];
                   1111:        p->flags |= ttypes[TTYPE_PROG];
                   1112:
1.1       kristaps 1113:        if (NULL == node->child)
                   1114:                term_word(p, meta->name);
                   1115:
                   1116:        return(1);
                   1117: }
                   1118:
                   1119:
                   1120: /* ARGSUSED */
                   1121: static int
                   1122: termp_fl_pre(DECL_ARGS)
                   1123: {
                   1124:
1.28      kristaps 1125:        pair->flag |= ttypes[TTYPE_CMD_FLAG];
                   1126:        p->flags |= ttypes[TTYPE_CMD_FLAG];
1.1       kristaps 1127:        term_word(p, "\\-");
                   1128:        p->flags |= TERMP_NOSPACE;
                   1129:        return(1);
                   1130: }
                   1131:
                   1132:
                   1133: /* ARGSUSED */
                   1134: static int
1.61    ! kristaps 1135: termp_an_pre(DECL_ARGS)
        !          1136: {
        !          1137:
        !          1138:        if (NULL == node->child)
        !          1139:                return(1);
        !          1140:
        !          1141:        /*
        !          1142:         * XXX: this is poorly documented.  If not in the AUTHORS
        !          1143:         * section, `An -split' will cause newlines to occur before the
        !          1144:         * author name.  If in the AUTHORS section, by default, the
        !          1145:         * first `An' invocation is nosplit, then all subsequent ones,
        !          1146:         * regardless of whether interspersed with other macros/text,
        !          1147:         * are split.  -split, in this case, will override the condition
        !          1148:         * of the implied first -nosplit.
        !          1149:         */
        !          1150:
        !          1151:        if (node->sec == SEC_AUTHORS) {
        !          1152:                if ( ! (TERMP_ANPREC & p->flags)) {
        !          1153:                        if (TERMP_SPLIT & p->flags)
        !          1154:                                term_newln(p);
        !          1155:                        return(1);
        !          1156:                }
        !          1157:                if (TERMP_NOSPLIT & p->flags)
        !          1158:                        return(1);
        !          1159:                term_newln(p);
        !          1160:                return(1);
        !          1161:        }
        !          1162:
        !          1163:        if (TERMP_SPLIT & p->flags)
        !          1164:                term_newln(p);
        !          1165:
        !          1166:        return(1);
        !          1167: }
        !          1168:
        !          1169:
        !          1170: /* ARGSUSED */
        !          1171: static void
        !          1172: termp_an_post(DECL_ARGS)
        !          1173: {
        !          1174:
        !          1175:        if (node->child) {
        !          1176:                if (SEC_AUTHORS == node->sec)
        !          1177:                        p->flags |= TERMP_ANPREC;
        !          1178:                return;
        !          1179:        }
        !          1180:
        !          1181:        if (arg_getattr(MDOC_Split, node) > -1) {
        !          1182:                p->flags &= ~TERMP_NOSPLIT;
        !          1183:                p->flags |= TERMP_SPLIT;
        !          1184:        } else {
        !          1185:                p->flags &= ~TERMP_SPLIT;
        !          1186:                p->flags |= TERMP_NOSPLIT;
        !          1187:        }
        !          1188:
        !          1189: }
        !          1190:
        !          1191:
        !          1192: /* ARGSUSED */
        !          1193: static int
1.1       kristaps 1194: termp_ar_pre(DECL_ARGS)
                   1195: {
                   1196:
1.28      kristaps 1197:        pair->flag |= ttypes[TTYPE_CMD_ARG];
1.1       kristaps 1198:        return(1);
                   1199: }
                   1200:
                   1201:
                   1202: /* ARGSUSED */
                   1203: static int
                   1204: termp_ns_pre(DECL_ARGS)
                   1205: {
                   1206:
                   1207:        p->flags |= TERMP_NOSPACE;
                   1208:        return(1);
                   1209: }
                   1210:
                   1211:
                   1212: /* ARGSUSED */
                   1213: static int
                   1214: termp_pp_pre(DECL_ARGS)
                   1215: {
                   1216:
                   1217:        term_vspace(p);
                   1218:        return(1);
                   1219: }
                   1220:
                   1221:
                   1222: /* ARGSUSED */
                   1223: static int
                   1224: termp_rs_pre(DECL_ARGS)
                   1225: {
                   1226:
                   1227:        if (MDOC_BLOCK == node->type && node->prev)
                   1228:                term_vspace(p);
                   1229:        return(1);
                   1230: }
                   1231:
                   1232:
                   1233: /* ARGSUSED */
                   1234: static int
                   1235: termp_rv_pre(DECL_ARGS)
                   1236: {
                   1237:        int              i;
                   1238:
1.34      kristaps 1239:        i = arg_getattr(MDOC_Std, node);
                   1240:        assert(-1 != i);
                   1241:        assert(node->args->argv[i].sz);
1.1       kristaps 1242:
                   1243:        term_newln(p);
                   1244:        term_word(p, "The");
                   1245:
                   1246:        p->flags |= ttypes[TTYPE_FUNC_NAME];
                   1247:        term_word(p, *node->args->argv[i].value);
                   1248:        p->flags &= ~ttypes[TTYPE_FUNC_NAME];
                   1249:        p->flags |= TERMP_NOSPACE;
                   1250:
                   1251:                term_word(p, "() function returns the value 0 if successful;");
                   1252:                term_word(p, "otherwise the value -1 is returned and the");
                   1253:                term_word(p, "global variable");
                   1254:
                   1255:        p->flags |= ttypes[TTYPE_VAR_DECL];
                   1256:        term_word(p, "errno");
                   1257:        p->flags &= ~ttypes[TTYPE_VAR_DECL];
                   1258:
                   1259:                term_word(p, "is set to indicate the error.");
                   1260:
                   1261:        return(1);
                   1262: }
                   1263:
                   1264:
                   1265: /* ARGSUSED */
                   1266: static int
                   1267: termp_ex_pre(DECL_ARGS)
                   1268: {
                   1269:        int              i;
                   1270:
1.34      kristaps 1271:        i = arg_getattr(MDOC_Std, node);
                   1272:        assert(-1 != i);
                   1273:        assert(node->args->argv[i].sz);
1.1       kristaps 1274:
                   1275:        term_word(p, "The");
                   1276:        p->flags |= ttypes[TTYPE_PROG];
                   1277:        term_word(p, *node->args->argv[i].value);
                   1278:        p->flags &= ~ttypes[TTYPE_PROG];
                   1279:                term_word(p, "utility exits 0 on success, and >0 if an error occurs.");
                   1280:
                   1281:        return(1);
                   1282: }
                   1283:
                   1284:
                   1285: /* ARGSUSED */
                   1286: static int
                   1287: termp_nd_pre(DECL_ARGS)
                   1288: {
1.25      kristaps 1289:
                   1290:        if (MDOC_BODY != node->type)
                   1291:                return(1);
1.30      kristaps 1292:
1.27      kristaps 1293: #if defined(__OpenBSD__) || defined(__linux__)
1.41      kristaps 1294:        term_word(p, "\\(en");
1.23      kristaps 1295: #else
                   1296:        term_word(p, "\\(em");
                   1297: #endif
1.1       kristaps 1298:        return(1);
                   1299: }
                   1300:
                   1301:
                   1302: /* ARGSUSED */
                   1303: static void
                   1304: termp_bl_post(DECL_ARGS)
                   1305: {
                   1306:
                   1307:        if (MDOC_BLOCK == node->type)
                   1308:                term_newln(p);
                   1309: }
                   1310:
                   1311:
                   1312: /* ARGSUSED */
                   1313: static void
                   1314: termp_op_post(DECL_ARGS)
                   1315: {
                   1316:
                   1317:        if (MDOC_BODY != node->type)
                   1318:                return;
                   1319:        p->flags |= TERMP_NOSPACE;
                   1320:        term_word(p, "\\(rB");
                   1321: }
                   1322:
                   1323:
                   1324: /* ARGSUSED */
                   1325: static int
                   1326: termp_xr_pre(DECL_ARGS)
                   1327: {
                   1328:        const struct mdoc_node *n;
                   1329:
1.11      kristaps 1330:        assert(node->child && MDOC_TEXT == node->child->type);
                   1331:        n = node->child;
                   1332:
1.1       kristaps 1333:        term_word(p, n->string);
                   1334:        if (NULL == (n = n->next))
                   1335:                return(0);
                   1336:        p->flags |= TERMP_NOSPACE;
                   1337:        term_word(p, "(");
                   1338:        p->flags |= TERMP_NOSPACE;
                   1339:        term_word(p, n->string);
                   1340:        p->flags |= TERMP_NOSPACE;
                   1341:        term_word(p, ")");
                   1342:        return(0);
                   1343: }
                   1344:
                   1345:
                   1346: /* ARGSUSED */
                   1347: static int
                   1348: termp_vt_pre(DECL_ARGS)
                   1349: {
                   1350:
                   1351:        /* FIXME: this can be "type name". */
1.29      kristaps 1352:        pair->flag |= ttypes[TTYPE_VAR_DECL];
1.1       kristaps 1353:        return(1);
                   1354: }
                   1355:
                   1356:
                   1357: /* ARGSUSED */
                   1358: static void
                   1359: termp_vt_post(DECL_ARGS)
                   1360: {
                   1361:
1.32      kristaps 1362:        if (node->sec != SEC_SYNOPSIS)
                   1363:                return;
                   1364:        if (node->next && MDOC_Vt == node->next->tok)
                   1365:                term_newln(p);
                   1366:        else if (node->next)
1.1       kristaps 1367:                term_vspace(p);
                   1368: }
                   1369:
                   1370:
                   1371: /* ARGSUSED */
                   1372: static int
                   1373: termp_fd_pre(DECL_ARGS)
                   1374: {
                   1375:
1.29      kristaps 1376:        pair->flag |= ttypes[TTYPE_FUNC_DECL];
1.1       kristaps 1377:        return(1);
                   1378: }
                   1379:
                   1380:
                   1381: /* ARGSUSED */
                   1382: static void
                   1383: termp_fd_post(DECL_ARGS)
                   1384: {
                   1385:
                   1386:        if (node->sec != SEC_SYNOPSIS)
                   1387:                return;
1.29      kristaps 1388:
1.1       kristaps 1389:        term_newln(p);
                   1390:        if (node->next && MDOC_Fd != node->next->tok)
                   1391:                term_vspace(p);
                   1392: }
                   1393:
                   1394:
                   1395: /* ARGSUSED */
                   1396: static int
                   1397: termp_sh_pre(DECL_ARGS)
                   1398: {
                   1399:
                   1400:        switch (node->type) {
                   1401:        case (MDOC_HEAD):
                   1402:                term_vspace(p);
1.28      kristaps 1403:                pair->flag |= ttypes[TTYPE_SECTION];
1.1       kristaps 1404:                break;
                   1405:        case (MDOC_BODY):
                   1406:                p->offset = INDENT;
                   1407:                break;
                   1408:        default:
                   1409:                break;
                   1410:        }
                   1411:        return(1);
                   1412: }
                   1413:
                   1414:
                   1415: /* ARGSUSED */
                   1416: static void
                   1417: termp_sh_post(DECL_ARGS)
                   1418: {
                   1419:
                   1420:        switch (node->type) {
                   1421:        case (MDOC_HEAD):
                   1422:                term_newln(p);
                   1423:                break;
                   1424:        case (MDOC_BODY):
                   1425:                term_newln(p);
                   1426:                p->offset = 0;
                   1427:                break;
                   1428:        default:
                   1429:                break;
                   1430:        }
                   1431: }
                   1432:
                   1433:
                   1434: /* ARGSUSED */
                   1435: static int
                   1436: termp_op_pre(DECL_ARGS)
                   1437: {
                   1438:
                   1439:        switch (node->type) {
                   1440:        case (MDOC_BODY):
                   1441:                term_word(p, "\\(lB");
                   1442:                p->flags |= TERMP_NOSPACE;
                   1443:                break;
                   1444:        default:
                   1445:                break;
                   1446:        }
                   1447:        return(1);
                   1448: }
                   1449:
                   1450:
                   1451: /* ARGSUSED */
                   1452: static int
                   1453: termp_bt_pre(DECL_ARGS)
                   1454: {
                   1455:
                   1456:        term_word(p, "is currently in beta test.");
                   1457:        return(1);
                   1458: }
                   1459:
                   1460:
                   1461: /* ARGSUSED */
                   1462: static void
                   1463: termp_lb_post(DECL_ARGS)
                   1464: {
                   1465:
                   1466:        term_newln(p);
                   1467: }
                   1468:
                   1469:
                   1470: /* ARGSUSED */
                   1471: static int
                   1472: termp_ud_pre(DECL_ARGS)
                   1473: {
                   1474:
                   1475:        term_word(p, "currently under development.");
                   1476:        return(1);
                   1477: }
                   1478:
                   1479:
                   1480: /* ARGSUSED */
                   1481: static int
                   1482: termp_d1_pre(DECL_ARGS)
                   1483: {
                   1484:
1.4       kristaps 1485:        if (MDOC_BLOCK != node->type)
1.1       kristaps 1486:                return(1);
                   1487:        term_newln(p);
1.29      kristaps 1488:        p->offset += (INDENT + 1);
1.1       kristaps 1489:        return(1);
                   1490: }
                   1491:
                   1492:
                   1493: /* ARGSUSED */
                   1494: static void
                   1495: termp_d1_post(DECL_ARGS)
                   1496: {
                   1497:
1.4       kristaps 1498:        if (MDOC_BLOCK != node->type)
1.1       kristaps 1499:                return;
                   1500:        term_newln(p);
                   1501: }
                   1502:
                   1503:
                   1504: /* ARGSUSED */
                   1505: static int
                   1506: termp_aq_pre(DECL_ARGS)
                   1507: {
                   1508:
                   1509:        if (MDOC_BODY != node->type)
                   1510:                return(1);
                   1511:        term_word(p, "\\(la");
                   1512:        p->flags |= TERMP_NOSPACE;
                   1513:        return(1);
                   1514: }
                   1515:
                   1516:
                   1517: /* ARGSUSED */
                   1518: static void
                   1519: termp_aq_post(DECL_ARGS)
                   1520: {
                   1521:
                   1522:        if (MDOC_BODY != node->type)
                   1523:                return;
                   1524:        p->flags |= TERMP_NOSPACE;
                   1525:        term_word(p, "\\(ra");
                   1526: }
                   1527:
                   1528:
                   1529: /* ARGSUSED */
                   1530: static int
                   1531: termp_ft_pre(DECL_ARGS)
                   1532: {
                   1533:
                   1534:        if (SEC_SYNOPSIS == node->sec)
                   1535:                if (node->prev && MDOC_Fo == node->prev->tok)
                   1536:                        term_vspace(p);
1.28      kristaps 1537:        pair->flag |= ttypes[TTYPE_FUNC_TYPE];
1.1       kristaps 1538:        return(1);
                   1539: }
                   1540:
                   1541:
                   1542: /* ARGSUSED */
                   1543: static void
                   1544: termp_ft_post(DECL_ARGS)
                   1545: {
                   1546:
                   1547:        if (SEC_SYNOPSIS == node->sec)
                   1548:                term_newln(p);
                   1549: }
                   1550:
                   1551:
                   1552: /* ARGSUSED */
                   1553: static int
                   1554: termp_fn_pre(DECL_ARGS)
                   1555: {
                   1556:        const struct mdoc_node *n;
                   1557:
1.11      kristaps 1558:        assert(node->child && MDOC_TEXT == node->child->type);
1.1       kristaps 1559:
                   1560:        /* FIXME: can be "type funcname" "type varname"... */
                   1561:
                   1562:        p->flags |= ttypes[TTYPE_FUNC_NAME];
                   1563:        term_word(p, node->child->string);
                   1564:        p->flags &= ~ttypes[TTYPE_FUNC_NAME];
                   1565:
                   1566:        p->flags |= TERMP_NOSPACE;
                   1567:        term_word(p, "(");
                   1568:
                   1569:        for (n = node->child->next; n; n = n->next) {
                   1570:                p->flags |= ttypes[TTYPE_FUNC_ARG];
                   1571:                term_word(p, n->string);
                   1572:                p->flags &= ~ttypes[TTYPE_FUNC_ARG];
                   1573:                if (n->next)
                   1574:                        term_word(p, ",");
                   1575:        }
                   1576:
                   1577:        term_word(p, ")");
                   1578:
                   1579:        if (SEC_SYNOPSIS == node->sec)
                   1580:                term_word(p, ";");
                   1581:
                   1582:        return(0);
                   1583: }
                   1584:
                   1585:
                   1586: /* ARGSUSED */
                   1587: static void
                   1588: termp_fn_post(DECL_ARGS)
                   1589: {
                   1590:
                   1591:        if (node->sec == SEC_SYNOPSIS && node->next)
                   1592:                term_vspace(p);
                   1593: }
                   1594:
                   1595:
                   1596: /* ARGSUSED */
                   1597: static int
                   1598: termp_sx_pre(DECL_ARGS)
                   1599: {
                   1600:
1.28      kristaps 1601:        pair->flag |= ttypes[TTYPE_LINK];
1.1       kristaps 1602:        return(1);
                   1603: }
                   1604:
                   1605:
                   1606: /* ARGSUSED */
                   1607: static int
                   1608: termp_fa_pre(DECL_ARGS)
                   1609: {
                   1610:        struct mdoc_node *n;
                   1611:
                   1612:        if (node->parent->tok != MDOC_Fo) {
1.28      kristaps 1613:                pair->flag |= ttypes[TTYPE_FUNC_ARG];
1.1       kristaps 1614:                return(1);
                   1615:        }
                   1616:
                   1617:        for (n = node->child; n; n = n->next) {
                   1618:                p->flags |= ttypes[TTYPE_FUNC_ARG];
                   1619:                term_word(p, n->string);
                   1620:                p->flags &= ~ttypes[TTYPE_FUNC_ARG];
                   1621:                if (n->next)
                   1622:                        term_word(p, ",");
                   1623:        }
                   1624:
                   1625:        if (node->child && node->next && node->next->tok == MDOC_Fa)
                   1626:                term_word(p, ",");
                   1627:
                   1628:        return(0);
                   1629: }
                   1630:
                   1631:
                   1632: /* ARGSUSED */
                   1633: static int
                   1634: termp_va_pre(DECL_ARGS)
                   1635: {
                   1636:
1.28      kristaps 1637:        pair->flag |= ttypes[TTYPE_VAR_DECL];
1.1       kristaps 1638:        return(1);
                   1639: }
                   1640:
                   1641:
                   1642: /* ARGSUSED */
                   1643: static int
                   1644: termp_bd_pre(DECL_ARGS)
                   1645: {
1.60      kristaps 1646:        int              i, type;
1.1       kristaps 1647:
                   1648:        /*
                   1649:         * This is fairly tricky due primarily to crappy documentation.
                   1650:         * If -ragged or -filled are specified, the block does nothing
                   1651:         * but change the indentation.
                   1652:         *
                   1653:         * If, on the other hand, -unfilled or -literal are specified,
                   1654:         * then the game changes.  Text is printed exactly as entered in
                   1655:         * the display: if a macro line, a newline is appended to the
                   1656:         * line.  Blank lines are allowed.
                   1657:         */
                   1658:
1.54      kristaps 1659:        if (MDOC_BLOCK == node->type) {
                   1660:                fmt_block_vspace(p, node, node);
                   1661:                return(1);
                   1662:        } else if (MDOC_BODY != node->type)
1.1       kristaps 1663:                return(1);
                   1664:
1.60      kristaps 1665:        assert(node->parent->args);
1.11      kristaps 1666:
1.60      kristaps 1667:        for (type = -1, i = 0; -1 == type &&
1.1       kristaps 1668:                        i < (int)node->parent->args->argc; i++) {
                   1669:                switch (node->parent->args->argv[i].arg) {
                   1670:                case (MDOC_Ragged):
                   1671:                        /* FALLTHROUGH */
                   1672:                case (MDOC_Filled):
                   1673:                        /* FALLTHROUGH */
                   1674:                case (MDOC_Unfilled):
                   1675:                        /* FALLTHROUGH */
                   1676:                case (MDOC_Literal):
                   1677:                        type = node->parent->args->argv[i].arg;
                   1678:                        break;
                   1679:                default:
                   1680:                        break;
                   1681:                }
                   1682:        }
1.60      kristaps 1683:
                   1684:        assert(type > -1);
1.1       kristaps 1685:
                   1686:        i = arg_getattr(MDOC_Offset, node->parent);
1.60      kristaps 1687:        if (-1 != i)
1.1       kristaps 1688:                p->offset += arg_offset(&node->parent->args->argv[i]);
                   1689:
                   1690:        switch (type) {
                   1691:        case (MDOC_Literal):
                   1692:                /* FALLTHROUGH */
                   1693:        case (MDOC_Unfilled):
                   1694:                break;
                   1695:        default:
                   1696:                return(1);
                   1697:        }
                   1698:
                   1699:        for (node = node->child; node; node = node->next) {
1.60      kristaps 1700:                p->flags |= TERMP_NOSPACE;
                   1701:                print_node(p, pair, meta, node);
                   1702:                if (node->next)
1.1       kristaps 1703:                        term_flushln(p);
                   1704:        }
                   1705:
                   1706:        return(0);
                   1707: }
                   1708:
                   1709:
                   1710: /* ARGSUSED */
                   1711: static void
                   1712: termp_bd_post(DECL_ARGS)
                   1713: {
                   1714:
                   1715:        if (MDOC_BODY != node->type)
                   1716:                return;
1.60      kristaps 1717:        p->flags |= TERMP_NOSPACE;
1.1       kristaps 1718:        term_flushln(p);
                   1719: }
                   1720:
                   1721:
                   1722: /* ARGSUSED */
                   1723: static int
                   1724: termp_qq_pre(DECL_ARGS)
                   1725: {
                   1726:
                   1727:        if (MDOC_BODY != node->type)
                   1728:                return(1);
                   1729:        term_word(p, "\"");
                   1730:        p->flags |= TERMP_NOSPACE;
                   1731:        return(1);
                   1732: }
                   1733:
                   1734:
                   1735: /* ARGSUSED */
                   1736: static void
                   1737: termp_qq_post(DECL_ARGS)
                   1738: {
                   1739:
                   1740:        if (MDOC_BODY != node->type)
                   1741:                return;
                   1742:        p->flags |= TERMP_NOSPACE;
                   1743:        term_word(p, "\"");
                   1744: }
                   1745:
                   1746:
                   1747: /* ARGSUSED */
                   1748: static void
                   1749: termp_bx_post(DECL_ARGS)
                   1750: {
                   1751:
                   1752:        if (node->child)
                   1753:                p->flags |= TERMP_NOSPACE;
                   1754:        term_word(p, "BSD");
                   1755: }
                   1756:
                   1757:
                   1758: /* ARGSUSED */
                   1759: static int
1.26      kristaps 1760: termp_xx_pre(DECL_ARGS)
1.1       kristaps 1761: {
1.26      kristaps 1762:        const char      *pp;
1.1       kristaps 1763:
1.26      kristaps 1764:        pp = NULL;
                   1765:        switch (node->tok) {
                   1766:        case (MDOC_Bsx):
                   1767:                pp = "BSDI BSD/OS";
                   1768:                break;
                   1769:        case (MDOC_Dx):
                   1770:                pp = "DragonFlyBSD";
                   1771:                break;
                   1772:        case (MDOC_Fx):
                   1773:                pp = "FreeBSD";
                   1774:                break;
                   1775:        case (MDOC_Nx):
                   1776:                pp = "NetBSD";
                   1777:                break;
                   1778:        case (MDOC_Ox):
                   1779:                pp = "OpenBSD";
                   1780:                break;
                   1781:        case (MDOC_Ux):
                   1782:                pp = "UNIX";
                   1783:                break;
                   1784:        default:
                   1785:                break;
                   1786:        }
1.1       kristaps 1787:
1.26      kristaps 1788:        assert(pp);
                   1789:        term_word(p, pp);
1.1       kristaps 1790:        return(1);
                   1791: }
                   1792:
                   1793:
                   1794: /* ARGSUSED */
                   1795: static int
                   1796: termp_sq_pre(DECL_ARGS)
                   1797: {
                   1798:
                   1799:        if (MDOC_BODY != node->type)
                   1800:                return(1);
                   1801:        term_word(p, "\\(oq");
                   1802:        p->flags |= TERMP_NOSPACE;
                   1803:        return(1);
                   1804: }
                   1805:
                   1806:
                   1807: /* ARGSUSED */
                   1808: static void
                   1809: termp_sq_post(DECL_ARGS)
                   1810: {
                   1811:
                   1812:        if (MDOC_BODY != node->type)
                   1813:                return;
                   1814:        p->flags |= TERMP_NOSPACE;
                   1815:        term_word(p, "\\(aq");
                   1816: }
                   1817:
                   1818:
                   1819: /* ARGSUSED */
                   1820: static int
1.57      kristaps 1821: termp_pa_pre(DECL_ARGS)
                   1822: {
                   1823:
                   1824:        pair->flag |= ttypes[TTYPE_FILE];
                   1825:        return(1);
                   1826: }
                   1827:
                   1828:
                   1829: /* ARGSUSED */
                   1830: static int
1.1       kristaps 1831: termp_pf_pre(DECL_ARGS)
                   1832: {
                   1833:
                   1834:        p->flags |= TERMP_IGNDELIM;
                   1835:        return(1);
                   1836: }
                   1837:
                   1838:
                   1839: /* ARGSUSED */
                   1840: static void
                   1841: termp_pf_post(DECL_ARGS)
                   1842: {
                   1843:
                   1844:        p->flags &= ~TERMP_IGNDELIM;
                   1845:        p->flags |= TERMP_NOSPACE;
                   1846: }
                   1847:
                   1848:
                   1849: /* ARGSUSED */
                   1850: static int
                   1851: termp_ss_pre(DECL_ARGS)
                   1852: {
                   1853:
                   1854:        switch (node->type) {
                   1855:        case (MDOC_BLOCK):
                   1856:                term_newln(p);
                   1857:                if (node->prev)
                   1858:                        term_vspace(p);
                   1859:                break;
                   1860:        case (MDOC_HEAD):
1.28      kristaps 1861:                pair->flag |= ttypes[TTYPE_SSECTION];
1.5       kristaps 1862:                p->offset = HALFINDENT;
1.1       kristaps 1863:                break;
                   1864:        default:
                   1865:                break;
                   1866:        }
                   1867:
                   1868:        return(1);
                   1869: }
                   1870:
                   1871:
                   1872: /* ARGSUSED */
                   1873: static void
                   1874: termp_ss_post(DECL_ARGS)
                   1875: {
                   1876:
1.29      kristaps 1877:        if (MDOC_HEAD == node->type)
1.1       kristaps 1878:                term_newln(p);
                   1879: }
                   1880:
                   1881:
                   1882: /* ARGSUSED */
                   1883: static int
                   1884: termp_em_pre(DECL_ARGS)
                   1885: {
                   1886:
1.28      kristaps 1887:        pair->flag |= ttypes[TTYPE_EMPH];
1.1       kristaps 1888:        return(1);
                   1889: }
                   1890:
                   1891:
                   1892: /* ARGSUSED */
                   1893: static int
                   1894: termp_cd_pre(DECL_ARGS)
                   1895: {
                   1896:
1.28      kristaps 1897:        pair->flag |= ttypes[TTYPE_CONFIG];
1.1       kristaps 1898:        term_newln(p);
                   1899:        return(1);
                   1900: }
                   1901:
                   1902:
                   1903: /* ARGSUSED */
                   1904: static int
                   1905: termp_cm_pre(DECL_ARGS)
                   1906: {
                   1907:
1.28      kristaps 1908:        pair->flag |= ttypes[TTYPE_CMD_FLAG];
1.1       kristaps 1909:        return(1);
                   1910: }
                   1911:
                   1912:
                   1913: /* ARGSUSED */
                   1914: static int
                   1915: termp_ic_pre(DECL_ARGS)
                   1916: {
                   1917:
1.28      kristaps 1918:        pair->flag |= ttypes[TTYPE_CMD];
1.1       kristaps 1919:        return(1);
                   1920: }
                   1921:
                   1922:
                   1923: /* ARGSUSED */
                   1924: static int
                   1925: termp_in_pre(DECL_ARGS)
                   1926: {
                   1927:
1.28      kristaps 1928:        pair->flag |= ttypes[TTYPE_INCLUDE];
                   1929:        p->flags |= ttypes[TTYPE_INCLUDE];
1.22      kristaps 1930:
                   1931:        if (SEC_SYNOPSIS == node->sec)
                   1932:                term_word(p, "#include");
                   1933:
1.1       kristaps 1934:        term_word(p, "<");
                   1935:        p->flags |= TERMP_NOSPACE;
                   1936:        return(1);
                   1937: }
                   1938:
                   1939:
                   1940: /* ARGSUSED */
                   1941: static void
                   1942: termp_in_post(DECL_ARGS)
                   1943: {
                   1944:
1.46      kristaps 1945:        p->flags |= TERMP_NOSPACE | ttypes[TTYPE_INCLUDE];
1.1       kristaps 1946:        term_word(p, ">");
1.46      kristaps 1947:        p->flags &= ~ttypes[TTYPE_INCLUDE];
1.1       kristaps 1948:
                   1949:        if (SEC_SYNOPSIS != node->sec)
                   1950:                return;
1.22      kristaps 1951:
                   1952:        term_newln(p);
                   1953:        /*
                   1954:         * XXX Not entirely correct.  If `.In foo bar' is specified in
                   1955:         * the SYNOPSIS section, then it produces a single break after
                   1956:         * the <foo>; mandoc asserts a vertical space.  Since this
                   1957:         * construction is rarely used, I think it's fine.
                   1958:         */
1.1       kristaps 1959:        if (node->next && MDOC_In != node->next->tok)
                   1960:                term_vspace(p);
                   1961: }
                   1962:
                   1963:
                   1964: /* ARGSUSED */
                   1965: static int
1.45      kristaps 1966: termp_sp_pre(DECL_ARGS)
                   1967: {
                   1968:        int              i, len;
                   1969:
                   1970:        if (NULL == node->child) {
                   1971:                term_vspace(p);
                   1972:                return(0);
                   1973:        }
                   1974:
                   1975:        len = atoi(node->child->string);
                   1976:        if (0 == len)
                   1977:                term_newln(p);
                   1978:        for (i = 0; i < len; i++)
                   1979:                term_vspace(p);
                   1980:
                   1981:        return(0);
                   1982: }
                   1983:
                   1984:
                   1985: /* ARGSUSED */
                   1986: static int
1.44      kristaps 1987: termp_br_pre(DECL_ARGS)
                   1988: {
                   1989:
                   1990:        term_newln(p);
                   1991:        return(1);
                   1992: }
                   1993:
                   1994:
                   1995: /* ARGSUSED */
                   1996: static int
1.1       kristaps 1997: termp_brq_pre(DECL_ARGS)
                   1998: {
                   1999:
                   2000:        if (MDOC_BODY != node->type)
                   2001:                return(1);
                   2002:        term_word(p, "\\(lC");
                   2003:        p->flags |= TERMP_NOSPACE;
                   2004:        return(1);
                   2005: }
                   2006:
                   2007:
                   2008: /* ARGSUSED */
                   2009: static void
                   2010: termp_brq_post(DECL_ARGS)
                   2011: {
                   2012:
                   2013:        if (MDOC_BODY != node->type)
                   2014:                return;
                   2015:        p->flags |= TERMP_NOSPACE;
                   2016:        term_word(p, "\\(rC");
                   2017: }
                   2018:
                   2019:
                   2020: /* ARGSUSED */
                   2021: static int
                   2022: termp_bq_pre(DECL_ARGS)
                   2023: {
                   2024:
                   2025:        if (MDOC_BODY != node->type)
                   2026:                return(1);
                   2027:        term_word(p, "\\(lB");
                   2028:        p->flags |= TERMP_NOSPACE;
                   2029:        return(1);
                   2030: }
                   2031:
                   2032:
                   2033: /* ARGSUSED */
                   2034: static void
                   2035: termp_bq_post(DECL_ARGS)
                   2036: {
                   2037:
                   2038:        if (MDOC_BODY != node->type)
                   2039:                return;
                   2040:        p->flags |= TERMP_NOSPACE;
                   2041:        term_word(p, "\\(rB");
                   2042: }
                   2043:
                   2044:
                   2045: /* ARGSUSED */
                   2046: static int
                   2047: termp_pq_pre(DECL_ARGS)
                   2048: {
                   2049:
                   2050:        if (MDOC_BODY != node->type)
                   2051:                return(1);
                   2052:        term_word(p, "\\&(");
                   2053:        p->flags |= TERMP_NOSPACE;
                   2054:        return(1);
                   2055: }
                   2056:
                   2057:
                   2058: /* ARGSUSED */
                   2059: static void
                   2060: termp_pq_post(DECL_ARGS)
                   2061: {
                   2062:
                   2063:        if (MDOC_BODY != node->type)
                   2064:                return;
                   2065:        term_word(p, ")");
                   2066: }
                   2067:
                   2068:
                   2069: /* ARGSUSED */
                   2070: static int
                   2071: termp_fo_pre(DECL_ARGS)
                   2072: {
                   2073:        const struct mdoc_node *n;
                   2074:
                   2075:        if (MDOC_BODY == node->type) {
1.33      kristaps 2076:                p->flags |= TERMP_NOSPACE;
1.1       kristaps 2077:                term_word(p, "(");
                   2078:                p->flags |= TERMP_NOSPACE;
                   2079:                return(1);
                   2080:        } else if (MDOC_HEAD != node->type)
                   2081:                return(1);
                   2082:
                   2083:        p->flags |= ttypes[TTYPE_FUNC_NAME];
                   2084:        for (n = node->child; n; n = n->next) {
1.11      kristaps 2085:                assert(MDOC_TEXT == n->type);
1.1       kristaps 2086:                term_word(p, n->string);
                   2087:        }
                   2088:        p->flags &= ~ttypes[TTYPE_FUNC_NAME];
                   2089:
                   2090:        return(0);
                   2091: }
                   2092:
                   2093:
                   2094: /* ARGSUSED */
                   2095: static void
                   2096: termp_fo_post(DECL_ARGS)
                   2097: {
                   2098:
                   2099:        if (MDOC_BODY != node->type)
                   2100:                return;
                   2101:        p->flags |= TERMP_NOSPACE;
                   2102:        term_word(p, ")");
                   2103:        p->flags |= TERMP_NOSPACE;
                   2104:        term_word(p, ";");
                   2105:        term_newln(p);
                   2106: }
                   2107:
                   2108:
                   2109: /* ARGSUSED */
                   2110: static int
                   2111: termp_bf_pre(DECL_ARGS)
                   2112: {
                   2113:        const struct mdoc_node  *n;
                   2114:
1.28      kristaps 2115:        if (MDOC_HEAD == node->type)
1.1       kristaps 2116:                return(0);
1.28      kristaps 2117:        else if (MDOC_BLOCK != node->type)
1.1       kristaps 2118:                return(1);
                   2119:
                   2120:        if (NULL == (n = node->head->child)) {
                   2121:                if (arg_hasattr(MDOC_Emphasis, node))
1.28      kristaps 2122:                        pair->flag |= ttypes[TTYPE_EMPH];
1.1       kristaps 2123:                else if (arg_hasattr(MDOC_Symbolic, node))
1.28      kristaps 2124:                        pair->flag |= ttypes[TTYPE_SYMB];
1.1       kristaps 2125:
                   2126:                return(1);
                   2127:        }
                   2128:
1.11      kristaps 2129:        assert(MDOC_TEXT == n->type);
1.1       kristaps 2130:        if (0 == strcmp("Em", n->string))
1.28      kristaps 2131:                pair->flag |= ttypes[TTYPE_EMPH];
1.1       kristaps 2132:        else if (0 == strcmp("Sy", n->string))
1.28      kristaps 2133:                pair->flag |= ttypes[TTYPE_SYMB];
1.1       kristaps 2134:
                   2135:        return(1);
                   2136: }
                   2137:
                   2138:
                   2139: /* ARGSUSED */
                   2140: static int
                   2141: termp_sy_pre(DECL_ARGS)
                   2142: {
                   2143:
1.28      kristaps 2144:        pair->flag |= ttypes[TTYPE_SYMB];
1.1       kristaps 2145:        return(1);
                   2146: }
                   2147:
                   2148:
                   2149: /* ARGSUSED */
                   2150: static int
                   2151: termp_ms_pre(DECL_ARGS)
                   2152: {
                   2153:
1.28      kristaps 2154:        pair->flag |= ttypes[TTYPE_SYMBOL];
1.1       kristaps 2155:        return(1);
                   2156: }
                   2157:
                   2158:
                   2159:
                   2160: /* ARGSUSED */
                   2161: static int
                   2162: termp_sm_pre(DECL_ARGS)
                   2163: {
                   2164:
1.11      kristaps 2165:        assert(node->child && MDOC_TEXT == node->child->type);
1.1       kristaps 2166:        if (0 == strcmp("on", node->child->string)) {
                   2167:                p->flags &= ~TERMP_NONOSPACE;
                   2168:                p->flags &= ~TERMP_NOSPACE;
                   2169:        } else
                   2170:                p->flags |= TERMP_NONOSPACE;
                   2171:
                   2172:        return(0);
                   2173: }
                   2174:
                   2175:
                   2176: /* ARGSUSED */
                   2177: static int
                   2178: termp_ap_pre(DECL_ARGS)
                   2179: {
                   2180:
                   2181:        p->flags |= TERMP_NOSPACE;
                   2182:        term_word(p, "\\(aq");
                   2183:        p->flags |= TERMP_NOSPACE;
                   2184:        return(1);
                   2185: }
                   2186:
                   2187:
                   2188: /* ARGSUSED */
                   2189: static int
                   2190: termp__j_pre(DECL_ARGS)
                   2191: {
                   2192:
1.28      kristaps 2193:        pair->flag |= ttypes[TTYPE_REF_JOURNAL];
1.1       kristaps 2194:        return(1);
                   2195: }
                   2196:
                   2197:
                   2198: /* ARGSUSED */
                   2199: static int
                   2200: termp__t_pre(DECL_ARGS)
                   2201: {
                   2202:
1.56      kristaps 2203:        pair->flag |= ttypes[TTYPE_REF_TITLE];
1.1       kristaps 2204:        return(1);
                   2205: }
                   2206:
                   2207:
                   2208: /* ARGSUSED */
                   2209: static void
                   2210: termp____post(DECL_ARGS)
                   2211: {
                   2212:
                   2213:        p->flags |= TERMP_NOSPACE;
                   2214:        term_word(p, node->next ? "," : ".");
                   2215: }
                   2216:
                   2217:
                   2218: /* ARGSUSED */
                   2219: static int
                   2220: termp_lk_pre(DECL_ARGS)
                   2221: {
                   2222:        const struct mdoc_node *n;
                   2223:
1.11      kristaps 2224:        assert(node->child);
                   2225:        n = node->child;
1.1       kristaps 2226:
1.12      kristaps 2227:        if (NULL == n->next) {
1.28      kristaps 2228:                pair->flag |= ttypes[TTYPE_LINK_ANCHOR];
1.12      kristaps 2229:                return(1);
                   2230:        }
                   2231:
1.1       kristaps 2232:        p->flags |= ttypes[TTYPE_LINK_ANCHOR];
                   2233:        term_word(p, n->string);
                   2234:        p->flags |= TERMP_NOSPACE;
                   2235:        term_word(p, ":");
1.12      kristaps 2236:        p->flags &= ~ttypes[TTYPE_LINK_ANCHOR];
1.1       kristaps 2237:
                   2238:        p->flags |= ttypes[TTYPE_LINK_TEXT];
1.12      kristaps 2239:        for (n = n->next; n; n = n->next)
1.1       kristaps 2240:                term_word(p, n->string);
1.12      kristaps 2241:
1.1       kristaps 2242:        p->flags &= ~ttypes[TTYPE_LINK_TEXT];
                   2243:        return(0);
                   2244: }
                   2245:
                   2246:
                   2247: /* ARGSUSED */
                   2248: static int
                   2249: termp_mt_pre(DECL_ARGS)
                   2250: {
                   2251:
1.28      kristaps 2252:        pair->flag |= ttypes[TTYPE_LINK_ANCHOR];
1.1       kristaps 2253:        return(1);
                   2254: }
                   2255:
                   2256:

CVSweb