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