]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_macro.c
1 /* $Id: mdoc_macro.c,v 1.33 2009/08/20 08:59:12 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.
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(struct mdoc
*, int, const char *);
50 static int lookup_raw(struct mdoc
*, 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(struct mdoc
*mdoc
, int from
, const char *p
)
264 if ( ! (MDOC_PARSED
& mdoc_macros
[from
].flags
))
266 return(lookup_raw(mdoc
, p
));
271 * Lookup a macro following the initial line macro.
274 lookup_raw(struct mdoc
*mdoc
, const char *p
)
278 if (MDOC_MAX
== (res
= mdoc_hash_find(mdoc
->htab
, p
)))
280 if (MDOC_CALLABLE
& mdoc_macros
[res
].flags
)
287 rew_last(struct mdoc
*mdoc
, const struct mdoc_node
*to
)
291 mdoc
->next
= MDOC_NEXT_SIBLING
;
294 while (mdoc
->last
!= to
) {
295 if ( ! mdoc_valid_post(mdoc
))
297 if ( ! mdoc_action_post(mdoc
))
299 mdoc
->last
= mdoc
->last
->parent
;
303 if ( ! mdoc_valid_post(mdoc
))
305 return(mdoc_action_post(mdoc
));
310 * Return the opening macro of a closing one, e.g., `Ec' has `Eo' as its
358 * Rewind rules. This indicates whether to stop rewinding
359 * (REWIND_HALT) without touching our current scope, stop rewinding and
360 * close our current scope (REWIND_REWIND), or continue (REWIND_NOHALT).
361 * The scope-closing and so on occurs in the various rew_* routines.
364 rew_dohalt(int tok
, enum mdoc_type type
, const struct mdoc_node
*p
)
367 if (MDOC_ROOT
== p
->type
)
369 if (MDOC_VALID
& p
->flags
)
370 return(REWIND_NOHALT
);
394 assert(MDOC_TAIL
!= type
);
395 if (type
== p
->type
&& tok
== p
->tok
)
396 return(REWIND_REWIND
);
399 assert(MDOC_TAIL
!= type
);
400 if (type
== p
->type
&& tok
== p
->tok
)
401 return(REWIND_REWIND
);
402 if (MDOC_BODY
== p
->type
&& MDOC_Bl
== p
->tok
)
406 if (type
== p
->type
&& tok
== p
->tok
)
407 return(REWIND_REWIND
);
412 assert(MDOC_TAIL
!= type
);
413 if (type
== p
->type
&& tok
== p
->tok
)
414 return(REWIND_REWIND
);
415 if (MDOC_BODY
== p
->type
&& MDOC_Sh
== p
->tok
)
449 if (type
== p
->type
&& tok
== p
->tok
)
450 return(REWIND_REWIND
);
452 /* Multi-line explicit scope close. */
484 if (type
== p
->type
&& rew_alt(tok
) == p
->tok
)
485 return(REWIND_REWIND
);
492 return(REWIND_NOHALT
);
497 * See if we can break an encountered scope (the rew_dohalt has returned
501 rew_dobreak(int tok
, const struct mdoc_node
*p
)
504 assert(MDOC_ROOT
!= p
->type
);
505 if (MDOC_ELEM
== p
->type
)
507 if (MDOC_TEXT
== p
->type
)
509 if (MDOC_VALID
& p
->flags
)
514 return(MDOC_It
== p
->tok
);
516 return(MDOC_Nd
== p
->tok
);
518 return(MDOC_Ss
== p
->tok
);
520 if (MDOC_Nd
== p
->tok
)
522 if (MDOC_Ss
== p
->tok
)
524 return(MDOC_Sh
== p
->tok
);
526 if (MDOC_It
== p
->tok
)
530 /* XXX - experimental! */
531 if (MDOC_Op
== p
->tok
)
538 if (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
)
539 return(p
->tok
== rew_alt(tok
));
540 else if (MDOC_BLOCK
== p
->type
)
543 return(tok
== p
->tok
);
548 rew_elem(struct mdoc
*mdoc
, int tok
)
553 if (MDOC_ELEM
!= n
->type
)
555 assert(MDOC_ELEM
== n
->type
);
556 assert(tok
== n
->tok
);
558 return(rew_last(mdoc
, n
));
563 rew_sub(enum mdoc_type t
, struct mdoc
*m
,
564 int tok
, int line
, int ppos
)
570 for (n
= m
->last
; n
; n
= n
->parent
) {
571 c
= rew_dohalt(tok
, t
, n
);
572 if (REWIND_HALT
== c
) {
575 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
))
577 return(mdoc_perr(m
, line
, ppos
, ENOCTX
));
579 if (REWIND_REWIND
== c
)
581 else if (rew_dobreak(tok
, n
))
583 if ( ! swarn(m
, t
, line
, ppos
, n
))
588 return(rew_last(m
, n
));
593 append_delims(struct mdoc
*mdoc
, int line
, int *pos
, char *buf
)
603 c
= mdoc_zargs(mdoc
, line
, pos
, buf
, ARGS_NOWARN
, &p
);
604 assert(ARGS_PHRASE
!= c
);
608 else if (ARGS_EOLN
== c
)
610 assert(mdoc_isdelim(p
));
611 if ( ! mdoc_word_alloc(mdoc
, line
, lastarg
, p
))
620 * Close out block partial/full explicit.
623 blk_exp_close(MACRO_PROT_ARGS
)
625 int j
, c
, lastarg
, maxargs
, flushed
;
637 if ( ! (MDOC_CALLABLE
& mdoc_macros
[tok
].flags
)) {
639 if ( ! mdoc_pwarn(m
, line
, ppos
, ENOLINE
))
642 if ( ! rew_sub(MDOC_BODY
, m
, tok
, line
, ppos
))
644 return(rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
));
647 if ( ! rew_sub(MDOC_BODY
, m
, tok
, line
, ppos
))
651 if ( ! mdoc_tail_alloc(m
, line
, ppos
, rew_alt(tok
)))
654 for (flushed
= j
= 0; ; j
++) {
657 if (j
== maxargs
&& ! flushed
) {
658 if ( ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
663 c
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
672 if (MDOC_MAX
!= (c
= lookup(m
, tok
, p
))) {
674 if ( ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
678 if ( ! mdoc_macro(m
, c
, line
, lastarg
, pos
, buf
))
683 if ( ! mdoc_word_alloc(m
, line
, lastarg
, p
))
687 if ( ! flushed
&& ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
692 return(append_delims(m
, line
, pos
, buf
));
697 in_line(MACRO_PROT_ARGS
)
699 int la
, lastpunct
, c
, w
, cnt
, d
, nc
;
700 struct mdoc_arg
*arg
;
704 * Whether we allow ignored elements (those without content,
705 * usually because of reserved words) to squeak by.
726 for (arg
= NULL
;; ) {
728 c
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
730 if (ARGV_WORD
== c
) {
743 for (cnt
= 0, lastpunct
= 1;; ) {
745 w
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
754 /* Quoted words shouldn't be looked-up. */
756 c
= ARGS_QWORD
== w
? MDOC_MAX
: lookup(m
, tok
, p
);
759 * In this case, we've located a submacro and must
760 * execute it. Close out scope, if open. If no
761 * elements have been generated, either create one (nc)
762 * or raise a warning.
766 if (0 == lastpunct
&& ! rew_elem(m
, tok
))
768 if (nc
&& 0 == cnt
) {
769 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
771 if ( ! rew_last(m
, m
->last
))
773 } else if ( ! nc
&& 0 == cnt
) {
775 if ( ! mdoc_pwarn(m
, line
, ppos
, EIGNE
))
778 c
= mdoc_macro(m
, c
, line
, la
, pos
, buf
);
783 return(append_delims(m
, line
, pos
, buf
));
787 * Non-quote-enclosed punctuation. Set up our scope, if
788 * a word; rewind the scope, if a delimiter; then append
794 if (ARGS_QWORD
!= w
&& d
) {
795 if (0 == lastpunct
&& ! rew_elem(m
, tok
))
798 } else if (lastpunct
) {
799 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
806 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
810 if (0 == lastpunct
&& ! rew_elem(m
, tok
))
814 * If no elements have been collected and we're allowed to have
815 * empties (nc), open a scope and close it out. Otherwise,
819 if (nc
&& 0 == cnt
) {
820 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
822 if ( ! rew_last(m
, m
->last
))
824 } else if ( ! nc
&& 0 == cnt
) {
826 if ( ! mdoc_pwarn(m
, line
, ppos
, EIGNE
))
832 return(append_delims(m
, line
, pos
, buf
));
837 blk_full(MACRO_PROT_ARGS
)
839 int c
, lastarg
, reopen
, dohead
;
840 struct mdoc_arg
*arg
;
844 * Whether to process a block-head section. If this is
845 * non-zero, then a head will be opened for all line arguments.
846 * If not, then the head will always be empty and only a body
847 * will be opened, which will stay open at the eoln.
859 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
)) {
860 if ( ! rew_sub(MDOC_BODY
, m
, tok
, line
, ppos
))
862 if ( ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
866 for (arg
= NULL
;; ) {
868 c
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
870 if (ARGV_WORD
== c
) {
884 if ( ! mdoc_block_alloc(m
, line
, ppos
, tok
, arg
))
887 if (0 == buf
[*pos
]) {
888 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
890 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
892 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
897 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
900 /* Immediately close out head and enter body, if applicable. */
903 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
905 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
909 for (reopen
= 0;; ) {
911 c
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
917 if (ARGS_PHRASE
== c
) {
919 if (reopen
&& ! mdoc_head_alloc(m
, line
, ppos
, tok
))
922 * Phrases are self-contained macro phrases used
923 * in the columnar output of a macro. They need
926 if ( ! phrase(m
, line
, lastarg
, buf
))
928 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
935 if (MDOC_MAX
== (c
= lookup(m
, tok
, p
))) {
936 if ( ! mdoc_word_alloc(m
, line
, lastarg
, p
))
941 if ( ! mdoc_macro(m
, c
, line
, lastarg
, pos
, buf
))
946 if (1 == ppos
&& ! append_delims(m
, line
, pos
, buf
))
949 /* If the body's already open, then just return. */
953 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
955 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
963 blk_part_imp(MACRO_PROT_ARGS
)
967 struct mdoc_node
*blk
, *body
, *n
;
969 /* If applicable, close out prior scopes. */
971 if ( ! mdoc_block_alloc(m
, line
, ppos
, tok
, NULL
))
973 /* Saved for later close-out. */
975 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
977 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
979 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
981 /* Saved for later close-out. */
984 /* Body argument processing. */
988 c
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
989 assert(ARGS_PHRASE
!= c
);
998 if (MDOC_MAX
== (c
= lookup(m
, tok
, p
))) {
999 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1004 if ( ! mdoc_macro(m
, c
, line
, la
, pos
, buf
))
1010 * If we can't rewind to our body, then our scope has already
1011 * been closed by another macro (like `Oc' closing `Op'). This
1012 * is ugly behaviour nodding its head to OpenBSD's overwhelming
1013 * crufty use of `Op' breakage--XXX, deprecate in time.
1015 for (n
= m
->last
; n
; n
= n
->parent
)
1018 if (NULL
== n
&& ! mdoc_nwarn(m
, body
, EIMPBRK
))
1020 if (n
&& ! rew_last(m
, body
))
1023 /* Standard appending of delimiters. */
1025 if (1 == ppos
&& ! append_delims(m
, line
, pos
, buf
))
1028 /* Rewind scope, if applicable. */
1030 if (n
&& ! rew_last(m
, blk
))
1038 blk_part_exp(MACRO_PROT_ARGS
)
1040 int la
, flushed
, j
, c
, maxargs
;
1043 /* Number of head arguments. Only `Eo' has these, */
1054 /* Begin the block scope. */
1056 if ( ! mdoc_block_alloc(m
, line
, ppos
, tok
, NULL
))
1060 * If no head arguments, open and then close out a head, noting
1061 * that we've flushed our terms. `flushed' means that we've
1062 * flushed out the head and the body is open.
1066 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
1068 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1070 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1074 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
1079 /* Process the head/head+body line arguments. */
1081 for (j
= 0; ; j
++) {
1083 if (j
== maxargs
&& ! flushed
) {
1084 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1087 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1091 c
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1092 assert(ARGS_PHRASE
!= c
);
1094 if (ARGS_ERROR
== c
)
1096 if (ARGS_PUNCT
== c
)
1101 if (MDOC_MAX
!= (c
= lookup(m
, tok
, p
))) {
1103 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1106 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1109 if ( ! mdoc_macro(m
, c
, line
, la
, pos
, buf
))
1114 if ( ! flushed
&& mdoc_isdelim(p
)) {
1115 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1118 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1122 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1126 /* Close the head and open the body, if applicable. */
1129 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1131 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1135 /* Standard appending of delimiters. */
1139 return(append_delims(m
, line
, pos
, buf
));
1144 in_line_argn(MACRO_PROT_ARGS
)
1146 int la
, flushed
, j
, c
, maxargs
;
1147 struct mdoc_arg
*arg
;
1150 /* Fixed maximum arguments per macro, if applicable. */
1167 /* Macro argument processing. */
1169 for (arg
= NULL
;; ) {
1171 c
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
1173 if (ARGV_WORD
== c
) {
1183 mdoc_argv_free(arg
);
1187 /* Open the element scope. */
1189 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
1192 /* Process element arguments. */
1194 for (flushed
= j
= 0; ; j
++) {
1197 if (j
== maxargs
&& ! flushed
) {
1198 if ( ! rew_elem(m
, tok
))
1203 c
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1205 if (ARGS_ERROR
== c
)
1207 if (ARGS_PUNCT
== c
)
1212 if (MDOC_MAX
!= (c
= lookup(m
, tok
, p
))) {
1213 if ( ! flushed
&& ! rew_elem(m
, tok
))
1216 if ( ! mdoc_macro(m
, c
, line
, la
, pos
, buf
))
1221 if ( ! (MDOC_IGNDELIM
& mdoc_macros
[tok
].flags
) &&
1222 ! flushed
&& mdoc_isdelim(p
)) {
1223 if ( ! rew_elem(m
, tok
))
1228 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1232 /* Close out and append delimiters. */
1234 if ( ! flushed
&& ! rew_elem(m
, tok
))
1239 return(append_delims(m
, line
, pos
, buf
));
1244 in_line_eoln(MACRO_PROT_ARGS
)
1247 struct mdoc_arg
*arg
;
1250 assert( ! (MDOC_PARSED
& mdoc_macros
[tok
].flags
));
1252 /* Parse macro arguments. */
1254 for (arg
= NULL
; ; ) {
1256 c
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
1258 if (ARGV_WORD
== c
) {
1267 mdoc_argv_free(arg
);
1271 /* Open element scope. */
1273 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
1276 /* Parse argument terms. */
1280 w
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1282 if (ARGS_ERROR
== w
)
1287 c
= ARGS_QWORD
== w
? MDOC_MAX
: lookup(m
, tok
, p
);
1289 if (MDOC_MAX
!= c
) {
1290 if ( ! rew_elem(m
, tok
))
1292 return(mdoc_macro(m
, c
, line
, la
, pos
, buf
));
1295 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1299 /* Close out (no delimiters). */
1301 return(rew_elem(m
, tok
));
1307 obsolete(MACRO_PROT_ARGS
)
1310 return(mdoc_pwarn(m
, line
, ppos
, EOBS
));
1315 * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
1316 * They're unusual because they're basically free-form text until a
1317 * macro is encountered.
1320 phrase(struct mdoc
*m
, int line
, int ppos
, char *buf
)
1325 for (pos
= ppos
; ; ) {
1328 /* Note: no calling context! */
1329 w
= mdoc_zargs(m
, line
, &pos
, buf
, 0, &p
);
1331 if (ARGS_ERROR
== w
)
1336 c
= ARGS_QWORD
== w
? MDOC_MAX
: lookup_raw(m
, p
);
1338 if (MDOC_MAX
!= c
) {
1339 if ( ! mdoc_macro(m
, c
, line
, la
, &pos
, buf
))
1341 return(append_delims(m
, line
, &pos
, buf
));
1344 if ( ! mdoc_word_alloc(m
, line
, la
, p
))