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