]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_man.c
Switch to autogenerated dependency rules; they are less error-prone.
[mandoc.git] / mdoc_man.c
1 /* $Id: mdoc_man.c,v 1.67 2014/07/30 00:19:16 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 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
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_bd(DECL_ARGS);
50 static void post_bf(DECL_ARGS);
51 static void post_bk(DECL_ARGS);
52 static void post_bl(DECL_ARGS);
53 static void post_dl(DECL_ARGS);
54 static void post_en(DECL_ARGS);
55 static void post_enc(DECL_ARGS);
56 static void post_eo(DECL_ARGS);
57 static void post_fa(DECL_ARGS);
58 static void post_fd(DECL_ARGS);
59 static void post_fl(DECL_ARGS);
60 static void post_fn(DECL_ARGS);
61 static void post_fo(DECL_ARGS);
62 static void post_font(DECL_ARGS);
63 static void post_in(DECL_ARGS);
64 static void post_it(DECL_ARGS);
65 static void post_lb(DECL_ARGS);
66 static void post_nm(DECL_ARGS);
67 static void post_percent(DECL_ARGS);
68 static void post_pf(DECL_ARGS);
69 static void post_sect(DECL_ARGS);
70 static void post_sp(DECL_ARGS);
71 static void post_vt(DECL_ARGS);
72 static int pre__t(DECL_ARGS);
73 static int pre_an(DECL_ARGS);
74 static int pre_ap(DECL_ARGS);
75 static int pre_bd(DECL_ARGS);
76 static int pre_bf(DECL_ARGS);
77 static int pre_bk(DECL_ARGS);
78 static int pre_bl(DECL_ARGS);
79 static int pre_br(DECL_ARGS);
80 static int pre_bx(DECL_ARGS);
81 static int pre_dl(DECL_ARGS);
82 static int pre_en(DECL_ARGS);
83 static int pre_enc(DECL_ARGS);
84 static int pre_em(DECL_ARGS);
85 static int pre_es(DECL_ARGS);
86 static int pre_ex(DECL_ARGS);
87 static int pre_fa(DECL_ARGS);
88 static int pre_fd(DECL_ARGS);
89 static int pre_fl(DECL_ARGS);
90 static int pre_fn(DECL_ARGS);
91 static int pre_fo(DECL_ARGS);
92 static int pre_ft(DECL_ARGS);
93 static int pre_in(DECL_ARGS);
94 static int pre_it(DECL_ARGS);
95 static int pre_lk(DECL_ARGS);
96 static int pre_li(DECL_ARGS);
97 static int pre_ll(DECL_ARGS);
98 static int pre_nm(DECL_ARGS);
99 static int pre_no(DECL_ARGS);
100 static int pre_ns(DECL_ARGS);
101 static int pre_pp(DECL_ARGS);
102 static int pre_rs(DECL_ARGS);
103 static int pre_rv(DECL_ARGS);
104 static int pre_sm(DECL_ARGS);
105 static int pre_sp(DECL_ARGS);
106 static int pre_sect(DECL_ARGS);
107 static int pre_sy(DECL_ARGS);
108 static void pre_syn(const struct mdoc_node *);
109 static int pre_vt(DECL_ARGS);
110 static int pre_ux(DECL_ARGS);
111 static int pre_xr(DECL_ARGS);
112 static void print_word(const char *);
113 static void print_line(const char *, int);
114 static void print_block(const char *, int);
115 static void print_offs(const char *);
116 static void print_width(const char *,
117 const struct mdoc_node *, size_t);
118 static void print_count(int *);
119 static void print_node(DECL_ARGS);
120
121 static const struct manact manacts[MDOC_MAX + 1] = {
122 { NULL, pre_ap, NULL, NULL, NULL }, /* Ap */
123 { NULL, NULL, NULL, NULL, NULL }, /* Dd */
124 { NULL, NULL, NULL, NULL, NULL }, /* Dt */
125 { NULL, NULL, NULL, NULL, NULL }, /* Os */
126 { NULL, pre_sect, post_sect, ".SH", NULL }, /* Sh */
127 { NULL, pre_sect, post_sect, ".SS", NULL }, /* Ss */
128 { NULL, pre_pp, NULL, NULL, NULL }, /* Pp */
129 { cond_body, pre_dl, post_dl, NULL, NULL }, /* D1 */
130 { cond_body, pre_dl, post_dl, NULL, NULL }, /* Dl */
131 { cond_body, pre_bd, post_bd, NULL, NULL }, /* Bd */
132 { NULL, NULL, NULL, NULL, NULL }, /* Ed */
133 { cond_body, pre_bl, post_bl, NULL, NULL }, /* Bl */
134 { NULL, NULL, NULL, NULL, NULL }, /* El */
135 { NULL, pre_it, post_it, NULL, NULL }, /* It */
136 { NULL, pre_em, post_font, NULL, NULL }, /* Ad */
137 { NULL, pre_an, NULL, NULL, NULL }, /* An */
138 { NULL, pre_em, post_font, NULL, NULL }, /* Ar */
139 { NULL, pre_sy, post_font, NULL, NULL }, /* Cd */
140 { NULL, pre_sy, post_font, NULL, NULL }, /* Cm */
141 { NULL, pre_li, post_font, NULL, NULL }, /* Dv */
142 { NULL, pre_li, post_font, NULL, NULL }, /* Er */
143 { NULL, pre_li, post_font, NULL, NULL }, /* Ev */
144 { NULL, pre_ex, NULL, NULL, NULL }, /* Ex */
145 { NULL, pre_fa, post_fa, NULL, NULL }, /* Fa */
146 { NULL, pre_fd, post_fd, NULL, NULL }, /* Fd */
147 { NULL, pre_fl, post_fl, NULL, NULL }, /* Fl */
148 { NULL, pre_fn, post_fn, NULL, NULL }, /* Fn */
149 { NULL, pre_ft, post_font, NULL, NULL }, /* Ft */
150 { NULL, pre_sy, post_font, NULL, NULL }, /* Ic */
151 { NULL, pre_in, post_in, NULL, NULL }, /* In */
152 { NULL, pre_li, post_font, NULL, NULL }, /* Li */
153 { cond_head, pre_enc, NULL, "\\- ", NULL }, /* Nd */
154 { NULL, pre_nm, post_nm, NULL, NULL }, /* Nm */
155 { cond_body, pre_enc, post_enc, "[", "]" }, /* Op */
156 { NULL, pre_ft, post_font, NULL, NULL }, /* Ot */
157 { NULL, pre_em, post_font, NULL, NULL }, /* Pa */
158 { NULL, pre_rv, NULL, NULL, NULL }, /* Rv */
159 { NULL, NULL, NULL, NULL, NULL }, /* St */
160 { NULL, pre_em, post_font, NULL, NULL }, /* Va */
161 { NULL, pre_vt, post_vt, NULL, NULL }, /* Vt */
162 { NULL, pre_xr, NULL, NULL, NULL }, /* Xr */
163 { NULL, NULL, post_percent, NULL, NULL }, /* %A */
164 { NULL, pre_em, post_percent, NULL, NULL }, /* %B */
165 { NULL, NULL, post_percent, NULL, NULL }, /* %D */
166 { NULL, pre_em, post_percent, NULL, NULL }, /* %I */
167 { NULL, pre_em, post_percent, NULL, NULL }, /* %J */
168 { NULL, NULL, post_percent, NULL, NULL }, /* %N */
169 { NULL, NULL, post_percent, NULL, NULL }, /* %O */
170 { NULL, NULL, post_percent, NULL, NULL }, /* %P */
171 { NULL, NULL, post_percent, NULL, NULL }, /* %R */
172 { NULL, pre__t, post__t, NULL, NULL }, /* %T */
173 { NULL, NULL, post_percent, NULL, NULL }, /* %V */
174 { NULL, NULL, NULL, NULL, NULL }, /* Ac */
175 { cond_body, pre_enc, post_enc, "<", ">" }, /* Ao */
176 { cond_body, pre_enc, post_enc, "<", ">" }, /* Aq */
177 { NULL, NULL, NULL, NULL, NULL }, /* At */
178 { NULL, NULL, NULL, NULL, NULL }, /* Bc */
179 { NULL, pre_bf, post_bf, NULL, NULL }, /* Bf */
180 { cond_body, pre_enc, post_enc, "[", "]" }, /* Bo */
181 { cond_body, pre_enc, post_enc, "[", "]" }, /* Bq */
182 { NULL, pre_ux, NULL, "BSD/OS", NULL }, /* Bsx */
183 { NULL, pre_bx, NULL, NULL, NULL }, /* Bx */
184 { NULL, NULL, NULL, NULL, NULL }, /* Db */
185 { NULL, NULL, NULL, NULL, NULL }, /* Dc */
186 { cond_body, pre_enc, post_enc, "\\(lq", "\\(rq" }, /* Do */
187 { cond_body, pre_enc, post_enc, "\\(lq", "\\(rq" }, /* Dq */
188 { NULL, NULL, NULL, NULL, NULL }, /* Ec */
189 { NULL, NULL, NULL, NULL, NULL }, /* Ef */
190 { NULL, pre_em, post_font, NULL, NULL }, /* Em */
191 { NULL, NULL, post_eo, NULL, NULL }, /* Eo */
192 { NULL, pre_ux, NULL, "FreeBSD", NULL }, /* Fx */
193 { NULL, pre_sy, post_font, NULL, NULL }, /* Ms */
194 { NULL, pre_no, NULL, NULL, NULL }, /* No */
195 { NULL, pre_ns, NULL, NULL, NULL }, /* Ns */
196 { NULL, pre_ux, NULL, "NetBSD", NULL }, /* Nx */
197 { NULL, pre_ux, NULL, "OpenBSD", NULL }, /* Ox */
198 { NULL, NULL, NULL, NULL, NULL }, /* Pc */
199 { NULL, NULL, post_pf, NULL, NULL }, /* Pf */
200 { cond_body, pre_enc, post_enc, "(", ")" }, /* Po */
201 { cond_body, pre_enc, post_enc, "(", ")" }, /* Pq */
202 { NULL, NULL, NULL, NULL, NULL }, /* Qc */
203 { cond_body, pre_enc, post_enc, "\\(oq", "\\(cq" }, /* Ql */
204 { cond_body, pre_enc, post_enc, "\"", "\"" }, /* Qo */
205 { cond_body, pre_enc, post_enc, "\"", "\"" }, /* Qq */
206 { NULL, NULL, NULL, NULL, NULL }, /* Re */
207 { cond_body, pre_rs, NULL, NULL, NULL }, /* Rs */
208 { NULL, NULL, NULL, NULL, NULL }, /* Sc */
209 { cond_body, pre_enc, post_enc, "\\(oq", "\\(cq" }, /* So */
210 { cond_body, pre_enc, post_enc, "\\(oq", "\\(cq" }, /* Sq */
211 { NULL, pre_sm, NULL, NULL, NULL }, /* Sm */
212 { NULL, pre_em, post_font, NULL, NULL }, /* Sx */
213 { NULL, pre_sy, post_font, NULL, NULL }, /* Sy */
214 { NULL, pre_li, post_font, NULL, NULL }, /* Tn */
215 { NULL, pre_ux, NULL, "UNIX", NULL }, /* Ux */
216 { NULL, NULL, NULL, NULL, NULL }, /* Xc */
217 { NULL, NULL, NULL, NULL, NULL }, /* Xo */
218 { NULL, pre_fo, post_fo, NULL, NULL }, /* Fo */
219 { NULL, NULL, NULL, NULL, NULL }, /* Fc */
220 { cond_body, pre_enc, post_enc, "[", "]" }, /* Oo */
221 { NULL, NULL, NULL, NULL, NULL }, /* Oc */
222 { NULL, pre_bk, post_bk, NULL, NULL }, /* Bk */
223 { NULL, NULL, NULL, NULL, NULL }, /* Ek */
224 { NULL, pre_ux, NULL, "is currently in beta test.", NULL }, /* Bt */
225 { NULL, NULL, NULL, NULL, NULL }, /* Hf */
226 { NULL, pre_em, post_font, NULL, NULL }, /* Fr */
227 { NULL, pre_ux, NULL, "currently under development.", NULL }, /* Ud */
228 { NULL, NULL, post_lb, NULL, NULL }, /* Lb */
229 { NULL, pre_pp, NULL, NULL, NULL }, /* Lp */
230 { NULL, pre_lk, NULL, NULL, NULL }, /* Lk */
231 { NULL, pre_em, post_font, NULL, NULL }, /* Mt */
232 { cond_body, pre_enc, post_enc, "{", "}" }, /* Brq */
233 { cond_body, pre_enc, post_enc, "{", "}" }, /* Bro */
234 { NULL, NULL, NULL, NULL, NULL }, /* Brc */
235 { NULL, NULL, post_percent, NULL, NULL }, /* %C */
236 { NULL, pre_es, NULL, NULL, NULL }, /* Es */
237 { cond_body, pre_en, post_en, NULL, NULL }, /* En */
238 { NULL, pre_ux, NULL, "DragonFly", NULL }, /* Dx */
239 { NULL, NULL, post_percent, NULL, NULL }, /* %Q */
240 { NULL, pre_br, NULL, NULL, NULL }, /* br */
241 { NULL, pre_sp, post_sp, NULL, NULL }, /* sp */
242 { NULL, NULL, post_percent, NULL, NULL }, /* %U */
243 { NULL, NULL, NULL, NULL, NULL }, /* Ta */
244 { NULL, pre_ll, post_sp, NULL, NULL }, /* ll */
245 { NULL, NULL, NULL, NULL, NULL }, /* ROOT */
246 };
247
248 static int outflags;
249 #define MMAN_spc (1 << 0) /* blank character before next word */
250 #define MMAN_spc_force (1 << 1) /* even before trailing punctuation */
251 #define MMAN_nl (1 << 2) /* break man(7) code line */
252 #define MMAN_br (1 << 3) /* break output line */
253 #define MMAN_sp (1 << 4) /* insert a blank output line */
254 #define MMAN_PP (1 << 5) /* reset indentation etc. */
255 #define MMAN_Sm (1 << 6) /* horizontal spacing mode */
256 #define MMAN_Bk (1 << 7) /* word keep mode */
257 #define MMAN_Bk_susp (1 << 8) /* suspend this (after a macro) */
258 #define MMAN_An_split (1 << 9) /* author mode is "split" */
259 #define MMAN_An_nosplit (1 << 10) /* author mode is "nosplit" */
260 #define MMAN_PD (1 << 11) /* inter-paragraph spacing disabled */
261 #define MMAN_nbrword (1 << 12) /* do not break the next word */
262
263 #define BL_STACK_MAX 32
264
265 static size_t Bl_stack[BL_STACK_MAX]; /* offsets [chars] */
266 static int Bl_stack_post[BL_STACK_MAX]; /* add final .RE */
267 static int Bl_stack_len; /* number of nested Bl blocks */
268 static int TPremain; /* characters before tag is full */
269
270 static struct {
271 char *head;
272 char *tail;
273 size_t size;
274 } fontqueue;
275
276
277 static void
278 font_push(char newfont)
279 {
280
281 if (fontqueue.head + fontqueue.size <= ++fontqueue.tail) {
282 fontqueue.size += 8;
283 fontqueue.head = mandoc_realloc(fontqueue.head,
284 fontqueue.size);
285 }
286 *fontqueue.tail = newfont;
287 print_word("");
288 printf("\\f");
289 putchar(newfont);
290 outflags &= ~MMAN_spc;
291 }
292
293 static void
294 font_pop(void)
295 {
296
297 if (fontqueue.tail > fontqueue.head)
298 fontqueue.tail--;
299 outflags &= ~MMAN_spc;
300 print_word("");
301 printf("\\f");
302 putchar(*fontqueue.tail);
303 }
304
305 static void
306 print_word(const char *s)
307 {
308
309 if ((MMAN_PP | MMAN_sp | MMAN_br | MMAN_nl) & outflags) {
310 /*
311 * If we need a newline, print it now and start afresh.
312 */
313 if (MMAN_PP & outflags) {
314 if (MMAN_sp & outflags) {
315 if (MMAN_PD & outflags) {
316 printf("\n.PD");
317 outflags &= ~MMAN_PD;
318 }
319 } else if ( ! (MMAN_PD & outflags)) {
320 printf("\n.PD 0");
321 outflags |= MMAN_PD;
322 }
323 printf("\n.PP\n");
324 } else if (MMAN_sp & outflags)
325 printf("\n.sp\n");
326 else if (MMAN_br & outflags)
327 printf("\n.br\n");
328 else if (MMAN_nl & outflags)
329 putchar('\n');
330 outflags &= ~(MMAN_PP|MMAN_sp|MMAN_br|MMAN_nl|MMAN_spc);
331 if (1 == TPremain)
332 printf(".br\n");
333 TPremain = 0;
334 } else if (MMAN_spc & outflags) {
335 /*
336 * If we need a space, only print it if
337 * (1) it is forced by `No' or
338 * (2) what follows is not terminating punctuation or
339 * (3) what follows is longer than one character.
340 */
341 if (MMAN_spc_force & outflags || '\0' == s[0] ||
342 NULL == strchr(".,:;)]?!", s[0]) || '\0' != s[1]) {
343 if (MMAN_Bk & outflags &&
344 ! (MMAN_Bk_susp & outflags))
345 putchar('\\');
346 putchar(' ');
347 if (TPremain)
348 TPremain--;
349 }
350 }
351
352 /*
353 * Reassign needing space if we're not following opening
354 * punctuation.
355 */
356 if (MMAN_Sm & outflags && ('\0' == s[0] ||
357 (('(' != s[0] && '[' != s[0]) || '\0' != s[1])))
358 outflags |= MMAN_spc;
359 else
360 outflags &= ~MMAN_spc;
361 outflags &= ~(MMAN_spc_force | MMAN_Bk_susp);
362
363 for ( ; *s; s++) {
364 switch (*s) {
365 case ASCII_NBRSP:
366 printf("\\ ");
367 break;
368 case ASCII_HYPH:
369 putchar('-');
370 break;
371 case ASCII_BREAK:
372 printf("\\:");
373 break;
374 case ' ':
375 if (MMAN_nbrword & outflags) {
376 printf("\\ ");
377 break;
378 }
379 /* FALLTHROUGH */
380 default:
381 putchar((unsigned char)*s);
382 break;
383 }
384 if (TPremain)
385 TPremain--;
386 }
387 outflags &= ~MMAN_nbrword;
388 }
389
390 static void
391 print_line(const char *s, int newflags)
392 {
393
394 outflags &= ~MMAN_br;
395 outflags |= MMAN_nl;
396 print_word(s);
397 outflags |= newflags;
398 }
399
400 static void
401 print_block(const char *s, int newflags)
402 {
403
404 outflags &= ~MMAN_PP;
405 if (MMAN_sp & outflags) {
406 outflags &= ~(MMAN_sp | MMAN_br);
407 if (MMAN_PD & outflags) {
408 print_line(".PD", 0);
409 outflags &= ~MMAN_PD;
410 }
411 } else if (! (MMAN_PD & outflags))
412 print_line(".PD 0", MMAN_PD);
413 outflags |= MMAN_nl;
414 print_word(s);
415 outflags |= MMAN_Bk_susp | newflags;
416 }
417
418 static void
419 print_offs(const char *v)
420 {
421 char buf[24];
422 struct roffsu su;
423 size_t sz;
424
425 print_line(".RS", MMAN_Bk_susp);
426
427 /* Convert v into a number (of characters). */
428 if (NULL == v || '\0' == *v || 0 == strcmp(v, "left"))
429 sz = 0;
430 else if (0 == strcmp(v, "indent"))
431 sz = 6;
432 else if (0 == strcmp(v, "indent-two"))
433 sz = 12;
434 else if (a2roffsu(v, &su, SCALE_MAX)) {
435 if (SCALE_EN == su.unit)
436 sz = su.scale;
437 else {
438 /*
439 * XXX
440 * If we are inside an enclosing list,
441 * there is no easy way to add the two
442 * indentations because they are provided
443 * in terms of different units.
444 */
445 print_word(v);
446 outflags |= MMAN_nl;
447 return;
448 }
449 } else
450 sz = strlen(v);
451
452 /*
453 * We are inside an enclosing list.
454 * Add the two indentations.
455 */
456 if (Bl_stack_len)
457 sz += Bl_stack[Bl_stack_len - 1];
458
459 (void)snprintf(buf, sizeof(buf), "%zun", sz);
460 print_word(buf);
461 outflags |= MMAN_nl;
462 }
463
464 /*
465 * Set up the indentation for a list item; used from pre_it().
466 */
467 static void
468 print_width(const char *v, const struct mdoc_node *child, size_t defsz)
469 {
470 char buf[24];
471 struct roffsu su;
472 size_t sz, chsz;
473 int numeric, remain;
474
475 numeric = 1;
476 remain = 0;
477
478 /* Convert v into a number (of characters). */
479 if (NULL == v)
480 sz = defsz;
481 else if (a2roffsu(v, &su, SCALE_MAX)) {
482 if (SCALE_EN == su.unit)
483 sz = su.scale;
484 else {
485 sz = 0;
486 numeric = 0;
487 }
488 } else
489 sz = strlen(v);
490
491 /* XXX Rough estimation, might have multiple parts. */
492 chsz = (NULL != child && MDOC_TEXT == child->type) ?
493 strlen(child->string) : 0;
494
495 /* Maybe we are inside an enclosing list? */
496 mid_it();
497
498 /*
499 * Save our own indentation,
500 * such that child lists can use it.
501 */
502 Bl_stack[Bl_stack_len++] = sz + 2;
503
504 /* Set up the current list. */
505 if (defsz && chsz > sz)
506 print_block(".HP", 0);
507 else {
508 print_block(".TP", 0);
509 remain = sz + 2;
510 }
511 if (numeric) {
512 (void)snprintf(buf, sizeof(buf), "%zun", sz + 2);
513 print_word(buf);
514 } else
515 print_word(v);
516 TPremain = remain;
517 }
518
519 static void
520 print_count(int *count)
521 {
522 char buf[24];
523
524 (void)snprintf(buf, sizeof(buf), "%d.", ++*count);
525 print_word(buf);
526 }
527
528 void
529 man_man(void *arg, const struct man *man)
530 {
531
532 /*
533 * Dump the keep buffer.
534 * We're guaranteed by now that this exists (is non-NULL).
535 * Flush stdout afterward, just in case.
536 */
537 fputs(mparse_getkeep(man_mparse(man)), stdout);
538 fflush(stdout);
539 }
540
541 void
542 man_mdoc(void *arg, const struct mdoc *mdoc)
543 {
544 const struct mdoc_meta *meta;
545 const struct mdoc_node *n;
546
547 meta = mdoc_meta(mdoc);
548 n = mdoc_node(mdoc);
549
550 printf(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"\n",
551 meta->title, meta->msec, meta->date,
552 meta->os, meta->vol);
553
554 /* Disable hyphenation and if nroff, disable justification. */
555 printf(".nh\n.if n .ad l");
556
557 outflags = MMAN_nl | MMAN_Sm;
558 if (0 == fontqueue.size) {
559 fontqueue.size = 8;
560 fontqueue.head = fontqueue.tail = mandoc_malloc(8);
561 *fontqueue.tail = 'R';
562 }
563 print_node(meta, n);
564 putchar('\n');
565 }
566
567 static void
568 print_node(DECL_ARGS)
569 {
570 const struct mdoc_node *sub;
571 const struct manact *act;
572 int cond, do_sub;
573
574 /*
575 * Break the line if we were parsed subsequent the current node.
576 * This makes the page structure be more consistent.
577 */
578 if (MMAN_spc & outflags && MDOC_LINE & n->flags)
579 outflags |= MMAN_nl;
580
581 act = NULL;
582 cond = 0;
583 do_sub = 1;
584
585 if (MDOC_TEXT == n->type) {
586 /*
587 * Make sure that we don't happen to start with a
588 * control character at the start of a line.
589 */
590 if (MMAN_nl & outflags &&
591 ('.' == *n->string || '\'' == *n->string)) {
592 print_word("");
593 printf("\\&");
594 outflags &= ~MMAN_spc;
595 }
596 print_word(n->string);
597 } else {
598 /*
599 * Conditionally run the pre-node action handler for a
600 * node.
601 */
602 act = manacts + n->tok;
603 cond = NULL == act->cond || (*act->cond)(meta, n);
604 if (cond && act->pre && ENDBODY_NOT == n->end)
605 do_sub = (*act->pre)(meta, n);
606 }
607
608 /*
609 * Conditionally run all child nodes.
610 * Note that this iterates over children instead of using
611 * recursion. This prevents unnecessary depth in the stack.
612 */
613 if (do_sub)
614 for (sub = n->child; sub; sub = sub->next)
615 print_node(meta, sub);
616
617 /*
618 * Lastly, conditionally run the post-node handler.
619 */
620 if (MDOC_ENDED & n->flags)
621 return;
622
623 if (cond && act->post)
624 (*act->post)(meta, n);
625
626 if (ENDBODY_NOT != n->end)
627 n->pending->flags |= MDOC_ENDED;
628
629 if (ENDBODY_NOSPACE == n->end)
630 outflags &= ~(MMAN_spc | MMAN_nl);
631 }
632
633 static int
634 cond_head(DECL_ARGS)
635 {
636
637 return(MDOC_HEAD == n->type);
638 }
639
640 static int
641 cond_body(DECL_ARGS)
642 {
643
644 return(MDOC_BODY == n->type);
645 }
646
647 static int
648 pre_enc(DECL_ARGS)
649 {
650 const char *prefix;
651
652 prefix = manacts[n->tok].prefix;
653 if (NULL == prefix)
654 return(1);
655 print_word(prefix);
656 outflags &= ~MMAN_spc;
657 return(1);
658 }
659
660 static void
661 post_enc(DECL_ARGS)
662 {
663 const char *suffix;
664
665 suffix = manacts[n->tok].suffix;
666 if (NULL == suffix)
667 return;
668 outflags &= ~(MMAN_spc | MMAN_nl);
669 print_word(suffix);
670 }
671
672 static int
673 pre_ex(DECL_ARGS)
674 {
675 int nchild;
676
677 outflags |= MMAN_br | MMAN_nl;
678
679 print_word("The");
680
681 nchild = n->nchild;
682 for (n = n->child; n; n = n->next) {
683 font_push('B');
684 print_word(n->string);
685 font_pop();
686
687 if (n->next == NULL)
688 continue;
689
690 if (nchild > 2) {
691 outflags &= ~MMAN_spc;
692 print_word(",");
693 }
694 if (n->next->next == NULL)
695 print_word("and");
696 }
697
698 if (nchild > 1)
699 print_word("utilities exit\\~0");
700 else
701 print_word("utility exits\\~0");
702
703 print_word("on success, and\\~>0 if an error occurs.");
704 outflags |= MMAN_nl;
705 return(0);
706 }
707
708 static void
709 post_font(DECL_ARGS)
710 {
711
712 font_pop();
713 }
714
715 static void
716 post_percent(DECL_ARGS)
717 {
718
719 if (pre_em == manacts[n->tok].pre)
720 font_pop();
721 if (n->next) {
722 print_word(",");
723 if (n->prev && n->prev->tok == n->tok &&
724 n->next->tok == n->tok)
725 print_word("and");
726 } else {
727 print_word(".");
728 outflags |= MMAN_nl;
729 }
730 }
731
732 static int
733 pre__t(DECL_ARGS)
734 {
735
736 if (n->parent && MDOC_Rs == n->parent->tok &&
737 n->parent->norm->Rs.quote_T) {
738 print_word("");
739 putchar('\"');
740 outflags &= ~MMAN_spc;
741 } else
742 font_push('I');
743 return(1);
744 }
745
746 static void
747 post__t(DECL_ARGS)
748 {
749
750 if (n->parent && MDOC_Rs == n->parent->tok &&
751 n->parent->norm->Rs.quote_T) {
752 outflags &= ~MMAN_spc;
753 print_word("");
754 putchar('\"');
755 } else
756 font_pop();
757 post_percent(meta, n);
758 }
759
760 /*
761 * Print before a section header.
762 */
763 static int
764 pre_sect(DECL_ARGS)
765 {
766
767 if (MDOC_HEAD == n->type) {
768 outflags |= MMAN_sp;
769 print_block(manacts[n->tok].prefix, 0);
770 print_word("");
771 putchar('\"');
772 outflags &= ~MMAN_spc;
773 }
774 return(1);
775 }
776
777 /*
778 * Print subsequent a section header.
779 */
780 static void
781 post_sect(DECL_ARGS)
782 {
783
784 if (MDOC_HEAD != n->type)
785 return;
786 outflags &= ~MMAN_spc;
787 print_word("");
788 putchar('\"');
789 outflags |= MMAN_nl;
790 if (MDOC_Sh == n->tok && SEC_AUTHORS == n->sec)
791 outflags &= ~(MMAN_An_split | MMAN_An_nosplit);
792 }
793
794 /* See mdoc_term.c, synopsis_pre() for comments. */
795 static void
796 pre_syn(const struct mdoc_node *n)
797 {
798
799 if (NULL == n->prev || ! (MDOC_SYNPRETTY & n->flags))
800 return;
801
802 if (n->prev->tok == n->tok &&
803 MDOC_Ft != n->tok &&
804 MDOC_Fo != n->tok &&
805 MDOC_Fn != n->tok) {
806 outflags |= MMAN_br;
807 return;
808 }
809
810 switch (n->prev->tok) {
811 case MDOC_Fd:
812 /* FALLTHROUGH */
813 case MDOC_Fn:
814 /* FALLTHROUGH */
815 case MDOC_Fo:
816 /* FALLTHROUGH */
817 case MDOC_In:
818 /* FALLTHROUGH */
819 case MDOC_Vt:
820 outflags |= MMAN_sp;
821 break;
822 case MDOC_Ft:
823 if (MDOC_Fn != n->tok && MDOC_Fo != n->tok) {
824 outflags |= MMAN_sp;
825 break;
826 }
827 /* FALLTHROUGH */
828 default:
829 outflags |= MMAN_br;
830 break;
831 }
832 }
833
834 static int
835 pre_an(DECL_ARGS)
836 {
837
838 switch (n->norm->An.auth) {
839 case AUTH_split:
840 outflags &= ~MMAN_An_nosplit;
841 outflags |= MMAN_An_split;
842 return(0);
843 case AUTH_nosplit:
844 outflags &= ~MMAN_An_split;
845 outflags |= MMAN_An_nosplit;
846 return(0);
847 default:
848 if (MMAN_An_split & outflags)
849 outflags |= MMAN_br;
850 else if (SEC_AUTHORS == n->sec &&
851 ! (MMAN_An_nosplit & outflags))
852 outflags |= MMAN_An_split;
853 return(1);
854 }
855 }
856
857 static int
858 pre_ap(DECL_ARGS)
859 {
860
861 outflags &= ~MMAN_spc;
862 print_word("'");
863 outflags &= ~MMAN_spc;
864 return(0);
865 }
866
867 static int
868 pre_bd(DECL_ARGS)
869 {
870
871 outflags &= ~(MMAN_PP | MMAN_sp | MMAN_br);
872
873 if (DISP_unfilled == n->norm->Bd.type ||
874 DISP_literal == n->norm->Bd.type)
875 print_line(".nf", 0);
876 if (0 == n->norm->Bd.comp && NULL != n->parent->prev)
877 outflags |= MMAN_sp;
878 print_offs(n->norm->Bd.offs);
879 return(1);
880 }
881
882 static void
883 post_bd(DECL_ARGS)
884 {
885
886 /* Close out this display. */
887 print_line(".RE", MMAN_nl);
888 if (DISP_unfilled == n->norm->Bd.type ||
889 DISP_literal == n->norm->Bd.type)
890 print_line(".fi", MMAN_nl);
891
892 /* Maybe we are inside an enclosing list? */
893 if (NULL != n->parent->next)
894 mid_it();
895 }
896
897 static int
898 pre_bf(DECL_ARGS)
899 {
900
901 switch (n->type) {
902 case MDOC_BLOCK:
903 return(1);
904 case MDOC_BODY:
905 break;
906 default:
907 return(0);
908 }
909 switch (n->norm->Bf.font) {
910 case FONT_Em:
911 font_push('I');
912 break;
913 case FONT_Sy:
914 font_push('B');
915 break;
916 default:
917 font_push('R');
918 break;
919 }
920 return(1);
921 }
922
923 static void
924 post_bf(DECL_ARGS)
925 {
926
927 if (MDOC_BODY == n->type)
928 font_pop();
929 }
930
931 static int
932 pre_bk(DECL_ARGS)
933 {
934
935 switch (n->type) {
936 case MDOC_BLOCK:
937 return(1);
938 case MDOC_BODY:
939 outflags |= MMAN_Bk;
940 return(1);
941 default:
942 return(0);
943 }
944 }
945
946 static void
947 post_bk(DECL_ARGS)
948 {
949
950 if (MDOC_BODY == n->type)
951 outflags &= ~MMAN_Bk;
952 }
953
954 static int
955 pre_bl(DECL_ARGS)
956 {
957 size_t icol;
958
959 /*
960 * print_offs() will increase the -offset to account for
961 * a possible enclosing .It, but any enclosed .It blocks
962 * just nest and do not add up their indentation.
963 */
964 if (n->norm->Bl.offs) {
965 print_offs(n->norm->Bl.offs);
966 Bl_stack[Bl_stack_len++] = 0;
967 }
968
969 switch (n->norm->Bl.type) {
970 case LIST_enum:
971 n->norm->Bl.count = 0;
972 return(1);
973 case LIST_column:
974 break;
975 default:
976 return(1);
977 }
978
979 print_line(".TS", MMAN_nl);
980 for (icol = 0; icol < n->norm->Bl.ncols; icol++)
981 print_word("l");
982 print_word(".");
983 outflags |= MMAN_nl;
984 return(1);
985 }
986
987 static void
988 post_bl(DECL_ARGS)
989 {
990
991 switch (n->norm->Bl.type) {
992 case LIST_column:
993 print_line(".TE", 0);
994 break;
995 case LIST_enum:
996 n->norm->Bl.count = 0;
997 break;
998 default:
999 break;
1000 }
1001
1002 if (n->norm->Bl.offs) {
1003 print_line(".RE", MMAN_nl);
1004 assert(Bl_stack_len);
1005 Bl_stack_len--;
1006 assert(0 == Bl_stack[Bl_stack_len]);
1007 } else {
1008 outflags |= MMAN_PP | MMAN_nl;
1009 outflags &= ~(MMAN_sp | MMAN_br);
1010 }
1011
1012 /* Maybe we are inside an enclosing list? */
1013 if (NULL != n->parent->next)
1014 mid_it();
1015
1016 }
1017
1018 static int
1019 pre_br(DECL_ARGS)
1020 {
1021
1022 outflags |= MMAN_br;
1023 return(0);
1024 }
1025
1026 static int
1027 pre_bx(DECL_ARGS)
1028 {
1029
1030 n = n->child;
1031 if (n) {
1032 print_word(n->string);
1033 outflags &= ~MMAN_spc;
1034 n = n->next;
1035 }
1036 print_word("BSD");
1037 if (NULL == n)
1038 return(0);
1039 outflags &= ~MMAN_spc;
1040 print_word("-");
1041 outflags &= ~MMAN_spc;
1042 print_word(n->string);
1043 return(0);
1044 }
1045
1046 static int
1047 pre_dl(DECL_ARGS)
1048 {
1049
1050 print_offs("6n");
1051 return(1);
1052 }
1053
1054 static void
1055 post_dl(DECL_ARGS)
1056 {
1057
1058 print_line(".RE", MMAN_nl);
1059
1060 /* Maybe we are inside an enclosing list? */
1061 if (NULL != n->parent->next)
1062 mid_it();
1063 }
1064
1065 static int
1066 pre_em(DECL_ARGS)
1067 {
1068
1069 font_push('I');
1070 return(1);
1071 }
1072
1073 static int
1074 pre_en(DECL_ARGS)
1075 {
1076
1077 if (NULL == n->norm->Es ||
1078 NULL == n->norm->Es->child)
1079 return(1);
1080
1081 print_word(n->norm->Es->child->string);
1082 outflags &= ~MMAN_spc;
1083 return(1);
1084 }
1085
1086 static void
1087 post_en(DECL_ARGS)
1088 {
1089
1090 if (NULL == n->norm->Es ||
1091 NULL == n->norm->Es->child ||
1092 NULL == n->norm->Es->child->next)
1093 return;
1094
1095 outflags &= ~MMAN_spc;
1096 print_word(n->norm->Es->child->next->string);
1097 return;
1098 }
1099
1100 static void
1101 post_eo(DECL_ARGS)
1102 {
1103
1104 if (MDOC_HEAD == n->type || MDOC_BODY == n->type)
1105 outflags &= ~MMAN_spc;
1106 }
1107
1108 static int
1109 pre_es(DECL_ARGS)
1110 {
1111
1112 return(0);
1113 }
1114
1115 static int
1116 pre_fa(DECL_ARGS)
1117 {
1118 int am_Fa;
1119
1120 am_Fa = MDOC_Fa == n->tok;
1121
1122 if (am_Fa)
1123 n = n->child;
1124
1125 while (NULL != n) {
1126 font_push('I');
1127 if (am_Fa || MDOC_SYNPRETTY & n->flags)
1128 outflags |= MMAN_nbrword;
1129 print_node(meta, n);
1130 font_pop();
1131 if (NULL != (n = n->next))
1132 print_word(",");
1133 }
1134 return(0);
1135 }
1136
1137 static void
1138 post_fa(DECL_ARGS)
1139 {
1140
1141 if (NULL != n->next && MDOC_Fa == n->next->tok)
1142 print_word(",");
1143 }
1144
1145 static int
1146 pre_fd(DECL_ARGS)
1147 {
1148
1149 pre_syn(n);
1150 font_push('B');
1151 return(1);
1152 }
1153
1154 static void
1155 post_fd(DECL_ARGS)
1156 {
1157
1158 font_pop();
1159 outflags |= MMAN_br;
1160 }
1161
1162 static int
1163 pre_fl(DECL_ARGS)
1164 {
1165
1166 font_push('B');
1167 print_word("\\-");
1168 outflags &= ~MMAN_spc;
1169 return(1);
1170 }
1171
1172 static void
1173 post_fl(DECL_ARGS)
1174 {
1175
1176 font_pop();
1177 if (0 == n->nchild && NULL != n->next &&
1178 n->next->line == n->line)
1179 outflags &= ~MMAN_spc;
1180 }
1181
1182 static int
1183 pre_fn(DECL_ARGS)
1184 {
1185
1186 pre_syn(n);
1187
1188 n = n->child;
1189 if (NULL == n)
1190 return(0);
1191
1192 if (MDOC_SYNPRETTY & n->flags)
1193 print_block(".HP 4n", MMAN_nl);
1194
1195 font_push('B');
1196 print_node(meta, n);
1197 font_pop();
1198 outflags &= ~MMAN_spc;
1199 print_word("(");
1200 outflags &= ~MMAN_spc;
1201
1202 n = n->next;
1203 if (NULL != n)
1204 pre_fa(meta, n);
1205 return(0);
1206 }
1207
1208 static void
1209 post_fn(DECL_ARGS)
1210 {
1211
1212 print_word(")");
1213 if (MDOC_SYNPRETTY & n->flags) {
1214 print_word(";");
1215 outflags |= MMAN_PP;
1216 }
1217 }
1218
1219 static int
1220 pre_fo(DECL_ARGS)
1221 {
1222
1223 switch (n->type) {
1224 case MDOC_BLOCK:
1225 pre_syn(n);
1226 break;
1227 case MDOC_HEAD:
1228 if (MDOC_SYNPRETTY & n->flags)
1229 print_block(".HP 4n", MMAN_nl);
1230 font_push('B');
1231 break;
1232 case MDOC_BODY:
1233 outflags &= ~MMAN_spc;
1234 print_word("(");
1235 outflags &= ~MMAN_spc;
1236 break;
1237 default:
1238 break;
1239 }
1240 return(1);
1241 }
1242
1243 static void
1244 post_fo(DECL_ARGS)
1245 {
1246
1247 switch (n->type) {
1248 case MDOC_HEAD:
1249 font_pop();
1250 break;
1251 case MDOC_BODY:
1252 post_fn(meta, n);
1253 break;
1254 default:
1255 break;
1256 }
1257 }
1258
1259 static int
1260 pre_ft(DECL_ARGS)
1261 {
1262
1263 pre_syn(n);
1264 font_push('I');
1265 return(1);
1266 }
1267
1268 static int
1269 pre_in(DECL_ARGS)
1270 {
1271
1272 if (MDOC_SYNPRETTY & n->flags) {
1273 pre_syn(n);
1274 font_push('B');
1275 print_word("#include <");
1276 outflags &= ~MMAN_spc;
1277 } else {
1278 print_word("<");
1279 outflags &= ~MMAN_spc;
1280 font_push('I');
1281 }
1282 return(1);
1283 }
1284
1285 static void
1286 post_in(DECL_ARGS)
1287 {
1288
1289 if (MDOC_SYNPRETTY & n->flags) {
1290 outflags &= ~MMAN_spc;
1291 print_word(">");
1292 font_pop();
1293 outflags |= MMAN_br;
1294 } else {
1295 font_pop();
1296 outflags &= ~MMAN_spc;
1297 print_word(">");
1298 }
1299 }
1300
1301 static int
1302 pre_it(DECL_ARGS)
1303 {
1304 const struct mdoc_node *bln;
1305
1306 switch (n->type) {
1307 case MDOC_HEAD:
1308 outflags |= MMAN_PP | MMAN_nl;
1309 bln = n->parent->parent;
1310 if (0 == bln->norm->Bl.comp ||
1311 (NULL == n->parent->prev &&
1312 NULL == bln->parent->prev))
1313 outflags |= MMAN_sp;
1314 outflags &= ~MMAN_br;
1315 switch (bln->norm->Bl.type) {
1316 case LIST_item:
1317 return(0);
1318 case LIST_inset:
1319 /* FALLTHROUGH */
1320 case LIST_diag:
1321 /* FALLTHROUGH */
1322 case LIST_ohang:
1323 if (bln->norm->Bl.type == LIST_diag)
1324 print_line(".B \"", 0);
1325 else
1326 print_line(".R \"", 0);
1327 outflags &= ~MMAN_spc;
1328 return(1);
1329 case LIST_bullet:
1330 /* FALLTHROUGH */
1331 case LIST_dash:
1332 /* FALLTHROUGH */
1333 case LIST_hyphen:
1334 print_width(bln->norm->Bl.width, NULL, 0);
1335 TPremain = 0;
1336 outflags |= MMAN_nl;
1337 font_push('B');
1338 if (LIST_bullet == bln->norm->Bl.type)
1339 print_word("o");
1340 else
1341 print_word("-");
1342 font_pop();
1343 outflags |= MMAN_nl;
1344 return(0);
1345 case LIST_enum:
1346 print_width(bln->norm->Bl.width, NULL, 0);
1347 TPremain = 0;
1348 outflags |= MMAN_nl;
1349 print_count(&bln->norm->Bl.count);
1350 outflags |= MMAN_nl;
1351 return(0);
1352 case LIST_hang:
1353 print_width(bln->norm->Bl.width, n->child, 6);
1354 TPremain = 0;
1355 outflags |= MMAN_nl;
1356 return(1);
1357 case LIST_tag:
1358 print_width(bln->norm->Bl.width, n->child, 0);
1359 putchar('\n');
1360 outflags &= ~MMAN_spc;
1361 return(1);
1362 default:
1363 return(1);
1364 }
1365 default:
1366 break;
1367 }
1368 return(1);
1369 }
1370
1371 /*
1372 * This function is called after closing out an indented block.
1373 * If we are inside an enclosing list, restore its indentation.
1374 */
1375 static void
1376 mid_it(void)
1377 {
1378 char buf[24];
1379
1380 /* Nothing to do outside a list. */
1381 if (0 == Bl_stack_len || 0 == Bl_stack[Bl_stack_len - 1])
1382 return;
1383
1384 /* The indentation has already been set up. */
1385 if (Bl_stack_post[Bl_stack_len - 1])
1386 return;
1387
1388 /* Restore the indentation of the enclosing list. */
1389 print_line(".RS", MMAN_Bk_susp);
1390 (void)snprintf(buf, sizeof(buf), "%zun",
1391 Bl_stack[Bl_stack_len - 1]);
1392 print_word(buf);
1393
1394 /* Remeber to close out this .RS block later. */
1395 Bl_stack_post[Bl_stack_len - 1] = 1;
1396 }
1397
1398 static void
1399 post_it(DECL_ARGS)
1400 {
1401 const struct mdoc_node *bln;
1402
1403 bln = n->parent->parent;
1404
1405 switch (n->type) {
1406 case MDOC_HEAD:
1407 switch (bln->norm->Bl.type) {
1408 case LIST_diag:
1409 outflags &= ~MMAN_spc;
1410 print_word("\\ ");
1411 break;
1412 case LIST_ohang:
1413 outflags |= MMAN_br;
1414 break;
1415 default:
1416 break;
1417 }
1418 break;
1419 case MDOC_BODY:
1420 switch (bln->norm->Bl.type) {
1421 case LIST_bullet:
1422 /* FALLTHROUGH */
1423 case LIST_dash:
1424 /* FALLTHROUGH */
1425 case LIST_hyphen:
1426 /* FALLTHROUGH */
1427 case LIST_enum:
1428 /* FALLTHROUGH */
1429 case LIST_hang:
1430 /* FALLTHROUGH */
1431 case LIST_tag:
1432 assert(Bl_stack_len);
1433 Bl_stack[--Bl_stack_len] = 0;
1434
1435 /*
1436 * Our indentation had to be restored
1437 * after a child display or child list.
1438 * Close out that indentation block now.
1439 */
1440 if (Bl_stack_post[Bl_stack_len]) {
1441 print_line(".RE", MMAN_nl);
1442 Bl_stack_post[Bl_stack_len] = 0;
1443 }
1444 break;
1445 case LIST_column:
1446 if (NULL != n->next) {
1447 putchar('\t');
1448 outflags &= ~MMAN_spc;
1449 }
1450 break;
1451 default:
1452 break;
1453 }
1454 break;
1455 default:
1456 break;
1457 }
1458 }
1459
1460 static void
1461 post_lb(DECL_ARGS)
1462 {
1463
1464 if (SEC_LIBRARY == n->sec)
1465 outflags |= MMAN_br;
1466 }
1467
1468 static int
1469 pre_lk(DECL_ARGS)
1470 {
1471 const struct mdoc_node *link, *descr;
1472
1473 if (NULL == (link = n->child))
1474 return(0);
1475
1476 if (NULL != (descr = link->next)) {
1477 font_push('I');
1478 while (NULL != descr) {
1479 print_word(descr->string);
1480 descr = descr->next;
1481 }
1482 print_word(":");
1483 font_pop();
1484 }
1485
1486 font_push('B');
1487 print_word(link->string);
1488 font_pop();
1489 return(0);
1490 }
1491
1492 static int
1493 pre_ll(DECL_ARGS)
1494 {
1495
1496 print_line(".ll", 0);
1497 return(1);
1498 }
1499
1500 static int
1501 pre_li(DECL_ARGS)
1502 {
1503
1504 font_push('R');
1505 return(1);
1506 }
1507
1508 static int
1509 pre_nm(DECL_ARGS)
1510 {
1511 char *name;
1512
1513 if (MDOC_BLOCK == n->type) {
1514 outflags |= MMAN_Bk;
1515 pre_syn(n);
1516 }
1517 if (MDOC_ELEM != n->type && MDOC_HEAD != n->type)
1518 return(1);
1519 name = n->child ? n->child->string : meta->name;
1520 if (NULL == name)
1521 return(0);
1522 if (MDOC_HEAD == n->type) {
1523 if (NULL == n->parent->prev)
1524 outflags |= MMAN_sp;
1525 print_block(".HP", 0);
1526 printf(" %zun", strlen(name) + 1);
1527 outflags |= MMAN_nl;
1528 }
1529 font_push('B');
1530 if (NULL == n->child)
1531 print_word(meta->name);
1532 return(1);
1533 }
1534
1535 static void
1536 post_nm(DECL_ARGS)
1537 {
1538
1539 switch (n->type) {
1540 case MDOC_BLOCK:
1541 outflags &= ~MMAN_Bk;
1542 break;
1543 case MDOC_HEAD:
1544 /* FALLTHROUGH */
1545 case MDOC_ELEM:
1546 if (n->child != NULL || meta->name != NULL)
1547 font_pop();
1548 break;
1549 default:
1550 break;
1551 }
1552 }
1553
1554 static int
1555 pre_no(DECL_ARGS)
1556 {
1557
1558 outflags |= MMAN_spc_force;
1559 return(1);
1560 }
1561
1562 static int
1563 pre_ns(DECL_ARGS)
1564 {
1565
1566 outflags &= ~MMAN_spc;
1567 return(0);
1568 }
1569
1570 static void
1571 post_pf(DECL_ARGS)
1572 {
1573
1574 outflags &= ~MMAN_spc;
1575 }
1576
1577 static int
1578 pre_pp(DECL_ARGS)
1579 {
1580
1581 if (MDOC_It != n->parent->tok)
1582 outflags |= MMAN_PP;
1583 outflags |= MMAN_sp | MMAN_nl;
1584 outflags &= ~MMAN_br;
1585 return(0);
1586 }
1587
1588 static int
1589 pre_rs(DECL_ARGS)
1590 {
1591
1592 if (SEC_SEE_ALSO == n->sec) {
1593 outflags |= MMAN_PP | MMAN_sp | MMAN_nl;
1594 outflags &= ~MMAN_br;
1595 }
1596 return(1);
1597 }
1598
1599 static int
1600 pre_rv(DECL_ARGS)
1601 {
1602 int nchild;
1603
1604 outflags |= MMAN_br | MMAN_nl;
1605
1606 nchild = n->nchild;
1607 if (nchild > 0) {
1608 print_word("The");
1609
1610 for (n = n->child; n; n = n->next) {
1611 font_push('B');
1612 print_word(n->string);
1613 font_pop();
1614
1615 outflags &= ~MMAN_spc;
1616 print_word("()");
1617
1618 if (n->next == NULL)
1619 continue;
1620
1621 if (nchild > 2) {
1622 outflags &= ~MMAN_spc;
1623 print_word(",");
1624 }
1625 if (n->next->next == NULL)
1626 print_word("and");
1627 }
1628
1629 if (nchild > 1)
1630 print_word("functions return");
1631 else
1632 print_word("function returns");
1633
1634 print_word("the value\\~0 if successful;");
1635 } else
1636 print_word("Upon successful completion, "
1637 "the value\\~0 is returned;");
1638
1639 print_word("otherwise the value\\~\\-1 is returned"
1640 " and the global variable");
1641
1642 font_push('I');
1643 print_word("errno");
1644 font_pop();
1645
1646 print_word("is set to indicate the error.");
1647 outflags |= MMAN_nl;
1648 return(0);
1649 }
1650
1651 static int
1652 pre_sm(DECL_ARGS)
1653 {
1654
1655 if (NULL == n->child)
1656 outflags ^= MMAN_Sm;
1657 else if (0 == strcmp("on", n->child->string))
1658 outflags |= MMAN_Sm;
1659 else
1660 outflags &= ~MMAN_Sm;
1661
1662 if (MMAN_Sm & outflags)
1663 outflags |= MMAN_spc;
1664
1665 return(0);
1666 }
1667
1668 static int
1669 pre_sp(DECL_ARGS)
1670 {
1671
1672 if (MMAN_PP & outflags) {
1673 outflags &= ~MMAN_PP;
1674 print_line(".PP", 0);
1675 } else
1676 print_line(".sp", 0);
1677 return(1);
1678 }
1679
1680 static void
1681 post_sp(DECL_ARGS)
1682 {
1683
1684 outflags |= MMAN_nl;
1685 }
1686
1687 static int
1688 pre_sy(DECL_ARGS)
1689 {
1690
1691 font_push('B');
1692 return(1);
1693 }
1694
1695 static int
1696 pre_vt(DECL_ARGS)
1697 {
1698
1699 if (MDOC_SYNPRETTY & n->flags) {
1700 switch (n->type) {
1701 case MDOC_BLOCK:
1702 pre_syn(n);
1703 return(1);
1704 case MDOC_BODY:
1705 break;
1706 default:
1707 return(0);
1708 }
1709 }
1710 font_push('I');
1711 return(1);
1712 }
1713
1714 static void
1715 post_vt(DECL_ARGS)
1716 {
1717
1718 if (MDOC_SYNPRETTY & n->flags && MDOC_BODY != n->type)
1719 return;
1720 font_pop();
1721 }
1722
1723 static int
1724 pre_xr(DECL_ARGS)
1725 {
1726
1727 n = n->child;
1728 if (NULL == n)
1729 return(0);
1730 print_node(meta, n);
1731 n = n->next;
1732 if (NULL == n)
1733 return(0);
1734 outflags &= ~MMAN_spc;
1735 print_word("(");
1736 print_node(meta, n);
1737 print_word(")");
1738 return(0);
1739 }
1740
1741 static int
1742 pre_ux(DECL_ARGS)
1743 {
1744
1745 print_word(manacts[n->tok].prefix);
1746 if (NULL == n->child)
1747 return(0);
1748 outflags &= ~MMAN_spc;
1749 print_word("\\ ");
1750 outflags &= ~MMAN_spc;
1751 return(1);
1752 }