]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_man.c
1 /* $Id: mdoc_man.c,v 1.126 2018/04/11 17:11:13 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_manacts
[ROFF_MAX
] = {
140 static const struct manact __manacts
[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 manact
*const manacts
= __manacts
- MDOC_Dd
;
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 */
294 man_strlen(const char *cp
)
302 rsz
= strcspn(cp
, "\\");
314 switch (mandoc_escape(&cp
, NULL
, NULL
)) {
318 case ESCAPE_NUMBERED
:
320 case ESCAPE_OVERSTRIKE
:
326 case ESCAPE_SKIPCHAR
:
337 font_push(char newfont
)
340 if (fontqueue
.head
+ fontqueue
.size
<= ++fontqueue
.tail
) {
342 fontqueue
.head
= mandoc_realloc(fontqueue
.head
,
345 *fontqueue
.tail
= newfont
;
349 outflags
&= ~MMAN_spc
;
356 if (fontqueue
.tail
> fontqueue
.head
)
358 outflags
&= ~MMAN_spc
;
361 putchar(*fontqueue
.tail
);
365 print_word(const char *s
)
368 if ((MMAN_PP
| MMAN_sp
| MMAN_br
| MMAN_nl
) & outflags
) {
370 * If we need a newline, print it now and start afresh.
372 if (MMAN_PP
& outflags
) {
373 if (MMAN_sp
& outflags
) {
374 if (MMAN_PD
& outflags
) {
376 outflags
&= ~MMAN_PD
;
378 } else if ( ! (MMAN_PD
& outflags
)) {
383 } else if (MMAN_sp
& outflags
)
385 else if (MMAN_br
& outflags
)
387 else if (MMAN_nl
& outflags
)
389 outflags
&= ~(MMAN_PP
|MMAN_sp
|MMAN_br
|MMAN_nl
|MMAN_spc
);
393 } else if (MMAN_spc
& outflags
) {
395 * If we need a space, only print it if
396 * (1) it is forced by `No' or
397 * (2) what follows is not terminating punctuation or
398 * (3) what follows is longer than one character.
400 if (MMAN_spc_force
& outflags
|| '\0' == s
[0] ||
401 NULL
== strchr(".,:;)]?!", s
[0]) || '\0' != s
[1]) {
402 if (MMAN_Bk
& outflags
&&
403 ! (MMAN_Bk_susp
& outflags
))
412 * Reassign needing space if we're not following opening
415 if (MMAN_Sm
& outflags
&& ('\0' == s
[0] ||
416 (('(' != s
[0] && '[' != s
[0]) || '\0' != s
[1])))
417 outflags
|= MMAN_spc
;
419 outflags
&= ~MMAN_spc
;
420 outflags
&= ~(MMAN_spc_force
| MMAN_Bk_susp
);
434 if (MMAN_nbrword
& outflags
) {
440 putchar((unsigned char)*s
);
446 outflags
&= ~MMAN_nbrword
;
450 print_line(const char *s
, int newflags
)
455 outflags
|= newflags
;
459 print_block(const char *s
, int newflags
)
462 outflags
&= ~MMAN_PP
;
463 if (MMAN_sp
& outflags
) {
464 outflags
&= ~(MMAN_sp
| MMAN_br
);
465 if (MMAN_PD
& outflags
) {
466 print_line(".PD", 0);
467 outflags
&= ~MMAN_PD
;
469 } else if (! (MMAN_PD
& outflags
))
470 print_line(".PD 0", MMAN_PD
);
473 outflags
|= MMAN_Bk_susp
| newflags
;
477 print_offs(const char *v
, int keywords
)
484 print_line(".RS", MMAN_Bk_susp
);
486 /* Convert v into a number (of characters). */
487 if (NULL
== v
|| '\0' == *v
|| (keywords
&& !strcmp(v
, "left")))
489 else if (keywords
&& !strcmp(v
, "indent"))
491 else if (keywords
&& !strcmp(v
, "indent-two"))
494 end
= a2roffsu(v
, &su
, SCALE_EN
);
495 if (end
== NULL
|| *end
!= '\0')
497 else if (SCALE_EN
== su
.unit
)
502 * If we are inside an enclosing list,
503 * there is no easy way to add the two
504 * indentations because they are provided
505 * in terms of different units.
514 * We are inside an enclosing list.
515 * Add the two indentations.
518 sz
+= Bl_stack
[Bl_stack_len
- 1];
520 (void)snprintf(buf
, sizeof(buf
), "%dn", sz
);
526 * Set up the indentation for a list item; used from pre_it().
529 print_width(const struct mdoc_bl
*bl
, const struct roff_node
*child
)
534 int numeric
, remain
, sz
, chsz
;
539 /* Convert the width into a number (of characters). */
540 if (bl
->width
== NULL
)
541 sz
= (bl
->type
== LIST_hang
) ? 6 : 0;
543 end
= a2roffsu(bl
->width
, &su
, SCALE_MAX
);
544 if (end
== NULL
|| *end
!= '\0')
545 sz
= man_strlen(bl
->width
);
546 else if (SCALE_EN
== su
.unit
)
554 /* XXX Rough estimation, might have multiple parts. */
555 if (bl
->type
== LIST_enum
)
556 chsz
= (bl
->count
> 8) + 1;
557 else if (child
!= NULL
&& child
->type
== ROFFT_TEXT
)
558 chsz
= man_strlen(child
->string
);
562 /* Maybe we are inside an enclosing list? */
566 * Save our own indentation,
567 * such that child lists can use it.
569 Bl_stack
[Bl_stack_len
++] = sz
+ 2;
571 /* Set up the current list. */
572 if (chsz
> sz
&& bl
->type
!= LIST_tag
)
573 print_block(".HP", 0);
575 print_block(".TP", 0);
579 (void)snprintf(buf
, sizeof(buf
), "%dn", sz
+ 2);
582 print_word(bl
->width
);
587 print_count(int *count
)
591 (void)snprintf(buf
, sizeof(buf
), "%d.\\&", ++*count
);
596 man_man(void *arg
, const struct roff_man
*man
)
600 * Dump the keep buffer.
601 * We're guaranteed by now that this exists (is non-NULL).
602 * Flush stdout afterward, just in case.
604 fputs(mparse_getkeep(man_mparse(man
)), stdout
);
609 man_mdoc(void *arg
, const struct roff_man
*mdoc
)
613 printf(".\\\" Automatically generated from an mdoc input file."
615 for (n
= mdoc
->first
->child
; n
!= NULL
; n
= n
->next
) {
616 if (n
->type
!= ROFFT_COMMENT
)
618 printf(".\\\"%s\n", n
->string
);
621 printf(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"\n",
623 (mdoc
->meta
.msec
== NULL
? "" : mdoc
->meta
.msec
),
624 mdoc
->meta
.date
, mdoc
->meta
.os
, mdoc
->meta
.vol
);
626 /* Disable hyphenation and if nroff, disable justification. */
627 printf(".nh\n.if n .ad l");
629 outflags
= MMAN_nl
| MMAN_Sm
;
630 if (0 == fontqueue
.size
) {
632 fontqueue
.head
= fontqueue
.tail
= mandoc_malloc(8);
633 *fontqueue
.tail
= 'R';
635 for (; n
!= NULL
; n
= n
->next
)
636 print_node(&mdoc
->meta
, n
);
641 print_node(DECL_ARGS
)
643 const struct manact
*act
;
644 struct roff_node
*sub
;
647 if (n
->flags
& NODE_NOPRT
)
651 * Break the line if we were parsed subsequent the current node.
652 * This makes the page structure be more consistent.
654 if (MMAN_spc
& outflags
&& NODE_LINE
& n
->flags
)
660 n
->flags
&= ~NODE_ENDED
;
662 if (n
->type
== ROFFT_TEXT
) {
664 * Make sure that we don't happen to start with a
665 * control character at the start of a line.
667 if (MMAN_nl
& outflags
&&
668 ('.' == *n
->string
|| '\'' == *n
->string
)) {
671 outflags
&= ~MMAN_spc
;
673 if (n
->flags
& NODE_DELIMC
)
674 outflags
&= ~(MMAN_spc
| MMAN_spc_force
);
675 else if (outflags
& MMAN_Sm
)
676 outflags
|= MMAN_spc_force
;
677 print_word(n
->string
);
678 if (n
->flags
& NODE_DELIMO
)
679 outflags
&= ~(MMAN_spc
| MMAN_spc_force
);
680 else if (outflags
& MMAN_Sm
)
681 outflags
|= MMAN_spc
;
682 } else if (n
->tok
< ROFF_MAX
) {
683 (*roff_manacts
[n
->tok
])(meta
, n
);
686 assert(n
->tok
>= MDOC_Dd
&& n
->tok
< MDOC_MAX
);
688 * Conditionally run the pre-node action handler for a
691 act
= manacts
+ n
->tok
;
692 cond
= act
->cond
== NULL
|| (*act
->cond
)(meta
, n
);
693 if (cond
&& act
->pre
!= NULL
&&
694 (n
->end
== ENDBODY_NOT
|| n
->child
!= NULL
))
695 do_sub
= (*act
->pre
)(meta
, n
);
699 * Conditionally run all child nodes.
700 * Note that this iterates over children instead of using
701 * recursion. This prevents unnecessary depth in the stack.
704 for (sub
= n
->child
; sub
; sub
= sub
->next
)
705 print_node(meta
, sub
);
708 * Lastly, conditionally run the post-node handler.
710 if (NODE_ENDED
& n
->flags
)
713 if (cond
&& act
->post
)
714 (*act
->post
)(meta
, n
);
716 if (ENDBODY_NOT
!= n
->end
)
717 n
->body
->flags
|= NODE_ENDED
;
724 return n
->type
== ROFFT_HEAD
;
731 return n
->type
== ROFFT_BODY
;
739 prefix
= manacts
[n
->tok
].prefix
;
743 outflags
&= ~MMAN_spc
;
752 suffix
= manacts
[n
->tok
].suffix
;
755 outflags
&= ~(MMAN_spc
| MMAN_nl
);
762 outflags
|= MMAN_br
| MMAN_nl
;
774 post_percent(DECL_ARGS
)
777 if (pre_em
== manacts
[n
->tok
].pre
)
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(manacts
[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
);