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

Annotation of mandoc/mdoc_man.c, Revision 1.55

1.55    ! schwarze    1: /*     $Id: mdoc_man.c,v 1.54 2013/12/24 20:45:27 schwarze Exp $ */
1.1       schwarze    2: /*
1.49      schwarze    3:  * Copyright (c) 2011, 2012, 2013 Ingo Schwarze <schwarze@openbsd.org>
1.1       schwarze    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
1.7       kristaps   17: #ifdef HAVE_CONFIG_H
                     18: #include "config.h"
                     19: #endif
                     20:
1.12      schwarze   21: #include <assert.h>
1.1       schwarze   22: #include <stdio.h>
                     23: #include <string.h>
                     24:
                     25: #include "mandoc.h"
1.11      schwarze   26: #include "out.h"
1.4       kristaps   27: #include "man.h"
1.1       schwarze   28: #include "mdoc.h"
                     29: #include "main.h"
                     30:
1.42      schwarze   31: #define        DECL_ARGS const struct mdoc_meta *meta, \
1.20      schwarze   32:                  const struct mdoc_node *n
1.1       schwarze   33:
                     34: struct manact {
1.5       kristaps   35:        int             (*cond)(DECL_ARGS); /* DON'T run actions */
                     36:        int             (*pre)(DECL_ARGS); /* pre-node action */
                     37:        void            (*post)(DECL_ARGS); /* post-node action */
                     38:        const char       *prefix; /* pre-node string constant */
                     39:        const char       *suffix; /* post-node string constant */
1.1       schwarze   40: };
                     41:
1.5       kristaps   42: static int       cond_body(DECL_ARGS);
1.1       schwarze   43: static int       cond_head(DECL_ARGS);
1.26      schwarze   44: static  void     font_push(char);
                     45: static void      font_pop(void);
1.47      schwarze   46: static void      mid_it(void);
1.34      schwarze   47: static void      post__t(DECL_ARGS);
1.5       kristaps   48: static void      post_bd(DECL_ARGS);
1.27      schwarze   49: static void      post_bf(DECL_ARGS);
1.13      schwarze   50: static void      post_bk(DECL_ARGS);
1.30      schwarze   51: static void      post_bl(DECL_ARGS);
1.5       kristaps   52: static void      post_dl(DECL_ARGS);
1.1       schwarze   53: static void      post_enc(DECL_ARGS);
1.28      schwarze   54: static void      post_eo(DECL_ARGS);
1.17      schwarze   55: static void      post_fa(DECL_ARGS);
1.29      schwarze   56: static void      post_fd(DECL_ARGS);
1.26      schwarze   57: static void      post_fl(DECL_ARGS);
1.16      schwarze   58: static void      post_fn(DECL_ARGS);
1.17      schwarze   59: static void      post_fo(DECL_ARGS);
1.26      schwarze   60: static void      post_font(DECL_ARGS);
1.15      schwarze   61: static void      post_in(DECL_ARGS);
1.30      schwarze   62: static void      post_it(DECL_ARGS);
1.14      schwarze   63: static void      post_lb(DECL_ARGS);
1.5       kristaps   64: static void      post_nm(DECL_ARGS);
1.1       schwarze   65: static void      post_percent(DECL_ARGS);
1.5       kristaps   66: static void      post_pf(DECL_ARGS);
1.3       schwarze   67: static void      post_sect(DECL_ARGS);
1.5       kristaps   68: static void      post_sp(DECL_ARGS);
1.18      schwarze   69: static void      post_vt(DECL_ARGS);
1.34      schwarze   70: static int       pre__t(DECL_ARGS);
1.22      schwarze   71: static int       pre_an(DECL_ARGS);
1.3       schwarze   72: static int       pre_ap(DECL_ARGS);
                     73: static int       pre_bd(DECL_ARGS);
1.27      schwarze   74: static int       pre_bf(DECL_ARGS);
1.13      schwarze   75: static int       pre_bk(DECL_ARGS);
1.30      schwarze   76: static int       pre_bl(DECL_ARGS);
1.3       schwarze   77: static int       pre_br(DECL_ARGS);
1.8       schwarze   78: static int       pre_bx(DECL_ARGS);
1.1       schwarze   79: static int       pre_dl(DECL_ARGS);
1.5       kristaps   80: static int       pre_enc(DECL_ARGS);
1.26      schwarze   81: static int       pre_em(DECL_ARGS);
1.17      schwarze   82: static int       pre_fa(DECL_ARGS);
1.29      schwarze   83: static int       pre_fd(DECL_ARGS);
1.26      schwarze   84: static int       pre_fl(DECL_ARGS);
1.16      schwarze   85: static int       pre_fn(DECL_ARGS);
1.17      schwarze   86: static int       pre_fo(DECL_ARGS);
1.23      schwarze   87: static int       pre_ft(DECL_ARGS);
1.15      schwarze   88: static int       pre_in(DECL_ARGS);
1.1       schwarze   89: static int       pre_it(DECL_ARGS);
1.24      schwarze   90: static int       pre_lk(DECL_ARGS);
1.26      schwarze   91: static int       pre_li(DECL_ARGS);
1.1       schwarze   92: static int       pre_nm(DECL_ARGS);
1.25      schwarze   93: static int       pre_no(DECL_ARGS);
1.1       schwarze   94: static int       pre_ns(DECL_ARGS);
                     95: static int       pre_pp(DECL_ARGS);
1.34      schwarze   96: static int       pre_rs(DECL_ARGS);
1.12      schwarze   97: static int       pre_sm(DECL_ARGS);
1.3       schwarze   98: static int       pre_sp(DECL_ARGS);
1.5       kristaps   99: static int       pre_sect(DECL_ARGS);
1.26      schwarze  100: static int       pre_sy(DECL_ARGS);
1.23      schwarze  101: static void      pre_syn(const struct mdoc_node *);
1.18      schwarze  102: static int       pre_vt(DECL_ARGS);
1.8       schwarze  103: static int       pre_ux(DECL_ARGS);
1.1       schwarze  104: static int       pre_xr(DECL_ARGS);
1.20      schwarze  105: static void      print_word(const char *);
1.36      schwarze  106: static void      print_line(const char *, int);
                    107: static void      print_block(const char *, int);
1.20      schwarze  108: static void      print_offs(const char *);
1.35      schwarze  109: static void      print_width(const char *,
                    110:                                const struct mdoc_node *, size_t);
