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