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

1.102   ! schwarze    1: /*     $Id: mdoc_term.c,v 1.101 2010/08/18 01:45:22 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.102   ! schwarze  841:                p->flags |= TERMP_IGNDELIM;
        !           842:
1.1       kristaps  843:                break;
1.82      schwarze  844:        case (LIST_diag):
1.60      schwarze  845:                if (MDOC_HEAD == n->type)
1.1       kristaps  846:                        p->flags |= TERMP_NOBREAK;
                    847:                break;
                    848:        default:
                    849:                break;
                    850:        }
                    851:
                    852:        /*
                    853:         * Margin control.  Set-head-width lists have their right
                    854:         * margins shortened.  The body for these lists has the offset
                    855:         * necessarily lengthened.  Everybody gets the offset.
                    856:         */
                    857:
                    858:        p->offset += offset;
                    859:
                    860:        switch (type) {
1.82      schwarze  861:        case (LIST_hang):
1.47      schwarze  862:                /*
                    863:                 * Same stipulation as above, regarding `-hang'.  We
                    864:                 * don't want to recalculate rmargin and offsets when
                    865:                 * using `Bd' or `Bl' within `-hang' overstep lists.
                    866:                 */
1.60      schwarze  867:                if (MDOC_HEAD == n->type && n->next->child &&
                    868:                                (MDOC_Bl == n->next->child->tok ||
                    869:                                 MDOC_Bd == n->next->child->tok))
1.47      schwarze  870:                        break;
                    871:                /* FALLTHROUGH */
1.82      schwarze  872:        case (LIST_bullet):
1.1       kristaps  873:                /* FALLTHROUGH */
1.82      schwarze  874:        case (LIST_dash):
1.1       kristaps  875:                /* FALLTHROUGH */
1.82      schwarze  876:        case (LIST_enum):
1.1       kristaps  877:                /* FALLTHROUGH */
1.82      schwarze  878:        case (LIST_hyphen):
1.33      schwarze  879:                /* FALLTHROUGH */
1.82      schwarze  880:        case (LIST_tag):
1.41      schwarze  881:                assert(width);
1.60      schwarze  882:                if (MDOC_HEAD == n->type)
1.1       kristaps  883:                        p->rmargin = p->offset + width;
                    884:                else
                    885:                        p->offset += width;
                    886:                break;
1.82      schwarze  887:        case (LIST_column):
1.41      schwarze  888:                assert(width);
1.1       kristaps  889:                p->rmargin = p->offset + width;
1.43      schwarze  890:                /*
                    891:                 * XXX - this behaviour is not documented: the
                    892:                 * right-most column is filled to the right margin.
                    893:                 */
1.85      schwarze  894:                if (MDOC_HEAD == n->type)
                    895:                        break;
                    896:                if (NULL == n->next && p->rmargin < p->maxrmargin)
1.43      schwarze  897:                        p->rmargin = p->maxrmargin;
1.1       kristaps  898:                break;
                    899:        default:
                    900:                break;
                    901:        }
                    902:
                    903:        /*
                    904:         * The dash, hyphen, bullet and enum lists all have a special
1.12      schwarze  905:         * HEAD character (temporarily bold, in some cases).
1.1       kristaps  906:         */
                    907:
1.60      schwarze  908:        if (MDOC_HEAD == n->type)
1.1       kristaps  909:                switch (type) {
1.82      schwarze  910:                case (LIST_bullet):
1.65      schwarze  911:                        term_fontpush(p, TERMFONT_BOLD);
1.1       kristaps  912:                        term_word(p, "\\[bu]");
1.65      schwarze  913:                        term_fontpop(p);
1.1       kristaps  914:                        break;
1.82      schwarze  915:                case (LIST_dash):
1.1       kristaps  916:                        /* FALLTHROUGH */
1.82      schwarze  917:                case (LIST_hyphen):
1.65      schwarze  918:                        term_fontpush(p, TERMFONT_BOLD);
1.35      schwarze  919:                        term_word(p, "\\(hy");
1.65      schwarze  920:                        term_fontpop(p);
1.1       kristaps  921:                        break;
1.82      schwarze  922:                case (LIST_enum):
1.1       kristaps  923:                        (pair->ppair->ppair->count)++;
1.68      schwarze  924:                        snprintf(buf, sizeof(buf), "%d.",
1.1       kristaps  925:                                        pair->ppair->ppair->count);
                    926:                        term_word(p, buf);
                    927:                        break;
                    928:                default:
                    929:                        break;
                    930:                }
