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

Annotation of mandoc/mdoc_term.c, Revision 1.202

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

CVSweb