[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.101

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