1.12      schwarze  931:
1.1       kristaps  932:        /*
                    933:         * If we're not going to process our children, indicate so here.
                    934:         */
                    935:
                    936:        switch (type) {
1.82      schwarze  937:        case (LIST_bullet):
1.1       kristaps  938:                /* FALLTHROUGH */
1.82      schwarze  939:        case (LIST_item):
1.1       kristaps  940:                /* FALLTHROUGH */
1.82      schwarze  941:        case (LIST_dash):
1.1       kristaps  942:                /* FALLTHROUGH */
1.82      schwarze  943:        case (LIST_hyphen):
1.1       kristaps  944:                /* FALLTHROUGH */
1.82      schwarze  945:        case (LIST_enum):
1.60      schwarze  946:                if (MDOC_HEAD == n->type)
1.1       kristaps  947:                        return(0);
                    948:                break;
1.82      schwarze  949:        case (LIST_column):
1.85      schwarze  950:                if (MDOC_HEAD == n->type)
1.1       kristaps  951:                        return(0);
                    952:                break;
                    953:        default:
                    954:                break;
                    955:        }
                    956:
                    957:        return(1);
                    958: }
                    959:
                    960:
                    961: /* ARGSUSED */
                    962: static void
                    963: termp_it_post(DECL_ARGS)
                    964: {
1.82      schwarze  965:        enum mdoc_list     type;
1.1       kristaps  966:
1.68      schwarze  967:        if (MDOC_BLOCK == n->type)
1.1       kristaps  968:                return;
                    969:
1.95      schwarze  970:        type = n->parent->parent->parent->data.Bl->type;
1.1       kristaps  971:
                    972:        switch (type) {
1.82      schwarze  973:        case (LIST_item):
1.44      schwarze  974:                /* FALLTHROUGH */
1.82      schwarze  975:        case (LIST_diag):
1.1       kristaps  976:                /* FALLTHROUGH */
1.82      schwarze  977:        case (LIST_inset):
1.60      schwarze  978:                if (MDOC_BODY == n->type)
1.74      schwarze  979:                        term_newln(p);
1.1       kristaps  980:                break;
1.82      schwarze  981:        case (LIST_column):
1.85      schwarze  982:                if (MDOC_BODY == n->type)
1.1       kristaps  983:                        term_flushln(p);
                    984:                break;
                    985:        default:
1.74      schwarze  986:                term_newln(p);
1.1       kristaps  987:                break;
                    988:        }
                    989:
1.68      schwarze  990:        /*
                    991:         * Now that our output is flushed, we can reset our tags.  Since
                    992:         * only `It' sets these flags, we're free to assume that nobody
                    993:         * has munged them in the meanwhile.
                    994:         */
                    995:
                    996:        p->flags &= ~TERMP_DANGLE;
                    997:        p->flags &= ~TERMP_NOBREAK;
                    998:        p->flags &= ~TERMP_TWOSPACE;
                    999:        p->flags &= ~TERMP_NOLPAD;
                   1000:        p->flags &= ~TERMP_HANG;
1.102   ! schwarze 1001:
        !          1002:        /*
        !          1003:         * TERMP_IGNDELIM is also set by `Pf', but it is safe
        !          1004:         * to clear it here because `Pf' cannot contain `It'.
        !          1005:         */
        !          1006:
        !          1007:        p->flags &= ~TERMP_IGNDELIM;
1.1       kristaps 1008: }
                   1009:
                   1010:
                   1011: /* ARGSUSED */
                   1012: static int
                   1013: termp_nm_pre(DECL_ARGS)
                   1014: {
                   1015:
1.94      schwarze 1016:        if (MDOC_BLOCK == n->type)
                   1017:                return(1);
                   1018:
                   1019:        if (MDOC_BODY == n->type) {
                   1020:                if (NULL == n->child)
                   1021:                        return(0);
                   1022:                p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
                   1023:                p->offset += term_len(p, 1) +
                   1024:                    (NULL == n->prev->child ? term_strlen(p, m->name) :
                   1025:                     MDOC_TEXT == n->prev->child->type ?
                   1026:                        term_strlen(p, n->prev->child->string) :
                   1027:                     term_len(p, 5));
                   1028:                return(1);
                   1029:        }
                   1030:
1.81      schwarze 1031:        if (NULL == n->child && NULL == m->name)
1.94      schwarze 1032:                return(0);
1.81      schwarze 1033:
1.97      schwarze 1034:        if (MDOC_HEAD == n->type)
                   1035:                synopsis_pre(p, n->parent);
1.65      schwarze 1036:
1.94      schwarze 1037:        if (MDOC_HEAD == n->type && n->next->child) {
                   1038:                p->flags |= TERMP_NOSPACE | TERMP_NOBREAK | TERMP_HANG;
                   1039:                p->rmargin = p->offset + term_len(p, 1) +
                   1040:                    (NULL == n->child ? term_strlen(p, m->name) :
                   1041:                     MDOC_TEXT == n->child->type ?
                   1042:                        term_strlen(p, n->child->string) :
                   1043:                     term_len(p, 5));
                   1044:        }
                   1045:
1.65      schwarze 1046:        term_fontpush(p, TERMFONT_BOLD);
1.60      schwarze 1047:        if (NULL == n->child)
                   1048:                term_word(p, m->name);
1.1       kristaps 1049:        return(1);
                   1050: }
                   1051:
                   1052:
1.94      schwarze 1053: /* ARGSUSED */
                   1054: static void
                   1055: termp_nm_post(DECL_ARGS)
                   1056: {
                   1057:
                   1058:        if (MDOC_HEAD == n->type && n->next->child) {
                   1059:                term_flushln(p);
                   1060:                p->flags &= ~(TERMP_NOBREAK | TERMP_HANG);
                   1061:        } else if (MDOC_BODY == n->type && n->child) {
                   1062:                term_flushln(p);
                   1063:                p->flags &= ~TERMP_NOLPAD;
                   1064:        }
                   1065: }
                   1066:
                   1067:
