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