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