1 /* $Id: mdoc_macro.c,v 1.25 2009/10/27 21:40:07 schwarze Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
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.
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.
25 #define REWIND_REWIND (1 << 0)
26 #define REWIND_NOHALT (1 << 1)
27 #define REWIND_HALT (1 << 2)
29 static int obsolete(MACRO_PROT_ARGS);
30 static int blk_part_exp(MACRO_PROT_ARGS);
31 static int in_line_eoln(MACRO_PROT_ARGS);
32 static int in_line_argn(MACRO_PROT_ARGS);
33 static int in_line(MACRO_PROT_ARGS);
34 static int blk_full(MACRO_PROT_ARGS);
35 static int blk_exp_close(MACRO_PROT_ARGS);
36 static int blk_part_imp(MACRO_PROT_ARGS);
38 static int phrase(struct mdoc *, int, int, char *);
39 static int rew_dohalt(int, enum mdoc_type,
40 const struct mdoc_node *);
41 static int rew_alt(int);
42 static int rew_dobreak(int, const struct mdoc_node *);
43 static int rew_elem(struct mdoc *, int);
44 static int rew_sub(enum mdoc_type, struct mdoc *,
46 static int rew_last(struct mdoc *,
47 const struct mdoc_node *);
48 static int append_delims(struct mdoc *, int, int *, char *);
49 static int lookup(int, const char *);
50 static int lookup_raw(const char *);
51 static int swarn(struct mdoc *, enum mdoc_type, int, int,
52 const struct mdoc_node *);
54 /* Central table of library: who gets parsed how. */
56 const struct mdoc_macro __mdoc_macros[MDOC_MAX] = {
57 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ap */
58 { in_line_eoln, MDOC_PROLOGUE }, /* Dd */
59 { in_line_eoln, MDOC_PROLOGUE }, /* Dt */
60 { in_line_eoln, MDOC_PROLOGUE }, /* Os */
61 { blk_full, 0 }, /* Sh */
62 { blk_full, 0 }, /* Ss */
63 { in_line_eoln, 0 }, /* Pp */
64 { blk_part_imp, MDOC_PARSED }, /* D1 */
65 { blk_part_imp, MDOC_PARSED }, /* Dl */
66 { blk_full, MDOC_EXPLICIT }, /* Bd */
67 { blk_exp_close, MDOC_EXPLICIT }, /* Ed */
68 { blk_full, MDOC_EXPLICIT }, /* Bl */
69 { blk_exp_close, MDOC_EXPLICIT }, /* El */
70 { blk_full, MDOC_PARSED }, /* It */
71 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ad */
72 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* An */
73 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ar */
74 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Cd */
75 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Cm */
76 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Dv */
77 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Er */
78 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ev */
79 { in_line_eoln, 0 }, /* Ex */
80 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fa */
81 { in_line_eoln, 0 }, /* Fd */
82 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fl */
83 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fn */
84 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ft */
85 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ic */
86 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* In */
87 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Li */
88 { blk_full, 0 }, /* Nd */
89 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Nm */
90 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Op */
91 { obsolete, 0 }, /* Ot */
92 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Pa */
93 { in_line_eoln, 0 }, /* Rv */
94 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* St */
95 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Va */
96 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Vt */
97 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Xr */
98 { in_line_eoln, 0 }, /* %A */
99 { in_line_eoln, 0 }, /* %B */
100 { in_line_eoln, 0 }, /* %D */
101 { in_line_eoln, 0 }, /* %I */
102 { in_line_eoln, 0 }, /* %J */
103 { in_line_eoln, 0 }, /* %N */
104 { in_line_eoln, 0 }, /* %O */
105 { in_line_eoln, 0 }, /* %P */
106 { in_line_eoln, 0 }, /* %R */
107 { in_line_eoln, 0 }, /* %T */
108 { in_line_eoln, 0 }, /* %V */
109 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Ac */
110 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Ao */
111 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Aq */
112 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* At */
113 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Bc */
114 { blk_full, MDOC_EXPLICIT }, /* Bf */
115 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Bo */
116 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Bq */
117 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bsx */
118 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bx */
119 { in_line_eoln, 0 }, /* Db */
120 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Dc */
121 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Do */
122 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Dq */
123 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Ec */
124 { blk_exp_close, MDOC_EXPLICIT }, /* Ef */
125 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Em */
126 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Eo */
127 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Fx */
128 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ms */
129 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* No */
130 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ns */
131 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Nx */
132 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ox */
133 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Pc */
134 { in_line_argn, MDOC_PARSED | MDOC_IGNDELIM }, /* Pf */
135 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Po */
136 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Pq */
137 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Qc */
138 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Ql */
139 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Qo */
140 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Qq */
141 { blk_exp_close, MDOC_EXPLICIT }, /* Re */
142 { blk_full, MDOC_EXPLICIT }, /* Rs */
143 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Sc */
144 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* So */
145 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Sq */
146 { in_line_eoln, 0 }, /* Sm */
147 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Sx */
148 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Sy */
149 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Tn */
150 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ux */
151 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Xc */
152 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Xo */
153 { blk_full, MDOC_EXPLICIT | MDOC_CALLABLE }, /* Fo */
154 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Fc */
155 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Oo */
156 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Oc */
157 { blk_full, MDOC_EXPLICIT }, /* Bk */
158 { blk_exp_close, MDOC_EXPLICIT }, /* Ek */
159 { in_line_eoln, 0 }, /* Bt */
160 { in_line_eoln, 0 }, /* Hf */
161 { obsolete, 0 }, /* Fr */
162 { in_line_eoln, 0 }, /* Ud */
163 { in_line_eoln, 0 }, /* Lb */
164 { in_line_eoln, 0 }, /* Lp */
165 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Lk */
166 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Mt */
167 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Brq */
168 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Bro */
169 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Brc */
170 { in_line_eoln, 0 }, /* %C */
171 { obsolete, 0 }, /* Es */
172 { obsolete, 0 }, /* En */
173 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Dx */
174 { in_line_eoln, 0 }, /* %Q */
175 { in_line_eoln, 0 }, /* br */
176 { in_line_eoln, 0 }, /* sp */
177 { in_line_eoln, 0 }, /* %U */
180 const struct mdoc_macro * const mdoc_macros = __mdoc_macros;
184 swarn(struct mdoc *mdoc, enum mdoc_type type,
185 int line, int pos, const struct mdoc_node *p)
187 const char *n, *t, *tt;
205 n = mdoc_macronames[p->tok];
209 n = mdoc_macronames[p->tok];
213 n = mdoc_macronames[p->tok];
220 if ( ! (MDOC_IGN_SCOPE & mdoc->pflags))
221 return(mdoc_verr(mdoc, line, pos,
222 "%s scope breaks %s scope of %s",
224 return(mdoc_vwarn(mdoc, line, pos,
225 "%s scope breaks %s scope of %s",
231 * This is called at the end of parsing. It must traverse up the tree,
232 * closing out open [implicit] scopes. Obviously, open explicit scopes
236 mdoc_macroend(struct mdoc *m)
240 /* Scan for open explicit scopes. */
242 n = MDOC_VALID & m->last->flags ? m->last->parent : m->last;
244 for ( ; n; n = n->parent) {
245 if (MDOC_BLOCK != n->type)
247 if ( ! (MDOC_EXPLICIT & mdoc_macros[n->tok].flags))
249 return(mdoc_nerr(m, n, EOPEN));
252 /* Rewind to the first. */
254 return(rew_last(m, m->first));
259 * Look up a macro from within a subsequent context.
262 lookup(int from, const char *p)
264 /* FIXME: make -diag lists be un-PARSED. */
266 if ( ! (MDOC_PARSED & mdoc_macros[from].flags))
268 return(lookup_raw(p));
273 * Lookup a macro following the initial line macro.
276 lookup_raw(const char *p)
280 if (MDOC_MAX == (res = mdoc_hash_find(p)))
282 if (MDOC_CALLABLE & mdoc_macros[res].flags)
289 rew_last(struct mdoc *mdoc, const struct mdoc_node *to)
293 mdoc->next = MDOC_NEXT_SIBLING;
296 while (mdoc->last != to) {
297 if ( ! mdoc_valid_post(mdoc))
299 if ( ! mdoc_action_post(mdoc))
301 mdoc->last = mdoc->last->parent;
305 if ( ! mdoc_valid_post(mdoc))
307 return(mdoc_action_post(mdoc));
312 * Return the opening macro of a closing one, e.g., `Ec' has `Eo' as its
360 * Rewind rules. This indicates whether to stop rewinding
361 * (REWIND_HALT) without touching our current scope, stop rewinding and
362 * close our current scope (REWIND_REWIND), or continue (REWIND_NOHALT).
363 * The scope-closing and so on occurs in the various rew_* routines.
366 rew_dohalt(int tok, enum mdoc_type type, const struct mdoc_node *p)
369 if (MDOC_ROOT == p->type)
371 if (MDOC_VALID & p->flags)
372 return(REWIND_NOHALT);
396 assert(MDOC_TAIL != type);
397 if (type == p->type && tok == p->tok)
398 return(REWIND_REWIND);
401 assert(MDOC_TAIL != type);
402 if (type == p->type && tok == p->tok)
403 return(REWIND_REWIND);
404 if (MDOC_BODY == p->type && MDOC_Bl == p->tok)
408 if (type == p->type && tok == p->tok)
409 return(REWIND_REWIND);
414 assert(MDOC_TAIL != type);
415 if (type == p->type && tok == p->tok)
416 return(REWIND_REWIND);
417 if (MDOC_BODY == p->type && MDOC_Sh == p->tok)
451 if (type == p->type && tok == p->tok)
452 return(REWIND_REWIND);
454 /* Multi-line explicit scope close. */
486 if (type == p->type && rew_alt(tok) == p->tok)
487 return(REWIND_REWIND);
494 return(REWIND_NOHALT);
499 * See if we can break an encountered scope (the rew_dohalt has returned
503 rew_dobreak(int tok, const struct mdoc_node *p)
506 assert(MDOC_ROOT != p->type);
507 if (MDOC_ELEM == p->type)
509 if (MDOC_TEXT == p->type)
511 if (MDOC_VALID & p->flags)
516 return(MDOC_It == p->tok);
518 return(MDOC_Nd == p->tok);
520 return(MDOC_Ss == p->tok);
522 if (MDOC_Nd == p->tok)
524 if (MDOC_Ss == p->tok)
526 return(MDOC_Sh == p->tok);
528 if (MDOC_It == p->tok)
532 /* XXX - experimental! */
533 if (MDOC_Op == p->tok)
540 if (MDOC_EXPLICIT & mdoc_macros[tok].flags)
541 return(p->tok == rew_alt(tok));
542 else if (MDOC_BLOCK == p->type)
545 return(tok == p->tok);
550 rew_elem(struct mdoc *mdoc, int tok)
555 if (MDOC_ELEM != n->type)
557 assert(MDOC_ELEM == n->type);
558 assert(tok == n->tok);
560 return(rew_last(mdoc, n));
565 rew_sub(enum mdoc_type t, struct mdoc *m,
566 int tok, int line, int ppos)
572 for (n = m->last; n; n = n->parent) {
573 c = rew_dohalt(tok, t, n);
574 if (REWIND_HALT == c) {
577 if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags))
579 return(mdoc_perr(m, line, ppos, ENOCTX));
581 if (REWIND_REWIND == c)
583 else if (rew_dobreak(tok, n))
585 if ( ! swarn(m, t, line, ppos, n))
590 return(rew_last(m, n));
595 append_delims(struct mdoc *mdoc, int line, int *pos, char *buf)
605 c = mdoc_zargs(mdoc, line, pos, buf, ARGS_NOWARN, &p);
606 assert(ARGS_PHRASE != c);
610 else if (ARGS_EOLN == c)
612 assert(mdoc_isdelim(p));
613 if ( ! mdoc_word_alloc(mdoc, line, lastarg, p))
622 * Close out block partial/full explicit.
625 blk_exp_close(MACRO_PROT_ARGS)
627 int j, c, lastarg, maxargs, flushed;
639 if ( ! (MDOC_CALLABLE & mdoc_macros[tok].flags)) {
641 if ( ! mdoc_pwarn(m, line, ppos, ENOLINE))
644 if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
646 return(rew_sub(MDOC_BLOCK, m, tok, line, ppos));
649 if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
653 if ( ! mdoc_tail_alloc(m, line, ppos, rew_alt(tok)))
656 for (flushed = j = 0; ; j++) {
659 if (j == maxargs && ! flushed) {
660 if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
665 c = mdoc_args(m, line, pos, buf, tok, &p);
674 if (MDOC_MAX != (c = lookup(tok, p))) {
676 if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
680 if ( ! mdoc_macro(m, c, line, lastarg, pos, buf))
685 if ( ! mdoc_word_alloc(m, line, lastarg, p))
689 if ( ! flushed && ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
694 return(append_delims(m, line, pos, buf));
699 in_line(MACRO_PROT_ARGS)
701 int la, lastpunct, c, w, cnt, d, nc;
702 struct mdoc_arg *arg;
706 * Whether we allow ignored elements (those without content,
707 * usually because of reserved words) to squeak by.
728 for (arg = NULL;; ) {
730 c = mdoc_argv(m, line, tok, &arg, pos, buf);
732 if (ARGV_WORD == c) {
745 for (cnt = 0, lastpunct = 1;; ) {
747 w = mdoc_args(m, line, pos, buf, tok, &p);
756 /* Quoted words shouldn't be looked-up. */
758 c = ARGS_QWORD == w ? MDOC_MAX : lookup(tok, p);
761 * In this case, we've located a submacro and must
762 * execute it. Close out scope, if open. If no
763 * elements have been generated, either create one (nc)
764 * or raise a warning.
768 if (0 == lastpunct && ! rew_elem(m, tok))
770 if (nc && 0 == cnt) {
771 if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
773 if ( ! rew_last(m, m->last))
775 } else if ( ! nc && 0 == cnt) {
777 if ( ! mdoc_pwarn(m, line, ppos, EIGNE))
780 c = mdoc_macro(m, c, line, la, pos, buf);
785 return(append_delims(m, line, pos, buf));
789 * Non-quote-enclosed punctuation. Set up our scope, if
790 * a word; rewind the scope, if a delimiter; then append
796 if (ARGS_QWORD != w && d) {
797 if (0 == lastpunct && ! rew_elem(m, tok))
800 } else if (lastpunct) {
801 if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
808 if ( ! mdoc_word_alloc(m, line, la, p))
812 if (0 == lastpunct && ! rew_elem(m, tok))
816 * If no elements have been collected and we're allowed to have
817 * empties (nc), open a scope and close it out. Otherwise,
821 if (nc && 0 == cnt) {
822 if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
824 if ( ! rew_last(m, m->last))
826 } else if ( ! nc && 0 == cnt) {
828 if ( ! mdoc_pwarn(m, line, ppos, EIGNE))
834 return(append_delims(m, line, pos, buf));
839 blk_full(MACRO_PROT_ARGS)
841 int c, lastarg, reopen, dohead;
842 struct mdoc_arg *arg;
846 * Whether to process a block-head section. If this is
847 * non-zero, then a head will be opened for all line arguments.
848 * If not, then the head will always be empty and only a body
849 * will be opened, which will stay open at the eoln.
861 if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)) {
862 if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
864 if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
868 for (arg = NULL;; ) {
870 c = mdoc_argv(m, line, tok, &arg, pos, buf);
872 if (ARGV_WORD == c) {
886 if ( ! mdoc_block_alloc(m, line, ppos, tok, arg))
889 if (0 == buf[*pos]) {
890 if ( ! mdoc_head_alloc(m, line, ppos, tok))
892 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
894 if ( ! mdoc_body_alloc(m, line, ppos, tok))
899 if ( ! mdoc_head_alloc(m, line, ppos, tok))
902 /* Immediately close out head and enter body, if applicable. */
905 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
907 if ( ! mdoc_body_alloc(m, line, ppos, tok))
911 for (reopen = 0;; ) {
913 c = mdoc_args(m, line, pos, buf, tok, &p);
919 if (ARGS_PHRASE == c) {
921 if (reopen && ! mdoc_head_alloc(m, line, ppos, tok))
924 * Phrases are self-contained macro phrases used
925 * in the columnar output of a macro. They need
928 if ( ! phrase(m, line, lastarg, buf))
930 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
937 if (MDOC_MAX == (c = lookup(tok, p))) {
938 if ( ! mdoc_word_alloc(m, line, lastarg, p))
943 if ( ! mdoc_macro(m, c, line, lastarg, pos, buf))
948 if (1 == ppos && ! append_delims(m, line, pos, buf))
951 /* If the body's already open, then just return. */
955 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
957 if ( ! mdoc_body_alloc(m, line, ppos, tok))
965 blk_part_imp(MACRO_PROT_ARGS)
969 struct mdoc_node *blk, *body, *n;
971 /* If applicable, close out prior scopes. */
973 if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL))
975 /* Saved for later close-out. */
977 if ( ! mdoc_head_alloc(m, line, ppos, tok))
979 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
981 if ( ! mdoc_body_alloc(m, line, ppos, tok))
983 /* Saved for later close-out. */
986 /* Body argument processing. */
990 c = mdoc_args(m, line, pos, buf, tok, &p);
991 assert(ARGS_PHRASE != c);
1000 if (MDOC_MAX == (c = lookup(tok, p))) {
1001 if ( ! mdoc_word_alloc(m, line, la, p))
1006 if ( ! mdoc_macro(m, c, line, la, pos, buf))
1012 * If we can't rewind to our body, then our scope has already
1013 * been closed by another macro (like `Oc' closing `Op'). This
1014 * is ugly behaviour nodding its head to OpenBSD's overwhelming
1015 * crufty use of `Op' breakage--XXX, deprecate in time.
1017 for (n = m->last; n; n = n->parent)
1020 if (NULL == n && ! mdoc_nwarn(m, body, EIMPBRK))
1022 if (n && ! rew_last(m, body))
1025 /* Standard appending of delimiters. */
1027 if (1 == ppos && ! append_delims(m, line, pos, buf))
1030 /* Rewind scope, if applicable. */
1032 if (n && ! rew_last(m, blk))
1040 blk_part_exp(MACRO_PROT_ARGS)
1042 int la, flushed, j, c, maxargs;
1045 /* Number of head arguments. Only `Eo' has these, */
1056 /* Begin the block scope. */
1058 if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL))
1062 * If no head arguments, open and then close out a head, noting
1063 * that we've flushed our terms. `flushed' means that we've
1064 * flushed out the head and the body is open.
1068 if ( ! mdoc_head_alloc(m, line, ppos, tok))
1070 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1072 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1076 if ( ! mdoc_head_alloc(m, line, ppos, tok))
1081 /* Process the head/head+body line arguments. */
1083 for (j = 0; ; j++) {
1085 if (j == maxargs && ! flushed) {
1086 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1089 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1093 c = mdoc_args(m, line, pos, buf, tok, &p);
1094 assert(ARGS_PHRASE != c);
1096 if (ARGS_ERROR == c)
1098 if (ARGS_PUNCT == c)
1103 if (MDOC_MAX != (c = lookup(tok, p))) {
1105 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1108 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1111 if ( ! mdoc_macro(m, c, line, la, pos, buf))
1116 if ( ! flushed && mdoc_isdelim(p)) {
1117 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1120 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1124 if ( ! mdoc_word_alloc(m, line, la, p))
1128 /* Close the head and open the body, if applicable. */
1131 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1133 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1137 /* Standard appending of delimiters. */
1141 return(append_delims(m, line, pos, buf));
1146 in_line_argn(MACRO_PROT_ARGS)
1148 int la, flushed, j, c, maxargs;
1149 struct mdoc_arg *arg;
1152 /* Fixed maximum arguments per macro, if applicable. */
1169 /* Macro argument processing. */
1171 for (arg = NULL;; ) {
1173 c = mdoc_argv(m, line, tok, &arg, pos, buf);
1175 if (ARGV_WORD == c) {
1185 mdoc_argv_free(arg);
1189 /* Open the element scope. */
1191 if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1194 /* Process element arguments. */
1196 for (flushed = j = 0; ; j++) {
1199 if (j == maxargs && ! flushed) {
1200 if ( ! rew_elem(m, tok))
1205 c = mdoc_args(m, line, pos, buf, tok, &p);
1207 if (ARGS_ERROR == c)
1209 if (ARGS_PUNCT == c)
1214 if (MDOC_MAX != (c = lookup(tok, p))) {
1215 if ( ! flushed && ! rew_elem(m, tok))
1218 if ( ! mdoc_macro(m, c, line, la, pos, buf))
1223 if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1224 ! flushed && mdoc_isdelim(p)) {
1225 if ( ! rew_elem(m, tok))
1230 if ( ! mdoc_word_alloc(m, line, la, p))
1234 /* Close out and append delimiters. */
1236 if ( ! flushed && ! rew_elem(m, tok))
1241 return(append_delims(m, line, pos, buf));
1246 in_line_eoln(MACRO_PROT_ARGS)
1249 struct mdoc_arg *arg;
1252 assert( ! (MDOC_PARSED & mdoc_macros[tok].flags));
1254 /* Parse macro arguments. */
1256 for (arg = NULL; ; ) {
1258 c = mdoc_argv(m, line, tok, &arg, pos, buf);
1260 if (ARGV_WORD == c) {
1269 mdoc_argv_free(arg);
1273 /* Open element scope. */
1275 if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1278 /* Parse argument terms. */
1282 w = mdoc_args(m, line, pos, buf, tok, &p);
1284 if (ARGS_ERROR == w)
1289 c = ARGS_QWORD == w ? MDOC_MAX : lookup(tok, p);
1291 if (MDOC_MAX != c) {
1292 if ( ! rew_elem(m, tok))
1294 return(mdoc_macro(m, c, line, la, pos, buf));
1297 if ( ! mdoc_word_alloc(m, line, la, p))
1301 /* Close out (no delimiters). */
1303 return(rew_elem(m, tok));
1309 obsolete(MACRO_PROT_ARGS)
1312 return(mdoc_pwarn(m, line, ppos, EOBS));
1317 * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
1318 * They're unusual because they're basically free-form text until a
1319 * macro is encountered.
1322 phrase(struct mdoc *m, int line, int ppos, char *buf)
1327 for (pos = ppos; ; ) {
1330 /* Note: no calling context! */
1331 w = mdoc_zargs(m, line, &pos, buf, 0, &p);
1333 if (ARGS_ERROR == w)
1338 c = ARGS_QWORD == w ? MDOC_MAX : lookup_raw(p);
1340 if (MDOC_MAX != c) {
1341 if ( ! mdoc_macro(m, c, line, la, &pos, buf))
1343 return(append_delims(m, line, &pos, buf));
1346 if ( ! mdoc_word_alloc(m, line, la, p))