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