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