1 /* $Id: mdoc_man.c,v 1.96 2016/01/08 17:48:09 schwarze Exp $ */
3 * Copyright (c) 2011-2016 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_bx(DECL_ARGS);
83 static int pre_dl(DECL_ARGS);
84 static int pre_en(DECL_ARGS);
85 static int pre_enc(DECL_ARGS);
86 static int pre_em(DECL_ARGS);
87 static int pre_skip(DECL_ARGS);
88 static int pre_eo(DECL_ARGS);
89 static int pre_ex(DECL_ARGS);
90 static int pre_fa(DECL_ARGS);
91 static int pre_fd(DECL_ARGS);
92 static int pre_fl(DECL_ARGS);
93 static int pre_fn(DECL_ARGS);
94 static int pre_fo(DECL_ARGS);
95 static int pre_ft(DECL_ARGS);
96 static int pre_in(DECL_ARGS);
97 static int pre_it(DECL_ARGS);
98 static int pre_lk(DECL_ARGS);
99 static int pre_li(DECL_ARGS);
100 static int pre_ll(DECL_ARGS);
101 static int pre_nm(DECL_ARGS);
102 static int pre_no(DECL_ARGS);
103 static int pre_ns(DECL_ARGS);
104 static int pre_pp(DECL_ARGS);
105 static int pre_rs(DECL_ARGS);
106 static int pre_rv(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_ux(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 + 1] = {
125 { NULL, pre_ap, NULL, NULL, NULL }, /* Ap */
126 { NULL, NULL, NULL, NULL, NULL }, /* Dd */
127 { NULL, NULL, NULL, NULL, NULL }, /* Dt */
128 { NULL, NULL, NULL, NULL, NULL }, /* Os */
129 { NULL, pre_sect, post_sect, ".SH", NULL }, /* Sh */
130 { NULL, pre_sect, post_sect, ".SS", NULL }, /* Ss */
131 { NULL, pre_pp, NULL, NULL, NULL }, /* Pp */
132 { cond_body, pre_dl, post_dl, NULL, NULL }, /* D1 */
133 { cond_body, pre_dl, post_dl, NULL, NULL }, /* Dl */
134 { cond_body, pre_bd, post_bd, NULL, NULL }, /* Bd */
135 { NULL, NULL, NULL, NULL, NULL }, /* Ed */
136 { cond_body, pre_bl, post_bl, NULL, NULL }, /* Bl */
137 { NULL, NULL, NULL, NULL, NULL }, /* El */
138 { NULL, pre_it, post_it, NULL, NULL }, /* It */
139 { NULL, pre_em, post_font, NULL, NULL }, /* Ad */
140 { NULL, pre_an, NULL, NULL, NULL }, /* An */
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_rv, 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, pre_ux, NULL, "BSD/OS", NULL }, /* Bsx */
186 { NULL, pre_bx, 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, pre_ux, NULL, "FreeBSD", 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, pre_ux, NULL, "NetBSD", NULL }, /* Nx */
200 { NULL, pre_ux, NULL, "OpenBSD", 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, pre_ux, NULL, "UNIX", 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, pre_ux, NULL, "is currently in beta test.", NULL }, /* Bt */
228 { NULL, NULL, NULL, NULL, NULL }, /* Hf */
229 { NULL, pre_em, post_font, NULL, NULL }, /* Fr */
230 { NULL, pre_ux, NULL, "currently under development.", 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, pre_ux, NULL, "DragonFly", NULL }, /* Dx */
242 { NULL, NULL, post_percent, NULL, NULL }, /* %Q */
243 { NULL, pre_br, NULL, NULL, NULL }, /* br */
244 { NULL, pre_sp, post_sp, NULL, NULL }, /* sp */
245 { NULL, NULL, post_percent, NULL, NULL }, /* %U */
246 { NULL, NULL, NULL, NULL, NULL }, /* Ta */
247 { NULL, pre_ll, post_sp, NULL, NULL }, /* ll */
248 { NULL, NULL, NULL, NULL, NULL }, /* ROOT */
252 #define MMAN_spc (1 << 0) /* blank character before next word */
253 #define MMAN_spc_force (1 << 1) /* even before trailing punctuation */
254 #define MMAN_nl (1 << 2) /* break man(7) code line */
255 #define MMAN_br (1 << 3) /* break output line */
256 #define MMAN_sp (1 << 4) /* insert a blank output line */
257 #define MMAN_PP (1 << 5) /* reset indentation etc. */
258 #define MMAN_Sm (1 << 6) /* horizontal spacing mode */
259 #define MMAN_Bk (1 << 7) /* word keep mode */
260 #define MMAN_Bk_susp (1 << 8) /* suspend this (after a macro) */
261 #define MMAN_An_split (1 << 9) /* author mode is "split" */
262 #define MMAN_An_nosplit (1 << 10) /* author mode is "nosplit" */
263 #define MMAN_PD (1 << 11) /* inter-paragraph spacing disabled */
264 #define MMAN_nbrword (1 << 12) /* do not break the next word */
266 #define BL_STACK_MAX 32
268 static int Bl_stack[BL_STACK_MAX]; /* offsets [chars] */
269 static int Bl_stack_post[BL_STACK_MAX]; /* add final .RE */
270 static int Bl_stack_len; /* number of nested Bl blocks */
271 static int TPremain; /* characters before tag is full */
281 font_push(char newfont)
284 if (fontqueue.head + fontqueue.size <= ++fontqueue.tail) {
286 fontqueue.head = mandoc_realloc(fontqueue.head,
289 *fontqueue.tail = newfont;
293 outflags &= ~MMAN_spc;
300 if (fontqueue.tail > fontqueue.head)
302 outflags &= ~MMAN_spc;
305 putchar(*fontqueue.tail);
309 print_word(const char *s)
312 if ((MMAN_PP | MMAN_sp | MMAN_br | MMAN_nl) & outflags) {
314 * If we need a newline, print it now and start afresh.
316 if (MMAN_PP & outflags) {
317 if (MMAN_sp & outflags) {
318 if (MMAN_PD & outflags) {
320 outflags &= ~MMAN_PD;
322 } else if ( ! (MMAN_PD & outflags)) {
327 } else if (MMAN_sp & outflags)
329 else if (MMAN_br & outflags)
331 else if (MMAN_nl & outflags)
333 outflags &= ~(MMAN_PP|MMAN_sp|MMAN_br|MMAN_nl|MMAN_spc);
337 } else if (MMAN_spc & outflags) {
339 * If we need a space, only print it if
340 * (1) it is forced by `No' or
341 * (2) what follows is not terminating punctuation or
342 * (3) what follows is longer than one character.
344 if (MMAN_spc_force & outflags || '\0' == s[0] ||
345 NULL == strchr(".,:;)]?!", s[0]) || '\0' != s[1]) {
346 if (MMAN_Bk & outflags &&
347 ! (MMAN_Bk_susp & outflags))
356 * Reassign needing space if we're not following opening
359 if (MMAN_Sm & outflags && ('\0' == s[0] ||
360 (('(' != s[0] && '[' != s[0]) || '\0' != s[1])))
361 outflags |= MMAN_spc;
363 outflags &= ~MMAN_spc;
364 outflags &= ~(MMAN_spc_force | MMAN_Bk_susp);
378 if (MMAN_nbrword & outflags) {
384 putchar((unsigned char)*s);
390 outflags &= ~MMAN_nbrword;
394 print_line(const char *s, int newflags)
397 outflags &= ~MMAN_br;
400 outflags |= newflags;
404 print_block(const char *s, int newflags)
407 outflags &= ~MMAN_PP;
408 if (MMAN_sp & outflags) {
409 outflags &= ~(MMAN_sp | MMAN_br);
410 if (MMAN_PD & outflags) {
411 print_line(".PD", 0);
412 outflags &= ~MMAN_PD;
414 } else if (! (MMAN_PD & outflags))
415 print_line(".PD 0", MMAN_PD);
418 outflags |= MMAN_Bk_susp | newflags;
422 print_offs(const char *v, int keywords)
428 print_line(".RS", MMAN_Bk_susp);
430 /* Convert v into a number (of characters). */
431 if (NULL == v || '\0' == *v || (keywords && !strcmp(v, "left")))
433 else if (keywords && !strcmp(v, "indent"))
435 else if (keywords && !strcmp(v, "indent-two"))
437 else if (a2roffsu(v, &su, SCALE_EN) > 1) {
438 if (SCALE_EN == su.unit)
443 * If we are inside an enclosing list,
444 * there is no easy way to add the two
445 * indentations because they are provided
446 * in terms of different units.
456 * We are inside an enclosing list.
457 * Add the two indentations.
460 sz += Bl_stack[Bl_stack_len - 1];
462 (void)snprintf(buf, sizeof(buf), "%dn", sz);
468 * Set up the indentation for a list item; used from pre_it().
471 print_width(const struct mdoc_bl *bl, const struct roff_node *child)
475 int numeric, remain, sz, chsz;
480 /* Convert the width into a number (of characters). */
481 if (bl->width == NULL)
482 sz = (bl->type == LIST_hang) ? 6 : 0;
483 else if (a2roffsu(bl->width, &su, SCALE_MAX) > 1) {
484 if (SCALE_EN == su.unit)
491 sz = strlen(bl->width);
493 /* XXX Rough estimation, might have multiple parts. */
494 if (bl->type == LIST_enum)
495 chsz = (bl->count > 8) + 1;
496 else if (child != NULL && child->type == ROFFT_TEXT)
497 chsz = strlen(child->string);
501 /* Maybe we are inside an enclosing list? */
505 * Save our own indentation,
506 * such that child lists can use it.
508 Bl_stack[Bl_stack_len++] = sz + 2;
510 /* Set up the current list. */
511 if (chsz > sz && bl->type != LIST_tag)
512 print_block(".HP", 0);
514 print_block(".TP", 0);
518 (void)snprintf(buf, sizeof(buf), "%dn", sz + 2);
521 print_word(bl->width);
526 print_count(int *count)
530 (void)snprintf(buf, sizeof(buf), "%d.\\&", ++*count);
535 man_man(void *arg, const struct roff_man *man)
539 * Dump the keep buffer.
540 * We're guaranteed by now that this exists (is non-NULL).
541 * Flush stdout afterward, just in case.
543 fputs(mparse_getkeep(man_mparse(man)), stdout);
548 man_mdoc(void *arg, const struct roff_man *mdoc)
552 printf(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"\n",
554 (mdoc->meta.msec == NULL ? "" : mdoc->meta.msec),
555 mdoc->meta.date, mdoc->meta.os, mdoc->meta.vol);
557 /* Disable hyphenation and if nroff, disable justification. */
558 printf(".nh\n.if n .ad l");
560 outflags = MMAN_nl | MMAN_Sm;
561 if (0 == fontqueue.size) {
563 fontqueue.head = fontqueue.tail = mandoc_malloc(8);
564 *fontqueue.tail = 'R';
566 for (n = mdoc->first->child; n != NULL; n = n->next)
567 print_node(&mdoc->meta, n);
572 print_node(DECL_ARGS)
574 const struct manact *act;
575 struct roff_node *sub;
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 && MDOC_LINE & n->flags)
588 n->flags &= ~MDOC_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 (outflags & MMAN_Sm && ! (n->flags & MDOC_DELIMC))
602 outflags |= MMAN_spc_force;
603 print_word(n->string);
604 if (outflags & MMAN_Sm && ! (n->flags & MDOC_DELIMO))
605 outflags |= MMAN_spc;
608 * Conditionally run the pre-node action handler for a
611 act = manacts + n->tok;
612 cond = act->cond == NULL || (*act->cond)(meta, n);
613 if (cond && act->pre != NULL &&
614 (n->end == ENDBODY_NOT || n->child != NULL))
615 do_sub = (*act->pre)(meta, n);
619 * Conditionally run all child nodes.
620 * Note that this iterates over children instead of using
621 * recursion. This prevents unnecessary depth in the stack.
624 for (sub = n->child; sub; sub = sub->next)
625 print_node(meta, sub);
628 * Lastly, conditionally run the post-node handler.
630 if (MDOC_ENDED & n->flags)
633 if (cond && act->post)
634 (*act->post)(meta, n);
636 if (ENDBODY_NOT != n->end)
637 n->body->flags |= MDOC_ENDED;
639 if (ENDBODY_NOSPACE == n->end)
640 outflags &= ~(MMAN_spc | MMAN_nl);
647 return n->type == ROFFT_HEAD;
654 return n->type == ROFFT_BODY;
662 prefix = manacts[n->tok].prefix;
666 outflags &= ~MMAN_spc;
675 suffix = manacts[n->tok].suffix;
678 outflags &= ~(MMAN_spc | MMAN_nl);
685 struct roff_node *nch;
687 outflags |= MMAN_br | MMAN_nl;
691 for (nch = n->child; nch != NULL; nch = nch->next) {
693 print_word(nch->string);
696 if (nch->next == NULL)
699 if (nch->prev != NULL || nch->next->next != NULL) {
700 outflags &= ~MMAN_spc;
703 if (nch->next->next == NULL)
707 if (n->child != NULL && n->child->next != NULL)
708 print_word("utilities exit\\~0");
710 print_word("utility exits\\~0");
712 print_word("on success, and\\~>0 if an error occurs.");
725 post_percent(DECL_ARGS)
728 if (pre_em == manacts[n->tok].pre)
732 if (n->prev && n->prev->tok == n->tok &&
733 n->next->tok == n->tok)
745 if (n->parent && MDOC_Rs == n->parent->tok &&
746 n->parent->norm->Rs.quote_T) {
749 outflags &= ~MMAN_spc;
759 if (n->parent && MDOC_Rs == n->parent->tok &&
760 n->parent->norm->Rs.quote_T) {
761 outflags &= ~MMAN_spc;
766 post_percent(meta, n);
770 * Print before a section header.
776 if (n->type == ROFFT_HEAD) {
778 print_block(manacts[n->tok].prefix, 0);
781 outflags &= ~MMAN_spc;
787 * Print subsequent a section header.
793 if (n->type != ROFFT_HEAD)
795 outflags &= ~MMAN_spc;
799 if (MDOC_Sh == n->tok && SEC_AUTHORS == n->sec)
800 outflags &= ~(MMAN_An_split | MMAN_An_nosplit);
803 /* See mdoc_term.c, synopsis_pre() for comments. */
805 pre_syn(const struct roff_node *n)
808 if (NULL == n->prev || ! (MDOC_SYNPRETTY & n->flags))
811 if (n->prev->tok == n->tok &&
819 switch (n->prev->tok) {
828 if (MDOC_Fn != n->tok && MDOC_Fo != n->tok) {
843 switch (n->norm->An.auth) {
845 outflags &= ~MMAN_An_nosplit;
846 outflags |= MMAN_An_split;
849 outflags &= ~MMAN_An_split;
850 outflags |= MMAN_An_nosplit;
853 if (MMAN_An_split & outflags)
855 else if (SEC_AUTHORS == n->sec &&
856 ! (MMAN_An_nosplit & outflags))
857 outflags |= MMAN_An_split;
866 outflags &= ~MMAN_spc;
868 outflags &= ~MMAN_spc;
876 print_word(n->child != NULL && n->child->next == NULL &&
877 n->child->tok == MDOC_Mt ? "<" : "\\(la");
878 outflags &= ~MMAN_spc;
886 outflags &= ~(MMAN_spc | MMAN_nl);
887 print_word(n->child != NULL && n->child->next == NULL &&
888 n->child->tok == MDOC_Mt ? ">" : "\\(ra");
895 outflags &= ~(MMAN_PP | MMAN_sp | MMAN_br);
897 if (DISP_unfilled == n->norm->Bd.type ||
898 DISP_literal == n->norm->Bd.type)
899 print_line(".nf", 0);
900 if (0 == n->norm->Bd.comp && NULL != n->parent->prev)
902 print_offs(n->norm->Bd.offs, 1);
910 /* Close out this display. */
911 print_line(".RE", MMAN_nl);
912 if (DISP_unfilled == n->norm->Bd.type ||
913 DISP_literal == n->norm->Bd.type)
914 print_line(".fi", MMAN_nl);
916 /* Maybe we are inside an enclosing list? */
917 if (NULL != n->parent->next)
933 switch (n->norm->Bf.font) {
951 if (n->type == ROFFT_BODY)
974 if (n->type == ROFFT_BODY)
975 outflags &= ~MMAN_Bk;
984 * print_offs() will increase the -offset to account for
985 * a possible enclosing .It, but any enclosed .It blocks
986 * just nest and do not add up their indentation.
988 if (n->norm->Bl.offs) {
989 print_offs(n->norm->Bl.offs, 0);
990 Bl_stack[Bl_stack_len++] = 0;
993 switch (n->norm->Bl.type) {
995 n->norm->Bl.count = 0;
1003 if (n->child != NULL) {
1004 print_line(".TS", MMAN_nl);
1005 for (icol = 0; icol < n->norm->Bl.ncols; icol++)
1009 outflags |= MMAN_nl;
1017 switch (n->norm->Bl.type) {
1019 if (n->child != NULL)
1020 print_line(".TE", 0);
1023 n->norm->Bl.count = 0;
1029 if (n->norm->Bl.offs) {
1030 print_line(".RE", MMAN_nl);
1031 assert(Bl_stack_len);
1033 assert(0 == Bl_stack[Bl_stack_len]);
1035 outflags |= MMAN_PP | MMAN_nl;
1036 outflags &= ~(MMAN_sp | MMAN_br);
1039 /* Maybe we are inside an enclosing list? */
1040 if (NULL != n->parent->next)
1049 outflags |= MMAN_br;
1059 print_word(n->string);
1060 outflags &= ~MMAN_spc;
1066 outflags &= ~MMAN_spc;
1068 outflags &= ~MMAN_spc;
1069 print_word(n->string);
1077 print_offs("6n", 0);
1085 print_line(".RE", MMAN_nl);
1087 /* Maybe we are inside an enclosing list? */
1088 if (NULL != n->parent->next)
1104 if (NULL == n->norm->Es ||
1105 NULL == n->norm->Es->child)
1108 print_word(n->norm->Es->child->string);
1109 outflags &= ~MMAN_spc;
1117 if (NULL == n->norm->Es ||
1118 NULL == n->norm->Es->child ||
1119 NULL == n->norm->Es->child->next)
1122 outflags &= ~MMAN_spc;
1123 print_word(n->norm->Es->child->next->string);
1131 if (n->end == ENDBODY_NOT &&
1132 n->parent->head->child == NULL &&
1134 n->child->end != ENDBODY_NOT)
1136 else if (n->end != ENDBODY_NOT ? n->child != NULL :
1137 n->parent->head->child != NULL && (n->child != NULL ||
1138 (n->parent->tail != NULL && n->parent->tail->child != NULL)))
1139 outflags &= ~(MMAN_spc | MMAN_nl);
1148 if (n->end != ENDBODY_NOT) {
1149 outflags |= MMAN_spc;
1153 body = n->child != NULL || n->parent->head->child != NULL;
1154 tail = n->parent->tail != NULL && n->parent->tail->child != NULL;
1157 outflags &= ~MMAN_spc;
1158 else if ( ! (body || tail))
1161 outflags |= MMAN_spc;
1169 am_Fa = MDOC_Fa == n->tok;
1176 if (am_Fa || MDOC_SYNPRETTY & n->flags)
1177 outflags |= MMAN_nbrword;
1178 print_node(meta, n);
1180 if (NULL != (n = n->next))
1190 if (NULL != n->next && MDOC_Fa == n->next->tok)
1208 outflags |= MMAN_br;
1217 if (n->child != NULL)
1218 outflags &= ~MMAN_spc;
1227 if (!(n->child != NULL ||
1229 n->next->type == ROFFT_TEXT ||
1230 n->next->flags & MDOC_LINE))
1231 outflags &= ~MMAN_spc;
1244 if (MDOC_SYNPRETTY & n->flags)
1245 print_block(".HP 4n", MMAN_nl);
1248 print_node(meta, n);
1250 outflags &= ~MMAN_spc;
1252 outflags &= ~MMAN_spc;
1265 if (MDOC_SYNPRETTY & n->flags) {
1267 outflags |= MMAN_PP;
1280 if (n->child == NULL)
1282 if (MDOC_SYNPRETTY & n->flags)
1283 print_block(".HP 4n", MMAN_nl);
1287 outflags &= ~(MMAN_spc | MMAN_nl);
1289 outflags &= ~MMAN_spc;
1303 if (n->child != NULL)
1327 if (MDOC_SYNPRETTY & n->flags) {
1330 print_word("#include <");
1331 outflags &= ~MMAN_spc;
1334 outflags &= ~MMAN_spc;
1344 if (MDOC_SYNPRETTY & n->flags) {
1345 outflags &= ~MMAN_spc;
1348 outflags |= MMAN_br;
1351 outflags &= ~MMAN_spc;
1359 const struct roff_node *bln;
1363 outflags |= MMAN_PP | MMAN_nl;
1364 bln = n->parent->parent;
1365 if (0 == bln->norm->Bl.comp ||
1366 (NULL == n->parent->prev &&
1367 NULL == bln->parent->prev))
1368 outflags |= MMAN_sp;
1369 outflags &= ~MMAN_br;
1370 switch (bln->norm->Bl.type) {
1376 if (bln->norm->Bl.type == LIST_diag)
1377 print_line(".B \"", 0);
1379 print_line(".R \"", 0);
1380 outflags &= ~MMAN_spc;
1385 print_width(&bln->norm->Bl, NULL);
1387 outflags |= MMAN_nl;
1389 if (LIST_bullet == bln->norm->Bl.type)
1390 print_word("\\(bu");
1394 outflags |= MMAN_nl;
1397 print_width(&bln->norm->Bl, NULL);
1399 outflags |= MMAN_nl;
1400 print_count(&bln->norm->Bl.count);
1401 outflags |= MMAN_nl;
1404 print_width(&bln->norm->Bl, n->child);
1406 outflags |= MMAN_nl;
1409 print_width(&bln->norm->Bl, n->child);
1411 outflags &= ~MMAN_spc;
1423 * This function is called after closing out an indented block.
1424 * If we are inside an enclosing list, restore its indentation.
1431 /* Nothing to do outside a list. */
1432 if (0 == Bl_stack_len || 0 == Bl_stack[Bl_stack_len - 1])
1435 /* The indentation has already been set up. */
1436 if (Bl_stack_post[Bl_stack_len - 1])
1439 /* Restore the indentation of the enclosing list. */
1440 print_line(".RS", MMAN_Bk_susp);
1441 (void)snprintf(buf, sizeof(buf), "%dn",
1442 Bl_stack[Bl_stack_len - 1]);
1445 /* Remeber to close out this .RS block later. */
1446 Bl_stack_post[Bl_stack_len - 1] = 1;
1452 const struct roff_node *bln;
1454 bln = n->parent->parent;
1458 switch (bln->norm->Bl.type) {
1460 outflags &= ~MMAN_spc;
1464 outflags |= MMAN_br;
1471 switch (bln->norm->Bl.type) {
1478 assert(Bl_stack_len);
1479 Bl_stack[--Bl_stack_len] = 0;
1482 * Our indentation had to be restored
1483 * after a child display or child list.
1484 * Close out that indentation block now.
1486 if (Bl_stack_post[Bl_stack_len]) {
1487 print_line(".RE", MMAN_nl);
1488 Bl_stack_post[Bl_stack_len] = 0;
1492 if (NULL != n->next) {
1494 outflags &= ~MMAN_spc;
1510 if (SEC_LIBRARY == n->sec)
1511 outflags |= MMAN_br;
1517 const struct roff_node *link, *descr;
1519 if (NULL == (link = n->child))
1522 if (NULL != (descr = link->next)) {
1524 while (NULL != descr) {
1525 print_word(descr->string);
1526 descr = descr->next;
1533 print_word(link->string);
1542 print_line(".ll", 0);
1559 if (n->type == ROFFT_BLOCK) {
1560 outflags |= MMAN_Bk;
1563 if (n->type != ROFFT_ELEM && n->type != ROFFT_HEAD)
1565 name = n->child ? n->child->string : meta->name;
1568 if (n->type == ROFFT_HEAD) {
1569 if (NULL == n->parent->prev)
1570 outflags |= MMAN_sp;
1571 print_block(".HP", 0);
1572 printf(" %zun", strlen(name) + 1);
1573 outflags |= MMAN_nl;
1576 if (NULL == n->child)
1577 print_word(meta->name);
1587 outflags &= ~MMAN_Bk;
1591 if (n->child != NULL || meta->name != NULL)
1603 outflags |= MMAN_spc_force;
1611 outflags &= ~MMAN_spc;
1619 if ( ! (n->next == NULL || n->next->flags & MDOC_LINE))
1620 outflags &= ~MMAN_spc;
1627 if (MDOC_It != n->parent->tok)
1628 outflags |= MMAN_PP;
1629 outflags |= MMAN_sp | MMAN_nl;
1630 outflags &= ~MMAN_br;
1638 if (SEC_SEE_ALSO == n->sec) {
1639 outflags |= MMAN_PP | MMAN_sp | MMAN_nl;
1640 outflags &= ~MMAN_br;
1648 struct roff_node *nch;
1650 outflags |= MMAN_br | MMAN_nl;
1652 if (n->child != NULL) {
1655 for (nch = n->child; nch != NULL; nch = nch->next) {
1657 print_word(nch->string);
1660 outflags &= ~MMAN_spc;
1663 if (nch->next == NULL)
1666 if (nch->prev != NULL || nch->next->next != NULL) {
1667 outflags &= ~MMAN_spc;
1670 if (nch->next->next == NULL)
1674 if (n->child != NULL && n->child->next != NULL)
1675 print_word("functions return");
1677 print_word("function returns");
1679 print_word("the value\\~0 if successful;");
1681 print_word("Upon successful completion, "
1682 "the value\\~0 is returned;");
1684 print_word("otherwise the value\\~\\-1 is returned"
1685 " and the global variable");
1688 print_word("errno");
1691 print_word("is set to indicate the error.");
1692 outflags |= MMAN_nl;
1707 if (NULL == n->child)
1708 outflags ^= MMAN_Sm;
1709 else if (0 == strcmp("on", n->child->string))
1710 outflags |= MMAN_Sm;
1712 outflags &= ~MMAN_Sm;
1714 if (MMAN_Sm & outflags)
1715 outflags |= MMAN_spc;
1724 if (MMAN_PP & outflags) {
1725 outflags &= ~MMAN_PP;
1726 print_line(".PP", 0);
1728 print_line(".sp", 0);
1736 outflags |= MMAN_nl;
1751 if (MDOC_SYNPRETTY & n->flags) {
1770 if (n->flags & MDOC_SYNPRETTY && n->type != ROFFT_BODY)
1782 print_node(meta, n);
1786 outflags &= ~MMAN_spc;
1788 print_node(meta, n);
1797 print_word(manacts[n->tok].prefix);
1798 if (NULL == n->child)
1800 outflags &= ~MMAN_spc;
1802 outflags &= ~MMAN_spc;