1 /* $Id: mdoc_macro.c,v 1.38 2009/10/26 17:05:44 kristaps 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.
26 #define REWIND_REWIND (1 << 0)
27 #define REWIND_NOHALT (1 << 1)
28 #define REWIND_HALT (1 << 2)
30 static int obsolete(MACRO_PROT_ARGS);
31 static int blk_part_exp(MACRO_PROT_ARGS);
32 static int in_line_eoln(MACRO_PROT_ARGS);
33 static int in_line_argn(MACRO_PROT_ARGS);
34 static int in_line(MACRO_PROT_ARGS);
35 static int blk_full(MACRO_PROT_ARGS);
36 static int blk_exp_close(MACRO_PROT_ARGS);
37 static int blk_part_imp(MACRO_PROT_ARGS);
39 static int phrase(struct mdoc *, int, int, char *);
40 static int rew_dohalt(int, enum mdoc_type,
41 const struct mdoc_node *);
42 static int rew_alt(int);
43 static int rew_dobreak(int, const struct mdoc_node *);
44 static int rew_elem(struct mdoc *, int);
45 static int rew_sub(enum mdoc_type, struct mdoc *,
47 static int rew_last(struct mdoc *,
48 const struct mdoc_node *);
49 static int append_delims(struct mdoc *, int, int *, char *);
50 static int lookup(int, const char *);
51 static int lookup_raw(const char *);
52 static int swarn(struct mdoc *, enum mdoc_type, int, int,
53 const struct mdoc_node *);
55 /* Central table of library: who gets parsed how. */
57 const struct mdoc_macro __mdoc_macros[MDOC_MAX] = {
58 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ap */
59 { in_line_eoln, MDOC_PROLOGUE }, /* Dd */
60 { in_line_eoln, MDOC_PROLOGUE }, /* Dt */
61 { in_line_eoln, MDOC_PROLOGUE }, /* Os */
62 { blk_full, 0 }, /* Sh */
63 { blk_full, 0 }, /* Ss */
64 { in_line_eoln, 0 }, /* Pp */
65 { blk_part_imp, MDOC_PARSED }, /* D1 */
66 { blk_part_imp, MDOC_PARSED }, /* Dl */
67 { blk_full, MDOC_EXPLICIT }, /* Bd */
68 { blk_exp_close, MDOC_EXPLICIT }, /* Ed */
69 { blk_full, MDOC_EXPLICIT }, /* Bl */
70 { blk_exp_close, MDOC_EXPLICIT }, /* El */
71 { blk_full, MDOC_PARSED }, /* It */
72 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ad */
73 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* An */
74 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ar */
75 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Cd */
76 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Cm */
77 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Dv */
78 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Er */
79 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ev */
80 { in_line_eoln, 0 }, /* Ex */
81 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fa */
82 { in_line_eoln, 0 }, /* Fd */
83 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fl */
84 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fn */
85 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ft */
86 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ic */
87 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* In */
88 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Li */
89 { blk_full, 0 }, /* Nd */
90 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Nm */
91 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Op */
92 { obsolete, 0 }, /* Ot */
93 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Pa */
94 { in_line_eoln, 0 }, /* Rv */
95 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* St */
96 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Va */
97 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Vt */
98 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Xr */
99 { in_line_eoln, 0 }, /* %A */
100 { in_line_eoln, 0 }, /* %B */
101 { in_line_eoln, 0 }, /* %D */
102 { in_line_eoln, 0 }, /* %I */
103 { in_line_eoln, 0 }, /* %J */
104 { in_line_eoln, 0 }, /* %N */
105 { in_line_eoln, 0 }, /* %O */
106 { in_line_eoln, 0 }, /* %P */
107 { in_line_eoln, 0 }, /* %R */
108 { in_line_eoln, 0 }, /* %T */
109 { in_line_eoln, 0 }, /* %V */
110 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Ac */
111 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Ao */
112 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Aq */
113 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* At */
114 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Bc */
115 { blk_full, MDOC_EXPLICIT }, /* Bf */
116 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Bo */
117 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Bq */
118 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bsx */
119 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bx */
120 { in_line_eoln, 0 }, /* Db */
121 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Dc */
122 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Do */
123 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Dq */
124 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Ec */
125 { blk_exp_close, MDOC_EXPLICIT }, /* Ef */
126 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Em */
127 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Eo */
128 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Fx */
129 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ms */
130 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* No */
131 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ns */
132 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Nx */
133 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ox */
134 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Pc */
135 { in_line_argn, MDOC_PARSED | MDOC_IGNDELIM }, /* Pf */
136 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Po */
137 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Pq */
138 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Qc */
139 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Ql */
140 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Qo */
141 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Qq */
142 { blk_exp_close, MDOC_EXPLICIT }, /* Re */
143 { blk_full, MDOC_EXPLICIT }, /* Rs */
144 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Sc */
145 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* So */
146 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Sq */
147 { in_line_eoln, 0 }, /* Sm */
148 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Sx */
149 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Sy */
150 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Tn */
151 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ux */
152 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Xc */
153 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Xo */
154 { blk_full, MDOC_EXPLICIT | MDOC_CALLABLE }, /* Fo */
155 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Fc */
156 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Oo */
157 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Oc */
158 { blk_full, MDOC_EXPLICIT }, /* Bk */
159 { blk_exp_close, MDOC_EXPLICIT }, /* Ek */
160 { in_line_eoln, 0 }, /* Bt */
161 { in_line_eoln, 0 }, /* Hf */
162 { obsolete, 0 }, /* Fr */
163 { in_line_eoln, 0 }, /* Ud */
164 { in_line_eoln, 0 }, /* Lb */
165 { in_line_eoln, 0 }, /* Lp */
166 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Lk */
167 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Mt */
168 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Brq */
169 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Bro */
170 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Brc */
171 { in_line_eoln, 0 }, /* %C */
172 { obsolete, 0 }, /* Es */
173 { obsolete, 0 }, /* En */
174 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Dx */
175 { in_line_eoln, 0 }, /* %Q */
176 { in_line_eoln, 0 }, /* br */
177 { in_line_eoln, 0 }, /* sp */
178 { in_line_eoln, 0 }, /* %U */
181 const struct mdoc_macro * const mdoc_macros = __mdoc_macros;
185 swarn(struct mdoc *mdoc, enum mdoc_type type,
186 int line, int pos, const struct mdoc_node *p)
188 const char *n, *t, *tt;
206 n = mdoc_macronames[p->tok];
210 n = mdoc_macronames[p->tok];
214 n = mdoc_macronames[p->tok];
221 if ( ! (MDOC_IGN_SCOPE & mdoc->pflags))
222 return(mdoc_verr(mdoc, line, pos,
223 "%s scope breaks %s scope of %s",
225 return(mdoc_vwarn(mdoc, line, pos,
226 "%s scope breaks %s scope of %s",
232 * This is called at the end of parsing. It must traverse up the tree,
233 * closing out open [implicit] scopes. Obviously, open explicit scopes
237 mdoc_macroend(struct mdoc *m)
241 /* Scan for open explicit scopes. */
243 n = MDOC_VALID & m->last->flags ? m->last->parent : m->last;
245 for ( ; n; n = n->parent) {
246 if (MDOC_BLOCK != n->type)
248 if ( ! (MDOC_EXPLICIT & mdoc_macros[n->tok].flags))
250 return(mdoc_nerr(m, n, EOPEN));
253 /* Rewind to the first. */
255 return(rew_last(m, m->first));
260 * Look up a macro from within a subsequent context.
263 lookup(int from, const char *p)
265 /* FIXME: make -diag lists be un-PARSED. */
267 if ( ! (MDOC_PARSED & mdoc_macros[from].flags))
269 return(lookup_raw(p));
274 * Lookup a macro following the initial line macro.
277 lookup_raw(const char *p)
281 if (MDOC_MAX == (res = mdoc_hash_find(p)))
283 if (MDOC_CALLABLE & mdoc_macros[res].flags)
290 rew_last(struct mdoc *mdoc, const struct mdoc_node *to)
294 mdoc->next = MDOC_NEXT_SIBLING;
297 while (mdoc->last != to) {
298 if ( ! mdoc_valid_post(mdoc))
300 if ( ! mdoc_action_post(mdoc))
302 mdoc->last = mdoc->last->parent;
306 if ( ! mdoc_valid_post(mdoc))
308 return(mdoc_action_post(mdoc));
313 * Return the opening macro of a closing one, e.g., `Ec' has `Eo' as its
361 * Rewind rules. This indicates whether to stop rewinding
362 * (REWIND_HALT) without touching our current scope, stop rewinding and
363 * close our current scope (REWIND_REWIND), or continue (REWIND_NOHALT).
364 * The scope-closing and so on occurs in the various rew_* routines.
367 rew_dohalt(int tok, enum mdoc_type type, const struct mdoc_node *p)
370 if (MDOC_ROOT == p->type)
372 if (MDOC_VALID & p->flags)
373 return(REWIND_NOHALT);
397 assert(MDOC_TAIL != type);
398 if (type == p->type && tok == p->tok)
399 return(REWIND_REWIND);
402 assert(MDOC_TAIL != type);
403 if (type == p->type && tok == p->tok)
404 return(REWIND_REWIND);
405 if (MDOC_BODY == p->type && MDOC_Bl == p->tok)
409 if (type == p->type && tok == p->tok)
410 return(REWIND_REWIND);
415 assert(MDOC_TAIL != type);
416 if (type == p->type && tok == p->tok)
417 return(REWIND_REWIND);
418 if (MDOC_BODY == p->type && MDOC_Sh == p->tok)
452 if (type == p->type && tok == p->tok)
453 return(REWIND_REWIND);
455 /* Multi-line explicit scope close. */
487 if (type == p->type && rew_alt(tok) == p->tok)
488 return(REWIND_REWIND);
495 return(REWIND_NOHALT);
500 * See if we can break an encountered scope (the rew_dohalt has returned
504 rew_dobreak(int tok, const struct mdoc_node *p)
507 assert(MDOC_ROOT != p->type);
508 if (MDOC_ELEM == p->type)
510 if (MDOC_TEXT == p->type)
512 if (MDOC_VALID & p->flags)
517 return(MDOC_It == p->tok);
519 return(MDOC_Nd == p->tok);
521 return(MDOC_Ss == p->tok);
523 if (MDOC_Nd == p->tok)
525 if (MDOC_Ss == p->tok)
527 return(MDOC_Sh == p->tok);
529 if (MDOC_It == p->tok)
533 /* XXX - experimental! */
534 if (MDOC_Op == p->tok)
541 if (MDOC_EXPLICIT & mdoc_macros[tok].flags)
542 return(p->tok == rew_alt(tok));
543 else if (MDOC_BLOCK == p->type)
546 return(tok == p->tok);
551 rew_elem(struct mdoc *mdoc, int tok)
556 if (MDOC_ELEM != n->type)
558 assert(MDOC_ELEM == n->type);
559 assert(tok == n->tok);
561 return(rew_last(mdoc, n));
566 rew_sub(enum mdoc_type t, struct mdoc *m,
567 int tok, int line, int ppos)
573 for (n = m->last; n; n = n->parent) {
574 c = rew_dohalt(tok, t, n);
575 if (REWIND_HALT == c) {
578 if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags))
580 return(mdoc_perr(m, line, ppos, ENOCTX));
582 if (REWIND_REWIND == c)
584 else if (rew_dobreak(tok, n))
586 if ( ! swarn(m, t, line, ppos, n))
591 return(rew_last(m, n));
596 append_delims(struct mdoc *mdoc, int line, int *pos, char *buf)
606 c = mdoc_zargs(mdoc, line, pos, buf, ARGS_NOWARN, &p);
607 assert(ARGS_PHRASE != c);
611 else if (ARGS_EOLN == c)
613 assert(mdoc_isdelim(p));
614 if ( ! mdoc_word_alloc(mdoc, line, lastarg, p))
623 * Close out block partial/full explicit.
626 blk_exp_close(MACRO_PROT_ARGS)
628 int j, c, lastarg, maxargs, flushed;
640 if ( ! (MDOC_CALLABLE & mdoc_macros[tok].flags)) {
642 if ( ! mdoc_pwarn(m, line, ppos, ENOLINE))
645 if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
647 return(rew_sub(MDOC_BLOCK, m, tok, line, ppos));
650 if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
654 if ( ! mdoc_tail_alloc(m, line, ppos, rew_alt(tok)))
657 for (flushed = j = 0; ; j++) {
660 if (j == maxargs && ! flushed) {
661 if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
666 c = mdoc_args(m, line, pos, buf, tok, &p);
675 if (MDOC_MAX != (c = lookup(tok, p))) {
677 if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
681 if ( ! mdoc_macro(m, c, line, lastarg, pos, buf))
686 if ( ! mdoc_word_alloc(m, line, lastarg, p))
690 if ( ! flushed && ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
695 return(append_delims(m, line, pos, buf));
700 in_line(MACRO_PROT_ARGS)
702 int la, lastpunct, c, w, cnt, d, nc;
703 struct mdoc_arg *arg;
707 * Whether we allow ignored elements (those without content,
708 * usually because of reserved words) to squeak by.
729 for (arg = NULL;; ) {
731 c = mdoc_argv(m, line, tok, &arg, pos, buf);
733 if (ARGV_WORD == c) {
746 for (cnt = 0, lastpunct = 1;; ) {
748 w = mdoc_args(m, line, pos, buf, tok, &p);
757 /* Quoted words shouldn't be looked-up. */
759 c = ARGS_QWORD == w ? MDOC_MAX : lookup(tok, p);
762 * In this case, we've located a submacro and must
763 * execute it. Close out scope, if open. If no
764 * elements have been generated, either create one (nc)
765 * or raise a warning.
769 if (0 == lastpunct && ! rew_elem(m, tok))
771 if (nc && 0 == cnt) {
772 if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
774 if ( ! rew_last(m, m->last))
776 } else if ( ! nc && 0 == cnt) {
778 if ( ! mdoc_pwarn(m, line, ppos, EIGNE))
781 c = mdoc_macro(m, c, line, la, pos, buf);
786 return(append_delims(m, line, pos, buf));
790 * Non-quote-enclosed punctuation. Set up our scope, if
791 * a word; rewind the scope, if a delimiter; then append
797 if (ARGS_QWORD != w && d) {
798 if (0 == lastpunct && ! rew_elem(m, tok))
801 } else if (lastpunct) {
802 if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
809 if ( ! mdoc_word_alloc(m, line, la, p))
813 if (0 == lastpunct && ! rew_elem(m, tok))
817 * If no elements have been collected and we're allowed to have
818 * empties (nc), open a scope and close it out. Otherwise,
822 if (nc && 0 == cnt) {
823 if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
825 if ( ! rew_last(m, m->last))
827 } else if ( ! nc && 0 == cnt) {
829 if ( ! mdoc_pwarn(m, line, ppos, EIGNE))
835 return(append_delims(m, line, pos, buf));
840 blk_full(MACRO_PROT_ARGS)
842 int c, lastarg, reopen, dohead;
843 struct mdoc_arg *arg;
847 * Whether to process a block-head section. If this is
848 * non-zero, then a head will be opened for all line arguments.
849 * If not, then the head will always be empty and only a body
850 * will be opened, which will stay open at the eoln.
862 if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)) {
863 if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
865 if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
869 for (arg = NULL;; ) {
871 c = mdoc_argv(m, line, tok, &arg, pos, buf);
873 if (ARGV_WORD == c) {
887 if ( ! mdoc_block_alloc(m, line, ppos, tok, arg))
890 if (0 == buf[*pos]) {
891 if ( ! mdoc_head_alloc(m, line, ppos, tok))
893 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
895 if ( ! mdoc_body_alloc(m, line, ppos, tok))
900 if ( ! mdoc_head_alloc(m, line, ppos, tok))
903 /* Immediately close out head and enter body, if applicable. */
906 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
908 if ( ! mdoc_body_alloc(m, line, ppos, tok))
912 for (reopen = 0;; ) {
914 c = mdoc_args(m, line, pos, buf, tok, &p);
920 if (ARGS_PHRASE == c) {
922 if (reopen && ! mdoc_head_alloc(m, line, ppos, tok))
925 * Phrases are self-contained macro phrases used
926 * in the columnar output of a macro. They need
929 if ( ! phrase(m, line, lastarg, buf))
931 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
938 if (MDOC_MAX == (c = lookup(tok, p))) {
939 if ( ! mdoc_word_alloc(m, line, lastarg, p))
944 if ( ! mdoc_macro(m, c, line, lastarg, pos, buf))
949 if (1 == ppos && ! append_delims(m, line, pos, buf))
952 /* If the body's already open, then just return. */
956 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
958 if ( ! mdoc_body_alloc(m, line, ppos, tok))
966 blk_part_imp(MACRO_PROT_ARGS)
970 struct mdoc_node *blk, *body, *n;
972 /* If applicable, close out prior scopes. */
974 if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL))
976 /* Saved for later close-out. */
978 if ( ! mdoc_head_alloc(m, line, ppos, tok))
980 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
982 if ( ! mdoc_body_alloc(m, line, ppos, tok))
984 /* Saved for later close-out. */
987 /* Body argument processing. */
991 c = mdoc_args(m, line, pos, buf, tok, &p);
992 assert(ARGS_PHRASE != c);
1001 if (MDOC_MAX == (c = lookup(tok, p))) {
1002 if ( ! mdoc_word_alloc(m, line, la, p))
1007 if ( ! mdoc_macro(m, c, line, la, pos, buf))
1013 * If we can't rewind to our body, then our scope has already
1014 * been closed by another macro (like `Oc' closing `Op'). This
1015 * is ugly behaviour nodding its head to OpenBSD's overwhelming
1016 * crufty use of `Op' breakage--XXX, deprecate in time.
1018 for (n = m->last; n; n = n->parent)
1021 if (NULL == n && ! mdoc_nwarn(m, body, EIMPBRK))
1023 if (n && ! rew_last(m, body))
1026 /* Standard appending of delimiters. */
1028 if (1 == ppos && ! append_delims(m, line, pos, buf))
1031 /* Rewind scope, if applicable. */
1033 if (n && ! rew_last(m, blk))
1041 blk_part_exp(MACRO_PROT_ARGS)
1043 int la, flushed, j, c, maxargs;
1046 /* Number of head arguments. Only `Eo' has these, */
1057 /* Begin the block scope. */
1059 if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL))
1063 * If no head arguments, open and then close out a head, noting
1064 * that we've flushed our terms. `flushed' means that we've
1065 * flushed out the head and the body is open.
1069 if ( ! mdoc_head_alloc(m, line, ppos, tok))
1071 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1073 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1077 if ( ! mdoc_head_alloc(m, line, ppos, tok))
1082 /* Process the head/head+body line arguments. */
1084 for (j = 0; ; j++) {
1086 if (j == maxargs && ! flushed) {
1087 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1090 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1094 c = mdoc_args(m, line, pos, buf, tok, &p);
1095 assert(ARGS_PHRASE != c);
1097 if (ARGS_ERROR == c)
1099 if (ARGS_PUNCT == c)
1104 if (MDOC_MAX != (c = lookup(tok, p))) {
1106 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1109 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1112 if ( ! mdoc_macro(m, c, line, la, pos, buf))
1117 if ( ! flushed && mdoc_isdelim(p)) {
1118 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1121 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1125 if ( ! mdoc_word_alloc(m, line, la, p))
1129 /* Close the head and open the body, if applicable. */
1132 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1134 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1138 /* Standard appending of delimiters. */
1142 return(append_delims(m, line, pos, buf));
1147 in_line_argn(MACRO_PROT_ARGS)
1149 int la, flushed, j, c, maxargs;
1150 struct mdoc_arg *arg;
1153 /* Fixed maximum arguments per macro, if applicable. */
1170 /* Macro argument processing. */
1172 for (arg = NULL;; ) {
1174 c = mdoc_argv(m, line, tok, &arg, pos, buf);
1176 if (ARGV_WORD == c) {
1186 mdoc_argv_free(arg);
1190 /* Open the element scope. */
1192 if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1195 /* Process element arguments. */
1197 for (flushed = j = 0; ; j++) {
1200 if (j == maxargs && ! flushed) {
1201 if ( ! rew_elem(m, tok))
1206 c = mdoc_args(m, line, pos, buf, tok, &p);
1208 if (ARGS_ERROR == c)
1210 if (ARGS_PUNCT == c)
1215 if (MDOC_MAX != (c = lookup(tok, p))) {
1216 if ( ! flushed && ! rew_elem(m, tok))
1219 if ( ! mdoc_macro(m, c, line, la, pos, buf))
1224 if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1225 ! flushed && mdoc_isdelim(p)) {
1226 if ( ! rew_elem(m, tok))
1231 if ( ! mdoc_word_alloc(m, line, la, p))
1235 /* Close out and append delimiters. */
1237 if ( ! flushed && ! rew_elem(m, tok))
1242 return(append_delims(m, line, pos, buf));
1247 in_line_eoln(MACRO_PROT_ARGS)
1250 struct mdoc_arg *arg;
1253 assert( ! (MDOC_PARSED & mdoc_macros[tok].flags));
1255 /* Parse macro arguments. */
1257 for (arg = NULL; ; ) {
1259 c = mdoc_argv(m, line, tok, &arg, pos, buf);
1261 if (ARGV_WORD == c) {
1270 mdoc_argv_free(arg);
1274 /* Open element scope. */
1276 if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1279 /* Parse argument terms. */
1283 w = mdoc_args(m, line, pos, buf, tok, &p);
1285 if (ARGS_ERROR == w)
1290 c = ARGS_QWORD == w ? MDOC_MAX : lookup(tok, p);
1292 if (MDOC_MAX != c) {
1293 if ( ! rew_elem(m, tok))
1295 return(mdoc_macro(m, c, line, la, pos, buf));
1298 if ( ! mdoc_word_alloc(m, line, la, p))
1302 /* Close out (no delimiters). */
1304 return(rew_elem(m, tok));
1310 obsolete(MACRO_PROT_ARGS)
1313 return(mdoc_pwarn(m, line, ppos, EOBS));
1318 * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
1319 * They're unusual because they're basically free-form text until a
1320 * macro is encountered.
1323 phrase(struct mdoc *m, int line, int ppos, char *buf)
1328 for (pos = ppos; ; ) {
1331 /* Note: no calling context! */
1332 w = mdoc_zargs(m, line, &pos, buf, 0, &p);
1334 if (ARGS_ERROR == w)
1339 c = ARGS_QWORD == w ? MDOC_MAX : lookup_raw(p);
1341 if (MDOC_MAX != c) {
1342 if ( ! mdoc_macro(m, c, line, la, &pos, buf))
1344 return(append_delims(m, line, &pos, buf));
1347 if ( ! mdoc_word_alloc(m, line, la, p))