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