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

Annotation of mandoc/mdoc_term.c, Revision 1.185

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

CVSweb