1 /* $Id: mdoc_man.c,v 1.132 2019/01/04 03:17:36 schwarze Exp $ */
3 * Copyright (c) 2011-2019 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 void pre_noarg(DECL_ARGS
);
108 static int pre_ns(DECL_ARGS
);
109 static void pre_onearg(DECL_ARGS
);
110 static int pre_pp(DECL_ARGS
);
111 static int pre_rs(DECL_ARGS
);
112 static int pre_sm(DECL_ARGS
);
113 static void pre_sp(DECL_ARGS
);
114 static int pre_sect(DECL_ARGS
);
115 static int pre_sy(DECL_ARGS
);
116 static void pre_syn(const struct roff_node
*);
117 static void pre_ta(DECL_ARGS
);
118 static int pre_vt(DECL_ARGS
);
119 static int pre_xr(DECL_ARGS
);
120 static void print_word(const char *);
121 static void print_line(const char *, int);
122 static void print_block(const char *, int);
123 static void print_offs(const char *, int);
124 static void print_width(const struct mdoc_bl
*,
125 const struct roff_node
*);
126 static void print_count(int *);
127 static void print_node(DECL_ARGS
);
129 static const void_fp roff_man_acts
[ROFF_MAX
] = {
144 static const struct mdoc_man_act mdoc_man_acts
[MDOC_MAX
- MDOC_Dd
] = {
145 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dd */
146 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dt */
147 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Os */
148 { NULL
, pre_sect
, post_sect
, ".SH", NULL
}, /* Sh */
149 { NULL
, pre_sect
, post_sect
, ".SS", NULL
}, /* Ss */
150 { NULL
, pre_pp
, NULL
, NULL
, NULL
}, /* Pp */
151 { cond_body
, pre_dl
, post_dl
, NULL
, NULL
}, /* D1 */
152 { cond_body
, pre_dl
, post_dl
, NULL
, NULL
}, /* Dl */
153 { cond_body
, pre_bd
, post_bd
, NULL
, NULL
}, /* Bd */
154 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ed */
155 { cond_body
, pre_bl
, post_bl
, NULL
, NULL
}, /* Bl */
156 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* El */
157 { NULL
, pre_it
, post_it
, NULL
, NULL
}, /* It */
158 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Ad */
159 { NULL
, pre_an
, NULL
, NULL
, NULL
}, /* An */
160 { NULL
, pre_ap
, NULL
, NULL
, NULL
}, /* Ap */
161 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Ar */
162 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Cd */
163 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Cm */
164 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Dv */
165 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Er */
166 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Ev */
167 { NULL
, pre_ex
, NULL
, NULL
, NULL
}, /* Ex */
168 { NULL
, pre_fa
, post_fa
, NULL
, NULL
}, /* Fa */
169 { NULL
, pre_fd
, post_fd
, NULL
, NULL
}, /* Fd */
170 { NULL
, pre_fl
, post_fl
, NULL
, NULL
}, /* Fl */
171 { NULL
, pre_fn
, post_fn
, NULL
, NULL
}, /* Fn */
172 { NULL
, pre_Ft
, post_font
, NULL
, NULL
}, /* Ft */
173 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Ic */
174 { NULL
, pre_in
, post_in
, NULL
, NULL
}, /* In */
175 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Li */
176 { cond_head
, pre_enc
, NULL
, "\\- ", NULL
}, /* Nd */
177 { NULL
, pre_nm
, post_nm
, NULL
, NULL
}, /* Nm */
178 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Op */
179 { NULL
, pre_abort
, NULL
, NULL
, NULL
}, /* Ot */
180 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Pa */
181 { NULL
, pre_ex
, NULL
, NULL
, NULL
}, /* Rv */
182 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* St */
183 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Va */
184 { NULL
, pre_vt
, post_vt
, NULL
, NULL
}, /* Vt */
185 { NULL
, pre_xr
, NULL
, NULL
, NULL
}, /* Xr */
186 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %A */
187 { NULL
, pre_em
, post_percent
, NULL
, NULL
}, /* %B */
188 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %D */
189 { NULL
, pre_em
, post_percent
, NULL
, NULL
}, /* %I */
190 { NULL
, pre_em
, post_percent
, NULL
, NULL
}, /* %J */
191 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %N */
192 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %O */
193 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %P */
194 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %R */
195 { NULL
, pre__t
, post__t
, NULL
, NULL
}, /* %T */
196 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %V */
197 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ac */
198 { cond_body
, pre_aq
, post_aq
, NULL
, NULL
}, /* Ao */
199 { cond_body
, pre_aq
, post_aq
, NULL
, NULL
}, /* Aq */
200 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* At */
201 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bc */
202 { NULL
, pre_bf
, post_bf
, NULL
, NULL
}, /* Bf */
203 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Bo */
204 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Bq */
205 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Bsx */
206 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Bx */
207 { NULL
, pre_skip
, NULL
, NULL
, NULL
}, /* Db */
208 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dc */
209 { cond_body
, pre_enc
, post_enc
, "\\(lq", "\\(rq" }, /* Do */
210 { cond_body
, pre_enc
, post_enc
, "\\(lq", "\\(rq" }, /* Dq */
211 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ec */
212 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ef */
213 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Em */
214 { cond_body
, pre_eo
, post_eo
, NULL
, NULL
}, /* Eo */
215 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Fx */
216 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Ms */
217 { NULL
, pre_no
, NULL
, NULL
, NULL
}, /* No */
218 { NULL
, pre_ns
, NULL
, NULL
, NULL
}, /* Ns */
219 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Nx */
220 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Ox */
221 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Pc */
222 { NULL
, NULL
, post_pf
, NULL
, NULL
}, /* Pf */
223 { cond_body
, pre_enc
, post_enc
, "(", ")" }, /* Po */
224 { cond_body
, pre_enc
, post_enc
, "(", ")" }, /* Pq */
225 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Qc */
226 { cond_body
, pre_enc
, post_enc
, "\\(oq", "\\(cq" }, /* Ql */
227 { cond_body
, pre_enc
, post_enc
, "\"", "\"" }, /* Qo */
228 { cond_body
, pre_enc
, post_enc
, "\"", "\"" }, /* Qq */
229 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Re */
230 { cond_body
, pre_rs
, NULL
, NULL
, NULL
}, /* Rs */
231 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Sc */
232 { cond_body
, pre_enc
, post_enc
, "\\(oq", "\\(cq" }, /* So */
233 { cond_body
, pre_enc
, post_enc
, "\\(oq", "\\(cq" }, /* Sq */
234 { NULL
, pre_sm
, NULL
, NULL
, NULL
}, /* Sm */
235 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Sx */
236 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Sy */
237 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Tn */
238 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ux */
239 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xc */
240 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xo */
241 { NULL
, pre_fo
, post_fo
, NULL
, NULL
}, /* Fo */
242 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fc */
243 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Oo */
244 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Oc */
245 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Bk */
246 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ek */
247 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bt */
248 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Hf */
249 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Fr */
250 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ud */
251 { NULL
, NULL
, post_lb
, NULL
, NULL
}, /* Lb */
252 { NULL
, pre_abort
, NULL
, NULL
, NULL
}, /* Lp */
253 { NULL
, pre_lk
, NULL
, NULL
, NULL
}, /* Lk */
254 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Mt */
255 { cond_body
, pre_enc
, post_enc
, "{", "}" }, /* Brq */
256 { cond_body
, pre_enc
, post_enc
, "{", "}" }, /* Bro */
257 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Brc */
258 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %C */
259 { NULL
, pre_skip
, NULL
, NULL
, NULL
}, /* Es */
260 { cond_body
, pre_en
, post_en
, NULL
, NULL
}, /* En */
261 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Dx */
262 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %Q */
263 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %U */
264 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ta */
266 static const struct mdoc_man_act
*mdoc_man_act(enum roff_tok
);
269 #define MMAN_spc (1 << 0) /* blank character before next word */
270 #define MMAN_spc_force (1 << 1) /* even before trailing punctuation */
271 #define MMAN_nl (1 << 2) /* break man(7) code line */
272 #define MMAN_br (1 << 3) /* break output line */
273 #define MMAN_sp (1 << 4) /* insert a blank output line */
274 #define MMAN_PP (1 << 5) /* reset indentation etc. */
275 #define MMAN_Sm (1 << 6) /* horizontal spacing mode */
276 #define MMAN_Bk (1 << 7) /* word keep mode */
277 #define MMAN_Bk_susp (1 << 8) /* suspend this (after a macro) */
278 #define MMAN_An_split (1 << 9) /* author mode is "split" */
279 #define MMAN_An_nosplit (1 << 10) /* author mode is "nosplit" */
280 #define MMAN_PD (1 << 11) /* inter-paragraph spacing disabled */
281 #define MMAN_nbrword (1 << 12) /* do not break the next word */
283 #define BL_STACK_MAX 32
285 static int Bl_stack
[BL_STACK_MAX
]; /* offsets [chars] */
286 static int Bl_stack_post
[BL_STACK_MAX
]; /* add final .RE */
287 static int Bl_stack_len
; /* number of nested Bl blocks */
288 static int TPremain
; /* characters before tag is full */
297 static const struct mdoc_man_act
*
298 mdoc_man_act(enum roff_tok tok
)
300 assert(tok
>= MDOC_Dd
&& tok
<= MDOC_MAX
);
301 return mdoc_man_acts
+ (tok
- MDOC_Dd
);
305 man_strlen(const char *cp
)
313 rsz
= strcspn(cp
, "\\");
325 switch (mandoc_escape(&cp
, NULL
, NULL
)) {
329 case ESCAPE_NUMBERED
:
332 case ESCAPE_OVERSTRIKE
:
338 case ESCAPE_SKIPCHAR
:
349 font_push(char newfont
)
352 if (fontqueue
.head
+ fontqueue
.size
<= ++fontqueue
.tail
) {
354 fontqueue
.head
= mandoc_realloc(fontqueue
.head
,
357 *fontqueue
.tail
= newfont
;
361 outflags
&= ~MMAN_spc
;
368 if (fontqueue
.tail
> fontqueue
.head
)
370 outflags
&= ~MMAN_spc
;
373 putchar(*fontqueue
.tail
);
377 print_word(const char *s
)
380 if ((MMAN_PP
| MMAN_sp
| MMAN_br
| MMAN_nl
) & outflags
) {
382 * If we need a newline, print it now and start afresh.
384 if (MMAN_PP
& outflags
) {
385 if (MMAN_sp
& outflags
) {
386 if (MMAN_PD
& outflags
) {
388 outflags
&= ~MMAN_PD
;
390 } else if ( ! (MMAN_PD
& outflags
)) {
395 } else if (MMAN_sp
& outflags
)
397 else if (MMAN_br
& outflags
)
399 else if (MMAN_nl
& outflags
)
401 outflags
&= ~(MMAN_PP
|MMAN_sp
|MMAN_br
|MMAN_nl
|MMAN_spc
);
405 } else if (MMAN_spc
& outflags
) {
407 * If we need a space, only print it if
408 * (1) it is forced by `No' or
409 * (2) what follows is not terminating punctuation or
410 * (3) what follows is longer than one character.
412 if (MMAN_spc_force
& outflags
|| '\0' == s
[0] ||
413 NULL
== strchr(".,:;)]?!", s
[0]) || '\0' != s
[1]) {
414 if (MMAN_Bk
& outflags
&&
415 ! (MMAN_Bk_susp
& outflags
))
424 * Reassign needing space if we're not following opening
427 if (MMAN_Sm
& outflags
&& ('\0' == s
[0] ||
428 (('(' != s
[0] && '[' != s
[0]) || '\0' != s
[1])))
429 outflags
|= MMAN_spc
;
431 outflags
&= ~MMAN_spc
;
432 outflags
&= ~(MMAN_spc_force
| MMAN_Bk_susp
);
446 if (MMAN_nbrword
& outflags
) {
452 putchar((unsigned char)*s
);
458 outflags
&= ~MMAN_nbrword
;
462 print_line(const char *s
, int newflags
)
467 outflags
|= newflags
;
471 print_block(const char *s
, int newflags
)
474 outflags
&= ~MMAN_PP
;
475 if (MMAN_sp
& outflags
) {
476 outflags
&= ~(MMAN_sp
| MMAN_br
);
477 if (MMAN_PD
& outflags
) {
478 print_line(".PD", 0);
479 outflags
&= ~MMAN_PD
;
481 } else if (! (MMAN_PD
& outflags
))
482 print_line(".PD 0", MMAN_PD
);
485 outflags
|= MMAN_Bk_susp
| newflags
;
489 print_offs(const char *v
, int keywords
)
496 print_line(".RS", MMAN_Bk_susp
);
498 /* Convert v into a number (of characters). */
499 if (NULL
== v
|| '\0' == *v
|| (keywords
&& !strcmp(v
, "left")))
501 else if (keywords
&& !strcmp(v
, "indent"))
503 else if (keywords
&& !strcmp(v
, "indent-two"))
506 end
= a2roffsu(v
, &su
, SCALE_EN
);
507 if (end
== NULL
|| *end
!= '\0')
509 else if (SCALE_EN
== su
.unit
)
514 * If we are inside an enclosing list,
515 * there is no easy way to add the two
516 * indentations because they are provided
517 * in terms of different units.
526 * We are inside an enclosing list.
527 * Add the two indentations.
530 sz
+= Bl_stack
[Bl_stack_len
- 1];
532 (void)snprintf(buf
, sizeof(buf
), "%dn", sz
);
538 * Set up the indentation for a list item; used from pre_it().
541 print_width(const struct mdoc_bl
*bl
, const struct roff_node
*child
)
546 int numeric
, remain
, sz
, chsz
;
551 /* Convert the width into a number (of characters). */
552 if (bl
->width
== NULL
)
553 sz
= (bl
->type
== LIST_hang
) ? 6 : 0;
555 end
= a2roffsu(bl
->width
, &su
, SCALE_MAX
);
556 if (end
== NULL
|| *end
!= '\0')
557 sz
= man_strlen(bl
->width
);
558 else if (SCALE_EN
== su
.unit
)
566 /* XXX Rough estimation, might have multiple parts. */
567 if (bl
->type
== LIST_enum
)
568 chsz
= (bl
->count
> 8) + 1;
569 else if (child
!= NULL
&& child
->type
== ROFFT_TEXT
)
570 chsz
= man_strlen(child
->string
);
574 /* Maybe we are inside an enclosing list? */
578 * Save our own indentation,
579 * such that child lists can use it.
581 Bl_stack
[Bl_stack_len
++] = sz
+ 2;
583 /* Set up the current list. */
584 if (chsz
> sz
&& bl
->type
!= LIST_tag
)
585 print_block(".HP", 0);
587 print_block(".TP", 0);
591 (void)snprintf(buf
, sizeof(buf
), "%dn", sz
+ 2);
594 print_word(bl
->width
);
599 print_count(int *count
)
603 (void)snprintf(buf
, sizeof(buf
), "%d.\\&", ++*count
);
608 man_mdoc(void *arg
, const struct roff_meta
*mdoc
)
612 printf(".\\\" Automatically generated from an mdoc input file."
614 for (n
= mdoc
->first
->child
; n
!= NULL
; n
= n
->next
) {
615 if (n
->type
!= ROFFT_COMMENT
)
617 printf(".\\\"%s\n", n
->string
);
620 printf(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"\n",
621 mdoc
->title
, (mdoc
->msec
== NULL
? "" : mdoc
->msec
),
622 mdoc
->date
, mdoc
->os
, mdoc
->vol
);
624 /* Disable hyphenation and if nroff, disable justification. */
625 printf(".nh\n.if n .ad l");
627 outflags
= MMAN_nl
| MMAN_Sm
;
628 if (0 == fontqueue
.size
) {
630 fontqueue
.head
= fontqueue
.tail
= mandoc_malloc(8);
631 *fontqueue
.tail
= 'R';
633 for (; n
!= NULL
; n
= n
->next
)
639 print_node(DECL_ARGS
)
641 const struct mdoc_man_act
*act
;
642 struct roff_node
*sub
;
645 if (n
->flags
& NODE_NOPRT
)
649 * Break the line if we were parsed subsequent the current node.
650 * This makes the page structure be more consistent.
652 if (MMAN_spc
& outflags
&& NODE_LINE
& n
->flags
)
658 n
->flags
&= ~NODE_ENDED
;
660 if (n
->type
== ROFFT_TEXT
) {
662 * Make sure that we don't happen to start with a
663 * control character at the start of a line.
665 if (MMAN_nl
& outflags
&&
666 ('.' == *n
->string
|| '\'' == *n
->string
)) {
669 outflags
&= ~MMAN_spc
;
671 if (n
->flags
& NODE_DELIMC
)
672 outflags
&= ~(MMAN_spc
| MMAN_spc_force
);
673 else if (outflags
& MMAN_Sm
)
674 outflags
|= MMAN_spc_force
;
675 print_word(n
->string
);
676 if (n
->flags
& NODE_DELIMO
)
677 outflags
&= ~(MMAN_spc
| MMAN_spc_force
);
678 else if (outflags
& MMAN_Sm
)
679 outflags
|= MMAN_spc
;
680 } else if (n
->tok
< ROFF_MAX
) {
681 (*roff_man_acts
[n
->tok
])(meta
, n
);
685 * Conditionally run the pre-node action handler for a
688 act
= mdoc_man_act(n
->tok
);
689 cond
= act
->cond
== NULL
|| (*act
->cond
)(meta
, n
);
690 if (cond
&& act
->pre
!= NULL
&&
691 (n
->end
== ENDBODY_NOT
|| n
->child
!= NULL
))
692 do_sub
= (*act
->pre
)(meta
, n
);
696 * Conditionally run all child nodes.
697 * Note that this iterates over children instead of using
698 * recursion. This prevents unnecessary depth in the stack.
701 for (sub
= n
->child
; sub
; sub
= sub
->next
)
702 print_node(meta
, sub
);
705 * Lastly, conditionally run the post-node handler.
707 if (NODE_ENDED
& n
->flags
)
710 if (cond
&& act
->post
)
711 (*act
->post
)(meta
, n
);
713 if (ENDBODY_NOT
!= n
->end
)
714 n
->body
->flags
|= NODE_ENDED
;
721 return n
->type
== ROFFT_HEAD
;
728 return n
->type
== ROFFT_BODY
;
742 prefix
= mdoc_man_act(n
->tok
)->prefix
;
746 outflags
&= ~MMAN_spc
;
755 suffix
= mdoc_man_act(n
->tok
)->suffix
;
758 outflags
&= ~(MMAN_spc
| MMAN_nl
);
765 outflags
|= MMAN_br
| MMAN_nl
;
777 post_percent(DECL_ARGS
)
780 if (mdoc_man_act(n
->tok
)->pre
== pre_em
)
784 if (n
->prev
&& n
->prev
->tok
== n
->tok
&&
785 n
->next
->tok
== n
->tok
)
797 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
) {
799 outflags
&= ~MMAN_spc
;
809 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
) {
810 outflags
&= ~MMAN_spc
;
814 post_percent(meta
, n
);
818 * Print before a section header.
824 if (n
->type
== ROFFT_HEAD
) {
826 print_block(mdoc_man_act(n
->tok
)->prefix
, 0);
829 outflags
&= ~MMAN_spc
;
835 * Print subsequent a section header.
841 if (n
->type
!= ROFFT_HEAD
)
843 outflags
&= ~MMAN_spc
;
847 if (MDOC_Sh
== n
->tok
&& SEC_AUTHORS
== n
->sec
)
848 outflags
&= ~(MMAN_An_split
| MMAN_An_nosplit
);
851 /* See mdoc_term.c, synopsis_pre() for comments. */
853 pre_syn(const struct roff_node
*n
)
856 if (NULL
== n
->prev
|| ! (NODE_SYNPRETTY
& n
->flags
))
859 if (n
->prev
->tok
== n
->tok
&&
867 switch (n
->prev
->tok
) {
876 if (MDOC_Fn
!= n
->tok
&& MDOC_Fo
!= n
->tok
) {
891 switch (n
->norm
->An
.auth
) {
893 outflags
&= ~MMAN_An_nosplit
;
894 outflags
|= MMAN_An_split
;
897 outflags
&= ~MMAN_An_split
;
898 outflags
|= MMAN_An_nosplit
;
901 if (MMAN_An_split
& outflags
)
903 else if (SEC_AUTHORS
== n
->sec
&&
904 ! (MMAN_An_nosplit
& outflags
))
905 outflags
|= MMAN_An_split
;
914 outflags
&= ~MMAN_spc
;
916 outflags
&= ~MMAN_spc
;
924 print_word(n
->child
!= NULL
&& n
->child
->next
== NULL
&&
925 n
->child
->tok
== MDOC_Mt
? "<" : "\\(la");
926 outflags
&= ~MMAN_spc
;
934 outflags
&= ~(MMAN_spc
| MMAN_nl
);
935 print_word(n
->child
!= NULL
&& n
->child
->next
== NULL
&&
936 n
->child
->tok
== MDOC_Mt
? ">" : "\\(ra");
942 outflags
&= ~(MMAN_PP
| MMAN_sp
| MMAN_br
);
944 if (DISP_unfilled
== n
->norm
->Bd
.type
||
945 DISP_literal
== n
->norm
->Bd
.type
)
946 print_line(".nf", 0);
947 if (0 == n
->norm
->Bd
.comp
&& NULL
!= n
->parent
->prev
)
949 print_offs(n
->norm
->Bd
.offs
, 1);
956 enum roff_tok bef
, now
;
958 /* Close out this display. */
959 print_line(".RE", MMAN_nl
);
960 bef
= n
->flags
& NODE_NOFILL
? ROFF_nf
: ROFF_fi
;
962 now
= n
->norm
->Bd
.type
== DISP_unfilled
||
963 n
->norm
->Bd
.type
== DISP_literal
? ROFF_nf
: ROFF_fi
;
964 else if (n
->last
->tok
== ROFF_nf
)
966 else if (n
->last
->tok
== ROFF_fi
)
969 now
= n
->last
->flags
& NODE_NOFILL
? ROFF_nf
: ROFF_fi
;
973 outflags
&= ~MMAN_spc
;
974 print_word(roff_name
[bef
]);
978 /* Maybe we are inside an enclosing list? */
979 if (NULL
!= n
->parent
->next
)
995 switch (n
->norm
->Bf
.font
) {
1013 if (n
->type
== ROFFT_BODY
)
1025 outflags
|= MMAN_Bk
;
1037 while ((n
= n
->parent
) != NULL
)
1038 if (n
->tok
== MDOC_Bk
)
1042 outflags
&= ~MMAN_Bk
;
1055 * print_offs() will increase the -offset to account for
1056 * a possible enclosing .It, but any enclosed .It blocks
1057 * just nest and do not add up their indentation.
1059 if (n
->norm
->Bl
.offs
) {
1060 print_offs(n
->norm
->Bl
.offs
, 0);
1061 Bl_stack
[Bl_stack_len
++] = 0;
1064 switch (n
->norm
->Bl
.type
) {
1066 n
->norm
->Bl
.count
= 0;
1074 if (n
->child
!= NULL
) {
1075 print_line(".TS", MMAN_nl
);
1076 for (icol
= 0; icol
< n
->norm
->Bl
.ncols
; icol
++)
1080 outflags
|= MMAN_nl
;
1088 switch (n
->norm
->Bl
.type
) {
1090 if (n
->child
!= NULL
)
1091 print_line(".TE", 0);
1094 n
->norm
->Bl
.count
= 0;
1100 if (n
->norm
->Bl
.offs
) {
1101 print_line(".RE", MMAN_nl
);
1102 assert(Bl_stack_len
);
1104 assert(0 == Bl_stack
[Bl_stack_len
]);
1106 outflags
|= MMAN_PP
| MMAN_nl
;
1107 outflags
&= ~(MMAN_sp
| MMAN_br
);
1110 /* Maybe we are inside an enclosing list? */
1111 if (NULL
!= n
->parent
->next
)
1119 outflags
|= MMAN_br
;
1126 print_offs("6n", 0);
1134 print_line(".RE", MMAN_nl
);
1136 /* Maybe we are inside an enclosing list? */
1137 if (NULL
!= n
->parent
->next
)
1153 if (NULL
== n
->norm
->Es
||
1154 NULL
== n
->norm
->Es
->child
)
1157 print_word(n
->norm
->Es
->child
->string
);
1158 outflags
&= ~MMAN_spc
;
1166 if (NULL
== n
->norm
->Es
||
1167 NULL
== n
->norm
->Es
->child
||
1168 NULL
== n
->norm
->Es
->child
->next
)
1171 outflags
&= ~MMAN_spc
;
1172 print_word(n
->norm
->Es
->child
->next
->string
);
1180 if (n
->end
== ENDBODY_NOT
&&
1181 n
->parent
->head
->child
== NULL
&&
1183 n
->child
->end
!= ENDBODY_NOT
)
1185 else if (n
->end
!= ENDBODY_NOT
? n
->child
!= NULL
:
1186 n
->parent
->head
->child
!= NULL
&& (n
->child
!= NULL
||
1187 (n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
)))
1188 outflags
&= ~(MMAN_spc
| MMAN_nl
);
1197 if (n
->end
!= ENDBODY_NOT
) {
1198 outflags
|= MMAN_spc
;
1202 body
= n
->child
!= NULL
|| n
->parent
->head
->child
!= NULL
;
1203 tail
= n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
;
1206 outflags
&= ~MMAN_spc
;
1207 else if ( ! (body
|| tail
))
1210 outflags
|= MMAN_spc
;
1218 am_Fa
= MDOC_Fa
== n
->tok
;
1225 if (am_Fa
|| NODE_SYNPRETTY
& n
->flags
)
1226 outflags
|= MMAN_nbrword
;
1227 print_node(meta
, n
);
1229 if (NULL
!= (n
= n
->next
))
1239 if (NULL
!= n
->next
&& MDOC_Fa
== n
->next
->tok
)
1257 outflags
|= MMAN_br
;
1266 if (n
->child
!= NULL
)
1267 outflags
&= ~MMAN_spc
;
1276 if (!(n
->child
!= NULL
||
1278 n
->next
->type
== ROFFT_TEXT
||
1279 n
->next
->flags
& NODE_LINE
))
1280 outflags
&= ~MMAN_spc
;
1293 if (NODE_SYNPRETTY
& n
->flags
)
1294 print_block(".HP 4n", MMAN_nl
);
1297 print_node(meta
, n
);
1299 outflags
&= ~MMAN_spc
;
1301 outflags
&= ~MMAN_spc
;
1314 if (NODE_SYNPRETTY
& n
->flags
) {
1316 outflags
|= MMAN_PP
;
1329 if (n
->child
== NULL
)
1331 if (NODE_SYNPRETTY
& n
->flags
)
1332 print_block(".HP 4n", MMAN_nl
);
1336 outflags
&= ~(MMAN_spc
| MMAN_nl
);
1338 outflags
&= ~MMAN_spc
;
1352 if (n
->child
!= NULL
)
1375 print_line(".ft", 0);
1376 print_word(n
->child
->string
);
1377 outflags
|= MMAN_nl
;
1384 if (NODE_SYNPRETTY
& n
->flags
) {
1387 print_word("#include <");
1388 outflags
&= ~MMAN_spc
;
1391 outflags
&= ~MMAN_spc
;
1401 if (NODE_SYNPRETTY
& n
->flags
) {
1402 outflags
&= ~MMAN_spc
;
1405 outflags
|= MMAN_br
;
1408 outflags
&= ~MMAN_spc
;
1416 const struct roff_node
*bln
;
1420 outflags
|= MMAN_PP
| MMAN_nl
;
1421 bln
= n
->parent
->parent
;
1422 if (0 == bln
->norm
->Bl
.comp
||
1423 (NULL
== n
->parent
->prev
&&
1424 NULL
== bln
->parent
->prev
))
1425 outflags
|= MMAN_sp
;
1426 outflags
&= ~MMAN_br
;
1427 switch (bln
->norm
->Bl
.type
) {
1433 if (bln
->norm
->Bl
.type
== LIST_diag
)
1434 print_line(".B \"", 0);
1436 print_line(".BR \\& \"", 0);
1437 outflags
&= ~MMAN_spc
;
1442 print_width(&bln
->norm
->Bl
, NULL
);
1444 outflags
|= MMAN_nl
;
1446 if (LIST_bullet
== bln
->norm
->Bl
.type
)
1447 print_word("\\(bu");
1451 outflags
|= MMAN_nl
;
1454 print_width(&bln
->norm
->Bl
, NULL
);
1456 outflags
|= MMAN_nl
;
1457 print_count(&bln
->norm
->Bl
.count
);
1458 outflags
|= MMAN_nl
;
1461 print_width(&bln
->norm
->Bl
, n
->child
);
1463 outflags
|= MMAN_nl
;
1466 print_width(&bln
->norm
->Bl
, n
->child
);
1468 outflags
&= ~MMAN_spc
;
1480 * This function is called after closing out an indented block.
1481 * If we are inside an enclosing list, restore its indentation.
1488 /* Nothing to do outside a list. */
1489 if (0 == Bl_stack_len
|| 0 == Bl_stack
[Bl_stack_len
- 1])
1492 /* The indentation has already been set up. */
1493 if (Bl_stack_post
[Bl_stack_len
- 1])
1496 /* Restore the indentation of the enclosing list. */
1497 print_line(".RS", MMAN_Bk_susp
);
1498 (void)snprintf(buf
, sizeof(buf
), "%dn",
1499 Bl_stack
[Bl_stack_len
- 1]);
1502 /* Remeber to close out this .RS block later. */
1503 Bl_stack_post
[Bl_stack_len
- 1] = 1;
1509 const struct roff_node
*bln
;
1511 bln
= n
->parent
->parent
;
1515 switch (bln
->norm
->Bl
.type
) {
1517 outflags
&= ~MMAN_spc
;
1521 outflags
|= MMAN_br
;
1528 switch (bln
->norm
->Bl
.type
) {
1535 assert(Bl_stack_len
);
1536 Bl_stack
[--Bl_stack_len
] = 0;
1539 * Our indentation had to be restored
1540 * after a child display or child list.
1541 * Close out that indentation block now.
1543 if (Bl_stack_post
[Bl_stack_len
]) {
1544 print_line(".RE", MMAN_nl
);
1545 Bl_stack_post
[Bl_stack_len
] = 0;
1549 if (NULL
!= n
->next
) {
1551 outflags
&= ~MMAN_spc
;
1567 if (SEC_LIBRARY
== n
->sec
)
1568 outflags
|= MMAN_br
;
1574 const struct roff_node
*link
, *descr
, *punct
;
1576 if ((link
= n
->child
) == NULL
)
1579 /* Find beginning of trailing punctuation. */
1581 while (punct
!= link
&& punct
->flags
& NODE_DELIMC
)
1582 punct
= punct
->prev
;
1583 punct
= punct
->next
;
1586 if ((descr
= link
->next
) != NULL
&& descr
!= punct
) {
1588 while (descr
!= punct
) {
1589 print_word(descr
->string
);
1590 descr
= descr
->next
;
1598 print_word(link
->string
);
1601 /* Trailing punctuation. */
1602 while (punct
!= NULL
) {
1603 print_word(punct
->string
);
1604 punct
= punct
->next
;
1610 pre_onearg(DECL_ARGS
)
1612 outflags
|= MMAN_nl
;
1614 outflags
&= ~MMAN_spc
;
1615 print_word(roff_name
[n
->tok
]);
1616 if (n
->child
!= NULL
)
1617 print_word(n
->child
->string
);
1618 outflags
|= MMAN_nl
;
1619 if (n
->tok
== ROFF_ce
)
1620 for (n
= n
->child
->next
; n
!= NULL
; n
= n
->next
)
1621 print_node(meta
, n
);
1636 if (n
->type
== ROFFT_BLOCK
) {
1637 outflags
|= MMAN_Bk
;
1640 if (n
->type
!= ROFFT_ELEM
&& n
->type
!= ROFFT_HEAD
)
1642 name
= n
->child
== NULL
? NULL
: n
->child
->string
;
1645 if (n
->type
== ROFFT_HEAD
) {
1646 if (NULL
== n
->parent
->prev
)
1647 outflags
|= MMAN_sp
;
1648 print_block(".HP", 0);
1649 printf(" %dn", man_strlen(name
) + 1);
1650 outflags
|= MMAN_nl
;
1661 outflags
&= ~MMAN_Bk
;
1665 if (n
->child
!= NULL
&& n
->child
->string
!= NULL
)
1676 outflags
|= MMAN_spc_force
;
1681 pre_noarg(DECL_ARGS
)
1683 outflags
|= MMAN_nl
;
1685 outflags
&= ~MMAN_spc
;
1686 print_word(roff_name
[n
->tok
]);
1687 outflags
|= MMAN_nl
;
1693 outflags
&= ~MMAN_spc
;
1701 if ( ! (n
->next
== NULL
|| n
->next
->flags
& NODE_LINE
))
1702 outflags
&= ~MMAN_spc
;
1709 if (MDOC_It
!= n
->parent
->tok
)
1710 outflags
|= MMAN_PP
;
1711 outflags
|= MMAN_sp
| MMAN_nl
;
1712 outflags
&= ~MMAN_br
;
1720 if (SEC_SEE_ALSO
== n
->sec
) {
1721 outflags
|= MMAN_PP
| MMAN_sp
| MMAN_nl
;
1722 outflags
&= ~MMAN_br
;
1738 if (NULL
== n
->child
)
1739 outflags
^= MMAN_Sm
;
1740 else if (0 == strcmp("on", n
->child
->string
))
1741 outflags
|= MMAN_Sm
;
1743 outflags
&= ~MMAN_Sm
;
1745 if (MMAN_Sm
& outflags
)
1746 outflags
|= MMAN_spc
;
1754 if (outflags
& MMAN_PP
) {
1755 outflags
&= ~MMAN_PP
;
1756 print_line(".PP", 0);
1758 print_line(".sp", 0);
1759 if (n
->child
!= NULL
)
1760 print_word(n
->child
->string
);
1762 outflags
|= MMAN_nl
;
1776 print_line(".ta", 0);
1777 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
1778 print_word(n
->string
);
1779 outflags
|= MMAN_nl
;
1786 if (NODE_SYNPRETTY
& n
->flags
) {
1805 if (n
->flags
& NODE_SYNPRETTY
&& n
->type
!= ROFFT_BODY
)
1817 print_node(meta
, n
);
1821 outflags
&= ~MMAN_spc
;
1823 print_node(meta
, n
);