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

Annotation of mandoc/mdoc_man.c, Revision 1.44

1.44    ! schwarze    1: /*     $Id: mdoc_man.c,v 1.43 2012/11/18 18:02:23 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.20      schwarze  337:                        if (MMAN_Bk & outflags) {
1.13      schwarze  338:                                putchar('\\');
                    339:                                putchar('~');
                    340:                        } else
                    341:                                putchar(' ');
1.37      schwarze  342:                        if (TPremain)
                    343:                                TPremain--;
1.13      schwarze  344:                }
1.37      schwarze  345:        }
1.5       kristaps  346:
                    347:        /*
                    348:         * Reassign needing space if we're not following opening
                    349:         * punctuation.
                    350:         */
1.37      schwarze  351:        if (MMAN_Sm & outflags && ('\0' == s[0] ||
                    352:            (('(' != s[0] && '[' != s[0]) || '\0' != s[1])))
1.20      schwarze  353:                outflags |= MMAN_spc;
                    354:        else
                    355:                outflags &= ~MMAN_spc;
1.25      schwarze  356:        outflags &= ~MMAN_spc_force;
1.5       kristaps  357:
1.1       schwarze  358:        for ( ; *s; s++) {
                    359:                switch (*s) {
                    360:                case (ASCII_NBRSP):
                    361:                        printf("\\~");
                    362:                        break;
                    363:                case (ASCII_HYPH):
                    364:                        putchar('-');
                    365:                        break;
                    366:                default:
1.5       kristaps  367:                        putchar((unsigned char)*s);
1.1       schwarze  368:                        break;
                    369:                }
1.37      schwarze  370:                if (TPremain)
                    371:                        TPremain--;
1.1       schwarze  372:        }
1.4       kristaps  373: }
                    374:
1.11      schwarze  375: static void
1.36      schwarze  376: print_line(const char *s, int newflags)
                    377: {
                    378:
                    379:        outflags &= ~MMAN_br;
                    380:        outflags |= MMAN_nl;
                    381:        print_word(s);
                    382:        outflags |= newflags;
                    383: }
                    384:
                    385: static void
                    386: print_block(const char *s, int newflags)
                    387: {
                    388:
                    389:        outflags &= ~MMAN_PP;
1.44    ! schwarze  390:        if (MMAN_sp & outflags) {
1.36      schwarze  391:                outflags &= ~(MMAN_sp | MMAN_br);
1.44    ! schwarze  392:                if (MMAN_PD & outflags) {
        !           393:                        print_line(".PD", 0);
        !           394:                        outflags &= ~MMAN_PD;
        !           395:                }
        !           396:        } else if (! (MMAN_PD & outflags)) {
        !           397:                print_line(".PD 0", 0);
        !           398:                outflags |= MMAN_PD;
        !           399:        }
1.36      schwarze  400:        outflags |= MMAN_nl;
                    401:        print_word(s);
                    402:        outflags |= newflags;
                    403: }
                    404:
                    405: static void
1.20      schwarze  406: print_offs(const char *v)
1.11      schwarze  407: {
                    408:        char              buf[24];
                    409:        struct roffsu     su;
                    410:        size_t            sz;
                    411:
1.43      schwarze  412:        /* Convert v into a number (of characters). */
1.11      schwarze  413:        if (NULL == v || '\0' == *v || 0 == strcmp(v, "left"))
                    414:                sz = 0;
                    415:        else if (0 == strcmp(v, "indent"))
                    416:                sz = 6;
                    417:        else if (0 == strcmp(v, "indent-two"))
                    418:                sz = 12;
                    419:        else if (a2roffsu(v, &su, SCALE_MAX)) {
1.43      schwarze  420:                if (SCALE_EN == su.unit)
                    421:                        sz = su.scale;
                    422:                else {
                    423:                        /*
                    424:                         * XXX
                    425:                         * If we are inside an enclosing list,
                    426:                         * there is no easy way to add the two
                    427:                         * indentations because they are provided
                    428:                         * in terms of different units.
                    429:                         */
                    430:                        print_word(v);
                    431:                        return;
                    432:                }
1.11      schwarze  433:        } else
                    434:                sz = strlen(v);
                    435:
1.43      schwarze  436:        /*
                    437:         * We are inside an enclosing list.
                    438:         * Add the two indentations.
                    439:         */
                    440:        if (Bl_stack_len)
                    441:                sz += Bl_stack[Bl_stack_len - 1];
                    442:
1.11      schwarze  443:        snprintf(buf, sizeof(buf), "%ldn", sz);
1.20      schwarze  444:        print_word(buf);
1.11      schwarze  445: }
                    446:
