1 /* $Id: mdoc_man.c,v 1.57 2013/12/25 22:00:45 schwarze Exp $ */
3 * Copyright (c) 2011, 2012, 2013 Ingo Schwarze <schwarze@openbsd.org>
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.
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.
31 #define DECL_ARGS const struct mdoc_meta *meta, \
32 const struct mdoc_node *n
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 */
42 static int cond_body(DECL_ARGS);
43 static int cond_head(DECL_ARGS);
44 static void font_push(char);
45 static void font_pop(void);
46 static void mid_it(void);
47 static void post__t(DECL_ARGS);
48 static void post_bd(DECL_ARGS);
49 static void post_bf(DECL_ARGS);
50 static void post_bk(DECL_ARGS);
51 static void post_bl(DECL_ARGS);
52 static void post_dl(DECL_ARGS);
53 static void post_enc(DECL_ARGS);
54 static void post_eo(DECL_ARGS);
55 static void post_fa(DECL_ARGS);
56 static void post_fd(DECL_ARGS);
57 static void post_fl(DECL_ARGS);
58 static void post_fn(DECL_ARGS);
59 static void post_fo(DECL_ARGS);
60 static void post_font(DECL_ARGS);
61 static void post_in(DECL_ARGS);
62 static void post_it(DECL_ARGS);
63 static void post_lb(DECL_ARGS);
64 static void post_nm(DECL_ARGS);
65 static void post_percent(DECL_ARGS);
66 static void post_pf(DECL_ARGS);
67 static void post_sect(DECL_ARGS);
68 static void post_sp(DECL_ARGS);
69 static void post_vt(DECL_ARGS);
70 static int pre__t(DECL_ARGS);
71 static int pre_an(DECL_ARGS);
72 static int pre_ap(DECL_ARGS);
73 static int pre_bd(DECL_ARGS);
74 static int pre_bf(DECL_ARGS);
75 static int pre_bk(DECL_ARGS);
76 static int pre_bl(DECL_ARGS);
77 static int pre_br(DECL_ARGS);
78 static int pre_bx(DECL_ARGS);
79 static int pre_dl(DECL_ARGS);
80 static int pre_enc(DECL_ARGS);
81 static int pre_em(DECL_ARGS);
82 static int pre_fa(DECL_ARGS);
83 static int pre_fd(DECL_ARGS);
84 static int pre_fl(DECL_ARGS);
85 static int pre_fn(DECL_ARGS);
86 static int pre_fo(DECL_ARGS);
87 static int pre_ft(DECL_ARGS);
88 static int pre_in(DECL_ARGS);
89 static int pre_it(DECL_ARGS);
90 static int pre_lk(DECL_ARGS);
91 static int pre_li(DECL_ARGS);
92 static int pre_nm(DECL_ARGS);
93 static int pre_no(DECL_ARGS);
94 static int pre_ns(DECL_ARGS);
95 static int pre_pp(DECL_ARGS);
96 static int pre_rs(DECL_ARGS);
97 static int pre_sm(DECL_ARGS);
98 static int pre_sp(DECL_ARGS);
99 static int pre_sect(DECL_ARGS);
100 static int pre_sy(DECL_ARGS);
101 static void pre_syn(const struct mdoc_node *);
102 static int pre_vt(DECL_ARGS);
103 static int pre_ux(DECL_ARGS);
104 static int pre_xr(DECL_ARGS);
105 static void print_word(const char *);
106 static void print_line(const char *, int);
107 static void print_block(const char *, int);
108 static void print_offs(const char *);
109 static void print_width(const char *,
110 const struct mdoc_node *, size_t);
111 static void print_count(int *);
112 static void print_node(DECL_ARGS);
114 static const struct manact manacts[MDOC_MAX + 1] = {
115 { NULL, pre_ap, NULL, NULL, NULL }, /* Ap */
116 { NULL, NULL, NULL, NULL, NULL }, /* Dd */
117 { NULL, NULL, NULL, NULL, NULL }, /* Dt */
118 { NULL, NULL, NULL, NULL, NULL }, /* Os */
119 { NULL, pre_sect, post_sect, ".SH", NULL }, /* Sh */
120 { NULL, pre_sect, post_sect, ".SS", NULL }, /* Ss */
121 { NULL, pre_pp, NULL, NULL, NULL }, /* Pp */
122 { cond_body, pre_dl, post_dl, NULL, NULL }, /* D1 */
123 { cond_body, pre_dl, post_dl, NULL, NULL }, /* Dl */
124 { cond_body, pre_bd, post_bd, NULL, NULL }, /* Bd */
125 { NULL, NULL, NULL, NULL, NULL }, /* Ed */
126 { cond_body, pre_bl, post_bl, NULL, NULL }, /* Bl */
127 { NULL, NULL, NULL, NULL, NULL }, /* El */
128 { NULL, pre_it, post_it, NULL, NULL }, /* It */
129 { NULL, pre_em, post_font, NULL, NULL }, /* Ad */
130 { NULL, pre_an, NULL, NULL, NULL }, /* An */
131 { NULL, pre_em, post_font, NULL, NULL }, /* Ar */
132 { NULL, pre_sy, post_font, NULL, NULL }, /* Cd */
133 { NULL, pre_sy, post_font, NULL, NULL }, /* Cm */
134 { NULL, pre_li, post_font, NULL, NULL }, /* Dv */
135 { NULL, pre_li, post_font, NULL, NULL }, /* Er */
136 { NULL, pre_li, post_font, NULL, NULL }, /* Ev */
137 { NULL, pre_enc, post_enc, "The \\fB",
138 "\\fP\nutility exits 0 on success, and >0 if an error occurs."
140 { NULL, pre_fa, post_fa, NULL, NULL }, /* Fa */
141 { NULL, pre_fd, post_fd, NULL, NULL }, /* Fd */
142 { NULL, pre_fl, post_fl, NULL, NULL }, /* Fl */
143 { NULL, pre_fn, post_fn, NULL, NULL }, /* Fn */
144 { NULL, pre_ft, post_font, NULL, NULL }, /* Ft */
145 { NULL, pre_sy, post_font, NULL, NULL }, /* Ic */
146 { NULL, pre_in, post_in, NULL, NULL }, /* In */
147 { NULL, pre_li, post_font, NULL, NULL }, /* Li */
148 { cond_head, pre_enc, NULL, "\\- ", NULL }, /* Nd */
149 { NULL, pre_nm, post_nm, NULL, NULL }, /* Nm */
150 { cond_body, pre_enc, post_enc, "[", "]" }, /* Op */
151 { NULL, NULL, NULL, NULL, NULL }, /* Ot */
152 { NULL, pre_em, post_font, NULL, NULL }, /* Pa */
153 { NULL, pre_enc, post_enc, "The \\fB",
154 "\\fP\nfunction returns the value 0 if successful;\n"
155 "otherwise the value -1 is returned and the global\n"
156 "variable \\fIerrno\\fP is set to indicate the error."
158 { NULL, NULL, NULL, NULL, NULL }, /* St */
159 { NULL, pre_em, post_font, NULL, NULL }, /* Va */
160 { NULL, pre_vt, post_vt, NULL, NULL }, /* Vt */
161 { NULL, pre_xr, NULL, NULL, NULL }, /* Xr */
162 { NULL, NULL, post_percent, NULL, NULL }, /* %A */
163 { NULL, pre_em, post_percent, NULL, NULL }, /* %B */
164 { NULL, NULL, post_percent, NULL, NULL }, /* %D */
165 { NULL, pre_em, post_percent, NULL, NULL }, /* %I */
166 { NULL, pre_em, post_percent, NULL, NULL }, /* %J */
167 { NULL, NULL, post_percent, NULL, NULL }, /* %N */
168 { NULL, NULL, post_percent, NULL, NULL }, /* %O */
169 { NULL, NULL, post_percent, NULL, NULL }, /* %P */
170 { NULL, NULL, post_percent, NULL, NULL }, /* %R */
171 { NULL, pre__t, post__t, NULL, NULL }, /* %T */
172 { NULL, NULL, post_percent, NULL, NULL }, /* %V */
173 { NULL, NULL, NULL, NULL, NULL }, /* Ac */
174 { cond_body, pre_enc, post_enc, "<", ">" }, /* Ao */
175 { cond_body, pre_enc, post_enc, "<", ">" }, /* Aq */
176 { NULL, NULL, NULL, NULL, NULL }, /* At */
177 { NULL, NULL, NULL, NULL, NULL }, /* Bc */
178 { NULL, pre_bf, post_bf, NULL, NULL }, /* Bf */
179 { cond_body, pre_enc, post_enc, "[", "]" }, /* Bo */
180 { cond_body, pre_enc, post_enc, "[", "]" }, /* Bq */
181 { NULL, pre_ux, NULL, "BSD/OS", NULL }, /* Bsx */
182 { NULL, pre_bx, NULL, NULL, NULL }, /* Bx */
183 { NULL, NULL, NULL, NULL, NULL }, /* Db */
184 { NULL, NULL, NULL, NULL, NULL }, /* Dc */
185 { cond_body, pre_enc, post_enc, "\\(lq", "\\(rq" }, /* Do */
186 { cond_body, pre_enc, post_enc, "\\(lq", "\\(rq" }, /* Dq */
187 { NULL, NULL, NULL, NULL, NULL }, /* Ec */
188 { NULL, NULL, NULL, NULL, NULL }, /* Ef */
189 { NULL, pre_em, post_font, NULL, NULL }, /* Em */
190 { NULL, NULL, post_eo, NULL, NULL }, /* Eo */
191 { NULL, pre_ux, NULL, "FreeBSD", NULL }, /* Fx */
192 { NULL, pre_sy, post_font, NULL, NULL }, /* Ms */
193 { NULL, pre_no, NULL, NULL, NULL }, /* No */
194 { NULL, pre_ns, NULL, NULL, NULL }, /* Ns */
195 { NULL, pre_ux, NULL, "NetBSD", NULL }, /* Nx */
196 { NULL, pre_ux, NULL, "OpenBSD", NULL }, /* Ox */
197 { NULL, NULL, NULL, NULL, NULL }, /* Pc */
198 { NULL, NULL, post_pf, NULL, NULL }, /* Pf */
199 { cond_body, pre_enc, post_enc, "(", ")" }, /* Po */
200 { cond_body, pre_enc, post_enc, "(", ")" }, /* Pq */
201 { NULL, NULL, NULL, NULL, NULL }, /* Qc */
202 { cond_body, pre_enc, post_enc, "\\(oq", "\\(cq" }, /* Ql */
203 { cond_body, pre_enc, post_enc, "\"", "\"" }, /* Qo */
204 { cond_body, pre_enc, post_enc, "\"", "\"" }, /* Qq */
205 { NULL, NULL, NULL, NULL, NULL }, /* Re */
206 { cond_body, pre_rs, NULL, NULL, NULL }, /* Rs */
207 { NULL, NULL, NULL, NULL, NULL }, /* Sc */
208 { cond_body, pre_enc, post_enc, "\\(oq", "\\(cq" }, /* So */
209 { cond_body, pre_enc, post_enc, "\\(oq", "\\(cq" }, /* Sq */
210 { NULL, pre_sm, NULL, NULL, NULL }, /* Sm */
211 { NULL, pre_em, post_font, NULL, NULL }, /* Sx */
212 { NULL, pre_sy, post_font, NULL, NULL }, /* Sy */
213 { NULL, pre_li, post_font, NULL, NULL }, /* Tn */
214 { NULL, pre_ux, NULL, "UNIX", NULL }, /* Ux */
215 { NULL, NULL, NULL, NULL, NULL }, /* Xc */
216 { NULL, NULL, NULL, NULL, NULL }, /* Xo */
217 { NULL, pre_fo, post_fo, NULL, NULL }, /* Fo */
218 { NULL, NULL, NULL, NULL, NULL }, /* Fc */
219 { cond_body, pre_enc, post_enc, "[", "]" }, /* Oo */
220 { NULL, NULL, NULL, NULL, NULL }, /* Oc */
221 { NULL, pre_bk, post_bk, NULL, NULL }, /* Bk */
222 { NULL, NULL, NULL, NULL, NULL }, /* Ek */
223 { NULL, pre_ux, NULL, "is currently in beta test.", NULL }, /* Bt */
224 { NULL, NULL, NULL, NULL, NULL }, /* Hf */
225 { NULL, NULL, NULL, NULL, NULL }, /* Fr */
226 { NULL, pre_ux, NULL, "currently under development.", NULL }, /* Ud */
227 { NULL, NULL, post_lb, NULL, NULL }, /* Lb */
228 { NULL, pre_pp, NULL, NULL, NULL }, /* Lp */
229 { NULL, pre_lk, NULL, NULL, NULL }, /* Lk */
230 { NULL, pre_em, post_font, NULL, NULL }, /* Mt */
231 { cond_body, pre_enc, post_enc, "{", "}" }, /* Brq */
232 { cond_body, pre_enc, post_enc, "{", "}" }, /* Bro */
233 { NULL, NULL, NULL, NULL, NULL }, /* Brc */
234 { NULL, NULL, post_percent, NULL, NULL }, /* %C */
235 { NULL, NULL, NULL, NULL, NULL }, /* Es */
236 { NULL, NULL, NULL, NULL, NULL }, /* En */
237 { NULL, pre_ux, NULL, "DragonFly", NULL }, /* Dx */
238 { NULL, NULL, post_percent, NULL, NULL }, /* %Q */
239 { NULL, pre_br, NULL, NULL, NULL }, /* br */
240 { NULL, pre_sp, post_sp, NULL, NULL }, /* sp */
241 { NULL, NULL, post_percent, NULL, NULL }, /* %U */
242 { NULL, NULL, NULL, NULL, NULL }, /* Ta */
243 { NULL, NULL, NULL, NULL, NULL }, /* ROOT */
247 #define MMAN_spc (1 << 0) /* blank character before next word */
248 #define MMAN_spc_force (1 << 1) /* even before trailing punctuation */
249 #define MMAN_nl (1 << 2) /* break man(7) code line */
250 #define MMAN_br (1 << 3) /* break output line */
251 #define MMAN_sp (1 << 4) /* insert a blank output line */
252 #define MMAN_PP (1 << 5) /* reset indentation etc. */
253 #define MMAN_Sm (1 << 6) /* horizontal spacing mode */
254 #define MMAN_Bk (1 << 7) /* word keep mode */
255 #define MMAN_Bk_susp (1 << 8) /* suspend this (after a macro) */
256 #define MMAN_An_split (1 << 9) /* author mode is "split" */
257 #define MMAN_An_nosplit (1 << 10) /* author mode is "nosplit" */
258 #define MMAN_PD (1 << 11) /* inter-paragraph spacing disabled */
259 #define MMAN_nbrword (1 << 12) /* do not break the next word */
261 #define BL_STACK_MAX 32
263 static size_t Bl_stack[BL_STACK_MAX]; /* offsets [chars] */
264 static int Bl_stack_post[BL_STACK_MAX]; /* add final .RE */
265 static int Bl_stack_len; /* number of nested Bl blocks */
266 static int TPremain; /* characters before tag is full */
275 font_push(char newfont)
278 if (fontqueue.head + fontqueue.size <= ++fontqueue.tail) {
280 fontqueue.head = mandoc_realloc(fontqueue.head,
283 *fontqueue.tail = newfont;
287 outflags &= ~MMAN_spc;
294 if (fontqueue.tail > fontqueue.head)
296 outflags &= ~MMAN_spc;
299 putchar(*fontqueue.tail);
303 print_word(const char *s)
306 if ((MMAN_PP | MMAN_sp | MMAN_br | MMAN_nl) & outflags) {
308 * If we need a newline, print it now and start afresh.
310 if (MMAN_PP & outflags) {
311 if (MMAN_sp & outflags) {
312 if (MMAN_PD & outflags) {
314 outflags &= ~MMAN_PD;
316 } else if ( ! (MMAN_PD & outflags)) {
321 } else if (MMAN_sp & outflags)
323 else if (MMAN_br & outflags)
325 else if (MMAN_nl & outflags)
327 outflags &= ~(MMAN_PP|MMAN_sp|MMAN_br|MMAN_nl|MMAN_spc);
331 } else if (MMAN_spc & outflags) {
333 * If we need a space, only print it if
334 * (1) it is forced by `No' or
335 * (2) what follows is not terminating punctuation or
336 * (3) what follows is longer than one character.
338 if (MMAN_spc_force & outflags || '\0' == s[0] ||
339 NULL == strchr(".,:;)]?!", s[0]) || '\0' != s[1]) {
340 if (MMAN_Bk & outflags &&
341 ! (MMAN_Bk_susp & outflags))
350 * Reassign needing space if we're not following opening
353 if (MMAN_Sm & outflags && ('\0' == s[0] ||
354 (('(' != s[0] && '[' != s[0]) || '\0' != s[1])))
355 outflags |= MMAN_spc;
357 outflags &= ~MMAN_spc;
358 outflags &= ~(MMAN_spc_force | MMAN_Bk_susp);
369 if (MMAN_nbrword & outflags) {
375 putchar((unsigned char)*s);
381 outflags &= ~MMAN_nbrword;
385 print_line(const char *s, int newflags)
388 outflags &= ~MMAN_br;
391 outflags |= newflags;
395 print_block(const char *s, int newflags)
398 outflags &= ~MMAN_PP;
399 if (MMAN_sp & outflags) {
400 outflags &= ~(MMAN_sp | MMAN_br);
401 if (MMAN_PD & outflags) {
402 print_line(".PD", 0);
403 outflags &= ~MMAN_PD;
405 } else if (! (MMAN_PD & outflags))
406 print_line(".PD 0", MMAN_PD);
409 outflags |= MMAN_Bk_susp | newflags;
413 print_offs(const char *v)
419 print_line(".RS", MMAN_Bk_susp);
421 /* Convert v into a number (of characters). */
422 if (NULL == v || '\0' == *v || 0 == strcmp(v, "left"))
424 else if (0 == strcmp(v, "indent"))
426 else if (0 == strcmp(v, "indent-two"))
428 else if (a2roffsu(v, &su, SCALE_MAX)) {
429 if (SCALE_EN == su.unit)
434 * If we are inside an enclosing list,
435 * there is no easy way to add the two
436 * indentations because they are provided
437 * in terms of different units.
447 * We are inside an enclosing list.
448 * Add the two indentations.
451 sz += Bl_stack[Bl_stack_len - 1];
453 snprintf(buf, sizeof(buf), "%zun", sz);
459 * Set up the indentation for a list item; used from pre_it().
462 print_width(const char *v, const struct mdoc_node *child, size_t defsz)
472 /* Convert v into a number (of characters). */
475 else if (a2roffsu(v, &su, SCALE_MAX)) {
476 if (SCALE_EN == su.unit)
485 /* XXX Rough estimation, might have multiple parts. */
486 chsz = (NULL != child && MDOC_TEXT == child->type) ?
487 strlen(child->string) : 0;
489 /* Maybe we are inside an enclosing list? */
493 * Save our own indentation,
494 * such that child lists can use it.
496 Bl_stack[Bl_stack_len++] = sz + 2;
498 /* Set up the current list. */
499 if (defsz && chsz > sz)
500 print_block(".HP", 0);
502 print_block(".TP", 0);
506 snprintf(buf, sizeof(buf), "%zun", sz + 2);
514 print_count(int *count)
518 snprintf(buf, sizeof(buf), "%d.", ++*count);
523 man_man(void *arg, const struct man *man)
527 * Dump the keep buffer.
528 * We're guaranteed by now that this exists (is non-NULL).
529 * Flush stdout afterward, just in case.
531 fputs(mparse_getkeep(man_mparse(man)), stdout);
536 man_mdoc(void *arg, const struct mdoc *mdoc)
538 const struct mdoc_meta *meta;
539 const struct mdoc_node *n;
541 meta = mdoc_meta(mdoc);
544 printf(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"\n",
545 meta->title, meta->msec, meta->date,
546 meta->os, meta->vol);
548 /* Disable hyphenation and if nroff, disable justification. */
549 printf(".nh\n.if n .ad l");
551 outflags = MMAN_nl | MMAN_Sm;
552 if (0 == fontqueue.size) {
554 fontqueue.head = fontqueue.tail = mandoc_malloc(8);
555 *fontqueue.tail = 'R';
562 print_node(DECL_ARGS)
564 const struct mdoc_node *sub;
565 const struct manact *act;
569 * Break the line if we were parsed subsequent the current node.
570 * This makes the page structure be more consistent.
572 if (MMAN_spc & outflags && MDOC_LINE & n->flags)
579 if (MDOC_TEXT == n->type) {
581 * Make sure that we don't happen to start with a
582 * control character at the start of a line.
584 if (MMAN_nl & outflags && ('.' == *n->string ||
585 '\'' == *n->string)) {
588 outflags &= ~MMAN_spc;
590 print_word(n->string);
593 * Conditionally run the pre-node action handler for a
596 act = manacts + n->tok;
597 cond = NULL == act->cond || (*act->cond)(meta, n);
598 if (cond && act->pre)
599 do_sub = (*act->pre)(meta, n);
603 * Conditionally run all child nodes.
604 * Note that this iterates over children instead of using
605 * recursion. This prevents unnecessary depth in the stack.
608 for (sub = n->child; sub; sub = sub->next)
609 print_node(meta, sub);
612 * Lastly, conditionally run the post-node handler.
614 if (cond && act->post)
615 (*act->post)(meta, n);
622 return(MDOC_HEAD == n->type);
629 return(MDOC_BODY == n->type);
637 prefix = manacts[n->tok].prefix;
641 outflags &= ~MMAN_spc;
650 suffix = manacts[n->tok].suffix;
653 outflags &= ~MMAN_spc;
665 post_percent(DECL_ARGS)
668 if (pre_em == manacts[n->tok].pre)
672 if (n->prev && n->prev->tok == n->tok &&
673 n->next->tok == n->tok)
685 if (n->parent && MDOC_Rs == n->parent->tok &&
686 n->parent->norm->Rs.quote_T) {
689 outflags &= ~MMAN_spc;
699 if (n->parent && MDOC_Rs == n->parent->tok &&
700 n->parent->norm->Rs.quote_T) {
701 outflags &= ~MMAN_spc;
706 post_percent(meta, n);
710 * Print before a section header.
716 if (MDOC_HEAD == n->type) {
718 print_block(manacts[n->tok].prefix, 0);
721 outflags &= ~MMAN_spc;
727 * Print subsequent a section header.
733 if (MDOC_HEAD != n->type)
735 outflags &= ~MMAN_spc;
739 if (MDOC_Sh == n->tok && SEC_AUTHORS == n->sec)
740 outflags &= ~(MMAN_An_split | MMAN_An_nosplit);
743 /* See mdoc_term.c, synopsis_pre() for comments. */
745 pre_syn(const struct mdoc_node *n)
748 if (NULL == n->prev || ! (MDOC_SYNPRETTY & n->flags))
751 if (n->prev->tok == n->tok &&
759 switch (n->prev->tok) {
772 if (MDOC_Fn != n->tok && MDOC_Fo != n->tok) {
787 switch (n->norm->An.auth) {
789 outflags &= ~MMAN_An_nosplit;
790 outflags |= MMAN_An_split;
793 outflags &= ~MMAN_An_split;
794 outflags |= MMAN_An_nosplit;
797 if (MMAN_An_split & outflags)
799 else if (SEC_AUTHORS == n->sec &&
800 ! (MMAN_An_nosplit & outflags))
801 outflags |= MMAN_An_split;
810 outflags &= ~MMAN_spc;
812 outflags &= ~MMAN_spc;
820 outflags &= ~(MMAN_PP | MMAN_sp | MMAN_br);
822 if (DISP_unfilled == n->norm->Bd.type ||
823 DISP_literal == n->norm->Bd.type)
824 print_line(".nf", 0);
825 if (0 == n->norm->Bd.comp && NULL != n->parent->prev)
827 print_offs(n->norm->Bd.offs);
835 /* Close out this display. */
836 print_line(".RE", MMAN_nl);
837 if (DISP_unfilled == n->norm->Bd.type ||
838 DISP_literal == n->norm->Bd.type)
839 print_line(".fi", MMAN_nl);
841 /* Maybe we are inside an enclosing list? */
842 if (NULL != n->parent->next)
858 switch (n->norm->Bf.font) {
876 if (MDOC_BODY == n->type)
899 if (MDOC_BODY == n->type)
900 outflags &= ~MMAN_Bk;
909 * print_offs() will increase the -offset to account for
910 * a possible enclosing .It, but any enclosed .It blocks
911 * just nest and do not add up their indentation.
913 if (n->norm->Bl.offs) {
914 print_offs(n->norm->Bl.offs);
915 Bl_stack[Bl_stack_len++] = 0;
918 switch (n->norm->Bl.type) {
920 n->norm->Bl.count = 0;
928 print_line(".TS", MMAN_nl);
929 for (icol = 0; icol < n->norm->Bl.ncols; icol++)
940 switch (n->norm->Bl.type) {
942 print_line(".TE", 0);
945 n->norm->Bl.count = 0;
951 if (n->norm->Bl.offs) {
952 print_line(".RE", MMAN_nl);
953 assert(Bl_stack_len);
955 assert(0 == Bl_stack[Bl_stack_len]);
957 outflags |= MMAN_PP | MMAN_nl;
958 outflags &= ~(MMAN_sp | MMAN_br);
961 /* Maybe we are inside an enclosing list? */
962 if (NULL != n->parent->next)
981 print_word(n->string);
982 outflags &= ~MMAN_spc;
988 outflags &= ~MMAN_spc;
990 outflags &= ~MMAN_spc;
991 print_word(n->string);
1007 print_line(".RE", MMAN_nl);
1009 /* Maybe we are inside an enclosing list? */
1010 if (NULL != n->parent->next)
1026 if (MDOC_HEAD == n->type || MDOC_BODY == n->type)
1027 outflags &= ~MMAN_spc;
1035 am_Fa = MDOC_Fa == n->tok;
1042 if (am_Fa || MDOC_SYNPRETTY & n->flags)
1043 outflags |= MMAN_nbrword;
1044 print_node(meta, n);
1046 if (NULL != (n = n->next))
1056 if (NULL != n->next && MDOC_Fa == n->next->tok)
1074 outflags |= MMAN_br;
1083 outflags &= ~MMAN_spc;
1092 if (0 == n->nchild && NULL != n->next &&
1093 n->next->line == n->line)
1094 outflags &= ~MMAN_spc;
1107 if (MDOC_SYNPRETTY & n->flags)
1108 print_block(".HP 4n", MMAN_nl);
1111 print_node(meta, n);
1113 outflags &= ~MMAN_spc;
1115 outflags &= ~MMAN_spc;
1128 if (MDOC_SYNPRETTY & n->flags) {
1130 outflags |= MMAN_PP;
1143 if (MDOC_SYNPRETTY & n->flags)
1144 print_block(".HP 4n", MMAN_nl);
1148 outflags &= ~MMAN_spc;
1150 outflags &= ~MMAN_spc;
1187 if (MDOC_SYNPRETTY & n->flags) {
1190 print_word("#include <");
1191 outflags &= ~MMAN_spc;
1194 outflags &= ~MMAN_spc;
1204 if (MDOC_SYNPRETTY & n->flags) {
1205 outflags &= ~MMAN_spc;
1208 outflags |= MMAN_br;
1211 outflags &= ~MMAN_spc;
1219 const struct mdoc_node *bln;
1223 outflags |= MMAN_PP | MMAN_nl;
1224 bln = n->parent->parent;
1225 if (0 == bln->norm->Bl.comp ||
1226 (NULL == n->parent->prev &&
1227 NULL == bln->parent->prev))
1228 outflags |= MMAN_sp;
1229 outflags &= ~MMAN_br;
1230 switch (bln->norm->Bl.type) {
1238 if (bln->norm->Bl.type == LIST_diag)
1239 print_line(".B \"", 0);
1241 print_line(".R \"", 0);
1242 outflags &= ~MMAN_spc;
1249 print_width(bln->norm->Bl.width, NULL, 0);
1251 outflags |= MMAN_nl;
1253 if (LIST_bullet == bln->norm->Bl.type)
1260 print_width(bln->norm->Bl.width, NULL, 0);
1262 outflags |= MMAN_nl;
1263 print_count(&bln->norm->Bl.count);
1266 print_width(bln->norm->Bl.width, n->child, 6);
1270 print_width(bln->norm->Bl.width, n->child, 0);
1272 outflags &= ~MMAN_spc;
1277 outflags |= MMAN_nl;
1285 * This function is called after closing out an indented block.
1286 * If we are inside an enclosing list, restore its indentation.
1293 /* Nothing to do outside a list. */
1294 if (0 == Bl_stack_len || 0 == Bl_stack[Bl_stack_len - 1])
1297 /* The indentation has already been set up. */
1298 if (Bl_stack_post[Bl_stack_len - 1])
1301 /* Restore the indentation of the enclosing list. */
1302 print_line(".RS", MMAN_Bk_susp);
1303 snprintf(buf, sizeof(buf), "%zun", Bl_stack[Bl_stack_len - 1]);
1306 /* Remeber to close out this .RS block later. */
1307 Bl_stack_post[Bl_stack_len - 1] = 1;
1313 const struct mdoc_node *bln;
1315 bln = n->parent->parent;
1319 switch (bln->norm->Bl.type) {
1321 outflags &= ~MMAN_spc;
1325 outflags |= MMAN_br;
1332 switch (bln->norm->Bl.type) {
1344 assert(Bl_stack_len);
1345 Bl_stack[--Bl_stack_len] = 0;
1348 * Our indentation had to be restored
1349 * after a child display or child list.
1350 * Close out that indentation block now.
1352 if (Bl_stack_post[Bl_stack_len]) {
1353 print_line(".RE", MMAN_nl);
1354 Bl_stack_post[Bl_stack_len] = 0;
1358 if (NULL != n->next) {
1360 outflags &= ~MMAN_spc;
1376 if (SEC_LIBRARY == n->sec)
1377 outflags |= MMAN_br;
1383 const struct mdoc_node *link, *descr;
1385 if (NULL == (link = n->child))
1388 if (NULL != (descr = link->next)) {
1390 while (NULL != descr) {
1391 print_word(descr->string);
1392 descr = descr->next;
1399 print_word(link->string);
1417 if (MDOC_BLOCK == n->type) {
1418 outflags |= MMAN_Bk;
1421 if (MDOC_ELEM != n->type && MDOC_HEAD != n->type)
1423 name = n->child ? n->child->string : meta->name;
1426 if (MDOC_HEAD == n->type) {
1427 if (NULL == n->parent->prev)
1428 outflags |= MMAN_sp;
1429 print_block(".HP", 0);
1430 printf(" %zun", strlen(name) + 1);
1431 outflags |= MMAN_nl;
1434 if (NULL == n->child)
1435 print_word(meta->name);
1445 outflags &= ~MMAN_Bk;
1461 outflags |= MMAN_spc_force;
1469 outflags &= ~MMAN_spc;
1477 outflags &= ~MMAN_spc;
1484 if (MDOC_It != n->parent->tok)
1485 outflags |= MMAN_PP;
1486 outflags |= MMAN_sp | MMAN_nl;
1487 outflags &= ~MMAN_br;
1495 if (SEC_SEE_ALSO == n->sec) {
1496 outflags |= MMAN_PP | MMAN_sp | MMAN_nl;
1497 outflags &= ~MMAN_br;
1506 assert(n->child && MDOC_TEXT == n->child->type);
1507 if (0 == strcmp("on", n->child->string))
1508 outflags |= MMAN_Sm | MMAN_spc;
1510 outflags &= ~MMAN_Sm;
1518 if (MMAN_PP & outflags) {
1519 outflags &= ~MMAN_PP;
1520 print_line(".PP", 0);
1522 print_line(".sp", 0);
1530 outflags |= MMAN_nl;
1545 if (MDOC_SYNPRETTY & n->flags) {
1564 if (MDOC_SYNPRETTY & n->flags && MDOC_BODY != n->type)
1576 print_node(meta, n);
1580 outflags &= ~MMAN_spc;
1582 print_node(meta, n);
1591 print_word(manacts[n->tok].prefix);
1592 if (NULL == n->child)
1594 outflags &= ~MMAN_spc;
1596 outflags &= ~MMAN_spc;