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