1 /* $Id: mdoc_macro.c,v 1.24 2009/09/21 21:11:37 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 */
179 const struct mdoc_macro * const mdoc_macros = __mdoc_macros;
183 swarn(struct mdoc *mdoc, enum mdoc_type type,
184 int line, int pos, const struct mdoc_node *p)
186 const char *n, *t, *tt;
204 n = mdoc_macronames[p->tok];
208 n = mdoc_macronames[p->tok];
212 n = mdoc_macronames[p->tok];
219 if ( ! (MDOC_IGN_SCOPE & mdoc->pflags))
220 return(mdoc_verr(mdoc, line, pos,
221 "%s scope breaks %s scope of %s",
223 return(mdoc_vwarn(mdoc, line, pos,
224 "%s scope breaks %s scope of %s",
230 * This is called at the end of parsing. It must traverse up the tree,
231 * closing out open [implicit] scopes. Obviously, open explicit scopes
235 mdoc_macroend(struct mdoc *m)
239 /* Scan for open explicit scopes. */
241 n = MDOC_VALID & m->last->flags ? m->last->parent : m->last;
243 for ( ; n; n = n->parent) {
244 if (MDOC_BLOCK != n->type)
246 if ( ! (MDOC_EXPLICIT & mdoc_macros[n->tok].flags))
248 return(mdoc_nerr(m, n, EOPEN));
251 /* Rewind to the first. */
253 return(rew_last(m, m->first));
258 * Look up a macro from within a subsequent context.
261 lookup(int from, const char *p)
263 /* FIXME: make -diag lists be un-PARSED. */
265 if ( ! (MDOC_PARSED & mdoc_macros[from].flags))
267 return(lookup_raw(p));
272 * Lookup a macro following the initial line macro.
275 lookup_raw(const char *p)
279 if (MDOC_MAX == (res = mdoc_hash_find(p)))
281 if (MDOC_CALLABLE & mdoc_macros[res].flags)
288 rew_last(struct mdoc *mdoc, const struct mdoc_node *to)
292 mdoc->next = MDOC_NEXT_SIBLING;
295 while (mdoc->last != to) {
296 if ( ! mdoc_valid_post(mdoc))
298 if ( ! mdoc_action_post(mdoc))
300 mdoc->last = mdoc->last->parent;
304 if ( ! mdoc_valid_post(mdoc))
306 return(mdoc_action_post(mdoc));
311 * Return the opening macro of a closing one, e.g., `Ec' has `Eo' as its
359 * Rewind rules. This indicates whether to stop rewinding
360 * (REWIND_HALT) without touching our current scope, stop rewinding and
361 * close our current scope (REWIND_REWIND), or continue (REWIND_NOHALT).
362 * The scope-closing and so on occurs in the various rew_* routines.
365 rew_dohalt(int tok, enum mdoc_type type, const struct mdoc_node *p)
368 if (MDOC_ROOT == p->type)
370 if (MDOC_VALID & p->flags)
371 return(REWIND_NOHALT);
395 assert(MDOC_TAIL != type);
396 if (type == p->type && tok == p->tok)
397 return(REWIND_REWIND);
400 assert(MDOC_TAIL != type);
401 if (type == p->type && tok == p->tok)
402 return(REWIND_REWIND);
403 if (MDOC_BODY == p->type && MDOC_Bl == p->tok)
407 if (type == p->type && tok == p->tok)
408 return(REWIND_REWIND);
413 assert(MDOC_TAIL != type);
414 if (type == p->type && tok == p->tok)
415 return(REWIND_REWIND);
416 if (MDOC_BODY == p->type && MDOC_Sh == p->tok)
450 if (type == p->type && tok == p->tok)
451 return(REWIND_REWIND);
453 /* Multi-line explicit scope close. */
485 if (type == p->type && rew_alt(tok) == p->tok)
486 return(REWIND_REWIND);
493 return(REWIND_NOHALT);
498 * See if we can break an encountered scope (the rew_dohalt has returned
502 rew_dobreak(int tok, const struct mdoc_node *p)
505 assert(MDOC_ROOT != p->type);
506 if (MDOC_ELEM == p->type)
508 if (MDOC_TEXT == p->type)
510 if (MDOC_VALID & p->flags)
515 return(MDOC_It == p->tok);
517 return(MDOC_Nd == p->tok);
519 return(MDOC_Ss == p->tok);
521 if (MDOC_Nd == p->tok)
523 if (MDOC_Ss == p->tok)
525 return(MDOC_Sh == p->tok);
527 if (MDOC_It == p->tok)
531 /* XXX - experimental! */
532 if (MDOC_Op == p->tok)
539 if (MDOC_EXPLICIT & mdoc_macros[tok].flags)
540 return(p->tok == rew_alt(tok));
541 else if (MDOC_BLOCK == p->type)
544 return(tok == p->tok);
549 rew_elem(struct mdoc *mdoc, int tok)
554 if (MDOC_ELEM != n->type)
556 assert(MDOC_ELEM == n->type);
557 assert(tok == n->tok);
559 return(rew_last(mdoc, n));
564 rew_sub(enum mdoc_type t, struct mdoc *m,
565 int tok, int line, int ppos)
571 for (n = m->last; n; n = n->parent) {
572 c = rew_dohalt(tok, t, n);
573 if (REWIND_HALT == c) {
576 if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags))
578 return(mdoc_perr(m, line, ppos, ENOCTX));
580 if (REWIND_REWIND == c)
582 else if (rew_dobreak(tok, n))
584 if ( ! swarn(m, t, line, ppos, n))
589 return(rew_last(m, n));
594 append_delims(struct mdoc *mdoc, int line, int *pos, char *buf)
604 c = mdoc_zargs(mdoc, line, pos, buf, ARGS_NOWARN, &p);
605 assert(ARGS_PHRASE != c);
609 else if (ARGS_EOLN == c)
611 assert(mdoc_isdelim(p));
612 if ( ! mdoc_word_alloc(mdoc, line, lastarg, p))
621 * Close out block partial/full explicit.
624 blk_exp_close(MACRO_PROT_ARGS)
626 int j, c, lastarg, maxargs, flushed;
638 if ( ! (MDOC_CALLABLE & mdoc_macros[tok].flags)) {
640 if ( ! mdoc_pwarn(m, line, ppos, ENOLINE))
643 if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
645 return(rew_sub(MDOC_BLOCK, m, tok, line, ppos));
648 if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
652 if ( ! mdoc_tail_alloc(m, line, ppos, rew_alt(tok)))
655 for (flushed = j = 0; ; j++) {
658 if (j == maxargs && ! flushed) {
659 if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
664 c = mdoc_args(m, line, pos, buf, tok, &p);
673 if (MDOC_MAX != (c = lookup(tok, p))) {
675 if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
679 if ( ! mdoc_macro(m, c, line, lastarg, pos, buf))
684 if ( ! mdoc_word_alloc(m, line, lastarg, p))
688 if ( ! flushed && ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
693 return(append_delims(m, line, pos, buf));
698 in_line(MACRO_PROT_ARGS)
700 int la, lastpunct, c, w, cnt, d, nc;
701 struct mdoc_arg *arg;
705 * Whether we allow ignored elements (those without content,
706 * usually because of reserved words) to squeak by.
727 for (arg = NULL;; ) {
729 c = mdoc_argv(m, line, tok, &arg, pos, buf);
731 if (ARGV_WORD == c) {
744 for (cnt = 0, lastpunct = 1;; ) {
746 w = mdoc_args(m, line, pos, buf, tok, &p);
755 /* Quoted words shouldn't be looked-up. */
757 c = ARGS_QWORD == w ? MDOC_MAX : lookup(tok, p);
760 * In this case, we've located a submacro and must
761 * execute it. Close out scope, if open. If no
762 * elements have been generated, either create one (nc)
763 * or raise a warning.
767 if (0 == lastpunct && ! rew_elem(m, tok))
769 if (nc && 0 == cnt) {
770 if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
772 if ( ! rew_last(m, m->last))
774 } else if ( ! nc && 0 == cnt) {
776 if ( ! mdoc_pwarn(m, line, ppos, EIGNE))
779 c = mdoc_macro(m, c, line, la, pos, buf);
784 return(append_delims(m, line, pos, buf));
788 * Non-quote-enclosed punctuation. Set up our scope, if
789 * a word; rewind the scope, if a delimiter; then append
795 if (ARGS_QWORD != w && d) {
796 if (0 == lastpunct && ! rew_elem(m, tok))
799 } else if (lastpunct) {
800 if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
807 if ( ! mdoc_word_alloc(m, line, la, p))
811 if (0 == lastpunct && ! rew_elem(m, tok))
815 * If no elements have been collected and we're allowed to have
816 * empties (nc), open a scope and close it out. Otherwise,
820 if (nc && 0 == cnt) {
821 if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
823 if ( ! rew_last(m, m->last))
825 } else if ( ! nc && 0 == cnt) {
827 if ( ! mdoc_pwarn(m, line, ppos, EIGNE))
833 return(append_delims(m, line, pos, buf));
838 blk_full(MACRO_PROT_ARGS)
840 int c, lastarg, reopen, dohead;
841 struct mdoc_arg *arg;
845 * Whether to process a block-head section. If this is
846 * non-zero, then a head will be opened for all line arguments.
847 * If not, then the head will always be empty and only a body
848 * will be opened, which will stay open at the eoln.
860 if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)) {
861 if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
863 if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
867 for (arg = NULL;; ) {
869 c = mdoc_argv(m, line, tok, &arg, pos, buf);
871 if (ARGV_WORD == c) {
885 if ( ! mdoc_block_alloc(m, line, ppos, tok, arg))
888 if (0 == buf[*pos]) {
889 if ( ! mdoc_head_alloc(m, line, ppos, tok))
891 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
893 if ( ! mdoc_body_alloc(m, line, ppos, tok))
898 if ( ! mdoc_head_alloc(m, line, ppos, tok))
901 /* Immediately close out head and enter body, if applicable. */
904 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
906 if ( ! mdoc_body_alloc(m, line, ppos, tok))
910 for (reopen = 0;; ) {
912 c = mdoc_args(m, line, pos, buf, tok, &p);
918 if (ARGS_PHRASE == c) {
920 if (reopen && ! mdoc_head_alloc(m, line, ppos, tok))
923 * Phrases are self-contained macro phrases used
924 * in the columnar output of a macro. They need
927 if ( ! phrase(m, line, lastarg, buf))
929 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
936 if (MDOC_MAX == (c = lookup(tok, p))) {
937 if ( ! mdoc_word_alloc(m, line, lastarg, p))
942 if ( ! mdoc_macro(m, c, line, lastarg, pos, buf))
947 if (1 == ppos && ! append_delims(m, line, pos, buf))
950 /* If the body's already open, then just return. */
954 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
956 if ( ! mdoc_body_alloc(m, line, ppos, tok))
964 blk_part_imp(MACRO_PROT_ARGS)
968 struct mdoc_node *blk, *body, *n;
970 /* If applicable, close out prior scopes. */
972 if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL))
974 /* Saved for later close-out. */
976 if ( ! mdoc_head_alloc(m, line, ppos, tok))
978 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
980 if ( ! mdoc_body_alloc(m, line, ppos, tok))
982 /* Saved for later close-out. */
985 /* Body argument processing. */
989 c = mdoc_args(m, line, pos, buf, tok, &p);
990 assert(ARGS_PHRASE != c);
999 if (MDOC_MAX == (c = lookup(tok, p))) {
1000 if ( ! mdoc_word_alloc(m, line, la, p))
1005 if ( ! mdoc_macro(m, c, line, la, pos, buf))
1011 * If we can't rewind to our body, then our scope has already
1012 * been closed by another macro (like `Oc' closing `Op'). This
1013 * is ugly behaviour nodding its head to OpenBSD's overwhelming
1014 * crufty use of `Op' breakage--XXX, deprecate in time.
1016 for (n = m->last; n; n = n->parent)
1019 if (NULL == n && ! mdoc_nwarn(m, body, EIMPBRK))
1021 if (n && ! rew_last(m, body))
1024 /* Standard appending of delimiters. */
1026 if (1 == ppos && ! append_delims(m, line, pos, buf))
1029 /* Rewind scope, if applicable. */
1031 if (n && ! rew_last(m, blk))
1039 blk_part_exp(MACRO_PROT_ARGS)
1041 int la, flushed, j, c, maxargs;
1044 /* Number of head arguments. Only `Eo' has these, */
1055 /* Begin the block scope. */
1057 if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL))
1061 * If no head arguments, open and then close out a head, noting
1062 * that we've flushed our terms. `flushed' means that we've
1063 * flushed out the head and the body is open.
1067 if ( ! mdoc_head_alloc(m, line, ppos, tok))
1069 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1071 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1075 if ( ! mdoc_head_alloc(m, line, ppos, tok))
1080 /* Process the head/head+body line arguments. */
1082 for (j = 0; ; j++) {
1084 if (j == maxargs && ! flushed) {
1085 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1088 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1092 c = mdoc_args(m, line, pos, buf, tok, &p);
1093 assert(ARGS_PHRASE != c);
1095 if (ARGS_ERROR == c)
1097 if (ARGS_PUNCT == c)
1102 if (MDOC_MAX != (c = lookup(tok, p))) {
1104 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1107 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1110 if ( ! mdoc_macro(m, c, line, la, pos, buf))
1115 if ( ! flushed && mdoc_isdelim(p)) {
1116 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1119 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1123 if ( ! mdoc_word_alloc(m, line, la, p))
1127 /* Close the head and open the body, if applicable. */
1130 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1132 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1136 /* Standard appending of delimiters. */
1140 return(append_delims(m, line, pos, buf));
1145 in_line_argn(MACRO_PROT_ARGS)
1147 int la, flushed, j, c, maxargs;
1148 struct mdoc_arg *arg;
1151 /* Fixed maximum arguments per macro, if applicable. */
1168 /* Macro argument processing. */
1170 for (arg = NULL;; ) {
1172 c = mdoc_argv(m, line, tok, &arg, pos, buf);
1174 if (ARGV_WORD == c) {
1184 mdoc_argv_free(arg);
1188 /* Open the element scope. */
1190 if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1193 /* Process element arguments. */
1195 for (flushed = j = 0; ; j++) {
1198 if (j == maxargs && ! flushed) {
1199 if ( ! rew_elem(m, tok))
1204 c = mdoc_args(m, line, pos, buf, tok, &p);
1206 if (ARGS_ERROR == c)
1208 if (ARGS_PUNCT == c)
1213 if (MDOC_MAX != (c = lookup(tok, p))) {
1214 if ( ! flushed && ! rew_elem(m, tok))
1217 if ( ! mdoc_macro(m, c, line, la, pos, buf))
1222 if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1223 ! flushed && mdoc_isdelim(p)) {
1224 if ( ! rew_elem(m, tok))
1229 if ( ! mdoc_word_alloc(m, line, la, p))
1233 /* Close out and append delimiters. */
1235 if ( ! flushed && ! rew_elem(m, tok))
1240 return(append_delims(m, line, pos, buf));
1245 in_line_eoln(MACRO_PROT_ARGS)
1248 struct mdoc_arg *arg;
1251 assert( ! (MDOC_PARSED & mdoc_macros[tok].flags));
1253 /* Parse macro arguments. */
1255 for (arg = NULL; ; ) {
1257 c = mdoc_argv(m, line, tok, &arg, pos, buf);
1259 if (ARGV_WORD == c) {
1268 mdoc_argv_free(arg);
1272 /* Open element scope. */
1274 if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1277 /* Parse argument terms. */
1281 w = mdoc_args(m, line, pos, buf, tok, &p);
1283 if (ARGS_ERROR == w)
1288 c = ARGS_QWORD == w ? MDOC_MAX : lookup(tok, p);
1290 if (MDOC_MAX != c) {
1291 if ( ! rew_elem(m, tok))
1293 return(mdoc_macro(m, c, line, la, pos, buf));
1296 if ( ! mdoc_word_alloc(m, line, la, p))
1300 /* Close out (no delimiters). */
1302 return(rew_elem(m, tok));
1308 obsolete(MACRO_PROT_ARGS)
1311 return(mdoc_pwarn(m, line, ppos, EOBS));
1316 * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
1317 * They're unusual because they're basically free-form text until a
1318 * macro is encountered.
1321 phrase(struct mdoc *m, int line, int ppos, char *buf)
1326 for (pos = ppos; ; ) {
1329 /* Note: no calling context! */
1330 w = mdoc_zargs(m, line, &pos, buf, 0, &p);
1332 if (ARGS_ERROR == w)
1337 c = ARGS_QWORD == w ? MDOC_MAX : lookup_raw(p);
1339 if (MDOC_MAX != c) {
1340 if ( ! mdoc_macro(m, c, line, la, &pos, buf))
1342 return(append_delims(m, line, &pos, buf));
1345 if ( ! mdoc_word_alloc(m, line, la, p))