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