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