1.1       kristaps 1068: /* ARGSUSED */
                   1069: static int
                   1070: termp_fl_pre(DECL_ARGS)
                   1071: {
                   1072:
1.65      schwarze 1073:        term_fontpush(p, TERMFONT_BOLD);
1.1       kristaps 1074:        term_word(p, "\\-");
1.67      schwarze 1075:
                   1076:        if (n->child)
                   1077:                p->flags |= TERMP_NOSPACE;
1.73      schwarze 1078:        else if (n->next && n->next->line == n->line)
                   1079:                p->flags |= TERMP_NOSPACE;
1.67      schwarze 1080:
1.1       kristaps 1081:        return(1);
1.49      schwarze 1082: }
                   1083:
                   1084:
                   1085: /* ARGSUSED */
                   1086: static int
                   1087: termp_an_pre(DECL_ARGS)
                   1088: {
                   1089:
1.60      schwarze 1090:        if (NULL == n->child)
1.49      schwarze 1091:                return(1);
                   1092:
                   1093:        /*
1.60      schwarze 1094:         * If not in the AUTHORS section, `An -split' will cause
                   1095:         * newlines to occur before the author name.  If in the AUTHORS
                   1096:         * section, by default, the first `An' invocation is nosplit,
                   1097:         * then all subsequent ones, regardless of whether interspersed
                   1098:         * with other macros/text, are split.  -split, in this case,
                   1099:         * will override the condition of the implied first -nosplit.
1.49      schwarze 1100:         */
                   1101:
1.60      schwarze 1102:        if (n->sec == SEC_AUTHORS) {
1.49      schwarze 1103:                if ( ! (TERMP_ANPREC & p->flags)) {
                   1104:                        if (TERMP_SPLIT & p->flags)
                   1105:                                term_newln(p);
                   1106:                        return(1);
                   1107:                }
                   1108:                if (TERMP_NOSPLIT & p->flags)
                   1109:                        return(1);
                   1110:                term_newln(p);
                   1111:                return(1);
                   1112:        }
                   1113:
                   1114:        if (TERMP_SPLIT & p->flags)
                   1115:                term_newln(p);
                   1116:
                   1117:        return(1);
                   1118: }
                   1119:
                   1120:
                   1121: /* ARGSUSED */
                   1122: static void
                   1123: termp_an_post(DECL_ARGS)
                   1124: {
                   1125:
1.60      schwarze 1126:        if (n->child) {
                   1127:                if (SEC_AUTHORS == n->sec)
1.49      schwarze 1128:                        p->flags |= TERMP_ANPREC;
                   1129:                return;
                   1130:        }
                   1131:
1.95      schwarze 1132:        if (AUTH_split == n->data.An.auth) {
1.49      schwarze 1133:                p->flags &= ~TERMP_NOSPLIT;
                   1134:                p->flags |= TERMP_SPLIT;
1.95      schwarze 1135:        } else if (AUTH_nosplit == n->data.An.auth) {
1.49      schwarze 1136:                p->flags &= ~TERMP_SPLIT;
                   1137:                p->flags |= TERMP_NOSPLIT;
                   1138:        }
                   1139:
1.1       kristaps 1140: }
                   1141:
                   1142:
                   1143: /* ARGSUSED */
                   1144: static int
                   1145: termp_ns_pre(DECL_ARGS)
                   1146: {
                   1147:
                   1148:        p->flags |= TERMP_NOSPACE;
                   1149:        return(1);
                   1150: }
                   1151:
                   1152:
                   1153: /* ARGSUSED */
                   1154: static int
                   1155: termp_rs_pre(DECL_ARGS)
                   1156: {
                   1157:
1.60      schwarze 1158:        if (SEC_SEE_ALSO != n->sec)
1.57      schwarze 1159:                return(1);
1.60      schwarze 1160:        if (MDOC_BLOCK == n->type && n->prev)
1.1       kristaps 1161:                term_vspace(p);
                   1162:        return(1);
                   1163: }
                   1164:
                   1165:
                   1166: /* ARGSUSED */
                   1167: static int
                   1168: termp_rv_pre(DECL_ARGS)
                   1169: {
1.53      schwarze 1170:        const struct mdoc_node  *nn;
1.1       kristaps 1171:
                   1172:        term_newln(p);
                   1173:        term_word(p, "The");
                   1174:
1.60      schwarze 1175:        for (nn = n->child; nn; nn = nn->next) {
1.65      schwarze 1176:                term_fontpush(p, TERMFONT_BOLD);
1.53      schwarze 1177:                term_word(p, nn->string);
1.65      schwarze 1178:                term_fontpop(p);
1.53      schwarze 1179:                p->flags |= TERMP_NOSPACE;
                   1180:                if (nn->next && NULL == nn->next->next)
                   1181:                        term_word(p, "(), and");
                   1182:                else if (nn->next)
                   1183:                        term_word(p, "(),");
                   1184:                else
                   1185:                        term_word(p, "()");
                   1186:        }
                   1187:
1.81      schwarze 1188:        if (n->child && n->child->next)
1.53      schwarze 1189:                term_word(p, "functions return");
                   1190:        else
                   1191:                term_word(p, "function returns");
1.1       kristaps 1192:
1.53      schwarze 1193:                term_word(p, "the value 0 if successful; otherwise the value "
                   1194:                        "-1 is returned and the global variable");
1.1       kristaps 1195:
1.65      schwarze 1196:        term_fontpush(p, TERMFONT_UNDER);
1.1       kristaps 1197:        term_word(p, "errno");
1.65      schwarze 1198:        term_fontpop(p);
1.1       kristaps 1199:
                   1200:                term_word(p, "is set to indicate the error.");
1.84      schwarze 1201:        p->flags |= TERMP_SENTENCE;
1.1       kristaps 1202:
1.53      schwarze 1203:        return(0);
1.1       kristaps 1204: }
                   1205:
                   1206:
                   1207: /* ARGSUSED */
                   1208: static int
                   1209: termp_ex_pre(DECL_ARGS)
                   1210: {
1.53      schwarze 1211:        const struct mdoc_node  *nn;
1.1       kristaps 1212:
1.53      schwarze 1213:        term_word(p, "The");
1.1       kristaps 1214:
1.60      schwarze 1215:        for (nn = n->child; nn; nn = nn->next) {
1.65      schwarze 1216:                term_fontpush(p, TERMFONT_BOLD);
1.53      schwarze 1217:                term_word(p, nn->string);
1.65      schwarze 1218:                term_fontpop(p);
1.53      schwarze 1219:                p->flags |= TERMP_NOSPACE;
                   1220:                if (nn->next && NULL == nn->next->next)
                   1221:                        term_word(p, ", and");
                   1222:                else if (nn->next)
                   1223:                        term_word(p, ",");
                   1224:                else
                   1225:                        p->flags &= ~TERMP_NOSPACE;
                   1226:        }
                   1227:
1.81      schwarze 1228:        if (n->child && n->child->next)
1.53      schwarze 1229:                term_word(p, "utilities exit");
                   1230:        else
                   1231:                term_word(p, "utility exits");
                   1232:
                   1233:                term_word(p, "0 on success, and >0 if an error occurs.");
1.84      schwarze 1234:        p->flags |= TERMP_SENTENCE;
1.1       kristaps 1235:
1.53      schwarze 1236:        return(0);
1.1       kristaps 1237: }
                   1238:
                   1239:
                   1240: /* ARGSUSED */
                   1241: static int
                   1242: termp_nd_pre(DECL_ARGS)
                   1243: {
1.25      schwarze 1244:
1.60      schwarze 1245:        if (MDOC_BODY != n->type)
1.25      schwarze 1246:                return(1);
                   1247:
                   1248: #if defined(__OpenBSD__) || defined(__linux__)
                   1249:        term_word(p, "\\(en");
1.24      schwarze 1250: #else
                   1251:        term_word(p, "\\(em");
                   1252: #endif
1.1       kristaps 1253:        return(1);
                   1254: }
                   1255:
                   1256:
                   1257: /* ARGSUSED */
1.73      schwarze 1258: static int
                   1259: termp_bl_pre(DECL_ARGS)
                   1260: {
                   1261:
                   1262:        return(MDOC_HEAD != n->type);
                   1263: }
                   1264:
                   1265:
                   1266: /* ARGSUSED */
1.1       kristaps 1267: static void
                   1268: termp_bl_post(DECL_ARGS)
                   1269: {
                   1270:
1.60      schwarze 1271:        if (MDOC_BLOCK == n->type)
1.1       kristaps 1272:                term_newln(p);
                   1273: }
                   1274:
                   1275:
                   1276: /* ARGSUSED */
                   1277: static void
                   1278: termp_op_post(DECL_ARGS)
                   1279: {
                   1280:
1.60      schwarze 1281:        if (MDOC_BODY != n->type)
1.1       kristaps 1282:                return;
                   1283:        p->flags |= TERMP_NOSPACE;
                   1284:        term_word(p, "\\(rB");
                   1285: }
                   1286:
                   1287:
                   1288: /* ARGSUSED */
                   1289: static int
                   1290: termp_xr_pre(DECL_ARGS)
                   1291: {
1.60      schwarze 1292:        const struct mdoc_node *nn;
1.1       kristaps 1293:
1.72      schwarze 1294:        if (NULL == n->child)
                   1295:                return(0);
                   1296:
                   1297:        assert(MDOC_TEXT == n->child->type);
1.60      schwarze 1298:        nn = n->child;
1.10      schwarze 1299:
1.60      schwarze 1300:        term_word(p, nn->string);
                   1301:        if (NULL == (nn = nn->next))
1.1       kristaps 1302:                return(0);
                   1303:        p->flags |= TERMP_NOSPACE;
                   1304:        term_word(p, "(");
                   1305:        p->flags |= TERMP_NOSPACE;
1.60      schwarze 1306:        term_word(p, nn->string);
1.1       kristaps 1307:        p->flags |= TERMP_NOSPACE;
                   1308:        term_word(p, ")");
1.60      schwarze 1309:
1.1       kristaps 1310:        return(0);
                   1311: }
                   1312:
                   1313:
