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