]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_man.c
1 /* $Id: mdoc_man.c,v 1.119 2017/06/08 12:54:58 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
] = {
138 static const struct manact __manacts
[MDOC_MAX
- MDOC_Dd
] = {
139 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dd */
140 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dt */
141 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Os */
142 { NULL
, pre_sect
, post_sect
, ".SH", NULL
}, /* Sh */
143 { NULL
, pre_sect
, post_sect
, ".SS", NULL
}, /* Ss */
144 { NULL
, pre_pp
, NULL
, NULL
, NULL
}, /* Pp */
145 { cond_body
, pre_dl
, post_dl
, NULL
, NULL
}, /* D1 */
146 { cond_body
, pre_dl
, post_dl
, NULL
, NULL
}, /* Dl */
147 { cond_body
, pre_bd
, post_bd
, NULL
, NULL
}, /* Bd */
148 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ed */
149 { cond_body
, pre_bl
, post_bl
, NULL
, NULL
}, /* Bl */
150 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* El */
151 { NULL
, pre_it
, post_it
, NULL
, NULL
}, /* It */
152 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Ad */
153 { NULL
, pre_an
, NULL
, NULL
, NULL
}, /* An */
154 { NULL
, pre_ap
, NULL
, NULL
, NULL
}, /* Ap */
155 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Ar */
156 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Cd */
157 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Cm */
158 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Dv */
159 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Er */
160 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Ev */
161 { NULL
, pre_ex
, NULL
, NULL
, NULL
}, /* Ex */
162 { NULL
, pre_fa
, post_fa
, NULL
, NULL
}, /* Fa */
163 { NULL
, pre_fd
, post_fd
, NULL
, NULL
}, /* Fd */
164 { NULL
, pre_fl
, post_fl
, NULL
, NULL
}, /* Fl */
165 { NULL
, pre_fn
, post_fn
, NULL
, NULL
}, /* Fn */
166 { NULL
, pre_Ft
, post_font
, NULL
, NULL
}, /* Ft */
167 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Ic */
168 { NULL
, pre_in
, post_in
, NULL
, NULL
}, /* In */
169 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Li */
170 { cond_head
, pre_enc
, NULL
, "\\- ", NULL
}, /* Nd */
171 { NULL
, pre_nm
, post_nm
, NULL
, NULL
}, /* Nm */
172 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Op */
173 { NULL
, pre_Ft
, post_font
, NULL
, NULL
}, /* Ot */
174 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Pa */
175 { NULL
, pre_ex
, NULL
, NULL
, NULL
}, /* Rv */
176 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* St */
177 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Va */
178 { NULL
, pre_vt
, post_vt
, NULL
, NULL
}, /* Vt */
179 { NULL
, pre_xr
, NULL
, NULL
, NULL
}, /* Xr */
180 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %A */
181 { NULL
, pre_em
, post_percent
, NULL
, NULL
}, /* %B */
182 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %D */
183 { NULL
, pre_em
, post_percent
, NULL
, NULL
}, /* %I */
184 { NULL
, pre_em
, post_percent
, NULL
, NULL
}, /* %J */
185 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %N */
186 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %O */
187 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %P */
188 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %R */
189 { NULL
, pre__t
, post__t
, NULL
, NULL
}, /* %T */
190 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %V */
191 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ac */
192 { cond_body
, pre_aq
, post_aq
, NULL
, NULL
}, /* Ao */
193 { cond_body
, pre_aq
, post_aq
, NULL
, NULL
}, /* Aq */
194 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* At */
195 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bc */
196 { NULL
, pre_bf
, post_bf
, NULL
, NULL
}, /* Bf */
197 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Bo */
198 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Bq */
199 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bsx */
200 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bx */
201 { NULL
, pre_skip
, NULL
, NULL
, NULL
}, /* Db */
202 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dc */
203 { cond_body
, pre_enc
, post_enc
, "\\(Lq", "\\(Rq" }, /* Do */
204 { cond_body
, pre_enc
, post_enc
, "\\(Lq", "\\(Rq" }, /* Dq */
205 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ec */
206 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ef */
207 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Em */
208 { cond_body
, pre_eo
, post_eo
, NULL
, NULL
}, /* Eo */
209 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fx */
210 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Ms */
211 { NULL
, pre_no
, NULL
, NULL
, NULL
}, /* No */
212 { NULL
, pre_ns
, NULL
, NULL
, NULL
}, /* Ns */
213 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Nx */
214 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ox */
215 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Pc */
216 { NULL
, NULL
, post_pf
, NULL
, NULL
}, /* Pf */
217 { cond_body
, pre_enc
, post_enc
, "(", ")" }, /* Po */
218 { cond_body
, pre_enc
, post_enc
, "(", ")" }, /* Pq */
219 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Qc */
220 { cond_body
, pre_enc
, post_enc
, "\\(oq", "\\(cq" }, /* Ql */
221 { cond_body
, pre_enc
, post_enc
, "\"", "\"" }, /* Qo */
222 { cond_body
, pre_enc
, post_enc
, "\"", "\"" }, /* Qq */
223 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Re */
224 { cond_body
, pre_rs
, NULL
, NULL
, NULL
}, /* Rs */
225 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Sc */
226 { cond_body
, pre_enc
, post_enc
, "\\(oq", "\\(cq" }, /* So */
227 { cond_body
, pre_enc
, post_enc
, "\\(oq", "\\(cq" }, /* Sq */
228 { NULL
, pre_sm
, NULL
, NULL
, NULL
}, /* Sm */
229 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Sx */
230 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Sy */
231 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Tn */
232 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ux */
233 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xc */
234 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xo */
235 { NULL
, pre_fo
, post_fo
, NULL
, NULL
}, /* Fo */
236 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fc */
237 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Oo */
238 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Oc */
239 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Bk */
240 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ek */
241 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bt */
242 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Hf */
243 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Fr */
244 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ud */
245 { NULL
, NULL
, post_lb
, NULL
, NULL
}, /* Lb */
246 { NULL
, pre_pp
, NULL
, NULL
, NULL
}, /* Lp */
247 { NULL
, pre_lk
, NULL
, NULL
, NULL
}, /* Lk */
248 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Mt */
249 { cond_body
, pre_enc
, post_enc
, "{", "}" }, /* Brq */
250 { cond_body
, pre_enc
, post_enc
, "{", "}" }, /* Bro */
251 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Brc */
252 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %C */
253 { NULL
, pre_skip
, NULL
, NULL
, NULL
}, /* Es */
254 { cond_body
, pre_en
, post_en
, NULL
, NULL
}, /* En */
255 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dx */
256 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %Q */
257 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %U */
258 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ta */
260 static const struct manact
*const manacts
= __manacts
- MDOC_Dd
;
263 #define MMAN_spc (1 << 0) /* blank character before next word */
264 #define MMAN_spc_force (1 << 1) /* even before trailing punctuation */
265 #define MMAN_nl (1 << 2) /* break man(7) code line */
266 #define MMAN_br (1 << 3) /* break output line */
267 #define MMAN_sp (1 << 4) /* insert a blank output line */
268 #define MMAN_PP (1 << 5) /* reset indentation etc. */
269 #define MMAN_Sm (1 << 6) /* horizontal spacing mode */
270 #define MMAN_Bk (1 << 7) /* word keep mode */
271 #define MMAN_Bk_susp (1 << 8) /* suspend this (after a macro) */
272 #define MMAN_An_split (1 << 9) /* author mode is "split" */
273 #define MMAN_An_nosplit (1 << 10) /* author mode is "nosplit" */
274 #define MMAN_PD (1 << 11) /* inter-paragraph spacing disabled */
275 #define MMAN_nbrword (1 << 12) /* do not break the next word */
277 #define BL_STACK_MAX 32
279 static int Bl_stack
[BL_STACK_MAX
]; /* offsets [chars] */
280 static int Bl_stack_post
[BL_STACK_MAX
]; /* add final .RE */
281 static int Bl_stack_len
; /* number of nested Bl blocks */
282 static int TPremain
; /* characters before tag is full */
292 man_strlen(const char *cp
)
300 rsz
= strcspn(cp
, "\\");
312 switch (mandoc_escape(&cp
, NULL
, NULL
)) {
316 case ESCAPE_NUMBERED
:
318 case ESCAPE_OVERSTRIKE
:
324 case ESCAPE_SKIPCHAR
:
335 font_push(char newfont
)
338 if (fontqueue
.head
+ fontqueue
.size
<= ++fontqueue
.tail
) {
340 fontqueue
.head
= mandoc_realloc(fontqueue
.head
,
343 *fontqueue
.tail
= newfont
;
347 outflags
&= ~MMAN_spc
;
354 if (fontqueue
.tail
> fontqueue
.head
)
356 outflags
&= ~MMAN_spc
;
359 putchar(*fontqueue
.tail
);
363 print_word(const char *s
)
366 if ((MMAN_PP
| MMAN_sp
| MMAN_br
| MMAN_nl
) & outflags
) {
368 * If we need a newline, print it now and start afresh.
370 if (MMAN_PP
& outflags
) {
371 if (MMAN_sp
& outflags
) {
372 if (MMAN_PD
& outflags
) {
374 outflags
&= ~MMAN_PD
;
376 } else if ( ! (MMAN_PD
& outflags
)) {
381 } else if (MMAN_sp
& outflags
)
383 else if (MMAN_br
& outflags
)
385 else if (MMAN_nl
& outflags
)
387 outflags
&= ~(MMAN_PP
|MMAN_sp
|MMAN_br
|MMAN_nl
|MMAN_spc
);
391 } else if (MMAN_spc
& outflags
) {
393 * If we need a space, only print it if
394 * (1) it is forced by `No' or
395 * (2) what follows is not terminating punctuation or
396 * (3) what follows is longer than one character.
398 if (MMAN_spc_force
& outflags
|| '\0' == s
[0] ||
399 NULL
== strchr(".,:;)]?!", s
[0]) || '\0' != s
[1]) {
400 if (MMAN_Bk
& outflags
&&
401 ! (MMAN_Bk_susp
& outflags
))
410 * Reassign needing space if we're not following opening
413 if (MMAN_Sm
& outflags
&& ('\0' == s
[0] ||
414 (('(' != s
[0] && '[' != s
[0]) || '\0' != s
[1])))
415 outflags
|= MMAN_spc
;
417 outflags
&= ~MMAN_spc
;
418 outflags
&= ~(MMAN_spc_force
| MMAN_Bk_susp
);
432 if (MMAN_nbrword
& outflags
) {
438 putchar((unsigned char)*s
);
444 outflags
&= ~MMAN_nbrword
;
448 print_line(const char *s
, int newflags
)
453 outflags
|= newflags
;
457 print_block(const char *s
, int newflags
)
460 outflags
&= ~MMAN_PP
;
461 if (MMAN_sp
& outflags
) {
462 outflags
&= ~(MMAN_sp
| MMAN_br
);
463 if (MMAN_PD
& outflags
) {
464 print_line(".PD", 0);
465 outflags
&= ~MMAN_PD
;
467 } else if (! (MMAN_PD
& outflags
))
468 print_line(".PD 0", MMAN_PD
);
471 outflags
|= MMAN_Bk_susp
| newflags
;
475 print_offs(const char *v
, int keywords
)
482 print_line(".RS", MMAN_Bk_susp
);
484 /* Convert v into a number (of characters). */
485 if (NULL
== v
|| '\0' == *v
|| (keywords
&& !strcmp(v
, "left")))
487 else if (keywords
&& !strcmp(v
, "indent"))
489 else if (keywords
&& !strcmp(v
, "indent-two"))
492 end
= a2roffsu(v
, &su
, SCALE_EN
);
493 if (end
== NULL
|| *end
!= '\0')
495 else if (SCALE_EN
== su
.unit
)
500 * If we are inside an enclosing list,
501 * there is no easy way to add the two
502 * indentations because they are provided
503 * in terms of different units.
512 * We are inside an enclosing list.
513 * Add the two indentations.
516 sz
+= Bl_stack
[Bl_stack_len
- 1];
518 (void)snprintf(buf
, sizeof(buf
), "%dn", sz
);
524 * Set up the indentation for a list item; used from pre_it().
527 print_width(const struct mdoc_bl
*bl
, const struct roff_node
*child
)
532 int numeric
, remain
, sz
, chsz
;
537 /* Convert the width into a number (of characters). */
538 if (bl
->width
== NULL
)
539 sz
= (bl
->type
== LIST_hang
) ? 6 : 0;
541 end
= a2roffsu(bl
->width
, &su
, SCALE_MAX
);
542 if (end
== NULL
|| *end
!= '\0')
543 sz
= man_strlen(bl
->width
);
544 else if (SCALE_EN
== su
.unit
)
552 /* XXX Rough estimation, might have multiple parts. */
553 if (bl
->type
== LIST_enum
)
554 chsz
= (bl
->count
> 8) + 1;
555 else if (child
!= NULL
&& child
->type
== ROFFT_TEXT
)
556 chsz
= man_strlen(child
->string
);
560 /* Maybe we are inside an enclosing list? */
564 * Save our own indentation,
565 * such that child lists can use it.
567 Bl_stack
[Bl_stack_len
++] = sz
+ 2;
569 /* Set up the current list. */
570 if (chsz
> sz
&& bl
->type
!= LIST_tag
)
571 print_block(".HP", 0);
573 print_block(".TP", 0);
577 (void)snprintf(buf
, sizeof(buf
), "%dn", sz
+ 2);
580 print_word(bl
->width
);
585 print_count(int *count
)
589 (void)snprintf(buf
, sizeof(buf
), "%d.\\&", ++*count
);
594 man_man(void *arg
, const struct roff_man
*man
)
598 * Dump the keep buffer.
599 * We're guaranteed by now that this exists (is non-NULL).
600 * Flush stdout afterward, just in case.
602 fputs(mparse_getkeep(man_mparse(man
)), stdout
);
607 man_mdoc(void *arg
, const struct roff_man
*mdoc
)
611 printf(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"\n",
613 (mdoc
->meta
.msec
== NULL
? "" : mdoc
->meta
.msec
),
614 mdoc
->meta
.date
, mdoc
->meta
.os
, mdoc
->meta
.vol
);
616 /* Disable hyphenation and if nroff, disable justification. */
617 printf(".nh\n.if n .ad l");
619 outflags
= MMAN_nl
| MMAN_Sm
;
620 if (0 == fontqueue
.size
) {
622 fontqueue
.head
= fontqueue
.tail
= mandoc_malloc(8);
623 *fontqueue
.tail
= 'R';
625 for (n
= mdoc
->first
->child
; n
!= NULL
; n
= n
->next
)
626 print_node(&mdoc
->meta
, n
);
631 print_node(DECL_ARGS
)
633 const struct manact
*act
;
634 struct roff_node
*sub
;
637 if (n
->flags
& NODE_NOPRT
)
641 * Break the line if we were parsed subsequent the current node.
642 * This makes the page structure be more consistent.
644 if (MMAN_spc
& outflags
&& NODE_LINE
& n
->flags
)
650 n
->flags
&= ~NODE_ENDED
;
652 if (n
->type
== ROFFT_TEXT
) {
654 * Make sure that we don't happen to start with a
655 * control character at the start of a line.
657 if (MMAN_nl
& outflags
&&
658 ('.' == *n
->string
|| '\'' == *n
->string
)) {
661 outflags
&= ~MMAN_spc
;
663 if (n
->flags
& NODE_DELIMC
)
664 outflags
&= ~(MMAN_spc
| MMAN_spc_force
);
665 else if (outflags
& MMAN_Sm
)
666 outflags
|= MMAN_spc_force
;
667 print_word(n
->string
);
668 if (n
->flags
& NODE_DELIMO
)
669 outflags
&= ~(MMAN_spc
| MMAN_spc_force
);
670 else if (outflags
& MMAN_Sm
)
671 outflags
|= MMAN_spc
;
672 } else if (n
->tok
< ROFF_MAX
) {
673 (*roff_manacts
[n
->tok
])(meta
, n
);
676 assert(n
->tok
>= MDOC_Dd
&& n
->tok
< MDOC_MAX
);
678 * Conditionally run the pre-node action handler for a
681 act
= manacts
+ n
->tok
;
682 cond
= act
->cond
== NULL
|| (*act
->cond
)(meta
, n
);
683 if (cond
&& act
->pre
!= NULL
&&
684 (n
->end
== ENDBODY_NOT
|| n
->child
!= NULL
))
685 do_sub
= (*act
->pre
)(meta
, n
);
689 * Conditionally run all child nodes.
690 * Note that this iterates over children instead of using
691 * recursion. This prevents unnecessary depth in the stack.
694 for (sub
= n
->child
; sub
; sub
= sub
->next
)
695 print_node(meta
, sub
);
698 * Lastly, conditionally run the post-node handler.
700 if (NODE_ENDED
& n
->flags
)
703 if (cond
&& act
->post
)
704 (*act
->post
)(meta
, n
);
706 if (ENDBODY_NOT
!= n
->end
)
707 n
->body
->flags
|= NODE_ENDED
;
714 return n
->type
== ROFFT_HEAD
;
721 return n
->type
== ROFFT_BODY
;
729 prefix
= manacts
[n
->tok
].prefix
;
733 outflags
&= ~MMAN_spc
;
742 suffix
= manacts
[n
->tok
].suffix
;
745 outflags
&= ~(MMAN_spc
| MMAN_nl
);
752 outflags
|= MMAN_br
| MMAN_nl
;
764 post_percent(DECL_ARGS
)
767 if (pre_em
== manacts
[n
->tok
].pre
)
771 if (n
->prev
&& n
->prev
->tok
== n
->tok
&&
772 n
->next
->tok
== n
->tok
)
784 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
) {
786 outflags
&= ~MMAN_spc
;
796 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
) {
797 outflags
&= ~MMAN_spc
;
801 post_percent(meta
, n
);
805 * Print before a section header.
811 if (n
->type
== ROFFT_HEAD
) {
813 print_block(manacts
[n
->tok
].prefix
, 0);
816 outflags
&= ~MMAN_spc
;
822 * Print subsequent a section header.
828 if (n
->type
!= ROFFT_HEAD
)
830 outflags
&= ~MMAN_spc
;
834 if (MDOC_Sh
== n
->tok
&& SEC_AUTHORS
== n
->sec
)
835 outflags
&= ~(MMAN_An_split
| MMAN_An_nosplit
);
838 /* See mdoc_term.c, synopsis_pre() for comments. */
840 pre_syn(const struct roff_node
*n
)
843 if (NULL
== n
->prev
|| ! (NODE_SYNPRETTY
& n
->flags
))
846 if (n
->prev
->tok
== n
->tok
&&
854 switch (n
->prev
->tok
) {
863 if (MDOC_Fn
!= n
->tok
&& MDOC_Fo
!= n
->tok
) {
878 switch (n
->norm
->An
.auth
) {
880 outflags
&= ~MMAN_An_nosplit
;
881 outflags
|= MMAN_An_split
;
884 outflags
&= ~MMAN_An_split
;
885 outflags
|= MMAN_An_nosplit
;
888 if (MMAN_An_split
& outflags
)
890 else if (SEC_AUTHORS
== n
->sec
&&
891 ! (MMAN_An_nosplit
& outflags
))
892 outflags
|= MMAN_An_split
;
901 outflags
&= ~MMAN_spc
;
903 outflags
&= ~MMAN_spc
;
911 print_word(n
->child
!= NULL
&& n
->child
->next
== NULL
&&
912 n
->child
->tok
== MDOC_Mt
? "<" : "\\(la");
913 outflags
&= ~MMAN_spc
;
921 outflags
&= ~(MMAN_spc
| MMAN_nl
);
922 print_word(n
->child
!= NULL
&& n
->child
->next
== NULL
&&
923 n
->child
->tok
== MDOC_Mt
? ">" : "\\(ra");
930 outflags
&= ~(MMAN_PP
| MMAN_sp
| MMAN_br
);
932 if (DISP_unfilled
== n
->norm
->Bd
.type
||
933 DISP_literal
== n
->norm
->Bd
.type
)
934 print_line(".nf", 0);
935 if (0 == n
->norm
->Bd
.comp
&& NULL
!= n
->parent
->prev
)
937 print_offs(n
->norm
->Bd
.offs
, 1);
945 /* Close out this display. */
946 print_line(".RE", MMAN_nl
);
947 if (DISP_unfilled
== n
->norm
->Bd
.type
||
948 DISP_literal
== n
->norm
->Bd
.type
)
949 print_line(".fi", MMAN_nl
);
951 /* Maybe we are inside an enclosing list? */
952 if (NULL
!= n
->parent
->next
)
968 switch (n
->norm
->Bf
.font
) {
986 if (n
->type
== ROFFT_BODY
)
1009 if (n
->type
== ROFFT_BODY
)
1010 outflags
&= ~MMAN_Bk
;
1019 * print_offs() will increase the -offset to account for
1020 * a possible enclosing .It, but any enclosed .It blocks
1021 * just nest and do not add up their indentation.
1023 if (n
->norm
->Bl
.offs
) {
1024 print_offs(n
->norm
->Bl
.offs
, 0);
1025 Bl_stack
[Bl_stack_len
++] = 0;
1028 switch (n
->norm
->Bl
.type
) {
1030 n
->norm
->Bl
.count
= 0;
1038 if (n
->child
!= NULL
) {
1039 print_line(".TS", MMAN_nl
);
1040 for (icol
= 0; icol
< n
->norm
->Bl
.ncols
; icol
++)
1044 outflags
|= MMAN_nl
;
1052 switch (n
->norm
->Bl
.type
) {
1054 if (n
->child
!= NULL
)
1055 print_line(".TE", 0);
1058 n
->norm
->Bl
.count
= 0;
1064 if (n
->norm
->Bl
.offs
) {
1065 print_line(".RE", MMAN_nl
);
1066 assert(Bl_stack_len
);
1068 assert(0 == Bl_stack
[Bl_stack_len
]);
1070 outflags
|= MMAN_PP
| MMAN_nl
;
1071 outflags
&= ~(MMAN_sp
| MMAN_br
);
1074 /* Maybe we are inside an enclosing list? */
1075 if (NULL
!= n
->parent
->next
)
1083 outflags
|= MMAN_br
;
1090 print_offs("6n", 0);
1098 print_line(".RE", MMAN_nl
);
1100 /* Maybe we are inside an enclosing list? */
1101 if (NULL
!= n
->parent
->next
)
1117 if (NULL
== n
->norm
->Es
||
1118 NULL
== n
->norm
->Es
->child
)
1121 print_word(n
->norm
->Es
->child
->string
);
1122 outflags
&= ~MMAN_spc
;
1130 if (NULL
== n
->norm
->Es
||
1131 NULL
== n
->norm
->Es
->child
||
1132 NULL
== n
->norm
->Es
->child
->next
)
1135 outflags
&= ~MMAN_spc
;
1136 print_word(n
->norm
->Es
->child
->next
->string
);
1144 if (n
->end
== ENDBODY_NOT
&&
1145 n
->parent
->head
->child
== NULL
&&
1147 n
->child
->end
!= ENDBODY_NOT
)
1149 else if (n
->end
!= ENDBODY_NOT
? n
->child
!= NULL
:
1150 n
->parent
->head
->child
!= NULL
&& (n
->child
!= NULL
||
1151 (n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
)))
1152 outflags
&= ~(MMAN_spc
| MMAN_nl
);
1161 if (n
->end
!= ENDBODY_NOT
) {
1162 outflags
|= MMAN_spc
;
1166 body
= n
->child
!= NULL
|| n
->parent
->head
->child
!= NULL
;
1167 tail
= n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
;
1170 outflags
&= ~MMAN_spc
;
1171 else if ( ! (body
|| tail
))
1174 outflags
|= MMAN_spc
;
1182 am_Fa
= MDOC_Fa
== n
->tok
;
1189 if (am_Fa
|| NODE_SYNPRETTY
& n
->flags
)
1190 outflags
|= MMAN_nbrword
;
1191 print_node(meta
, n
);
1193 if (NULL
!= (n
= n
->next
))
1203 if (NULL
!= n
->next
&& MDOC_Fa
== n
->next
->tok
)
1221 outflags
|= MMAN_br
;
1230 if (n
->child
!= NULL
)
1231 outflags
&= ~MMAN_spc
;
1240 if (!(n
->child
!= NULL
||
1242 n
->next
->type
== ROFFT_TEXT
||
1243 n
->next
->flags
& NODE_LINE
))
1244 outflags
&= ~MMAN_spc
;
1257 if (NODE_SYNPRETTY
& n
->flags
)
1258 print_block(".HP 4n", MMAN_nl
);
1261 print_node(meta
, n
);
1263 outflags
&= ~MMAN_spc
;
1265 outflags
&= ~MMAN_spc
;
1278 if (NODE_SYNPRETTY
& n
->flags
) {
1280 outflags
|= MMAN_PP
;
1293 if (n
->child
== NULL
)
1295 if (NODE_SYNPRETTY
& n
->flags
)
1296 print_block(".HP 4n", MMAN_nl
);
1300 outflags
&= ~(MMAN_spc
| MMAN_nl
);
1302 outflags
&= ~MMAN_spc
;
1316 if (n
->child
!= NULL
)
1339 print_line(".ft", 0);
1340 print_word(n
->child
->string
);
1341 outflags
|= MMAN_nl
;
1348 if (NODE_SYNPRETTY
& n
->flags
) {
1351 print_word("#include <");
1352 outflags
&= ~MMAN_spc
;
1355 outflags
&= ~MMAN_spc
;
1365 if (NODE_SYNPRETTY
& n
->flags
) {
1366 outflags
&= ~MMAN_spc
;
1369 outflags
|= MMAN_br
;
1372 outflags
&= ~MMAN_spc
;
1380 const struct roff_node
*bln
;
1384 outflags
|= MMAN_PP
| MMAN_nl
;
1385 bln
= n
->parent
->parent
;
1386 if (0 == bln
->norm
->Bl
.comp
||
1387 (NULL
== n
->parent
->prev
&&
1388 NULL
== bln
->parent
->prev
))
1389 outflags
|= MMAN_sp
;
1390 outflags
&= ~MMAN_br
;
1391 switch (bln
->norm
->Bl
.type
) {
1397 if (bln
->norm
->Bl
.type
== LIST_diag
)
1398 print_line(".B \"", 0);
1400 print_line(".R \"", 0);
1401 outflags
&= ~MMAN_spc
;
1406 print_width(&bln
->norm
->Bl
, NULL
);
1408 outflags
|= MMAN_nl
;
1410 if (LIST_bullet
== bln
->norm
->Bl
.type
)
1411 print_word("\\(bu");
1415 outflags
|= MMAN_nl
;
1418 print_width(&bln
->norm
->Bl
, NULL
);
1420 outflags
|= MMAN_nl
;
1421 print_count(&bln
->norm
->Bl
.count
);
1422 outflags
|= MMAN_nl
;
1425 print_width(&bln
->norm
->Bl
, n
->child
);
1427 outflags
|= MMAN_nl
;
1430 print_width(&bln
->norm
->Bl
, n
->child
);
1432 outflags
&= ~MMAN_spc
;
1444 * This function is called after closing out an indented block.
1445 * If we are inside an enclosing list, restore its indentation.
1452 /* Nothing to do outside a list. */
1453 if (0 == Bl_stack_len
|| 0 == Bl_stack
[Bl_stack_len
- 1])
1456 /* The indentation has already been set up. */
1457 if (Bl_stack_post
[Bl_stack_len
- 1])
1460 /* Restore the indentation of the enclosing list. */
1461 print_line(".RS", MMAN_Bk_susp
);
1462 (void)snprintf(buf
, sizeof(buf
), "%dn",
1463 Bl_stack
[Bl_stack_len
- 1]);
1466 /* Remeber to close out this .RS block later. */
1467 Bl_stack_post
[Bl_stack_len
- 1] = 1;
1473 const struct roff_node
*bln
;
1475 bln
= n
->parent
->parent
;
1479 switch (bln
->norm
->Bl
.type
) {
1481 outflags
&= ~MMAN_spc
;
1485 outflags
|= MMAN_br
;
1492 switch (bln
->norm
->Bl
.type
) {
1499 assert(Bl_stack_len
);
1500 Bl_stack
[--Bl_stack_len
] = 0;
1503 * Our indentation had to be restored
1504 * after a child display or child list.
1505 * Close out that indentation block now.
1507 if (Bl_stack_post
[Bl_stack_len
]) {
1508 print_line(".RE", MMAN_nl
);
1509 Bl_stack_post
[Bl_stack_len
] = 0;
1513 if (NULL
!= n
->next
) {
1515 outflags
&= ~MMAN_spc
;
1531 if (SEC_LIBRARY
== n
->sec
)
1532 outflags
|= MMAN_br
;
1538 const struct roff_node
*link
, *descr
, *punct
;
1541 if ((link
= n
->child
) == NULL
)
1544 /* Find beginning of trailing punctuation. */
1546 while (punct
!= link
&& punct
->flags
& NODE_DELIMC
)
1547 punct
= punct
->prev
;
1548 punct
= punct
->next
;
1551 if ((descr
= link
->next
) != NULL
&& descr
!= punct
) {
1553 while (descr
!= punct
) {
1554 print_word(descr
->string
);
1555 descr
= descr
->next
;
1562 display
= man_strlen(link
->string
) >= 26;
1564 print_line(".RS", MMAN_Bk_susp
);
1566 outflags
|= MMAN_nl
;
1569 print_word(link
->string
);
1572 /* Trailing punctuation. */
1573 while (punct
!= NULL
) {
1574 print_word(punct
->string
);
1575 punct
= punct
->next
;
1578 print_line(".RE", MMAN_nl
);
1583 pre_onearg(DECL_ARGS
)
1585 outflags
|= MMAN_nl
;
1587 outflags
&= ~MMAN_spc
;
1588 print_word(roff_name
[n
->tok
]);
1589 if (n
->child
!= NULL
)
1590 print_word(n
->child
->string
);
1591 outflags
|= MMAN_nl
;
1592 if (n
->tok
== ROFF_ce
)
1593 for (n
= n
->child
->next
; n
!= NULL
; n
= n
->next
)
1594 print_node(meta
, n
);
1610 if (n
->type
== ROFFT_BLOCK
) {
1611 outflags
|= MMAN_Bk
;
1614 if (n
->type
!= ROFFT_ELEM
&& n
->type
!= ROFFT_HEAD
)
1616 name
= n
->child
== NULL
? NULL
: n
->child
->string
;
1619 if (n
->type
== ROFFT_HEAD
) {
1620 if (NULL
== n
->parent
->prev
)
1621 outflags
|= MMAN_sp
;
1622 print_block(".HP", 0);
1623 printf(" %dn", man_strlen(name
) + 1);
1624 outflags
|= MMAN_nl
;
1636 outflags
&= ~MMAN_Bk
;
1640 if (n
->child
!= NULL
&& n
->child
->string
!= NULL
)
1652 outflags
|= MMAN_spc_force
;
1660 outflags
&= ~MMAN_spc
;
1668 if ( ! (n
->next
== NULL
|| n
->next
->flags
& NODE_LINE
))
1669 outflags
&= ~MMAN_spc
;
1676 if (MDOC_It
!= n
->parent
->tok
)
1677 outflags
|= MMAN_PP
;
1678 outflags
|= MMAN_sp
| MMAN_nl
;
1679 outflags
&= ~MMAN_br
;
1687 if (SEC_SEE_ALSO
== n
->sec
) {
1688 outflags
|= MMAN_PP
| MMAN_sp
| MMAN_nl
;
1689 outflags
&= ~MMAN_br
;
1705 if (NULL
== n
->child
)
1706 outflags
^= MMAN_Sm
;
1707 else if (0 == strcmp("on", n
->child
->string
))
1708 outflags
|= MMAN_Sm
;
1710 outflags
&= ~MMAN_Sm
;
1712 if (MMAN_Sm
& outflags
)
1713 outflags
|= MMAN_spc
;
1721 if (outflags
& MMAN_PP
) {
1722 outflags
&= ~MMAN_PP
;
1723 print_line(".PP", 0);
1725 print_line(".sp", 0);
1726 if (n
->child
!= NULL
)
1727 print_word(n
->child
->string
);
1729 outflags
|= MMAN_nl
;
1743 print_line(".ta", 0);
1744 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
1745 print_word(n
->string
);
1746 outflags
|= MMAN_nl
;
1753 if (NODE_SYNPRETTY
& n
->flags
) {
1772 if (n
->flags
& NODE_SYNPRETTY
&& n
->type
!= ROFFT_BODY
)
1784 print_node(meta
, n
);
1788 outflags
&= ~MMAN_spc
;
1790 print_node(meta
, n
);