1.86      schwarze 1314: /*
                   1315:  * This decides how to assert whitespace before any of the SYNOPSIS set
                   1316:  * of macros (which, as in the case of Ft/Fo and Ft/Fn, may contain
                   1317:  * macro combos).
                   1318:  */
                   1319: static void
                   1320: synopsis_pre(struct termp *p, const struct mdoc_node *n)
                   1321: {
                   1322:        /*
                   1323:         * Obviously, if we're not in a SYNOPSIS or no prior macros
                   1324:         * exist, do nothing.
                   1325:         */
1.92      schwarze 1326:        if (NULL == n->prev || ! (MDOC_SYNPRETTY & n->flags))
1.86      schwarze 1327:                return;
                   1328:
                   1329:        /*
                   1330:         * If we're the second in a pair of like elements, emit our
                   1331:         * newline and return.  UNLESS we're `Fo', `Fn', `Fn', in which
                   1332:         * case we soldier on.
                   1333:         */
                   1334:        if (n->prev->tok == n->tok &&
                   1335:                        MDOC_Ft != n->tok &&
                   1336:                        MDOC_Fo != n->tok &&
                   1337:                        MDOC_Fn != n->tok) {
                   1338:                term_newln(p);
                   1339:                return;
                   1340:        }
                   1341:
                   1342:        /*
                   1343:         * If we're one of the SYNOPSIS set and non-like pair-wise after
                   1344:         * another (or Fn/Fo, which we've let slip through) then assert
                   1345:         * vertical space, else only newline and move on.
                   1346:         */
                   1347:        switch (n->prev->tok) {
                   1348:        case (MDOC_Fd):
                   1349:                /* FALLTHROUGH */
                   1350:        case (MDOC_Fn):
                   1351:                /* FALLTHROUGH */
                   1352:        case (MDOC_Fo):
                   1353:                /* FALLTHROUGH */
                   1354:        case (MDOC_In):
                   1355:                /* FALLTHROUGH */
                   1356:        case (MDOC_Vt):
                   1357:                term_vspace(p);
                   1358:                break;
                   1359:        case (MDOC_Ft):
                   1360:                if (MDOC_Fn != n->tok && MDOC_Fo != n->tok) {
                   1361:                        term_vspace(p);
                   1362:                        break;
                   1363:                }
                   1364:                /* FALLTHROUGH */
                   1365:        default:
                   1366:                term_newln(p);
                   1367:                break;
                   1368:        }
                   1369: }
                   1370:
                   1371:
1.69      schwarze 1372: static int
                   1373: termp_vt_pre(DECL_ARGS)
                   1374: {
                   1375:
1.86      schwarze 1376:        if (MDOC_ELEM == n->type) {
                   1377:                synopsis_pre(p, n);
1.69      schwarze 1378:                return(termp_under_pre(p, pair, m, n));
1.86      schwarze 1379:        } else if (MDOC_BLOCK == n->type) {
                   1380:                synopsis_pre(p, n);
                   1381:                return(1);
                   1382:        } else if (MDOC_HEAD == n->type)
1.69      schwarze 1383:                return(0);
                   1384:
                   1385:        return(termp_under_pre(p, pair, m, n));
                   1386: }
                   1387:
                   1388:
1.1       kristaps 1389: /* ARGSUSED */
                   1390: static int
1.54      schwarze 1391: termp_bold_pre(DECL_ARGS)
1.1       kristaps 1392: {
                   1393:
1.65      schwarze 1394:        term_fontpush(p, TERMFONT_BOLD);
1.1       kristaps 1395:        return(1);
                   1396: }
                   1397:
                   1398:
                   1399: /* ARGSUSED */
1.86      schwarze 1400: static int
                   1401: termp_fd_pre(DECL_ARGS)