1.30      schwarze  111: static void      print_count(int *);
1.5       kristaps  112: static void      print_node(DECL_ARGS);
1.1       schwarze  113:
1.3       schwarze  114: static const struct manact manacts[MDOC_MAX + 1] = {
                    115:        { NULL, pre_ap, NULL, NULL, NULL }, /* Ap */
                    116:        { NULL, NULL, NULL, NULL, NULL }, /* Dd */
                    117:        { NULL, NULL, NULL, NULL, NULL }, /* Dt */
1.8       schwarze  118:        { NULL, NULL, NULL, NULL, NULL }, /* Os */
1.3       schwarze  119:        { NULL, pre_sect, post_sect, ".SH", NULL }, /* Sh */
                    120:        { NULL, pre_sect, post_sect, ".SS", NULL }, /* Ss */
1.1       schwarze  121:        { NULL, pre_pp, NULL, NULL, NULL }, /* Pp */
1.3       schwarze  122:        { cond_body, pre_dl, post_dl, NULL, NULL }, /* D1 */
1.1       schwarze  123:        { cond_body, pre_dl, post_dl, NULL, NULL }, /* Dl */
1.3       schwarze  124:        { cond_body, pre_bd, post_bd, NULL, NULL }, /* Bd */
                    125:        { NULL, NULL, NULL, NULL, NULL }, /* Ed */
1.30      schwarze  126:        { cond_body, pre_bl, post_bl, NULL, NULL }, /* Bl */
1.3       schwarze  127:        { NULL, NULL, NULL, NULL, NULL }, /* El */
1.30      schwarze  128:        { NULL, pre_it, post_it, NULL, NULL }, /* It */
1.26      schwarze  129:        { NULL, pre_em, post_font, NULL, NULL }, /* Ad */
1.22      schwarze  130:        { NULL, pre_an, NULL, NULL, NULL }, /* An */
1.26      schwarze  131:        { NULL, pre_em, post_font, NULL, NULL }, /* Ar */
                    132:        { NULL, pre_sy, post_font, NULL, NULL }, /* Cd */
                    133:        { NULL, pre_sy, post_font, NULL, NULL }, /* Cm */
                    134:        { NULL, pre_li, post_font, NULL, NULL }, /* Dv */
                    135:        { NULL, pre_li, post_font, NULL, NULL }, /* Er */
                    136:        { NULL, pre_li, post_font, NULL, NULL }, /* Ev */
1.1       schwarze  137:        { NULL, pre_enc, post_enc, "The \\fB",
                    138:            "\\fP\nutility exits 0 on success, and >0 if an error occurs."
                    139:            }, /* Ex */
1.17      schwarze  140:        { NULL, pre_fa, post_fa, NULL, NULL }, /* Fa */
1.29      schwarze  141:        { NULL, pre_fd, post_fd, NULL, NULL }, /* Fd */
1.26      schwarze  142:        { NULL, pre_fl, post_fl, NULL, NULL }, /* Fl */
1.16      schwarze  143:        { NULL, pre_fn, post_fn, NULL, NULL }, /* Fn */
1.26      schwarze  144:        { NULL, pre_ft, post_font, NULL, NULL }, /* Ft */
                    145:        { NULL, pre_sy, post_font, NULL, NULL }, /* Ic */
1.15      schwarze  146:        { NULL, pre_in, post_in, NULL, NULL }, /* In */
1.26      schwarze  147:        { NULL, pre_li, post_font, NULL, NULL }, /* Li */
1.1       schwarze  148:        { cond_head, pre_enc, NULL, "\\- ", NULL }, /* Nd */
                    149:        { NULL, pre_nm, post_nm, NULL, NULL }, /* Nm */
                    150:        { cond_body, pre_enc, post_enc, "[", "]" }, /* Op */
1.8       schwarze  151:        { NULL, NULL, NULL, NULL, NULL }, /* Ot */
1.26      schwarze  152:        { NULL, pre_em, post_font, NULL, NULL }, /* Pa */
1.6       kristaps  153:        { NULL, pre_enc, post_enc, "The \\fB",
                    154:                "\\fP\nfunction returns the value 0 if successful;\n"
                    155:                "otherwise the value -1 is returned and the global\n"
                    156:                "variable \\fIerrno\\fP is set to indicate the error."
                    157:                }, /* Rv */
1.8       schwarze  158:        { NULL, NULL, NULL, NULL, NULL }, /* St */
1.26      schwarze  159:        { NULL, pre_em, post_font, NULL, NULL }, /* Va */
1.18      schwarze  160:        { NULL, pre_vt, post_vt, NULL, NULL }, /* Vt */
1.8       schwarze  161:        { NULL, pre_xr, NULL, NULL, NULL }, /* Xr */
1.34      schwarze  162:        { NULL, NULL, post_percent, NULL, NULL }, /* %A */
                    163:        { NULL, pre_em, post_percent, NULL, NULL }, /* %B */
                    164:        { NULL, NULL, post_percent, NULL, NULL }, /* %D */
                    165:        { NULL, pre_em, post_percent, NULL, NULL }, /* %I */
                    166:        { NULL, pre_em, post_percent, NULL, NULL }, /* %J */
                    167:        { NULL, NULL, post_percent, NULL, NULL }, /* %N */
                    168:        { NULL, NULL, post_percent, NULL, NULL }, /* %O */
                    169:        { NULL, NULL, post_percent, NULL, NULL }, /* %P */
                    170:        { NULL, NULL, post_percent, NULL, NULL }, /* %R */
                    171:        { NULL, pre__t, post__t, NULL, NULL }, /* %T */
                    172:        { NULL, NULL, post_percent, NULL, NULL }, /* %V */
1.9       schwarze  173:        { NULL, NULL, NULL, NULL, NULL }, /* Ac */
                    174:        { cond_body, pre_enc, post_enc, "<", ">" }, /* Ao */
1.1       schwarze  175:        { cond_body, pre_enc, post_enc, "<", ">" }, /* Aq */
1.8       schwarze  176:        { NULL, NULL, NULL, NULL, NULL }, /* At */
1.3       schwarze  177:        { NULL, NULL, NULL, NULL, NULL }, /* Bc */
1.27      schwarze  178:        { NULL, pre_bf, post_bf, NULL, NULL }, /* Bf */
1.3       schwarze  179:        { cond_body, pre_enc, post_enc, "[", "]" }, /* Bo */
                    180:        { cond_body, pre_enc, post_enc, "[", "]" }, /* Bq */
1.8       schwarze  181:        { NULL, pre_ux, NULL, "BSD/OS", NULL }, /* Bsx */
                    182:        { NULL, pre_bx, NULL, NULL, NULL }, /* Bx */
                    183:        { NULL, NULL, NULL, NULL, NULL }, /* Db */
1.9       schwarze  184:        { NULL, NULL, NULL, NULL, NULL }, /* Dc */
1.51      schwarze  185:        { cond_body, pre_enc, post_enc, "\\(lq", "\\(rq" }, /* Do */
                    186:        { cond_body, pre_enc, post_enc, "\\(lq", "\\(rq" }, /* Dq */
1.28      schwarze  187:        { NULL, NULL, NULL, NULL, NULL }, /* Ec */
                    188:        { NULL, NULL, NULL, NULL, NULL }, /* Ef */
1.26      schwarze  189:        { NULL, pre_em, post_font, NULL, NULL }, /* Em */
1.28      schwarze  190:        { NULL, NULL, post_eo, NULL, NULL }, /* Eo */
1.8       schwarze  191:        { NULL, pre_ux, NULL, "FreeBSD", NULL }, /* Fx */
1.26      schwarze  192:        { NULL, pre_sy, post_font, NULL, NULL }, /* Ms */
1.25      schwarze  193:        { NULL, pre_no, NULL, NULL, NULL }, /* No */
1.1       schwarze  194:        { NULL, pre_ns, NULL, NULL, NULL }, /* Ns */
1.8       schwarze  195:        { NULL, pre_ux, NULL, "NetBSD", NULL }, /* Nx */
                    196:        { NULL, pre_ux, NULL, "OpenBSD", NULL }, /* Ox */
1.3       schwarze  197:        { NULL, NULL, NULL, NULL, NULL }, /* Pc */
                    198:        { NULL, NULL, post_pf, NULL, NULL }, /* Pf */
                    199:        { cond_body, pre_enc, post_enc, "(", ")" }, /* Po */
                    200:        { cond_body, pre_enc, post_enc, "(", ")" }, /* Pq */
1.9       schwarze  201:        { NULL, NULL, NULL, NULL, NULL }, /* Qc */
1.51      schwarze  202:        { cond_body, pre_enc, post_enc, "\\(oq", "\\(cq" }, /* Ql */
1.9       schwarze  203:        { cond_body, pre_enc, post_enc, "\"", "\"" }, /* Qo */
                    204:        { cond_body, pre_enc, post_enc, "\"", "\"" }, /* Qq */
                    205:        { NULL, NULL, NULL, NULL, NULL }, /* Re */
1.34      schwarze  206:        { cond_body, pre_rs, NULL, NULL, NULL }, /* Rs */
1.9       schwarze  207:        { NULL, NULL, NULL, NULL, NULL }, /* Sc */
1.51      schwarze  208:        { cond_body, pre_enc, post_enc, "\\(oq", "\\(cq" }, /* So */
                    209:        { cond_body, pre_enc, post_enc, "\\(oq", "\\(cq" }, /* Sq */
1.12      schwarze  210:        { NULL, pre_sm, NULL, NULL, NULL }, /* Sm */
1.26      schwarze  211:        { NULL, pre_em, post_font, NULL, NULL }, /* Sx */
                    212:        { NULL, pre_sy, post_font, NULL, NULL }, /* Sy */
                    213:        { NULL, pre_li, post_font, NULL, NULL }, /* Tn */
1.8       schwarze  214:        { NULL, pre_ux, NULL, "UNIX", NULL }, /* Ux */
1.34      schwarze  215:        { NULL, NULL, NULL, NULL, NULL }, /* Xc */
                    216:        { NULL, NULL, NULL, NULL, NULL }, /* Xo */
1.17      schwarze  217:        { NULL, pre_fo, post_fo, NULL, NULL }, /* Fo */
                    218:        { NULL, NULL, NULL, NULL, NULL }, /* Fc */
1.3       schwarze  219:        { cond_body, pre_enc, post_enc, "[", "]" }, /* Oo */
1.9       schwarze  220:        { NULL, NULL, NULL, NULL, NULL }, /* Oc */
1.13      schwarze  221:        { NULL, pre_bk, post_bk, NULL, NULL }, /* Bk */
                    222:        { NULL, NULL, NULL, NULL, NULL }, /* Ek */
1.8       schwarze  223:        { NULL, pre_ux, NULL, "is currently in beta test.", NULL }, /* Bt */
                    224:        { NULL, NULL, NULL, NULL, NULL }, /* Hf */
                    225:        { NULL, NULL, NULL, NULL, NULL }, /* Fr */
                    226:        { NULL, pre_ux, NULL, "currently under development.", NULL }, /* Ud */
1.14      schwarze  227:        { NULL, NULL, post_lb, NULL, NULL }, /* Lb */
1.3       schwarze  228:        { NULL, pre_pp, NULL, NULL, NULL }, /* Lp */
1.24      schwarze  229:        { NULL, pre_lk, NULL, NULL, NULL }, /* Lk */
1.26      schwarze  230:        { NULL, pre_em, post_font, NULL, NULL }, /* Mt */
1.9       schwarze  231:        { cond_body, pre_enc, post_enc, "{", "}" }, /* Brq */
                    232:        { cond_body, pre_enc, post_enc, "{", "}" }, /* Bro */
                    233:        { NULL, NULL, NULL, NULL, NULL }, /* Brc */
1.34      schwarze  234:        { NULL, NULL, post_percent, NULL, NULL }, /* %C */
1.29      schwarze  235:        { NULL, NULL, NULL, NULL, NULL }, /* Es */
                    236:        { NULL, NULL, NULL, NULL, NULL }, /* En */
1.8       schwarze  237:        { NULL, pre_ux, NULL, "DragonFly", NULL }, /* Dx */
1.34      schwarze  238:        { NULL, NULL, post_percent, NULL, NULL }, /* %Q */
1.3       schwarze  239:        { NULL, pre_br, NULL, NULL, NULL }, /* br */
                    240:        { NULL, pre_sp, post_sp, NULL, NULL }, /* sp */
1.34      schwarze  241:        { NULL, NULL, post_percent, NULL, NULL }, /* %U */
                    242:        { NULL, NULL, NULL, NULL, NULL }, /* Ta */
1.3       schwarze  243:        { NULL, NULL, NULL, NULL, NULL }, /* ROOT */
1.1       schwarze  244: };
                    245:
