]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_man.c
38fb57e6ea52249176f700bfeee7cb437f236adb
1 /* $Id: mdoc_man.c,v 1.110 2017/05/04 17:48:29 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_in(DECL_ARGS
);
98 static int pre_it(DECL_ARGS
);
99 static int pre_lk(DECL_ARGS
);
100 static int pre_li(DECL_ARGS
);
101 static int pre_ll(DECL_ARGS
);
102 static int pre_nm(DECL_ARGS
);
103 static int pre_no(DECL_ARGS
);
104 static int pre_ns(DECL_ARGS
);
105 static int pre_pp(DECL_ARGS
);
106 static int pre_rs(DECL_ARGS
);
107 static int pre_sm(DECL_ARGS
);
108 static int pre_sp(DECL_ARGS
);
109 static int pre_sect(DECL_ARGS
);
110 static int pre_sy(DECL_ARGS
);
111 static void pre_syn(const struct roff_node
*);
112 static int pre_vt(DECL_ARGS
);
113 static int pre_xr(DECL_ARGS
);
114 static void print_word(const char *);
115 static void print_line(const char *, int);
116 static void print_block(const char *, int);
117 static void print_offs(const char *, int);
118 static void print_width(const struct mdoc_bl
*,
119 const struct roff_node
*);
120 static void print_count(int *);
121 static void print_node(DECL_ARGS
);
123 static const struct manact __manacts
[MDOC_MAX
- MDOC_Dd
] = {
124 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dd */
125 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dt */
126 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Os */
127 { NULL
, pre_sect
, post_sect
, ".SH", NULL
}, /* Sh */
128 { NULL
, pre_sect
, post_sect
, ".SS", NULL
}, /* Ss */
129 { NULL
, pre_pp
, NULL
, NULL
, NULL
}, /* Pp */
130 { cond_body
, pre_dl
, post_dl
, NULL
, NULL
}, /* D1 */
131 { cond_body
, pre_dl
, post_dl
, NULL
, NULL
}, /* Dl */
132 { cond_body
, pre_bd
, post_bd
, NULL
, NULL
}, /* Bd */
133 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ed */
134 { cond_body
, pre_bl
, post_bl
, NULL
, NULL
}, /* Bl */
135 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* El */
136 { NULL
, pre_it
, post_it
, NULL
, NULL
}, /* It */
137 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Ad */
138 { NULL
, pre_an
, NULL
, NULL
, NULL
}, /* An */
139 { NULL
, pre_ap
, NULL
, NULL
, NULL
}, /* Ap */
140 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Ar */
141 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Cd */
142 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Cm */
143 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Dv */
144 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Er */
145 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Ev */
146 { NULL
, pre_ex
, NULL
, NULL
, NULL
}, /* Ex */
147 { NULL
, pre_fa
, post_fa
, NULL
, NULL
}, /* Fa */
148 { NULL
, pre_fd
, post_fd
, NULL
, NULL
}, /* Fd */
149 { NULL
, pre_fl
, post_fl
, NULL
, NULL
}, /* Fl */
150 { NULL
, pre_fn
, post_fn
, NULL
, NULL
}, /* Fn */
151 { NULL
, pre_ft
, post_font
, NULL
, NULL
}, /* Ft */
152 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Ic */
153 { NULL
, pre_in
, post_in
, NULL
, NULL
}, /* In */
154 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Li */
155 { cond_head
, pre_enc
, NULL
, "\\- ", NULL
}, /* Nd */
156 { NULL
, pre_nm
, post_nm
, NULL
, NULL
}, /* Nm */
157 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Op */
158 { NULL
, pre_ft
, post_font
, NULL
, NULL
}, /* Ot */
159 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Pa */
160 { NULL
, pre_ex
, NULL
, NULL
, NULL
}, /* Rv */
161 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* St */
162 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Va */
163 { NULL
, pre_vt
, post_vt
, NULL
, NULL
}, /* Vt */
164 { NULL
, pre_xr
, NULL
, NULL
, NULL
}, /* Xr */
165 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %A */
166 { NULL
, pre_em
, post_percent
, NULL
, NULL
}, /* %B */
167 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %D */
168 { NULL
, pre_em
, post_percent
, NULL
, NULL
}, /* %I */
169 { NULL
, pre_em
, post_percent
, NULL
, NULL
}, /* %J */
170 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %N */
171 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %O */
172 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %P */
173 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %R */
174 { NULL
, pre__t
, post__t
, NULL
, NULL
}, /* %T */
175 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %V */
176 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ac */
177 { cond_body
, pre_aq
, post_aq
, NULL
, NULL
}, /* Ao */
178 { cond_body
, pre_aq
, post_aq
, NULL
, NULL
}, /* Aq */
179 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* At */
180 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bc */
181 { NULL
, pre_bf
, post_bf
, NULL
, NULL
}, /* Bf */
182 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Bo */
183 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Bq */
184 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bsx */
185 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bx */
186 { NULL
, pre_skip
, NULL
, NULL
, NULL
}, /* Db */
187 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dc */
188 { cond_body
, pre_enc
, post_enc
, "\\(Lq", "\\(Rq" }, /* Do */
189 { cond_body
, pre_enc
, post_enc
, "\\(Lq", "\\(Rq" }, /* Dq */
190 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ec */
191 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ef */
192 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Em */
193 { cond_body
, pre_eo
, post_eo
, NULL
, NULL
}, /* Eo */
194 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fx */
195 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Ms */
196 { NULL
, pre_no
, NULL
, NULL
, NULL
}, /* No */
197 { NULL
, pre_ns
, NULL
, NULL
, NULL
}, /* Ns */
198 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Nx */
199 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ox */
200 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Pc */
201 { NULL
, NULL
, post_pf
, NULL
, NULL
}, /* Pf */
202 { cond_body
, pre_enc
, post_enc
, "(", ")" }, /* Po */
203 { cond_body
, pre_enc
, post_enc
, "(", ")" }, /* Pq */
204 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Qc */
205 { cond_body
, pre_enc
, post_enc
, "\\(oq", "\\(cq" }, /* Ql */
206 { cond_body
, pre_enc
, post_enc
, "\"", "\"" }, /* Qo */
207 { cond_body
, pre_enc
, post_enc
, "\"", "\"" }, /* Qq */
208 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Re */
209 { cond_body
, pre_rs
, NULL
, NULL
, NULL
}, /* Rs */
210 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Sc */
211 { cond_body
, pre_enc
, post_enc
, "\\(oq", "\\(cq" }, /* So */
212 { cond_body
, pre_enc
, post_enc
, "\\(oq", "\\(cq" }, /* Sq */
213 { NULL
, pre_sm
, NULL
, NULL
, NULL
}, /* Sm */
214 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Sx */
215 { NULL
, pre_sy
, post_font
, NULL
, NULL
}, /* Sy */
216 { NULL
, pre_li
, post_font
, NULL
, NULL
}, /* Tn */
217 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ux */
218 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xc */
219 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Xo */
220 { NULL
, pre_fo
, post_fo
, NULL
, NULL
}, /* Fo */
221 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Fc */
222 { cond_body
, pre_enc
, post_enc
, "[", "]" }, /* Oo */
223 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Oc */
224 { NULL
, pre_bk
, post_bk
, NULL
, NULL
}, /* Bk */
225 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ek */
226 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Bt */
227 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Hf */
228 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Fr */
229 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ud */
230 { NULL
, NULL
, post_lb
, NULL
, NULL
}, /* Lb */
231 { NULL
, pre_pp
, NULL
, NULL
, NULL
}, /* Lp */
232 { NULL
, pre_lk
, NULL
, NULL
, NULL
}, /* Lk */
233 { NULL
, pre_em
, post_font
, NULL
, NULL
}, /* Mt */
234 { cond_body
, pre_enc
, post_enc
, "{", "}" }, /* Brq */
235 { cond_body
, pre_enc
, post_enc
, "{", "}" }, /* Bro */
236 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Brc */
237 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %C */
238 { NULL
, pre_skip
, NULL
, NULL
, NULL
}, /* Es */
239 { cond_body
, pre_en
, post_en
, NULL
, NULL
}, /* En */
240 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Dx */
241 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %Q */
242 { NULL
, pre_sp
, post_sp
, NULL
, NULL
}, /* sp */
243 { NULL
, NULL
, post_percent
, NULL
, NULL
}, /* %U */
244 { NULL
, NULL
, NULL
, NULL
, NULL
}, /* Ta */
245 { NULL
, pre_ll
, post_sp
, NULL
, NULL
}, /* ll */
247 static const struct manact
*const manacts
= __manacts
- MDOC_Dd
;
250 #define MMAN_spc (1 << 0) /* blank character before next word */
251 #define MMAN_spc_force (1 << 1) /* even before trailing punctuation */
252 #define MMAN_nl (1 << 2) /* break man(7) code line */
253 #define MMAN_br (1 << 3) /* break output line */
254 #define MMAN_sp (1 << 4) /* insert a blank output line */
255 #define MMAN_PP (1 << 5) /* reset indentation etc. */
256 #define MMAN_Sm (1 << 6) /* horizontal spacing mode */
257 #define MMAN_Bk (1 << 7) /* word keep mode */
258 #define MMAN_Bk_susp (1 << 8) /* suspend this (after a macro) */
259 #define MMAN_An_split (1 << 9) /* author mode is "split" */
260 #define MMAN_An_nosplit (1 << 10) /* author mode is "nosplit" */
261 #define MMAN_PD (1 << 11) /* inter-paragraph spacing disabled */
262 #define MMAN_nbrword (1 << 12) /* do not break the next word */
264 #define BL_STACK_MAX 32
266 static int Bl_stack
[BL_STACK_MAX
]; /* offsets [chars] */
267 static int Bl_stack_post
[BL_STACK_MAX
]; /* add final .RE */
268 static int Bl_stack_len
; /* number of nested Bl blocks */
269 static int TPremain
; /* characters before tag is full */
279 man_strlen(const char *cp
)
287 rsz
= strcspn(cp
, "\\");
299 switch (mandoc_escape(&cp
, NULL
, NULL
)) {
303 case ESCAPE_NUMBERED
:
305 case ESCAPE_OVERSTRIKE
:
311 case ESCAPE_SKIPCHAR
:
322 font_push(char newfont
)
325 if (fontqueue
.head
+ fontqueue
.size
<= ++fontqueue
.tail
) {
327 fontqueue
.head
= mandoc_realloc(fontqueue
.head
,
330 *fontqueue
.tail
= newfont
;
334 outflags
&= ~MMAN_spc
;
341 if (fontqueue
.tail
> fontqueue
.head
)
343 outflags
&= ~MMAN_spc
;
346 putchar(*fontqueue
.tail
);
350 print_word(const char *s
)
353 if ((MMAN_PP
| MMAN_sp
| MMAN_br
| MMAN_nl
) & outflags
) {
355 * If we need a newline, print it now and start afresh.
357 if (MMAN_PP
& outflags
) {
358 if (MMAN_sp
& outflags
) {
359 if (MMAN_PD
& outflags
) {
361 outflags
&= ~MMAN_PD
;
363 } else if ( ! (MMAN_PD
& outflags
)) {
368 } else if (MMAN_sp
& outflags
)
370 else if (MMAN_br
& outflags
)
372 else if (MMAN_nl
& outflags
)
374 outflags
&= ~(MMAN_PP
|MMAN_sp
|MMAN_br
|MMAN_nl
|MMAN_spc
);
378 } else if (MMAN_spc
& outflags
) {
380 * If we need a space, only print it if
381 * (1) it is forced by `No' or
382 * (2) what follows is not terminating punctuation or
383 * (3) what follows is longer than one character.
385 if (MMAN_spc_force
& outflags
|| '\0' == s
[0] ||
386 NULL
== strchr(".,:;)]?!", s
[0]) || '\0' != s
[1]) {
387 if (MMAN_Bk
& outflags
&&
388 ! (MMAN_Bk_susp
& outflags
))
397 * Reassign needing space if we're not following opening
400 if (MMAN_Sm
& outflags
&& ('\0' == s
[0] ||
401 (('(' != s
[0] && '[' != s
[0]) || '\0' != s
[1])))
402 outflags
|= MMAN_spc
;
404 outflags
&= ~MMAN_spc
;
405 outflags
&= ~(MMAN_spc_force
| MMAN_Bk_susp
);
419 if (MMAN_nbrword
& outflags
) {
425 putchar((unsigned char)*s
);
431 outflags
&= ~MMAN_nbrword
;
435 print_line(const char *s
, int newflags
)
438 outflags
&= ~MMAN_br
;
441 outflags
|= newflags
;
445 print_block(const char *s
, int newflags
)
448 outflags
&= ~MMAN_PP
;
449 if (MMAN_sp
& outflags
) {
450 outflags
&= ~(MMAN_sp
| MMAN_br
);
451 if (MMAN_PD
& outflags
) {
452 print_line(".PD", 0);
453 outflags
&= ~MMAN_PD
;
455 } else if (! (MMAN_PD
& outflags
))
456 print_line(".PD 0", MMAN_PD
);
459 outflags
|= MMAN_Bk_susp
| newflags
;
463 print_offs(const char *v
, int keywords
)
469 print_line(".RS", MMAN_Bk_susp
);
471 /* Convert v into a number (of characters). */
472 if (NULL
== v
|| '\0' == *v
|| (keywords
&& !strcmp(v
, "left")))
474 else if (keywords
&& !strcmp(v
, "indent"))
476 else if (keywords
&& !strcmp(v
, "indent-two"))
478 else if (a2roffsu(v
, &su
, SCALE_EN
) > 1) {
479 if (SCALE_EN
== su
.unit
)
484 * If we are inside an enclosing list,
485 * there is no easy way to add the two
486 * indentations because they are provided
487 * in terms of different units.
497 * We are inside an enclosing list.
498 * Add the two indentations.
501 sz
+= Bl_stack
[Bl_stack_len
- 1];
503 (void)snprintf(buf
, sizeof(buf
), "%dn", sz
);
509 * Set up the indentation for a list item; used from pre_it().
512 print_width(const struct mdoc_bl
*bl
, const struct roff_node
*child
)
516 int numeric
, remain
, sz
, chsz
;
521 /* Convert the width into a number (of characters). */
522 if (bl
->width
== NULL
)
523 sz
= (bl
->type
== LIST_hang
) ? 6 : 0;
524 else if (a2roffsu(bl
->width
, &su
, SCALE_MAX
) > 1) {
525 if (SCALE_EN
== su
.unit
)
532 sz
= man_strlen(bl
->width
);
534 /* XXX Rough estimation, might have multiple parts. */
535 if (bl
->type
== LIST_enum
)
536 chsz
= (bl
->count
> 8) + 1;
537 else if (child
!= NULL
&& child
->type
== ROFFT_TEXT
)
538 chsz
= man_strlen(child
->string
);
542 /* Maybe we are inside an enclosing list? */
546 * Save our own indentation,
547 * such that child lists can use it.
549 Bl_stack
[Bl_stack_len
++] = sz
+ 2;
551 /* Set up the current list. */
552 if (chsz
> sz
&& bl
->type
!= LIST_tag
)
553 print_block(".HP", 0);
555 print_block(".TP", 0);
559 (void)snprintf(buf
, sizeof(buf
), "%dn", sz
+ 2);
562 print_word(bl
->width
);
567 print_count(int *count
)
571 (void)snprintf(buf
, sizeof(buf
), "%d.\\&", ++*count
);
576 man_man(void *arg
, const struct roff_man
*man
)
580 * Dump the keep buffer.
581 * We're guaranteed by now that this exists (is non-NULL).
582 * Flush stdout afterward, just in case.
584 fputs(mparse_getkeep(man_mparse(man
)), stdout
);
589 man_mdoc(void *arg
, const struct roff_man
*mdoc
)
593 printf(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"\n",
595 (mdoc
->meta
.msec
== NULL
? "" : mdoc
->meta
.msec
),
596 mdoc
->meta
.date
, mdoc
->meta
.os
, mdoc
->meta
.vol
);
598 /* Disable hyphenation and if nroff, disable justification. */
599 printf(".nh\n.if n .ad l");
601 outflags
= MMAN_nl
| MMAN_Sm
;
602 if (0 == fontqueue
.size
) {
604 fontqueue
.head
= fontqueue
.tail
= mandoc_malloc(8);
605 *fontqueue
.tail
= 'R';
607 for (n
= mdoc
->first
->child
; n
!= NULL
; n
= n
->next
)
608 print_node(&mdoc
->meta
, n
);
613 print_node(DECL_ARGS
)
615 const struct manact
*act
;
616 struct roff_node
*sub
;
619 if (n
->flags
& NODE_NOPRT
)
623 * Break the line if we were parsed subsequent the current node.
624 * This makes the page structure be more consistent.
626 if (MMAN_spc
& outflags
&& NODE_LINE
& n
->flags
)
632 n
->flags
&= ~NODE_ENDED
;
634 if (n
->type
== ROFFT_TEXT
) {
636 * Make sure that we don't happen to start with a
637 * control character at the start of a line.
639 if (MMAN_nl
& outflags
&&
640 ('.' == *n
->string
|| '\'' == *n
->string
)) {
643 outflags
&= ~MMAN_spc
;
645 if (n
->flags
& NODE_DELIMC
)
646 outflags
&= ~(MMAN_spc
| MMAN_spc_force
);
647 else if (outflags
& MMAN_Sm
)
648 outflags
|= MMAN_spc_force
;
649 print_word(n
->string
);
650 if (n
->flags
& NODE_DELIMO
)
651 outflags
&= ~(MMAN_spc
| MMAN_spc_force
);
652 else if (outflags
& MMAN_Sm
)
653 outflags
|= MMAN_spc
;
654 } else if (n
->tok
< ROFF_MAX
) {
663 assert(n
->tok
>= MDOC_Dd
&& n
->tok
< MDOC_MAX
);
665 * Conditionally run the pre-node action handler for a
668 act
= manacts
+ n
->tok
;
669 cond
= act
->cond
== NULL
|| (*act
->cond
)(meta
, n
);
670 if (cond
&& act
->pre
!= NULL
&&
671 (n
->end
== ENDBODY_NOT
|| n
->child
!= NULL
))
672 do_sub
= (*act
->pre
)(meta
, n
);
676 * Conditionally run all child nodes.
677 * Note that this iterates over children instead of using
678 * recursion. This prevents unnecessary depth in the stack.
681 for (sub
= n
->child
; sub
; sub
= sub
->next
)
682 print_node(meta
, sub
);
685 * Lastly, conditionally run the post-node handler.
687 if (NODE_ENDED
& n
->flags
)
690 if (cond
&& act
->post
)
691 (*act
->post
)(meta
, n
);
693 if (ENDBODY_NOT
!= n
->end
)
694 n
->body
->flags
|= NODE_ENDED
;
701 return n
->type
== ROFFT_HEAD
;
708 return n
->type
== ROFFT_BODY
;
716 prefix
= manacts
[n
->tok
].prefix
;
720 outflags
&= ~MMAN_spc
;
729 suffix
= manacts
[n
->tok
].suffix
;
732 outflags
&= ~(MMAN_spc
| MMAN_nl
);
739 outflags
|= MMAN_br
| MMAN_nl
;
751 post_percent(DECL_ARGS
)
754 if (pre_em
== manacts
[n
->tok
].pre
)
758 if (n
->prev
&& n
->prev
->tok
== n
->tok
&&
759 n
->next
->tok
== n
->tok
)
771 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
) {
773 outflags
&= ~MMAN_spc
;
783 if (n
->parent
->tok
== MDOC_Rs
&& n
->parent
->norm
->Rs
.quote_T
) {
784 outflags
&= ~MMAN_spc
;
788 post_percent(meta
, n
);
792 * Print before a section header.
798 if (n
->type
== ROFFT_HEAD
) {
800 print_block(manacts
[n
->tok
].prefix
, 0);
803 outflags
&= ~MMAN_spc
;
809 * Print subsequent a section header.
815 if (n
->type
!= ROFFT_HEAD
)
817 outflags
&= ~MMAN_spc
;
821 if (MDOC_Sh
== n
->tok
&& SEC_AUTHORS
== n
->sec
)
822 outflags
&= ~(MMAN_An_split
| MMAN_An_nosplit
);
825 /* See mdoc_term.c, synopsis_pre() for comments. */
827 pre_syn(const struct roff_node
*n
)
830 if (NULL
== n
->prev
|| ! (NODE_SYNPRETTY
& n
->flags
))
833 if (n
->prev
->tok
== n
->tok
&&
841 switch (n
->prev
->tok
) {
850 if (MDOC_Fn
!= n
->tok
&& MDOC_Fo
!= n
->tok
) {
865 switch (n
->norm
->An
.auth
) {
867 outflags
&= ~MMAN_An_nosplit
;
868 outflags
|= MMAN_An_split
;
871 outflags
&= ~MMAN_An_split
;
872 outflags
|= MMAN_An_nosplit
;
875 if (MMAN_An_split
& outflags
)
877 else if (SEC_AUTHORS
== n
->sec
&&
878 ! (MMAN_An_nosplit
& outflags
))
879 outflags
|= MMAN_An_split
;
888 outflags
&= ~MMAN_spc
;
890 outflags
&= ~MMAN_spc
;
898 print_word(n
->child
!= NULL
&& n
->child
->next
== NULL
&&
899 n
->child
->tok
== MDOC_Mt
? "<" : "\\(la");
900 outflags
&= ~MMAN_spc
;
908 outflags
&= ~(MMAN_spc
| MMAN_nl
);
909 print_word(n
->child
!= NULL
&& n
->child
->next
== NULL
&&
910 n
->child
->tok
== MDOC_Mt
? ">" : "\\(ra");
917 outflags
&= ~(MMAN_PP
| MMAN_sp
| MMAN_br
);
919 if (DISP_unfilled
== n
->norm
->Bd
.type
||
920 DISP_literal
== n
->norm
->Bd
.type
)
921 print_line(".nf", 0);
922 if (0 == n
->norm
->Bd
.comp
&& NULL
!= n
->parent
->prev
)
924 print_offs(n
->norm
->Bd
.offs
, 1);
932 /* Close out this display. */
933 print_line(".RE", MMAN_nl
);
934 if (DISP_unfilled
== n
->norm
->Bd
.type
||
935 DISP_literal
== n
->norm
->Bd
.type
)
936 print_line(".fi", MMAN_nl
);
938 /* Maybe we are inside an enclosing list? */
939 if (NULL
!= n
->parent
->next
)
955 switch (n
->norm
->Bf
.font
) {
973 if (n
->type
== ROFFT_BODY
)
996 if (n
->type
== ROFFT_BODY
)
997 outflags
&= ~MMAN_Bk
;
1006 * print_offs() will increase the -offset to account for
1007 * a possible enclosing .It, but any enclosed .It blocks
1008 * just nest and do not add up their indentation.
1010 if (n
->norm
->Bl
.offs
) {
1011 print_offs(n
->norm
->Bl
.offs
, 0);
1012 Bl_stack
[Bl_stack_len
++] = 0;
1015 switch (n
->norm
->Bl
.type
) {
1017 n
->norm
->Bl
.count
= 0;
1025 if (n
->child
!= NULL
) {
1026 print_line(".TS", MMAN_nl
);
1027 for (icol
= 0; icol
< n
->norm
->Bl
.ncols
; icol
++)
1031 outflags
|= MMAN_nl
;
1039 switch (n
->norm
->Bl
.type
) {
1041 if (n
->child
!= NULL
)
1042 print_line(".TE", 0);
1045 n
->norm
->Bl
.count
= 0;
1051 if (n
->norm
->Bl
.offs
) {
1052 print_line(".RE", MMAN_nl
);
1053 assert(Bl_stack_len
);
1055 assert(0 == Bl_stack
[Bl_stack_len
]);
1057 outflags
|= MMAN_PP
| MMAN_nl
;
1058 outflags
&= ~(MMAN_sp
| MMAN_br
);
1061 /* Maybe we are inside an enclosing list? */
1062 if (NULL
!= n
->parent
->next
)
1071 outflags
|= MMAN_br
;
1079 print_offs("6n", 0);
1087 print_line(".RE", MMAN_nl
);
1089 /* Maybe we are inside an enclosing list? */
1090 if (NULL
!= n
->parent
->next
)
1106 if (NULL
== n
->norm
->Es
||
1107 NULL
== n
->norm
->Es
->child
)
1110 print_word(n
->norm
->Es
->child
->string
);
1111 outflags
&= ~MMAN_spc
;
1119 if (NULL
== n
->norm
->Es
||
1120 NULL
== n
->norm
->Es
->child
||
1121 NULL
== n
->norm
->Es
->child
->next
)
1124 outflags
&= ~MMAN_spc
;
1125 print_word(n
->norm
->Es
->child
->next
->string
);
1133 if (n
->end
== ENDBODY_NOT
&&
1134 n
->parent
->head
->child
== NULL
&&
1136 n
->child
->end
!= ENDBODY_NOT
)
1138 else if (n
->end
!= ENDBODY_NOT
? n
->child
!= NULL
:
1139 n
->parent
->head
->child
!= NULL
&& (n
->child
!= NULL
||
1140 (n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
)))
1141 outflags
&= ~(MMAN_spc
| MMAN_nl
);
1150 if (n
->end
!= ENDBODY_NOT
) {
1151 outflags
|= MMAN_spc
;
1155 body
= n
->child
!= NULL
|| n
->parent
->head
->child
!= NULL
;
1156 tail
= n
->parent
->tail
!= NULL
&& n
->parent
->tail
->child
!= NULL
;
1159 outflags
&= ~MMAN_spc
;
1160 else if ( ! (body
|| tail
))
1163 outflags
|= MMAN_spc
;
1171 am_Fa
= MDOC_Fa
== n
->tok
;
1178 if (am_Fa
|| NODE_SYNPRETTY
& n
->flags
)
1179 outflags
|= MMAN_nbrword
;
1180 print_node(meta
, n
);
1182 if (NULL
!= (n
= n
->next
))
1192 if (NULL
!= n
->next
&& MDOC_Fa
== n
->next
->tok
)
1210 outflags
|= MMAN_br
;
1219 if (n
->child
!= NULL
)
1220 outflags
&= ~MMAN_spc
;
1229 if (!(n
->child
!= NULL
||
1231 n
->next
->type
== ROFFT_TEXT
||
1232 n
->next
->flags
& NODE_LINE
))
1233 outflags
&= ~MMAN_spc
;
1246 if (NODE_SYNPRETTY
& n
->flags
)
1247 print_block(".HP 4n", MMAN_nl
);
1250 print_node(meta
, n
);
1252 outflags
&= ~MMAN_spc
;
1254 outflags
&= ~MMAN_spc
;
1267 if (NODE_SYNPRETTY
& n
->flags
) {
1269 outflags
|= MMAN_PP
;
1282 if (n
->child
== NULL
)
1284 if (NODE_SYNPRETTY
& n
->flags
)
1285 print_block(".HP 4n", MMAN_nl
);
1289 outflags
&= ~(MMAN_spc
| MMAN_nl
);
1291 outflags
&= ~MMAN_spc
;
1305 if (n
->child
!= NULL
)
1329 if (NODE_SYNPRETTY
& n
->flags
) {
1332 print_word("#include <");
1333 outflags
&= ~MMAN_spc
;
1336 outflags
&= ~MMAN_spc
;
1346 if (NODE_SYNPRETTY
& n
->flags
) {
1347 outflags
&= ~MMAN_spc
;
1350 outflags
|= MMAN_br
;
1353 outflags
&= ~MMAN_spc
;
1361 const struct roff_node
*bln
;
1365 outflags
|= MMAN_PP
| MMAN_nl
;
1366 bln
= n
->parent
->parent
;
1367 if (0 == bln
->norm
->Bl
.comp
||
1368 (NULL
== n
->parent
->prev
&&
1369 NULL
== bln
->parent
->prev
))
1370 outflags
|= MMAN_sp
;
1371 outflags
&= ~MMAN_br
;
1372 switch (bln
->norm
->Bl
.type
) {
1378 if (bln
->norm
->Bl
.type
== LIST_diag
)
1379 print_line(".B \"", 0);
1381 print_line(".R \"", 0);
1382 outflags
&= ~MMAN_spc
;
1387 print_width(&bln
->norm
->Bl
, NULL
);
1389 outflags
|= MMAN_nl
;
1391 if (LIST_bullet
== bln
->norm
->Bl
.type
)
1392 print_word("\\(bu");
1396 outflags
|= MMAN_nl
;
1399 print_width(&bln
->norm
->Bl
, NULL
);
1401 outflags
|= MMAN_nl
;
1402 print_count(&bln
->norm
->Bl
.count
);
1403 outflags
|= MMAN_nl
;
1406 print_width(&bln
->norm
->Bl
, n
->child
);
1408 outflags
|= MMAN_nl
;
1411 print_width(&bln
->norm
->Bl
, n
->child
);
1413 outflags
&= ~MMAN_spc
;
1425 * This function is called after closing out an indented block.
1426 * If we are inside an enclosing list, restore its indentation.
1433 /* Nothing to do outside a list. */
1434 if (0 == Bl_stack_len
|| 0 == Bl_stack
[Bl_stack_len
- 1])
1437 /* The indentation has already been set up. */
1438 if (Bl_stack_post
[Bl_stack_len
- 1])
1441 /* Restore the indentation of the enclosing list. */
1442 print_line(".RS", MMAN_Bk_susp
);
1443 (void)snprintf(buf
, sizeof(buf
), "%dn",
1444 Bl_stack
[Bl_stack_len
- 1]);
1447 /* Remeber to close out this .RS block later. */
1448 Bl_stack_post
[Bl_stack_len
- 1] = 1;
1454 const struct roff_node
*bln
;
1456 bln
= n
->parent
->parent
;
1460 switch (bln
->norm
->Bl
.type
) {
1462 outflags
&= ~MMAN_spc
;
1466 outflags
|= MMAN_br
;
1473 switch (bln
->norm
->Bl
.type
) {
1480 assert(Bl_stack_len
);
1481 Bl_stack
[--Bl_stack_len
] = 0;
1484 * Our indentation had to be restored
1485 * after a child display or child list.
1486 * Close out that indentation block now.
1488 if (Bl_stack_post
[Bl_stack_len
]) {
1489 print_line(".RE", MMAN_nl
);
1490 Bl_stack_post
[Bl_stack_len
] = 0;
1494 if (NULL
!= n
->next
) {
1496 outflags
&= ~MMAN_spc
;
1512 if (SEC_LIBRARY
== n
->sec
)
1513 outflags
|= MMAN_br
;
1519 const struct roff_node
*link
, *descr
;
1522 if ((link
= n
->child
) == NULL
)
1526 if ((descr
= link
->next
) != NULL
&& !(descr
->flags
& NODE_DELIMC
)) {
1528 while (descr
!= NULL
&& !(descr
->flags
& NODE_DELIMC
)) {
1529 print_word(descr
->string
);
1530 descr
= descr
->next
;
1537 display
= man_strlen(link
->string
) >= 26;
1539 print_line(".RS", MMAN_Bk_susp
);
1541 outflags
|= MMAN_nl
;
1544 print_word(link
->string
);
1547 /* Trailing punctuation. */
1548 while (descr
!= NULL
) {
1549 print_word(descr
->string
);
1550 descr
= descr
->next
;
1553 print_line(".RE", MMAN_nl
);
1561 print_line(".ll", 0);
1578 if (n
->type
== ROFFT_BLOCK
) {
1579 outflags
|= MMAN_Bk
;
1582 if (n
->type
!= ROFFT_ELEM
&& n
->type
!= ROFFT_HEAD
)
1584 name
= n
->child
== NULL
? NULL
: n
->child
->string
;
1587 if (n
->type
== ROFFT_HEAD
) {
1588 if (NULL
== n
->parent
->prev
)
1589 outflags
|= MMAN_sp
;
1590 print_block(".HP", 0);
1591 printf(" %dn", man_strlen(name
) + 1);
1592 outflags
|= MMAN_nl
;
1604 outflags
&= ~MMAN_Bk
;
1608 if (n
->child
!= NULL
&& n
->child
->string
!= NULL
)
1620 outflags
|= MMAN_spc_force
;
1628 outflags
&= ~MMAN_spc
;
1636 if ( ! (n
->next
== NULL
|| n
->next
->flags
& NODE_LINE
))
1637 outflags
&= ~MMAN_spc
;
1644 if (MDOC_It
!= n
->parent
->tok
)
1645 outflags
|= MMAN_PP
;
1646 outflags
|= MMAN_sp
| MMAN_nl
;
1647 outflags
&= ~MMAN_br
;
1655 if (SEC_SEE_ALSO
== n
->sec
) {
1656 outflags
|= MMAN_PP
| MMAN_sp
| MMAN_nl
;
1657 outflags
&= ~MMAN_br
;
1673 if (NULL
== n
->child
)
1674 outflags
^= MMAN_Sm
;
1675 else if (0 == strcmp("on", n
->child
->string
))
1676 outflags
|= MMAN_Sm
;
1678 outflags
&= ~MMAN_Sm
;
1680 if (MMAN_Sm
& outflags
)
1681 outflags
|= MMAN_spc
;
1690 if (MMAN_PP
& outflags
) {
1691 outflags
&= ~MMAN_PP
;
1692 print_line(".PP", 0);
1694 print_line(".sp", 0);
1702 outflags
|= MMAN_nl
;
1717 if (NODE_SYNPRETTY
& n
->flags
) {
1736 if (n
->flags
& NODE_SYNPRETTY
&& n
->type
!= ROFFT_BODY
)
1748 print_node(meta
, n
);
1752 outflags
&= ~MMAN_spc
;
1754 print_node(meta
, n
);