]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_man.c
Basic implementation of -Tman .Fo and .Fa;
[mandoc.git] / mdoc_man.c
1 /* $Id: mdoc_man.c,v 1.17 2012/07/08 10:19:37 schwarze Exp $ */
2 /*
3 * Copyright (c) 2011, 2012 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 "out.h"
27 #include "man.h"
28 #include "mdoc.h"
29 #include "main.h"
30
31 #define DECL_ARGS const struct mdoc_meta *m, \
32 const struct mdoc_node *n, \
33 struct mman *mm
34
35 struct mman {
36 int mode_space; /* spacing mode: 1 = on */
37 int need_space; /* next word needs prior ws */
38 int mode_keep; /* currently inside a keep */
39 int need_nl; /* next word needs prior nl */
40 };
41
42 struct manact {
43 int (*cond)(DECL_ARGS); /* DON'T run actions */
44 int (*pre)(DECL_ARGS); /* pre-node action */
45 void (*post)(DECL_ARGS); /* post-node action */
46 const char *prefix; /* pre-node string constant */
47 const char *suffix; /* post-node string constant */
48 };
49
50 static int cond_body(DECL_ARGS);
51 static int cond_head(DECL_ARGS);
52 static void post_bd(DECL_ARGS);
53 static void post_bk(DECL_ARGS);
54 static void post_dl(DECL_ARGS);
55 static void post_enc(DECL_ARGS);
56 static void post_fa(DECL_ARGS);
57 static void post_fn(DECL_ARGS);
58 static void post_fo(DECL_ARGS);
59 static void post_in(DECL_ARGS);
60 static void post_lb(DECL_ARGS);
61 static void post_nm(DECL_ARGS);
62 static void post_percent(DECL_ARGS);
63 static void post_pf(DECL_ARGS);
64 static void post_sect(DECL_ARGS);
65 static void post_sp(DECL_ARGS);
66 static int pre_ap(DECL_ARGS);
67 static int pre_bd(DECL_ARGS);
68 static int pre_bk(DECL_ARGS);
69 static int pre_br(DECL_ARGS);
70 static int pre_bx(DECL_ARGS);
71 static int pre_dl(DECL_ARGS);
72 static int pre_enc(DECL_ARGS);
73 static int pre_fa(DECL_ARGS);
74 static int pre_fn(DECL_ARGS);
75 static int pre_fo(DECL_ARGS);
76 static int pre_in(DECL_ARGS);
77 static int pre_it(DECL_ARGS);
78 static int pre_nm(DECL_ARGS);
79 static int pre_ns(DECL_ARGS);
80 static int pre_pp(DECL_ARGS);
81 static int pre_sm(DECL_ARGS);
82 static int pre_sp(DECL_ARGS);
83 static int pre_sect(DECL_ARGS);
84 static int pre_ux(DECL_ARGS);
85 static int pre_xr(DECL_ARGS);
86 static void print_word(struct mman *, const char *);
87 static void print_offs(struct mman *, const char *);
88 static void print_node(DECL_ARGS);
89
90 static const struct manact manacts[MDOC_MAX + 1] = {
91 { NULL, pre_ap, NULL, NULL, NULL }, /* Ap */
92 { NULL, NULL, NULL, NULL, NULL }, /* Dd */
93 { NULL, NULL, NULL, NULL, NULL }, /* Dt */
94 { NULL, NULL, NULL, NULL, NULL }, /* Os */
95 { NULL, pre_sect, post_sect, ".SH", NULL }, /* Sh */
96 { NULL, pre_sect, post_sect, ".SS", NULL }, /* Ss */
97 { NULL, pre_pp, NULL, NULL, NULL }, /* Pp */
98 { cond_body, pre_dl, post_dl, NULL, NULL }, /* D1 */
99 { cond_body, pre_dl, post_dl, NULL, NULL }, /* Dl */
100 { cond_body, pre_bd, post_bd, NULL, NULL }, /* Bd */
101 { NULL, NULL, NULL, NULL, NULL }, /* Ed */
102 { NULL, NULL, NULL, NULL, NULL }, /* Bl */
103 { NULL, NULL, NULL, NULL, NULL }, /* El */
104 { NULL, pre_it, NULL, NULL, NULL }, /* _It */
105 { NULL, pre_enc, post_enc, "\\fI", "\\fP" }, /* Ad */
106 { NULL, NULL, NULL, NULL, NULL }, /* _An */
107 { NULL, pre_enc, post_enc, "\\fI", "\\fP" }, /* Ar */
108 { NULL, pre_enc, post_enc, "\\fB", "\\fP" }, /* Cd */
109 { NULL, pre_enc, post_enc, "\\fB", "\\fP" }, /* Cm */
110 { NULL, pre_enc, post_enc, "\\fR", "\\fP" }, /* Dv */
111 { NULL, pre_enc, post_enc, "\\fR", "\\fP" }, /* Er */
112 { NULL, pre_enc, post_enc, "\\fR", "\\fP" }, /* Ev */
113 { NULL, pre_enc, post_enc, "The \\fB",
114 "\\fP\nutility exits 0 on success, and >0 if an error occurs."
115 }, /* Ex */
116 { NULL, pre_fa, post_fa, NULL, NULL }, /* Fa */
117 { NULL, NULL, NULL, NULL, NULL }, /* _Fd */
118 { NULL, pre_enc, post_enc, "\\fB-", "\\fP" }, /* Fl */
119 { NULL, pre_fn, post_fn, NULL, NULL }, /* Fn */
120 { NULL, pre_enc, post_enc, "\\fI", "\\fP" }, /* Ft */
121 { NULL, pre_enc, post_enc, "\\fB", "\\fP" }, /* Ic */
122 { NULL, pre_in, post_in, NULL, NULL }, /* In */
123 { NULL, pre_enc, post_enc, "\\fR", "\\fP" }, /* Li */
124 { cond_head, pre_enc, NULL, "\\- ", NULL }, /* Nd */
125 { NULL, pre_nm, post_nm, NULL, NULL }, /* Nm */
126 { cond_body, pre_enc, post_enc, "[", "]" }, /* Op */
127 { NULL, NULL, NULL, NULL, NULL }, /* Ot */
128 { NULL, pre_enc, post_enc, "\\fI", "\\fP" }, /* Pa */
129 { NULL, pre_enc, post_enc, "The \\fB",
130 "\\fP\nfunction returns the value 0 if successful;\n"
131 "otherwise the value -1 is returned and the global\n"
132 "variable \\fIerrno\\fP is set to indicate the error."
133 }, /* Rv */
134 { NULL, NULL, NULL, NULL, NULL }, /* St */
135 { NULL, NULL, NULL, NULL, NULL }, /* _Va */
136 { NULL, NULL, NULL, NULL, NULL }, /* _Vt */
137 { NULL, pre_xr, NULL, NULL, NULL }, /* Xr */
138 { NULL, NULL, post_percent, NULL, NULL }, /* _%A */
139 { NULL, NULL, NULL, NULL, NULL }, /* _%B */
140 { NULL, NULL, post_percent, NULL, NULL }, /* _%D */
141 { NULL, NULL, NULL, NULL, NULL }, /* _%I */
142 { NULL, pre_enc, post_percent, "\\fI", "\\fP" }, /* %J */
143 { NULL, NULL, NULL, NULL, NULL }, /* _%N */
144 { NULL, NULL, NULL, NULL, NULL }, /* _%O */
145 { NULL, NULL, NULL, NULL, NULL }, /* _%P */
146 { NULL, NULL, NULL, NULL, NULL }, /* _%R */
147 { NULL, pre_enc, post_percent, "\"", "\"" }, /* %T */
148 { NULL, NULL, NULL, NULL, NULL }, /* _%V */
149 { NULL, NULL, NULL, NULL, NULL }, /* Ac */
150 { cond_body, pre_enc, post_enc, "<", ">" }, /* Ao */
151 { cond_body, pre_enc, post_enc, "<", ">" }, /* Aq */
152 { NULL, NULL, NULL, NULL, NULL }, /* At */
153 { NULL, NULL, NULL, NULL, NULL }, /* Bc */
154 { NULL, NULL, NULL, NULL, NULL }, /* _Bf */
155 { cond_body, pre_enc, post_enc, "[", "]" }, /* Bo */
156 { cond_body, pre_enc, post_enc, "[", "]" }, /* Bq */
157 { NULL, pre_ux, NULL, "BSD/OS", NULL }, /* Bsx */
158 { NULL, pre_bx, NULL, NULL, NULL }, /* Bx */
159 { NULL, NULL, NULL, NULL, NULL }, /* Db */
160 { NULL, NULL, NULL, NULL, NULL }, /* Dc */
161 { cond_body, pre_enc, post_enc, "``", "''" }, /* Do */
162 { cond_body, pre_enc, post_enc, "``", "''" }, /* Dq */
163 { NULL, NULL, NULL, NULL, NULL }, /* _Ec */
164 { NULL, NULL, NULL, NULL, NULL }, /* _Ef */
165 { NULL, pre_enc, post_enc, "\\fI", "\\fP" }, /* Em */
166 { NULL, NULL, NULL, NULL, NULL }, /* _Eo */
167 { NULL, pre_ux, NULL, "FreeBSD", NULL }, /* Fx */
168 { NULL, pre_enc, post_enc, "\\fB", "\\fP" }, /* Ms */
169 { NULL, NULL, NULL, NULL, NULL }, /* No */
170 { NULL, pre_ns, NULL, NULL, NULL }, /* Ns */
171 { NULL, pre_ux, NULL, "NetBSD", NULL }, /* Nx */
172 { NULL, pre_ux, NULL, "OpenBSD", NULL }, /* Ox */
173 { NULL, NULL, NULL, NULL, NULL }, /* Pc */
174 { NULL, NULL, post_pf, NULL, NULL }, /* Pf */
175 { cond_body, pre_enc, post_enc, "(", ")" }, /* Po */
176 { cond_body, pre_enc, post_enc, "(", ")" }, /* Pq */
177 { NULL, NULL, NULL, NULL, NULL }, /* Qc */
178 { cond_body, pre_enc, post_enc, "`", "'" }, /* Ql */
179 { cond_body, pre_enc, post_enc, "\"", "\"" }, /* Qo */
180 { cond_body, pre_enc, post_enc, "\"", "\"" }, /* Qq */
181 { NULL, NULL, NULL, NULL, NULL }, /* Re */
182 { cond_body, pre_pp, NULL, NULL, NULL }, /* Rs */
183 { NULL, NULL, NULL, NULL, NULL }, /* Sc */
184 { cond_body, pre_enc, post_enc, "`", "'" }, /* So */
185 { cond_body, pre_enc, post_enc, "`", "'" }, /* Sq */
186 { NULL, pre_sm, NULL, NULL, NULL }, /* Sm */
187 { NULL, pre_enc, post_enc, "\\fI", "\\fP" }, /* Sx */
188 { NULL, pre_enc, post_enc, "\\fB", "\\fP" }, /* Sy */
189 { NULL, pre_enc, post_enc, "\\fR", "\\fP" }, /* Tn */
190 { NULL, pre_ux, NULL, "UNIX", NULL }, /* Ux */
191 { NULL, NULL, NULL, NULL, NULL }, /* _Xc */
192 { NULL, NULL, NULL, NULL, NULL }, /* _Xo */
193 { NULL, pre_fo, post_fo, NULL, NULL }, /* Fo */
194 { NULL, NULL, NULL, NULL, NULL }, /* Fc */
195 { cond_body, pre_enc, post_enc, "[", "]" }, /* Oo */
196 { NULL, NULL, NULL, NULL, NULL }, /* Oc */
197 { NULL, pre_bk, post_bk, NULL, NULL }, /* Bk */
198 { NULL, NULL, NULL, NULL, NULL }, /* Ek */
199 { NULL, pre_ux, NULL, "is currently in beta test.", NULL }, /* Bt */
200 { NULL, NULL, NULL, NULL, NULL }, /* Hf */
201 { NULL, NULL, NULL, NULL, NULL }, /* Fr */
202 { NULL, pre_ux, NULL, "currently under development.", NULL }, /* Ud */
203 { NULL, NULL, post_lb, NULL, NULL }, /* Lb */
204 { NULL, pre_pp, NULL, NULL, NULL }, /* Lp */
205 { NULL, NULL, NULL, NULL, NULL }, /* _Lk */
206 { NULL, NULL, NULL, NULL, NULL }, /* _Mt */
207 { cond_body, pre_enc, post_enc, "{", "}" }, /* Brq */
208 { cond_body, pre_enc, post_enc, "{", "}" }, /* Bro */
209 { NULL, NULL, NULL, NULL, NULL }, /* Brc */
210 { NULL, NULL, NULL, NULL, NULL }, /* _%C */
211 { NULL, NULL, NULL, NULL, NULL }, /* _Es */
212 { NULL, NULL, NULL, NULL, NULL }, /* _En */
213 { NULL, pre_ux, NULL, "DragonFly", NULL }, /* Dx */
214 { NULL, NULL, NULL, NULL, NULL }, /* _%Q */
215 { NULL, pre_br, NULL, NULL, NULL }, /* br */
216 { NULL, pre_sp, post_sp, NULL, NULL }, /* sp */
217 { NULL, NULL, NULL, NULL, NULL }, /* _%U */
218 { NULL, NULL, NULL, NULL, NULL }, /* _Ta */
219 { NULL, NULL, NULL, NULL, NULL }, /* ROOT */
220 };
221
222 static void
223 print_word(struct mman *mm, const char *s)
224 {
225
226 if (mm->need_nl) {
227 /*
228 * If we need a newline, print it now and start afresh.
229 */
230 putchar('\n');
231 mm->need_space = 0;
232 mm->need_nl = 0;
233 } else if (mm->need_space && '\0' != s[0])
234 /*
235 * If we need a space, only print it before
236 * (1) a nonzero length word;
237 * (2) a word that is non-punctuation; and
238 * (3) if punctuation, non-terminating puncutation.
239 */
240 if (NULL == strchr(".,:;)]?!", s[0]) || '\0' != s[1]) {
241 if (mm->mode_keep) {
242 putchar('\\');
243 putchar('~');
244 } else
245 putchar(' ');
246 }
247
248 /*
249 * Reassign needing space if we're not following opening
250 * punctuation.
251 */
252 mm->need_space = mm->mode_space &&
253 (('(' != s[0] && '[' != s[0]) || '\0' != s[1]);
254
255 for ( ; *s; s++) {
256 switch (*s) {
257 case (ASCII_NBRSP):
258 printf("\\~");
259 break;
260 case (ASCII_HYPH):
261 putchar('-');
262 break;
263 default:
264 putchar((unsigned char)*s);
265 break;
266 }
267 }
268 }
269
270 static void
271 print_offs(struct mman *mm, const char *v)
272 {
273 char buf[24];
274 struct roffsu su;
275 size_t sz;
276
277 if (NULL == v || '\0' == *v || 0 == strcmp(v, "left"))
278 sz = 0;
279 else if (0 == strcmp(v, "indent"))
280 sz = 6;
281 else if (0 == strcmp(v, "indent-two"))
282 sz = 12;
283 else if (a2roffsu(v, &su, SCALE_MAX)) {
284 print_word(mm, v);
285 return;
286 } else
287 sz = strlen(v);
288
289 snprintf(buf, sizeof(buf), "%ldn", sz);
290 print_word(mm, buf);
291 }
292
293 void
294 man_man(void *arg, const struct man *man)
295 {
296
297 /*
298 * Dump the keep buffer.
299 * We're guaranteed by now that this exists (is non-NULL).
300 * Flush stdout afterward, just in case.
301 */
302 fputs(mparse_getkeep(man_mparse(man)), stdout);
303 fflush(stdout);
304 }
305
306 void
307 man_mdoc(void *arg, const struct mdoc *mdoc)
308 {
309 const struct mdoc_meta *m;
310 const struct mdoc_node *n;
311 struct mman mm;
312
313 m = mdoc_meta(mdoc);
314 n = mdoc_node(mdoc);
315
316 printf(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"",
317 m->title, m->msec, m->date, m->os, m->vol);
318
319 memset(&mm, 0, sizeof(struct mman));
320
321 mm.mode_space = 1;
322 mm.need_nl = 1;
323 print_node(m, n, &mm);
324 putchar('\n');
325 }
326
327 static void
328 print_node(DECL_ARGS)
329 {
330 const struct mdoc_node *prev, *sub;
331 const struct manact *act;
332 int cond, do_sub;
333
334 /*
335 * Break the line if we were parsed subsequent the current node.
336 * This makes the page structure be more consistent.
337 */
338 prev = n->prev ? n->prev : n->parent;
339 if (prev && prev->line < n->line &&
340 MDOC_Fo != prev->tok && MDOC_Ns != prev->tok)
341 mm->need_nl = 1;
342
343 act = NULL;
344 cond = 0;
345 do_sub = 1;
346
347 if (MDOC_TEXT == n->type) {
348 /*
349 * Make sure that we don't happen to start with a
350 * control character at the start of a line.
351 */
352 if (mm->need_nl && ('.' == *n->string ||
353 '\'' == *n->string)) {
354 print_word(mm, "\\&");
355 mm->need_space = 0;
356 }
357 print_word(mm, n->string);
358 } else {
359 /*
360 * Conditionally run the pre-node action handler for a
361 * node.
362 */
363 act = manacts + n->tok;
364 cond = NULL == act->cond || (*act->cond)(m, n, mm);
365 if (cond && act->pre)
366 do_sub = (*act->pre)(m, n, mm);
367 }
368
369 /*
370 * Conditionally run all child nodes.
371 * Note that this iterates over children instead of using
372 * recursion. This prevents unnecessary depth in the stack.
373 */
374 if (do_sub)
375 for (sub = n->child; sub; sub = sub->next)
376 print_node(m, sub, mm);
377
378 /*
379 * Lastly, conditionally run the post-node handler.
380 */
381 if (cond && act->post)
382 (*act->post)(m, n, mm);
383 }
384
385 static int
386 cond_head(DECL_ARGS)
387 {
388
389 return(MDOC_HEAD == n->type);
390 }
391
392 static int
393 cond_body(DECL_ARGS)
394 {
395
396 return(MDOC_BODY == n->type);
397 }
398
399 /*
400 * Output a font encoding before a node, e.g., \fR.
401 * This obviously has no trailing space.
402 */
403 static int
404 pre_enc(DECL_ARGS)
405 {
406 const char *prefix;
407
408 prefix = manacts[n->tok].prefix;
409 if (NULL == prefix)
410 return(1);
411 print_word(mm, prefix);
412 mm->need_space = 0;
413 return(1);
414 }
415
416 /*
417 * Output a font encoding subsequent a node, e.g., \fP.
418 */
419 static void
420 post_enc(DECL_ARGS)
421 {
422 const char *suffix;
423
424 suffix = manacts[n->tok].suffix;
425 if (NULL == suffix)
426 return;
427 mm->need_space = 0;
428 print_word(mm, suffix);
429 if (MDOC_Fl == n->tok && 0 == n->nchild)
430 mm->need_space = 0;
431 }
432
433 /*
434 * Used in listings (percent = %A, e.g.).
435 * FIXME: this is incomplete.
436 * It doesn't print a nice ", and" for lists.
437 */
438 static void
439 post_percent(DECL_ARGS)
440 {
441
442 post_enc(m, n, mm);
443 if (n->next)
444 print_word(mm, ",");
445 else {
446 print_word(mm, ".");
447 mm->need_nl = 1;
448 }
449 }
450
451 /*
452 * Print before a section header.
453 */
454 static int
455 pre_sect(DECL_ARGS)
456 {
457
458 if (MDOC_HEAD != n->type)
459 return(1);
460 mm->need_nl = 1;
461 print_word(mm, manacts[n->tok].prefix);
462 print_word(mm, "\"");
463 mm->need_space = 0;
464 return(1);
465 }
466
467 /*
468 * Print subsequent a section header.
469 */
470 static void
471 post_sect(DECL_ARGS)
472 {
473
474 if (MDOC_HEAD != n->type)
475 return;
476 mm->need_space = 0;
477 print_word(mm, "\"");
478 mm->need_nl = 1;
479 }
480
481 static int
482 pre_ap(DECL_ARGS)
483 {
484
485 mm->need_space = 0;
486 print_word(mm, "'");
487 mm->need_space = 0;
488 return(0);
489 }
490
491 static int
492 pre_bd(DECL_ARGS)
493 {
494
495 if (0 == n->norm->Bd.comp) {
496 mm->need_nl = 1;
497 print_word(mm, ".sp");
498 }
499 if (DISP_unfilled == n->norm->Bd.type ||
500 DISP_literal == n->norm->Bd.type) {
501 mm->need_nl = 1;
502 print_word(mm, ".nf");
503 }
504 mm->need_nl = 1;
505 print_word(mm, ".RS");
506 print_offs(mm, n->norm->Bd.offs);
507 mm->need_nl = 1;
508 return(1);
509 }
510
511 static void
512 post_bd(DECL_ARGS)
513 {
514
515 mm->need_nl = 1;
516 print_word(mm, ".RE");
517 if (DISP_unfilled == n->norm->Bd.type ||
518 DISP_literal == n->norm->Bd.type) {
519 mm->need_nl = 1;
520 print_word(mm, ".fi");
521 }
522 mm->need_nl = 1;
523 }
524
525 static int
526 pre_bk(DECL_ARGS)
527 {
528
529 switch (n->type) {
530 case (MDOC_BLOCK):
531 return(1);
532 case (MDOC_BODY):
533 mm->mode_keep = 1;
534 return(1);
535 default:
536 return(0);
537 }
538 }
539
540 static void
541 post_bk(DECL_ARGS)
542 {
543
544 if (MDOC_BODY == n->type)
545 mm->mode_keep = 0;
546 }
547
548 static int
549 pre_br(DECL_ARGS)
550 {
551
552 mm->need_nl = 1;
553 print_word(mm, ".br");
554 mm->need_nl = 1;
555 return(0);
556 }
557
558 static int
559 pre_bx(DECL_ARGS)
560 {
561
562 n = n->child;
563 if (n) {
564 print_word(mm, n->string);
565 mm->need_space = 0;
566 n = n->next;
567 }
568 print_word(mm, "BSD");
569 if (NULL == n)
570 return(0);
571 mm->need_space = 0;
572 print_word(mm, "-");
573 mm->need_space = 0;
574 print_word(mm, n->string);
575 return(0);
576 }
577
578 static int
579 pre_dl(DECL_ARGS)
580 {
581
582 mm->need_nl = 1;
583 print_word(mm, ".RS 6n");
584 mm->need_nl = 1;
585 return(1);
586 }
587
588 static void
589 post_dl(DECL_ARGS)
590 {
591
592 mm->need_nl = 1;
593 print_word(mm, ".RE");
594 mm->need_nl = 1;
595 }
596
597 static int
598 pre_fa(DECL_ARGS)
599 {
600
601 if (MDOC_Fa == n->tok)
602 n = n->child;
603
604 while (NULL != n) {
605 print_word(mm, "\\fI");
606 mm->need_space = 0;
607 print_node(m, n, mm);
608 mm->need_space = 0;
609 print_word(mm, "\\fP");
610 if (NULL != (n = n->next))
611 print_word(mm, ",");
612 }
613 return(0);
614 }
615
616 static void
617 post_fa(DECL_ARGS)
618 {
619
620 if (NULL != n->next && MDOC_Fa == n->next->tok)
621 print_word(mm, ",");
622 }
623
624 static int
625 pre_fn(DECL_ARGS)
626 {
627
628 n = n->child;
629 if (NULL == n)
630 return(0);
631
632 if (MDOC_SYNPRETTY & n->flags) {
633 mm->need_nl = 1;
634 print_word(mm, ".br");
635 mm->need_nl = 1;
636 }
637 print_word(mm, "\\fB");
638 mm->need_space = 0;
639 print_node(m, n, mm);
640 mm->need_space = 0;
641 print_word(mm, "\\fP(");
642 mm->need_space = 0;
643 return(pre_fa(m, n->next, mm));
644 }
645
646 static void
647 post_fn(DECL_ARGS)
648 {
649
650 print_word(mm, ")");
651 if (MDOC_SYNPRETTY & n->flags) {
652 print_word(mm, ";");
653 mm->need_nl = 1;
654 print_word(mm, ".br");
655 mm->need_nl = 1;
656 }
657 }
658
659 static int
660 pre_fo(DECL_ARGS)
661 {
662
663 switch (n->type) {
664 case (MDOC_HEAD):
665 if (MDOC_SYNPRETTY & n->flags) {
666 mm->need_nl = 1;
667 print_word(mm, ".br");
668 mm->need_nl = 1;
669 }
670 print_word(mm, "\\fB");
671 mm->need_space = 0;
672 break;
673 case (MDOC_BODY):
674 mm->need_space = 0;
675 print_word(mm, "(");
676 mm->need_space = 0;
677 break;
678 default:
679 break;
680 }
681 return(1);
682 }
683
684 static void
685 post_fo(DECL_ARGS)
686 {
687
688 switch (n->type) {
689 case (MDOC_HEAD):
690 mm->need_space = 0;
691 print_word(mm, "\\fP");
692 break;
693 case (MDOC_BODY):
694 post_fn(m, n, mm);
695 break;
696 default:
697 break;
698 }
699 }
700
701 static int
702 pre_in(DECL_ARGS)
703 {
704
705 if (MDOC_SYNPRETTY & n->flags) {
706 mm->need_nl = 1;
707 print_word(mm, ".br");
708 mm->need_nl = 1;
709 print_word(mm, "\\fB#include <");
710 } else
711 print_word(mm, "<\\fI");
712 mm->need_space = 0;
713 return(1);
714 }
715
716 static void
717 post_in(DECL_ARGS)
718 {
719
720 mm->need_space = 0;
721 if (MDOC_SYNPRETTY & n->flags) {
722 print_word(mm, ">\\fP");
723 mm->need_nl = 1;
724 print_word(mm, ".br");
725 mm->need_nl = 1;
726 } else
727 print_word(mm, "\\fP>");
728 }
729
730 static int
731 pre_it(DECL_ARGS)
732 {
733 const struct mdoc_node *bln;
734
735 if (MDOC_HEAD == n->type) {
736 mm->need_nl = 1;
737 print_word(mm, ".TP");
738 bln = n->parent->parent->prev;
739 switch (bln->norm->Bl.type) {
740 case (LIST_bullet):
741 print_word(mm, "4n");
742 mm->need_nl = 1;
743 print_word(mm, "\\fBo\\fP");
744 break;
745 default:
746 if (bln->norm->Bl.width)
747 print_word(mm, bln->norm->Bl.width);
748 break;
749 }
750 mm->need_nl = 1;
751 }
752 return(1);
753 }
754
755 static void
756 post_lb(DECL_ARGS)
757 {
758
759 if (SEC_LIBRARY == n->sec) {
760 mm->need_nl = 1;
761 print_word(mm, ".br");
762 mm->need_nl = 1;
763 }
764 }
765
766 static int
767 pre_nm(DECL_ARGS)
768 {
769
770 if (MDOC_ELEM != n->type && MDOC_HEAD != n->type)
771 return(1);
772 if (MDOC_SYNPRETTY & n->flags) {
773 mm->need_nl = 1;
774 print_word(mm, ".br");
775 mm->need_nl = 1;
776 }
777 print_word(mm, "\\fB");
778 mm->need_space = 0;
779 if (NULL == n->child)
780 print_word(mm, m->name);
781 return(1);
782 }
783
784 static void
785 post_nm(DECL_ARGS)
786 {
787
788 if (MDOC_ELEM != n->type && MDOC_HEAD != n->type)
789 return;
790 mm->need_space = 0;
791 print_word(mm, "\\fP");
792 }
793
794 static int
795 pre_ns(DECL_ARGS)
796 {
797
798 mm->need_space = 0;
799 return(0);
800 }
801
802 static void
803 post_pf(DECL_ARGS)
804 {
805
806 mm->need_space = 0;
807 }
808
809 static int
810 pre_pp(DECL_ARGS)
811 {
812
813 mm->need_nl = 1;
814 if (MDOC_It == n->parent->tok)
815 print_word(mm, ".sp");
816 else
817 print_word(mm, ".PP");
818 mm->need_nl = 1;
819 return(MDOC_Rs == n->tok);
820 }
821
822 static int
823 pre_sm(DECL_ARGS)
824 {
825
826 assert(n->child && MDOC_TEXT == n->child->type);
827 if (0 == strcmp("on", n->child->string))
828 mm->mode_space = 1;
829 else
830 mm->mode_space = 0;
831 return(0);
832 }
833
834 static int
835 pre_sp(DECL_ARGS)
836 {
837
838 mm->need_nl = 1;
839 print_word(mm, ".sp");
840 return(1);
841 }
842
843 static void
844 post_sp(DECL_ARGS)
845 {
846
847 mm->need_nl = 1;
848 }
849
850 static int
851 pre_xr(DECL_ARGS)
852 {
853
854 n = n->child;
855 if (NULL == n)
856 return(0);
857 print_node(m, n, mm);
858 n = n->next;
859 if (NULL == n)
860 return(0);
861 mm->need_space = 0;
862 print_word(mm, "(");
863 print_node(m, n, mm);
864 print_word(mm, ")");
865 return(0);
866 }
867
868 static int
869 pre_ux(DECL_ARGS)
870 {
871
872 print_word(mm, manacts[n->tok].prefix);
873 if (NULL == n->child)
874 return(0);
875 mm->need_space = 0;
876 print_word(mm, "\\~");
877 mm->need_space = 0;
878 return(1);
879 }