1.4       kristaps  447: void
1.35      schwarze  448: print_width(const char *v, const struct mdoc_node *child, size_t defsz)
1.30      schwarze  449: {
                    450:        char              buf[24];
                    451:        struct roffsu     su;
1.31      schwarze  452:        size_t            sz, chsz;
1.37      schwarze  453:        int               numeric, remain;
1.31      schwarze  454:
1.37      schwarze  455:        numeric = 1;
                    456:        remain = 0;
1.43      schwarze  457:
                    458:        /* Convert v into a number (of characters). */
1.35      schwarze  459:        if (NULL == v)
                    460:                sz = defsz;
                    461:        else if (a2roffsu(v, &su, SCALE_MAX)) {
1.30      schwarze  462:                if (SCALE_EN == su.unit)
                    463:                        sz = su.scale;
                    464:                else {
1.37      schwarze  465:                        sz = 0;
                    466:                        numeric = 0;
1.30      schwarze  467:                }
                    468:        } else
                    469:                sz = strlen(v);
                    470:
1.37      schwarze  471:        /* XXX Rough estimation, might have multiple parts. */
                    472:        chsz = (NULL != child && MDOC_TEXT == child->type) ?
                    473:                        strlen(child->string) : 0;
                    474:
1.43      schwarze  475:        /*
                    476:         * If we are inside an enclosing list,
                    477:         * preserve its indentation.
                    478:         */
                    479:        if (Bl_stack_len && Bl_stack[Bl_stack_len - 1]) {
                    480:                print_line(".RS", 0);
                    481:                snprintf(buf, sizeof(buf), "%ldn",
                    482:                                Bl_stack[Bl_stack_len - 1]);
                    483:                print_word(buf);
                    484:        }
                    485:
                    486:        /*
                    487:         * Save our own indentation,
                    488:         * such that child lists can use it.
                    489:         */
                    490:        Bl_stack[Bl_stack_len++] = sz + 2;
                    491:
                    492:        /* Set up the current list. */
1.37      schwarze  493:        if (defsz && chsz > sz)
1.36      schwarze  494:                print_block(".HP", 0);
1.37      schwarze  495:        else {
1.36      schwarze  496:                print_block(".TP", 0);
1.37      schwarze  497:                remain = sz + 2;
                    498:        }
                    499:        if (numeric) {
                    500:                snprintf(buf, sizeof(buf), "%ldn", sz + 2);
                    501:                print_word(buf);
                    502:        } else
                    503:                print_word(v);
                    504:        TPremain = remain;
1.30      schwarze  505: }
                    506:
                    507: void
                    508: print_count(int *count)
                    509: {
                    510:        char              buf[12];
                    511:
                    512:        snprintf(buf, sizeof(buf), "%d.", ++*count);
                    513:        print_word(buf);
                    514: }
                    515:
                    516: void
