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