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