1.4       kristaps  517: man_man(void *arg, const struct man *man)
                    518: {
                    519:
1.5       kristaps  520:        /*
                    521:         * Dump the keep buffer.
                    522:         * We're guaranteed by now that this exists (is non-NULL).
                    523:         * Flush stdout afterward, just in case.
                    524:         */
1.4       kristaps  525:        fputs(mparse_getkeep(man_mparse(man)), stdout);
1.5       kristaps  526:        fflush(stdout);
1.1       schwarze  527: }
                    528:
                    529: void
                    530: man_mdoc(void *arg, const struct mdoc *mdoc)
                    531: {
1.42      schwarze  532:        const struct mdoc_meta *meta;
1.1       schwarze  533:        const struct mdoc_node *n;
                    534:
1.42      schwarze  535:        meta = mdoc_meta(mdoc);
1.1       schwarze  536:        n = mdoc_node(mdoc);
                    537:
1.3       schwarze  538:        printf(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"",
1.42      schwarze  539:                        meta->title, meta->msec, meta->date,
                    540:                        meta->os, meta->vol);
1.1       schwarze  541:
1.20      schwarze  542:        outflags = MMAN_nl | MMAN_Sm;
1.26      schwarze  543:        if (0 == fontqueue.size) {
                    544:                fontqueue.size = 8;
                    545:                fontqueue.head = fontqueue.tail = mandoc_malloc(8);
                    546:                *fontqueue.tail = 'R';
                    547:        }
1.42      schwarze  548:        print_node(meta, n);
1.3       schwarze  549:        putchar('\n');
1.1       schwarze  550: }
                    551:
                    552: static void
                    553: print_node(DECL_ARGS)
                    554: {
                    555:        const struct mdoc_node  *prev, *sub;
1.5       kristaps  556:        const struct manact     *act;
1.1       schwarze  557:        int                      cond, do_sub;
1.5       kristaps  558:
                    559:        /*
                    560:         * Break the line if we were parsed subsequent the current node.
                    561:         * This makes the page structure be more consistent.
                    562:         */
1.1       schwarze  563:        prev = n->prev ? n->prev : n->parent;
1.27      schwarze  564:        if (MMAN_spc & outflags && prev && prev->line < n->line)
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:
                    708:        if (MDOC_HEAD != n->type)
                    709:                return(1);
1.36      schwarze  710:        outflags |= MMAN_sp;
                    711:        print_block(manacts[n->tok].prefix, 0);
1.37      schwarze  712:        print_word("");
                    713:        putchar('\"');
1.20      schwarze  714:        outflags &= ~MMAN_spc;
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.36      schwarze  819:        print_line(".RS", 0);
1.20      schwarze  820:        print_offs(n->norm->Bd.offs);
                    821:        outflags |= MMAN_nl;
1.3       schwarze  822:        return(1);
                    823: }
                    824:
                    825: static void
                    826: post_bd(DECL_ARGS)
                    827: {
1.43      schwarze  828:        char             buf[24];
1.3       schwarze  829:
1.43      schwarze  830:        /* Close out this display. */
1.36      schwarze  831:        print_line(".RE", MMAN_nl);
1.3       schwarze  832:        if (DISP_unfilled == n->norm->Bd.type ||
1.36      schwarze  833:            DISP_literal  == n->norm->Bd.type)
                    834:                print_line(".fi", MMAN_nl);
1.43      schwarze  835:
                    836:        /*
                    837:         * If we are inside an enclosing list and the current
                    838:         * list item is not yet finished, restore the correct
                    839:         * indentation for what remains of that item.
                    840:         */
                    841:        if (NULL != n->parent->next &&
                    842:            Bl_stack_len && Bl_stack[Bl_stack_len - 1]) {
                    843:                print_line(".RS", 0);
                    844:                snprintf(buf, sizeof(buf), "%ldn",
                    845:                                Bl_stack[Bl_stack_len - 1]);
                    846:                print_word(buf);
                    847:                /* Remeber to close out this .RS block later. */
                    848:                Bl_stack_post[Bl_stack_len - 1] = 1;
                    849:        }
1.13      schwarze  850: }
                    851:
                    852: static int
1.27      schwarze  853: pre_bf(DECL_ARGS)
                    854: {
                    855:
                    856:        switch (n->type) {
                    857:        case (MDOC_BLOCK):
                    858:                return(1);
                    859:        case (MDOC_BODY):
                    860:                break;
                    861:        default:
                    862:                return(0);
                    863:        }
                    864:        switch (n->norm->Bf.font) {
                    865:        case (FONT_Em):
                    866:                font_push('I');
                    867:                break;
                    868:        case (FONT_Sy):
                    869:                font_push('B');
                    870:                break;
                    871:        default:
                    872:                font_push('R');
                    873:                break;
                    874:        }
                    875:        return(1);
                    876: }
                    877:
                    878: static void
                    879: post_bf(DECL_ARGS)
                    880: {
                    881:
                    882:        if (MDOC_BODY == n->type)
                    883:                font_pop();
                    884: }
                    885:
                    886: static int
1.13      schwarze  887: pre_bk(DECL_ARGS)
                    888: {
                    889:
                    890:        switch (n->type) {
                    891:        case (MDOC_BLOCK):
                    892:                return(1);
                    893:        case (MDOC_BODY):
1.20      schwarze  894:                outflags |= MMAN_Bk;
1.13      schwarze  895:                return(1);
                    896:        default:
                    897:                return(0);
                    898:        }
                    899: }
                    900:
                    901: static void
                    902: post_bk(DECL_ARGS)
                    903: {
                    904:
                    905:        if (MDOC_BODY == n->type)
1.20      schwarze  906:                outflags &= ~MMAN_Bk;
1.3       schwarze  907: }
                    908:
                    909: static int
1.30      schwarze  910: pre_bl(DECL_ARGS)
                    911: {
1.33      schwarze  912:        size_t           icol;
1.30      schwarze  913:
1.33      schwarze  914:        switch (n->norm->Bl.type) {
                    915:        case (LIST_enum):
1.30      schwarze  916:                n->norm->Bl.count = 0;
1.33      schwarze  917:                return(1);
                    918:        case (LIST_column):
                    919:                break;
                    920:        default:
                    921:                return(1);
                    922:        }
                    923:
1.36      schwarze  924:        print_line(".TS", MMAN_nl);
1.33      schwarze  925:        for (icol = 0; icol < n->norm->Bl.ncols; icol++)
                    926:                print_word("l");
                    927:        print_word(".");
1.36      schwarze  928:        outflags |= MMAN_nl;
1.30      schwarze  929:        return(1);
                    930: }
                    931:
                    932: static void
                    933: post_bl(DECL_ARGS)
                    934: {
                    935:
1.33      schwarze  936:        switch (n->norm->Bl.type) {
1.36      schwarze  937:        case (LIST_column):
                    938:                print_line(".TE", 0);
                    939:                break;
1.33      schwarze  940:        case (LIST_enum):
                    941:                n->norm->Bl.count = 0;
                    942:                break;
                    943:        default:
                    944:                break;
                    945:        }
1.36      schwarze  946:        outflags |= MMAN_PP | MMAN_nl;
                    947:        outflags &= ~(MMAN_sp | MMAN_br);
1.30      schwarze  948: }
                    949:
                    950: static int