1.20      schwarze  246: static int             outflags;
1.36      schwarze  247: #define        MMAN_spc        (1 << 0)  /* blank character before next word */
                    248: #define        MMAN_spc_force  (1 << 1)  /* even before trailing punctuation */
                    249: #define        MMAN_nl         (1 << 2)  /* break man(7) code line */
                    250: #define        MMAN_br         (1 << 3)  /* break output line */
                    251: #define        MMAN_sp         (1 << 4)  /* insert a blank output line */
                    252: #define        MMAN_PP         (1 << 5)  /* reset indentation etc. */
                    253: #define        MMAN_Sm         (1 << 6)  /* horizontal spacing mode */
                    254: #define        MMAN_Bk         (1 << 7)  /* word keep mode */
1.46      schwarze  255: #define        MMAN_Bk_susp    (1 << 8)  /* suspend this (after a macro) */
                    256: #define        MMAN_An_split   (1 << 9)  /* author mode is "split" */
                    257: #define        MMAN_An_nosplit (1 << 10) /* author mode is "nosplit" */
                    258: #define        MMAN_PD         (1 << 11) /* inter-paragraph spacing disabled */
1.20      schwarze  259:
1.43      schwarze  260: #define        BL_STACK_MAX    32
                    261:
                    262: static size_t          Bl_stack[BL_STACK_MAX];  /* offsets [chars] */
                    263: static int             Bl_stack_post[BL_STACK_MAX];  /* add final .RE */
                    264: static int             Bl_stack_len;  /* number of nested Bl blocks */
1.37      schwarze  265: static int             TPremain;  /* characters before tag is full */
                    266:
1.26      schwarze  267: static struct {
                    268:        char    *head;
                    269:        char    *tail;
                    270:        size_t   size;
                    271: }      fontqueue;
                    272:
                    273: static void
                    274: font_push(char newfont)
                    275: {
                    276:
                    277:        if (fontqueue.head + fontqueue.size <= ++fontqueue.tail) {
                    278:                fontqueue.size += 8;
                    279:                fontqueue.head = mandoc_realloc(fontqueue.head,
                    280:                                fontqueue.size);
                    281:        }
                    282:        *fontqueue.tail = newfont;
1.37      schwarze  283:        print_word("");
                    284:        printf("\\f");
1.26      schwarze  285:        putchar(newfont);
                    286:        outflags &= ~MMAN_spc;
                    287: }
                    288:
                    289: static void
                    290: font_pop(void)
                    291: {
                    292:
                    293:        if (fontqueue.tail > fontqueue.head)
                    294:                fontqueue.tail--;
                    295:        outflags &= ~MMAN_spc;
1.37      schwarze  296:        print_word("");
                    297:        printf("\\f");
1.26      schwarze  298:        putchar(*fontqueue.tail);
                    299: }
                    300:
1.1       schwarze  301: static void
1.20      schwarze  302: print_word(const char *s)
1.1       schwarze  303: {
1.5       kristaps  304:
1.36      schwarze  305:        if ((MMAN_PP | MMAN_sp | MMAN_br | MMAN_nl) & outflags) {
1.5       kristaps  306:                /*
                    307:                 * If we need a newline, print it now and start afresh.
                    308:                 */
1.36      schwarze  309:                if (MMAN_PP & outflags) {
1.44      schwarze  310:                        if (MMAN_sp & outflags) {
                    311:                                if (MMAN_PD & outflags) {
                    312:                                        printf("\n.PD");
                    313:                                        outflags &= ~MMAN_PD;
                    314:                                }
                    315:                        } else if ( ! (MMAN_PD & outflags)) {
                    316:                                printf("\n.PD 0");
                    317:                                outflags |= MMAN_PD;
                    318:                        }
1.36      schwarze  319:                        printf("\n.PP\n");
                    320:                } else if (MMAN_sp & outflags)
1.21      schwarze  321:                        printf("\n.sp\n");
                    322:                else if (MMAN_br & outflags)
                    323:                        printf("\n.br\n");
                    324:                else if (MMAN_nl & outflags)
                    325:                        putchar('\n');
1.36      schwarze  326:                outflags &= ~(MMAN_PP|MMAN_sp|MMAN_br|MMAN_nl|MMAN_spc);
1.37      schwarze  327:                if (1 == TPremain)
                    328:                        printf(".br\n");
                    329:                TPremain = 0;
                    330:        } else if (MMAN_spc & outflags) {
1.5       kristaps  331:                /*
1.25      schwarze  332:                 * If we need a space, only print it if
                    333:                 * (1) it is forced by `No' or
                    334:                 * (2) what follows is not terminating punctuation or
                    335:                 * (3) what follows is longer than one character.
1.5       kristaps  336:                 */
1.37      schwarze  337:                if (MMAN_spc_force & outflags || '\0' == s[0] ||
1.25      schwarze  338:                    NULL == strchr(".,:;)]?!", s[0]) || '\0' != s[1]) {
1.46      schwarze  339:                        if (MMAN_Bk & outflags &&
                    340:                            ! (MMAN_Bk_susp & outflags))
1.13      schwarze  341:                                putchar('\\');
1.45      schwarze  342:                        putchar(' ');
1.37      schwarze  343:                        if (TPremain)
                    344:                                TPremain--;
1.13      schwarze  345:                }
1.37      schwarze  346:        }
1.5       kristaps  347:
                    348:        /*
                    349:         * Reassign needing space if we're not following opening
                    350:         * punctuation.
                    351:         */
1.37      schwarze  352:        if (MMAN_Sm & outflags && ('\0' == s[0] ||
                    353:            (('(' != s[0] && '[' != s[0]) || '\0' != s[1])))
1.20      schwarze  354:                outflags |= MMAN_spc;
                    355:        else
                    356:                outflags &= ~MMAN_spc;
1.46      schwarze  357:        outflags &= ~(MMAN_spc_force | MMAN_Bk_susp);
1.5       kristaps  358:
1.1       schwarze  359:        for ( ; *s; s++) {
                    360:                switch (*s) {
                    361:                case (ASCII_NBRSP):
1.45      schwarze  362:                        printf("\\ ");
1.1       schwarze  363:                        break;
                    364:                case (ASCII_HYPH):
                    365:                        putchar('-');
                    366:                        break;
                    367:                default:
1.5       kristaps  368:                        putchar((unsigned char)*s);
1.1       schwarze  369:                        break;
                    370:                }
1.37      schwarze  371:                if (TPremain)
                    372:                        TPremain--;
1.1       schwarze  373:        }
1.4       kristaps  374: }
                    375:
1.11      schwarze  376: static void
1.36      schwarze  377: print_line(const char *s, int newflags)
                    378: {
                    379:
                    380:        outflags &= ~MMAN_br;
                    381:        outflags |= MMAN_nl;
                    382:        print_word(s);
                    383:        outflags |= newflags;
                    384: }
                    385:
                    386: static void
                    387: print_block(const char *s, int newflags)
                    388: {
                    389:
                    390:        outflags &= ~MMAN_PP;
1.44      schwarze  391:        if (MMAN_sp & outflags) {
1.36      schwarze  392:                outflags &= ~(MMAN_sp | MMAN_br);
1.44      schwarze  393:                if (MMAN_PD & outflags) {
                    394:                        print_line(".PD", 0);
                    395:                        outflags &= ~MMAN_PD;
                    396:                }
1.46      schwarze  397:        } else if (! (MMAN_PD & outflags))
                    398:                print_line(".PD 0", MMAN_PD);
1.36      schwarze  399:        outflags |= MMAN_nl;
                    400:        print_word(s);
1.46      schwarze  401:        outflags |= MMAN_Bk_susp | newflags;
1.36      schwarze  402: }
                    403:
                    404: static void
