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