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