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