1.20      schwarze  405: print_offs(const char *v)
1.11      schwarze  406: {
                    407:        char              buf[24];
                    408:        struct roffsu     su;
                    409:        size_t            sz;
                    410:
1.48      schwarze  411:        print_line(".RS", MMAN_Bk_susp);
                    412:
1.43      schwarze  413:        /* Convert v into a number (of characters). */
1.11      schwarze  414:        if (NULL == v || '\0' == *v || 0 == strcmp(v, "left"))
                    415:                sz = 0;
                    416:        else if (0 == strcmp(v, "indent"))
                    417:                sz = 6;
                    418:        else if (0 == strcmp(v, "indent-two"))
                    419:                sz = 12;
                    420:        else if (a2roffsu(v, &su, SCALE_MAX)) {
1.43      schwarze  421:                if (SCALE_EN == su.unit)
                    422:                        sz = su.scale;
                    423:                else {
                    424:                        /*
                    425:                         * XXX
                    426:                         * If we are inside an enclosing list,
                    427:                         * there is no easy way to add the two
                    428:                         * indentations because they are provided
                    429:                         * in terms of different units.
                    430:                         */
                    431:                        print_word(v);
1.48      schwarze  432:                        outflags |= MMAN_nl;
1.43      schwarze  433:                        return;
                    434:                }
1.11      schwarze  435:        } else
                    436:                sz = strlen(v);
                    437:
1.43      schwarze  438:        /*
                    439:         * We are inside an enclosing list.
                    440:         * Add the two indentations.
                    441:         */
                    442:        if (Bl_stack_len)
                    443:                sz += Bl_stack[Bl_stack_len - 1];
                    444:
1.53      schwarze  445:        snprintf(buf, sizeof(buf), "%zun", sz);
1.20      schwarze  446:        print_word(buf);
1.48      schwarze  447:        outflags |= MMAN_nl;
1.11      schwarze  448: }
                    449:
1.47      schwarze  450: /*
                    451:  * Set up the indentation for a list item; used from pre_it().
                    452:  */
1.4       kristaps  453: void
1.35      schwarze  454: print_width(const char *v, const struct mdoc_node *child, size_t defsz)
1.30      schwarze  455: {
                    456:        char              buf[24];
                    457:        struct roffsu     su;
1.31      schwarze  458:        size_t            sz, chsz;
1.37      schwarze  459:        int               numeric, remain;
1.31      schwarze  460:
1.37      schwarze  461:        numeric = 1;
                    462:        remain = 0;
1.43      schwarze  463:
                    464:        /* Convert v into a number (of characters). */
1.35      schwarze  465:        if (NULL == v)
                    466:                sz = defsz;
                    467:        else if (a2roffsu(v, &su, SCALE_MAX)) {
1.30      schwarze  468:                if (SCALE_EN == su.unit)
                    469:                        sz = su.scale;
                    470:                else {
1.37      schwarze  471:                        sz = 0;
                    472:                        numeric = 0;
1.30      schwarze  473:                }
                    474:        } else
                    475:                sz = strlen(v);
                    476:
1.37      schwarze  477:        /* XXX Rough estimation, might have multiple parts. */
                    478:        chsz = (NULL != child && MDOC_TEXT == child->type) ?
                    479:                        strlen(child->string) : 0;
                    480:
1.47      schwarze  481:        /* Maybe we are inside an enclosing list? */
                    482:        mid_it();
1.43      schwarze  483:
                    484:        /*
                    485:         * Save our own indentation,
                    486:         * such that child lists can use it.
                    487:         */
                    488:        Bl_stack[Bl_stack_len++] = sz + 2;
                    489:
                    490:        /* Set up the current list. */
1.37      schwarze  491:        if (defsz && chsz > sz)
1.36      schwarze  492:                print_block(".HP", 0);
1.37      schwarze  493:        else {
1.36      schwarze  494:                print_block(".TP", 0);
1.37      schwarze  495:                remain = sz + 2;
                    496:        }
                    497:        if (numeric) {
1.53      schwarze  498:                snprintf(buf, sizeof(buf), "%zun", sz + 2);
1.37      schwarze  499:                print_word(buf);
                    500:        } else
                    501:                print_word(v);
                    502:        TPremain = remain;
1.30      schwarze  503: }
                    504:
                    505: void
                    506: print_count(int *count)
                    507: {
                    508:        char              buf[12];
                    509:
                    510:        snprintf(buf, sizeof(buf), "%d.", ++*count);
                    511:        print_word(buf);
                    512: }
                    513:
                    514: void
1.4       kristaps  515: man_man(void *arg, const struct man *man)
                    516: {
                    517:
1.5       kristaps  518:        /*
                    519:         * Dump the keep buffer.
                    520:         * We're guaranteed by now that this exists (is non-NULL).
                    521:         * Flush stdout afterward, just in case.
                    522:         */
1.4       kristaps  523:        fputs(mparse_getkeep(man_mparse(man)), stdout);
1.5       kristaps  524:        fflush(stdout);
1.1       schwarze  525: }
                    526:
                    527: void
                    528: man_mdoc(void *arg, const struct mdoc *mdoc)
                    529: {
1.42      schwarze  530:        const struct mdoc_meta *meta;
1.1       schwarze  531:        const struct mdoc_node *n;
                    532:
1.42      schwarze  533:        meta = mdoc_meta(mdoc);
1.1       schwarze  534:        n = mdoc_node(mdoc);
                    535:
1.45      schwarze  536:        printf(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"\n",
1.42      schwarze  537:                        meta->title, meta->msec, meta->date,
                    538:                        meta->os, meta->vol);
1.1       schwarze  539:
1.45      schwarze  540:        /* Disable hyphenation and if nroff, disable justification. */
                    541:        printf(".nh\n.if n .ad l");
                    542:
1.20      schwarze  543:        outflags = MMAN_nl | MMAN_Sm;
1.26      schwarze  544:        if (0 == fontqueue.size) {
                    545:                fontqueue.size = 8;
                    546:                fontqueue.head = fontqueue.tail = mandoc_malloc(8);
                    547:                *fontqueue.tail = 'R';
                    548:        }
1.42      schwarze  549:        print_node(meta, n);
1.3       schwarze  550:        putchar('\n');
1.1       schwarze  551: }
                    552:
                    553: static void
                    554: print_node(DECL_ARGS)
                    555: {
1.52      schwarze  556:        const struct mdoc_node  *sub;
1.5       kristaps  557:        const struct manact     *act;
1.1       schwarze  558:        int                      cond, do_sub;
1.46      schwarze  559:
1.5       kristaps  560:        /*
                    561:         * Break the line if we were parsed subsequent the current node.
                    562:         * This makes the page structure be more consistent.
                    563:         */
1.52      schwarze  564:        if (MMAN_spc & outflags && MDOC_LINE & n->flags)
1.20      schwarze  565:                outflags |= MMAN_nl;
1.1       schwarze  566:
1.5       kristaps  567:        act = NULL;
1.1       schwarze  568:        cond = 0;
                    569:        do_sub = 1;
1.5       kristaps  570:
1.1       schwarze  571:        if (MDOC_TEXT == n->type) {
1.5       kristaps  572:                /*
                    573:                 * Make sure that we don't happen to start with a
                    574:                 * control character at the start of a line.
                    575:                 */
1.20      schwarze  576:                if (MMAN_nl & outflags && ('.' == *n->string ||
1.5       kristaps  577:                                        '\'' == *n->string)) {
1.37      schwarze  578:                        print_word("");
                    579:                        printf("\\&");
1.20      schwarze  580:                        outflags &= ~MMAN_spc;
1.3       schwarze  581:                }
1.20      schwarze  582:                print_word(n->string);
1.1       schwarze  583:        } else {
1.5       kristaps  584:                /*
                    585:                 * Conditionally run the pre-node action handler for a
                    586:                 * node.
                    587:                 */
1.1       schwarze  588:                act = manacts + n->tok;
1.42      schwarze  589:                cond = NULL == act->cond || (*act->cond)(meta, n);
1.1       schwarze  590:                if (cond && act->pre)
1.42      schwarze  591:                        do_sub = (*act->pre)(meta, n);
1.1       schwarze  592:        }
                    593:
1.5       kristaps  594:        /*
                    595:         * Conditionally run all child nodes.
                    596:         * Note that this iterates over children instead of using
                    597:         * recursion.  This prevents unnecessary depth in the stack.
                    598:         */
1.1       schwarze  599:        if (do_sub)
                    600:                for (sub = n->child; sub; sub = sub->next)
1.42      schwarze  601:                        print_node(meta, sub);
1.1       schwarze  602:
1.5       kristaps  603:        /*
                    604:         * Lastly, conditionally run the post-node handler.
                    605:         */
1.1       schwarze  606:        if (cond && act->post)
1.42      schwarze  607:                (*act->post)(meta, n);
1.1       schwarze  608: }
                    609:
                    610: static int
                    611: cond_head(DECL_ARGS)
                    612: {
1.5       kristaps  613:
1.1       schwarze  614:        return(MDOC_HEAD == n->type);
                    615: }
                    616:
                    617: static int
                    618: cond_body(DECL_ARGS)
                    619: {
1.5       kristaps  620:
1.1       schwarze  621:        return(MDOC_BODY == n->type);
                    622: }
                    623:
                    624: static int
                    625: pre_enc(DECL_ARGS)
                    626: {
1.5       kristaps  627:        const char      *prefix;
1.1       schwarze  628:
                    629:        prefix = manacts[n->tok].prefix;
                    630:        if (NULL == prefix)
                    631:                return(1);
1.20      schwarze  632:        print_word(prefix);
                    633:        outflags &= ~MMAN_spc;
1.1       schwarze  634:        return(1);
                    635: }
                    636:
                    637: static void
                    638: post_enc(DECL_ARGS)
                    639: {
                    640:        const char *suffix;
                    641:
                    642:        suffix = manacts[n->tok].suffix;
                    643:        if (NULL == suffix)
                    644:                return;
1.20      schwarze  645:        outflags &= ~MMAN_spc;
                    646:        print_word(suffix);
1.26      schwarze  647: }
                    648:
                    649: static void
                    650: post_font(DECL_ARGS)
                    651: {
                    652:
                    653:        font_pop();
1.1       schwarze  654: }
                    655:
                    656: static void
                    657: post_percent(DECL_ARGS)
                    658: {
                    659:
1.34      schwarze  660:        if (pre_em == manacts[n->tok].pre)
                    661:                font_pop();
                    662:        if (n->next) {
1.20      schwarze  663:                print_word(",");
1.34      schwarze  664:                if (n->prev &&  n->prev->tok == n->tok &&
                    665:                                n->next->tok == n->tok)
                    666:                        print_word("and");
                    667:        } else {
1.20      schwarze  668:                print_word(".");
                    669:                outflags |= MMAN_nl;
1.1       schwarze  670:        }
                    671: }
                    672:
