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