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