]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_macro.c
Start fixing issues that beck@ helped find:
[mandoc.git] / mdoc_macro.c
1 /* $Id: mdoc_macro.c,v 1.137 2014/07/07 21:36:20 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 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
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_Nd == p->tok || MDOC_Ss == p->tok ||
428 MDOC_Sh == p->tok)
429 return(REWIND_MORE);
430 break;
431 default:
432 break;
433 }
434
435 /*
436 * Default block rewinding rules.
437 * In particular, always skip block end markers,
438 * and let all blocks rewind Nm children.
439 */
440 if (ENDBODY_NOT != p->end || MDOC_Nm == p->tok ||
441 (MDOC_BLOCK == p->type &&
442 ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)))
443 return(REWIND_MORE);
444
445 /*
446 * By default, closing out full blocks
447 * forces closing of broken explicit blocks,
448 * while closing out partial blocks
449 * allows delayed rewinding by default.
450 */
451 return (&blk_full == mdoc_macros[tok].fp ?
452 REWIND_FORCE : REWIND_LATER);
453 }
454
455 static int
456 rew_elem(struct mdoc *mdoc, enum mdoct tok)
457 {
458 struct mdoc_node *n;
459
460 n = mdoc->last;
461 if (MDOC_ELEM != n->type)
462 n = n->parent;
463 assert(MDOC_ELEM == n->type);
464 assert(tok == n->tok);
465
466 return(rew_last(mdoc, n));
467 }
468
469 /*
470 * We are trying to close a block identified by tok,
471 * but the child block *broken is still open.
472 * Thus, postpone closing the tok block
473 * until the rew_sub call closing *broken.
474 */
475 static int
476 make_pending(struct mdoc_node *broken, enum mdoct tok,
477 struct mdoc *mdoc, int line, int ppos)
478 {
479 struct mdoc_node *breaker;
480
481 /*
482 * Iterate backwards, searching for the block matching tok,
483 * that is, the block breaking the *broken block.
484 */
485 for (breaker = broken->parent; breaker; breaker = breaker->parent) {
486
487 /*
488 * If the *broken block had already been broken before
489 * and we encounter its breaker, make the tok block
490 * pending on the inner breaker.
491 * Graphically, "[A breaker=[B broken=[C->B B] tok=A] C]"
492 * becomes "[A broken=[B [C->B B] tok=A] C]"
493 * and finally "[A [B->A [C->B B] A] C]".
494 */
495 if (breaker == broken->pending) {
496 broken = breaker;
497 continue;
498 }
499
500 if (REWIND_THIS != rew_dohalt(tok, MDOC_BLOCK, breaker))
501 continue;
502 if (MDOC_BODY == broken->type)
503 broken = broken->parent;
504
505 /*
506 * Found the breaker.
507 * If another, outer breaker is already pending on
508 * the *broken block, we must not clobber the link
509 * to the outer breaker, but make it pending on the
510 * new, now inner breaker.
511 * Graphically, "[A breaker=[B broken=[C->A A] tok=B] C]"
512 * becomes "[A breaker=[B->A broken=[C A] tok=B] C]"
513 * and finally "[A [B->A [C->B A] B] C]".
514 */
515 if (broken->pending) {
516 struct mdoc_node *taker;
517
518 /*
519 * If the breaker had also been broken before,
520 * it cannot take on the outer breaker itself,
521 * but must hand it on to its own breakers.
522 * Graphically, this is the following situation:
523 * "[A [B breaker=[C->B B] broken=[D->A A] tok=C] D]"
524 * "[A taker=[B->A breaker=[C->B B] [D->C A] C] D]"
525 */
526 taker = breaker;
527 while (taker->pending)
528 taker = taker->pending;
529 taker->pending = broken->pending;
530 }
531 broken->pending = breaker;
532 mandoc_vmsg(MANDOCERR_BLK_NEST, mdoc->parse, line, ppos,
533 "%s breaks %s", mdoc_macronames[tok],
534 mdoc_macronames[broken->tok]);
535 return(1);
536 }
537
538 /*
539 * Found no matching block for tok.
540 * Are you trying to close a block that is not open?
541 */
542 return(0);
543 }
544
545 static int
546 rew_sub(enum mdoc_type t, struct mdoc *mdoc,
547 enum mdoct tok, int line, int ppos)
548 {
549 struct mdoc_node *n;
550
551 n = mdoc->last;
552 while (n) {
553 switch (rew_dohalt(tok, t, n)) {
554 case REWIND_NONE:
555 return(1);
556 case REWIND_THIS:
557 n->lastline = line -
558 (MDOC_NEWLINE & mdoc->flags &&
559 ! (MDOC_EXPLICIT & mdoc_macros[tok].flags));
560 break;
561 case REWIND_FORCE:
562 mandoc_vmsg(MANDOCERR_BLK_BROKEN, mdoc->parse,
563 line, ppos, "%s breaks %s",
564 mdoc_macronames[tok],
565 mdoc_macronames[n->tok]);
566 /* FALLTHROUGH */
567 case REWIND_MORE:
568 n->lastline = line -
569 (MDOC_NEWLINE & mdoc->flags ? 1 : 0);
570 n = n->parent;
571 continue;
572 case REWIND_LATER:
573 if (make_pending(n, tok, mdoc, line, ppos) ||
574 MDOC_BLOCK != t)
575 return(1);
576 /* FALLTHROUGH */
577 case REWIND_ERROR:
578 mandoc_msg(MANDOCERR_BLK_NOTOPEN,
579 mdoc->parse, line, ppos,
580 mdoc_macronames[tok]);
581 return(1);
582 }
583 break;
584 }
585
586 assert(n);
587 if ( ! rew_last(mdoc, n))
588 return(0);
589
590 /*
591 * The current block extends an enclosing block.
592 * Now that the current block ends, close the enclosing block, too.
593 */
594 while (NULL != (n = n->pending)) {
595 if ( ! rew_last(mdoc, n))
596 return(0);
597 if (MDOC_HEAD == n->type &&
598 ! mdoc_body_alloc(mdoc, n->line, n->pos, n->tok))
599 return(0);
600 }
601
602 return(1);
603 }
604
605 /*
606 * Allocate a word and check whether it's punctuation or not.
607 * Punctuation consists of those tokens found in mdoc_isdelim().
608 */
609 static int
610 dword(struct mdoc *mdoc, int line, int col, const char *p,
611 enum mdelim d, int may_append)
612 {
613
614 if (DELIM_MAX == d)
615 d = mdoc_isdelim(p);
616
617 if (may_append &&
618 ! ((MDOC_SYNOPSIS | MDOC_KEEP | MDOC_SMOFF) & mdoc->flags) &&
619 DELIM_NONE == d && MDOC_TEXT == mdoc->last->type &&
620 DELIM_NONE == mdoc_isdelim(mdoc->last->string)) {
621 mdoc_word_append(mdoc, p);
622 return(1);
623 }
624
625 if ( ! mdoc_word_alloc(mdoc, line, col, p))
626 return(0);
627
628 if (DELIM_OPEN == d)
629 mdoc->last->flags |= MDOC_DELIMO;
630
631 /*
632 * Closing delimiters only suppress the preceding space
633 * when they follow something, not when they start a new
634 * block or element, and not when they follow `No'.
635 *
636 * XXX Explicitly special-casing MDOC_No here feels
637 * like a layering violation. Find a better way
638 * and solve this in the code related to `No'!
639 */
640
641 else if (DELIM_CLOSE == d && mdoc->last->prev &&
642 mdoc->last->prev->tok != MDOC_No &&
643 mdoc->last->parent->tok != MDOC_Fd)
644 mdoc->last->flags |= MDOC_DELIMC;
645
646 return(1);
647 }
648
649 static int
650 append_delims(struct mdoc *mdoc, int line, int *pos, char *buf)
651 {
652 int la;
653 enum margserr ac;
654 char *p;
655
656 if ('\0' == buf[*pos])
657 return(1);
658
659 for (;;) {
660 la = *pos;
661 ac = mdoc_zargs(mdoc, line, pos, buf, &p);
662
663 if (ARGS_ERROR == ac)
664 return(0);
665 else if (ARGS_EOLN == ac)
666 break;
667
668 dword(mdoc, line, la, p, DELIM_MAX, 1);
669
670 /*
671 * If we encounter end-of-sentence symbols, then trigger
672 * the double-space.
673 *
674 * XXX: it's easy to allow this to propagate outward to
675 * the last symbol, such that `. )' will cause the
676 * correct double-spacing. However, (1) groff isn't
677 * smart enough to do this and (2) it would require
678 * knowing which symbols break this behaviour, for
679 * example, `. ;' shouldn't propagate the double-space.
680 */
681 if (mandoc_eos(p, strlen(p)))
682 mdoc->last->flags |= MDOC_EOS;
683 }
684
685 return(1);
686 }
687
688 /*
689 * Close out block partial/full explicit.
690 */
691 static int
692 blk_exp_close(MACRO_PROT_ARGS)
693 {
694 struct mdoc_node *body; /* Our own body. */
695 struct mdoc_node *later; /* A sub-block starting later. */
696 struct mdoc_node *n; /* For searching backwards. */
697
698 int j, lastarg, maxargs, flushed, nl;
699 enum margserr ac;
700 enum mdoct atok, ntok;
701 char *p;
702
703 nl = MDOC_NEWLINE & mdoc->flags;
704
705 switch (tok) {
706 case MDOC_Ec:
707 maxargs = 1;
708 break;
709 case MDOC_Ek:
710 mdoc->flags &= ~MDOC_KEEP;
711 default:
712 maxargs = 0;
713 break;
714 }
715
716 /*
717 * Search backwards for beginnings of blocks,
718 * both of our own and of pending sub-blocks.
719 */
720 atok = rew_alt(tok);
721 body = later = NULL;
722 for (n = mdoc->last; n; n = n->parent) {
723 if (MDOC_VALID & n->flags)
724 continue;
725
726 /* Remember the start of our own body. */
727 if (MDOC_BODY == n->type && atok == n->tok) {
728 if (ENDBODY_NOT == n->end)
729 body = n;
730 continue;
731 }
732
733 if (MDOC_BLOCK != n->type || MDOC_Nm == n->tok)
734 continue;
735 if (atok == n->tok) {
736 assert(body);
737
738 /*
739 * Found the start of our own block.
740 * When there is no pending sub block,
741 * just proceed to closing out.
742 */
743 if (NULL == later)
744 break;
745
746 /*
747 * When there is a pending sub block,
748 * postpone closing out the current block
749 * until the rew_sub() closing out the sub-block.
750 */
751 make_pending(later, tok, mdoc, line, ppos);
752
753 /*
754 * Mark the place where the formatting - but not
755 * the scope - of the current block ends.
756 */
757 if ( ! mdoc_endbody_alloc(mdoc, line, ppos,
758 atok, body, ENDBODY_SPACE))
759 return(0);
760 break;
761 }
762
763 /*
764 * When finding an open sub block, remember the last
765 * open explicit block, or, in case there are only
766 * implicit ones, the first open implicit block.
767 */
768 if (later &&
769 MDOC_EXPLICIT & mdoc_macros[later->tok].flags)
770 continue;
771 if (MDOC_It != n->tok)
772 later = n;
773 }
774
775 if ( ! (MDOC_PARSED & mdoc_macros[tok].flags)) {
776 if ('\0' != buf[*pos])
777 mandoc_vmsg(MANDOCERR_ARG_SKIP,
778 mdoc->parse, line, ppos,
779 "%s %s", mdoc_macronames[tok],
780 buf + *pos);
781 if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
782 return(0);
783 return(rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos));
784 }
785
786 if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
787 return(0);
788
789 if (NULL == later && maxargs > 0)
790 if ( ! mdoc_tail_alloc(mdoc, line, ppos, rew_alt(tok)))
791 return(0);
792
793 for (flushed = j = 0; ; j++) {
794 lastarg = *pos;
795
796 if (j == maxargs && ! flushed) {
797 if ( ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
798 return(0);
799 flushed = 1;
800 }
801
802 ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
803
804 if (ARGS_ERROR == ac)
805 return(0);
806 if (ARGS_PUNCT == ac)
807 break;
808 if (ARGS_EOLN == ac)
809 break;
810
811 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
812
813 if (MDOC_MAX == ntok) {
814 if ( ! dword(mdoc, line, lastarg, p, DELIM_MAX,
815 MDOC_JOIN & mdoc_macros[tok].flags))
816 return(0);
817 continue;
818 }
819
820 if ( ! flushed) {
821 if ( ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
822 return(0);
823 flushed = 1;
824 }
825
826 mdoc->flags &= ~MDOC_NEWLINE;
827
828 if ( ! mdoc_macro(mdoc, ntok, line, lastarg, pos, buf))
829 return(0);
830 break;
831 }
832
833 if ( ! flushed && ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
834 return(0);
835
836 if ( ! nl)
837 return(1);
838 return(append_delims(mdoc, line, pos, buf));
839 }
840
841 static int
842 in_line(MACRO_PROT_ARGS)
843 {
844 int la, scope, cnt, nc, nl;
845 enum margverr av;
846 enum mdoct ntok;
847 enum margserr ac;
848 enum mdelim d;
849 struct mdoc_arg *arg;
850 char *p;
851
852 nl = MDOC_NEWLINE & mdoc->flags;
853
854 /*
855 * Whether we allow ignored elements (those without content,
856 * usually because of reserved words) to squeak by.
857 */
858
859 switch (tok) {
860 case MDOC_An:
861 /* FALLTHROUGH */
862 case MDOC_Ar:
863 /* FALLTHROUGH */
864 case MDOC_Fl:
865 /* FALLTHROUGH */
866 case MDOC_Mt:
867 /* FALLTHROUGH */
868 case MDOC_Nm:
869 /* FALLTHROUGH */
870 case MDOC_Pa:
871 nc = 1;
872 break;
873 default:
874 nc = 0;
875 break;
876 }
877
878 for (arg = NULL;; ) {
879 la = *pos;
880 av = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
881
882 if (ARGV_WORD == av) {
883 *pos = la;
884 break;
885 }
886 if (ARGV_EOLN == av)
887 break;
888 if (ARGV_ARG == av)
889 continue;
890
891 mdoc_argv_free(arg);
892 return(0);
893 }
894
895 for (cnt = scope = 0;; ) {
896 la = *pos;
897 ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
898
899 if (ARGS_ERROR == ac)
900 return(0);
901 if (ARGS_EOLN == ac)
902 break;
903 if (ARGS_PUNCT == ac)
904 break;
905
906 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
907
908 /*
909 * In this case, we've located a submacro and must
910 * execute it. Close out scope, if open. If no
911 * elements have been generated, either create one (nc)
912 * or raise a warning.
913 */
914
915 if (MDOC_MAX != ntok) {
916 if (scope && ! rew_elem(mdoc, tok))
917 return(0);
918 if (nc && 0 == cnt) {
919 if ( ! mdoc_elem_alloc(mdoc,
920 line, ppos, tok, arg))
921 return(0);
922 if ( ! rew_last(mdoc, mdoc->last))
923 return(0);
924 } else if ( ! nc && 0 == cnt) {
925 mdoc_argv_free(arg);
926 mandoc_msg(MANDOCERR_MACRO_EMPTY,
927 mdoc->parse, line, ppos,
928 mdoc_macronames[tok]);
929 }
930
931 if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
932 return(0);
933 if ( ! nl)
934 return(1);
935 return(append_delims(mdoc, line, pos, buf));
936 }
937
938 /*
939 * Non-quote-enclosed punctuation. Set up our scope, if
940 * a word; rewind the scope, if a delimiter; then append
941 * the word.
942 */
943
944 d = ARGS_QWORD == ac ? DELIM_NONE : mdoc_isdelim(p);
945
946 if (DELIM_NONE != d) {
947 /*
948 * If we encounter closing punctuation, no word
949 * has been omitted, no scope is open, and we're
950 * allowed to have an empty element, then start
951 * a new scope. `Ar', `Fl', and `Li', only do
952 * this once per invocation. There may be more
953 * of these (all of them?).
954 */
955 if (0 == cnt && (nc || MDOC_Li == tok) &&
956 DELIM_CLOSE == d && ! scope) {
957 if ( ! mdoc_elem_alloc(mdoc,
958 line, ppos, tok, arg))
959 return(0);
960 if (MDOC_Ar == tok || MDOC_Li == tok ||
961 MDOC_Fl == tok)
962 cnt++;
963 scope = 1;
964 }
965 /*
966 * Close out our scope, if one is open, before
967 * any punctuation.
968 */
969 if (scope && ! rew_elem(mdoc, tok))
970 return(0);
971 scope = 0;
972 } else if ( ! scope) {
973 if ( ! mdoc_elem_alloc(mdoc, line, ppos, tok, arg))
974 return(0);
975 scope = 1;
976 }
977
978 if (DELIM_NONE == d)
979 cnt++;
980
981 if ( ! dword(mdoc, line, la, p, d,
982 MDOC_JOIN & mdoc_macros[tok].flags))
983 return(0);
984
985 /*
986 * `Fl' macros have their scope re-opened with each new
987 * word so that the `-' can be added to each one without
988 * having to parse out spaces.
989 */
990 if (scope && MDOC_Fl == tok) {
991 if ( ! rew_elem(mdoc, tok))
992 return(0);
993 scope = 0;
994 }
995 }
996
997 if (scope && ! rew_elem(mdoc, tok))
998 return(0);
999
1000 /*
1001 * If no elements have been collected and we're allowed to have
1002 * empties (nc), open a scope and close it out. Otherwise,
1003 * raise a warning.
1004 */
1005
1006 if (nc && 0 == cnt) {
1007 if ( ! mdoc_elem_alloc(mdoc, line, ppos, tok, arg))
1008 return(0);
1009 if ( ! rew_last(mdoc, mdoc->last))
1010 return(0);
1011 } else if ( ! nc && 0 == cnt) {
1012 mdoc_argv_free(arg);
1013 mandoc_msg(MANDOCERR_MACRO_EMPTY, mdoc->parse,
1014 line, ppos, mdoc_macronames[tok]);
1015 }
1016
1017 if ( ! nl)
1018 return(1);
1019 return(append_delims(mdoc, line, pos, buf));
1020 }
1021
1022 static int
1023 blk_full(MACRO_PROT_ARGS)
1024 {
1025 int la, nl, nparsed;
1026 struct mdoc_arg *arg;
1027 struct mdoc_node *head; /* save of head macro */
1028 struct mdoc_node *body; /* save of body macro */
1029 struct mdoc_node *n;
1030 enum mdoc_type mtt;
1031 enum mdoct ntok;
1032 enum margserr ac, lac;
1033 enum margverr av;
1034 char *p;
1035
1036 nl = MDOC_NEWLINE & mdoc->flags;
1037
1038 /* Close out prior implicit scope. */
1039
1040 if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)) {
1041 if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
1042 return(0);
1043 if ( ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
1044 return(0);
1045 }
1046
1047 /*
1048 * This routine accommodates implicitly- and explicitly-scoped
1049 * macro openings. Implicit ones first close out prior scope
1050 * (seen above). Delay opening the head until necessary to
1051 * allow leading punctuation to print. Special consideration
1052 * for `It -column', which has phrase-part syntax instead of
1053 * regular child nodes.
1054 */
1055
1056 for (arg = NULL;; ) {
1057 la = *pos;
1058 av = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1059
1060 if (ARGV_WORD == av) {
1061 *pos = la;
1062 break;
1063 }
1064
1065 if (ARGV_EOLN == av)
1066 break;
1067 if (ARGV_ARG == av)
1068 continue;
1069
1070 mdoc_argv_free(arg);
1071 return(0);
1072 }
1073
1074 if ( ! mdoc_block_alloc(mdoc, line, ppos, tok, arg))
1075 return(0);
1076
1077 head = body = NULL;
1078
1079 /*
1080 * Exception: Heads of `It' macros in `-diag' lists are not
1081 * parsed, even though `It' macros in general are parsed.
1082 */
1083 nparsed = MDOC_It == tok &&
1084 MDOC_Bl == mdoc->last->parent->tok &&
1085 LIST_diag == mdoc->last->parent->norm->Bl.type;
1086
1087 /*
1088 * The `Nd' macro has all arguments in its body: it's a hybrid
1089 * of block partial-explicit and full-implicit. Stupid.
1090 */
1091
1092 if (MDOC_Nd == tok) {
1093 if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1094 return(0);
1095 head = mdoc->last;
1096 if ( ! rew_sub(MDOC_HEAD, mdoc, tok, line, ppos))
1097 return(0);
1098 if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1099 return(0);
1100 body = mdoc->last;
1101 }
1102
1103 if (MDOC_Bk == tok)
1104 mdoc->flags |= MDOC_KEEP;
1105
1106 ac = ARGS_ERROR;
1107
1108 for ( ; ; ) {
1109 la = *pos;
1110 /* Initialise last-phrase-type with ARGS_PEND. */
1111 lac = ARGS_ERROR == ac ? ARGS_PEND : ac;
1112 ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1113
1114 if (ARGS_PUNCT == ac)
1115 break;
1116
1117 if (ARGS_ERROR == ac)
1118 return(0);
1119
1120 if (ARGS_EOLN == ac) {
1121 if (ARGS_PPHRASE != lac && ARGS_PHRASE != lac)
1122 break;
1123 /*
1124 * This is necessary: if the last token on a
1125 * line is a `Ta' or tab, then we'll get
1126 * ARGS_EOLN, so we must be smart enough to
1127 * reopen our scope if the last parse was a
1128 * phrase or partial phrase.
1129 */
1130 if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
1131 return(0);
1132 if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1133 return(0);
1134 body = mdoc->last;
1135 break;
1136 }
1137
1138 /*
1139 * Emit leading punctuation (i.e., punctuation before
1140 * the MDOC_HEAD) for non-phrase types.
1141 */
1142
1143 if (NULL == head &&
1144 ARGS_PEND != ac &&
1145 ARGS_PHRASE != ac &&
1146 ARGS_PPHRASE != ac &&
1147 ARGS_QWORD != ac &&
1148 DELIM_OPEN == mdoc_isdelim(p)) {
1149 if ( ! dword(mdoc, line, la, p, DELIM_OPEN, 0))
1150 return(0);
1151 continue;
1152 }
1153
1154 /* Open a head if one hasn't been opened. */
1155
1156 if (NULL == head) {
1157 if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1158 return(0);
1159 head = mdoc->last;
1160 }
1161
1162 if (ARGS_PHRASE == ac ||
1163 ARGS_PEND == ac ||
1164 ARGS_PPHRASE == ac) {
1165 /*
1166 * If we haven't opened a body yet, rewind the
1167 * head; if we have, rewind that instead.
1168 */
1169
1170 mtt = body ? MDOC_BODY : MDOC_HEAD;
1171 if ( ! rew_sub(mtt, mdoc, tok, line, ppos))
1172 return(0);
1173
1174 /* Then allocate our body context. */
1175
1176 if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1177 return(0);
1178 body = mdoc->last;
1179
1180 /*
1181 * Process phrases: set whether we're in a
1182 * partial-phrase (this effects line handling)
1183 * then call down into the phrase parser.
1184 */
1185
1186 if (ARGS_PPHRASE == ac)
1187 mdoc->flags |= MDOC_PPHRASE;
1188 if (ARGS_PEND == ac && ARGS_PPHRASE == lac)
1189 mdoc->flags |= MDOC_PPHRASE;
1190
1191 if ( ! phrase(mdoc, line, la, buf))
1192 return(0);
1193
1194 mdoc->flags &= ~MDOC_PPHRASE;
1195 continue;
1196 }
1197
1198 ntok = nparsed || ARGS_QWORD == ac ?
1199 MDOC_MAX : lookup(tok, p);
1200
1201 if (MDOC_MAX == ntok) {
1202 if ( ! dword(mdoc, line, la, p, DELIM_MAX,
1203 MDOC_JOIN & mdoc_macros[tok].flags))
1204 return(0);
1205 continue;
1206 }
1207
1208 if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
1209 return(0);
1210 break;
1211 }
1212
1213 if (NULL == head) {
1214 if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1215 return(0);
1216 head = mdoc->last;
1217 }
1218
1219 if (nl && ! append_delims(mdoc, line, pos, buf))
1220 return(0);
1221
1222 /* If we've already opened our body, exit now. */
1223
1224 if (NULL != body)
1225 goto out;
1226
1227 /*
1228 * If there is an open (i.e., unvalidated) sub-block requiring
1229 * explicit close-out, postpone switching the current block from
1230 * head to body until the rew_sub() call closing out that
1231 * sub-block.
1232 */
1233 for (n = mdoc->last; n && n != head; n = n->parent) {
1234 if (MDOC_BLOCK == n->type &&
1235 MDOC_EXPLICIT & mdoc_macros[n->tok].flags &&
1236 ! (MDOC_VALID & n->flags)) {
1237 n->pending = head;
1238 return(1);
1239 }
1240 }
1241
1242 /* Close out scopes to remain in a consistent state. */
1243
1244 if ( ! rew_sub(MDOC_HEAD, mdoc, tok, line, ppos))
1245 return(0);
1246 if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1247 return(0);
1248
1249 out:
1250 if ( ! (MDOC_FREECOL & mdoc->flags))
1251 return(1);
1252
1253 if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
1254 return(0);
1255 if ( ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
1256 return(0);
1257
1258 mdoc->flags &= ~MDOC_FREECOL;
1259 return(1);
1260 }
1261
1262 static int
1263 blk_part_imp(MACRO_PROT_ARGS)
1264 {
1265 int la, nl;
1266 enum mdoct ntok;
1267 enum margserr ac;
1268 char *p;
1269 struct mdoc_node *blk; /* saved block context */
1270 struct mdoc_node *body; /* saved body context */
1271 struct mdoc_node *n;
1272
1273 nl = MDOC_NEWLINE & mdoc->flags;
1274
1275 /*
1276 * A macro that spans to the end of the line. This is generally
1277 * (but not necessarily) called as the first macro. The block
1278 * has a head as the immediate child, which is always empty,
1279 * followed by zero or more opening punctuation nodes, then the
1280 * body (which may be empty, depending on the macro), then zero
1281 * or more closing punctuation nodes.
1282 */
1283
1284 if ( ! mdoc_block_alloc(mdoc, line, ppos, tok, NULL))
1285 return(0);
1286
1287 blk = mdoc->last;
1288
1289 if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1290 return(0);
1291 if ( ! rew_sub(MDOC_HEAD, mdoc, tok, line, ppos))
1292 return(0);
1293
1294 /*
1295 * Open the body scope "on-demand", that is, after we've
1296 * processed all our the leading delimiters (open parenthesis,
1297 * etc.).
1298 */
1299
1300 for (body = NULL; ; ) {
1301 la = *pos;
1302 ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1303
1304 if (ARGS_ERROR == ac)
1305 return(0);
1306 if (ARGS_EOLN == ac)
1307 break;
1308 if (ARGS_PUNCT == ac)
1309 break;
1310
1311 if (NULL == body && ARGS_QWORD != ac &&
1312 DELIM_OPEN == mdoc_isdelim(p)) {
1313 if ( ! dword(mdoc, line, la, p, DELIM_OPEN, 0))
1314 return(0);
1315 continue;
1316 }
1317
1318 if (NULL == body) {
1319 if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1320 return(0);
1321 body = mdoc->last;
1322 }
1323
1324 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1325
1326 if (MDOC_MAX == ntok) {
1327 if ( ! dword(mdoc, line, la, p, DELIM_MAX,
1328 MDOC_JOIN & mdoc_macros[tok].flags))
1329 return(0);
1330 continue;
1331 }
1332
1333 if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
1334 return(0);
1335 break;
1336 }
1337
1338 /* Clean-ups to leave in a consistent state. */
1339
1340 if (NULL == body) {
1341 if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1342 return(0);
1343 body = mdoc->last;
1344 }
1345
1346 /*
1347 * If there is an open sub-block requiring explicit close-out,
1348 * postpone closing out the current block
1349 * until the rew_sub() call closing out the sub-block.
1350 */
1351 for (n = mdoc->last; n && n != body && n != blk->parent;
1352 n = n->parent) {
1353 if (MDOC_BLOCK == n->type &&
1354 MDOC_EXPLICIT & mdoc_macros[n->tok].flags &&
1355 ! (MDOC_VALID & n->flags)) {
1356 make_pending(n, tok, mdoc, line, ppos);
1357 if ( ! mdoc_endbody_alloc(mdoc, line, ppos,
1358 tok, body, ENDBODY_NOSPACE))
1359 return(0);
1360 return(1);
1361 }
1362 }
1363 assert(n == body);
1364
1365 if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
1366 return(0);
1367
1368 /* Standard appending of delimiters. */
1369
1370 if (nl && ! append_delims(mdoc, line, pos, buf))
1371 return(0);
1372
1373 /* Rewind scope, if applicable. */
1374
1375 if ( ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
1376 return(0);
1377
1378 /* Move trailing .Ns out of scope. */
1379
1380 for (n = body->child; n && n->next; n = n->next)
1381 /* Do nothing. */ ;
1382 if (n && MDOC_Ns == n->tok)
1383 mdoc_node_relink(mdoc, n);
1384
1385 return(1);
1386 }
1387
1388 static int
1389 blk_part_exp(MACRO_PROT_ARGS)
1390 {
1391 int la, nl;
1392 enum margserr ac;
1393 struct mdoc_node *head; /* keep track of head */
1394 struct mdoc_node *body; /* keep track of body */
1395 char *p;
1396 enum mdoct ntok;
1397
1398 nl = MDOC_NEWLINE & mdoc->flags;
1399
1400 /*
1401 * The opening of an explicit macro having zero or more leading
1402 * punctuation nodes; a head with optional single element (the
1403 * case of `Eo'); and a body that may be empty.
1404 */
1405
1406 if ( ! mdoc_block_alloc(mdoc, line, ppos, tok, NULL))
1407 return(0);
1408
1409 for (head = body = NULL; ; ) {
1410 la = *pos;
1411 ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1412
1413 if (ARGS_ERROR == ac)
1414 return(0);
1415 if (ARGS_PUNCT == ac)
1416 break;
1417 if (ARGS_EOLN == ac)
1418 break;
1419
1420 /* Flush out leading punctuation. */
1421
1422 if (NULL == head && ARGS_QWORD != ac &&
1423 DELIM_OPEN == mdoc_isdelim(p)) {
1424 assert(NULL == body);
1425 if ( ! dword(mdoc, line, la, p, DELIM_OPEN, 0))
1426 return(0);
1427 continue;
1428 }
1429
1430 if (NULL == head) {
1431 assert(NULL == body);
1432 if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1433 return(0);
1434 head = mdoc->last;
1435 }
1436
1437 /*
1438 * `Eo' gobbles any data into the head, but most other
1439 * macros just immediately close out and begin the body.
1440 */
1441
1442 if (NULL == body) {
1443 assert(head);
1444 /* No check whether it's a macro! */
1445 if (MDOC_Eo == tok)
1446 if ( ! dword(mdoc, line, la, p, DELIM_MAX, 0))
1447 return(0);
1448
1449 if ( ! rew_sub(MDOC_HEAD, mdoc, tok, line, ppos))
1450 return(0);
1451 if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1452 return(0);
1453 body = mdoc->last;
1454
1455 if (MDOC_Eo == tok)
1456 continue;
1457 }
1458
1459 assert(NULL != head && NULL != body);
1460
1461 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1462
1463 if (MDOC_MAX == ntok) {
1464 if ( ! dword(mdoc, line, la, p, DELIM_MAX,
1465 MDOC_JOIN & mdoc_macros[tok].flags))
1466 return(0);
1467 continue;
1468 }
1469
1470 if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
1471 return(0);
1472 break;
1473 }
1474
1475 /* Clean-up to leave in a consistent state. */
1476
1477 if (NULL == head)
1478 if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1479 return(0);
1480
1481 if (NULL == body) {
1482 if ( ! rew_sub(MDOC_HEAD, mdoc, tok, line, ppos))
1483 return(0);
1484 if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1485 return(0);
1486 }
1487
1488 /* Standard appending of delimiters. */
1489
1490 if ( ! nl)
1491 return(1);
1492 return(append_delims(mdoc, line, pos, buf));
1493 }
1494
1495 static int
1496 in_line_argn(MACRO_PROT_ARGS)
1497 {
1498 int la, flushed, j, maxargs, nl;
1499 enum margserr ac;
1500 enum margverr av;
1501 struct mdoc_arg *arg;
1502 char *p;
1503 enum mdoct ntok;
1504
1505 nl = MDOC_NEWLINE & mdoc->flags;
1506
1507 /*
1508 * A line macro that has a fixed number of arguments (maxargs).
1509 * Only open the scope once the first non-leading-punctuation is
1510 * found (unless MDOC_IGNDELIM is noted, like in `Pf'), then
1511 * keep it open until the maximum number of arguments are
1512 * exhausted.
1513 */
1514
1515 switch (tok) {
1516 case MDOC_Ap:
1517 /* FALLTHROUGH */
1518 case MDOC_No:
1519 /* FALLTHROUGH */
1520 case MDOC_Ns:
1521 /* FALLTHROUGH */
1522 case MDOC_Ux:
1523 maxargs = 0;
1524 break;
1525 case MDOC_Bx:
1526 /* FALLTHROUGH */
1527 case MDOC_Es:
1528 /* FALLTHROUGH */
1529 case MDOC_Xr:
1530 maxargs = 2;
1531 break;
1532 default:
1533 maxargs = 1;
1534 break;
1535 }
1536
1537 for (arg = NULL; ; ) {
1538 la = *pos;
1539 av = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1540
1541 if (ARGV_WORD == av) {
1542 *pos = la;
1543 break;
1544 }
1545
1546 if (ARGV_EOLN == av)
1547 break;
1548 if (ARGV_ARG == av)
1549 continue;
1550
1551 mdoc_argv_free(arg);
1552 return(0);
1553 }
1554
1555 for (flushed = j = 0; ; ) {
1556 la = *pos;
1557 ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1558
1559 if (ARGS_ERROR == ac)
1560 return(0);
1561 if (ARGS_PUNCT == ac)
1562 break;
1563 if (ARGS_EOLN == ac)
1564 break;
1565
1566 if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1567 ARGS_QWORD != ac && 0 == j &&
1568 DELIM_OPEN == mdoc_isdelim(p)) {
1569 if ( ! dword(mdoc, line, la, p, DELIM_OPEN, 0))
1570 return(0);
1571 continue;
1572 } else if (0 == j)
1573 if ( ! mdoc_elem_alloc(mdoc, line, ppos, tok, arg))
1574 return(0);
1575
1576 if (j == maxargs && ! flushed) {
1577 if ( ! rew_elem(mdoc, tok))
1578 return(0);
1579 flushed = 1;
1580 }
1581
1582 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1583
1584 if (MDOC_MAX != ntok) {
1585 if ( ! flushed && ! rew_elem(mdoc, tok))
1586 return(0);
1587 flushed = 1;
1588 if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
1589 return(0);
1590 j++;
1591 break;
1592 }
1593
1594 if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1595 ARGS_QWORD != ac &&
1596 ! flushed &&
1597 DELIM_NONE != mdoc_isdelim(p)) {
1598 if ( ! rew_elem(mdoc, tok))
1599 return(0);
1600 flushed = 1;
1601 }
1602
1603 if ( ! dword(mdoc, line, la, p, DELIM_MAX,
1604 MDOC_JOIN & mdoc_macros[tok].flags))
1605 return(0);
1606 j++;
1607 }
1608
1609 if (0 == j && ! mdoc_elem_alloc(mdoc, line, ppos, tok, arg))
1610 return(0);
1611
1612 /* Close out in a consistent state. */
1613
1614 if ( ! flushed && ! rew_elem(mdoc, tok))
1615 return(0);
1616 if ( ! nl)
1617 return(1);
1618 return(append_delims(mdoc, line, pos, buf));
1619 }
1620
1621 static int
1622 in_line_eoln(MACRO_PROT_ARGS)
1623 {
1624 int la;
1625 enum margserr ac;
1626 enum margverr av;
1627 struct mdoc_arg *arg;
1628 char *p;
1629 enum mdoct ntok;
1630
1631 assert( ! (MDOC_PARSED & mdoc_macros[tok].flags));
1632
1633 if (tok == MDOC_Pp)
1634 rew_sub(MDOC_BLOCK, mdoc, MDOC_Nm, line, ppos);
1635
1636 /* Parse macro arguments. */
1637
1638 for (arg = NULL; ; ) {
1639 la = *pos;
1640 av = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1641
1642 if (ARGV_WORD == av) {
1643 *pos = la;
1644 break;
1645 }
1646 if (ARGV_EOLN == av)
1647 break;
1648 if (ARGV_ARG == av)
1649 continue;
1650
1651 mdoc_argv_free(arg);
1652 return(0);
1653 }
1654
1655 /* Open element scope. */
1656
1657 if ( ! mdoc_elem_alloc(mdoc, line, ppos, tok, arg))
1658 return(0);
1659
1660 /* Parse argument terms. */
1661
1662 for (;;) {
1663 la = *pos;
1664 ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1665
1666 if (ARGS_ERROR == ac)
1667 return(0);
1668 if (ARGS_EOLN == ac)
1669 break;
1670
1671 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1672
1673 if (MDOC_MAX == ntok) {
1674 if ( ! dword(mdoc, line, la, p, DELIM_MAX,
1675 MDOC_JOIN & mdoc_macros[tok].flags))
1676 return(0);
1677 continue;
1678 }
1679
1680 if ( ! rew_elem(mdoc, tok))
1681 return(0);
1682 return(mdoc_macro(mdoc, ntok, line, la, pos, buf));
1683 }
1684
1685 /* Close out (no delimiters). */
1686
1687 return(rew_elem(mdoc, tok));
1688 }
1689
1690 static int
1691 ctx_synopsis(MACRO_PROT_ARGS)
1692 {
1693 int nl;
1694
1695 nl = MDOC_NEWLINE & mdoc->flags;
1696
1697 /* If we're not in the SYNOPSIS, go straight to in-line. */
1698 if ( ! (MDOC_SYNOPSIS & mdoc->flags))
1699 return(in_line(mdoc, tok, line, ppos, pos, buf));
1700
1701 /* If we're a nested call, same place. */
1702 if ( ! nl)
1703 return(in_line(mdoc, tok, line, ppos, pos, buf));
1704
1705 /*
1706 * XXX: this will open a block scope; however, if later we end
1707 * up formatting the block scope, then child nodes will inherit
1708 * the formatting. Be careful.
1709 */
1710 if (MDOC_Nm == tok)
1711 return(blk_full(mdoc, tok, line, ppos, pos, buf));
1712 assert(MDOC_Vt == tok);
1713 return(blk_part_imp(mdoc, tok, line, ppos, pos, buf));
1714 }
1715
1716 /*
1717 * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
1718 * They're unusual because they're basically free-form text until a
1719 * macro is encountered.
1720 */
1721 static int
1722 phrase(struct mdoc *mdoc, int line, int ppos, char *buf)
1723 {
1724 int la, pos;
1725 enum margserr ac;
1726 enum mdoct ntok;
1727 char *p;
1728
1729 for (pos = ppos; ; ) {
1730 la = pos;
1731
1732 ac = mdoc_zargs(mdoc, line, &pos, buf, &p);
1733
1734 if (ARGS_ERROR == ac)
1735 return(0);
1736 if (ARGS_EOLN == ac)
1737 break;
1738
1739 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup_raw(p);
1740
1741 if (MDOC_MAX == ntok) {
1742 if ( ! dword(mdoc, line, la, p, DELIM_MAX, 1))
1743 return(0);
1744 continue;
1745 }
1746
1747 if ( ! mdoc_macro(mdoc, ntok, line, la, &pos, buf))
1748 return(0);
1749 return(append_delims(mdoc, line, &pos, buf));
1750 }
1751
1752 return(1);
1753 }
1754
1755 static int
1756 phrase_ta(MACRO_PROT_ARGS)
1757 {
1758 struct mdoc_node *n;
1759 int la;
1760 enum mdoct ntok;
1761 enum margserr ac;
1762 char *p;
1763
1764 /* Make sure we are in a column list or ignore this macro. */
1765 n = mdoc->last;
1766 while (NULL != n && MDOC_Bl != n->tok)
1767 n = n->parent;
1768 if (NULL == n || LIST_column != n->norm->Bl.type) {
1769 mandoc_msg(MANDOCERR_TA_STRAY, mdoc->parse,
1770 line, ppos, NULL);
1771 return(1);
1772 }
1773
1774 /* Advance to the next column. */
1775 if ( ! rew_sub(MDOC_BODY, mdoc, MDOC_It, line, ppos))
1776 return(0);
1777 if ( ! mdoc_body_alloc(mdoc, line, ppos, MDOC_It))
1778 return(0);
1779
1780 for (;;) {
1781 la = *pos;
1782 ac = mdoc_zargs(mdoc, line, pos, buf, &p);
1783
1784 if (ARGS_ERROR == ac)
1785 return(0);
1786 if (ARGS_EOLN == ac)
1787 break;
1788
1789 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup_raw(p);
1790
1791 if (MDOC_MAX == ntok) {
1792 if ( ! dword(mdoc, line, la, p, DELIM_MAX,
1793 MDOC_JOIN & mdoc_macros[tok].flags))
1794 return(0);
1795 continue;
1796 }
1797
1798 if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
1799 return(0);
1800 return(append_delims(mdoc, line, pos, buf));
1801 }
1802
1803 return(1);
1804 }