xref: /openbsd-src/usr.bin/mandoc/mdoc_macro.c (revision 48950c12d106c85f315112191a0228d7b83b9510)
1 /*	$Id: mdoc_macro.c,v 1.78 2012/11/19 22:28:35 schwarze Exp $ */
2 /*
3  * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4  * Copyright (c) 2010, 2012 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 AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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 <assert.h>
19 #include <ctype.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <time.h>
24 
25 #include "mdoc.h"
26 #include "mandoc.h"
27 #include "libmdoc.h"
28 #include "libmandoc.h"
29 
30 enum	rew {	/* see rew_dohalt() */
31 	REWIND_NONE,
32 	REWIND_THIS,
33 	REWIND_MORE,
34 	REWIND_FORCE,
35 	REWIND_LATER,
36 	REWIND_ERROR
37 };
38 
39 static	int	  	blk_full(MACRO_PROT_ARGS);
40 static	int	  	blk_exp_close(MACRO_PROT_ARGS);
41 static	int	  	blk_part_exp(MACRO_PROT_ARGS);
42 static	int	  	blk_part_imp(MACRO_PROT_ARGS);
43 static	int	  	ctx_synopsis(MACRO_PROT_ARGS);
44 static	int	  	in_line_eoln(MACRO_PROT_ARGS);
45 static	int	  	in_line_argn(MACRO_PROT_ARGS);
46 static	int	  	in_line(MACRO_PROT_ARGS);
47 static	int	  	obsolete(MACRO_PROT_ARGS);
48 static	int	  	phrase_ta(MACRO_PROT_ARGS);
49 
50 static	int		dword(struct mdoc *, int, int,
51 				const char *, enum mdelim);
52 static	int	  	append_delims(struct mdoc *,
53 				int, int *, char *);
54 static	enum mdoct	lookup(enum mdoct, const char *);
55 static	enum mdoct	lookup_raw(const char *);
56 static	int		make_pending(struct mdoc_node *, enum mdoct,
57 				struct mdoc *, int, int);
58 static	int	  	phrase(struct mdoc *, int, int, char *);
59 static	enum mdoct 	rew_alt(enum mdoct);
60 static	enum rew  	rew_dohalt(enum mdoct, enum mdoc_type,
61 				const struct mdoc_node *);
62 static	int	  	rew_elem(struct mdoc *, enum mdoct);
63 static	int	  	rew_last(struct mdoc *,
64 				const struct mdoc_node *);
65 static	int	  	rew_sub(enum mdoc_type, struct mdoc *,
66 				enum mdoct, int, int);
67 
68 const	struct mdoc_macro __mdoc_macros[MDOC_MAX] = {
69 	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ap */
70 	{ in_line_eoln, MDOC_PROLOGUE }, /* Dd */
71 	{ in_line_eoln, MDOC_PROLOGUE }, /* Dt */
72 	{ in_line_eoln, MDOC_PROLOGUE }, /* Os */
73 	{ blk_full, MDOC_PARSED }, /* Sh */
74 	{ blk_full, MDOC_PARSED }, /* Ss */
75 	{ in_line_eoln, 0 }, /* Pp */
76 	{ blk_part_imp, MDOC_PARSED }, /* D1 */
77 	{ blk_part_imp, MDOC_PARSED }, /* Dl */
78 	{ blk_full, MDOC_EXPLICIT }, /* Bd */
79 	{ blk_exp_close, MDOC_EXPLICIT }, /* Ed */
80 	{ blk_full, MDOC_EXPLICIT }, /* Bl */
81 	{ blk_exp_close, MDOC_EXPLICIT }, /* El */
82 	{ blk_full, MDOC_PARSED }, /* It */
83 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ad */
84 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* An */
85 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ar */
86 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Cd */
87 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Cm */
88 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Dv */
89 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Er */
90 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ev */
91 	{ in_line_eoln, 0 }, /* Ex */
92 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fa */
93 	{ in_line_eoln, 0 }, /* Fd */
94 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fl */
95 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fn */
96 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ft */
97 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ic */
98 	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* In */
99 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Li */
100 	{ blk_full, 0 }, /* Nd */
101 	{ ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED }, /* Nm */
102 	{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Op */
103 	{ obsolete, 0 }, /* Ot */
104 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Pa */
105 	{ in_line_eoln, 0 }, /* Rv */
106 	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* St */
107 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Va */
108 	{ ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED }, /* Vt */
109 	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Xr */
110 	{ in_line_eoln, 0 }, /* %A */
111 	{ in_line_eoln, 0 }, /* %B */
112 	{ in_line_eoln, 0 }, /* %D */
113 	{ in_line_eoln, 0 }, /* %I */
114 	{ in_line_eoln, 0 }, /* %J */
115 	{ in_line_eoln, 0 }, /* %N */
116 	{ in_line_eoln, 0 }, /* %O */
117 	{ in_line_eoln, 0 }, /* %P */
118 	{ in_line_eoln, 0 }, /* %R */
119 	{ in_line_eoln, 0 }, /* %T */
120 	{ in_line_eoln, 0 }, /* %V */
121 	{ blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Ac */
122 	{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Ao */
123 	{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Aq */
124 	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* At */
125 	{ blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Bc */
126 	{ blk_full, MDOC_EXPLICIT }, /* Bf */
127 	{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Bo */
128 	{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* 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_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Dc */
133 	{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Do */
134 	{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Dq */
135 	{ blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Ec */
136 	{ blk_exp_close, MDOC_EXPLICIT }, /* Ef */
137 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Em */
138 	{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Eo */
139 	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Fx */
140 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ms */
141 	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_IGNDELIM }, /* No */
142 	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_IGNDELIM }, /* Ns */
143 	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Nx */
144 	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ox */
145 	{ blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Pc */
146 	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_IGNDELIM }, /* Pf */
147 	{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Po */
148 	{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Pq */
149 	{ blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Qc */
150 	{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Ql */
151 	{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Qo */
152 	{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Qq */
153 	{ blk_exp_close, MDOC_EXPLICIT }, /* Re */
154 	{ blk_full, MDOC_EXPLICIT }, /* Rs */
155 	{ blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Sc */
156 	{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* So */
157 	{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Sq */
158 	{ in_line_eoln, 0 }, /* Sm */
159 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Sx */
160 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Sy */
161 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Tn */
162 	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ux */
163 	{ blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Xc */
164 	{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Xo */
165 	{ blk_full, MDOC_EXPLICIT | MDOC_CALLABLE }, /* Fo */
166 	{ blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Fc */
167 	{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Oo */
168 	{ blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Oc */
169 	{ blk_full, MDOC_EXPLICIT }, /* Bk */
170 	{ blk_exp_close, MDOC_EXPLICIT }, /* Ek */
171 	{ in_line_eoln, 0 }, /* Bt */
172 	{ in_line_eoln, 0 }, /* Hf */
173 	{ obsolete, 0 }, /* Fr */
174 	{ in_line_eoln, 0 }, /* Ud */
175 	{ in_line, 0 }, /* Lb */
176 	{ in_line_eoln, 0 }, /* Lp */
177 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Lk */
178 	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Mt */
179 	{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Brq */
180 	{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Bro */
181 	{ blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Brc */
182 	{ in_line_eoln, 0 }, /* %C */
183 	{ obsolete, 0 }, /* Es */
184 	{ obsolete, 0 }, /* En */
185 	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Dx */
186 	{ in_line_eoln, 0 }, /* %Q */
187 	{ in_line_eoln, 0 }, /* br */
188 	{ in_line_eoln, 0 }, /* sp */
189 	{ in_line_eoln, 0 }, /* %U */
190 	{ phrase_ta, MDOC_CALLABLE | MDOC_PARSED }, /* Ta */
191 };
192 
193 const	struct mdoc_macro * const mdoc_macros = __mdoc_macros;
194 
195 
196 /*
197  * This is called at the end of parsing.  It must traverse up the tree,
198  * closing out open [implicit] scopes.  Obviously, open explicit scopes
199  * are errors.
200  */
201 int
202 mdoc_macroend(struct mdoc *mdoc)
203 {
204 	struct mdoc_node *n;
205 
206 	/* Scan for open explicit scopes. */
207 
208 	n = MDOC_VALID & mdoc->last->flags ?
209 			mdoc->last->parent : mdoc->last;
210 
211 	for ( ; n; n = n->parent)
212 		if (MDOC_BLOCK == n->type &&
213 		    MDOC_EXPLICIT & mdoc_macros[n->tok].flags)
214 			mdoc_nmsg(mdoc, n, MANDOCERR_SCOPEEXIT);
215 
216 	/* Rewind to the first. */
217 
218 	return(rew_last(mdoc, mdoc->first));
219 }
220 
221 
222 /*
223  * Look up a macro from within a subsequent context.
224  */
225 static enum mdoct
226 lookup(enum mdoct from, const char *p)
227 {
228 
229 	if ( ! (MDOC_PARSED & mdoc_macros[from].flags))
230 		return(MDOC_MAX);
231 	return(lookup_raw(p));
232 }
233 
234 
235 /*
236  * Lookup a macro following the initial line macro.
237  */
238 static enum mdoct
239 lookup_raw(const char *p)
240 {
241 	enum mdoct	 res;
242 
243 	if (MDOC_MAX == (res = mdoc_hash_find(p)))
244 		return(MDOC_MAX);
245 	if (MDOC_CALLABLE & mdoc_macros[res].flags)
246 		return(res);
247 	return(MDOC_MAX);
248 }
249 
250 
251 static int
252 rew_last(struct mdoc *mdoc, const struct mdoc_node *to)
253 {
254 	struct mdoc_node *n, *np;
255 
256 	assert(to);
257 	mdoc->next = MDOC_NEXT_SIBLING;
258 
259 	/* LINTED */
260 	while (mdoc->last != to) {
261 		/*
262 		 * Save the parent here, because we may delete the
263 		 * mdoc->last node in the post-validation phase and reset
264 		 * it to mdoc->last->parent, causing a step in the closing
265 		 * out to be lost.
266 		 */
267 		np = mdoc->last->parent;
268 		if ( ! mdoc_valid_post(mdoc))
269 			return(0);
270 		n = mdoc->last;
271 		mdoc->last = np;
272 		assert(mdoc->last);
273 		mdoc->last->last = n;
274 	}
275 
276 	return(mdoc_valid_post(mdoc));
277 }
278 
279 
280 /*
281  * For a block closing macro, return the corresponding opening one.
282  * Otherwise, return the macro itself.
283  */
284 static enum mdoct
285 rew_alt(enum mdoct tok)
286 {
287 	switch (tok) {
288 	case (MDOC_Ac):
289 		return(MDOC_Ao);
290 	case (MDOC_Bc):
291 		return(MDOC_Bo);
292 	case (MDOC_Brc):
293 		return(MDOC_Bro);
294 	case (MDOC_Dc):
295 		return(MDOC_Do);
296 	case (MDOC_Ec):
297 		return(MDOC_Eo);
298 	case (MDOC_Ed):
299 		return(MDOC_Bd);
300 	case (MDOC_Ef):
301 		return(MDOC_Bf);
302 	case (MDOC_Ek):
303 		return(MDOC_Bk);
304 	case (MDOC_El):
305 		return(MDOC_Bl);
306 	case (MDOC_Fc):
307 		return(MDOC_Fo);
308 	case (MDOC_Oc):
309 		return(MDOC_Oo);
310 	case (MDOC_Pc):
311 		return(MDOC_Po);
312 	case (MDOC_Qc):
313 		return(MDOC_Qo);
314 	case (MDOC_Re):
315 		return(MDOC_Rs);
316 	case (MDOC_Sc):
317 		return(MDOC_So);
318 	case (MDOC_Xc):
319 		return(MDOC_Xo);
320 	default:
321 		return(tok);
322 	}
323 	/* NOTREACHED */
324 }
325 
326 
327 /*
328  * Rewinding to tok, how do we have to handle *p?
329  * REWIND_NONE: *p would delimit tok, but no tok scope is open
330  *   inside *p, so there is no need to rewind anything at all.
331  * REWIND_THIS: *p matches tok, so rewind *p and nothing else.
332  * REWIND_MORE: *p is implicit, rewind it and keep searching for tok.
333  * REWIND_FORCE: *p is explicit, but tok is full, force rewinding *p.
334  * REWIND_LATER: *p is explicit and still open, postpone rewinding.
335  * REWIND_ERROR: No tok block is open at all.
336  */
337 static enum rew
338 rew_dohalt(enum mdoct tok, enum mdoc_type type,
339 		const struct mdoc_node *p)
340 {
341 
342 	/*
343 	 * No matching token, no delimiting block, no broken block.
344 	 * This can happen when full implicit macros are called for
345 	 * the first time but try to rewind their previous
346 	 * instance anyway.
347 	 */
348 	if (MDOC_ROOT == p->type)
349 		return(MDOC_BLOCK == type &&
350 		    MDOC_EXPLICIT & mdoc_macros[tok].flags ?
351 		    REWIND_ERROR : REWIND_NONE);
352 
353 	/*
354 	 * When starting to rewind, skip plain text
355 	 * and nodes that have already been rewound.
356 	 */
357 	if (MDOC_TEXT == p->type || MDOC_VALID & p->flags)
358 		return(REWIND_MORE);
359 
360 	/*
361 	 * The easiest case:  Found a matching token.
362 	 * This applies to both blocks and elements.
363 	 */
364 	tok = rew_alt(tok);
365 	if (tok == p->tok)
366 		return(p->end ? REWIND_NONE :
367 		    type == p->type ? REWIND_THIS : REWIND_MORE);
368 
369 	/*
370 	 * While elements do require rewinding for themselves,
371 	 * they never affect rewinding of other nodes.
372 	 */
373 	if (MDOC_ELEM == p->type)
374 		return(REWIND_MORE);
375 
376 	/*
377 	 * Blocks delimited by our target token get REWIND_MORE.
378 	 * Blocks delimiting our target token get REWIND_NONE.
379 	 */
380 	switch (tok) {
381 	case (MDOC_Bl):
382 		if (MDOC_It == p->tok)
383 			return(REWIND_MORE);
384 		break;
385 	case (MDOC_It):
386 		if (MDOC_BODY == p->type && MDOC_Bl == p->tok)
387 			return(REWIND_NONE);
388 		break;
389 	/*
390 	 * XXX Badly nested block handling still fails badly
391 	 * when one block is breaking two blocks of the same type.
392 	 * This is an incomplete and extremely ugly workaround,
393 	 * required to let the OpenBSD tree build.
394 	 */
395 	case (MDOC_Oo):
396 		if (MDOC_Op == p->tok)
397 			return(REWIND_MORE);
398 		break;
399 	case (MDOC_Nm):
400 		return(REWIND_NONE);
401 	case (MDOC_Nd):
402 		/* FALLTHROUGH */
403 	case (MDOC_Ss):
404 		if (MDOC_BODY == p->type && MDOC_Sh == p->tok)
405 			return(REWIND_NONE);
406 		/* FALLTHROUGH */
407 	case (MDOC_Sh):
408 		if (MDOC_Nd == p->tok || MDOC_Ss == p->tok ||
409 		    MDOC_Sh == p->tok)
410 			return(REWIND_MORE);
411 		break;
412 	default:
413 		break;
414 	}
415 
416 	/*
417 	 * Default block rewinding rules.
418 	 * In particular, always skip block end markers,
419 	 * and let all blocks rewind Nm children.
420 	 */
421 	if (ENDBODY_NOT != p->end || MDOC_Nm == p->tok ||
422 	    (MDOC_BLOCK == p->type &&
423 	    ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)))
424 		return(REWIND_MORE);
425 
426 	/*
427 	 * By default, closing out full blocks
428 	 * forces closing of broken explicit blocks,
429 	 * while closing out partial blocks
430 	 * allows delayed rewinding by default.
431 	 */
432 	return (&blk_full == mdoc_macros[tok].fp ?
433 	    REWIND_FORCE : REWIND_LATER);
434 }
435 
436 
437 static int
438 rew_elem(struct mdoc *mdoc, enum mdoct tok)
439 {
440 	struct mdoc_node *n;
441 
442 	n = mdoc->last;
443 	if (MDOC_ELEM != n->type)
444 		n = n->parent;
445 	assert(MDOC_ELEM == n->type);
446 	assert(tok == n->tok);
447 
448 	return(rew_last(mdoc, n));
449 }
450 
451 
452 /*
453  * We are trying to close a block identified by tok,
454  * but the child block *broken is still open.
455  * Thus, postpone closing the tok block
456  * until the rew_sub call closing *broken.
457  */
458 static int
459 make_pending(struct mdoc_node *broken, enum mdoct tok,
460 		struct mdoc *mdoc, int line, int ppos)
461 {
462 	struct mdoc_node *breaker;
463 
464 	/*
465 	 * Iterate backwards, searching for the block matching tok,
466 	 * that is, the block breaking the *broken block.
467 	 */
468 	for (breaker = broken->parent; breaker; breaker = breaker->parent) {
469 
470 		/*
471 		 * If the *broken block had already been broken before
472 		 * and we encounter its breaker, make the tok block
473 		 * pending on the inner breaker.
474 		 * Graphically, "[A breaker=[B broken=[C->B B] tok=A] C]"
475 		 * becomes "[A broken=[B [C->B B] tok=A] C]"
476 		 * and finally "[A [B->A [C->B B] A] C]".
477 		 */
478 		if (breaker == broken->pending) {
479 			broken = breaker;
480 			continue;
481 		}
482 
483 		if (REWIND_THIS != rew_dohalt(tok, MDOC_BLOCK, breaker))
484 			continue;
485 		if (MDOC_BODY == broken->type)
486 			broken = broken->parent;
487 
488 		/*
489 		 * Found the breaker.
490 		 * If another, outer breaker is already pending on
491 		 * the *broken block, we must not clobber the link
492 		 * to the outer breaker, but make it pending on the
493 		 * new, now inner breaker.
494 		 * Graphically, "[A breaker=[B broken=[C->A A] tok=B] C]"
495 		 * becomes "[A breaker=[B->A broken=[C A] tok=B] C]"
496 		 * and finally "[A [B->A [C->B A] B] C]".
497 		 */
498 		if (broken->pending) {
499 			struct mdoc_node *taker;
500 
501 			/*
502 			 * If the breaker had also been broken before,
503 			 * it cannot take on the outer breaker itself,
504 			 * but must hand it on to its own breakers.
505 			 * Graphically, this is the following situation:
506 			 * "[A [B breaker=[C->B B] broken=[D->A A] tok=C] D]"
507 			 * "[A taker=[B->A breaker=[C->B B] [D->C A] C] D]"
508 			 */
509 			taker = breaker;
510 			while (taker->pending)
511 				taker = taker->pending;
512 			taker->pending = broken->pending;
513 		}
514 		broken->pending = breaker;
515 		mandoc_vmsg(MANDOCERR_SCOPENEST, mdoc->parse, line, ppos,
516 				"%s breaks %s", mdoc_macronames[tok],
517 				mdoc_macronames[broken->tok]);
518 		return(1);
519 	}
520 
521 	/*
522 	 * Found no matching block for tok.
523 	 * Are you trying to close a block that is not open?
524 	 */
525 	return(0);
526 }
527 
528 
529 static int
530 rew_sub(enum mdoc_type t, struct mdoc *mdoc,
531 		enum mdoct tok, int line, int ppos)
532 {
533 	struct mdoc_node *n;
534 
535 	n = mdoc->last;
536 	while (n) {
537 		switch (rew_dohalt(tok, t, n)) {
538 		case (REWIND_NONE):
539 			return(1);
540 		case (REWIND_THIS):
541 			break;
542 		case (REWIND_FORCE):
543 			mandoc_vmsg(MANDOCERR_SCOPEBROKEN, mdoc->parse,
544 					line, ppos, "%s breaks %s",
545 					mdoc_macronames[tok],
546 					mdoc_macronames[n->tok]);
547 			/* FALLTHROUGH */
548 		case (REWIND_MORE):
549 			n = n->parent;
550 			continue;
551 		case (REWIND_LATER):
552 			if (make_pending(n, tok, mdoc, line, ppos) ||
553 			    MDOC_BLOCK != t)
554 				return(1);
555 			/* FALLTHROUGH */
556 		case (REWIND_ERROR):
557 			mdoc_pmsg(mdoc, line, ppos, MANDOCERR_NOSCOPE);
558 			return(1);
559 		}
560 		break;
561 	}
562 
563 	assert(n);
564 	if ( ! rew_last(mdoc, n))
565 		return(0);
566 
567 	/*
568 	 * The current block extends an enclosing block.
569 	 * Now that the current block ends, close the enclosing block, too.
570 	 */
571 	while (NULL != (n = n->pending)) {
572 		if ( ! rew_last(mdoc, n))
573 			return(0);
574 		if (MDOC_HEAD == n->type &&
575 		    ! mdoc_body_alloc(mdoc, n->line, n->pos, n->tok))
576 			return(0);
577 	}
578 
579 	return(1);
580 }
581 
582 /*
583  * Allocate a word and check whether it's punctuation or not.
584  * Punctuation consists of those tokens found in mdoc_isdelim().
585  */
586 static int
587 dword(struct mdoc *mdoc, int line,
588 		int col, const char *p, enum mdelim d)
589 {
590 
591 	if (DELIM_MAX == d)
592 		d = mdoc_isdelim(p);
593 
594 	if ( ! mdoc_word_alloc(mdoc, line, col, p))
595 		return(0);
596 
597 	if (DELIM_OPEN == d)
598 		mdoc->last->flags |= MDOC_DELIMO;
599 
600 	/*
601 	 * Closing delimiters only suppress the preceding space
602 	 * when they follow something, not when they start a new
603 	 * block or element, and not when they follow `No'.
604 	 *
605 	 * XXX	Explicitly special-casing MDOC_No here feels
606 	 *	like a layering violation.  Find a better way
607 	 *	and solve this in the code related to `No'!
608 	 */
609 
610 	else if (DELIM_CLOSE == d && mdoc->last->prev &&
611 			mdoc->last->prev->tok != MDOC_No &&
612 			mdoc->last->parent->tok != MDOC_Fd)
613 		mdoc->last->flags |= MDOC_DELIMC;
614 
615 	return(1);
616 }
617 
618 static int
619 append_delims(struct mdoc *mdoc, int line, int *pos, char *buf)
620 {
621 	int		 la;
622 	enum margserr	 ac;
623 	char		*p;
624 
625 	if ('\0' == buf[*pos])
626 		return(1);
627 
628 	for (;;) {
629 		la = *pos;
630 		ac = mdoc_zargs(mdoc, line, pos, buf, &p);
631 
632 		if (ARGS_ERROR == ac)
633 			return(0);
634 		else if (ARGS_EOLN == ac)
635 			break;
636 
637 		dword(mdoc, line, la, p, DELIM_MAX);
638 
639 		/*
640 		 * If we encounter end-of-sentence symbols, then trigger
641 		 * the double-space.
642 		 *
643 		 * XXX: it's easy to allow this to propagate outward to
644 		 * the last symbol, such that `. )' will cause the
645 		 * correct double-spacing.  However, (1) groff isn't
646 		 * smart enough to do this and (2) it would require
647 		 * knowing which symbols break this behaviour, for
648 		 * example, `.  ;' shouldn't propagate the double-space.
649 		 */
650 		if (mandoc_eos(p, strlen(p), 0))
651 			mdoc->last->flags |= MDOC_EOS;
652 	}
653 
654 	return(1);
655 }
656 
657 
658 /*
659  * Close out block partial/full explicit.
660  */
661 static int
662 blk_exp_close(MACRO_PROT_ARGS)
663 {
664 	struct mdoc_node *body;		/* Our own body. */
665 	struct mdoc_node *later;	/* A sub-block starting later. */
666 	struct mdoc_node *n;		/* For searching backwards. */
667 
668 	int	 	 j, lastarg, maxargs, flushed, nl;
669 	enum margserr	 ac;
670 	enum mdoct	 atok, ntok;
671 	char		*p;
672 
673 	nl = MDOC_NEWLINE & mdoc->flags;
674 
675 	switch (tok) {
676 	case (MDOC_Ec):
677 		maxargs = 1;
678 		break;
679 	default:
680 		maxargs = 0;
681 		break;
682 	}
683 
684 	/*
685 	 * Search backwards for beginnings of blocks,
686 	 * both of our own and of pending sub-blocks.
687 	 */
688 	atok = rew_alt(tok);
689 	body = later = NULL;
690 	for (n = mdoc->last; n; n = n->parent) {
691 		if (MDOC_VALID & n->flags)
692 			continue;
693 
694 		/* Remember the start of our own body. */
695 		if (MDOC_BODY == n->type && atok == n->tok) {
696 			if (ENDBODY_NOT == n->end)
697 				body = n;
698 			continue;
699 		}
700 
701 		if (MDOC_BLOCK != n->type || MDOC_Nm == n->tok)
702 			continue;
703 		if (atok == n->tok) {
704 			assert(body);
705 
706 			/*
707 			 * Found the start of our own block.
708 			 * When there is no pending sub block,
709 			 * just proceed to closing out.
710 			 */
711 			if (NULL == later)
712 				break;
713 
714 			/*
715 			 * When there is a pending sub block,
716 			 * postpone closing out the current block
717 			 * until the rew_sub() closing out the sub-block.
718 			 */
719 			make_pending(later, tok, mdoc, line, ppos);
720 
721 			/*
722 			 * Mark the place where the formatting - but not
723 			 * the scope - of the current block ends.
724 			 */
725 			if ( ! mdoc_endbody_alloc(mdoc, line, ppos,
726 			    atok, body, ENDBODY_SPACE))
727 				return(0);
728 			break;
729 		}
730 
731 		/*
732 		 * When finding an open sub block, remember the last
733 		 * open explicit block, or, in case there are only
734 		 * implicit ones, the first open implicit block.
735 		 */
736 		if (later &&
737 		    MDOC_EXPLICIT & mdoc_macros[later->tok].flags)
738 			continue;
739 		if (MDOC_It != n->tok)
740 			later = n;
741 	}
742 
743 	if ( ! (MDOC_CALLABLE & mdoc_macros[tok].flags)) {
744 		/* FIXME: do this in validate */
745 		if (buf[*pos])
746 			mdoc_pmsg(mdoc, line, ppos, MANDOCERR_ARGSLOST);
747 
748 		if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
749 			return(0);
750 		return(rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos));
751 	}
752 
753 	if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
754 		return(0);
755 
756 	if (NULL == later && maxargs > 0)
757 		if ( ! mdoc_tail_alloc(mdoc, line, ppos, rew_alt(tok)))
758 			return(0);
759 
760 	for (flushed = j = 0; ; j++) {
761 		lastarg = *pos;
762 
763 		if (j == maxargs && ! flushed) {
764 			if ( ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
765 				return(0);
766 			flushed = 1;
767 		}
768 
769 		ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
770 
771 		if (ARGS_ERROR == ac)
772 			return(0);
773 		if (ARGS_PUNCT == ac)
774 			break;
775 		if (ARGS_EOLN == ac)
776 			break;
777 
778 		ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
779 
780 		if (MDOC_MAX == ntok) {
781 			if ( ! dword(mdoc, line, lastarg, p, DELIM_MAX))
782 				return(0);
783 			continue;
784 		}
785 
786 		if ( ! flushed) {
787 			if ( ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
788 				return(0);
789 			flushed = 1;
790 		}
791 		if ( ! mdoc_macro(mdoc, ntok, line, lastarg, pos, buf))
792 			return(0);
793 		break;
794 	}
795 
796 	if ( ! flushed && ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
797 		return(0);
798 
799 	if ( ! nl)
800 		return(1);
801 	return(append_delims(mdoc, line, pos, buf));
802 }
803 
804 
805 static int
806 in_line(MACRO_PROT_ARGS)
807 {
808 	int		 la, scope, cnt, nc, nl;
809 	enum margverr	 av;
810 	enum mdoct	 ntok;
811 	enum margserr	 ac;
812 	enum mdelim	 d;
813 	struct mdoc_arg	*arg;
814 	char		*p;
815 
816 	nl = MDOC_NEWLINE & mdoc->flags;
817 
818 	/*
819 	 * Whether we allow ignored elements (those without content,
820 	 * usually because of reserved words) to squeak by.
821 	 */
822 
823 	switch (tok) {
824 	case (MDOC_An):
825 		/* FALLTHROUGH */
826 	case (MDOC_Ar):
827 		/* FALLTHROUGH */
828 	case (MDOC_Fl):
829 		/* FALLTHROUGH */
830 	case (MDOC_Mt):
831 		/* FALLTHROUGH */
832 	case (MDOC_Nm):
833 		/* FALLTHROUGH */
834 	case (MDOC_Pa):
835 		nc = 1;
836 		break;
837 	default:
838 		nc = 0;
839 		break;
840 	}
841 
842 	for (arg = NULL;; ) {
843 		la = *pos;
844 		av = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
845 
846 		if (ARGV_WORD == av) {
847 			*pos = la;
848 			break;
849 		}
850 		if (ARGV_EOLN == av)
851 			break;
852 		if (ARGV_ARG == av)
853 			continue;
854 
855 		mdoc_argv_free(arg);
856 		return(0);
857 	}
858 
859 	for (cnt = scope = 0;; ) {
860 		la = *pos;
861 		ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
862 
863 		if (ARGS_ERROR == ac)
864 			return(0);
865 		if (ARGS_EOLN == ac)
866 			break;
867 		if (ARGS_PUNCT == ac)
868 			break;
869 
870 		ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
871 
872 		/*
873 		 * In this case, we've located a submacro and must
874 		 * execute it.  Close out scope, if open.  If no
875 		 * elements have been generated, either create one (nc)
876 		 * or raise a warning.
877 		 */
878 
879 		if (MDOC_MAX != ntok) {
880 			if (scope && ! rew_elem(mdoc, tok))
881 				return(0);
882 			if (nc && 0 == cnt) {
883 				if ( ! mdoc_elem_alloc(mdoc, line,
884 						ppos, tok, arg))
885 					return(0);
886 				if ( ! rew_last(mdoc, mdoc->last))
887 					return(0);
888 			} else if ( ! nc && 0 == cnt) {
889 				mdoc_argv_free(arg);
890 				mdoc_pmsg(mdoc, line, ppos,
891 					MANDOCERR_MACROEMPTY);
892 			}
893 
894 			if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
895 				return(0);
896 			if ( ! nl)
897 				return(1);
898 			return(append_delims(mdoc, line, pos, buf));
899 		}
900 
901 		/*
902 		 * Non-quote-enclosed punctuation.  Set up our scope, if
903 		 * a word; rewind the scope, if a delimiter; then append
904 		 * the word.
905 		 */
906 
907 		d = ARGS_QWORD == ac ? DELIM_NONE : mdoc_isdelim(p);
908 
909 		if (DELIM_NONE != d) {
910 			/*
911 			 * If we encounter closing punctuation, no word
912 			 * has been omitted, no scope is open, and we're
913 			 * allowed to have an empty element, then start
914 			 * a new scope.  `Ar', `Fl', and `Li', only do
915 			 * this once per invocation.  There may be more
916 			 * of these (all of them?).
917 			 */
918 			if (0 == cnt && (nc || MDOC_Li == tok) &&
919 					DELIM_CLOSE == d && ! scope) {
920 				if ( ! mdoc_elem_alloc(mdoc, line,
921 						ppos, tok, arg))
922 					return(0);
923 				if (MDOC_Ar == tok || MDOC_Li == tok ||
924 						MDOC_Fl == tok)
925 					cnt++;
926 				scope = 1;
927 			}
928 			/*
929 			 * Close out our scope, if one is open, before
930 			 * any punctuation.
931 			 */
932 			if (scope && ! rew_elem(mdoc, tok))
933 				return(0);
934 			scope = 0;
935 		} else if ( ! scope) {
936 			if ( ! mdoc_elem_alloc(mdoc, line, ppos, tok, arg))
937 				return(0);
938 			scope = 1;
939 		}
940 
941 		if (DELIM_NONE == d)
942 			cnt++;
943 
944 		if ( ! dword(mdoc, line, la, p, d))
945 			return(0);
946 
947 		/*
948 		 * `Fl' macros have their scope re-opened with each new
949 		 * word so that the `-' can be added to each one without
950 		 * having to parse out spaces.
951 		 */
952 		if (scope && MDOC_Fl == tok) {
953 			if ( ! rew_elem(mdoc, tok))
954 				return(0);
955 			scope = 0;
956 		}
957 	}
958 
959 	if (scope && ! rew_elem(mdoc, tok))
960 		return(0);
961 
962 	/*
963 	 * If no elements have been collected and we're allowed to have
964 	 * empties (nc), open a scope and close it out.  Otherwise,
965 	 * raise a warning.
966 	 */
967 
968 	if (nc && 0 == cnt) {
969 		if ( ! mdoc_elem_alloc(mdoc, line, ppos, tok, arg))
970 			return(0);
971 		if ( ! rew_last(mdoc, mdoc->last))
972 			return(0);
973 	} else if ( ! nc && 0 == cnt) {
974 		mdoc_argv_free(arg);
975 		mdoc_pmsg(mdoc, line, ppos, MANDOCERR_MACROEMPTY);
976 	}
977 
978 	if ( ! nl)
979 		return(1);
980 	return(append_delims(mdoc, line, pos, buf));
981 }
982 
983 
984 static int
985 blk_full(MACRO_PROT_ARGS)
986 {
987 	int		  la, nl, nparsed;
988 	struct mdoc_arg	 *arg;
989 	struct mdoc_node *head; /* save of head macro */
990 	struct mdoc_node *body; /* save of body macro */
991 	struct mdoc_node *n;
992 	enum mdoc_type	  mtt;
993 	enum mdoct	  ntok;
994 	enum margserr	  ac, lac;
995 	enum margverr	  av;
996 	char		 *p;
997 
998 	nl = MDOC_NEWLINE & mdoc->flags;
999 
1000 	/* Close out prior implicit scope. */
1001 
1002 	if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)) {
1003 		if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
1004 			return(0);
1005 		if ( ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
1006 			return(0);
1007 	}
1008 
1009 	/*
1010 	 * This routine accommodates implicitly- and explicitly-scoped
1011 	 * macro openings.  Implicit ones first close out prior scope
1012 	 * (seen above).  Delay opening the head until necessary to
1013 	 * allow leading punctuation to print.  Special consideration
1014 	 * for `It -column', which has phrase-part syntax instead of
1015 	 * regular child nodes.
1016 	 */
1017 
1018 	for (arg = NULL;; ) {
1019 		la = *pos;
1020 		av = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1021 
1022 		if (ARGV_WORD == av) {
1023 			*pos = la;
1024 			break;
1025 		}
1026 
1027 		if (ARGV_EOLN == av)
1028 			break;
1029 		if (ARGV_ARG == av)
1030 			continue;
1031 
1032 		mdoc_argv_free(arg);
1033 		return(0);
1034 	}
1035 
1036 	if ( ! mdoc_block_alloc(mdoc, line, ppos, tok, arg))
1037 		return(0);
1038 
1039 	head = body = NULL;
1040 
1041 	/*
1042 	 * Exception: Heads of `It' macros in `-diag' lists are not
1043 	 * parsed, even though `It' macros in general are parsed.
1044 	 */
1045 	nparsed = MDOC_It == tok &&
1046 		MDOC_Bl == mdoc->last->parent->tok &&
1047 		LIST_diag == mdoc->last->parent->norm->Bl.type;
1048 
1049 	/*
1050 	 * The `Nd' macro has all arguments in its body: it's a hybrid
1051 	 * of block partial-explicit and full-implicit.  Stupid.
1052 	 */
1053 
1054 	if (MDOC_Nd == tok) {
1055 		if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1056 			return(0);
1057 		head = mdoc->last;
1058 		if ( ! rew_sub(MDOC_HEAD, mdoc, tok, line, ppos))
1059 			return(0);
1060 		if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1061 			return(0);
1062 		body = mdoc->last;
1063 	}
1064 
1065 	ac = ARGS_ERROR;
1066 
1067 	for ( ; ; ) {
1068 		la = *pos;
1069 		/* Initialise last-phrase-type with ARGS_PEND. */
1070 		lac = ARGS_ERROR == ac ? ARGS_PEND : ac;
1071 		ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1072 
1073 		if (ARGS_PUNCT == ac)
1074 			break;
1075 
1076 		if (ARGS_ERROR == ac)
1077 			return(0);
1078 
1079 		if (ARGS_EOLN == ac) {
1080 			if (ARGS_PPHRASE != lac && ARGS_PHRASE != lac)
1081 				break;
1082 			/*
1083 			 * This is necessary: if the last token on a
1084 			 * line is a `Ta' or tab, then we'll get
1085 			 * ARGS_EOLN, so we must be smart enough to
1086 			 * reopen our scope if the last parse was a
1087 			 * phrase or partial phrase.
1088 			 */
1089 			if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
1090 				return(0);
1091 			if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1092 				return(0);
1093 			body = mdoc->last;
1094 			break;
1095 		}
1096 
1097 		/*
1098 		 * Emit leading punctuation (i.e., punctuation before
1099 		 * the MDOC_HEAD) for non-phrase types.
1100 		 */
1101 
1102 		if (NULL == head &&
1103 				ARGS_PEND != ac &&
1104 				ARGS_PHRASE != ac &&
1105 				ARGS_PPHRASE != ac &&
1106 				ARGS_QWORD != ac &&
1107 				DELIM_OPEN == mdoc_isdelim(p)) {
1108 			if ( ! dword(mdoc, line, la, p, DELIM_OPEN))
1109 				return(0);
1110 			continue;
1111 		}
1112 
1113 		/* Open a head if one hasn't been opened. */
1114 
1115 		if (NULL == head) {
1116 			if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1117 				return(0);
1118 			head = mdoc->last;
1119 		}
1120 
1121 		if (ARGS_PHRASE == ac ||
1122 				ARGS_PEND == ac ||
1123 				ARGS_PPHRASE == ac) {
1124 			/*
1125 			 * If we haven't opened a body yet, rewind the
1126 			 * head; if we have, rewind that instead.
1127 			 */
1128 
1129 			mtt = body ? MDOC_BODY : MDOC_HEAD;
1130 			if ( ! rew_sub(mtt, mdoc, tok, line, ppos))
1131 				return(0);
1132 
1133 			/* Then allocate our body context. */
1134 
1135 			if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1136 				return(0);
1137 			body = mdoc->last;
1138 
1139 			/*
1140 			 * Process phrases: set whether we're in a
1141 			 * partial-phrase (this effects line handling)
1142 			 * then call down into the phrase parser.
1143 			 */
1144 
1145 			if (ARGS_PPHRASE == ac)
1146 				mdoc->flags |= MDOC_PPHRASE;
1147 			if (ARGS_PEND == ac && ARGS_PPHRASE == lac)
1148 				mdoc->flags |= MDOC_PPHRASE;
1149 
1150 			if ( ! phrase(mdoc, line, la, buf))
1151 				return(0);
1152 
1153 			mdoc->flags &= ~MDOC_PPHRASE;
1154 			continue;
1155 		}
1156 
1157 		ntok = nparsed || ARGS_QWORD == ac ?
1158 			MDOC_MAX : lookup(tok, p);
1159 
1160 		if (MDOC_MAX == ntok) {
1161 			if ( ! dword(mdoc, line, la, p, DELIM_MAX))
1162 				return(0);
1163 			continue;
1164 		}
1165 
1166 		if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
1167 			return(0);
1168 		break;
1169 	}
1170 
1171 	if (NULL == head) {
1172 		if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1173 			return(0);
1174 		head = mdoc->last;
1175 	}
1176 
1177 	if (nl && ! append_delims(mdoc, line, pos, buf))
1178 		return(0);
1179 
1180 	/* If we've already opened our body, exit now. */
1181 
1182 	if (NULL != body)
1183 		goto out;
1184 
1185 	/*
1186 	 * If there is an open (i.e., unvalidated) sub-block requiring
1187 	 * explicit close-out, postpone switching the current block from
1188 	 * head to body until the rew_sub() call closing out that
1189 	 * sub-block.
1190 	 */
1191 	for (n = mdoc->last; n && n != head; n = n->parent) {
1192 		if (MDOC_BLOCK == n->type &&
1193 				MDOC_EXPLICIT & mdoc_macros[n->tok].flags &&
1194 				! (MDOC_VALID & n->flags)) {
1195 			n->pending = head;
1196 			return(1);
1197 		}
1198 	}
1199 
1200 	/* Close out scopes to remain in a consistent state. */
1201 
1202 	if ( ! rew_sub(MDOC_HEAD, mdoc, tok, line, ppos))
1203 		return(0);
1204 	if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1205 		return(0);
1206 
1207 out:
1208 	if ( ! (MDOC_FREECOL & mdoc->flags))
1209 		return(1);
1210 
1211 	if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
1212 		return(0);
1213 	if ( ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
1214 		return(0);
1215 
1216 	mdoc->flags &= ~MDOC_FREECOL;
1217 	return(1);
1218 }
1219 
1220 
1221 static int
1222 blk_part_imp(MACRO_PROT_ARGS)
1223 {
1224 	int		  la, nl;
1225 	enum mdoct	  ntok;
1226 	enum margserr	  ac;
1227 	char		 *p;
1228 	struct mdoc_node *blk; /* saved block context */
1229 	struct mdoc_node *body; /* saved body context */
1230 	struct mdoc_node *n;
1231 
1232 	nl = MDOC_NEWLINE & mdoc->flags;
1233 
1234 	/*
1235 	 * A macro that spans to the end of the line.  This is generally
1236 	 * (but not necessarily) called as the first macro.  The block
1237 	 * has a head as the immediate child, which is always empty,
1238 	 * followed by zero or more opening punctuation nodes, then the
1239 	 * body (which may be empty, depending on the macro), then zero
1240 	 * or more closing punctuation nodes.
1241 	 */
1242 
1243 	if ( ! mdoc_block_alloc(mdoc, line, ppos, tok, NULL))
1244 		return(0);
1245 
1246 	blk = mdoc->last;
1247 
1248 	if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1249 		return(0);
1250 	if ( ! rew_sub(MDOC_HEAD, mdoc, tok, line, ppos))
1251 		return(0);
1252 
1253 	/*
1254 	 * Open the body scope "on-demand", that is, after we've
1255 	 * processed all our the leading delimiters (open parenthesis,
1256 	 * etc.).
1257 	 */
1258 
1259 	for (body = NULL; ; ) {
1260 		la = *pos;
1261 		ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1262 
1263 		if (ARGS_ERROR == ac)
1264 			return(0);
1265 		if (ARGS_EOLN == ac)
1266 			break;
1267 		if (ARGS_PUNCT == ac)
1268 			break;
1269 
1270 		if (NULL == body && ARGS_QWORD != ac &&
1271 				DELIM_OPEN == mdoc_isdelim(p)) {
1272 			if ( ! dword(mdoc, line, la, p, DELIM_OPEN))
1273 				return(0);
1274 			continue;
1275 		}
1276 
1277 		if (NULL == body) {
1278 		       if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1279 			       return(0);
1280 			body = mdoc->last;
1281 		}
1282 
1283 		ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1284 
1285 		if (MDOC_MAX == ntok) {
1286 			if ( ! dword(mdoc, line, la, p, DELIM_MAX))
1287 				return(0);
1288 			continue;
1289 		}
1290 
1291 		if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
1292 			return(0);
1293 		break;
1294 	}
1295 
1296 	/* Clean-ups to leave in a consistent state. */
1297 
1298 	if (NULL == body) {
1299 		if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1300 			return(0);
1301 		body = mdoc->last;
1302 	}
1303 
1304 	for (n = body->child; n && n->next; n = n->next)
1305 		/* Do nothing. */ ;
1306 
1307 	/*
1308 	 * End of sentence spacing: if the last node is a text node and
1309 	 * has a trailing period, then mark it as being end-of-sentence.
1310 	 */
1311 
1312 	if (n && MDOC_TEXT == n->type && n->string)
1313 		if (mandoc_eos(n->string, strlen(n->string), 1))
1314 			n->flags |= MDOC_EOS;
1315 
1316 	/* Up-propagate the end-of-space flag. */
1317 
1318 	if (n && (MDOC_EOS & n->flags)) {
1319 		body->flags |= MDOC_EOS;
1320 		body->parent->flags |= MDOC_EOS;
1321 	}
1322 
1323 	/*
1324 	 * If there is an open sub-block requiring explicit close-out,
1325 	 * postpone closing out the current block
1326 	 * until the rew_sub() call closing out the sub-block.
1327 	 */
1328 	for (n = mdoc->last; n && n != body && n != blk->parent;
1329 			n = n->parent) {
1330 		if (MDOC_BLOCK == n->type &&
1331 		    MDOC_EXPLICIT & mdoc_macros[n->tok].flags &&
1332 		    ! (MDOC_VALID & n->flags)) {
1333 			make_pending(n, tok, mdoc, line, ppos);
1334 			if ( ! mdoc_endbody_alloc(mdoc, line, ppos,
1335 			    tok, body, ENDBODY_NOSPACE))
1336 				return(0);
1337 			return(1);
1338 		}
1339 	}
1340 
1341 	/*
1342 	 * If we can't rewind to our body, then our scope has already
1343 	 * been closed by another macro (like `Oc' closing `Op').  This
1344 	 * is ugly behaviour nodding its head to OpenBSD's overwhelming
1345 	 * crufty use of `Op' breakage.
1346 	 */
1347 	if (n != body)
1348 		mandoc_vmsg(MANDOCERR_SCOPENEST, mdoc->parse, line, ppos,
1349 				"%s broken", mdoc_macronames[tok]);
1350 
1351 	if (n && ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
1352 		return(0);
1353 
1354 	/* Standard appending of delimiters. */
1355 
1356 	if (nl && ! append_delims(mdoc, line, pos, buf))
1357 		return(0);
1358 
1359 	/* Rewind scope, if applicable. */
1360 
1361 	if (n && ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
1362 		return(0);
1363 
1364 	/* Move trailing .Ns out of scope. */
1365 
1366 	for (n = body->child; n && n->next; n = n->next)
1367 		/* Do nothing. */ ;
1368 	if (n && MDOC_Ns == n->tok)
1369 		mdoc_node_relink(mdoc, n);
1370 
1371 	return(1);
1372 }
1373 
1374 
1375 static int
1376 blk_part_exp(MACRO_PROT_ARGS)
1377 {
1378 	int		  la, nl;
1379 	enum margserr	  ac;
1380 	struct mdoc_node *head; /* keep track of head */
1381 	struct mdoc_node *body; /* keep track of body */
1382 	char		 *p;
1383 	enum mdoct	  ntok;
1384 
1385 	nl = MDOC_NEWLINE & mdoc->flags;
1386 
1387 	/*
1388 	 * The opening of an explicit macro having zero or more leading
1389 	 * punctuation nodes; a head with optional single element (the
1390 	 * case of `Eo'); and a body that may be empty.
1391 	 */
1392 
1393 	if ( ! mdoc_block_alloc(mdoc, line, ppos, tok, NULL))
1394 		return(0);
1395 
1396 	for (head = body = NULL; ; ) {
1397 		la = *pos;
1398 		ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1399 
1400 		if (ARGS_ERROR == ac)
1401 			return(0);
1402 		if (ARGS_PUNCT == ac)
1403 			break;
1404 		if (ARGS_EOLN == ac)
1405 			break;
1406 
1407 		/* Flush out leading punctuation. */
1408 
1409 		if (NULL == head && ARGS_QWORD != ac &&
1410 				DELIM_OPEN == mdoc_isdelim(p)) {
1411 			assert(NULL == body);
1412 			if ( ! dword(mdoc, line, la, p, DELIM_OPEN))
1413 				return(0);
1414 			continue;
1415 		}
1416 
1417 		if (NULL == head) {
1418 			assert(NULL == body);
1419 			if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1420 				return(0);
1421 			head = mdoc->last;
1422 		}
1423 
1424 		/*
1425 		 * `Eo' gobbles any data into the head, but most other
1426 		 * macros just immediately close out and begin the body.
1427 		 */
1428 
1429 		if (NULL == body) {
1430 			assert(head);
1431 			/* No check whether it's a macro! */
1432 			if (MDOC_Eo == tok)
1433 				if ( ! dword(mdoc, line, la, p, DELIM_MAX))
1434 					return(0);
1435 
1436 			if ( ! rew_sub(MDOC_HEAD, mdoc, tok, line, ppos))
1437 				return(0);
1438 			if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1439 				return(0);
1440 			body = mdoc->last;
1441 
1442 			if (MDOC_Eo == tok)
1443 				continue;
1444 		}
1445 
1446 		assert(NULL != head && NULL != body);
1447 
1448 		ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1449 
1450 		if (MDOC_MAX == ntok) {
1451 			if ( ! dword(mdoc, line, la, p, DELIM_MAX))
1452 				return(0);
1453 			continue;
1454 		}
1455 
1456 		if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
1457 			return(0);
1458 		break;
1459 	}
1460 
1461 	/* Clean-up to leave in a consistent state. */
1462 
1463 	if (NULL == head)
1464 		if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1465 			return(0);
1466 
1467 	if (NULL == body) {
1468 		if ( ! rew_sub(MDOC_HEAD, mdoc, tok, line, ppos))
1469 			return(0);
1470 		if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1471 			return(0);
1472 	}
1473 
1474 	/* Standard appending of delimiters. */
1475 
1476 	if ( ! nl)
1477 		return(1);
1478 	return(append_delims(mdoc, line, pos, buf));
1479 }
1480 
1481 
1482 /* ARGSUSED */
1483 static int
1484 in_line_argn(MACRO_PROT_ARGS)
1485 {
1486 	int		 la, flushed, j, maxargs, nl;
1487 	enum margserr	 ac;
1488 	enum margverr	 av;
1489 	struct mdoc_arg	*arg;
1490 	char		*p;
1491 	enum mdoct	 ntok;
1492 
1493 	nl = MDOC_NEWLINE & mdoc->flags;
1494 
1495 	/*
1496 	 * A line macro that has a fixed number of arguments (maxargs).
1497 	 * Only open the scope once the first non-leading-punctuation is
1498 	 * found (unless MDOC_IGNDELIM is noted, like in `Pf'), then
1499 	 * keep it open until the maximum number of arguments are
1500 	 * exhausted.
1501 	 */
1502 
1503 	switch (tok) {
1504 	case (MDOC_Ap):
1505 		/* FALLTHROUGH */
1506 	case (MDOC_No):
1507 		/* FALLTHROUGH */
1508 	case (MDOC_Ns):
1509 		/* FALLTHROUGH */
1510 	case (MDOC_Ux):
1511 		maxargs = 0;
1512 		break;
1513 	case (MDOC_Bx):
1514 		/* FALLTHROUGH */
1515 	case (MDOC_Xr):
1516 		maxargs = 2;
1517 		break;
1518 	default:
1519 		maxargs = 1;
1520 		break;
1521 	}
1522 
1523 	for (arg = NULL; ; ) {
1524 		la = *pos;
1525 		av = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1526 
1527 		if (ARGV_WORD == av) {
1528 			*pos = la;
1529 			break;
1530 		}
1531 
1532 		if (ARGV_EOLN == av)
1533 			break;
1534 		if (ARGV_ARG == av)
1535 			continue;
1536 
1537 		mdoc_argv_free(arg);
1538 		return(0);
1539 	}
1540 
1541 	for (flushed = j = 0; ; ) {
1542 		la = *pos;
1543 		ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1544 
1545 		if (ARGS_ERROR == ac)
1546 			return(0);
1547 		if (ARGS_PUNCT == ac)
1548 			break;
1549 		if (ARGS_EOLN == ac)
1550 			break;
1551 
1552 		if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1553 				ARGS_QWORD != ac && 0 == j &&
1554 				DELIM_OPEN == mdoc_isdelim(p)) {
1555 			if ( ! dword(mdoc, line, la, p, DELIM_OPEN))
1556 				return(0);
1557 			continue;
1558 		} else if (0 == j)
1559 		       if ( ! mdoc_elem_alloc(mdoc, line, la, tok, arg))
1560 			       return(0);
1561 
1562 		if (j == maxargs && ! flushed) {
1563 			if ( ! rew_elem(mdoc, tok))
1564 				return(0);
1565 			flushed = 1;
1566 		}
1567 
1568 		ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1569 
1570 		if (MDOC_MAX != ntok) {
1571 			if ( ! flushed && ! rew_elem(mdoc, tok))
1572 				return(0);
1573 			flushed = 1;
1574 			if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
1575 				return(0);
1576 			j++;
1577 			break;
1578 		}
1579 
1580 		if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1581 				ARGS_QWORD != ac &&
1582 				! flushed &&
1583 				DELIM_NONE != mdoc_isdelim(p)) {
1584 			if ( ! rew_elem(mdoc, tok))
1585 				return(0);
1586 			flushed = 1;
1587 		}
1588 
1589 		if ( ! dword(mdoc, line, la, p, DELIM_MAX))
1590 			return(0);
1591 		j++;
1592 	}
1593 
1594 	if (0 == j && ! mdoc_elem_alloc(mdoc, line, la, tok, arg))
1595 	       return(0);
1596 
1597 	/* Close out in a consistent state. */
1598 
1599 	if ( ! flushed && ! rew_elem(mdoc, tok))
1600 		return(0);
1601 	if ( ! nl)
1602 		return(1);
1603 	return(append_delims(mdoc, line, pos, buf));
1604 }
1605 
1606 
1607 static int
1608 in_line_eoln(MACRO_PROT_ARGS)
1609 {
1610 	int		 la;
1611 	enum margserr	 ac;
1612 	enum margverr	 av;
1613 	struct mdoc_arg	*arg;
1614 	char		*p;
1615 	enum mdoct	 ntok;
1616 
1617 	assert( ! (MDOC_PARSED & mdoc_macros[tok].flags));
1618 
1619 	if (tok == MDOC_Pp)
1620 		rew_sub(MDOC_BLOCK, mdoc, MDOC_Nm, line, ppos);
1621 
1622 	/* Parse macro arguments. */
1623 
1624 	for (arg = NULL; ; ) {
1625 		la = *pos;
1626 		av = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1627 
1628 		if (ARGV_WORD == av) {
1629 			*pos = la;
1630 			break;
1631 		}
1632 		if (ARGV_EOLN == av)
1633 			break;
1634 		if (ARGV_ARG == av)
1635 			continue;
1636 
1637 		mdoc_argv_free(arg);
1638 		return(0);
1639 	}
1640 
1641 	/* Open element scope. */
1642 
1643 	if ( ! mdoc_elem_alloc(mdoc, line, ppos, tok, arg))
1644 		return(0);
1645 
1646 	/* Parse argument terms. */
1647 
1648 	for (;;) {
1649 		la = *pos;
1650 		ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1651 
1652 		if (ARGS_ERROR == ac)
1653 			return(0);
1654 		if (ARGS_EOLN == ac)
1655 			break;
1656 
1657 		ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1658 
1659 		if (MDOC_MAX == ntok) {
1660 			if ( ! dword(mdoc, line, la, p, DELIM_MAX))
1661 				return(0);
1662 			continue;
1663 		}
1664 
1665 		if ( ! rew_elem(mdoc, tok))
1666 			return(0);
1667 		return(mdoc_macro(mdoc, ntok, line, la, pos, buf));
1668 	}
1669 
1670 	/* Close out (no delimiters). */
1671 
1672 	return(rew_elem(mdoc, tok));
1673 }
1674 
1675 
1676 /* ARGSUSED */
1677 static int
1678 ctx_synopsis(MACRO_PROT_ARGS)
1679 {
1680 	int		 nl;
1681 
1682 	nl = MDOC_NEWLINE & mdoc->flags;
1683 
1684 	/* If we're not in the SYNOPSIS, go straight to in-line. */
1685 	if ( ! (MDOC_SYNOPSIS & mdoc->flags))
1686 		return(in_line(mdoc, tok, line, ppos, pos, buf));
1687 
1688 	/* If we're a nested call, same place. */
1689 	if ( ! nl)
1690 		return(in_line(mdoc, tok, line, ppos, pos, buf));
1691 
1692 	/*
1693 	 * XXX: this will open a block scope; however, if later we end
1694 	 * up formatting the block scope, then child nodes will inherit
1695 	 * the formatting.  Be careful.
1696 	 */
1697 	if (MDOC_Nm == tok)
1698 		return(blk_full(mdoc, tok, line, ppos, pos, buf));
1699 	assert(MDOC_Vt == tok);
1700 	return(blk_part_imp(mdoc, tok, line, ppos, pos, buf));
1701 }
1702 
1703 
1704 /* ARGSUSED */
1705 static int
1706 obsolete(MACRO_PROT_ARGS)
1707 {
1708 
1709 	mdoc_pmsg(mdoc, line, ppos, MANDOCERR_MACROOBS);
1710 	return(1);
1711 }
1712 
1713 
1714 /*
1715  * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
1716  * They're unusual because they're basically free-form text until a
1717  * macro is encountered.
1718  */
1719 static int
1720 phrase(struct mdoc *mdoc, int line, int ppos, char *buf)
1721 {
1722 	int		 la, pos;
1723 	enum margserr	 ac;
1724 	enum mdoct	 ntok;
1725 	char		*p;
1726 
1727 	for (pos = ppos; ; ) {
1728 		la = pos;
1729 
1730 		ac = mdoc_zargs(mdoc, line, &pos, buf, &p);
1731 
1732 		if (ARGS_ERROR == ac)
1733 			return(0);
1734 		if (ARGS_EOLN == ac)
1735 			break;
1736 
1737 		ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup_raw(p);
1738 
1739 		if (MDOC_MAX == ntok) {
1740 			if ( ! dword(mdoc, line, la, p, DELIM_MAX))
1741 				return(0);
1742 			continue;
1743 		}
1744 
1745 		if ( ! mdoc_macro(mdoc, ntok, line, la, &pos, buf))
1746 			return(0);
1747 		return(append_delims(mdoc, line, &pos, buf));
1748 	}
1749 
1750 	return(1);
1751 }
1752 
1753 
1754 /* ARGSUSED */
1755 static int
1756 phrase_ta(MACRO_PROT_ARGS)
1757 {
1758 	struct mdoc_node *n;
1759 	int		  la;
1760 	enum mdoct	  ntok;
1761 	enum margserr	  ac;
1762 	char		 *p;
1763 
1764 	/* Make sure we are in a column list or ignore this macro. */
1765 	n = mdoc->last;
1766 	while (NULL != n && MDOC_Bl != n->tok)
1767 		n = n->parent;
1768 	if (NULL == n || LIST_column != n->norm->Bl.type) {
1769 		mdoc_pmsg(mdoc, line, ppos, MANDOCERR_STRAYTA);
1770 		return(1);
1771 	}
1772 
1773 	/* Advance to the next column. */
1774 	if ( ! rew_sub(MDOC_BODY, mdoc, MDOC_It, line, ppos))
1775 		return(0);
1776 	if ( ! mdoc_body_alloc(mdoc, line, ppos, MDOC_It))
1777 		return(0);
1778 
1779 	for (;;) {
1780 		la = *pos;
1781 		ac = mdoc_zargs(mdoc, line, pos, buf, &p);
1782 
1783 		if (ARGS_ERROR == ac)
1784 			return(0);
1785 		if (ARGS_EOLN == ac)
1786 			break;
1787 
1788 		ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup_raw(p);
1789 
1790 		if (MDOC_MAX == ntok) {
1791 			if ( ! dword(mdoc, line, la, p, DELIM_MAX))
1792 				return(0);
1793 			continue;
1794 		}
1795 
1796 		if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
1797 			return(0);
1798 		return(append_delims(mdoc, line, pos, buf));
1799 	}
1800 
1801 	return(1);
1802 }
1803