1.34      schwarze  673: static int
                    674: pre__t(DECL_ARGS)
                    675: {
                    676:
                    677:         if (n->parent && MDOC_Rs == n->parent->tok &&
                    678:                         n->parent->norm->Rs.quote_T) {
1.37      schwarze  679:                print_word("");
                    680:                putchar('\"');
1.34      schwarze  681:                outflags &= ~MMAN_spc;
                    682:        } else
                    683:                font_push('I');
                    684:        return(1);
                    685: }
                    686:
                    687: static void
                    688: post__t(DECL_ARGS)
                    689: {
                    690:
                    691:         if (n->parent && MDOC_Rs == n->parent->tok &&
                    692:                         n->parent->norm->Rs.quote_T) {
                    693:                outflags &= ~MMAN_spc;
1.37      schwarze  694:                print_word("");
                    695:                putchar('\"');
1.34      schwarze  696:        } else
                    697:                font_pop();
1.42      schwarze  698:        post_percent(meta, n);
1.34      schwarze  699: }
                    700:
1.5       kristaps  701: /*
                    702:  * Print before a section header.
                    703:  */
1.1       schwarze  704: static int
1.3       schwarze  705: pre_sect(DECL_ARGS)
                    706: {
                    707:
1.54      schwarze  708:        if (MDOC_HEAD == n->type) {
1.46      schwarze  709:                outflags |= MMAN_sp;
                    710:                print_block(manacts[n->tok].prefix, 0);
                    711:                print_word("");
                    712:                putchar('\"');
                    713:                outflags &= ~MMAN_spc;
                    714:        }
1.3       schwarze  715:        return(1);
                    716: }
                    717:
1.5       kristaps  718: /*
                    719:  * Print subsequent a section header.
                    720:  */
1.3       schwarze  721: static void
                    722: post_sect(DECL_ARGS)
                    723: {
                    724:
                    725:        if (MDOC_HEAD != n->type)
                    726:                return;
1.20      schwarze  727:        outflags &= ~MMAN_spc;
1.37      schwarze  728:        print_word("");
                    729:        putchar('\"');
1.20      schwarze  730:        outflags |= MMAN_nl;
1.22      schwarze  731:        if (MDOC_Sh == n->tok && SEC_AUTHORS == n->sec)
                    732:                outflags &= ~(MMAN_An_split | MMAN_An_nosplit);
                    733: }
                    734:
1.23      schwarze  735: /* See mdoc_term.c, synopsis_pre() for comments. */
                    736: static void
                    737: pre_syn(const struct mdoc_node *n)
                    738: {
                    739:
                    740:        if (NULL == n->prev || ! (MDOC_SYNPRETTY & n->flags))
                    741:                return;
                    742:
                    743:        if (n->prev->tok == n->tok &&
                    744:                        MDOC_Ft != n->tok &&
                    745:                        MDOC_Fo != n->tok &&
                    746:                        MDOC_Fn != n->tok) {
                    747:                outflags |= MMAN_br;
                    748:                return;
                    749:        }
                    750:
                    751:        switch (n->prev->tok) {
                    752:        case (MDOC_Fd):
                    753:                /* FALLTHROUGH */
                    754:        case (MDOC_Fn):
                    755:                /* FALLTHROUGH */
                    756:        case (MDOC_Fo):
                    757:                /* FALLTHROUGH */
                    758:        case (MDOC_In):
                    759:                /* FALLTHROUGH */
                    760:        case (MDOC_Vt):
                    761:                outflags |= MMAN_sp;
                    762:                break;
                    763:        case (MDOC_Ft):
                    764:                if (MDOC_Fn != n->tok && MDOC_Fo != n->tok) {
                    765:                        outflags |= MMAN_sp;
                    766:                        break;
                    767:                }
                    768:                /* FALLTHROUGH */
                    769:        default:
                    770:                outflags |= MMAN_br;
                    771:                break;
                    772:        }
                    773: }
                    774:
1.22      schwarze  775: static int
                    776: pre_an(DECL_ARGS)
                    777: {
                    778:
                    779:        switch (n->norm->An.auth) {
                    780:        case (AUTH_split):
                    781:                outflags &= ~MMAN_An_nosplit;
                    782:                outflags |= MMAN_An_split;
                    783:                return(0);
                    784:        case (AUTH_nosplit):
                    785:                outflags &= ~MMAN_An_split;
                    786:                outflags |= MMAN_An_nosplit;
                    787:                return(0);
                    788:        default:
                    789:                if (MMAN_An_split & outflags)
                    790:                        outflags |= MMAN_br;
                    791:                else if (SEC_AUTHORS == n->sec &&
                    792:                    ! (MMAN_An_nosplit & outflags))
                    793:                        outflags |= MMAN_An_split;
                    794:                return(1);
                    795:        }
1.3       schwarze  796: }
                    797:
                    798: static int
                    799: pre_ap(DECL_ARGS)
                    800: {
                    801:
1.20      schwarze  802:        outflags &= ~MMAN_spc;
                    803:        print_word("'");
                    804:        outflags &= ~MMAN_spc;
1.3       schwarze  805:        return(0);
                    806: }
                    807:
                    808: static int
                    809: pre_bd(DECL_ARGS)
                    810: {
                    811:
1.36      schwarze  812:        outflags &= ~(MMAN_PP | MMAN_sp | MMAN_br);
                    813:
                    814:        if (DISP_unfilled == n->norm->Bd.type ||
                    815:            DISP_literal  == n->norm->Bd.type)
                    816:                print_line(".nf", 0);
                    817:        if (0 == n->norm->Bd.comp && NULL != n->parent->prev)
1.21      schwarze  818:                outflags |= MMAN_sp;
1.20      schwarze  819:        print_offs(n->norm->Bd.offs);
1.3       schwarze  820:        return(1);
                    821: }
                    822:
                    823: static void
                    824: post_bd(DECL_ARGS)
                    825: {
                    826:
1.43      schwarze  827:        /* Close out this display. */
1.36      schwarze  828:        print_line(".RE", MMAN_nl);
1.3       schwarze  829:        if (DISP_unfilled == n->norm->Bd.type ||
1.36      schwarze  830:            DISP_literal  == n->norm->Bd.type)
                    831:                print_line(".fi", MMAN_nl);
1.43      schwarze  832:
1.47      schwarze  833:        /* Maybe we are inside an enclosing list? */
                    834:        if (NULL != n->parent->next)
                    835:                mid_it();
1.13      schwarze  836: }
                    837:
                    838: static int
1.27      schwarze  839: pre_bf(DECL_ARGS)
                    840: {
                    841:
                    842:        switch (n->type) {
                    843:        case (MDOC_BLOCK):
                    844:                return(1);
                    845:        case (MDOC_BODY):
                    846:                break;
                    847:        default:
                    848:                return(0);
                    849:        }
                    850:        switch (n->norm->Bf.font) {
                    851:        case (FONT_Em):
                    852:                font_push('I');
                    853:                break;
                    854:        case (FONT_Sy):
                    855:                font_push('B');
                    856:                break;
                    857:        default:
                    858:                font_push('R');
                    859:                break;
                    860:        }
                    861:        return(1);
                    862: }
                    863:
                    864: static void
                    865: post_bf(DECL_ARGS)
                    866: {
                    867:
                    868:        if (MDOC_BODY == n->type)
                    869:                font_pop();
                    870: }
                    871:
                    872: static int