1.1       kristaps 1402: {
                   1403:
1.86      schwarze 1404:        synopsis_pre(p, n);
                   1405:        return(termp_bold_pre(p, pair, m, n));
1.1       kristaps 1406: }
                   1407:
                   1408:
                   1409: /* ARGSUSED */
                   1410: static int
                   1411: termp_sh_pre(DECL_ARGS)
                   1412: {
1.60      schwarze 1413:
                   1414:        /* No vspace between consecutive `Sh' calls. */
                   1415:
                   1416:        switch (n->type) {
1.52      schwarze 1417:        case (MDOC_BLOCK):
1.60      schwarze 1418:                if (n->prev && MDOC_Sh == n->prev->tok)
                   1419:                        if (NULL == n->prev->body->child)
1.52      schwarze 1420:                                break;
                   1421:                term_vspace(p);
                   1422:                break;
1.1       kristaps 1423:        case (MDOC_HEAD):
1.65      schwarze 1424:                term_fontpush(p, TERMFONT_BOLD);
1.1       kristaps 1425:                break;
                   1426:        case (MDOC_BODY):
1.89      schwarze 1427:                p->offset = term_len(p, INDENT);
1.1       kristaps 1428:                break;
                   1429:        default:
                   1430:                break;
                   1431:        }
                   1432:        return(1);
                   1433: }
                   1434:
                   1435:
                   1436: /* ARGSUSED */
                   1437: static void
                   1438: termp_sh_post(DECL_ARGS)
                   1439: {
                   1440:
1.60      schwarze 1441:        switch (n->type) {
1.1       kristaps 1442:        case (MDOC_HEAD):
                   1443:                term_newln(p);
                   1444:                break;
                   1445:        case (MDOC_BODY):
                   1446:                term_newln(p);
                   1447:                p->offset = 0;
                   1448:                break;
                   1449:        default:
                   1450:                break;
                   1451:        }
                   1452: }
                   1453:
                   1454:
                   1455: /* ARGSUSED */
                   1456: static int
                   1457: termp_op_pre(DECL_ARGS)
                   1458: {
                   1459:
1.60      schwarze 1460:        switch (n->type) {
1.1       kristaps 1461:        case (MDOC_BODY):
                   1462:                term_word(p, "\\(lB");
                   1463:                p->flags |= TERMP_NOSPACE;
                   1464:                break;
                   1465:        default:
                   1466:                break;
                   1467:        }
                   1468:        return(1);
                   1469: }
                   1470:
                   1471:
                   1472: /* ARGSUSED */
                   1473: static int
                   1474: termp_bt_pre(DECL_ARGS)
                   1475: {
                   1476:
                   1477:        term_word(p, "is currently in beta test.");
1.84      schwarze 1478:        p->flags |= TERMP_SENTENCE;
1.59      schwarze 1479:        return(0);
1.1       kristaps 1480: }
                   1481:
                   1482:
                   1483: /* ARGSUSED */
                   1484: static void
                   1485: termp_lb_post(DECL_ARGS)
                   1486: {
                   1487:
1.77      schwarze 1488:        if (SEC_LIBRARY == n->sec && MDOC_LINE & n->flags)
1.57      schwarze 1489:                term_newln(p);
1.1       kristaps 1490: }
                   1491:
                   1492:
                   1493: /* ARGSUSED */
                   1494: static int
                   1495: termp_ud_pre(DECL_ARGS)
                   1496: {
                   1497:
                   1498:        term_word(p, "currently under development.");
1.84      schwarze 1499:        p->flags |= TERMP_SENTENCE;
1.60      schwarze 1500:        return(0);
1.1       kristaps 1501: }
                   1502:
                   1503:
                   1504: /* ARGSUSED */
                   1505: static int
                   1506: termp_d1_pre(DECL_ARGS)
                   1507: {
                   1508:
1.60      schwarze 1509:        if (MDOC_BLOCK != n->type)
1.1       kristaps 1510:                return(1);
                   1511:        term_newln(p);
1.89      schwarze 1512:        p->offset += term_len(p, (INDENT + 1));
1.1       kristaps 1513:        return(1);
                   1514: }
                   1515:
                   1516:
                   1517: /* ARGSUSED */
                   1518: static void
                   1519: termp_d1_post(DECL_ARGS)
                   1520: {
                   1521:
1.60      schwarze 1522:        if (MDOC_BLOCK != n->type)
1.1       kristaps 1523:                return;
                   1524:        term_newln(p);
                   1525: }
                   1526:
                   1527:
                   1528: /* ARGSUSED */
                   1529: static int
                   1530: termp_aq_pre(DECL_ARGS)
                   1531: {
                   1532:
1.60      schwarze 1533:        if (MDOC_BODY != n->type)
1.1       kristaps 1534:                return(1);
                   1535:        term_word(p, "\\(la");
                   1536:        p->flags |= TERMP_NOSPACE;
                   1537:        return(1);
                   1538: }
                   1539:
                   1540:
                   1541: /* ARGSUSED */
                   1542: static void
                   1543: termp_aq_post(DECL_ARGS)
                   1544: {
                   1545:
1.60      schwarze 1546:        if (MDOC_BODY != n->type)
1.1       kristaps 1547:                return;
                   1548:        p->flags |= TERMP_NOSPACE;
                   1549:        term_word(p, "\\(ra");
                   1550: }
                   1551:
                   1552:
                   1553: /* ARGSUSED */
                   1554: static int
                   1555: termp_ft_pre(DECL_ARGS)
                   1556: {
                   1557:
1.85      schwarze 1558:        /* NB: MDOC_LINE does not effect this! */
1.86      schwarze 1559:        synopsis_pre(p, n);
1.65      schwarze 1560:        term_fontpush(p, TERMFONT_UNDER);
1.1       kristaps 1561:        return(1);
                   1562: }
                   1563:
                   1564:
                   1565: /* ARGSUSED */
                   1566: static int
                   1567: termp_fn_pre(DECL_ARGS)
                   1568: {
1.60      schwarze 1569:        const struct mdoc_node  *nn;
1.1       kristaps 1570:
1.86      schwarze 1571:        synopsis_pre(p, n);
1.85      schwarze 1572:
1.65      schwarze 1573:        term_fontpush(p, TERMFONT_BOLD);
1.60      schwarze 1574:        term_word(p, n->child->string);
1.65      schwarze 1575:        term_fontpop(p);
1.1       kristaps 1576:
                   1577:        p->flags |= TERMP_NOSPACE;
                   1578:        term_word(p, "(");
                   1579:
1.60      schwarze 1580:        for (nn = n->child->next; nn; nn = nn->next) {
1.65      schwarze 1581:                term_fontpush(p, TERMFONT_UNDER);
1.60      schwarze 1582:                term_word(p, nn->string);
1.65      schwarze 1583:                term_fontpop(p);
                   1584:
1.60      schwarze 1585:                if (nn->next)
1.1       kristaps 1586:                        term_word(p, ",");
                   1587:        }
                   1588:
                   1589:        term_word(p, ")");
                   1590:
1.92      schwarze 1591:        if (MDOC_SYNPRETTY & n->flags)
1.1       kristaps 1592:                term_word(p, ";");
                   1593:
                   1594:        return(0);
                   1595: }
                   1596:
                   1597:
                   1598: /* ARGSUSED */
                   1599: static int
                   1600: termp_fa_pre(DECL_ARGS)
                   1601: {
1.60      schwarze 1602:        const struct mdoc_node  *nn;
1.1       kristaps 1603:
1.60      schwarze 1604:        if (n->parent->tok != MDOC_Fo) {
1.65      schwarze 1605:                term_fontpush(p, TERMFONT_UNDER);
1.1       kristaps 1606:                return(1);
                   1607:        }
                   1608:
1.60      schwarze 1609:        for (nn = n->child; nn; nn = nn->next) {
1.65      schwarze 1610:                term_fontpush(p, TERMFONT_UNDER);
1.60      schwarze 1611:                term_word(p, nn->string);
1.65      schwarze 1612:                term_fontpop(p);
                   1613:
1.60      schwarze 1614:                if (nn->next)
1.1       kristaps 1615:                        term_word(p, ",");
                   1616:        }
                   1617:
1.60      schwarze 1618:        if (n->child && n->next && n->next->tok == MDOC_Fa)
1.1       kristaps 1619:                term_word(p, ",");
                   1620:
                   1621:        return(0);
                   1622: }
                   1623:
                   1624:
                   1625: /* ARGSUSED */
                   1626: static int
                   1627: termp_bd_pre(DECL_ARGS)
                   1628: {
1.99      schwarze 1629:        size_t                   tabwidth, rm, rmax;
1.60      schwarze 1630:        const struct mdoc_node  *nn;
1.1       kristaps 1631:
1.60      schwarze 1632:        if (MDOC_BLOCK == n->type) {
1.61      schwarze 1633:                print_bvspace(p, n, n);
1.45      schwarze 1634:                return(1);
1.73      schwarze 1635:        } else if (MDOC_HEAD == n->type)
                   1636:                return(0);
1.1       kristaps 1637:
1.95      schwarze 1638:        assert(n->data.Bd);
                   1639:        if (n->data.Bd->offs)
                   1640:                p->offset += a2offs(p, n->data.Bd->offs);
1.60      schwarze 1641:
                   1642:        /*
                   1643:         * If -ragged or -filled are specified, the block does nothing
                   1644:         * but change the indentation.  If -unfilled or -literal are
                   1645:         * specified, text is printed exactly as entered in the display:
                   1646:         * for macro lines, a newline is appended to the line.  Blank
                   1647:         * lines are allowed.
                   1648:         */
1.48      schwarze 1649:
1.95      schwarze 1650:        if (DISP_literal != n->data.Bd->type &&
                   1651:                        DISP_unfilled != n->data.Bd->type)
1.1       kristaps 1652:                return(1);
                   1653:
1.75      schwarze 1654:        tabwidth = p->tabwidth;
1.89      schwarze 1655:        p->tabwidth = term_len(p, 8);
1.77      schwarze 1656:        rm = p->rmargin;
                   1657:        rmax = p->maxrmargin;
                   1658:        p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
                   1659:
1.60      schwarze 1660:        for (nn = n->child; nn; nn = nn->next) {
1.100     schwarze 1661:                print_mdoc_node(p, pair, m, nn);
                   1662:                if (nn->next && nn->next->line == nn->line)
                   1663:                        continue;
                   1664:                term_flushln(p);
1.48      schwarze 1665:                p->flags |= TERMP_NOSPACE;
1.1       kristaps 1666:        }
1.88      schwarze 1667:
1.75      schwarze 1668:        p->tabwidth = tabwidth;
1.77      schwarze 1669:        p->rmargin = rm;
                   1670:        p->maxrmargin = rmax;
1.1       kristaps 1671:        return(0);
                   1672: }
                   1673:
                   1674:
                   1675: /* ARGSUSED */
                   1676: static void
                   1677: termp_bd_post(DECL_ARGS)
                   1678: {
1.77      schwarze 1679:        size_t           rm, rmax;
1.1       kristaps 1680:
1.60      schwarze 1681:        if (MDOC_BODY != n->type)
1.1       kristaps 1682:                return;
1.77      schwarze 1683:
                   1684:        rm = p->rmargin;
                   1685:        rmax = p->maxrmargin;
                   1686:
1.95      schwarze 1687:        assert(n->data.Bd);
                   1688:        if (DISP_literal == n->data.Bd->type ||
                   1689:                        DISP_unfilled == n->data.Bd->type)
1.77      schwarze 1690:                p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
                   1691:
1.48      schwarze 1692:        p->flags |= TERMP_NOSPACE;
1.75      schwarze 1693:        term_newln(p);
1.77      schwarze 1694:
                   1695:        p->rmargin = rm;
                   1696:        p->maxrmargin = rmax;
1.1       kristaps 1697: }
                   1698:
                   1699:
                   1700: /* ARGSUSED */
                   1701: static int
                   1702: termp_qq_pre(DECL_ARGS)
                   1703: {
                   1704:
1.60      schwarze 1705:        if (MDOC_BODY != n->type)
1.1       kristaps 1706:                return(1);
                   1707:        term_word(p, "\"");
                   1708:        p->flags |= TERMP_NOSPACE;
                   1709:        return(1);
                   1710: }
                   1711:
                   1712:
                   1713: /* ARGSUSED */
                   1714: static void
                   1715: termp_qq_post(DECL_ARGS)
                   1716: {
                   1717:
1.60      schwarze 1718:        if (MDOC_BODY != n->type)
1.1       kristaps 1719:                return;
                   1720:        p->flags |= TERMP_NOSPACE;
                   1721:        term_word(p, "\"");
                   1722: }
                   1723:
                   1724:
                   1725: /* ARGSUSED */
                   1726: static void
                   1727: termp_bx_post(DECL_ARGS)
                   1728: {
                   1729:
1.60      schwarze 1730:        if (n->child)
1.1       kristaps 1731:                p->flags |= TERMP_NOSPACE;
                   1732:        term_word(p, "BSD");
                   1733: }
                   1734:
                   1735:
                   1736: /* ARGSUSED */
                   1737: static int
