xref: /openbsd-src/usr.bin/mandoc/mdoc_macro.c (revision cb39b41371628601fbe4c618205356d538b9d08a)
1 /*	$OpenBSD: mdoc_macro.c,v 1.156 2015/05/01 16:56:36 schwarze Exp $ */
2 /*
3  * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4  * Copyright (c) 2010, 2012-2015 Ingo Schwarze <schwarze@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 #include <sys/types.h>
19 
20 #include <assert.h>
21 #include <ctype.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <time.h>
26 
27 #include "mandoc.h"
28 #include "roff.h"
29 #include "mdoc.h"
30 #include "libmandoc.h"
31 #include "roff_int.h"
32 #include "libmdoc.h"
33 
34 static	void		blk_full(MACRO_PROT_ARGS);
35 static	void		blk_exp_close(MACRO_PROT_ARGS);
36 static	void		blk_part_exp(MACRO_PROT_ARGS);
37 static	void		blk_part_imp(MACRO_PROT_ARGS);
38 static	void		ctx_synopsis(MACRO_PROT_ARGS);
39 static	void		in_line_eoln(MACRO_PROT_ARGS);
40 static	void		in_line_argn(MACRO_PROT_ARGS);
41 static	void		in_line(MACRO_PROT_ARGS);
42 static	void		phrase_ta(MACRO_PROT_ARGS);
43 
44 static	void		append_delims(struct roff_man *, int, int *, char *);
45 static	void		dword(struct roff_man *, int, int, const char *,
46 				enum mdelim, int);
47 static	int		find_pending(struct roff_man *, int, int, int,
48 				struct roff_node *);
49 static	int		lookup(struct roff_man *, int, int, int, const char *);
50 static	int		macro_or_word(MACRO_PROT_ARGS, int);
51 static	int		parse_rest(struct roff_man *, int, int, int *, char *);
52 static	int		rew_alt(int);
53 static	void		rew_elem(struct roff_man *, int);
54 static	void		rew_last(struct roff_man *, const struct roff_node *);
55 static	void		rew_pending(struct roff_man *,
56 				const struct roff_node *);
57 
58 const	struct mdoc_macro __mdoc_macros[MDOC_MAX] = {
59 	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ap */
60 	{ in_line_eoln, MDOC_PROLOGUE }, /* Dd */
61 	{ in_line_eoln, MDOC_PROLOGUE }, /* Dt */
62 	{ in_line_eoln, MDOC_PROLOGUE }, /* Os */
63 	{ blk_full, MDOC_PARSED | MDOC_JOIN }, /* Sh */
64 	{ blk_full, MDOC_PARSED | MDOC_JOIN }, /* Ss */
65 	{ in_line_eoln, 0 }, /* Pp */
66 	{ blk_part_imp, MDOC_PARSED | MDOC_JOIN }, /* D1 */
67 	{ blk_part_imp, MDOC_PARSED | MDOC_JOIN }, /* Dl */
68 	{ blk_full, MDOC_EXPLICIT }, /* Bd */
69 	{ blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Ed */
70 	{ blk_full, MDOC_EXPLICIT }, /* Bl */
71 	{ blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* El */
72 	{ blk_full, MDOC_PARSED | MDOC_JOIN }, /* It */
73 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ad */
74 	{ in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* An */
75 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ar */
76 	{ in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Cd */
77 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Cm */
78 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Dv */
79 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Er */
80 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ev */
81 	{ in_line_eoln, 0 }, /* Ex */
82 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fa */
83 	{ in_line_eoln, 0 }, /* Fd */
84 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fl */
85 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fn */
86 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ft */
87 	{ in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ic */
88 	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* In */
89 	{ in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Li */
90 	{ blk_full, MDOC_JOIN }, /* Nd */
91 	{ ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED }, /* Nm */
92 	{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Op */
93 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ot */
94 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Pa */
95 	{ in_line_eoln, 0 }, /* Rv */
96 	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* St */
97 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Va */
98 	{ ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED }, /* Vt */
99 	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Xr */
100 	{ in_line_eoln, MDOC_JOIN }, /* %A */
101 	{ in_line_eoln, MDOC_JOIN }, /* %B */
102 	{ in_line_eoln, MDOC_JOIN }, /* %D */
103 	{ in_line_eoln, MDOC_JOIN }, /* %I */
104 	{ in_line_eoln, MDOC_JOIN }, /* %J */
105 	{ in_line_eoln, 0 }, /* %N */
106 	{ in_line_eoln, MDOC_JOIN }, /* %O */
107 	{ in_line_eoln, 0 }, /* %P */
108 	{ in_line_eoln, MDOC_JOIN }, /* %R */
109 	{ in_line_eoln, MDOC_JOIN }, /* %T */
110 	{ in_line_eoln, 0 }, /* %V */
111 	{ blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
112 			 MDOC_EXPLICIT | MDOC_JOIN }, /* Ac */
113 	{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
114 			MDOC_EXPLICIT | MDOC_JOIN }, /* Ao */
115 	{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Aq */
116 	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* At */
117 	{ blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
118 			 MDOC_EXPLICIT | MDOC_JOIN }, /* Bc */
119 	{ blk_full, MDOC_EXPLICIT }, /* Bf */
120 	{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
121 			MDOC_EXPLICIT | MDOC_JOIN }, /* Bo */
122 	{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Bq */
123 	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bsx */
124 	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bx */
125 	{ in_line_eoln, 0 }, /* Db */
126 	{ blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
127 			 MDOC_EXPLICIT | MDOC_JOIN }, /* Dc */
128 	{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
129 			MDOC_EXPLICIT | MDOC_JOIN }, /* Do */
130 	{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Dq */
131 	{ blk_exp_close, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Ec */
132 	{ blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Ef */
133 	{ in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Em */
134 	{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Eo */
135 	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Fx */
136 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ms */
137 	{ in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* No */
138 	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED |
139 			MDOC_IGNDELIM | MDOC_JOIN }, /* Ns */
140 	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Nx */
141 	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ox */
142 	{ blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
143 			 MDOC_EXPLICIT | MDOC_JOIN }, /* Pc */
144 	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_IGNDELIM }, /* Pf */
145 	{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
146 			MDOC_EXPLICIT | MDOC_JOIN }, /* Po */
147 	{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Pq */
148 	{ blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
149 			 MDOC_EXPLICIT | MDOC_JOIN }, /* Qc */
150 	{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ql */
151 	{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
152 			MDOC_EXPLICIT | MDOC_JOIN }, /* Qo */
153 	{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Qq */
154 	{ blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Re */
155 	{ blk_full, MDOC_EXPLICIT }, /* Rs */
156 	{ blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
157 			 MDOC_EXPLICIT | MDOC_JOIN }, /* Sc */
158 	{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
159 			MDOC_EXPLICIT | MDOC_JOIN }, /* So */
160 	{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Sq */
161 	{ in_line_argn, 0 }, /* Sm */
162 	{ in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Sx */
163 	{ in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Sy */
164 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Tn */
165 	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ux */
166 	{ blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Xc */
167 	{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Xo */
168 	{ blk_full, MDOC_EXPLICIT | MDOC_CALLABLE }, /* Fo */
169 	{ blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
170 			 MDOC_EXPLICIT | MDOC_JOIN }, /* Fc */
171 	{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
172 			MDOC_EXPLICIT | MDOC_JOIN }, /* Oo */
173 	{ blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
174 			 MDOC_EXPLICIT | MDOC_JOIN }, /* Oc */
175 	{ blk_full, MDOC_EXPLICIT }, /* Bk */
176 	{ blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Ek */
177 	{ in_line_eoln, 0 }, /* Bt */
178 	{ in_line_eoln, 0 }, /* Hf */
179 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fr */
180 	{ in_line_eoln, 0 }, /* Ud */
181 	{ in_line, 0 }, /* Lb */
182 	{ in_line_eoln, 0 }, /* Lp */
183 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Lk */
184 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Mt */
185 	{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Brq */
186 	{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
187 			MDOC_EXPLICIT | MDOC_JOIN }, /* Bro */
188 	{ blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
189 			 MDOC_EXPLICIT | MDOC_JOIN }, /* Brc */
190 	{ in_line_eoln, MDOC_JOIN }, /* %C */
191 	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Es */
192 	{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* En */
193 	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Dx */
194 	{ in_line_eoln, MDOC_JOIN }, /* %Q */
195 	{ in_line_eoln, 0 }, /* br */
196 	{ in_line_eoln, 0 }, /* sp */
197 	{ in_line_eoln, 0 }, /* %U */
198 	{ phrase_ta, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ta */
199 	{ in_line_eoln, MDOC_PROLOGUE }, /* ll */
200 };
201 
202 const	struct mdoc_macro * const mdoc_macros = __mdoc_macros;
203 
204 
205 /*
206  * This is called at the end of parsing.  It must traverse up the tree,
207  * closing out open [implicit] scopes.  Obviously, open explicit scopes
208  * are errors.
209  */
210 void
211 mdoc_endparse(struct roff_man *mdoc)
212 {
213 	struct roff_node *n;
214 
215 	/* Scan for open explicit scopes. */
216 
217 	n = mdoc->last->flags & MDOC_VALID ?
218 	    mdoc->last->parent : mdoc->last;
219 
220 	for ( ; n; n = n->parent)
221 		if (n->type == ROFFT_BLOCK &&
222 		    mdoc_macros[n->tok].flags & MDOC_EXPLICIT)
223 			mandoc_msg(MANDOCERR_BLK_NOEND, mdoc->parse,
224 			    n->line, n->pos, mdoc_macronames[n->tok]);
225 
226 	/* Rewind to the first. */
227 
228 	rew_last(mdoc, mdoc->first);
229 }
230 
231 /*
232  * Look up the macro at *p called by "from",
233  * or as a line macro if from == TOKEN_NONE.
234  */
235 static int
236 lookup(struct roff_man *mdoc, int from, int line, int ppos, const char *p)
237 {
238 	int	 res;
239 
240 	if (from == TOKEN_NONE || mdoc_macros[from].flags & MDOC_PARSED) {
241 		res = mdoc_hash_find(p);
242 		if (res != TOKEN_NONE) {
243 			if (mdoc_macros[res].flags & MDOC_CALLABLE)
244 				return(res);
245 			if (res != MDOC_br && res != MDOC_sp && res != MDOC_ll)
246 				mandoc_msg(MANDOCERR_MACRO_CALL,
247 				    mdoc->parse, line, ppos, p);
248 		}
249 	}
250 	return(TOKEN_NONE);
251 }
252 
253 /*
254  * Rewind up to and including a specific node.
255  */
256 static void
257 rew_last(struct roff_man *mdoc, const struct roff_node *to)
258 {
259 	struct roff_node	*np;
260 
261 	if (to->flags & MDOC_VALID)
262 		return;
263 
264 	while (mdoc->last != to) {
265 		/*
266 		 * Save the parent here, because we may delete the
267 		 * mdoc->last node in the post-validation phase and reset
268 		 * it to mdoc->last->parent, causing a step in the closing
269 		 * out to be lost.
270 		 */
271 		np = mdoc->last->parent;
272 		mdoc_valid_post(mdoc);
273 		mdoc->last = np;
274 		assert(mdoc->last);
275 	}
276 	mdoc->next = ROFF_NEXT_SIBLING;
277 	mdoc_valid_post(mdoc);
278 }
279 
280 /*
281  * Rewind up to a specific block, including all blocks that broke it.
282  */
283 static void
284 rew_pending(struct roff_man *mdoc, const struct roff_node *n)
285 {
286 
287 	for (;;) {
288 		rew_last(mdoc, n);
289 
290 		if (mdoc->last == n) {
291 			switch (n->type) {
292 			case ROFFT_HEAD:
293 				roff_body_alloc(mdoc, n->line, n->pos,
294 				    n->tok);
295 				return;
296 			case ROFFT_BLOCK:
297 				break;
298 			default:
299 				return;
300 			}
301 			if ( ! (n->flags & MDOC_BROKEN))
302 				return;
303 		} else
304 			n = mdoc->last;
305 
306 		for (;;) {
307 			if ((n = n->parent) == NULL)
308 				return;
309 
310 			if (n->type == ROFFT_BLOCK ||
311 			    n->type == ROFFT_HEAD) {
312 				if (n->flags & MDOC_ENDED)
313 					break;
314 				else
315 					return;
316 			}
317 		}
318 	}
319 }
320 
321 /*
322  * For a block closing macro, return the corresponding opening one.
323  * Otherwise, return the macro itself.
324  */
325 static int
326 rew_alt(int tok)
327 {
328 	switch (tok) {
329 	case MDOC_Ac:
330 		return(MDOC_Ao);
331 	case MDOC_Bc:
332 		return(MDOC_Bo);
333 	case MDOC_Brc:
334 		return(MDOC_Bro);
335 	case MDOC_Dc:
336 		return(MDOC_Do);
337 	case MDOC_Ec:
338 		return(MDOC_Eo);
339 	case MDOC_Ed:
340 		return(MDOC_Bd);
341 	case MDOC_Ef:
342 		return(MDOC_Bf);
343 	case MDOC_Ek:
344 		return(MDOC_Bk);
345 	case MDOC_El:
346 		return(MDOC_Bl);
347 	case MDOC_Fc:
348 		return(MDOC_Fo);
349 	case MDOC_Oc:
350 		return(MDOC_Oo);
351 	case MDOC_Pc:
352 		return(MDOC_Po);
353 	case MDOC_Qc:
354 		return(MDOC_Qo);
355 	case MDOC_Re:
356 		return(MDOC_Rs);
357 	case MDOC_Sc:
358 		return(MDOC_So);
359 	case MDOC_Xc:
360 		return(MDOC_Xo);
361 	default:
362 		return(tok);
363 	}
364 	/* NOTREACHED */
365 }
366 
367 static void
368 rew_elem(struct roff_man *mdoc, int tok)
369 {
370 	struct roff_node *n;
371 
372 	n = mdoc->last;
373 	if (n->type != ROFFT_ELEM)
374 		n = n->parent;
375 	assert(n->type == ROFFT_ELEM);
376 	assert(tok == n->tok);
377 	rew_last(mdoc, n);
378 }
379 
380 /*
381  * If there is an open sub-block of the target requiring
382  * explicit close-out, postpone closing out the target until
383  * the rew_pending() call closing out the sub-block.
384  */
385 static int
386 find_pending(struct roff_man *mdoc, int tok, int line, int ppos,
387 	struct roff_node *target)
388 {
389 	struct roff_node	*n;
390 	int			 irc;
391 
392 	irc = 0;
393 	for (n = mdoc->last; n != NULL && n != target; n = n->parent) {
394 		if (n->flags & MDOC_ENDED) {
395 			if ( ! (n->flags & MDOC_VALID))
396 				n->flags |= MDOC_BROKEN;
397 			continue;
398 		}
399 		if (n->type == ROFFT_BLOCK &&
400 		    mdoc_macros[n->tok].flags & MDOC_EXPLICIT) {
401 			irc = 1;
402 			n->flags = MDOC_BROKEN;
403 			if (target->type == ROFFT_HEAD)
404 				target->flags = MDOC_ENDED;
405 			else if ( ! (target->flags & MDOC_ENDED)) {
406 				mandoc_vmsg(MANDOCERR_BLK_NEST,
407 				    mdoc->parse, line, ppos,
408 				    "%s breaks %s", mdoc_macronames[tok],
409 				    mdoc_macronames[n->tok]);
410 				mdoc_endbody_alloc(mdoc, line, ppos,
411 				    tok, target, ENDBODY_NOSPACE);
412 			}
413 		}
414 	}
415 	return(irc);
416 }
417 
418 /*
419  * Allocate a word and check whether it's punctuation or not.
420  * Punctuation consists of those tokens found in mdoc_isdelim().
421  */
422 static void
423 dword(struct roff_man *mdoc, int line, int col, const char *p,
424 		enum mdelim d, int may_append)
425 {
426 
427 	if (d == DELIM_MAX)
428 		d = mdoc_isdelim(p);
429 
430 	if (may_append &&
431 	    ! (mdoc->flags & (MDOC_SYNOPSIS | MDOC_KEEP | MDOC_SMOFF)) &&
432 	    d == DELIM_NONE && mdoc->last->type == ROFFT_TEXT &&
433 	    mdoc_isdelim(mdoc->last->string) == DELIM_NONE) {
434 		roff_word_append(mdoc, p);
435 		return;
436 	}
437 
438 	roff_word_alloc(mdoc, line, col, p);
439 
440 	/*
441 	 * If the word consists of a bare delimiter,
442 	 * flag the new node accordingly,
443 	 * unless doing so was vetoed by the invoking macro.
444 	 * Always clear the veto, it is only valid for one word.
445 	 */
446 
447 	if (d == DELIM_OPEN)
448 		mdoc->last->flags |= MDOC_DELIMO;
449 	else if (d == DELIM_CLOSE &&
450 	    ! (mdoc->flags & MDOC_NODELIMC) &&
451 	    mdoc->last->parent->tok != MDOC_Fd)
452 		mdoc->last->flags |= MDOC_DELIMC;
453 	mdoc->flags &= ~MDOC_NODELIMC;
454 }
455 
456 static void
457 append_delims(struct roff_man *mdoc, int line, int *pos, char *buf)
458 {
459 	char		*p;
460 	int		 la;
461 
462 	if (buf[*pos] == '\0')
463 		return;
464 
465 	for (;;) {
466 		la = *pos;
467 		if (mdoc_args(mdoc, line, pos, buf, TOKEN_NONE, &p) ==
468 		    ARGS_EOLN)
469 			break;
470 		dword(mdoc, line, la, p, DELIM_MAX, 1);
471 
472 		/*
473 		 * If we encounter end-of-sentence symbols, then trigger
474 		 * the double-space.
475 		 *
476 		 * XXX: it's easy to allow this to propagate outward to
477 		 * the last symbol, such that `. )' will cause the
478 		 * correct double-spacing.  However, (1) groff isn't
479 		 * smart enough to do this and (2) it would require
480 		 * knowing which symbols break this behaviour, for
481 		 * example, `.  ;' shouldn't propagate the double-space.
482 		 */
483 
484 		if (mandoc_eos(p, strlen(p)))
485 			mdoc->last->flags |= MDOC_EOS;
486 	}
487 }
488 
489 /*
490  * Parse one word.
491  * If it is a macro, call it and return 1.
492  * Otherwise, allocate it and return 0.
493  */
494 static int
495 macro_or_word(MACRO_PROT_ARGS, int parsed)
496 {
497 	char		*p;
498 	int		 ntok;
499 
500 	p = buf + ppos;
501 	ntok = TOKEN_NONE;
502 	if (*p == '"')
503 		p++;
504 	else if (parsed && ! (mdoc->flags & MDOC_PHRASELIT))
505 		ntok = lookup(mdoc, tok, line, ppos, p);
506 
507 	if (ntok == TOKEN_NONE) {
508 		dword(mdoc, line, ppos, p, DELIM_MAX, tok == TOKEN_NONE ||
509 		    mdoc_macros[tok].flags & MDOC_JOIN);
510 		return(0);
511 	} else {
512 		if (mdoc_macros[tok].fp == in_line_eoln)
513 			rew_elem(mdoc, tok);
514 		mdoc_macro(mdoc, ntok, line, ppos, pos, buf);
515 		if (tok == TOKEN_NONE)
516 			append_delims(mdoc, line, pos, buf);
517 		return(1);
518 	}
519 }
520 
521 /*
522  * Close out block partial/full explicit.
523  */
524 static void
525 blk_exp_close(MACRO_PROT_ARGS)
526 {
527 	struct roff_node *body;		/* Our own body. */
528 	struct roff_node *endbody;	/* Our own end marker. */
529 	struct roff_node *itblk;	/* An It block starting later. */
530 	struct roff_node *later;	/* A sub-block starting later. */
531 	struct roff_node *n;		/* Search back to our block. */
532 	struct roff_node *target;	/* For find_pending(). */
533 
534 	int		 j, lastarg, maxargs, nl, pending;
535 	enum margserr	 ac;
536 	int		 atok, ntok;
537 	char		*p;
538 
539 	nl = MDOC_NEWLINE & mdoc->flags;
540 
541 	switch (tok) {
542 	case MDOC_Ec:
543 		maxargs = 1;
544 		break;
545 	case MDOC_Ek:
546 		mdoc->flags &= ~MDOC_KEEP;
547 		/* FALLTHROUGH */
548 	default:
549 		maxargs = 0;
550 		break;
551 	}
552 
553 	/*
554 	 * Search backwards for beginnings of blocks,
555 	 * both of our own and of pending sub-blocks.
556 	 */
557 
558 	atok = rew_alt(tok);
559 	body = endbody = itblk = later = NULL;
560 	for (n = mdoc->last; n; n = n->parent) {
561 		if (n->flags & MDOC_ENDED) {
562 			if ( ! (n->flags & MDOC_VALID))
563 				n->flags |= MDOC_BROKEN;
564 			continue;
565 		}
566 
567 		/* Remember the start of our own body. */
568 
569 		if (n->type == ROFFT_BODY && atok == n->tok) {
570 			if (n->end == ENDBODY_NOT)
571 				body = n;
572 			continue;
573 		}
574 
575 		if (n->type != ROFFT_BLOCK || n->tok == MDOC_Nm)
576 			continue;
577 
578 		if (n->tok == MDOC_It) {
579 			itblk = n;
580 			continue;
581 		}
582 
583 		if (atok == n->tok) {
584 			assert(body);
585 
586 			/*
587 			 * Found the start of our own block.
588 			 * When there is no pending sub block,
589 			 * just proceed to closing out.
590 			 */
591 
592 			if (later == NULL ||
593 			    (tok == MDOC_El && itblk == NULL))
594 				break;
595 
596 			/*
597 			 * When there is a pending sub block, postpone
598 			 * closing out the current block until the
599 			 * rew_pending() closing out the sub-block.
600 			 * Mark the place where the formatting - but not
601 			 * the scope - of the current block ends.
602 			 */
603 
604 			mandoc_vmsg(MANDOCERR_BLK_NEST, mdoc->parse,
605 			    line, ppos, "%s breaks %s",
606 			    mdoc_macronames[atok],
607 			    mdoc_macronames[later->tok]);
608 
609 			endbody = mdoc_endbody_alloc(mdoc, line, ppos,
610 			    atok, body, ENDBODY_SPACE);
611 
612 			if (tok == MDOC_El)
613 				itblk->flags |= MDOC_ENDED | MDOC_BROKEN;
614 
615 			/*
616 			 * If a block closing macro taking arguments
617 			 * breaks another block, put the arguments
618 			 * into the end marker.
619 			 */
620 
621 			if (maxargs)
622 				mdoc->next = ROFF_NEXT_CHILD;
623 			break;
624 		}
625 
626 		/* Explicit blocks close out description lines. */
627 
628 		if (n->tok == MDOC_Nd) {
629 			rew_last(mdoc, n);
630 			continue;
631 		}
632 
633 		/* Breaking an open sub block. */
634 
635 		n->flags |= MDOC_BROKEN;
636 		if (later == NULL)
637 			later = n;
638 	}
639 
640 	if (body == NULL) {
641 		mandoc_msg(MANDOCERR_BLK_NOTOPEN, mdoc->parse,
642 		    line, ppos, mdoc_macronames[tok]);
643 		if (later != NULL)
644 			later->flags &= ~MDOC_BROKEN;
645 		if (maxargs && endbody == NULL) {
646 			/*
647 			 * Stray .Ec without previous .Eo:
648 			 * Break the output line, keep the arguments.
649 			 */
650 			roff_elem_alloc(mdoc, line, ppos, MDOC_br);
651 			rew_elem(mdoc, MDOC_br);
652 		}
653 	} else if (endbody == NULL) {
654 		rew_last(mdoc, body);
655 		if (maxargs)
656 			mdoc_tail_alloc(mdoc, line, ppos, atok);
657 	}
658 
659 	if ( ! (mdoc_macros[tok].flags & MDOC_PARSED)) {
660 		if (buf[*pos] != '\0')
661 			mandoc_vmsg(MANDOCERR_ARG_SKIP,
662 			    mdoc->parse, line, ppos,
663 			    "%s %s", mdoc_macronames[tok],
664 			    buf + *pos);
665 		if (endbody == NULL && n != NULL)
666 			rew_pending(mdoc, n);
667 		return;
668 	}
669 
670 	if (endbody != NULL)
671 		n = endbody;
672 
673 	ntok = TOKEN_NONE;
674 	for (j = 0; ; j++) {
675 		lastarg = *pos;
676 
677 		if (j == maxargs && n != NULL)
678 			rew_last(mdoc, n);
679 
680 		ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
681 		if (ac == ARGS_PUNCT || ac == ARGS_EOLN)
682 			break;
683 
684 		ntok = ac == ARGS_QWORD ? TOKEN_NONE :
685 		    lookup(mdoc, tok, line, lastarg, p);
686 
687 		if (ntok == TOKEN_NONE) {
688 			dword(mdoc, line, lastarg, p, DELIM_MAX,
689 			    MDOC_JOIN & mdoc_macros[tok].flags);
690 			continue;
691 		}
692 
693 		if (n != NULL)
694 			rew_last(mdoc, n);
695 		mdoc->flags &= ~MDOC_NEWLINE;
696 		mdoc_macro(mdoc, ntok, line, lastarg, pos, buf);
697 		break;
698 	}
699 
700 	if (n != NULL) {
701 		if (ntok != TOKEN_NONE && n->flags & MDOC_BROKEN) {
702 			target = n;
703 			do
704 				target = target->parent;
705 			while ( ! (target->flags & MDOC_ENDED));
706 			pending = find_pending(mdoc, ntok, line, ppos,
707 			    target);
708 		} else
709 			pending = 0;
710 		if ( ! pending)
711 			rew_pending(mdoc, n);
712 	}
713 	if (nl)
714 		append_delims(mdoc, line, pos, buf);
715 }
716 
717 static void
718 in_line(MACRO_PROT_ARGS)
719 {
720 	int		 la, scope, cnt, firstarg, mayopen, nc, nl;
721 	int		 ntok;
722 	enum margserr	 ac;
723 	enum mdelim	 d;
724 	struct mdoc_arg	*arg;
725 	char		*p;
726 
727 	nl = MDOC_NEWLINE & mdoc->flags;
728 
729 	/*
730 	 * Whether we allow ignored elements (those without content,
731 	 * usually because of reserved words) to squeak by.
732 	 */
733 
734 	switch (tok) {
735 	case MDOC_An:
736 		/* FALLTHROUGH */
737 	case MDOC_Ar:
738 		/* FALLTHROUGH */
739 	case MDOC_Fl:
740 		/* FALLTHROUGH */
741 	case MDOC_Mt:
742 		/* FALLTHROUGH */
743 	case MDOC_Nm:
744 		/* FALLTHROUGH */
745 	case MDOC_Pa:
746 		nc = 1;
747 		break;
748 	default:
749 		nc = 0;
750 		break;
751 	}
752 
753 	mdoc_argv(mdoc, line, tok, &arg, pos, buf);
754 
755 	d = DELIM_NONE;
756 	firstarg = 1;
757 	mayopen = 1;
758 	for (cnt = scope = 0;; ) {
759 		la = *pos;
760 		ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
761 
762 		/*
763 		 * At the end of a macro line,
764 		 * opening delimiters do not suppress spacing.
765 		 */
766 
767 		if (ac == ARGS_EOLN) {
768 			if (d == DELIM_OPEN)
769 				mdoc->last->flags &= ~MDOC_DELIMO;
770 			break;
771 		}
772 
773 		/*
774 		 * The rest of the macro line is only punctuation,
775 		 * to be handled by append_delims().
776 		 * If there were no other arguments,
777 		 * do not allow the first one to suppress spacing,
778 		 * even if it turns out to be a closing one.
779 		 */
780 
781 		if (ac == ARGS_PUNCT) {
782 			if (cnt == 0 && (nc == 0 || tok == MDOC_An))
783 				mdoc->flags |= MDOC_NODELIMC;
784 			break;
785 		}
786 
787 		ntok = (ac == ARGS_QWORD || (tok == MDOC_Fn && !cnt)) ?
788 		    TOKEN_NONE : lookup(mdoc, tok, line, la, p);
789 
790 		/*
791 		 * In this case, we've located a submacro and must
792 		 * execute it.  Close out scope, if open.  If no
793 		 * elements have been generated, either create one (nc)
794 		 * or raise a warning.
795 		 */
796 
797 		if (ntok != TOKEN_NONE) {
798 			if (scope)
799 				rew_elem(mdoc, tok);
800 			if (nc && ! cnt) {
801 				mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
802 				rew_last(mdoc, mdoc->last);
803 			} else if ( ! nc && ! cnt) {
804 				mdoc_argv_free(arg);
805 				mandoc_msg(MANDOCERR_MACRO_EMPTY,
806 				    mdoc->parse, line, ppos,
807 				    mdoc_macronames[tok]);
808 			}
809 			mdoc_macro(mdoc, ntok, line, la, pos, buf);
810 			if (nl)
811 				append_delims(mdoc, line, pos, buf);
812 			return;
813 		}
814 
815 		/*
816 		 * Non-quote-enclosed punctuation.  Set up our scope, if
817 		 * a word; rewind the scope, if a delimiter; then append
818 		 * the word.
819 		 */
820 
821 		d = ac == ARGS_QWORD ? DELIM_NONE : mdoc_isdelim(p);
822 
823 		if (DELIM_NONE != d) {
824 			/*
825 			 * If we encounter closing punctuation, no word
826 			 * has been emitted, no scope is open, and we're
827 			 * allowed to have an empty element, then start
828 			 * a new scope.
829 			 */
830 			if ((d == DELIM_CLOSE ||
831 			     (d == DELIM_MIDDLE && tok == MDOC_Fl)) &&
832 			    !cnt && !scope && nc && mayopen) {
833 				mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
834 				scope = 1;
835 				cnt++;
836 				if (tok == MDOC_Nm)
837 					mayopen = 0;
838 			}
839 			/*
840 			 * Close out our scope, if one is open, before
841 			 * any punctuation.
842 			 */
843 			if (scope)
844 				rew_elem(mdoc, tok);
845 			scope = 0;
846 			if (tok == MDOC_Fn)
847 				mayopen = 0;
848 		} else if (mayopen && !scope) {
849 			mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
850 			scope = 1;
851 			cnt++;
852 		}
853 
854 		dword(mdoc, line, la, p, d,
855 		    MDOC_JOIN & mdoc_macros[tok].flags);
856 
857 		/*
858 		 * If the first argument is a closing delimiter,
859 		 * do not suppress spacing before it.
860 		 */
861 
862 		if (firstarg && d == DELIM_CLOSE && !nc)
863 			mdoc->last->flags &= ~MDOC_DELIMC;
864 		firstarg = 0;
865 
866 		/*
867 		 * `Fl' macros have their scope re-opened with each new
868 		 * word so that the `-' can be added to each one without
869 		 * having to parse out spaces.
870 		 */
871 		if (scope && tok == MDOC_Fl) {
872 			rew_elem(mdoc, tok);
873 			scope = 0;
874 		}
875 	}
876 
877 	if (scope)
878 		rew_elem(mdoc, tok);
879 
880 	/*
881 	 * If no elements have been collected and we're allowed to have
882 	 * empties (nc), open a scope and close it out.  Otherwise,
883 	 * raise a warning.
884 	 */
885 
886 	if ( ! cnt) {
887 		if (nc) {
888 			mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
889 			rew_last(mdoc, mdoc->last);
890 		} else {
891 			mdoc_argv_free(arg);
892 			mandoc_msg(MANDOCERR_MACRO_EMPTY, mdoc->parse,
893 			    line, ppos, mdoc_macronames[tok]);
894 		}
895 	}
896 	if (nl)
897 		append_delims(mdoc, line, pos, buf);
898 }
899 
900 static void
901 blk_full(MACRO_PROT_ARGS)
902 {
903 	int		  la, nl, parsed;
904 	struct mdoc_arg	 *arg;
905 	struct roff_node *blk; /* Our own or a broken block. */
906 	struct roff_node *head; /* Our own head. */
907 	struct roff_node *body; /* Our own body. */
908 	struct roff_node *n;
909 	enum margserr	  ac, lac;
910 	char		 *p;
911 
912 	nl = MDOC_NEWLINE & mdoc->flags;
913 
914 	if (buf[*pos] == '\0' && (tok == MDOC_Sh || tok == MDOC_Ss)) {
915 		mandoc_msg(MANDOCERR_MACRO_EMPTY, mdoc->parse,
916 		    line, ppos, mdoc_macronames[tok]);
917 		return;
918 	}
919 
920 	if ( ! (mdoc_macros[tok].flags & MDOC_EXPLICIT)) {
921 
922 		/* Here, tok is one of Sh Ss Nm Nd It. */
923 
924 		blk = NULL;
925 		for (n = mdoc->last; n != NULL; n = n->parent) {
926 			if (n->flags & MDOC_ENDED) {
927 				if ( ! (n->flags & MDOC_VALID))
928 					n->flags |= MDOC_BROKEN;
929 				continue;
930 			}
931 			if (n->type != ROFFT_BLOCK)
932 				continue;
933 
934 			if (tok == MDOC_It && n->tok == MDOC_Bl) {
935 				if (blk != NULL) {
936 					mandoc_vmsg(MANDOCERR_BLK_BROKEN,
937 					    mdoc->parse, line, ppos,
938 					    "It breaks %s",
939 					    mdoc_macronames[blk->tok]);
940 					rew_pending(mdoc, blk);
941 				}
942 				break;
943 			}
944 
945 			if (mdoc_macros[n->tok].flags & MDOC_EXPLICIT) {
946 				switch (tok) {
947 				case MDOC_Sh:
948 					/* FALLTHROUGH */
949 				case MDOC_Ss:
950 					mandoc_vmsg(MANDOCERR_BLK_BROKEN,
951 					    mdoc->parse, line, ppos,
952 					    "%s breaks %s",
953 					    mdoc_macronames[tok],
954 					    mdoc_macronames[n->tok]);
955 					rew_pending(mdoc, n);
956 					n = mdoc->last;
957 					continue;
958 				case MDOC_It:
959 					/* Delay in case it's astray. */
960 					blk = n;
961 					continue;
962 				default:
963 					break;
964 				}
965 				break;
966 			}
967 
968 			/* Here, n is one of Sh Ss Nm Nd It. */
969 
970 			if (tok != MDOC_Sh && (n->tok == MDOC_Sh ||
971 			    (tok != MDOC_Ss && (n->tok == MDOC_Ss ||
972 			     (tok != MDOC_It && n->tok == MDOC_It)))))
973 				break;
974 
975 			/* Item breaking an explicit block. */
976 
977 			if (blk != NULL) {
978 				mandoc_vmsg(MANDOCERR_BLK_BROKEN,
979 				    mdoc->parse, line, ppos,
980 				    "It breaks %s",
981 				    mdoc_macronames[blk->tok]);
982 				rew_pending(mdoc, blk);
983 				blk = NULL;
984 			}
985 
986 			/* Close out prior implicit scopes. */
987 
988 			rew_last(mdoc, n);
989 		}
990 
991 		/* Skip items outside lists. */
992 
993 		if (tok == MDOC_It && (n == NULL || n->tok != MDOC_Bl)) {
994 			mandoc_vmsg(MANDOCERR_IT_STRAY, mdoc->parse,
995 			    line, ppos, "It %s", buf + *pos);
996 			roff_elem_alloc(mdoc, line, ppos, MDOC_br);
997 			rew_elem(mdoc, MDOC_br);
998 			return;
999 		}
1000 	}
1001 
1002 	/*
1003 	 * This routine accommodates implicitly- and explicitly-scoped
1004 	 * macro openings.  Implicit ones first close out prior scope
1005 	 * (seen above).  Delay opening the head until necessary to
1006 	 * allow leading punctuation to print.  Special consideration
1007 	 * for `It -column', which has phrase-part syntax instead of
1008 	 * regular child nodes.
1009 	 */
1010 
1011 	mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1012 	blk = mdoc_block_alloc(mdoc, line, ppos, tok, arg);
1013 	head = body = NULL;
1014 
1015 	/*
1016 	 * Exception: Heads of `It' macros in `-diag' lists are not
1017 	 * parsed, even though `It' macros in general are parsed.
1018 	 */
1019 
1020 	parsed = tok != MDOC_It ||
1021 	    mdoc->last->parent->tok != MDOC_Bl ||
1022 	    mdoc->last->parent->norm->Bl.type != LIST_diag;
1023 
1024 	/*
1025 	 * The `Nd' macro has all arguments in its body: it's a hybrid
1026 	 * of block partial-explicit and full-implicit.  Stupid.
1027 	 */
1028 
1029 	if (tok == MDOC_Nd) {
1030 		head = roff_head_alloc(mdoc, line, ppos, tok);
1031 		rew_last(mdoc, head);
1032 		body = roff_body_alloc(mdoc, line, ppos, tok);
1033 	}
1034 
1035 	if (tok == MDOC_Bk)
1036 		mdoc->flags |= MDOC_KEEP;
1037 
1038 	ac = ARGS_PEND;
1039 	for (;;) {
1040 		la = *pos;
1041 		lac = ac;
1042 		ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1043 		if (ac == ARGS_EOLN) {
1044 			if (lac != ARGS_PPHRASE && lac != ARGS_PHRASE)
1045 				break;
1046 			/*
1047 			 * This is necessary: if the last token on a
1048 			 * line is a `Ta' or tab, then we'll get
1049 			 * ARGS_EOLN, so we must be smart enough to
1050 			 * reopen our scope if the last parse was a
1051 			 * phrase or partial phrase.
1052 			 */
1053 			if (body != NULL)
1054 				rew_last(mdoc, body);
1055 			body = roff_body_alloc(mdoc, line, ppos, tok);
1056 			break;
1057 		}
1058 		if (tok == MDOC_Bd || tok == MDOC_Bk) {
1059 			mandoc_vmsg(MANDOCERR_ARG_EXCESS,
1060 			    mdoc->parse, line, la, "%s ... %s",
1061 			    mdoc_macronames[tok], buf + la);
1062 			break;
1063 		}
1064 		if (tok == MDOC_Rs) {
1065 			mandoc_vmsg(MANDOCERR_ARG_SKIP, mdoc->parse,
1066 			    line, la, "Rs %s", buf + la);
1067 			break;
1068 		}
1069 		if (ac == ARGS_PUNCT)
1070 			break;
1071 
1072 		/*
1073 		 * Emit leading punctuation (i.e., punctuation before
1074 		 * the ROFFT_HEAD) for non-phrase types.
1075 		 */
1076 
1077 		if (head == NULL &&
1078 		    ac != ARGS_PEND &&
1079 		    ac != ARGS_PHRASE &&
1080 		    ac != ARGS_PPHRASE &&
1081 		    ac != ARGS_QWORD &&
1082 		    mdoc_isdelim(p) == DELIM_OPEN) {
1083 			dword(mdoc, line, la, p, DELIM_OPEN, 0);
1084 			continue;
1085 		}
1086 
1087 		/* Open a head if one hasn't been opened. */
1088 
1089 		if (head == NULL)
1090 			head = roff_head_alloc(mdoc, line, ppos, tok);
1091 
1092 		if (ac == ARGS_PHRASE ||
1093 		    ac == ARGS_PEND ||
1094 		    ac == ARGS_PPHRASE) {
1095 
1096 			/*
1097 			 * If we haven't opened a body yet, rewind the
1098 			 * head; if we have, rewind that instead.
1099 			 */
1100 
1101 			rew_last(mdoc, body == NULL ? head : body);
1102 			body = roff_body_alloc(mdoc, line, ppos, tok);
1103 
1104 			/*
1105 			 * Process phrases: set whether we're in a
1106 			 * partial-phrase (this effects line handling)
1107 			 * then call down into the phrase parser.
1108 			 */
1109 
1110 			if (ac == ARGS_PPHRASE)
1111 				mdoc->flags |= MDOC_PPHRASE;
1112 			if (ac == ARGS_PEND && lac == ARGS_PPHRASE)
1113 				mdoc->flags |= MDOC_PPHRASE;
1114 			parse_rest(mdoc, TOKEN_NONE, line, &la, buf);
1115 			mdoc->flags &= ~MDOC_PPHRASE;
1116 			continue;
1117 		}
1118 
1119 		if (macro_or_word(mdoc, tok, line, la, pos, buf, parsed))
1120 			break;
1121 	}
1122 
1123 	if (blk->flags & MDOC_VALID)
1124 		return;
1125 	if (head == NULL)
1126 		head = roff_head_alloc(mdoc, line, ppos, tok);
1127 	if (nl && tok != MDOC_Bd && tok != MDOC_Bl && tok != MDOC_Rs)
1128 		append_delims(mdoc, line, pos, buf);
1129 	if (body != NULL)
1130 		goto out;
1131 	if (find_pending(mdoc, tok, line, ppos, head))
1132 		return;
1133 
1134 	/* Close out scopes to remain in a consistent state. */
1135 
1136 	rew_last(mdoc, head);
1137 	body = roff_body_alloc(mdoc, line, ppos, tok);
1138 out:
1139 	if (mdoc->flags & MDOC_FREECOL) {
1140 		rew_last(mdoc, body);
1141 		rew_last(mdoc, blk);
1142 		mdoc->flags &= ~MDOC_FREECOL;
1143 	}
1144 }
1145 
1146 static void
1147 blk_part_imp(MACRO_PROT_ARGS)
1148 {
1149 	int		  la, nl;
1150 	enum margserr	  ac;
1151 	char		 *p;
1152 	struct roff_node *blk; /* saved block context */
1153 	struct roff_node *body; /* saved body context */
1154 	struct roff_node *n;
1155 
1156 	nl = MDOC_NEWLINE & mdoc->flags;
1157 
1158 	/*
1159 	 * A macro that spans to the end of the line.  This is generally
1160 	 * (but not necessarily) called as the first macro.  The block
1161 	 * has a head as the immediate child, which is always empty,
1162 	 * followed by zero or more opening punctuation nodes, then the
1163 	 * body (which may be empty, depending on the macro), then zero
1164 	 * or more closing punctuation nodes.
1165 	 */
1166 
1167 	blk = mdoc_block_alloc(mdoc, line, ppos, tok, NULL);
1168 	rew_last(mdoc, roff_head_alloc(mdoc, line, ppos, tok));
1169 
1170 	/*
1171 	 * Open the body scope "on-demand", that is, after we've
1172 	 * processed all our the leading delimiters (open parenthesis,
1173 	 * etc.).
1174 	 */
1175 
1176 	for (body = NULL; ; ) {
1177 		la = *pos;
1178 		ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1179 		if (ac == ARGS_EOLN || ac == ARGS_PUNCT)
1180 			break;
1181 
1182 		if (body == NULL && ac != ARGS_QWORD &&
1183 		    mdoc_isdelim(p) == DELIM_OPEN) {
1184 			dword(mdoc, line, la, p, DELIM_OPEN, 0);
1185 			continue;
1186 		}
1187 
1188 		if (body == NULL)
1189 			body = roff_body_alloc(mdoc, line, ppos, tok);
1190 
1191 		if (macro_or_word(mdoc, tok, line, la, pos, buf, 1))
1192 			break;
1193 	}
1194 	if (body == NULL)
1195 		body = roff_body_alloc(mdoc, line, ppos, tok);
1196 
1197 	if (find_pending(mdoc, tok, line, ppos, body))
1198 		return;
1199 
1200 	rew_last(mdoc, body);
1201 	if (nl)
1202 		append_delims(mdoc, line, pos, buf);
1203 	rew_pending(mdoc, blk);
1204 
1205 	/* Move trailing .Ns out of scope. */
1206 
1207 	for (n = body->child; n && n->next; n = n->next)
1208 		/* Do nothing. */ ;
1209 	if (n && n->tok == MDOC_Ns)
1210 		mdoc_node_relink(mdoc, n);
1211 }
1212 
1213 static void
1214 blk_part_exp(MACRO_PROT_ARGS)
1215 {
1216 	int		  la, nl;
1217 	enum margserr	  ac;
1218 	struct roff_node *head; /* keep track of head */
1219 	char		 *p;
1220 
1221 	nl = MDOC_NEWLINE & mdoc->flags;
1222 
1223 	/*
1224 	 * The opening of an explicit macro having zero or more leading
1225 	 * punctuation nodes; a head with optional single element (the
1226 	 * case of `Eo'); and a body that may be empty.
1227 	 */
1228 
1229 	roff_block_alloc(mdoc, line, ppos, tok);
1230 	head = NULL;
1231 	for (;;) {
1232 		la = *pos;
1233 		ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1234 		if (ac == ARGS_PUNCT || ac == ARGS_EOLN)
1235 			break;
1236 
1237 		/* Flush out leading punctuation. */
1238 
1239 		if (head == NULL && ac != ARGS_QWORD &&
1240 		    mdoc_isdelim(p) == DELIM_OPEN) {
1241 			dword(mdoc, line, la, p, DELIM_OPEN, 0);
1242 			continue;
1243 		}
1244 
1245 		if (head == NULL) {
1246 			head = roff_head_alloc(mdoc, line, ppos, tok);
1247 			if (tok == MDOC_Eo)  /* Not parsed. */
1248 				dword(mdoc, line, la, p, DELIM_MAX, 0);
1249 			rew_last(mdoc, head);
1250 			roff_body_alloc(mdoc, line, ppos, tok);
1251 			if (tok == MDOC_Eo)
1252 				continue;
1253 		}
1254 
1255 		if (macro_or_word(mdoc, tok, line, la, pos, buf, 1))
1256 			break;
1257 	}
1258 
1259 	/* Clean-up to leave in a consistent state. */
1260 
1261 	if (head == NULL) {
1262 		rew_last(mdoc, roff_head_alloc(mdoc, line, ppos, tok));
1263 		roff_body_alloc(mdoc, line, ppos, tok);
1264 	}
1265 	if (nl)
1266 		append_delims(mdoc, line, pos, buf);
1267 }
1268 
1269 static void
1270 in_line_argn(MACRO_PROT_ARGS)
1271 {
1272 	struct mdoc_arg	*arg;
1273 	char		*p;
1274 	enum margserr	 ac;
1275 	int		 ntok;
1276 	int		 state; /* arg#; -1: not yet open; -2: closed */
1277 	int		 la, maxargs, nl;
1278 
1279 	nl = mdoc->flags & MDOC_NEWLINE;
1280 
1281 	/*
1282 	 * A line macro that has a fixed number of arguments (maxargs).
1283 	 * Only open the scope once the first non-leading-punctuation is
1284 	 * found (unless MDOC_IGNDELIM is noted, like in `Pf'), then
1285 	 * keep it open until the maximum number of arguments are
1286 	 * exhausted.
1287 	 */
1288 
1289 	switch (tok) {
1290 	case MDOC_Ap:
1291 		/* FALLTHROUGH */
1292 	case MDOC_Ns:
1293 		/* FALLTHROUGH */
1294 	case MDOC_Ux:
1295 		maxargs = 0;
1296 		break;
1297 	case MDOC_Bx:
1298 		/* FALLTHROUGH */
1299 	case MDOC_Es:
1300 		/* FALLTHROUGH */
1301 	case MDOC_Xr:
1302 		maxargs = 2;
1303 		break;
1304 	default:
1305 		maxargs = 1;
1306 		break;
1307 	}
1308 
1309 	mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1310 
1311 	state = -1;
1312 	p = NULL;
1313 	for (;;) {
1314 		la = *pos;
1315 		ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1316 
1317 		if (ac == ARGS_WORD && state == -1 &&
1318 		    ! (mdoc_macros[tok].flags & MDOC_IGNDELIM) &&
1319 		    mdoc_isdelim(p) == DELIM_OPEN) {
1320 			dword(mdoc, line, la, p, DELIM_OPEN, 0);
1321 			continue;
1322 		}
1323 
1324 		if (state == -1 && tok != MDOC_In &&
1325 		    tok != MDOC_St && tok != MDOC_Xr) {
1326 			mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
1327 			state = 0;
1328 		}
1329 
1330 		if (ac == ARGS_PUNCT || ac == ARGS_EOLN) {
1331 			if (abs(state) < 2 && tok == MDOC_Pf)
1332 				mandoc_vmsg(MANDOCERR_PF_SKIP,
1333 				    mdoc->parse, line, ppos, "Pf %s",
1334 				    p == NULL ? "at eol" : p);
1335 			break;
1336 		}
1337 
1338 		if (state == maxargs) {
1339 			rew_elem(mdoc, tok);
1340 			state = -2;
1341 		}
1342 
1343 		ntok = (ac == ARGS_QWORD || (tok == MDOC_Pf && state == 0)) ?
1344 		    TOKEN_NONE : lookup(mdoc, tok, line, la, p);
1345 
1346 		if (ntok != TOKEN_NONE) {
1347 			if (state >= 0) {
1348 				rew_elem(mdoc, tok);
1349 				state = -2;
1350 			}
1351 			mdoc_macro(mdoc, ntok, line, la, pos, buf);
1352 			break;
1353 		}
1354 
1355 		if (ac == ARGS_QWORD ||
1356 		    mdoc_macros[tok].flags & MDOC_IGNDELIM ||
1357 		    mdoc_isdelim(p) == DELIM_NONE) {
1358 			if (state == -1) {
1359 				mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
1360 				state = 1;
1361 			} else if (state >= 0)
1362 				state++;
1363 		} else if (state >= 0) {
1364 			rew_elem(mdoc, tok);
1365 			state = -2;
1366 		}
1367 
1368 		dword(mdoc, line, la, p, DELIM_MAX,
1369 		    MDOC_JOIN & mdoc_macros[tok].flags);
1370 	}
1371 
1372 	if (state == -1) {
1373 		mandoc_msg(MANDOCERR_MACRO_EMPTY, mdoc->parse,
1374 		    line, ppos, mdoc_macronames[tok]);
1375 		return;
1376 	}
1377 
1378 	if (state == 0 && tok == MDOC_Pf)
1379 		append_delims(mdoc, line, pos, buf);
1380 	if (state >= 0)
1381 		rew_elem(mdoc, tok);
1382 	if (nl)
1383 		append_delims(mdoc, line, pos, buf);
1384 }
1385 
1386 static void
1387 in_line_eoln(MACRO_PROT_ARGS)
1388 {
1389 	struct roff_node	*n;
1390 	struct mdoc_arg		*arg;
1391 
1392 	if ((tok == MDOC_Pp || tok == MDOC_Lp) &&
1393 	    ! (mdoc->flags & MDOC_SYNOPSIS)) {
1394 		n = mdoc->last;
1395 		if (mdoc->next == ROFF_NEXT_SIBLING)
1396 			n = n->parent;
1397 		if (n->tok == MDOC_Nm)
1398 			rew_last(mdoc, n->parent);
1399 	}
1400 
1401 	if (buf[*pos] == '\0' &&
1402 	    (tok == MDOC_Fd || mdoc_macronames[tok][0] == '%')) {
1403 		mandoc_msg(MANDOCERR_MACRO_EMPTY, mdoc->parse,
1404 		    line, ppos, mdoc_macronames[tok]);
1405 		return;
1406 	}
1407 
1408 	mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1409 	mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
1410 	if (parse_rest(mdoc, tok, line, pos, buf))
1411 		return;
1412 	rew_elem(mdoc, tok);
1413 }
1414 
1415 /*
1416  * The simplest argument parser available: Parse the remaining
1417  * words until the end of the phrase or line and return 0
1418  * or until the next macro, call that macro, and return 1.
1419  */
1420 static int
1421 parse_rest(struct roff_man *mdoc, int tok, int line, int *pos, char *buf)
1422 {
1423 	int		 la;
1424 
1425 	for (;;) {
1426 		la = *pos;
1427 		if (mdoc_args(mdoc, line, pos, buf, tok, NULL) == ARGS_EOLN)
1428 			return(0);
1429 		if (macro_or_word(mdoc, tok, line, la, pos, buf, 1))
1430 			return(1);
1431 	}
1432 }
1433 
1434 static void
1435 ctx_synopsis(MACRO_PROT_ARGS)
1436 {
1437 
1438 	if (~mdoc->flags & (MDOC_SYNOPSIS | MDOC_NEWLINE))
1439 		in_line(mdoc, tok, line, ppos, pos, buf);
1440 	else if (tok == MDOC_Nm)
1441 		blk_full(mdoc, tok, line, ppos, pos, buf);
1442 	else {
1443 		assert(tok == MDOC_Vt);
1444 		blk_part_imp(mdoc, tok, line, ppos, pos, buf);
1445 	}
1446 }
1447 
1448 /*
1449  * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
1450  * They're unusual because they're basically free-form text until a
1451  * macro is encountered.
1452  */
1453 static void
1454 phrase_ta(MACRO_PROT_ARGS)
1455 {
1456 	struct roff_node *body, *n;
1457 
1458 	/* Make sure we are in a column list or ignore this macro. */
1459 
1460 	body = NULL;
1461 	for (n = mdoc->last; n != NULL; n = n->parent) {
1462 		if (n->flags & MDOC_ENDED)
1463 			continue;
1464 		if (n->tok == MDOC_It && n->type == ROFFT_BODY)
1465 			body = n;
1466 		if (n->tok == MDOC_Bl)
1467 			break;
1468 	}
1469 
1470 	if (n == NULL || n->norm->Bl.type != LIST_column) {
1471 		mandoc_msg(MANDOCERR_TA_STRAY, mdoc->parse,
1472 		    line, ppos, "Ta");
1473 		return;
1474 	}
1475 
1476 	/* Advance to the next column. */
1477 
1478 	rew_last(mdoc, body);
1479 	roff_body_alloc(mdoc, line, ppos, MDOC_It);
1480 	parse_rest(mdoc, TOKEN_NONE, line, pos, buf);
1481 }
1482