]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_man.c
When a man(7) document contains unreasonably large numbers for
[mandoc.git] / mdoc_man.c
1 /* $Id: mdoc_man.c,v 1.80 2014/12/23 13:48:57 schwarze Exp $ */
2 /*
3 * Copyright (c) 2011, 2012, 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
4 *
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.
8 *
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.
16 */
17 #include "config.h"
18
19 #include <sys/types.h>
20
21 #include <assert.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include "mandoc.h"
26 #include "mandoc_aux.h"
27 #include "out.h"
28 #include "man.h"
29 #include "mdoc.h"
30 #include "main.h"
31
32 #define DECL_ARGS const struct mdoc_meta *meta, \
33 const struct mdoc_node *n
34
35 struct manact {
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 */
41 };
42
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 mdoc_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 char *,
120 const struct mdoc_node *, size_t);
121 static void print_count(int *);
122 static void print_node(DECL_ARGS);
123
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 */
249 };
250
251 static int outflags;
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 */
265
266 #define BL_STACK_MAX 32
267
268 static size_t 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 */
272
273 static struct {
274 char *head;
275 char *tail;
276 size_t size;
277 } fontqueue;
278
279
280 static void
281 font_push(char newfont)
282 {
283
284 if (fontqueue.head + fontqueue.size <= ++fontqueue.tail) {
285 fontqueue.size += 8;
286 fontqueue.head = mandoc_realloc(fontqueue.head,
287 fontqueue.size);
288 }
289 *fontqueue.tail = newfont;
290 print_word("");
291 printf("\\f");
292 putchar(newfont);
293 outflags &= ~MMAN_spc;
294 }
295
296 static void
297 font_pop(void)
298 {
299
300 if (fontqueue.tail > fontqueue.head)
301 fontqueue.tail--;
302 outflags &= ~MMAN_spc;
303 print_word("");
304 printf("\\f");
305 putchar(*fontqueue.tail);
306 }
307
308 static void
309 print_word(const char *s)
310 {
311
312 if ((MMAN_PP | MMAN_sp | MMAN_br | MMAN_nl) & outflags) {
313 /*
314 * If we need a newline, print it now and start afresh.
315 */
316 if (MMAN_PP & outflags) {
317 if (MMAN_sp & outflags) {
318 if (MMAN_PD & outflags) {
319 printf("\n.PD");
320 outflags &= ~MMAN_PD;
321 }
322 } else if ( ! (MMAN_PD & outflags)) {
323 printf("\n.PD 0");
324 outflags |= MMAN_PD;
325 }
326 printf("\n.PP\n");
327 } else if (MMAN_sp & outflags)
328 printf("\n.sp\n");
329 else if (MMAN_br & outflags)
330 printf("\n.br\n");
331 else if (MMAN_nl & outflags)
332 putchar('\n');
333 outflags &= ~(MMAN_PP|MMAN_sp|MMAN_br|MMAN_nl|MMAN_spc);
334 if (1 == TPremain)
335 printf(".br\n");
336 TPremain = 0;
337 } else if (MMAN_spc & outflags) {
338 /*
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.
343 */
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))
348 putchar('\\');
349 putchar(' ');
350 if (TPremain)
351 TPremain--;
352 }
353 }
354
355 /*
356 * Reassign needing space if we're not following opening
357 * punctuation.
358 */
359 if (MMAN_Sm & outflags && ('\0' == s[0] ||
360 (('(' != s[0] && '[' != s[0]) || '\0' != s[1])))
361 outflags |= MMAN_spc;
362 else
363 outflags &= ~MMAN_spc;
364 outflags &= ~(MMAN_spc_force | MMAN_Bk_susp);
365
366 for ( ; *s; s++) {
367 switch (*s) {
368 case ASCII_NBRSP:
369 printf("\\ ");
370 break;
371 case ASCII_HYPH:
372 putchar('-');
373 break;
374 case ASCII_BREAK:
375 printf("\\:");
376 break;
377 case ' ':
378 if (MMAN_nbrword & outflags) {
379 printf("\\ ");
380 break;
381 }
382 /* FALLTHROUGH */
383 default:
384 putchar((unsigned char)*s);
385 break;
386 }
387 if (TPremain)
388 TPremain--;
389 }
390 outflags &= ~MMAN_nbrword;
391 }
392
393 static void
394 print_line(const char *s, int newflags)
395 {
396
397 outflags &= ~MMAN_br;
398 outflags |= MMAN_nl;
399 print_word(s);
400 outflags |= newflags;
401 }
402
403 static void
404 print_block(const char *s, int newflags)
405 {
406
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;
413 }
414 } else if (! (MMAN_PD & outflags))
415 print_line(".PD 0", MMAN_PD);
416 outflags |= MMAN_nl;
417 print_word(s);
418 outflags |= MMAN_Bk_susp | newflags;
419 }
420
421 static void
422 print_offs(const char *v, int keywords)
423 {
424 char buf[24];
425 struct roffsu su;
426 size_t sz;
427
428 print_line(".RS", MMAN_Bk_susp);
429
430 /* Convert v into a number (of characters). */
431 if (NULL == v || '\0' == *v || (keywords && !strcmp(v, "left")))
432 sz = 0;
433 else if (keywords && !strcmp(v, "indent"))
434 sz = 6;
435 else if (keywords && !strcmp(v, "indent-two"))
436 sz = 12;
437 else if (a2roffsu(v, &su, SCALE_EN) > 1) {
438 if (su.scale < 0.0)
439 su.scale = 0.0;
440 if (SCALE_EN == su.unit)
441 sz = su.scale;
442 else {
443 /*
444 * XXX
445 * If we are inside an enclosing list,
446 * there is no easy way to add the two
447 * indentations because they are provided
448 * in terms of different units.
449 */
450 print_word(v);
451 outflags |= MMAN_nl;
452 return;
453 }
454 } else
455 sz = strlen(v);
456
457 /*
458 * We are inside an enclosing list.
459 * Add the two indentations.
460 */
461 if (Bl_stack_len)
462 sz += Bl_stack[Bl_stack_len - 1];
463
464 (void)snprintf(buf, sizeof(buf), "%zun", sz);
465 print_word(buf);
466 outflags |= MMAN_nl;
467 }
468
469 /*
470 * Set up the indentation for a list item; used from pre_it().
471 */
472 static void
473 print_width(const char *v, const struct mdoc_node *child, size_t defsz)
474 {
475 char buf[24];
476 struct roffsu su;
477 size_t sz, chsz;
478 int numeric, remain;
479
480 numeric = 1;
481 remain = 0;
482
483 /* Convert v into a number (of characters). */
484 if (NULL == v)
485 sz = defsz;
486 else if (a2roffsu(v, &su, SCALE_MAX) > 1) {
487 if (su.scale < 0.0)
488 su.scale = 0.0;
489 if (SCALE_EN == su.unit)
490 sz = su.scale;
491 else {
492 sz = 0;
493 numeric = 0;
494 }
495 } else
496 sz = strlen(v);
497
498 /* XXX Rough estimation, might have multiple parts. */
499 chsz = (NULL != child && MDOC_TEXT == child->type) ?
500 strlen(child->string) : 0;
501
502 /* Maybe we are inside an enclosing list? */
503 mid_it();
504
505 /*
506 * Save our own indentation,
507 * such that child lists can use it.
508 */
509 Bl_stack[Bl_stack_len++] = sz + 2;
510
511 /* Set up the current list. */
512 if (defsz && chsz > sz)
513 print_block(".HP", 0);
514 else {
515 print_block(".TP", 0);
516 remain = sz + 2;
517 }
518 if (numeric) {
519 (void)snprintf(buf, sizeof(buf), "%zun", sz + 2);
520 print_word(buf);
521 } else
522 print_word(v);
523 TPremain = remain;
524 }
525
526 static void
527 print_count(int *count)
528 {
529 char buf[24];
530
531 (void)snprintf(buf, sizeof(buf), "%d.", ++*count);
532 print_word(buf);
533 }
534
535 void
536 man_man(void *arg, const struct man *man)
537 {
538
539 /*
540 * Dump the keep buffer.
541 * We're guaranteed by now that this exists (is non-NULL).
542 * Flush stdout afterward, just in case.
543 */
544 fputs(mparse_getkeep(man_mparse(man)), stdout);
545 fflush(stdout);
546 }
547
548 void
549 man_mdoc(void *arg, const struct mdoc *mdoc)
550 {
551 const struct mdoc_meta *meta;
552 const struct mdoc_node *n;
553
554 meta = mdoc_meta(mdoc);
555 n = mdoc_node(mdoc);
556
557 printf(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"\n",
558 meta->title,
559 (meta->msec == NULL ? "" : meta->msec),
560 meta->date, meta->os, meta->vol);
561
562 /* Disable hyphenation and if nroff, disable justification. */
563 printf(".nh\n.if n .ad l");
564
565 outflags = MMAN_nl | MMAN_Sm;
566 if (0 == fontqueue.size) {
567 fontqueue.size = 8;
568 fontqueue.head = fontqueue.tail = mandoc_malloc(8);
569 *fontqueue.tail = 'R';
570 }
571 print_node(meta, n);
572 putchar('\n');
573 }
574
575 static void
576 print_node(DECL_ARGS)
577 {
578 const struct mdoc_node *sub;
579 const struct manact *act;
580 int cond, do_sub;
581
582 /*
583 * Break the line if we were parsed subsequent the current node.
584 * This makes the page structure be more consistent.
585 */
586 if (MMAN_spc & outflags && MDOC_LINE & n->flags)
587 outflags |= MMAN_nl;
588
589 act = NULL;
590 cond = 0;
591 do_sub = 1;
592
593 if (MDOC_TEXT == n->type) {
594 /*
595 * Make sure that we don't happen to start with a
596 * control character at the start of a line.
597 */
598 if (MMAN_nl & outflags &&
599 ('.' == *n->string || '\'' == *n->string)) {
600 print_word("");
601 printf("\\&");
602 outflags &= ~MMAN_spc;
603 }
604 if (outflags & MMAN_Sm && ! (n->flags & MDOC_DELIMC))
605 outflags |= MMAN_spc_force;
606 print_word(n->string);
607 if (outflags & MMAN_Sm && ! (n->flags & MDOC_DELIMO))
608 outflags |= MMAN_spc;
609 } else {
610 /*
611 * Conditionally run the pre-node action handler for a
612 * node.
613 */
614 act = manacts + n->tok;
615 cond = act->cond == NULL || (*act->cond)(meta, n);
616 if (cond && act->pre && (n->end == ENDBODY_NOT || n->nchild))
617 do_sub = (*act->pre)(meta, n);
618 }
619
620 /*
621 * Conditionally run all child nodes.
622 * Note that this iterates over children instead of using
623 * recursion. This prevents unnecessary depth in the stack.
624 */
625 if (do_sub)
626 for (sub = n->child; sub; sub = sub->next)
627 print_node(meta, sub);
628
629 /*
630 * Lastly, conditionally run the post-node handler.
631 */
632 if (MDOC_ENDED & n->flags)
633 return;
634
635 if (cond && act->post)
636 (*act->post)(meta, n);
637
638 if (ENDBODY_NOT != n->end)
639 n->pending->flags |= MDOC_ENDED;
640
641 if (ENDBODY_NOSPACE == n->end)
642 outflags &= ~(MMAN_spc | MMAN_nl);
643 }
644
645 static int
646 cond_head(DECL_ARGS)
647 {
648
649 return(MDOC_HEAD == n->type);
650 }
651
652 static int
653 cond_body(DECL_ARGS)
654 {
655
656 return(MDOC_BODY == n->type);
657 }
658
659 static int
660 pre_enc(DECL_ARGS)
661 {
662 const char *prefix;
663
664 prefix = manacts[n->tok].prefix;
665 if (NULL == prefix)
666 return(1);
667 print_word(prefix);
668 outflags &= ~MMAN_spc;
669 return(1);
670 }
671
672 static void
673 post_enc(DECL_ARGS)
674 {
675 const char *suffix;
676
677 suffix = manacts[n->tok].suffix;
678 if (NULL == suffix)
679 return;
680 outflags &= ~(MMAN_spc | MMAN_nl);
681 print_word(suffix);
682 }
683
684 static int
685 pre_ex(DECL_ARGS)
686 {
687 int nchild;
688
689 outflags |= MMAN_br | MMAN_nl;
690
691 print_word("The");
692
693 nchild = n->nchild;
694 for (n = n->child; n; n = n->next) {
695 font_push('B');
696 print_word(n->string);
697 font_pop();
698
699 if (n->next == NULL)
700 continue;
701
702 if (nchild > 2) {
703 outflags &= ~MMAN_spc;
704 print_word(",");
705 }
706 if (n->next->next == NULL)
707 print_word("and");
708 }
709
710 if (nchild > 1)
711 print_word("utilities exit\\~0");
712 else
713 print_word("utility exits\\~0");
714
715 print_word("on success, and\\~>0 if an error occurs.");
716 outflags |= MMAN_nl;
717 return(0);
718 }
719
720 static void
721 post_font(DECL_ARGS)
722 {
723
724 font_pop();
725 }
726
727 static void
728 post_percent(DECL_ARGS)
729 {
730
731 if (pre_em == manacts[n->tok].pre)
732 font_pop();
733 if (n->next) {
734 print_word(",");
735 if (n->prev && n->prev->tok == n->tok &&
736 n->next->tok == n->tok)
737 print_word("and");
738 } else {
739 print_word(".");
740 outflags |= MMAN_nl;
741 }
742 }
743
744 static int
745 pre__t(DECL_ARGS)
746 {
747
748 if (n->parent && MDOC_Rs == n->parent->tok &&
749 n->parent->norm->Rs.quote_T) {
750 print_word("");
751 putchar('\"');
752 outflags &= ~MMAN_spc;
753 } else
754 font_push('I');
755 return(1);
756 }
757
758 static void
759 post__t(DECL_ARGS)
760 {
761
762 if (n->parent && MDOC_Rs == n->parent->tok &&
763 n->parent->norm->Rs.quote_T) {
764 outflags &= ~MMAN_spc;
765 print_word("");
766 putchar('\"');
767 } else
768 font_pop();
769 post_percent(meta, n);
770 }
771
772 /*
773 * Print before a section header.
774 */
775 static int
776 pre_sect(DECL_ARGS)
777 {
778
779 if (MDOC_HEAD == n->type) {
780 outflags |= MMAN_sp;
781 print_block(manacts[n->tok].prefix, 0);
782 print_word("");
783 putchar('\"');
784 outflags &= ~MMAN_spc;
785 }
786 return(1);
787 }
788
789 /*
790 * Print subsequent a section header.
791 */
792 static void
793 post_sect(DECL_ARGS)
794 {
795
796 if (MDOC_HEAD != n->type)
797 return;
798 outflags &= ~MMAN_spc;
799 print_word("");
800 putchar('\"');
801 outflags |= MMAN_nl;
802 if (MDOC_Sh == n->tok && SEC_AUTHORS == n->sec)
803 outflags &= ~(MMAN_An_split | MMAN_An_nosplit);
804 }
805
806 /* See mdoc_term.c, synopsis_pre() for comments. */
807 static void
808 pre_syn(const struct mdoc_node *n)
809 {
810
811 if (NULL == n->prev || ! (MDOC_SYNPRETTY & n->flags))
812 return;
813
814 if (n->prev->tok == n->tok &&
815 MDOC_Ft != n->tok &&
816 MDOC_Fo != n->tok &&
817 MDOC_Fn != n->tok) {
818 outflags |= MMAN_br;
819 return;
820 }
821
822 switch (n->prev->tok) {
823 case MDOC_Fd:
824 /* FALLTHROUGH */
825 case MDOC_Fn:
826 /* FALLTHROUGH */
827 case MDOC_Fo:
828 /* FALLTHROUGH */
829 case MDOC_In:
830 /* FALLTHROUGH */
831 case MDOC_Vt:
832 outflags |= MMAN_sp;
833 break;
834 case MDOC_Ft:
835 if (MDOC_Fn != n->tok && MDOC_Fo != n->tok) {
836 outflags |= MMAN_sp;
837 break;
838 }
839 /* FALLTHROUGH */
840 default:
841 outflags |= MMAN_br;
842 break;
843 }
844 }
845
846 static int
847 pre_an(DECL_ARGS)
848 {
849
850 switch (n->norm->An.auth) {
851 case AUTH_split:
852 outflags &= ~MMAN_An_nosplit;
853 outflags |= MMAN_An_split;
854 return(0);
855 case AUTH_nosplit:
856 outflags &= ~MMAN_An_split;
857 outflags |= MMAN_An_nosplit;
858 return(0);
859 default:
860 if (MMAN_An_split & outflags)
861 outflags |= MMAN_br;
862 else if (SEC_AUTHORS == n->sec &&
863 ! (MMAN_An_nosplit & outflags))
864 outflags |= MMAN_An_split;
865 return(1);
866 }
867 }
868
869 static int
870 pre_ap(DECL_ARGS)
871 {
872
873 outflags &= ~MMAN_spc;
874 print_word("'");
875 outflags &= ~MMAN_spc;
876 return(0);
877 }
878
879 static int
880 pre_aq(DECL_ARGS)
881 {
882
883 print_word(n->parent->prev != NULL &&
884 n->parent->prev->tok == MDOC_An ? "<" : "\\(la");
885 outflags &= ~MMAN_spc;
886 return(1);
887 }
888
889 static void
890 post_aq(DECL_ARGS)
891 {
892
893 outflags &= ~(MMAN_spc | MMAN_nl);
894 print_word(n->parent->prev != NULL &&
895 n->parent->prev->tok == MDOC_An ? ">" : "\\(ra");
896 }
897
898 static int
899 pre_bd(DECL_ARGS)
900 {
901
902 outflags &= ~(MMAN_PP | MMAN_sp | MMAN_br);
903
904 if (DISP_unfilled == n->norm->Bd.type ||
905 DISP_literal == n->norm->Bd.type)
906 print_line(".nf", 0);
907 if (0 == n->norm->Bd.comp && NULL != n->parent->prev)
908 outflags |= MMAN_sp;
909 print_offs(n->norm->Bd.offs, 1);
910 return(1);
911 }
912
913 static void
914 post_bd(DECL_ARGS)
915 {
916
917 /* Close out this display. */
918 print_line(".RE", MMAN_nl);
919 if (DISP_unfilled == n->norm->Bd.type ||
920 DISP_literal == n->norm->Bd.type)
921 print_line(".fi", MMAN_nl);
922
923 /* Maybe we are inside an enclosing list? */
924 if (NULL != n->parent->next)
925 mid_it();
926 }
927
928 static int
929 pre_bf(DECL_ARGS)
930 {
931
932 switch (n->type) {
933 case MDOC_BLOCK:
934 return(1);
935 case MDOC_BODY:
936 break;
937 default:
938 return(0);
939 }
940 switch (n->norm->Bf.font) {
941 case FONT_Em:
942 font_push('I');
943 break;
944 case FONT_Sy:
945 font_push('B');
946 break;
947 default:
948 font_push('R');
949 break;
950 }
951 return(1);
952 }
953
954 static void
955 post_bf(DECL_ARGS)
956 {
957
958 if (MDOC_BODY == n->type)
959 font_pop();
960 }
961
962 static int
963 pre_bk(DECL_ARGS)
964 {
965
966 switch (n->type) {
967 case MDOC_BLOCK:
968 return(1);
969 case MDOC_BODY:
970 outflags |= MMAN_Bk;
971 return(1);
972 default:
973 return(0);
974 }
975 }
976
977 static void
978 post_bk(DECL_ARGS)
979 {
980
981 if (MDOC_BODY == n->type)
982 outflags &= ~MMAN_Bk;
983 }
984
985 static int
986 pre_bl(DECL_ARGS)
987 {
988 size_t icol;
989
990 /*
991 * print_offs() will increase the -offset to account for
992 * a possible enclosing .It, but any enclosed .It blocks
993 * just nest and do not add up their indentation.
994 */
995 if (n->norm->Bl.offs) {
996 print_offs(n->norm->Bl.offs, 0);
997 Bl_stack[Bl_stack_len++] = 0;
998 }
999
1000 switch (n->norm->Bl.type) {
1001 case LIST_enum:
1002 n->norm->Bl.count = 0;
1003 return(1);
1004 case LIST_column:
1005 break;
1006 default:
1007 return(1);
1008 }
1009
1010 print_line(".TS", MMAN_nl);
1011 for (icol = 0; icol < n->norm->Bl.ncols; icol++)
1012 print_word("l");
1013 print_word(".");
1014 outflags |= MMAN_nl;
1015 return(1);
1016 }
1017
1018 static void
1019 post_bl(DECL_ARGS)
1020 {
1021
1022 switch (n->norm->Bl.type) {
1023 case LIST_column:
1024 print_line(".TE", 0);
1025 break;
1026 case LIST_enum:
1027 n->norm->Bl.count = 0;
1028 break;
1029 default:
1030 break;
1031 }
1032
1033 if (n->norm->Bl.offs) {
1034 print_line(".RE", MMAN_nl);
1035 assert(Bl_stack_len);
1036 Bl_stack_len--;
1037 assert(0 == Bl_stack[Bl_stack_len]);
1038 } else {
1039 outflags |= MMAN_PP | MMAN_nl;
1040 outflags &= ~(MMAN_sp | MMAN_br);
1041 }
1042
1043 /* Maybe we are inside an enclosing list? */
1044 if (NULL != n->parent->next)
1045 mid_it();
1046
1047 }
1048
1049 static int
1050 pre_br(DECL_ARGS)
1051 {
1052
1053 outflags |= MMAN_br;
1054 return(0);
1055 }
1056
1057 static int
1058 pre_bx(DECL_ARGS)
1059 {
1060
1061 n = n->child;
1062 if (n) {
1063 print_word(n->string);
1064 outflags &= ~MMAN_spc;
1065 n = n->next;
1066 }
1067 print_word("BSD");
1068 if (NULL == n)
1069 return(0);
1070 outflags &= ~MMAN_spc;
1071 print_word("-");
1072 outflags &= ~MMAN_spc;
1073 print_word(n->string);
1074 return(0);
1075 }
1076
1077 static int
1078 pre_dl(DECL_ARGS)
1079 {
1080
1081 print_offs("6n", 0);
1082 return(1);
1083 }
1084
1085 static void
1086 post_dl(DECL_ARGS)
1087 {
1088
1089 print_line(".RE", MMAN_nl);
1090
1091 /* Maybe we are inside an enclosing list? */
1092 if (NULL != n->parent->next)
1093 mid_it();
1094 }
1095
1096 static int
1097 pre_em(DECL_ARGS)
1098 {
1099
1100 font_push('I');
1101 return(1);
1102 }
1103
1104 static int
1105 pre_en(DECL_ARGS)
1106 {
1107
1108 if (NULL == n->norm->Es ||
1109 NULL == n->norm->Es->child)
1110 return(1);
1111
1112 print_word(n->norm->Es->child->string);
1113 outflags &= ~MMAN_spc;
1114 return(1);
1115 }
1116
1117 static void
1118 post_en(DECL_ARGS)
1119 {
1120
1121 if (NULL == n->norm->Es ||
1122 NULL == n->norm->Es->child ||
1123 NULL == n->norm->Es->child->next)
1124 return;
1125
1126 outflags &= ~MMAN_spc;
1127 print_word(n->norm->Es->child->next->string);
1128 return;
1129 }
1130
1131 static int
1132 pre_eo(DECL_ARGS)
1133 {
1134
1135 outflags &= ~(MMAN_spc | MMAN_nl);
1136 return(1);
1137 }
1138
1139 static void
1140 post_eo(DECL_ARGS)
1141 {
1142
1143 if (n->end != ENDBODY_SPACE)
1144 outflags &= ~MMAN_spc;
1145 }
1146
1147 static int
1148 pre_fa(DECL_ARGS)
1149 {
1150 int am_Fa;
1151
1152 am_Fa = MDOC_Fa == n->tok;
1153
1154 if (am_Fa)
1155 n = n->child;
1156
1157 while (NULL != n) {
1158 font_push('I');
1159 if (am_Fa || MDOC_SYNPRETTY & n->flags)
1160 outflags |= MMAN_nbrword;
1161 print_node(meta, n);
1162 font_pop();
1163 if (NULL != (n = n->next))
1164 print_word(",");
1165 }
1166 return(0);
1167 }
1168
1169 static void
1170 post_fa(DECL_ARGS)
1171 {
1172
1173 if (NULL != n->next && MDOC_Fa == n->next->tok)
1174 print_word(",");
1175 }
1176
1177 static int
1178 pre_fd(DECL_ARGS)
1179 {
1180
1181 pre_syn(n);
1182 font_push('B');
1183 return(1);
1184 }
1185
1186 static void
1187 post_fd(DECL_ARGS)
1188 {
1189
1190 font_pop();
1191 outflags |= MMAN_br;
1192 }
1193
1194 static int
1195 pre_fl(DECL_ARGS)
1196 {
1197
1198 font_push('B');
1199 print_word("\\-");
1200 if (n->nchild)
1201 outflags &= ~MMAN_spc;
1202 return(1);
1203 }
1204
1205 static void
1206 post_fl(DECL_ARGS)
1207 {
1208
1209 font_pop();
1210 if ( ! (n->nchild ||
1211 n->next == NULL ||
1212 n->next->type == MDOC_TEXT ||
1213 n->next->flags & MDOC_LINE))
1214 outflags &= ~MMAN_spc;
1215 }
1216
1217 static int
1218 pre_fn(DECL_ARGS)
1219 {
1220
1221 pre_syn(n);
1222
1223 n = n->child;
1224 if (NULL == n)
1225 return(0);
1226
1227 if (MDOC_SYNPRETTY & n->flags)
1228 print_block(".HP 4n", MMAN_nl);
1229
1230 font_push('B');
1231 print_node(meta, n);
1232 font_pop();
1233 outflags &= ~MMAN_spc;
1234 print_word("(");
1235 outflags &= ~MMAN_spc;
1236
1237 n = n->next;
1238 if (NULL != n)
1239 pre_fa(meta, n);
1240 return(0);
1241 }
1242
1243 static void
1244 post_fn(DECL_ARGS)
1245 {
1246
1247 print_word(")");
1248 if (MDOC_SYNPRETTY & n->flags) {
1249 print_word(";");
1250 outflags |= MMAN_PP;
1251 }
1252 }
1253
1254 static int
1255 pre_fo(DECL_ARGS)
1256 {
1257
1258 switch (n->type) {
1259 case MDOC_BLOCK:
1260 pre_syn(n);
1261 break;
1262 case MDOC_HEAD:
1263 if (MDOC_SYNPRETTY & n->flags)
1264 print_block(".HP 4n", MMAN_nl);
1265 font_push('B');
1266 break;
1267 case MDOC_BODY:
1268 outflags &= ~MMAN_spc;
1269 print_word("(");
1270 outflags &= ~MMAN_spc;
1271 break;
1272 default:
1273 break;
1274 }
1275 return(1);
1276 }
1277
1278 static void
1279 post_fo(DECL_ARGS)
1280 {
1281
1282 switch (n->type) {
1283 case MDOC_HEAD:
1284 font_pop();
1285 break;
1286 case MDOC_BODY:
1287 post_fn(meta, n);
1288 break;
1289 default:
1290 break;
1291 }
1292 }
1293
1294 static int
1295 pre_ft(DECL_ARGS)
1296 {
1297
1298 pre_syn(n);
1299 font_push('I');
1300 return(1);
1301 }
1302
1303 static int
1304 pre_in(DECL_ARGS)
1305 {
1306
1307 if (MDOC_SYNPRETTY & n->flags) {
1308 pre_syn(n);
1309 font_push('B');
1310 print_word("#include <");
1311 outflags &= ~MMAN_spc;
1312 } else {
1313 print_word("<");
1314 outflags &= ~MMAN_spc;
1315 font_push('I');
1316 }
1317 return(1);
1318 }
1319
1320 static void
1321 post_in(DECL_ARGS)
1322 {
1323
1324 if (MDOC_SYNPRETTY & n->flags) {
1325 outflags &= ~MMAN_spc;
1326 print_word(">");
1327 font_pop();
1328 outflags |= MMAN_br;
1329 } else {
1330 font_pop();
1331 outflags &= ~MMAN_spc;
1332 print_word(">");
1333 }
1334 }
1335
1336 static int
1337 pre_it(DECL_ARGS)
1338 {
1339 const struct mdoc_node *bln;
1340
1341 switch (n->type) {
1342 case MDOC_HEAD:
1343 outflags |= MMAN_PP | MMAN_nl;
1344 bln = n->parent->parent;
1345 if (0 == bln->norm->Bl.comp ||
1346 (NULL == n->parent->prev &&
1347 NULL == bln->parent->prev))
1348 outflags |= MMAN_sp;
1349 outflags &= ~MMAN_br;
1350 switch (bln->norm->Bl.type) {
1351 case LIST_item:
1352 return(0);
1353 case LIST_inset:
1354 /* FALLTHROUGH */
1355 case LIST_diag:
1356 /* FALLTHROUGH */
1357 case LIST_ohang:
1358 if (bln->norm->Bl.type == LIST_diag)
1359 print_line(".B \"", 0);
1360 else
1361 print_line(".R \"", 0);
1362 outflags &= ~MMAN_spc;
1363 return(1);
1364 case LIST_bullet:
1365 /* FALLTHROUGH */
1366 case LIST_dash:
1367 /* FALLTHROUGH */
1368 case LIST_hyphen:
1369 print_width(bln->norm->Bl.width, NULL, 0);
1370 TPremain = 0;
1371 outflags |= MMAN_nl;
1372 font_push('B');
1373 if (LIST_bullet == bln->norm->Bl.type)
1374 print_word("\\(bu");
1375 else
1376 print_word("-");
1377 font_pop();
1378 outflags |= MMAN_nl;
1379 return(0);
1380 case LIST_enum:
1381 print_width(bln->norm->Bl.width, NULL, 0);
1382 TPremain = 0;
1383 outflags |= MMAN_nl;
1384 print_count(&bln->norm->Bl.count);
1385 outflags |= MMAN_nl;
1386 return(0);
1387 case LIST_hang:
1388 print_width(bln->norm->Bl.width, n->child, 6);
1389 TPremain = 0;
1390 outflags |= MMAN_nl;
1391 return(1);
1392 case LIST_tag:
1393 print_width(bln->norm->Bl.width, n->child, 0);
1394 putchar('\n');
1395 outflags &= ~MMAN_spc;
1396 return(1);
1397 default:
1398 return(1);
1399 }
1400 default:
1401 break;
1402 }
1403 return(1);
1404 }
1405
1406 /*
1407 * This function is called after closing out an indented block.
1408 * If we are inside an enclosing list, restore its indentation.
1409 */
1410 static void
1411 mid_it(void)
1412 {
1413 char buf[24];
1414
1415 /* Nothing to do outside a list. */
1416 if (0 == Bl_stack_len || 0 == Bl_stack[Bl_stack_len - 1])
1417 return;
1418
1419 /* The indentation has already been set up. */
1420 if (Bl_stack_post[Bl_stack_len - 1])
1421 return;
1422
1423 /* Restore the indentation of the enclosing list. */
1424 print_line(".RS", MMAN_Bk_susp);
1425 (void)snprintf(buf, sizeof(buf), "%zun",
1426 Bl_stack[Bl_stack_len - 1]);
1427 print_word(buf);
1428
1429 /* Remeber to close out this .RS block later. */
1430 Bl_stack_post[Bl_stack_len - 1] = 1;
1431 }
1432
1433 static void
1434 post_it(DECL_ARGS)
1435 {
1436 const struct mdoc_node *bln;
1437
1438 bln = n->parent->parent;
1439
1440 switch (n->type) {
1441 case MDOC_HEAD:
1442 switch (bln->norm->Bl.type) {
1443 case LIST_diag:
1444 outflags &= ~MMAN_spc;
1445 print_word("\\ ");
1446 break;
1447 case LIST_ohang:
1448 outflags |= MMAN_br;
1449 break;
1450 default:
1451 break;
1452 }
1453 break;
1454 case MDOC_BODY:
1455 switch (bln->norm->Bl.type) {
1456 case LIST_bullet:
1457 /* FALLTHROUGH */
1458 case LIST_dash:
1459 /* FALLTHROUGH */
1460 case LIST_hyphen:
1461 /* FALLTHROUGH */
1462 case LIST_enum:
1463 /* FALLTHROUGH */
1464 case LIST_hang:
1465 /* FALLTHROUGH */
1466 case LIST_tag:
1467 assert(Bl_stack_len);
1468 Bl_stack[--Bl_stack_len] = 0;
1469
1470 /*
1471 * Our indentation had to be restored
1472 * after a child display or child list.
1473 * Close out that indentation block now.
1474 */
1475 if (Bl_stack_post[Bl_stack_len]) {
1476 print_line(".RE", MMAN_nl);
1477 Bl_stack_post[Bl_stack_len] = 0;
1478 }
1479 break;
1480 case LIST_column:
1481 if (NULL != n->next) {
1482 putchar('\t');
1483 outflags &= ~MMAN_spc;
1484 }
1485 break;
1486 default:
1487 break;
1488 }
1489 break;
1490 default:
1491 break;
1492 }
1493 }
1494
1495 static void
1496 post_lb(DECL_ARGS)
1497 {
1498
1499 if (SEC_LIBRARY == n->sec)
1500 outflags |= MMAN_br;
1501 }
1502
1503 static int
1504 pre_lk(DECL_ARGS)
1505 {
1506 const struct mdoc_node *link, *descr;
1507
1508 if (NULL == (link = n->child))
1509 return(0);
1510
1511 if (NULL != (descr = link->next)) {
1512 font_push('I');
1513 while (NULL != descr) {
1514 print_word(descr->string);
1515 descr = descr->next;
1516 }
1517 print_word(":");
1518 font_pop();
1519 }
1520
1521 font_push('B');
1522 print_word(link->string);
1523 font_pop();
1524 return(0);
1525 }
1526
1527 static int
1528 pre_ll(DECL_ARGS)
1529 {
1530
1531 print_line(".ll", 0);
1532 return(1);
1533 }
1534
1535 static int
1536 pre_li(DECL_ARGS)
1537 {
1538
1539 font_push('R');
1540 return(1);
1541 }
1542
1543 static int
1544 pre_nm(DECL_ARGS)
1545 {
1546 char *name;
1547
1548 if (MDOC_BLOCK == n->type) {
1549 outflags |= MMAN_Bk;
1550 pre_syn(n);
1551 }
1552 if (MDOC_ELEM != n->type && MDOC_HEAD != n->type)
1553 return(1);
1554 name = n->child ? n->child->string : meta->name;
1555 if (NULL == name)
1556 return(0);
1557 if (MDOC_HEAD == n->type) {
1558 if (NULL == n->parent->prev)
1559 outflags |= MMAN_sp;
1560 print_block(".HP", 0);
1561 printf(" %zun", strlen(name) + 1);
1562 outflags |= MMAN_nl;
1563 }
1564 font_push('B');
1565 if (NULL == n->child)
1566 print_word(meta->name);
1567 return(1);
1568 }
1569
1570 static void
1571 post_nm(DECL_ARGS)
1572 {
1573
1574 switch (n->type) {
1575 case MDOC_BLOCK:
1576 outflags &= ~MMAN_Bk;
1577 break;
1578 case MDOC_HEAD:
1579 /* FALLTHROUGH */
1580 case MDOC_ELEM:
1581 if (n->child != NULL || meta->name != NULL)
1582 font_pop();
1583 break;
1584 default:
1585 break;
1586 }
1587 }
1588
1589 static int
1590 pre_no(DECL_ARGS)
1591 {
1592
1593 outflags |= MMAN_spc_force;
1594 return(1);
1595 }
1596
1597 static int
1598 pre_ns(DECL_ARGS)
1599 {
1600
1601 outflags &= ~MMAN_spc;
1602 return(0);
1603 }
1604
1605 static void
1606 post_pf(DECL_ARGS)
1607 {
1608
1609 if ( ! (n->next == NULL || n->next->flags & MDOC_LINE))
1610 outflags &= ~MMAN_spc;
1611 }
1612
1613 static int
1614 pre_pp(DECL_ARGS)
1615 {
1616
1617 if (MDOC_It != n->parent->tok)
1618 outflags |= MMAN_PP;
1619 outflags |= MMAN_sp | MMAN_nl;
1620 outflags &= ~MMAN_br;
1621 return(0);
1622 }
1623
1624 static int
1625 pre_rs(DECL_ARGS)
1626 {
1627
1628 if (SEC_SEE_ALSO == n->sec) {
1629 outflags |= MMAN_PP | MMAN_sp | MMAN_nl;
1630 outflags &= ~MMAN_br;
1631 }
1632 return(1);
1633 }
1634
1635 static int
1636 pre_rv(DECL_ARGS)
1637 {
1638 int nchild;
1639
1640 outflags |= MMAN_br | MMAN_nl;
1641
1642 nchild = n->nchild;
1643 if (nchild > 0) {
1644 print_word("The");
1645
1646 for (n = n->child; n; n = n->next) {
1647 font_push('B');
1648 print_word(n->string);
1649 font_pop();
1650
1651 outflags &= ~MMAN_spc;
1652 print_word("()");
1653
1654 if (n->next == NULL)
1655 continue;
1656
1657 if (nchild > 2) {
1658 outflags &= ~MMAN_spc;
1659 print_word(",");
1660 }
1661 if (n->next->next == NULL)
1662 print_word("and");
1663 }
1664
1665 if (nchild > 1)
1666 print_word("functions return");
1667 else
1668 print_word("function returns");
1669
1670 print_word("the value\\~0 if successful;");
1671 } else
1672 print_word("Upon successful completion, "
1673 "the value\\~0 is returned;");
1674
1675 print_word("otherwise the value\\~\\-1 is returned"
1676 " and the global variable");
1677
1678 font_push('I');
1679 print_word("errno");
1680 font_pop();
1681
1682 print_word("is set to indicate the error.");
1683 outflags |= MMAN_nl;
1684 return(0);
1685 }
1686
1687 static int
1688 pre_skip(DECL_ARGS)
1689 {
1690
1691 return(0);
1692 }
1693
1694 static int
1695 pre_sm(DECL_ARGS)
1696 {
1697
1698 if (NULL == n->child)
1699 outflags ^= MMAN_Sm;
1700 else if (0 == strcmp("on", n->child->string))
1701 outflags |= MMAN_Sm;
1702 else
1703 outflags &= ~MMAN_Sm;
1704
1705 if (MMAN_Sm & outflags)
1706 outflags |= MMAN_spc;
1707
1708 return(0);
1709 }
1710
1711 static int
1712 pre_sp(DECL_ARGS)
1713 {
1714
1715 if (MMAN_PP & outflags) {
1716 outflags &= ~MMAN_PP;
1717 print_line(".PP", 0);
1718 } else
1719 print_line(".sp", 0);
1720 return(1);
1721 }
1722
1723 static void
1724 post_sp(DECL_ARGS)
1725 {
1726
1727 outflags |= MMAN_nl;
1728 }
1729
1730 static int
1731 pre_sy(DECL_ARGS)
1732 {
1733
1734 font_push('B');
1735 return(1);
1736 }
1737
1738 static int
1739 pre_vt(DECL_ARGS)
1740 {
1741
1742 if (MDOC_SYNPRETTY & n->flags) {
1743 switch (n->type) {
1744 case MDOC_BLOCK:
1745 pre_syn(n);
1746 return(1);
1747 case MDOC_BODY:
1748 break;
1749 default:
1750 return(0);
1751 }
1752 }
1753 font_push('I');
1754 return(1);
1755 }
1756
1757 static void
1758 post_vt(DECL_ARGS)
1759 {
1760
1761 if (MDOC_SYNPRETTY & n->flags && MDOC_BODY != n->type)
1762 return;
1763 font_pop();
1764 }
1765
1766 static int
1767 pre_xr(DECL_ARGS)
1768 {
1769
1770 n = n->child;
1771 if (NULL == n)
1772 return(0);
1773 print_node(meta, n);
1774 n = n->next;
1775 if (NULL == n)
1776 return(0);
1777 outflags &= ~MMAN_spc;
1778 print_word("(");
1779 print_node(meta, n);
1780 print_word(")");
1781 return(0);
1782 }
1783
1784 static int
1785 pre_ux(DECL_ARGS)
1786 {
1787
1788 print_word(manacts[n->tok].prefix);
1789 if (NULL == n->child)
1790 return(0);
1791 outflags &= ~MMAN_spc;
1792 print_word("\\ ");
1793 outflags &= ~MMAN_spc;
1794 return(1);
1795 }