1 /* $Id: mdoc_man.c,v 1.129 2018/12/03 21:00:10 schwarze Exp $ */
3 * Copyright (c) 2011-2018 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"
34 #define DECL_ARGS const struct roff_meta *meta, struct roff_node *n
36 typedef int (*int_fp
)(DECL_ARGS
);
37 typedef void (*void_fp
)(DECL_ARGS
);
40 int_fp cond
; /* DON'T run actions */
41 int_fp pre
; /* pre-node action */
42 void_fp post
; /* post-node action */
43 const char *prefix
; /* pre-node string constant */
44 const char *suffix
; /* post-node string constant */
47 static int cond_body(DECL_ARGS
);
48 static int cond_head(DECL_ARGS
);
49 static void font_push(char);
50 static void font_pop(void);
51 static int man_strlen(const char *);
52 static void mid_it(void);
53 static void post__t(DECL_ARGS
);
54 static void post_aq(DECL_ARGS
);
55 static void post_bd(DECL_ARGS
);
56 static void post_bf(DECL_ARGS
);
57 static void post_bk(DECL_ARGS
);
58 static void post_bl(DECL_ARGS
);
59 static void post_dl(DECL_ARGS
);
60 static void post_en(DECL_ARGS
);
61 static void post_enc(DECL_ARGS
);
62 static void post_eo(DECL_ARGS
);
63 static void post_fa(DECL_ARGS
);
64 static void post_fd(DECL_ARGS
);
65 static void post_fl(DECL_ARGS
);
66 static void post_fn(DECL_ARGS
);
67 static void post_fo(DECL_ARGS
);
68 static void post_font(DECL_ARGS
);
69 static void post_in(DECL_ARGS
);
70 static void post_it(DECL_ARGS
);
71 static void post_lb(DECL_ARGS
);
72 static void post_nm(DECL_ARGS
);
73 static void post_percent(DECL_ARGS
);
74 static void post_pf(DECL_ARGS
);
75 static void post_sect(DECL_ARGS
);
76 static void post_vt(DECL_ARGS
);
77 static int pre__t(DECL_ARGS
);
78 static int pre_abort(DECL_ARGS
);
79 static int pre_an(DECL_ARGS
);
80 static int pre_ap(DECL_ARGS
);
81 static int pre_aq(DECL_ARGS
);
82 static int pre_bd(DECL_ARGS
);
83 static int pre_bf(DECL_ARGS
);
84 static int pre_bk(DECL_ARGS
);
85 static int pre_bl(DECL_ARGS
);
86 static void pre_br(DECL_ARGS
);
87 static int pre_dl(DECL_ARGS
);
88 static int pre_en(DECL_ARGS
);
89 static int pre_enc(DECL_ARGS
);
90 static int pre_em(DECL_ARGS
);
91 static int pre_skip(DECL_ARGS
);
92 static int pre_eo(DECL_ARGS
);
93 static int pre_ex(DECL_ARGS
);
94 static int pre_fa(DECL_ARGS
);
95 static int pre_fd(DECL_ARGS
);
96 static int pre_fl(DECL_ARGS
);
97 static int pre_fn(DECL_ARGS
);
98 static int pre_fo(DECL_ARGS
);
99 static void pre_ft(DECL_ARGS
);
100 static int pre_Ft(DECL_ARGS
);
101 static int pre_in(DECL_ARGS
);
102 static int pre_it(DECL_ARGS
);
103 static int pre_lk(DECL_ARGS
);
104 static int pre_li(DECL_ARGS
);
105 static int pre_nm(DECL_ARGS
);
106 static int pre_no(DECL_ARGS
);
107 static int pre_ns(DECL_ARGS
);
108 static void pre_onearg(DECL_ARGS
);
109 static int pre_pp(DECL_ARGS
);
110 static int pre_rs(DECL_ARGS
);
111 static int pre_sm(DECL_ARGS
);
112 static void pre_sp(DECL_ARGS
);
113 static int pre_sect(DECL_ARGS
);
114 static int pre_sy(DECL_ARGS
);
115 static void pre_syn(const struct roff_node
*);
116 static void pre_ta(DECL_ARGS
);
117 static int pre_vt(DECL_ARGS
);
118 static int pre_xr(DECL_ARGS
);
119 static void print_word(const char *);
120 static void print_line(const char *, int);
121 static void print_block(const char *, int);
122 static void print_offs(const char *, int);
123 static void print_width(const struct mdoc_bl
*,
124 const struct roff_node
*);
125 static void print_count(int *);
126 static void print_node(DECL_ARGS
);
128 static const void_fp roff_man_acts
[ROFF_MAX
] = {
141 static const struct mdoc_man_act mdoc_man_acts
[MDOC_MAX
- MDOC_Dd
] = {
142 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dd */
143 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dt */
144 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Os */
145 { NULL
, pre_sect
, post_sect
, ".SH", NULL
}, /* Sh */
146 { NULL
, pre_sect
, post_sect
, ".SS", NULL
}, /* Ss */
147 { NULL
, pre_pp
, NULL
, NULL
, NULL
}, /* Pp */
148 { cond_body
, pre_dl
, post_dl
, NULL
, NULL
}, /* D1 */
149 { cond_body
, pre_dl
, post_dl
, NULL
, NULL
}, /* Dl */
150 { cond_body
, pre_bd
, post_bd
, NULL
, NULL
}, /* Bd */
151 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ed */
152 { cond_body
, pre_bl
, post_bl
, NULL
, NULL
}, /* Bl */
153 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* El */
154 { NULL
, pre_it
, post_it
, NULL
, NULL
}, /* It */
155 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Ad */
156 { NULL
, pre_an
, NULL
, NULL
, NULL
}, /* An */
157 { NULL
, pre_ap
, NULL
, NULL
, NULL
}, /* Ap */
158 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Ar */
159 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Cd */
160 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Cm */
161 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Dv */
162 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Er */
163 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Ev */
164 { NULL
, pre_ex
, NULL
, NULL
, NULL
}, /* Ex */
165 { NULL
, pre_fa
, post_fa
, NULL
, NULL
}, /* Fa */
166 { NULL
, pre_fd
, post_fd
, NULL
, NULL
}, /* Fd */
167 { NULL
, pre_fl
, post_fl
, NULL
, NULL
}, /* Fl */
168 { NULL
, pre_fn
, post_fn
, NULL
, NULL
}, /* Fn */
169 { NULL
, pre_Ft
, post_font
, NULL
, NULL
}, /* Ft */
170 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Ic */
171 { NULL
, pre_in
, post_in
, NULL
, NULL
}, /* In */
172 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Li */
173 { cond_head
, pre_enc
, NULL
, "\\- ", NULL
}, /* Nd */
174 { NULL
, pre_nm
, post_nm
, NULL
, NULL
}, /* Nm */
175 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Op */
176 { NULL
, pre_abort
, NULL
, NULL
, NULL
}, /* Ot */
177 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Pa */
178 { NULL
, pre_ex
, NULL
, NULL
, NULL
}, /* Rv */
179 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* St */
180 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Va */
181 { NULL
, pre_vt
, post_vt
, NULL
, NULL
}, /* Vt */
182 { NULL
, pre_xr
, NULL
, NULL
, NULL
}, /* Xr */
183 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %A */
184 { NULL
, pre_em
, post_percent
, NULL
, NULL
}, /* %B */
185 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %D */
186 { NULL
, pre_em
, post_percent
, NULL
, NULL
}, /* %I */
187 { NULL
, pre_em
, post_percent
, NULL
, NULL
}, /* %J */
188 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %N */
189 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %O */
190 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %P */
191 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %R */
192 { NULL
, pre__t
, post__t
, NULL
, NULL
}, /* %T */
193 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %V */
194 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ac */
195 { cond_body
, pre_aq
, post_aq
, NULL
, NULL
}, /* Ao */
196 { cond_body
, pre_aq
, post_aq
, NULL
, NULL
}, /* Aq */
197 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* At */
198 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bc */
199 { NULL
, pre_bf
, post_bf
, NULL
, NULL
}, /* Bf */
200 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Bo */
201 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Bq */
202 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Bsx */
203 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Bx */
204 { NULL
, pre_skip
, NULL
, NULL
, NULL
}, /* Db */
205 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dc */
206 { cond_body
, pre_enc
, post_enc
, "\\(lq", "\\(rq" }, /* Do */
207 { cond_body
, pre_enc
, post_enc
, "\\(lq", "\\(rq" }, /* Dq */
208 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ec */
209 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ef */
210 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Em */
211 { cond_body
, pre_eo
, post_eo
, NULL
, NULL
}, /* Eo */
212 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Fx */
213 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Ms */
214 { NULL
, pre_no
, NULL
, NULL
, NULL
}, /* No */
215 { NULL
, pre_ns
, NULL
, NULL
, NULL
}, /* Ns */
216 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Nx */
217 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Ox */
218 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Pc */
219 { NULL
, NULL
, post_pf
, NULL
, NULL
}, /* Pf */
220 { cond_body
, pre_enc
, post_enc
, "(", ")" }, /* Po */
221 { cond_body
, pre_enc
, post_enc
, "(", ")" }, /* Pq */
222 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Qc */
223 { cond_body
, pre_enc
, post_enc
, "\\(oq", "\\(cq" }, /* Ql */
224 { cond_body
, pre_enc
, post_enc
, "\"", "\"" }, /* Qo */
225 { cond_body
, pre_enc
, post_enc
, "\"", "\"" }, /* Qq */
226 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Re */
227 { cond_body
, pre_rs
, NULL
, NULL
, NULL
}, /* Rs */
228 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Sc */
229 { cond_body
, pre_enc
, post_enc
, "\\(oq", "\\(cq" }, /* So */
230 { cond_body
, pre_enc
, post_enc
, "\\(oq", "\\(cq" }, /* Sq */
231 { NULL
, pre_sm
, NULL
, NULL
, NULL
}, /* Sm */
232 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Sx */
233 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Sy */
234 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Tn */
235 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ux */
236 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xc */
237 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xo */
238 { NULL
, pre_fo
, post_fo
, NULL
, NULL
}, /* Fo */
239 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fc */
240 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Oo */
241 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Oc */
242 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Bk */
243 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ek */
244 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bt */
245 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Hf */
246 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Fr */
247 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ud */
248 { NULL
, NULL
, post_lb
, NULL
, NULL
}, /* Lb */
249 { NULL
, pre_abort
, NULL
, NULL
, NULL
}, /* Lp */
250 { NULL
, pre_lk
, NULL
, NULL
, NULL
}, /* Lk */
251 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Mt */
252 { cond_body
, pre_enc
, post_enc
, "{", "}" }, /* Brq */
253 { cond_body
, pre_enc
, post_enc
, "{", "}" }, /* Bro */
254 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Brc */
255 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %C */
256 { NULL
, pre_skip
, NULL
, NULL
, NULL
}, /* Es */
257 { cond_body
, pre_en
, post_en
, NULL
, NULL
}, /* En */
258 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Dx */
259 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %Q */
260 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %U */
261 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ta */
263 static const struct mdoc_man_act
*mdoc_man_act(enum roff_tok
);
266 #define MMAN_spc (1 << 0) /* blank character before next word */
267 #define MMAN_spc_force (1 << 1) /* even before trailing punctuation */
268 #define MMAN_nl (1 << 2) /* break man(7) code line */
269 #define MMAN_br (1 << 3) /* break output line */
270 #define MMAN_sp (1 << 4) /* insert a blank output line */
271 #define MMAN_PP (1 << 5) /* reset indentation etc. */
272 #define MMAN_Sm (1 << 6) /* horizontal spacing mode */
273 #define MMAN_Bk (1 << 7) /* word keep mode */
274 #define MMAN_Bk_susp (1 << 8) /* suspend this (after a macro) */
275 #define MMAN_An_split (1 << 9) /* author mode is "split" */
276 #define MMAN_An_nosplit (1 << 10) /* author mode is "nosplit" */
277 #define MMAN_PD (1 << 11) /* inter-paragraph spacing disabled */
278 #define MMAN_nbrword (1 << 12) /* do not break the next word */
280 #define BL_STACK_MAX 32
282 static int Bl_stack
[BL_STACK_MAX
]; /* offsets [chars] */
283 static int Bl_stack_post
[BL_STACK_MAX
]; /* add final .RE */
284 static int Bl_stack_len
; /* number of nested Bl blocks */
285 static int TPremain
; /* characters before tag is full */
294 static const struct mdoc_man_act
*
295 mdoc_man_act(enum roff_tok tok
)
297 assert(tok
>= MDOC_Dd
&& tok
<= MDOC_MAX
);
298 return mdoc_man_acts
+ (tok
- MDOC_Dd
);
302 man_strlen(const char *cp
)
310 rsz
= strcspn(cp
, "\\");
322 switch (mandoc_escape(&cp
, NULL
, NULL
)) {
326 case ESCAPE_NUMBERED
:
328 case ESCAPE_OVERSTRIKE
:
334 case ESCAPE_SKIPCHAR
:
345 font_push(char newfont
)
348 if (fontqueue
.head
+ fontqueue
.size
<= ++fontqueue
.tail
) {
350 fontqueue
.head
= mandoc_realloc(fontqueue
.head
,
353 *fontqueue
.tail
= newfont
;
357 outflags
&= ~MMAN_spc
;
364 if (fontqueue
.tail
> fontqueue
.head
)
366 outflags
&= ~MMAN_spc
;
369 putchar(*fontqueue
.tail
);
373 print_word(const char *s
)
376 if ((MMAN_PP
| MMAN_sp
| MMAN_br
| MMAN_nl
) & outflags
) {
378 * If we need a newline, print it now and start afresh.
380 if (MMAN_PP
& outflags
) {
381 if (MMAN_sp
& outflags
) {
382 if (MMAN_PD
& outflags
) {
384 outflags
&= ~MMAN_PD
;
386 } else if ( ! (MMAN_PD
& outflags
)) {
391 } else if (MMAN_sp
& outflags
)
393 else if (MMAN_br
& outflags
)
395 else if (MMAN_nl
& outflags
)
397 outflags
&= ~(MMAN_PP
|MMAN_sp
|MMAN_br
|MMAN_nl
|MMAN_spc
);
401 } else if (MMAN_spc
& outflags
) {
403 * If we need a space, only print it if
404 * (1) it is forced by `No' or
405 * (2) what follows is not terminating punctuation or
406 * (3) what follows is longer than one character.
408 if (MMAN_spc_force
& outflags
|| '\0' == s
[0] ||
409 NULL
== strchr(".,:;)]?!", s
[0]) || '\0' != s
[1]) {
410 if (MMAN_Bk
& outflags
&&
411 ! (MMAN_Bk_susp
& outflags
))
420 * Reassign needing space if we're not following opening
423 if (MMAN_Sm
& outflags
&& ('\0' == s
[0] ||
424 (('(' != s
[0] && '[' != s
[0]) || '\0' != s
[1])))
425 outflags
|= MMAN_spc
;
427 outflags
&= ~MMAN_spc
;
428 outflags
&= ~(MMAN_spc_force
| MMAN_Bk_susp
);
442 if (MMAN_nbrword
& outflags
) {
448 putchar((unsigned char)*s
);
454 outflags
&= ~MMAN_nbrword
;
458 print_line(const char *s
, int newflags
)
463 outflags
|= newflags
;
467 print_block(const char *s
, int newflags
)
470 outflags
&= ~MMAN_PP
;
471 if (MMAN_sp
& outflags
) {
472 outflags
&= ~(MMAN_sp
| MMAN_br
);
473 if (MMAN_PD
& outflags
) {
474 print_line(".PD", 0);
475 outflags
&= ~MMAN_PD
;
477 } else if (! (MMAN_PD
& outflags
))
478 print_line(".PD 0", MMAN_PD
);
481 outflags
|= MMAN_Bk_susp
| newflags
;
485 print_offs(const char *v
, int keywords
)
492 print_line(".RS", MMAN_Bk_susp
);
494 /* Convert v into a number (of characters). */
495 if (NULL
== v
|| '\0' == *v
|| (keywords
&& !strcmp(v
, "left")))
497 else if (keywords
&& !strcmp(v
, "indent"))
499 else if (keywords
&& !strcmp(v
, "indent-two"))
502 end
= a2roffsu(v
, &su
, SCALE_EN
);
503 if (end
== NULL
|| *end
!= '\0')
505 else if (SCALE_EN
== su
.unit
)
510 * If we are inside an enclosing list,
511 * there is no easy way to add the two
512 * indentations because they are provided
513 * in terms of different units.
522 * We are inside an enclosing list.
523 * Add the two indentations.
526 sz
+= Bl_stack
[Bl_stack_len
- 1];
528 (void)snprintf(buf
, sizeof(buf
), "%dn", sz
);
534 * Set up the indentation for a list item; used from pre_it().
537 print_width(const struct mdoc_bl
*bl
, const struct roff_node
*child
)
542 int numeric
, remain
, sz
, chsz
;
547 /* Convert the width into a number (of characters). */
548 if (bl
->width
== NULL
)
549 sz
= (bl
->type
== LIST_hang
) ? 6 : 0;
551 end
= a2roffsu(bl
->width
, &su
, SCALE_MAX
);
552 if (end
== NULL
|| *end
!= '\0')
553 sz
= man_strlen(bl
->width
);
554 else if (SCALE_EN
== su
.unit
)
562 /* XXX Rough estimation, might have multiple parts. */
563 if (bl
->type
== LIST_enum
)
564 chsz
= (bl
->count
> 8) + 1;
565 else if (child
!= NULL
&& child
->type
== ROFFT_TEXT
)
566 chsz
= man_strlen(child
->string
);
570 /* Maybe we are inside an enclosing list? */
574 * Save our own indentation,
575 * such that child lists can use it.
577 Bl_stack
[Bl_stack_len
++] = sz
+ 2;
579 /* Set up the current list. */
580 if (chsz
> sz
&& bl
->type
!= LIST_tag
)
581 print_block(".HP", 0);
583 print_block(".TP", 0);
587 (void)snprintf(buf
, sizeof(buf
), "%dn", sz
+ 2);
590 print_word(bl
->width
);
595 print_count(int *count
)
599 (void)snprintf(buf
, sizeof(buf
), "%d.\\&", ++*count
);
604 man_mdoc(void *arg
, const struct roff_man
*mdoc
)
608 printf(".\\\" Automatically generated from an mdoc input file."
610 for (n
= mdoc
->first
->child
; n
!= NULL
; n
= n
->next
) {
611 if (n
->type
!= ROFFT_COMMENT
)
613 printf(".\\\"%s\n", n
->string
);
616 printf(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"\n",
618 (mdoc
->meta
.msec
== NULL
? "" : mdoc
->meta
.msec
),
619 mdoc
->meta
.date
, mdoc
->meta
.os
, mdoc
->meta
.vol
);
621 /* Disable hyphenation and if nroff, disable justification. */
622 printf(".nh\n.if n .ad l");
624 outflags
= MMAN_nl
| MMAN_Sm
;
625 if (0 == fontqueue
.size
) {
627 fontqueue
.head
= fontqueue
.tail
= mandoc_malloc(8);
628 *fontqueue
.tail
= 'R';
630 for (; n
!= NULL
; n
= n
->next
)
631 print_node(&mdoc
->meta
, n
);
636 print_node(DECL_ARGS
)
638 const struct mdoc_man_act
*act
;
639 struct roff_node
*sub
;
642 if (n
->flags
& NODE_NOPRT
)
646 * Break the line if we were parsed subsequent the current node.
647 * This makes the page structure be more consistent.
649 if (MMAN_spc
& outflags
&& NODE_LINE
& n
->flags
)
655 n
->flags
&= ~NODE_ENDED
;
657 if (n
->type
== ROFFT_TEXT
) {
659 * Make sure that we don't happen to start with a
660 * control character at the start of a line.
662 if (MMAN_nl
& outflags
&&
663 ('.' == *n
->string
|| '\'' == *n
->string
)) {
666 outflags
&= ~MMAN_spc
;
668 if (n
->flags
& NODE_DELIMC
)
669 outflags
&= ~(MMAN_spc
| MMAN_spc_force
);
670 else if (outflags
& MMAN_Sm
)
671 outflags
|= MMAN_spc_force
;
672 print_word(n
->string
);
673 if (n
->flags
& NODE_DELIMO
)
674 outflags
&= ~(MMAN_spc
| MMAN_spc_force
);
675 else if (outflags
& MMAN_Sm
)
676 outflags
|= MMAN_spc
;
677 } else if (n
->tok
< ROFF_MAX
) {
678 (*roff_man_acts
[n
->tok
])(meta
, n
);
682 * Conditionally run the pre-node action handler for a
685 act
= mdoc_man_act(n
->tok
);
686 cond
= act
->cond
== NULL
|| (*act
->cond
)(meta
, n
);
687 if (cond
&& act
->pre
!= NULL
&&
688 (n
->end
== ENDBODY_NOT
|| n
->child
!= NULL
))
689 do_sub
= (*act
->pre
)(meta
, n
);
693 * Conditionally run all child nodes.
694 * Note that this iterates over children instead of using
695 * recursion. This prevents unnecessary depth in the stack.
698 for (sub
= n
->child
; sub
; sub
= sub
->next
)
699 print_node(meta
, sub
);
702 * Lastly, conditionally run the post-node handler.
704 if (NODE_ENDED
& n
->flags
)
707 if (cond
&& act
->post
)
708 (*act
->post
)(meta
, n
);
710 if (ENDBODY_NOT
!= n
->end
)
711 n
->body
->flags
|= NODE_ENDED
;
718 return n
->type
== ROFFT_HEAD
;
725 return n
->type
== ROFFT_BODY
;
739 prefix
= mdoc_man_act(n
->tok
)->prefix
;
743 outflags
&= ~MMAN_spc
;
752 suffix
= mdoc_man_act(n
->tok
)->suffix
;
755 outflags
&= ~(MMAN_spc
| MMAN_nl
);
762 outflags
|= MMAN_br
| MMAN_nl
;
774 post_percent(DECL_ARGS
)
777 if (mdoc_man_act(n
->tok
)->pre
== pre_em
)
781 if (n
->prev
&& n
->prev
->tok
== n
->tok
&&
782 n
->next
->tok
== n
->tok
)
794 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
) {
796 outflags
&= ~MMAN_spc
;
806 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
) {
807 outflags
&= ~MMAN_spc
;
811 post_percent(meta
, n
);
815 * Print before a section header.
821 if (n
->type
== ROFFT_HEAD
) {
823 print_block(mdoc_man_act(n
->tok
)->prefix
, 0);
826 outflags
&= ~MMAN_spc
;
832 * Print subsequent a section header.
838 if (n
->type
!= ROFFT_HEAD
)
840 outflags
&= ~MMAN_spc
;
844 if (MDOC_Sh
== n
->tok
&& SEC_AUTHORS
== n
->sec
)
845 outflags
&= ~(MMAN_An_split
| MMAN_An_nosplit
);
848 /* See mdoc_term.c, synopsis_pre() for comments. */
850 pre_syn(const struct roff_node
*n
)
853 if (NULL
== n
->prev
|| ! (NODE_SYNPRETTY
& n
->flags
))
856 if (n
->prev
->tok
== n
->tok
&&
864 switch (n
->prev
->tok
) {
873 if (MDOC_Fn
!= n
->tok
&& MDOC_Fo
!= n
->tok
) {
888 switch (n
->norm
->An
.auth
) {
890 outflags
&= ~MMAN_An_nosplit
;
891 outflags
|= MMAN_An_split
;
894 outflags
&= ~MMAN_An_split
;
895 outflags
|= MMAN_An_nosplit
;
898 if (MMAN_An_split
& outflags
)
900 else if (SEC_AUTHORS
== n
->sec
&&
901 ! (MMAN_An_nosplit
& outflags
))
902 outflags
|= MMAN_An_split
;
911 outflags
&= ~MMAN_spc
;
913 outflags
&= ~MMAN_spc
;
921 print_word(n
->child
!= NULL
&& n
->child
->next
== NULL
&&
922 n
->child
->tok
== MDOC_Mt
? "<" : "\\(la");
923 outflags
&= ~MMAN_spc
;
931 outflags
&= ~(MMAN_spc
| MMAN_nl
);
932 print_word(n
->child
!= NULL
&& n
->child
->next
== NULL
&&
933 n
->child
->tok
== MDOC_Mt
? ">" : "\\(ra");
940 outflags
&= ~(MMAN_PP
| MMAN_sp
| MMAN_br
);
942 if (DISP_unfilled
== n
->norm
->Bd
.type
||
943 DISP_literal
== n
->norm
->Bd
.type
)
944 print_line(".nf", 0);
945 if (0 == n
->norm
->Bd
.comp
&& NULL
!= n
->parent
->prev
)
947 print_offs(n
->norm
->Bd
.offs
, 1);
955 /* Close out this display. */
956 print_line(".RE", MMAN_nl
);
957 if (DISP_unfilled
== n
->norm
->Bd
.type
||
958 DISP_literal
== n
->norm
->Bd
.type
)
959 print_line(".fi", MMAN_nl
);
961 /* Maybe we are inside an enclosing list? */
962 if (NULL
!= n
->parent
->next
)
978 switch (n
->norm
->Bf
.font
) {
996 if (n
->type
== ROFFT_BODY
)
1008 outflags
|= MMAN_Bk
;
1020 while ((n
= n
->parent
) != NULL
)
1021 if (n
->tok
== MDOC_Bk
)
1025 outflags
&= ~MMAN_Bk
;
1038 * print_offs() will increase the -offset to account for
1039 * a possible enclosing .It, but any enclosed .It blocks
1040 * just nest and do not add up their indentation.
1042 if (n
->norm
->Bl
.offs
) {
1043 print_offs(n
->norm
->Bl
.offs
, 0);
1044 Bl_stack
[Bl_stack_len
++] = 0;
1047 switch (n
->norm
->Bl
.type
) {
1049 n
->norm
->Bl
.count
= 0;
1057 if (n
->child
!= NULL
) {
1058 print_line(".TS", MMAN_nl
);
1059 for (icol
= 0; icol
< n
->norm
->Bl
.ncols
; icol
++)
1063 outflags
|= MMAN_nl
;
1071 switch (n
->norm
->Bl
.type
) {
1073 if (n
->child
!= NULL
)
1074 print_line(".TE", 0);
1077 n
->norm
->Bl
.count
= 0;
1083 if (n
->norm
->Bl
.offs
) {
1084 print_line(".RE", MMAN_nl
);
1085 assert(Bl_stack_len
);
1087 assert(0 == Bl_stack
[Bl_stack_len
]);
1089 outflags
|= MMAN_PP
| MMAN_nl
;
1090 outflags
&= ~(MMAN_sp
| MMAN_br
);
1093 /* Maybe we are inside an enclosing list? */
1094 if (NULL
!= n
->parent
->next
)
1102 outflags
|= MMAN_br
;
1109 print_offs("6n", 0);
1117 print_line(".RE", MMAN_nl
);
1119 /* Maybe we are inside an enclosing list? */
1120 if (NULL
!= n
->parent
->next
)
1136 if (NULL
== n
->norm
->Es
||
1137 NULL
== n
->norm
->Es
->child
)
1140 print_word(n
->norm
->Es
->child
->string
);
1141 outflags
&= ~MMAN_spc
;
1149 if (NULL
== n
->norm
->Es
||
1150 NULL
== n
->norm
->Es
->child
||
1151 NULL
== n
->norm
->Es
->child
->next
)
1154 outflags
&= ~MMAN_spc
;
1155 print_word(n
->norm
->Es
->child
->next
->string
);
1163 if (n
->end
== ENDBODY_NOT
&&
1164 n
->parent
->head
->child
== NULL
&&
1166 n
->child
->end
!= ENDBODY_NOT
)
1168 else if (n
->end
!= ENDBODY_NOT
? n
->child
!= NULL
:
1169 n
->parent
->head
->child
!= NULL
&& (n
->child
!= NULL
||
1170 (n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
)))
1171 outflags
&= ~(MMAN_spc
| MMAN_nl
);
1180 if (n
->end
!= ENDBODY_NOT
) {
1181 outflags
|= MMAN_spc
;
1185 body
= n
->child
!= NULL
|| n
->parent
->head
->child
!= NULL
;
1186 tail
= n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
;
1189 outflags
&= ~MMAN_spc
;
1190 else if ( ! (body
|| tail
))
1193 outflags
|= MMAN_spc
;
1201 am_Fa
= MDOC_Fa
== n
->tok
;
1208 if (am_Fa
|| NODE_SYNPRETTY
& n
->flags
)
1209 outflags
|= MMAN_nbrword
;
1210 print_node(meta
, n
);
1212 if (NULL
!= (n
= n
->next
))
1222 if (NULL
!= n
->next
&& MDOC_Fa
== n
->next
->tok
)
1240 outflags
|= MMAN_br
;
1249 if (n
->child
!= NULL
)
1250 outflags
&= ~MMAN_spc
;
1259 if (!(n
->child
!= NULL
||
1261 n
->next
->type
== ROFFT_TEXT
||
1262 n
->next
->flags
& NODE_LINE
))
1263 outflags
&= ~MMAN_spc
;
1276 if (NODE_SYNPRETTY
& n
->flags
)
1277 print_block(".HP 4n", MMAN_nl
);
1280 print_node(meta
, n
);
1282 outflags
&= ~MMAN_spc
;
1284 outflags
&= ~MMAN_spc
;
1297 if (NODE_SYNPRETTY
& n
->flags
) {
1299 outflags
|= MMAN_PP
;
1312 if (n
->child
== NULL
)
1314 if (NODE_SYNPRETTY
& n
->flags
)
1315 print_block(".HP 4n", MMAN_nl
);
1319 outflags
&= ~(MMAN_spc
| MMAN_nl
);
1321 outflags
&= ~MMAN_spc
;
1335 if (n
->child
!= NULL
)
1358 print_line(".ft", 0);
1359 print_word(n
->child
->string
);
1360 outflags
|= MMAN_nl
;
1367 if (NODE_SYNPRETTY
& n
->flags
) {
1370 print_word("#include <");
1371 outflags
&= ~MMAN_spc
;
1374 outflags
&= ~MMAN_spc
;
1384 if (NODE_SYNPRETTY
& n
->flags
) {
1385 outflags
&= ~MMAN_spc
;
1388 outflags
|= MMAN_br
;
1391 outflags
&= ~MMAN_spc
;
1399 const struct roff_node
*bln
;
1403 outflags
|= MMAN_PP
| MMAN_nl
;
1404 bln
= n
->parent
->parent
;
1405 if (0 == bln
->norm
->Bl
.comp
||
1406 (NULL
== n
->parent
->prev
&&
1407 NULL
== bln
->parent
->prev
))
1408 outflags
|= MMAN_sp
;
1409 outflags
&= ~MMAN_br
;
1410 switch (bln
->norm
->Bl
.type
) {
1416 if (bln
->norm
->Bl
.type
== LIST_diag
)
1417 print_line(".B \"", 0);
1419 print_line(".BR \\& \"", 0);
1420 outflags
&= ~MMAN_spc
;
1425 print_width(&bln
->norm
->Bl
, NULL
);
1427 outflags
|= MMAN_nl
;
1429 if (LIST_bullet
== bln
->norm
->Bl
.type
)
1430 print_word("\\(bu");
1434 outflags
|= MMAN_nl
;
1437 print_width(&bln
->norm
->Bl
, NULL
);
1439 outflags
|= MMAN_nl
;
1440 print_count(&bln
->norm
->Bl
.count
);
1441 outflags
|= MMAN_nl
;
1444 print_width(&bln
->norm
->Bl
, n
->child
);
1446 outflags
|= MMAN_nl
;
1449 print_width(&bln
->norm
->Bl
, n
->child
);
1451 outflags
&= ~MMAN_spc
;
1463 * This function is called after closing out an indented block.
1464 * If we are inside an enclosing list, restore its indentation.
1471 /* Nothing to do outside a list. */
1472 if (0 == Bl_stack_len
|| 0 == Bl_stack
[Bl_stack_len
- 1])
1475 /* The indentation has already been set up. */
1476 if (Bl_stack_post
[Bl_stack_len
- 1])
1479 /* Restore the indentation of the enclosing list. */
1480 print_line(".RS", MMAN_Bk_susp
);
1481 (void)snprintf(buf
, sizeof(buf
), "%dn",
1482 Bl_stack
[Bl_stack_len
- 1]);
1485 /* Remeber to close out this .RS block later. */
1486 Bl_stack_post
[Bl_stack_len
- 1] = 1;
1492 const struct roff_node
*bln
;
1494 bln
= n
->parent
->parent
;
1498 switch (bln
->norm
->Bl
.type
) {
1500 outflags
&= ~MMAN_spc
;
1504 outflags
|= MMAN_br
;
1511 switch (bln
->norm
->Bl
.type
) {
1518 assert(Bl_stack_len
);
1519 Bl_stack
[--Bl_stack_len
] = 0;
1522 * Our indentation had to be restored
1523 * after a child display or child list.
1524 * Close out that indentation block now.
1526 if (Bl_stack_post
[Bl_stack_len
]) {
1527 print_line(".RE", MMAN_nl
);
1528 Bl_stack_post
[Bl_stack_len
] = 0;
1532 if (NULL
!= n
->next
) {
1534 outflags
&= ~MMAN_spc
;
1550 if (SEC_LIBRARY
== n
->sec
)
1551 outflags
|= MMAN_br
;
1557 const struct roff_node
*link
, *descr
, *punct
;
1559 if ((link
= n
->child
) == NULL
)
1562 /* Find beginning of trailing punctuation. */
1564 while (punct
!= link
&& punct
->flags
& NODE_DELIMC
)
1565 punct
= punct
->prev
;
1566 punct
= punct
->next
;
1569 if ((descr
= link
->next
) != NULL
&& descr
!= punct
) {
1571 while (descr
!= punct
) {
1572 print_word(descr
->string
);
1573 descr
= descr
->next
;
1581 print_word(link
->string
);
1584 /* Trailing punctuation. */
1585 while (punct
!= NULL
) {
1586 print_word(punct
->string
);
1587 punct
= punct
->next
;
1593 pre_onearg(DECL_ARGS
)
1595 outflags
|= MMAN_nl
;
1597 outflags
&= ~MMAN_spc
;
1598 print_word(roff_name
[n
->tok
]);
1599 if (n
->child
!= NULL
)
1600 print_word(n
->child
->string
);
1601 outflags
|= MMAN_nl
;
1602 if (n
->tok
== ROFF_ce
)
1603 for (n
= n
->child
->next
; n
!= NULL
; n
= n
->next
)
1604 print_node(meta
, n
);
1620 if (n
->type
== ROFFT_BLOCK
) {
1621 outflags
|= MMAN_Bk
;
1624 if (n
->type
!= ROFFT_ELEM
&& n
->type
!= ROFFT_HEAD
)
1626 name
= n
->child
== NULL
? NULL
: n
->child
->string
;
1629 if (n
->type
== ROFFT_HEAD
) {
1630 if (NULL
== n
->parent
->prev
)
1631 outflags
|= MMAN_sp
;
1632 print_block(".HP", 0);
1633 printf(" %dn", man_strlen(name
) + 1);
1634 outflags
|= MMAN_nl
;
1646 outflags
&= ~MMAN_Bk
;
1650 if (n
->child
!= NULL
&& n
->child
->string
!= NULL
)
1662 outflags
|= MMAN_spc_force
;
1670 outflags
&= ~MMAN_spc
;
1678 if ( ! (n
->next
== NULL
|| n
->next
->flags
& NODE_LINE
))
1679 outflags
&= ~MMAN_spc
;
1686 if (MDOC_It
!= n
->parent
->tok
)
1687 outflags
|= MMAN_PP
;
1688 outflags
|= MMAN_sp
| MMAN_nl
;
1689 outflags
&= ~MMAN_br
;
1697 if (SEC_SEE_ALSO
== n
->sec
) {
1698 outflags
|= MMAN_PP
| MMAN_sp
| MMAN_nl
;
1699 outflags
&= ~MMAN_br
;
1715 if (NULL
== n
->child
)
1716 outflags
^= MMAN_Sm
;
1717 else if (0 == strcmp("on", n
->child
->string
))
1718 outflags
|= MMAN_Sm
;
1720 outflags
&= ~MMAN_Sm
;
1722 if (MMAN_Sm
& outflags
)
1723 outflags
|= MMAN_spc
;
1731 if (outflags
& MMAN_PP
) {
1732 outflags
&= ~MMAN_PP
;
1733 print_line(".PP", 0);
1735 print_line(".sp", 0);
1736 if (n
->child
!= NULL
)
1737 print_word(n
->child
->string
);
1739 outflags
|= MMAN_nl
;
1753 print_line(".ta", 0);
1754 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
1755 print_word(n
->string
);
1756 outflags
|= MMAN_nl
;
1763 if (NODE_SYNPRETTY
& n
->flags
) {
1782 if (n
->flags
& NODE_SYNPRETTY
&& n
->type
!= ROFFT_BODY
)
1794 print_node(meta
, n
);
1798 outflags
&= ~MMAN_spc
;
1800 print_node(meta
, n
);