]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_man.c
1 /* $Id: mdoc_man.c,v 1.115 2017/05/08 15:34:54 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
] = {
136 static const struct manact __manacts
[MDOC_MAX
- MDOC_Dd
] = {
137 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dd */
138 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dt */
139 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Os */
140 { NULL
, pre_sect
, post_sect
, ".SH", NULL
}, /* Sh */
141 { NULL
, pre_sect
, post_sect
, ".SS", NULL
}, /* Ss */
142 { NULL
, pre_pp
, NULL
, NULL
, NULL
}, /* Pp */
143 { cond_body
, pre_dl
, post_dl
, NULL
, NULL
}, /* D1 */
144 { cond_body
, pre_dl
, post_dl
, NULL
, NULL
}, /* Dl */
145 { cond_body
, pre_bd
, post_bd
, NULL
, NULL
}, /* Bd */
146 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ed */
147 { cond_body
, pre_bl
, post_bl
, NULL
, NULL
}, /* Bl */
148 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* El */
149 { NULL
, pre_it
, post_it
, NULL
, NULL
}, /* It */
150 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Ad */
151 { NULL
, pre_an
, NULL
, NULL
, NULL
}, /* An */
152 { NULL
, pre_ap
, NULL
, NULL
, NULL
}, /* Ap */
153 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Ar */
154 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Cd */
155 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Cm */
156 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Dv */
157 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Er */
158 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Ev */
159 { NULL
, pre_ex
, NULL
, NULL
, NULL
}, /* Ex */
160 { NULL
, pre_fa
, post_fa
, NULL
, NULL
}, /* Fa */
161 { NULL
, pre_fd
, post_fd
, NULL
, NULL
}, /* Fd */
162 { NULL
, pre_fl
, post_fl
, NULL
, NULL
}, /* Fl */
163 { NULL
, pre_fn
, post_fn
, NULL
, NULL
}, /* Fn */
164 { NULL
, pre_Ft
, post_font
, NULL
, NULL
}, /* Ft */
165 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Ic */
166 { NULL
, pre_in
, post_in
, NULL
, NULL
}, /* In */
167 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Li */
168 { cond_head
, pre_enc
, NULL
, "\\- ", NULL
}, /* Nd */
169 { NULL
, pre_nm
, post_nm
, NULL
, NULL
}, /* Nm */
170 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Op */
171 { NULL
, pre_Ft
, post_font
, NULL
, NULL
}, /* Ot */
172 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Pa */
173 { NULL
, pre_ex
, NULL
, NULL
, NULL
}, /* Rv */
174 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* St */
175 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Va */
176 { NULL
, pre_vt
, post_vt
, NULL
, NULL
}, /* Vt */
177 { NULL
, pre_xr
, NULL
, NULL
, NULL
}, /* Xr */
178 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %A */
179 { NULL
, pre_em
, post_percent
, NULL
, NULL
}, /* %B */
180 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %D */
181 { NULL
, pre_em
, post_percent
, NULL
, NULL
}, /* %I */
182 { NULL
, pre_em
, post_percent
, NULL
, NULL
}, /* %J */
183 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %N */
184 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %O */
185 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %P */
186 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %R */
187 { NULL
, pre__t
, post__t
, NULL
, NULL
}, /* %T */
188 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %V */
189 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ac */
190 { cond_body
, pre_aq
, post_aq
, NULL
, NULL
}, /* Ao */
191 { cond_body
, pre_aq
, post_aq
, NULL
, NULL
}, /* Aq */
192 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* At */
193 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bc */
194 { NULL
, pre_bf
, post_bf
, NULL
, NULL
}, /* Bf */
195 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Bo */
196 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Bq */
197 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bsx */
198 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bx */
199 { NULL
, pre_skip
, NULL
, NULL
, NULL
}, /* Db */
200 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dc */
201 { cond_body
, pre_enc
, post_enc
, "\\(Lq", "\\(Rq" }, /* Do */
202 { cond_body
, pre_enc
, post_enc
, "\\(Lq", "\\(Rq" }, /* Dq */
203 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ec */
204 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ef */
205 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Em */
206 { cond_body
, pre_eo
, post_eo
, NULL
, NULL
}, /* Eo */
207 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fx */
208 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Ms */
209 { NULL
, pre_no
, NULL
, NULL
, NULL
}, /* No */
210 { NULL
, pre_ns
, NULL
, NULL
, NULL
}, /* Ns */
211 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Nx */
212 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ox */
213 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Pc */
214 { NULL
, NULL
, post_pf
, NULL
, NULL
}, /* Pf */
215 { cond_body
, pre_enc
, post_enc
, "(", ")" }, /* Po */
216 { cond_body
, pre_enc
, post_enc
, "(", ")" }, /* Pq */
217 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Qc */
218 { cond_body
, pre_enc
, post_enc
, "\\(oq", "\\(cq" }, /* Ql */
219 { cond_body
, pre_enc
, post_enc
, "\"", "\"" }, /* Qo */
220 { cond_body
, pre_enc
, post_enc
, "\"", "\"" }, /* Qq */
221 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Re */
222 { cond_body
, pre_rs
, NULL
, NULL
, NULL
}, /* Rs */
223 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Sc */
224 { cond_body
, pre_enc
, post_enc
, "\\(oq", "\\(cq" }, /* So */
225 { cond_body
, pre_enc
, post_enc
, "\\(oq", "\\(cq" }, /* Sq */
226 { NULL
, pre_sm
, NULL
, NULL
, NULL
}, /* Sm */
227 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Sx */
228 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Sy */
229 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Tn */
230 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ux */
231 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xc */
232 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xo */
233 { NULL
, pre_fo
, post_fo
, NULL
, NULL
}, /* Fo */
234 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fc */
235 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Oo */
236 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Oc */
237 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Bk */
238 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ek */
239 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bt */
240 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Hf */
241 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Fr */
242 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ud */
243 { NULL
, NULL
, post_lb
, NULL
, NULL
}, /* Lb */
244 { NULL
, pre_pp
, NULL
, NULL
, NULL
}, /* Lp */
245 { NULL
, pre_lk
, NULL
, NULL
, NULL
}, /* Lk */
246 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Mt */
247 { cond_body
, pre_enc
, post_enc
, "{", "}" }, /* Brq */
248 { cond_body
, pre_enc
, post_enc
, "{", "}" }, /* Bro */
249 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Brc */
250 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %C */
251 { NULL
, pre_skip
, NULL
, NULL
, NULL
}, /* Es */
252 { cond_body
, pre_en
, post_en
, NULL
, NULL
}, /* En */
253 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dx */
254 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %Q */
255 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %U */
256 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ta */
258 static const struct manact
*const manacts
= __manacts
- MDOC_Dd
;
261 #define MMAN_spc (1 << 0) /* blank character before next word */
262 #define MMAN_spc_force (1 << 1) /* even before trailing punctuation */
263 #define MMAN_nl (1 << 2) /* break man(7) code line */
264 #define MMAN_br (1 << 3) /* break output line */
265 #define MMAN_sp (1 << 4) /* insert a blank output line */
266 #define MMAN_PP (1 << 5) /* reset indentation etc. */
267 #define MMAN_Sm (1 << 6) /* horizontal spacing mode */
268 #define MMAN_Bk (1 << 7) /* word keep mode */
269 #define MMAN_Bk_susp (1 << 8) /* suspend this (after a macro) */
270 #define MMAN_An_split (1 << 9) /* author mode is "split" */
271 #define MMAN_An_nosplit (1 << 10) /* author mode is "nosplit" */
272 #define MMAN_PD (1 << 11) /* inter-paragraph spacing disabled */
273 #define MMAN_nbrword (1 << 12) /* do not break the next word */
275 #define BL_STACK_MAX 32
277 static int Bl_stack
[BL_STACK_MAX
]; /* offsets [chars] */
278 static int Bl_stack_post
[BL_STACK_MAX
]; /* add final .RE */
279 static int Bl_stack_len
; /* number of nested Bl blocks */
280 static int TPremain
; /* characters before tag is full */
290 man_strlen(const char *cp
)
298 rsz
= strcspn(cp
, "\\");
310 switch (mandoc_escape(&cp
, NULL
, NULL
)) {
314 case ESCAPE_NUMBERED
:
316 case ESCAPE_OVERSTRIKE
:
322 case ESCAPE_SKIPCHAR
:
333 font_push(char newfont
)
336 if (fontqueue
.head
+ fontqueue
.size
<= ++fontqueue
.tail
) {
338 fontqueue
.head
= mandoc_realloc(fontqueue
.head
,
341 *fontqueue
.tail
= newfont
;
345 outflags
&= ~MMAN_spc
;
352 if (fontqueue
.tail
> fontqueue
.head
)
354 outflags
&= ~MMAN_spc
;
357 putchar(*fontqueue
.tail
);
361 print_word(const char *s
)
364 if ((MMAN_PP
| MMAN_sp
| MMAN_br
| MMAN_nl
) & outflags
) {
366 * If we need a newline, print it now and start afresh.
368 if (MMAN_PP
& outflags
) {
369 if (MMAN_sp
& outflags
) {
370 if (MMAN_PD
& outflags
) {
372 outflags
&= ~MMAN_PD
;
374 } else if ( ! (MMAN_PD
& outflags
)) {
379 } else if (MMAN_sp
& outflags
)
381 else if (MMAN_br
& outflags
)
383 else if (MMAN_nl
& outflags
)
385 outflags
&= ~(MMAN_PP
|MMAN_sp
|MMAN_br
|MMAN_nl
|MMAN_spc
);
389 } else if (MMAN_spc
& outflags
) {
391 * If we need a space, only print it if
392 * (1) it is forced by `No' or
393 * (2) what follows is not terminating punctuation or
394 * (3) what follows is longer than one character.
396 if (MMAN_spc_force
& outflags
|| '\0' == s
[0] ||
397 NULL
== strchr(".,:;)]?!", s
[0]) || '\0' != s
[1]) {
398 if (MMAN_Bk
& outflags
&&
399 ! (MMAN_Bk_susp
& outflags
))
408 * Reassign needing space if we're not following opening
411 if (MMAN_Sm
& outflags
&& ('\0' == s
[0] ||
412 (('(' != s
[0] && '[' != s
[0]) || '\0' != s
[1])))
413 outflags
|= MMAN_spc
;
415 outflags
&= ~MMAN_spc
;
416 outflags
&= ~(MMAN_spc_force
| MMAN_Bk_susp
);
430 if (MMAN_nbrword
& outflags
) {
436 putchar((unsigned char)*s
);
442 outflags
&= ~MMAN_nbrword
;
446 print_line(const char *s
, int newflags
)
451 outflags
|= newflags
;
455 print_block(const char *s
, int newflags
)
458 outflags
&= ~MMAN_PP
;
459 if (MMAN_sp
& outflags
) {
460 outflags
&= ~(MMAN_sp
| MMAN_br
);
461 if (MMAN_PD
& outflags
) {
462 print_line(".PD", 0);
463 outflags
&= ~MMAN_PD
;
465 } else if (! (MMAN_PD
& outflags
))
466 print_line(".PD 0", MMAN_PD
);
469 outflags
|= MMAN_Bk_susp
| newflags
;
473 print_offs(const char *v
, int keywords
)
479 print_line(".RS", MMAN_Bk_susp
);
481 /* Convert v into a number (of characters). */
482 if (NULL
== v
|| '\0' == *v
|| (keywords
&& !strcmp(v
, "left")))
484 else if (keywords
&& !strcmp(v
, "indent"))
486 else if (keywords
&& !strcmp(v
, "indent-two"))
488 else if (a2roffsu(v
, &su
, SCALE_EN
) > 1) {
489 if (SCALE_EN
== su
.unit
)
494 * If we are inside an enclosing list,
495 * there is no easy way to add the two
496 * indentations because they are provided
497 * in terms of different units.
507 * We are inside an enclosing list.
508 * Add the two indentations.
511 sz
+= Bl_stack
[Bl_stack_len
- 1];
513 (void)snprintf(buf
, sizeof(buf
), "%dn", sz
);
519 * Set up the indentation for a list item; used from pre_it().
522 print_width(const struct mdoc_bl
*bl
, const struct roff_node
*child
)
526 int numeric
, remain
, sz
, chsz
;
531 /* Convert the width into a number (of characters). */
532 if (bl
->width
== NULL
)
533 sz
= (bl
->type
== LIST_hang
) ? 6 : 0;
534 else if (a2roffsu(bl
->width
, &su
, SCALE_MAX
) > 1) {
535 if (SCALE_EN
== su
.unit
)
542 sz
= man_strlen(bl
->width
);
544 /* XXX Rough estimation, might have multiple parts. */
545 if (bl
->type
== LIST_enum
)
546 chsz
= (bl
->count
> 8) + 1;
547 else if (child
!= NULL
&& child
->type
== ROFFT_TEXT
)
548 chsz
= man_strlen(child
->string
);
552 /* Maybe we are inside an enclosing list? */
556 * Save our own indentation,
557 * such that child lists can use it.
559 Bl_stack
[Bl_stack_len
++] = sz
+ 2;
561 /* Set up the current list. */
562 if (chsz
> sz
&& bl
->type
!= LIST_tag
)
563 print_block(".HP", 0);
565 print_block(".TP", 0);
569 (void)snprintf(buf
, sizeof(buf
), "%dn", sz
+ 2);
572 print_word(bl
->width
);
577 print_count(int *count
)
581 (void)snprintf(buf
, sizeof(buf
), "%d.\\&", ++*count
);
586 man_man(void *arg
, const struct roff_man
*man
)
590 * Dump the keep buffer.
591 * We're guaranteed by now that this exists (is non-NULL).
592 * Flush stdout afterward, just in case.
594 fputs(mparse_getkeep(man_mparse(man
)), stdout
);
599 man_mdoc(void *arg
, const struct roff_man
*mdoc
)
603 printf(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"\n",
605 (mdoc
->meta
.msec
== NULL
? "" : mdoc
->meta
.msec
),
606 mdoc
->meta
.date
, mdoc
->meta
.os
, mdoc
->meta
.vol
);
608 /* Disable hyphenation and if nroff, disable justification. */
609 printf(".nh\n.if n .ad l");
611 outflags
= MMAN_nl
| MMAN_Sm
;
612 if (0 == fontqueue
.size
) {
614 fontqueue
.head
= fontqueue
.tail
= mandoc_malloc(8);
615 *fontqueue
.tail
= 'R';
617 for (n
= mdoc
->first
->child
; n
!= NULL
; n
= n
->next
)
618 print_node(&mdoc
->meta
, n
);
623 print_node(DECL_ARGS
)
625 const struct manact
*act
;
626 struct roff_node
*sub
;
629 if (n
->flags
& NODE_NOPRT
)
633 * Break the line if we were parsed subsequent the current node.
634 * This makes the page structure be more consistent.
636 if (MMAN_spc
& outflags
&& NODE_LINE
& n
->flags
)
642 n
->flags
&= ~NODE_ENDED
;
644 if (n
->type
== ROFFT_TEXT
) {
646 * Make sure that we don't happen to start with a
647 * control character at the start of a line.
649 if (MMAN_nl
& outflags
&&
650 ('.' == *n
->string
|| '\'' == *n
->string
)) {
653 outflags
&= ~MMAN_spc
;
655 if (n
->flags
& NODE_DELIMC
)
656 outflags
&= ~(MMAN_spc
| MMAN_spc_force
);
657 else if (outflags
& MMAN_Sm
)
658 outflags
|= MMAN_spc_force
;
659 print_word(n
->string
);
660 if (n
->flags
& NODE_DELIMO
)
661 outflags
&= ~(MMAN_spc
| MMAN_spc_force
);
662 else if (outflags
& MMAN_Sm
)
663 outflags
|= MMAN_spc
;
664 } else if (n
->tok
< ROFF_MAX
) {
665 (*roff_manacts
[n
->tok
])(meta
, n
);
668 assert(n
->tok
>= MDOC_Dd
&& n
->tok
< MDOC_MAX
);
670 * Conditionally run the pre-node action handler for a
673 act
= manacts
+ n
->tok
;
674 cond
= act
->cond
== NULL
|| (*act
->cond
)(meta
, n
);
675 if (cond
&& act
->pre
!= NULL
&&
676 (n
->end
== ENDBODY_NOT
|| n
->child
!= NULL
))
677 do_sub
= (*act
->pre
)(meta
, n
);
681 * Conditionally run all child nodes.
682 * Note that this iterates over children instead of using
683 * recursion. This prevents unnecessary depth in the stack.
686 for (sub
= n
->child
; sub
; sub
= sub
->next
)
687 print_node(meta
, sub
);
690 * Lastly, conditionally run the post-node handler.
692 if (NODE_ENDED
& n
->flags
)
695 if (cond
&& act
->post
)
696 (*act
->post
)(meta
, n
);
698 if (ENDBODY_NOT
!= n
->end
)
699 n
->body
->flags
|= NODE_ENDED
;
706 return n
->type
== ROFFT_HEAD
;
713 return n
->type
== ROFFT_BODY
;
721 prefix
= manacts
[n
->tok
].prefix
;
725 outflags
&= ~MMAN_spc
;
734 suffix
= manacts
[n
->tok
].suffix
;
737 outflags
&= ~(MMAN_spc
| MMAN_nl
);
744 outflags
|= MMAN_br
| MMAN_nl
;
756 post_percent(DECL_ARGS
)
759 if (pre_em
== manacts
[n
->tok
].pre
)
763 if (n
->prev
&& n
->prev
->tok
== n
->tok
&&
764 n
->next
->tok
== n
->tok
)
776 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
) {
778 outflags
&= ~MMAN_spc
;
788 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
) {
789 outflags
&= ~MMAN_spc
;
793 post_percent(meta
, n
);
797 * Print before a section header.
803 if (n
->type
== ROFFT_HEAD
) {
805 print_block(manacts
[n
->tok
].prefix
, 0);
808 outflags
&= ~MMAN_spc
;
814 * Print subsequent a section header.
820 if (n
->type
!= ROFFT_HEAD
)
822 outflags
&= ~MMAN_spc
;
826 if (MDOC_Sh
== n
->tok
&& SEC_AUTHORS
== n
->sec
)
827 outflags
&= ~(MMAN_An_split
| MMAN_An_nosplit
);
830 /* See mdoc_term.c, synopsis_pre() for comments. */
832 pre_syn(const struct roff_node
*n
)
835 if (NULL
== n
->prev
|| ! (NODE_SYNPRETTY
& n
->flags
))
838 if (n
->prev
->tok
== n
->tok
&&
846 switch (n
->prev
->tok
) {
855 if (MDOC_Fn
!= n
->tok
&& MDOC_Fo
!= n
->tok
) {
870 switch (n
->norm
->An
.auth
) {
872 outflags
&= ~MMAN_An_nosplit
;
873 outflags
|= MMAN_An_split
;
876 outflags
&= ~MMAN_An_split
;
877 outflags
|= MMAN_An_nosplit
;
880 if (MMAN_An_split
& outflags
)
882 else if (SEC_AUTHORS
== n
->sec
&&
883 ! (MMAN_An_nosplit
& outflags
))
884 outflags
|= MMAN_An_split
;
893 outflags
&= ~MMAN_spc
;
895 outflags
&= ~MMAN_spc
;
903 print_word(n
->child
!= NULL
&& n
->child
->next
== NULL
&&
904 n
->child
->tok
== MDOC_Mt
? "<" : "\\(la");
905 outflags
&= ~MMAN_spc
;
913 outflags
&= ~(MMAN_spc
| MMAN_nl
);
914 print_word(n
->child
!= NULL
&& n
->child
->next
== NULL
&&
915 n
->child
->tok
== MDOC_Mt
? ">" : "\\(ra");
922 outflags
&= ~(MMAN_PP
| MMAN_sp
| MMAN_br
);
924 if (DISP_unfilled
== n
->norm
->Bd
.type
||
925 DISP_literal
== n
->norm
->Bd
.type
)
926 print_line(".nf", 0);
927 if (0 == n
->norm
->Bd
.comp
&& NULL
!= n
->parent
->prev
)
929 print_offs(n
->norm
->Bd
.offs
, 1);
937 /* Close out this display. */
938 print_line(".RE", MMAN_nl
);
939 if (DISP_unfilled
== n
->norm
->Bd
.type
||
940 DISP_literal
== n
->norm
->Bd
.type
)
941 print_line(".fi", MMAN_nl
);
943 /* Maybe we are inside an enclosing list? */
944 if (NULL
!= n
->parent
->next
)
960 switch (n
->norm
->Bf
.font
) {
978 if (n
->type
== ROFFT_BODY
)
1001 if (n
->type
== ROFFT_BODY
)
1002 outflags
&= ~MMAN_Bk
;
1011 * print_offs() will increase the -offset to account for
1012 * a possible enclosing .It, but any enclosed .It blocks
1013 * just nest and do not add up their indentation.
1015 if (n
->norm
->Bl
.offs
) {
1016 print_offs(n
->norm
->Bl
.offs
, 0);
1017 Bl_stack
[Bl_stack_len
++] = 0;
1020 switch (n
->norm
->Bl
.type
) {
1022 n
->norm
->Bl
.count
= 0;
1030 if (n
->child
!= NULL
) {
1031 print_line(".TS", MMAN_nl
);
1032 for (icol
= 0; icol
< n
->norm
->Bl
.ncols
; icol
++)
1036 outflags
|= MMAN_nl
;
1044 switch (n
->norm
->Bl
.type
) {
1046 if (n
->child
!= NULL
)
1047 print_line(".TE", 0);
1050 n
->norm
->Bl
.count
= 0;
1056 if (n
->norm
->Bl
.offs
) {
1057 print_line(".RE", MMAN_nl
);
1058 assert(Bl_stack_len
);
1060 assert(0 == Bl_stack
[Bl_stack_len
]);
1062 outflags
|= MMAN_PP
| MMAN_nl
;
1063 outflags
&= ~(MMAN_sp
| MMAN_br
);
1066 /* Maybe we are inside an enclosing list? */
1067 if (NULL
!= n
->parent
->next
)
1075 outflags
|= MMAN_br
;
1082 print_offs("6n", 0);
1090 print_line(".RE", MMAN_nl
);
1092 /* Maybe we are inside an enclosing list? */
1093 if (NULL
!= n
->parent
->next
)
1109 if (NULL
== n
->norm
->Es
||
1110 NULL
== n
->norm
->Es
->child
)
1113 print_word(n
->norm
->Es
->child
->string
);
1114 outflags
&= ~MMAN_spc
;
1122 if (NULL
== n
->norm
->Es
||
1123 NULL
== n
->norm
->Es
->child
||
1124 NULL
== n
->norm
->Es
->child
->next
)
1127 outflags
&= ~MMAN_spc
;
1128 print_word(n
->norm
->Es
->child
->next
->string
);
1136 if (n
->end
== ENDBODY_NOT
&&
1137 n
->parent
->head
->child
== NULL
&&
1139 n
->child
->end
!= ENDBODY_NOT
)
1141 else if (n
->end
!= ENDBODY_NOT
? n
->child
!= NULL
:
1142 n
->parent
->head
->child
!= NULL
&& (n
->child
!= NULL
||
1143 (n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
)))
1144 outflags
&= ~(MMAN_spc
| MMAN_nl
);
1153 if (n
->end
!= ENDBODY_NOT
) {
1154 outflags
|= MMAN_spc
;
1158 body
= n
->child
!= NULL
|| n
->parent
->head
->child
!= NULL
;
1159 tail
= n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
;
1162 outflags
&= ~MMAN_spc
;
1163 else if ( ! (body
|| tail
))
1166 outflags
|= MMAN_spc
;
1174 am_Fa
= MDOC_Fa
== n
->tok
;
1181 if (am_Fa
|| NODE_SYNPRETTY
& n
->flags
)
1182 outflags
|= MMAN_nbrword
;
1183 print_node(meta
, n
);
1185 if (NULL
!= (n
= n
->next
))
1195 if (NULL
!= n
->next
&& MDOC_Fa
== n
->next
->tok
)
1213 outflags
|= MMAN_br
;
1222 if (n
->child
!= NULL
)
1223 outflags
&= ~MMAN_spc
;
1232 if (!(n
->child
!= NULL
||
1234 n
->next
->type
== ROFFT_TEXT
||
1235 n
->next
->flags
& NODE_LINE
))
1236 outflags
&= ~MMAN_spc
;
1249 if (NODE_SYNPRETTY
& n
->flags
)
1250 print_block(".HP 4n", MMAN_nl
);
1253 print_node(meta
, n
);
1255 outflags
&= ~MMAN_spc
;
1257 outflags
&= ~MMAN_spc
;
1270 if (NODE_SYNPRETTY
& n
->flags
) {
1272 outflags
|= MMAN_PP
;
1285 if (n
->child
== NULL
)
1287 if (NODE_SYNPRETTY
& n
->flags
)
1288 print_block(".HP 4n", MMAN_nl
);
1292 outflags
&= ~(MMAN_spc
| MMAN_nl
);
1294 outflags
&= ~MMAN_spc
;
1308 if (n
->child
!= NULL
)
1331 print_line(".ft", 0);
1332 print_word(n
->child
->string
);
1333 outflags
|= MMAN_nl
;
1340 if (NODE_SYNPRETTY
& n
->flags
) {
1343 print_word("#include <");
1344 outflags
&= ~MMAN_spc
;
1347 outflags
&= ~MMAN_spc
;
1357 if (NODE_SYNPRETTY
& n
->flags
) {
1358 outflags
&= ~MMAN_spc
;
1361 outflags
|= MMAN_br
;
1364 outflags
&= ~MMAN_spc
;
1372 const struct roff_node
*bln
;
1376 outflags
|= MMAN_PP
| MMAN_nl
;
1377 bln
= n
->parent
->parent
;
1378 if (0 == bln
->norm
->Bl
.comp
||
1379 (NULL
== n
->parent
->prev
&&
1380 NULL
== bln
->parent
->prev
))
1381 outflags
|= MMAN_sp
;
1382 outflags
&= ~MMAN_br
;
1383 switch (bln
->norm
->Bl
.type
) {
1389 if (bln
->norm
->Bl
.type
== LIST_diag
)
1390 print_line(".B \"", 0);
1392 print_line(".R \"", 0);
1393 outflags
&= ~MMAN_spc
;
1398 print_width(&bln
->norm
->Bl
, NULL
);
1400 outflags
|= MMAN_nl
;
1402 if (LIST_bullet
== bln
->norm
->Bl
.type
)
1403 print_word("\\(bu");
1407 outflags
|= MMAN_nl
;
1410 print_width(&bln
->norm
->Bl
, NULL
);
1412 outflags
|= MMAN_nl
;
1413 print_count(&bln
->norm
->Bl
.count
);
1414 outflags
|= MMAN_nl
;
1417 print_width(&bln
->norm
->Bl
, n
->child
);
1419 outflags
|= MMAN_nl
;
1422 print_width(&bln
->norm
->Bl
, n
->child
);
1424 outflags
&= ~MMAN_spc
;
1436 * This function is called after closing out an indented block.
1437 * If we are inside an enclosing list, restore its indentation.
1444 /* Nothing to do outside a list. */
1445 if (0 == Bl_stack_len
|| 0 == Bl_stack
[Bl_stack_len
- 1])
1448 /* The indentation has already been set up. */
1449 if (Bl_stack_post
[Bl_stack_len
- 1])
1452 /* Restore the indentation of the enclosing list. */
1453 print_line(".RS", MMAN_Bk_susp
);
1454 (void)snprintf(buf
, sizeof(buf
), "%dn",
1455 Bl_stack
[Bl_stack_len
- 1]);
1458 /* Remeber to close out this .RS block later. */
1459 Bl_stack_post
[Bl_stack_len
- 1] = 1;
1465 const struct roff_node
*bln
;
1467 bln
= n
->parent
->parent
;
1471 switch (bln
->norm
->Bl
.type
) {
1473 outflags
&= ~MMAN_spc
;
1477 outflags
|= MMAN_br
;
1484 switch (bln
->norm
->Bl
.type
) {
1491 assert(Bl_stack_len
);
1492 Bl_stack
[--Bl_stack_len
] = 0;
1495 * Our indentation had to be restored
1496 * after a child display or child list.
1497 * Close out that indentation block now.
1499 if (Bl_stack_post
[Bl_stack_len
]) {
1500 print_line(".RE", MMAN_nl
);
1501 Bl_stack_post
[Bl_stack_len
] = 0;
1505 if (NULL
!= n
->next
) {
1507 outflags
&= ~MMAN_spc
;
1523 if (SEC_LIBRARY
== n
->sec
)
1524 outflags
|= MMAN_br
;
1530 const struct roff_node
*link
, *descr
;
1533 if ((link
= n
->child
) == NULL
)
1537 if ((descr
= link
->next
) != NULL
&& !(descr
->flags
& NODE_DELIMC
)) {
1539 while (descr
!= NULL
&& !(descr
->flags
& NODE_DELIMC
)) {
1540 print_word(descr
->string
);
1541 descr
= descr
->next
;
1548 display
= man_strlen(link
->string
) >= 26;
1550 print_line(".RS", MMAN_Bk_susp
);
1552 outflags
|= MMAN_nl
;
1555 print_word(link
->string
);
1558 /* Trailing punctuation. */
1559 while (descr
!= NULL
) {
1560 print_word(descr
->string
);
1561 descr
= descr
->next
;
1564 print_line(".RE", MMAN_nl
);
1569 pre_onearg(DECL_ARGS
)
1571 outflags
|= MMAN_nl
;
1573 outflags
&= ~MMAN_spc
;
1574 print_word(roff_name
[n
->tok
]);
1575 if (n
->child
!= NULL
)
1576 print_word(n
->child
->string
);
1577 outflags
|= MMAN_nl
;
1593 if (n
->type
== ROFFT_BLOCK
) {
1594 outflags
|= MMAN_Bk
;
1597 if (n
->type
!= ROFFT_ELEM
&& n
->type
!= ROFFT_HEAD
)
1599 name
= n
->child
== NULL
? NULL
: n
->child
->string
;
1602 if (n
->type
== ROFFT_HEAD
) {
1603 if (NULL
== n
->parent
->prev
)
1604 outflags
|= MMAN_sp
;
1605 print_block(".HP", 0);
1606 printf(" %dn", man_strlen(name
) + 1);
1607 outflags
|= MMAN_nl
;
1619 outflags
&= ~MMAN_Bk
;
1623 if (n
->child
!= NULL
&& n
->child
->string
!= NULL
)
1635 outflags
|= MMAN_spc_force
;
1643 outflags
&= ~MMAN_spc
;
1651 if ( ! (n
->next
== NULL
|| n
->next
->flags
& NODE_LINE
))
1652 outflags
&= ~MMAN_spc
;
1659 if (MDOC_It
!= n
->parent
->tok
)
1660 outflags
|= MMAN_PP
;
1661 outflags
|= MMAN_sp
| MMAN_nl
;
1662 outflags
&= ~MMAN_br
;
1670 if (SEC_SEE_ALSO
== n
->sec
) {
1671 outflags
|= MMAN_PP
| MMAN_sp
| MMAN_nl
;
1672 outflags
&= ~MMAN_br
;
1688 if (NULL
== n
->child
)
1689 outflags
^= MMAN_Sm
;
1690 else if (0 == strcmp("on", n
->child
->string
))
1691 outflags
|= MMAN_Sm
;
1693 outflags
&= ~MMAN_Sm
;
1695 if (MMAN_Sm
& outflags
)
1696 outflags
|= MMAN_spc
;
1704 if (outflags
& MMAN_PP
) {
1705 outflags
&= ~MMAN_PP
;
1706 print_line(".PP", 0);
1708 print_line(".sp", 0);
1709 if (n
->child
!= NULL
)
1710 print_word(n
->child
->string
);
1712 outflags
|= MMAN_nl
;
1726 print_line(".ta", 0);
1727 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
1728 print_word(n
->string
);
1729 outflags
|= MMAN_nl
;
1736 if (NODE_SYNPRETTY
& n
->flags
) {
1755 if (n
->flags
& NODE_SYNPRETTY
&& n
->type
!= ROFFT_BODY
)
1767 print_node(meta
, n
);
1771 outflags
&= ~MMAN_spc
;
1773 print_node(meta
, n
);