1 /* $Id: mdoc_man.c,v 1.88 2015/02/17 20:37:17 schwarze Exp $ */
3 * Copyright (c) 2011-2015 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"
32 #define DECL_ARGS const struct mdoc_meta *meta, struct mdoc_node *n
35 int (*cond)(DECL_ARGS); /* DON'T run actions */
36 int (*pre)(DECL_ARGS); /* pre-node action */
37 void (*post)(DECL_ARGS); /* post-node action */
38 const char *prefix; /* pre-node string constant */
39 const char *suffix; /* post-node string constant */
42 static int cond_body(DECL_ARGS);
43 static int cond_head(DECL_ARGS);
44 static void font_push(char);
45 static void font_pop(void);
46 static void mid_it(void);
47 static void post__t(DECL_ARGS);
48 static void post_aq(DECL_ARGS);
49 static void post_bd(DECL_ARGS);
50 static void post_bf(DECL_ARGS);
51 static void post_bk(DECL_ARGS);
52 static void post_bl(DECL_ARGS);
53 static void post_dl(DECL_ARGS);
54 static void post_en(DECL_ARGS);
55 static void post_enc(DECL_ARGS);
56 static void post_eo(DECL_ARGS);
57 static void post_fa(DECL_ARGS);
58 static void post_fd(DECL_ARGS);
59 static void post_fl(DECL_ARGS);
60 static void post_fn(DECL_ARGS);
61 static void post_fo(DECL_ARGS);
62 static void post_font(DECL_ARGS);
63 static void post_in(DECL_ARGS);
64 static void post_it(DECL_ARGS);
65 static void post_lb(DECL_ARGS);
66 static void post_nm(DECL_ARGS);
67 static void post_percent(DECL_ARGS);
68 static void post_pf(DECL_ARGS);
69 static void post_sect(DECL_ARGS);
70 static void post_sp(DECL_ARGS);
71 static void post_vt(DECL_ARGS);
72 static int pre__t(DECL_ARGS);
73 static int pre_an(DECL_ARGS);
74 static int pre_ap(DECL_ARGS);
75 static int pre_aq(DECL_ARGS);
76 static int pre_bd(DECL_ARGS);
77 static int pre_bf(DECL_ARGS);
78 static int pre_bk(DECL_ARGS);
79 static int pre_bl(DECL_ARGS);
80 static int pre_br(DECL_ARGS);
81 static int pre_bx(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_rv(DECL_ARGS);
106 static int pre_sm(DECL_ARGS);
107 static int pre_sp(DECL_ARGS);
108 static int pre_sect(DECL_ARGS);
109 static int pre_sy(DECL_ARGS);
110 static void pre_syn(const struct mdoc_node *);
111 static int pre_vt(DECL_ARGS);
112 static int pre_ux(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 mdoc_node *);
120 static void print_count(int *);
121 static void print_node(DECL_ARGS);
123 static const struct manact manacts[MDOC_MAX + 1] = {
124 { NULL, pre_ap, NULL, NULL, NULL }, /* Ap */
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_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_rv, 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, pre_ux, NULL, "BSD/OS", NULL }, /* Bsx */
185 { NULL, pre_bx, 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, pre_ux, NULL, "FreeBSD", 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, pre_ux, NULL, "NetBSD", NULL }, /* Nx */
199 { NULL, pre_ux, NULL, "OpenBSD", 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, pre_ux, NULL, "UNIX", 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, pre_ux, NULL, "is currently in beta test.", NULL }, /* Bt */
227 { NULL, NULL, NULL, NULL, NULL }, /* Hf */
228 { NULL, pre_em, post_font, NULL, NULL }, /* Fr */
229 { NULL, pre_ux, NULL, "currently under development.", 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, pre_ux, NULL, "DragonFly", NULL }, /* Dx */
241 { NULL, NULL, post_percent, NULL, NULL }, /* %Q */
242 { NULL, pre_br, NULL, NULL, NULL }, /* br */
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 */
247 { NULL, NULL, NULL, NULL, NULL }, /* ROOT */
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 font_push(char newfont)
283 if (fontqueue.head + fontqueue.size <= ++fontqueue.tail) {
285 fontqueue.head = mandoc_realloc(fontqueue.head,
288 *fontqueue.tail = newfont;
292 outflags &= ~MMAN_spc;
299 if (fontqueue.tail > fontqueue.head)
301 outflags &= ~MMAN_spc;
304 putchar(*fontqueue.tail);
308 print_word(const char *s)
311 if ((MMAN_PP | MMAN_sp | MMAN_br | MMAN_nl) & outflags) {
313 * If we need a newline, print it now and start afresh.
315 if (MMAN_PP & outflags) {
316 if (MMAN_sp & outflags) {
317 if (MMAN_PD & outflags) {
319 outflags &= ~MMAN_PD;
321 } else if ( ! (MMAN_PD & outflags)) {
326 } else if (MMAN_sp & outflags)
328 else if (MMAN_br & outflags)
330 else if (MMAN_nl & outflags)
332 outflags &= ~(MMAN_PP|MMAN_sp|MMAN_br|MMAN_nl|MMAN_spc);
336 } else if (MMAN_spc & outflags) {
338 * If we need a space, only print it if
339 * (1) it is forced by `No' or
340 * (2) what follows is not terminating punctuation or
341 * (3) what follows is longer than one character.
343 if (MMAN_spc_force & outflags || '\0' == s[0] ||
344 NULL == strchr(".,:;)]?!", s[0]) || '\0' != s[1]) {
345 if (MMAN_Bk & outflags &&
346 ! (MMAN_Bk_susp & outflags))
355 * Reassign needing space if we're not following opening
358 if (MMAN_Sm & outflags && ('\0' == s[0] ||
359 (('(' != s[0] && '[' != s[0]) || '\0' != s[1])))
360 outflags |= MMAN_spc;
362 outflags &= ~MMAN_spc;
363 outflags &= ~(MMAN_spc_force | MMAN_Bk_susp);
377 if (MMAN_nbrword & outflags) {
383 putchar((unsigned char)*s);
389 outflags &= ~MMAN_nbrword;
393 print_line(const char *s, int newflags)
396 outflags &= ~MMAN_br;
399 outflags |= newflags;
403 print_block(const char *s, int newflags)
406 outflags &= ~MMAN_PP;
407 if (MMAN_sp & outflags) {
408 outflags &= ~(MMAN_sp | MMAN_br);
409 if (MMAN_PD & outflags) {
410 print_line(".PD", 0);
411 outflags &= ~MMAN_PD;
413 } else if (! (MMAN_PD & outflags))
414 print_line(".PD 0", MMAN_PD);
417 outflags |= MMAN_Bk_susp | newflags;
421 print_offs(const char *v, int keywords)
427 print_line(".RS", MMAN_Bk_susp);
429 /* Convert v into a number (of characters). */
430 if (NULL == v || '\0' == *v || (keywords && !strcmp(v, "left")))
432 else if (keywords && !strcmp(v, "indent"))
434 else if (keywords && !strcmp(v, "indent-two"))
436 else if (a2roffsu(v, &su, SCALE_EN) > 1) {
437 if (SCALE_EN == su.unit)
442 * If we are inside an enclosing list,
443 * there is no easy way to add the two
444 * indentations because they are provided
445 * in terms of different units.
455 * We are inside an enclosing list.
456 * Add the two indentations.
459 sz += Bl_stack[Bl_stack_len - 1];
461 (void)snprintf(buf, sizeof(buf), "%dn", sz);
467 * Set up the indentation for a list item; used from pre_it().
470 print_width(const struct mdoc_bl *bl, const struct mdoc_node *child)
474 int numeric, remain, sz, chsz;
479 /* Convert the width into a number (of characters). */
480 if (bl->width == NULL)
481 sz = (bl->type == LIST_hang) ? 6 : 0;
482 else if (a2roffsu(bl->width, &su, SCALE_MAX) > 1) {
483 if (SCALE_EN == su.unit)
490 sz = strlen(bl->width);
492 /* XXX Rough estimation, might have multiple parts. */
493 if (bl->type == LIST_enum)
494 chsz = (bl->count > 8) + 1;
495 else if (child != NULL && child->type == MDOC_TEXT)
496 chsz = strlen(child->string);
500 /* Maybe we are inside an enclosing list? */
504 * Save our own indentation,
505 * such that child lists can use it.
507 Bl_stack[Bl_stack_len++] = sz + 2;
509 /* Set up the current list. */
510 if (chsz > sz && bl->type != LIST_tag)
511 print_block(".HP", 0);
513 print_block(".TP", 0);
517 (void)snprintf(buf, sizeof(buf), "%dn", sz + 2);
520 print_word(bl->width);
525 print_count(int *count)
529 (void)snprintf(buf, sizeof(buf), "%d.\\&", ++*count);
534 man_man(void *arg, const struct man *man)
538 * Dump the keep buffer.
539 * We're guaranteed by now that this exists (is non-NULL).
540 * Flush stdout afterward, just in case.
542 fputs(mparse_getkeep(man_mparse(man)), stdout);
547 man_mdoc(void *arg, const struct mdoc *mdoc)
549 const struct mdoc_meta *meta;
552 meta = mdoc_meta(mdoc);
553 n = mdoc_node(mdoc)->child;
555 printf(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"\n",
557 (meta->msec == NULL ? "" : meta->msec),
558 meta->date, meta->os, meta->vol);
560 /* Disable hyphenation and if nroff, disable justification. */
561 printf(".nh\n.if n .ad l");
563 outflags = MMAN_nl | MMAN_Sm;
564 if (0 == fontqueue.size) {
566 fontqueue.head = fontqueue.tail = mandoc_malloc(8);
567 *fontqueue.tail = 'R';
577 print_node(DECL_ARGS)
579 const struct manact *act;
580 struct mdoc_node *sub;
584 * Break the line if we were parsed subsequent the current node.
585 * This makes the page structure be more consistent.
587 if (MMAN_spc & outflags && MDOC_LINE & n->flags)
593 n->flags &= ~MDOC_ENDED;
595 if (MDOC_TEXT == n->type) {
597 * Make sure that we don't happen to start with a
598 * control character at the start of a line.
600 if (MMAN_nl & outflags &&
601 ('.' == *n->string || '\'' == *n->string)) {
604 outflags &= ~MMAN_spc;
606 if (outflags & MMAN_Sm && ! (n->flags & MDOC_DELIMC))
607 outflags |= MMAN_spc_force;
608 print_word(n->string);
609 if (outflags & MMAN_Sm && ! (n->flags & MDOC_DELIMO))
610 outflags |= MMAN_spc;
613 * Conditionally run the pre-node action handler for a
616 act = manacts + n->tok;
617 cond = act->cond == NULL || (*act->cond)(meta, n);
618 if (cond && act->pre && (n->end == ENDBODY_NOT || n->nchild))
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 (MDOC_ENDED & n->flags)
637 if (cond && act->post)
638 (*act->post)(meta, n);
640 if (ENDBODY_NOT != n->end)
641 n->body->flags |= MDOC_ENDED;
643 if (ENDBODY_NOSPACE == n->end)
644 outflags &= ~(MMAN_spc | MMAN_nl);
651 return(MDOC_HEAD == n->type);
658 return(MDOC_BODY == n->type);
666 prefix = manacts[n->tok].prefix;
670 outflags &= ~MMAN_spc;
679 suffix = manacts[n->tok].suffix;
682 outflags &= ~(MMAN_spc | MMAN_nl);
691 outflags |= MMAN_br | MMAN_nl;
696 for (n = n->child; n; n = n->next) {
698 print_word(n->string);
705 outflags &= ~MMAN_spc;
708 if (n->next->next == NULL)
713 print_word("utilities exit\\~0");
715 print_word("utility exits\\~0");
717 print_word("on success, and\\~>0 if an error occurs.");
730 post_percent(DECL_ARGS)
733 if (pre_em == manacts[n->tok].pre)
737 if (n->prev && n->prev->tok == n->tok &&
738 n->next->tok == n->tok)
750 if (n->parent && MDOC_Rs == n->parent->tok &&
751 n->parent->norm->Rs.quote_T) {
754 outflags &= ~MMAN_spc;
764 if (n->parent && MDOC_Rs == n->parent->tok &&
765 n->parent->norm->Rs.quote_T) {
766 outflags &= ~MMAN_spc;
771 post_percent(meta, n);
775 * Print before a section header.
781 if (MDOC_HEAD == n->type) {
783 print_block(manacts[n->tok].prefix, 0);
786 outflags &= ~MMAN_spc;
792 * Print subsequent a section header.
798 if (MDOC_HEAD != n->type)
800 outflags &= ~MMAN_spc;
804 if (MDOC_Sh == n->tok && SEC_AUTHORS == n->sec)
805 outflags &= ~(MMAN_An_split | MMAN_An_nosplit);
808 /* See mdoc_term.c, synopsis_pre() for comments. */
810 pre_syn(const struct mdoc_node *n)
813 if (NULL == n->prev || ! (MDOC_SYNPRETTY & n->flags))
816 if (n->prev->tok == n->tok &&
824 switch (n->prev->tok) {
837 if (MDOC_Fn != n->tok && MDOC_Fo != n->tok) {
852 switch (n->norm->An.auth) {
854 outflags &= ~MMAN_An_nosplit;
855 outflags |= MMAN_An_split;
858 outflags &= ~MMAN_An_split;
859 outflags |= MMAN_An_nosplit;
862 if (MMAN_An_split & outflags)
864 else if (SEC_AUTHORS == n->sec &&
865 ! (MMAN_An_nosplit & outflags))
866 outflags |= MMAN_An_split;
875 outflags &= ~MMAN_spc;
877 outflags &= ~MMAN_spc;
885 print_word(n->nchild == 1 &&
886 n->child->tok == MDOC_Mt ? "<" : "\\(la");
887 outflags &= ~MMAN_spc;
895 outflags &= ~(MMAN_spc | MMAN_nl);
896 print_word(n->nchild == 1 &&
897 n->child->tok == MDOC_Mt ? ">" : "\\(ra");
904 outflags &= ~(MMAN_PP | MMAN_sp | MMAN_br);
906 if (DISP_unfilled == n->norm->Bd.type ||
907 DISP_literal == n->norm->Bd.type)
908 print_line(".nf", 0);
909 if (0 == n->norm->Bd.comp && NULL != n->parent->prev)
911 print_offs(n->norm->Bd.offs, 1);
919 /* Close out this display. */
920 print_line(".RE", MMAN_nl);
921 if (DISP_unfilled == n->norm->Bd.type ||
922 DISP_literal == n->norm->Bd.type)
923 print_line(".fi", MMAN_nl);
925 /* Maybe we are inside an enclosing list? */
926 if (NULL != n->parent->next)
942 switch (n->norm->Bf.font) {
960 if (MDOC_BODY == n->type)
983 if (MDOC_BODY == n->type)
984 outflags &= ~MMAN_Bk;
993 * print_offs() will increase the -offset to account for
994 * a possible enclosing .It, but any enclosed .It blocks
995 * just nest and do not add up their indentation.
997 if (n->norm->Bl.offs) {
998 print_offs(n->norm->Bl.offs, 0);
999 Bl_stack[Bl_stack_len++] = 0;
1002 switch (n->norm->Bl.type) {
1004 n->norm->Bl.count = 0;
1013 print_line(".TS", MMAN_nl);
1014 for (icol = 0; icol < n->norm->Bl.ncols; icol++)
1018 outflags |= MMAN_nl;
1026 switch (n->norm->Bl.type) {
1029 print_line(".TE", 0);
1032 n->norm->Bl.count = 0;
1038 if (n->norm->Bl.offs) {
1039 print_line(".RE", MMAN_nl);
1040 assert(Bl_stack_len);
1042 assert(0 == Bl_stack[Bl_stack_len]);
1044 outflags |= MMAN_PP | MMAN_nl;
1045 outflags &= ~(MMAN_sp | MMAN_br);
1048 /* Maybe we are inside an enclosing list? */
1049 if (NULL != n->parent->next)
1058 outflags |= MMAN_br;
1068 print_word(n->string);
1069 outflags &= ~MMAN_spc;
1075 outflags &= ~MMAN_spc;
1077 outflags &= ~MMAN_spc;
1078 print_word(n->string);
1086 print_offs("6n", 0);
1094 print_line(".RE", MMAN_nl);
1096 /* Maybe we are inside an enclosing list? */
1097 if (NULL != n->parent->next)
1113 if (NULL == n->norm->Es ||
1114 NULL == n->norm->Es->child)
1117 print_word(n->norm->Es->child->string);
1118 outflags &= ~MMAN_spc;
1126 if (NULL == n->norm->Es ||
1127 NULL == n->norm->Es->child ||
1128 NULL == n->norm->Es->child->next)
1131 outflags &= ~MMAN_spc;
1132 print_word(n->norm->Es->child->next->string);
1140 if (n->end == ENDBODY_NOT &&
1141 n->parent->head->child == NULL &&
1143 n->child->end != ENDBODY_NOT)
1145 else if (n->end != ENDBODY_NOT ? n->child != NULL :
1146 n->parent->head->child != NULL && (n->child != NULL ||
1147 (n->parent->tail != NULL && n->parent->tail->child != NULL)))
1148 outflags &= ~(MMAN_spc | MMAN_nl);
1157 if (n->end != ENDBODY_NOT) {
1158 outflags |= MMAN_spc;
1162 body = n->child != NULL || n->parent->head->child != NULL;
1163 tail = n->parent->tail != NULL && n->parent->tail->child != NULL;
1166 outflags &= ~MMAN_spc;
1167 else if ( ! (body || tail))
1170 outflags |= MMAN_spc;
1178 am_Fa = MDOC_Fa == n->tok;
1185 if (am_Fa || MDOC_SYNPRETTY & n->flags)
1186 outflags |= MMAN_nbrword;
1187 print_node(meta, n);
1189 if (NULL != (n = n->next))
1199 if (NULL != n->next && MDOC_Fa == n->next->tok)
1217 outflags |= MMAN_br;
1227 outflags &= ~MMAN_spc;
1236 if ( ! (n->nchild ||
1238 n->next->type == MDOC_TEXT ||
1239 n->next->flags & MDOC_LINE))
1240 outflags &= ~MMAN_spc;
1253 if (MDOC_SYNPRETTY & n->flags)
1254 print_block(".HP 4n", MMAN_nl);
1257 print_node(meta, n);
1259 outflags &= ~MMAN_spc;
1261 outflags &= ~MMAN_spc;
1274 if (MDOC_SYNPRETTY & n->flags) {
1276 outflags |= MMAN_PP;
1289 if (n->child == NULL)
1291 if (MDOC_SYNPRETTY & n->flags)
1292 print_block(".HP 4n", MMAN_nl);
1296 outflags &= ~(MMAN_spc | MMAN_nl);
1298 outflags &= ~MMAN_spc;
1312 if (n->child != NULL)
1336 if (MDOC_SYNPRETTY & n->flags) {
1339 print_word("#include <");
1340 outflags &= ~MMAN_spc;
1343 outflags &= ~MMAN_spc;
1353 if (MDOC_SYNPRETTY & n->flags) {
1354 outflags &= ~MMAN_spc;
1357 outflags |= MMAN_br;
1360 outflags &= ~MMAN_spc;
1368 const struct mdoc_node *bln;
1372 outflags |= MMAN_PP | MMAN_nl;
1373 bln = n->parent->parent;
1374 if (0 == bln->norm->Bl.comp ||
1375 (NULL == n->parent->prev &&
1376 NULL == bln->parent->prev))
1377 outflags |= MMAN_sp;
1378 outflags &= ~MMAN_br;
1379 switch (bln->norm->Bl.type) {
1387 if (bln->norm->Bl.type == LIST_diag)
1388 print_line(".B \"", 0);
1390 print_line(".R \"", 0);
1391 outflags &= ~MMAN_spc;
1398 print_width(&bln->norm->Bl, NULL);
1400 outflags |= MMAN_nl;
1402 if (LIST_bullet == bln->norm->Bl.type)
1403 print_word("\\(bu");
1407 outflags |= MMAN_nl;
1410 print_width(&bln->norm->Bl, NULL);
1412 outflags |= MMAN_nl;
1413 print_count(&bln->norm->Bl.count);
1414 outflags |= MMAN_nl;
1417 print_width(&bln->norm->Bl, n->child);
1419 outflags |= MMAN_nl;
1422 print_width(&bln->norm->Bl, n->child);
1424 outflags &= ~MMAN_spc;
1436 * This function is called after closing out an indented block.
1437 * If we are inside an enclosing list, restore its indentation.
1444 /* Nothing to do outside a list. */
1445 if (0 == Bl_stack_len || 0 == Bl_stack[Bl_stack_len - 1])
1448 /* The indentation has already been set up. */
1449 if (Bl_stack_post[Bl_stack_len - 1])
1452 /* Restore the indentation of the enclosing list. */
1453 print_line(".RS", MMAN_Bk_susp);
1454 (void)snprintf(buf, sizeof(buf), "%dn",
1455 Bl_stack[Bl_stack_len - 1]);
1458 /* Remeber to close out this .RS block later. */
1459 Bl_stack_post[Bl_stack_len - 1] = 1;
1465 const struct mdoc_node *bln;
1467 bln = n->parent->parent;
1471 switch (bln->norm->Bl.type) {
1473 outflags &= ~MMAN_spc;
1477 outflags |= MMAN_br;
1484 switch (bln->norm->Bl.type) {
1496 assert(Bl_stack_len);
1497 Bl_stack[--Bl_stack_len] = 0;
1500 * Our indentation had to be restored
1501 * after a child display or child list.
1502 * Close out that indentation block now.
1504 if (Bl_stack_post[Bl_stack_len]) {
1505 print_line(".RE", MMAN_nl);
1506 Bl_stack_post[Bl_stack_len] = 0;
1510 if (NULL != n->next) {
1512 outflags &= ~MMAN_spc;
1528 if (SEC_LIBRARY == n->sec)
1529 outflags |= MMAN_br;
1535 const struct mdoc_node *link, *descr;
1537 if (NULL == (link = n->child))
1540 if (NULL != (descr = link->next)) {
1542 while (NULL != descr) {
1543 print_word(descr->string);
1544 descr = descr->next;
1551 print_word(link->string);
1560 print_line(".ll", 0);
1577 if (MDOC_BLOCK == n->type) {
1578 outflags |= MMAN_Bk;
1581 if (MDOC_ELEM != n->type && MDOC_HEAD != n->type)
1583 name = n->child ? n->child->string : meta->name;
1586 if (MDOC_HEAD == n->type) {
1587 if (NULL == n->parent->prev)
1588 outflags |= MMAN_sp;
1589 print_block(".HP", 0);
1590 printf(" %zun", strlen(name) + 1);
1591 outflags |= MMAN_nl;
1594 if (NULL == n->child)
1595 print_word(meta->name);
1605 outflags &= ~MMAN_Bk;
1610 if (n->child != NULL || meta->name != NULL)
1622 outflags |= MMAN_spc_force;
1630 outflags &= ~MMAN_spc;
1638 if ( ! (n->next == NULL || n->next->flags & MDOC_LINE))
1639 outflags &= ~MMAN_spc;
1646 if (MDOC_It != n->parent->tok)
1647 outflags |= MMAN_PP;
1648 outflags |= MMAN_sp | MMAN_nl;
1649 outflags &= ~MMAN_br;
1657 if (SEC_SEE_ALSO == n->sec) {
1658 outflags |= MMAN_PP | MMAN_sp | MMAN_nl;
1659 outflags &= ~MMAN_br;
1669 outflags |= MMAN_br | MMAN_nl;
1675 for (n = n->child; n; n = n->next) {
1677 print_word(n->string);
1680 outflags &= ~MMAN_spc;
1683 if (n->next == NULL)
1687 outflags &= ~MMAN_spc;
1690 if (n->next->next == NULL)
1695 print_word("functions return");
1697 print_word("function returns");
1699 print_word("the value\\~0 if successful;");
1701 print_word("Upon successful completion, "
1702 "the value\\~0 is returned;");
1704 print_word("otherwise the value\\~\\-1 is returned"
1705 " and the global variable");
1708 print_word("errno");
1711 print_word("is set to indicate the error.");
1712 outflags |= MMAN_nl;
1727 if (NULL == n->child)
1728 outflags ^= MMAN_Sm;
1729 else if (0 == strcmp("on", n->child->string))
1730 outflags |= MMAN_Sm;
1732 outflags &= ~MMAN_Sm;
1734 if (MMAN_Sm & outflags)
1735 outflags |= MMAN_spc;
1744 if (MMAN_PP & outflags) {
1745 outflags &= ~MMAN_PP;
1746 print_line(".PP", 0);
1748 print_line(".sp", 0);
1756 outflags |= MMAN_nl;
1771 if (MDOC_SYNPRETTY & n->flags) {
1790 if (MDOC_SYNPRETTY & n->flags && MDOC_BODY != n->type)
1802 print_node(meta, n);
1806 outflags &= ~MMAN_spc;
1808 print_node(meta, n);
1817 print_word(manacts[n->tok].prefix);
1818 if (NULL == n->child)
1820 outflags &= ~MMAN_spc;
1822 outflags &= ~MMAN_spc;