1.26      schwarze 1738: termp_xx_pre(DECL_ARGS)
1.1       kristaps 1739: {
1.26      schwarze 1740:        const char      *pp;
1.1       kristaps 1741:
1.26      schwarze 1742:        pp = NULL;
1.60      schwarze 1743:        switch (n->tok) {
1.26      schwarze 1744:        case (MDOC_Bsx):
                   1745:                pp = "BSDI BSD/OS";
                   1746:                break;
                   1747:        case (MDOC_Dx):
1.64      schwarze 1748:                pp = "DragonFly";
1.26      schwarze 1749:                break;
                   1750:        case (MDOC_Fx):
                   1751:                pp = "FreeBSD";
                   1752:                break;
                   1753:        case (MDOC_Nx):
                   1754:                pp = "NetBSD";
                   1755:                break;
                   1756:        case (MDOC_Ox):
                   1757:                pp = "OpenBSD";
                   1758:                break;
                   1759:        case (MDOC_Ux):
                   1760:                pp = "UNIX";
                   1761:                break;
                   1762:        default:
                   1763:                break;
                   1764:        }
1.1       kristaps 1765:
1.26      schwarze 1766:        assert(pp);
                   1767:        term_word(p, pp);
1.1       kristaps 1768:        return(1);
                   1769: }
                   1770:
                   1771:
                   1772: /* ARGSUSED */
                   1773: static int
                   1774: termp_sq_pre(DECL_ARGS)
                   1775: {
                   1776:
1.60      schwarze 1777:        if (MDOC_BODY != n->type)
1.1       kristaps 1778:                return(1);
                   1779:        term_word(p, "\\(oq");
                   1780:        p->flags |= TERMP_NOSPACE;
                   1781:        return(1);
                   1782: }
                   1783:
                   1784:
                   1785: /* ARGSUSED */
                   1786: static void
                   1787: termp_sq_post(DECL_ARGS)
                   1788: {
                   1789:
1.60      schwarze 1790:        if (MDOC_BODY != n->type)
1.1       kristaps 1791:                return;
                   1792:        p->flags |= TERMP_NOSPACE;
                   1793:        term_word(p, "\\(aq");
                   1794: }
                   1795:
                   1796:
                   1797: /* ARGSUSED */
                   1798: static int
                   1799: termp_pf_pre(DECL_ARGS)
                   1800: {
                   1801:
                   1802:        p->flags |= TERMP_IGNDELIM;
                   1803:        return(1);
                   1804: }
                   1805:
                   1806:
                   1807: /* ARGSUSED */
                   1808: static void
                   1809: termp_pf_post(DECL_ARGS)
                   1810: {
                   1811:
1.102   ! schwarze 1812:        /*
        !          1813:         * XXX Resetting TERMP_IGNDELIM here is not safe
        !          1814:         * because `Pf' can be used inside `Bl -column'.
        !          1815:         */
1.1       kristaps 1816:        p->flags &= ~TERMP_IGNDELIM;
                   1817:        p->flags |= TERMP_NOSPACE;
                   1818: }
                   1819:
                   1820:
                   1821: /* ARGSUSED */
                   1822: static int
                   1823: termp_ss_pre(DECL_ARGS)
                   1824: {
                   1825:
1.60      schwarze 1826:        switch (n->type) {
1.1       kristaps 1827:        case (MDOC_BLOCK):
                   1828:                term_newln(p);
1.60      schwarze 1829:                if (n->prev)
1.1       kristaps 1830:                        term_vspace(p);
                   1831:                break;
                   1832:        case (MDOC_HEAD):
1.65      schwarze 1833:                term_fontpush(p, TERMFONT_BOLD);
1.89      schwarze 1834:                p->offset = term_len(p, HALFINDENT);
1.1       kristaps 1835:                break;
                   1836:        default:
                   1837:                break;
                   1838:        }
                   1839:
                   1840:        return(1);
                   1841: }
                   1842:
                   1843:
                   1844: /* ARGSUSED */
                   1845: static void
                   1846: termp_ss_post(DECL_ARGS)
                   1847: {
                   1848:
1.60      schwarze 1849:        if (MDOC_HEAD == n->type)
1.1       kristaps 1850:                term_newln(p);
                   1851: }
                   1852:
                   1853:
                   1854: /* ARGSUSED */
                   1855: static int
                   1856: termp_cd_pre(DECL_ARGS)
                   1857: {
                   1858:
1.86      schwarze 1859:        synopsis_pre(p, n);
1.65      schwarze 1860:        term_fontpush(p, TERMFONT_BOLD);
1.1       kristaps 1861:        return(1);
                   1862: }
                   1863:
                   1864:
                   1865: /* ARGSUSED */
                   1866: static int
                   1867: termp_in_pre(DECL_ARGS)
                   1868: {
                   1869:
1.86      schwarze 1870:        synopsis_pre(p, n);
1.85      schwarze 1871:
1.92      schwarze 1872:        if (MDOC_SYNPRETTY & n->flags && MDOC_LINE & n->flags) {
1.85      schwarze 1873:                term_fontpush(p, TERMFONT_BOLD);
1.23      schwarze 1874:                term_word(p, "#include");
1.85      schwarze 1875:                term_word(p, "<");
                   1876:        } else {
                   1877:                term_word(p, "<");
                   1878:                term_fontpush(p, TERMFONT_UNDER);
                   1879:        }
1.23      schwarze 1880:
1.1       kristaps 1881:        p->flags |= TERMP_NOSPACE;
                   1882:        return(1);
                   1883: }
                   1884:
                   1885:
                   1886: /* ARGSUSED */
                   1887: static void
                   1888: termp_in_post(DECL_ARGS)
                   1889: {
                   1890:
1.92      schwarze 1891:        if (MDOC_SYNPRETTY & n->flags)
1.85      schwarze 1892:                term_fontpush(p, TERMFONT_BOLD);
                   1893:
1.59      schwarze 1894:        p->flags |= TERMP_NOSPACE;
1.1       kristaps 1895:        term_word(p, ">");
                   1896:
1.92      schwarze 1897:        if (MDOC_SYNPRETTY & n->flags)
1.85      schwarze 1898:                term_fontpop(p);
1.38      schwarze 1899: }
                   1900:
                   1901:
                   1902: /* ARGSUSED */
                   1903: static int
                   1904: termp_sp_pre(DECL_ARGS)
                   1905: {
1.61      schwarze 1906:        size_t           i, len;
1.38      schwarze 1907:
1.60      schwarze 1908:        switch (n->tok) {
1.54      schwarze 1909:        case (MDOC_sp):
1.89      schwarze 1910:                len = n->child ? a2height(p, n->child->string) : 1;
1.54      schwarze 1911:                break;
                   1912:        case (MDOC_br):
                   1913:                len = 0;
                   1914:                break;
                   1915:        default:
1.98      schwarze 1916:                assert(n->parent);
                   1917:                if ((NULL == n->next || NULL == n->prev) &&
                   1918:                                (MDOC_Ss == n->parent->tok ||
                   1919:                                 MDOC_Sh == n->parent->tok))
                   1920:                        return(0);
1.54      schwarze 1921:                len = 1;
                   1922:                break;
1.38      schwarze 1923:        }
                   1924:
                   1925:        if (0 == len)
                   1926:                term_newln(p);
                   1927:        for (i = 0; i < len; i++)
                   1928:                term_vspace(p);
                   1929:
                   1930:        return(0);
                   1931: }
                   1932:
                   1933:
                   1934: /* ARGSUSED */
                   1935: static int