1.13      schwarze  873: pre_bk(DECL_ARGS)
                    874: {
                    875:
                    876:        switch (n->type) {
                    877:        case (MDOC_BLOCK):
                    878:                return(1);
                    879:        case (MDOC_BODY):
1.20      schwarze  880:                outflags |= MMAN_Bk;
1.13      schwarze  881:                return(1);
                    882:        default:
                    883:                return(0);
                    884:        }
                    885: }
                    886:
                    887: static void
                    888: post_bk(DECL_ARGS)
                    889: {
                    890:
1.54      schwarze  891:        if (MDOC_BODY == n->type)
1.20      schwarze  892:                outflags &= ~MMAN_Bk;
1.3       schwarze  893: }
                    894:
                    895: static int
1.30      schwarze  896: pre_bl(DECL_ARGS)
                    897: {
1.33      schwarze  898:        size_t           icol;
1.30      schwarze  899:
1.49      schwarze  900:        /*
                    901:         * print_offs() will increase the -offset to account for
                    902:         * a possible enclosing .It, but any enclosed .It blocks
                    903:         * just nest and do not add up their indentation.
                    904:         */
                    905:        if (n->norm->Bl.offs) {
                    906:                print_offs(n->norm->Bl.offs);
                    907:                Bl_stack[Bl_stack_len++] = 0;
                    908:        }
                    909:
1.33      schwarze  910:        switch (n->norm->Bl.type) {
                    911:        case (LIST_enum):
1.30      schwarze  912:                n->norm->Bl.count = 0;
1.33      schwarze  913:                return(1);
                    914:        case (LIST_column):
                    915:                break;
                    916:        default:
                    917:                return(1);
                    918:        }
                    919:
1.36      schwarze  920:        print_line(".TS", MMAN_nl);
1.33      schwarze  921:        for (icol = 0; icol < n->norm->Bl.ncols; icol++)
                    922:                print_word("l");
                    923:        print_word(".");
1.36      schwarze  924:        outflags |= MMAN_nl;
1.30      schwarze  925:        return(1);
                    926: }
                    927:
                    928: static void
                    929: post_bl(DECL_ARGS)
                    930: {
                    931:
1.33      schwarze  932:        switch (n->norm->Bl.type) {
1.36      schwarze  933:        case (LIST_column):
                    934:                print_line(".TE", 0);
                    935:                break;
1.33      schwarze  936:        case (LIST_enum):
                    937:                n->norm->Bl.count = 0;
                    938:                break;
                    939:        default:
                    940:                break;
                    941:        }
1.49      schwarze  942:
                    943:        if (n->norm->Bl.offs) {
                    944:                print_line(".RE", MMAN_nl);
                    945:                assert(Bl_stack_len);
                    946:                Bl_stack_len--;
                    947:                assert(0 == Bl_stack[Bl_stack_len]);
                    948:        } else {
                    949:                outflags |= MMAN_PP | MMAN_nl;
                    950:                outflags &= ~(MMAN_sp | MMAN_br);
                    951:        }
1.47      schwarze  952:
                    953:        /* Maybe we are inside an enclosing list? */
                    954:        if (NULL != n->parent->next)
                    955:                mid_it();
                    956:
1.30      schwarze  957: }
                    958:
                    959: static int
1.3       schwarze  960: pre_br(DECL_ARGS)
                    961: {
                    962:
1.21      schwarze  963:        outflags |= MMAN_br;
1.3       schwarze  964:        return(0);
                    965: }
                    966:
                    967: static int
1.8       schwarze  968: pre_bx(DECL_ARGS)
                    969: {
                    970:
                    971:        n = n->child;
                    972:        if (n) {
1.20      schwarze  973:                print_word(n->string);
                    974:                outflags &= ~MMAN_spc;
1.8       schwarze  975:                n = n->next;
                    976:        }
1.20      schwarze  977:        print_word("BSD");
1.8       schwarze  978:        if (NULL == n)
                    979:                return(0);
1.20      schwarze  980:        outflags &= ~MMAN_spc;
                    981:        print_word("-");
                    982:        outflags &= ~MMAN_spc;
                    983:        print_word(n->string);
1.8       schwarze  984:        return(0);
                    985: }
                    986:
                    987: static int
1.1       schwarze  988: pre_dl(DECL_ARGS)
                    989: {
                    990:
1.47      schwarze  991:        print_offs("6n");
1.1       schwarze  992:        return(1);
                    993: }
                    994:
                    995: static void
                    996: post_dl(DECL_ARGS)
                    997: {
                    998:
1.36      schwarze  999:        print_line(".RE", MMAN_nl);
1.47      schwarze 1000:
                   1001:        /* Maybe we are inside an enclosing list? */
                   1002:        if (NULL != n->parent->next)
                   1003:                mid_it();
1.16      schwarze 1004: }
                   1005:
                   1006: static int
1.26      schwarze 1007: pre_em(DECL_ARGS)
                   1008: {
                   1009:
                   1010:        font_push('I');
                   1011:        return(1);
1.28      schwarze 1012: }
                   1013:
                   1014: static void
                   1015: post_eo(DECL_ARGS)
                   1016: {
                   1017:
                   1018:        if (MDOC_HEAD == n->type || MDOC_BODY == n->type)
                   1019:                outflags &= ~MMAN_spc;
1.26      schwarze 1020: }
                   1021:
                   1022: static int
1.17      schwarze 1023: pre_fa(DECL_ARGS)
                   1024: {
                   1025:
                   1026:        if (MDOC_Fa == n->tok)
                   1027:                n = n->child;
                   1028:
                   1029:        while (NULL != n) {
1.26      schwarze 1030:                font_push('I');
1.42      schwarze 1031:                print_node(meta, n);
1.26      schwarze 1032:                font_pop();
1.17      schwarze 1033:                if (NULL != (n = n->next))
1.20      schwarze 1034:                        print_word(",");
1.17      schwarze 1035:        }
                   1036:        return(0);
                   1037: }
                   1038:
                   1039: static void
                   1040: post_fa(DECL_ARGS)
                   1041: {
                   1042:
                   1043:        if (NULL != n->next && MDOC_Fa == n->next->tok)
1.20      schwarze 1044:                print_word(",");
1.29      schwarze 1045: }
                   1046:
                   1047: static int
                   1048: pre_fd(DECL_ARGS)
                   1049: {
                   1050:
                   1051:        pre_syn(n);
                   1052:        font_push('B');
                   1053:        return(1);
                   1054: }
                   1055:
                   1056: static void
                   1057: post_fd(DECL_ARGS)
                   1058: {
                   1059:
                   1060:        font_pop();
                   1061:        outflags |= MMAN_br;
1.17      schwarze 1062: }
                   1063:
                   1064: static int
1.26      schwarze 1065: pre_fl(DECL_ARGS)
                   1066: {
                   1067:
                   1068:        font_push('B');
1.45      schwarze 1069:        print_word("\\-");
1.26      schwarze 1070:        outflags &= ~MMAN_spc;
                   1071:        return(1);
                   1072: }
                   1073:
                   1074: static void
                   1075: post_fl(DECL_ARGS)
                   1076: {
                   1077:
                   1078:        font_pop();
1.27      schwarze 1079:        if (0 == n->nchild && NULL != n->next &&
                   1080:                        n->next->line == n->line)
1.26      schwarze 1081:                outflags &= ~MMAN_spc;
                   1082: }
                   1083:
                   1084: static int
1.16      schwarze 1085: pre_fn(DECL_ARGS)
                   1086: {
                   1087:
1.23      schwarze 1088:        pre_syn(n);
                   1089:
1.16      schwarze 1090:        n = n->child;
                   1091:        if (NULL == n)
                   1092:                return(0);
                   1093:
1.55    ! schwarze 1094:        if (MDOC_SYNPRETTY & n->flags)
        !          1095:                print_block(".HP 4n", MMAN_nl);
        !          1096:
1.26      schwarze 1097:        font_push('B');
1.42      schwarze 1098:        print_node(meta, n);
1.26      schwarze 1099:        font_pop();
1.20      schwarze 1100:        outflags &= ~MMAN_spc;
1.26      schwarze 1101:        print_word("(");
1.20      schwarze 1102:        outflags &= ~MMAN_spc;
1.35      schwarze 1103:
                   1104:        n = n->next;
                   1105:        if (NULL != n)
1.42      schwarze 1106:                pre_fa(meta, n);
1.35      schwarze 1107:        return(0);
1.16      schwarze 1108: }
                   1109:
                   1110: static void
                   1111: post_fn(DECL_ARGS)
                   1112: {
                   1113:
1.20      schwarze 1114:        print_word(")");
1.16      schwarze 1115:        if (MDOC_SYNPRETTY & n->flags) {
1.20      schwarze 1116:                print_word(";");
1.55    ! schwarze 1117:                outflags |= MMAN_PP;
1.17      schwarze 1118:        }
                   1119: }
                   1120:
                   1121: static int
                   1122: pre_fo(DECL_ARGS)
                   1123: {
                   1124:
                   1125:        switch (n->type) {
1.23      schwarze 1126:        case (MDOC_BLOCK):
                   1127:                pre_syn(n);
                   1128:                break;
1.17      schwarze 1129:        case (MDOC_HEAD):
1.26      schwarze 1130:                font_push('B');
1.17      schwarze 1131:                break;
                   1132:        case (MDOC_BODY):
1.20      schwarze 1133:                outflags &= ~MMAN_spc;
                   1134:                print_word("(");
                   1135:                outflags &= ~MMAN_spc;
1.17      schwarze 1136:                break;
                   1137:        default:
                   1138:                break;
                   1139:        }
                   1140:        return(1);
                   1141: }
                   1142:
                   1143: static void
                   1144: post_fo(DECL_ARGS)
                   1145: {
                   1146:
                   1147:        switch (n->type) {
                   1148:        case (MDOC_HEAD):
1.26      schwarze 1149:                font_pop();
1.17      schwarze 1150:                break;
                   1151:        case (MDOC_BODY):
1.42      schwarze 1152:                post_fn(meta, n);
1.17      schwarze 1153:                break;
                   1154:        default:
                   1155:                break;
1.16      schwarze 1156:        }
1.15      schwarze 1157: }
                   1158:
                   1159: static int
