xref: /openbsd-src/usr.bin/mandoc/man_term.c (revision d1df930ffab53da22f3324c32bed7ac5709915e6)
1 /*	$OpenBSD: man_term.c,v 1.173 2018/08/26 16:18:38 schwarze Exp $ */
2 /*
3  * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4  * Copyright (c) 2010-2015, 2017, 2018 Ingo Schwarze <schwarze@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 #include <sys/types.h>
19 
20 #include <assert.h>
21 #include <ctype.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include "mandoc_aux.h"
28 #include "mandoc.h"
29 #include "roff.h"
30 #include "man.h"
31 #include "out.h"
32 #include "term.h"
33 #include "main.h"
34 
35 #define	MAXMARGINS	  64 /* maximum number of indented scopes */
36 
37 struct	mtermp {
38 	int		  fl;
39 #define	MANT_LITERAL	 (1 << 0)
40 	int		  lmargin[MAXMARGINS]; /* margins (incl. vis. page) */
41 	int		  lmargincur; /* index of current margin */
42 	int		  lmarginsz; /* actual number of nested margins */
43 	size_t		  offset; /* default offset to visible page */
44 	int		  pardist; /* vert. space before par., unit: [v] */
45 };
46 
47 #define	DECL_ARGS	  struct termp *p, \
48 			  struct mtermp *mt, \
49 			  struct roff_node *n, \
50 			  const struct roff_meta *meta
51 
52 struct	man_term_act {
53 	int		(*pre)(DECL_ARGS);
54 	void		(*post)(DECL_ARGS);
55 	int		  flags;
56 #define	MAN_NOTEXT	 (1 << 0) /* Never has text children. */
57 };
58 
59 static	void		  print_man_nodelist(DECL_ARGS);
60 static	void		  print_man_node(DECL_ARGS);
61 static	void		  print_man_head(struct termp *,
62 				const struct roff_meta *);
63 static	void		  print_man_foot(struct termp *,
64 				const struct roff_meta *);
65 static	void		  print_bvspace(struct termp *,
66 				const struct roff_node *, int);
67 
68 static	int		  pre_B(DECL_ARGS);
69 static	int		  pre_DT(DECL_ARGS);
70 static	int		  pre_HP(DECL_ARGS);
71 static	int		  pre_I(DECL_ARGS);
72 static	int		  pre_IP(DECL_ARGS);
73 static	int		  pre_OP(DECL_ARGS);
74 static	int		  pre_PD(DECL_ARGS);
75 static	int		  pre_PP(DECL_ARGS);
76 static	int		  pre_RS(DECL_ARGS);
77 static	int		  pre_SH(DECL_ARGS);
78 static	int		  pre_SS(DECL_ARGS);
79 static	int		  pre_SY(DECL_ARGS);
80 static	int		  pre_TP(DECL_ARGS);
81 static	int		  pre_UR(DECL_ARGS);
82 static	int		  pre_alternate(DECL_ARGS);
83 static	int		  pre_ign(DECL_ARGS);
84 static	int		  pre_in(DECL_ARGS);
85 static	int		  pre_literal(DECL_ARGS);
86 
87 static	void		  post_IP(DECL_ARGS);
88 static	void		  post_HP(DECL_ARGS);
89 static	void		  post_RS(DECL_ARGS);
90 static	void		  post_SH(DECL_ARGS);
91 static	void		  post_SS(DECL_ARGS);
92 static	void		  post_SY(DECL_ARGS);
93 static	void		  post_TP(DECL_ARGS);
94 static	void		  post_UR(DECL_ARGS);
95 
96 static const struct man_term_act man_term_acts[MAN_MAX - MAN_TH] = {
97 	{ NULL, NULL, 0 }, /* TH */
98 	{ pre_SH, post_SH, 0 }, /* SH */
99 	{ pre_SS, post_SS, 0 }, /* SS */
100 	{ pre_TP, post_TP, 0 }, /* TP */
101 	{ pre_TP, post_TP, 0 }, /* TQ */
102 	{ pre_PP, NULL, 0 }, /* LP */
103 	{ pre_PP, NULL, 0 }, /* PP */
104 	{ pre_PP, NULL, 0 }, /* P */
105 	{ pre_IP, post_IP, 0 }, /* IP */
106 	{ pre_HP, post_HP, 0 }, /* HP */
107 	{ NULL, NULL, 0 }, /* SM */
108 	{ pre_B, NULL, 0 }, /* SB */
109 	{ pre_alternate, NULL, 0 }, /* BI */
110 	{ pre_alternate, NULL, 0 }, /* IB */
111 	{ pre_alternate, NULL, 0 }, /* BR */
112 	{ pre_alternate, NULL, 0 }, /* RB */
113 	{ NULL, NULL, 0 }, /* R */
114 	{ pre_B, NULL, 0 }, /* B */
115 	{ pre_I, NULL, 0 }, /* I */
116 	{ pre_alternate, NULL, 0 }, /* IR */
117 	{ pre_alternate, NULL, 0 }, /* RI */
118 	{ pre_literal, NULL, MAN_NOTEXT }, /* nf */
119 	{ pre_literal, NULL, MAN_NOTEXT }, /* fi */
120 	{ NULL, NULL, 0 }, /* RE */
121 	{ pre_RS, post_RS, 0 }, /* RS */
122 	{ pre_DT, NULL, 0 }, /* DT */
123 	{ pre_ign, NULL, MAN_NOTEXT }, /* UC */
124 	{ pre_PD, NULL, MAN_NOTEXT }, /* PD */
125 	{ pre_ign, NULL, 0 }, /* AT */
126 	{ pre_in, NULL, MAN_NOTEXT }, /* in */
127 	{ pre_SY, post_SY, 0 }, /* SY */
128 	{ NULL, NULL, 0 }, /* YS */
129 	{ pre_OP, NULL, 0 }, /* OP */
130 	{ pre_literal, NULL, 0 }, /* EX */
131 	{ pre_literal, NULL, 0 }, /* EE */
132 	{ pre_UR, post_UR, 0 }, /* UR */
133 	{ NULL, NULL, 0 }, /* UE */
134 	{ pre_UR, post_UR, 0 }, /* MT */
135 	{ NULL, NULL, 0 }, /* ME */
136 };
137 static const struct man_term_act *man_term_act(enum roff_tok);
138 
139 
140 static const struct man_term_act *
141 man_term_act(enum roff_tok tok)
142 {
143 	assert(tok >= MAN_TH && tok <= MAN_MAX);
144 	return man_term_acts + (tok - MAN_TH);
145 }
146 
147 void
148 terminal_man(void *arg, const struct roff_man *man)
149 {
150 	struct termp		*p;
151 	struct roff_node	*n;
152 	struct mtermp		 mt;
153 	size_t			 save_defindent;
154 
155 	p = (struct termp *)arg;
156 	save_defindent = p->defindent;
157 	if (p->synopsisonly == 0 && p->defindent == 0)
158 		p->defindent = 7;
159 	p->tcol->rmargin = p->maxrmargin = p->defrmargin;
160 	term_tab_set(p, NULL);
161 	term_tab_set(p, "T");
162 	term_tab_set(p, ".5i");
163 
164 	memset(&mt, 0, sizeof(struct mtermp));
165 	mt.lmargin[mt.lmargincur] = term_len(p, p->defindent);
166 	mt.offset = term_len(p, p->defindent);
167 	mt.pardist = 1;
168 
169 	n = man->first->child;
170 	if (p->synopsisonly) {
171 		while (n != NULL) {
172 			if (n->tok == MAN_SH &&
173 			    n->child->child->type == ROFFT_TEXT &&
174 			    !strcmp(n->child->child->string, "SYNOPSIS")) {
175 				if (n->child->next->child != NULL)
176 					print_man_nodelist(p, &mt,
177 					    n->child->next->child,
178 					    &man->meta);
179 				term_newln(p);
180 				break;
181 			}
182 			n = n->next;
183 		}
184 	} else {
185 		term_begin(p, print_man_head, print_man_foot, &man->meta);
186 		p->flags |= TERMP_NOSPACE;
187 		if (n != NULL)
188 			print_man_nodelist(p, &mt, n, &man->meta);
189 		term_end(p);
190 	}
191 	p->defindent = save_defindent;
192 }
193 
194 /*
195  * Printing leading vertical space before a block.
196  * This is used for the paragraph macros.
197  * The rules are pretty simple, since there's very little nesting going
198  * on here.  Basically, if we're the first within another block (SS/SH),
199  * then don't emit vertical space.  If we are (RS), then do.  If not the
200  * first, print it.
201  */
202 static void
203 print_bvspace(struct termp *p, const struct roff_node *n, int pardist)
204 {
205 	int	 i;
206 
207 	term_newln(p);
208 
209 	if (n->body && n->body->child)
210 		if (n->body->child->type == ROFFT_TBL)
211 			return;
212 
213 	if (n->parent->type == ROFFT_ROOT || n->parent->tok != MAN_RS)
214 		if (NULL == n->prev)
215 			return;
216 
217 	for (i = 0; i < pardist; i++)
218 		term_vspace(p);
219 }
220 
221 
222 static int
223 pre_ign(DECL_ARGS)
224 {
225 
226 	return 0;
227 }
228 
229 static int
230 pre_I(DECL_ARGS)
231 {
232 
233 	term_fontrepl(p, TERMFONT_UNDER);
234 	return 1;
235 }
236 
237 static int
238 pre_literal(DECL_ARGS)
239 {
240 
241 	term_newln(p);
242 
243 	if (n->tok == MAN_nf || n->tok == MAN_EX)
244 		mt->fl |= MANT_LITERAL;
245 	else
246 		mt->fl &= ~MANT_LITERAL;
247 
248 	/*
249 	 * Unlike .IP and .TP, .HP does not have a HEAD.
250 	 * So in case a second call to term_flushln() is needed,
251 	 * indentation has to be set up explicitly.
252 	 */
253 	if (n->parent->tok == MAN_HP && p->tcol->rmargin < p->maxrmargin) {
254 		p->tcol->offset = p->tcol->rmargin;
255 		p->tcol->rmargin = p->maxrmargin;
256 		p->trailspace = 0;
257 		p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
258 		p->flags |= TERMP_NOSPACE;
259 	}
260 
261 	return 0;
262 }
263 
264 static int
265 pre_PD(DECL_ARGS)
266 {
267 	struct roffsu	 su;
268 
269 	n = n->child;
270 	if (n == NULL) {
271 		mt->pardist = 1;
272 		return 0;
273 	}
274 	assert(n->type == ROFFT_TEXT);
275 	if (a2roffsu(n->string, &su, SCALE_VS) != NULL)
276 		mt->pardist = term_vspan(p, &su);
277 	return 0;
278 }
279 
280 static int
281 pre_alternate(DECL_ARGS)
282 {
283 	enum termfont		 font[2];
284 	struct roff_node	*nn;
285 	int			 savelit, i;
286 
287 	switch (n->tok) {
288 	case MAN_RB:
289 		font[0] = TERMFONT_NONE;
290 		font[1] = TERMFONT_BOLD;
291 		break;
292 	case MAN_RI:
293 		font[0] = TERMFONT_NONE;
294 		font[1] = TERMFONT_UNDER;
295 		break;
296 	case MAN_BR:
297 		font[0] = TERMFONT_BOLD;
298 		font[1] = TERMFONT_NONE;
299 		break;
300 	case MAN_BI:
301 		font[0] = TERMFONT_BOLD;
302 		font[1] = TERMFONT_UNDER;
303 		break;
304 	case MAN_IR:
305 		font[0] = TERMFONT_UNDER;
306 		font[1] = TERMFONT_NONE;
307 		break;
308 	case MAN_IB:
309 		font[0] = TERMFONT_UNDER;
310 		font[1] = TERMFONT_BOLD;
311 		break;
312 	default:
313 		abort();
314 	}
315 
316 	savelit = MANT_LITERAL & mt->fl;
317 	mt->fl &= ~MANT_LITERAL;
318 
319 	for (i = 0, nn = n->child; nn; nn = nn->next, i = 1 - i) {
320 		term_fontrepl(p, font[i]);
321 		if (savelit && NULL == nn->next)
322 			mt->fl |= MANT_LITERAL;
323 		assert(nn->type == ROFFT_TEXT);
324 		term_word(p, nn->string);
325 		if (nn->flags & NODE_EOS)
326                 	p->flags |= TERMP_SENTENCE;
327 		if (nn->next)
328 			p->flags |= TERMP_NOSPACE;
329 	}
330 
331 	return 0;
332 }
333 
334 static int
335 pre_B(DECL_ARGS)
336 {
337 
338 	term_fontrepl(p, TERMFONT_BOLD);
339 	return 1;
340 }
341 
342 static int
343 pre_OP(DECL_ARGS)
344 {
345 
346 	term_word(p, "[");
347 	p->flags |= TERMP_KEEP | TERMP_NOSPACE;
348 
349 	if (NULL != (n = n->child)) {
350 		term_fontrepl(p, TERMFONT_BOLD);
351 		term_word(p, n->string);
352 	}
353 	if (NULL != n && NULL != n->next) {
354 		term_fontrepl(p, TERMFONT_UNDER);
355 		term_word(p, n->next->string);
356 	}
357 
358 	term_fontrepl(p, TERMFONT_NONE);
359 	p->flags &= ~TERMP_KEEP;
360 	p->flags |= TERMP_NOSPACE;
361 	term_word(p, "]");
362 	return 0;
363 }
364 
365 static int
366 pre_in(DECL_ARGS)
367 {
368 	struct roffsu	 su;
369 	const char	*cp;
370 	size_t		 v;
371 	int		 less;
372 
373 	term_newln(p);
374 
375 	if (n->child == NULL) {
376 		p->tcol->offset = mt->offset;
377 		return 0;
378 	}
379 
380 	cp = n->child->string;
381 	less = 0;
382 
383 	if ('-' == *cp)
384 		less = -1;
385 	else if ('+' == *cp)
386 		less = 1;
387 	else
388 		cp--;
389 
390 	if (a2roffsu(++cp, &su, SCALE_EN) == NULL)
391 		return 0;
392 
393 	v = term_hen(p, &su);
394 
395 	if (less < 0)
396 		p->tcol->offset -= p->tcol->offset > v ? v : p->tcol->offset;
397 	else if (less > 0)
398 		p->tcol->offset += v;
399 	else
400 		p->tcol->offset = v;
401 	if (p->tcol->offset > SHRT_MAX)
402 		p->tcol->offset = term_len(p, p->defindent);
403 
404 	return 0;
405 }
406 
407 static int
408 pre_DT(DECL_ARGS)
409 {
410 	term_tab_set(p, NULL);
411 	term_tab_set(p, "T");
412 	term_tab_set(p, ".5i");
413 	return 0;
414 }
415 
416 static int
417 pre_HP(DECL_ARGS)
418 {
419 	struct roffsu		 su;
420 	const struct roff_node	*nn;
421 	int			 len;
422 
423 	switch (n->type) {
424 	case ROFFT_BLOCK:
425 		print_bvspace(p, n, mt->pardist);
426 		return 1;
427 	case ROFFT_BODY:
428 		break;
429 	default:
430 		return 0;
431 	}
432 
433 	if ( ! (MANT_LITERAL & mt->fl)) {
434 		p->flags |= TERMP_NOBREAK | TERMP_BRIND;
435 		p->trailspace = 2;
436 	}
437 
438 	/* Calculate offset. */
439 
440 	if ((nn = n->parent->head->child) != NULL &&
441 	    a2roffsu(nn->string, &su, SCALE_EN) != NULL) {
442 		len = term_hen(p, &su);
443 		if (len < 0 && (size_t)(-len) > mt->offset)
444 			len = -mt->offset;
445 		else if (len > SHRT_MAX)
446 			len = term_len(p, p->defindent);
447 		mt->lmargin[mt->lmargincur] = len;
448 	} else
449 		len = mt->lmargin[mt->lmargincur];
450 
451 	p->tcol->offset = mt->offset;
452 	p->tcol->rmargin = mt->offset + len;
453 	return 1;
454 }
455 
456 static void
457 post_HP(DECL_ARGS)
458 {
459 
460 	switch (n->type) {
461 	case ROFFT_BODY:
462 		term_newln(p);
463 
464 		/*
465 		 * Compatibility with a groff bug.
466 		 * The .HP macro uses the undocumented .tag request
467 		 * which causes a line break and cancels no-space
468 		 * mode even if there isn't any output.
469 		 */
470 
471 		if (n->child == NULL)
472 			term_vspace(p);
473 
474 		p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
475 		p->trailspace = 0;
476 		p->tcol->offset = mt->offset;
477 		p->tcol->rmargin = p->maxrmargin;
478 		break;
479 	default:
480 		break;
481 	}
482 }
483 
484 static int
485 pre_PP(DECL_ARGS)
486 {
487 
488 	switch (n->type) {
489 	case ROFFT_BLOCK:
490 		mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
491 		print_bvspace(p, n, mt->pardist);
492 		break;
493 	default:
494 		p->tcol->offset = mt->offset;
495 		break;
496 	}
497 
498 	return n->type != ROFFT_HEAD;
499 }
500 
501 static int
502 pre_IP(DECL_ARGS)
503 {
504 	struct roffsu		 su;
505 	const struct roff_node	*nn;
506 	int			 len, savelit;
507 
508 	switch (n->type) {
509 	case ROFFT_BODY:
510 		p->flags |= TERMP_NOSPACE;
511 		break;
512 	case ROFFT_HEAD:
513 		p->flags |= TERMP_NOBREAK;
514 		p->trailspace = 1;
515 		break;
516 	case ROFFT_BLOCK:
517 		print_bvspace(p, n, mt->pardist);
518 		/* FALLTHROUGH */
519 	default:
520 		return 1;
521 	}
522 
523 	/* Calculate the offset from the optional second argument. */
524 	if ((nn = n->parent->head->child) != NULL &&
525 	    (nn = nn->next) != NULL &&
526 	    a2roffsu(nn->string, &su, SCALE_EN) != NULL) {
527 		len = term_hen(p, &su);
528 		if (len < 0 && (size_t)(-len) > mt->offset)
529 			len = -mt->offset;
530 		else if (len > SHRT_MAX)
531 			len = term_len(p, p->defindent);
532 		mt->lmargin[mt->lmargincur] = len;
533 	} else
534 		len = mt->lmargin[mt->lmargincur];
535 
536 	switch (n->type) {
537 	case ROFFT_HEAD:
538 		p->tcol->offset = mt->offset;
539 		p->tcol->rmargin = mt->offset + len;
540 
541 		savelit = MANT_LITERAL & mt->fl;
542 		mt->fl &= ~MANT_LITERAL;
543 
544 		if (n->child)
545 			print_man_node(p, mt, n->child, meta);
546 
547 		if (savelit)
548 			mt->fl |= MANT_LITERAL;
549 
550 		return 0;
551 	case ROFFT_BODY:
552 		p->tcol->offset = mt->offset + len;
553 		p->tcol->rmargin = p->maxrmargin;
554 		break;
555 	default:
556 		break;
557 	}
558 
559 	return 1;
560 }
561 
562 static void
563 post_IP(DECL_ARGS)
564 {
565 
566 	switch (n->type) {
567 	case ROFFT_HEAD:
568 		term_flushln(p);
569 		p->flags &= ~TERMP_NOBREAK;
570 		p->trailspace = 0;
571 		p->tcol->rmargin = p->maxrmargin;
572 		break;
573 	case ROFFT_BODY:
574 		term_newln(p);
575 		p->tcol->offset = mt->offset;
576 		break;
577 	default:
578 		break;
579 	}
580 }
581 
582 static int
583 pre_TP(DECL_ARGS)
584 {
585 	struct roffsu		 su;
586 	struct roff_node	*nn;
587 	int			 len, savelit;
588 
589 	switch (n->type) {
590 	case ROFFT_HEAD:
591 		p->flags |= TERMP_NOBREAK | TERMP_BRTRSP;
592 		p->trailspace = 1;
593 		break;
594 	case ROFFT_BODY:
595 		p->flags |= TERMP_NOSPACE;
596 		break;
597 	case ROFFT_BLOCK:
598 		if (n->tok == MAN_TP)
599 			print_bvspace(p, n, mt->pardist);
600 		/* FALLTHROUGH */
601 	default:
602 		return 1;
603 	}
604 
605 	/* Calculate offset. */
606 
607 	if ((nn = n->parent->head->child) != NULL &&
608 	    nn->string != NULL && ! (NODE_LINE & nn->flags) &&
609 	    a2roffsu(nn->string, &su, SCALE_EN) != NULL) {
610 		len = term_hen(p, &su);
611 		if (len < 0 && (size_t)(-len) > mt->offset)
612 			len = -mt->offset;
613 		else if (len > SHRT_MAX)
614 			len = term_len(p, p->defindent);
615 		mt->lmargin[mt->lmargincur] = len;
616 	} else
617 		len = mt->lmargin[mt->lmargincur];
618 
619 	switch (n->type) {
620 	case ROFFT_HEAD:
621 		p->tcol->offset = mt->offset;
622 		p->tcol->rmargin = mt->offset + len;
623 
624 		savelit = MANT_LITERAL & mt->fl;
625 		mt->fl &= ~MANT_LITERAL;
626 
627 		/* Don't print same-line elements. */
628 		nn = n->child;
629 		while (NULL != nn && 0 == (NODE_LINE & nn->flags))
630 			nn = nn->next;
631 
632 		while (NULL != nn) {
633 			print_man_node(p, mt, nn, meta);
634 			nn = nn->next;
635 		}
636 
637 		if (savelit)
638 			mt->fl |= MANT_LITERAL;
639 		return 0;
640 	case ROFFT_BODY:
641 		p->tcol->offset = mt->offset + len;
642 		p->tcol->rmargin = p->maxrmargin;
643 		p->trailspace = 0;
644 		p->flags &= ~(TERMP_NOBREAK | TERMP_BRTRSP);
645 		break;
646 	default:
647 		break;
648 	}
649 
650 	return 1;
651 }
652 
653 static void
654 post_TP(DECL_ARGS)
655 {
656 
657 	switch (n->type) {
658 	case ROFFT_HEAD:
659 		term_flushln(p);
660 		break;
661 	case ROFFT_BODY:
662 		term_newln(p);
663 		p->tcol->offset = mt->offset;
664 		break;
665 	default:
666 		break;
667 	}
668 }
669 
670 static int
671 pre_SS(DECL_ARGS)
672 {
673 	int	 i;
674 
675 	switch (n->type) {
676 	case ROFFT_BLOCK:
677 		mt->fl &= ~MANT_LITERAL;
678 		mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
679 		mt->offset = term_len(p, p->defindent);
680 
681 		/*
682 		 * No vertical space before the first subsection
683 		 * and after an empty subsection.
684 		 */
685 
686 		do {
687 			n = n->prev;
688 		} while (n != NULL && n->tok >= MAN_TH &&
689 		    man_term_act(n->tok)->flags & MAN_NOTEXT);
690 		if (n == NULL || n->type == ROFFT_COMMENT ||
691 		    (n->tok == MAN_SS && n->body->child == NULL))
692 			break;
693 
694 		for (i = 0; i < mt->pardist; i++)
695 			term_vspace(p);
696 		break;
697 	case ROFFT_HEAD:
698 		term_fontrepl(p, TERMFONT_BOLD);
699 		p->tcol->offset = term_len(p, 3);
700 		p->tcol->rmargin = mt->offset;
701 		p->trailspace = mt->offset;
702 		p->flags |= TERMP_NOBREAK | TERMP_BRIND;
703 		break;
704 	case ROFFT_BODY:
705 		p->tcol->offset = mt->offset;
706 		p->tcol->rmargin = p->maxrmargin;
707 		p->trailspace = 0;
708 		p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
709 		break;
710 	default:
711 		break;
712 	}
713 
714 	return 1;
715 }
716 
717 static void
718 post_SS(DECL_ARGS)
719 {
720 
721 	switch (n->type) {
722 	case ROFFT_HEAD:
723 		term_newln(p);
724 		break;
725 	case ROFFT_BODY:
726 		term_newln(p);
727 		break;
728 	default:
729 		break;
730 	}
731 }
732 
733 static int
734 pre_SH(DECL_ARGS)
735 {
736 	int	 i;
737 
738 	switch (n->type) {
739 	case ROFFT_BLOCK:
740 		mt->fl &= ~MANT_LITERAL;
741 		mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
742 		mt->offset = term_len(p, p->defindent);
743 
744 		/*
745 		 * No vertical space before the first section
746 		 * and after an empty section.
747 		 */
748 
749 		do {
750 			n = n->prev;
751 		} while (n != NULL && n->tok >= MAN_TH &&
752 		    man_term_act(n->tok)->flags & MAN_NOTEXT);
753 		if (n == NULL || n->type == ROFFT_COMMENT ||
754 		    (n->tok == MAN_SH && n->body->child == NULL))
755 			break;
756 
757 		for (i = 0; i < mt->pardist; i++)
758 			term_vspace(p);
759 		break;
760 	case ROFFT_HEAD:
761 		term_fontrepl(p, TERMFONT_BOLD);
762 		p->tcol->offset = 0;
763 		p->tcol->rmargin = mt->offset;
764 		p->trailspace = mt->offset;
765 		p->flags |= TERMP_NOBREAK | TERMP_BRIND;
766 		break;
767 	case ROFFT_BODY:
768 		p->tcol->offset = mt->offset;
769 		p->tcol->rmargin = p->maxrmargin;
770 		p->trailspace = 0;
771 		p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
772 		break;
773 	default:
774 		break;
775 	}
776 
777 	return 1;
778 }
779 
780 static void
781 post_SH(DECL_ARGS)
782 {
783 
784 	switch (n->type) {
785 	case ROFFT_HEAD:
786 		term_newln(p);
787 		break;
788 	case ROFFT_BODY:
789 		term_newln(p);
790 		break;
791 	default:
792 		break;
793 	}
794 }
795 
796 static int
797 pre_RS(DECL_ARGS)
798 {
799 	struct roffsu	 su;
800 
801 	switch (n->type) {
802 	case ROFFT_BLOCK:
803 		term_newln(p);
804 		return 1;
805 	case ROFFT_HEAD:
806 		return 0;
807 	default:
808 		break;
809 	}
810 
811 	n = n->parent->head;
812 	n->aux = SHRT_MAX + 1;
813 	if (n->child == NULL)
814 		n->aux = mt->lmargin[mt->lmargincur];
815 	else if (a2roffsu(n->child->string, &su, SCALE_EN) != NULL)
816 		n->aux = term_hen(p, &su);
817 	if (n->aux < 0 && (size_t)(-n->aux) > mt->offset)
818 		n->aux = -mt->offset;
819 	else if (n->aux > SHRT_MAX)
820 		n->aux = term_len(p, p->defindent);
821 
822 	mt->offset += n->aux;
823 	p->tcol->offset = mt->offset;
824 	p->tcol->rmargin = p->maxrmargin;
825 
826 	if (++mt->lmarginsz < MAXMARGINS)
827 		mt->lmargincur = mt->lmarginsz;
828 
829 	mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
830 	return 1;
831 }
832 
833 static void
834 post_RS(DECL_ARGS)
835 {
836 
837 	switch (n->type) {
838 	case ROFFT_BLOCK:
839 		return;
840 	case ROFFT_HEAD:
841 		return;
842 	default:
843 		term_newln(p);
844 		break;
845 	}
846 
847 	mt->offset -= n->parent->head->aux;
848 	p->tcol->offset = mt->offset;
849 
850 	if (--mt->lmarginsz < MAXMARGINS)
851 		mt->lmargincur = mt->lmarginsz;
852 }
853 
854 static int
855 pre_SY(DECL_ARGS)
856 {
857 	const struct roff_node	*nn;
858 	int			 len;
859 
860 	switch (n->type) {
861 	case ROFFT_BLOCK:
862 		if (n->prev == NULL || n->prev->tok != MAN_SY)
863 			print_bvspace(p, n, mt->pardist);
864 		return 1;
865 	case ROFFT_HEAD:
866 	case ROFFT_BODY:
867 		break;
868 	default:
869 		abort();
870 	}
871 
872 	nn = n->parent->head->child;
873 	len = nn == NULL ? 1 : term_strlen(p, nn->string) + 1;
874 
875 	switch (n->type) {
876 	case ROFFT_HEAD:
877 		p->tcol->offset = mt->offset;
878 		p->tcol->rmargin = mt->offset + len;
879 		p->flags |= TERMP_NOBREAK;
880 		term_fontrepl(p, TERMFONT_BOLD);
881 		break;
882 	case ROFFT_BODY:
883 		mt->lmargin[mt->lmargincur] = len;
884 		p->tcol->offset = mt->offset + len;
885 		p->tcol->rmargin = p->maxrmargin;
886 		p->flags |= TERMP_NOSPACE;
887 		break;
888 	default:
889 		abort();
890 	}
891 	return 1;
892 }
893 
894 static void
895 post_SY(DECL_ARGS)
896 {
897 	switch (n->type) {
898 	case ROFFT_HEAD:
899 		term_flushln(p);
900 		p->flags &= ~TERMP_NOBREAK;
901 		break;
902 	case ROFFT_BODY:
903 		term_newln(p);
904 		p->tcol->offset = mt->offset;
905 		break;
906 	default:
907 		break;
908 	}
909 }
910 
911 static int
912 pre_UR(DECL_ARGS)
913 {
914 
915 	return n->type != ROFFT_HEAD;
916 }
917 
918 static void
919 post_UR(DECL_ARGS)
920 {
921 
922 	if (n->type != ROFFT_BLOCK)
923 		return;
924 
925 	term_word(p, "<");
926 	p->flags |= TERMP_NOSPACE;
927 
928 	if (NULL != n->child->child)
929 		print_man_node(p, mt, n->child->child, meta);
930 
931 	p->flags |= TERMP_NOSPACE;
932 	term_word(p, ">");
933 }
934 
935 static void
936 print_man_node(DECL_ARGS)
937 {
938 	const struct man_term_act *act;
939 	int c;
940 
941 	switch (n->type) {
942 	case ROFFT_TEXT:
943 		/*
944 		 * If we have a blank line, output a vertical space.
945 		 * If we have a space as the first character, break
946 		 * before printing the line's data.
947 		 */
948 		if (*n->string == '\0') {
949 			if (p->flags & TERMP_NONEWLINE)
950 				term_newln(p);
951 			else
952 				term_vspace(p);
953 			return;
954 		} else if (*n->string == ' ' && n->flags & NODE_LINE &&
955 		    (p->flags & TERMP_NONEWLINE) == 0)
956 			term_newln(p);
957 		else if (n->flags & NODE_DELIMC)
958 			p->flags |= TERMP_NOSPACE;
959 
960 		term_word(p, n->string);
961 		goto out;
962 	case ROFFT_COMMENT:
963 		return;
964 	case ROFFT_EQN:
965 		if ( ! (n->flags & NODE_LINE))
966 			p->flags |= TERMP_NOSPACE;
967 		term_eqn(p, n->eqn);
968 		if (n->next != NULL && ! (n->next->flags & NODE_LINE))
969 			p->flags |= TERMP_NOSPACE;
970 		return;
971 	case ROFFT_TBL:
972 		if (p->tbl.cols == NULL)
973 			term_vspace(p);
974 		term_tbl(p, n->span);
975 		return;
976 	default:
977 		break;
978 	}
979 
980 	if (n->tok < ROFF_MAX) {
981 		roff_term_pre(p, n);
982 		return;
983 	}
984 
985 	act = man_term_act(n->tok);
986 	if ((act->flags & MAN_NOTEXT) == 0 && n->tok != MAN_SM)
987 		term_fontrepl(p, TERMFONT_NONE);
988 
989 	c = 1;
990 	if (act->pre != NULL)
991 		c = (*act->pre)(p, mt, n, meta);
992 
993 	if (c && n->child)
994 		print_man_nodelist(p, mt, n->child, meta);
995 
996 	if (act->post != NULL)
997 		(*act->post)(p, mt, n, meta);
998 	if ((act->flags & MAN_NOTEXT) == 0 && n->tok != MAN_SM)
999 		term_fontrepl(p, TERMFONT_NONE);
1000 
1001 out:
1002 	/*
1003 	 * If we're in a literal context, make sure that words
1004 	 * together on the same line stay together.  This is a
1005 	 * POST-printing call, so we check the NEXT word.  Since
1006 	 * -man doesn't have nested macros, we don't need to be
1007 	 * more specific than this.
1008 	 */
1009 	if (mt->fl & MANT_LITERAL &&
1010 	    ! (p->flags & (TERMP_NOBREAK | TERMP_NONEWLINE)) &&
1011 	    (n->next == NULL || n->next->flags & NODE_LINE)) {
1012 		p->flags |= TERMP_BRNEVER | TERMP_NOSPACE;
1013 		if (n->string != NULL && *n->string != '\0')
1014 			term_flushln(p);
1015 		else
1016 			term_newln(p);
1017 		p->flags &= ~TERMP_BRNEVER;
1018 		if (p->tcol->rmargin < p->maxrmargin &&
1019 		    n->parent->tok == MAN_HP) {
1020 			p->tcol->offset = p->tcol->rmargin;
1021 			p->tcol->rmargin = p->maxrmargin;
1022 		}
1023 	}
1024 	if (NODE_EOS & n->flags)
1025 		p->flags |= TERMP_SENTENCE;
1026 }
1027 
1028 
1029 static void
1030 print_man_nodelist(DECL_ARGS)
1031 {
1032 
1033 	while (n != NULL) {
1034 		print_man_node(p, mt, n, meta);
1035 		n = n->next;
1036 	}
1037 }
1038 
1039 static void
1040 print_man_foot(struct termp *p, const struct roff_meta *meta)
1041 {
1042 	char			*title;
1043 	size_t			 datelen, titlen;
1044 
1045 	assert(meta->title);
1046 	assert(meta->msec);
1047 	assert(meta->date);
1048 
1049 	term_fontrepl(p, TERMFONT_NONE);
1050 
1051 	if (meta->hasbody)
1052 		term_vspace(p);
1053 
1054 	/*
1055 	 * Temporary, undocumented option to imitate mdoc(7) output.
1056 	 * In the bottom right corner, use the operating system
1057 	 * instead of the title.
1058 	 */
1059 
1060 	if ( ! p->mdocstyle) {
1061 		if (meta->hasbody) {
1062 			term_vspace(p);
1063 			term_vspace(p);
1064 		}
1065 		mandoc_asprintf(&title, "%s(%s)",
1066 		    meta->title, meta->msec);
1067 	} else if (meta->os) {
1068 		title = mandoc_strdup(meta->os);
1069 	} else {
1070 		title = mandoc_strdup("");
1071 	}
1072 	datelen = term_strlen(p, meta->date);
1073 
1074 	/* Bottom left corner: operating system. */
1075 
1076 	p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
1077 	p->trailspace = 1;
1078 	p->tcol->offset = 0;
1079 	p->tcol->rmargin = p->maxrmargin > datelen ?
1080 	    (p->maxrmargin + term_len(p, 1) - datelen) / 2 : 0;
1081 
1082 	if (meta->os)
1083 		term_word(p, meta->os);
1084 	term_flushln(p);
1085 
1086 	/* At the bottom in the middle: manual date. */
1087 
1088 	p->tcol->offset = p->tcol->rmargin;
1089 	titlen = term_strlen(p, title);
1090 	p->tcol->rmargin = p->maxrmargin > titlen ?
1091 	    p->maxrmargin - titlen : 0;
1092 	p->flags |= TERMP_NOSPACE;
1093 
1094 	term_word(p, meta->date);
1095 	term_flushln(p);
1096 
1097 	/* Bottom right corner: manual title and section. */
1098 
1099 	p->flags &= ~TERMP_NOBREAK;
1100 	p->flags |= TERMP_NOSPACE;
1101 	p->trailspace = 0;
1102 	p->tcol->offset = p->tcol->rmargin;
1103 	p->tcol->rmargin = p->maxrmargin;
1104 
1105 	term_word(p, title);
1106 	term_flushln(p);
1107 
1108 	/*
1109 	 * Reset the terminal state for more output after the footer:
1110 	 * Some output modes, in particular PostScript and PDF, print
1111 	 * the header and the footer into a buffer such that it can be
1112 	 * reused for multiple output pages, then go on to format the
1113 	 * main text.
1114 	 */
1115 
1116         p->tcol->offset = 0;
1117         p->flags = 0;
1118 
1119 	free(title);
1120 }
1121 
1122 static void
1123 print_man_head(struct termp *p, const struct roff_meta *meta)
1124 {
1125 	const char		*volume;
1126 	char			*title;
1127 	size_t			 vollen, titlen;
1128 
1129 	assert(meta->title);
1130 	assert(meta->msec);
1131 
1132 	volume = NULL == meta->vol ? "" : meta->vol;
1133 	vollen = term_strlen(p, volume);
1134 
1135 	/* Top left corner: manual title and section. */
1136 
1137 	mandoc_asprintf(&title, "%s(%s)", meta->title, meta->msec);
1138 	titlen = term_strlen(p, title);
1139 
1140 	p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
1141 	p->trailspace = 1;
1142 	p->tcol->offset = 0;
1143 	p->tcol->rmargin = 2 * (titlen+1) + vollen < p->maxrmargin ?
1144 	    (p->maxrmargin - vollen + term_len(p, 1)) / 2 :
1145 	    vollen < p->maxrmargin ? p->maxrmargin - vollen : 0;
1146 
1147 	term_word(p, title);
1148 	term_flushln(p);
1149 
1150 	/* At the top in the middle: manual volume. */
1151 
1152 	p->flags |= TERMP_NOSPACE;
1153 	p->tcol->offset = p->tcol->rmargin;
1154 	p->tcol->rmargin = p->tcol->offset + vollen + titlen <
1155 	    p->maxrmargin ?  p->maxrmargin - titlen : p->maxrmargin;
1156 
1157 	term_word(p, volume);
1158 	term_flushln(p);
1159 
1160 	/* Top right corner: title and section, again. */
1161 
1162 	p->flags &= ~TERMP_NOBREAK;
1163 	p->trailspace = 0;
1164 	if (p->tcol->rmargin + titlen <= p->maxrmargin) {
1165 		p->flags |= TERMP_NOSPACE;
1166 		p->tcol->offset = p->tcol->rmargin;
1167 		p->tcol->rmargin = p->maxrmargin;
1168 		term_word(p, title);
1169 		term_flushln(p);
1170 	}
1171 
1172 	p->flags &= ~TERMP_NOSPACE;
1173 	p->tcol->offset = 0;
1174 	p->tcol->rmargin = p->maxrmargin;
1175 
1176 	/*
1177 	 * Groff prints three blank lines before the content.
1178 	 * Do the same, except in the temporary, undocumented
1179 	 * mode imitating mdoc(7) output.
1180 	 */
1181 
1182 	term_vspace(p);
1183 	if ( ! p->mdocstyle) {
1184 		term_vspace(p);
1185 		term_vspace(p);
1186 	}
1187 	free(title);
1188 }
1189