]> git.cameronkatri.com Git - mandoc.git/blob - macro.c
*** empty log message ***
[mandoc.git] / macro.c
1 /* $Id: macro.c,v 1.30 2009/01/09 14:45:44 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008 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
7 * above copyright notice and this permission notice appear in all
8 * copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19 #include <assert.h>
20 #include <ctype.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #ifdef __linux__
25 #include <time.h>
26 #endif
27
28 #include "private.h"
29
30 /* FIXME: maxlineargs should be per LINE, no per TOKEN. */
31
32 static int rewind_elem(struct mdoc *, int);
33 static int rewind_impblock(struct mdoc *, int);
34 static int rewind_expblock(struct mdoc *, int);
35 static int rewind_head(struct mdoc *, int);
36 static int rewind_body(struct mdoc *, int);
37 static int rewind_last(struct mdoc *, struct mdoc_node *);
38 static int append_delims(struct mdoc *,
39 int, int, int *, char *);
40 static int lookup(struct mdoc *, int, int, int, const char *);
41
42
43 static int
44 lookup(struct mdoc *mdoc, int line, int pos, int from, const char *p)
45 {
46 int res;
47
48 res = mdoc_find(mdoc, p);
49 if (MDOC_PARSED & mdoc_macros[from].flags)
50 return(res);
51 if (MDOC_MAX == res)
52 return(res);
53
54 if ( ! mdoc_pwarn(mdoc, line, pos, WARN_SYNTAX_MACLIKE))
55 return(-1);
56 return(MDOC_MAX);
57 }
58
59
60 static int
61 rewind_last(struct mdoc *mdoc, struct mdoc_node *to)
62 {
63
64 assert(to);
65 mdoc->next = MDOC_NEXT_SIBLING;
66 if (mdoc->last == to) {
67 if ( ! mdoc_valid_post(mdoc))
68 return(0);
69 if ( ! mdoc_action_post(mdoc))
70 return(0);
71 return(1);
72 }
73
74 do {
75 mdoc->last = mdoc->last->parent;
76 assert(mdoc->last);
77 if ( ! mdoc_valid_post(mdoc))
78 return(0);
79 if ( ! mdoc_action_post(mdoc))
80 return(0);
81 } while (mdoc->last != to);
82
83 return(1);
84 }
85
86
87 static int
88 rewind_elem(struct mdoc *mdoc, int tok)
89 {
90 struct mdoc_node *n;
91
92 n = mdoc->last;
93 if (MDOC_ELEM != n->type)
94 n = n->parent;
95 assert(MDOC_ELEM == n->type);
96 assert(tok == n->data.elem.tok);
97
98 return(rewind_last(mdoc, n));
99 }
100
101
102 static int
103 rewind_body(struct mdoc *mdoc, int tok)
104 {
105 struct mdoc_node *n;
106 int t;
107
108 assert(mdoc->last);
109
110 /* LINTED */
111 for (n = mdoc->last; n; n = n->parent) {
112 if (MDOC_BODY != n->type)
113 continue;
114 if (tok == (t = n->data.head.tok))
115 break;
116 if ( ! (MDOC_EXPLICIT & mdoc_macros[t].flags))
117 continue;
118 return(mdoc_verr(mdoc, n, ERR_SCOPE_BREAK));
119 }
120
121 assert(n);
122 return(rewind_last(mdoc, n));
123 }
124
125
126 static int
127 rewind_head(struct mdoc *mdoc, int tok)
128 {
129 struct mdoc_node *n;
130 int t;
131
132 assert(mdoc->last);
133
134 /* LINTED */
135 for (n = mdoc->last; n; n = n->parent) {
136 if (MDOC_HEAD != n->type)
137 continue;
138 if (tok == (t = n->data.head.tok))
139 break;
140 if ( ! (MDOC_EXPLICIT & mdoc_macros[t].flags))
141 continue;
142 return(mdoc_verr(mdoc, n, ERR_SCOPE_BREAK));
143 }
144
145 assert(n);
146 return(rewind_last(mdoc, n));
147 }
148
149
150 static int
151 rewind_expblock(struct mdoc *mdoc, int tok)
152 {
153 struct mdoc_node *n;
154 int t;
155
156 assert(mdoc->last);
157
158 /* LINTED */
159 for (n = mdoc->last; n; n = n->parent) {
160 if (MDOC_BLOCK != n->type)
161 continue;
162 if (tok == (t = n->data.block.tok))
163 break;
164 if (MDOC_NESTED & mdoc_macros[t].flags)
165 continue;
166 return(mdoc_verr(mdoc, n, ERR_SCOPE_BREAK));
167 }
168
169 assert(n);
170 return(rewind_last(mdoc, n));
171 }
172
173
174 static int
175 rewind_impblock(struct mdoc *mdoc, int tok)
176 {
177 int t;
178 struct mdoc_node *n;
179
180 n = mdoc->last ? mdoc->last->parent : NULL;
181
182 /* LINTED */
183 for ( ; n; n = n->parent) {
184 if (MDOC_BLOCK != n->type)
185 continue;
186 if (tok == (t = n->data.block.tok))
187 break;
188 if ( ! (MDOC_EXPLICIT & mdoc_macros[t].flags))
189 continue;
190 if (MDOC_NESTED & mdoc_macros[t].flags)
191 return(1);
192 return(mdoc_verr(mdoc, n, ERR_SCOPE_BREAK));
193 }
194
195 if (NULL == n)
196 return(1);
197 return(rewind_last(mdoc, n));
198 }
199
200
201 static int
202 append_delims(struct mdoc *mdoc, int tok,
203 int line, int *pos, char *buf)
204 {
205 int c, lastarg;
206 char *p;
207
208 if (0 == buf[*pos])
209 return(1);
210
211 for (;;) {
212 lastarg = *pos;
213 c = mdoc_args(mdoc, line, pos, buf, 0, &p);
214 if (ARGS_ERROR == c)
215 return(0);
216 else if (ARGS_EOLN == c)
217 break;
218 assert(mdoc_isdelim(p));
219 if ( ! mdoc_word_alloc(mdoc, line, lastarg, p))
220 return(0);
221 mdoc->next = MDOC_NEXT_SIBLING;
222 }
223
224 return(1);
225 }
226
227
228 /* ARGSUSED */
229 int
230 macro_close_explicit(MACRO_PROT_ARGS)
231 {
232 int tt, j, c, lastarg, maxargs, flushed;
233 char *p;
234
235 switch (tok) {
236 case (MDOC_Ac):
237 tt = MDOC_Ao;
238 break;
239 case (MDOC_Bc):
240 tt = MDOC_Bo;
241 break;
242 case (MDOC_Dc):
243 tt = MDOC_Do;
244 break;
245 case (MDOC_Ec):
246 tt = MDOC_Eo;
247 break;
248 case (MDOC_Ed):
249 tt = MDOC_Bd;
250 break;
251 case (MDOC_Ef):
252 tt = MDOC_Bf;
253 break;
254 case (MDOC_Ek):
255 tt = MDOC_Bk;
256 break;
257 case (MDOC_El):
258 tt = MDOC_Bl;
259 break;
260 case (MDOC_Fc):
261 tt = MDOC_Fo;
262 break;
263 case (MDOC_Oc):
264 tt = MDOC_Oo;
265 break;
266 case (MDOC_Pc):
267 tt = MDOC_Po;
268 break;
269 case (MDOC_Qc):
270 tt = MDOC_Qo;
271 break;
272 case (MDOC_Re):
273 tt = MDOC_Rs;
274 break;
275 case (MDOC_Sc):
276 tt = MDOC_So;
277 break;
278 case (MDOC_Xc):
279 tt = MDOC_Xo;
280 break;
281 default:
282 abort();
283 /* NOTREACHED */
284 }
285
286 switch (tok) {
287 case (MDOC_Ec):
288 maxargs = 1;
289 break;
290 default:
291 maxargs = 0;
292 break;
293 }
294
295 if ( ! (MDOC_CALLABLE & mdoc_macros[tok].flags)) {
296 if (buf[*pos])
297 return(rewind_expblock(mdoc, tt));
298 return(mdoc_perr(mdoc, line, ppos, ERR_ARGS_EQ0));
299 }
300
301 if ( ! rewind_body(mdoc, tt))
302 return(0);
303
304 lastarg = ppos;
305 flushed = 0;
306
307 if (maxargs > 0) {
308 if ( ! mdoc_tail_alloc(mdoc, line, ppos, tt))
309 return(0);
310 mdoc->next = MDOC_NEXT_CHILD;
311 }
312
313 for (j = 0; j < MDOC_LINEARG_MAX; j++) {
314 lastarg = *pos;
315
316 if (j == maxargs && ! flushed) {
317 if ( ! rewind_expblock(mdoc, tt))
318 return(0);
319 flushed = 1;
320 }
321
322 c = mdoc_args(mdoc, line, pos, buf, ARGS_DELIM, &p);
323 if (ARGS_ERROR == c)
324 return(0);
325 if (ARGS_PUNCT == c)
326 break;
327 if (ARGS_EOLN == c)
328 break;
329
330 if (-1 == (c = lookup(mdoc, line, lastarg, tok, p)))
331 return(0);
332 else if (MDOC_MAX != c) {
333 if ( ! flushed) {
334 if ( ! rewind_expblock(mdoc, tt))
335 return(0);
336 flushed = 1;
337 }
338 if ( ! mdoc_macro(mdoc, c, line, lastarg, pos, buf))
339 return(0);
340 break;
341 }
342
343 if ( ! mdoc_word_alloc(mdoc, line, lastarg, p))
344 return(0);
345 mdoc->next = MDOC_NEXT_SIBLING;
346 }
347
348 if (MDOC_LINEARG_MAX == j)
349 return(mdoc_perr(mdoc, line, ppos, ERR_ARGS_MANY));
350
351 if ( ! flushed && ! rewind_expblock(mdoc, tt))
352 return(0);
353
354 if (ppos > 1)
355 return(1);
356 return(append_delims(mdoc, tok, line, pos, buf));
357 }
358
359
360 /*
361 * A general text domain macro. When invoked, this opens a scope that
362 * accepts words until either end-of-line, only-punctuation, or a
363 * callable macro. If the word is punctuation (not only-punctuation),
364 * then the scope is closed out, the punctuation appended, then the
365 * scope opened again. If any terminating conditions are met, the scope
366 * is closed out. If this is the first macro in the line and
367 * only-punctuation remains, this punctuation is flushed.
368 */
369 int
370 macro_text(MACRO_PROT_ARGS)
371 {
372 int la, lastpunct, c, sz, fl, argc;
373 struct mdoc_arg argv[MDOC_LINEARG_MAX];
374 char *p;
375
376 la = ppos;
377 lastpunct = 0;
378
379 for (argc = 0; argc < MDOC_LINEARG_MAX; argc++) {
380 la = *pos;
381
382 c = mdoc_argv(mdoc, line, tok, &argv[argc], pos, buf);
383 if (ARGV_EOLN == c || ARGV_WORD == c)
384 break;
385 else if (ARGV_ARG == c)
386 continue;
387 mdoc_argv_free(argc, argv);
388 return(0);
389 }
390
391 if (MDOC_LINEARG_MAX == argc) {
392 mdoc_argv_free(argc, argv);
393 return(mdoc_perr(mdoc, line, ppos, ERR_ARGS_MANY));
394 }
395
396 c = mdoc_elem_alloc(mdoc, line, la, tok, argc, argv);
397
398 if (0 == c) {
399 mdoc_argv_free(argc, argv);
400 return(0);
401 }
402
403 mdoc->next = MDOC_NEXT_CHILD;
404
405 fl = ARGS_DELIM;
406 if (MDOC_QUOTABLE & mdoc_macros[tok].flags)
407 fl |= ARGS_QUOTED;
408
409 for (lastpunct = sz = 0; sz + argc < MDOC_LINEARG_MAX; sz++) {
410 la = *pos;
411
412 if (lastpunct) {
413 c = mdoc_elem_alloc(mdoc, line,
414 la, tok, argc, argv);
415 if (0 == c) {
416 mdoc_argv_free(argc, argv);
417 return(0);
418 }
419 mdoc->next = MDOC_NEXT_CHILD;
420 lastpunct = 0;
421 }
422
423 c = mdoc_args(mdoc, line, pos, buf, fl, &p);
424 if (ARGS_ERROR == c) {
425 mdoc_argv_free(argc, argv);
426 return(0);
427 }
428
429 if (ARGS_EOLN == c)
430 break;
431 if (ARGS_PUNCT == c)
432 break;
433
434 if (-1 == (c = lookup(mdoc, line, la, tok, p)))
435 return(0);
436 else if (MDOC_MAX != c) {
437 if ( ! rewind_elem(mdoc, tok)) {
438 mdoc_argv_free(argc, argv);
439 return(0);
440 }
441 mdoc_argv_free(argc, argv);
442
443 c = mdoc_macro(mdoc, c, line, la, pos, buf);
444 if (0 == c)
445 return(0);
446 if (ppos > 1)
447 return(1);
448 return(append_delims(mdoc, tok, line, pos, buf));
449 }
450
451 if (mdoc_isdelim(p)) {
452 if ( ! rewind_elem(mdoc, tok)) {
453 mdoc_argv_free(argc, argv);
454 return(0);
455 }
456 lastpunct = 1;
457 }
458 if ( ! mdoc_word_alloc(mdoc, line, la, p))
459 return(0);
460 mdoc->next = MDOC_NEXT_SIBLING;
461 }
462
463 mdoc_argv_free(argc, argv);
464
465 if (sz == MDOC_LINEARG_MAX)
466 return(mdoc_perr(mdoc, line, ppos, ERR_ARGS_MANY));
467
468 if ( ! rewind_elem(mdoc, tok))
469 return(0);
470 if (ppos > 1)
471 return(1);
472 return(append_delims(mdoc, tok, line, pos, buf));
473 }
474
475
476 /*
477 * Implicit- or explicit-end multi-line scoped macro.
478 */
479 int
480 macro_scoped(MACRO_PROT_ARGS)
481 {
482 int c, lastarg, argc, j;
483 struct mdoc_arg argv[MDOC_LINEARG_MAX];
484 char *p;
485
486 assert ( ! (MDOC_CALLABLE & mdoc_macros[tok].flags));
487
488 if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags))
489 if ( ! rewind_impblock(mdoc, tok))
490 return(0);
491
492 for (argc = 0; argc < MDOC_LINEARG_MAX; argc++) {
493 lastarg = *pos;
494 c = mdoc_argv(mdoc, line, tok, &argv[argc], pos, buf);
495 if (ARGV_EOLN == c || ARGV_WORD == c)
496 break;
497 else if (ARGV_ARG == c)
498 continue;
499 mdoc_argv_free(argc, argv);
500 return(0);
501 }
502
503 if (MDOC_LINEARG_MAX == argc) {
504 mdoc_argv_free(argc, argv);
505 return(mdoc_perr(mdoc, line, ppos, ERR_ARGS_MANY));
506 }
507
508 c = mdoc_block_alloc(mdoc, line, ppos,
509 tok, (size_t)argc, argv);
510 mdoc_argv_free(argc, argv);
511
512 if (0 == c)
513 return(0);
514
515 mdoc->next = MDOC_NEXT_CHILD;
516
517 if (0 == buf[*pos]) {
518 if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
519 return(0);
520 if ( ! rewind_head(mdoc, tok))
521 return(0);
522 if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
523 return(0);
524 mdoc->next = MDOC_NEXT_CHILD;
525 return(1);
526 }
527
528 if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
529 return(0);
530 mdoc->next = MDOC_NEXT_CHILD;
531
532 for (j = 0; j < MDOC_LINEARG_MAX; j++) {
533 lastarg = *pos;
534 c = mdoc_args(mdoc, line, pos, buf, ARGS_DELIM, &p);
535
536 if (ARGS_ERROR == c)
537 return(0);
538 if (ARGS_PUNCT == c)
539 break;
540 if (ARGS_EOLN == c)
541 break;
542
543 if (-1 == (c = lookup(mdoc, line, lastarg, tok, p)))
544 return(0);
545 else if (MDOC_MAX == c) {
546 if ( ! mdoc_word_alloc(mdoc, line, lastarg, p))
547 return(0);
548 mdoc->next = MDOC_NEXT_SIBLING;
549 continue;
550 }
551
552 if ( ! mdoc_macro(mdoc, c, line, lastarg, pos, buf))
553 return(0);
554 break;
555 }
556
557 if (j == MDOC_LINEARG_MAX)
558 return(mdoc_perr(mdoc, line, ppos, ERR_ARGS_MANY));
559
560 if ( ! rewind_head(mdoc, tok))
561 return(0);
562 if (1 == ppos && ! append_delims(mdoc, tok, line, pos, buf))
563 return(0);
564
565 if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
566 return(0);
567 mdoc->next = MDOC_NEXT_CHILD;
568
569 return(1);
570 }
571
572
573 /*
574 * When scoped to a line, a macro encompasses all of the contents. This
575 * differs from constants or text macros, where a new macro will
576 * terminate the existing context.
577 */
578 int
579 macro_scoped_line(MACRO_PROT_ARGS)
580 {
581 int lastarg, c, j;
582 char *p;
583
584 if ( ! mdoc_block_alloc(mdoc, line, ppos, tok, 0, NULL))
585 return(0);
586 mdoc->next = MDOC_NEXT_CHILD;
587
588 if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
589 return(0);
590 mdoc->next = MDOC_NEXT_CHILD;
591
592 /* XXX - no known argument macros. */
593
594 for (lastarg = ppos, j = 0; j < MDOC_LINEARG_MAX; j++) {
595 lastarg = *pos;
596 c = mdoc_args(mdoc, line, pos, buf, ARGS_DELIM, &p);
597
598 if (ARGS_ERROR == c)
599 return(0);
600 if (ARGS_PUNCT == c)
601 break;
602 if (ARGS_EOLN == c)
603 break;
604
605 if (-1 == (c = lookup(mdoc, line, lastarg, tok, p)))
606 return(0);
607 else if (MDOC_MAX == c) {
608 if ( ! mdoc_word_alloc(mdoc, line, lastarg, p))
609 return(0);
610 mdoc->next = MDOC_NEXT_SIBLING;
611 continue;
612 }
613
614 if ( ! mdoc_macro(mdoc, c, line, lastarg, pos, buf))
615 return(0);
616 break;
617 }
618
619 if (j == MDOC_LINEARG_MAX)
620 return(mdoc_perr(mdoc, line, ppos, ERR_ARGS_MANY));
621
622 if (1 == ppos) {
623 if ( ! rewind_head(mdoc, tok))
624 return(0);
625 if ( ! append_delims(mdoc, tok, line, pos, buf))
626 return(0);
627 }
628 return(rewind_impblock(mdoc, tok));
629 }
630
631
632 /*
633 * Constant-scope macros accept a fixed number of arguments and behave
634 * like constant macros except that they're scoped across lines.
635 */
636 int
637 macro_constant_scoped(MACRO_PROT_ARGS)
638 {
639 int lastarg, flushed, j, c, maxargs;
640 char *p;
641
642 lastarg = ppos;
643 flushed = 0;
644
645 switch (tok) {
646 case (MDOC_Eo):
647 maxargs = 1;
648 break;
649 default:
650 maxargs = 0;
651 break;
652 }
653
654 if ( ! mdoc_block_alloc(mdoc, line, ppos, tok, 0, NULL))
655 return(0);
656 mdoc->next = MDOC_NEXT_CHILD;
657
658 if (0 == maxargs) {
659 if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
660 return(0);
661 if ( ! rewind_head(mdoc, tok))
662 return(0);
663 if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
664 return(0);
665 flushed = 1;
666 } else if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
667 return(0);
668
669 mdoc->next = MDOC_NEXT_CHILD;
670
671 for (j = 0; j < MDOC_LINEARG_MAX; j++) {
672 lastarg = *pos;
673
674 if (j == maxargs && ! flushed) {
675 if ( ! rewind_head(mdoc, tok))
676 return(0);
677 flushed = 1;
678 if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
679 return(0);
680 mdoc->next = MDOC_NEXT_CHILD;
681 }
682
683 c = mdoc_args(mdoc, line, pos, buf, ARGS_DELIM, &p);
684 if (ARGS_ERROR == c)
685 return(0);
686 if (ARGS_PUNCT == c)
687 break;
688 if (ARGS_EOLN == c)
689 break;
690
691 if (-1 == (c = lookup(mdoc, line, lastarg, tok, p)))
692 return(0);
693 else if (MDOC_MAX != c) {
694 if ( ! flushed) {
695 if ( ! rewind_head(mdoc, tok))
696 return(0);
697 flushed = 1;
698 if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
699 return(0);
700 mdoc->next = MDOC_NEXT_CHILD;
701 }
702 if ( ! mdoc_macro(mdoc, c, line, lastarg, pos, buf))
703 return(0);
704 break;
705 }
706
707 if ( ! flushed && mdoc_isdelim(p)) {
708 if ( ! rewind_head(mdoc, tok))
709 return(0);
710 flushed = 1;
711 if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
712 return(0);
713 mdoc->next = MDOC_NEXT_CHILD;
714 }
715
716 if ( ! mdoc_word_alloc(mdoc, line, lastarg, p))
717 return(0);
718 mdoc->next = MDOC_NEXT_SIBLING;
719 }
720
721 if (MDOC_LINEARG_MAX == j)
722 return(mdoc_perr(mdoc, line, ppos, ERR_ARGS_MANY));
723
724 if ( ! flushed) {
725 if ( ! rewind_head(mdoc, tok))
726 return(0);
727 if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
728 return(0);
729 mdoc->next = MDOC_NEXT_CHILD;
730 }
731
732 if (ppos > 1)
733 return(1);
734 return(append_delims(mdoc, tok, line, pos, buf));
735 }
736
737
738 /*
739 * Delimited macros are like text macros except that, should punctuation
740 * be encountered, the macro isn't re-started with remaining tokens
741 * (it's only emitted once). Delimited macros can have a maximum number
742 * of arguments.
743 */
744 int
745 macro_constant_delimited(MACRO_PROT_ARGS)
746 {
747 int lastarg, flushed, j, c, maxargs, argc;
748 struct mdoc_arg argv[MDOC_LINEARG_MAX];
749 char *p;
750
751 lastarg = ppos;
752 flushed = 0;
753
754 switch (tok) {
755 case (MDOC_No):
756 /* FALLTHROUGH */
757 case (MDOC_Ns):
758 /* FALLTHROUGH */
759 case (MDOC_Ux):
760 /* FALLTHROUGH */
761 case (MDOC_St):
762 maxargs = 0;
763 break;
764 default:
765 maxargs = 1;
766 break;
767 }
768
769 for (argc = 0; argc < MDOC_LINEARG_MAX; argc++) {
770 lastarg = *pos;
771 c = mdoc_argv(mdoc, line, tok, &argv[argc], pos, buf);
772 if (ARGV_EOLN == c || ARGV_WORD == c)
773 break;
774 else if (ARGV_ARG == c)
775 continue;
776 mdoc_argv_free(argc, argv);
777 return(0);
778 }
779
780 c = mdoc_elem_alloc(mdoc, line, lastarg, tok, argc, argv);
781 mdoc_argv_free(argc, argv);
782
783 if (0 == c)
784 return(0);
785
786 mdoc->next = MDOC_NEXT_CHILD;
787
788 for (j = 0; j < MDOC_LINEARG_MAX; j++) {
789 lastarg = *pos;
790
791 if (j == maxargs && ! flushed) {
792 if ( ! rewind_elem(mdoc, tok))
793 return(0);
794 flushed = 1;
795 }
796
797 c = mdoc_args(mdoc, line, pos, buf, ARGS_DELIM, &p);
798 if (ARGS_ERROR == c)
799 return(0);
800 if (ARGS_PUNCT == c)
801 break;
802 if (ARGS_EOLN == c)
803 break;
804
805 if (-1 == (c = lookup(mdoc, line, lastarg, tok, p)))
806 return(0);
807 else if (MDOC_MAX != c) {
808 if ( ! flushed && ! rewind_elem(mdoc, tok))
809 return(0);
810 flushed = 1;
811 if ( ! mdoc_macro(mdoc, c, line, lastarg, pos, buf))
812 return(0);
813 break;
814 }
815
816 if ( ! flushed && mdoc_isdelim(p)) {
817 if ( ! rewind_elem(mdoc, tok))
818 return(0);
819 flushed = 1;
820 }
821
822 if ( ! mdoc_word_alloc(mdoc, line, lastarg, p))
823 return(0);
824 mdoc->next = MDOC_NEXT_SIBLING;
825 }
826
827 if (MDOC_LINEARG_MAX == j)
828 return(mdoc_perr(mdoc, line, ppos, ERR_ARGS_MANY));
829
830 if ( ! flushed && rewind_elem(mdoc, tok))
831 return(0);
832
833 if (ppos > 1)
834 return(1);
835 return(append_delims(mdoc, tok, line, pos, buf));
836 }
837
838
839 /*
840 * Constant macros span an entire line: they constitute a macro and all
841 * of its arguments and child data.
842 */
843 int
844 macro_constant(MACRO_PROT_ARGS)
845 {
846 int c, lastarg, argc, sz, fl;
847 struct mdoc_arg argv[MDOC_LINEARG_MAX];
848 char *p;
849
850 /* FIXME: parsing macros! */
851
852 fl = 0;
853 if (MDOC_QUOTABLE & mdoc_macros[tok].flags)
854 fl = ARGS_QUOTED;
855
856 for (argc = 0; argc < MDOC_LINEARG_MAX; argc++) {
857 lastarg = *pos;
858 c = mdoc_argv(mdoc, line, tok, &argv[argc], pos, buf);
859 if (ARGV_EOLN == c)
860 break;
861 else if (ARGV_ARG == c)
862 continue;
863 else if (ARGV_WORD == c)
864 break;
865
866 mdoc_argv_free(argc, argv);
867 return(0);
868 }
869
870 c = mdoc_elem_alloc(mdoc, line, ppos, tok, argc, argv);
871 mdoc_argv_free(argc, argv);
872
873 if (0 == c)
874 return(0);
875
876 mdoc->next = MDOC_NEXT_CHILD;
877
878 if (MDOC_LINEARG_MAX == argc)
879 return(mdoc_perr(mdoc, line, ppos, ERR_ARGS_MANY));
880
881 for (sz = 0; sz + argc < MDOC_LINEARG_MAX; sz++) {
882 lastarg = *pos;
883 c = mdoc_args(mdoc, line, pos, buf, fl, &p);
884 if (ARGS_ERROR == c)
885 return(0);
886 if (ARGS_EOLN == c)
887 break;
888
889 if ( ! mdoc_word_alloc(mdoc, line, lastarg, p))
890 return(0);
891 mdoc->next = MDOC_NEXT_SIBLING;
892 }
893
894 if (MDOC_LINEARG_MAX == sz + argc)
895 return(mdoc_perr(mdoc, line, ppos, ERR_ARGS_MANY));
896
897 return(rewind_elem(mdoc, tok));
898 }
899
900
901 /* ARGSUSED */
902 int
903 macro_obsolete(MACRO_PROT_ARGS)
904 {
905
906 return(mdoc_pwarn(mdoc, line, ppos, WARN_IGN_OBSOLETE));
907 }
908
909
910 int
911 macro_end(struct mdoc *mdoc)
912 {
913
914 assert(mdoc->first);
915 assert(mdoc->last);
916 return(rewind_last(mdoc, mdoc->first));
917 }