1.1       kristaps 1936: termp_brq_pre(DECL_ARGS)
                   1937: {
                   1938:
1.60      schwarze 1939:        if (MDOC_BODY != n->type)
1.1       kristaps 1940:                return(1);
                   1941:        term_word(p, "\\(lC");
                   1942:        p->flags |= TERMP_NOSPACE;
                   1943:        return(1);
                   1944: }
                   1945:
                   1946:
                   1947: /* ARGSUSED */
                   1948: static void
                   1949: termp_brq_post(DECL_ARGS)
                   1950: {
                   1951:
1.60      schwarze 1952:        if (MDOC_BODY != n->type)
1.1       kristaps 1953:                return;
                   1954:        p->flags |= TERMP_NOSPACE;
                   1955:        term_word(p, "\\(rC");
                   1956: }
                   1957:
                   1958:
                   1959: /* ARGSUSED */
                   1960: static int
                   1961: termp_bq_pre(DECL_ARGS)
                   1962: {
                   1963:
1.60      schwarze 1964:        if (MDOC_BODY != n->type)
1.1       kristaps 1965:                return(1);
                   1966:        term_word(p, "\\(lB");
                   1967:        p->flags |= TERMP_NOSPACE;
                   1968:        return(1);
                   1969: }
                   1970:
                   1971:
                   1972: /* ARGSUSED */
                   1973: static void
                   1974: termp_bq_post(DECL_ARGS)
                   1975: {
                   1976:
1.60      schwarze 1977:        if (MDOC_BODY != n->type)
1.1       kristaps 1978:                return;
                   1979:        p->flags |= TERMP_NOSPACE;
                   1980:        term_word(p, "\\(rB");
                   1981: }
                   1982:
                   1983:
                   1984: /* ARGSUSED */
                   1985: static int
                   1986: termp_pq_pre(DECL_ARGS)
                   1987: {
                   1988:
1.60      schwarze 1989:        if (MDOC_BODY != n->type)
1.1       kristaps 1990:                return(1);
                   1991:        term_word(p, "\\&(");
                   1992:        p->flags |= TERMP_NOSPACE;
                   1993:        return(1);
                   1994: }
                   1995:
                   1996:
                   1997: /* ARGSUSED */
                   1998: static void
                   1999: termp_pq_post(DECL_ARGS)
                   2000: {
                   2001:
1.60      schwarze 2002:        if (MDOC_BODY != n->type)
1.1       kristaps 2003:                return;
                   2004:        term_word(p, ")");
                   2005: }
                   2006:
                   2007:
                   2008: /* ARGSUSED */
                   2009: static int
                   2010: termp_fo_pre(DECL_ARGS)
                   2011: {
                   2012:
1.85      schwarze 2013:        if (MDOC_BLOCK == n->type) {
1.86      schwarze 2014:                synopsis_pre(p, n);
1.85      schwarze 2015:                return(1);
                   2016:        } else if (MDOC_BODY == n->type) {
1.31      schwarze 2017:                p->flags |= TERMP_NOSPACE;
1.1       kristaps 2018:                term_word(p, "(");
                   2019:                p->flags |= TERMP_NOSPACE;
                   2020:                return(1);
1.85      schwarze 2021:        }
                   2022:
1.95      schwarze 2023:        if (NULL == n->child)
                   2024:                return(0);
                   2025:
1.85      schwarze 2026:        /* XXX: we drop non-initial arguments as per groff. */
1.1       kristaps 2027:
1.85      schwarze 2028:        assert(n->child->string);
1.65      schwarze 2029:        term_fontpush(p, TERMFONT_BOLD);
1.85      schwarze 2030:        term_word(p, n->child->string);
1.1       kristaps 2031:        return(0);
                   2032: }
                   2033:
                   2034:
                   2035: /* ARGSUSED */
                   2036: static void
                   2037: termp_fo_post(DECL_ARGS)
                   2038: {
                   2039:
1.86      schwarze 2040:        if (MDOC_BODY != n->type)
                   2041:                return;
                   2042:
                   2043:        p->flags |= TERMP_NOSPACE;
                   2044:        term_word(p, ")");
                   2045:
1.92      schwarze 2046:        if (MDOC_SYNPRETTY & n->flags) {
1.85      schwarze 2047:                p->flags |= TERMP_NOSPACE;
1.86      schwarze 2048:                term_word(p, ";");
1.85      schwarze 2049:        }
1.1       kristaps 2050: }
                   2051:
                   2052:
                   2053: /* ARGSUSED */
                   2054: static int
                   2055: termp_bf_pre(DECL_ARGS)
                   2056: {
                   2057:
1.60      schwarze 2058:        if (MDOC_HEAD == n->type)
1.1       kristaps 2059:                return(0);
1.60      schwarze 2060:        else if (MDOC_BLOCK != n->type)
1.1       kristaps 2061:                return(1);
                   2062:
1.95      schwarze 2063:        assert(n->data.Bf);
1.1       kristaps 2064:
1.95      schwarze 2065:        if (FONT_Em == n->data.Bf->font)
1.65      schwarze 2066:                term_fontpush(p, TERMFONT_UNDER);
1.95      schwarze 2067:        else if (FONT_Sy == n->data.Bf->font)
1.65      schwarze 2068:                term_fontpush(p, TERMFONT_BOLD);
1.95      schwarze 2069:        else
1.65      schwarze 2070:                term_fontpush(p, TERMFONT_NONE);
1.1       kristaps 2071:
                   2072:        return(1);
                   2073: }
                   2074:
                   2075:
                   2076: /* ARGSUSED */
                   2077: static int
                   2078: termp_sm_pre(DECL_ARGS)
                   2079: {
                   2080:
1.60      schwarze 2081:        assert(n->child && MDOC_TEXT == n->child->type);
1.96      schwarze 2082:        if (0 == strcmp("on", n->child->string)) {
                   2083:                if (p->col)
                   2084:                        p->flags &= ~TERMP_NOSPACE;
1.1       kristaps 2085:                p->flags &= ~TERMP_NONOSPACE;
1.96      schwarze 2086:        } else
1.1       kristaps 2087:                p->flags |= TERMP_NONOSPACE;
                   2088:
                   2089:        return(0);
                   2090: }
                   2091:
                   2092:
                   2093: /* ARGSUSED */
                   2094: static int
                   2095: termp_ap_pre(DECL_ARGS)
                   2096: {
                   2097:
                   2098:        p->flags |= TERMP_NOSPACE;
                   2099:        term_word(p, "\\(aq");
                   2100:        p->flags |= TERMP_NOSPACE;
                   2101:        return(1);
                   2102: }
                   2103:
                   2104:
                   2105: /* ARGSUSED */
                   2106: static void
                   2107: termp____post(DECL_ARGS)
                   2108: {
                   2109:
1.62      schwarze 2110:        /* TODO: %U. */
                   2111:
1.1       kristaps 2112:        p->flags |= TERMP_NOSPACE;
1.60      schwarze 2113:        term_word(p, n->next ? "," : ".");
1.1       kristaps 2114: }
                   2115:
                   2116:
                   2117: /* ARGSUSED */
                   2118: static int
