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