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