1.23      schwarze 1160: pre_ft(DECL_ARGS)
                   1161: {
                   1162:
                   1163:        pre_syn(n);
1.26      schwarze 1164:        font_push('I');
1.23      schwarze 1165:        return(1);
                   1166: }
                   1167:
                   1168: static int
1.15      schwarze 1169: pre_in(DECL_ARGS)
                   1170: {
                   1171:
                   1172:        if (MDOC_SYNPRETTY & n->flags) {
1.23      schwarze 1173:                pre_syn(n);
1.26      schwarze 1174:                font_push('B');
                   1175:                print_word("#include <");
                   1176:                outflags &= ~MMAN_spc;
                   1177:        } else {
                   1178:                print_word("<");
                   1179:                outflags &= ~MMAN_spc;
                   1180:                font_push('I');
                   1181:        }
1.15      schwarze 1182:        return(1);
                   1183: }
                   1184:
                   1185: static void
                   1186: post_in(DECL_ARGS)
                   1187: {
                   1188:
                   1189:        if (MDOC_SYNPRETTY & n->flags) {
1.26      schwarze 1190:                outflags &= ~MMAN_spc;
                   1191:                print_word(">");
                   1192:                font_pop();
1.21      schwarze 1193:                outflags |= MMAN_br;
1.26      schwarze 1194:        } else {
                   1195:                font_pop();
                   1196:                outflags &= ~MMAN_spc;
                   1197:                print_word(">");
                   1198:        }
1.1       schwarze 1199: }
                   1200:
                   1201: static int
                   1202: pre_it(DECL_ARGS)
                   1203: {
                   1204:        const struct mdoc_node *bln;
                   1205:
1.30      schwarze 1206:        switch (n->type) {
                   1207:        case (MDOC_HEAD):
1.36      schwarze 1208:                outflags |= MMAN_PP | MMAN_nl;
1.30      schwarze 1209:                bln = n->parent->parent;
1.36      schwarze 1210:                if (0 == bln->norm->Bl.comp ||
1.39      schwarze 1211:                    (NULL == n->parent->prev &&
                   1212:                     NULL == bln->parent->prev))
1.36      schwarze 1213:                        outflags |= MMAN_sp;
                   1214:                outflags &= ~MMAN_br;
1.3       schwarze 1215:                switch (bln->norm->Bl.type) {
1.30      schwarze 1216:                case (LIST_item):
                   1217:                        return(0);
                   1218:                case (LIST_inset):
                   1219:                        /* FALLTHROUGH */
                   1220:                case (LIST_diag):
                   1221:                        /* FALLTHROUGH */
                   1222:                case (LIST_ohang):
                   1223:                        if (bln->norm->Bl.type == LIST_diag)
1.36      schwarze 1224:                                print_line(".B \"", 0);
1.30      schwarze 1225:                        else
1.36      schwarze 1226:                                print_line(".R \"", 0);
1.30      schwarze 1227:                        outflags &= ~MMAN_spc;
                   1228:                        return(1);
1.3       schwarze 1229:                case (LIST_bullet):
1.30      schwarze 1230:                        /* FALLTHROUGH */
                   1231:                case (LIST_dash):
                   1232:                        /* FALLTHROUGH */
                   1233:                case (LIST_hyphen):
1.35      schwarze 1234:                        print_width(bln->norm->Bl.width, NULL, 0);
1.37      schwarze 1235:                        TPremain = 0;
1.30      schwarze 1236:                        outflags |= MMAN_nl;
                   1237:                        font_push('B');
                   1238:                        if (LIST_bullet == bln->norm->Bl.type)
                   1239:                                print_word("o");
                   1240:                        else
                   1241:                                print_word("-");
                   1242:                        font_pop();
                   1243:                        break;
                   1244:                case (LIST_enum):
1.35      schwarze 1245:                        print_width(bln->norm->Bl.width, NULL, 0);
1.37      schwarze 1246:                        TPremain = 0;
1.30      schwarze 1247:                        outflags |= MMAN_nl;
                   1248:                        print_count(&bln->norm->Bl.count);
1.3       schwarze 1249:                        break;
1.31      schwarze 1250:                case (LIST_hang):
1.35      schwarze 1251:                        print_width(bln->norm->Bl.width, n->child, 6);
1.37      schwarze 1252:                        TPremain = 0;
1.32      schwarze 1253:                        break;
                   1254:                case (LIST_tag):
1.37      schwarze 1255:                        print_width(bln->norm->Bl.width, n->child, 0);
                   1256:                        putchar('\n');
                   1257:                        outflags &= ~MMAN_spc;
                   1258:                        return(1);
1.3       schwarze 1259:                default:
1.32      schwarze 1260:                        return(1);
1.3       schwarze 1261:                }
1.20      schwarze 1262:                outflags |= MMAN_nl;
1.30      schwarze 1263:        default:
                   1264:                break;
1.1       schwarze 1265:        }
                   1266:        return(1);
1.30      schwarze 1267: }
                   1268:
1.47      schwarze 1269: /*
                   1270:  * This function is called after closing out an indented block.
                   1271:  * If we are inside an enclosing list, restore its indentation.
                   1272:  */
                   1273: static void
                   1274: mid_it(void)
                   1275: {
                   1276:        char             buf[24];
                   1277:
                   1278:        /* Nothing to do outside a list. */
                   1279:        if (0 == Bl_stack_len || 0 == Bl_stack[Bl_stack_len - 1])
                   1280:                return;
                   1281:
                   1282:        /* The indentation has already been set up. */
                   1283:        if (Bl_stack_post[Bl_stack_len - 1])
                   1284:                return;
                   1285:
                   1286:        /* Restore the indentation of the enclosing list. */
                   1287:        print_line(".RS", MMAN_Bk_susp);
1.53      schwarze 1288:        snprintf(buf, sizeof(buf), "%zun", Bl_stack[Bl_stack_len - 1]);
1.47      schwarze 1289:        print_word(buf);
                   1290:
                   1291:        /* Remeber to close out this .RS block later. */
                   1292:        Bl_stack_post[Bl_stack_len - 1] = 1;
                   1293: }
                   1294:
1.30      schwarze 1295: static void
                   1296: post_it(DECL_ARGS)
                   1297: {
                   1298:        const struct mdoc_node *bln;
                   1299:
1.33      schwarze 1300:        bln = n->parent->parent;
                   1301:
                   1302:        switch (n->type) {
                   1303:        case (MDOC_HEAD):
1.30      schwarze 1304:                switch (bln->norm->Bl.type) {
                   1305:                case (LIST_diag):
                   1306:                        outflags &= ~MMAN_spc;
                   1307:                        print_word("\\ ");
                   1308:                        break;
                   1309:                case (LIST_ohang):
                   1310:                        outflags |= MMAN_br;
                   1311:                        break;
                   1312:                default:
                   1313:                        break;
                   1314:                }
1.33      schwarze 1315:                break;
                   1316:        case (MDOC_BODY):
1.43      schwarze 1317:                switch (bln->norm->Bl.type) {
                   1318:                case (LIST_bullet):
                   1319:                        /* FALLTHROUGH */
                   1320:                case (LIST_dash):
                   1321:                        /* FALLTHROUGH */
                   1322:                case (LIST_hyphen):
                   1323:                        /* FALLTHROUGH */
                   1324:                case (LIST_enum):
                   1325:                        /* FALLTHROUGH */
                   1326:                case (LIST_hang):
                   1327:                        /* FALLTHROUGH */
                   1328:                case (LIST_tag):
                   1329:                        assert(Bl_stack_len);
                   1330:                        Bl_stack[--Bl_stack_len] = 0;
                   1331:
                   1332:                        /*
                   1333:                         * Our indentation had to be restored
1.47      schwarze 1334:                         * after a child display or child list.
1.43      schwarze 1335:                         * Close out that indentation block now.
                   1336:                         */
                   1337:                        if (Bl_stack_post[Bl_stack_len]) {
                   1338:                                print_line(".RE", MMAN_nl);
                   1339:                                Bl_stack_post[Bl_stack_len] = 0;
                   1340:                        }
                   1341:                        break;
                   1342:                case (LIST_column):
                   1343:                        if (NULL != n->next) {
                   1344:                                putchar('\t');
                   1345:                                outflags &= ~MMAN_spc;
                   1346:                        }
                   1347:                        break;
                   1348:                default:
                   1349:                        break;
1.33      schwarze 1350:                }
                   1351:                break;
                   1352:        default:
                   1353:                break;
1.30      schwarze 1354:        }
1.14      schwarze 1355: }
                   1356:
                   1357: static void
                   1358: post_lb(DECL_ARGS)
                   1359: {
                   1360:
1.21      schwarze 1361:        if (SEC_LIBRARY == n->sec)
                   1362:                outflags |= MMAN_br;
1.24      schwarze 1363: }
                   1364:
                   1365: static int
                   1366: pre_lk(DECL_ARGS)
                   1367: {
                   1368:        const struct mdoc_node *link, *descr;
                   1369:
                   1370:        if (NULL == (link = n->child))
                   1371:                return(0);
                   1372:
                   1373:        if (NULL != (descr = link->next)) {
1.26      schwarze 1374:                font_push('I');
1.24      schwarze 1375:                while (NULL != descr) {
                   1376:                        print_word(descr->string);
                   1377:                        descr = descr->next;
                   1378:                }
                   1379:                print_word(":");
1.26      schwarze 1380:                font_pop();
1.24      schwarze 1381:        }
                   1382:
1.26      schwarze 1383:        font_push('B');
1.24      schwarze 1384:        print_word(link->string);
1.26      schwarze 1385:        font_pop();
1.24      schwarze 1386:        return(0);
1.1       schwarze 1387: }
                   1388:
                   1389: static int
