]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_man.c
Further apropos(1) speed optimization was trickier than anticipated.
[mandoc.git] / mdoc_man.c
1 /* $Id: mdoc_man.c,v 1.61 2014/03/30 23:34:32 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_enc(DECL_ARGS);
55 static void post_eo(DECL_ARGS);
56 static void post_fa(DECL_ARGS);
57 static void post_fd(DECL_ARGS);
58 static void post_fl(DECL_ARGS);
59 static void post_fn(DECL_ARGS);
60 static void post_fo(DECL_ARGS);
61 static void post_font(DECL_ARGS);
62 static void post_in(DECL_ARGS);
63 static void post_it(DECL_ARGS);
64 static void post_lb(DECL_ARGS);
65 static void post_nm(DECL_ARGS);
66 static void post_percent(DECL_ARGS);
67 static void post_pf(DECL_ARGS);
68 static void post_sect(DECL_ARGS);
69 static void post_sp(DECL_ARGS);
70 static void post_vt(DECL_ARGS);
71 static int pre__t(DECL_ARGS);
72 static int pre_an(DECL_ARGS);
73 static int pre_ap(DECL_ARGS);
74 static int pre_bd(DECL_ARGS);
75 static int pre_bf(DECL_ARGS);
76 static int pre_bk(DECL_ARGS);
77 static int pre_bl(DECL_ARGS);
78 static int pre_br(DECL_ARGS);
79 static int pre_bx(DECL_ARGS);
80 static int pre_dl(DECL_ARGS);
81 static int pre_enc(DECL_ARGS);
82 static int pre_em(DECL_ARGS);
83 static int pre_fa(DECL_ARGS);
84 static int pre_fd(DECL_ARGS);
85 static int pre_fl(DECL_ARGS);
86 static int pre_fn(DECL_ARGS);
87 static int pre_fo(DECL_ARGS);
88 static int pre_ft(DECL_ARGS);
89 static int pre_in(DECL_ARGS);
90 static int pre_it(DECL_ARGS);
91 static int pre_lk(DECL_ARGS);
92 static int pre_li(DECL_ARGS);
93 static int pre_ll(DECL_ARGS);
94 static int pre_nm(DECL_ARGS);
95 static int pre_no(DECL_ARGS);
96 static int pre_ns(DECL_ARGS);
97 static int pre_pp(DECL_ARGS);
98 static int pre_rs(DECL_ARGS);
99 static int pre_sm(DECL_ARGS);
100 static int pre_sp(DECL_ARGS);
101 static int pre_sect(DECL_ARGS);
102 static int pre_sy(DECL_ARGS);
103 static void pre_syn(const struct mdoc_node *);
104 static int pre_vt(DECL_ARGS);
105 static int pre_ux(DECL_ARGS);
106 static int pre_xr(DECL_ARGS);
107 static void print_word(const char *);
108 static void print_line(const char *, int);
109 static void print_block(const char *, int);
110 static void print_offs(const char *);
111 static void print_width(const char *,
112 const struct mdoc_node *, size_t);
113 static void print_count(int *);
114 static void print_node(DECL_ARGS);
115
116 static const struct manact manacts[MDOC_MAX + 1] = {
117 { NULL, pre_ap, NULL, NULL, NULL }, /* Ap */
118 { NULL, NULL, NULL, NULL, NULL }, /* Dd */
119 { NULL, NULL, NULL, NULL, NULL }, /* Dt */
120 { NULL, NULL, NULL, NULL, NULL }, /* Os */
121 { NULL, pre_sect, post_sect, ".SH", NULL }, /* Sh */
122 { NULL, pre_sect, post_sect, ".SS", NULL }, /* Ss */
123 { NULL, pre_pp, NULL, NULL, NULL }, /* Pp */
124 { cond_body, pre_dl, post_dl, NULL, NULL }, /* D1 */
125 { cond_body, pre_dl, post_dl, NULL, NULL }, /* Dl */
126 { cond_body, pre_bd, post_bd, NULL, NULL }, /* Bd */
127 { NULL, NULL, NULL, NULL, NULL }, /* Ed */
128 { cond_body, pre_bl, post_bl, NULL, NULL }, /* Bl */
129 { NULL, NULL, NULL, NULL, NULL }, /* El */
130 { NULL, pre_it, post_it, NULL, NULL }, /* It */
131 { NULL, pre_em, post_font, NULL, NULL }, /* Ad */
132 { NULL, pre_an, NULL, NULL, NULL }, /* An */
133 { NULL, pre_em, post_font, NULL, NULL }, /* Ar */
134 { NULL, pre_sy, post_font, NULL, NULL }, /* Cd */
135 { NULL, pre_sy, post_font, NULL, NULL }, /* Cm */
136 { NULL, pre_li, post_font, NULL, NULL }, /* Dv */
137 { NULL, pre_li, post_font, NULL, NULL }, /* Er */
138 { NULL, pre_li, post_font, NULL, NULL }, /* Ev */
139 { NULL, pre_enc, post_enc, "The \\fB",
140 "\\fP\nutility exits 0 on success, and >0 if an error occurs."
141 }, /* Ex */
142 { NULL, pre_fa, post_fa, NULL, NULL }, /* Fa */
143 { NULL, pre_fd, post_fd, NULL, NULL }, /* Fd */
144 { NULL, pre_fl, post_fl, NULL, NULL }, /* Fl */
145 { NULL, pre_fn, post_fn, NULL, NULL }, /* Fn */
146 { NULL, pre_ft, post_font, NULL, NULL }, /* Ft */
147 { NULL, pre_sy, post_font, NULL, NULL }, /* Ic */
148 { NULL, pre_in, post_in, NULL, NULL }, /* In */
149 { NULL, pre_li, post_font, NULL, NULL }, /* Li */
150 { cond_head, pre_enc, NULL, "\\- ", NULL }, /* Nd */
151 { NULL, pre_nm, post_nm, NULL, NULL }, /* Nm */
152 { cond_body, pre_enc, post_enc, "[", "]" }, /* Op */
153 { NULL, NULL, NULL, NULL, NULL }, /* Ot */
154 { NULL, pre_em, post_font, NULL, NULL }, /* Pa */
155 { NULL, pre_enc, post_enc, "The \\fB",
156 "\\fP\nfunction returns the value 0 if successful;\n"
157 "otherwise the value -1 is returned and the global\n"
158 "variable \\fIerrno\\fP is set to indicate the error."
159 }, /* Rv */
160 { NULL, NULL, NULL, NULL, NULL }, /* St */
161 { NULL, pre_em, post_font, NULL, NULL }, /* Va */
162 { NULL, pre_vt, post_vt, NULL, NULL }, /* Vt */
163 { NULL, pre_xr, NULL, NULL, NULL }, /* Xr */
164 { NULL, NULL, post_percent, NULL, NULL }, /* %A */
165 { NULL, pre_em, post_percent, NULL, NULL }, /* %B */
166 { NULL, NULL, post_percent, NULL, NULL }, /* %D */
167 { NULL, pre_em, post_percent, NULL, NULL }, /* %I */
168 { NULL, pre_em, post_percent, NULL, NULL }, /* %J */
169 { NULL, NULL, post_percent, NULL, NULL }, /* %N */
170 { NULL, NULL, post_percent, NULL, NULL }, /* %O */
171 { NULL, NULL, post_percent, NULL, NULL }, /* %P */
172 { NULL, NULL, post_percent, NULL, NULL }, /* %R */
173 { NULL, pre__t, post__t, NULL, NULL }, /* %T */
174 { NULL, NULL, post_percent, NULL, NULL }, /* %V */
175 { NULL, NULL, NULL, NULL, NULL }, /* Ac */
176 { cond_body, pre_enc, post_enc, "<", ">" }, /* Ao */
177 { cond_body, pre_enc, post_enc, "<", ">" }, /* Aq */
178 { NULL, NULL, NULL, NULL, NULL }, /* At */
179 { NULL, NULL, NULL, NULL, NULL }, /* Bc */
180 { NULL, pre_bf, post_bf, NULL, NULL }, /* Bf */
181 { cond_body, pre_enc, post_enc, "[", "]" }, /* Bo */
182 { cond_body, pre_enc, post_enc, "[", "]" }, /* Bq */
183 { NULL, pre_ux, NULL, "BSD/OS", NULL }, /* Bsx */
184 { NULL, pre_bx, NULL, NULL, NULL }, /* Bx */
185 { NULL, NULL, NULL, NULL, NULL }, /* Db */
186 { NULL, NULL, NULL, NULL, NULL }, /* Dc */
187 { cond_body, pre_enc, post_enc, "\\(lq", "\\(rq" }, /* Do */
188 { cond_body, pre_enc, post_enc, "\\(lq", "\\(rq" }, /* Dq */
189 { NULL, NULL, NULL, NULL, NULL }, /* Ec */
190 { NULL, NULL, NULL, NULL, NULL }, /* Ef */
191 { NULL, pre_em, post_font, NULL, NULL }, /* Em */
192 { NULL, NULL, post_eo, NULL, NULL }, /* Eo */
193 { NULL, pre_ux, NULL, "FreeBSD", NULL }, /* Fx */
194 { NULL, pre_sy, post_font, NULL, NULL }, /* Ms */
195 { NULL, pre_no, NULL, NULL, NULL }, /* No */
196 { NULL, pre_ns, NULL, NULL, NULL }, /* Ns */
197 { NULL, pre_ux, NULL, "NetBSD", NULL }, /* Nx */
198 { NULL, pre_ux, NULL, "OpenBSD", NULL }, /* Ox */
199 { NULL, NULL, NULL, NULL, NULL }, /* Pc */
200 { NULL, NULL, post_pf, NULL, NULL }, /* Pf */
201 { cond_body, pre_enc, post_enc, "(", ")" }, /* Po */
202 { cond_body, pre_enc, post_enc, "(", ")" }, /* Pq */
203 { NULL, NULL, NULL, NULL, NULL }, /* Qc */
204 { cond_body, pre_enc, post_enc, "\\(oq", "\\(cq" }, /* Ql */
205 { cond_body, pre_enc, post_enc, "\"", "\"" }, /* Qo */
206 { cond_body, pre_enc, post_enc, "\"", "\"" }, /* Qq */
207 { NULL, NULL, NULL, NULL, NULL }, /* Re */
208 { cond_body, pre_rs, NULL, NULL, NULL }, /* Rs */
209 { NULL, NULL, NULL, NULL, NULL }, /* Sc */
210 { cond_body, pre_enc, post_enc, "\\(oq", "\\(cq" }, /* So */
211 { cond_body, pre_enc, post_enc, "\\(oq", "\\(cq" }, /* Sq */
212 { NULL, pre_sm, NULL, NULL, NULL }, /* Sm */
213 { NULL, pre_em, post_font, NULL, NULL }, /* Sx */
214 { NULL, pre_sy, post_font, NULL, NULL }, /* Sy */
215 { NULL, pre_li, post_font, NULL, NULL }, /* Tn */
216 { NULL, pre_ux, NULL, "UNIX", NULL }, /* Ux */
217 { NULL, NULL, NULL, NULL, NULL }, /* Xc */
218 { NULL, NULL, NULL, NULL, NULL }, /* Xo */
219 { NULL, pre_fo, post_fo, NULL, NULL }, /* Fo */
220 { NULL, NULL, NULL, NULL, NULL }, /* Fc */
221 { cond_body, pre_enc, post_enc, "[", "]" }, /* Oo */
222 { NULL, NULL, NULL, NULL, NULL }, /* Oc */
223 { NULL, pre_bk, post_bk, NULL, NULL }, /* Bk */
224 { NULL, NULL, NULL, NULL, NULL }, /* Ek */
225 { NULL, pre_ux, NULL, "is currently in beta test.", NULL }, /* Bt */
226 { NULL, NULL, NULL, NULL, NULL }, /* Hf */
227 { NULL, NULL, NULL, NULL, NULL }, /* Fr */
228 { NULL, pre_ux, NULL, "currently under development.", NULL }, /* Ud */
229 { NULL, NULL, post_lb, NULL, NULL }, /* Lb */
230 { NULL, pre_pp, NULL, NULL, NULL }, /* Lp */
231 { NULL, pre_lk, NULL, NULL, NULL }, /* Lk */
232 { NULL, pre_em, post_font, NULL, NULL }, /* Mt */
233 { cond_body, pre_enc, post_enc, "{", "}" }, /* Brq */
234 { cond_body, pre_enc, post_enc, "{", "}" }, /* Bro */
235 { NULL, NULL, NULL, NULL, NULL }, /* Brc */
236 { NULL, NULL, post_percent, NULL, NULL }, /* %C */
237 { NULL, NULL, NULL, NULL, NULL }, /* Es */
238 { NULL, NULL, NULL, NULL, NULL }, /* En */
239 { NULL, pre_ux, NULL, "DragonFly", NULL }, /* Dx */
240 { NULL, NULL, post_percent, NULL, NULL }, /* %Q */
241 { NULL, pre_br, NULL, NULL, NULL }, /* br */
242 { NULL, pre_sp, post_sp, NULL, NULL }, /* sp */
243 { NULL, NULL, post_percent, NULL, NULL }, /* %U */
244 { NULL, NULL, NULL, NULL, NULL }, /* Ta */
245 { NULL, pre_ll, post_sp, NULL, NULL }, /* ll */
246 { NULL, NULL, NULL, NULL, NULL }, /* ROOT */
247 };
248
249 static int outflags;
250 #define MMAN_spc (1 << 0) /* blank character before next word */
251 #define MMAN_spc_force (1 << 1) /* even before trailing punctuation */
252 #define MMAN_nl (1 << 2) /* break man(7) code line */
253 #define MMAN_br (1 << 3) /* break output line */
254 #define MMAN_sp (1 << 4) /* insert a blank output line */
255 #define MMAN_PP (1 << 5) /* reset indentation etc. */
256 #define MMAN_Sm (1 << 6) /* horizontal spacing mode */
257 #define MMAN_Bk (1 << 7) /* word keep mode */
258 #define MMAN_Bk_susp (1 << 8) /* suspend this (after a macro) */
259 #define MMAN_An_split (1 << 9) /* author mode is "split" */
260 #define MMAN_An_nosplit (1 << 10) /* author mode is "nosplit" */
261 #define MMAN_PD (1 << 11) /* inter-paragraph spacing disabled */
262 #define MMAN_nbrword (1 << 12) /* do not break the next word */
263
264 #define BL_STACK_MAX 32
265
266 static size_t Bl_stack[BL_STACK_MAX]; /* offsets [chars] */
267 static int Bl_stack_post[BL_STACK_MAX]; /* add final .RE */
268 static int Bl_stack_len; /* number of nested Bl blocks */
269 static int TPremain; /* characters before tag is full */
270
271 static struct {
272 char *head;
273 char *tail;
274 size_t size;
275 } fontqueue;
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 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 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[12];
523
524 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 && ('.' == *n->string ||
591 '\'' == *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 void
673 post_font(DECL_ARGS)
674 {
675
676 font_pop();
677 }
678
679 static void
680 post_percent(DECL_ARGS)
681 {
682
683 if (pre_em == manacts[n->tok].pre)
684 font_pop();
685 if (n->next) {
686 print_word(",");
687 if (n->prev && n->prev->tok == n->tok &&
688 n->next->tok == n->tok)
689 print_word("and");
690 } else {
691 print_word(".");
692 outflags |= MMAN_nl;
693 }
694 }
695
696 static int
697 pre__t(DECL_ARGS)
698 {
699
700 if (n->parent && MDOC_Rs == n->parent->tok &&
701 n->parent->norm->Rs.quote_T) {
702 print_word("");
703 putchar('\"');
704 outflags &= ~MMAN_spc;
705 } else
706 font_push('I');
707 return(1);
708 }
709
710 static void
711 post__t(DECL_ARGS)
712 {
713
714 if (n->parent && MDOC_Rs == n->parent->tok &&
715 n->parent->norm->Rs.quote_T) {
716 outflags &= ~MMAN_spc;
717 print_word("");
718 putchar('\"');
719 } else
720 font_pop();
721 post_percent(meta, n);
722 }
723
724 /*
725 * Print before a section header.
726 */
727 static int
728 pre_sect(DECL_ARGS)
729 {
730
731 if (MDOC_HEAD == n->type) {
732 outflags |= MMAN_sp;
733 print_block(manacts[n->tok].prefix, 0);
734 print_word("");
735 putchar('\"');
736 outflags &= ~MMAN_spc;
737 }
738 return(1);
739 }
740
741 /*
742 * Print subsequent a section header.
743 */
744 static void
745 post_sect(DECL_ARGS)
746 {
747
748 if (MDOC_HEAD != n->type)
749 return;
750 outflags &= ~MMAN_spc;
751 print_word("");
752 putchar('\"');
753 outflags |= MMAN_nl;
754 if (MDOC_Sh == n->tok && SEC_AUTHORS == n->sec)
755 outflags &= ~(MMAN_An_split | MMAN_An_nosplit);
756 }
757
758 /* See mdoc_term.c, synopsis_pre() for comments. */
759 static void
760 pre_syn(const struct mdoc_node *n)
761 {
762
763 if (NULL == n->prev || ! (MDOC_SYNPRETTY & n->flags))
764 return;
765
766 if (n->prev->tok == n->tok &&
767 MDOC_Ft != n->tok &&
768 MDOC_Fo != n->tok &&
769 MDOC_Fn != n->tok) {
770 outflags |= MMAN_br;
771 return;
772 }
773
774 switch (n->prev->tok) {
775 case (MDOC_Fd):
776 /* FALLTHROUGH */
777 case (MDOC_Fn):
778 /* FALLTHROUGH */
779 case (MDOC_Fo):
780 /* FALLTHROUGH */
781 case (MDOC_In):
782 /* FALLTHROUGH */
783 case (MDOC_Vt):
784 outflags |= MMAN_sp;
785 break;
786 case (MDOC_Ft):
787 if (MDOC_Fn != n->tok && MDOC_Fo != n->tok) {
788 outflags |= MMAN_sp;
789 break;
790 }
791 /* FALLTHROUGH */
792 default:
793 outflags |= MMAN_br;
794 break;
795 }
796 }
797
798 static int
799 pre_an(DECL_ARGS)
800 {
801
802 switch (n->norm->An.auth) {
803 case (AUTH_split):
804 outflags &= ~MMAN_An_nosplit;
805 outflags |= MMAN_An_split;
806 return(0);
807 case (AUTH_nosplit):
808 outflags &= ~MMAN_An_split;
809 outflags |= MMAN_An_nosplit;
810 return(0);
811 default:
812 if (MMAN_An_split & outflags)
813 outflags |= MMAN_br;
814 else if (SEC_AUTHORS == n->sec &&
815 ! (MMAN_An_nosplit & outflags))
816 outflags |= MMAN_An_split;
817 return(1);
818 }
819 }
820
821 static int
822 pre_ap(DECL_ARGS)
823 {
824
825 outflags &= ~MMAN_spc;
826 print_word("'");
827 outflags &= ~MMAN_spc;
828 return(0);
829 }
830
831 static int
832 pre_bd(DECL_ARGS)
833 {
834
835 outflags &= ~(MMAN_PP | MMAN_sp | MMAN_br);
836
837 if (DISP_unfilled == n->norm->Bd.type ||
838 DISP_literal == n->norm->Bd.type)
839 print_line(".nf", 0);
840 if (0 == n->norm->Bd.comp && NULL != n->parent->prev)
841 outflags |= MMAN_sp;
842 print_offs(n->norm->Bd.offs);
843 return(1);
844 }
845
846 static void
847 post_bd(DECL_ARGS)
848 {
849
850 /* Close out this display. */
851 print_line(".RE", MMAN_nl);
852 if (DISP_unfilled == n->norm->Bd.type ||
853 DISP_literal == n->norm->Bd.type)
854 print_line(".fi", MMAN_nl);
855
856 /* Maybe we are inside an enclosing list? */
857 if (NULL != n->parent->next)
858 mid_it();
859 }
860
861 static int
862 pre_bf(DECL_ARGS)
863 {
864
865 switch (n->type) {
866 case (MDOC_BLOCK):
867 return(1);
868 case (MDOC_BODY):
869 break;
870 default:
871 return(0);
872 }
873 switch (n->norm->Bf.font) {
874 case (FONT_Em):
875 font_push('I');
876 break;
877 case (FONT_Sy):
878 font_push('B');
879 break;
880 default:
881 font_push('R');
882 break;
883 }
884 return(1);
885 }
886
887 static void
888 post_bf(DECL_ARGS)
889 {
890
891 if (MDOC_BODY == n->type)
892 font_pop();
893 }
894
895 static int
896 pre_bk(DECL_ARGS)
897 {
898
899 switch (n->type) {
900 case (MDOC_BLOCK):
901 return(1);
902 case (MDOC_BODY):
903 outflags |= MMAN_Bk;
904 return(1);
905 default:
906 return(0);
907 }
908 }
909
910 static void
911 post_bk(DECL_ARGS)
912 {
913
914 if (MDOC_BODY == n->type)
915 outflags &= ~MMAN_Bk;
916 }
917
918 static int
919 pre_bl(DECL_ARGS)
920 {
921 size_t icol;
922
923 /*
924 * print_offs() will increase the -offset to account for
925 * a possible enclosing .It, but any enclosed .It blocks
926 * just nest and do not add up their indentation.
927 */
928 if (n->norm->Bl.offs) {
929 print_offs(n->norm->Bl.offs);
930 Bl_stack[Bl_stack_len++] = 0;
931 }
932
933 switch (n->norm->Bl.type) {
934 case (LIST_enum):
935 n->norm->Bl.count = 0;
936 return(1);
937 case (LIST_column):
938 break;
939 default:
940 return(1);
941 }
942
943 print_line(".TS", MMAN_nl);
944 for (icol = 0; icol < n->norm->Bl.ncols; icol++)
945 print_word("l");
946 print_word(".");
947 outflags |= MMAN_nl;
948 return(1);
949 }
950
951 static void
952 post_bl(DECL_ARGS)
953 {
954
955 switch (n->norm->Bl.type) {
956 case (LIST_column):
957 print_line(".TE", 0);
958 break;
959 case (LIST_enum):
960 n->norm->Bl.count = 0;
961 break;
962 default:
963 break;
964 }
965
966 if (n->norm->Bl.offs) {
967 print_line(".RE", MMAN_nl);
968 assert(Bl_stack_len);
969 Bl_stack_len--;
970 assert(0 == Bl_stack[Bl_stack_len]);
971 } else {
972 outflags |= MMAN_PP | MMAN_nl;
973 outflags &= ~(MMAN_sp | MMAN_br);
974 }
975
976 /* Maybe we are inside an enclosing list? */
977 if (NULL != n->parent->next)
978 mid_it();
979
980 }
981
982 static int
983 pre_br(DECL_ARGS)
984 {
985
986 outflags |= MMAN_br;
987 return(0);
988 }
989
990 static int
991 pre_bx(DECL_ARGS)
992 {
993
994 n = n->child;
995 if (n) {
996 print_word(n->string);
997 outflags &= ~MMAN_spc;
998 n = n->next;
999 }
1000 print_word("BSD");
1001 if (NULL == n)
1002 return(0);
1003 outflags &= ~MMAN_spc;
1004 print_word("-");
1005 outflags &= ~MMAN_spc;
1006 print_word(n->string);
1007 return(0);
1008 }
1009
1010 static int
1011 pre_dl(DECL_ARGS)
1012 {
1013
1014 print_offs("6n");
1015 return(1);
1016 }
1017
1018 static void
1019 post_dl(DECL_ARGS)
1020 {
1021
1022 print_line(".RE", MMAN_nl);
1023
1024 /* Maybe we are inside an enclosing list? */
1025 if (NULL != n->parent->next)
1026 mid_it();
1027 }
1028
1029 static int
1030 pre_em(DECL_ARGS)
1031 {
1032
1033 font_push('I');
1034 return(1);
1035 }
1036
1037 static void
1038 post_eo(DECL_ARGS)
1039 {
1040
1041 if (MDOC_HEAD == n->type || MDOC_BODY == n->type)
1042 outflags &= ~MMAN_spc;
1043 }
1044
1045 static int
1046 pre_fa(DECL_ARGS)
1047 {
1048 int am_Fa;
1049
1050 am_Fa = MDOC_Fa == n->tok;
1051
1052 if (am_Fa)
1053 n = n->child;
1054
1055 while (NULL != n) {
1056 font_push('I');
1057 if (am_Fa || MDOC_SYNPRETTY & n->flags)
1058 outflags |= MMAN_nbrword;
1059 print_node(meta, n);
1060 font_pop();
1061 if (NULL != (n = n->next))
1062 print_word(",");
1063 }
1064 return(0);
1065 }
1066
1067 static void
1068 post_fa(DECL_ARGS)
1069 {
1070
1071 if (NULL != n->next && MDOC_Fa == n->next->tok)
1072 print_word(",");
1073 }
1074
1075 static int
1076 pre_fd(DECL_ARGS)
1077 {
1078
1079 pre_syn(n);
1080 font_push('B');
1081 return(1);
1082 }
1083
1084 static void
1085 post_fd(DECL_ARGS)
1086 {
1087
1088 font_pop();
1089 outflags |= MMAN_br;
1090 }
1091
1092 static int
1093 pre_fl(DECL_ARGS)
1094 {
1095
1096 font_push('B');
1097 print_word("\\-");
1098 outflags &= ~MMAN_spc;
1099 return(1);
1100 }
1101
1102 static void
1103 post_fl(DECL_ARGS)
1104 {
1105
1106 font_pop();
1107 if (0 == n->nchild && NULL != n->next &&
1108 n->next->line == n->line)
1109 outflags &= ~MMAN_spc;
1110 }
1111
1112 static int
1113 pre_fn(DECL_ARGS)
1114 {
1115
1116 pre_syn(n);
1117
1118 n = n->child;
1119 if (NULL == n)
1120 return(0);
1121
1122 if (MDOC_SYNPRETTY & n->flags)
1123 print_block(".HP 4n", MMAN_nl);
1124
1125 font_push('B');
1126 print_node(meta, n);
1127 font_pop();
1128 outflags &= ~MMAN_spc;
1129 print_word("(");
1130 outflags &= ~MMAN_spc;
1131
1132 n = n->next;
1133 if (NULL != n)
1134 pre_fa(meta, n);
1135 return(0);
1136 }
1137
1138 static void
1139 post_fn(DECL_ARGS)
1140 {
1141
1142 print_word(")");
1143 if (MDOC_SYNPRETTY & n->flags) {
1144 print_word(";");
1145 outflags |= MMAN_PP;
1146 }
1147 }
1148
1149 static int
1150 pre_fo(DECL_ARGS)
1151 {
1152
1153 switch (n->type) {
1154 case (MDOC_BLOCK):
1155 pre_syn(n);
1156 break;
1157 case (MDOC_HEAD):
1158 if (MDOC_SYNPRETTY & n->flags)
1159 print_block(".HP 4n", MMAN_nl);
1160 font_push('B');
1161 break;
1162 case (MDOC_BODY):
1163 outflags &= ~MMAN_spc;
1164 print_word("(");
1165 outflags &= ~MMAN_spc;
1166 break;
1167 default:
1168 break;
1169 }
1170 return(1);
1171 }
1172
1173 static void
1174 post_fo(DECL_ARGS)
1175 {
1176
1177 switch (n->type) {
1178 case (MDOC_HEAD):
1179 font_pop();
1180 break;
1181 case (MDOC_BODY):
1182 post_fn(meta, n);
1183 break;
1184 default:
1185 break;
1186 }
1187 }
1188
1189 static int
1190 pre_ft(DECL_ARGS)
1191 {
1192
1193 pre_syn(n);
1194 font_push('I');
1195 return(1);
1196 }
1197
1198 static int
1199 pre_in(DECL_ARGS)
1200 {
1201
1202 if (MDOC_SYNPRETTY & n->flags) {
1203 pre_syn(n);
1204 font_push('B');
1205 print_word("#include <");
1206 outflags &= ~MMAN_spc;
1207 } else {
1208 print_word("<");
1209 outflags &= ~MMAN_spc;
1210 font_push('I');
1211 }
1212 return(1);
1213 }
1214
1215 static void
1216 post_in(DECL_ARGS)
1217 {
1218
1219 if (MDOC_SYNPRETTY & n->flags) {
1220 outflags &= ~MMAN_spc;
1221 print_word(">");
1222 font_pop();
1223 outflags |= MMAN_br;
1224 } else {
1225 font_pop();
1226 outflags &= ~MMAN_spc;
1227 print_word(">");
1228 }
1229 }
1230
1231 static int
1232 pre_it(DECL_ARGS)
1233 {
1234 const struct mdoc_node *bln;
1235
1236 switch (n->type) {
1237 case (MDOC_HEAD):
1238 outflags |= MMAN_PP | MMAN_nl;
1239 bln = n->parent->parent;
1240 if (0 == bln->norm->Bl.comp ||
1241 (NULL == n->parent->prev &&
1242 NULL == bln->parent->prev))
1243 outflags |= MMAN_sp;
1244 outflags &= ~MMAN_br;
1245 switch (bln->norm->Bl.type) {
1246 case (LIST_item):
1247 return(0);
1248 case (LIST_inset):
1249 /* FALLTHROUGH */
1250 case (LIST_diag):
1251 /* FALLTHROUGH */
1252 case (LIST_ohang):
1253 if (bln->norm->Bl.type == LIST_diag)
1254 print_line(".B \"", 0);
1255 else
1256 print_line(".R \"", 0);
1257 outflags &= ~MMAN_spc;
1258 return(1);
1259 case (LIST_bullet):
1260 /* FALLTHROUGH */
1261 case (LIST_dash):
1262 /* FALLTHROUGH */
1263 case (LIST_hyphen):
1264 print_width(bln->norm->Bl.width, NULL, 0);
1265 TPremain = 0;
1266 outflags |= MMAN_nl;
1267 font_push('B');
1268 if (LIST_bullet == bln->norm->Bl.type)
1269 print_word("o");
1270 else
1271 print_word("-");
1272 font_pop();
1273 break;
1274 case (LIST_enum):
1275 print_width(bln->norm->Bl.width, NULL, 0);
1276 TPremain = 0;
1277 outflags |= MMAN_nl;
1278 print_count(&bln->norm->Bl.count);
1279 break;
1280 case (LIST_hang):
1281 print_width(bln->norm->Bl.width, n->child, 6);
1282 TPremain = 0;
1283 break;
1284 case (LIST_tag):
1285 print_width(bln->norm->Bl.width, n->child, 0);
1286 putchar('\n');
1287 outflags &= ~MMAN_spc;
1288 return(1);
1289 default:
1290 return(1);
1291 }
1292 outflags |= MMAN_nl;
1293 default:
1294 break;
1295 }
1296 return(1);
1297 }
1298
1299 /*
1300 * This function is called after closing out an indented block.
1301 * If we are inside an enclosing list, restore its indentation.
1302 */
1303 static void
1304 mid_it(void)
1305 {
1306 char buf[24];
1307
1308 /* Nothing to do outside a list. */
1309 if (0 == Bl_stack_len || 0 == Bl_stack[Bl_stack_len - 1])
1310 return;
1311
1312 /* The indentation has already been set up. */
1313 if (Bl_stack_post[Bl_stack_len - 1])
1314 return;
1315
1316 /* Restore the indentation of the enclosing list. */
1317 print_line(".RS", MMAN_Bk_susp);
1318 snprintf(buf, sizeof(buf), "%zun", Bl_stack[Bl_stack_len - 1]);
1319 print_word(buf);
1320
1321 /* Remeber to close out this .RS block later. */
1322 Bl_stack_post[Bl_stack_len - 1] = 1;
1323 }
1324
1325 static void
1326 post_it(DECL_ARGS)
1327 {
1328 const struct mdoc_node *bln;
1329
1330 bln = n->parent->parent;
1331
1332 switch (n->type) {
1333 case (MDOC_HEAD):
1334 switch (bln->norm->Bl.type) {
1335 case (LIST_diag):
1336 outflags &= ~MMAN_spc;
1337 print_word("\\ ");
1338 break;
1339 case (LIST_ohang):
1340 outflags |= MMAN_br;
1341 break;
1342 default:
1343 break;
1344 }
1345 break;
1346 case (MDOC_BODY):
1347 switch (bln->norm->Bl.type) {
1348 case (LIST_bullet):
1349 /* FALLTHROUGH */
1350 case (LIST_dash):
1351 /* FALLTHROUGH */
1352 case (LIST_hyphen):
1353 /* FALLTHROUGH */
1354 case (LIST_enum):
1355 /* FALLTHROUGH */
1356 case (LIST_hang):
1357 /* FALLTHROUGH */
1358 case (LIST_tag):
1359 assert(Bl_stack_len);
1360 Bl_stack[--Bl_stack_len] = 0;
1361
1362 /*
1363 * Our indentation had to be restored
1364 * after a child display or child list.
1365 * Close out that indentation block now.
1366 */
1367 if (Bl_stack_post[Bl_stack_len]) {
1368 print_line(".RE", MMAN_nl);
1369 Bl_stack_post[Bl_stack_len] = 0;
1370 }
1371 break;
1372 case (LIST_column):
1373 if (NULL != n->next) {
1374 putchar('\t');
1375 outflags &= ~MMAN_spc;
1376 }
1377 break;
1378 default:
1379 break;
1380 }
1381 break;
1382 default:
1383 break;
1384 }
1385 }
1386
1387 static void
1388 post_lb(DECL_ARGS)
1389 {
1390
1391 if (SEC_LIBRARY == n->sec)
1392 outflags |= MMAN_br;
1393 }
1394
1395 static int
1396 pre_lk(DECL_ARGS)
1397 {
1398 const struct mdoc_node *link, *descr;
1399
1400 if (NULL == (link = n->child))
1401 return(0);
1402
1403 if (NULL != (descr = link->next)) {
1404 font_push('I');
1405 while (NULL != descr) {
1406 print_word(descr->string);
1407 descr = descr->next;
1408 }
1409 print_word(":");
1410 font_pop();
1411 }
1412
1413 font_push('B');
1414 print_word(link->string);
1415 font_pop();
1416 return(0);
1417 }
1418
1419 static int
1420 pre_ll(DECL_ARGS)
1421 {
1422
1423 print_line(".ll", 0);
1424 return(1);
1425 }
1426
1427 static int
1428 pre_li(DECL_ARGS)
1429 {
1430
1431 font_push('R');
1432 return(1);
1433 }
1434
1435 static int
1436 pre_nm(DECL_ARGS)
1437 {
1438 char *name;
1439
1440 if (MDOC_BLOCK == n->type) {
1441 outflags |= MMAN_Bk;
1442 pre_syn(n);
1443 }
1444 if (MDOC_ELEM != n->type && MDOC_HEAD != n->type)
1445 return(1);
1446 name = n->child ? n->child->string : meta->name;
1447 if (NULL == name)
1448 return(0);
1449 if (MDOC_HEAD == n->type) {
1450 if (NULL == n->parent->prev)
1451 outflags |= MMAN_sp;
1452 print_block(".HP", 0);
1453 printf(" %zun", strlen(name) + 1);
1454 outflags |= MMAN_nl;
1455 }
1456 font_push('B');
1457 if (NULL == n->child)
1458 print_word(meta->name);
1459 return(1);
1460 }
1461
1462 static void
1463 post_nm(DECL_ARGS)
1464 {
1465
1466 switch (n->type) {
1467 case (MDOC_BLOCK):
1468 outflags &= ~MMAN_Bk;
1469 break;
1470 case (MDOC_HEAD):
1471 /* FALLTHROUGH */
1472 case (MDOC_ELEM):
1473 font_pop();
1474 break;
1475 default:
1476 break;
1477 }
1478 }
1479
1480 static int
1481 pre_no(DECL_ARGS)
1482 {
1483
1484 outflags |= MMAN_spc_force;
1485 return(1);
1486 }
1487
1488 static int
1489 pre_ns(DECL_ARGS)
1490 {
1491
1492 outflags &= ~MMAN_spc;
1493 return(0);
1494 }
1495
1496 static void
1497 post_pf(DECL_ARGS)
1498 {
1499
1500 outflags &= ~MMAN_spc;
1501 }
1502
1503 static int
1504 pre_pp(DECL_ARGS)
1505 {
1506
1507 if (MDOC_It != n->parent->tok)
1508 outflags |= MMAN_PP;
1509 outflags |= MMAN_sp | MMAN_nl;
1510 outflags &= ~MMAN_br;
1511 return(0);
1512 }
1513
1514 static int
1515 pre_rs(DECL_ARGS)
1516 {
1517
1518 if (SEC_SEE_ALSO == n->sec) {
1519 outflags |= MMAN_PP | MMAN_sp | MMAN_nl;
1520 outflags &= ~MMAN_br;
1521 }
1522 return(1);
1523 }
1524
1525 static int
1526 pre_sm(DECL_ARGS)
1527 {
1528
1529 assert(n->child && MDOC_TEXT == n->child->type);
1530 if (0 == strcmp("on", n->child->string))
1531 outflags |= MMAN_Sm | MMAN_spc;
1532 else
1533 outflags &= ~MMAN_Sm;
1534 return(0);
1535 }
1536
1537 static int
1538 pre_sp(DECL_ARGS)
1539 {
1540
1541 if (MMAN_PP & outflags) {
1542 outflags &= ~MMAN_PP;
1543 print_line(".PP", 0);
1544 } else
1545 print_line(".sp", 0);
1546 return(1);
1547 }
1548
1549 static void
1550 post_sp(DECL_ARGS)
1551 {
1552
1553 outflags |= MMAN_nl;
1554 }
1555
1556 static int
1557 pre_sy(DECL_ARGS)
1558 {
1559
1560 font_push('B');
1561 return(1);
1562 }
1563
1564 static int
1565 pre_vt(DECL_ARGS)
1566 {
1567
1568 if (MDOC_SYNPRETTY & n->flags) {
1569 switch (n->type) {
1570 case (MDOC_BLOCK):
1571 pre_syn(n);
1572 return(1);
1573 case (MDOC_BODY):
1574 break;
1575 default:
1576 return(0);
1577 }
1578 }
1579 font_push('I');
1580 return(1);
1581 }
1582
1583 static void
1584 post_vt(DECL_ARGS)
1585 {
1586
1587 if (MDOC_SYNPRETTY & n->flags && MDOC_BODY != n->type)
1588 return;
1589 font_pop();
1590 }
1591
1592 static int
1593 pre_xr(DECL_ARGS)
1594 {
1595
1596 n = n->child;
1597 if (NULL == n)
1598 return(0);
1599 print_node(meta, n);
1600 n = n->next;
1601 if (NULL == n)
1602 return(0);
1603 outflags &= ~MMAN_spc;
1604 print_word("(");
1605 print_node(meta, n);
1606 print_word(")");
1607 return(0);
1608 }
1609
1610 static int
1611 pre_ux(DECL_ARGS)
1612 {
1613
1614 print_word(manacts[n->tok].prefix);
1615 if (NULL == n->child)
1616 return(0);
1617 outflags &= ~MMAN_spc;
1618 print_word("\\ ");
1619 outflags &= ~MMAN_spc;
1620 return(1);
1621 }