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

Annotation of mandoc/mdoc_term.c, Revision 1.182

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

CVSweb