1.26      schwarze 1390: pre_li(DECL_ARGS)
                   1391: {
                   1392:
                   1393:        font_push('R');
                   1394:        return(1);
                   1395: }
                   1396:
                   1397: static int
1.1       schwarze 1398: pre_nm(DECL_ARGS)
                   1399: {
1.38      schwarze 1400:        char    *name;
1.1       schwarze 1401:
1.54      schwarze 1402:        if (MDOC_BLOCK == n->type) {
                   1403:                outflags |= MMAN_Bk;
1.23      schwarze 1404:                pre_syn(n);
1.54      schwarze 1405:        }
1.1       schwarze 1406:        if (MDOC_ELEM != n->type && MDOC_HEAD != n->type)
                   1407:                return(1);
1.42      schwarze 1408:        name = n->child ? n->child->string : meta->name;
1.38      schwarze 1409:        if (NULL == name)
1.23      schwarze 1410:                return(0);
1.38      schwarze 1411:        if (MDOC_HEAD == n->type) {
                   1412:                if (NULL == n->parent->prev)
                   1413:                        outflags |= MMAN_sp;
                   1414:                print_block(".HP", 0);
1.53      schwarze 1415:                printf(" %zun", strlen(name) + 1);
1.38      schwarze 1416:                outflags |= MMAN_nl;
                   1417:        }
1.26      schwarze 1418:        font_push('B');
1.1       schwarze 1419:        if (NULL == n->child)
1.42      schwarze 1420:                print_word(meta->name);
1.1       schwarze 1421:        return(1);
                   1422: }
                   1423:
                   1424: static void
                   1425: post_nm(DECL_ARGS)
                   1426: {
                   1427:
1.54      schwarze 1428:        switch (n->type) {
                   1429:        case (MDOC_BLOCK):
                   1430:                outflags &= ~MMAN_Bk;
                   1431:                break;
                   1432:        case (MDOC_HEAD):
                   1433:                /* FALLTHROUGH */
                   1434:        case (MDOC_ELEM):
                   1435:                font_pop();
                   1436:                break;
                   1437:        default:
                   1438:                break;
                   1439:        }
1.25      schwarze 1440: }
                   1441:
                   1442: static int
                   1443: pre_no(DECL_ARGS)
                   1444: {
                   1445:
                   1446:        outflags |= MMAN_spc_force;
                   1447:        return(1);
1.1       schwarze 1448: }
                   1449:
                   1450: static int
                   1451: pre_ns(DECL_ARGS)
                   1452: {
                   1453:
1.20      schwarze 1454:        outflags &= ~MMAN_spc;
1.1       schwarze 1455:        return(0);
                   1456: }
                   1457:
1.3       schwarze 1458: static void
                   1459: post_pf(DECL_ARGS)
                   1460: {
                   1461:
1.20      schwarze 1462:        outflags &= ~MMAN_spc;
1.3       schwarze 1463: }
                   1464:
1.1       schwarze 1465: static int
                   1466: pre_pp(DECL_ARGS)
                   1467: {
                   1468:
1.36      schwarze 1469:        if (MDOC_It != n->parent->tok)
                   1470:                outflags |= MMAN_PP;
                   1471:        outflags |= MMAN_sp | MMAN_nl;
                   1472:        outflags &= ~MMAN_br;
1.34      schwarze 1473:        return(0);
                   1474: }
                   1475:
                   1476: static int
                   1477: pre_rs(DECL_ARGS)
                   1478: {
                   1479:
                   1480:        if (SEC_SEE_ALSO == n->sec) {
1.36      schwarze 1481:                outflags |= MMAN_PP | MMAN_sp | MMAN_nl;
                   1482:                outflags &= ~MMAN_br;
1.34      schwarze 1483:        }
                   1484:        return(1);
1.12      schwarze 1485: }
                   1486:
                   1487: static int
                   1488: pre_sm(DECL_ARGS)
                   1489: {
                   1490:
                   1491:        assert(n->child && MDOC_TEXT == n->child->type);
                   1492:        if (0 == strcmp("on", n->child->string))
1.27      schwarze 1493:                outflags |= MMAN_Sm | MMAN_spc;
1.12      schwarze 1494:        else
1.20      schwarze 1495:                outflags &= ~MMAN_Sm;
1.12      schwarze 1496:        return(0);
1.1       schwarze 1497: }
                   1498:
                   1499: static int
1.3       schwarze 1500: pre_sp(DECL_ARGS)
1.1       schwarze 1501: {
                   1502:
1.41      schwarze 1503:        if (MMAN_PP & outflags) {
                   1504:                outflags &= ~MMAN_PP;
1.40      schwarze 1505:                print_line(".PP", 0);
1.41      schwarze 1506:        } else
1.40      schwarze 1507:                print_line(".sp", 0);
1.1       schwarze 1508:        return(1);
                   1509: }
                   1510:
                   1511: static void
1.3       schwarze 1512: post_sp(DECL_ARGS)
1.1       schwarze 1513: {
                   1514:
1.20      schwarze 1515:        outflags |= MMAN_nl;
1.18      schwarze 1516: }
                   1517:
                   1518: static int
1.26      schwarze 1519: pre_sy(DECL_ARGS)
                   1520: {
                   1521:
                   1522:        font_push('B');
                   1523:        return(1);
                   1524: }
                   1525:
                   1526: static int
1.18      schwarze 1527: pre_vt(DECL_ARGS)
                   1528: {
                   1529:
                   1530:        if (MDOC_SYNPRETTY & n->flags) {
                   1531:                switch (n->type) {
                   1532:                case (MDOC_BLOCK):
1.23      schwarze 1533:                        pre_syn(n);
1.18      schwarze 1534:                        return(1);
                   1535:                case (MDOC_BODY):
                   1536:                        break;
                   1537:                default:
                   1538:                        return(0);
                   1539:                }
                   1540:        }
1.26      schwarze 1541:        font_push('I');
1.18      schwarze 1542:        return(1);
                   1543: }
                   1544:
                   1545: static void
                   1546: post_vt(DECL_ARGS)
                   1547: {
                   1548:
1.19      schwarze 1549:        if (MDOC_SYNPRETTY & n->flags && MDOC_BODY != n->type)
1.18      schwarze 1550:                return;
1.26      schwarze 1551:        font_pop();
1.1       schwarze 1552: }
                   1553:
                   1554: static int
                   1555: pre_xr(DECL_ARGS)
                   1556: {
                   1557:
                   1558:        n = n->child;
                   1559:        if (NULL == n)
                   1560:                return(0);
1.42      schwarze 1561:        print_node(meta, n);
1.1       schwarze 1562:        n = n->next;
                   1563:        if (NULL == n)
                   1564:                return(0);
1.20      schwarze 1565:        outflags &= ~MMAN_spc;
                   1566:        print_word("(");
1.42      schwarze 1567:        print_node(meta, n);
1.20      schwarze 1568:        print_word(")");
1.1       schwarze 1569:        return(0);
1.8       schwarze 1570: }
                   1571:
                   1572: static int
                   1573: pre_ux(DECL_ARGS)
                   1574: {
                   1575:
1.20      schwarze 1576:        print_word(manacts[n->tok].prefix);
1.8       schwarze 1577:        if (NULL == n->child)
                   1578:                return(0);
1.20      schwarze 1579:        outflags &= ~MMAN_spc;
1.45      schwarze 1580:        print_word("\\ ");
1.20      schwarze 1581:        outflags &= ~MMAN_spc;
1.8       schwarze 1582:        return(1);
1.1       schwarze 1583: }

CVSweb