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

Annotation of mandoc/mdoc_man.c, Revision 1.45

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

CVSweb