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