xref: /openbsd/usr.bin/mandoc/man_macro.c (revision 14a309e3)
1 /*	$OpenBSD: man_macro.c,v 1.78 2017/04/24 23:06:09 schwarze Exp $ */
2 /*
3  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4  * Copyright (c) 2012-2015, 2017 Ingo Schwarze <schwarze@openbsd.org>
5  * Copyright (c) 2013 Franco Fichtner <franco@lastsummer.de>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 #include <sys/types.h>
20 
21 #include <assert.h>
22 #include <ctype.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include "mandoc.h"
27 #include "roff.h"
28 #include "man.h"
29 #include "libmandoc.h"
30 #include "roff_int.h"
31 #include "libman.h"
32 
33 static	void		 blk_close(MACRO_PROT_ARGS);
34 static	void		 blk_exp(MACRO_PROT_ARGS);
35 static	void		 blk_imp(MACRO_PROT_ARGS);
36 static	void		 in_line_eoln(MACRO_PROT_ARGS);
37 static	int		 man_args(struct roff_man *, int,
38 				int *, char *, char **);
39 static	void		 rew_scope(struct roff_man *, enum roff_tok);
40 
41 const	struct man_macro __man_macros[MAN_MAX - MAN_TH] = {
42 	{ in_line_eoln, MAN_BSCOPE }, /* TH */
43 	{ blk_imp, MAN_BSCOPE | MAN_SCOPED }, /* SH */
44 	{ blk_imp, MAN_BSCOPE | MAN_SCOPED }, /* SS */
45 	{ blk_imp, MAN_BSCOPE | MAN_SCOPED }, /* TP */
46 	{ blk_imp, MAN_BSCOPE }, /* LP */
47 	{ blk_imp, MAN_BSCOPE }, /* PP */
48 	{ blk_imp, MAN_BSCOPE }, /* P */
49 	{ blk_imp, MAN_BSCOPE }, /* IP */
50 	{ blk_imp, MAN_BSCOPE }, /* HP */
51 	{ in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* SM */
52 	{ in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* SB */
53 	{ in_line_eoln, 0 }, /* BI */
54 	{ in_line_eoln, 0 }, /* IB */
55 	{ in_line_eoln, 0 }, /* BR */
56 	{ in_line_eoln, 0 }, /* RB */
57 	{ in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* R */
58 	{ in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* B */
59 	{ in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* I */
60 	{ in_line_eoln, 0 }, /* IR */
61 	{ in_line_eoln, 0 }, /* RI */
62 	{ in_line_eoln, MAN_NSCOPED }, /* br */
63 	{ in_line_eoln, MAN_NSCOPED }, /* sp */
64 	{ in_line_eoln, MAN_NSCOPED }, /* nf */
65 	{ in_line_eoln, MAN_NSCOPED }, /* fi */
66 	{ blk_close, MAN_BSCOPE }, /* RE */
67 	{ blk_exp, MAN_BSCOPE }, /* RS */
68 	{ in_line_eoln, 0 }, /* DT */
69 	{ in_line_eoln, 0 }, /* UC */
70 	{ in_line_eoln, MAN_NSCOPED }, /* PD */
71 	{ in_line_eoln, 0 }, /* AT */
72 	{ in_line_eoln, 0 }, /* in */
73 	{ in_line_eoln, 0 }, /* ft */
74 	{ in_line_eoln, 0 }, /* OP */
75 	{ in_line_eoln, MAN_BSCOPE }, /* EX */
76 	{ in_line_eoln, MAN_BSCOPE }, /* EE */
77 	{ blk_exp, MAN_BSCOPE }, /* UR */
78 	{ blk_close, MAN_BSCOPE }, /* UE */
79 	{ in_line_eoln, 0 }, /* ll */
80 };
81 const	struct man_macro *const man_macros = __man_macros - MAN_TH;
82 
83 
84 void
85 man_unscope(struct roff_man *man, const struct roff_node *to)
86 {
87 	struct roff_node *n;
88 
89 	to = to->parent;
90 	n = man->last;
91 	while (n != to) {
92 
93 		/* Reached the end of the document? */
94 
95 		if (to == NULL && ! (n->flags & NODE_VALID)) {
96 			if (man->flags & (MAN_BLINE | MAN_ELINE) &&
97 			    man_macros[n->tok].flags & MAN_SCOPED) {
98 				mandoc_vmsg(MANDOCERR_BLK_LINE,
99 				    man->parse, n->line, n->pos,
100 				    "EOF breaks %s", roff_name[n->tok]);
101 				if (man->flags & MAN_ELINE)
102 					man->flags &= ~MAN_ELINE;
103 				else {
104 					assert(n->type == ROFFT_HEAD);
105 					n = n->parent;
106 					man->flags &= ~MAN_BLINE;
107 				}
108 				man->last = n;
109 				n = n->parent;
110 				roff_node_delete(man, man->last);
111 				continue;
112 			}
113 			if (n->type == ROFFT_BLOCK &&
114 			    man_macros[n->tok].fp == blk_exp)
115 				mandoc_msg(MANDOCERR_BLK_NOEND,
116 				    man->parse, n->line, n->pos,
117 				    roff_name[n->tok]);
118 		}
119 
120 		/*
121 		 * We might delete the man->last node
122 		 * in the post-validation phase.
123 		 * Save a pointer to the parent such that
124 		 * we know where to continue the iteration.
125 		 */
126 
127 		man->last = n;
128 		n = n->parent;
129 		man->last->flags |= NODE_VALID;
130 	}
131 
132 	/*
133 	 * If we ended up at the parent of the node we were
134 	 * supposed to rewind to, that means the target node
135 	 * got deleted, so add the next node we parse as a child
136 	 * of the parent instead of as a sibling of the target.
137 	 */
138 
139 	man->next = (man->last == to) ?
140 	    ROFF_NEXT_CHILD : ROFF_NEXT_SIBLING;
141 }
142 
143 /*
144  * Rewinding entails ascending the parse tree until a coherent point,
145  * for example, the `SH' macro will close out any intervening `SS'
146  * scopes.  When a scope is closed, it must be validated and actioned.
147  */
148 static void
149 rew_scope(struct roff_man *man, enum roff_tok tok)
150 {
151 	struct roff_node *n;
152 
153 	/* Preserve empty paragraphs before RS. */
154 
155 	n = man->last;
156 	if (tok == MAN_RS && n->child == NULL &&
157 	    (n->tok == MAN_P || n->tok == MAN_PP || n->tok == MAN_LP))
158 		return;
159 
160 	for (;;) {
161 		if (n->type == ROFFT_ROOT)
162 			return;
163 		if (n->flags & NODE_VALID) {
164 			n = n->parent;
165 			continue;
166 		}
167 		if (n->type != ROFFT_BLOCK) {
168 			if (n->parent->type == ROFFT_ROOT) {
169 				man_unscope(man, n);
170 				return;
171 			} else {
172 				n = n->parent;
173 				continue;
174 			}
175 		}
176 		if (tok != MAN_SH && (n->tok == MAN_SH ||
177 		    (tok != MAN_SS && (n->tok == MAN_SS ||
178 		     man_macros[n->tok].fp == blk_exp))))
179 			return;
180 		man_unscope(man, n);
181 		n = man->last;
182 	}
183 }
184 
185 
186 /*
187  * Close out a generic explicit macro.
188  */
189 void
190 blk_close(MACRO_PROT_ARGS)
191 {
192 	enum roff_tok		 ntok;
193 	const struct roff_node	*nn;
194 	char			*p;
195 	int			 nrew, target;
196 
197 	nrew = 1;
198 	switch (tok) {
199 	case MAN_RE:
200 		ntok = MAN_RS;
201 		if ( ! man_args(man, line, pos, buf, &p))
202 			break;
203 		for (nn = man->last->parent; nn; nn = nn->parent)
204 			if (nn->tok == ntok && nn->type == ROFFT_BLOCK)
205 				nrew++;
206 		target = strtol(p, &p, 10);
207 		if (*p != '\0')
208 			mandoc_vmsg(MANDOCERR_ARG_EXCESS, man->parse,
209 			    line, p - buf, "RE ... %s", p);
210 		if (target == 0)
211 			target = 1;
212 		nrew -= target;
213 		if (nrew < 1) {
214 			mandoc_vmsg(MANDOCERR_RE_NOTOPEN, man->parse,
215 			    line, ppos, "RE %d", target);
216 			return;
217 		}
218 		break;
219 	case MAN_UE:
220 		ntok = MAN_UR;
221 		break;
222 	default:
223 		abort();
224 	}
225 
226 	for (nn = man->last->parent; nn; nn = nn->parent)
227 		if (nn->tok == ntok && nn->type == ROFFT_BLOCK && ! --nrew)
228 			break;
229 
230 	if (nn == NULL) {
231 		mandoc_msg(MANDOCERR_BLK_NOTOPEN, man->parse,
232 		    line, ppos, roff_name[tok]);
233 		rew_scope(man, MAN_PP);
234 	} else {
235 		line = man->last->line;
236 		ppos = man->last->pos;
237 		ntok = man->last->tok;
238 		man_unscope(man, nn);
239 
240 		/* Move a trailing paragraph behind the block. */
241 
242 		if (ntok == MAN_LP || ntok == MAN_PP || ntok == MAN_P) {
243 			*pos = strlen(buf);
244 			blk_imp(man, ntok, line, ppos, pos, buf);
245 		}
246 	}
247 }
248 
249 void
250 blk_exp(MACRO_PROT_ARGS)
251 {
252 	struct roff_node *head;
253 	char		*p;
254 	int		 la;
255 
256 	rew_scope(man, tok);
257 	roff_block_alloc(man, line, ppos, tok);
258 	head = roff_head_alloc(man, line, ppos, tok);
259 
260 	la = *pos;
261 	if (man_args(man, line, pos, buf, &p))
262 		roff_word_alloc(man, line, la, p);
263 
264 	if (buf[*pos] != '\0')
265 		mandoc_vmsg(MANDOCERR_ARG_EXCESS, man->parse, line,
266 		    *pos, "%s ... %s", roff_name[tok], buf + *pos);
267 
268 	man_unscope(man, head);
269 	roff_body_alloc(man, line, ppos, tok);
270 }
271 
272 /*
273  * Parse an implicit-block macro.  These contain a ROFFT_HEAD and a
274  * ROFFT_BODY contained within a ROFFT_BLOCK.  Rules for closing out other
275  * scopes, such as `SH' closing out an `SS', are defined in the rew
276  * routines.
277  */
278 void
279 blk_imp(MACRO_PROT_ARGS)
280 {
281 	int		 la;
282 	char		*p;
283 	struct roff_node *n;
284 
285 	rew_scope(man, tok);
286 	n = roff_block_alloc(man, line, ppos, tok);
287 	if (n->tok == MAN_SH || n->tok == MAN_SS)
288 		man->flags &= ~MAN_LITERAL;
289 	n = roff_head_alloc(man, line, ppos, tok);
290 
291 	/* Add line arguments. */
292 
293 	for (;;) {
294 		la = *pos;
295 		if ( ! man_args(man, line, pos, buf, &p))
296 			break;
297 		roff_word_alloc(man, line, la, p);
298 	}
299 
300 	/*
301 	 * For macros having optional next-line scope,
302 	 * keep the head open if there were no arguments.
303 	 * For `TP', always keep the head open.
304 	 */
305 
306 	if (man_macros[tok].flags & MAN_SCOPED &&
307 	    (tok == MAN_TP || n == man->last)) {
308 		man->flags |= MAN_BLINE;
309 		return;
310 	}
311 
312 	/* Close out the head and open the body. */
313 
314 	man_unscope(man, n);
315 	roff_body_alloc(man, line, ppos, tok);
316 }
317 
318 void
319 in_line_eoln(MACRO_PROT_ARGS)
320 {
321 	int		 la;
322 	char		*p;
323 	struct roff_node *n;
324 
325 	roff_elem_alloc(man, line, ppos, tok);
326 	n = man->last;
327 
328 	for (;;) {
329 		if (buf[*pos] != '\0' && (tok == MAN_br ||
330 		    tok == MAN_fi || tok == MAN_nf)) {
331 			mandoc_vmsg(MANDOCERR_ARG_SKIP,
332 			    man->parse, line, *pos, "%s %s",
333 			    roff_name[tok], buf + *pos);
334 			break;
335 		}
336 		if (buf[*pos] != '\0' && man->last != n &&
337 		    (tok == MAN_PD || tok == MAN_ft || tok == MAN_sp)) {
338 			mandoc_vmsg(MANDOCERR_ARG_EXCESS,
339 			    man->parse, line, *pos, "%s ... %s",
340 			    roff_name[tok], buf + *pos);
341 			break;
342 		}
343 		la = *pos;
344 		if ( ! man_args(man, line, pos, buf, &p))
345 			break;
346 		if (man_macros[tok].flags & MAN_JOIN &&
347 		    man->last->type == ROFFT_TEXT)
348 			roff_word_append(man, p);
349 		else
350 			roff_word_alloc(man, line, la, p);
351 	}
352 
353 	/*
354 	 * Append NODE_EOS in case the last snipped argument
355 	 * ends with a dot, e.g. `.IR syslog (3).'
356 	 */
357 
358 	if (n != man->last &&
359 	    mandoc_eos(man->last->string, strlen(man->last->string)))
360 		man->last->flags |= NODE_EOS;
361 
362 	/*
363 	 * If no arguments are specified and this is MAN_SCOPED (i.e.,
364 	 * next-line scoped), then set our mode to indicate that we're
365 	 * waiting for terms to load into our context.
366 	 */
367 
368 	if (n == man->last && man_macros[tok].flags & MAN_SCOPED) {
369 		assert( ! (man_macros[tok].flags & MAN_NSCOPED));
370 		man->flags |= MAN_ELINE;
371 		return;
372 	}
373 
374 	assert(man->last->type != ROFFT_ROOT);
375 	man->next = ROFF_NEXT_SIBLING;
376 
377 	/* Rewind our element scope. */
378 
379 	for ( ; man->last; man->last = man->last->parent) {
380 		man_state(man, man->last);
381 		if (man->last == n)
382 			break;
383 	}
384 }
385 
386 void
387 man_endparse(struct roff_man *man)
388 {
389 
390 	man_unscope(man, man->first);
391 	man->flags &= ~MAN_LITERAL;
392 }
393 
394 static int
395 man_args(struct roff_man *man, int line, int *pos, char *buf, char **v)
396 {
397 	char	 *start;
398 
399 	assert(*pos);
400 	*v = start = buf + *pos;
401 	assert(' ' != *start);
402 
403 	if ('\0' == *start)
404 		return 0;
405 
406 	*v = mandoc_getarg(man->parse, v, line, pos);
407 	return 1;
408 }
409