xref: /illumos-gate/usr/src/cmd/mandoc/man_macro.c (revision 4d131170)
1*4d131170SRobert Mustacchi /* $Id: man_macro.c,v 1.145 2020/09/09 17:01:10 schwarze Exp $ */
295c635efSGarrett D'Amore /*
395c635efSGarrett D'Amore  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4*4d131170SRobert Mustacchi  * Copyright (c) 2012-2015, 2017-2020 Ingo Schwarze <schwarze@openbsd.org>
5698f87a4SGarrett D'Amore  * Copyright (c) 2013 Franco Fichtner <franco@lastsummer.de>
695c635efSGarrett D'Amore  *
795c635efSGarrett D'Amore  * Permission to use, copy, modify, and distribute this software for any
895c635efSGarrett D'Amore  * purpose with or without fee is hereby granted, provided that the above
995c635efSGarrett D'Amore  * copyright notice and this permission notice appear in all copies.
1095c635efSGarrett D'Amore  *
11371584c2SYuri Pankov  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1295c635efSGarrett D'Amore  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13371584c2SYuri Pankov  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1495c635efSGarrett D'Amore  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1595c635efSGarrett D'Amore  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1695c635efSGarrett D'Amore  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1795c635efSGarrett D'Amore  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1895c635efSGarrett D'Amore  */
1995c635efSGarrett D'Amore #include "config.h"
20260e9a87SYuri Pankov 
21260e9a87SYuri Pankov #include <sys/types.h>
2295c635efSGarrett D'Amore 
2395c635efSGarrett D'Amore #include <assert.h>
2495c635efSGarrett D'Amore #include <ctype.h>
25cec8643bSMichal Nowak #include <stdio.h>
2695c635efSGarrett D'Amore #include <stdlib.h>
2795c635efSGarrett D'Amore #include <string.h>
2895c635efSGarrett D'Amore 
2995c635efSGarrett D'Amore #include "mandoc.h"
30371584c2SYuri Pankov #include "roff.h"
31371584c2SYuri Pankov #include "man.h"
3295c635efSGarrett D'Amore #include "libmandoc.h"
33371584c2SYuri Pankov #include "roff_int.h"
3495c635efSGarrett D'Amore #include "libman.h"
3595c635efSGarrett D'Amore 
36260e9a87SYuri Pankov static	void		 blk_close(MACRO_PROT_ARGS);
37260e9a87SYuri Pankov static	void		 blk_exp(MACRO_PROT_ARGS);
38260e9a87SYuri Pankov static	void		 blk_imp(MACRO_PROT_ARGS);
39260e9a87SYuri Pankov static	void		 in_line_eoln(MACRO_PROT_ARGS);
40371584c2SYuri Pankov static	int		 man_args(struct roff_man *, int,
4195c635efSGarrett D'Amore 				int *, char *, char **);
42c66b8046SYuri Pankov static	void		 rew_scope(struct roff_man *, enum roff_tok);
4395c635efSGarrett D'Amore 
44cec8643bSMichal Nowak static const struct man_macro man_macros[MAN_MAX - MAN_TH] = {
45cec8643bSMichal Nowak 	{ in_line_eoln, MAN_XSCOPE }, /* TH */
46cec8643bSMichal Nowak 	{ blk_imp, MAN_XSCOPE | MAN_BSCOPED }, /* SH */
47cec8643bSMichal Nowak 	{ blk_imp, MAN_XSCOPE | MAN_BSCOPED }, /* SS */
48cec8643bSMichal Nowak 	{ blk_imp, MAN_XSCOPE | MAN_BSCOPED }, /* TP */
49cec8643bSMichal Nowak 	{ blk_imp, MAN_XSCOPE | MAN_BSCOPED }, /* TQ */
50cec8643bSMichal Nowak 	{ blk_imp, MAN_XSCOPE }, /* LP */
51cec8643bSMichal Nowak 	{ blk_imp, MAN_XSCOPE }, /* PP */
52cec8643bSMichal Nowak 	{ blk_imp, MAN_XSCOPE }, /* P */
53cec8643bSMichal Nowak 	{ blk_imp, MAN_XSCOPE }, /* IP */
54cec8643bSMichal Nowak 	{ blk_imp, MAN_XSCOPE }, /* HP */
55cec8643bSMichal Nowak 	{ in_line_eoln, MAN_NSCOPED | MAN_ESCOPED | MAN_JOIN }, /* SM */
56cec8643bSMichal Nowak 	{ in_line_eoln, MAN_NSCOPED | MAN_ESCOPED | MAN_JOIN }, /* SB */
5795c635efSGarrett D'Amore 	{ in_line_eoln, 0 }, /* BI */
5895c635efSGarrett D'Amore 	{ in_line_eoln, 0 }, /* IB */
5995c635efSGarrett D'Amore 	{ in_line_eoln, 0 }, /* BR */
6095c635efSGarrett D'Amore 	{ in_line_eoln, 0 }, /* RB */
61cec8643bSMichal Nowak 	{ in_line_eoln, MAN_NSCOPED | MAN_ESCOPED | MAN_JOIN }, /* R */
62cec8643bSMichal Nowak 	{ in_line_eoln, MAN_NSCOPED | MAN_ESCOPED | MAN_JOIN }, /* B */
63cec8643bSMichal Nowak 	{ in_line_eoln, MAN_NSCOPED | MAN_ESCOPED | MAN_JOIN }, /* I */
6495c635efSGarrett D'Amore 	{ in_line_eoln, 0 }, /* IR */
6595c635efSGarrett D'Amore 	{ in_line_eoln, 0 }, /* RI */
66cec8643bSMichal Nowak 	{ blk_close, MAN_XSCOPE }, /* RE */
67cec8643bSMichal Nowak 	{ blk_exp, MAN_XSCOPE }, /* RS */
6895c635efSGarrett D'Amore 	{ in_line_eoln, 0 }, /* DT */
6995c635efSGarrett D'Amore 	{ in_line_eoln, 0 }, /* UC */
70371584c2SYuri Pankov 	{ in_line_eoln, MAN_NSCOPED }, /* PD */
7195c635efSGarrett D'Amore 	{ in_line_eoln, 0 }, /* AT */
72c66b8046SYuri Pankov 	{ in_line_eoln, MAN_NSCOPED }, /* in */
73cec8643bSMichal Nowak 	{ blk_imp, MAN_XSCOPE }, /* SY */
74cec8643bSMichal Nowak 	{ blk_close, MAN_XSCOPE }, /* YS */
7595c635efSGarrett D'Amore 	{ in_line_eoln, 0 }, /* OP */
76cec8643bSMichal Nowak 	{ in_line_eoln, MAN_XSCOPE }, /* EX */
77cec8643bSMichal Nowak 	{ in_line_eoln, MAN_XSCOPE }, /* EE */
78cec8643bSMichal Nowak 	{ blk_exp, MAN_XSCOPE }, /* UR */
79cec8643bSMichal Nowak 	{ blk_close, MAN_XSCOPE }, /* UE */
80cec8643bSMichal Nowak 	{ blk_exp, MAN_XSCOPE }, /* MT */
81cec8643bSMichal Nowak 	{ blk_close, MAN_XSCOPE }, /* ME */
8295c635efSGarrett D'Amore };
8395c635efSGarrett D'Amore 
8495c635efSGarrett D'Amore 
85cec8643bSMichal Nowak const struct man_macro *
man_macro(enum roff_tok tok)86cec8643bSMichal Nowak man_macro(enum roff_tok tok)
87cec8643bSMichal Nowak {
88cec8643bSMichal Nowak 	assert(tok >= MAN_TH && tok <= MAN_MAX);
89cec8643bSMichal Nowak 	return man_macros + (tok - MAN_TH);
90cec8643bSMichal Nowak }
91cec8643bSMichal Nowak 
92260e9a87SYuri Pankov void
man_unscope(struct roff_man * man,const struct roff_node * to)93371584c2SYuri Pankov man_unscope(struct roff_man *man, const struct roff_node *to)
9495c635efSGarrett D'Amore {
95371584c2SYuri Pankov 	struct roff_node *n;
9695c635efSGarrett D'Amore 
97260e9a87SYuri Pankov 	to = to->parent;
98260e9a87SYuri Pankov 	n = man->last;
99260e9a87SYuri Pankov 	while (n != to) {
100260e9a87SYuri Pankov 
101260e9a87SYuri Pankov 		/* Reached the end of the document? */
102260e9a87SYuri Pankov 
103a40ea1a7SYuri Pankov 		if (to == NULL && ! (n->flags & NODE_VALID)) {
104260e9a87SYuri Pankov 			if (man->flags & (MAN_BLINE | MAN_ELINE) &&
105cec8643bSMichal Nowak 			    man_macro(n->tok)->flags &
106cec8643bSMichal Nowak 			     (MAN_BSCOPED | MAN_NSCOPED)) {
107cec8643bSMichal Nowak 				mandoc_msg(MANDOCERR_BLK_LINE,
108cec8643bSMichal Nowak 				    n->line, n->pos,
109c66b8046SYuri Pankov 				    "EOF breaks %s", roff_name[n->tok]);
110*4d131170SRobert Mustacchi 				if (man->flags & MAN_ELINE) {
111*4d131170SRobert Mustacchi 					if ((man_macro(n->parent->tok)->flags &
112*4d131170SRobert Mustacchi 					    MAN_ESCOPED) == 0)
113*4d131170SRobert Mustacchi 						man->flags &= ~MAN_ELINE;
114*4d131170SRobert Mustacchi 				} else {
115371584c2SYuri Pankov 					assert(n->type == ROFFT_HEAD);
116260e9a87SYuri Pankov 					n = n->parent;
117260e9a87SYuri Pankov 					man->flags &= ~MAN_BLINE;
118260e9a87SYuri Pankov 				}
119260e9a87SYuri Pankov 				man->last = n;
120260e9a87SYuri Pankov 				n = n->parent;
121371584c2SYuri Pankov 				roff_node_delete(man, man->last);
122260e9a87SYuri Pankov 				continue;
123260e9a87SYuri Pankov 			}
124371584c2SYuri Pankov 			if (n->type == ROFFT_BLOCK &&
125cec8643bSMichal Nowak 			    man_macro(n->tok)->fp == blk_exp)
126260e9a87SYuri Pankov 				mandoc_msg(MANDOCERR_BLK_NOEND,
127cec8643bSMichal Nowak 				    n->line, n->pos, "%s",
128c66b8046SYuri Pankov 				    roff_name[n->tok]);
129260e9a87SYuri Pankov 		}
13095c635efSGarrett D'Amore 
13195c635efSGarrett D'Amore 		/*
132260e9a87SYuri Pankov 		 * We might delete the man->last node
133260e9a87SYuri Pankov 		 * in the post-validation phase.
134260e9a87SYuri Pankov 		 * Save a pointer to the parent such that
135260e9a87SYuri Pankov 		 * we know where to continue the iteration.
13695c635efSGarrett D'Amore 		 */
137260e9a87SYuri Pankov 
138698f87a4SGarrett D'Amore 		man->last = n;
139260e9a87SYuri Pankov 		n = n->parent;
140a40ea1a7SYuri Pankov 		man->last->flags |= NODE_VALID;
14195c635efSGarrett D'Amore 	}
14295c635efSGarrett D'Amore 
143260e9a87SYuri Pankov 	/*
144260e9a87SYuri Pankov 	 * If we ended up at the parent of the node we were
145260e9a87SYuri Pankov 	 * supposed to rewind to, that means the target node
146260e9a87SYuri Pankov 	 * got deleted, so add the next node we parse as a child
147260e9a87SYuri Pankov 	 * of the parent instead of as a sibling of the target.
148260e9a87SYuri Pankov 	 */
14995c635efSGarrett D'Amore 
150260e9a87SYuri Pankov 	man->next = (man->last == to) ?
151371584c2SYuri Pankov 	    ROFF_NEXT_CHILD : ROFF_NEXT_SIBLING;
15295c635efSGarrett D'Amore }
15395c635efSGarrett D'Amore 
15495c635efSGarrett D'Amore /*
15595c635efSGarrett D'Amore  * Rewinding entails ascending the parse tree until a coherent point,
15695c635efSGarrett D'Amore  * for example, the `SH' macro will close out any intervening `SS'
15795c635efSGarrett D'Amore  * scopes.  When a scope is closed, it must be validated and actioned.
15895c635efSGarrett D'Amore  */
159260e9a87SYuri Pankov static void
rew_scope(struct roff_man * man,enum roff_tok tok)160c66b8046SYuri Pankov rew_scope(struct roff_man *man, enum roff_tok tok)
16195c635efSGarrett D'Amore {
162371584c2SYuri Pankov 	struct roff_node *n;
16395c635efSGarrett D'Amore 
164371584c2SYuri Pankov 	/* Preserve empty paragraphs before RS. */
16595c635efSGarrett D'Amore 
166371584c2SYuri Pankov 	n = man->last;
167371584c2SYuri Pankov 	if (tok == MAN_RS && n->child == NULL &&
168371584c2SYuri Pankov 	    (n->tok == MAN_P || n->tok == MAN_PP || n->tok == MAN_LP))
169371584c2SYuri Pankov 		return;
17095c635efSGarrett D'Amore 
171371584c2SYuri Pankov 	for (;;) {
172371584c2SYuri Pankov 		if (n->type == ROFFT_ROOT)
173371584c2SYuri Pankov 			return;
174a40ea1a7SYuri Pankov 		if (n->flags & NODE_VALID) {
175371584c2SYuri Pankov 			n = n->parent;
176371584c2SYuri Pankov 			continue;
177371584c2SYuri Pankov 		}
178371584c2SYuri Pankov 		if (n->type != ROFFT_BLOCK) {
179371584c2SYuri Pankov 			if (n->parent->type == ROFFT_ROOT) {
180371584c2SYuri Pankov 				man_unscope(man, n);
181371584c2SYuri Pankov 				return;
182371584c2SYuri Pankov 			} else {
183371584c2SYuri Pankov 				n = n->parent;
184371584c2SYuri Pankov 				continue;
185371584c2SYuri Pankov 			}
186371584c2SYuri Pankov 		}
187371584c2SYuri Pankov 		if (tok != MAN_SH && (n->tok == MAN_SH ||
188371584c2SYuri Pankov 		    (tok != MAN_SS && (n->tok == MAN_SS ||
189cec8643bSMichal Nowak 		     man_macro(n->tok)->fp == blk_exp))))
190371584c2SYuri Pankov 			return;
191371584c2SYuri Pankov 		man_unscope(man, n);
192371584c2SYuri Pankov 		n = man->last;
193371584c2SYuri Pankov 	}
19495c635efSGarrett D'Amore }
19595c635efSGarrett D'Amore 
19695c635efSGarrett D'Amore 
19795c635efSGarrett D'Amore /*
19895c635efSGarrett D'Amore  * Close out a generic explicit macro.
19995c635efSGarrett D'Amore  */
200260e9a87SYuri Pankov void
blk_close(MACRO_PROT_ARGS)20195c635efSGarrett D'Amore blk_close(MACRO_PROT_ARGS)
20295c635efSGarrett D'Amore {
203cec8643bSMichal Nowak 	enum roff_tok		 ctok, ntok;
204371584c2SYuri Pankov 	const struct roff_node	*nn;
205cec8643bSMichal Nowak 	char			*p, *ep;
206cec8643bSMichal Nowak 	int			 cline, cpos, la, nrew, target;
20795c635efSGarrett D'Amore 
208260e9a87SYuri Pankov 	nrew = 1;
20995c635efSGarrett D'Amore 	switch (tok) {
210260e9a87SYuri Pankov 	case MAN_RE:
21195c635efSGarrett D'Amore 		ntok = MAN_RS;
212cec8643bSMichal Nowak 		la = *pos;
213260e9a87SYuri Pankov 		if ( ! man_args(man, line, pos, buf, &p))
214260e9a87SYuri Pankov 			break;
215260e9a87SYuri Pankov 		for (nn = man->last->parent; nn; nn = nn->parent)
216371584c2SYuri Pankov 			if (nn->tok == ntok && nn->type == ROFFT_BLOCK)
217260e9a87SYuri Pankov 				nrew++;
218cec8643bSMichal Nowak 		target = strtol(p, &ep, 10);
219cec8643bSMichal Nowak 		if (*ep != '\0')
220cec8643bSMichal Nowak 			mandoc_msg(MANDOCERR_ARG_EXCESS, line,
221cec8643bSMichal Nowak 			    la + (buf[la] == '"') + (int)(ep - p),
222cec8643bSMichal Nowak 			    "RE ... %s", ep);
223cec8643bSMichal Nowak 		free(p);
224260e9a87SYuri Pankov 		if (target == 0)
225260e9a87SYuri Pankov 			target = 1;
226260e9a87SYuri Pankov 		nrew -= target;
227260e9a87SYuri Pankov 		if (nrew < 1) {
228cec8643bSMichal Nowak 			mandoc_msg(MANDOCERR_RE_NOTOPEN,
229260e9a87SYuri Pankov 			    line, ppos, "RE %d", target);
230260e9a87SYuri Pankov 			return;
231260e9a87SYuri Pankov 		}
23295c635efSGarrett D'Amore 		break;
233cec8643bSMichal Nowak 	case MAN_YS:
234cec8643bSMichal Nowak 		ntok = MAN_SY;
235cec8643bSMichal Nowak 		break;
236260e9a87SYuri Pankov 	case MAN_UE:
237698f87a4SGarrett D'Amore 		ntok = MAN_UR;
238698f87a4SGarrett D'Amore 		break;
239c66b8046SYuri Pankov 	case MAN_ME:
240c66b8046SYuri Pankov 		ntok = MAN_MT;
241c66b8046SYuri Pankov 		break;
24295c635efSGarrett D'Amore 	default:
24395c635efSGarrett D'Amore 		abort();
24495c635efSGarrett D'Amore 	}
24595c635efSGarrett D'Amore 
246698f87a4SGarrett D'Amore 	for (nn = man->last->parent; nn; nn = nn->parent)
247371584c2SYuri Pankov 		if (nn->tok == ntok && nn->type == ROFFT_BLOCK && ! --nrew)
24895c635efSGarrett D'Amore 			break;
24995c635efSGarrett D'Amore 
250260e9a87SYuri Pankov 	if (nn == NULL) {
251cec8643bSMichal Nowak 		mandoc_msg(MANDOCERR_BLK_NOTOPEN,
252cec8643bSMichal Nowak 		    line, ppos, "%s", roff_name[tok]);
253371584c2SYuri Pankov 		rew_scope(man, MAN_PP);
254cec8643bSMichal Nowak 		if (tok == MAN_RE) {
255cec8643bSMichal Nowak 			roff_elem_alloc(man, line, ppos, ROFF_br);
256cec8643bSMichal Nowak 			man->last->flags |= NODE_LINE |
257cec8643bSMichal Nowak 			    NODE_VALID | NODE_ENDED;
258cec8643bSMichal Nowak 			man->next = ROFF_NEXT_SIBLING;
259cec8643bSMichal Nowak 		}
260cec8643bSMichal Nowak 		return;
261cec8643bSMichal Nowak 	}
262260e9a87SYuri Pankov 
263cec8643bSMichal Nowak 	cline = man->last->line;
264cec8643bSMichal Nowak 	cpos = man->last->pos;
265cec8643bSMichal Nowak 	ctok = man->last->tok;
266cec8643bSMichal Nowak 	man_unscope(man, nn);
267c66b8046SYuri Pankov 
268cec8643bSMichal Nowak 	if (tok == MAN_RE && nn->head->aux > 0)
269cec8643bSMichal Nowak 		roff_setreg(man->roff, "an-margin", nn->head->aux, '-');
270260e9a87SYuri Pankov 
271cec8643bSMichal Nowak 	/* Trailing text. */
272cec8643bSMichal Nowak 
273cec8643bSMichal Nowak 	if (buf[*pos] != '\0') {
274cec8643bSMichal Nowak 		roff_word_alloc(man, line, ppos, buf + *pos);
275cec8643bSMichal Nowak 		man->last->flags |= NODE_DELIMC;
276cec8643bSMichal Nowak 		if (mandoc_eos(man->last->string, strlen(man->last->string)))
277cec8643bSMichal Nowak 			man->last->flags |= NODE_EOS;
278cec8643bSMichal Nowak 	}
279cec8643bSMichal Nowak 
280cec8643bSMichal Nowak 	/* Move a trailing paragraph behind the block. */
281cec8643bSMichal Nowak 
282cec8643bSMichal Nowak 	if (ctok == MAN_LP || ctok == MAN_PP || ctok == MAN_P) {
283cec8643bSMichal Nowak 		*pos = strlen(buf);
284cec8643bSMichal Nowak 		blk_imp(man, ctok, cline, cpos, pos, buf);
285cec8643bSMichal Nowak 	}
286cec8643bSMichal Nowak 
287cec8643bSMichal Nowak 	/* Synopsis blocks need an explicit end marker for spacing. */
288cec8643bSMichal Nowak 
289cec8643bSMichal Nowak 	if (tok == MAN_YS && man->last == nn) {
290cec8643bSMichal Nowak 		roff_elem_alloc(man, line, ppos, tok);
291cec8643bSMichal Nowak 		man_unscope(man, man->last);
292260e9a87SYuri Pankov 	}
29395c635efSGarrett D'Amore }
29495c635efSGarrett D'Amore 
295260e9a87SYuri Pankov void
blk_exp(MACRO_PROT_ARGS)29695c635efSGarrett D'Amore blk_exp(MACRO_PROT_ARGS)
29795c635efSGarrett D'Amore {
298371584c2SYuri Pankov 	struct roff_node *head;
29995c635efSGarrett D'Amore 	char		*p;
300260e9a87SYuri Pankov 	int		 la;
30195c635efSGarrett D'Amore 
302cec8643bSMichal Nowak 	if (tok == MAN_RS) {
303cec8643bSMichal Nowak 		rew_scope(man, tok);
304cec8643bSMichal Nowak 		man->flags |= ROFF_NONOFILL;
305cec8643bSMichal Nowak 	}
306371584c2SYuri Pankov 	roff_block_alloc(man, line, ppos, tok);
307371584c2SYuri Pankov 	head = roff_head_alloc(man, line, ppos, tok);
30895c635efSGarrett D'Amore 
309260e9a87SYuri Pankov 	la = *pos;
310c66b8046SYuri Pankov 	if (man_args(man, line, pos, buf, &p)) {
311371584c2SYuri Pankov 		roff_word_alloc(man, line, la, p);
312c66b8046SYuri Pankov 		if (tok == MAN_RS) {
313c66b8046SYuri Pankov 			if (roff_getreg(man->roff, "an-margin") == 0)
314c66b8046SYuri Pankov 				roff_setreg(man->roff, "an-margin",
315c66b8046SYuri Pankov 				    7 * 24, '=');
316c66b8046SYuri Pankov 			if ((head->aux = strtod(p, NULL) * 24.0) > 0)
317c66b8046SYuri Pankov 				roff_setreg(man->roff, "an-margin",
318c66b8046SYuri Pankov 				    head->aux, '+');
319c66b8046SYuri Pankov 		}
320cec8643bSMichal Nowak 		free(p);
321c66b8046SYuri Pankov 	}
32295c635efSGarrett D'Amore 
323260e9a87SYuri Pankov 	if (buf[*pos] != '\0')
324cec8643bSMichal Nowak 		mandoc_msg(MANDOCERR_ARG_EXCESS, line, *pos,
325cec8643bSMichal Nowak 		    "%s ... %s", roff_name[tok], buf + *pos);
326698f87a4SGarrett D'Amore 
327260e9a87SYuri Pankov 	man_unscope(man, head);
328371584c2SYuri Pankov 	roff_body_alloc(man, line, ppos, tok);
329cec8643bSMichal Nowak 	man->flags &= ~ROFF_NONOFILL;
33095c635efSGarrett D'Amore }
33195c635efSGarrett D'Amore 
33295c635efSGarrett D'Amore /*
333371584c2SYuri Pankov  * Parse an implicit-block macro.  These contain a ROFFT_HEAD and a
334371584c2SYuri Pankov  * ROFFT_BODY contained within a ROFFT_BLOCK.  Rules for closing out other
33595c635efSGarrett D'Amore  * scopes, such as `SH' closing out an `SS', are defined in the rew
33695c635efSGarrett D'Amore  * routines.
33795c635efSGarrett D'Amore  */
338260e9a87SYuri Pankov void
blk_imp(MACRO_PROT_ARGS)33995c635efSGarrett D'Amore blk_imp(MACRO_PROT_ARGS)
34095c635efSGarrett D'Amore {
34195c635efSGarrett D'Amore 	int		 la;
34295c635efSGarrett D'Amore 	char		*p;
343371584c2SYuri Pankov 	struct roff_node *n;
34495c635efSGarrett D'Amore 
345371584c2SYuri Pankov 	rew_scope(man, tok);
346cec8643bSMichal Nowak 	man->flags |= ROFF_NONOFILL;
347cec8643bSMichal Nowak 	if (tok == MAN_SH || tok == MAN_SS)
348cec8643bSMichal Nowak 		man->flags &= ~ROFF_NOFILL;
349cec8643bSMichal Nowak 	roff_block_alloc(man, line, ppos, tok);
350371584c2SYuri Pankov 	n = roff_head_alloc(man, line, ppos, tok);
35195c635efSGarrett D'Amore 
35295c635efSGarrett D'Amore 	/* Add line arguments. */
35395c635efSGarrett D'Amore 
35495c635efSGarrett D'Amore 	for (;;) {
35595c635efSGarrett D'Amore 		la = *pos;
356698f87a4SGarrett D'Amore 		if ( ! man_args(man, line, pos, buf, &p))
35795c635efSGarrett D'Amore 			break;
358371584c2SYuri Pankov 		roff_word_alloc(man, line, la, p);
359cec8643bSMichal Nowak 		free(p);
36095c635efSGarrett D'Amore 	}
36195c635efSGarrett D'Amore 
362371584c2SYuri Pankov 	/*
363371584c2SYuri Pankov 	 * For macros having optional next-line scope,
364371584c2SYuri Pankov 	 * keep the head open if there were no arguments.
365cec8643bSMichal Nowak 	 * For `TP' and `TQ', always keep the head open.
366371584c2SYuri Pankov 	 */
36795c635efSGarrett D'Amore 
368cec8643bSMichal Nowak 	if (man_macro(tok)->flags & MAN_BSCOPED &&
369cec8643bSMichal Nowak 	    (tok == MAN_TP || tok == MAN_TQ || n == man->last)) {
370371584c2SYuri Pankov 		man->flags |= MAN_BLINE;
371371584c2SYuri Pankov 		return;
37295c635efSGarrett D'Amore 	}
373371584c2SYuri Pankov 
374371584c2SYuri Pankov 	/* Close out the head and open the body. */
375371584c2SYuri Pankov 
376371584c2SYuri Pankov 	man_unscope(man, n);
377371584c2SYuri Pankov 	roff_body_alloc(man, line, ppos, tok);
378cec8643bSMichal Nowak 	man->flags &= ~ROFF_NONOFILL;
37995c635efSGarrett D'Amore }
38095c635efSGarrett D'Amore 
381260e9a87SYuri Pankov void
in_line_eoln(MACRO_PROT_ARGS)38295c635efSGarrett D'Amore in_line_eoln(MACRO_PROT_ARGS)
38395c635efSGarrett D'Amore {
38495c635efSGarrett D'Amore 	int		 la;
38595c635efSGarrett D'Amore 	char		*p;
386371584c2SYuri Pankov 	struct roff_node *n;
38795c635efSGarrett D'Amore 
388371584c2SYuri Pankov 	roff_elem_alloc(man, line, ppos, tok);
389698f87a4SGarrett D'Amore 	n = man->last;
39095c635efSGarrett D'Amore 
391cec8643bSMichal Nowak 	if (tok == MAN_EX)
392cec8643bSMichal Nowak 		man->flags |= ROFF_NOFILL;
393cec8643bSMichal Nowak 	else if (tok == MAN_EE)
394cec8643bSMichal Nowak 		man->flags &= ~ROFF_NOFILL;
395cec8643bSMichal Nowak 
39695c635efSGarrett D'Amore 	for (;;) {
397c66b8046SYuri Pankov 		if (buf[*pos] != '\0' && man->last != n && tok == MAN_PD) {
398cec8643bSMichal Nowak 			mandoc_msg(MANDOCERR_ARG_EXCESS, line, *pos,
399cec8643bSMichal Nowak 			    "%s ... %s", roff_name[tok], buf + *pos);
400260e9a87SYuri Pankov 			break;
401260e9a87SYuri Pankov 		}
40295c635efSGarrett D'Amore 		la = *pos;
403698f87a4SGarrett D'Amore 		if ( ! man_args(man, line, pos, buf, &p))
40495c635efSGarrett D'Amore 			break;
405cec8643bSMichal Nowak 		if (man_macro(tok)->flags & MAN_JOIN &&
406371584c2SYuri Pankov 		    man->last->type == ROFFT_TEXT)
407371584c2SYuri Pankov 			roff_word_append(man, p);
408260e9a87SYuri Pankov 		else
409371584c2SYuri Pankov 			roff_word_alloc(man, line, la, p);
410cec8643bSMichal Nowak 		free(p);
41195c635efSGarrett D'Amore 	}
41295c635efSGarrett D'Amore 
413698f87a4SGarrett D'Amore 	/*
414a40ea1a7SYuri Pankov 	 * Append NODE_EOS in case the last snipped argument
415698f87a4SGarrett D'Amore 	 * ends with a dot, e.g. `.IR syslog (3).'
416698f87a4SGarrett D'Amore 	 */
417698f87a4SGarrett D'Amore 
418698f87a4SGarrett D'Amore 	if (n != man->last &&
419260e9a87SYuri Pankov 	    mandoc_eos(man->last->string, strlen(man->last->string)))
420a40ea1a7SYuri Pankov 		man->last->flags |= NODE_EOS;
421698f87a4SGarrett D'Amore 
42295c635efSGarrett D'Amore 	/*
423cec8643bSMichal Nowak 	 * If no arguments are specified and this is MAN_ESCOPED (i.e.,
42495c635efSGarrett D'Amore 	 * next-line scoped), then set our mode to indicate that we're
42595c635efSGarrett D'Amore 	 * waiting for terms to load into our context.
42695c635efSGarrett D'Amore 	 */
42795c635efSGarrett D'Amore 
428cec8643bSMichal Nowak 	if (n == man->last && man_macro(tok)->flags & MAN_ESCOPED) {
429698f87a4SGarrett D'Amore 		man->flags |= MAN_ELINE;
430260e9a87SYuri Pankov 		return;
43195c635efSGarrett D'Amore 	}
43295c635efSGarrett D'Amore 
433371584c2SYuri Pankov 	assert(man->last->type != ROFFT_ROOT);
434371584c2SYuri Pankov 	man->next = ROFF_NEXT_SIBLING;
435260e9a87SYuri Pankov 
436371584c2SYuri Pankov 	/* Rewind our element scope. */
43795c635efSGarrett D'Amore 
438698f87a4SGarrett D'Amore 	for ( ; man->last; man->last = man->last->parent) {
439cec8643bSMichal Nowak 		man->last->flags |= NODE_VALID;
440698f87a4SGarrett D'Amore 		if (man->last == n)
44195c635efSGarrett D'Amore 			break;
44295c635efSGarrett D'Amore 	}
443cec8643bSMichal Nowak 
444cec8643bSMichal Nowak 	/* Rewind next-line scoped ancestors, if any. */
445cec8643bSMichal Nowak 
446cec8643bSMichal Nowak 	if (man_macro(tok)->flags & MAN_ESCOPED)
447cec8643bSMichal Nowak 		man_descope(man, line, ppos, NULL);
44895c635efSGarrett D'Amore }
44995c635efSGarrett D'Amore 
450260e9a87SYuri Pankov void
man_endparse(struct roff_man * man)451371584c2SYuri Pankov man_endparse(struct roff_man *man)
45295c635efSGarrett D'Amore {
453cec8643bSMichal Nowak 	man_unscope(man, man->meta.first);
45495c635efSGarrett D'Amore }
45595c635efSGarrett D'Amore 
45695c635efSGarrett D'Amore static int
man_args(struct roff_man * man,int line,int * pos,char * buf,char ** v)457371584c2SYuri Pankov man_args(struct roff_man *man, int line, int *pos, char *buf, char **v)
45895c635efSGarrett D'Amore {
45995c635efSGarrett D'Amore 	char	 *start;
46095c635efSGarrett D'Amore 
46195c635efSGarrett D'Amore 	assert(*pos);
46295c635efSGarrett D'Amore 	*v = start = buf + *pos;
46395c635efSGarrett D'Amore 	assert(' ' != *start);
46495c635efSGarrett D'Amore 
46595c635efSGarrett D'Amore 	if ('\0' == *start)
466371584c2SYuri Pankov 		return 0;
46795c635efSGarrett D'Amore 
468cec8643bSMichal Nowak 	*v = roff_getarg(man->roff, v, line, pos);
469371584c2SYuri Pankov 	return 1;
47095c635efSGarrett D'Amore }
471