]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_man.c
1 /* $Id: mdoc_man.c,v 1.85 2015/02/06 03:38:45 schwarze Exp $ */
3 * Copyright (c) 2011-2015 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.
19 #include <sys/types.h>
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_aq(DECL_ARGS
);
50 static void post_bd(DECL_ARGS
);
51 static void post_bf(DECL_ARGS
);
52 static void post_bk(DECL_ARGS
);
53 static void post_bl(DECL_ARGS
);
54 static void post_dl(DECL_ARGS
);
55 static void post_en(DECL_ARGS
);
56 static void post_enc(DECL_ARGS
);
57 static void post_eo(DECL_ARGS
);
58 static void post_fa(DECL_ARGS
);
59 static void post_fd(DECL_ARGS
);
60 static void post_fl(DECL_ARGS
);
61 static void post_fn(DECL_ARGS
);
62 static void post_fo(DECL_ARGS
);
63 static void post_font(DECL_ARGS
);
64 static void post_in(DECL_ARGS
);
65 static void post_it(DECL_ARGS
);
66 static void post_lb(DECL_ARGS
);
67 static void post_nm(DECL_ARGS
);
68 static void post_percent(DECL_ARGS
);
69 static void post_pf(DECL_ARGS
);
70 static void post_sect(DECL_ARGS
);
71 static void post_sp(DECL_ARGS
);
72 static void post_vt(DECL_ARGS
);
73 static int pre__t(DECL_ARGS
);
74 static int pre_an(DECL_ARGS
);
75 static int pre_ap(DECL_ARGS
);
76 static int pre_aq(DECL_ARGS
);
77 static int pre_bd(DECL_ARGS
);
78 static int pre_bf(DECL_ARGS
);
79 static int pre_bk(DECL_ARGS
);
80 static int pre_bl(DECL_ARGS
);
81 static int pre_br(DECL_ARGS
);
82 static int pre_bx(DECL_ARGS
);
83 static int pre_dl(DECL_ARGS
);
84 static int pre_en(DECL_ARGS
);
85 static int pre_enc(DECL_ARGS
);
86 static int pre_em(DECL_ARGS
);
87 static int pre_skip(DECL_ARGS
);
88 static int pre_eo(DECL_ARGS
);
89 static int pre_ex(DECL_ARGS
);
90 static int pre_fa(DECL_ARGS
);
91 static int pre_fd(DECL_ARGS
);
92 static int pre_fl(DECL_ARGS
);
93 static int pre_fn(DECL_ARGS
);
94 static int pre_fo(DECL_ARGS
);
95 static int pre_ft(DECL_ARGS
);
96 static int pre_in(DECL_ARGS
);
97 static int pre_it(DECL_ARGS
);
98 static int pre_lk(DECL_ARGS
);
99 static int pre_li(DECL_ARGS
);
100 static int pre_ll(DECL_ARGS
);
101 static int pre_nm(DECL_ARGS
);
102 static int pre_no(DECL_ARGS
);
103 static int pre_ns(DECL_ARGS
);
104 static int pre_pp(DECL_ARGS
);
105 static int pre_rs(DECL_ARGS
);
106 static int pre_rv(DECL_ARGS
);
107 static int pre_sm(DECL_ARGS
);
108 static int pre_sp(DECL_ARGS
);
109 static int pre_sect(DECL_ARGS
);
110 static int pre_sy(DECL_ARGS
);
111 static void pre_syn(const struct mdoc_node
*);
112 static int pre_vt(DECL_ARGS
);
113 static int pre_ux(DECL_ARGS
);
114 static int pre_xr(DECL_ARGS
);
115 static void print_word(const char *);
116 static void print_line(const char *, int);
117 static void print_block(const char *, int);
118 static void print_offs(const char *, int);
119 static void print_width(const struct mdoc_bl
*,
120 const struct mdoc_node
*);
121 static void print_count(int *);
122 static void print_node(DECL_ARGS
);
124 static const struct manact manacts
[MDOC_MAX
+ 1] = {
125 { NULL
, pre_ap
, NULL
, NULL
, NULL
}, /* Ap */
126 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dd */
127 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dt */
128 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Os */
129 { NULL
, pre_sect
, post_sect
, ".SH", NULL
}, /* Sh */
130 { NULL
, pre_sect
, post_sect
, ".SS", NULL
}, /* Ss */
131 { NULL
, pre_pp
, NULL
, NULL
, NULL
}, /* Pp */
132 { cond_body
, pre_dl
, post_dl
, NULL
, NULL
}, /* D1 */
133 { cond_body
, pre_dl
, post_dl
, NULL
, NULL
}, /* Dl */
134 { cond_body
, pre_bd
, post_bd
, NULL
, NULL
}, /* Bd */
135 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ed */
136 { cond_body
, pre_bl
, post_bl
, NULL
, NULL
}, /* Bl */
137 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* El */
138 { NULL
, pre_it
, post_it
, NULL
, NULL
}, /* It */
139 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Ad */
140 { NULL
, pre_an
, NULL
, NULL
, NULL
}, /* An */
141 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Ar */
142 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Cd */
143 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Cm */
144 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Dv */
145 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Er */
146 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Ev */
147 { NULL
, pre_ex
, NULL
, NULL
, NULL
}, /* Ex */
148 { NULL
, pre_fa
, post_fa
, NULL
, NULL
}, /* Fa */
149 { NULL
, pre_fd
, post_fd
, NULL
, NULL
}, /* Fd */
150 { NULL
, pre_fl
, post_fl
, NULL
, NULL
}, /* Fl */
151 { NULL
, pre_fn
, post_fn
, NULL
, NULL
}, /* Fn */
152 { NULL
, pre_ft
, post_font
, NULL
, NULL
}, /* Ft */
153 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Ic */
154 { NULL
, pre_in
, post_in
, NULL
, NULL
}, /* In */
155 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Li */
156 { cond_head
, pre_enc
, NULL
, "\\- ", NULL
}, /* Nd */
157 { NULL
, pre_nm
, post_nm
, NULL
, NULL
}, /* Nm */
158 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Op */
159 { NULL
, pre_ft
, post_font
, NULL
, NULL
}, /* Ot */
160 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Pa */
161 { NULL
, pre_rv
, NULL
, NULL
, NULL
}, /* Rv */
162 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* St */
163 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Va */
164 { NULL
, pre_vt
, post_vt
, NULL
, NULL
}, /* Vt */
165 { NULL
, pre_xr
, NULL
, NULL
, NULL
}, /* Xr */
166 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %A */
167 { NULL
, pre_em
, post_percent
, NULL
, NULL
}, /* %B */
168 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %D */
169 { NULL
, pre_em
, post_percent
, NULL
, NULL
}, /* %I */
170 { NULL
, pre_em
, post_percent
, NULL
, NULL
}, /* %J */
171 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %N */
172 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %O */
173 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %P */
174 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %R */
175 { NULL
, pre__t
, post__t
, NULL
, NULL
}, /* %T */
176 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %V */
177 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ac */
178 { cond_body
, pre_aq
, post_aq
, NULL
, NULL
}, /* Ao */
179 { cond_body
, pre_aq
, post_aq
, NULL
, NULL
}, /* Aq */
180 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* At */
181 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bc */
182 { NULL
, pre_bf
, post_bf
, NULL
, NULL
}, /* Bf */
183 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Bo */
184 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Bq */
185 { NULL
, pre_ux
, NULL
, "BSD/OS", NULL
}, /* Bsx */
186 { NULL
, pre_bx
, NULL
, NULL
, NULL
}, /* Bx */
187 { NULL
, pre_skip
, NULL
, NULL
, NULL
}, /* Db */
188 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dc */
189 { cond_body
, pre_enc
, post_enc
, "\\(lq", "\\(rq" }, /* Do */
190 { cond_body
, pre_enc
, post_enc
, "\\(lq", "\\(rq" }, /* Dq */
191 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ec */
192 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ef */
193 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Em */
194 { cond_body
, pre_eo
, post_eo
, NULL
, NULL
}, /* Eo */
195 { NULL
, pre_ux
, NULL
, "FreeBSD", NULL
}, /* Fx */
196 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Ms */
197 { NULL
, pre_no
, NULL
, NULL
, NULL
}, /* No */
198 { NULL
, pre_ns
, NULL
, NULL
, NULL
}, /* Ns */
199 { NULL
, pre_ux
, NULL
, "NetBSD", NULL
}, /* Nx */
200 { NULL
, pre_ux
, NULL
, "OpenBSD", NULL
}, /* Ox */
201 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Pc */
202 { NULL
, NULL
, post_pf
, NULL
, NULL
}, /* Pf */
203 { cond_body
, pre_enc
, post_enc
, "(", ")" }, /* Po */
204 { cond_body
, pre_enc
, post_enc
, "(", ")" }, /* Pq */
205 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Qc */
206 { cond_body
, pre_enc
, post_enc
, "\\(oq", "\\(cq" }, /* Ql */
207 { cond_body
, pre_enc
, post_enc
, "\"", "\"" }, /* Qo */
208 { cond_body
, pre_enc
, post_enc
, "\"", "\"" }, /* Qq */
209 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Re */
210 { cond_body
, pre_rs
, NULL
, NULL
, NULL
}, /* Rs */
211 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Sc */
212 { cond_body
, pre_enc
, post_enc
, "\\(oq", "\\(cq" }, /* So */
213 { cond_body
, pre_enc
, post_enc
, "\\(oq", "\\(cq" }, /* Sq */
214 { NULL
, pre_sm
, NULL
, NULL
, NULL
}, /* Sm */
215 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Sx */
216 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Sy */
217 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Tn */
218 { NULL
, pre_ux
, NULL
, "UNIX", NULL
}, /* Ux */
219 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xc */
220 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xo */
221 { NULL
, pre_fo
, post_fo
, NULL
, NULL
}, /* Fo */
222 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fc */
223 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Oo */
224 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Oc */
225 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Bk */
226 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ek */
227 { NULL
, pre_ux
, NULL
, "is currently in beta test.", NULL
}, /* Bt */
228 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Hf */
229 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Fr */
230 { NULL
, pre_ux
, NULL
, "currently under development.", NULL
}, /* Ud */
231 { NULL
, NULL
, post_lb
, NULL
, NULL
}, /* Lb */
232 { NULL
, pre_pp
, NULL
, NULL
, NULL
}, /* Lp */
233 { NULL
, pre_lk
, NULL
, NULL
, NULL
}, /* Lk */
234 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Mt */
235 { cond_body
, pre_enc
, post_enc
, "{", "}" }, /* Brq */
236 { cond_body
, pre_enc
, post_enc
, "{", "}" }, /* Bro */
237 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Brc */
238 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %C */
239 { NULL
, pre_skip
, NULL
, NULL
, NULL
}, /* Es */
240 { cond_body
, pre_en
, post_en
, NULL
, NULL
}, /* En */
241 { NULL
, pre_ux
, NULL
, "DragonFly", NULL
}, /* Dx */
242 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %Q */
243 { NULL
, pre_br
, NULL
, NULL
, NULL
}, /* br */
244 { NULL
, pre_sp
, post_sp
, NULL
, NULL
}, /* sp */
245 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %U */
246 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ta */
247 { NULL
, pre_ll
, post_sp
, NULL
, NULL
}, /* ll */
248 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* ROOT */
252 #define MMAN_spc (1 << 0) /* blank character before next word */
253 #define MMAN_spc_force (1 << 1) /* even before trailing punctuation */
254 #define MMAN_nl (1 << 2) /* break man(7) code line */
255 #define MMAN_br (1 << 3) /* break output line */
256 #define MMAN_sp (1 << 4) /* insert a blank output line */
257 #define MMAN_PP (1 << 5) /* reset indentation etc. */
258 #define MMAN_Sm (1 << 6) /* horizontal spacing mode */
259 #define MMAN_Bk (1 << 7) /* word keep mode */
260 #define MMAN_Bk_susp (1 << 8) /* suspend this (after a macro) */
261 #define MMAN_An_split (1 << 9) /* author mode is "split" */
262 #define MMAN_An_nosplit (1 << 10) /* author mode is "nosplit" */
263 #define MMAN_PD (1 << 11) /* inter-paragraph spacing disabled */
264 #define MMAN_nbrword (1 << 12) /* do not break the next word */
266 #define BL_STACK_MAX 32
268 static int Bl_stack
[BL_STACK_MAX
]; /* offsets [chars] */
269 static int Bl_stack_post
[BL_STACK_MAX
]; /* add final .RE */
270 static int Bl_stack_len
; /* number of nested Bl blocks */
271 static int TPremain
; /* characters before tag is full */
281 font_push(char newfont
)
284 if (fontqueue
.head
+ fontqueue
.size
<= ++fontqueue
.tail
) {
286 fontqueue
.head
= mandoc_realloc(fontqueue
.head
,
289 *fontqueue
.tail
= newfont
;
293 outflags
&= ~MMAN_spc
;
300 if (fontqueue
.tail
> fontqueue
.head
)
302 outflags
&= ~MMAN_spc
;
305 putchar(*fontqueue
.tail
);
309 print_word(const char *s
)
312 if ((MMAN_PP
| MMAN_sp
| MMAN_br
| MMAN_nl
) & outflags
) {
314 * If we need a newline, print it now and start afresh.
316 if (MMAN_PP
& outflags
) {
317 if (MMAN_sp
& outflags
) {
318 if (MMAN_PD
& outflags
) {
320 outflags
&= ~MMAN_PD
;
322 } else if ( ! (MMAN_PD
& outflags
)) {
327 } else if (MMAN_sp
& outflags
)
329 else if (MMAN_br
& outflags
)
331 else if (MMAN_nl
& outflags
)
333 outflags
&= ~(MMAN_PP
|MMAN_sp
|MMAN_br
|MMAN_nl
|MMAN_spc
);
337 } else if (MMAN_spc
& outflags
) {
339 * If we need a space, only print it if
340 * (1) it is forced by `No' or
341 * (2) what follows is not terminating punctuation or
342 * (3) what follows is longer than one character.
344 if (MMAN_spc_force
& outflags
|| '\0' == s
[0] ||
345 NULL
== strchr(".,:;)]?!", s
[0]) || '\0' != s
[1]) {
346 if (MMAN_Bk
& outflags
&&
347 ! (MMAN_Bk_susp
& outflags
))
356 * Reassign needing space if we're not following opening
359 if (MMAN_Sm
& outflags
&& ('\0' == s
[0] ||
360 (('(' != s
[0] && '[' != s
[0]) || '\0' != s
[1])))
361 outflags
|= MMAN_spc
;
363 outflags
&= ~MMAN_spc
;
364 outflags
&= ~(MMAN_spc_force
| MMAN_Bk_susp
);
378 if (MMAN_nbrword
& outflags
) {
384 putchar((unsigned char)*s
);
390 outflags
&= ~MMAN_nbrword
;
394 print_line(const char *s
, int newflags
)
397 outflags
&= ~MMAN_br
;
400 outflags
|= newflags
;
404 print_block(const char *s
, int newflags
)
407 outflags
&= ~MMAN_PP
;
408 if (MMAN_sp
& outflags
) {
409 outflags
&= ~(MMAN_sp
| MMAN_br
);
410 if (MMAN_PD
& outflags
) {
411 print_line(".PD", 0);
412 outflags
&= ~MMAN_PD
;
414 } else if (! (MMAN_PD
& outflags
))
415 print_line(".PD 0", MMAN_PD
);
418 outflags
|= MMAN_Bk_susp
| newflags
;
422 print_offs(const char *v
, int keywords
)
428 print_line(".RS", MMAN_Bk_susp
);
430 /* Convert v into a number (of characters). */
431 if (NULL
== v
|| '\0' == *v
|| (keywords
&& !strcmp(v
, "left")))
433 else if (keywords
&& !strcmp(v
, "indent"))
435 else if (keywords
&& !strcmp(v
, "indent-two"))
437 else if (a2roffsu(v
, &su
, SCALE_EN
) > 1) {
438 if (SCALE_EN
== su
.unit
)
443 * If we are inside an enclosing list,
444 * there is no easy way to add the two
445 * indentations because they are provided
446 * in terms of different units.
456 * We are inside an enclosing list.
457 * Add the two indentations.
460 sz
+= Bl_stack
[Bl_stack_len
- 1];
462 (void)snprintf(buf
, sizeof(buf
), "%dn", sz
);
468 * Set up the indentation for a list item; used from pre_it().
471 print_width(const struct mdoc_bl
*bl
, const struct mdoc_node
*child
)
475 int numeric
, remain
, sz
, chsz
;
480 /* Convert the width into a number (of characters). */
481 if (bl
->width
== NULL
)
482 sz
= (bl
->type
== LIST_hang
) ? 6 : 0;
483 else if (a2roffsu(bl
->width
, &su
, SCALE_MAX
) > 1) {
484 if (SCALE_EN
== su
.unit
)
491 sz
= strlen(bl
->width
);
493 /* XXX Rough estimation, might have multiple parts. */
494 if (bl
->type
== LIST_enum
)
495 chsz
= (bl
->count
> 8) + 1;
496 else if (child
!= NULL
&& child
->type
== MDOC_TEXT
)
497 chsz
= strlen(child
->string
);
501 /* Maybe we are inside an enclosing list? */
505 * Save our own indentation,
506 * such that child lists can use it.
508 Bl_stack
[Bl_stack_len
++] = sz
+ 2;
510 /* Set up the current list. */
511 if (chsz
> sz
&& bl
->type
!= LIST_tag
)
512 print_block(".HP", 0);
514 print_block(".TP", 0);
518 (void)snprintf(buf
, sizeof(buf
), "%dn", sz
+ 2);
521 print_word(bl
->width
);
526 print_count(int *count
)
530 (void)snprintf(buf
, sizeof(buf
), "%d.\\&", ++*count
);
535 man_man(void *arg
, const struct man
*man
)
539 * Dump the keep buffer.
540 * We're guaranteed by now that this exists (is non-NULL).
541 * Flush stdout afterward, just in case.
543 fputs(mparse_getkeep(man_mparse(man
)), stdout
);
548 man_mdoc(void *arg
, const struct mdoc
*mdoc
)
550 const struct mdoc_meta
*meta
;
551 const struct mdoc_node
*n
;
553 meta
= mdoc_meta(mdoc
);
556 printf(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"\n",
558 (meta
->msec
== NULL
? "" : meta
->msec
),
559 meta
->date
, meta
->os
, meta
->vol
);
561 /* Disable hyphenation and if nroff, disable justification. */
562 printf(".nh\n.if n .ad l");
564 outflags
= MMAN_nl
| MMAN_Sm
;
565 if (0 == fontqueue
.size
) {
567 fontqueue
.head
= fontqueue
.tail
= mandoc_malloc(8);
568 *fontqueue
.tail
= 'R';
575 print_node(DECL_ARGS
)
577 const struct mdoc_node
*sub
;
578 const struct manact
*act
;
582 * Break the line if we were parsed subsequent the current node.
583 * This makes the page structure be more consistent.
585 if (MMAN_spc
& outflags
&& MDOC_LINE
& n
->flags
)
592 if (MDOC_TEXT
== n
->type
) {
594 * Make sure that we don't happen to start with a
595 * control character at the start of a line.
597 if (MMAN_nl
& outflags
&&
598 ('.' == *n
->string
|| '\'' == *n
->string
)) {
601 outflags
&= ~MMAN_spc
;
603 if (outflags
& MMAN_Sm
&& ! (n
->flags
& MDOC_DELIMC
))
604 outflags
|= MMAN_spc_force
;
605 print_word(n
->string
);
606 if (outflags
& MMAN_Sm
&& ! (n
->flags
& MDOC_DELIMO
))
607 outflags
|= MMAN_spc
;
610 * Conditionally run the pre-node action handler for a
613 act
= manacts
+ n
->tok
;
614 cond
= act
->cond
== NULL
|| (*act
->cond
)(meta
, n
);
615 if (cond
&& act
->pre
&& (n
->end
== ENDBODY_NOT
|| n
->nchild
))
616 do_sub
= (*act
->pre
)(meta
, n
);
620 * Conditionally run all child nodes.
621 * Note that this iterates over children instead of using
622 * recursion. This prevents unnecessary depth in the stack.
625 for (sub
= n
->child
; sub
; sub
= sub
->next
)
626 print_node(meta
, sub
);
629 * Lastly, conditionally run the post-node handler.
631 if (MDOC_ENDED
& n
->flags
)
634 if (cond
&& act
->post
)
635 (*act
->post
)(meta
, n
);
637 if (ENDBODY_NOT
!= n
->end
)
638 n
->pending
->flags
|= MDOC_ENDED
;
640 if (ENDBODY_NOSPACE
== n
->end
)
641 outflags
&= ~(MMAN_spc
| MMAN_nl
);
648 return(MDOC_HEAD
== n
->type
);
655 return(MDOC_BODY
== n
->type
);
663 prefix
= manacts
[n
->tok
].prefix
;
667 outflags
&= ~MMAN_spc
;
676 suffix
= manacts
[n
->tok
].suffix
;
679 outflags
&= ~(MMAN_spc
| MMAN_nl
);
688 outflags
|= MMAN_br
| MMAN_nl
;
693 for (n
= n
->child
; n
; n
= n
->next
) {
695 print_word(n
->string
);
702 outflags
&= ~MMAN_spc
;
705 if (n
->next
->next
== NULL
)
710 print_word("utilities exit\\~0");
712 print_word("utility exits\\~0");
714 print_word("on success, and\\~>0 if an error occurs.");
727 post_percent(DECL_ARGS
)
730 if (pre_em
== manacts
[n
->tok
].pre
)
734 if (n
->prev
&& n
->prev
->tok
== n
->tok
&&
735 n
->next
->tok
== n
->tok
)
747 if (n
->parent
&& MDOC_Rs
== n
->parent
->tok
&&
748 n
->parent
->norm
->Rs
.quote_T
) {
751 outflags
&= ~MMAN_spc
;
761 if (n
->parent
&& MDOC_Rs
== n
->parent
->tok
&&
762 n
->parent
->norm
->Rs
.quote_T
) {
763 outflags
&= ~MMAN_spc
;
768 post_percent(meta
, n
);
772 * Print before a section header.
778 if (MDOC_HEAD
== n
->type
) {
780 print_block(manacts
[n
->tok
].prefix
, 0);
783 outflags
&= ~MMAN_spc
;
789 * Print subsequent a section header.
795 if (MDOC_HEAD
!= n
->type
)
797 outflags
&= ~MMAN_spc
;
801 if (MDOC_Sh
== n
->tok
&& SEC_AUTHORS
== n
->sec
)
802 outflags
&= ~(MMAN_An_split
| MMAN_An_nosplit
);
805 /* See mdoc_term.c, synopsis_pre() for comments. */
807 pre_syn(const struct mdoc_node
*n
)
810 if (NULL
== n
->prev
|| ! (MDOC_SYNPRETTY
& n
->flags
))
813 if (n
->prev
->tok
== n
->tok
&&
821 switch (n
->prev
->tok
) {
834 if (MDOC_Fn
!= n
->tok
&& MDOC_Fo
!= n
->tok
) {
849 switch (n
->norm
->An
.auth
) {
851 outflags
&= ~MMAN_An_nosplit
;
852 outflags
|= MMAN_An_split
;
855 outflags
&= ~MMAN_An_split
;
856 outflags
|= MMAN_An_nosplit
;
859 if (MMAN_An_split
& outflags
)
861 else if (SEC_AUTHORS
== n
->sec
&&
862 ! (MMAN_An_nosplit
& outflags
))
863 outflags
|= MMAN_An_split
;
872 outflags
&= ~MMAN_spc
;
874 outflags
&= ~MMAN_spc
;
882 print_word(n
->nchild
== 1 &&
883 n
->child
->tok
== MDOC_Mt
? "<" : "\\(la");
884 outflags
&= ~MMAN_spc
;
892 outflags
&= ~(MMAN_spc
| MMAN_nl
);
893 print_word(n
->nchild
== 1 &&
894 n
->child
->tok
== MDOC_Mt
? ">" : "\\(ra");
901 outflags
&= ~(MMAN_PP
| MMAN_sp
| MMAN_br
);
903 if (DISP_unfilled
== n
->norm
->Bd
.type
||
904 DISP_literal
== n
->norm
->Bd
.type
)
905 print_line(".nf", 0);
906 if (0 == n
->norm
->Bd
.comp
&& NULL
!= n
->parent
->prev
)
908 print_offs(n
->norm
->Bd
.offs
, 1);
916 /* Close out this display. */
917 print_line(".RE", MMAN_nl
);
918 if (DISP_unfilled
== n
->norm
->Bd
.type
||
919 DISP_literal
== n
->norm
->Bd
.type
)
920 print_line(".fi", MMAN_nl
);
922 /* Maybe we are inside an enclosing list? */
923 if (NULL
!= n
->parent
->next
)
939 switch (n
->norm
->Bf
.font
) {
957 if (MDOC_BODY
== n
->type
)
980 if (MDOC_BODY
== n
->type
)
981 outflags
&= ~MMAN_Bk
;
990 * print_offs() will increase the -offset to account for
991 * a possible enclosing .It, but any enclosed .It blocks
992 * just nest and do not add up their indentation.
994 if (n
->norm
->Bl
.offs
) {
995 print_offs(n
->norm
->Bl
.offs
, 0);
996 Bl_stack
[Bl_stack_len
++] = 0;
999 switch (n
->norm
->Bl
.type
) {
1001 n
->norm
->Bl
.count
= 0;
1010 print_line(".TS", MMAN_nl
);
1011 for (icol
= 0; icol
< n
->norm
->Bl
.ncols
; icol
++)
1015 outflags
|= MMAN_nl
;
1023 switch (n
->norm
->Bl
.type
) {
1026 print_line(".TE", 0);
1029 n
->norm
->Bl
.count
= 0;
1035 if (n
->norm
->Bl
.offs
) {
1036 print_line(".RE", MMAN_nl
);
1037 assert(Bl_stack_len
);
1039 assert(0 == Bl_stack
[Bl_stack_len
]);
1041 outflags
|= MMAN_PP
| MMAN_nl
;
1042 outflags
&= ~(MMAN_sp
| MMAN_br
);
1045 /* Maybe we are inside an enclosing list? */
1046 if (NULL
!= n
->parent
->next
)
1055 outflags
|= MMAN_br
;
1065 print_word(n
->string
);
1066 outflags
&= ~MMAN_spc
;
1072 outflags
&= ~MMAN_spc
;
1074 outflags
&= ~MMAN_spc
;
1075 print_word(n
->string
);
1083 print_offs("6n", 0);
1091 print_line(".RE", MMAN_nl
);
1093 /* Maybe we are inside an enclosing list? */
1094 if (NULL
!= n
->parent
->next
)
1110 if (NULL
== n
->norm
->Es
||
1111 NULL
== n
->norm
->Es
->child
)
1114 print_word(n
->norm
->Es
->child
->string
);
1115 outflags
&= ~MMAN_spc
;
1123 if (NULL
== n
->norm
->Es
||
1124 NULL
== n
->norm
->Es
->child
||
1125 NULL
== n
->norm
->Es
->child
->next
)
1128 outflags
&= ~MMAN_spc
;
1129 print_word(n
->norm
->Es
->child
->next
->string
);
1137 if (n
->end
== ENDBODY_NOT
&&
1138 n
->parent
->head
->child
== NULL
&&
1140 n
->child
->end
!= ENDBODY_NOT
)
1142 else if (n
->end
!= ENDBODY_NOT
? n
->child
!= NULL
:
1143 n
->parent
->head
->child
!= NULL
&&
1144 (n
->parent
->body
->child
!= NULL
||
1145 n
->parent
->tail
->child
!= NULL
))
1146 outflags
&= ~(MMAN_spc
| MMAN_nl
);
1155 if (n
->end
!= ENDBODY_NOT
) {
1156 outflags
|= MMAN_spc
;
1160 body
= n
->child
!= NULL
|| n
->parent
->head
->child
!= NULL
;
1161 tail
= n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
;
1164 outflags
&= ~MMAN_spc
;
1165 else if ( ! (body
|| tail
))
1168 outflags
|= MMAN_spc
;
1176 am_Fa
= MDOC_Fa
== n
->tok
;
1183 if (am_Fa
|| MDOC_SYNPRETTY
& n
->flags
)
1184 outflags
|= MMAN_nbrword
;
1185 print_node(meta
, n
);
1187 if (NULL
!= (n
= n
->next
))
1197 if (NULL
!= n
->next
&& MDOC_Fa
== n
->next
->tok
)
1215 outflags
|= MMAN_br
;
1225 outflags
&= ~MMAN_spc
;
1234 if ( ! (n
->nchild
||
1236 n
->next
->type
== MDOC_TEXT
||
1237 n
->next
->flags
& MDOC_LINE
))
1238 outflags
&= ~MMAN_spc
;
1251 if (MDOC_SYNPRETTY
& n
->flags
)
1252 print_block(".HP 4n", MMAN_nl
);
1255 print_node(meta
, n
);
1257 outflags
&= ~MMAN_spc
;
1259 outflags
&= ~MMAN_spc
;
1272 if (MDOC_SYNPRETTY
& n
->flags
) {
1274 outflags
|= MMAN_PP
;
1287 if (n
->child
== NULL
)
1289 if (MDOC_SYNPRETTY
& n
->flags
)
1290 print_block(".HP 4n", MMAN_nl
);
1294 outflags
&= ~(MMAN_spc
| MMAN_nl
);
1296 outflags
&= ~MMAN_spc
;
1310 if (n
->child
!= NULL
)
1334 if (MDOC_SYNPRETTY
& n
->flags
) {
1337 print_word("#include <");
1338 outflags
&= ~MMAN_spc
;
1341 outflags
&= ~MMAN_spc
;
1351 if (MDOC_SYNPRETTY
& n
->flags
) {
1352 outflags
&= ~MMAN_spc
;
1355 outflags
|= MMAN_br
;
1358 outflags
&= ~MMAN_spc
;
1366 const struct mdoc_node
*bln
;
1370 outflags
|= MMAN_PP
| MMAN_nl
;
1371 bln
= n
->parent
->parent
;
1372 if (0 == bln
->norm
->Bl
.comp
||
1373 (NULL
== n
->parent
->prev
&&
1374 NULL
== bln
->parent
->prev
))
1375 outflags
|= MMAN_sp
;
1376 outflags
&= ~MMAN_br
;
1377 switch (bln
->norm
->Bl
.type
) {
1385 if (bln
->norm
->Bl
.type
== LIST_diag
)
1386 print_line(".B \"", 0);
1388 print_line(".R \"", 0);
1389 outflags
&= ~MMAN_spc
;
1396 print_width(&bln
->norm
->Bl
, NULL
);
1398 outflags
|= MMAN_nl
;
1400 if (LIST_bullet
== bln
->norm
->Bl
.type
)
1401 print_word("\\(bu");
1405 outflags
|= MMAN_nl
;
1408 print_width(&bln
->norm
->Bl
, NULL
);
1410 outflags
|= MMAN_nl
;
1411 print_count(&bln
->norm
->Bl
.count
);
1412 outflags
|= MMAN_nl
;
1415 print_width(&bln
->norm
->Bl
, n
->child
);
1417 outflags
|= MMAN_nl
;
1420 print_width(&bln
->norm
->Bl
, n
->child
);
1422 outflags
&= ~MMAN_spc
;
1434 * This function is called after closing out an indented block.
1435 * If we are inside an enclosing list, restore its indentation.
1442 /* Nothing to do outside a list. */
1443 if (0 == Bl_stack_len
|| 0 == Bl_stack
[Bl_stack_len
- 1])
1446 /* The indentation has already been set up. */
1447 if (Bl_stack_post
[Bl_stack_len
- 1])
1450 /* Restore the indentation of the enclosing list. */
1451 print_line(".RS", MMAN_Bk_susp
);
1452 (void)snprintf(buf
, sizeof(buf
), "%dn",
1453 Bl_stack
[Bl_stack_len
- 1]);
1456 /* Remeber to close out this .RS block later. */
1457 Bl_stack_post
[Bl_stack_len
- 1] = 1;
1463 const struct mdoc_node
*bln
;
1465 bln
= n
->parent
->parent
;
1469 switch (bln
->norm
->Bl
.type
) {
1471 outflags
&= ~MMAN_spc
;
1475 outflags
|= MMAN_br
;
1482 switch (bln
->norm
->Bl
.type
) {
1494 assert(Bl_stack_len
);
1495 Bl_stack
[--Bl_stack_len
] = 0;
1498 * Our indentation had to be restored
1499 * after a child display or child list.
1500 * Close out that indentation block now.
1502 if (Bl_stack_post
[Bl_stack_len
]) {
1503 print_line(".RE", MMAN_nl
);
1504 Bl_stack_post
[Bl_stack_len
] = 0;
1508 if (NULL
!= n
->next
) {
1510 outflags
&= ~MMAN_spc
;
1526 if (SEC_LIBRARY
== n
->sec
)
1527 outflags
|= MMAN_br
;
1533 const struct mdoc_node
*link
, *descr
;
1535 if (NULL
== (link
= n
->child
))
1538 if (NULL
!= (descr
= link
->next
)) {
1540 while (NULL
!= descr
) {
1541 print_word(descr
->string
);
1542 descr
= descr
->next
;
1549 print_word(link
->string
);
1558 print_line(".ll", 0);
1575 if (MDOC_BLOCK
== n
->type
) {
1576 outflags
|= MMAN_Bk
;
1579 if (MDOC_ELEM
!= n
->type
&& MDOC_HEAD
!= n
->type
)
1581 name
= n
->child
? n
->child
->string
: meta
->name
;
1584 if (MDOC_HEAD
== n
->type
) {
1585 if (NULL
== n
->parent
->prev
)
1586 outflags
|= MMAN_sp
;
1587 print_block(".HP", 0);
1588 printf(" %zun", strlen(name
) + 1);
1589 outflags
|= MMAN_nl
;
1592 if (NULL
== n
->child
)
1593 print_word(meta
->name
);
1603 outflags
&= ~MMAN_Bk
;
1608 if (n
->child
!= NULL
|| meta
->name
!= NULL
)
1620 outflags
|= MMAN_spc_force
;
1628 outflags
&= ~MMAN_spc
;
1636 if ( ! (n
->next
== NULL
|| n
->next
->flags
& MDOC_LINE
))
1637 outflags
&= ~MMAN_spc
;
1644 if (MDOC_It
!= n
->parent
->tok
)
1645 outflags
|= MMAN_PP
;
1646 outflags
|= MMAN_sp
| MMAN_nl
;
1647 outflags
&= ~MMAN_br
;
1655 if (SEC_SEE_ALSO
== n
->sec
) {
1656 outflags
|= MMAN_PP
| MMAN_sp
| MMAN_nl
;
1657 outflags
&= ~MMAN_br
;
1667 outflags
|= MMAN_br
| MMAN_nl
;
1673 for (n
= n
->child
; n
; n
= n
->next
) {
1675 print_word(n
->string
);
1678 outflags
&= ~MMAN_spc
;
1681 if (n
->next
== NULL
)
1685 outflags
&= ~MMAN_spc
;
1688 if (n
->next
->next
== NULL
)
1693 print_word("functions return");
1695 print_word("function returns");
1697 print_word("the value\\~0 if successful;");
1699 print_word("Upon successful completion, "
1700 "the value\\~0 is returned;");
1702 print_word("otherwise the value\\~\\-1 is returned"
1703 " and the global variable");
1706 print_word("errno");
1709 print_word("is set to indicate the error.");
1710 outflags
|= MMAN_nl
;
1725 if (NULL
== n
->child
)
1726 outflags
^= MMAN_Sm
;
1727 else if (0 == strcmp("on", n
->child
->string
))
1728 outflags
|= MMAN_Sm
;
1730 outflags
&= ~MMAN_Sm
;
1732 if (MMAN_Sm
& outflags
)
1733 outflags
|= MMAN_spc
;
1742 if (MMAN_PP
& outflags
) {
1743 outflags
&= ~MMAN_PP
;
1744 print_line(".PP", 0);
1746 print_line(".sp", 0);
1754 outflags
|= MMAN_nl
;
1769 if (MDOC_SYNPRETTY
& n
->flags
) {
1788 if (MDOC_SYNPRETTY
& n
->flags
&& MDOC_BODY
!= n
->type
)
1800 print_node(meta
, n
);
1804 outflags
&= ~MMAN_spc
;
1806 print_node(meta
, n
);
1815 print_word(manacts
[n
->tok
].prefix
);
1816 if (NULL
== n
->child
)
1818 outflags
&= ~MMAN_spc
;
1820 outflags
&= ~MMAN_spc
;