1.3       schwarze  951: pre_br(DECL_ARGS)
                    952: {
                    953:
1.21      schwarze  954:        outflags |= MMAN_br;
1.3       schwarze  955:        return(0);
                    956: }
                    957:
                    958: static int
1.8       schwarze  959: pre_bx(DECL_ARGS)
                    960: {
                    961:
                    962:        n = n->child;
                    963:        if (n) {
1.20      schwarze  964:                print_word(n->string);
                    965:                outflags &= ~MMAN_spc;
1.8       schwarze  966:                n = n->next;
                    967:        }
1.20      schwarze  968:        print_word("BSD");
1.8       schwarze  969:        if (NULL == n)
                    970:                return(0);
1.20      schwarze  971:        outflags &= ~MMAN_spc;
                    972:        print_word("-");
                    973:        outflags &= ~MMAN_spc;
                    974:        print_word(n->string);
1.8       schwarze  975:        return(0);
                    976: }
                    977:
                    978: static int
1.1       schwarze  979: pre_dl(DECL_ARGS)
                    980: {
                    981:
1.36      schwarze  982:        print_line(".RS 6n", MMAN_nl);
1.1       schwarze  983:        return(1);
                    984: }
                    985:
                    986: static void
                    987: post_dl(DECL_ARGS)
                    988: {
                    989:
1.36      schwarze  990:        print_line(".RE", MMAN_nl);
1.16      schwarze  991: }
                    992:
                    993: static int
1.26      schwarze  994: pre_em(DECL_ARGS)
                    995: {
                    996:
                    997:        font_push('I');
                    998:        return(1);
1.28      schwarze  999: }
                   1000:
                   1001: static void
                   1002: post_eo(DECL_ARGS)
                   1003: {
                   1004:
                   1005:        if (MDOC_HEAD == n->type || MDOC_BODY == n->type)
                   1006:                outflags &= ~MMAN_spc;
1.26      schwarze 1007: }
                   1008:
                   1009: static int
1.17      schwarze 1010: pre_fa(DECL_ARGS)
                   1011: {
                   1012:
                   1013:        if (MDOC_Fa == n->tok)
                   1014:                n = n->child;
                   1015:
                   1016:        while (NULL != n) {
1.26      schwarze 1017:                font_push('I');
1.42      schwarze 1018:                print_node(meta, n);
1.26      schwarze 1019:                font_pop();
1.17      schwarze 1020:                if (NULL != (n = n->next))
1.20      schwarze 1021:                        print_word(",");
1.17      schwarze 1022:        }
                   1023:        return(0);
                   1024: }
                   1025:
                   1026: static void
                   1027: post_fa(DECL_ARGS)
                   1028: {
                   1029:
                   1030:        if (NULL != n->next && MDOC_Fa == n->next->tok)
1.20      schwarze 1031:                print_word(",");
1.29      schwarze 1032: }
                   1033:
                   1034: static int
                   1035: pre_fd(DECL_ARGS)
                   1036: {
                   1037:
                   1038:        pre_syn(n);
                   1039:        font_push('B');
                   1040:        return(1);
                   1041: }
                   1042:
                   1043: static void
                   1044: post_fd(DECL_ARGS)
                   1045: {
                   1046:
                   1047:        font_pop();
                   1048:        outflags |= MMAN_br;
1.17      schwarze 1049: }
                   1050:
                   1051: static int
1.26      schwarze 1052: pre_fl(DECL_ARGS)
                   1053: {
                   1054:
                   1055:        font_push('B');
                   1056:        print_word("-");
                   1057:        outflags &= ~MMAN_spc;
                   1058:        return(1);
                   1059: }
                   1060:
                   1061: static void
                   1062: post_fl(DECL_ARGS)
                   1063: {
                   1064:
                   1065:        font_pop();
1.27      schwarze 1066:        if (0 == n->nchild && NULL != n->next &&
                   1067:                        n->next->line == n->line)
1.26      schwarze 1068:                outflags &= ~MMAN_spc;
                   1069: }
                   1070:
                   1071: static int
