]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_macro.c
d9cf11dab2b577e1d43fef816c1dfe4072abdc98
1 /* $Id: mdoc_macro.c,v 1.61 2010/05/13 11:34:45 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,
51 char *, enum margserr
, int);
52 static enum mdoct
rew_alt(enum mdoct
);
53 static int rew_dobreak(enum mdoct
,
54 const struct mdoc_node
*);
55 static enum rew
rew_dohalt(enum mdoct
, enum mdoc_type
,
56 const struct mdoc_node
*);
57 static int rew_elem(struct mdoc
*, enum mdoct
);
58 static int rew_last(struct mdoc
*,
59 const struct mdoc_node
*);
60 static int rew_sub(enum mdoc_type
, struct mdoc
*,
61 enum mdoct
, int, int);
62 static int swarn(struct mdoc
*, enum mdoc_type
, int,
63 int, const struct mdoc_node
*);
65 const struct mdoc_macro __mdoc_macros
[MDOC_MAX
] = {
66 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ap */
67 { in_line_eoln
, MDOC_PROLOGUE
}, /* Dd */
68 { in_line_eoln
, MDOC_PROLOGUE
}, /* Dt */
69 { in_line_eoln
, MDOC_PROLOGUE
}, /* Os */
70 { blk_full
, 0 }, /* Sh */
71 { blk_full
, 0 }, /* Ss */
72 { in_line_eoln
, 0 }, /* Pp */
73 { blk_part_imp
, MDOC_PARSED
}, /* D1 */
74 { blk_part_imp
, MDOC_PARSED
}, /* Dl */
75 { blk_full
, MDOC_EXPLICIT
}, /* Bd */
76 { blk_exp_close
, MDOC_EXPLICIT
}, /* Ed */
77 { blk_full
, MDOC_EXPLICIT
}, /* Bl */
78 { blk_exp_close
, MDOC_EXPLICIT
}, /* El */
79 { blk_full
, MDOC_PARSED
}, /* It */
80 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ad */
81 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* An */
82 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ar */
83 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Cd */
84 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Cm */
85 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Dv */
86 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Er */
87 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ev */
88 { in_line_eoln
, 0 }, /* Ex */
89 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fa */
90 { in_line_eoln
, 0 }, /* Fd */
91 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fl */
92 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fn */
93 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ft */
94 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ic */
95 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* In */
96 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Li */
97 { blk_full
, 0 }, /* Nd */
98 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Nm */
99 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Op */
100 { obsolete
, 0 }, /* Ot */
101 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Pa */
102 { in_line_eoln
, 0 }, /* Rv */
103 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* St */
104 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Va */
105 { ctx_synopsis
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Vt */
106 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Xr */
107 { in_line_eoln
, 0 }, /* %A */
108 { in_line_eoln
, 0 }, /* %B */
109 { in_line_eoln
, 0 }, /* %D */
110 { in_line_eoln
, 0 }, /* %I */
111 { in_line_eoln
, 0 }, /* %J */
112 { in_line_eoln
, 0 }, /* %N */
113 { in_line_eoln
, 0 }, /* %O */
114 { in_line_eoln
, 0 }, /* %P */
115 { in_line_eoln
, 0 }, /* %R */
116 { in_line_eoln
, 0 }, /* %T */
117 { in_line_eoln
, 0 }, /* %V */
118 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Ac */
119 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Ao */
120 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Aq */
121 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* At */
122 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Bc */
123 { blk_full
, MDOC_EXPLICIT
}, /* Bf */
124 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Bo */
125 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Bq */
126 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Bsx */
127 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Bx */
128 { in_line_eoln
, 0 }, /* Db */
129 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Dc */
130 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Do */
131 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Dq */
132 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Ec */
133 { blk_exp_close
, MDOC_EXPLICIT
}, /* Ef */
134 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Em */
135 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Eo */
136 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fx */
137 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ms */
138 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* No */
139 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ns */
140 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Nx */
141 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ox */
142 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Pc */
143 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_IGNDELIM
}, /* Pf */
144 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Po */
145 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Pq */
146 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Qc */
147 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ql */
148 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Qo */
149 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Qq */
150 { blk_exp_close
, MDOC_EXPLICIT
}, /* Re */
151 { blk_full
, MDOC_EXPLICIT
}, /* Rs */
152 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Sc */
153 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* So */
154 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Sq */
155 { in_line_eoln
, 0 }, /* Sm */
156 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Sx */
157 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Sy */
158 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Tn */
159 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ux */
160 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Xc */
161 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Xo */
162 { blk_full
, MDOC_EXPLICIT
| MDOC_CALLABLE
}, /* Fo */
163 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Fc */
164 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Oo */
165 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Oc */
166 { blk_full
, MDOC_EXPLICIT
}, /* Bk */
167 { blk_exp_close
, MDOC_EXPLICIT
}, /* Ek */
168 { in_line_eoln
, 0 }, /* Bt */
169 { in_line_eoln
, 0 }, /* Hf */
170 { obsolete
, 0 }, /* Fr */
171 { in_line_eoln
, 0 }, /* Ud */
172 { in_line_eoln
, 0 }, /* Lb */
173 { in_line_eoln
, 0 }, /* Lp */
174 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Lk */
175 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Mt */
176 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Brq */
177 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Bro */
178 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Brc */
179 { in_line_eoln
, 0 }, /* %C */
180 { obsolete
, 0 }, /* Es */
181 { obsolete
, 0 }, /* En */
182 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Dx */
183 { in_line_eoln
, 0 }, /* %Q */
184 { in_line_eoln
, 0 }, /* br */
185 { in_line_eoln
, 0 }, /* sp */
186 { in_line_eoln
, 0 }, /* %U */
189 const struct mdoc_macro
* const mdoc_macros
= __mdoc_macros
;
193 swarn(struct mdoc
*mdoc
, enum mdoc_type type
,
194 int line
, int pos
, const struct mdoc_node
*p
)
196 const char *n
, *t
, *tt
;
214 n
= mdoc_macronames
[p
->tok
];
218 n
= mdoc_macronames
[p
->tok
];
222 n
= mdoc_macronames
[p
->tok
];
229 if ( ! (MDOC_IGN_SCOPE
& mdoc
->pflags
))
230 return(mdoc_verr(mdoc
, line
, pos
,
231 "%s scope breaks %s scope of %s",
233 return(mdoc_vwarn(mdoc
, line
, pos
,
234 "%s scope breaks %s scope of %s",
240 * This is called at the end of parsing. It must traverse up the tree,
241 * closing out open [implicit] scopes. Obviously, open explicit scopes
245 mdoc_macroend(struct mdoc
*m
)
249 /* Scan for open explicit scopes. */
251 n
= MDOC_VALID
& m
->last
->flags
? m
->last
->parent
: m
->last
;
253 for ( ; n
; n
= n
->parent
) {
254 if (MDOC_BLOCK
!= n
->type
)
256 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[n
->tok
].flags
))
258 return(mdoc_nerr(m
, n
, EOPEN
));
261 /* Rewind to the first. */
263 return(rew_last(m
, m
->first
));
268 * Look up a macro from within a subsequent context.
271 lookup(enum mdoct from
, const char *p
)
273 /* FIXME: make -diag lists be un-PARSED. */
275 if ( ! (MDOC_PARSED
& mdoc_macros
[from
].flags
))
277 return(lookup_raw(p
));
282 * Lookup a macro following the initial line macro.
285 lookup_raw(const char *p
)
289 if (MDOC_MAX
== (res
= mdoc_hash_find(p
)))
291 if (MDOC_CALLABLE
& mdoc_macros
[res
].flags
)
298 rew_last(struct mdoc
*mdoc
, const struct mdoc_node
*to
)
302 mdoc
->next
= MDOC_NEXT_SIBLING
;
305 while (mdoc
->last
!= to
) {
306 if ( ! mdoc_valid_post(mdoc
))
308 if ( ! mdoc_action_post(mdoc
))
310 mdoc
->last
= mdoc
->last
->parent
;
314 if ( ! mdoc_valid_post(mdoc
))
316 return(mdoc_action_post(mdoc
));
321 * Return the opening macro of a closing one, e.g., `Ec' has `Eo' as its
325 rew_alt(enum mdoct tok
)
369 * Rewind rules. This indicates whether to stop rewinding
370 * (REWIND_HALT) without touching our current scope, stop rewinding and
371 * close our current scope (REWIND_REWIND), or continue (REWIND_NOHALT).
372 * The scope-closing and so on occurs in the various rew_* routines.
375 rew_dohalt(enum mdoct tok
, enum mdoc_type type
,
376 const struct mdoc_node
*p
)
379 if (MDOC_ROOT
== p
->type
)
381 if (MDOC_VALID
& p
->flags
)
382 return(REWIND_NOHALT
);
408 assert(MDOC_TAIL
!= type
);
409 if (type
== p
->type
&& tok
== p
->tok
)
410 return(REWIND_REWIND
);
413 assert(MDOC_TAIL
!= type
);
414 if (type
== p
->type
&& tok
== p
->tok
)
415 return(REWIND_REWIND
);
416 if (MDOC_BODY
== p
->type
&& MDOC_Bl
== p
->tok
)
420 if (type
== p
->type
&& tok
== p
->tok
)
421 return(REWIND_REWIND
);
426 assert(MDOC_TAIL
!= type
);
427 if (type
== p
->type
&& tok
== p
->tok
)
428 return(REWIND_REWIND
);
429 if (MDOC_BODY
== p
->type
&& MDOC_Sh
== p
->tok
)
463 if (type
== p
->type
&& tok
== p
->tok
)
464 return(REWIND_REWIND
);
466 /* Multi-line explicit scope close. */
498 if (type
== p
->type
&& rew_alt(tok
) == p
->tok
)
499 return(REWIND_REWIND
);
506 return(REWIND_NOHALT
);
511 * See if we can break an encountered scope (the rew_dohalt has returned
515 rew_dobreak(enum mdoct tok
, const struct mdoc_node
*p
)
518 assert(MDOC_ROOT
!= p
->type
);
519 if (MDOC_ELEM
== p
->type
)
521 if (MDOC_TEXT
== p
->type
)
523 if (MDOC_VALID
& p
->flags
)
528 return(MDOC_It
== p
->tok
);
530 return(MDOC_Nd
== p
->tok
);
532 return(MDOC_Ss
== p
->tok
);
534 if (MDOC_Nd
== p
->tok
)
536 if (MDOC_Ss
== p
->tok
)
538 return(MDOC_Sh
== p
->tok
);
540 if (MDOC_It
== p
->tok
)
544 if (MDOC_Op
== p
->tok
)
551 if (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
)
552 return(p
->tok
== rew_alt(tok
));
553 else if (MDOC_BLOCK
== p
->type
)
556 return(tok
== p
->tok
);
561 rew_elem(struct mdoc
*mdoc
, enum mdoct tok
)
566 if (MDOC_ELEM
!= n
->type
)
568 assert(MDOC_ELEM
== n
->type
);
569 assert(tok
== n
->tok
);
571 return(rew_last(mdoc
, n
));
576 rew_sub(enum mdoc_type t
, struct mdoc
*m
,
577 enum mdoct tok
, int line
, int ppos
)
583 for (n
= m
->last
; n
; n
= n
->parent
) {
584 c
= rew_dohalt(tok
, t
, n
);
585 if (REWIND_HALT
== c
) {
588 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
))
590 return(mdoc_perr(m
, line
, ppos
, ENOCTX
));
592 if (REWIND_REWIND
== c
)
594 else if (rew_dobreak(tok
, n
))
596 if ( ! swarn(m
, t
, line
, ppos
, n
))
601 if ( ! rew_last(m
, n
))
606 * The current block extends an enclosing block beyond a line
607 * break. Now that the current block ends, close the enclosing
610 if (NULL
!= (n
= n
->pending
)) {
611 assert(MDOC_HEAD
== n
->type
);
612 if ( ! rew_last(m
, n
))
614 if ( ! mdoc_body_alloc(m
, n
->line
, n
->pos
, n
->tok
))
624 append_delims(struct mdoc
*mdoc
, int line
, int *pos
, char *buf
)
635 ac
= mdoc_zargs(mdoc
, line
, pos
, buf
, ARGS_NOWARN
, &p
);
637 if (ARGS_ERROR
== ac
)
639 else if (ARGS_EOLN
== ac
)
641 assert(mdoc_isdelim(p
));
642 if ( ! mdoc_word_alloc(mdoc
, line
, lastarg
, p
))
651 * Close out block partial/full explicit.
654 blk_exp_close(MACRO_PROT_ARGS
)
656 int j
, lastarg
, maxargs
, flushed
, nl
;
661 nl
= MDOC_NEWLINE
& m
->flags
;
672 if ( ! (MDOC_CALLABLE
& mdoc_macros
[tok
].flags
)) {
674 if ( ! mdoc_pwarn(m
, line
, ppos
, ENOLINE
))
677 if ( ! rew_sub(MDOC_BODY
, m
, tok
, line
, ppos
))
679 return(rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
));
682 if ( ! rew_sub(MDOC_BODY
, m
, tok
, line
, ppos
))
686 if ( ! mdoc_tail_alloc(m
, line
, ppos
, rew_alt(tok
)))
689 for (flushed
= j
= 0; ; j
++) {
692 if (j
== maxargs
&& ! flushed
) {
693 if ( ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
698 ac
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
700 if (ARGS_ERROR
== ac
)
702 if (ARGS_PUNCT
== ac
)
707 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
709 if (MDOC_MAX
== ntok
) {
710 if ( ! mdoc_word_alloc(m
, line
, lastarg
, p
))
716 if ( ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
720 if ( ! mdoc_macro(m
, ntok
, line
, lastarg
, pos
, buf
))
725 if ( ! flushed
&& ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
730 return(append_delims(m
, line
, pos
, buf
));
735 in_line(MACRO_PROT_ARGS
)
737 int la
, lastpunct
, cnt
, d
, nc
, nl
;
741 struct mdoc_arg
*arg
;
744 nl
= MDOC_NEWLINE
& m
->flags
;
747 * Whether we allow ignored elements (those without content,
748 * usually because of reserved words) to squeak by.
770 for (arg
= NULL
;; ) {
772 av
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
774 if (ARGV_WORD
== av
) {
787 for (cnt
= 0, lastpunct
= 1;; ) {
789 ac
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
791 if (ARGS_ERROR
== ac
)
795 if (ARGS_PUNCT
== ac
)
798 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
801 * In this case, we've located a submacro and must
802 * execute it. Close out scope, if open. If no
803 * elements have been generated, either create one (nc)
804 * or raise a warning.
807 if (MDOC_MAX
!= ntok
) {
808 if (0 == lastpunct
&& ! rew_elem(m
, tok
))
810 if (nc
&& 0 == cnt
) {
811 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
813 if ( ! rew_last(m
, m
->last
))
815 } else if ( ! nc
&& 0 == cnt
) {
817 if ( ! mdoc_pwarn(m
, line
, ppos
, EIGNE
))
820 if ( ! mdoc_macro(m
, ntok
, line
, la
, pos
, buf
))
824 return(append_delims(m
, line
, pos
, buf
));
828 * Non-quote-enclosed punctuation. Set up our scope, if
829 * a word; rewind the scope, if a delimiter; then append
833 d
= ARGS_QWORD
== ac
? 0 : mdoc_isdelim(p
);
835 if (ARGS_QWORD
!= ac
&& d
) {
836 if (0 == lastpunct
&& ! rew_elem(m
, tok
))
839 } else if (lastpunct
) {
840 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
847 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
851 * `Fl' macros have their scope re-opened with each new
852 * word so that the `-' can be added to each one without
853 * having to parse out spaces.
855 if (0 == lastpunct
&& MDOC_Fl
== tok
) {
856 if ( ! rew_elem(m
, tok
))
862 if (0 == lastpunct
&& ! rew_elem(m
, tok
))
866 * If no elements have been collected and we're allowed to have
867 * empties (nc), open a scope and close it out. Otherwise,
871 if (nc
&& 0 == cnt
) {
872 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
874 if ( ! rew_last(m
, m
->last
))
876 } else if ( ! nc
&& 0 == cnt
) {
878 if ( ! mdoc_pwarn(m
, line
, ppos
, EIGNE
))
884 return(append_delims(m
, line
, pos
, buf
));
889 blk_full(MACRO_PROT_ARGS
)
892 struct mdoc_arg
*arg
;
893 struct mdoc_node
*head
; /* save of head macro */
894 struct mdoc_node
*body
; /* save of body macro */
899 enum margserr ac
, lac
;
903 /* Close out prior implicit scope. */
905 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
)) {
906 if ( ! rew_sub(MDOC_BODY
, m
, tok
, line
, ppos
))
908 if ( ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
913 * This routine accomodates implicitly- and explicitly-scoped
914 * macro openings. Implicit ones first close out prior scope
915 * (seen above). Delay opening the head until necessary to
916 * allow leading punctuation to print. Special consideration
917 * for `It -column', which has phrase-part syntax instead of
918 * regular child nodes.
921 for (arg
= NULL
;; ) {
923 av
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
925 if (ARGV_WORD
== av
) {
939 if ( ! mdoc_block_alloc(m
, line
, ppos
, tok
, arg
))
945 * The `Nd' macro has all arguments in its body: it's a hybrid
946 * of block partial-explicit and full-implicit. Stupid.
949 if (MDOC_Nd
== tok
) {
950 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
953 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
955 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
965 ac
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
967 if (ARGS_ERROR
== ac
)
972 /* Don't emit leading punct. for phrases. */
976 ARGS_PPHRASE
!= ac
&&
979 1 == mdoc_isdelim(p
)) {
980 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
985 /* Always re-open head for phrases. */
990 ARGS_PPHRASE
== ac
) {
991 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
996 if (ARGS_PHRASE
== ac
||
998 ARGS_PPHRASE
== ac
) {
1000 * Special treatment for the last phrase. A
1001 * prior ARGS_PHRASE gets is handled as a
1002 * regular ARGS_PHRASE, but a prior ARGS_PPHRASE
1003 * has special handling.
1005 if (ARGS_PEND
== ac
&& ARGS_ERROR
== lac
)
1007 else if (ARGS_PEND
== ac
&& ARGS_PHRASE
== lac
)
1010 if ( ! phrase(m
, line
, la
, buf
, ac
, pcnt
++))
1012 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1017 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
1019 if (MDOC_MAX
== ntok
) {
1020 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1025 if ( ! mdoc_macro(m
, ntok
, line
, la
, pos
, buf
))
1031 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
1036 if (1 == ppos
&& ! append_delims(m
, line
, pos
, buf
))
1039 /* If we've already opened our body, exit now. */
1046 * If there is an open (i.e., unvalidated) sub-block requiring
1047 * explicit close-out, postpone switching the current block from
1048 * head to body until the rew_sub() call closing out that
1051 for (n
= m
->last
; n
&& n
!= head
; n
= n
->parent
) {
1052 if (MDOC_BLOCK
== n
->type
&&
1053 MDOC_EXPLICIT
& mdoc_macros
[n
->tok
].flags
&&
1054 ! (MDOC_VALID
& n
->flags
)) {
1055 assert( ! (MDOC_ACTED
& n
->flags
));
1062 /* Close out scopes to remain in a consistent state. */
1064 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1066 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1074 blk_part_imp(MACRO_PROT_ARGS
)
1080 struct mdoc_node
*blk
; /* saved block context */
1081 struct mdoc_node
*body
; /* saved body context */
1082 struct mdoc_node
*n
;
1085 * A macro that spans to the end of the line. This is generally
1086 * (but not necessarily) called as the first macro. The block
1087 * has a head as the immediate child, which is always empty,
1088 * followed by zero or more opening punctuation nodes, then the
1089 * body (which may be empty, depending on the macro), then zero
1090 * or more closing punctuation nodes.
1093 if ( ! mdoc_block_alloc(m
, line
, ppos
, tok
, NULL
))
1098 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
1100 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1104 * Open the body scope "on-demand", that is, after we've
1105 * processed all our the leading delimiters (open parenthesis,
1109 for (body
= NULL
; ; ) {
1111 ac
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1113 if (ARGS_ERROR
== ac
)
1115 if (ARGS_EOLN
== ac
)
1117 if (ARGS_PUNCT
== ac
)
1120 if (NULL
== body
&& ARGS_QWORD
!= ac
&&
1121 1 == mdoc_isdelim(p
)) {
1122 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1128 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1133 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
1135 if (MDOC_MAX
== ntok
) {
1136 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1141 if ( ! mdoc_macro(m
, ntok
, line
, la
, pos
, buf
))
1146 /* Clean-ups to leave in a consistent state. */
1149 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1155 * If we can't rewind to our body, then our scope has already
1156 * been closed by another macro (like `Oc' closing `Op'). This
1157 * is ugly behaviour nodding its head to OpenBSD's overwhelming
1158 * crufty use of `Op' breakage.
1160 for (n
= m
->last
; n
; n
= n
->parent
)
1164 if (NULL
== n
&& ! mdoc_nwarn(m
, body
, EIMPBRK
))
1167 if (n
&& ! rew_last(m
, body
))
1170 /* Standard appending of delimiters. */
1172 if (1 == ppos
&& ! append_delims(m
, line
, pos
, buf
))
1175 /* Rewind scope, if applicable. */
1177 if (n
&& ! rew_last(m
, blk
))
1185 blk_part_exp(MACRO_PROT_ARGS
)
1189 struct mdoc_node
*head
; /* keep track of head */
1190 struct mdoc_node
*body
; /* keep track of body */
1194 nl
= MDOC_NEWLINE
& m
->flags
;
1197 * The opening of an explicit macro having zero or more leading
1198 * punctuation nodes; a head with optional single element (the
1199 * case of `Eo'); and a body that may be empty.
1202 if ( ! mdoc_block_alloc(m
, line
, ppos
, tok
, NULL
))
1205 for (head
= body
= NULL
; ; ) {
1207 ac
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1209 if (ARGS_ERROR
== ac
)
1211 if (ARGS_PUNCT
== ac
)
1213 if (ARGS_EOLN
== ac
)
1216 /* Flush out leading punctuation. */
1218 if (NULL
== head
&& ARGS_QWORD
!= ac
&&
1219 1 == mdoc_isdelim(p
)) {
1220 assert(NULL
== body
);
1221 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1227 assert(NULL
== body
);
1228 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
1234 * `Eo' gobbles any data into the head, but most other
1235 * macros just immediately close out and begin the body.
1240 /* No check whether it's a macro! */
1242 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1245 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1247 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1255 assert(NULL
!= head
&& NULL
!= body
);
1257 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
1259 if (MDOC_MAX
== ntok
) {
1260 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1265 if ( ! mdoc_macro(m
, ntok
, line
, la
, pos
, buf
))
1270 /* Clean-up to leave in a consistent state. */
1273 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
1279 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1281 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1286 /* Standard appending of delimiters. */
1290 return(append_delims(m
, line
, pos
, buf
));
1296 in_line_argn(MACRO_PROT_ARGS
)
1298 int la
, flushed
, j
, maxargs
, nl
;
1301 struct mdoc_arg
*arg
;
1305 nl
= MDOC_NEWLINE
& m
->flags
;
1308 * A line macro that has a fixed number of arguments (maxargs).
1309 * Only open the scope once the first non-leading-punctuation is
1310 * found (unless MDOC_IGNDELIM is noted, like in `Pf'), then
1311 * keep it open until the maximum number of arguments are
1333 for (arg
= NULL
; ; ) {
1335 av
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
1337 if (ARGV_WORD
== av
) {
1342 if (ARGV_EOLN
== av
)
1347 mdoc_argv_free(arg
);
1351 for (flushed
= j
= 0; ; ) {
1353 ac
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1355 if (ARGS_ERROR
== ac
)
1357 if (ARGS_PUNCT
== ac
)
1359 if (ARGS_EOLN
== ac
)
1362 if ( ! (MDOC_IGNDELIM
& mdoc_macros
[tok
].flags
) &&
1364 0 == j
&& 1 == mdoc_isdelim(p
)) {
1365 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1369 if ( ! mdoc_elem_alloc(m
, line
, la
, tok
, arg
))
1372 if (j
== maxargs
&& ! flushed
) {
1373 if ( ! rew_elem(m
, tok
))
1378 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
1380 if (MDOC_MAX
!= ntok
) {
1381 if ( ! flushed
&& ! rew_elem(m
, tok
))
1384 if ( ! mdoc_macro(m
, ntok
, line
, la
, pos
, buf
))
1390 if ( ! (MDOC_IGNDELIM
& mdoc_macros
[tok
].flags
) &&
1392 ! flushed
&& mdoc_isdelim(p
)) {
1393 if ( ! rew_elem(m
, tok
))
1399 * XXX: this is a hack to work around groff's ugliness
1400 * as regards `Xr' and extraneous arguments. It should
1401 * ideally be deprecated behaviour, but because this is
1402 * code is no here, it's unlikely to be removed.
1406 if (MDOC_Xr
== tok
&& j
== maxargs
) {
1407 if ( ! mdoc_elem_alloc(m
, line
, la
, MDOC_Ns
, NULL
))
1409 if ( ! rew_elem(m
, MDOC_Ns
))
1414 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1419 if (0 == j
&& ! mdoc_elem_alloc(m
, line
, la
, tok
, arg
))
1422 /* Close out in a consistent state. */
1424 if ( ! flushed
&& ! rew_elem(m
, tok
))
1428 return(append_delims(m
, line
, pos
, buf
));
1433 in_line_eoln(MACRO_PROT_ARGS
)
1438 struct mdoc_arg
*arg
;
1442 assert( ! (MDOC_PARSED
& mdoc_macros
[tok
].flags
));
1444 /* Parse macro arguments. */
1446 for (arg
= NULL
; ; ) {
1448 av
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
1450 if (ARGV_WORD
== av
) {
1454 if (ARGV_EOLN
== av
)
1459 mdoc_argv_free(arg
);
1463 /* Open element scope. */
1465 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
1468 /* Parse argument terms. */
1472 ac
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1474 if (ARGS_ERROR
== ac
)
1476 if (ARGS_EOLN
== ac
)
1479 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
1481 if (MDOC_MAX
== ntok
) {
1482 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1487 if ( ! rew_elem(m
, tok
))
1489 return(mdoc_macro(m
, ntok
, line
, la
, pos
, buf
));
1492 /* Close out (no delimiters). */
1494 return(rew_elem(m
, tok
));
1500 ctx_synopsis(MACRO_PROT_ARGS
)
1504 nl
= MDOC_NEWLINE
& m
->flags
;
1506 /* If we're not in the SYNOPSIS, go straight to in-line. */
1507 if (SEC_SYNOPSIS
!= m
->lastsec
)
1508 return(in_line(m
, tok
, line
, ppos
, pos
, buf
));
1510 /* If we're a nested call, same place. */
1512 return(in_line(m
, tok
, line
, ppos
, pos
, buf
));
1515 * XXX: this will open a block scope; however, if later we end
1516 * up formatting the block scope, then child nodes will inherit
1517 * the formatting. Be careful.
1520 return(blk_part_imp(m
, tok
, line
, ppos
, pos
, buf
));
1526 obsolete(MACRO_PROT_ARGS
)
1529 return(mdoc_pwarn(m
, line
, ppos
, EOBS
));
1534 * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
1535 * They're unusual because they're basically free-form text until a
1536 * macro is encountered.
1539 phrase(struct mdoc
*m
, int line
, int ppos
, char *buf
,
1540 enum margserr ac
, int count
)
1547 assert(ARGS_PHRASE
== ac
||
1549 ARGS_PPHRASE
== ac
);
1551 if (count
&& ARGS_PPHRASE
== ac
)
1552 return(mdoc_word_alloc(m
, line
, ppos
, &buf
[ppos
]));
1554 for (pos
= ppos
; ; ) {
1557 /* Note: no calling context! */
1558 aac
= mdoc_zargs(m
, line
, &pos
, buf
, 0, &p
);
1560 if (ARGS_ERROR
== aac
)
1562 if (ARGS_EOLN
== aac
)
1565 ntok
= ARGS_QWORD
== aac
|| ARGS_PEND
== ac
?
1566 MDOC_MAX
: lookup_raw(p
);
1568 if (MDOC_MAX
== ntok
) {
1569 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1574 if ( ! mdoc_macro(m
, ntok
, line
, la
, &pos
, buf
))
1576 return(append_delims(m
, line
, &pos
, buf
));