]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_man.c
ffb141b160543b473eb90e205e5174868e91b7f4
[mandoc.git] / mdoc_man.c
1 /* $Id: mdoc_man.c,v 1.81 2014/12/24 23:32:42 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 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->parent->prev != NULL &&
883 n->parent->prev->tok == MDOC_An ? "<" : "\\(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->parent->prev != NULL &&
894 n->parent->prev->tok == MDOC_An ? ">" : "\\(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 print_line(".TS", MMAN_nl);
1010 for (icol = 0; icol < n->norm->Bl.ncols; icol++)
1011 print_word("l");
1012 print_word(".");
1013 outflags |= MMAN_nl;
1014 return(1);
1015 }
1016
1017 static void
1018 post_bl(DECL_ARGS)
1019 {
1020
1021 switch (n->norm->Bl.type) {
1022 case LIST_column:
1023 print_line(".TE", 0);
1024 break;
1025 case LIST_enum:
1026 n->norm->Bl.count = 0;
1027 break;
1028 default:
1029 break;
1030 }
1031
1032 if (n->norm->Bl.offs) {
1033 print_line(".RE", MMAN_nl);
1034 assert(Bl_stack_len);
1035 Bl_stack_len--;
1036 assert(0 == Bl_stack[Bl_stack_len]);
1037 } else {
1038 outflags |= MMAN_PP | MMAN_nl;
1039 outflags &= ~(MMAN_sp | MMAN_br);
1040 }
1041
1042 /* Maybe we are inside an enclosing list? */
1043 if (NULL != n->parent->next)
1044 mid_it();
1045
1046 }
1047
1048 static int
1049 pre_br(DECL_ARGS)
1050 {
1051
1052 outflags |= MMAN_br;
1053 return(0);
1054 }
1055
1056 static int
1057 pre_bx(DECL_ARGS)
1058 {
1059
1060 n = n->child;
1061 if (n) {
1062 print_word(n->string);
1063 outflags &= ~MMAN_spc;
1064 n = n->next;
1065 }
1066 print_word("BSD");
1067 if (NULL == n)
1068 return(0);
1069 outflags &= ~MMAN_spc;
1070 print_word("-");
1071 outflags &= ~MMAN_spc;
1072 print_word(n->string);
1073 return(0);
1074 }
1075
1076 static int
1077 pre_dl(DECL_ARGS)
1078 {
1079
1080 print_offs("6n", 0);
1081 return(1);
1082 }
1083
1084 static void
1085 post_dl(DECL_ARGS)
1086 {
1087
1088 print_line(".RE", MMAN_nl);
1089
1090 /* Maybe we are inside an enclosing list? */
1091 if (NULL != n->parent->next)
1092 mid_it();
1093 }
1094
1095 static int
1096 pre_em(DECL_ARGS)
1097 {
1098
1099 font_push('I');
1100 return(1);
1101 }
1102
1103 static int
1104 pre_en(DECL_ARGS)
1105 {
1106
1107 if (NULL == n->norm->Es ||
1108 NULL == n->norm->Es->child)
1109 return(1);
1110
1111 print_word(n->norm->Es->child->string);
1112 outflags &= ~MMAN_spc;
1113 return(1);
1114 }
1115
1116 static void
1117 post_en(DECL_ARGS)
1118 {
1119
1120 if (NULL == n->norm->Es ||
1121 NULL == n->norm->Es->child ||
1122 NULL == n->norm->Es->child->next)
1123 return;
1124
1125 outflags &= ~MMAN_spc;
1126 print_word(n->norm->Es->child->next->string);
1127 return;
1128 }
1129
1130 static int
1131 pre_eo(DECL_ARGS)
1132 {
1133
1134 outflags &= ~(MMAN_spc | MMAN_nl);
1135 return(1);
1136 }
1137
1138 static void
1139 post_eo(DECL_ARGS)
1140 {
1141
1142 if (n->end != ENDBODY_SPACE)
1143 outflags &= ~MMAN_spc;
1144 }
1145
1146 static int
1147 pre_fa(DECL_ARGS)
1148 {
1149 int am_Fa;
1150
1151 am_Fa = MDOC_Fa == n->tok;
1152
1153 if (am_Fa)
1154 n = n->child;
1155
1156 while (NULL != n) {
1157 font_push('I');
1158 if (am_Fa || MDOC_SYNPRETTY & n->flags)
1159 outflags |= MMAN_nbrword;
1160 print_node(meta, n);
1161 font_pop();
1162 if (NULL != (n = n->next))
1163 print_word(",");
1164 }
1165 return(0);
1166 }
1167
1168 static void
1169 post_fa(DECL_ARGS)
1170 {
1171
1172 if (NULL != n->next && MDOC_Fa == n->next->tok)
1173 print_word(",");
1174 }
1175
1176 static int
1177 pre_fd(DECL_ARGS)
1178 {
1179
1180 pre_syn(n);
1181 font_push('B');
1182 return(1);
1183 }
1184
1185 static void
1186 post_fd(DECL_ARGS)
1187 {
1188
1189 font_pop();
1190 outflags |= MMAN_br;
1191 }
1192
1193 static int
1194 pre_fl(DECL_ARGS)
1195 {
1196
1197 font_push('B');
1198 print_word("\\-");
1199 if (n->nchild)
1200 outflags &= ~MMAN_spc;
1201 return(1);
1202 }
1203
1204 static void
1205 post_fl(DECL_ARGS)
1206 {
1207
1208 font_pop();
1209 if ( ! (n->nchild ||
1210 n->next == NULL ||
1211 n->next->type == MDOC_TEXT ||
1212 n->next->flags & MDOC_LINE))
1213 outflags &= ~MMAN_spc;
1214 }
1215
1216 static int
1217 pre_fn(DECL_ARGS)
1218 {
1219
1220 pre_syn(n);
1221
1222 n = n->child;
1223 if (NULL == n)
1224 return(0);
1225
1226 if (MDOC_SYNPRETTY & n->flags)
1227 print_block(".HP 4n", MMAN_nl);
1228
1229 font_push('B');
1230 print_node(meta, n);
1231 font_pop();
1232 outflags &= ~MMAN_spc;
1233 print_word("(");
1234 outflags &= ~MMAN_spc;
1235
1236 n = n->next;
1237 if (NULL != n)
1238 pre_fa(meta, n);
1239 return(0);
1240 }
1241
1242 static void
1243 post_fn(DECL_ARGS)
1244 {
1245
1246 print_word(")");
1247 if (MDOC_SYNPRETTY & n->flags) {
1248 print_word(";");
1249 outflags |= MMAN_PP;
1250 }
1251 }
1252
1253 static int
1254 pre_fo(DECL_ARGS)
1255 {
1256
1257 switch (n->type) {
1258 case MDOC_BLOCK:
1259 pre_syn(n);
1260 break;
1261 case MDOC_HEAD:
1262 if (MDOC_SYNPRETTY & n->flags)
1263 print_block(".HP 4n", MMAN_nl);
1264 font_push('B');
1265 break;
1266 case MDOC_BODY:
1267 outflags &= ~MMAN_spc;
1268 print_word("(");
1269 outflags &= ~MMAN_spc;
1270 break;
1271 default:
1272 break;
1273 }
1274 return(1);
1275 }
1276
1277 static void
1278 post_fo(DECL_ARGS)
1279 {
1280
1281 switch (n->type) {
1282 case MDOC_HEAD:
1283 font_pop();
1284 break;
1285 case MDOC_BODY:
1286 post_fn(meta, n);
1287 break;
1288 default:
1289 break;
1290 }
1291 }
1292
1293 static int
1294 pre_ft(DECL_ARGS)
1295 {
1296
1297 pre_syn(n);
1298 font_push('I');
1299 return(1);
1300 }
1301
1302 static int
1303 pre_in(DECL_ARGS)
1304 {
1305
1306 if (MDOC_SYNPRETTY & n->flags) {
1307 pre_syn(n);
1308 font_push('B');
1309 print_word("#include <");
1310 outflags &= ~MMAN_spc;
1311 } else {
1312 print_word("<");
1313 outflags &= ~MMAN_spc;
1314 font_push('I');
1315 }
1316 return(1);
1317 }
1318
1319 static void
1320 post_in(DECL_ARGS)
1321 {
1322
1323 if (MDOC_SYNPRETTY & n->flags) {
1324 outflags &= ~MMAN_spc;
1325 print_word(">");
1326 font_pop();
1327 outflags |= MMAN_br;
1328 } else {
1329 font_pop();
1330 outflags &= ~MMAN_spc;
1331 print_word(">");
1332 }
1333 }
1334
1335 static int
1336 pre_it(DECL_ARGS)
1337 {
1338 const struct mdoc_node *bln;
1339
1340 switch (n->type) {
1341 case MDOC_HEAD:
1342 outflags |= MMAN_PP | MMAN_nl;
1343 bln = n->parent->parent;
1344 if (0 == bln->norm->Bl.comp ||
1345 (NULL == n->parent->prev &&
1346 NULL == bln->parent->prev))
1347 outflags |= MMAN_sp;
1348 outflags &= ~MMAN_br;
1349 switch (bln->norm->Bl.type) {
1350 case LIST_item:
1351 return(0);
1352 case LIST_inset:
1353 /* FALLTHROUGH */
1354 case LIST_diag:
1355 /* FALLTHROUGH */
1356 case LIST_ohang:
1357 if (bln->norm->Bl.type == LIST_diag)
1358 print_line(".B \"", 0);
1359 else
1360 print_line(".R \"", 0);
1361 outflags &= ~MMAN_spc;
1362 return(1);
1363 case LIST_bullet:
1364 /* FALLTHROUGH */
1365 case LIST_dash:
1366 /* FALLTHROUGH */
1367 case LIST_hyphen:
1368 print_width(&bln->norm->Bl, NULL);
1369 TPremain = 0;
1370 outflags |= MMAN_nl;
1371 font_push('B');
1372 if (LIST_bullet == bln->norm->Bl.type)
1373 print_word("\\(bu");
1374 else
1375 print_word("-");
1376 font_pop();
1377 outflags |= MMAN_nl;
1378 return(0);
1379 case LIST_enum:
1380 print_width(&bln->norm->Bl, NULL);
1381 TPremain = 0;
1382 outflags |= MMAN_nl;
1383 print_count(&bln->norm->Bl.count);
1384 outflags |= MMAN_nl;
1385 return(0);
1386 case LIST_hang:
1387 print_width(&bln->norm->Bl, n->child);
1388 TPremain = 0;
1389 outflags |= MMAN_nl;
1390 return(1);
1391 case LIST_tag:
1392 print_width(&bln->norm->Bl, n->child);
1393 putchar('\n');
1394 outflags &= ~MMAN_spc;
1395 return(1);
1396 default:
1397 return(1);
1398 }
1399 default:
1400 break;
1401 }
1402 return(1);
1403 }
1404
1405 /*
1406 * This function is called after closing out an indented block.
1407 * If we are inside an enclosing list, restore its indentation.
1408 */
1409 static void
1410 mid_it(void)
1411 {
1412 char buf[24];
1413
1414 /* Nothing to do outside a list. */
1415 if (0 == Bl_stack_len || 0 == Bl_stack[Bl_stack_len - 1])
1416 return;
1417
1418 /* The indentation has already been set up. */
1419 if (Bl_stack_post[Bl_stack_len - 1])
1420 return;
1421
1422 /* Restore the indentation of the enclosing list. */
1423 print_line(".RS", MMAN_Bk_susp);
1424 (void)snprintf(buf, sizeof(buf), "%dn",
1425 Bl_stack[Bl_stack_len - 1]);
1426 print_word(buf);
1427
1428 /* Remeber to close out this .RS block later. */
1429 Bl_stack_post[Bl_stack_len - 1] = 1;
1430 }
1431
1432 static void
1433 post_it(DECL_ARGS)
1434 {
1435 const struct mdoc_node *bln;
1436
1437 bln = n->parent->parent;
1438
1439 switch (n->type) {
1440 case MDOC_HEAD:
1441 switch (bln->norm->Bl.type) {
1442 case LIST_diag:
1443 outflags &= ~MMAN_spc;
1444 print_word("\\ ");
1445 break;
1446 case LIST_ohang:
1447 outflags |= MMAN_br;
1448 break;
1449 default:
1450 break;
1451 }
1452 break;
1453 case MDOC_BODY:
1454 switch (bln->norm->Bl.type) {
1455 case LIST_bullet:
1456 /* FALLTHROUGH */
1457 case LIST_dash:
1458 /* FALLTHROUGH */
1459 case LIST_hyphen:
1460 /* FALLTHROUGH */
1461 case LIST_enum:
1462 /* FALLTHROUGH */
1463 case LIST_hang:
1464 /* FALLTHROUGH */
1465 case LIST_tag:
1466 assert(Bl_stack_len);
1467 Bl_stack[--Bl_stack_len] = 0;
1468
1469 /*
1470 * Our indentation had to be restored
1471 * after a child display or child list.
1472 * Close out that indentation block now.
1473 */
1474 if (Bl_stack_post[Bl_stack_len]) {
1475 print_line(".RE", MMAN_nl);
1476 Bl_stack_post[Bl_stack_len] = 0;
1477 }
1478 break;
1479 case LIST_column:
1480 if (NULL != n->next) {
1481 putchar('\t');
1482 outflags &= ~MMAN_spc;
1483 }
1484 break;
1485 default:
1486 break;
1487 }
1488 break;
1489 default:
1490 break;
1491 }
1492 }
1493
1494 static void
1495 post_lb(DECL_ARGS)
1496 {
1497
1498 if (SEC_LIBRARY == n->sec)
1499 outflags |= MMAN_br;
1500 }
1501
1502 static int
1503 pre_lk(DECL_ARGS)
1504 {
1505 const struct mdoc_node *link, *descr;
1506
1507 if (NULL == (link = n->child))
1508 return(0);
1509
1510 if (NULL != (descr = link->next)) {
1511 font_push('I');
1512 while (NULL != descr) {
1513 print_word(descr->string);
1514 descr = descr->next;
1515 }
1516 print_word(":");
1517 font_pop();
1518 }
1519
1520 font_push('B');
1521 print_word(link->string);
1522 font_pop();
1523 return(0);
1524 }
1525
1526 static int
1527 pre_ll(DECL_ARGS)
1528 {
1529
1530 print_line(".ll", 0);
1531 return(1);
1532 }
1533
1534 static int
1535 pre_li(DECL_ARGS)
1536 {
1537
1538 font_push('R');
1539 return(1);
1540 }
1541
1542 static int
1543 pre_nm(DECL_ARGS)
1544 {
1545 char *name;
1546
1547 if (MDOC_BLOCK == n->type) {
1548 outflags |= MMAN_Bk;
1549 pre_syn(n);
1550 }
1551 if (MDOC_ELEM != n->type && MDOC_HEAD != n->type)
1552 return(1);
1553 name = n->child ? n->child->string : meta->name;
1554 if (NULL == name)
1555 return(0);
1556 if (MDOC_HEAD == n->type) {
1557 if (NULL == n->parent->prev)
1558 outflags |= MMAN_sp;
1559 print_block(".HP", 0);
1560 printf(" %zun", strlen(name) + 1);
1561 outflags |= MMAN_nl;
1562 }
1563 font_push('B');
1564 if (NULL == n->child)
1565 print_word(meta->name);
1566 return(1);
1567 }
1568
1569 static void
1570 post_nm(DECL_ARGS)
1571 {
1572
1573 switch (n->type) {
1574 case MDOC_BLOCK:
1575 outflags &= ~MMAN_Bk;
1576 break;
1577 case MDOC_HEAD:
1578 /* FALLTHROUGH */
1579 case MDOC_ELEM:
1580 if (n->child != NULL || meta->name != NULL)
1581 font_pop();
1582 break;
1583 default:
1584 break;
1585 }
1586 }
1587
1588 static int
1589 pre_no(DECL_ARGS)
1590 {
1591
1592 outflags |= MMAN_spc_force;
1593 return(1);
1594 }
1595
1596 static int
1597 pre_ns(DECL_ARGS)
1598 {
1599
1600 outflags &= ~MMAN_spc;
1601 return(0);
1602 }
1603
1604 static void
1605 post_pf(DECL_ARGS)
1606 {
1607
1608 if ( ! (n->next == NULL || n->next->flags & MDOC_LINE))
1609 outflags &= ~MMAN_spc;
1610 }
1611
1612 static int
1613 pre_pp(DECL_ARGS)
1614 {
1615
1616 if (MDOC_It != n->parent->tok)
1617 outflags |= MMAN_PP;
1618 outflags |= MMAN_sp | MMAN_nl;
1619 outflags &= ~MMAN_br;
1620 return(0);
1621 }
1622
1623 static int
1624 pre_rs(DECL_ARGS)
1625 {
1626
1627 if (SEC_SEE_ALSO == n->sec) {
1628 outflags |= MMAN_PP | MMAN_sp | MMAN_nl;
1629 outflags &= ~MMAN_br;
1630 }
1631 return(1);
1632 }
1633
1634 static int
1635 pre_rv(DECL_ARGS)
1636 {
1637 int nchild;
1638
1639 outflags |= MMAN_br | MMAN_nl;
1640
1641 nchild = n->nchild;
1642 if (nchild > 0) {
1643 print_word("The");
1644
1645 for (n = n->child; n; n = n->next) {
1646 font_push('B');
1647 print_word(n->string);
1648 font_pop();
1649
1650 outflags &= ~MMAN_spc;
1651 print_word("()");
1652
1653 if (n->next == NULL)
1654 continue;
1655
1656 if (nchild > 2) {
1657 outflags &= ~MMAN_spc;
1658 print_word(",");
1659 }
1660 if (n->next->next == NULL)
1661 print_word("and");
1662 }
1663
1664 if (nchild > 1)
1665 print_word("functions return");
1666 else
1667 print_word("function returns");
1668
1669 print_word("the value\\~0 if successful;");
1670 } else
1671 print_word("Upon successful completion, "
1672 "the value\\~0 is returned;");
1673
1674 print_word("otherwise the value\\~\\-1 is returned"
1675 " and the global variable");
1676
1677 font_push('I');
1678 print_word("errno");
1679 font_pop();
1680
1681 print_word("is set to indicate the error.");
1682 outflags |= MMAN_nl;
1683 return(0);
1684 }
1685
1686 static int
1687 pre_skip(DECL_ARGS)
1688 {
1689
1690 return(0);
1691 }
1692
1693 static int
1694 pre_sm(DECL_ARGS)
1695 {
1696
1697 if (NULL == n->child)
1698 outflags ^= MMAN_Sm;
1699 else if (0 == strcmp("on", n->child->string))
1700 outflags |= MMAN_Sm;
1701 else
1702 outflags &= ~MMAN_Sm;
1703
1704 if (MMAN_Sm & outflags)
1705 outflags |= MMAN_spc;
1706
1707 return(0);
1708 }
1709
1710 static int
1711 pre_sp(DECL_ARGS)
1712 {
1713
1714 if (MMAN_PP & outflags) {
1715 outflags &= ~MMAN_PP;
1716 print_line(".PP", 0);
1717 } else
1718 print_line(".sp", 0);
1719 return(1);
1720 }
1721
1722 static void
1723 post_sp(DECL_ARGS)
1724 {
1725
1726 outflags |= MMAN_nl;
1727 }
1728
1729 static int
1730 pre_sy(DECL_ARGS)
1731 {
1732
1733 font_push('B');
1734 return(1);
1735 }
1736
1737 static int
1738 pre_vt(DECL_ARGS)
1739 {
1740
1741 if (MDOC_SYNPRETTY & n->flags) {
1742 switch (n->type) {
1743 case MDOC_BLOCK:
1744 pre_syn(n);
1745 return(1);
1746 case MDOC_BODY:
1747 break;
1748 default:
1749 return(0);
1750 }
1751 }
1752 font_push('I');
1753 return(1);
1754 }
1755
1756 static void
1757 post_vt(DECL_ARGS)
1758 {
1759
1760 if (MDOC_SYNPRETTY & n->flags && MDOC_BODY != n->type)
1761 return;
1762 font_pop();
1763 }
1764
1765 static int
1766 pre_xr(DECL_ARGS)
1767 {
1768
1769 n = n->child;
1770 if (NULL == n)
1771 return(0);
1772 print_node(meta, n);
1773 n = n->next;
1774 if (NULL == n)
1775 return(0);
1776 outflags &= ~MMAN_spc;
1777 print_word("(");
1778 print_node(meta, n);
1779 print_word(")");
1780 return(0);
1781 }
1782
1783 static int
1784 pre_ux(DECL_ARGS)
1785 {
1786
1787 print_word(manacts[n->tok].prefix);
1788 if (NULL == n->child)
1789 return(0);
1790 outflags &= ~MMAN_spc;
1791 print_word("\\ ");
1792 outflags &= ~MMAN_spc;
1793 return(1);
1794 }