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

Annotation of mandoc/mdoc_term.c, Revision 1.194

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

CVSweb