]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_man.c
1 /* $Id: mdoc_man.c,v 1.65 2014/07/02 19:55:10 schwarze Exp $ */
3 * Copyright (c) 2011, 2012, 2013, 2014 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.
26 #include "mandoc_aux.h"
32 #define DECL_ARGS const struct mdoc_meta *meta, \
33 const struct mdoc_node *n
36 int (*cond
)(DECL_ARGS
); /* DON'T run actions */
37 int (*pre
)(DECL_ARGS
); /* pre-node action */
38 void (*post
)(DECL_ARGS
); /* post-node action */
39 const char *prefix
; /* pre-node string constant */
40 const char *suffix
; /* post-node string constant */
43 static int cond_body(DECL_ARGS
);
44 static int cond_head(DECL_ARGS
);
45 static void font_push(char);
46 static void font_pop(void);
47 static void mid_it(void);
48 static void post__t(DECL_ARGS
);
49 static void post_bd(DECL_ARGS
);
50 static void post_bf(DECL_ARGS
);
51 static void post_bk(DECL_ARGS
);
52 static void post_bl(DECL_ARGS
);
53 static void post_dl(DECL_ARGS
);
54 static void post_en(DECL_ARGS
);
55 static void post_enc(DECL_ARGS
);
56 static void post_eo(DECL_ARGS
);
57 static void post_fa(DECL_ARGS
);
58 static void post_fd(DECL_ARGS
);
59 static void post_fl(DECL_ARGS
);
60 static void post_fn(DECL_ARGS
);
61 static void post_fo(DECL_ARGS
);
62 static void post_font(DECL_ARGS
);
63 static void post_in(DECL_ARGS
);
64 static void post_it(DECL_ARGS
);
65 static void post_lb(DECL_ARGS
);
66 static void post_nm(DECL_ARGS
);
67 static void post_percent(DECL_ARGS
);
68 static void post_pf(DECL_ARGS
);
69 static void post_sect(DECL_ARGS
);
70 static void post_sp(DECL_ARGS
);
71 static void post_vt(DECL_ARGS
);
72 static int pre__t(DECL_ARGS
);
73 static int pre_an(DECL_ARGS
);
74 static int pre_ap(DECL_ARGS
);
75 static int pre_bd(DECL_ARGS
);
76 static int pre_bf(DECL_ARGS
);
77 static int pre_bk(DECL_ARGS
);
78 static int pre_bl(DECL_ARGS
);
79 static int pre_br(DECL_ARGS
);
80 static int pre_bx(DECL_ARGS
);
81 static int pre_dl(DECL_ARGS
);
82 static int pre_en(DECL_ARGS
);
83 static int pre_enc(DECL_ARGS
);
84 static int pre_em(DECL_ARGS
);
85 static int pre_es(DECL_ARGS
);
86 static int pre_fa(DECL_ARGS
);
87 static int pre_fd(DECL_ARGS
);
88 static int pre_fl(DECL_ARGS
);
89 static int pre_fn(DECL_ARGS
);
90 static int pre_fo(DECL_ARGS
);
91 static int pre_ft(DECL_ARGS
);
92 static int pre_in(DECL_ARGS
);
93 static int pre_it(DECL_ARGS
);
94 static int pre_lk(DECL_ARGS
);
95 static int pre_li(DECL_ARGS
);
96 static int pre_ll(DECL_ARGS
);
97 static int pre_nm(DECL_ARGS
);
98 static int pre_no(DECL_ARGS
);
99 static int pre_ns(DECL_ARGS
);
100 static int pre_pp(DECL_ARGS
);
101 static int pre_rs(DECL_ARGS
);
102 static int pre_sm(DECL_ARGS
);
103 static int pre_sp(DECL_ARGS
);
104 static int pre_sect(DECL_ARGS
);
105 static int pre_sy(DECL_ARGS
);
106 static void pre_syn(const struct mdoc_node
*);
107 static int pre_vt(DECL_ARGS
);
108 static int pre_ux(DECL_ARGS
);
109 static int pre_xr(DECL_ARGS
);
110 static void print_word(const char *);
111 static void print_line(const char *, int);
112 static void print_block(const char *, int);
113 static void print_offs(const char *);
114 static void print_width(const char *,
115 const struct mdoc_node
*, size_t);
116 static void print_count(int *);
117 static void print_node(DECL_ARGS
);
119 static const struct manact manacts
[MDOC_MAX
+ 1] = {
120 { NULL
, pre_ap
, NULL
, NULL
, NULL
}, /* Ap */
121 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dd */
122 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dt */
123 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Os */
124 { NULL
, pre_sect
, post_sect
, ".SH", NULL
}, /* Sh */
125 { NULL
, pre_sect
, post_sect
, ".SS", NULL
}, /* Ss */
126 { NULL
, pre_pp
, NULL
, NULL
, NULL
}, /* Pp */
127 { cond_body
, pre_dl
, post_dl
, NULL
, NULL
}, /* D1 */
128 { cond_body
, pre_dl
, post_dl
, NULL
, NULL
}, /* Dl */
129 { cond_body
, pre_bd
, post_bd
, NULL
, NULL
}, /* Bd */
130 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ed */
131 { cond_body
, pre_bl
, post_bl
, NULL
, NULL
}, /* Bl */
132 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* El */
133 { NULL
, pre_it
, post_it
, NULL
, NULL
}, /* It */
134 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Ad */
135 { NULL
, pre_an
, NULL
, NULL
, NULL
}, /* An */
136 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Ar */
137 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Cd */
138 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Cm */
139 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Dv */
140 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Er */
141 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Ev */
142 { NULL
, pre_enc
, post_enc
, "The \\fB",
143 "\\fP\nutility exits 0 on success, and >0 if an error occurs."
145 { NULL
, pre_fa
, post_fa
, NULL
, NULL
}, /* Fa */
146 { NULL
, pre_fd
, post_fd
, NULL
, NULL
}, /* Fd */
147 { NULL
, pre_fl
, post_fl
, NULL
, NULL
}, /* Fl */
148 { NULL
, pre_fn
, post_fn
, NULL
, NULL
}, /* Fn */
149 { NULL
, pre_ft
, post_font
, NULL
, NULL
}, /* Ft */
150 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Ic */
151 { NULL
, pre_in
, post_in
, NULL
, NULL
}, /* In */
152 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Li */
153 { cond_head
, pre_enc
, NULL
, "\\- ", NULL
}, /* Nd */
154 { NULL
, pre_nm
, post_nm
, NULL
, NULL
}, /* Nm */
155 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Op */
156 { NULL
, pre_ft
, post_font
, NULL
, NULL
}, /* Ot */
157 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Pa */
158 { NULL
, pre_enc
, post_enc
, "The \\fB",
159 "\\fP\nfunction returns the value 0 if successful;\n"
160 "otherwise the value -1 is returned and the global\n"
161 "variable \\fIerrno\\fP is set to indicate the error."
163 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* St */
164 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Va */
165 { NULL
, pre_vt
, post_vt
, NULL
, NULL
}, /* Vt */
166 { NULL
, pre_xr
, NULL
, NULL
, NULL
}, /* Xr */
167 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %A */
168 { NULL
, pre_em
, post_percent
, NULL
, NULL
}, /* %B */
169 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %D */
170 { NULL
, pre_em
, post_percent
, NULL
, NULL
}, /* %I */
171 { NULL
, pre_em
, post_percent
, NULL
, NULL
}, /* %J */
172 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %N */
173 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %O */
174 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %P */
175 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %R */
176 { NULL
, pre__t
, post__t
, NULL
, NULL
}, /* %T */
177 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %V */
178 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ac */
179 { cond_body
, pre_enc
, post_enc
, "<", ">" }, /* Ao */
180 { cond_body
, pre_enc
, post_enc
, "<", ">" }, /* Aq */
181 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* At */
182 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bc */
183 { NULL
, pre_bf
, post_bf
, NULL
, NULL
}, /* Bf */
184 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Bo */
185 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Bq */
186 { NULL
, pre_ux
, NULL
, "BSD/OS", NULL
}, /* Bsx */
187 { NULL
, pre_bx
, NULL
, NULL
, NULL
}, /* Bx */
188 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Db */
189 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dc */
190 { cond_body
, pre_enc
, post_enc
, "\\(lq", "\\(rq" }, /* Do */
191 { cond_body
, pre_enc
, post_enc
, "\\(lq", "\\(rq" }, /* Dq */
192 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ec */
193 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ef */
194 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Em */
195 { NULL
, NULL
, post_eo
, NULL
, NULL
}, /* Eo */
196 { NULL
, pre_ux
, NULL
, "FreeBSD", NULL
}, /* Fx */
197 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Ms */
198 { NULL
, pre_no
, NULL
, NULL
, NULL
}, /* No */
199 { NULL
, pre_ns
, NULL
, NULL
, NULL
}, /* Ns */
200 { NULL
, pre_ux
, NULL
, "NetBSD", NULL
}, /* Nx */
201 { NULL
, pre_ux
, NULL
, "OpenBSD", NULL
}, /* Ox */
202 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Pc */
203 { NULL
, NULL
, post_pf
, NULL
, NULL
}, /* Pf */
204 { cond_body
, pre_enc
, post_enc
, "(", ")" }, /* Po */
205 { cond_body
, pre_enc
, post_enc
, "(", ")" }, /* Pq */
206 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Qc */
207 { cond_body
, pre_enc
, post_enc
, "\\(oq", "\\(cq" }, /* Ql */
208 { cond_body
, pre_enc
, post_enc
, "\"", "\"" }, /* Qo */
209 { cond_body
, pre_enc
, post_enc
, "\"", "\"" }, /* Qq */
210 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Re */
211 { cond_body
, pre_rs
, NULL
, NULL
, NULL
}, /* Rs */
212 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Sc */
213 { cond_body
, pre_enc
, post_enc
, "\\(oq", "\\(cq" }, /* So */
214 { cond_body
, pre_enc
, post_enc
, "\\(oq", "\\(cq" }, /* Sq */
215 { NULL
, pre_sm
, NULL
, NULL
, NULL
}, /* Sm */
216 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Sx */
217 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Sy */
218 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Tn */
219 { NULL
, pre_ux
, NULL
, "UNIX", NULL
}, /* Ux */
220 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xc */
221 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xo */
222 { NULL
, pre_fo
, post_fo
, NULL
, NULL
}, /* Fo */
223 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fc */
224 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Oo */
225 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Oc */
226 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Bk */
227 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ek */
228 { NULL
, pre_ux
, NULL
, "is currently in beta test.", NULL
}, /* Bt */
229 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Hf */
230 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Fr */
231 { NULL
, pre_ux
, NULL
, "currently under development.", NULL
}, /* Ud */
232 { NULL
, NULL
, post_lb
, NULL
, NULL
}, /* Lb */
233 { NULL
, pre_pp
, NULL
, NULL
, NULL
}, /* Lp */
234 { NULL
, pre_lk
, NULL
, NULL
, NULL
}, /* Lk */
235 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Mt */
236 { cond_body
, pre_enc
, post_enc
, "{", "}" }, /* Brq */
237 { cond_body
, pre_enc
, post_enc
, "{", "}" }, /* Bro */
238 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Brc */
239 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %C */
240 { NULL
, pre_es
, NULL
, NULL
, NULL
}, /* Es */
241 { cond_body
, pre_en
, post_en
, NULL
, NULL
}, /* En */
242 { NULL
, pre_ux
, NULL
, "DragonFly", NULL
}, /* Dx */
243 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %Q */
244 { NULL
, pre_br
, NULL
, NULL
, NULL
}, /* br */
245 { NULL
, pre_sp
, post_sp
, NULL
, NULL
}, /* sp */
246 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %U */
247 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ta */
248 { NULL
, pre_ll
, post_sp
, NULL
, NULL
}, /* ll */
249 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* ROOT */
253 #define MMAN_spc (1 << 0) /* blank character before next word */
254 #define MMAN_spc_force (1 << 1) /* even before trailing punctuation */
255 #define MMAN_nl (1 << 2) /* break man(7) code line */
256 #define MMAN_br (1 << 3) /* break output line */
257 #define MMAN_sp (1 << 4) /* insert a blank output line */
258 #define MMAN_PP (1 << 5) /* reset indentation etc. */
259 #define MMAN_Sm (1 << 6) /* horizontal spacing mode */
260 #define MMAN_Bk (1 << 7) /* word keep mode */
261 #define MMAN_Bk_susp (1 << 8) /* suspend this (after a macro) */
262 #define MMAN_An_split (1 << 9) /* author mode is "split" */
263 #define MMAN_An_nosplit (1 << 10) /* author mode is "nosplit" */
264 #define MMAN_PD (1 << 11) /* inter-paragraph spacing disabled */
265 #define MMAN_nbrword (1 << 12) /* do not break the next word */
267 #define BL_STACK_MAX 32
269 static size_t Bl_stack
[BL_STACK_MAX
]; /* offsets [chars] */
270 static int Bl_stack_post
[BL_STACK_MAX
]; /* add final .RE */
271 static int Bl_stack_len
; /* number of nested Bl blocks */
272 static int TPremain
; /* characters before tag is full */
282 font_push(char newfont
)
285 if (fontqueue
.head
+ fontqueue
.size
<= ++fontqueue
.tail
) {
287 fontqueue
.head
= mandoc_realloc(fontqueue
.head
,
290 *fontqueue
.tail
= newfont
;
294 outflags
&= ~MMAN_spc
;
301 if (fontqueue
.tail
> fontqueue
.head
)
303 outflags
&= ~MMAN_spc
;
306 putchar(*fontqueue
.tail
);
310 print_word(const char *s
)
313 if ((MMAN_PP
| MMAN_sp
| MMAN_br
| MMAN_nl
) & outflags
) {
315 * If we need a newline, print it now and start afresh.
317 if (MMAN_PP
& outflags
) {
318 if (MMAN_sp
& outflags
) {
319 if (MMAN_PD
& outflags
) {
321 outflags
&= ~MMAN_PD
;
323 } else if ( ! (MMAN_PD
& outflags
)) {
328 } else if (MMAN_sp
& outflags
)
330 else if (MMAN_br
& outflags
)
332 else if (MMAN_nl
& outflags
)
334 outflags
&= ~(MMAN_PP
|MMAN_sp
|MMAN_br
|MMAN_nl
|MMAN_spc
);
338 } else if (MMAN_spc
& outflags
) {
340 * If we need a space, only print it if
341 * (1) it is forced by `No' or
342 * (2) what follows is not terminating punctuation or
343 * (3) what follows is longer than one character.
345 if (MMAN_spc_force
& outflags
|| '\0' == s
[0] ||
346 NULL
== strchr(".,:;)]?!", s
[0]) || '\0' != s
[1]) {
347 if (MMAN_Bk
& outflags
&&
348 ! (MMAN_Bk_susp
& outflags
))
357 * Reassign needing space if we're not following opening
360 if (MMAN_Sm
& outflags
&& ('\0' == s
[0] ||
361 (('(' != s
[0] && '[' != s
[0]) || '\0' != s
[1])))
362 outflags
|= MMAN_spc
;
364 outflags
&= ~MMAN_spc
;
365 outflags
&= ~(MMAN_spc_force
| MMAN_Bk_susp
);
379 if (MMAN_nbrword
& outflags
) {
385 putchar((unsigned char)*s
);
391 outflags
&= ~MMAN_nbrword
;
395 print_line(const char *s
, int newflags
)
398 outflags
&= ~MMAN_br
;
401 outflags
|= newflags
;
405 print_block(const char *s
, int newflags
)
408 outflags
&= ~MMAN_PP
;
409 if (MMAN_sp
& outflags
) {
410 outflags
&= ~(MMAN_sp
| MMAN_br
);
411 if (MMAN_PD
& outflags
) {
412 print_line(".PD", 0);
413 outflags
&= ~MMAN_PD
;
415 } else if (! (MMAN_PD
& outflags
))
416 print_line(".PD 0", MMAN_PD
);
419 outflags
|= MMAN_Bk_susp
| newflags
;
423 print_offs(const char *v
)
429 print_line(".RS", MMAN_Bk_susp
);
431 /* Convert v into a number (of characters). */
432 if (NULL
== v
|| '\0' == *v
|| 0 == strcmp(v
, "left"))
434 else if (0 == strcmp(v
, "indent"))
436 else if (0 == strcmp(v
, "indent-two"))
438 else if (a2roffsu(v
, &su
, SCALE_MAX
)) {
439 if (SCALE_EN
== su
.unit
)
444 * If we are inside an enclosing list,
445 * there is no easy way to add the two
446 * indentations because they are provided
447 * in terms of different units.
457 * We are inside an enclosing list.
458 * Add the two indentations.
461 sz
+= Bl_stack
[Bl_stack_len
- 1];
463 (void)snprintf(buf
, sizeof(buf
), "%zun", sz
);
469 * Set up the indentation for a list item; used from pre_it().
472 print_width(const char *v
, const struct mdoc_node
*child
, size_t defsz
)
482 /* Convert v into a number (of characters). */
485 else if (a2roffsu(v
, &su
, SCALE_MAX
)) {
486 if (SCALE_EN
== su
.unit
)
495 /* XXX Rough estimation, might have multiple parts. */
496 chsz
= (NULL
!= child
&& MDOC_TEXT
== child
->type
) ?
497 strlen(child
->string
) : 0;
499 /* Maybe we are inside an enclosing list? */
503 * Save our own indentation,
504 * such that child lists can use it.
506 Bl_stack
[Bl_stack_len
++] = sz
+ 2;
508 /* Set up the current list. */
509 if (defsz
&& chsz
> sz
)
510 print_block(".HP", 0);
512 print_block(".TP", 0);
516 (void)snprintf(buf
, sizeof(buf
), "%zun", sz
+ 2);
524 print_count(int *count
)
528 (void)snprintf(buf
, sizeof(buf
), "%d.", ++*count
);
533 man_man(void *arg
, const struct man
*man
)
537 * Dump the keep buffer.
538 * We're guaranteed by now that this exists (is non-NULL).
539 * Flush stdout afterward, just in case.
541 fputs(mparse_getkeep(man_mparse(man
)), stdout
);
546 man_mdoc(void *arg
, const struct mdoc
*mdoc
)
548 const struct mdoc_meta
*meta
;
549 const struct mdoc_node
*n
;
551 meta
= mdoc_meta(mdoc
);
554 printf(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"\n",
555 meta
->title
, meta
->msec
, meta
->date
,
556 meta
->os
, meta
->vol
);
558 /* Disable hyphenation and if nroff, disable justification. */
559 printf(".nh\n.if n .ad l");
561 outflags
= MMAN_nl
| MMAN_Sm
;
562 if (0 == fontqueue
.size
) {
564 fontqueue
.head
= fontqueue
.tail
= mandoc_malloc(8);
565 *fontqueue
.tail
= 'R';
572 print_node(DECL_ARGS
)
574 const struct mdoc_node
*sub
;
575 const struct manact
*act
;
579 * Break the line if we were parsed subsequent the current node.
580 * This makes the page structure be more consistent.
582 if (MMAN_spc
& outflags
&& MDOC_LINE
& n
->flags
)
589 if (MDOC_TEXT
== n
->type
) {
591 * Make sure that we don't happen to start with a
592 * control character at the start of a line.
594 if (MMAN_nl
& outflags
&&
595 ('.' == *n
->string
|| '\'' == *n
->string
)) {
598 outflags
&= ~MMAN_spc
;
600 print_word(n
->string
);
603 * Conditionally run the pre-node action handler for a
606 act
= manacts
+ n
->tok
;
607 cond
= NULL
== act
->cond
|| (*act
->cond
)(meta
, n
);
608 if (cond
&& act
->pre
&& ENDBODY_NOT
== n
->end
)
609 do_sub
= (*act
->pre
)(meta
, n
);
613 * Conditionally run all child nodes.
614 * Note that this iterates over children instead of using
615 * recursion. This prevents unnecessary depth in the stack.
618 for (sub
= n
->child
; sub
; sub
= sub
->next
)
619 print_node(meta
, sub
);
622 * Lastly, conditionally run the post-node handler.
624 if (MDOC_ENDED
& n
->flags
)
627 if (cond
&& act
->post
)
628 (*act
->post
)(meta
, n
);
630 if (ENDBODY_NOT
!= n
->end
)
631 n
->pending
->flags
|= MDOC_ENDED
;
633 if (ENDBODY_NOSPACE
== n
->end
)
634 outflags
&= ~(MMAN_spc
| MMAN_nl
);
641 return(MDOC_HEAD
== n
->type
);
648 return(MDOC_BODY
== n
->type
);
656 prefix
= manacts
[n
->tok
].prefix
;
660 outflags
&= ~MMAN_spc
;
669 suffix
= manacts
[n
->tok
].suffix
;
672 outflags
&= ~(MMAN_spc
| MMAN_nl
);
684 post_percent(DECL_ARGS
)
687 if (pre_em
== manacts
[n
->tok
].pre
)
691 if (n
->prev
&& n
->prev
->tok
== n
->tok
&&
692 n
->next
->tok
== n
->tok
)
704 if (n
->parent
&& MDOC_Rs
== n
->parent
->tok
&&
705 n
->parent
->norm
->Rs
.quote_T
) {
708 outflags
&= ~MMAN_spc
;
718 if (n
->parent
&& MDOC_Rs
== n
->parent
->tok
&&
719 n
->parent
->norm
->Rs
.quote_T
) {
720 outflags
&= ~MMAN_spc
;
725 post_percent(meta
, n
);
729 * Print before a section header.
735 if (MDOC_HEAD
== n
->type
) {
737 print_block(manacts
[n
->tok
].prefix
, 0);
740 outflags
&= ~MMAN_spc
;
746 * Print subsequent a section header.
752 if (MDOC_HEAD
!= n
->type
)
754 outflags
&= ~MMAN_spc
;
758 if (MDOC_Sh
== n
->tok
&& SEC_AUTHORS
== n
->sec
)
759 outflags
&= ~(MMAN_An_split
| MMAN_An_nosplit
);
762 /* See mdoc_term.c, synopsis_pre() for comments. */
764 pre_syn(const struct mdoc_node
*n
)
767 if (NULL
== n
->prev
|| ! (MDOC_SYNPRETTY
& n
->flags
))
770 if (n
->prev
->tok
== n
->tok
&&
778 switch (n
->prev
->tok
) {
791 if (MDOC_Fn
!= n
->tok
&& MDOC_Fo
!= n
->tok
) {
806 switch (n
->norm
->An
.auth
) {
808 outflags
&= ~MMAN_An_nosplit
;
809 outflags
|= MMAN_An_split
;
812 outflags
&= ~MMAN_An_split
;
813 outflags
|= MMAN_An_nosplit
;
816 if (MMAN_An_split
& outflags
)
818 else if (SEC_AUTHORS
== n
->sec
&&
819 ! (MMAN_An_nosplit
& outflags
))
820 outflags
|= MMAN_An_split
;
829 outflags
&= ~MMAN_spc
;
831 outflags
&= ~MMAN_spc
;
839 outflags
&= ~(MMAN_PP
| MMAN_sp
| MMAN_br
);
841 if (DISP_unfilled
== n
->norm
->Bd
.type
||
842 DISP_literal
== n
->norm
->Bd
.type
)
843 print_line(".nf", 0);
844 if (0 == n
->norm
->Bd
.comp
&& NULL
!= n
->parent
->prev
)
846 print_offs(n
->norm
->Bd
.offs
);
854 /* Close out this display. */
855 print_line(".RE", MMAN_nl
);
856 if (DISP_unfilled
== n
->norm
->Bd
.type
||
857 DISP_literal
== n
->norm
->Bd
.type
)
858 print_line(".fi", MMAN_nl
);
860 /* Maybe we are inside an enclosing list? */
861 if (NULL
!= n
->parent
->next
)
877 switch (n
->norm
->Bf
.font
) {
895 if (MDOC_BODY
== n
->type
)
918 if (MDOC_BODY
== n
->type
)
919 outflags
&= ~MMAN_Bk
;
928 * print_offs() will increase the -offset to account for
929 * a possible enclosing .It, but any enclosed .It blocks
930 * just nest and do not add up their indentation.
932 if (n
->norm
->Bl
.offs
) {
933 print_offs(n
->norm
->Bl
.offs
);
934 Bl_stack
[Bl_stack_len
++] = 0;
937 switch (n
->norm
->Bl
.type
) {
939 n
->norm
->Bl
.count
= 0;
947 print_line(".TS", MMAN_nl
);
948 for (icol
= 0; icol
< n
->norm
->Bl
.ncols
; icol
++)
959 switch (n
->norm
->Bl
.type
) {
961 print_line(".TE", 0);
964 n
->norm
->Bl
.count
= 0;
970 if (n
->norm
->Bl
.offs
) {
971 print_line(".RE", MMAN_nl
);
972 assert(Bl_stack_len
);
974 assert(0 == Bl_stack
[Bl_stack_len
]);
976 outflags
|= MMAN_PP
| MMAN_nl
;
977 outflags
&= ~(MMAN_sp
| MMAN_br
);
980 /* Maybe we are inside an enclosing list? */
981 if (NULL
!= n
->parent
->next
)
1000 print_word(n
->string
);
1001 outflags
&= ~MMAN_spc
;
1007 outflags
&= ~MMAN_spc
;
1009 outflags
&= ~MMAN_spc
;
1010 print_word(n
->string
);
1026 print_line(".RE", MMAN_nl
);
1028 /* Maybe we are inside an enclosing list? */
1029 if (NULL
!= n
->parent
->next
)
1045 if (NULL
== n
->norm
->Es
||
1046 NULL
== n
->norm
->Es
->child
)
1049 print_word(n
->norm
->Es
->child
->string
);
1050 outflags
&= ~MMAN_spc
;
1058 if (NULL
== n
->norm
->Es
||
1059 NULL
== n
->norm
->Es
->child
||
1060 NULL
== n
->norm
->Es
->child
->next
)
1063 outflags
&= ~MMAN_spc
;
1064 print_word(n
->norm
->Es
->child
->next
->string
);
1072 if (MDOC_HEAD
== n
->type
|| MDOC_BODY
== n
->type
)
1073 outflags
&= ~MMAN_spc
;
1088 am_Fa
= MDOC_Fa
== n
->tok
;
1095 if (am_Fa
|| MDOC_SYNPRETTY
& n
->flags
)
1096 outflags
|= MMAN_nbrword
;
1097 print_node(meta
, n
);
1099 if (NULL
!= (n
= n
->next
))
1109 if (NULL
!= n
->next
&& MDOC_Fa
== n
->next
->tok
)
1127 outflags
|= MMAN_br
;
1136 outflags
&= ~MMAN_spc
;
1145 if (0 == n
->nchild
&& NULL
!= n
->next
&&
1146 n
->next
->line
== n
->line
)
1147 outflags
&= ~MMAN_spc
;
1160 if (MDOC_SYNPRETTY
& n
->flags
)
1161 print_block(".HP 4n", MMAN_nl
);
1164 print_node(meta
, n
);
1166 outflags
&= ~MMAN_spc
;
1168 outflags
&= ~MMAN_spc
;
1181 if (MDOC_SYNPRETTY
& n
->flags
) {
1183 outflags
|= MMAN_PP
;
1196 if (MDOC_SYNPRETTY
& n
->flags
)
1197 print_block(".HP 4n", MMAN_nl
);
1201 outflags
&= ~MMAN_spc
;
1203 outflags
&= ~MMAN_spc
;
1240 if (MDOC_SYNPRETTY
& n
->flags
) {
1243 print_word("#include <");
1244 outflags
&= ~MMAN_spc
;
1247 outflags
&= ~MMAN_spc
;
1257 if (MDOC_SYNPRETTY
& n
->flags
) {
1258 outflags
&= ~MMAN_spc
;
1261 outflags
|= MMAN_br
;
1264 outflags
&= ~MMAN_spc
;
1272 const struct mdoc_node
*bln
;
1276 outflags
|= MMAN_PP
| MMAN_nl
;
1277 bln
= n
->parent
->parent
;
1278 if (0 == bln
->norm
->Bl
.comp
||
1279 (NULL
== n
->parent
->prev
&&
1280 NULL
== bln
->parent
->prev
))
1281 outflags
|= MMAN_sp
;
1282 outflags
&= ~MMAN_br
;
1283 switch (bln
->norm
->Bl
.type
) {
1291 if (bln
->norm
->Bl
.type
== LIST_diag
)
1292 print_line(".B \"", 0);
1294 print_line(".R \"", 0);
1295 outflags
&= ~MMAN_spc
;
1302 print_width(bln
->norm
->Bl
.width
, NULL
, 0);
1304 outflags
|= MMAN_nl
;
1306 if (LIST_bullet
== bln
->norm
->Bl
.type
)
1313 print_width(bln
->norm
->Bl
.width
, NULL
, 0);
1315 outflags
|= MMAN_nl
;
1316 print_count(&bln
->norm
->Bl
.count
);
1319 print_width(bln
->norm
->Bl
.width
, n
->child
, 6);
1323 print_width(bln
->norm
->Bl
.width
, n
->child
, 0);
1325 outflags
&= ~MMAN_spc
;
1330 outflags
|= MMAN_nl
;
1338 * This function is called after closing out an indented block.
1339 * If we are inside an enclosing list, restore its indentation.
1346 /* Nothing to do outside a list. */
1347 if (0 == Bl_stack_len
|| 0 == Bl_stack
[Bl_stack_len
- 1])
1350 /* The indentation has already been set up. */
1351 if (Bl_stack_post
[Bl_stack_len
- 1])
1354 /* Restore the indentation of the enclosing list. */
1355 print_line(".RS", MMAN_Bk_susp
);
1356 (void)snprintf(buf
, sizeof(buf
), "%zun",
1357 Bl_stack
[Bl_stack_len
- 1]);
1360 /* Remeber to close out this .RS block later. */
1361 Bl_stack_post
[Bl_stack_len
- 1] = 1;
1367 const struct mdoc_node
*bln
;
1369 bln
= n
->parent
->parent
;
1373 switch (bln
->norm
->Bl
.type
) {
1375 outflags
&= ~MMAN_spc
;
1379 outflags
|= MMAN_br
;
1386 switch (bln
->norm
->Bl
.type
) {
1398 assert(Bl_stack_len
);
1399 Bl_stack
[--Bl_stack_len
] = 0;
1402 * Our indentation had to be restored
1403 * after a child display or child list.
1404 * Close out that indentation block now.
1406 if (Bl_stack_post
[Bl_stack_len
]) {
1407 print_line(".RE", MMAN_nl
);
1408 Bl_stack_post
[Bl_stack_len
] = 0;
1412 if (NULL
!= n
->next
) {
1414 outflags
&= ~MMAN_spc
;
1430 if (SEC_LIBRARY
== n
->sec
)
1431 outflags
|= MMAN_br
;
1437 const struct mdoc_node
*link
, *descr
;
1439 if (NULL
== (link
= n
->child
))
1442 if (NULL
!= (descr
= link
->next
)) {
1444 while (NULL
!= descr
) {
1445 print_word(descr
->string
);
1446 descr
= descr
->next
;
1453 print_word(link
->string
);
1462 print_line(".ll", 0);
1479 if (MDOC_BLOCK
== n
->type
) {
1480 outflags
|= MMAN_Bk
;
1483 if (MDOC_ELEM
!= n
->type
&& MDOC_HEAD
!= n
->type
)
1485 name
= n
->child
? n
->child
->string
: meta
->name
;
1488 if (MDOC_HEAD
== n
->type
) {
1489 if (NULL
== n
->parent
->prev
)
1490 outflags
|= MMAN_sp
;
1491 print_block(".HP", 0);
1492 printf(" %zun", strlen(name
) + 1);
1493 outflags
|= MMAN_nl
;
1496 if (NULL
== n
->child
)
1497 print_word(meta
->name
);
1507 outflags
&= ~MMAN_Bk
;
1523 outflags
|= MMAN_spc_force
;
1531 outflags
&= ~MMAN_spc
;
1539 outflags
&= ~MMAN_spc
;
1546 if (MDOC_It
!= n
->parent
->tok
)
1547 outflags
|= MMAN_PP
;
1548 outflags
|= MMAN_sp
| MMAN_nl
;
1549 outflags
&= ~MMAN_br
;
1557 if (SEC_SEE_ALSO
== n
->sec
) {
1558 outflags
|= MMAN_PP
| MMAN_sp
| MMAN_nl
;
1559 outflags
&= ~MMAN_br
;
1568 if (NULL
== n
->child
)
1569 outflags
^= MMAN_Sm
;
1570 else if (0 == strcmp("on", n
->child
->string
))
1571 outflags
|= MMAN_Sm
;
1573 outflags
&= ~MMAN_Sm
;
1575 if (MMAN_Sm
& outflags
)
1576 outflags
|= MMAN_spc
;
1585 if (MMAN_PP
& outflags
) {
1586 outflags
&= ~MMAN_PP
;
1587 print_line(".PP", 0);
1589 print_line(".sp", 0);
1597 outflags
|= MMAN_nl
;
1612 if (MDOC_SYNPRETTY
& n
->flags
) {
1631 if (MDOC_SYNPRETTY
& n
->flags
&& MDOC_BODY
!= n
->type
)
1643 print_node(meta
, n
);
1647 outflags
&= ~MMAN_spc
;
1649 print_node(meta
, n
);
1658 print_word(manacts
[n
->tok
].prefix
);
1659 if (NULL
== n
->child
)
1661 outflags
&= ~MMAN_spc
;
1663 outflags
&= ~MMAN_spc
;