]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_man.c
unify names of AST node flags; no change of cpp output
[mandoc.git] / mdoc_man.c
1 /* $Id: mdoc_man.c,v 1.98 2017/01/10 13:47:00 schwarze Exp $ */
2 /*
3 * Copyright (c) 2011-2017 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_aux.h"
26 #include "mandoc.h"
27 #include "roff.h"
28 #include "mdoc.h"
29 #include "man.h"
30 #include "out.h"
31 #include "main.h"
32
33 #define DECL_ARGS const struct roff_meta *meta, struct roff_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 roff_node *);
112 static int pre_vt(DECL_ARGS);
113 static int pre_ux(DECL_ARGS);
114 static int pre_xr(DECL_ARGS);
115 static void print_word(const char *);
116 static void print_line(const char *, int);
117 static void print_block(const char *, int);
118 static void print_offs(const char *, int);
119 static void print_width(const struct mdoc_bl *,
120 const struct roff_node *);
121 static void print_count(int *);
122 static void print_node(DECL_ARGS);
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 roff_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 == ROFFT_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 roff_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 roff_man *mdoc)
549 {
550 struct roff_node *n;
551
552 printf(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"\n",
553 mdoc->meta.title,
554 (mdoc->meta.msec == NULL ? "" : mdoc->meta.msec),
555 mdoc->meta.date, mdoc->meta.os, mdoc->meta.vol);
556
557 /* Disable hyphenation and if nroff, disable justification. */
558 printf(".nh\n.if n .ad l");
559
560 outflags = MMAN_nl | MMAN_Sm;
561 if (0 == fontqueue.size) {
562 fontqueue.size = 8;
563 fontqueue.head = fontqueue.tail = mandoc_malloc(8);
564 *fontqueue.tail = 'R';
565 }
566 for (n = mdoc->first->child; n != NULL; n = n->next)
567 print_node(&mdoc->meta, n);
568 putchar('\n');
569 }
570
571 static void
572 print_node(DECL_ARGS)
573 {
574 const struct manact *act;
575 struct roff_node *sub;
576 int cond, do_sub;
577
578 if (n->flags & NODE_NOPRT)
579 return;
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 && NODE_LINE & n->flags)
586 outflags |= MMAN_nl;
587
588 act = NULL;
589 cond = 0;
590 do_sub = 1;
591 n->flags &= ~NODE_ENDED;
592
593 if (n->type == ROFFT_TEXT) {
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 & NODE_DELIMC))
605 outflags |= MMAN_spc_force;
606 print_word(n->string);
607 if (outflags & MMAN_Sm && ! (n->flags & NODE_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 != NULL &&
617 (n->end == ENDBODY_NOT || n->child != NULL))
618 do_sub = (*act->pre)(meta, n);
619 }
620
621 /*
622 * Conditionally run all child nodes.
623 * Note that this iterates over children instead of using
624 * recursion. This prevents unnecessary depth in the stack.
625 */
626 if (do_sub)
627 for (sub = n->child; sub; sub = sub->next)
628 print_node(meta, sub);
629
630 /*
631 * Lastly, conditionally run the post-node handler.
632 */
633 if (NODE_ENDED & n->flags)
634 return;
635
636 if (cond && act->post)
637 (*act->post)(meta, n);
638
639 if (ENDBODY_NOT != n->end)
640 n->body->flags |= NODE_ENDED;
641
642 if (ENDBODY_NOSPACE == n->end)
643 outflags &= ~(MMAN_spc | MMAN_nl);
644 }
645
646 static int
647 cond_head(DECL_ARGS)
648 {
649
650 return n->type == ROFFT_HEAD;
651 }
652
653 static int
654 cond_body(DECL_ARGS)
655 {
656
657 return n->type == ROFFT_BODY;
658 }
659
660 static int
661 pre_enc(DECL_ARGS)
662 {
663 const char *prefix;
664
665 prefix = manacts[n->tok].prefix;
666 if (NULL == prefix)
667 return 1;
668 print_word(prefix);
669 outflags &= ~MMAN_spc;
670 return 1;
671 }
672
673 static void
674 post_enc(DECL_ARGS)
675 {
676 const char *suffix;
677
678 suffix = manacts[n->tok].suffix;
679 if (NULL == suffix)
680 return;
681 outflags &= ~(MMAN_spc | MMAN_nl);
682 print_word(suffix);
683 }
684
685 static int
686 pre_ex(DECL_ARGS)
687 {
688 struct roff_node *nch;
689
690 outflags |= MMAN_br | MMAN_nl;
691
692 print_word("The");
693
694 for (nch = n->child; nch != NULL; nch = nch->next) {
695 font_push('B');
696 print_word(nch->string);
697 font_pop();
698
699 if (nch->next == NULL)
700 continue;
701
702 if (nch->prev != NULL || nch->next->next != NULL) {
703 outflags &= ~MMAN_spc;
704 print_word(",");
705 }
706 if (nch->next->next == NULL)
707 print_word("and");
708 }
709
710 if (n->child != NULL && n->child->next != NULL)
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 (n->type == ROFFT_HEAD) {
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 (n->type != ROFFT_HEAD)
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 roff_node *n)
809 {
810
811 if (NULL == n->prev || ! (NODE_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 case MDOC_Fn:
825 case MDOC_Fo:
826 case MDOC_In:
827 case MDOC_Vt:
828 outflags |= MMAN_sp;
829 break;
830 case MDOC_Ft:
831 if (MDOC_Fn != n->tok && MDOC_Fo != n->tok) {
832 outflags |= MMAN_sp;
833 break;
834 }
835 /* FALLTHROUGH */
836 default:
837 outflags |= MMAN_br;
838 break;
839 }
840 }
841
842 static int
843 pre_an(DECL_ARGS)
844 {
845
846 switch (n->norm->An.auth) {
847 case AUTH_split:
848 outflags &= ~MMAN_An_nosplit;
849 outflags |= MMAN_An_split;
850 return 0;
851 case AUTH_nosplit:
852 outflags &= ~MMAN_An_split;
853 outflags |= MMAN_An_nosplit;
854 return 0;
855 default:
856 if (MMAN_An_split & outflags)
857 outflags |= MMAN_br;
858 else if (SEC_AUTHORS == n->sec &&
859 ! (MMAN_An_nosplit & outflags))
860 outflags |= MMAN_An_split;
861 return 1;
862 }
863 }
864
865 static int
866 pre_ap(DECL_ARGS)
867 {
868
869 outflags &= ~MMAN_spc;
870 print_word("'");
871 outflags &= ~MMAN_spc;
872 return 0;
873 }
874
875 static int
876 pre_aq(DECL_ARGS)
877 {
878
879 print_word(n->child != NULL && n->child->next == NULL &&
880 n->child->tok == MDOC_Mt ? "<" : "\\(la");
881 outflags &= ~MMAN_spc;
882 return 1;
883 }
884
885 static void
886 post_aq(DECL_ARGS)
887 {
888
889 outflags &= ~(MMAN_spc | MMAN_nl);
890 print_word(n->child != NULL && n->child->next == NULL &&
891 n->child->tok == MDOC_Mt ? ">" : "\\(ra");
892 }
893
894 static int
895 pre_bd(DECL_ARGS)
896 {
897
898 outflags &= ~(MMAN_PP | MMAN_sp | MMAN_br);
899
900 if (DISP_unfilled == n->norm->Bd.type ||
901 DISP_literal == n->norm->Bd.type)
902 print_line(".nf", 0);
903 if (0 == n->norm->Bd.comp && NULL != n->parent->prev)
904 outflags |= MMAN_sp;
905 print_offs(n->norm->Bd.offs, 1);
906 return 1;
907 }
908
909 static void
910 post_bd(DECL_ARGS)
911 {
912
913 /* Close out this display. */
914 print_line(".RE", MMAN_nl);
915 if (DISP_unfilled == n->norm->Bd.type ||
916 DISP_literal == n->norm->Bd.type)
917 print_line(".fi", MMAN_nl);
918
919 /* Maybe we are inside an enclosing list? */
920 if (NULL != n->parent->next)
921 mid_it();
922 }
923
924 static int
925 pre_bf(DECL_ARGS)
926 {
927
928 switch (n->type) {
929 case ROFFT_BLOCK:
930 return 1;
931 case ROFFT_BODY:
932 break;
933 default:
934 return 0;
935 }
936 switch (n->norm->Bf.font) {
937 case FONT_Em:
938 font_push('I');
939 break;
940 case FONT_Sy:
941 font_push('B');
942 break;
943 default:
944 font_push('R');
945 break;
946 }
947 return 1;
948 }
949
950 static void
951 post_bf(DECL_ARGS)
952 {
953
954 if (n->type == ROFFT_BODY)
955 font_pop();
956 }
957
958 static int
959 pre_bk(DECL_ARGS)
960 {
961
962 switch (n->type) {
963 case ROFFT_BLOCK:
964 return 1;
965 case ROFFT_BODY:
966 outflags |= MMAN_Bk;
967 return 1;
968 default:
969 return 0;
970 }
971 }
972
973 static void
974 post_bk(DECL_ARGS)
975 {
976
977 if (n->type == ROFFT_BODY)
978 outflags &= ~MMAN_Bk;
979 }
980
981 static int
982 pre_bl(DECL_ARGS)
983 {
984 size_t icol;
985
986 /*
987 * print_offs() will increase the -offset to account for
988 * a possible enclosing .It, but any enclosed .It blocks
989 * just nest and do not add up their indentation.
990 */
991 if (n->norm->Bl.offs) {
992 print_offs(n->norm->Bl.offs, 0);
993 Bl_stack[Bl_stack_len++] = 0;
994 }
995
996 switch (n->norm->Bl.type) {
997 case LIST_enum:
998 n->norm->Bl.count = 0;
999 return 1;
1000 case LIST_column:
1001 break;
1002 default:
1003 return 1;
1004 }
1005
1006 if (n->child != NULL) {
1007 print_line(".TS", MMAN_nl);
1008 for (icol = 0; icol < n->norm->Bl.ncols; icol++)
1009 print_word("l");
1010 print_word(".");
1011 }
1012 outflags |= MMAN_nl;
1013 return 1;
1014 }
1015
1016 static void
1017 post_bl(DECL_ARGS)
1018 {
1019
1020 switch (n->norm->Bl.type) {
1021 case LIST_column:
1022 if (n->child != NULL)
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 if (n->end == ENDBODY_NOT &&
1135 n->parent->head->child == NULL &&
1136 n->child != NULL &&
1137 n->child->end != ENDBODY_NOT)
1138 print_word("\\&");
1139 else if (n->end != ENDBODY_NOT ? n->child != NULL :
1140 n->parent->head->child != NULL && (n->child != NULL ||
1141 (n->parent->tail != NULL && n->parent->tail->child != NULL)))
1142 outflags &= ~(MMAN_spc | MMAN_nl);
1143 return 1;
1144 }
1145
1146 static void
1147 post_eo(DECL_ARGS)
1148 {
1149 int body, tail;
1150
1151 if (n->end != ENDBODY_NOT) {
1152 outflags |= MMAN_spc;
1153 return;
1154 }
1155
1156 body = n->child != NULL || n->parent->head->child != NULL;
1157 tail = n->parent->tail != NULL && n->parent->tail->child != NULL;
1158
1159 if (body && tail)
1160 outflags &= ~MMAN_spc;
1161 else if ( ! (body || tail))
1162 print_word("\\&");
1163 else if ( ! tail)
1164 outflags |= MMAN_spc;
1165 }
1166
1167 static int
1168 pre_fa(DECL_ARGS)
1169 {
1170 int am_Fa;
1171
1172 am_Fa = MDOC_Fa == n->tok;
1173
1174 if (am_Fa)
1175 n = n->child;
1176
1177 while (NULL != n) {
1178 font_push('I');
1179 if (am_Fa || NODE_SYNPRETTY & n->flags)
1180 outflags |= MMAN_nbrword;
1181 print_node(meta, n);
1182 font_pop();
1183 if (NULL != (n = n->next))
1184 print_word(",");
1185 }
1186 return 0;
1187 }
1188
1189 static void
1190 post_fa(DECL_ARGS)
1191 {
1192
1193 if (NULL != n->next && MDOC_Fa == n->next->tok)
1194 print_word(",");
1195 }
1196
1197 static int
1198 pre_fd(DECL_ARGS)
1199 {
1200
1201 pre_syn(n);
1202 font_push('B');
1203 return 1;
1204 }
1205
1206 static void
1207 post_fd(DECL_ARGS)
1208 {
1209
1210 font_pop();
1211 outflags |= MMAN_br;
1212 }
1213
1214 static int
1215 pre_fl(DECL_ARGS)
1216 {
1217
1218 font_push('B');
1219 print_word("\\-");
1220 if (n->child != NULL)
1221 outflags &= ~MMAN_spc;
1222 return 1;
1223 }
1224
1225 static void
1226 post_fl(DECL_ARGS)
1227 {
1228
1229 font_pop();
1230 if (!(n->child != NULL ||
1231 n->next == NULL ||
1232 n->next->type == ROFFT_TEXT ||
1233 n->next->flags & NODE_LINE))
1234 outflags &= ~MMAN_spc;
1235 }
1236
1237 static int
1238 pre_fn(DECL_ARGS)
1239 {
1240
1241 pre_syn(n);
1242
1243 n = n->child;
1244 if (NULL == n)
1245 return 0;
1246
1247 if (NODE_SYNPRETTY & n->flags)
1248 print_block(".HP 4n", MMAN_nl);
1249
1250 font_push('B');
1251 print_node(meta, n);
1252 font_pop();
1253 outflags &= ~MMAN_spc;
1254 print_word("(");
1255 outflags &= ~MMAN_spc;
1256
1257 n = n->next;
1258 if (NULL != n)
1259 pre_fa(meta, n);
1260 return 0;
1261 }
1262
1263 static void
1264 post_fn(DECL_ARGS)
1265 {
1266
1267 print_word(")");
1268 if (NODE_SYNPRETTY & n->flags) {
1269 print_word(";");
1270 outflags |= MMAN_PP;
1271 }
1272 }
1273
1274 static int
1275 pre_fo(DECL_ARGS)
1276 {
1277
1278 switch (n->type) {
1279 case ROFFT_BLOCK:
1280 pre_syn(n);
1281 break;
1282 case ROFFT_HEAD:
1283 if (n->child == NULL)
1284 return 0;
1285 if (NODE_SYNPRETTY & n->flags)
1286 print_block(".HP 4n", MMAN_nl);
1287 font_push('B');
1288 break;
1289 case ROFFT_BODY:
1290 outflags &= ~(MMAN_spc | MMAN_nl);
1291 print_word("(");
1292 outflags &= ~MMAN_spc;
1293 break;
1294 default:
1295 break;
1296 }
1297 return 1;
1298 }
1299
1300 static void
1301 post_fo(DECL_ARGS)
1302 {
1303
1304 switch (n->type) {
1305 case ROFFT_HEAD:
1306 if (n->child != NULL)
1307 font_pop();
1308 break;
1309 case ROFFT_BODY:
1310 post_fn(meta, n);
1311 break;
1312 default:
1313 break;
1314 }
1315 }
1316
1317 static int
1318 pre_ft(DECL_ARGS)
1319 {
1320
1321 pre_syn(n);
1322 font_push('I');
1323 return 1;
1324 }
1325
1326 static int
1327 pre_in(DECL_ARGS)
1328 {
1329
1330 if (NODE_SYNPRETTY & n->flags) {
1331 pre_syn(n);
1332 font_push('B');
1333 print_word("#include <");
1334 outflags &= ~MMAN_spc;
1335 } else {
1336 print_word("<");
1337 outflags &= ~MMAN_spc;
1338 font_push('I');
1339 }
1340 return 1;
1341 }
1342
1343 static void
1344 post_in(DECL_ARGS)
1345 {
1346
1347 if (NODE_SYNPRETTY & n->flags) {
1348 outflags &= ~MMAN_spc;
1349 print_word(">");
1350 font_pop();
1351 outflags |= MMAN_br;
1352 } else {
1353 font_pop();
1354 outflags &= ~MMAN_spc;
1355 print_word(">");
1356 }
1357 }
1358
1359 static int
1360 pre_it(DECL_ARGS)
1361 {
1362 const struct roff_node *bln;
1363
1364 switch (n->type) {
1365 case ROFFT_HEAD:
1366 outflags |= MMAN_PP | MMAN_nl;
1367 bln = n->parent->parent;
1368 if (0 == bln->norm->Bl.comp ||
1369 (NULL == n->parent->prev &&
1370 NULL == bln->parent->prev))
1371 outflags |= MMAN_sp;
1372 outflags &= ~MMAN_br;
1373 switch (bln->norm->Bl.type) {
1374 case LIST_item:
1375 return 0;
1376 case LIST_inset:
1377 case LIST_diag:
1378 case LIST_ohang:
1379 if (bln->norm->Bl.type == LIST_diag)
1380 print_line(".B \"", 0);
1381 else
1382 print_line(".R \"", 0);
1383 outflags &= ~MMAN_spc;
1384 return 1;
1385 case LIST_bullet:
1386 case LIST_dash:
1387 case LIST_hyphen:
1388 print_width(&bln->norm->Bl, NULL);
1389 TPremain = 0;
1390 outflags |= MMAN_nl;
1391 font_push('B');
1392 if (LIST_bullet == bln->norm->Bl.type)
1393 print_word("\\(bu");
1394 else
1395 print_word("-");
1396 font_pop();
1397 outflags |= MMAN_nl;
1398 return 0;
1399 case LIST_enum:
1400 print_width(&bln->norm->Bl, NULL);
1401 TPremain = 0;
1402 outflags |= MMAN_nl;
1403 print_count(&bln->norm->Bl.count);
1404 outflags |= MMAN_nl;
1405 return 0;
1406 case LIST_hang:
1407 print_width(&bln->norm->Bl, n->child);
1408 TPremain = 0;
1409 outflags |= MMAN_nl;
1410 return 1;
1411 case LIST_tag:
1412 print_width(&bln->norm->Bl, n->child);
1413 putchar('\n');
1414 outflags &= ~MMAN_spc;
1415 return 1;
1416 default:
1417 return 1;
1418 }
1419 default:
1420 break;
1421 }
1422 return 1;
1423 }
1424
1425 /*
1426 * This function is called after closing out an indented block.
1427 * If we are inside an enclosing list, restore its indentation.
1428 */
1429 static void
1430 mid_it(void)
1431 {
1432 char buf[24];
1433
1434 /* Nothing to do outside a list. */
1435 if (0 == Bl_stack_len || 0 == Bl_stack[Bl_stack_len - 1])
1436 return;
1437
1438 /* The indentation has already been set up. */
1439 if (Bl_stack_post[Bl_stack_len - 1])
1440 return;
1441
1442 /* Restore the indentation of the enclosing list. */
1443 print_line(".RS", MMAN_Bk_susp);
1444 (void)snprintf(buf, sizeof(buf), "%dn",
1445 Bl_stack[Bl_stack_len - 1]);
1446 print_word(buf);
1447
1448 /* Remeber to close out this .RS block later. */
1449 Bl_stack_post[Bl_stack_len - 1] = 1;
1450 }
1451
1452 static void
1453 post_it(DECL_ARGS)
1454 {
1455 const struct roff_node *bln;
1456
1457 bln = n->parent->parent;
1458
1459 switch (n->type) {
1460 case ROFFT_HEAD:
1461 switch (bln->norm->Bl.type) {
1462 case LIST_diag:
1463 outflags &= ~MMAN_spc;
1464 print_word("\\ ");
1465 break;
1466 case LIST_ohang:
1467 outflags |= MMAN_br;
1468 break;
1469 default:
1470 break;
1471 }
1472 break;
1473 case ROFFT_BODY:
1474 switch (bln->norm->Bl.type) {
1475 case LIST_bullet:
1476 case LIST_dash:
1477 case LIST_hyphen:
1478 case LIST_enum:
1479 case LIST_hang:
1480 case LIST_tag:
1481 assert(Bl_stack_len);
1482 Bl_stack[--Bl_stack_len] = 0;
1483
1484 /*
1485 * Our indentation had to be restored
1486 * after a child display or child list.
1487 * Close out that indentation block now.
1488 */
1489 if (Bl_stack_post[Bl_stack_len]) {
1490 print_line(".RE", MMAN_nl);
1491 Bl_stack_post[Bl_stack_len] = 0;
1492 }
1493 break;
1494 case LIST_column:
1495 if (NULL != n->next) {
1496 putchar('\t');
1497 outflags &= ~MMAN_spc;
1498 }
1499 break;
1500 default:
1501 break;
1502 }
1503 break;
1504 default:
1505 break;
1506 }
1507 }
1508
1509 static void
1510 post_lb(DECL_ARGS)
1511 {
1512
1513 if (SEC_LIBRARY == n->sec)
1514 outflags |= MMAN_br;
1515 }
1516
1517 static int
1518 pre_lk(DECL_ARGS)
1519 {
1520 const struct roff_node *link, *descr;
1521
1522 if (NULL == (link = n->child))
1523 return 0;
1524
1525 if (NULL != (descr = link->next)) {
1526 font_push('I');
1527 while (NULL != descr) {
1528 print_word(descr->string);
1529 descr = descr->next;
1530 }
1531 print_word(":");
1532 font_pop();
1533 }
1534
1535 font_push('B');
1536 print_word(link->string);
1537 font_pop();
1538 return 0;
1539 }
1540
1541 static int
1542 pre_ll(DECL_ARGS)
1543 {
1544
1545 print_line(".ll", 0);
1546 return 1;
1547 }
1548
1549 static int
1550 pre_li(DECL_ARGS)
1551 {
1552
1553 font_push('R');
1554 return 1;
1555 }
1556
1557 static int
1558 pre_nm(DECL_ARGS)
1559 {
1560 char *name;
1561
1562 if (n->type == ROFFT_BLOCK) {
1563 outflags |= MMAN_Bk;
1564 pre_syn(n);
1565 }
1566 if (n->type != ROFFT_ELEM && n->type != ROFFT_HEAD)
1567 return 1;
1568 name = n->child ? n->child->string : meta->name;
1569 if (NULL == name)
1570 return 0;
1571 if (n->type == ROFFT_HEAD) {
1572 if (NULL == n->parent->prev)
1573 outflags |= MMAN_sp;
1574 print_block(".HP", 0);
1575 printf(" %zun", strlen(name) + 1);
1576 outflags |= MMAN_nl;
1577 }
1578 font_push('B');
1579 if (NULL == n->child)
1580 print_word(meta->name);
1581 return 1;
1582 }
1583
1584 static void
1585 post_nm(DECL_ARGS)
1586 {
1587
1588 switch (n->type) {
1589 case ROFFT_BLOCK:
1590 outflags &= ~MMAN_Bk;
1591 break;
1592 case ROFFT_HEAD:
1593 case ROFFT_ELEM:
1594 if (n->child != NULL || meta->name != NULL)
1595 font_pop();
1596 break;
1597 default:
1598 break;
1599 }
1600 }
1601
1602 static int
1603 pre_no(DECL_ARGS)
1604 {
1605
1606 outflags |= MMAN_spc_force;
1607 return 1;
1608 }
1609
1610 static int
1611 pre_ns(DECL_ARGS)
1612 {
1613
1614 outflags &= ~MMAN_spc;
1615 return 0;
1616 }
1617
1618 static void
1619 post_pf(DECL_ARGS)
1620 {
1621
1622 if ( ! (n->next == NULL || n->next->flags & NODE_LINE))
1623 outflags &= ~MMAN_spc;
1624 }
1625
1626 static int
1627 pre_pp(DECL_ARGS)
1628 {
1629
1630 if (MDOC_It != n->parent->tok)
1631 outflags |= MMAN_PP;
1632 outflags |= MMAN_sp | MMAN_nl;
1633 outflags &= ~MMAN_br;
1634 return 0;
1635 }
1636
1637 static int
1638 pre_rs(DECL_ARGS)
1639 {
1640
1641 if (SEC_SEE_ALSO == n->sec) {
1642 outflags |= MMAN_PP | MMAN_sp | MMAN_nl;
1643 outflags &= ~MMAN_br;
1644 }
1645 return 1;
1646 }
1647
1648 static int
1649 pre_rv(DECL_ARGS)
1650 {
1651 struct roff_node *nch;
1652
1653 outflags |= MMAN_br | MMAN_nl;
1654
1655 if (n->child != NULL) {
1656 print_word("The");
1657
1658 for (nch = n->child; nch != NULL; nch = nch->next) {
1659 font_push('B');
1660 print_word(nch->string);
1661 font_pop();
1662
1663 outflags &= ~MMAN_spc;
1664 print_word("()");
1665
1666 if (nch->next == NULL)
1667 continue;
1668
1669 if (nch->prev != NULL || nch->next->next != NULL) {
1670 outflags &= ~MMAN_spc;
1671 print_word(",");
1672 }
1673 if (nch->next->next == NULL)
1674 print_word("and");
1675 }
1676
1677 if (n->child != NULL && n->child->next != NULL)
1678 print_word("functions return");
1679 else
1680 print_word("function returns");
1681
1682 print_word("the value\\~0 if successful;");
1683 } else
1684 print_word("Upon successful completion, "
1685 "the value\\~0 is returned;");
1686
1687 print_word("otherwise the value\\~\\-1 is returned"
1688 " and the global variable");
1689
1690 font_push('I');
1691 print_word("errno");
1692 font_pop();
1693
1694 print_word("is set to indicate the error.");
1695 outflags |= MMAN_nl;
1696 return 0;
1697 }
1698
1699 static int
1700 pre_skip(DECL_ARGS)
1701 {
1702
1703 return 0;
1704 }
1705
1706 static int
1707 pre_sm(DECL_ARGS)
1708 {
1709
1710 if (NULL == n->child)
1711 outflags ^= MMAN_Sm;
1712 else if (0 == strcmp("on", n->child->string))
1713 outflags |= MMAN_Sm;
1714 else
1715 outflags &= ~MMAN_Sm;
1716
1717 if (MMAN_Sm & outflags)
1718 outflags |= MMAN_spc;
1719
1720 return 0;
1721 }
1722
1723 static int
1724 pre_sp(DECL_ARGS)
1725 {
1726
1727 if (MMAN_PP & outflags) {
1728 outflags &= ~MMAN_PP;
1729 print_line(".PP", 0);
1730 } else
1731 print_line(".sp", 0);
1732 return 1;
1733 }
1734
1735 static void
1736 post_sp(DECL_ARGS)
1737 {
1738
1739 outflags |= MMAN_nl;
1740 }
1741
1742 static int
1743 pre_sy(DECL_ARGS)
1744 {
1745
1746 font_push('B');
1747 return 1;
1748 }
1749
1750 static int
1751 pre_vt(DECL_ARGS)
1752 {
1753
1754 if (NODE_SYNPRETTY & n->flags) {
1755 switch (n->type) {
1756 case ROFFT_BLOCK:
1757 pre_syn(n);
1758 return 1;
1759 case ROFFT_BODY:
1760 break;
1761 default:
1762 return 0;
1763 }
1764 }
1765 font_push('I');
1766 return 1;
1767 }
1768
1769 static void
1770 post_vt(DECL_ARGS)
1771 {
1772
1773 if (n->flags & NODE_SYNPRETTY && n->type != ROFFT_BODY)
1774 return;
1775 font_pop();
1776 }
1777
1778 static int
1779 pre_xr(DECL_ARGS)
1780 {
1781
1782 n = n->child;
1783 if (NULL == n)
1784 return 0;
1785 print_node(meta, n);
1786 n = n->next;
1787 if (NULL == n)
1788 return 0;
1789 outflags &= ~MMAN_spc;
1790 print_word("(");
1791 print_node(meta, n);
1792 print_word(")");
1793 return 0;
1794 }
1795
1796 static int
1797 pre_ux(DECL_ARGS)
1798 {
1799
1800 print_word(manacts[n->tok].prefix);
1801 if (NULL == n->child)
1802 return 0;
1803 outflags &= ~MMAN_spc;
1804 print_word("\\ ");
1805 outflags &= ~MMAN_spc;
1806 return 1;
1807 }