1.16      schwarze 1072: pre_fn(DECL_ARGS)
                   1073: {
                   1074:
1.23      schwarze 1075:        pre_syn(n);
                   1076:
1.16      schwarze 1077:        n = n->child;
                   1078:        if (NULL == n)
                   1079:                return(0);
                   1080:
1.26      schwarze 1081:        font_push('B');
1.42      schwarze 1082:        print_node(meta, n);
1.26      schwarze 1083:        font_pop();
1.20      schwarze 1084:        outflags &= ~MMAN_spc;
1.26      schwarze 1085:        print_word("(");
1.20      schwarze 1086:        outflags &= ~MMAN_spc;
1.35      schwarze 1087:
                   1088:        n = n->next;
                   1089:        if (NULL != n)
1.42      schwarze 1090:                pre_fa(meta, n);
1.35      schwarze 1091:        return(0);
1.16      schwarze 1092: }
                   1093:
                   1094: static void
                   1095: post_fn(DECL_ARGS)
                   1096: {
                   1097:
1.20      schwarze 1098:        print_word(")");
1.16      schwarze 1099:        if (MDOC_SYNPRETTY & n->flags) {
1.20      schwarze 1100:                print_word(";");
1.21      schwarze 1101:                outflags |= MMAN_br;
1.17      schwarze 1102:        }
                   1103: }
                   1104:
                   1105: static int
                   1106: pre_fo(DECL_ARGS)
                   1107: {
                   1108:
                   1109:        switch (n->type) {
1.23      schwarze 1110:        case (MDOC_BLOCK):
                   1111:                pre_syn(n);
                   1112:                break;
1.17      schwarze 1113:        case (MDOC_HEAD):
1.26      schwarze 1114:                font_push('B');
1.17      schwarze 1115:                break;
                   1116:        case (MDOC_BODY):
1.20      schwarze 1117:                outflags &= ~MMAN_spc;
                   1118:                print_word("(");
                   1119:                outflags &= ~MMAN_spc;
1.17      schwarze 1120:                break;
                   1121:        default:
                   1122:                break;
                   1123:        }
                   1124:        return(1);
                   1125: }
                   1126:
                   1127: static void
                   1128: post_fo(DECL_ARGS)
                   1129: {
                   1130:
                   1131:        switch (n->type) {
                   1132:        case (MDOC_HEAD):
1.26      schwarze 1133:                font_pop();
1.17      schwarze 1134:                break;
                   1135:        case (MDOC_BODY):
1.42      schwarze 1136:                post_fn(meta, n);
1.17      schwarze 1137:                break;
                   1138:        default:
                   1139:                break;
1.16      schwarze 1140:        }
1.15      schwarze 1141: }
                   1142:
                   1143: static int
1.23      schwarze 1144: pre_ft(DECL_ARGS)
                   1145: {
                   1146:
                   1147:        pre_syn(n);
1.26      schwarze 1148:        font_push('I');
1.23      schwarze 1149:        return(1);
                   1150: }
                   1151:
                   1152: static int
