1*4d131170SRobert Mustacchi /* $Id: man_term.c,v 1.236 2021/06/28 19:50:15 schwarze Exp $ */
295c635efSGarrett D'Amore /*
3*4d131170SRobert Mustacchi * Copyright (c) 2010-2015, 2017-2020 Ingo Schwarze <schwarze@openbsd.org>
4698f87a4SGarrett D'Amore * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
595c635efSGarrett D'Amore *
695c635efSGarrett D'Amore * Permission to use, copy, modify, and distribute this software for any
795c635efSGarrett D'Amore * purpose with or without fee is hereby granted, provided that the above
895c635efSGarrett D'Amore * copyright notice and this permission notice appear in all copies.
995c635efSGarrett D'Amore *
10371584c2SYuri Pankov * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1195c635efSGarrett D'Amore * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12371584c2SYuri Pankov * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1395c635efSGarrett D'Amore * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1495c635efSGarrett D'Amore * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1595c635efSGarrett D'Amore * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1695c635efSGarrett D'Amore * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*4d131170SRobert Mustacchi *
18*4d131170SRobert Mustacchi * Plain text formatter for man(7), used by mandoc(1)
19*4d131170SRobert Mustacchi * for ASCII, UTF-8, PostScript, and PDF output.
2095c635efSGarrett D'Amore */
2195c635efSGarrett D'Amore #include "config.h"
2295c635efSGarrett D'Amore
2395c635efSGarrett D'Amore #include <sys/types.h>
2495c635efSGarrett D'Amore
2595c635efSGarrett D'Amore #include <assert.h>
2695c635efSGarrett D'Amore #include <ctype.h>
27260e9a87SYuri Pankov #include <limits.h>
2895c635efSGarrett D'Amore #include <stdio.h>
2995c635efSGarrett D'Amore #include <stdlib.h>
3095c635efSGarrett D'Amore #include <string.h>
3195c635efSGarrett D'Amore
32260e9a87SYuri Pankov #include "mandoc_aux.h"
33*4d131170SRobert Mustacchi #include "mandoc.h"
34371584c2SYuri Pankov #include "roff.h"
3595c635efSGarrett D'Amore #include "man.h"
36371584c2SYuri Pankov #include "out.h"
3795c635efSGarrett D'Amore #include "term.h"
38*4d131170SRobert Mustacchi #include "term_tag.h"
3995c635efSGarrett D'Amore #include "main.h"
4095c635efSGarrett D'Amore
4195c635efSGarrett D'Amore #define MAXMARGINS 64 /* maximum number of indented scopes */
4295c635efSGarrett D'Amore
4395c635efSGarrett D'Amore struct mtermp {
44260e9a87SYuri Pankov int lmargin[MAXMARGINS]; /* margins (incl. vis. page) */
4595c635efSGarrett D'Amore int lmargincur; /* index of current margin */
4695c635efSGarrett D'Amore int lmarginsz; /* actual number of nested margins */
4795c635efSGarrett D'Amore size_t offset; /* default offset to visible page */
48698f87a4SGarrett D'Amore int pardist; /* vert. space before par., unit: [v] */
4995c635efSGarrett D'Amore };
5095c635efSGarrett D'Amore
51260e9a87SYuri Pankov #define DECL_ARGS struct termp *p, \
5295c635efSGarrett D'Amore struct mtermp *mt, \
53371584c2SYuri Pankov struct roff_node *n, \
54371584c2SYuri Pankov const struct roff_meta *meta
5595c635efSGarrett D'Amore
56cec8643bSMichal Nowak struct man_term_act {
5795c635efSGarrett D'Amore int (*pre)(DECL_ARGS);
5895c635efSGarrett D'Amore void (*post)(DECL_ARGS);
5995c635efSGarrett D'Amore int flags;
6095c635efSGarrett D'Amore #define MAN_NOTEXT (1 << 0) /* Never has text children. */
6195c635efSGarrett D'Amore };
6295c635efSGarrett D'Amore
6395c635efSGarrett D'Amore static void print_man_nodelist(DECL_ARGS);
6495c635efSGarrett D'Amore static void print_man_node(DECL_ARGS);
65371584c2SYuri Pankov static void print_man_head(struct termp *,
66371584c2SYuri Pankov const struct roff_meta *);
67371584c2SYuri Pankov static void print_man_foot(struct termp *,
68371584c2SYuri Pankov const struct roff_meta *);
69260e9a87SYuri Pankov static void print_bvspace(struct termp *,
70*4d131170SRobert Mustacchi struct roff_node *, int);
7195c635efSGarrett D'Amore
7295c635efSGarrett D'Amore static int pre_B(DECL_ARGS);
73c66b8046SYuri Pankov static int pre_DT(DECL_ARGS);
7495c635efSGarrett D'Amore static int pre_HP(DECL_ARGS);
7595c635efSGarrett D'Amore static int pre_I(DECL_ARGS);
7695c635efSGarrett D'Amore static int pre_IP(DECL_ARGS);
7795c635efSGarrett D'Amore static int pre_OP(DECL_ARGS);
78698f87a4SGarrett D'Amore static int pre_PD(DECL_ARGS);
7995c635efSGarrett D'Amore static int pre_PP(DECL_ARGS);
8095c635efSGarrett D'Amore static int pre_RS(DECL_ARGS);
8195c635efSGarrett D'Amore static int pre_SH(DECL_ARGS);
8295c635efSGarrett D'Amore static int pre_SS(DECL_ARGS);
83cec8643bSMichal Nowak static int pre_SY(DECL_ARGS);
8495c635efSGarrett D'Amore static int pre_TP(DECL_ARGS);
85698f87a4SGarrett D'Amore static int pre_UR(DECL_ARGS);
86cec8643bSMichal Nowak static int pre_abort(DECL_ARGS);
8795c635efSGarrett D'Amore static int pre_alternate(DECL_ARGS);
8895c635efSGarrett D'Amore static int pre_ign(DECL_ARGS);
8995c635efSGarrett D'Amore static int pre_in(DECL_ARGS);
9095c635efSGarrett D'Amore static int pre_literal(DECL_ARGS);
9195c635efSGarrett D'Amore
9295c635efSGarrett D'Amore static void post_IP(DECL_ARGS);
9395c635efSGarrett D'Amore static void post_HP(DECL_ARGS);
9495c635efSGarrett D'Amore static void post_RS(DECL_ARGS);
9595c635efSGarrett D'Amore static void post_SH(DECL_ARGS);
96cec8643bSMichal Nowak static void post_SY(DECL_ARGS);
9795c635efSGarrett D'Amore static void post_TP(DECL_ARGS);
98698f87a4SGarrett D'Amore static void post_UR(DECL_ARGS);
9995c635efSGarrett D'Amore
100cec8643bSMichal Nowak static const struct man_term_act man_term_acts[MAN_MAX - MAN_TH] = {
10195c635efSGarrett D'Amore { NULL, NULL, 0 }, /* TH */
10295c635efSGarrett D'Amore { pre_SH, post_SH, 0 }, /* SH */
103cec8643bSMichal Nowak { pre_SS, post_SH, 0 }, /* SS */
10495c635efSGarrett D'Amore { pre_TP, post_TP, 0 }, /* TP */
105cec8643bSMichal Nowak { pre_TP, post_TP, 0 }, /* TQ */
106cec8643bSMichal Nowak { pre_abort, NULL, 0 }, /* LP */
10795c635efSGarrett D'Amore { pre_PP, NULL, 0 }, /* PP */
108cec8643bSMichal Nowak { pre_abort, NULL, 0 }, /* P */
10995c635efSGarrett D'Amore { pre_IP, post_IP, 0 }, /* IP */
110260e9a87SYuri Pankov { pre_HP, post_HP, 0 }, /* HP */
11195c635efSGarrett D'Amore { NULL, NULL, 0 }, /* SM */
11295c635efSGarrett D'Amore { pre_B, NULL, 0 }, /* SB */
11395c635efSGarrett D'Amore { pre_alternate, NULL, 0 }, /* BI */
11495c635efSGarrett D'Amore { pre_alternate, NULL, 0 }, /* IB */
11595c635efSGarrett D'Amore { pre_alternate, NULL, 0 }, /* BR */
11695c635efSGarrett D'Amore { pre_alternate, NULL, 0 }, /* RB */
11795c635efSGarrett D'Amore { NULL, NULL, 0 }, /* R */
11895c635efSGarrett D'Amore { pre_B, NULL, 0 }, /* B */
11995c635efSGarrett D'Amore { pre_I, NULL, 0 }, /* I */
12095c635efSGarrett D'Amore { pre_alternate, NULL, 0 }, /* IR */
12195c635efSGarrett D'Amore { pre_alternate, NULL, 0 }, /* RI */
12295c635efSGarrett D'Amore { NULL, NULL, 0 }, /* RE */
12395c635efSGarrett D'Amore { pre_RS, post_RS, 0 }, /* RS */
124c66b8046SYuri Pankov { pre_DT, NULL, 0 }, /* DT */
125260e9a87SYuri Pankov { pre_ign, NULL, MAN_NOTEXT }, /* UC */
126698f87a4SGarrett D'Amore { pre_PD, NULL, MAN_NOTEXT }, /* PD */
12795c635efSGarrett D'Amore { pre_ign, NULL, 0 }, /* AT */
12895c635efSGarrett D'Amore { pre_in, NULL, MAN_NOTEXT }, /* in */
129cec8643bSMichal Nowak { pre_SY, post_SY, 0 }, /* SY */
130cec8643bSMichal Nowak { NULL, NULL, 0 }, /* YS */
13195c635efSGarrett D'Amore { pre_OP, NULL, 0 }, /* OP */
132698f87a4SGarrett D'Amore { pre_literal, NULL, 0 }, /* EX */
133698f87a4SGarrett D'Amore { pre_literal, NULL, 0 }, /* EE */
134698f87a4SGarrett D'Amore { pre_UR, post_UR, 0 }, /* UR */
135698f87a4SGarrett D'Amore { NULL, NULL, 0 }, /* UE */
136c66b8046SYuri Pankov { pre_UR, post_UR, 0 }, /* MT */
137c66b8046SYuri Pankov { NULL, NULL, 0 }, /* ME */
13895c635efSGarrett D'Amore };
139cec8643bSMichal Nowak static const struct man_term_act *man_term_act(enum roff_tok);
14095c635efSGarrett D'Amore
14195c635efSGarrett D'Amore
142cec8643bSMichal Nowak static const struct man_term_act *
man_term_act(enum roff_tok tok)143cec8643bSMichal Nowak man_term_act(enum roff_tok tok)
144cec8643bSMichal Nowak {
145cec8643bSMichal Nowak assert(tok >= MAN_TH && tok <= MAN_MAX);
146cec8643bSMichal Nowak return man_term_acts + (tok - MAN_TH);
147cec8643bSMichal Nowak }
148cec8643bSMichal Nowak
14995c635efSGarrett D'Amore void
terminal_man(void * arg,const struct roff_meta * man)150cec8643bSMichal Nowak terminal_man(void *arg, const struct roff_meta *man)
15195c635efSGarrett D'Amore {
152cec8643bSMichal Nowak struct mtermp mt;
15395c635efSGarrett D'Amore struct termp *p;
154*4d131170SRobert Mustacchi struct roff_node *n, *nc, *nn;
155a40ea1a7SYuri Pankov size_t save_defindent;
15695c635efSGarrett D'Amore
15795c635efSGarrett D'Amore p = (struct termp *)arg;
158c66b8046SYuri Pankov save_defindent = p->defindent;
159c66b8046SYuri Pankov if (p->synopsisonly == 0 && p->defindent == 0)
160c66b8046SYuri Pankov p->defindent = 7;
161c66b8046SYuri Pankov p->tcol->rmargin = p->maxrmargin = p->defrmargin;
162c66b8046SYuri Pankov term_tab_set(p, NULL);
163c66b8046SYuri Pankov term_tab_set(p, "T");
164c66b8046SYuri Pankov term_tab_set(p, ".5i");
16595c635efSGarrett D'Amore
166cec8643bSMichal Nowak memset(&mt, 0, sizeof(mt));
16795c635efSGarrett D'Amore mt.lmargin[mt.lmargincur] = term_len(p, p->defindent);
16895c635efSGarrett D'Amore mt.offset = term_len(p, p->defindent);
169698f87a4SGarrett D'Amore mt.pardist = 1;
17095c635efSGarrett D'Amore
171371584c2SYuri Pankov n = man->first->child;
172260e9a87SYuri Pankov if (p->synopsisonly) {
173*4d131170SRobert Mustacchi for (nn = NULL; n != NULL; n = n->next) {
174*4d131170SRobert Mustacchi if (n->tok != MAN_SH)
175*4d131170SRobert Mustacchi continue;
176*4d131170SRobert Mustacchi nc = n->child->child;
177*4d131170SRobert Mustacchi if (nc->type != ROFFT_TEXT)
178*4d131170SRobert Mustacchi continue;
179*4d131170SRobert Mustacchi if (strcmp(nc->string, "SYNOPSIS") == 0)
180260e9a87SYuri Pankov break;
181*4d131170SRobert Mustacchi if (nn == NULL && strcmp(nc->string, "NAME") == 0)
182*4d131170SRobert Mustacchi nn = n;
183260e9a87SYuri Pankov }
184*4d131170SRobert Mustacchi if (n == NULL)
185*4d131170SRobert Mustacchi n = nn;
186*4d131170SRobert Mustacchi p->flags |= TERMP_NOSPACE;
187*4d131170SRobert Mustacchi if (n != NULL && (n = n->child->next->child) != NULL)
188*4d131170SRobert Mustacchi print_man_nodelist(p, &mt, n, man);
189*4d131170SRobert Mustacchi term_newln(p);
190260e9a87SYuri Pankov } else {
191cec8643bSMichal Nowak term_begin(p, print_man_head, print_man_foot, man);
192260e9a87SYuri Pankov p->flags |= TERMP_NOSPACE;
193260e9a87SYuri Pankov if (n != NULL)
194cec8643bSMichal Nowak print_man_nodelist(p, &mt, n, man);
195260e9a87SYuri Pankov term_end(p);
196260e9a87SYuri Pankov }
197c66b8046SYuri Pankov p->defindent = save_defindent;
19895c635efSGarrett D'Amore }
19995c635efSGarrett D'Amore
20095c635efSGarrett D'Amore /*
20195c635efSGarrett D'Amore * Printing leading vertical space before a block.
20295c635efSGarrett D'Amore * This is used for the paragraph macros.
20395c635efSGarrett D'Amore * The rules are pretty simple, since there's very little nesting going
20495c635efSGarrett D'Amore * on here. Basically, if we're the first within another block (SS/SH),
20595c635efSGarrett D'Amore * then don't emit vertical space. If we are (RS), then do. If not the
20695c635efSGarrett D'Amore * first, print it.
20795c635efSGarrett D'Amore */
20895c635efSGarrett D'Amore static void
print_bvspace(struct termp * p,struct roff_node * n,int pardist)209*4d131170SRobert Mustacchi print_bvspace(struct termp *p, struct roff_node *n, int pardist)
21095c635efSGarrett D'Amore {
211*4d131170SRobert Mustacchi struct roff_node *nch;
212*4d131170SRobert Mustacchi int i;
21395c635efSGarrett D'Amore
21495c635efSGarrett D'Amore term_newln(p);
21595c635efSGarrett D'Amore
216*4d131170SRobert Mustacchi if (n->body != NULL &&
217*4d131170SRobert Mustacchi (nch = roff_node_child(n->body)) != NULL &&
218*4d131170SRobert Mustacchi nch->type == ROFFT_TBL)
219*4d131170SRobert Mustacchi return;
22095c635efSGarrett D'Amore
221*4d131170SRobert Mustacchi if (n->parent->tok != MAN_RS && roff_node_prev(n) == NULL)
222*4d131170SRobert Mustacchi return;
22395c635efSGarrett D'Amore
224698f87a4SGarrett D'Amore for (i = 0; i < pardist; i++)
225698f87a4SGarrett D'Amore term_vspace(p);
22695c635efSGarrett D'Amore }
22795c635efSGarrett D'Amore
228260e9a87SYuri Pankov
22995c635efSGarrett D'Amore static int
pre_abort(DECL_ARGS)230cec8643bSMichal Nowak pre_abort(DECL_ARGS)
23195c635efSGarrett D'Amore {
232cec8643bSMichal Nowak abort();
233cec8643bSMichal Nowak }
23495c635efSGarrett D'Amore
235cec8643bSMichal Nowak static int
pre_ign(DECL_ARGS)236cec8643bSMichal Nowak pre_ign(DECL_ARGS)
237cec8643bSMichal Nowak {
238371584c2SYuri Pankov return 0;
23995c635efSGarrett D'Amore }
24095c635efSGarrett D'Amore
24195c635efSGarrett D'Amore static int
pre_I(DECL_ARGS)24295c635efSGarrett D'Amore pre_I(DECL_ARGS)
24395c635efSGarrett D'Amore {
24495c635efSGarrett D'Amore term_fontrepl(p, TERMFONT_UNDER);
245371584c2SYuri Pankov return 1;
24695c635efSGarrett D'Amore }
24795c635efSGarrett D'Amore
24895c635efSGarrett D'Amore static int
pre_literal(DECL_ARGS)24995c635efSGarrett D'Amore pre_literal(DECL_ARGS)
25095c635efSGarrett D'Amore {
25195c635efSGarrett D'Amore term_newln(p);
25295c635efSGarrett D'Amore
25395c635efSGarrett D'Amore /*
25495c635efSGarrett D'Amore * Unlike .IP and .TP, .HP does not have a HEAD.
25595c635efSGarrett D'Amore * So in case a second call to term_flushln() is needed,
25695c635efSGarrett D'Amore * indentation has to be set up explicitly.
25795c635efSGarrett D'Amore */
258c66b8046SYuri Pankov if (n->parent->tok == MAN_HP && p->tcol->rmargin < p->maxrmargin) {
259c66b8046SYuri Pankov p->tcol->offset = p->tcol->rmargin;
260c66b8046SYuri Pankov p->tcol->rmargin = p->maxrmargin;
261698f87a4SGarrett D'Amore p->trailspace = 0;
262260e9a87SYuri Pankov p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
26395c635efSGarrett D'Amore p->flags |= TERMP_NOSPACE;
26495c635efSGarrett D'Amore }
265371584c2SYuri Pankov return 0;
26695c635efSGarrett D'Amore }
26795c635efSGarrett D'Amore
268698f87a4SGarrett D'Amore static int
pre_PD(DECL_ARGS)269698f87a4SGarrett D'Amore pre_PD(DECL_ARGS)
270698f87a4SGarrett D'Amore {
271260e9a87SYuri Pankov struct roffsu su;
272698f87a4SGarrett D'Amore
273698f87a4SGarrett D'Amore n = n->child;
274260e9a87SYuri Pankov if (n == NULL) {
275698f87a4SGarrett D'Amore mt->pardist = 1;
276371584c2SYuri Pankov return 0;
277698f87a4SGarrett D'Amore }
278371584c2SYuri Pankov assert(n->type == ROFFT_TEXT);
279c66b8046SYuri Pankov if (a2roffsu(n->string, &su, SCALE_VS) != NULL)
280260e9a87SYuri Pankov mt->pardist = term_vspan(p, &su);
281371584c2SYuri Pankov return 0;
282698f87a4SGarrett D'Amore }
283698f87a4SGarrett D'Amore
28495c635efSGarrett D'Amore static int
pre_alternate(DECL_ARGS)28595c635efSGarrett D'Amore pre_alternate(DECL_ARGS)
28695c635efSGarrett D'Amore {
28795c635efSGarrett D'Amore enum termfont font[2];
288371584c2SYuri Pankov struct roff_node *nn;
289cec8643bSMichal Nowak int i;
29095c635efSGarrett D'Amore
29195c635efSGarrett D'Amore switch (n->tok) {
292260e9a87SYuri Pankov case MAN_RB:
29395c635efSGarrett D'Amore font[0] = TERMFONT_NONE;
29495c635efSGarrett D'Amore font[1] = TERMFONT_BOLD;
29595c635efSGarrett D'Amore break;
296260e9a87SYuri Pankov case MAN_RI:
29795c635efSGarrett D'Amore font[0] = TERMFONT_NONE;
29895c635efSGarrett D'Amore font[1] = TERMFONT_UNDER;
29995c635efSGarrett D'Amore break;
300260e9a87SYuri Pankov case MAN_BR:
30195c635efSGarrett D'Amore font[0] = TERMFONT_BOLD;
30295c635efSGarrett D'Amore font[1] = TERMFONT_NONE;
30395c635efSGarrett D'Amore break;
304260e9a87SYuri Pankov case MAN_BI:
30595c635efSGarrett D'Amore font[0] = TERMFONT_BOLD;
30695c635efSGarrett D'Amore font[1] = TERMFONT_UNDER;
30795c635efSGarrett D'Amore break;
308260e9a87SYuri Pankov case MAN_IR:
30995c635efSGarrett D'Amore font[0] = TERMFONT_UNDER;
31095c635efSGarrett D'Amore font[1] = TERMFONT_NONE;
31195c635efSGarrett D'Amore break;
312260e9a87SYuri Pankov case MAN_IB:
31395c635efSGarrett D'Amore font[0] = TERMFONT_UNDER;
31495c635efSGarrett D'Amore font[1] = TERMFONT_BOLD;
31595c635efSGarrett D'Amore break;
31695c635efSGarrett D'Amore default:
31795c635efSGarrett D'Amore abort();
31895c635efSGarrett D'Amore }
319cec8643bSMichal Nowak for (i = 0, nn = n->child; nn != NULL; nn = nn->next, i = 1 - i) {
32095c635efSGarrett D'Amore term_fontrepl(p, font[i]);
321371584c2SYuri Pankov assert(nn->type == ROFFT_TEXT);
322371584c2SYuri Pankov term_word(p, nn->string);
323a40ea1a7SYuri Pankov if (nn->flags & NODE_EOS)
324cec8643bSMichal Nowak p->flags |= TERMP_SENTENCE;
325cec8643bSMichal Nowak if (nn->next != NULL)
32695c635efSGarrett D'Amore p->flags |= TERMP_NOSPACE;
32795c635efSGarrett D'Amore }
328371584c2SYuri Pankov return 0;
32995c635efSGarrett D'Amore }
33095c635efSGarrett D'Amore
33195c635efSGarrett D'Amore static int
pre_B(DECL_ARGS)33295c635efSGarrett D'Amore pre_B(DECL_ARGS)
33395c635efSGarrett D'Amore {
33495c635efSGarrett D'Amore term_fontrepl(p, TERMFONT_BOLD);
335371584c2SYuri Pankov return 1;
33695c635efSGarrett D'Amore }
33795c635efSGarrett D'Amore
33895c635efSGarrett D'Amore static int
pre_OP(DECL_ARGS)33995c635efSGarrett D'Amore pre_OP(DECL_ARGS)
34095c635efSGarrett D'Amore {
34195c635efSGarrett D'Amore term_word(p, "[");
342cec8643bSMichal Nowak p->flags |= TERMP_KEEP | TERMP_NOSPACE;
34395c635efSGarrett D'Amore
344cec8643bSMichal Nowak if ((n = n->child) != NULL) {
34595c635efSGarrett D'Amore term_fontrepl(p, TERMFONT_BOLD);
34695c635efSGarrett D'Amore term_word(p, n->string);
34795c635efSGarrett D'Amore }
348cec8643bSMichal Nowak if (n != NULL && n->next != NULL) {
34995c635efSGarrett D'Amore term_fontrepl(p, TERMFONT_UNDER);
35095c635efSGarrett D'Amore term_word(p, n->next->string);
35195c635efSGarrett D'Amore }
35295c635efSGarrett D'Amore term_fontrepl(p, TERMFONT_NONE);
353cec8643bSMichal Nowak p->flags &= ~TERMP_KEEP;
35495c635efSGarrett D'Amore p->flags |= TERMP_NOSPACE;
35595c635efSGarrett D'Amore term_word(p, "]");
356371584c2SYuri Pankov return 0;
35795c635efSGarrett D'Amore }
35895c635efSGarrett D'Amore
35995c635efSGarrett D'Amore static int
pre_in(DECL_ARGS)36095c635efSGarrett D'Amore pre_in(DECL_ARGS)
36195c635efSGarrett D'Amore {
362260e9a87SYuri Pankov struct roffsu su;
36395c635efSGarrett D'Amore const char *cp;
364260e9a87SYuri Pankov size_t v;
365260e9a87SYuri Pankov int less;
36695c635efSGarrett D'Amore
36795c635efSGarrett D'Amore term_newln(p);
36895c635efSGarrett D'Amore
369c66b8046SYuri Pankov if (n->child == NULL) {
370c66b8046SYuri Pankov p->tcol->offset = mt->offset;
371371584c2SYuri Pankov return 0;
37295c635efSGarrett D'Amore }
37395c635efSGarrett D'Amore
37495c635efSGarrett D'Amore cp = n->child->string;
37595c635efSGarrett D'Amore less = 0;
37695c635efSGarrett D'Amore
377cec8643bSMichal Nowak if (*cp == '-')
37895c635efSGarrett D'Amore less = -1;
379cec8643bSMichal Nowak else if (*cp == '+')
38095c635efSGarrett D'Amore less = 1;
38195c635efSGarrett D'Amore else
38295c635efSGarrett D'Amore cp--;
38395c635efSGarrett D'Amore
384c66b8046SYuri Pankov if (a2roffsu(++cp, &su, SCALE_EN) == NULL)
385371584c2SYuri Pankov return 0;
38695c635efSGarrett D'Amore
387c66b8046SYuri Pankov v = term_hen(p, &su);
38895c635efSGarrett D'Amore
38995c635efSGarrett D'Amore if (less < 0)
390c66b8046SYuri Pankov p->tcol->offset -= p->tcol->offset > v ? v : p->tcol->offset;
39195c635efSGarrett D'Amore else if (less > 0)
392c66b8046SYuri Pankov p->tcol->offset += v;
393260e9a87SYuri Pankov else
394c66b8046SYuri Pankov p->tcol->offset = v;
395c66b8046SYuri Pankov if (p->tcol->offset > SHRT_MAX)
396c66b8046SYuri Pankov p->tcol->offset = term_len(p, p->defindent);
39795c635efSGarrett D'Amore
398371584c2SYuri Pankov return 0;
39995c635efSGarrett D'Amore }
40095c635efSGarrett D'Amore
40195c635efSGarrett D'Amore static int
pre_DT(DECL_ARGS)402c66b8046SYuri Pankov pre_DT(DECL_ARGS)
40395c635efSGarrett D'Amore {
404c66b8046SYuri Pankov term_tab_set(p, NULL);
405c66b8046SYuri Pankov term_tab_set(p, "T");
406c66b8046SYuri Pankov term_tab_set(p, ".5i");
407371584c2SYuri Pankov return 0;
40895c635efSGarrett D'Amore }
40995c635efSGarrett D'Amore
41095c635efSGarrett D'Amore static int
pre_HP(DECL_ARGS)41195c635efSGarrett D'Amore pre_HP(DECL_ARGS)
41295c635efSGarrett D'Amore {
413260e9a87SYuri Pankov struct roffsu su;
414371584c2SYuri Pankov const struct roff_node *nn;
415260e9a87SYuri Pankov int len;
41695c635efSGarrett D'Amore
41795c635efSGarrett D'Amore switch (n->type) {
418371584c2SYuri Pankov case ROFFT_BLOCK:
419698f87a4SGarrett D'Amore print_bvspace(p, n, mt->pardist);
420371584c2SYuri Pankov return 1;
421cec8643bSMichal Nowak case ROFFT_HEAD:
422cec8643bSMichal Nowak return 0;
423371584c2SYuri Pankov case ROFFT_BODY:
42495c635efSGarrett D'Amore break;
42595c635efSGarrett D'Amore default:
426cec8643bSMichal Nowak abort();
42795c635efSGarrett D'Amore }
42895c635efSGarrett D'Amore
429cec8643bSMichal Nowak if (n->child == NULL)
430cec8643bSMichal Nowak return 0;
431cec8643bSMichal Nowak
432cec8643bSMichal Nowak if ((n->child->flags & NODE_NOFILL) == 0) {
433260e9a87SYuri Pankov p->flags |= TERMP_NOBREAK | TERMP_BRIND;
434698f87a4SGarrett D'Amore p->trailspace = 2;
435698f87a4SGarrett D'Amore }
436698f87a4SGarrett D'Amore
43795c635efSGarrett D'Amore /* Calculate offset. */
43895c635efSGarrett D'Amore
439260e9a87SYuri Pankov if ((nn = n->parent->head->child) != NULL &&
440c66b8046SYuri Pankov a2roffsu(nn->string, &su, SCALE_EN) != NULL) {
441c66b8046SYuri Pankov len = term_hen(p, &su);
442260e9a87SYuri Pankov if (len < 0 && (size_t)(-len) > mt->offset)
443260e9a87SYuri Pankov len = -mt->offset;
444260e9a87SYuri Pankov else if (len > SHRT_MAX)
445260e9a87SYuri Pankov len = term_len(p, p->defindent);
446260e9a87SYuri Pankov mt->lmargin[mt->lmargincur] = len;
447260e9a87SYuri Pankov } else
448260e9a87SYuri Pankov len = mt->lmargin[mt->lmargincur];
44995c635efSGarrett D'Amore
450c66b8046SYuri Pankov p->tcol->offset = mt->offset;
451c66b8046SYuri Pankov p->tcol->rmargin = mt->offset + len;
452371584c2SYuri Pankov return 1;
45395c635efSGarrett D'Amore }
45495c635efSGarrett D'Amore
45595c635efSGarrett D'Amore static void
post_HP(DECL_ARGS)45695c635efSGarrett D'Amore post_HP(DECL_ARGS)
45795c635efSGarrett D'Amore {
45895c635efSGarrett D'Amore switch (n->type) {
459cec8643bSMichal Nowak case ROFFT_BLOCK:
460cec8643bSMichal Nowak case ROFFT_HEAD:
461cec8643bSMichal Nowak break;
462371584c2SYuri Pankov case ROFFT_BODY:
463698f87a4SGarrett D'Amore term_newln(p);
464371584c2SYuri Pankov
465371584c2SYuri Pankov /*
466371584c2SYuri Pankov * Compatibility with a groff bug.
467371584c2SYuri Pankov * The .HP macro uses the undocumented .tag request
468371584c2SYuri Pankov * which causes a line break and cancels no-space
469371584c2SYuri Pankov * mode even if there isn't any output.
470371584c2SYuri Pankov */
471371584c2SYuri Pankov
472371584c2SYuri Pankov if (n->child == NULL)
473371584c2SYuri Pankov term_vspace(p);
474371584c2SYuri Pankov
475260e9a87SYuri Pankov p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
476698f87a4SGarrett D'Amore p->trailspace = 0;
477c66b8046SYuri Pankov p->tcol->offset = mt->offset;
478c66b8046SYuri Pankov p->tcol->rmargin = p->maxrmargin;
47995c635efSGarrett D'Amore break;
48095c635efSGarrett D'Amore default:
481cec8643bSMichal Nowak abort();
48295c635efSGarrett D'Amore }
48395c635efSGarrett D'Amore }
48495c635efSGarrett D'Amore
48595c635efSGarrett D'Amore static int
pre_PP(DECL_ARGS)48695c635efSGarrett D'Amore pre_PP(DECL_ARGS)
48795c635efSGarrett D'Amore {
48895c635efSGarrett D'Amore switch (n->type) {
489371584c2SYuri Pankov case ROFFT_BLOCK:
49095c635efSGarrett D'Amore mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
491698f87a4SGarrett D'Amore print_bvspace(p, n, mt->pardist);
49295c635efSGarrett D'Amore break;
493cec8643bSMichal Nowak case ROFFT_HEAD:
494cec8643bSMichal Nowak return 0;
495cec8643bSMichal Nowak case ROFFT_BODY:
496c66b8046SYuri Pankov p->tcol->offset = mt->offset;
49795c635efSGarrett D'Amore break;
498cec8643bSMichal Nowak default:
499cec8643bSMichal Nowak abort();
50095c635efSGarrett D'Amore }
501cec8643bSMichal Nowak return 1;
50295c635efSGarrett D'Amore }
50395c635efSGarrett D'Amore
50495c635efSGarrett D'Amore static int
pre_IP(DECL_ARGS)50595c635efSGarrett D'Amore pre_IP(DECL_ARGS)
50695c635efSGarrett D'Amore {
507260e9a87SYuri Pankov struct roffsu su;
508371584c2SYuri Pankov const struct roff_node *nn;
509cec8643bSMichal Nowak int len;
51095c635efSGarrett D'Amore
51195c635efSGarrett D'Amore switch (n->type) {
512cec8643bSMichal Nowak case ROFFT_BLOCK:
513cec8643bSMichal Nowak print_bvspace(p, n, mt->pardist);
514cec8643bSMichal Nowak return 1;
515371584c2SYuri Pankov case ROFFT_HEAD:
51695c635efSGarrett D'Amore p->flags |= TERMP_NOBREAK;
517698f87a4SGarrett D'Amore p->trailspace = 1;
51895c635efSGarrett D'Amore break;
519cec8643bSMichal Nowak case ROFFT_BODY:
520cec8643bSMichal Nowak p->flags |= TERMP_NOSPACE;
521cec8643bSMichal Nowak break;
52295c635efSGarrett D'Amore default:
523cec8643bSMichal Nowak abort();
52495c635efSGarrett D'Amore }
52595c635efSGarrett D'Amore
52695c635efSGarrett D'Amore /* Calculate the offset from the optional second argument. */
527260e9a87SYuri Pankov if ((nn = n->parent->head->child) != NULL &&
528260e9a87SYuri Pankov (nn = nn->next) != NULL &&
529c66b8046SYuri Pankov a2roffsu(nn->string, &su, SCALE_EN) != NULL) {
530c66b8046SYuri Pankov len = term_hen(p, &su);
531260e9a87SYuri Pankov if (len < 0 && (size_t)(-len) > mt->offset)
532260e9a87SYuri Pankov len = -mt->offset;
533260e9a87SYuri Pankov else if (len > SHRT_MAX)
534260e9a87SYuri Pankov len = term_len(p, p->defindent);
535260e9a87SYuri Pankov mt->lmargin[mt->lmargincur] = len;
536260e9a87SYuri Pankov } else
537260e9a87SYuri Pankov len = mt->lmargin[mt->lmargincur];
53895c635efSGarrett D'Amore
53995c635efSGarrett D'Amore switch (n->type) {
540371584c2SYuri Pankov case ROFFT_HEAD:
541c66b8046SYuri Pankov p->tcol->offset = mt->offset;
542c66b8046SYuri Pankov p->tcol->rmargin = mt->offset + len;
543cec8643bSMichal Nowak if (n->child != NULL)
544698f87a4SGarrett D'Amore print_man_node(p, mt, n->child, meta);
545371584c2SYuri Pankov return 0;
546371584c2SYuri Pankov case ROFFT_BODY:
547c66b8046SYuri Pankov p->tcol->offset = mt->offset + len;
548c66b8046SYuri Pankov p->tcol->rmargin = p->maxrmargin;
54995c635efSGarrett D'Amore break;
55095c635efSGarrett D'Amore default:
551cec8643bSMichal Nowak abort();
55295c635efSGarrett D'Amore }
553371584c2SYuri Pankov return 1;
55495c635efSGarrett D'Amore }
55595c635efSGarrett D'Amore
55695c635efSGarrett D'Amore static void
post_IP(DECL_ARGS)55795c635efSGarrett D'Amore post_IP(DECL_ARGS)
55895c635efSGarrett D'Amore {
55995c635efSGarrett D'Amore switch (n->type) {
560cec8643bSMichal Nowak case ROFFT_BLOCK:
561cec8643bSMichal Nowak break;
562371584c2SYuri Pankov case ROFFT_HEAD:
56395c635efSGarrett D'Amore term_flushln(p);
56495c635efSGarrett D'Amore p->flags &= ~TERMP_NOBREAK;
565698f87a4SGarrett D'Amore p->trailspace = 0;
566c66b8046SYuri Pankov p->tcol->rmargin = p->maxrmargin;
56795c635efSGarrett D'Amore break;
568371584c2SYuri Pankov case ROFFT_BODY:
56995c635efSGarrett D'Amore term_newln(p);
570c66b8046SYuri Pankov p->tcol->offset = mt->offset;
57195c635efSGarrett D'Amore break;
57295c635efSGarrett D'Amore default:
573cec8643bSMichal Nowak abort();
57495c635efSGarrett D'Amore }
57595c635efSGarrett D'Amore }
57695c635efSGarrett D'Amore
57795c635efSGarrett D'Amore static int
pre_TP(DECL_ARGS)57895c635efSGarrett D'Amore pre_TP(DECL_ARGS)
57995c635efSGarrett D'Amore {
580260e9a87SYuri Pankov struct roffsu su;
581371584c2SYuri Pankov struct roff_node *nn;
582cec8643bSMichal Nowak int len;
58395c635efSGarrett D'Amore
58495c635efSGarrett D'Amore switch (n->type) {
585cec8643bSMichal Nowak case ROFFT_BLOCK:
586cec8643bSMichal Nowak if (n->tok == MAN_TP)
587cec8643bSMichal Nowak print_bvspace(p, n, mt->pardist);
588cec8643bSMichal Nowak return 1;
589371584c2SYuri Pankov case ROFFT_HEAD:
590371584c2SYuri Pankov p->flags |= TERMP_NOBREAK | TERMP_BRTRSP;
591698f87a4SGarrett D'Amore p->trailspace = 1;
59295c635efSGarrett D'Amore break;
593371584c2SYuri Pankov case ROFFT_BODY:
59495c635efSGarrett D'Amore p->flags |= TERMP_NOSPACE;
59595c635efSGarrett D'Amore break;
59695c635efSGarrett D'Amore default:
597cec8643bSMichal Nowak abort();
59895c635efSGarrett D'Amore }
59995c635efSGarrett D'Amore
60095c635efSGarrett D'Amore /* Calculate offset. */
60195c635efSGarrett D'Amore
602260e9a87SYuri Pankov if ((nn = n->parent->head->child) != NULL &&
603a40ea1a7SYuri Pankov nn->string != NULL && ! (NODE_LINE & nn->flags) &&
604c66b8046SYuri Pankov a2roffsu(nn->string, &su, SCALE_EN) != NULL) {
605c66b8046SYuri Pankov len = term_hen(p, &su);
606260e9a87SYuri Pankov if (len < 0 && (size_t)(-len) > mt->offset)
607260e9a87SYuri Pankov len = -mt->offset;
608260e9a87SYuri Pankov else if (len > SHRT_MAX)
609260e9a87SYuri Pankov len = term_len(p, p->defindent);
610260e9a87SYuri Pankov mt->lmargin[mt->lmargincur] = len;
611260e9a87SYuri Pankov } else
612260e9a87SYuri Pankov len = mt->lmargin[mt->lmargincur];
61395c635efSGarrett D'Amore
61495c635efSGarrett D'Amore switch (n->type) {
615371584c2SYuri Pankov case ROFFT_HEAD:
616c66b8046SYuri Pankov p->tcol->offset = mt->offset;
617c66b8046SYuri Pankov p->tcol->rmargin = mt->offset + len;
61895c635efSGarrett D'Amore
61995c635efSGarrett D'Amore /* Don't print same-line elements. */
620260e9a87SYuri Pankov nn = n->child;
621cec8643bSMichal Nowak while (nn != NULL && (nn->flags & NODE_LINE) == 0)
622260e9a87SYuri Pankov nn = nn->next;
623260e9a87SYuri Pankov
624cec8643bSMichal Nowak while (nn != NULL) {
625260e9a87SYuri Pankov print_man_node(p, mt, nn, meta);
626260e9a87SYuri Pankov nn = nn->next;
627260e9a87SYuri Pankov }
628371584c2SYuri Pankov return 0;
629371584c2SYuri Pankov case ROFFT_BODY:
630c66b8046SYuri Pankov p->tcol->offset = mt->offset + len;
631c66b8046SYuri Pankov p->tcol->rmargin = p->maxrmargin;
632698f87a4SGarrett D'Amore p->trailspace = 0;
633371584c2SYuri Pankov p->flags &= ~(TERMP_NOBREAK | TERMP_BRTRSP);
63495c635efSGarrett D'Amore break;
63595c635efSGarrett D'Amore default:
636cec8643bSMichal Nowak abort();
63795c635efSGarrett D'Amore }
638371584c2SYuri Pankov return 1;
63995c635efSGarrett D'Amore }
64095c635efSGarrett D'Amore
64195c635efSGarrett D'Amore static void
post_TP(DECL_ARGS)64295c635efSGarrett D'Amore post_TP(DECL_ARGS)
64395c635efSGarrett D'Amore {
64495c635efSGarrett D'Amore switch (n->type) {
645cec8643bSMichal Nowak case ROFFT_BLOCK:
646cec8643bSMichal Nowak break;
647371584c2SYuri Pankov case ROFFT_HEAD:
64895c635efSGarrett D'Amore term_flushln(p);
64995c635efSGarrett D'Amore break;
650371584c2SYuri Pankov case ROFFT_BODY:
65195c635efSGarrett D'Amore term_newln(p);
652c66b8046SYuri Pankov p->tcol->offset = mt->offset;
65395c635efSGarrett D'Amore break;
65495c635efSGarrett D'Amore default:
655cec8643bSMichal Nowak abort();
65695c635efSGarrett D'Amore }
65795c635efSGarrett D'Amore }
65895c635efSGarrett D'Amore
65995c635efSGarrett D'Amore static int
pre_SS(DECL_ARGS)66095c635efSGarrett D'Amore pre_SS(DECL_ARGS)
66195c635efSGarrett D'Amore {
662698f87a4SGarrett D'Amore int i;
66395c635efSGarrett D'Amore
66495c635efSGarrett D'Amore switch (n->type) {
665371584c2SYuri Pankov case ROFFT_BLOCK:
66695c635efSGarrett D'Amore mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
66795c635efSGarrett D'Amore mt->offset = term_len(p, p->defindent);
668260e9a87SYuri Pankov
669260e9a87SYuri Pankov /*
670260e9a87SYuri Pankov * No vertical space before the first subsection
671260e9a87SYuri Pankov * and after an empty subsection.
672260e9a87SYuri Pankov */
673260e9a87SYuri Pankov
674*4d131170SRobert Mustacchi if ((n = roff_node_prev(n)) == NULL ||
675*4d131170SRobert Mustacchi (n->tok == MAN_SS && roff_node_child(n->body) == NULL))
67695c635efSGarrett D'Amore break;
677260e9a87SYuri Pankov
678698f87a4SGarrett D'Amore for (i = 0; i < mt->pardist; i++)
679698f87a4SGarrett D'Amore term_vspace(p);
68095c635efSGarrett D'Amore break;
681371584c2SYuri Pankov case ROFFT_HEAD:
68295c635efSGarrett D'Amore term_fontrepl(p, TERMFONT_BOLD);
683c66b8046SYuri Pankov p->tcol->offset = term_len(p, 3);
684c66b8046SYuri Pankov p->tcol->rmargin = mt->offset;
685371584c2SYuri Pankov p->trailspace = mt->offset;
686371584c2SYuri Pankov p->flags |= TERMP_NOBREAK | TERMP_BRIND;
68795c635efSGarrett D'Amore break;
688371584c2SYuri Pankov case ROFFT_BODY:
689c66b8046SYuri Pankov p->tcol->offset = mt->offset;
690c66b8046SYuri Pankov p->tcol->rmargin = p->maxrmargin;
691371584c2SYuri Pankov p->trailspace = 0;
692371584c2SYuri Pankov p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
69395c635efSGarrett D'Amore break;
69495c635efSGarrett D'Amore default:
69595c635efSGarrett D'Amore break;
69695c635efSGarrett D'Amore }
697371584c2SYuri Pankov return 1;
69895c635efSGarrett D'Amore }
69995c635efSGarrett D'Amore
70095c635efSGarrett D'Amore static int
pre_SH(DECL_ARGS)70195c635efSGarrett D'Amore pre_SH(DECL_ARGS)
70295c635efSGarrett D'Amore {
703698f87a4SGarrett D'Amore int i;
70495c635efSGarrett D'Amore
70595c635efSGarrett D'Amore switch (n->type) {
706371584c2SYuri Pankov case ROFFT_BLOCK:
70795c635efSGarrett D'Amore mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
70895c635efSGarrett D'Amore mt->offset = term_len(p, p->defindent);
709260e9a87SYuri Pankov
710260e9a87SYuri Pankov /*
711260e9a87SYuri Pankov * No vertical space before the first section
712260e9a87SYuri Pankov * and after an empty section.
713260e9a87SYuri Pankov */
714260e9a87SYuri Pankov
715*4d131170SRobert Mustacchi if ((n = roff_node_prev(n)) == NULL ||
716*4d131170SRobert Mustacchi (n->tok == MAN_SH && roff_node_child(n->body) == NULL))
71795c635efSGarrett D'Amore break;
718260e9a87SYuri Pankov
719698f87a4SGarrett D'Amore for (i = 0; i < mt->pardist; i++)
720698f87a4SGarrett D'Amore term_vspace(p);
72195c635efSGarrett D'Amore break;
722371584c2SYuri Pankov case ROFFT_HEAD:
72395c635efSGarrett D'Amore term_fontrepl(p, TERMFONT_BOLD);
724c66b8046SYuri Pankov p->tcol->offset = 0;
725c66b8046SYuri Pankov p->tcol->rmargin = mt->offset;
726371584c2SYuri Pankov p->trailspace = mt->offset;
727371584c2SYuri Pankov p->flags |= TERMP_NOBREAK | TERMP_BRIND;
72895c635efSGarrett D'Amore break;
729371584c2SYuri Pankov case ROFFT_BODY:
730c66b8046SYuri Pankov p->tcol->offset = mt->offset;
731c66b8046SYuri Pankov p->tcol->rmargin = p->maxrmargin;
732371584c2SYuri Pankov p->trailspace = 0;
733371584c2SYuri Pankov p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
73495c635efSGarrett D'Amore break;
73595c635efSGarrett D'Amore default:
736cec8643bSMichal Nowak abort();
73795c635efSGarrett D'Amore }
738371584c2SYuri Pankov return 1;
73995c635efSGarrett D'Amore }
74095c635efSGarrett D'Amore
74195c635efSGarrett D'Amore static void
post_SH(DECL_ARGS)74295c635efSGarrett D'Amore post_SH(DECL_ARGS)
74395c635efSGarrett D'Amore {
74495c635efSGarrett D'Amore switch (n->type) {
745cec8643bSMichal Nowak case ROFFT_BLOCK:
74695c635efSGarrett D'Amore break;
747cec8643bSMichal Nowak case ROFFT_HEAD:
748371584c2SYuri Pankov case ROFFT_BODY:
74995c635efSGarrett D'Amore term_newln(p);
75095c635efSGarrett D'Amore break;
75195c635efSGarrett D'Amore default:
752cec8643bSMichal Nowak abort();
75395c635efSGarrett D'Amore }
75495c635efSGarrett D'Amore }
75595c635efSGarrett D'Amore
75695c635efSGarrett D'Amore static int
pre_RS(DECL_ARGS)75795c635efSGarrett D'Amore pre_RS(DECL_ARGS)
75895c635efSGarrett D'Amore {
759260e9a87SYuri Pankov struct roffsu su;
76095c635efSGarrett D'Amore
76195c635efSGarrett D'Amore switch (n->type) {
762371584c2SYuri Pankov case ROFFT_BLOCK:
76395c635efSGarrett D'Amore term_newln(p);
764371584c2SYuri Pankov return 1;
765371584c2SYuri Pankov case ROFFT_HEAD:
766371584c2SYuri Pankov return 0;
767cec8643bSMichal Nowak case ROFFT_BODY:
76895c635efSGarrett D'Amore break;
769cec8643bSMichal Nowak default:
770cec8643bSMichal Nowak abort();
77195c635efSGarrett D'Amore }
77295c635efSGarrett D'Amore
773260e9a87SYuri Pankov n = n->parent->head;
774260e9a87SYuri Pankov n->aux = SHRT_MAX + 1;
775371584c2SYuri Pankov if (n->child == NULL)
776371584c2SYuri Pankov n->aux = mt->lmargin[mt->lmargincur];
777c66b8046SYuri Pankov else if (a2roffsu(n->child->string, &su, SCALE_EN) != NULL)
778c66b8046SYuri Pankov n->aux = term_hen(p, &su);
779260e9a87SYuri Pankov if (n->aux < 0 && (size_t)(-n->aux) > mt->offset)
780260e9a87SYuri Pankov n->aux = -mt->offset;
781260e9a87SYuri Pankov else if (n->aux > SHRT_MAX)
782260e9a87SYuri Pankov n->aux = term_len(p, p->defindent);
78395c635efSGarrett D'Amore
784260e9a87SYuri Pankov mt->offset += n->aux;
785c66b8046SYuri Pankov p->tcol->offset = mt->offset;
786c66b8046SYuri Pankov p->tcol->rmargin = p->maxrmargin;
78795c635efSGarrett D'Amore
78895c635efSGarrett D'Amore if (++mt->lmarginsz < MAXMARGINS)
78995c635efSGarrett D'Amore mt->lmargincur = mt->lmarginsz;
79095c635efSGarrett D'Amore
791371584c2SYuri Pankov mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
792371584c2SYuri Pankov return 1;
79395c635efSGarrett D'Amore }
79495c635efSGarrett D'Amore
79595c635efSGarrett D'Amore static void
post_RS(DECL_ARGS)79695c635efSGarrett D'Amore post_RS(DECL_ARGS)
79795c635efSGarrett D'Amore {
79895c635efSGarrett D'Amore switch (n->type) {
799371584c2SYuri Pankov case ROFFT_BLOCK:
800371584c2SYuri Pankov case ROFFT_HEAD:
80195c635efSGarrett D'Amore return;
802cec8643bSMichal Nowak case ROFFT_BODY:
80395c635efSGarrett D'Amore break;
804cec8643bSMichal Nowak default:
805cec8643bSMichal Nowak abort();
80695c635efSGarrett D'Amore }
807cec8643bSMichal Nowak term_newln(p);
808260e9a87SYuri Pankov mt->offset -= n->parent->head->aux;
809c66b8046SYuri Pankov p->tcol->offset = mt->offset;
81095c635efSGarrett D'Amore if (--mt->lmarginsz < MAXMARGINS)
81195c635efSGarrett D'Amore mt->lmargincur = mt->lmarginsz;
81295c635efSGarrett D'Amore }
81395c635efSGarrett D'Amore
814698f87a4SGarrett D'Amore static int
pre_SY(DECL_ARGS)815cec8643bSMichal Nowak pre_SY(DECL_ARGS)
816cec8643bSMichal Nowak {
817cec8643bSMichal Nowak const struct roff_node *nn;
818cec8643bSMichal Nowak int len;
819cec8643bSMichal Nowak
820cec8643bSMichal Nowak switch (n->type) {
821cec8643bSMichal Nowak case ROFFT_BLOCK:
822*4d131170SRobert Mustacchi if ((nn = roff_node_prev(n)) == NULL || nn->tok != MAN_SY)
823cec8643bSMichal Nowak print_bvspace(p, n, mt->pardist);
824cec8643bSMichal Nowak return 1;
825cec8643bSMichal Nowak case ROFFT_HEAD:
826cec8643bSMichal Nowak case ROFFT_BODY:
827cec8643bSMichal Nowak break;
828cec8643bSMichal Nowak default:
829cec8643bSMichal Nowak abort();
830cec8643bSMichal Nowak }
831cec8643bSMichal Nowak
832cec8643bSMichal Nowak nn = n->parent->head->child;
833cec8643bSMichal Nowak len = nn == NULL ? 1 : term_strlen(p, nn->string) + 1;
834cec8643bSMichal Nowak
835cec8643bSMichal Nowak switch (n->type) {
836cec8643bSMichal Nowak case ROFFT_HEAD:
837cec8643bSMichal Nowak p->tcol->offset = mt->offset;
838cec8643bSMichal Nowak p->tcol->rmargin = mt->offset + len;
839cec8643bSMichal Nowak if (n->next->child == NULL ||
840cec8643bSMichal Nowak (n->next->child->flags & NODE_NOFILL) == 0)
841cec8643bSMichal Nowak p->flags |= TERMP_NOBREAK;
842cec8643bSMichal Nowak term_fontrepl(p, TERMFONT_BOLD);
843cec8643bSMichal Nowak break;
844cec8643bSMichal Nowak case ROFFT_BODY:
845cec8643bSMichal Nowak mt->lmargin[mt->lmargincur] = len;
846cec8643bSMichal Nowak p->tcol->offset = mt->offset + len;
847cec8643bSMichal Nowak p->tcol->rmargin = p->maxrmargin;
848cec8643bSMichal Nowak p->flags |= TERMP_NOSPACE;
849cec8643bSMichal Nowak break;
850cec8643bSMichal Nowak default:
851cec8643bSMichal Nowak abort();
852cec8643bSMichal Nowak }
853cec8643bSMichal Nowak return 1;
854cec8643bSMichal Nowak }
855cec8643bSMichal Nowak
856cec8643bSMichal Nowak static void
post_SY(DECL_ARGS)857cec8643bSMichal Nowak post_SY(DECL_ARGS)
858698f87a4SGarrett D'Amore {
859cec8643bSMichal Nowak switch (n->type) {
860cec8643bSMichal Nowak case ROFFT_BLOCK:
861cec8643bSMichal Nowak break;
862cec8643bSMichal Nowak case ROFFT_HEAD:
863cec8643bSMichal Nowak term_flushln(p);
864cec8643bSMichal Nowak p->flags &= ~TERMP_NOBREAK;
865cec8643bSMichal Nowak break;
866cec8643bSMichal Nowak case ROFFT_BODY:
867cec8643bSMichal Nowak term_newln(p);
868cec8643bSMichal Nowak p->tcol->offset = mt->offset;
869cec8643bSMichal Nowak break;
870cec8643bSMichal Nowak default:
871cec8643bSMichal Nowak abort();
872cec8643bSMichal Nowak }
873cec8643bSMichal Nowak }
874698f87a4SGarrett D'Amore
875cec8643bSMichal Nowak static int
pre_UR(DECL_ARGS)876cec8643bSMichal Nowak pre_UR(DECL_ARGS)
877cec8643bSMichal Nowak {
878371584c2SYuri Pankov return n->type != ROFFT_HEAD;
879698f87a4SGarrett D'Amore }
880698f87a4SGarrett D'Amore
881698f87a4SGarrett D'Amore static void
post_UR(DECL_ARGS)882698f87a4SGarrett D'Amore post_UR(DECL_ARGS)
883698f87a4SGarrett D'Amore {
884371584c2SYuri Pankov if (n->type != ROFFT_BLOCK)
885698f87a4SGarrett D'Amore return;
886698f87a4SGarrett D'Amore
887698f87a4SGarrett D'Amore term_word(p, "<");
888698f87a4SGarrett D'Amore p->flags |= TERMP_NOSPACE;
889698f87a4SGarrett D'Amore
890cec8643bSMichal Nowak if (n->child->child != NULL)
891698f87a4SGarrett D'Amore print_man_node(p, mt, n->child->child, meta);
892698f87a4SGarrett D'Amore
893698f87a4SGarrett D'Amore p->flags |= TERMP_NOSPACE;
894698f87a4SGarrett D'Amore term_word(p, ">");
895698f87a4SGarrett D'Amore }
896698f87a4SGarrett D'Amore
89795c635efSGarrett D'Amore static void
print_man_node(DECL_ARGS)89895c635efSGarrett D'Amore print_man_node(DECL_ARGS)
89995c635efSGarrett D'Amore {
900cec8643bSMichal Nowak const struct man_term_act *act;
901cec8643bSMichal Nowak int c;
90295c635efSGarrett D'Amore
903*4d131170SRobert Mustacchi if (n->flags & NODE_ID)
904*4d131170SRobert Mustacchi term_tag_write(n, p->line);
905*4d131170SRobert Mustacchi
90695c635efSGarrett D'Amore switch (n->type) {
907371584c2SYuri Pankov case ROFFT_TEXT:
90895c635efSGarrett D'Amore /*
90995c635efSGarrett D'Amore * If we have a blank line, output a vertical space.
91095c635efSGarrett D'Amore * If we have a space as the first character, break
91195c635efSGarrett D'Amore * before printing the line's data.
91295c635efSGarrett D'Amore */
913c66b8046SYuri Pankov if (*n->string == '\0') {
914c66b8046SYuri Pankov if (p->flags & TERMP_NONEWLINE)
915c66b8046SYuri Pankov term_newln(p);
916c66b8046SYuri Pankov else
917c66b8046SYuri Pankov term_vspace(p);
91895c635efSGarrett D'Amore return;
919c66b8046SYuri Pankov } else if (*n->string == ' ' && n->flags & NODE_LINE &&
920c66b8046SYuri Pankov (p->flags & TERMP_NONEWLINE) == 0)
92195c635efSGarrett D'Amore term_newln(p);
922cec8643bSMichal Nowak else if (n->flags & NODE_DELIMC)
923cec8643bSMichal Nowak p->flags |= TERMP_NOSPACE;
92495c635efSGarrett D'Amore
92595c635efSGarrett D'Amore term_word(p, n->string);
926698f87a4SGarrett D'Amore goto out;
9276640c13bSYuri Pankov case ROFFT_COMMENT:
9286640c13bSYuri Pankov return;
929371584c2SYuri Pankov case ROFFT_EQN:
930a40ea1a7SYuri Pankov if ( ! (n->flags & NODE_LINE))
931260e9a87SYuri Pankov p->flags |= TERMP_NOSPACE;
93295c635efSGarrett D'Amore term_eqn(p, n->eqn);
933a40ea1a7SYuri Pankov if (n->next != NULL && ! (n->next->flags & NODE_LINE))
934260e9a87SYuri Pankov p->flags |= TERMP_NOSPACE;
93595c635efSGarrett D'Amore return;
936371584c2SYuri Pankov case ROFFT_TBL:
937260e9a87SYuri Pankov if (p->tbl.cols == NULL)
938260e9a87SYuri Pankov term_vspace(p);
93995c635efSGarrett D'Amore term_tbl(p, n->span);
94095c635efSGarrett D'Amore return;
94195c635efSGarrett D'Amore default:
94295c635efSGarrett D'Amore break;
94395c635efSGarrett D'Amore }
94495c635efSGarrett D'Amore
945c66b8046SYuri Pankov if (n->tok < ROFF_MAX) {
946c66b8046SYuri Pankov roff_term_pre(p, n);
947c66b8046SYuri Pankov return;
948c66b8046SYuri Pankov }
949c66b8046SYuri Pankov
950cec8643bSMichal Nowak act = man_term_act(n->tok);
951cec8643bSMichal Nowak if ((act->flags & MAN_NOTEXT) == 0 && n->tok != MAN_SM)
95295c635efSGarrett D'Amore term_fontrepl(p, TERMFONT_NONE);
95395c635efSGarrett D'Amore
95495c635efSGarrett D'Amore c = 1;
955cec8643bSMichal Nowak if (act->pre != NULL)
956cec8643bSMichal Nowak c = (*act->pre)(p, mt, n, meta);
95795c635efSGarrett D'Amore
958cec8643bSMichal Nowak if (c && n->child != NULL)
959698f87a4SGarrett D'Amore print_man_nodelist(p, mt, n->child, meta);
96095c635efSGarrett D'Amore
961cec8643bSMichal Nowak if (act->post != NULL)
962cec8643bSMichal Nowak (*act->post)(p, mt, n, meta);
963cec8643bSMichal Nowak if ((act->flags & MAN_NOTEXT) == 0 && n->tok != MAN_SM)
96495c635efSGarrett D'Amore term_fontrepl(p, TERMFONT_NONE);
96595c635efSGarrett D'Amore
966698f87a4SGarrett D'Amore out:
967698f87a4SGarrett D'Amore /*
968698f87a4SGarrett D'Amore * If we're in a literal context, make sure that words
969698f87a4SGarrett D'Amore * together on the same line stay together. This is a
970698f87a4SGarrett D'Amore * POST-printing call, so we check the NEXT word. Since
971698f87a4SGarrett D'Amore * -man doesn't have nested macros, we don't need to be
972698f87a4SGarrett D'Amore * more specific than this.
973698f87a4SGarrett D'Amore */
974cec8643bSMichal Nowak if (n->flags & NODE_NOFILL &&
975260e9a87SYuri Pankov ! (p->flags & (TERMP_NOBREAK | TERMP_NONEWLINE)) &&
976a40ea1a7SYuri Pankov (n->next == NULL || n->next->flags & NODE_LINE)) {
977c66b8046SYuri Pankov p->flags |= TERMP_BRNEVER | TERMP_NOSPACE;
978260e9a87SYuri Pankov if (n->string != NULL && *n->string != '\0')
979698f87a4SGarrett D'Amore term_flushln(p);
980698f87a4SGarrett D'Amore else
981698f87a4SGarrett D'Amore term_newln(p);
982c66b8046SYuri Pankov p->flags &= ~TERMP_BRNEVER;
983c66b8046SYuri Pankov if (p->tcol->rmargin < p->maxrmargin &&
984c66b8046SYuri Pankov n->parent->tok == MAN_HP) {
985c66b8046SYuri Pankov p->tcol->offset = p->tcol->rmargin;
986c66b8046SYuri Pankov p->tcol->rmargin = p->maxrmargin;
987c66b8046SYuri Pankov }
988698f87a4SGarrett D'Amore }
989cec8643bSMichal Nowak if (n->flags & NODE_EOS)
99095c635efSGarrett D'Amore p->flags |= TERMP_SENTENCE;
99195c635efSGarrett D'Amore }
99295c635efSGarrett D'Amore
99395c635efSGarrett D'Amore static void
print_man_nodelist(DECL_ARGS)99495c635efSGarrett D'Amore print_man_nodelist(DECL_ARGS)
99595c635efSGarrett D'Amore {
996260e9a87SYuri Pankov while (n != NULL) {
997260e9a87SYuri Pankov print_man_node(p, mt, n, meta);
998260e9a87SYuri Pankov n = n->next;
999260e9a87SYuri Pankov }
100095c635efSGarrett D'Amore }
100195c635efSGarrett D'Amore
100295c635efSGarrett D'Amore static void
print_man_foot(struct termp * p,const struct roff_meta * meta)1003371584c2SYuri Pankov print_man_foot(struct termp *p, const struct roff_meta *meta)
100495c635efSGarrett D'Amore {
1005260e9a87SYuri Pankov char *title;
1006260e9a87SYuri Pankov size_t datelen, titlen;
100795c635efSGarrett D'Amore
100895c635efSGarrett D'Amore assert(meta->title);
100995c635efSGarrett D'Amore assert(meta->msec);
101095c635efSGarrett D'Amore assert(meta->date);
101195c635efSGarrett D'Amore
101295c635efSGarrett D'Amore term_fontrepl(p, TERMFONT_NONE);
101395c635efSGarrett D'Amore
1014260e9a87SYuri Pankov if (meta->hasbody)
1015260e9a87SYuri Pankov term_vspace(p);
101695c635efSGarrett D'Amore
101795c635efSGarrett D'Amore /*
101895c635efSGarrett D'Amore * Temporary, undocumented option to imitate mdoc(7) output.
1019371584c2SYuri Pankov * In the bottom right corner, use the operating system
1020371584c2SYuri Pankov * instead of the title.
102195c635efSGarrett D'Amore */
102295c635efSGarrett D'Amore
102395c635efSGarrett D'Amore if ( ! p->mdocstyle) {
1024260e9a87SYuri Pankov mandoc_asprintf(&title, "%s(%s)",
1025260e9a87SYuri Pankov meta->title, meta->msec);
1026cec8643bSMichal Nowak } else if (meta->os != NULL) {
1027371584c2SYuri Pankov title = mandoc_strdup(meta->os);
102895c635efSGarrett D'Amore } else {
1029260e9a87SYuri Pankov title = mandoc_strdup("");
103095c635efSGarrett D'Amore }
103195c635efSGarrett D'Amore datelen = term_strlen(p, meta->date);
103295c635efSGarrett D'Amore
1033371584c2SYuri Pankov /* Bottom left corner: operating system. */
103495c635efSGarrett D'Amore
103595c635efSGarrett D'Amore p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
1036698f87a4SGarrett D'Amore p->trailspace = 1;
1037c66b8046SYuri Pankov p->tcol->offset = 0;
1038c66b8046SYuri Pankov p->tcol->rmargin = p->maxrmargin > datelen ?
1039260e9a87SYuri Pankov (p->maxrmargin + term_len(p, 1) - datelen) / 2 : 0;
104095c635efSGarrett D'Amore
1041371584c2SYuri Pankov if (meta->os)
1042371584c2SYuri Pankov term_word(p, meta->os);
104395c635efSGarrett D'Amore term_flushln(p);
104495c635efSGarrett D'Amore
104595c635efSGarrett D'Amore /* At the bottom in the middle: manual date. */
104695c635efSGarrett D'Amore
1047c66b8046SYuri Pankov p->tcol->offset = p->tcol->rmargin;
1048260e9a87SYuri Pankov titlen = term_strlen(p, title);
1049c66b8046SYuri Pankov p->tcol->rmargin = p->maxrmargin > titlen ?
1050c66b8046SYuri Pankov p->maxrmargin - titlen : 0;
1051260e9a87SYuri Pankov p->flags |= TERMP_NOSPACE;
105295c635efSGarrett D'Amore
105395c635efSGarrett D'Amore term_word(p, meta->date);
105495c635efSGarrett D'Amore term_flushln(p);
105595c635efSGarrett D'Amore
105695c635efSGarrett D'Amore /* Bottom right corner: manual title and section. */
105795c635efSGarrett D'Amore
105895c635efSGarrett D'Amore p->flags &= ~TERMP_NOBREAK;
105995c635efSGarrett D'Amore p->flags |= TERMP_NOSPACE;
1060698f87a4SGarrett D'Amore p->trailspace = 0;
1061c66b8046SYuri Pankov p->tcol->offset = p->tcol->rmargin;
1062c66b8046SYuri Pankov p->tcol->rmargin = p->maxrmargin;
106395c635efSGarrett D'Amore
106495c635efSGarrett D'Amore term_word(p, title);
106595c635efSGarrett D'Amore term_flushln(p);
10666640c13bSYuri Pankov
10676640c13bSYuri Pankov /*
10686640c13bSYuri Pankov * Reset the terminal state for more output after the footer:
10696640c13bSYuri Pankov * Some output modes, in particular PostScript and PDF, print
10706640c13bSYuri Pankov * the header and the footer into a buffer such that it can be
10716640c13bSYuri Pankov * reused for multiple output pages, then go on to format the
10726640c13bSYuri Pankov * main text.
10736640c13bSYuri Pankov */
10746640c13bSYuri Pankov
10756640c13bSYuri Pankov p->tcol->offset = 0;
10766640c13bSYuri Pankov p->flags = 0;
10776640c13bSYuri Pankov
1078260e9a87SYuri Pankov free(title);
107995c635efSGarrett D'Amore }
108095c635efSGarrett D'Amore
108195c635efSGarrett D'Amore static void
print_man_head(struct termp * p,const struct roff_meta * meta)1082371584c2SYuri Pankov print_man_head(struct termp *p, const struct roff_meta *meta)
108395c635efSGarrett D'Amore {
1084260e9a87SYuri Pankov const char *volume;
1085260e9a87SYuri Pankov char *title;
1086260e9a87SYuri Pankov size_t vollen, titlen;
108795c635efSGarrett D'Amore
1088698f87a4SGarrett D'Amore assert(meta->title);
1089698f87a4SGarrett D'Amore assert(meta->msec);
109095c635efSGarrett D'Amore
1091260e9a87SYuri Pankov volume = NULL == meta->vol ? "" : meta->vol;
1092260e9a87SYuri Pankov vollen = term_strlen(p, volume);
109395c635efSGarrett D'Amore
109495c635efSGarrett D'Amore /* Top left corner: manual title and section. */
109595c635efSGarrett D'Amore
1096260e9a87SYuri Pankov mandoc_asprintf(&title, "%s(%s)", meta->title, meta->msec);
109795c635efSGarrett D'Amore titlen = term_strlen(p, title);
109895c635efSGarrett D'Amore
109995c635efSGarrett D'Amore p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
1100698f87a4SGarrett D'Amore p->trailspace = 1;
1101c66b8046SYuri Pankov p->tcol->offset = 0;
1102c66b8046SYuri Pankov p->tcol->rmargin = 2 * (titlen+1) + vollen < p->maxrmargin ?
1103260e9a87SYuri Pankov (p->maxrmargin - vollen + term_len(p, 1)) / 2 :
1104260e9a87SYuri Pankov vollen < p->maxrmargin ? p->maxrmargin - vollen : 0;
110595c635efSGarrett D'Amore
110695c635efSGarrett D'Amore term_word(p, title);
110795c635efSGarrett D'Amore term_flushln(p);
110895c635efSGarrett D'Amore
110995c635efSGarrett D'Amore /* At the top in the middle: manual volume. */
111095c635efSGarrett D'Amore
111195c635efSGarrett D'Amore p->flags |= TERMP_NOSPACE;
1112c66b8046SYuri Pankov p->tcol->offset = p->tcol->rmargin;
1113c66b8046SYuri Pankov p->tcol->rmargin = p->tcol->offset + vollen + titlen <
1114c66b8046SYuri Pankov p->maxrmargin ? p->maxrmargin - titlen : p->maxrmargin;
111595c635efSGarrett D'Amore
1116260e9a87SYuri Pankov term_word(p, volume);
111795c635efSGarrett D'Amore term_flushln(p);
111895c635efSGarrett D'Amore
111995c635efSGarrett D'Amore /* Top right corner: title and section, again. */
112095c635efSGarrett D'Amore
112195c635efSGarrett D'Amore p->flags &= ~TERMP_NOBREAK;
1122698f87a4SGarrett D'Amore p->trailspace = 0;
1123c66b8046SYuri Pankov if (p->tcol->rmargin + titlen <= p->maxrmargin) {
112495c635efSGarrett D'Amore p->flags |= TERMP_NOSPACE;
1125c66b8046SYuri Pankov p->tcol->offset = p->tcol->rmargin;
1126c66b8046SYuri Pankov p->tcol->rmargin = p->maxrmargin;
112795c635efSGarrett D'Amore term_word(p, title);
112895c635efSGarrett D'Amore term_flushln(p);
112995c635efSGarrett D'Amore }
113095c635efSGarrett D'Amore
113195c635efSGarrett D'Amore p->flags &= ~TERMP_NOSPACE;
1132c66b8046SYuri Pankov p->tcol->offset = 0;
1133c66b8046SYuri Pankov p->tcol->rmargin = p->maxrmargin;
113495c635efSGarrett D'Amore
1135260e9a87SYuri Pankov /*
113695c635efSGarrett D'Amore * Groff prints three blank lines before the content.
113795c635efSGarrett D'Amore * Do the same, except in the temporary, undocumented
113895c635efSGarrett D'Amore * mode imitating mdoc(7) output.
113995c635efSGarrett D'Amore */
114095c635efSGarrett D'Amore
114195c635efSGarrett D'Amore term_vspace(p);
1142260e9a87SYuri Pankov free(title);
114395c635efSGarrett D'Amore }
1144