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