]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_man.c
1 /* $Id: mdoc_man.c,v 1.111 2017/05/05 02:06:19 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
37 int (*cond
)(DECL_ARGS
); /* DON'T run actions */
38 int (*pre
)(DECL_ARGS
); /* pre-node action */
39 void (*post
)(DECL_ARGS
); /* post-node action */
40 const char *prefix
; /* pre-node string constant */
41 const char *suffix
; /* post-node string constant */
44 static int cond_body(DECL_ARGS
);
45 static int cond_head(DECL_ARGS
);
46 static void font_push(char);
47 static void font_pop(void);
48 static int man_strlen(const char *);
49 static void mid_it(void);
50 static void post__t(DECL_ARGS
);
51 static void post_aq(DECL_ARGS
);
52 static void post_bd(DECL_ARGS
);
53 static void post_bf(DECL_ARGS
);
54 static void post_bk(DECL_ARGS
);
55 static void post_bl(DECL_ARGS
);
56 static void post_dl(DECL_ARGS
);
57 static void post_en(DECL_ARGS
);
58 static void post_enc(DECL_ARGS
);
59 static void post_eo(DECL_ARGS
);
60 static void post_fa(DECL_ARGS
);
61 static void post_fd(DECL_ARGS
);
62 static void post_fl(DECL_ARGS
);
63 static void post_fn(DECL_ARGS
);
64 static void post_fo(DECL_ARGS
);
65 static void post_font(DECL_ARGS
);
66 static void post_in(DECL_ARGS
);
67 static void post_it(DECL_ARGS
);
68 static void post_lb(DECL_ARGS
);
69 static void post_nm(DECL_ARGS
);
70 static void post_percent(DECL_ARGS
);
71 static void post_pf(DECL_ARGS
);
72 static void post_sect(DECL_ARGS
);
73 static void post_sp(DECL_ARGS
);
74 static void post_vt(DECL_ARGS
);
75 static int pre__t(DECL_ARGS
);
76 static int pre_an(DECL_ARGS
);
77 static int pre_ap(DECL_ARGS
);
78 static int pre_aq(DECL_ARGS
);
79 static int pre_bd(DECL_ARGS
);
80 static int pre_bf(DECL_ARGS
);
81 static int pre_bk(DECL_ARGS
);
82 static int pre_bl(DECL_ARGS
);
83 static int pre_br(DECL_ARGS
);
84 static int pre_dl(DECL_ARGS
);
85 static int pre_en(DECL_ARGS
);
86 static int pre_enc(DECL_ARGS
);
87 static int pre_em(DECL_ARGS
);
88 static int pre_skip(DECL_ARGS
);
89 static int pre_eo(DECL_ARGS
);
90 static int pre_ex(DECL_ARGS
);
91 static int pre_fa(DECL_ARGS
);
92 static int pre_fd(DECL_ARGS
);
93 static int pre_fl(DECL_ARGS
);
94 static int pre_fn(DECL_ARGS
);
95 static int pre_fo(DECL_ARGS
);
96 static int pre_ft(DECL_ARGS
);
97 static int pre_Ft(DECL_ARGS
);
98 static int pre_in(DECL_ARGS
);
99 static int pre_it(DECL_ARGS
);
100 static int pre_lk(DECL_ARGS
);
101 static int pre_li(DECL_ARGS
);
102 static int pre_ll(DECL_ARGS
);
103 static int pre_nm(DECL_ARGS
);
104 static int pre_no(DECL_ARGS
);
105 static int pre_ns(DECL_ARGS
);
106 static int pre_pp(DECL_ARGS
);
107 static int pre_rs(DECL_ARGS
);
108 static int pre_sm(DECL_ARGS
);
109 static int pre_sp(DECL_ARGS
);
110 static int pre_sect(DECL_ARGS
);
111 static int pre_sy(DECL_ARGS
);
112 static void pre_syn(const struct roff_node
*);
113 static int pre_vt(DECL_ARGS
);
114 static int pre_xr(DECL_ARGS
);
115 static void print_word(const char *);
116 static void print_line(const char *, int);
117 static void print_block(const char *, int);
118 static void print_offs(const char *, int);
119 static void print_width(const struct mdoc_bl
*,
120 const struct roff_node
*);
121 static void print_count(int *);
122 static void print_node(DECL_ARGS
);
124 static const struct manact __manacts
[MDOC_MAX
- MDOC_Dd
] = {
125 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dd */
126 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dt */
127 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Os */
128 { NULL
, pre_sect
, post_sect
, ".SH", NULL
}, /* Sh */
129 { NULL
, pre_sect
, post_sect
, ".SS", NULL
}, /* Ss */
130 { NULL
, pre_pp
, NULL
, NULL
, NULL
}, /* Pp */
131 { cond_body
, pre_dl
, post_dl
, NULL
, NULL
}, /* D1 */
132 { cond_body
, pre_dl
, post_dl
, NULL
, NULL
}, /* Dl */
133 { cond_body
, pre_bd
, post_bd
, NULL
, NULL
}, /* Bd */
134 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ed */
135 { cond_body
, pre_bl
, post_bl
, NULL
, NULL
}, /* Bl */
136 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* El */
137 { NULL
, pre_it
, post_it
, NULL
, NULL
}, /* It */
138 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Ad */
139 { NULL
, pre_an
, NULL
, NULL
, NULL
}, /* An */
140 { NULL
, pre_ap
, NULL
, NULL
, NULL
}, /* Ap */
141 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Ar */
142 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Cd */
143 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Cm */
144 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Dv */
145 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Er */
146 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Ev */
147 { NULL
, pre_ex
, NULL
, NULL
, NULL
}, /* Ex */
148 { NULL
, pre_fa
, post_fa
, NULL
, NULL
}, /* Fa */
149 { NULL
, pre_fd
, post_fd
, NULL
, NULL
}, /* Fd */
150 { NULL
, pre_fl
, post_fl
, NULL
, NULL
}, /* Fl */
151 { NULL
, pre_fn
, post_fn
, NULL
, NULL
}, /* Fn */
152 { NULL
, pre_Ft
, post_font
, NULL
, NULL
}, /* Ft */
153 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Ic */
154 { NULL
, pre_in
, post_in
, NULL
, NULL
}, /* In */
155 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Li */
156 { cond_head
, pre_enc
, NULL
, "\\- ", NULL
}, /* Nd */
157 { NULL
, pre_nm
, post_nm
, NULL
, NULL
}, /* Nm */
158 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Op */
159 { NULL
, pre_Ft
, post_font
, NULL
, NULL
}, /* Ot */
160 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Pa */
161 { NULL
, pre_ex
, NULL
, NULL
, NULL
}, /* Rv */
162 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* St */
163 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Va */
164 { NULL
, pre_vt
, post_vt
, NULL
, NULL
}, /* Vt */
165 { NULL
, pre_xr
, NULL
, NULL
, NULL
}, /* Xr */
166 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %A */
167 { NULL
, pre_em
, post_percent
, NULL
, NULL
}, /* %B */
168 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %D */
169 { NULL
, pre_em
, post_percent
, NULL
, NULL
}, /* %I */
170 { NULL
, pre_em
, post_percent
, NULL
, NULL
}, /* %J */
171 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %N */
172 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %O */
173 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %P */
174 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %R */
175 { NULL
, pre__t
, post__t
, NULL
, NULL
}, /* %T */
176 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %V */
177 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ac */
178 { cond_body
, pre_aq
, post_aq
, NULL
, NULL
}, /* Ao */
179 { cond_body
, pre_aq
, post_aq
, NULL
, NULL
}, /* Aq */
180 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* At */
181 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bc */
182 { NULL
, pre_bf
, post_bf
, NULL
, NULL
}, /* Bf */
183 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Bo */
184 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Bq */
185 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bsx */
186 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bx */
187 { NULL
, pre_skip
, NULL
, NULL
, NULL
}, /* Db */
188 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dc */
189 { cond_body
, pre_enc
, post_enc
, "\\(Lq", "\\(Rq" }, /* Do */
190 { cond_body
, pre_enc
, post_enc
, "\\(Lq", "\\(Rq" }, /* Dq */
191 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ec */
192 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ef */
193 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Em */
194 { cond_body
, pre_eo
, post_eo
, NULL
, NULL
}, /* Eo */
195 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fx */
196 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Ms */
197 { NULL
, pre_no
, NULL
, NULL
, NULL
}, /* No */
198 { NULL
, pre_ns
, NULL
, NULL
, NULL
}, /* Ns */
199 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Nx */
200 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ox */
201 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Pc */
202 { NULL
, NULL
, post_pf
, NULL
, NULL
}, /* Pf */
203 { cond_body
, pre_enc
, post_enc
, "(", ")" }, /* Po */
204 { cond_body
, pre_enc
, post_enc
, "(", ")" }, /* Pq */
205 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Qc */
206 { cond_body
, pre_enc
, post_enc
, "\\(oq", "\\(cq" }, /* Ql */
207 { cond_body
, pre_enc
, post_enc
, "\"", "\"" }, /* Qo */
208 { cond_body
, pre_enc
, post_enc
, "\"", "\"" }, /* Qq */
209 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Re */
210 { cond_body
, pre_rs
, NULL
, NULL
, NULL
}, /* Rs */
211 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Sc */
212 { cond_body
, pre_enc
, post_enc
, "\\(oq", "\\(cq" }, /* So */
213 { cond_body
, pre_enc
, post_enc
, "\\(oq", "\\(cq" }, /* Sq */
214 { NULL
, pre_sm
, NULL
, NULL
, NULL
}, /* Sm */
215 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Sx */
216 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Sy */
217 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Tn */
218 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ux */
219 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xc */
220 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xo */
221 { NULL
, pre_fo
, post_fo
, NULL
, NULL
}, /* Fo */
222 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fc */
223 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Oo */
224 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Oc */
225 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Bk */
226 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ek */
227 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bt */
228 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Hf */
229 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Fr */
230 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ud */
231 { NULL
, NULL
, post_lb
, NULL
, NULL
}, /* Lb */
232 { NULL
, pre_pp
, NULL
, NULL
, NULL
}, /* Lp */
233 { NULL
, pre_lk
, NULL
, NULL
, NULL
}, /* Lk */
234 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Mt */
235 { cond_body
, pre_enc
, post_enc
, "{", "}" }, /* Brq */
236 { cond_body
, pre_enc
, post_enc
, "{", "}" }, /* Bro */
237 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Brc */
238 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %C */
239 { NULL
, pre_skip
, NULL
, NULL
, NULL
}, /* Es */
240 { cond_body
, pre_en
, post_en
, NULL
, NULL
}, /* En */
241 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dx */
242 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %Q */
243 { NULL
, pre_sp
, post_sp
, NULL
, NULL
}, /* sp */
244 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %U */
245 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ta */
246 { NULL
, pre_ll
, post_sp
, NULL
, NULL
}, /* ll */
248 static const struct manact
*const manacts
= __manacts
- MDOC_Dd
;
251 #define MMAN_spc (1 << 0) /* blank character before next word */
252 #define MMAN_spc_force (1 << 1) /* even before trailing punctuation */
253 #define MMAN_nl (1 << 2) /* break man(7) code line */
254 #define MMAN_br (1 << 3) /* break output line */
255 #define MMAN_sp (1 << 4) /* insert a blank output line */
256 #define MMAN_PP (1 << 5) /* reset indentation etc. */
257 #define MMAN_Sm (1 << 6) /* horizontal spacing mode */
258 #define MMAN_Bk (1 << 7) /* word keep mode */
259 #define MMAN_Bk_susp (1 << 8) /* suspend this (after a macro) */
260 #define MMAN_An_split (1 << 9) /* author mode is "split" */
261 #define MMAN_An_nosplit (1 << 10) /* author mode is "nosplit" */
262 #define MMAN_PD (1 << 11) /* inter-paragraph spacing disabled */
263 #define MMAN_nbrword (1 << 12) /* do not break the next word */
265 #define BL_STACK_MAX 32
267 static int Bl_stack
[BL_STACK_MAX
]; /* offsets [chars] */
268 static int Bl_stack_post
[BL_STACK_MAX
]; /* add final .RE */
269 static int Bl_stack_len
; /* number of nested Bl blocks */
270 static int TPremain
; /* characters before tag is full */
280 man_strlen(const char *cp
)
288 rsz
= strcspn(cp
, "\\");
300 switch (mandoc_escape(&cp
, NULL
, NULL
)) {
304 case ESCAPE_NUMBERED
:
306 case ESCAPE_OVERSTRIKE
:
312 case ESCAPE_SKIPCHAR
:
323 font_push(char newfont
)
326 if (fontqueue
.head
+ fontqueue
.size
<= ++fontqueue
.tail
) {
328 fontqueue
.head
= mandoc_realloc(fontqueue
.head
,
331 *fontqueue
.tail
= newfont
;
335 outflags
&= ~MMAN_spc
;
342 if (fontqueue
.tail
> fontqueue
.head
)
344 outflags
&= ~MMAN_spc
;
347 putchar(*fontqueue
.tail
);
351 print_word(const char *s
)
354 if ((MMAN_PP
| MMAN_sp
| MMAN_br
| MMAN_nl
) & outflags
) {
356 * If we need a newline, print it now and start afresh.
358 if (MMAN_PP
& outflags
) {
359 if (MMAN_sp
& outflags
) {
360 if (MMAN_PD
& outflags
) {
362 outflags
&= ~MMAN_PD
;
364 } else if ( ! (MMAN_PD
& outflags
)) {
369 } else if (MMAN_sp
& outflags
)
371 else if (MMAN_br
& outflags
)
373 else if (MMAN_nl
& outflags
)
375 outflags
&= ~(MMAN_PP
|MMAN_sp
|MMAN_br
|MMAN_nl
|MMAN_spc
);
379 } else if (MMAN_spc
& outflags
) {
381 * If we need a space, only print it if
382 * (1) it is forced by `No' or
383 * (2) what follows is not terminating punctuation or
384 * (3) what follows is longer than one character.
386 if (MMAN_spc_force
& outflags
|| '\0' == s
[0] ||
387 NULL
== strchr(".,:;)]?!", s
[0]) || '\0' != s
[1]) {
388 if (MMAN_Bk
& outflags
&&
389 ! (MMAN_Bk_susp
& outflags
))
398 * Reassign needing space if we're not following opening
401 if (MMAN_Sm
& outflags
&& ('\0' == s
[0] ||
402 (('(' != s
[0] && '[' != s
[0]) || '\0' != s
[1])))
403 outflags
|= MMAN_spc
;
405 outflags
&= ~MMAN_spc
;
406 outflags
&= ~(MMAN_spc_force
| MMAN_Bk_susp
);
420 if (MMAN_nbrword
& outflags
) {
426 putchar((unsigned char)*s
);
432 outflags
&= ~MMAN_nbrword
;
436 print_line(const char *s
, int newflags
)
439 outflags
&= ~MMAN_br
;
442 outflags
|= newflags
;
446 print_block(const char *s
, int newflags
)
449 outflags
&= ~MMAN_PP
;
450 if (MMAN_sp
& outflags
) {
451 outflags
&= ~(MMAN_sp
| MMAN_br
);
452 if (MMAN_PD
& outflags
) {
453 print_line(".PD", 0);
454 outflags
&= ~MMAN_PD
;
456 } else if (! (MMAN_PD
& outflags
))
457 print_line(".PD 0", MMAN_PD
);
460 outflags
|= MMAN_Bk_susp
| newflags
;
464 print_offs(const char *v
, int keywords
)
470 print_line(".RS", MMAN_Bk_susp
);
472 /* Convert v into a number (of characters). */
473 if (NULL
== v
|| '\0' == *v
|| (keywords
&& !strcmp(v
, "left")))
475 else if (keywords
&& !strcmp(v
, "indent"))
477 else if (keywords
&& !strcmp(v
, "indent-two"))
479 else if (a2roffsu(v
, &su
, SCALE_EN
) > 1) {
480 if (SCALE_EN
== su
.unit
)
485 * If we are inside an enclosing list,
486 * there is no easy way to add the two
487 * indentations because they are provided
488 * in terms of different units.
498 * We are inside an enclosing list.
499 * Add the two indentations.
502 sz
+= Bl_stack
[Bl_stack_len
- 1];
504 (void)snprintf(buf
, sizeof(buf
), "%dn", sz
);
510 * Set up the indentation for a list item; used from pre_it().
513 print_width(const struct mdoc_bl
*bl
, const struct roff_node
*child
)
517 int numeric
, remain
, sz
, chsz
;
522 /* Convert the width into a number (of characters). */
523 if (bl
->width
== NULL
)
524 sz
= (bl
->type
== LIST_hang
) ? 6 : 0;
525 else if (a2roffsu(bl
->width
, &su
, SCALE_MAX
) > 1) {
526 if (SCALE_EN
== su
.unit
)
533 sz
= man_strlen(bl
->width
);
535 /* XXX Rough estimation, might have multiple parts. */
536 if (bl
->type
== LIST_enum
)
537 chsz
= (bl
->count
> 8) + 1;
538 else if (child
!= NULL
&& child
->type
== ROFFT_TEXT
)
539 chsz
= man_strlen(child
->string
);
543 /* Maybe we are inside an enclosing list? */
547 * Save our own indentation,
548 * such that child lists can use it.
550 Bl_stack
[Bl_stack_len
++] = sz
+ 2;
552 /* Set up the current list. */
553 if (chsz
> sz
&& bl
->type
!= LIST_tag
)
554 print_block(".HP", 0);
556 print_block(".TP", 0);
560 (void)snprintf(buf
, sizeof(buf
), "%dn", sz
+ 2);
563 print_word(bl
->width
);
568 print_count(int *count
)
572 (void)snprintf(buf
, sizeof(buf
), "%d.\\&", ++*count
);
577 man_man(void *arg
, const struct roff_man
*man
)
581 * Dump the keep buffer.
582 * We're guaranteed by now that this exists (is non-NULL).
583 * Flush stdout afterward, just in case.
585 fputs(mparse_getkeep(man_mparse(man
)), stdout
);
590 man_mdoc(void *arg
, const struct roff_man
*mdoc
)
594 printf(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"\n",
596 (mdoc
->meta
.msec
== NULL
? "" : mdoc
->meta
.msec
),
597 mdoc
->meta
.date
, mdoc
->meta
.os
, mdoc
->meta
.vol
);
599 /* Disable hyphenation and if nroff, disable justification. */
600 printf(".nh\n.if n .ad l");
602 outflags
= MMAN_nl
| MMAN_Sm
;
603 if (0 == fontqueue
.size
) {
605 fontqueue
.head
= fontqueue
.tail
= mandoc_malloc(8);
606 *fontqueue
.tail
= 'R';
608 for (n
= mdoc
->first
->child
; n
!= NULL
; n
= n
->next
)
609 print_node(&mdoc
->meta
, n
);
614 print_node(DECL_ARGS
)
616 const struct manact
*act
;
617 struct roff_node
*sub
;
620 if (n
->flags
& NODE_NOPRT
)
624 * Break the line if we were parsed subsequent the current node.
625 * This makes the page structure be more consistent.
627 if (MMAN_spc
& outflags
&& NODE_LINE
& n
->flags
)
633 n
->flags
&= ~NODE_ENDED
;
635 if (n
->type
== ROFFT_TEXT
) {
637 * Make sure that we don't happen to start with a
638 * control character at the start of a line.
640 if (MMAN_nl
& outflags
&&
641 ('.' == *n
->string
|| '\'' == *n
->string
)) {
644 outflags
&= ~MMAN_spc
;
646 if (n
->flags
& NODE_DELIMC
)
647 outflags
&= ~(MMAN_spc
| MMAN_spc_force
);
648 else if (outflags
& MMAN_Sm
)
649 outflags
|= MMAN_spc_force
;
650 print_word(n
->string
);
651 if (n
->flags
& NODE_DELIMO
)
652 outflags
&= ~(MMAN_spc
| MMAN_spc_force
);
653 else if (outflags
& MMAN_Sm
)
654 outflags
|= MMAN_spc
;
655 } else if (n
->tok
< ROFF_MAX
) {
658 do_sub
= pre_br(meta
, n
);
661 do_sub
= pre_ft(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
)
1075 outflags
|= MMAN_br
;
1083 print_offs("6n", 0);
1091 print_line(".RE", MMAN_nl
);
1093 /* Maybe we are inside an enclosing list? */
1094 if (NULL
!= n
->parent
->next
)
1110 if (NULL
== n
->norm
->Es
||
1111 NULL
== n
->norm
->Es
->child
)
1114 print_word(n
->norm
->Es
->child
->string
);
1115 outflags
&= ~MMAN_spc
;
1123 if (NULL
== n
->norm
->Es
||
1124 NULL
== n
->norm
->Es
->child
||
1125 NULL
== n
->norm
->Es
->child
->next
)
1128 outflags
&= ~MMAN_spc
;
1129 print_word(n
->norm
->Es
->child
->next
->string
);
1137 if (n
->end
== ENDBODY_NOT
&&
1138 n
->parent
->head
->child
== NULL
&&
1140 n
->child
->end
!= ENDBODY_NOT
)
1142 else if (n
->end
!= ENDBODY_NOT
? n
->child
!= NULL
:
1143 n
->parent
->head
->child
!= NULL
&& (n
->child
!= NULL
||
1144 (n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
)))
1145 outflags
&= ~(MMAN_spc
| MMAN_nl
);
1154 if (n
->end
!= ENDBODY_NOT
) {
1155 outflags
|= MMAN_spc
;
1159 body
= n
->child
!= NULL
|| n
->parent
->head
->child
!= NULL
;
1160 tail
= n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
;
1163 outflags
&= ~MMAN_spc
;
1164 else if ( ! (body
|| tail
))
1167 outflags
|= MMAN_spc
;
1175 am_Fa
= MDOC_Fa
== n
->tok
;
1182 if (am_Fa
|| NODE_SYNPRETTY
& n
->flags
)
1183 outflags
|= MMAN_nbrword
;
1184 print_node(meta
, n
);
1186 if (NULL
!= (n
= n
->next
))
1196 if (NULL
!= n
->next
&& MDOC_Fa
== n
->next
->tok
)
1214 outflags
|= MMAN_br
;
1223 if (n
->child
!= NULL
)
1224 outflags
&= ~MMAN_spc
;
1233 if (!(n
->child
!= NULL
||
1235 n
->next
->type
== ROFFT_TEXT
||
1236 n
->next
->flags
& NODE_LINE
))
1237 outflags
&= ~MMAN_spc
;
1250 if (NODE_SYNPRETTY
& n
->flags
)
1251 print_block(".HP 4n", MMAN_nl
);
1254 print_node(meta
, n
);
1256 outflags
&= ~MMAN_spc
;
1258 outflags
&= ~MMAN_spc
;
1271 if (NODE_SYNPRETTY
& n
->flags
) {
1273 outflags
|= MMAN_PP
;
1286 if (n
->child
== NULL
)
1288 if (NODE_SYNPRETTY
& n
->flags
)
1289 print_block(".HP 4n", MMAN_nl
);
1293 outflags
&= ~(MMAN_spc
| MMAN_nl
);
1295 outflags
&= ~MMAN_spc
;
1309 if (n
->child
!= NULL
)
1332 print_line(".ft", 0);
1333 print_word(n
->child
->string
);
1334 outflags
|= MMAN_nl
;
1342 if (NODE_SYNPRETTY
& n
->flags
) {
1345 print_word("#include <");
1346 outflags
&= ~MMAN_spc
;
1349 outflags
&= ~MMAN_spc
;
1359 if (NODE_SYNPRETTY
& n
->flags
) {
1360 outflags
&= ~MMAN_spc
;
1363 outflags
|= MMAN_br
;
1366 outflags
&= ~MMAN_spc
;
1374 const struct roff_node
*bln
;
1378 outflags
|= MMAN_PP
| MMAN_nl
;
1379 bln
= n
->parent
->parent
;
1380 if (0 == bln
->norm
->Bl
.comp
||
1381 (NULL
== n
->parent
->prev
&&
1382 NULL
== bln
->parent
->prev
))
1383 outflags
|= MMAN_sp
;
1384 outflags
&= ~MMAN_br
;
1385 switch (bln
->norm
->Bl
.type
) {
1391 if (bln
->norm
->Bl
.type
== LIST_diag
)
1392 print_line(".B \"", 0);
1394 print_line(".R \"", 0);
1395 outflags
&= ~MMAN_spc
;
1400 print_width(&bln
->norm
->Bl
, NULL
);
1402 outflags
|= MMAN_nl
;
1404 if (LIST_bullet
== bln
->norm
->Bl
.type
)
1405 print_word("\\(bu");
1409 outflags
|= MMAN_nl
;
1412 print_width(&bln
->norm
->Bl
, NULL
);
1414 outflags
|= MMAN_nl
;
1415 print_count(&bln
->norm
->Bl
.count
);
1416 outflags
|= MMAN_nl
;
1419 print_width(&bln
->norm
->Bl
, n
->child
);
1421 outflags
|= MMAN_nl
;
1424 print_width(&bln
->norm
->Bl
, n
->child
);
1426 outflags
&= ~MMAN_spc
;
1438 * This function is called after closing out an indented block.
1439 * If we are inside an enclosing list, restore its indentation.
1446 /* Nothing to do outside a list. */
1447 if (0 == Bl_stack_len
|| 0 == Bl_stack
[Bl_stack_len
- 1])
1450 /* The indentation has already been set up. */
1451 if (Bl_stack_post
[Bl_stack_len
- 1])
1454 /* Restore the indentation of the enclosing list. */
1455 print_line(".RS", MMAN_Bk_susp
);
1456 (void)snprintf(buf
, sizeof(buf
), "%dn",
1457 Bl_stack
[Bl_stack_len
- 1]);
1460 /* Remeber to close out this .RS block later. */
1461 Bl_stack_post
[Bl_stack_len
- 1] = 1;
1467 const struct roff_node
*bln
;
1469 bln
= n
->parent
->parent
;
1473 switch (bln
->norm
->Bl
.type
) {
1475 outflags
&= ~MMAN_spc
;
1479 outflags
|= MMAN_br
;
1486 switch (bln
->norm
->Bl
.type
) {
1493 assert(Bl_stack_len
);
1494 Bl_stack
[--Bl_stack_len
] = 0;
1497 * Our indentation had to be restored
1498 * after a child display or child list.
1499 * Close out that indentation block now.
1501 if (Bl_stack_post
[Bl_stack_len
]) {
1502 print_line(".RE", MMAN_nl
);
1503 Bl_stack_post
[Bl_stack_len
] = 0;
1507 if (NULL
!= n
->next
) {
1509 outflags
&= ~MMAN_spc
;
1525 if (SEC_LIBRARY
== n
->sec
)
1526 outflags
|= MMAN_br
;
1532 const struct roff_node
*link
, *descr
;
1535 if ((link
= n
->child
) == NULL
)
1539 if ((descr
= link
->next
) != NULL
&& !(descr
->flags
& NODE_DELIMC
)) {
1541 while (descr
!= NULL
&& !(descr
->flags
& NODE_DELIMC
)) {
1542 print_word(descr
->string
);
1543 descr
= descr
->next
;
1550 display
= man_strlen(link
->string
) >= 26;
1552 print_line(".RS", MMAN_Bk_susp
);
1554 outflags
|= MMAN_nl
;
1557 print_word(link
->string
);
1560 /* Trailing punctuation. */
1561 while (descr
!= NULL
) {
1562 print_word(descr
->string
);
1563 descr
= descr
->next
;
1566 print_line(".RE", MMAN_nl
);
1574 print_line(".ll", 0);
1591 if (n
->type
== ROFFT_BLOCK
) {
1592 outflags
|= MMAN_Bk
;
1595 if (n
->type
!= ROFFT_ELEM
&& n
->type
!= ROFFT_HEAD
)
1597 name
= n
->child
== NULL
? NULL
: n
->child
->string
;
1600 if (n
->type
== ROFFT_HEAD
) {
1601 if (NULL
== n
->parent
->prev
)
1602 outflags
|= MMAN_sp
;
1603 print_block(".HP", 0);
1604 printf(" %dn", man_strlen(name
) + 1);
1605 outflags
|= MMAN_nl
;
1617 outflags
&= ~MMAN_Bk
;
1621 if (n
->child
!= NULL
&& n
->child
->string
!= NULL
)
1633 outflags
|= MMAN_spc_force
;
1641 outflags
&= ~MMAN_spc
;
1649 if ( ! (n
->next
== NULL
|| n
->next
->flags
& NODE_LINE
))
1650 outflags
&= ~MMAN_spc
;
1657 if (MDOC_It
!= n
->parent
->tok
)
1658 outflags
|= MMAN_PP
;
1659 outflags
|= MMAN_sp
| MMAN_nl
;
1660 outflags
&= ~MMAN_br
;
1668 if (SEC_SEE_ALSO
== n
->sec
) {
1669 outflags
|= MMAN_PP
| MMAN_sp
| MMAN_nl
;
1670 outflags
&= ~MMAN_br
;
1686 if (NULL
== n
->child
)
1687 outflags
^= MMAN_Sm
;
1688 else if (0 == strcmp("on", n
->child
->string
))
1689 outflags
|= MMAN_Sm
;
1691 outflags
&= ~MMAN_Sm
;
1693 if (MMAN_Sm
& outflags
)
1694 outflags
|= MMAN_spc
;
1703 if (MMAN_PP
& outflags
) {
1704 outflags
&= ~MMAN_PP
;
1705 print_line(".PP", 0);
1707 print_line(".sp", 0);
1715 outflags
|= MMAN_nl
;
1730 if (NODE_SYNPRETTY
& n
->flags
) {
1749 if (n
->flags
& NODE_SYNPRETTY
&& n
->type
!= ROFFT_BODY
)
1761 print_node(meta
, n
);
1765 outflags
&= ~MMAN_spc
;
1767 print_node(meta
, n
);