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