1.15      schwarze 1153: pre_in(DECL_ARGS)
                   1154: {
                   1155:
                   1156:        if (MDOC_SYNPRETTY & n->flags) {
1.23      schwarze 1157:                pre_syn(n);
1.26      schwarze 1158:                font_push('B');
                   1159:                print_word("#include <");
                   1160:                outflags &= ~MMAN_spc;
                   1161:        } else {
                   1162:                print_word("<");
                   1163:                outflags &= ~MMAN_spc;
                   1164:                font_push('I');
                   1165:        }
1.15      schwarze 1166:        return(1);
                   1167: }
                   1168:
                   1169: static void
                   1170: post_in(DECL_ARGS)
                   1171: {
                   1172:
                   1173:        if (MDOC_SYNPRETTY & n->flags) {
1.26      schwarze 1174:                outflags &= ~MMAN_spc;
                   1175:                print_word(">");
                   1176:                font_pop();
1.21      schwarze 1177:                outflags |= MMAN_br;
1.26      schwarze 1178:        } else {
                   1179:                font_pop();
                   1180:                outflags &= ~MMAN_spc;
                   1181:                print_word(">");
                   1182:        }
1.1       schwarze 1183: }
                   1184:
                   1185: static int
                   1186: pre_it(DECL_ARGS)
                   1187: {
                   1188:        const struct mdoc_node *bln;
                   1189:
1.30      schwarze 1190:        switch (n->type) {
                   1191:        case (MDOC_HEAD):
1.36      schwarze 1192:                outflags |= MMAN_PP | MMAN_nl;
1.30      schwarze 1193:                bln = n->parent->parent;
1.36      schwarze 1194:                if (0 == bln->norm->Bl.comp ||
1.39      schwarze 1195:                    (NULL == n->parent->prev &&
                   1196:                     NULL == bln->parent->prev))
1.36      schwarze 1197:                        outflags |= MMAN_sp;
                   1198:                outflags &= ~MMAN_br;
1.3       schwarze 1199:                switch (bln->norm->Bl.type) {
1.30      schwarze 1200:                case (LIST_item):
                   1201:                        return(0);
                   1202:                case (LIST_inset):
                   1203:                        /* FALLTHROUGH */
                   1204:                case (LIST_diag):
                   1205:                        /* FALLTHROUGH */
                   1206:                case (LIST_ohang):
                   1207:                        if (bln->norm->Bl.type == LIST_diag)
1.36      schwarze 1208:                                print_line(".B \"", 0);
1.30      schwarze 1209:                        else
1.36      schwarze 1210:                                print_line(".R \"", 0);
1.30      schwarze 1211:                        outflags &= ~MMAN_spc;
                   1212:                        return(1);
1.3       schwarze 1213:                case (LIST_bullet):
1.30      schwarze 1214:                        /* FALLTHROUGH */
                   1215:                case (LIST_dash):
                   1216:                        /* FALLTHROUGH */
                   1217:                case (LIST_hyphen):
1.35      schwarze 1218:                        print_width(bln->norm->Bl.width, NULL, 0);
1.37      schwarze 1219:                        TPremain = 0;
1.30      schwarze 1220:                        outflags |= MMAN_nl;
                   1221:                        font_push('B');
                   1222:                        if (LIST_bullet == bln->norm->Bl.type)
                   1223:                                print_word("o");
                   1224:                        else
                   1225:                                print_word("-");
                   1226:                        font_pop();
                   1227:                        break;
                   1228:                case (LIST_enum):
1.35      schwarze 1229:                        print_width(bln->norm->Bl.width, NULL, 0);
1.37      schwarze 1230:                        TPremain = 0;
1.30      schwarze 1231:                        outflags |= MMAN_nl;
                   1232:                        print_count(&bln->norm->Bl.count);
1.3       schwarze 1233:                        break;
1.31      schwarze 1234:                case (LIST_hang):
1.35      schwarze 1235:                        print_width(bln->norm->Bl.width, n->child, 6);
1.37      schwarze 1236:                        TPremain = 0;
1.32      schwarze 1237:                        break;
                   1238:                case (LIST_tag):
1.37      schwarze 1239:                        print_width(bln->norm->Bl.width, n->child, 0);
                   1240:                        putchar('\n');
                   1241:                        outflags &= ~MMAN_spc;
                   1242:                        return(1);
1.3       schwarze 1243:                default:
1.32      schwarze 1244:                        return(1);
1.3       schwarze 1245:                }
1.20      schwarze 1246:                outflags |= MMAN_nl;
1.30      schwarze 1247:        default:
                   1248:                break;
1.1       schwarze 1249:        }
                   1250:        return(1);
1.30      schwarze 1251: }
                   1252:
                   1253: static void
                   1254: post_it(DECL_ARGS)
                   1255: {
                   1256:        const struct mdoc_node *bln;
                   1257:
1.33      schwarze 1258:        bln = n->parent->parent;
                   1259:
                   1260:        switch (n->type) {
                   1261:        case (MDOC_HEAD):
1.30      schwarze 1262:                switch (bln->norm->Bl.type) {
                   1263:                case (LIST_diag):
                   1264:                        outflags &= ~MMAN_spc;
                   1265:                        print_word("\\ ");
                   1266:                        break;
                   1267:                case (LIST_ohang):
                   1268:                        outflags |= MMAN_br;
                   1269:                        break;
                   1270:                default:
                   1271:                        break;
                   1272:                }
1.33      schwarze 1273:                break;
                   1274:        case (MDOC_BODY):
1.43      schwarze 1275:                switch (bln->norm->Bl.type) {
                   1276:                case (LIST_bullet):
                   1277:                        /* FALLTHROUGH */
                   1278:                case (LIST_dash):
                   1279:                        /* FALLTHROUGH */
                   1280:                case (LIST_hyphen):
                   1281:                        /* FALLTHROUGH */
                   1282:                case (LIST_enum):
                   1283:                        /* FALLTHROUGH */
                   1284:                case (LIST_hang):
                   1285:                        /* FALLTHROUGH */
                   1286:                case (LIST_tag):
                   1287:                        assert(Bl_stack_len);
                   1288:                        Bl_stack[--Bl_stack_len] = 0;
                   1289:
                   1290:                        /*
                   1291:                         * Our indentation had to be restored
                   1292:                         * after a child display.
                   1293:                         * Close out that indentation block now.
                   1294:                         */
                   1295:                        if (Bl_stack_post[Bl_stack_len]) {
                   1296:                                print_line(".RE", MMAN_nl);
                   1297:                                Bl_stack_post[Bl_stack_len] = 0;
                   1298:                        }
                   1299:
                   1300:                        /*
                   1301:                         * We are inside an enclosing list.
                   1302:                         * Restore the indentation of that list.
                   1303:                         */
                   1304:                        if (Bl_stack_len && Bl_stack[Bl_stack_len - 1])
                   1305:                                print_line(".RE", MMAN_nl);
                   1306:                        break;
                   1307:                case (LIST_column):
                   1308:                        if (NULL != n->next) {
                   1309:                                putchar('\t');
                   1310:                                outflags &= ~MMAN_spc;
                   1311:                        }
                   1312:                        break;
                   1313:                default:
                   1314:                        break;
1.33      schwarze 1315:                }
                   1316:                break;
                   1317:        default:
                   1318:                break;
1.30      schwarze 1319:        }
1.14      schwarze 1320: }
                   1321:
                   1322: static void
                   1323: post_lb(DECL_ARGS)
                   1324: {
                   1325:
1.21      schwarze 1326:        if (SEC_LIBRARY == n->sec)
                   1327:                outflags |= MMAN_br;
1.24      schwarze 1328: }
                   1329:
                   1330: static int
                   1331: pre_lk(DECL_ARGS)
                   1332: {
                   1333:        const struct mdoc_node *link, *descr;
                   1334:
                   1335:        if (NULL == (link = n->child))
                   1336:                return(0);
                   1337:
                   1338:        if (NULL != (descr = link->next)) {
1.26      schwarze 1339:                font_push('I');
1.24      schwarze 1340:                while (NULL != descr) {
                   1341:                        print_word(descr->string);
                   1342:                        descr = descr->next;
                   1343:                }
                   1344:                print_word(":");
1.26      schwarze 1345:                font_pop();
1.24      schwarze 1346:        }
                   1347:
1.26      schwarze 1348:        font_push('B');
1.24      schwarze 1349:        print_word(link->string);
1.26      schwarze 1350:        font_pop();
1.24      schwarze 1351:        return(0);
1.1       schwarze 1352: }
                   1353:
                   1354: static int
