]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_man.c
1 /* $Id: mdoc_man.c,v 1.122 2017/06/14 22:51:25 schwarze Exp $ */
3 * Copyright (c) 2011-2017 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(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"\n",
615 (mdoc
->meta
.msec
== NULL
? "" : mdoc
->meta
.msec
),
616 mdoc
->meta
.date
, mdoc
->meta
.os
, mdoc
->meta
.vol
);
618 /* Disable hyphenation and if nroff, disable justification. */
619 printf(".nh\n.if n .ad l");
621 outflags
= MMAN_nl
| MMAN_Sm
;
622 if (0 == fontqueue
.size
) {
624 fontqueue
.head
= fontqueue
.tail
= mandoc_malloc(8);
625 *fontqueue
.tail
= 'R';
627 for (n
= mdoc
->first
->child
; n
!= NULL
; n
= n
->next
)
628 print_node(&mdoc
->meta
, n
);
633 print_node(DECL_ARGS
)
635 const struct manact
*act
;
636 struct roff_node
*sub
;
639 if (n
->flags
& NODE_NOPRT
)
643 * Break the line if we were parsed subsequent the current node.
644 * This makes the page structure be more consistent.
646 if (MMAN_spc
& outflags
&& NODE_LINE
& n
->flags
)
652 n
->flags
&= ~NODE_ENDED
;
654 if (n
->type
== ROFFT_TEXT
) {
656 * Make sure that we don't happen to start with a
657 * control character at the start of a line.
659 if (MMAN_nl
& outflags
&&
660 ('.' == *n
->string
|| '\'' == *n
->string
)) {
663 outflags
&= ~MMAN_spc
;
665 if (n
->flags
& NODE_DELIMC
)
666 outflags
&= ~(MMAN_spc
| MMAN_spc_force
);
667 else if (outflags
& MMAN_Sm
)
668 outflags
|= MMAN_spc_force
;
669 print_word(n
->string
);
670 if (n
->flags
& NODE_DELIMO
)
671 outflags
&= ~(MMAN_spc
| MMAN_spc_force
);
672 else if (outflags
& MMAN_Sm
)
673 outflags
|= MMAN_spc
;
674 } else if (n
->tok
< ROFF_MAX
) {
675 (*roff_manacts
[n
->tok
])(meta
, n
);
678 assert(n
->tok
>= MDOC_Dd
&& n
->tok
< MDOC_MAX
);
680 * Conditionally run the pre-node action handler for a
683 act
= manacts
+ n
->tok
;
684 cond
= act
->cond
== NULL
|| (*act
->cond
)(meta
, n
);
685 if (cond
&& act
->pre
!= NULL
&&
686 (n
->end
== ENDBODY_NOT
|| n
->child
!= NULL
))
687 do_sub
= (*act
->pre
)(meta
, n
);
691 * Conditionally run all child nodes.
692 * Note that this iterates over children instead of using
693 * recursion. This prevents unnecessary depth in the stack.
696 for (sub
= n
->child
; sub
; sub
= sub
->next
)
697 print_node(meta
, sub
);
700 * Lastly, conditionally run the post-node handler.
702 if (NODE_ENDED
& n
->flags
)
705 if (cond
&& act
->post
)
706 (*act
->post
)(meta
, n
);
708 if (ENDBODY_NOT
!= n
->end
)
709 n
->body
->flags
|= NODE_ENDED
;
716 return n
->type
== ROFFT_HEAD
;
723 return n
->type
== ROFFT_BODY
;
731 prefix
= manacts
[n
->tok
].prefix
;
735 outflags
&= ~MMAN_spc
;
744 suffix
= manacts
[n
->tok
].suffix
;
747 outflags
&= ~(MMAN_spc
| MMAN_nl
);
754 outflags
|= MMAN_br
| MMAN_nl
;
766 post_percent(DECL_ARGS
)
769 if (pre_em
== manacts
[n
->tok
].pre
)
773 if (n
->prev
&& n
->prev
->tok
== n
->tok
&&
774 n
->next
->tok
== n
->tok
)
786 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
) {
788 outflags
&= ~MMAN_spc
;
798 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
) {
799 outflags
&= ~MMAN_spc
;
803 post_percent(meta
, n
);
807 * Print before a section header.
813 if (n
->type
== ROFFT_HEAD
) {
815 print_block(manacts
[n
->tok
].prefix
, 0);
818 outflags
&= ~MMAN_spc
;
824 * Print subsequent a section header.
830 if (n
->type
!= ROFFT_HEAD
)
832 outflags
&= ~MMAN_spc
;
836 if (MDOC_Sh
== n
->tok
&& SEC_AUTHORS
== n
->sec
)
837 outflags
&= ~(MMAN_An_split
| MMAN_An_nosplit
);
840 /* See mdoc_term.c, synopsis_pre() for comments. */
842 pre_syn(const struct roff_node
*n
)
845 if (NULL
== n
->prev
|| ! (NODE_SYNPRETTY
& n
->flags
))
848 if (n
->prev
->tok
== n
->tok
&&
856 switch (n
->prev
->tok
) {
865 if (MDOC_Fn
!= n
->tok
&& MDOC_Fo
!= n
->tok
) {
880 switch (n
->norm
->An
.auth
) {
882 outflags
&= ~MMAN_An_nosplit
;
883 outflags
|= MMAN_An_split
;
886 outflags
&= ~MMAN_An_split
;
887 outflags
|= MMAN_An_nosplit
;
890 if (MMAN_An_split
& outflags
)
892 else if (SEC_AUTHORS
== n
->sec
&&
893 ! (MMAN_An_nosplit
& outflags
))
894 outflags
|= MMAN_An_split
;
903 outflags
&= ~MMAN_spc
;
905 outflags
&= ~MMAN_spc
;
913 print_word(n
->child
!= NULL
&& n
->child
->next
== NULL
&&
914 n
->child
->tok
== MDOC_Mt
? "<" : "\\(la");
915 outflags
&= ~MMAN_spc
;
923 outflags
&= ~(MMAN_spc
| MMAN_nl
);
924 print_word(n
->child
!= NULL
&& n
->child
->next
== NULL
&&
925 n
->child
->tok
== MDOC_Mt
? ">" : "\\(ra");
932 outflags
&= ~(MMAN_PP
| MMAN_sp
| MMAN_br
);
934 if (DISP_unfilled
== n
->norm
->Bd
.type
||
935 DISP_literal
== n
->norm
->Bd
.type
)
936 print_line(".nf", 0);
937 if (0 == n
->norm
->Bd
.comp
&& NULL
!= n
->parent
->prev
)
939 print_offs(n
->norm
->Bd
.offs
, 1);
947 /* Close out this display. */
948 print_line(".RE", MMAN_nl
);
949 if (DISP_unfilled
== n
->norm
->Bd
.type
||
950 DISP_literal
== n
->norm
->Bd
.type
)
951 print_line(".fi", MMAN_nl
);
953 /* Maybe we are inside an enclosing list? */
954 if (NULL
!= n
->parent
->next
)
970 switch (n
->norm
->Bf
.font
) {
988 if (n
->type
== ROFFT_BODY
)
1000 outflags
|= MMAN_Bk
;
1012 while ((n
= n
->parent
) != NULL
)
1013 if (n
->tok
== MDOC_Bk
)
1017 outflags
&= ~MMAN_Bk
;
1030 * print_offs() will increase the -offset to account for
1031 * a possible enclosing .It, but any enclosed .It blocks
1032 * just nest and do not add up their indentation.
1034 if (n
->norm
->Bl
.offs
) {
1035 print_offs(n
->norm
->Bl
.offs
, 0);
1036 Bl_stack
[Bl_stack_len
++] = 0;
1039 switch (n
->norm
->Bl
.type
) {
1041 n
->norm
->Bl
.count
= 0;
1049 if (n
->child
!= NULL
) {
1050 print_line(".TS", MMAN_nl
);
1051 for (icol
= 0; icol
< n
->norm
->Bl
.ncols
; icol
++)
1055 outflags
|= MMAN_nl
;
1063 switch (n
->norm
->Bl
.type
) {
1065 if (n
->child
!= NULL
)
1066 print_line(".TE", 0);
1069 n
->norm
->Bl
.count
= 0;
1075 if (n
->norm
->Bl
.offs
) {
1076 print_line(".RE", MMAN_nl
);
1077 assert(Bl_stack_len
);
1079 assert(0 == Bl_stack
[Bl_stack_len
]);
1081 outflags
|= MMAN_PP
| MMAN_nl
;
1082 outflags
&= ~(MMAN_sp
| MMAN_br
);
1085 /* Maybe we are inside an enclosing list? */
1086 if (NULL
!= n
->parent
->next
)
1094 outflags
|= MMAN_br
;
1101 print_offs("6n", 0);
1109 print_line(".RE", MMAN_nl
);
1111 /* Maybe we are inside an enclosing list? */
1112 if (NULL
!= n
->parent
->next
)
1128 if (NULL
== n
->norm
->Es
||
1129 NULL
== n
->norm
->Es
->child
)
1132 print_word(n
->norm
->Es
->child
->string
);
1133 outflags
&= ~MMAN_spc
;
1141 if (NULL
== n
->norm
->Es
||
1142 NULL
== n
->norm
->Es
->child
||
1143 NULL
== n
->norm
->Es
->child
->next
)
1146 outflags
&= ~MMAN_spc
;
1147 print_word(n
->norm
->Es
->child
->next
->string
);
1155 if (n
->end
== ENDBODY_NOT
&&
1156 n
->parent
->head
->child
== NULL
&&
1158 n
->child
->end
!= ENDBODY_NOT
)
1160 else if (n
->end
!= ENDBODY_NOT
? n
->child
!= NULL
:
1161 n
->parent
->head
->child
!= NULL
&& (n
->child
!= NULL
||
1162 (n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
)))
1163 outflags
&= ~(MMAN_spc
| MMAN_nl
);
1172 if (n
->end
!= ENDBODY_NOT
) {
1173 outflags
|= MMAN_spc
;
1177 body
= n
->child
!= NULL
|| n
->parent
->head
->child
!= NULL
;
1178 tail
= n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
;
1181 outflags
&= ~MMAN_spc
;
1182 else if ( ! (body
|| tail
))
1185 outflags
|= MMAN_spc
;
1193 am_Fa
= MDOC_Fa
== n
->tok
;
1200 if (am_Fa
|| NODE_SYNPRETTY
& n
->flags
)
1201 outflags
|= MMAN_nbrword
;
1202 print_node(meta
, n
);
1204 if (NULL
!= (n
= n
->next
))
1214 if (NULL
!= n
->next
&& MDOC_Fa
== n
->next
->tok
)
1232 outflags
|= MMAN_br
;
1241 if (n
->child
!= NULL
)
1242 outflags
&= ~MMAN_spc
;
1251 if (!(n
->child
!= NULL
||
1253 n
->next
->type
== ROFFT_TEXT
||
1254 n
->next
->flags
& NODE_LINE
))
1255 outflags
&= ~MMAN_spc
;
1268 if (NODE_SYNPRETTY
& n
->flags
)
1269 print_block(".HP 4n", MMAN_nl
);
1272 print_node(meta
, n
);
1274 outflags
&= ~MMAN_spc
;
1276 outflags
&= ~MMAN_spc
;
1289 if (NODE_SYNPRETTY
& n
->flags
) {
1291 outflags
|= MMAN_PP
;
1304 if (n
->child
== NULL
)
1306 if (NODE_SYNPRETTY
& n
->flags
)
1307 print_block(".HP 4n", MMAN_nl
);
1311 outflags
&= ~(MMAN_spc
| MMAN_nl
);
1313 outflags
&= ~MMAN_spc
;
1327 if (n
->child
!= NULL
)
1350 print_line(".ft", 0);
1351 print_word(n
->child
->string
);
1352 outflags
|= MMAN_nl
;
1359 if (NODE_SYNPRETTY
& n
->flags
) {
1362 print_word("#include <");
1363 outflags
&= ~MMAN_spc
;
1366 outflags
&= ~MMAN_spc
;
1376 if (NODE_SYNPRETTY
& n
->flags
) {
1377 outflags
&= ~MMAN_spc
;
1380 outflags
|= MMAN_br
;
1383 outflags
&= ~MMAN_spc
;
1391 const struct roff_node
*bln
;
1395 outflags
|= MMAN_PP
| MMAN_nl
;
1396 bln
= n
->parent
->parent
;
1397 if (0 == bln
->norm
->Bl
.comp
||
1398 (NULL
== n
->parent
->prev
&&
1399 NULL
== bln
->parent
->prev
))
1400 outflags
|= MMAN_sp
;
1401 outflags
&= ~MMAN_br
;
1402 switch (bln
->norm
->Bl
.type
) {
1408 if (bln
->norm
->Bl
.type
== LIST_diag
)
1409 print_line(".B \"", 0);
1411 print_line(".R \"", 0);
1412 outflags
&= ~MMAN_spc
;
1417 print_width(&bln
->norm
->Bl
, NULL
);
1419 outflags
|= MMAN_nl
;
1421 if (LIST_bullet
== bln
->norm
->Bl
.type
)
1422 print_word("\\(bu");
1426 outflags
|= MMAN_nl
;
1429 print_width(&bln
->norm
->Bl
, NULL
);
1431 outflags
|= MMAN_nl
;
1432 print_count(&bln
->norm
->Bl
.count
);
1433 outflags
|= MMAN_nl
;
1436 print_width(&bln
->norm
->Bl
, n
->child
);
1438 outflags
|= MMAN_nl
;
1441 print_width(&bln
->norm
->Bl
, n
->child
);
1443 outflags
&= ~MMAN_spc
;
1455 * This function is called after closing out an indented block.
1456 * If we are inside an enclosing list, restore its indentation.
1463 /* Nothing to do outside a list. */
1464 if (0 == Bl_stack_len
|| 0 == Bl_stack
[Bl_stack_len
- 1])
1467 /* The indentation has already been set up. */
1468 if (Bl_stack_post
[Bl_stack_len
- 1])
1471 /* Restore the indentation of the enclosing list. */
1472 print_line(".RS", MMAN_Bk_susp
);
1473 (void)snprintf(buf
, sizeof(buf
), "%dn",
1474 Bl_stack
[Bl_stack_len
- 1]);
1477 /* Remeber to close out this .RS block later. */
1478 Bl_stack_post
[Bl_stack_len
- 1] = 1;
1484 const struct roff_node
*bln
;
1486 bln
= n
->parent
->parent
;
1490 switch (bln
->norm
->Bl
.type
) {
1492 outflags
&= ~MMAN_spc
;
1496 outflags
|= MMAN_br
;
1503 switch (bln
->norm
->Bl
.type
) {
1510 assert(Bl_stack_len
);
1511 Bl_stack
[--Bl_stack_len
] = 0;
1514 * Our indentation had to be restored
1515 * after a child display or child list.
1516 * Close out that indentation block now.
1518 if (Bl_stack_post
[Bl_stack_len
]) {
1519 print_line(".RE", MMAN_nl
);
1520 Bl_stack_post
[Bl_stack_len
] = 0;
1524 if (NULL
!= n
->next
) {
1526 outflags
&= ~MMAN_spc
;
1542 if (SEC_LIBRARY
== n
->sec
)
1543 outflags
|= MMAN_br
;
1549 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
;
1573 display
= man_strlen(link
->string
) >= 26;
1575 print_line(".RS", MMAN_Bk_susp
);
1577 outflags
|= MMAN_nl
;
1580 print_word(link
->string
);
1583 /* Trailing punctuation. */
1584 while (punct
!= NULL
) {
1585 print_word(punct
->string
);
1586 punct
= punct
->next
;
1589 print_line(".RE", MMAN_nl
);
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
);