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