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