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