]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_macro.c
1 /* $Id: mdoc_macro.c,v 1.58 2010/05/08 07:30:19 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.
36 static int blk_full(MACRO_PROT_ARGS
);
37 static int blk_exp_close(MACRO_PROT_ARGS
);
38 static int blk_part_exp(MACRO_PROT_ARGS
);
39 static int blk_part_imp(MACRO_PROT_ARGS
);
40 static int ctx_synopsis(MACRO_PROT_ARGS
);
41 static int in_line_eoln(MACRO_PROT_ARGS
);
42 static int in_line_argn(MACRO_PROT_ARGS
);
43 static int in_line(MACRO_PROT_ARGS
);
44 static int obsolete(MACRO_PROT_ARGS
);
46 static int append_delims(struct mdoc
*,
48 static enum mdoct
lookup(enum mdoct
, const char *);
49 static enum mdoct
lookup_raw(const char *);
50 static int phrase(struct mdoc
*, int, int, char *);
51 static enum mdoct
rew_alt(enum mdoct
);
52 static int rew_dobreak(enum mdoct
,
53 const struct mdoc_node
*);
54 static enum rew
rew_dohalt(enum mdoct
, enum mdoc_type
,
55 const struct mdoc_node
*);
56 static int rew_elem(struct mdoc
*, enum mdoct
);
57 static int rew_last(struct mdoc
*,
58 const struct mdoc_node
*);
59 static int rew_sub(enum mdoc_type
, struct mdoc
*,
60 enum mdoct
, int, int);
61 static int swarn(struct mdoc
*, enum mdoc_type
, int,
62 int, const struct mdoc_node
*);
64 const struct mdoc_macro __mdoc_macros
[MDOC_MAX
] = {
65 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ap */
66 { in_line_eoln
, MDOC_PROLOGUE
}, /* Dd */
67 { in_line_eoln
, MDOC_PROLOGUE
}, /* Dt */
68 { in_line_eoln
, MDOC_PROLOGUE
}, /* Os */
69 { blk_full
, 0 }, /* Sh */
70 { blk_full
, 0 }, /* Ss */
71 { in_line_eoln
, 0 }, /* Pp */
72 { blk_part_imp
, MDOC_PARSED
}, /* D1 */
73 { blk_part_imp
, MDOC_PARSED
}, /* Dl */
74 { blk_full
, MDOC_EXPLICIT
}, /* Bd */
75 { blk_exp_close
, MDOC_EXPLICIT
}, /* Ed */
76 { blk_full
, MDOC_EXPLICIT
}, /* Bl */
77 { blk_exp_close
, MDOC_EXPLICIT
}, /* El */
78 { blk_full
, MDOC_PARSED
}, /* It */
79 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ad */
80 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* An */
81 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ar */
82 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Cd */
83 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Cm */
84 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Dv */
85 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Er */
86 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ev */
87 { in_line_eoln
, 0 }, /* Ex */
88 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fa */
89 { in_line_eoln
, 0 }, /* Fd */
90 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fl */
91 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fn */
92 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ft */
93 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ic */
94 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* In */
95 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Li */
96 { blk_full
, 0 }, /* Nd */
97 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Nm */
98 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Op */
99 { obsolete
, 0 }, /* Ot */
100 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Pa */
101 { in_line_eoln
, 0 }, /* Rv */
102 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* St */
103 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Va */
104 { ctx_synopsis
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Vt */
105 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Xr */
106 { in_line_eoln
, 0 }, /* %A */
107 { in_line_eoln
, 0 }, /* %B */
108 { in_line_eoln
, 0 }, /* %D */
109 { in_line_eoln
, 0 }, /* %I */
110 { in_line_eoln
, 0 }, /* %J */
111 { in_line_eoln
, 0 }, /* %N */
112 { in_line_eoln
, 0 }, /* %O */
113 { in_line_eoln
, 0 }, /* %P */
114 { in_line_eoln
, 0 }, /* %R */
115 { in_line_eoln
, 0 }, /* %T */
116 { in_line_eoln
, 0 }, /* %V */
117 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Ac */
118 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Ao */
119 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Aq */
120 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* At */
121 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Bc */
122 { blk_full
, MDOC_EXPLICIT
}, /* Bf */
123 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Bo */
124 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Bq */
125 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Bsx */
126 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Bx */
127 { in_line_eoln
, 0 }, /* Db */
128 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Dc */
129 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Do */
130 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Dq */
131 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Ec */
132 { blk_exp_close
, MDOC_EXPLICIT
}, /* Ef */
133 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Em */
134 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Eo */
135 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fx */
136 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ms */
137 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* No */
138 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ns */
139 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Nx */
140 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ox */
141 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Pc */
142 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_IGNDELIM
}, /* Pf */
143 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Po */
144 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Pq */
145 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Qc */
146 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ql */
147 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Qo */
148 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Qq */
149 { blk_exp_close
, MDOC_EXPLICIT
}, /* Re */
150 { blk_full
, MDOC_EXPLICIT
}, /* Rs */
151 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Sc */
152 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* So */
153 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Sq */
154 { in_line_eoln
, 0 }, /* Sm */
155 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Sx */
156 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Sy */
157 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Tn */
158 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ux */
159 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Xc */
160 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Xo */
161 { blk_full
, MDOC_EXPLICIT
| MDOC_CALLABLE
}, /* Fo */
162 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Fc */
163 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Oo */
164 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Oc */
165 { blk_full
, MDOC_EXPLICIT
}, /* Bk */
166 { blk_exp_close
, MDOC_EXPLICIT
}, /* Ek */
167 { in_line_eoln
, 0 }, /* Bt */
168 { in_line_eoln
, 0 }, /* Hf */
169 { obsolete
, 0 }, /* Fr */
170 { in_line_eoln
, 0 }, /* Ud */
171 { in_line_eoln
, 0 }, /* Lb */
172 { in_line_eoln
, 0 }, /* Lp */
173 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Lk */
174 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Mt */
175 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Brq */
176 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Bro */
177 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Brc */
178 { in_line_eoln
, 0 }, /* %C */
179 { obsolete
, 0 }, /* Es */
180 { obsolete
, 0 }, /* En */
181 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Dx */
182 { in_line_eoln
, 0 }, /* %Q */
183 { in_line_eoln
, 0 }, /* br */
184 { in_line_eoln
, 0 }, /* sp */
185 { in_line_eoln
, 0 }, /* %U */
188 const struct mdoc_macro
* const mdoc_macros
= __mdoc_macros
;
192 swarn(struct mdoc
*mdoc
, enum mdoc_type type
,
193 int line
, int pos
, const struct mdoc_node
*p
)
195 const char *n
, *t
, *tt
;
213 n
= mdoc_macronames
[p
->tok
];
217 n
= mdoc_macronames
[p
->tok
];
221 n
= mdoc_macronames
[p
->tok
];
228 if ( ! (MDOC_IGN_SCOPE
& mdoc
->pflags
))
229 return(mdoc_verr(mdoc
, line
, pos
,
230 "%s scope breaks %s scope of %s",
232 return(mdoc_vwarn(mdoc
, line
, pos
,
233 "%s scope breaks %s scope of %s",
239 * This is called at the end of parsing. It must traverse up the tree,
240 * closing out open [implicit] scopes. Obviously, open explicit scopes
244 mdoc_macroend(struct mdoc
*m
)
248 /* Scan for open explicit scopes. */
250 n
= MDOC_VALID
& m
->last
->flags
? m
->last
->parent
: m
->last
;
252 for ( ; n
; n
= n
->parent
) {
253 if (MDOC_BLOCK
!= n
->type
)
255 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[n
->tok
].flags
))
257 return(mdoc_nerr(m
, n
, EOPEN
));
260 /* Rewind to the first. */
262 return(rew_last(m
, m
->first
));
267 * Look up a macro from within a subsequent context.
270 lookup(enum mdoct from
, const char *p
)
272 /* FIXME: make -diag lists be un-PARSED. */
274 if ( ! (MDOC_PARSED
& mdoc_macros
[from
].flags
))
276 return(lookup_raw(p
));
281 * Lookup a macro following the initial line macro.
284 lookup_raw(const char *p
)
288 if (MDOC_MAX
== (res
= mdoc_hash_find(p
)))
290 if (MDOC_CALLABLE
& mdoc_macros
[res
].flags
)
297 rew_last(struct mdoc
*mdoc
, const struct mdoc_node
*to
)
301 mdoc
->next
= MDOC_NEXT_SIBLING
;
304 while (mdoc
->last
!= to
) {
305 if ( ! mdoc_valid_post(mdoc
))
307 if ( ! mdoc_action_post(mdoc
))
309 mdoc
->last
= mdoc
->last
->parent
;
313 if ( ! mdoc_valid_post(mdoc
))
315 return(mdoc_action_post(mdoc
));
320 * Return the opening macro of a closing one, e.g., `Ec' has `Eo' as its
324 rew_alt(enum mdoct tok
)
368 * Rewind rules. This indicates whether to stop rewinding
369 * (REWIND_HALT) without touching our current scope, stop rewinding and
370 * close our current scope (REWIND_REWIND), or continue (REWIND_NOHALT).
371 * The scope-closing and so on occurs in the various rew_* routines.
374 rew_dohalt(enum mdoct tok
, enum mdoc_type type
,
375 const struct mdoc_node
*p
)
378 if (MDOC_ROOT
== p
->type
)
380 if (MDOC_VALID
& p
->flags
)
381 return(REWIND_NOHALT
);
407 assert(MDOC_TAIL
!= type
);
408 if (type
== p
->type
&& tok
== p
->tok
)
409 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_Bl
== p
->tok
)
419 if (type
== p
->type
&& tok
== p
->tok
)
420 return(REWIND_REWIND
);
425 assert(MDOC_TAIL
!= type
);
426 if (type
== p
->type
&& tok
== p
->tok
)
427 return(REWIND_REWIND
);
428 if (MDOC_BODY
== p
->type
&& MDOC_Sh
== p
->tok
)
462 if (type
== p
->type
&& tok
== p
->tok
)
463 return(REWIND_REWIND
);
465 /* Multi-line explicit scope close. */
497 if (type
== p
->type
&& rew_alt(tok
) == p
->tok
)
498 return(REWIND_REWIND
);
505 return(REWIND_NOHALT
);
510 * See if we can break an encountered scope (the rew_dohalt has returned
514 rew_dobreak(enum mdoct tok
, const struct mdoc_node
*p
)
517 assert(MDOC_ROOT
!= p
->type
);
518 if (MDOC_ELEM
== p
->type
)
520 if (MDOC_TEXT
== p
->type
)
522 if (MDOC_VALID
& p
->flags
)
527 return(MDOC_It
== p
->tok
);
529 return(MDOC_Nd
== p
->tok
);
531 return(MDOC_Ss
== p
->tok
);
533 if (MDOC_Nd
== p
->tok
)
535 if (MDOC_Ss
== p
->tok
)
537 return(MDOC_Sh
== p
->tok
);
539 if (MDOC_It
== p
->tok
)
543 if (MDOC_Op
== p
->tok
)
550 if (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
)
551 return(p
->tok
== rew_alt(tok
));
552 else if (MDOC_BLOCK
== p
->type
)
555 return(tok
== p
->tok
);
560 rew_elem(struct mdoc
*mdoc
, enum mdoct tok
)
565 if (MDOC_ELEM
!= n
->type
)
567 assert(MDOC_ELEM
== n
->type
);
568 assert(tok
== n
->tok
);
570 return(rew_last(mdoc
, n
));
575 rew_sub(enum mdoc_type t
, struct mdoc
*m
,
576 enum mdoct tok
, int line
, int ppos
)
582 for (n
= m
->last
; n
; n
= n
->parent
) {
583 c
= rew_dohalt(tok
, t
, n
);
584 if (REWIND_HALT
== c
) {
587 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
))
589 return(mdoc_perr(m
, line
, ppos
, ENOCTX
));
591 if (REWIND_REWIND
== c
)
593 else if (rew_dobreak(tok
, n
))
595 if ( ! swarn(m
, t
, line
, ppos
, n
))
600 if ( ! rew_last(m
, n
))
605 * The current block extends an enclosing block beyond a line
606 * break. Now that the current block ends, close the enclosing
609 if (NULL
!= (n
= n
->pending
)) {
610 assert(MDOC_HEAD
== n
->type
);
611 if ( ! rew_last(m
, n
))
613 if ( ! mdoc_body_alloc(m
, n
->line
, n
->pos
, n
->tok
))
623 append_delims(struct mdoc
*mdoc
, int line
, int *pos
, char *buf
)
634 ac
= mdoc_zargs(mdoc
, line
, pos
, buf
, ARGS_NOWARN
, &p
);
636 if (ARGS_ERROR
== ac
)
638 else if (ARGS_EOLN
== ac
)
640 assert(mdoc_isdelim(p
));
641 if ( ! mdoc_word_alloc(mdoc
, line
, lastarg
, p
))
650 * Close out block partial/full explicit.
653 blk_exp_close(MACRO_PROT_ARGS
)
655 int j
, lastarg
, maxargs
, flushed
;
669 if ( ! (MDOC_CALLABLE
& mdoc_macros
[tok
].flags
)) {
671 if ( ! mdoc_pwarn(m
, line
, ppos
, ENOLINE
))
674 if ( ! rew_sub(MDOC_BODY
, m
, tok
, line
, ppos
))
676 return(rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
));
679 if ( ! rew_sub(MDOC_BODY
, m
, tok
, line
, ppos
))
683 if ( ! mdoc_tail_alloc(m
, line
, ppos
, rew_alt(tok
)))
686 for (flushed
= j
= 0; ; j
++) {
689 if (j
== maxargs
&& ! flushed
) {
690 if ( ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
695 ac
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
697 if (ARGS_ERROR
== ac
)
699 if (ARGS_PUNCT
== ac
)
704 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
706 if (MDOC_MAX
== ntok
) {
707 if ( ! mdoc_word_alloc(m
, line
, lastarg
, p
))
713 if ( ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
717 if ( ! mdoc_macro(m
, ntok
, line
, lastarg
, pos
, buf
))
722 if ( ! flushed
&& ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
727 return(append_delims(m
, line
, pos
, buf
));
732 in_line(MACRO_PROT_ARGS
)
734 int la
, lastpunct
, cnt
, d
, nc
;
738 struct mdoc_arg
*arg
;
742 * Whether we allow ignored elements (those without content,
743 * usually because of reserved words) to squeak by.
765 for (arg
= NULL
;; ) {
767 av
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
769 if (ARGV_WORD
== av
) {
782 for (cnt
= 0, lastpunct
= 1;; ) {
784 ac
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
786 if (ARGS_ERROR
== ac
)
790 if (ARGS_PUNCT
== ac
)
793 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
796 * In this case, we've located a submacro and must
797 * execute it. Close out scope, if open. If no
798 * elements have been generated, either create one (nc)
799 * or raise a warning.
802 if (MDOC_MAX
!= ntok
) {
803 if (0 == lastpunct
&& ! rew_elem(m
, tok
))
805 if (nc
&& 0 == cnt
) {
806 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
808 if ( ! rew_last(m
, m
->last
))
810 } else if ( ! nc
&& 0 == cnt
) {
812 if ( ! mdoc_pwarn(m
, line
, ppos
, EIGNE
))
815 if ( ! mdoc_macro(m
, ntok
, line
, la
, pos
, buf
))
819 return(append_delims(m
, line
, pos
, buf
));
823 * Non-quote-enclosed punctuation. Set up our scope, if
824 * a word; rewind the scope, if a delimiter; then append
828 d
= ARGS_QWORD
== ac
? 0 : mdoc_isdelim(p
);
830 if (ARGS_QWORD
!= ac
&& d
) {
831 if (0 == lastpunct
&& ! rew_elem(m
, tok
))
834 } else if (lastpunct
) {
835 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
842 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
846 * `Fl' macros have their scope re-opened with each new
847 * word so that the `-' can be added to each one without
848 * having to parse out spaces.
850 if (0 == lastpunct
&& MDOC_Fl
== tok
) {
851 if ( ! rew_elem(m
, tok
))
857 if (0 == lastpunct
&& ! rew_elem(m
, tok
))
861 * If no elements have been collected and we're allowed to have
862 * empties (nc), open a scope and close it out. Otherwise,
866 if (nc
&& 0 == cnt
) {
867 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
869 if ( ! rew_last(m
, m
->last
))
871 } else if ( ! nc
&& 0 == cnt
) {
873 if ( ! mdoc_pwarn(m
, line
, ppos
, EIGNE
))
879 return(append_delims(m
, line
, pos
, buf
));
884 blk_full(MACRO_PROT_ARGS
)
887 struct mdoc_arg
*arg
;
888 struct mdoc_node
*head
; /* save of head macro */
889 struct mdoc_node
*body
; /* save of body macro */
898 /* Close out prior implicit scope. */
900 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
)) {
901 if ( ! rew_sub(MDOC_BODY
, m
, tok
, line
, ppos
))
903 if ( ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
908 * This routine accomodates implicitly- and explicitly-scoped
909 * macro openings. Implicit ones first close out prior scope
910 * (seen above). Delay opening the head until necessary to
911 * allow leading punctuation to print. Special consideration
912 * for `It -column', which has phrase-part syntax instead of
913 * regular child nodes.
916 for (arg
= NULL
;; ) {
918 av
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
920 if (ARGV_WORD
== av
) {
934 if ( ! mdoc_block_alloc(m
, line
, ppos
, tok
, arg
))
940 * The `Nd' macro has all arguments in its body: it's a hybrid
941 * of block partial-explicit and full-implicit. Stupid.
944 if (MDOC_Nd
== tok
) {
945 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
948 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
950 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
957 ac
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
959 if (ARGS_ERROR
== ac
)
964 /* Don't emit leading punct. for phrases. */
966 if (NULL
== head
&& ARGS_PHRASE
!= ac
&&
967 ARGS_PPHRASE
!= ac
&&
969 1 == mdoc_isdelim(p
)) {
970 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
975 /* Always re-open head for phrases. */
977 if (NULL
== head
|| ARGS_PHRASE
== ac
||
978 ARGS_PPHRASE
== ac
) {
979 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
984 if (ARGS_PHRASE
== ac
|| ARGS_PPHRASE
== ac
) {
985 if ( ! phrase(m
, line
, la
, buf
))
987 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
992 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
994 if (MDOC_MAX
== ntok
) {
995 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1000 if ( ! mdoc_macro(m
, ntok
, line
, la
, pos
, buf
))
1006 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
1011 if (1 == ppos
&& ! append_delims(m
, line
, pos
, buf
))
1014 /* If we've already opened our body, exit now. */
1021 * If there is an open (i.e., unvalidated) sub-block requiring
1022 * explicit close-out, postpone switching the current block from
1023 * head to body until the rew_sub() call closing out that
1026 for (n
= m
->last
; n
&& n
!= head
; n
= n
->parent
) {
1027 if (MDOC_BLOCK
== n
->type
&&
1028 MDOC_EXPLICIT
& mdoc_macros
[n
->tok
].flags
&&
1029 ! (MDOC_VALID
& n
->flags
)) {
1030 assert( ! (MDOC_ACTED
& n
->flags
));
1037 /* Close out scopes to remain in a consistent state. */
1039 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1041 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1049 blk_part_imp(MACRO_PROT_ARGS
)
1055 struct mdoc_node
*blk
; /* saved block context */
1056 struct mdoc_node
*body
; /* saved body context */
1057 struct mdoc_node
*n
;
1060 * A macro that spans to the end of the line. This is generally
1061 * (but not necessarily) called as the first macro. The block
1062 * has a head as the immediate child, which is always empty,
1063 * followed by zero or more opening punctuation nodes, then the
1064 * body (which may be empty, depending on the macro), then zero
1065 * or more closing punctuation nodes.
1068 if ( ! mdoc_block_alloc(m
, line
, ppos
, tok
, NULL
))
1073 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
1075 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1079 * Open the body scope "on-demand", that is, after we've
1080 * processed all our the leading delimiters (open parenthesis,
1084 for (body
= NULL
; ; ) {
1086 ac
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1088 if (ARGS_ERROR
== ac
)
1090 if (ARGS_EOLN
== ac
)
1092 if (ARGS_PUNCT
== ac
)
1095 if (NULL
== body
&& ARGS_QWORD
!= ac
&&
1096 1 == mdoc_isdelim(p
)) {
1097 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1103 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1108 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
1110 if (MDOC_MAX
== ntok
) {
1111 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1116 if ( ! mdoc_macro(m
, ntok
, line
, la
, pos
, buf
))
1121 /* Clean-ups to leave in a consistent state. */
1124 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1130 * If we can't rewind to our body, then our scope has already
1131 * been closed by another macro (like `Oc' closing `Op'). This
1132 * is ugly behaviour nodding its head to OpenBSD's overwhelming
1133 * crufty use of `Op' breakage.
1135 for (n
= m
->last
; n
; n
= n
->parent
)
1139 if (NULL
== n
&& ! mdoc_nwarn(m
, body
, EIMPBRK
))
1142 if (n
&& ! rew_last(m
, body
))
1145 /* Standard appending of delimiters. */
1147 if (1 == ppos
&& ! append_delims(m
, line
, pos
, buf
))
1150 /* Rewind scope, if applicable. */
1152 if (n
&& ! rew_last(m
, blk
))
1160 blk_part_exp(MACRO_PROT_ARGS
)
1164 struct mdoc_node
*head
; /* keep track of head */
1165 struct mdoc_node
*body
; /* keep track of body */
1170 * The opening of an explicit macro having zero or more leading
1171 * punctuation nodes; a head with optional single element (the
1172 * case of `Eo'); and a body that may be empty.
1175 if ( ! mdoc_block_alloc(m
, line
, ppos
, tok
, NULL
))
1178 for (head
= body
= NULL
; ; ) {
1180 ac
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1182 if (ARGS_ERROR
== ac
)
1184 if (ARGS_PUNCT
== ac
)
1186 if (ARGS_EOLN
== ac
)
1189 /* Flush out leading punctuation. */
1191 if (NULL
== head
&& ARGS_QWORD
!= ac
&&
1192 1 == mdoc_isdelim(p
)) {
1193 assert(NULL
== body
);
1194 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1200 assert(NULL
== body
);
1201 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
1207 * `Eo' gobbles any data into the head, but most other
1208 * macros just immediately close out and begin the body.
1213 /* No check whether it's a macro! */
1215 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1218 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1220 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1228 assert(NULL
!= head
&& NULL
!= body
);
1230 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
1232 if (MDOC_MAX
== ntok
) {
1233 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1238 if ( ! mdoc_macro(m
, ntok
, line
, la
, pos
, buf
))
1243 /* Clean-up to leave in a consistent state. */
1246 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
1252 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1254 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1259 /* Standard appending of delimiters. */
1264 return(append_delims(m
, line
, pos
, buf
));
1269 in_line_argn(MACRO_PROT_ARGS
)
1271 int la
, flushed
, j
, maxargs
;
1274 struct mdoc_arg
*arg
;
1279 * A line macro that has a fixed number of arguments (maxargs).
1280 * Only open the scope once the first non-leading-punctuation is
1281 * found (unless MDOC_IGNDELIM is noted, like in `Pf'), then
1282 * keep it open until the maximum number of arguments are
1304 for (arg
= NULL
; ; ) {
1306 av
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
1308 if (ARGV_WORD
== av
) {
1313 if (ARGV_EOLN
== av
)
1318 mdoc_argv_free(arg
);
1322 for (flushed
= j
= 0; ; ) {
1324 ac
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1326 if (ARGS_ERROR
== ac
)
1328 if (ARGS_PUNCT
== ac
)
1330 if (ARGS_EOLN
== ac
)
1333 if ( ! (MDOC_IGNDELIM
& mdoc_macros
[tok
].flags
) &&
1335 0 == j
&& 1 == mdoc_isdelim(p
)) {
1336 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1340 if ( ! mdoc_elem_alloc(m
, line
, la
, tok
, arg
))
1343 if (j
== maxargs
&& ! flushed
) {
1344 if ( ! rew_elem(m
, tok
))
1349 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
1351 if (MDOC_MAX
!= ntok
) {
1352 if ( ! flushed
&& ! rew_elem(m
, tok
))
1355 if ( ! mdoc_macro(m
, ntok
, line
, la
, pos
, buf
))
1361 if ( ! (MDOC_IGNDELIM
& mdoc_macros
[tok
].flags
) &&
1363 ! flushed
&& mdoc_isdelim(p
)) {
1364 if ( ! rew_elem(m
, tok
))
1370 * XXX: this is a hack to work around groff's ugliness
1371 * as regards `Xr' and extraneous arguments. It should
1372 * ideally be deprecated behaviour, but because this is
1373 * code is no here, it's unlikely to be removed.
1377 if (MDOC_Xr
== tok
&& j
== maxargs
) {
1378 if ( ! mdoc_elem_alloc(m
, line
, la
, MDOC_Ns
, NULL
))
1380 if ( ! rew_elem(m
, MDOC_Ns
))
1385 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1390 if (0 == j
&& ! mdoc_elem_alloc(m
, line
, la
, tok
, arg
))
1393 /* Close out in a consistent state. */
1395 if ( ! flushed
&& ! rew_elem(m
, tok
))
1400 return(append_delims(m
, line
, pos
, buf
));
1405 in_line_eoln(MACRO_PROT_ARGS
)
1410 struct mdoc_arg
*arg
;
1414 assert( ! (MDOC_PARSED
& mdoc_macros
[tok
].flags
));
1416 /* Parse macro arguments. */
1418 for (arg
= NULL
; ; ) {
1420 av
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
1422 if (ARGV_WORD
== av
) {
1426 if (ARGV_EOLN
== av
)
1431 mdoc_argv_free(arg
);
1435 /* Open element scope. */
1437 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
1440 /* Parse argument terms. */
1444 ac
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1446 if (ARGS_ERROR
== ac
)
1448 if (ARGS_EOLN
== ac
)
1451 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
1453 if (MDOC_MAX
== ntok
) {
1454 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1459 if ( ! rew_elem(m
, tok
))
1461 return(mdoc_macro(m
, ntok
, line
, la
, pos
, buf
));
1464 /* Close out (no delimiters). */
1466 return(rew_elem(m
, tok
));
1472 ctx_synopsis(MACRO_PROT_ARGS
)
1475 /* If we're not in the SYNOPSIS, go straight to in-line. */
1476 if (SEC_SYNOPSIS
!= m
->lastsec
)
1477 return(in_line(m
, tok
, line
, ppos
, pos
, buf
));
1479 /* If we're a nested call, same place. */
1481 return(in_line(m
, tok
, line
, ppos
, pos
, buf
));
1484 * XXX: this will open a block scope; however, if later we end
1485 * up formatting the block scope, then child nodes will inherit
1486 * the formatting. Be careful.
1489 return(blk_part_imp(m
, tok
, line
, ppos
, pos
, buf
));
1495 obsolete(MACRO_PROT_ARGS
)
1498 return(mdoc_pwarn(m
, line
, ppos
, EOBS
));
1503 * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
1504 * They're unusual because they're basically free-form text until a
1505 * macro is encountered.
1508 phrase(struct mdoc
*m
, int line
, int ppos
, char *buf
)
1515 for (pos
= ppos
; ; ) {
1518 /* Note: no calling context! */
1519 ac
= mdoc_zargs(m
, line
, &pos
, buf
, 0, &p
);
1521 if (ARGS_ERROR
== ac
)
1523 if (ARGS_EOLN
== ac
)
1526 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup_raw(p
);
1528 if (MDOC_MAX
== ntok
) {
1529 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1534 if ( ! mdoc_macro(m
, ntok
, line
, la
, &pos
, buf
))
1536 return(append_delims(m
, line
, &pos
, buf
));