1 /* $Id: mdoc_man.c,v 1.101 2017/01/11 17:39:53 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>
25 #include "mandoc_aux.h"
33 #define DECL_ARGS const struct roff_meta *meta, struct roff_node *n
36 int (*cond)(DECL_ARGS); /* DON'T run actions */
37 int (*pre)(DECL_ARGS); /* pre-node action */
38 void (*post)(DECL_ARGS); /* post-node action */
39 const char *prefix; /* pre-node string constant */
40 const char *suffix; /* post-node string constant */
43 static int cond_body(DECL_ARGS);
44 static int cond_head(DECL_ARGS);
45 static void font_push(char);
46 static void font_pop(void);
47 static void mid_it(void);
48 static void post__t(DECL_ARGS);
49 static void post_aq(DECL_ARGS);
50 static void post_bd(DECL_ARGS);
51 static void post_bf(DECL_ARGS);
52 static void post_bk(DECL_ARGS);
53 static void post_bl(DECL_ARGS);
54 static void post_dl(DECL_ARGS);
55 static void post_en(DECL_ARGS);
56 static void post_enc(DECL_ARGS);
57 static void post_eo(DECL_ARGS);
58 static void post_fa(DECL_ARGS);
59 static void post_fd(DECL_ARGS);
60 static void post_fl(DECL_ARGS);
61 static void post_fn(DECL_ARGS);
62 static void post_fo(DECL_ARGS);
63 static void post_font(DECL_ARGS);
64 static void post_in(DECL_ARGS);
65 static void post_it(DECL_ARGS);
66 static void post_lb(DECL_ARGS);
67 static void post_nm(DECL_ARGS);
68 static void post_percent(DECL_ARGS);
69 static void post_pf(DECL_ARGS);
70 static void post_sect(DECL_ARGS);
71 static void post_sp(DECL_ARGS);
72 static void post_vt(DECL_ARGS);
73 static int pre__t(DECL_ARGS);
74 static int pre_an(DECL_ARGS);
75 static int pre_ap(DECL_ARGS);
76 static int pre_aq(DECL_ARGS);
77 static int pre_bd(DECL_ARGS);
78 static int pre_bf(DECL_ARGS);
79 static int pre_bk(DECL_ARGS);
80 static int pre_bl(DECL_ARGS);
81 static int pre_br(DECL_ARGS);
82 static int pre_dl(DECL_ARGS);
83 static int pre_en(DECL_ARGS);
84 static int pre_enc(DECL_ARGS);
85 static int pre_em(DECL_ARGS);
86 static int pre_skip(DECL_ARGS);
87 static int pre_eo(DECL_ARGS);
88 static int pre_ex(DECL_ARGS);
89 static int pre_fa(DECL_ARGS);
90 static int pre_fd(DECL_ARGS);
91 static int pre_fl(DECL_ARGS);
92 static int pre_fn(DECL_ARGS);
93 static int pre_fo(DECL_ARGS);
94 static int pre_ft(DECL_ARGS);
95 static int pre_in(DECL_ARGS);
96 static int pre_it(DECL_ARGS);
97 static int pre_lk(DECL_ARGS);
98 static int pre_li(DECL_ARGS);
99 static int pre_ll(DECL_ARGS);
100 static int pre_nm(DECL_ARGS);
101 static int pre_no(DECL_ARGS);
102 static int pre_ns(DECL_ARGS);
103 static int pre_pp(DECL_ARGS);
104 static int pre_rs(DECL_ARGS);
105 static int pre_sm(DECL_ARGS);
106 static int pre_sp(DECL_ARGS);
107 static int pre_sect(DECL_ARGS);
108 static int pre_sy(DECL_ARGS);
109 static void pre_syn(const struct roff_node *);
110 static int pre_vt(DECL_ARGS);
111 static int pre_xr(DECL_ARGS);
112 static void print_word(const char *);
113 static void print_line(const char *, int);
114 static void print_block(const char *, int);
115 static void print_offs(const char *, int);
116 static void print_width(const struct mdoc_bl *,
117 const struct roff_node *);
118 static void print_count(int *);
119 static void print_node(DECL_ARGS);
121 static const struct manact manacts[MDOC_MAX + 1] = {
122 { NULL, pre_ap, NULL, NULL, NULL }, /* Ap */
123 { NULL, NULL, NULL, NULL, NULL }, /* Dd */
124 { NULL, NULL, NULL, NULL, NULL }, /* Dt */
125 { NULL, NULL, NULL, NULL, NULL }, /* Os */
126 { NULL, pre_sect, post_sect, ".SH", NULL }, /* Sh */
127 { NULL, pre_sect, post_sect, ".SS", NULL }, /* Ss */
128 { NULL, pre_pp, NULL, NULL, NULL }, /* Pp */
129 { cond_body, pre_dl, post_dl, NULL, NULL }, /* D1 */
130 { cond_body, pre_dl, post_dl, NULL, NULL }, /* Dl */
131 { cond_body, pre_bd, post_bd, NULL, NULL }, /* Bd */
132 { NULL, NULL, NULL, NULL, NULL }, /* Ed */
133 { cond_body, pre_bl, post_bl, NULL, NULL }, /* Bl */
134 { NULL, NULL, NULL, NULL, NULL }, /* El */
135 { NULL, pre_it, post_it, NULL, NULL }, /* It */
136 { NULL, pre_em, post_font, NULL, NULL }, /* Ad */
137 { NULL, pre_an, NULL, NULL, NULL }, /* An */
138 { NULL, pre_em, post_font, NULL, NULL }, /* Ar */
139 { NULL, pre_sy, post_font, NULL, NULL }, /* Cd */
140 { NULL, pre_sy, post_font, NULL, NULL }, /* Cm */
141 { NULL, pre_li, post_font, NULL, NULL }, /* Dv */
142 { NULL, pre_li, post_font, NULL, NULL }, /* Er */
143 { NULL, pre_li, post_font, NULL, NULL }, /* Ev */
144 { NULL, pre_ex, NULL, NULL, NULL }, /* Ex */
145 { NULL, pre_fa, post_fa, NULL, NULL }, /* Fa */
146 { NULL, pre_fd, post_fd, NULL, NULL }, /* Fd */
147 { NULL, pre_fl, post_fl, NULL, NULL }, /* Fl */
148 { NULL, pre_fn, post_fn, NULL, NULL }, /* Fn */
149 { NULL, pre_ft, post_font, NULL, NULL }, /* Ft */
150 { NULL, pre_sy, post_font, NULL, NULL }, /* Ic */
151 { NULL, pre_in, post_in, NULL, NULL }, /* In */
152 { NULL, pre_li, post_font, NULL, NULL }, /* Li */
153 { cond_head, pre_enc, NULL, "\\- ", NULL }, /* Nd */
154 { NULL, pre_nm, post_nm, NULL, NULL }, /* Nm */
155 { cond_body, pre_enc, post_enc, "[", "]" }, /* Op */
156 { NULL, pre_ft, post_font, NULL, NULL }, /* Ot */
157 { NULL, pre_em, post_font, NULL, NULL }, /* Pa */
158 { NULL, pre_ex, NULL, NULL, NULL }, /* Rv */
159 { NULL, NULL, NULL, NULL, NULL }, /* St */
160 { NULL, pre_em, post_font, NULL, NULL }, /* Va */
161 { NULL, pre_vt, post_vt, NULL, NULL }, /* Vt */
162 { NULL, pre_xr, NULL, NULL, NULL }, /* Xr */
163 { NULL, NULL, post_percent, NULL, NULL }, /* %A */
164 { NULL, pre_em, post_percent, NULL, NULL }, /* %B */
165 { NULL, NULL, post_percent, NULL, NULL }, /* %D */
166 { NULL, pre_em, post_percent, NULL, NULL }, /* %I */
167 { NULL, pre_em, post_percent, NULL, NULL }, /* %J */
168 { NULL, NULL, post_percent, NULL, NULL }, /* %N */
169 { NULL, NULL, post_percent, NULL, NULL }, /* %O */
170 { NULL, NULL, post_percent, NULL, NULL }, /* %P */
171 { NULL, NULL, post_percent, NULL, NULL }, /* %R */
172 { NULL, pre__t, post__t, NULL, NULL }, /* %T */
173 { NULL, NULL, post_percent, NULL, NULL }, /* %V */
174 { NULL, NULL, NULL, NULL, NULL }, /* Ac */
175 { cond_body, pre_aq, post_aq, NULL, NULL }, /* Ao */
176 { cond_body, pre_aq, post_aq, NULL, NULL }, /* Aq */
177 { NULL, NULL, NULL, NULL, NULL }, /* At */
178 { NULL, NULL, NULL, NULL, NULL }, /* Bc */
179 { NULL, pre_bf, post_bf, NULL, NULL }, /* Bf */
180 { cond_body, pre_enc, post_enc, "[", "]" }, /* Bo */
181 { cond_body, pre_enc, post_enc, "[", "]" }, /* Bq */
182 { NULL, NULL, NULL, NULL, NULL }, /* Bsx */
183 { NULL, NULL, NULL, NULL, NULL }, /* Bx */
184 { NULL, pre_skip, NULL, NULL, NULL }, /* Db */
185 { NULL, NULL, NULL, NULL, NULL }, /* Dc */
186 { cond_body, pre_enc, post_enc, "\\(Lq", "\\(Rq" }, /* Do */
187 { cond_body, pre_enc, post_enc, "\\(Lq", "\\(Rq" }, /* Dq */
188 { NULL, NULL, NULL, NULL, NULL }, /* Ec */
189 { NULL, NULL, NULL, NULL, NULL }, /* Ef */
190 { NULL, pre_em, post_font, NULL, NULL }, /* Em */
191 { cond_body, pre_eo, post_eo, NULL, NULL }, /* Eo */
192 { NULL, NULL, NULL, NULL, NULL }, /* Fx */
193 { NULL, pre_sy, post_font, NULL, NULL }, /* Ms */
194 { NULL, pre_no, NULL, NULL, NULL }, /* No */
195 { NULL, pre_ns, NULL, NULL, NULL }, /* Ns */
196 { NULL, NULL, NULL, NULL, NULL }, /* Nx */
197 { NULL, NULL, NULL, NULL, NULL }, /* Ox */
198 { NULL, NULL, NULL, NULL, NULL }, /* Pc */
199 { NULL, NULL, post_pf, NULL, NULL }, /* Pf */
200 { cond_body, pre_enc, post_enc, "(", ")" }, /* Po */
201 { cond_body, pre_enc, post_enc, "(", ")" }, /* Pq */
202 { NULL, NULL, NULL, NULL, NULL }, /* Qc */
203 { cond_body, pre_enc, post_enc, "\\(oq", "\\(cq" }, /* Ql */
204 { cond_body, pre_enc, post_enc, "\"", "\"" }, /* Qo */
205 { cond_body, pre_enc, post_enc, "\"", "\"" }, /* Qq */
206 { NULL, NULL, NULL, NULL, NULL }, /* Re */
207 { cond_body, pre_rs, NULL, NULL, NULL }, /* Rs */
208 { NULL, NULL, NULL, NULL, NULL }, /* Sc */
209 { cond_body, pre_enc, post_enc, "\\(oq", "\\(cq" }, /* So */
210 { cond_body, pre_enc, post_enc, "\\(oq", "\\(cq" }, /* Sq */
211 { NULL, pre_sm, NULL, NULL, NULL }, /* Sm */
212 { NULL, pre_em, post_font, NULL, NULL }, /* Sx */
213 { NULL, pre_sy, post_font, NULL, NULL }, /* Sy */
214 { NULL, pre_li, post_font, NULL, NULL }, /* Tn */
215 { NULL, NULL, NULL, NULL, NULL }, /* Ux */
216 { NULL, NULL, NULL, NULL, NULL }, /* Xc */
217 { NULL, NULL, NULL, NULL, NULL }, /* Xo */
218 { NULL, pre_fo, post_fo, NULL, NULL }, /* Fo */
219 { NULL, NULL, NULL, NULL, NULL }, /* Fc */
220 { cond_body, pre_enc, post_enc, "[", "]" }, /* Oo */
221 { NULL, NULL, NULL, NULL, NULL }, /* Oc */
222 { NULL, pre_bk, post_bk, NULL, NULL }, /* Bk */
223 { NULL, NULL, NULL, NULL, NULL }, /* Ek */
224 { NULL, NULL, NULL, NULL, NULL }, /* Bt */
225 { NULL, NULL, NULL, NULL, NULL }, /* Hf */
226 { NULL, pre_em, post_font, NULL, NULL }, /* Fr */
227 { NULL, NULL, NULL, NULL, NULL }, /* Ud */
228 { NULL, NULL, post_lb, NULL, NULL }, /* Lb */
229 { NULL, pre_pp, NULL, NULL, NULL }, /* Lp */
230 { NULL, pre_lk, NULL, NULL, NULL }, /* Lk */
231 { NULL, pre_em, post_font, NULL, NULL }, /* Mt */
232 { cond_body, pre_enc, post_enc, "{", "}" }, /* Brq */
233 { cond_body, pre_enc, post_enc, "{", "}" }, /* Bro */
234 { NULL, NULL, NULL, NULL, NULL }, /* Brc */
235 { NULL, NULL, post_percent, NULL, NULL }, /* %C */
236 { NULL, pre_skip, NULL, NULL, NULL }, /* Es */
237 { cond_body, pre_en, post_en, NULL, NULL }, /* En */
238 { NULL, NULL, NULL, NULL, NULL }, /* Dx */
239 { NULL, NULL, post_percent, NULL, NULL }, /* %Q */
240 { NULL, pre_br, NULL, NULL, NULL }, /* br */
241 { NULL, pre_sp, post_sp, NULL, NULL }, /* sp */
242 { NULL, NULL, post_percent, NULL, NULL }, /* %U */
243 { NULL, NULL, NULL, NULL, NULL }, /* Ta */
244 { NULL, pre_ll, post_sp, NULL, NULL }, /* ll */
245 { NULL, NULL, NULL, NULL, NULL }, /* ROOT */
249 #define MMAN_spc (1 << 0) /* blank character before next word */
250 #define MMAN_spc_force (1 << 1) /* even before trailing punctuation */
251 #define MMAN_nl (1 << 2) /* break man(7) code line */
252 #define MMAN_br (1 << 3) /* break output line */
253 #define MMAN_sp (1 << 4) /* insert a blank output line */
254 #define MMAN_PP (1 << 5) /* reset indentation etc. */
255 #define MMAN_Sm (1 << 6) /* horizontal spacing mode */
256 #define MMAN_Bk (1 << 7) /* word keep mode */
257 #define MMAN_Bk_susp (1 << 8) /* suspend this (after a macro) */
258 #define MMAN_An_split (1 << 9) /* author mode is "split" */
259 #define MMAN_An_nosplit (1 << 10) /* author mode is "nosplit" */
260 #define MMAN_PD (1 << 11) /* inter-paragraph spacing disabled */
261 #define MMAN_nbrword (1 << 12) /* do not break the next word */
263 #define BL_STACK_MAX 32
265 static int Bl_stack[BL_STACK_MAX]; /* offsets [chars] */
266 static int Bl_stack_post[BL_STACK_MAX]; /* add final .RE */
267 static int Bl_stack_len; /* number of nested Bl blocks */
268 static int TPremain; /* characters before tag is full */
278 font_push(char newfont)
281 if (fontqueue.head + fontqueue.size <= ++fontqueue.tail) {
283 fontqueue.head = mandoc_realloc(fontqueue.head,
286 *fontqueue.tail = newfont;
290 outflags &= ~MMAN_spc;
297 if (fontqueue.tail > fontqueue.head)
299 outflags &= ~MMAN_spc;
302 putchar(*fontqueue.tail);
306 print_word(const char *s)
309 if ((MMAN_PP | MMAN_sp | MMAN_br | MMAN_nl) & outflags) {
311 * If we need a newline, print it now and start afresh.
313 if (MMAN_PP & outflags) {
314 if (MMAN_sp & outflags) {
315 if (MMAN_PD & outflags) {
317 outflags &= ~MMAN_PD;
319 } else if ( ! (MMAN_PD & outflags)) {
324 } else if (MMAN_sp & outflags)
326 else if (MMAN_br & outflags)
328 else if (MMAN_nl & outflags)
330 outflags &= ~(MMAN_PP|MMAN_sp|MMAN_br|MMAN_nl|MMAN_spc);
334 } else if (MMAN_spc & outflags) {
336 * If we need a space, only print it if
337 * (1) it is forced by `No' or
338 * (2) what follows is not terminating punctuation or
339 * (3) what follows is longer than one character.
341 if (MMAN_spc_force & outflags || '\0' == s[0] ||
342 NULL == strchr(".,:;)]?!", s[0]) || '\0' != s[1]) {
343 if (MMAN_Bk & outflags &&
344 ! (MMAN_Bk_susp & outflags))
353 * Reassign needing space if we're not following opening
356 if (MMAN_Sm & outflags && ('\0' == s[0] ||
357 (('(' != s[0] && '[' != s[0]) || '\0' != s[1])))
358 outflags |= MMAN_spc;
360 outflags &= ~MMAN_spc;
361 outflags &= ~(MMAN_spc_force | MMAN_Bk_susp);
375 if (MMAN_nbrword & outflags) {
381 putchar((unsigned char)*s);
387 outflags &= ~MMAN_nbrword;
391 print_line(const char *s, int newflags)
394 outflags &= ~MMAN_br;
397 outflags |= newflags;
401 print_block(const char *s, int newflags)
404 outflags &= ~MMAN_PP;
405 if (MMAN_sp & outflags) {
406 outflags &= ~(MMAN_sp | MMAN_br);
407 if (MMAN_PD & outflags) {
408 print_line(".PD", 0);
409 outflags &= ~MMAN_PD;
411 } else if (! (MMAN_PD & outflags))
412 print_line(".PD 0", MMAN_PD);
415 outflags |= MMAN_Bk_susp | newflags;
419 print_offs(const char *v, int keywords)
425 print_line(".RS", MMAN_Bk_susp);
427 /* Convert v into a number (of characters). */
428 if (NULL == v || '\0' == *v || (keywords && !strcmp(v, "left")))
430 else if (keywords && !strcmp(v, "indent"))
432 else if (keywords && !strcmp(v, "indent-two"))
434 else if (a2roffsu(v, &su, SCALE_EN) > 1) {
435 if (SCALE_EN == su.unit)
440 * If we are inside an enclosing list,
441 * there is no easy way to add the two
442 * indentations because they are provided
443 * in terms of different units.
453 * We are inside an enclosing list.
454 * Add the two indentations.
457 sz += Bl_stack[Bl_stack_len - 1];
459 (void)snprintf(buf, sizeof(buf), "%dn", sz);
465 * Set up the indentation for a list item; used from pre_it().
468 print_width(const struct mdoc_bl *bl, const struct roff_node *child)
472 int numeric, remain, sz, chsz;
477 /* Convert the width into a number (of characters). */
478 if (bl->width == NULL)
479 sz = (bl->type == LIST_hang) ? 6 : 0;
480 else if (a2roffsu(bl->width, &su, SCALE_MAX) > 1) {
481 if (SCALE_EN == su.unit)
488 sz = strlen(bl->width);
490 /* XXX Rough estimation, might have multiple parts. */
491 if (bl->type == LIST_enum)
492 chsz = (bl->count > 8) + 1;
493 else if (child != NULL && child->type == ROFFT_TEXT)
494 chsz = strlen(child->string);
498 /* Maybe we are inside an enclosing list? */
502 * Save our own indentation,
503 * such that child lists can use it.
505 Bl_stack[Bl_stack_len++] = sz + 2;
507 /* Set up the current list. */
508 if (chsz > sz && bl->type != LIST_tag)
509 print_block(".HP", 0);
511 print_block(".TP", 0);
515 (void)snprintf(buf, sizeof(buf), "%dn", sz + 2);
518 print_word(bl->width);
523 print_count(int *count)
527 (void)snprintf(buf, sizeof(buf), "%d.\\&", ++*count);
532 man_man(void *arg, const struct roff_man *man)
536 * Dump the keep buffer.
537 * We're guaranteed by now that this exists (is non-NULL).
538 * Flush stdout afterward, just in case.
540 fputs(mparse_getkeep(man_mparse(man)), stdout);
545 man_mdoc(void *arg, const struct roff_man *mdoc)
549 printf(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"\n",
551 (mdoc->meta.msec == NULL ? "" : mdoc->meta.msec),
552 mdoc->meta.date, mdoc->meta.os, mdoc->meta.vol);
554 /* Disable hyphenation and if nroff, disable justification. */
555 printf(".nh\n.if n .ad l");
557 outflags = MMAN_nl | MMAN_Sm;
558 if (0 == fontqueue.size) {
560 fontqueue.head = fontqueue.tail = mandoc_malloc(8);
561 *fontqueue.tail = 'R';
563 for (n = mdoc->first->child; n != NULL; n = n->next)
564 print_node(&mdoc->meta, n);
569 print_node(DECL_ARGS)
571 const struct manact *act;
572 struct roff_node *sub;
575 if (n->flags & NODE_NOPRT)
579 * Break the line if we were parsed subsequent the current node.
580 * This makes the page structure be more consistent.
582 if (MMAN_spc & outflags && NODE_LINE & n->flags)
588 n->flags &= ~NODE_ENDED;
590 if (n->type == ROFFT_TEXT) {
592 * Make sure that we don't happen to start with a
593 * control character at the start of a line.
595 if (MMAN_nl & outflags &&
596 ('.' == *n->string || '\'' == *n->string)) {
599 outflags &= ~MMAN_spc;
601 if (n->flags & NODE_DELIMC)
602 outflags &= ~(MMAN_spc | MMAN_spc_force);
603 else if (outflags & MMAN_Sm)
604 outflags |= MMAN_spc_force;
605 print_word(n->string);
606 if (n->flags & NODE_DELIMO)
607 outflags &= ~(MMAN_spc | MMAN_spc_force);
608 else if (outflags & MMAN_Sm)
609 outflags |= MMAN_spc;
612 * Conditionally run the pre-node action handler for a
615 act = manacts + n->tok;
616 cond = act->cond == NULL || (*act->cond)(meta, n);
617 if (cond && act->pre != NULL &&
618 (n->end == ENDBODY_NOT || n->child != NULL))
619 do_sub = (*act->pre)(meta, n);
623 * Conditionally run all child nodes.
624 * Note that this iterates over children instead of using
625 * recursion. This prevents unnecessary depth in the stack.
628 for (sub = n->child; sub; sub = sub->next)
629 print_node(meta, sub);
632 * Lastly, conditionally run the post-node handler.
634 if (NODE_ENDED & n->flags)
637 if (cond && act->post)
638 (*act->post)(meta, n);
640 if (ENDBODY_NOT != n->end)
641 n->body->flags |= NODE_ENDED;
643 if (ENDBODY_NOSPACE == n->end)
644 outflags &= ~(MMAN_spc | MMAN_nl);
651 return n->type == ROFFT_HEAD;
658 return n->type == ROFFT_BODY;
666 prefix = manacts[n->tok].prefix;
670 outflags &= ~MMAN_spc;
679 suffix = manacts[n->tok].suffix;
682 outflags &= ~(MMAN_spc | MMAN_nl);
689 outflags |= MMAN_br | MMAN_nl;
701 post_percent(DECL_ARGS)
704 if (pre_em == manacts[n->tok].pre)
708 if (n->prev && n->prev->tok == n->tok &&
709 n->next->tok == n->tok)
721 if (n->parent && MDOC_Rs == n->parent->tok &&
722 n->parent->norm->Rs.quote_T) {
725 outflags &= ~MMAN_spc;
735 if (n->parent && MDOC_Rs == n->parent->tok &&
736 n->parent->norm->Rs.quote_T) {
737 outflags &= ~MMAN_spc;
742 post_percent(meta, n);
746 * Print before a section header.
752 if (n->type == ROFFT_HEAD) {
754 print_block(manacts[n->tok].prefix, 0);
757 outflags &= ~MMAN_spc;
763 * Print subsequent a section header.
769 if (n->type != ROFFT_HEAD)
771 outflags &= ~MMAN_spc;
775 if (MDOC_Sh == n->tok && SEC_AUTHORS == n->sec)
776 outflags &= ~(MMAN_An_split | MMAN_An_nosplit);
779 /* See mdoc_term.c, synopsis_pre() for comments. */
781 pre_syn(const struct roff_node *n)
784 if (NULL == n->prev || ! (NODE_SYNPRETTY & n->flags))
787 if (n->prev->tok == n->tok &&
795 switch (n->prev->tok) {
804 if (MDOC_Fn != n->tok && MDOC_Fo != n->tok) {
819 switch (n->norm->An.auth) {
821 outflags &= ~MMAN_An_nosplit;
822 outflags |= MMAN_An_split;
825 outflags &= ~MMAN_An_split;
826 outflags |= MMAN_An_nosplit;
829 if (MMAN_An_split & outflags)
831 else if (SEC_AUTHORS == n->sec &&
832 ! (MMAN_An_nosplit & outflags))
833 outflags |= MMAN_An_split;
842 outflags &= ~MMAN_spc;
844 outflags &= ~MMAN_spc;
852 print_word(n->child != NULL && n->child->next == NULL &&
853 n->child->tok == MDOC_Mt ? "<" : "\\(la");
854 outflags &= ~MMAN_spc;
862 outflags &= ~(MMAN_spc | MMAN_nl);
863 print_word(n->child != NULL && n->child->next == NULL &&
864 n->child->tok == MDOC_Mt ? ">" : "\\(ra");
871 outflags &= ~(MMAN_PP | MMAN_sp | MMAN_br);
873 if (DISP_unfilled == n->norm->Bd.type ||
874 DISP_literal == n->norm->Bd.type)
875 print_line(".nf", 0);
876 if (0 == n->norm->Bd.comp && NULL != n->parent->prev)
878 print_offs(n->norm->Bd.offs, 1);
886 /* Close out this display. */
887 print_line(".RE", MMAN_nl);
888 if (DISP_unfilled == n->norm->Bd.type ||
889 DISP_literal == n->norm->Bd.type)
890 print_line(".fi", MMAN_nl);
892 /* Maybe we are inside an enclosing list? */
893 if (NULL != n->parent->next)
909 switch (n->norm->Bf.font) {
927 if (n->type == ROFFT_BODY)
950 if (n->type == ROFFT_BODY)
951 outflags &= ~MMAN_Bk;
960 * print_offs() will increase the -offset to account for
961 * a possible enclosing .It, but any enclosed .It blocks
962 * just nest and do not add up their indentation.
964 if (n->norm->Bl.offs) {
965 print_offs(n->norm->Bl.offs, 0);
966 Bl_stack[Bl_stack_len++] = 0;
969 switch (n->norm->Bl.type) {
971 n->norm->Bl.count = 0;
979 if (n->child != NULL) {
980 print_line(".TS", MMAN_nl);
981 for (icol = 0; icol < n->norm->Bl.ncols; icol++)
993 switch (n->norm->Bl.type) {
995 if (n->child != NULL)
996 print_line(".TE", 0);
999 n->norm->Bl.count = 0;
1005 if (n->norm->Bl.offs) {
1006 print_line(".RE", MMAN_nl);
1007 assert(Bl_stack_len);
1009 assert(0 == Bl_stack[Bl_stack_len]);
1011 outflags |= MMAN_PP | MMAN_nl;
1012 outflags &= ~(MMAN_sp | MMAN_br);
1015 /* Maybe we are inside an enclosing list? */
1016 if (NULL != n->parent->next)
1025 outflags |= MMAN_br;
1033 print_offs("6n", 0);
1041 print_line(".RE", MMAN_nl);
1043 /* Maybe we are inside an enclosing list? */
1044 if (NULL != n->parent->next)
1060 if (NULL == n->norm->Es ||
1061 NULL == n->norm->Es->child)
1064 print_word(n->norm->Es->child->string);
1065 outflags &= ~MMAN_spc;
1073 if (NULL == n->norm->Es ||
1074 NULL == n->norm->Es->child ||
1075 NULL == n->norm->Es->child->next)
1078 outflags &= ~MMAN_spc;
1079 print_word(n->norm->Es->child->next->string);
1087 if (n->end == ENDBODY_NOT &&
1088 n->parent->head->child == NULL &&
1090 n->child->end != ENDBODY_NOT)
1092 else if (n->end != ENDBODY_NOT ? n->child != NULL :
1093 n->parent->head->child != NULL && (n->child != NULL ||
1094 (n->parent->tail != NULL && n->parent->tail->child != NULL)))
1095 outflags &= ~(MMAN_spc | MMAN_nl);
1104 if (n->end != ENDBODY_NOT) {
1105 outflags |= MMAN_spc;
1109 body = n->child != NULL || n->parent->head->child != NULL;
1110 tail = n->parent->tail != NULL && n->parent->tail->child != NULL;
1113 outflags &= ~MMAN_spc;
1114 else if ( ! (body || tail))
1117 outflags |= MMAN_spc;
1125 am_Fa = MDOC_Fa == n->tok;
1132 if (am_Fa || NODE_SYNPRETTY & n->flags)
1133 outflags |= MMAN_nbrword;
1134 print_node(meta, n);
1136 if (NULL != (n = n->next))
1146 if (NULL != n->next && MDOC_Fa == n->next->tok)
1164 outflags |= MMAN_br;
1173 if (n->child != NULL)
1174 outflags &= ~MMAN_spc;
1183 if (!(n->child != NULL ||
1185 n->next->type == ROFFT_TEXT ||
1186 n->next->flags & NODE_LINE))
1187 outflags &= ~MMAN_spc;
1200 if (NODE_SYNPRETTY & n->flags)
1201 print_block(".HP 4n", MMAN_nl);
1204 print_node(meta, n);
1206 outflags &= ~MMAN_spc;
1208 outflags &= ~MMAN_spc;
1221 if (NODE_SYNPRETTY & n->flags) {
1223 outflags |= MMAN_PP;
1236 if (n->child == NULL)
1238 if (NODE_SYNPRETTY & n->flags)
1239 print_block(".HP 4n", MMAN_nl);
1243 outflags &= ~(MMAN_spc | MMAN_nl);
1245 outflags &= ~MMAN_spc;
1259 if (n->child != NULL)
1283 if (NODE_SYNPRETTY & n->flags) {
1286 print_word("#include <");
1287 outflags &= ~MMAN_spc;
1290 outflags &= ~MMAN_spc;
1300 if (NODE_SYNPRETTY & n->flags) {
1301 outflags &= ~MMAN_spc;
1304 outflags |= MMAN_br;
1307 outflags &= ~MMAN_spc;
1315 const struct roff_node *bln;
1319 outflags |= MMAN_PP | MMAN_nl;
1320 bln = n->parent->parent;
1321 if (0 == bln->norm->Bl.comp ||
1322 (NULL == n->parent->prev &&
1323 NULL == bln->parent->prev))
1324 outflags |= MMAN_sp;
1325 outflags &= ~MMAN_br;
1326 switch (bln->norm->Bl.type) {
1332 if (bln->norm->Bl.type == LIST_diag)
1333 print_line(".B \"", 0);
1335 print_line(".R \"", 0);
1336 outflags &= ~MMAN_spc;
1341 print_width(&bln->norm->Bl, NULL);
1343 outflags |= MMAN_nl;
1345 if (LIST_bullet == bln->norm->Bl.type)
1346 print_word("\\(bu");
1350 outflags |= MMAN_nl;
1353 print_width(&bln->norm->Bl, NULL);
1355 outflags |= MMAN_nl;
1356 print_count(&bln->norm->Bl.count);
1357 outflags |= MMAN_nl;
1360 print_width(&bln->norm->Bl, n->child);
1362 outflags |= MMAN_nl;
1365 print_width(&bln->norm->Bl, n->child);
1367 outflags &= ~MMAN_spc;
1379 * This function is called after closing out an indented block.
1380 * If we are inside an enclosing list, restore its indentation.
1387 /* Nothing to do outside a list. */
1388 if (0 == Bl_stack_len || 0 == Bl_stack[Bl_stack_len - 1])
1391 /* The indentation has already been set up. */
1392 if (Bl_stack_post[Bl_stack_len - 1])
1395 /* Restore the indentation of the enclosing list. */
1396 print_line(".RS", MMAN_Bk_susp);
1397 (void)snprintf(buf, sizeof(buf), "%dn",
1398 Bl_stack[Bl_stack_len - 1]);
1401 /* Remeber to close out this .RS block later. */
1402 Bl_stack_post[Bl_stack_len - 1] = 1;
1408 const struct roff_node *bln;
1410 bln = n->parent->parent;
1414 switch (bln->norm->Bl.type) {
1416 outflags &= ~MMAN_spc;
1420 outflags |= MMAN_br;
1427 switch (bln->norm->Bl.type) {
1434 assert(Bl_stack_len);
1435 Bl_stack[--Bl_stack_len] = 0;
1438 * Our indentation had to be restored
1439 * after a child display or child list.
1440 * Close out that indentation block now.
1442 if (Bl_stack_post[Bl_stack_len]) {
1443 print_line(".RE", MMAN_nl);
1444 Bl_stack_post[Bl_stack_len] = 0;
1448 if (NULL != n->next) {
1450 outflags &= ~MMAN_spc;
1466 if (SEC_LIBRARY == n->sec)
1467 outflags |= MMAN_br;
1473 const struct roff_node *link, *descr;
1475 if (NULL == (link = n->child))
1478 if (NULL != (descr = link->next)) {
1480 while (NULL != descr) {
1481 print_word(descr->string);
1482 descr = descr->next;
1489 print_word(link->string);
1498 print_line(".ll", 0);
1515 if (n->type == ROFFT_BLOCK) {
1516 outflags |= MMAN_Bk;
1519 if (n->type != ROFFT_ELEM && n->type != ROFFT_HEAD)
1521 name = n->child ? n->child->string : meta->name;
1524 if (n->type == ROFFT_HEAD) {
1525 if (NULL == n->parent->prev)
1526 outflags |= MMAN_sp;
1527 print_block(".HP", 0);
1528 printf(" %zun", strlen(name) + 1);
1529 outflags |= MMAN_nl;
1532 if (NULL == n->child)
1533 print_word(meta->name);
1543 outflags &= ~MMAN_Bk;
1547 if (n->child != NULL || meta->name != NULL)
1559 outflags |= MMAN_spc_force;
1567 outflags &= ~MMAN_spc;
1575 if ( ! (n->next == NULL || n->next->flags & NODE_LINE))
1576 outflags &= ~MMAN_spc;
1583 if (MDOC_It != n->parent->tok)
1584 outflags |= MMAN_PP;
1585 outflags |= MMAN_sp | MMAN_nl;
1586 outflags &= ~MMAN_br;
1594 if (SEC_SEE_ALSO == n->sec) {
1595 outflags |= MMAN_PP | MMAN_sp | MMAN_nl;
1596 outflags &= ~MMAN_br;
1612 if (NULL == n->child)
1613 outflags ^= MMAN_Sm;
1614 else if (0 == strcmp("on", n->child->string))
1615 outflags |= MMAN_Sm;
1617 outflags &= ~MMAN_Sm;
1619 if (MMAN_Sm & outflags)
1620 outflags |= MMAN_spc;
1629 if (MMAN_PP & outflags) {
1630 outflags &= ~MMAN_PP;
1631 print_line(".PP", 0);
1633 print_line(".sp", 0);
1641 outflags |= MMAN_nl;
1656 if (NODE_SYNPRETTY & n->flags) {
1675 if (n->flags & NODE_SYNPRETTY && n->type != ROFFT_BODY)
1687 print_node(meta, n);
1691 outflags &= ~MMAN_spc;
1693 print_node(meta, n);