]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_man.c
1 /* $Id: mdoc_man.c,v 1.114 2017/05/07 17:31:45 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 void pre_ll(DECL_ARGS
);
105 static int pre_nm(DECL_ARGS
);
106 static int pre_no(DECL_ARGS
);
107 static int pre_ns(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
] = {
135 static const struct manact __manacts
[MDOC_MAX
- MDOC_Dd
] = {
136 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dd */
137 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dt */
138 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Os */
139 { NULL
, pre_sect
, post_sect
, ".SH", NULL
}, /* Sh */
140 { NULL
, pre_sect
, post_sect
, ".SS", NULL
}, /* Ss */
141 { NULL
, pre_pp
, NULL
, NULL
, NULL
}, /* Pp */
142 { cond_body
, pre_dl
, post_dl
, NULL
, NULL
}, /* D1 */
143 { cond_body
, pre_dl
, post_dl
, NULL
, NULL
}, /* Dl */
144 { cond_body
, pre_bd
, post_bd
, NULL
, NULL
}, /* Bd */
145 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ed */
146 { cond_body
, pre_bl
, post_bl
, NULL
, NULL
}, /* Bl */
147 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* El */
148 { NULL
, pre_it
, post_it
, NULL
, NULL
}, /* It */
149 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Ad */
150 { NULL
, pre_an
, NULL
, NULL
, NULL
}, /* An */
151 { NULL
, pre_ap
, NULL
, NULL
, NULL
}, /* Ap */
152 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Ar */
153 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Cd */
154 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Cm */
155 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Dv */
156 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Er */
157 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Ev */
158 { NULL
, pre_ex
, NULL
, NULL
, NULL
}, /* Ex */
159 { NULL
, pre_fa
, post_fa
, NULL
, NULL
}, /* Fa */
160 { NULL
, pre_fd
, post_fd
, NULL
, NULL
}, /* Fd */
161 { NULL
, pre_fl
, post_fl
, NULL
, NULL
}, /* Fl */
162 { NULL
, pre_fn
, post_fn
, NULL
, NULL
}, /* Fn */
163 { NULL
, pre_Ft
, post_font
, NULL
, NULL
}, /* Ft */
164 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Ic */
165 { NULL
, pre_in
, post_in
, NULL
, NULL
}, /* In */
166 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Li */
167 { cond_head
, pre_enc
, NULL
, "\\- ", NULL
}, /* Nd */
168 { NULL
, pre_nm
, post_nm
, NULL
, NULL
}, /* Nm */
169 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Op */
170 { NULL
, pre_Ft
, post_font
, NULL
, NULL
}, /* Ot */
171 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Pa */
172 { NULL
, pre_ex
, NULL
, NULL
, NULL
}, /* Rv */
173 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* St */
174 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Va */
175 { NULL
, pre_vt
, post_vt
, NULL
, NULL
}, /* Vt */
176 { NULL
, pre_xr
, NULL
, NULL
, NULL
}, /* Xr */
177 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %A */
178 { NULL
, pre_em
, post_percent
, NULL
, NULL
}, /* %B */
179 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %D */
180 { NULL
, pre_em
, post_percent
, NULL
, NULL
}, /* %I */
181 { NULL
, pre_em
, post_percent
, NULL
, NULL
}, /* %J */
182 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %N */
183 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %O */
184 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %P */
185 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %R */
186 { NULL
, pre__t
, post__t
, NULL
, NULL
}, /* %T */
187 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %V */
188 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ac */
189 { cond_body
, pre_aq
, post_aq
, NULL
, NULL
}, /* Ao */
190 { cond_body
, pre_aq
, post_aq
, NULL
, NULL
}, /* Aq */
191 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* At */
192 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bc */
193 { NULL
, pre_bf
, post_bf
, NULL
, NULL
}, /* Bf */
194 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Bo */
195 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Bq */
196 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bsx */
197 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bx */
198 { NULL
, pre_skip
, NULL
, NULL
, NULL
}, /* Db */
199 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dc */
200 { cond_body
, pre_enc
, post_enc
, "\\(Lq", "\\(Rq" }, /* Do */
201 { cond_body
, pre_enc
, post_enc
, "\\(Lq", "\\(Rq" }, /* Dq */
202 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ec */
203 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ef */
204 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Em */
205 { cond_body
, pre_eo
, post_eo
, NULL
, NULL
}, /* Eo */
206 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fx */
207 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Ms */
208 { NULL
, pre_no
, NULL
, NULL
, NULL
}, /* No */
209 { NULL
, pre_ns
, NULL
, NULL
, NULL
}, /* Ns */
210 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Nx */
211 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ox */
212 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Pc */
213 { NULL
, NULL
, post_pf
, NULL
, NULL
}, /* Pf */
214 { cond_body
, pre_enc
, post_enc
, "(", ")" }, /* Po */
215 { cond_body
, pre_enc
, post_enc
, "(", ")" }, /* Pq */
216 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Qc */
217 { cond_body
, pre_enc
, post_enc
, "\\(oq", "\\(cq" }, /* Ql */
218 { cond_body
, pre_enc
, post_enc
, "\"", "\"" }, /* Qo */
219 { cond_body
, pre_enc
, post_enc
, "\"", "\"" }, /* Qq */
220 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Re */
221 { cond_body
, pre_rs
, NULL
, NULL
, NULL
}, /* Rs */
222 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Sc */
223 { cond_body
, pre_enc
, post_enc
, "\\(oq", "\\(cq" }, /* So */
224 { cond_body
, pre_enc
, post_enc
, "\\(oq", "\\(cq" }, /* Sq */
225 { NULL
, pre_sm
, NULL
, NULL
, NULL
}, /* Sm */
226 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Sx */
227 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Sy */
228 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Tn */
229 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ux */
230 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xc */
231 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xo */
232 { NULL
, pre_fo
, post_fo
, NULL
, NULL
}, /* Fo */
233 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fc */
234 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Oo */
235 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Oc */
236 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Bk */
237 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ek */
238 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bt */
239 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Hf */
240 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Fr */
241 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ud */
242 { NULL
, NULL
, post_lb
, NULL
, NULL
}, /* Lb */
243 { NULL
, pre_pp
, NULL
, NULL
, NULL
}, /* Lp */
244 { NULL
, pre_lk
, NULL
, NULL
, NULL
}, /* Lk */
245 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Mt */
246 { cond_body
, pre_enc
, post_enc
, "{", "}" }, /* Brq */
247 { cond_body
, pre_enc
, post_enc
, "{", "}" }, /* Bro */
248 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Brc */
249 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %C */
250 { NULL
, pre_skip
, NULL
, NULL
, NULL
}, /* Es */
251 { cond_body
, pre_en
, post_en
, NULL
, NULL
}, /* En */
252 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dx */
253 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %Q */
254 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %U */
255 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ta */
257 static const struct manact
*const manacts
= __manacts
- MDOC_Dd
;
260 #define MMAN_spc (1 << 0) /* blank character before next word */
261 #define MMAN_spc_force (1 << 1) /* even before trailing punctuation */
262 #define MMAN_nl (1 << 2) /* break man(7) code line */
263 #define MMAN_br (1 << 3) /* break output line */
264 #define MMAN_sp (1 << 4) /* insert a blank output line */
265 #define MMAN_PP (1 << 5) /* reset indentation etc. */
266 #define MMAN_Sm (1 << 6) /* horizontal spacing mode */
267 #define MMAN_Bk (1 << 7) /* word keep mode */
268 #define MMAN_Bk_susp (1 << 8) /* suspend this (after a macro) */
269 #define MMAN_An_split (1 << 9) /* author mode is "split" */
270 #define MMAN_An_nosplit (1 << 10) /* author mode is "nosplit" */
271 #define MMAN_PD (1 << 11) /* inter-paragraph spacing disabled */
272 #define MMAN_nbrword (1 << 12) /* do not break the next word */
274 #define BL_STACK_MAX 32
276 static int Bl_stack
[BL_STACK_MAX
]; /* offsets [chars] */
277 static int Bl_stack_post
[BL_STACK_MAX
]; /* add final .RE */
278 static int Bl_stack_len
; /* number of nested Bl blocks */
279 static int TPremain
; /* characters before tag is full */
289 man_strlen(const char *cp
)
297 rsz
= strcspn(cp
, "\\");
309 switch (mandoc_escape(&cp
, NULL
, NULL
)) {
313 case ESCAPE_NUMBERED
:
315 case ESCAPE_OVERSTRIKE
:
321 case ESCAPE_SKIPCHAR
:
332 font_push(char newfont
)
335 if (fontqueue
.head
+ fontqueue
.size
<= ++fontqueue
.tail
) {
337 fontqueue
.head
= mandoc_realloc(fontqueue
.head
,
340 *fontqueue
.tail
= newfont
;
344 outflags
&= ~MMAN_spc
;
351 if (fontqueue
.tail
> fontqueue
.head
)
353 outflags
&= ~MMAN_spc
;
356 putchar(*fontqueue
.tail
);
360 print_word(const char *s
)
363 if ((MMAN_PP
| MMAN_sp
| MMAN_br
| MMAN_nl
) & outflags
) {
365 * If we need a newline, print it now and start afresh.
367 if (MMAN_PP
& outflags
) {
368 if (MMAN_sp
& outflags
) {
369 if (MMAN_PD
& outflags
) {
371 outflags
&= ~MMAN_PD
;
373 } else if ( ! (MMAN_PD
& outflags
)) {
378 } else if (MMAN_sp
& outflags
)
380 else if (MMAN_br
& outflags
)
382 else if (MMAN_nl
& outflags
)
384 outflags
&= ~(MMAN_PP
|MMAN_sp
|MMAN_br
|MMAN_nl
|MMAN_spc
);
388 } else if (MMAN_spc
& outflags
) {
390 * If we need a space, only print it if
391 * (1) it is forced by `No' or
392 * (2) what follows is not terminating punctuation or
393 * (3) what follows is longer than one character.
395 if (MMAN_spc_force
& outflags
|| '\0' == s
[0] ||
396 NULL
== strchr(".,:;)]?!", s
[0]) || '\0' != s
[1]) {
397 if (MMAN_Bk
& outflags
&&
398 ! (MMAN_Bk_susp
& outflags
))
407 * Reassign needing space if we're not following opening
410 if (MMAN_Sm
& outflags
&& ('\0' == s
[0] ||
411 (('(' != s
[0] && '[' != s
[0]) || '\0' != s
[1])))
412 outflags
|= MMAN_spc
;
414 outflags
&= ~MMAN_spc
;
415 outflags
&= ~(MMAN_spc_force
| MMAN_Bk_susp
);
429 if (MMAN_nbrword
& outflags
) {
435 putchar((unsigned char)*s
);
441 outflags
&= ~MMAN_nbrword
;
445 print_line(const char *s
, int newflags
)
450 outflags
|= newflags
;
454 print_block(const char *s
, int newflags
)
457 outflags
&= ~MMAN_PP
;
458 if (MMAN_sp
& outflags
) {
459 outflags
&= ~(MMAN_sp
| MMAN_br
);
460 if (MMAN_PD
& outflags
) {
461 print_line(".PD", 0);
462 outflags
&= ~MMAN_PD
;
464 } else if (! (MMAN_PD
& outflags
))
465 print_line(".PD 0", MMAN_PD
);
468 outflags
|= MMAN_Bk_susp
| newflags
;
472 print_offs(const char *v
, int keywords
)
478 print_line(".RS", MMAN_Bk_susp
);
480 /* Convert v into a number (of characters). */
481 if (NULL
== v
|| '\0' == *v
|| (keywords
&& !strcmp(v
, "left")))
483 else if (keywords
&& !strcmp(v
, "indent"))
485 else if (keywords
&& !strcmp(v
, "indent-two"))
487 else if (a2roffsu(v
, &su
, SCALE_EN
) > 1) {
488 if (SCALE_EN
== su
.unit
)
493 * If we are inside an enclosing list,
494 * there is no easy way to add the two
495 * indentations because they are provided
496 * in terms of different units.
506 * We are inside an enclosing list.
507 * Add the two indentations.
510 sz
+= Bl_stack
[Bl_stack_len
- 1];
512 (void)snprintf(buf
, sizeof(buf
), "%dn", sz
);
518 * Set up the indentation for a list item; used from pre_it().
521 print_width(const struct mdoc_bl
*bl
, const struct roff_node
*child
)
525 int numeric
, remain
, sz
, chsz
;
530 /* Convert the width into a number (of characters). */
531 if (bl
->width
== NULL
)
532 sz
= (bl
->type
== LIST_hang
) ? 6 : 0;
533 else if (a2roffsu(bl
->width
, &su
, SCALE_MAX
) > 1) {
534 if (SCALE_EN
== su
.unit
)
541 sz
= man_strlen(bl
->width
);
543 /* XXX Rough estimation, might have multiple parts. */
544 if (bl
->type
== LIST_enum
)
545 chsz
= (bl
->count
> 8) + 1;
546 else if (child
!= NULL
&& child
->type
== ROFFT_TEXT
)
547 chsz
= man_strlen(child
->string
);
551 /* Maybe we are inside an enclosing list? */
555 * Save our own indentation,
556 * such that child lists can use it.
558 Bl_stack
[Bl_stack_len
++] = sz
+ 2;
560 /* Set up the current list. */
561 if (chsz
> sz
&& bl
->type
!= LIST_tag
)
562 print_block(".HP", 0);
564 print_block(".TP", 0);
568 (void)snprintf(buf
, sizeof(buf
), "%dn", sz
+ 2);
571 print_word(bl
->width
);
576 print_count(int *count
)
580 (void)snprintf(buf
, sizeof(buf
), "%d.\\&", ++*count
);
585 man_man(void *arg
, const struct roff_man
*man
)
589 * Dump the keep buffer.
590 * We're guaranteed by now that this exists (is non-NULL).
591 * Flush stdout afterward, just in case.
593 fputs(mparse_getkeep(man_mparse(man
)), stdout
);
598 man_mdoc(void *arg
, const struct roff_man
*mdoc
)
602 printf(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"\n",
604 (mdoc
->meta
.msec
== NULL
? "" : mdoc
->meta
.msec
),
605 mdoc
->meta
.date
, mdoc
->meta
.os
, mdoc
->meta
.vol
);
607 /* Disable hyphenation and if nroff, disable justification. */
608 printf(".nh\n.if n .ad l");
610 outflags
= MMAN_nl
| MMAN_Sm
;
611 if (0 == fontqueue
.size
) {
613 fontqueue
.head
= fontqueue
.tail
= mandoc_malloc(8);
614 *fontqueue
.tail
= 'R';
616 for (n
= mdoc
->first
->child
; n
!= NULL
; n
= n
->next
)
617 print_node(&mdoc
->meta
, n
);
622 print_node(DECL_ARGS
)
624 const struct manact
*act
;
625 struct roff_node
*sub
;
628 if (n
->flags
& NODE_NOPRT
)
632 * Break the line if we were parsed subsequent the current node.
633 * This makes the page structure be more consistent.
635 if (MMAN_spc
& outflags
&& NODE_LINE
& n
->flags
)
641 n
->flags
&= ~NODE_ENDED
;
643 if (n
->type
== ROFFT_TEXT
) {
645 * Make sure that we don't happen to start with a
646 * control character at the start of a line.
648 if (MMAN_nl
& outflags
&&
649 ('.' == *n
->string
|| '\'' == *n
->string
)) {
652 outflags
&= ~MMAN_spc
;
654 if (n
->flags
& NODE_DELIMC
)
655 outflags
&= ~(MMAN_spc
| MMAN_spc_force
);
656 else if (outflags
& MMAN_Sm
)
657 outflags
|= MMAN_spc_force
;
658 print_word(n
->string
);
659 if (n
->flags
& NODE_DELIMO
)
660 outflags
&= ~(MMAN_spc
| MMAN_spc_force
);
661 else if (outflags
& MMAN_Sm
)
662 outflags
|= MMAN_spc
;
663 } else if (n
->tok
< ROFF_MAX
) {
664 (*roff_manacts
[n
->tok
])(meta
, n
);
667 assert(n
->tok
>= MDOC_Dd
&& n
->tok
< MDOC_MAX
);
669 * Conditionally run the pre-node action handler for a
672 act
= manacts
+ n
->tok
;
673 cond
= act
->cond
== NULL
|| (*act
->cond
)(meta
, n
);
674 if (cond
&& act
->pre
!= NULL
&&
675 (n
->end
== ENDBODY_NOT
|| n
->child
!= NULL
))
676 do_sub
= (*act
->pre
)(meta
, n
);
680 * Conditionally run all child nodes.
681 * Note that this iterates over children instead of using
682 * recursion. This prevents unnecessary depth in the stack.
685 for (sub
= n
->child
; sub
; sub
= sub
->next
)
686 print_node(meta
, sub
);
689 * Lastly, conditionally run the post-node handler.
691 if (NODE_ENDED
& n
->flags
)
694 if (cond
&& act
->post
)
695 (*act
->post
)(meta
, n
);
697 if (ENDBODY_NOT
!= n
->end
)
698 n
->body
->flags
|= NODE_ENDED
;
705 return n
->type
== ROFFT_HEAD
;
712 return n
->type
== ROFFT_BODY
;
720 prefix
= manacts
[n
->tok
].prefix
;
724 outflags
&= ~MMAN_spc
;
733 suffix
= manacts
[n
->tok
].suffix
;
736 outflags
&= ~(MMAN_spc
| MMAN_nl
);
743 outflags
|= MMAN_br
| MMAN_nl
;
755 post_percent(DECL_ARGS
)
758 if (pre_em
== manacts
[n
->tok
].pre
)
762 if (n
->prev
&& n
->prev
->tok
== n
->tok
&&
763 n
->next
->tok
== n
->tok
)
775 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
) {
777 outflags
&= ~MMAN_spc
;
787 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
) {
788 outflags
&= ~MMAN_spc
;
792 post_percent(meta
, n
);
796 * Print before a section header.
802 if (n
->type
== ROFFT_HEAD
) {
804 print_block(manacts
[n
->tok
].prefix
, 0);
807 outflags
&= ~MMAN_spc
;
813 * Print subsequent a section header.
819 if (n
->type
!= ROFFT_HEAD
)
821 outflags
&= ~MMAN_spc
;
825 if (MDOC_Sh
== n
->tok
&& SEC_AUTHORS
== n
->sec
)
826 outflags
&= ~(MMAN_An_split
| MMAN_An_nosplit
);
829 /* See mdoc_term.c, synopsis_pre() for comments. */
831 pre_syn(const struct roff_node
*n
)
834 if (NULL
== n
->prev
|| ! (NODE_SYNPRETTY
& n
->flags
))
837 if (n
->prev
->tok
== n
->tok
&&
845 switch (n
->prev
->tok
) {
854 if (MDOC_Fn
!= n
->tok
&& MDOC_Fo
!= n
->tok
) {
869 switch (n
->norm
->An
.auth
) {
871 outflags
&= ~MMAN_An_nosplit
;
872 outflags
|= MMAN_An_split
;
875 outflags
&= ~MMAN_An_split
;
876 outflags
|= MMAN_An_nosplit
;
879 if (MMAN_An_split
& outflags
)
881 else if (SEC_AUTHORS
== n
->sec
&&
882 ! (MMAN_An_nosplit
& outflags
))
883 outflags
|= MMAN_An_split
;
892 outflags
&= ~MMAN_spc
;
894 outflags
&= ~MMAN_spc
;
902 print_word(n
->child
!= NULL
&& n
->child
->next
== NULL
&&
903 n
->child
->tok
== MDOC_Mt
? "<" : "\\(la");
904 outflags
&= ~MMAN_spc
;
912 outflags
&= ~(MMAN_spc
| MMAN_nl
);
913 print_word(n
->child
!= NULL
&& n
->child
->next
== NULL
&&
914 n
->child
->tok
== MDOC_Mt
? ">" : "\\(ra");
921 outflags
&= ~(MMAN_PP
| MMAN_sp
| MMAN_br
);
923 if (DISP_unfilled
== n
->norm
->Bd
.type
||
924 DISP_literal
== n
->norm
->Bd
.type
)
925 print_line(".nf", 0);
926 if (0 == n
->norm
->Bd
.comp
&& NULL
!= n
->parent
->prev
)
928 print_offs(n
->norm
->Bd
.offs
, 1);
936 /* Close out this display. */
937 print_line(".RE", MMAN_nl
);
938 if (DISP_unfilled
== n
->norm
->Bd
.type
||
939 DISP_literal
== n
->norm
->Bd
.type
)
940 print_line(".fi", MMAN_nl
);
942 /* Maybe we are inside an enclosing list? */
943 if (NULL
!= n
->parent
->next
)
959 switch (n
->norm
->Bf
.font
) {
977 if (n
->type
== ROFFT_BODY
)
1000 if (n
->type
== ROFFT_BODY
)
1001 outflags
&= ~MMAN_Bk
;
1010 * print_offs() will increase the -offset to account for
1011 * a possible enclosing .It, but any enclosed .It blocks
1012 * just nest and do not add up their indentation.
1014 if (n
->norm
->Bl
.offs
) {
1015 print_offs(n
->norm
->Bl
.offs
, 0);
1016 Bl_stack
[Bl_stack_len
++] = 0;
1019 switch (n
->norm
->Bl
.type
) {
1021 n
->norm
->Bl
.count
= 0;
1029 if (n
->child
!= NULL
) {
1030 print_line(".TS", MMAN_nl
);
1031 for (icol
= 0; icol
< n
->norm
->Bl
.ncols
; icol
++)
1035 outflags
|= MMAN_nl
;
1043 switch (n
->norm
->Bl
.type
) {
1045 if (n
->child
!= NULL
)
1046 print_line(".TE", 0);
1049 n
->norm
->Bl
.count
= 0;
1055 if (n
->norm
->Bl
.offs
) {
1056 print_line(".RE", MMAN_nl
);
1057 assert(Bl_stack_len
);
1059 assert(0 == Bl_stack
[Bl_stack_len
]);
1061 outflags
|= MMAN_PP
| MMAN_nl
;
1062 outflags
&= ~(MMAN_sp
| MMAN_br
);
1065 /* Maybe we are inside an enclosing list? */
1066 if (NULL
!= n
->parent
->next
)
1074 outflags
|= MMAN_br
;
1081 print_offs("6n", 0);
1089 print_line(".RE", MMAN_nl
);
1091 /* Maybe we are inside an enclosing list? */
1092 if (NULL
!= n
->parent
->next
)
1108 if (NULL
== n
->norm
->Es
||
1109 NULL
== n
->norm
->Es
->child
)
1112 print_word(n
->norm
->Es
->child
->string
);
1113 outflags
&= ~MMAN_spc
;
1121 if (NULL
== n
->norm
->Es
||
1122 NULL
== n
->norm
->Es
->child
||
1123 NULL
== n
->norm
->Es
->child
->next
)
1126 outflags
&= ~MMAN_spc
;
1127 print_word(n
->norm
->Es
->child
->next
->string
);
1135 if (n
->end
== ENDBODY_NOT
&&
1136 n
->parent
->head
->child
== NULL
&&
1138 n
->child
->end
!= ENDBODY_NOT
)
1140 else if (n
->end
!= ENDBODY_NOT
? n
->child
!= NULL
:
1141 n
->parent
->head
->child
!= NULL
&& (n
->child
!= NULL
||
1142 (n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
)))
1143 outflags
&= ~(MMAN_spc
| MMAN_nl
);
1152 if (n
->end
!= ENDBODY_NOT
) {
1153 outflags
|= MMAN_spc
;
1157 body
= n
->child
!= NULL
|| n
->parent
->head
->child
!= NULL
;
1158 tail
= n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
;
1161 outflags
&= ~MMAN_spc
;
1162 else if ( ! (body
|| tail
))
1165 outflags
|= MMAN_spc
;
1173 am_Fa
= MDOC_Fa
== n
->tok
;
1180 if (am_Fa
|| NODE_SYNPRETTY
& n
->flags
)
1181 outflags
|= MMAN_nbrword
;
1182 print_node(meta
, n
);
1184 if (NULL
!= (n
= n
->next
))
1194 if (NULL
!= n
->next
&& MDOC_Fa
== n
->next
->tok
)
1212 outflags
|= MMAN_br
;
1221 if (n
->child
!= NULL
)
1222 outflags
&= ~MMAN_spc
;
1231 if (!(n
->child
!= NULL
||
1233 n
->next
->type
== ROFFT_TEXT
||
1234 n
->next
->flags
& NODE_LINE
))
1235 outflags
&= ~MMAN_spc
;
1248 if (NODE_SYNPRETTY
& n
->flags
)
1249 print_block(".HP 4n", MMAN_nl
);
1252 print_node(meta
, n
);
1254 outflags
&= ~MMAN_spc
;
1256 outflags
&= ~MMAN_spc
;
1269 if (NODE_SYNPRETTY
& n
->flags
) {
1271 outflags
|= MMAN_PP
;
1284 if (n
->child
== NULL
)
1286 if (NODE_SYNPRETTY
& n
->flags
)
1287 print_block(".HP 4n", MMAN_nl
);
1291 outflags
&= ~(MMAN_spc
| MMAN_nl
);
1293 outflags
&= ~MMAN_spc
;
1307 if (n
->child
!= NULL
)
1330 print_line(".ft", 0);
1331 print_word(n
->child
->string
);
1332 outflags
|= MMAN_nl
;
1339 if (NODE_SYNPRETTY
& n
->flags
) {
1342 print_word("#include <");
1343 outflags
&= ~MMAN_spc
;
1346 outflags
&= ~MMAN_spc
;
1356 if (NODE_SYNPRETTY
& n
->flags
) {
1357 outflags
&= ~MMAN_spc
;
1360 outflags
|= MMAN_br
;
1363 outflags
&= ~MMAN_spc
;
1371 const struct roff_node
*bln
;
1375 outflags
|= MMAN_PP
| MMAN_nl
;
1376 bln
= n
->parent
->parent
;
1377 if (0 == bln
->norm
->Bl
.comp
||
1378 (NULL
== n
->parent
->prev
&&
1379 NULL
== bln
->parent
->prev
))
1380 outflags
|= MMAN_sp
;
1381 outflags
&= ~MMAN_br
;
1382 switch (bln
->norm
->Bl
.type
) {
1388 if (bln
->norm
->Bl
.type
== LIST_diag
)
1389 print_line(".B \"", 0);
1391 print_line(".R \"", 0);
1392 outflags
&= ~MMAN_spc
;
1397 print_width(&bln
->norm
->Bl
, NULL
);
1399 outflags
|= MMAN_nl
;
1401 if (LIST_bullet
== bln
->norm
->Bl
.type
)
1402 print_word("\\(bu");
1406 outflags
|= MMAN_nl
;
1409 print_width(&bln
->norm
->Bl
, NULL
);
1411 outflags
|= MMAN_nl
;
1412 print_count(&bln
->norm
->Bl
.count
);
1413 outflags
|= MMAN_nl
;
1416 print_width(&bln
->norm
->Bl
, n
->child
);
1418 outflags
|= MMAN_nl
;
1421 print_width(&bln
->norm
->Bl
, n
->child
);
1423 outflags
&= ~MMAN_spc
;
1435 * This function is called after closing out an indented block.
1436 * If we are inside an enclosing list, restore its indentation.
1443 /* Nothing to do outside a list. */
1444 if (0 == Bl_stack_len
|| 0 == Bl_stack
[Bl_stack_len
- 1])
1447 /* The indentation has already been set up. */
1448 if (Bl_stack_post
[Bl_stack_len
- 1])
1451 /* Restore the indentation of the enclosing list. */
1452 print_line(".RS", MMAN_Bk_susp
);
1453 (void)snprintf(buf
, sizeof(buf
), "%dn",
1454 Bl_stack
[Bl_stack_len
- 1]);
1457 /* Remeber to close out this .RS block later. */
1458 Bl_stack_post
[Bl_stack_len
- 1] = 1;
1464 const struct roff_node
*bln
;
1466 bln
= n
->parent
->parent
;
1470 switch (bln
->norm
->Bl
.type
) {
1472 outflags
&= ~MMAN_spc
;
1476 outflags
|= MMAN_br
;
1483 switch (bln
->norm
->Bl
.type
) {
1490 assert(Bl_stack_len
);
1491 Bl_stack
[--Bl_stack_len
] = 0;
1494 * Our indentation had to be restored
1495 * after a child display or child list.
1496 * Close out that indentation block now.
1498 if (Bl_stack_post
[Bl_stack_len
]) {
1499 print_line(".RE", MMAN_nl
);
1500 Bl_stack_post
[Bl_stack_len
] = 0;
1504 if (NULL
!= n
->next
) {
1506 outflags
&= ~MMAN_spc
;
1522 if (SEC_LIBRARY
== n
->sec
)
1523 outflags
|= MMAN_br
;
1529 const struct roff_node
*link
, *descr
;
1532 if ((link
= n
->child
) == NULL
)
1536 if ((descr
= link
->next
) != NULL
&& !(descr
->flags
& NODE_DELIMC
)) {
1538 while (descr
!= NULL
&& !(descr
->flags
& NODE_DELIMC
)) {
1539 print_word(descr
->string
);
1540 descr
= descr
->next
;
1547 display
= man_strlen(link
->string
) >= 26;
1549 print_line(".RS", MMAN_Bk_susp
);
1551 outflags
|= MMAN_nl
;
1554 print_word(link
->string
);
1557 /* Trailing punctuation. */
1558 while (descr
!= NULL
) {
1559 print_word(descr
->string
);
1560 descr
= descr
->next
;
1563 print_line(".RE", MMAN_nl
);
1570 print_line(".ll", 0);
1571 if (n
->child
!= NULL
)
1572 print_word(n
->child
->string
);
1573 outflags
|= MMAN_nl
;
1589 if (n
->type
== ROFFT_BLOCK
) {
1590 outflags
|= MMAN_Bk
;
1593 if (n
->type
!= ROFFT_ELEM
&& n
->type
!= ROFFT_HEAD
)
1595 name
= n
->child
== NULL
? NULL
: n
->child
->string
;
1598 if (n
->type
== ROFFT_HEAD
) {
1599 if (NULL
== n
->parent
->prev
)
1600 outflags
|= MMAN_sp
;
1601 print_block(".HP", 0);
1602 printf(" %dn", man_strlen(name
) + 1);
1603 outflags
|= MMAN_nl
;
1615 outflags
&= ~MMAN_Bk
;
1619 if (n
->child
!= NULL
&& n
->child
->string
!= NULL
)
1631 outflags
|= MMAN_spc_force
;
1639 outflags
&= ~MMAN_spc
;
1647 if ( ! (n
->next
== NULL
|| n
->next
->flags
& NODE_LINE
))
1648 outflags
&= ~MMAN_spc
;
1655 if (MDOC_It
!= n
->parent
->tok
)
1656 outflags
|= MMAN_PP
;
1657 outflags
|= MMAN_sp
| MMAN_nl
;
1658 outflags
&= ~MMAN_br
;
1666 if (SEC_SEE_ALSO
== n
->sec
) {
1667 outflags
|= MMAN_PP
| MMAN_sp
| MMAN_nl
;
1668 outflags
&= ~MMAN_br
;
1684 if (NULL
== n
->child
)
1685 outflags
^= MMAN_Sm
;
1686 else if (0 == strcmp("on", n
->child
->string
))
1687 outflags
|= MMAN_Sm
;
1689 outflags
&= ~MMAN_Sm
;
1691 if (MMAN_Sm
& outflags
)
1692 outflags
|= MMAN_spc
;
1700 if (outflags
& MMAN_PP
) {
1701 outflags
&= ~MMAN_PP
;
1702 print_line(".PP", 0);
1704 print_line(".sp", 0);
1705 if (n
->child
!= NULL
)
1706 print_word(n
->child
->string
);
1708 outflags
|= MMAN_nl
;
1722 print_line(".ta", 0);
1723 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
1724 print_word(n
->string
);
1725 outflags
|= MMAN_nl
;
1732 if (NODE_SYNPRETTY
& n
->flags
) {
1751 if (n
->flags
& NODE_SYNPRETTY
&& n
->type
!= ROFFT_BODY
)
1763 print_node(meta
, n
);
1767 outflags
&= ~MMAN_spc
;
1769 print_node(meta
, n
);