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