1 /* $Id: mdoc_man.c,v 1.128 2018/08/23 19:33:27 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_an(DECL_ARGS
);
79 static int pre_ap(DECL_ARGS
);
80 static int pre_aq(DECL_ARGS
);
81 static int pre_bd(DECL_ARGS
);
82 static int pre_bf(DECL_ARGS
);
83 static int pre_bk(DECL_ARGS
);
84 static int pre_bl(DECL_ARGS
);
85 static void pre_br(DECL_ARGS
);
86 static int pre_dl(DECL_ARGS
);
87 static int pre_en(DECL_ARGS
);
88 static int pre_enc(DECL_ARGS
);
89 static int pre_em(DECL_ARGS
);
90 static int pre_skip(DECL_ARGS
);
91 static int pre_eo(DECL_ARGS
);
92 static int pre_ex(DECL_ARGS
);
93 static int pre_fa(DECL_ARGS
);
94 static int pre_fd(DECL_ARGS
);
95 static int pre_fl(DECL_ARGS
);
96 static int pre_fn(DECL_ARGS
);
97 static int pre_fo(DECL_ARGS
);
98 static void pre_ft(DECL_ARGS
);
99 static int pre_Ft(DECL_ARGS
);
100 static int pre_in(DECL_ARGS
);
101 static int pre_it(DECL_ARGS
);
102 static int pre_lk(DECL_ARGS
);
103 static int pre_li(DECL_ARGS
);
104 static int pre_nm(DECL_ARGS
);
105 static int pre_no(DECL_ARGS
);
106 static int pre_ns(DECL_ARGS
);
107 static void pre_onearg(DECL_ARGS
);
108 static int pre_pp(DECL_ARGS
);
109 static int pre_rs(DECL_ARGS
);
110 static int pre_sm(DECL_ARGS
);
111 static void pre_sp(DECL_ARGS
);
112 static int pre_sect(DECL_ARGS
);
113 static int pre_sy(DECL_ARGS
);
114 static void pre_syn(const struct roff_node
*);
115 static void pre_ta(DECL_ARGS
);
116 static int pre_vt(DECL_ARGS
);
117 static int pre_xr(DECL_ARGS
);
118 static void print_word(const char *);
119 static void print_line(const char *, int);
120 static void print_block(const char *, int);
121 static void print_offs(const char *, int);
122 static void print_width(const struct mdoc_bl
*,
123 const struct roff_node
*);
124 static void print_count(int *);
125 static void print_node(DECL_ARGS
);
127 static const void_fp roff_man_acts
[ROFF_MAX
] = {
140 static const struct mdoc_man_act mdoc_man_acts
[MDOC_MAX
- MDOC_Dd
] = {
141 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dd */
142 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dt */
143 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Os */
144 { NULL
, pre_sect
, post_sect
, ".SH", NULL
}, /* Sh */
145 { NULL
, pre_sect
, post_sect
, ".SS", NULL
}, /* Ss */
146 { NULL
, pre_pp
, NULL
, NULL
, NULL
}, /* Pp */
147 { cond_body
, pre_dl
, post_dl
, NULL
, NULL
}, /* D1 */
148 { cond_body
, pre_dl
, post_dl
, NULL
, NULL
}, /* Dl */
149 { cond_body
, pre_bd
, post_bd
, NULL
, NULL
}, /* Bd */
150 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ed */
151 { cond_body
, pre_bl
, post_bl
, NULL
, NULL
}, /* Bl */
152 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* El */
153 { NULL
, pre_it
, post_it
, NULL
, NULL
}, /* It */
154 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Ad */
155 { NULL
, pre_an
, NULL
, NULL
, NULL
}, /* An */
156 { NULL
, pre_ap
, NULL
, NULL
, NULL
}, /* Ap */
157 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Ar */
158 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Cd */
159 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Cm */
160 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Dv */
161 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Er */
162 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Ev */
163 { NULL
, pre_ex
, NULL
, NULL
, NULL
}, /* Ex */
164 { NULL
, pre_fa
, post_fa
, NULL
, NULL
}, /* Fa */
165 { NULL
, pre_fd
, post_fd
, NULL
, NULL
}, /* Fd */
166 { NULL
, pre_fl
, post_fl
, NULL
, NULL
}, /* Fl */
167 { NULL
, pre_fn
, post_fn
, NULL
, NULL
}, /* Fn */
168 { NULL
, pre_Ft
, post_font
, NULL
, NULL
}, /* Ft */
169 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Ic */
170 { NULL
, pre_in
, post_in
, NULL
, NULL
}, /* In */
171 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Li */
172 { cond_head
, pre_enc
, NULL
, "\\- ", NULL
}, /* Nd */
173 { NULL
, pre_nm
, post_nm
, NULL
, NULL
}, /* Nm */
174 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Op */
175 { NULL
, pre_Ft
, post_font
, NULL
, NULL
}, /* Ot */
176 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Pa */
177 { NULL
, pre_ex
, NULL
, NULL
, NULL
}, /* Rv */
178 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* St */
179 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Va */
180 { NULL
, pre_vt
, post_vt
, NULL
, NULL
}, /* Vt */
181 { NULL
, pre_xr
, NULL
, NULL
, NULL
}, /* Xr */
182 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %A */
183 { NULL
, pre_em
, post_percent
, NULL
, NULL
}, /* %B */
184 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %D */
185 { NULL
, pre_em
, post_percent
, NULL
, NULL
}, /* %I */
186 { NULL
, pre_em
, post_percent
, NULL
, NULL
}, /* %J */
187 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %N */
188 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %O */
189 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %P */
190 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %R */
191 { NULL
, pre__t
, post__t
, NULL
, NULL
}, /* %T */
192 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %V */
193 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ac */
194 { cond_body
, pre_aq
, post_aq
, NULL
, NULL
}, /* Ao */
195 { cond_body
, pre_aq
, post_aq
, NULL
, NULL
}, /* Aq */
196 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* At */
197 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bc */
198 { NULL
, pre_bf
, post_bf
, NULL
, NULL
}, /* Bf */
199 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Bo */
200 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Bq */
201 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Bsx */
202 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Bx */
203 { NULL
, pre_skip
, NULL
, NULL
, NULL
}, /* Db */
204 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dc */
205 { cond_body
, pre_enc
, post_enc
, "\\(lq", "\\(rq" }, /* Do */
206 { cond_body
, pre_enc
, post_enc
, "\\(lq", "\\(rq" }, /* Dq */
207 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ec */
208 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ef */
209 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Em */
210 { cond_body
, pre_eo
, post_eo
, NULL
, NULL
}, /* Eo */
211 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Fx */
212 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Ms */
213 { NULL
, pre_no
, NULL
, NULL
, NULL
}, /* No */
214 { NULL
, pre_ns
, NULL
, NULL
, NULL
}, /* Ns */
215 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Nx */
216 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Ox */
217 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Pc */
218 { NULL
, NULL
, post_pf
, NULL
, NULL
}, /* Pf */
219 { cond_body
, pre_enc
, post_enc
, "(", ")" }, /* Po */
220 { cond_body
, pre_enc
, post_enc
, "(", ")" }, /* Pq */
221 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Qc */
222 { cond_body
, pre_enc
, post_enc
, "\\(oq", "\\(cq" }, /* Ql */
223 { cond_body
, pre_enc
, post_enc
, "\"", "\"" }, /* Qo */
224 { cond_body
, pre_enc
, post_enc
, "\"", "\"" }, /* Qq */
225 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Re */
226 { cond_body
, pre_rs
, NULL
, NULL
, NULL
}, /* Rs */
227 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Sc */
228 { cond_body
, pre_enc
, post_enc
, "\\(oq", "\\(cq" }, /* So */
229 { cond_body
, pre_enc
, post_enc
, "\\(oq", "\\(cq" }, /* Sq */
230 { NULL
, pre_sm
, NULL
, NULL
, NULL
}, /* Sm */
231 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Sx */
232 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Sy */
233 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Tn */
234 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ux */
235 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xc */
236 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xo */
237 { NULL
, pre_fo
, post_fo
, NULL
, NULL
}, /* Fo */
238 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fc */
239 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Oo */
240 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Oc */
241 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Bk */
242 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ek */
243 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bt */
244 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Hf */
245 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Fr */
246 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ud */
247 { NULL
, NULL
, post_lb
, NULL
, NULL
}, /* Lb */
248 { NULL
, pre_pp
, NULL
, NULL
, NULL
}, /* Lp */
249 { NULL
, pre_lk
, NULL
, NULL
, NULL
}, /* Lk */
250 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Mt */
251 { cond_body
, pre_enc
, post_enc
, "{", "}" }, /* Brq */
252 { cond_body
, pre_enc
, post_enc
, "{", "}" }, /* Bro */
253 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Brc */
254 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %C */
255 { NULL
, pre_skip
, NULL
, NULL
, NULL
}, /* Es */
256 { cond_body
, pre_en
, post_en
, NULL
, NULL
}, /* En */
257 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Dx */
258 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %Q */
259 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %U */
260 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ta */
262 static const struct mdoc_man_act
*mdoc_man_act(enum roff_tok
);
265 #define MMAN_spc (1 << 0) /* blank character before next word */
266 #define MMAN_spc_force (1 << 1) /* even before trailing punctuation */
267 #define MMAN_nl (1 << 2) /* break man(7) code line */
268 #define MMAN_br (1 << 3) /* break output line */
269 #define MMAN_sp (1 << 4) /* insert a blank output line */
270 #define MMAN_PP (1 << 5) /* reset indentation etc. */
271 #define MMAN_Sm (1 << 6) /* horizontal spacing mode */
272 #define MMAN_Bk (1 << 7) /* word keep mode */
273 #define MMAN_Bk_susp (1 << 8) /* suspend this (after a macro) */
274 #define MMAN_An_split (1 << 9) /* author mode is "split" */
275 #define MMAN_An_nosplit (1 << 10) /* author mode is "nosplit" */
276 #define MMAN_PD (1 << 11) /* inter-paragraph spacing disabled */
277 #define MMAN_nbrword (1 << 12) /* do not break the next word */
279 #define BL_STACK_MAX 32
281 static int Bl_stack
[BL_STACK_MAX
]; /* offsets [chars] */
282 static int Bl_stack_post
[BL_STACK_MAX
]; /* add final .RE */
283 static int Bl_stack_len
; /* number of nested Bl blocks */
284 static int TPremain
; /* characters before tag is full */
293 static const struct mdoc_man_act
*
294 mdoc_man_act(enum roff_tok tok
)
296 assert(tok
>= MDOC_Dd
&& tok
<= MDOC_MAX
);
297 return mdoc_man_acts
+ (tok
- MDOC_Dd
);
301 man_strlen(const char *cp
)
309 rsz
= strcspn(cp
, "\\");
321 switch (mandoc_escape(&cp
, NULL
, NULL
)) {
325 case ESCAPE_NUMBERED
:
327 case ESCAPE_OVERSTRIKE
:
333 case ESCAPE_SKIPCHAR
:
344 font_push(char newfont
)
347 if (fontqueue
.head
+ fontqueue
.size
<= ++fontqueue
.tail
) {
349 fontqueue
.head
= mandoc_realloc(fontqueue
.head
,
352 *fontqueue
.tail
= newfont
;
356 outflags
&= ~MMAN_spc
;
363 if (fontqueue
.tail
> fontqueue
.head
)
365 outflags
&= ~MMAN_spc
;
368 putchar(*fontqueue
.tail
);
372 print_word(const char *s
)
375 if ((MMAN_PP
| MMAN_sp
| MMAN_br
| MMAN_nl
) & outflags
) {
377 * If we need a newline, print it now and start afresh.
379 if (MMAN_PP
& outflags
) {
380 if (MMAN_sp
& outflags
) {
381 if (MMAN_PD
& outflags
) {
383 outflags
&= ~MMAN_PD
;
385 } else if ( ! (MMAN_PD
& outflags
)) {
390 } else if (MMAN_sp
& outflags
)
392 else if (MMAN_br
& outflags
)
394 else if (MMAN_nl
& outflags
)
396 outflags
&= ~(MMAN_PP
|MMAN_sp
|MMAN_br
|MMAN_nl
|MMAN_spc
);
400 } else if (MMAN_spc
& outflags
) {
402 * If we need a space, only print it if
403 * (1) it is forced by `No' or
404 * (2) what follows is not terminating punctuation or
405 * (3) what follows is longer than one character.
407 if (MMAN_spc_force
& outflags
|| '\0' == s
[0] ||
408 NULL
== strchr(".,:;)]?!", s
[0]) || '\0' != s
[1]) {
409 if (MMAN_Bk
& outflags
&&
410 ! (MMAN_Bk_susp
& outflags
))
419 * Reassign needing space if we're not following opening
422 if (MMAN_Sm
& outflags
&& ('\0' == s
[0] ||
423 (('(' != s
[0] && '[' != s
[0]) || '\0' != s
[1])))
424 outflags
|= MMAN_spc
;
426 outflags
&= ~MMAN_spc
;
427 outflags
&= ~(MMAN_spc_force
| MMAN_Bk_susp
);
441 if (MMAN_nbrword
& outflags
) {
447 putchar((unsigned char)*s
);
453 outflags
&= ~MMAN_nbrword
;
457 print_line(const char *s
, int newflags
)
462 outflags
|= newflags
;
466 print_block(const char *s
, int newflags
)
469 outflags
&= ~MMAN_PP
;
470 if (MMAN_sp
& outflags
) {
471 outflags
&= ~(MMAN_sp
| MMAN_br
);
472 if (MMAN_PD
& outflags
) {
473 print_line(".PD", 0);
474 outflags
&= ~MMAN_PD
;
476 } else if (! (MMAN_PD
& outflags
))
477 print_line(".PD 0", MMAN_PD
);
480 outflags
|= MMAN_Bk_susp
| newflags
;
484 print_offs(const char *v
, int keywords
)
491 print_line(".RS", MMAN_Bk_susp
);
493 /* Convert v into a number (of characters). */
494 if (NULL
== v
|| '\0' == *v
|| (keywords
&& !strcmp(v
, "left")))
496 else if (keywords
&& !strcmp(v
, "indent"))
498 else if (keywords
&& !strcmp(v
, "indent-two"))
501 end
= a2roffsu(v
, &su
, SCALE_EN
);
502 if (end
== NULL
|| *end
!= '\0')
504 else if (SCALE_EN
== su
.unit
)
509 * If we are inside an enclosing list,
510 * there is no easy way to add the two
511 * indentations because they are provided
512 * in terms of different units.
521 * We are inside an enclosing list.
522 * Add the two indentations.
525 sz
+= Bl_stack
[Bl_stack_len
- 1];
527 (void)snprintf(buf
, sizeof(buf
), "%dn", sz
);
533 * Set up the indentation for a list item; used from pre_it().
536 print_width(const struct mdoc_bl
*bl
, const struct roff_node
*child
)
541 int numeric
, remain
, sz
, chsz
;
546 /* Convert the width into a number (of characters). */
547 if (bl
->width
== NULL
)
548 sz
= (bl
->type
== LIST_hang
) ? 6 : 0;
550 end
= a2roffsu(bl
->width
, &su
, SCALE_MAX
);
551 if (end
== NULL
|| *end
!= '\0')
552 sz
= man_strlen(bl
->width
);
553 else if (SCALE_EN
== su
.unit
)
561 /* XXX Rough estimation, might have multiple parts. */
562 if (bl
->type
== LIST_enum
)
563 chsz
= (bl
->count
> 8) + 1;
564 else if (child
!= NULL
&& child
->type
== ROFFT_TEXT
)
565 chsz
= man_strlen(child
->string
);
569 /* Maybe we are inside an enclosing list? */
573 * Save our own indentation,
574 * such that child lists can use it.
576 Bl_stack
[Bl_stack_len
++] = sz
+ 2;
578 /* Set up the current list. */
579 if (chsz
> sz
&& bl
->type
!= LIST_tag
)
580 print_block(".HP", 0);
582 print_block(".TP", 0);
586 (void)snprintf(buf
, sizeof(buf
), "%dn", sz
+ 2);
589 print_word(bl
->width
);
594 print_count(int *count
)
598 (void)snprintf(buf
, sizeof(buf
), "%d.\\&", ++*count
);
603 man_mdoc(void *arg
, const struct roff_man
*mdoc
)
607 printf(".\\\" Automatically generated from an mdoc input file."
609 for (n
= mdoc
->first
->child
; n
!= NULL
; n
= n
->next
) {
610 if (n
->type
!= ROFFT_COMMENT
)
612 printf(".\\\"%s\n", n
->string
);
615 printf(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"\n",
617 (mdoc
->meta
.msec
== NULL
? "" : mdoc
->meta
.msec
),
618 mdoc
->meta
.date
, mdoc
->meta
.os
, mdoc
->meta
.vol
);
620 /* Disable hyphenation and if nroff, disable justification. */
621 printf(".nh\n.if n .ad l");
623 outflags
= MMAN_nl
| MMAN_Sm
;
624 if (0 == fontqueue
.size
) {
626 fontqueue
.head
= fontqueue
.tail
= mandoc_malloc(8);
627 *fontqueue
.tail
= 'R';
629 for (; n
!= NULL
; n
= n
->next
)
630 print_node(&mdoc
->meta
, n
);
635 print_node(DECL_ARGS
)
637 const struct mdoc_man_act
*act
;
638 struct roff_node
*sub
;
641 if (n
->flags
& NODE_NOPRT
)
645 * Break the line if we were parsed subsequent the current node.
646 * This makes the page structure be more consistent.
648 if (MMAN_spc
& outflags
&& NODE_LINE
& n
->flags
)
654 n
->flags
&= ~NODE_ENDED
;
656 if (n
->type
== ROFFT_TEXT
) {
658 * Make sure that we don't happen to start with a
659 * control character at the start of a line.
661 if (MMAN_nl
& outflags
&&
662 ('.' == *n
->string
|| '\'' == *n
->string
)) {
665 outflags
&= ~MMAN_spc
;
667 if (n
->flags
& NODE_DELIMC
)
668 outflags
&= ~(MMAN_spc
| MMAN_spc_force
);
669 else if (outflags
& MMAN_Sm
)
670 outflags
|= MMAN_spc_force
;
671 print_word(n
->string
);
672 if (n
->flags
& NODE_DELIMO
)
673 outflags
&= ~(MMAN_spc
| MMAN_spc_force
);
674 else if (outflags
& MMAN_Sm
)
675 outflags
|= MMAN_spc
;
676 } else if (n
->tok
< ROFF_MAX
) {
677 (*roff_man_acts
[n
->tok
])(meta
, n
);
681 * Conditionally run the pre-node action handler for a
684 act
= mdoc_man_act(n
->tok
);
685 cond
= act
->cond
== NULL
|| (*act
->cond
)(meta
, n
);
686 if (cond
&& act
->pre
!= NULL
&&
687 (n
->end
== ENDBODY_NOT
|| n
->child
!= NULL
))
688 do_sub
= (*act
->pre
)(meta
, n
);
692 * Conditionally run all child nodes.
693 * Note that this iterates over children instead of using
694 * recursion. This prevents unnecessary depth in the stack.
697 for (sub
= n
->child
; sub
; sub
= sub
->next
)
698 print_node(meta
, sub
);
701 * Lastly, conditionally run the post-node handler.
703 if (NODE_ENDED
& n
->flags
)
706 if (cond
&& act
->post
)
707 (*act
->post
)(meta
, n
);
709 if (ENDBODY_NOT
!= n
->end
)
710 n
->body
->flags
|= NODE_ENDED
;
717 return n
->type
== ROFFT_HEAD
;
724 return n
->type
== ROFFT_BODY
;
732 prefix
= mdoc_man_act(n
->tok
)->prefix
;
736 outflags
&= ~MMAN_spc
;
745 suffix
= mdoc_man_act(n
->tok
)->suffix
;
748 outflags
&= ~(MMAN_spc
| MMAN_nl
);
755 outflags
|= MMAN_br
| MMAN_nl
;
767 post_percent(DECL_ARGS
)
770 if (mdoc_man_act(n
->tok
)->pre
== pre_em
)
774 if (n
->prev
&& n
->prev
->tok
== n
->tok
&&
775 n
->next
->tok
== n
->tok
)
787 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
) {
789 outflags
&= ~MMAN_spc
;
799 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
) {
800 outflags
&= ~MMAN_spc
;
804 post_percent(meta
, n
);
808 * Print before a section header.
814 if (n
->type
== ROFFT_HEAD
) {
816 print_block(mdoc_man_act(n
->tok
)->prefix
, 0);
819 outflags
&= ~MMAN_spc
;
825 * Print subsequent a section header.
831 if (n
->type
!= ROFFT_HEAD
)
833 outflags
&= ~MMAN_spc
;
837 if (MDOC_Sh
== n
->tok
&& SEC_AUTHORS
== n
->sec
)
838 outflags
&= ~(MMAN_An_split
| MMAN_An_nosplit
);
841 /* See mdoc_term.c, synopsis_pre() for comments. */
843 pre_syn(const struct roff_node
*n
)
846 if (NULL
== n
->prev
|| ! (NODE_SYNPRETTY
& n
->flags
))
849 if (n
->prev
->tok
== n
->tok
&&
857 switch (n
->prev
->tok
) {
866 if (MDOC_Fn
!= n
->tok
&& MDOC_Fo
!= n
->tok
) {
881 switch (n
->norm
->An
.auth
) {
883 outflags
&= ~MMAN_An_nosplit
;
884 outflags
|= MMAN_An_split
;
887 outflags
&= ~MMAN_An_split
;
888 outflags
|= MMAN_An_nosplit
;
891 if (MMAN_An_split
& outflags
)
893 else if (SEC_AUTHORS
== n
->sec
&&
894 ! (MMAN_An_nosplit
& outflags
))
895 outflags
|= MMAN_An_split
;
904 outflags
&= ~MMAN_spc
;
906 outflags
&= ~MMAN_spc
;
914 print_word(n
->child
!= NULL
&& n
->child
->next
== NULL
&&
915 n
->child
->tok
== MDOC_Mt
? "<" : "\\(la");
916 outflags
&= ~MMAN_spc
;
924 outflags
&= ~(MMAN_spc
| MMAN_nl
);
925 print_word(n
->child
!= NULL
&& n
->child
->next
== NULL
&&
926 n
->child
->tok
== MDOC_Mt
? ">" : "\\(ra");
933 outflags
&= ~(MMAN_PP
| MMAN_sp
| MMAN_br
);
935 if (DISP_unfilled
== n
->norm
->Bd
.type
||
936 DISP_literal
== n
->norm
->Bd
.type
)
937 print_line(".nf", 0);
938 if (0 == n
->norm
->Bd
.comp
&& NULL
!= n
->parent
->prev
)
940 print_offs(n
->norm
->Bd
.offs
, 1);
948 /* Close out this display. */
949 print_line(".RE", MMAN_nl
);
950 if (DISP_unfilled
== n
->norm
->Bd
.type
||
951 DISP_literal
== n
->norm
->Bd
.type
)
952 print_line(".fi", MMAN_nl
);
954 /* Maybe we are inside an enclosing list? */
955 if (NULL
!= n
->parent
->next
)
971 switch (n
->norm
->Bf
.font
) {
989 if (n
->type
== ROFFT_BODY
)
1001 outflags
|= MMAN_Bk
;
1013 while ((n
= n
->parent
) != NULL
)
1014 if (n
->tok
== MDOC_Bk
)
1018 outflags
&= ~MMAN_Bk
;
1031 * print_offs() will increase the -offset to account for
1032 * a possible enclosing .It, but any enclosed .It blocks
1033 * just nest and do not add up their indentation.
1035 if (n
->norm
->Bl
.offs
) {
1036 print_offs(n
->norm
->Bl
.offs
, 0);
1037 Bl_stack
[Bl_stack_len
++] = 0;
1040 switch (n
->norm
->Bl
.type
) {
1042 n
->norm
->Bl
.count
= 0;
1050 if (n
->child
!= NULL
) {
1051 print_line(".TS", MMAN_nl
);
1052 for (icol
= 0; icol
< n
->norm
->Bl
.ncols
; icol
++)
1056 outflags
|= MMAN_nl
;
1064 switch (n
->norm
->Bl
.type
) {
1066 if (n
->child
!= NULL
)
1067 print_line(".TE", 0);
1070 n
->norm
->Bl
.count
= 0;
1076 if (n
->norm
->Bl
.offs
) {
1077 print_line(".RE", MMAN_nl
);
1078 assert(Bl_stack_len
);
1080 assert(0 == Bl_stack
[Bl_stack_len
]);
1082 outflags
|= MMAN_PP
| MMAN_nl
;
1083 outflags
&= ~(MMAN_sp
| MMAN_br
);
1086 /* Maybe we are inside an enclosing list? */
1087 if (NULL
!= n
->parent
->next
)
1095 outflags
|= MMAN_br
;
1102 print_offs("6n", 0);
1110 print_line(".RE", MMAN_nl
);
1112 /* Maybe we are inside an enclosing list? */
1113 if (NULL
!= n
->parent
->next
)
1129 if (NULL
== n
->norm
->Es
||
1130 NULL
== n
->norm
->Es
->child
)
1133 print_word(n
->norm
->Es
->child
->string
);
1134 outflags
&= ~MMAN_spc
;
1142 if (NULL
== n
->norm
->Es
||
1143 NULL
== n
->norm
->Es
->child
||
1144 NULL
== n
->norm
->Es
->child
->next
)
1147 outflags
&= ~MMAN_spc
;
1148 print_word(n
->norm
->Es
->child
->next
->string
);
1156 if (n
->end
== ENDBODY_NOT
&&
1157 n
->parent
->head
->child
== NULL
&&
1159 n
->child
->end
!= ENDBODY_NOT
)
1161 else if (n
->end
!= ENDBODY_NOT
? n
->child
!= NULL
:
1162 n
->parent
->head
->child
!= NULL
&& (n
->child
!= NULL
||
1163 (n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
)))
1164 outflags
&= ~(MMAN_spc
| MMAN_nl
);
1173 if (n
->end
!= ENDBODY_NOT
) {
1174 outflags
|= MMAN_spc
;
1178 body
= n
->child
!= NULL
|| n
->parent
->head
->child
!= NULL
;
1179 tail
= n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
;
1182 outflags
&= ~MMAN_spc
;
1183 else if ( ! (body
|| tail
))
1186 outflags
|= MMAN_spc
;
1194 am_Fa
= MDOC_Fa
== n
->tok
;
1201 if (am_Fa
|| NODE_SYNPRETTY
& n
->flags
)
1202 outflags
|= MMAN_nbrword
;
1203 print_node(meta
, n
);
1205 if (NULL
!= (n
= n
->next
))
1215 if (NULL
!= n
->next
&& MDOC_Fa
== n
->next
->tok
)
1233 outflags
|= MMAN_br
;
1242 if (n
->child
!= NULL
)
1243 outflags
&= ~MMAN_spc
;
1252 if (!(n
->child
!= NULL
||
1254 n
->next
->type
== ROFFT_TEXT
||
1255 n
->next
->flags
& NODE_LINE
))
1256 outflags
&= ~MMAN_spc
;
1269 if (NODE_SYNPRETTY
& n
->flags
)
1270 print_block(".HP 4n", MMAN_nl
);
1273 print_node(meta
, n
);
1275 outflags
&= ~MMAN_spc
;
1277 outflags
&= ~MMAN_spc
;
1290 if (NODE_SYNPRETTY
& n
->flags
) {
1292 outflags
|= MMAN_PP
;
1305 if (n
->child
== NULL
)
1307 if (NODE_SYNPRETTY
& n
->flags
)
1308 print_block(".HP 4n", MMAN_nl
);
1312 outflags
&= ~(MMAN_spc
| MMAN_nl
);
1314 outflags
&= ~MMAN_spc
;
1328 if (n
->child
!= NULL
)
1351 print_line(".ft", 0);
1352 print_word(n
->child
->string
);
1353 outflags
|= MMAN_nl
;
1360 if (NODE_SYNPRETTY
& n
->flags
) {
1363 print_word("#include <");
1364 outflags
&= ~MMAN_spc
;
1367 outflags
&= ~MMAN_spc
;
1377 if (NODE_SYNPRETTY
& n
->flags
) {
1378 outflags
&= ~MMAN_spc
;
1381 outflags
|= MMAN_br
;
1384 outflags
&= ~MMAN_spc
;
1392 const struct roff_node
*bln
;
1396 outflags
|= MMAN_PP
| MMAN_nl
;
1397 bln
= n
->parent
->parent
;
1398 if (0 == bln
->norm
->Bl
.comp
||
1399 (NULL
== n
->parent
->prev
&&
1400 NULL
== bln
->parent
->prev
))
1401 outflags
|= MMAN_sp
;
1402 outflags
&= ~MMAN_br
;
1403 switch (bln
->norm
->Bl
.type
) {
1409 if (bln
->norm
->Bl
.type
== LIST_diag
)
1410 print_line(".B \"", 0);
1412 print_line(".BR \\& \"", 0);
1413 outflags
&= ~MMAN_spc
;
1418 print_width(&bln
->norm
->Bl
, NULL
);
1420 outflags
|= MMAN_nl
;
1422 if (LIST_bullet
== bln
->norm
->Bl
.type
)
1423 print_word("\\(bu");
1427 outflags
|= MMAN_nl
;
1430 print_width(&bln
->norm
->Bl
, NULL
);
1432 outflags
|= MMAN_nl
;
1433 print_count(&bln
->norm
->Bl
.count
);
1434 outflags
|= MMAN_nl
;
1437 print_width(&bln
->norm
->Bl
, n
->child
);
1439 outflags
|= MMAN_nl
;
1442 print_width(&bln
->norm
->Bl
, n
->child
);
1444 outflags
&= ~MMAN_spc
;
1456 * This function is called after closing out an indented block.
1457 * If we are inside an enclosing list, restore its indentation.
1464 /* Nothing to do outside a list. */
1465 if (0 == Bl_stack_len
|| 0 == Bl_stack
[Bl_stack_len
- 1])
1468 /* The indentation has already been set up. */
1469 if (Bl_stack_post
[Bl_stack_len
- 1])
1472 /* Restore the indentation of the enclosing list. */
1473 print_line(".RS", MMAN_Bk_susp
);
1474 (void)snprintf(buf
, sizeof(buf
), "%dn",
1475 Bl_stack
[Bl_stack_len
- 1]);
1478 /* Remeber to close out this .RS block later. */
1479 Bl_stack_post
[Bl_stack_len
- 1] = 1;
1485 const struct roff_node
*bln
;
1487 bln
= n
->parent
->parent
;
1491 switch (bln
->norm
->Bl
.type
) {
1493 outflags
&= ~MMAN_spc
;
1497 outflags
|= MMAN_br
;
1504 switch (bln
->norm
->Bl
.type
) {
1511 assert(Bl_stack_len
);
1512 Bl_stack
[--Bl_stack_len
] = 0;
1515 * Our indentation had to be restored
1516 * after a child display or child list.
1517 * Close out that indentation block now.
1519 if (Bl_stack_post
[Bl_stack_len
]) {
1520 print_line(".RE", MMAN_nl
);
1521 Bl_stack_post
[Bl_stack_len
] = 0;
1525 if (NULL
!= n
->next
) {
1527 outflags
&= ~MMAN_spc
;
1543 if (SEC_LIBRARY
== n
->sec
)
1544 outflags
|= MMAN_br
;
1550 const struct roff_node
*link
, *descr
, *punct
;
1552 if ((link
= n
->child
) == NULL
)
1555 /* Find beginning of trailing punctuation. */
1557 while (punct
!= link
&& punct
->flags
& NODE_DELIMC
)
1558 punct
= punct
->prev
;
1559 punct
= punct
->next
;
1562 if ((descr
= link
->next
) != NULL
&& descr
!= punct
) {
1564 while (descr
!= punct
) {
1565 print_word(descr
->string
);
1566 descr
= descr
->next
;
1574 print_word(link
->string
);
1577 /* Trailing punctuation. */
1578 while (punct
!= NULL
) {
1579 print_word(punct
->string
);
1580 punct
= punct
->next
;
1586 pre_onearg(DECL_ARGS
)
1588 outflags
|= MMAN_nl
;
1590 outflags
&= ~MMAN_spc
;
1591 print_word(roff_name
[n
->tok
]);
1592 if (n
->child
!= NULL
)
1593 print_word(n
->child
->string
);
1594 outflags
|= MMAN_nl
;
1595 if (n
->tok
== ROFF_ce
)
1596 for (n
= n
->child
->next
; n
!= NULL
; n
= n
->next
)
1597 print_node(meta
, n
);
1613 if (n
->type
== ROFFT_BLOCK
) {
1614 outflags
|= MMAN_Bk
;
1617 if (n
->type
!= ROFFT_ELEM
&& n
->type
!= ROFFT_HEAD
)
1619 name
= n
->child
== NULL
? NULL
: n
->child
->string
;
1622 if (n
->type
== ROFFT_HEAD
) {
1623 if (NULL
== n
->parent
->prev
)
1624 outflags
|= MMAN_sp
;
1625 print_block(".HP", 0);
1626 printf(" %dn", man_strlen(name
) + 1);
1627 outflags
|= MMAN_nl
;
1639 outflags
&= ~MMAN_Bk
;
1643 if (n
->child
!= NULL
&& n
->child
->string
!= NULL
)
1655 outflags
|= MMAN_spc_force
;
1663 outflags
&= ~MMAN_spc
;
1671 if ( ! (n
->next
== NULL
|| n
->next
->flags
& NODE_LINE
))
1672 outflags
&= ~MMAN_spc
;
1679 if (MDOC_It
!= n
->parent
->tok
)
1680 outflags
|= MMAN_PP
;
1681 outflags
|= MMAN_sp
| MMAN_nl
;
1682 outflags
&= ~MMAN_br
;
1690 if (SEC_SEE_ALSO
== n
->sec
) {
1691 outflags
|= MMAN_PP
| MMAN_sp
| MMAN_nl
;
1692 outflags
&= ~MMAN_br
;
1708 if (NULL
== n
->child
)
1709 outflags
^= MMAN_Sm
;
1710 else if (0 == strcmp("on", n
->child
->string
))
1711 outflags
|= MMAN_Sm
;
1713 outflags
&= ~MMAN_Sm
;
1715 if (MMAN_Sm
& outflags
)
1716 outflags
|= MMAN_spc
;
1724 if (outflags
& MMAN_PP
) {
1725 outflags
&= ~MMAN_PP
;
1726 print_line(".PP", 0);
1728 print_line(".sp", 0);
1729 if (n
->child
!= NULL
)
1730 print_word(n
->child
->string
);
1732 outflags
|= MMAN_nl
;
1746 print_line(".ta", 0);
1747 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
1748 print_word(n
->string
);
1749 outflags
|= MMAN_nl
;
1756 if (NODE_SYNPRETTY
& n
->flags
) {
1775 if (n
->flags
& NODE_SYNPRETTY
&& n
->type
!= ROFFT_BODY
)
1787 print_node(meta
, n
);
1791 outflags
&= ~MMAN_spc
;
1793 print_node(meta
, n
);