1.26      schwarze 1355: pre_li(DECL_ARGS)
                   1356: {
                   1357:
                   1358:        font_push('R');
                   1359:        return(1);
                   1360: }
                   1361:
                   1362: static int
1.1       schwarze 1363: pre_nm(DECL_ARGS)
                   1364: {
1.38      schwarze 1365:        char    *name;
1.1       schwarze 1366:
1.23      schwarze 1367:        if (MDOC_BLOCK == n->type)
                   1368:                pre_syn(n);
1.1       schwarze 1369:        if (MDOC_ELEM != n->type && MDOC_HEAD != n->type)
                   1370:                return(1);
1.42      schwarze 1371:        name = n->child ? n->child->string : meta->name;
1.38      schwarze 1372:        if (NULL == name)
1.23      schwarze 1373:                return(0);
1.38      schwarze 1374:        if (MDOC_HEAD == n->type) {
                   1375:                if (NULL == n->parent->prev)
                   1376:                        outflags |= MMAN_sp;
                   1377:                print_block(".HP", 0);
                   1378:                printf(" %ldn", strlen(name) + 1);
                   1379:                outflags |= MMAN_nl;
                   1380:        }
1.26      schwarze 1381:        font_push('B');
1.1       schwarze 1382:        if (NULL == n->child)
1.42      schwarze 1383:                print_word(meta->name);
1.1       schwarze 1384:        return(1);
                   1385: }
                   1386:
                   1387: static void
                   1388: post_nm(DECL_ARGS)
                   1389: {
                   1390:
                   1391:        if (MDOC_ELEM != n->type && MDOC_HEAD != n->type)
                   1392:                return;
1.26      schwarze 1393:        font_pop();
1.25      schwarze 1394: }
                   1395:
                   1396: static int
                   1397: pre_no(DECL_ARGS)
                   1398: {
                   1399:
                   1400:        outflags |= MMAN_spc_force;
                   1401:        return(1);
1.1       schwarze 1402: }
                   1403:
                   1404: static int
                   1405: pre_ns(DECL_ARGS)
                   1406: {
                   1407:
1.20      schwarze 1408:        outflags &= ~MMAN_spc;
1.1       schwarze 1409:        return(0);
                   1410: }
                   1411:
1.3       schwarze 1412: static void
                   1413: post_pf(DECL_ARGS)
                   1414: {
                   1415:
1.20      schwarze 1416:        outflags &= ~MMAN_spc;
1.3       schwarze 1417: }
                   1418:
