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