[BACK]Return to mdoc_term.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/mdoc_term.c, Revision 1.94

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