1.65      schwarze 2119: termp_li_pre(DECL_ARGS)
                   2120: {
                   2121:
                   2122:        term_fontpush(p, TERMFONT_NONE);
                   2123:        return(1);
                   2124: }
                   2125:
                   2126:
                   2127: /* ARGSUSED */
                   2128: static int
1.1       kristaps 2129: termp_lk_pre(DECL_ARGS)
                   2130: {
1.101     schwarze 2131:        const struct mdoc_node *nn, *sv;
1.1       kristaps 2132:
1.65      schwarze 2133:        term_fontpush(p, TERMFONT_UNDER);
1.101     schwarze 2134:
                   2135:        nn = sv = n->child;
1.62      schwarze 2136:
                   2137:        if (NULL == nn->next)
1.6       schwarze 2138:                return(1);
1.1       kristaps 2139:
1.101     schwarze 2140:        for (nn = nn->next; nn; nn = nn->next)
                   2141:                term_word(p, nn->string);
                   2142:
1.65      schwarze 2143:        term_fontpop(p);
1.62      schwarze 2144:
1.1       kristaps 2145:        p->flags |= TERMP_NOSPACE;
                   2146:        term_word(p, ":");
                   2147:
1.65      schwarze 2148:        term_fontpush(p, TERMFONT_BOLD);
1.101     schwarze 2149:        term_word(p, sv->string);
1.65      schwarze 2150:        term_fontpop(p);
1.6       schwarze 2151:
1.1       kristaps 2152:        return(0);
                   2153: }
                   2154:
1.90      schwarze 2155:
                   2156: /* ARGSUSED */
                   2157: static int
                   2158: termp_bk_pre(DECL_ARGS)
                   2159: {
                   2160:
1.91      schwarze 2161:        switch (n->type) {
                   2162:        case (MDOC_BLOCK):
1.95      schwarze 2163:                break;
1.91      schwarze 2164:        case (MDOC_HEAD):
                   2165:                return(0);
                   2166:        case (MDOC_BODY):
                   2167:                p->flags |= TERMP_PREKEEP;
1.95      schwarze 2168:                break;
1.91      schwarze 2169:        default:
                   2170:                abort();
1.95      schwarze 2171:                /* NOTREACHED */
1.91      schwarze 2172:        }
1.95      schwarze 2173:
                   2174:        return(1);
1.90      schwarze 2175: }
                   2176:
                   2177:
                   2178: /* ARGSUSED */
                   2179: static void
                   2180: termp_bk_post(DECL_ARGS)
                   2181: {
                   2182:
1.91      schwarze 2183:        if (MDOC_BODY == n->type)
                   2184:                p->flags &= ~(TERMP_KEEP | TERMP_PREKEEP);
1.90      schwarze 2185: }
1.1       kristaps 2186:
                   2187: /* ARGSUSED */
                   2188: static int
1.54      schwarze 2189: termp_under_pre(DECL_ARGS)
1.1       kristaps 2190: {
                   2191:
1.65      schwarze 2192:        term_fontpush(p, TERMFONT_UNDER);
1.1       kristaps 2193:        return(1);
                   2194: }