1 /* $Id: mdoc_man.c,v 1.130 2018/12/15 19:30:26 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
:
329 case ESCAPE_OVERSTRIKE
:
335 case ESCAPE_SKIPCHAR
:
346 font_push(char newfont
)
349 if (fontqueue
.head
+ fontqueue
.size
<= ++fontqueue
.tail
) {
351 fontqueue
.head
= mandoc_realloc(fontqueue
.head
,
354 *fontqueue
.tail
= newfont
;
358 outflags
&= ~MMAN_spc
;
365 if (fontqueue
.tail
> fontqueue
.head
)
367 outflags
&= ~MMAN_spc
;
370 putchar(*fontqueue
.tail
);
374 print_word(const char *s
)
377 if ((MMAN_PP
| MMAN_sp
| MMAN_br
| MMAN_nl
) & outflags
) {
379 * If we need a newline, print it now and start afresh.
381 if (MMAN_PP
& outflags
) {
382 if (MMAN_sp
& outflags
) {
383 if (MMAN_PD
& outflags
) {
385 outflags
&= ~MMAN_PD
;
387 } else if ( ! (MMAN_PD
& outflags
)) {
392 } else if (MMAN_sp
& outflags
)
394 else if (MMAN_br
& outflags
)
396 else if (MMAN_nl
& outflags
)
398 outflags
&= ~(MMAN_PP
|MMAN_sp
|MMAN_br
|MMAN_nl
|MMAN_spc
);
402 } else if (MMAN_spc
& outflags
) {
404 * If we need a space, only print it if
405 * (1) it is forced by `No' or
406 * (2) what follows is not terminating punctuation or
407 * (3) what follows is longer than one character.
409 if (MMAN_spc_force
& outflags
|| '\0' == s
[0] ||
410 NULL
== strchr(".,:;)]?!", s
[0]) || '\0' != s
[1]) {
411 if (MMAN_Bk
& outflags
&&
412 ! (MMAN_Bk_susp
& outflags
))
421 * Reassign needing space if we're not following opening
424 if (MMAN_Sm
& outflags
&& ('\0' == s
[0] ||
425 (('(' != s
[0] && '[' != s
[0]) || '\0' != s
[1])))
426 outflags
|= MMAN_spc
;
428 outflags
&= ~MMAN_spc
;
429 outflags
&= ~(MMAN_spc_force
| MMAN_Bk_susp
);
443 if (MMAN_nbrword
& outflags
) {
449 putchar((unsigned char)*s
);
455 outflags
&= ~MMAN_nbrword
;
459 print_line(const char *s
, int newflags
)
464 outflags
|= newflags
;
468 print_block(const char *s
, int newflags
)
471 outflags
&= ~MMAN_PP
;
472 if (MMAN_sp
& outflags
) {
473 outflags
&= ~(MMAN_sp
| MMAN_br
);
474 if (MMAN_PD
& outflags
) {
475 print_line(".PD", 0);
476 outflags
&= ~MMAN_PD
;
478 } else if (! (MMAN_PD
& outflags
))
479 print_line(".PD 0", MMAN_PD
);
482 outflags
|= MMAN_Bk_susp
| newflags
;
486 print_offs(const char *v
, int keywords
)
493 print_line(".RS", MMAN_Bk_susp
);
495 /* Convert v into a number (of characters). */
496 if (NULL
== v
|| '\0' == *v
|| (keywords
&& !strcmp(v
, "left")))
498 else if (keywords
&& !strcmp(v
, "indent"))
500 else if (keywords
&& !strcmp(v
, "indent-two"))
503 end
= a2roffsu(v
, &su
, SCALE_EN
);
504 if (end
== NULL
|| *end
!= '\0')
506 else if (SCALE_EN
== su
.unit
)
511 * If we are inside an enclosing list,
512 * there is no easy way to add the two
513 * indentations because they are provided
514 * in terms of different units.
523 * We are inside an enclosing list.
524 * Add the two indentations.
527 sz
+= Bl_stack
[Bl_stack_len
- 1];
529 (void)snprintf(buf
, sizeof(buf
), "%dn", sz
);
535 * Set up the indentation for a list item; used from pre_it().
538 print_width(const struct mdoc_bl
*bl
, const struct roff_node
*child
)
543 int numeric
, remain
, sz
, chsz
;
548 /* Convert the width into a number (of characters). */
549 if (bl
->width
== NULL
)
550 sz
= (bl
->type
== LIST_hang
) ? 6 : 0;
552 end
= a2roffsu(bl
->width
, &su
, SCALE_MAX
);
553 if (end
== NULL
|| *end
!= '\0')
554 sz
= man_strlen(bl
->width
);
555 else if (SCALE_EN
== su
.unit
)
563 /* XXX Rough estimation, might have multiple parts. */
564 if (bl
->type
== LIST_enum
)
565 chsz
= (bl
->count
> 8) + 1;
566 else if (child
!= NULL
&& child
->type
== ROFFT_TEXT
)
567 chsz
= man_strlen(child
->string
);
571 /* Maybe we are inside an enclosing list? */
575 * Save our own indentation,
576 * such that child lists can use it.
578 Bl_stack
[Bl_stack_len
++] = sz
+ 2;
580 /* Set up the current list. */
581 if (chsz
> sz
&& bl
->type
!= LIST_tag
)
582 print_block(".HP", 0);
584 print_block(".TP", 0);
588 (void)snprintf(buf
, sizeof(buf
), "%dn", sz
+ 2);
591 print_word(bl
->width
);
596 print_count(int *count
)
600 (void)snprintf(buf
, sizeof(buf
), "%d.\\&", ++*count
);
605 man_mdoc(void *arg
, const struct roff_man
*mdoc
)
609 printf(".\\\" Automatically generated from an mdoc input file."
611 for (n
= mdoc
->first
->child
; n
!= NULL
; n
= n
->next
) {
612 if (n
->type
!= ROFFT_COMMENT
)
614 printf(".\\\"%s\n", n
->string
);
617 printf(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"\n",
619 (mdoc
->meta
.msec
== NULL
? "" : mdoc
->meta
.msec
),
620 mdoc
->meta
.date
, mdoc
->meta
.os
, mdoc
->meta
.vol
);
622 /* Disable hyphenation and if nroff, disable justification. */
623 printf(".nh\n.if n .ad l");
625 outflags
= MMAN_nl
| MMAN_Sm
;
626 if (0 == fontqueue
.size
) {
628 fontqueue
.head
= fontqueue
.tail
= mandoc_malloc(8);
629 *fontqueue
.tail
= 'R';
631 for (; n
!= NULL
; n
= n
->next
)
632 print_node(&mdoc
->meta
, n
);
637 print_node(DECL_ARGS
)
639 const struct mdoc_man_act
*act
;
640 struct roff_node
*sub
;
643 if (n
->flags
& NODE_NOPRT
)
647 * Break the line if we were parsed subsequent the current node.
648 * This makes the page structure be more consistent.
650 if (MMAN_spc
& outflags
&& NODE_LINE
& n
->flags
)
656 n
->flags
&= ~NODE_ENDED
;
658 if (n
->type
== ROFFT_TEXT
) {
660 * Make sure that we don't happen to start with a
661 * control character at the start of a line.
663 if (MMAN_nl
& outflags
&&
664 ('.' == *n
->string
|| '\'' == *n
->string
)) {
667 outflags
&= ~MMAN_spc
;
669 if (n
->flags
& NODE_DELIMC
)
670 outflags
&= ~(MMAN_spc
| MMAN_spc_force
);
671 else if (outflags
& MMAN_Sm
)
672 outflags
|= MMAN_spc_force
;
673 print_word(n
->string
);
674 if (n
->flags
& NODE_DELIMO
)
675 outflags
&= ~(MMAN_spc
| MMAN_spc_force
);
676 else if (outflags
& MMAN_Sm
)
677 outflags
|= MMAN_spc
;
678 } else if (n
->tok
< ROFF_MAX
) {
679 (*roff_man_acts
[n
->tok
])(meta
, n
);
683 * Conditionally run the pre-node action handler for a
686 act
= mdoc_man_act(n
->tok
);
687 cond
= act
->cond
== NULL
|| (*act
->cond
)(meta
, n
);
688 if (cond
&& act
->pre
!= NULL
&&
689 (n
->end
== ENDBODY_NOT
|| n
->child
!= NULL
))
690 do_sub
= (*act
->pre
)(meta
, n
);
694 * Conditionally run all child nodes.
695 * Note that this iterates over children instead of using
696 * recursion. This prevents unnecessary depth in the stack.
699 for (sub
= n
->child
; sub
; sub
= sub
->next
)
700 print_node(meta
, sub
);
703 * Lastly, conditionally run the post-node handler.
705 if (NODE_ENDED
& n
->flags
)
708 if (cond
&& act
->post
)
709 (*act
->post
)(meta
, n
);
711 if (ENDBODY_NOT
!= n
->end
)
712 n
->body
->flags
|= NODE_ENDED
;
719 return n
->type
== ROFFT_HEAD
;
726 return n
->type
== ROFFT_BODY
;
740 prefix
= mdoc_man_act(n
->tok
)->prefix
;
744 outflags
&= ~MMAN_spc
;
753 suffix
= mdoc_man_act(n
->tok
)->suffix
;
756 outflags
&= ~(MMAN_spc
| MMAN_nl
);
763 outflags
|= MMAN_br
| MMAN_nl
;
775 post_percent(DECL_ARGS
)
778 if (mdoc_man_act(n
->tok
)->pre
== pre_em
)
782 if (n
->prev
&& n
->prev
->tok
== n
->tok
&&
783 n
->next
->tok
== n
->tok
)
795 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
) {
797 outflags
&= ~MMAN_spc
;
807 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
) {
808 outflags
&= ~MMAN_spc
;
812 post_percent(meta
, n
);
816 * Print before a section header.
822 if (n
->type
== ROFFT_HEAD
) {
824 print_block(mdoc_man_act(n
->tok
)->prefix
, 0);
827 outflags
&= ~MMAN_spc
;
833 * Print subsequent a section header.
839 if (n
->type
!= ROFFT_HEAD
)
841 outflags
&= ~MMAN_spc
;
845 if (MDOC_Sh
== n
->tok
&& SEC_AUTHORS
== n
->sec
)
846 outflags
&= ~(MMAN_An_split
| MMAN_An_nosplit
);
849 /* See mdoc_term.c, synopsis_pre() for comments. */
851 pre_syn(const struct roff_node
*n
)
854 if (NULL
== n
->prev
|| ! (NODE_SYNPRETTY
& n
->flags
))
857 if (n
->prev
->tok
== n
->tok
&&
865 switch (n
->prev
->tok
) {
874 if (MDOC_Fn
!= n
->tok
&& MDOC_Fo
!= n
->tok
) {
889 switch (n
->norm
->An
.auth
) {
891 outflags
&= ~MMAN_An_nosplit
;
892 outflags
|= MMAN_An_split
;
895 outflags
&= ~MMAN_An_split
;
896 outflags
|= MMAN_An_nosplit
;
899 if (MMAN_An_split
& outflags
)
901 else if (SEC_AUTHORS
== n
->sec
&&
902 ! (MMAN_An_nosplit
& outflags
))
903 outflags
|= MMAN_An_split
;
912 outflags
&= ~MMAN_spc
;
914 outflags
&= ~MMAN_spc
;
922 print_word(n
->child
!= NULL
&& n
->child
->next
== NULL
&&
923 n
->child
->tok
== MDOC_Mt
? "<" : "\\(la");
924 outflags
&= ~MMAN_spc
;
932 outflags
&= ~(MMAN_spc
| MMAN_nl
);
933 print_word(n
->child
!= NULL
&& n
->child
->next
== NULL
&&
934 n
->child
->tok
== MDOC_Mt
? ">" : "\\(ra");
941 outflags
&= ~(MMAN_PP
| MMAN_sp
| MMAN_br
);
943 if (DISP_unfilled
== n
->norm
->Bd
.type
||
944 DISP_literal
== n
->norm
->Bd
.type
)
945 print_line(".nf", 0);
946 if (0 == n
->norm
->Bd
.comp
&& NULL
!= n
->parent
->prev
)
948 print_offs(n
->norm
->Bd
.offs
, 1);
956 /* Close out this display. */
957 print_line(".RE", MMAN_nl
);
958 if (DISP_unfilled
== n
->norm
->Bd
.type
||
959 DISP_literal
== n
->norm
->Bd
.type
)
960 print_line(".fi", MMAN_nl
);
962 /* Maybe we are inside an enclosing list? */
963 if (NULL
!= n
->parent
->next
)
979 switch (n
->norm
->Bf
.font
) {
997 if (n
->type
== ROFFT_BODY
)
1009 outflags
|= MMAN_Bk
;
1021 while ((n
= n
->parent
) != NULL
)
1022 if (n
->tok
== MDOC_Bk
)
1026 outflags
&= ~MMAN_Bk
;
1039 * print_offs() will increase the -offset to account for
1040 * a possible enclosing .It, but any enclosed .It blocks
1041 * just nest and do not add up their indentation.
1043 if (n
->norm
->Bl
.offs
) {
1044 print_offs(n
->norm
->Bl
.offs
, 0);
1045 Bl_stack
[Bl_stack_len
++] = 0;
1048 switch (n
->norm
->Bl
.type
) {
1050 n
->norm
->Bl
.count
= 0;
1058 if (n
->child
!= NULL
) {
1059 print_line(".TS", MMAN_nl
);
1060 for (icol
= 0; icol
< n
->norm
->Bl
.ncols
; icol
++)
1064 outflags
|= MMAN_nl
;
1072 switch (n
->norm
->Bl
.type
) {
1074 if (n
->child
!= NULL
)
1075 print_line(".TE", 0);
1078 n
->norm
->Bl
.count
= 0;
1084 if (n
->norm
->Bl
.offs
) {
1085 print_line(".RE", MMAN_nl
);
1086 assert(Bl_stack_len
);
1088 assert(0 == Bl_stack
[Bl_stack_len
]);
1090 outflags
|= MMAN_PP
| MMAN_nl
;
1091 outflags
&= ~(MMAN_sp
| MMAN_br
);
1094 /* Maybe we are inside an enclosing list? */
1095 if (NULL
!= n
->parent
->next
)
1103 outflags
|= MMAN_br
;
1110 print_offs("6n", 0);
1118 print_line(".RE", MMAN_nl
);
1120 /* Maybe we are inside an enclosing list? */
1121 if (NULL
!= n
->parent
->next
)
1137 if (NULL
== n
->norm
->Es
||
1138 NULL
== n
->norm
->Es
->child
)
1141 print_word(n
->norm
->Es
->child
->string
);
1142 outflags
&= ~MMAN_spc
;
1150 if (NULL
== n
->norm
->Es
||
1151 NULL
== n
->norm
->Es
->child
||
1152 NULL
== n
->norm
->Es
->child
->next
)
1155 outflags
&= ~MMAN_spc
;
1156 print_word(n
->norm
->Es
->child
->next
->string
);
1164 if (n
->end
== ENDBODY_NOT
&&
1165 n
->parent
->head
->child
== NULL
&&
1167 n
->child
->end
!= ENDBODY_NOT
)
1169 else if (n
->end
!= ENDBODY_NOT
? n
->child
!= NULL
:
1170 n
->parent
->head
->child
!= NULL
&& (n
->child
!= NULL
||
1171 (n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
)))
1172 outflags
&= ~(MMAN_spc
| MMAN_nl
);
1181 if (n
->end
!= ENDBODY_NOT
) {
1182 outflags
|= MMAN_spc
;
1186 body
= n
->child
!= NULL
|| n
->parent
->head
->child
!= NULL
;
1187 tail
= n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
;
1190 outflags
&= ~MMAN_spc
;
1191 else if ( ! (body
|| tail
))
1194 outflags
|= MMAN_spc
;
1202 am_Fa
= MDOC_Fa
== n
->tok
;
1209 if (am_Fa
|| NODE_SYNPRETTY
& n
->flags
)
1210 outflags
|= MMAN_nbrword
;
1211 print_node(meta
, n
);
1213 if (NULL
!= (n
= n
->next
))
1223 if (NULL
!= n
->next
&& MDOC_Fa
== n
->next
->tok
)
1241 outflags
|= MMAN_br
;
1250 if (n
->child
!= NULL
)
1251 outflags
&= ~MMAN_spc
;
1260 if (!(n
->child
!= NULL
||
1262 n
->next
->type
== ROFFT_TEXT
||
1263 n
->next
->flags
& NODE_LINE
))
1264 outflags
&= ~MMAN_spc
;
1277 if (NODE_SYNPRETTY
& n
->flags
)
1278 print_block(".HP 4n", MMAN_nl
);
1281 print_node(meta
, n
);
1283 outflags
&= ~MMAN_spc
;
1285 outflags
&= ~MMAN_spc
;
1298 if (NODE_SYNPRETTY
& n
->flags
) {
1300 outflags
|= MMAN_PP
;
1313 if (n
->child
== NULL
)
1315 if (NODE_SYNPRETTY
& n
->flags
)
1316 print_block(".HP 4n", MMAN_nl
);
1320 outflags
&= ~(MMAN_spc
| MMAN_nl
);
1322 outflags
&= ~MMAN_spc
;
1336 if (n
->child
!= NULL
)
1359 print_line(".ft", 0);
1360 print_word(n
->child
->string
);
1361 outflags
|= MMAN_nl
;
1368 if (NODE_SYNPRETTY
& n
->flags
) {
1371 print_word("#include <");
1372 outflags
&= ~MMAN_spc
;
1375 outflags
&= ~MMAN_spc
;
1385 if (NODE_SYNPRETTY
& n
->flags
) {
1386 outflags
&= ~MMAN_spc
;
1389 outflags
|= MMAN_br
;
1392 outflags
&= ~MMAN_spc
;
1400 const struct roff_node
*bln
;
1404 outflags
|= MMAN_PP
| MMAN_nl
;
1405 bln
= n
->parent
->parent
;
1406 if (0 == bln
->norm
->Bl
.comp
||
1407 (NULL
== n
->parent
->prev
&&
1408 NULL
== bln
->parent
->prev
))
1409 outflags
|= MMAN_sp
;
1410 outflags
&= ~MMAN_br
;
1411 switch (bln
->norm
->Bl
.type
) {
1417 if (bln
->norm
->Bl
.type
== LIST_diag
)
1418 print_line(".B \"", 0);
1420 print_line(".BR \\& \"", 0);
1421 outflags
&= ~MMAN_spc
;
1426 print_width(&bln
->norm
->Bl
, NULL
);
1428 outflags
|= MMAN_nl
;
1430 if (LIST_bullet
== bln
->norm
->Bl
.type
)
1431 print_word("\\(bu");
1435 outflags
|= MMAN_nl
;
1438 print_width(&bln
->norm
->Bl
, NULL
);
1440 outflags
|= MMAN_nl
;
1441 print_count(&bln
->norm
->Bl
.count
);
1442 outflags
|= MMAN_nl
;
1445 print_width(&bln
->norm
->Bl
, n
->child
);
1447 outflags
|= MMAN_nl
;
1450 print_width(&bln
->norm
->Bl
, n
->child
);
1452 outflags
&= ~MMAN_spc
;
1464 * This function is called after closing out an indented block.
1465 * If we are inside an enclosing list, restore its indentation.
1472 /* Nothing to do outside a list. */
1473 if (0 == Bl_stack_len
|| 0 == Bl_stack
[Bl_stack_len
- 1])
1476 /* The indentation has already been set up. */
1477 if (Bl_stack_post
[Bl_stack_len
- 1])
1480 /* Restore the indentation of the enclosing list. */
1481 print_line(".RS", MMAN_Bk_susp
);
1482 (void)snprintf(buf
, sizeof(buf
), "%dn",
1483 Bl_stack
[Bl_stack_len
- 1]);
1486 /* Remeber to close out this .RS block later. */
1487 Bl_stack_post
[Bl_stack_len
- 1] = 1;
1493 const struct roff_node
*bln
;
1495 bln
= n
->parent
->parent
;
1499 switch (bln
->norm
->Bl
.type
) {
1501 outflags
&= ~MMAN_spc
;
1505 outflags
|= MMAN_br
;
1512 switch (bln
->norm
->Bl
.type
) {
1519 assert(Bl_stack_len
);
1520 Bl_stack
[--Bl_stack_len
] = 0;
1523 * Our indentation had to be restored
1524 * after a child display or child list.
1525 * Close out that indentation block now.
1527 if (Bl_stack_post
[Bl_stack_len
]) {
1528 print_line(".RE", MMAN_nl
);
1529 Bl_stack_post
[Bl_stack_len
] = 0;
1533 if (NULL
!= n
->next
) {
1535 outflags
&= ~MMAN_spc
;
1551 if (SEC_LIBRARY
== n
->sec
)
1552 outflags
|= MMAN_br
;
1558 const struct roff_node
*link
, *descr
, *punct
;
1560 if ((link
= n
->child
) == NULL
)
1563 /* Find beginning of trailing punctuation. */
1565 while (punct
!= link
&& punct
->flags
& NODE_DELIMC
)
1566 punct
= punct
->prev
;
1567 punct
= punct
->next
;
1570 if ((descr
= link
->next
) != NULL
&& descr
!= punct
) {
1572 while (descr
!= punct
) {
1573 print_word(descr
->string
);
1574 descr
= descr
->next
;
1582 print_word(link
->string
);
1585 /* Trailing punctuation. */
1586 while (punct
!= NULL
) {
1587 print_word(punct
->string
);
1588 punct
= punct
->next
;
1594 pre_onearg(DECL_ARGS
)
1596 outflags
|= MMAN_nl
;
1598 outflags
&= ~MMAN_spc
;
1599 print_word(roff_name
[n
->tok
]);
1600 if (n
->child
!= NULL
)
1601 print_word(n
->child
->string
);
1602 outflags
|= MMAN_nl
;
1603 if (n
->tok
== ROFF_ce
)
1604 for (n
= n
->child
->next
; n
!= NULL
; n
= n
->next
)
1605 print_node(meta
, n
);
1621 if (n
->type
== ROFFT_BLOCK
) {
1622 outflags
|= MMAN_Bk
;
1625 if (n
->type
!= ROFFT_ELEM
&& n
->type
!= ROFFT_HEAD
)
1627 name
= n
->child
== NULL
? NULL
: n
->child
->string
;
1630 if (n
->type
== ROFFT_HEAD
) {
1631 if (NULL
== n
->parent
->prev
)
1632 outflags
|= MMAN_sp
;
1633 print_block(".HP", 0);
1634 printf(" %dn", man_strlen(name
) + 1);
1635 outflags
|= MMAN_nl
;
1647 outflags
&= ~MMAN_Bk
;
1651 if (n
->child
!= NULL
&& n
->child
->string
!= NULL
)
1663 outflags
|= MMAN_spc_force
;
1671 outflags
&= ~MMAN_spc
;
1679 if ( ! (n
->next
== NULL
|| n
->next
->flags
& NODE_LINE
))
1680 outflags
&= ~MMAN_spc
;
1687 if (MDOC_It
!= n
->parent
->tok
)
1688 outflags
|= MMAN_PP
;
1689 outflags
|= MMAN_sp
| MMAN_nl
;
1690 outflags
&= ~MMAN_br
;
1698 if (SEC_SEE_ALSO
== n
->sec
) {
1699 outflags
|= MMAN_PP
| MMAN_sp
| MMAN_nl
;
1700 outflags
&= ~MMAN_br
;
1716 if (NULL
== n
->child
)
1717 outflags
^= MMAN_Sm
;
1718 else if (0 == strcmp("on", n
->child
->string
))
1719 outflags
|= MMAN_Sm
;
1721 outflags
&= ~MMAN_Sm
;
1723 if (MMAN_Sm
& outflags
)
1724 outflags
|= MMAN_spc
;
1732 if (outflags
& MMAN_PP
) {
1733 outflags
&= ~MMAN_PP
;
1734 print_line(".PP", 0);
1736 print_line(".sp", 0);
1737 if (n
->child
!= NULL
)
1738 print_word(n
->child
->string
);
1740 outflags
|= MMAN_nl
;
1754 print_line(".ta", 0);
1755 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
1756 print_word(n
->string
);
1757 outflags
|= MMAN_nl
;
1764 if (NODE_SYNPRETTY
& n
->flags
) {
1783 if (n
->flags
& NODE_SYNPRETTY
&& n
->type
!= ROFFT_BODY
)
1795 print_node(meta
, n
);
1799 outflags
&= ~MMAN_spc
;
1801 print_node(meta
, n
);