]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_macro.c
1 /* $Id: mdoc_macro.c,v 1.40 2010/01/01 17:14:29 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.
30 #define REWIND_REWIND (1 << 0)
31 #define REWIND_NOHALT (1 << 1)
32 #define REWIND_HALT (1 << 2)
34 static int obsolete(MACRO_PROT_ARGS
);
35 static int blk_part_exp(MACRO_PROT_ARGS
);
36 static int in_line_eoln(MACRO_PROT_ARGS
);
37 static int in_line_argn(MACRO_PROT_ARGS
);
38 static int in_line(MACRO_PROT_ARGS
);
39 static int blk_full(MACRO_PROT_ARGS
);
40 static int blk_exp_close(MACRO_PROT_ARGS
);
41 static int blk_part_imp(MACRO_PROT_ARGS
);
43 static int phrase(struct mdoc
*, int, int, char *);
44 static int rew_dohalt(int, enum mdoc_type
,
45 const struct mdoc_node
*);
46 static int rew_alt(int);
47 static int rew_dobreak(int, const struct mdoc_node
*);
48 static int rew_elem(struct mdoc
*, int);
49 static int rew_sub(enum mdoc_type
, struct mdoc
*,
51 static int rew_last(struct mdoc
*,
52 const struct mdoc_node
*);
53 static int append_delims(struct mdoc
*, int, int *, char *);
54 static int lookup(int, const char *);
55 static int lookup_raw(const char *);
56 static int swarn(struct mdoc
*, enum mdoc_type
, int, int,
57 const struct mdoc_node
*);
59 /* Central table of library: who gets parsed how. */
61 const struct mdoc_macro __mdoc_macros
[MDOC_MAX
] = {
62 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ap */
63 { in_line_eoln
, MDOC_PROLOGUE
}, /* Dd */
64 { in_line_eoln
, MDOC_PROLOGUE
}, /* Dt */
65 { in_line_eoln
, MDOC_PROLOGUE
}, /* Os */
66 { blk_full
, 0 }, /* Sh */
67 { blk_full
, 0 }, /* Ss */
68 { in_line_eoln
, 0 }, /* Pp */
69 { blk_part_imp
, MDOC_PARSED
}, /* D1 */
70 { blk_part_imp
, MDOC_PARSED
}, /* Dl */
71 { blk_full
, MDOC_EXPLICIT
}, /* Bd */
72 { blk_exp_close
, MDOC_EXPLICIT
}, /* Ed */
73 { blk_full
, MDOC_EXPLICIT
}, /* Bl */
74 { blk_exp_close
, MDOC_EXPLICIT
}, /* El */
75 { blk_full
, MDOC_PARSED
}, /* It */
76 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ad */
77 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* An */
78 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ar */
79 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Cd */
80 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Cm */
81 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Dv */
82 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Er */
83 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ev */
84 { in_line_eoln
, 0 }, /* Ex */
85 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fa */
86 { in_line_eoln
, 0 }, /* Fd */
87 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fl */
88 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fn */
89 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ft */
90 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ic */
91 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* In */
92 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Li */
93 { blk_full
, 0 }, /* Nd */
94 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Nm */
95 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Op */
96 { obsolete
, 0 }, /* Ot */
97 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Pa */
98 { in_line_eoln
, 0 }, /* Rv */
99 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* St */
100 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Va */
101 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Vt */
102 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Xr */
103 { in_line_eoln
, 0 }, /* %A */
104 { in_line_eoln
, 0 }, /* %B */
105 { in_line_eoln
, 0 }, /* %D */
106 { in_line_eoln
, 0 }, /* %I */
107 { in_line_eoln
, 0 }, /* %J */
108 { in_line_eoln
, 0 }, /* %N */
109 { in_line_eoln
, 0 }, /* %O */
110 { in_line_eoln
, 0 }, /* %P */
111 { in_line_eoln
, 0 }, /* %R */
112 { in_line_eoln
, 0 }, /* %T */
113 { in_line_eoln
, 0 }, /* %V */
114 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Ac */
115 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Ao */
116 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Aq */
117 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* At */
118 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Bc */
119 { blk_full
, MDOC_EXPLICIT
}, /* Bf */
120 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Bo */
121 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Bq */
122 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Bsx */
123 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Bx */
124 { in_line_eoln
, 0 }, /* Db */
125 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Dc */
126 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Do */
127 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Dq */
128 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Ec */
129 { blk_exp_close
, MDOC_EXPLICIT
}, /* Ef */
130 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Em */
131 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Eo */
132 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fx */
133 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ms */
134 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* No */
135 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ns */
136 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Nx */
137 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ox */
138 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Pc */
139 { in_line_argn
, MDOC_PARSED
| MDOC_IGNDELIM
}, /* Pf */
140 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Po */
141 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Pq */
142 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Qc */
143 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ql */
144 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Qo */
145 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Qq */
146 { blk_exp_close
, MDOC_EXPLICIT
}, /* Re */
147 { blk_full
, MDOC_EXPLICIT
}, /* Rs */
148 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Sc */
149 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* So */
150 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Sq */
151 { in_line_eoln
, 0 }, /* Sm */
152 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Sx */
153 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Sy */
154 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Tn */
155 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ux */
156 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Xc */
157 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Xo */
158 { blk_full
, MDOC_EXPLICIT
| MDOC_CALLABLE
}, /* Fo */
159 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Fc */
160 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Oo */
161 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Oc */
162 { blk_full
, MDOC_EXPLICIT
}, /* Bk */
163 { blk_exp_close
, MDOC_EXPLICIT
}, /* Ek */
164 { in_line_eoln
, 0 }, /* Bt */
165 { in_line_eoln
, 0 }, /* Hf */
166 { obsolete
, 0 }, /* Fr */
167 { in_line_eoln
, 0 }, /* Ud */
168 { in_line_eoln
, 0 }, /* Lb */
169 { in_line_eoln
, 0 }, /* Lp */
170 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Lk */
171 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Mt */
172 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Brq */
173 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Bro */
174 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Brc */
175 { in_line_eoln
, 0 }, /* %C */
176 { obsolete
, 0 }, /* Es */
177 { obsolete
, 0 }, /* En */
178 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Dx */
179 { in_line_eoln
, 0 }, /* %Q */
180 { in_line_eoln
, 0 }, /* br */
181 { in_line_eoln
, 0 }, /* sp */
182 { in_line_eoln
, 0 }, /* %U */
185 const struct mdoc_macro
* const mdoc_macros
= __mdoc_macros
;
189 swarn(struct mdoc
*mdoc
, enum mdoc_type type
,
190 int line
, int pos
, const struct mdoc_node
*p
)
192 const char *n
, *t
, *tt
;
210 n
= mdoc_macronames
[p
->tok
];
214 n
= mdoc_macronames
[p
->tok
];
218 n
= mdoc_macronames
[p
->tok
];
225 if ( ! (MDOC_IGN_SCOPE
& mdoc
->pflags
))
226 return(mdoc_verr(mdoc
, line
, pos
,
227 "%s scope breaks %s scope of %s",
229 return(mdoc_vwarn(mdoc
, line
, pos
,
230 "%s scope breaks %s scope of %s",
236 * This is called at the end of parsing. It must traverse up the tree,
237 * closing out open [implicit] scopes. Obviously, open explicit scopes
241 mdoc_macroend(struct mdoc
*m
)
245 /* Scan for open explicit scopes. */
247 n
= MDOC_VALID
& m
->last
->flags
? m
->last
->parent
: m
->last
;
249 for ( ; n
; n
= n
->parent
) {
250 if (MDOC_BLOCK
!= n
->type
)
252 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[n
->tok
].flags
))
254 return(mdoc_nerr(m
, n
, EOPEN
));
257 /* Rewind to the first. */
259 return(rew_last(m
, m
->first
));
264 * Look up a macro from within a subsequent context.
267 lookup(int from
, const char *p
)
269 /* FIXME: make -diag lists be un-PARSED. */
271 if ( ! (MDOC_PARSED
& mdoc_macros
[from
].flags
))
273 return(lookup_raw(p
));
278 * Lookup a macro following the initial line macro.
281 lookup_raw(const char *p
)
285 if (MDOC_MAX
== (res
= mdoc_hash_find(p
)))
287 if (MDOC_CALLABLE
& mdoc_macros
[res
].flags
)
294 rew_last(struct mdoc
*mdoc
, const struct mdoc_node
*to
)
298 mdoc
->next
= MDOC_NEXT_SIBLING
;
301 while (mdoc
->last
!= to
) {
302 if ( ! mdoc_valid_post(mdoc
))
304 if ( ! mdoc_action_post(mdoc
))
306 mdoc
->last
= mdoc
->last
->parent
;
310 if ( ! mdoc_valid_post(mdoc
))
312 return(mdoc_action_post(mdoc
));
317 * Return the opening macro of a closing one, e.g., `Ec' has `Eo' as its
365 * Rewind rules. This indicates whether to stop rewinding
366 * (REWIND_HALT) without touching our current scope, stop rewinding and
367 * close our current scope (REWIND_REWIND), or continue (REWIND_NOHALT).
368 * The scope-closing and so on occurs in the various rew_* routines.
371 rew_dohalt(int tok
, enum mdoc_type type
, const struct mdoc_node
*p
)
374 if (MDOC_ROOT
== p
->type
)
376 if (MDOC_VALID
& p
->flags
)
377 return(REWIND_NOHALT
);
401 assert(MDOC_TAIL
!= type
);
402 if (type
== p
->type
&& tok
== p
->tok
)
403 return(REWIND_REWIND
);
406 assert(MDOC_TAIL
!= type
);
407 if (type
== p
->type
&& tok
== p
->tok
)
408 return(REWIND_REWIND
);
409 if (MDOC_BODY
== p
->type
&& MDOC_Bl
== p
->tok
)
413 if (type
== p
->type
&& tok
== p
->tok
)
414 return(REWIND_REWIND
);
419 assert(MDOC_TAIL
!= type
);
420 if (type
== p
->type
&& tok
== p
->tok
)
421 return(REWIND_REWIND
);
422 if (MDOC_BODY
== p
->type
&& MDOC_Sh
== p
->tok
)
456 if (type
== p
->type
&& tok
== p
->tok
)
457 return(REWIND_REWIND
);
459 /* Multi-line explicit scope close. */
491 if (type
== p
->type
&& rew_alt(tok
) == p
->tok
)
492 return(REWIND_REWIND
);
499 return(REWIND_NOHALT
);
504 * See if we can break an encountered scope (the rew_dohalt has returned
508 rew_dobreak(int tok
, const struct mdoc_node
*p
)
511 assert(MDOC_ROOT
!= p
->type
);
512 if (MDOC_ELEM
== p
->type
)
514 if (MDOC_TEXT
== p
->type
)
516 if (MDOC_VALID
& p
->flags
)
521 return(MDOC_It
== p
->tok
);
523 return(MDOC_Nd
== p
->tok
);
525 return(MDOC_Ss
== p
->tok
);
527 if (MDOC_Nd
== p
->tok
)
529 if (MDOC_Ss
== p
->tok
)
531 return(MDOC_Sh
== p
->tok
);
533 if (MDOC_It
== p
->tok
)
537 /* XXX - experimental! */
538 if (MDOC_Op
== p
->tok
)
545 if (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
)
546 return(p
->tok
== rew_alt(tok
));
547 else if (MDOC_BLOCK
== p
->type
)
550 return(tok
== p
->tok
);
555 rew_elem(struct mdoc
*mdoc
, int tok
)
560 if (MDOC_ELEM
!= n
->type
)
562 assert(MDOC_ELEM
== n
->type
);
563 assert(tok
== n
->tok
);
565 return(rew_last(mdoc
, n
));
570 rew_sub(enum mdoc_type t
, struct mdoc
*m
,
571 int tok
, int line
, int ppos
)
577 for (n
= m
->last
; n
; n
= n
->parent
) {
578 c
= rew_dohalt(tok
, t
, n
);
579 if (REWIND_HALT
== c
) {
582 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
))
584 return(mdoc_perr(m
, line
, ppos
, ENOCTX
));
586 if (REWIND_REWIND
== c
)
588 else if (rew_dobreak(tok
, n
))
590 if ( ! swarn(m
, t
, line
, ppos
, n
))
595 return(rew_last(m
, n
));
600 append_delims(struct mdoc
*mdoc
, int line
, int *pos
, char *buf
)
610 c
= mdoc_zargs(mdoc
, line
, pos
, buf
, ARGS_NOWARN
, &p
);
611 assert(ARGS_PHRASE
!= c
);
615 else if (ARGS_EOLN
== c
)
617 assert(mdoc_isdelim(p
));
618 if ( ! mdoc_word_alloc(mdoc
, line
, lastarg
, p
))
627 * Close out block partial/full explicit.
630 blk_exp_close(MACRO_PROT_ARGS
)
632 int j
, c
, lastarg
, maxargs
, flushed
;
644 if ( ! (MDOC_CALLABLE
& mdoc_macros
[tok
].flags
)) {
646 if ( ! mdoc_pwarn(m
, line
, ppos
, ENOLINE
))
649 if ( ! rew_sub(MDOC_BODY
, m
, tok
, line
, ppos
))
651 return(rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
));
654 if ( ! rew_sub(MDOC_BODY
, m
, tok
, line
, ppos
))
658 if ( ! mdoc_tail_alloc(m
, line
, ppos
, rew_alt(tok
)))
661 for (flushed
= j
= 0; ; j
++) {
664 if (j
== maxargs
&& ! flushed
) {
665 if ( ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
670 c
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
679 if (MDOC_MAX
!= (c
= lookup(tok
, p
))) {
681 if ( ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
685 if ( ! mdoc_macro(m
, c
, line
, lastarg
, pos
, buf
))
690 if ( ! mdoc_word_alloc(m
, line
, lastarg
, p
))
694 if ( ! flushed
&& ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
699 return(append_delims(m
, line
, pos
, buf
));
704 in_line(MACRO_PROT_ARGS
)
706 int la
, lastpunct
, c
, w
, cnt
, d
, nc
;
707 struct mdoc_arg
*arg
;
711 * Whether we allow ignored elements (those without content,
712 * usually because of reserved words) to squeak by.
733 for (arg
= NULL
;; ) {
735 c
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
737 if (ARGV_WORD
== c
) {
750 for (cnt
= 0, lastpunct
= 1;; ) {
752 w
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
761 /* Quoted words shouldn't be looked-up. */
763 c
= ARGS_QWORD
== w
? MDOC_MAX
: lookup(tok
, p
);
766 * In this case, we've located a submacro and must
767 * execute it. Close out scope, if open. If no
768 * elements have been generated, either create one (nc)
769 * or raise a warning.
773 if (0 == lastpunct
&& ! rew_elem(m
, tok
))
775 if (nc
&& 0 == cnt
) {
776 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
778 if ( ! rew_last(m
, m
->last
))
780 } else if ( ! nc
&& 0 == cnt
) {
782 if ( ! mdoc_pwarn(m
, line
, ppos
, EIGNE
))
785 c
= mdoc_macro(m
, c
, line
, la
, pos
, buf
);
790 return(append_delims(m
, line
, pos
, buf
));
794 * Non-quote-enclosed punctuation. Set up our scope, if
795 * a word; rewind the scope, if a delimiter; then append
801 if (ARGS_QWORD
!= w
&& d
) {
802 if (0 == lastpunct
&& ! rew_elem(m
, tok
))
805 } else if (lastpunct
) {
806 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
813 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
817 * `Fl' macros have their scope re-opened with each new
818 * word so that the `-' can be added to each one without
819 * having to parse out spaces.
821 if (0 == lastpunct
&& MDOC_Fl
== tok
) {
822 if ( ! rew_elem(m
, tok
))
828 if (0 == lastpunct
&& ! rew_elem(m
, tok
))
832 * If no elements have been collected and we're allowed to have
833 * empties (nc), open a scope and close it out. Otherwise,
837 if (nc
&& 0 == cnt
) {
838 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
840 if ( ! rew_last(m
, m
->last
))
842 } else if ( ! nc
&& 0 == cnt
) {
844 if ( ! mdoc_pwarn(m
, line
, ppos
, EIGNE
))
850 return(append_delims(m
, line
, pos
, buf
));
855 blk_full(MACRO_PROT_ARGS
)
857 int c
, lastarg
, reopen
, dohead
;
858 struct mdoc_arg
*arg
;
862 * Whether to process a block-head section. If this is
863 * non-zero, then a head will be opened for all line arguments.
864 * If not, then the head will always be empty and only a body
865 * will be opened, which will stay open at the eoln.
877 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
)) {
878 if ( ! rew_sub(MDOC_BODY
, m
, tok
, line
, ppos
))
880 if ( ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
884 for (arg
= NULL
;; ) {
886 c
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
888 if (ARGV_WORD
== c
) {
902 if ( ! mdoc_block_alloc(m
, line
, ppos
, tok
, arg
))
905 if (0 == buf
[*pos
]) {
906 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
908 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
910 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
915 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
918 /* Immediately close out head and enter body, if applicable. */
921 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
923 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
927 for (reopen
= 0;; ) {
929 c
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
935 if (ARGS_PHRASE
== c
) {
937 if (reopen
&& ! mdoc_head_alloc(m
, line
, ppos
, tok
))
940 * Phrases are self-contained macro phrases used
941 * in the columnar output of a macro. They need
944 if ( ! phrase(m
, line
, lastarg
, buf
))
946 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
953 if (MDOC_MAX
== (c
= lookup(tok
, p
))) {
954 if ( ! mdoc_word_alloc(m
, line
, lastarg
, p
))
959 if ( ! mdoc_macro(m
, c
, line
, lastarg
, pos
, buf
))
964 if (1 == ppos
&& ! append_delims(m
, line
, pos
, buf
))
967 /* If the body's already open, then just return. */
971 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
973 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
981 blk_part_imp(MACRO_PROT_ARGS
)
985 struct mdoc_node
*blk
, *body
, *n
;
987 /* If applicable, close out prior scopes. */
989 if ( ! mdoc_block_alloc(m
, line
, ppos
, tok
, NULL
))
991 /* Saved for later close-out. */
993 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
995 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
997 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
999 /* Saved for later close-out. */
1002 /* Body argument processing. */
1006 c
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1007 assert(ARGS_PHRASE
!= c
);
1009 if (ARGS_ERROR
== c
)
1011 if (ARGS_PUNCT
== c
)
1016 if (MDOC_MAX
== (c
= lookup(tok
, p
))) {
1017 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1022 if ( ! mdoc_macro(m
, c
, line
, la
, pos
, buf
))
1028 * If we can't rewind to our body, then our scope has already
1029 * been closed by another macro (like `Oc' closing `Op'). This
1030 * is ugly behaviour nodding its head to OpenBSD's overwhelming
1031 * crufty use of `Op' breakage--XXX, deprecate in time.
1033 for (n
= m
->last
; n
; n
= n
->parent
)
1036 if (NULL
== n
&& ! mdoc_nwarn(m
, body
, EIMPBRK
))
1038 if (n
&& ! rew_last(m
, body
))
1041 /* Standard appending of delimiters. */
1043 if (1 == ppos
&& ! append_delims(m
, line
, pos
, buf
))
1046 /* Rewind scope, if applicable. */
1048 if (n
&& ! rew_last(m
, blk
))
1056 blk_part_exp(MACRO_PROT_ARGS
)
1058 int la
, flushed
, j
, c
, maxargs
;
1061 /* Number of head arguments. Only `Eo' has these, */
1072 /* Begin the block scope. */
1074 if ( ! mdoc_block_alloc(m
, line
, ppos
, tok
, NULL
))
1078 * If no head arguments, open and then close out a head, noting
1079 * that we've flushed our terms. `flushed' means that we've
1080 * flushed out the head and the body is open.
1084 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
1086 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1088 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1092 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
1097 /* Process the head/head+body line arguments. */
1099 for (j
= 0; ; j
++) {
1101 if (j
== maxargs
&& ! flushed
) {
1102 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1105 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1109 c
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1110 assert(ARGS_PHRASE
!= c
);
1112 if (ARGS_ERROR
== c
)
1114 if (ARGS_PUNCT
== c
)
1119 if (MDOC_MAX
!= (c
= lookup(tok
, p
))) {
1121 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1124 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1127 if ( ! mdoc_macro(m
, c
, line
, la
, pos
, buf
))
1132 if ( ! flushed
&& mdoc_isdelim(p
)) {
1133 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1136 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1140 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1144 /* Close the head and open the body, if applicable. */
1147 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1149 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1153 /* Standard appending of delimiters. */
1157 return(append_delims(m
, line
, pos
, buf
));
1162 in_line_argn(MACRO_PROT_ARGS
)
1164 int la
, flushed
, j
, c
, maxargs
;
1165 struct mdoc_arg
*arg
;
1168 /* Fixed maximum arguments per macro, if applicable. */
1185 /* Macro argument processing. */
1187 for (arg
= NULL
;; ) {
1189 c
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
1191 if (ARGV_WORD
== c
) {
1201 mdoc_argv_free(arg
);
1205 /* Open the element scope. */
1207 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
1210 /* Process element arguments. */
1212 for (flushed
= j
= 0; ; j
++) {
1215 if (j
== maxargs
&& ! flushed
) {
1216 if ( ! rew_elem(m
, tok
))
1221 c
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1223 if (ARGS_ERROR
== c
)
1225 if (ARGS_PUNCT
== c
)
1230 if (MDOC_MAX
!= (c
= lookup(tok
, p
))) {
1231 if ( ! flushed
&& ! rew_elem(m
, tok
))
1234 if ( ! mdoc_macro(m
, c
, line
, la
, pos
, buf
))
1239 if ( ! (MDOC_IGNDELIM
& mdoc_macros
[tok
].flags
) &&
1240 ! flushed
&& mdoc_isdelim(p
)) {
1241 if ( ! rew_elem(m
, tok
))
1246 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1250 /* Close out and append delimiters. */
1252 if ( ! flushed
&& ! rew_elem(m
, tok
))
1257 return(append_delims(m
, line
, pos
, buf
));
1262 in_line_eoln(MACRO_PROT_ARGS
)
1265 struct mdoc_arg
*arg
;
1268 assert( ! (MDOC_PARSED
& mdoc_macros
[tok
].flags
));
1270 /* Parse macro arguments. */
1272 for (arg
= NULL
; ; ) {
1274 c
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
1276 if (ARGV_WORD
== c
) {
1285 mdoc_argv_free(arg
);
1289 /* Open element scope. */
1291 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
1294 /* Parse argument terms. */
1298 w
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1300 if (ARGS_ERROR
== w
)
1305 c
= ARGS_QWORD
== w
? MDOC_MAX
: lookup(tok
, p
);
1307 if (MDOC_MAX
!= c
) {
1308 if ( ! rew_elem(m
, tok
))
1310 return(mdoc_macro(m
, c
, line
, la
, pos
, buf
));
1313 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1317 /* Close out (no delimiters). */
1319 return(rew_elem(m
, tok
));
1325 obsolete(MACRO_PROT_ARGS
)
1328 return(mdoc_pwarn(m
, line
, ppos
, EOBS
));
1333 * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
1334 * They're unusual because they're basically free-form text until a
1335 * macro is encountered.
1338 phrase(struct mdoc
*m
, int line
, int ppos
, char *buf
)
1343 for (pos
= ppos
; ; ) {
1346 /* Note: no calling context! */
1347 w
= mdoc_zargs(m
, line
, &pos
, buf
, 0, &p
);
1349 if (ARGS_ERROR
== w
)
1354 c
= ARGS_QWORD
== w
? MDOC_MAX
: lookup_raw(p
);
1356 if (MDOC_MAX
!= c
) {
1357 if ( ! mdoc_macro(m
, c
, line
, la
, &pos
, buf
))
1359 return(append_delims(m
, line
, &pos
, buf
));
1362 if ( ! mdoc_word_alloc(m
, line
, la
, p
))