1.1       schwarze 1419: static int
                   1420: pre_pp(DECL_ARGS)
                   1421: {
                   1422:
1.36      schwarze 1423:        if (MDOC_It != n->parent->tok)
                   1424:                outflags |= MMAN_PP;
                   1425:        outflags |= MMAN_sp | MMAN_nl;
                   1426:        outflags &= ~MMAN_br;
1.34      schwarze 1427:        return(0);
                   1428: }
                   1429:
                   1430: static int
                   1431: pre_rs(DECL_ARGS)
                   1432: {
                   1433:
                   1434:        if (SEC_SEE_ALSO == n->sec) {
1.36      schwarze 1435:                outflags |= MMAN_PP | MMAN_sp | MMAN_nl;
                   1436:                outflags &= ~MMAN_br;
1.34      schwarze 1437:        }
                   1438:        return(1);
1.12      schwarze 1439: }
                   1440:
                   1441: static int
                   1442: pre_sm(DECL_ARGS)
                   1443: {
                   1444:
                   1445:        assert(n->child && MDOC_TEXT == n->child->type);
                   1446:        if (0 == strcmp("on", n->child->string))
1.27      schwarze 1447:                outflags |= MMAN_Sm | MMAN_spc;
1.12      schwarze 1448:        else
1.20      schwarze 1449:                outflags &= ~MMAN_Sm;
1.12      schwarze 1450:        return(0);
1.1       schwarze 1451: }
                   1452:
                   1453: static int
1.3       schwarze 1454: pre_sp(DECL_ARGS)
1.1       schwarze 1455: {
                   1456:
1.41      schwarze 1457:        if (MMAN_PP & outflags) {
                   1458:                outflags &= ~MMAN_PP;
1.40      schwarze 1459:                print_line(".PP", 0);
1.41      schwarze 1460:        } else
1.40      schwarze 1461:                print_line(".sp", 0);
1.1       schwarze 1462:        return(1);
                   1463: }
                   1464:
                   1465: static void
1.3       schwarze 1466: post_sp(DECL_ARGS)
1.1       schwarze 1467: {
                   1468:
1.20      schwarze 1469:        outflags |= MMAN_nl;
1.18      schwarze 1470: }
                   1471:
                   1472: static int
1.26      schwarze 1473: pre_sy(DECL_ARGS)
                   1474: {
                   1475:
                   1476:        font_push('B');
                   1477:        return(1);
                   1478: }
                   1479:
                   1480: static int
1.18      schwarze 1481: pre_vt(DECL_ARGS)
                   1482: {
                   1483:
                   1484:        if (MDOC_SYNPRETTY & n->flags) {
                   1485:                switch (n->type) {
                   1486:                case (MDOC_BLOCK):
1.23      schwarze 1487:                        pre_syn(n);
1.18      schwarze 1488:                        return(1);
                   1489:                case (MDOC_BODY):
                   1490:                        break;
                   1491:                default:
                   1492:                        return(0);
                   1493:                }
                   1494:        }
1.26      schwarze 1495:        font_push('I');
1.18      schwarze 1496:        return(1);
                   1497: }
                   1498:
                   1499: static void
                   1500: post_vt(DECL_ARGS)
                   1501: {
                   1502:
1.19      schwarze 1503:        if (MDOC_SYNPRETTY & n->flags && MDOC_BODY != n->type)
1.18      schwarze 1504:                return;
1.26      schwarze 1505:        font_pop();
1.1       schwarze 1506: }
                   1507:
                   1508: static int
                   1509: pre_xr(DECL_ARGS)
                   1510: {
                   1511:
                   1512:        n = n->child;
                   1513:        if (NULL == n)
                   1514:                return(0);
1.42      schwarze 1515:        print_node(meta, n);
1.1       schwarze 1516:        n = n->next;
                   1517:        if (NULL == n)
                   1518:                return(0);
1.20      schwarze 1519:        outflags &= ~MMAN_spc;
                   1520:        print_word("(");
1.42      schwarze 1521:        print_node(meta, n);
1.20      schwarze 1522:        print_word(")");
1.1       schwarze 1523:        return(0);
1.8       schwarze 1524: }
                   1525:
                   1526: static int
                   1527: pre_ux(DECL_ARGS)
                   1528: {
                   1529:
1.20      schwarze 1530:        print_word(manacts[n->tok].prefix);
1.8       schwarze 1531:        if (NULL == n->child)
                   1532:                return(0);
1.20      schwarze 1533:        outflags &= ~MMAN_spc;
                   1534:        print_word("\\~");
                   1535:        outflags &= ~MMAN_spc;
1.8       schwarze 1536:        return(1);
1.1       schwarze 1537: }

CVSweb