]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_macro.c
1 /* $Id: mdoc_macro.c,v 1.79 2010/05/31 23:49:16 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 #include "libmandoc.h"
38 static int blk_full(MACRO_PROT_ARGS
);
39 static int blk_exp_close(MACRO_PROT_ARGS
);
40 static int blk_part_exp(MACRO_PROT_ARGS
);
41 static int blk_part_imp(MACRO_PROT_ARGS
);
42 static int ctx_synopsis(MACRO_PROT_ARGS
);
43 static int in_line_eoln(MACRO_PROT_ARGS
);
44 static int in_line_argn(MACRO_PROT_ARGS
);
45 static int in_line(MACRO_PROT_ARGS
);
46 static int obsolete(MACRO_PROT_ARGS
);
47 static int phrase_ta(MACRO_PROT_ARGS
);
49 static int append_delims(struct mdoc
*,
51 static enum mdoct
lookup(enum mdoct
, const char *);
52 static enum mdoct
lookup_raw(const char *);
53 static int phrase(struct mdoc
*, int, int, char *);
54 static enum mdoct
rew_alt(enum mdoct
);
55 static int rew_dobreak(enum mdoct
,
56 const struct mdoc_node
*);
57 static enum rew
rew_dohalt(enum mdoct
, enum mdoc_type
,
58 const struct mdoc_node
*);
59 static int rew_elem(struct mdoc
*, enum mdoct
);
60 static int rew_last(struct mdoc
*,
61 const struct mdoc_node
*);
62 static int rew_sub(enum mdoc_type
, struct mdoc
*,
63 enum mdoct
, int, int);
64 static int swarn(struct mdoc
*, enum mdoc_type
, int,
65 int, const struct mdoc_node
*);
67 const struct mdoc_macro __mdoc_macros
[MDOC_MAX
] = {
68 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ap */
69 { in_line_eoln
, MDOC_PROLOGUE
}, /* Dd */
70 { in_line_eoln
, MDOC_PROLOGUE
}, /* Dt */
71 { in_line_eoln
, MDOC_PROLOGUE
}, /* Os */
72 { blk_full
, 0 }, /* Sh */
73 { blk_full
, 0 }, /* Ss */
74 { in_line_eoln
, 0 }, /* Pp */
75 { blk_part_imp
, MDOC_PARSED
}, /* D1 */
76 { blk_part_imp
, MDOC_PARSED
}, /* Dl */
77 { blk_full
, MDOC_EXPLICIT
}, /* Bd */
78 { blk_exp_close
, MDOC_EXPLICIT
}, /* Ed */
79 { blk_full
, MDOC_EXPLICIT
}, /* Bl */
80 { blk_exp_close
, MDOC_EXPLICIT
}, /* El */
81 { blk_full
, MDOC_PARSED
}, /* It */
82 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ad */
83 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* An */
84 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ar */
85 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Cd */
86 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Cm */
87 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Dv */
88 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Er */
89 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ev */
90 { in_line_eoln
, 0 }, /* Ex */
91 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fa */
92 { in_line_eoln
, 0 }, /* Fd */
93 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fl */
94 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fn */
95 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ft */
96 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ic */
97 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* In */
98 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Li */
99 { blk_full
, 0 }, /* Nd */
100 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Nm */
101 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Op */
102 { obsolete
, 0 }, /* Ot */
103 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Pa */
104 { in_line_eoln
, 0 }, /* Rv */
105 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* St */
106 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Va */
107 { ctx_synopsis
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Vt */
108 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Xr */
109 { in_line_eoln
, 0 }, /* %A */
110 { in_line_eoln
, 0 }, /* %B */
111 { in_line_eoln
, 0 }, /* %D */
112 { in_line_eoln
, 0 }, /* %I */
113 { in_line_eoln
, 0 }, /* %J */
114 { in_line_eoln
, 0 }, /* %N */
115 { in_line_eoln
, 0 }, /* %O */
116 { in_line_eoln
, 0 }, /* %P */
117 { in_line_eoln
, 0 }, /* %R */
118 { in_line_eoln
, 0 }, /* %T */
119 { in_line_eoln
, 0 }, /* %V */
120 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Ac */
121 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Ao */
122 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Aq */
123 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* At */
124 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Bc */
125 { blk_full
, MDOC_EXPLICIT
}, /* Bf */
126 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Bo */
127 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Bq */
128 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Bsx */
129 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Bx */
130 { in_line_eoln
, 0 }, /* Db */
131 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Dc */
132 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Do */
133 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Dq */
134 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Ec */
135 { blk_exp_close
, MDOC_EXPLICIT
}, /* Ef */
136 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Em */
137 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Eo */
138 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fx */
139 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ms */
140 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* No */
141 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ns */
142 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Nx */
143 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ox */
144 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Pc */
145 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_IGNDELIM
}, /* Pf */
146 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Po */
147 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Pq */
148 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Qc */
149 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ql */
150 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Qo */
151 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Qq */
152 { blk_exp_close
, MDOC_EXPLICIT
}, /* Re */
153 { blk_full
, MDOC_EXPLICIT
}, /* Rs */
154 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Sc */
155 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* So */
156 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Sq */
157 { in_line_eoln
, 0 }, /* Sm */
158 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Sx */
159 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Sy */
160 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Tn */
161 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ux */
162 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Xc */
163 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Xo */
164 { blk_full
, MDOC_EXPLICIT
| MDOC_CALLABLE
}, /* Fo */
165 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Fc */
166 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Oo */
167 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Oc */
168 { blk_full
, MDOC_EXPLICIT
}, /* Bk */
169 { blk_exp_close
, MDOC_EXPLICIT
}, /* Ek */
170 { in_line_eoln
, 0 }, /* Bt */
171 { in_line_eoln
, 0 }, /* Hf */
172 { obsolete
, 0 }, /* Fr */
173 { in_line_eoln
, 0 }, /* Ud */
174 { in_line
, 0 }, /* Lb */
175 { in_line_eoln
, 0 }, /* Lp */
176 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Lk */
177 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Mt */
178 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Brq */
179 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Bro */
180 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Brc */
181 { in_line_eoln
, 0 }, /* %C */
182 { obsolete
, 0 }, /* Es */
183 { obsolete
, 0 }, /* En */
184 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Dx */
185 { in_line_eoln
, 0 }, /* %Q */
186 { in_line_eoln
, 0 }, /* br */
187 { in_line_eoln
, 0 }, /* sp */
188 { in_line_eoln
, 0 }, /* %U */
189 { phrase_ta
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ta */
192 const struct mdoc_macro
* const mdoc_macros
= __mdoc_macros
;
196 swarn(struct mdoc
*mdoc
, enum mdoc_type type
,
197 int line
, int pos
, const struct mdoc_node
*p
)
199 const char *n
, *t
, *tt
;
218 n
= mdoc_macronames
[p
->tok
];
222 n
= mdoc_macronames
[p
->tok
];
226 n
= mdoc_macronames
[p
->tok
];
233 ec
= (MDOC_IGN_SCOPE
& mdoc
->pflags
) ?
234 MANDOCERR_SCOPE
: MANDOCERR_SYNTSCOPE
;
236 return(mdoc_vmsg(mdoc
, ec
, line
, pos
,
237 "%s scope breaks %s of %s",
243 * This is called at the end of parsing. It must traverse up the tree,
244 * closing out open [implicit] scopes. Obviously, open explicit scopes
248 mdoc_macroend(struct mdoc
*m
)
252 /* Scan for open explicit scopes. */
254 n
= MDOC_VALID
& m
->last
->flags
? m
->last
->parent
: m
->last
;
256 for ( ; n
; n
= n
->parent
) {
257 if (MDOC_BLOCK
!= n
->type
)
259 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[n
->tok
].flags
))
261 mdoc_nmsg(m
, n
, MANDOCERR_SYNTSCOPE
);
265 /* Rewind to the first. */
267 return(rew_last(m
, m
->first
));
272 * Look up a macro from within a subsequent context.
275 lookup(enum mdoct from
, const char *p
)
277 /* FIXME: make -diag lists be un-PARSED. */
279 if ( ! (MDOC_PARSED
& mdoc_macros
[from
].flags
))
281 return(lookup_raw(p
));
286 * Lookup a macro following the initial line macro.
289 lookup_raw(const char *p
)
293 if (MDOC_MAX
== (res
= mdoc_hash_find(p
)))
295 if (MDOC_CALLABLE
& mdoc_macros
[res
].flags
)
302 rew_last(struct mdoc
*mdoc
, const struct mdoc_node
*to
)
306 mdoc
->next
= MDOC_NEXT_SIBLING
;
309 while (mdoc
->last
!= to
) {
310 if ( ! mdoc_valid_post(mdoc
))
312 if ( ! mdoc_action_post(mdoc
))
314 mdoc
->last
= mdoc
->last
->parent
;
318 if ( ! mdoc_valid_post(mdoc
))
320 return(mdoc_action_post(mdoc
));
325 * Return the opening macro of a closing one, e.g., `Ec' has `Eo' as its
329 rew_alt(enum mdoct tok
)
373 * Rewind rules. This indicates whether to stop rewinding
374 * (REWIND_HALT) without touching our current scope, stop rewinding and
375 * close our current scope (REWIND_REWIND), or continue (REWIND_NOHALT).
376 * The scope-closing and so on occurs in the various rew_* routines.
379 rew_dohalt(enum mdoct tok
, enum mdoc_type type
,
380 const struct mdoc_node
*p
)
383 if (MDOC_ROOT
== p
->type
)
385 if (MDOC_VALID
& p
->flags
)
386 return(REWIND_NOHALT
);
412 assert(MDOC_TAIL
!= type
);
413 if (type
== p
->type
&& tok
== p
->tok
)
414 return(REWIND_REWIND
);
417 assert(MDOC_TAIL
!= type
);
418 if (type
== p
->type
&& tok
== p
->tok
)
419 return(REWIND_REWIND
);
420 if (MDOC_BODY
== p
->type
&& MDOC_Bl
== p
->tok
)
424 if (type
== p
->type
&& tok
== p
->tok
)
425 return(REWIND_REWIND
);
430 assert(MDOC_TAIL
!= type
);
431 if (type
== p
->type
&& tok
== p
->tok
)
432 return(REWIND_REWIND
);
433 if (MDOC_BODY
== p
->type
&& MDOC_Sh
== p
->tok
)
467 if (type
== p
->type
&& tok
== p
->tok
)
468 return(REWIND_REWIND
);
470 /* Multi-line explicit scope close. */
502 if (type
== p
->type
&& rew_alt(tok
) == p
->tok
)
503 return(REWIND_REWIND
);
510 return(REWIND_NOHALT
);
515 * See if we can break an encountered scope (the rew_dohalt has returned
519 rew_dobreak(enum mdoct tok
, const struct mdoc_node
*p
)
522 assert(MDOC_ROOT
!= p
->type
);
523 if (MDOC_ELEM
== p
->type
)
525 if (MDOC_TEXT
== p
->type
)
527 if (MDOC_VALID
& p
->flags
)
532 return(MDOC_It
== p
->tok
);
534 return(MDOC_Nd
== p
->tok
);
536 return(MDOC_Ss
== p
->tok
);
538 if (MDOC_Nd
== p
->tok
)
540 if (MDOC_Ss
== p
->tok
)
542 return(MDOC_Sh
== p
->tok
);
544 if (MDOC_It
== p
->tok
)
548 if (MDOC_Op
== p
->tok
)
555 if (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
)
556 return(p
->tok
== rew_alt(tok
));
557 else if (MDOC_BLOCK
== p
->type
)
560 return(tok
== p
->tok
);
565 rew_elem(struct mdoc
*mdoc
, enum mdoct tok
)
570 if (MDOC_ELEM
!= n
->type
)
572 assert(MDOC_ELEM
== n
->type
);
573 assert(tok
== n
->tok
);
575 return(rew_last(mdoc
, n
));
580 rew_sub(enum mdoc_type t
, struct mdoc
*m
,
581 enum mdoct tok
, int line
, int ppos
)
587 for (n
= m
->last
; n
; n
= n
->parent
) {
588 c
= rew_dohalt(tok
, t
, n
);
589 if (REWIND_HALT
== c
) {
592 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
))
594 /* FIXME: shouldn't raise an error */
595 mdoc_pmsg(m
, line
, ppos
, MANDOCERR_SYNTNOSCOPE
);
598 if (REWIND_REWIND
== c
)
600 else if (rew_dobreak(tok
, n
))
602 if ( ! swarn(m
, t
, line
, ppos
, n
))
607 if ( ! rew_last(m
, n
))
612 * The current block extends an enclosing block beyond a line
613 * break. Now that the current block ends, close the enclosing
616 if (NULL
!= (n
= n
->pending
)) {
617 assert(MDOC_HEAD
== n
->type
);
618 if ( ! rew_last(m
, n
))
620 if ( ! mdoc_body_alloc(m
, n
->line
, n
->pos
, n
->tok
))
630 append_delims(struct mdoc
*m
, int line
, int *pos
, char *buf
)
636 if ('\0' == buf
[*pos
])
641 ac
= mdoc_zargs(m
, line
, pos
, buf
, ARGS_NOWARN
, &p
);
643 if (ARGS_ERROR
== ac
)
645 else if (ARGS_EOLN
== ac
)
648 assert(DELIM_NONE
!= mdoc_isdelim(p
));
649 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
653 * If we encounter end-of-sentence symbols, then trigger
656 * XXX: it's easy to allow this to propogate outward to
657 * the last symbol, such that `. )' will cause the
658 * correct double-spacing. However, (1) groff isn't
659 * smart enough to do this and (2) it would require
660 * knowing which symbols break this behaviour, for
661 * example, `. ;' shouldn't propogate the double-space.
663 if (mandoc_eos(p
, strlen(p
)))
664 m
->last
->flags
|= MDOC_EOS
;
672 * Close out block partial/full explicit.
675 blk_exp_close(MACRO_PROT_ARGS
)
677 int j
, lastarg
, maxargs
, flushed
, nl
;
682 nl
= MDOC_NEWLINE
& m
->flags
;
693 if ( ! (MDOC_CALLABLE
& mdoc_macros
[tok
].flags
)) {
694 /* FIXME: do this in validate */
696 if ( ! mdoc_pmsg(m
, line
, ppos
, MANDOCERR_ARGSLOST
))
699 if ( ! rew_sub(MDOC_BODY
, m
, tok
, line
, ppos
))
701 return(rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
));
704 if ( ! rew_sub(MDOC_BODY
, m
, tok
, line
, ppos
))
708 if ( ! mdoc_tail_alloc(m
, line
, ppos
, rew_alt(tok
)))
711 for (flushed
= j
= 0; ; j
++) {
714 if (j
== maxargs
&& ! flushed
) {
715 if ( ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
720 ac
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
722 if (ARGS_ERROR
== ac
)
724 if (ARGS_PUNCT
== ac
)
729 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
731 if (MDOC_MAX
== ntok
) {
732 if ( ! mdoc_word_alloc(m
, line
, lastarg
, p
))
738 if ( ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
742 if ( ! mdoc_macro(m
, ntok
, line
, lastarg
, pos
, buf
))
747 if ( ! flushed
&& ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
752 return(append_delims(m
, line
, pos
, buf
));
757 in_line(MACRO_PROT_ARGS
)
759 int la
, scope
, cnt
, nc
, nl
;
764 struct mdoc_arg
*arg
;
767 nl
= MDOC_NEWLINE
& m
->flags
;
770 * Whether we allow ignored elements (those without content,
771 * usually because of reserved words) to squeak by.
793 for (arg
= NULL
;; ) {
795 av
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
797 if (ARGV_WORD
== av
) {
810 for (cnt
= scope
= 0;; ) {
812 ac
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
814 if (ARGS_ERROR
== ac
)
818 if (ARGS_PUNCT
== ac
)
821 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
824 * In this case, we've located a submacro and must
825 * execute it. Close out scope, if open. If no
826 * elements have been generated, either create one (nc)
827 * or raise a warning.
830 if (MDOC_MAX
!= ntok
) {
831 if (scope
&& ! rew_elem(m
, tok
))
833 if (nc
&& 0 == cnt
) {
834 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
836 if ( ! rew_last(m
, m
->last
))
838 } else if ( ! nc
&& 0 == cnt
) {
840 if ( ! mdoc_pmsg(m
, line
, ppos
, MANDOCERR_MACROEMPTY
))
843 if ( ! mdoc_macro(m
, ntok
, line
, la
, pos
, buf
))
847 return(append_delims(m
, line
, pos
, buf
));
851 * Non-quote-enclosed punctuation. Set up our scope, if
852 * a word; rewind the scope, if a delimiter; then append
856 d
= ARGS_QWORD
== ac
? DELIM_NONE
: mdoc_isdelim(p
);
858 if (DELIM_NONE
!= d
) {
860 * If we encounter closing punctuation, no word
861 * has been omitted, no scope is open, and we're
862 * allowed to have an empty element, then start
863 * a new scope. `Ar', `Fl', and `Li', only do
864 * this once per invocation. There may be more
865 * of these (all of them?).
867 if (0 == cnt
&& (nc
|| MDOC_Li
== tok
) &&
868 DELIM_CLOSE
== d
&& ! scope
) {
869 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
871 if (MDOC_Ar
== tok
|| MDOC_Li
== tok
||
877 * Close out our scope, if one is open, before
880 if (scope
&& ! rew_elem(m
, tok
))
883 } else if ( ! scope
) {
884 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
891 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
895 * `Fl' macros have their scope re-opened with each new
896 * word so that the `-' can be added to each one without
897 * having to parse out spaces.
899 if (scope
&& MDOC_Fl
== tok
) {
900 if ( ! rew_elem(m
, tok
))
906 if (scope
&& ! rew_elem(m
, tok
))
910 * If no elements have been collected and we're allowed to have
911 * empties (nc), open a scope and close it out. Otherwise,
915 if (nc
&& 0 == cnt
) {
916 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
918 if ( ! rew_last(m
, m
->last
))
920 } else if ( ! nc
&& 0 == cnt
) {
922 if ( ! mdoc_pmsg(m
, line
, ppos
, MANDOCERR_MACROEMPTY
))
928 return(append_delims(m
, line
, pos
, buf
));
933 blk_full(MACRO_PROT_ARGS
)
936 struct mdoc_arg
*arg
;
937 struct mdoc_node
*head
; /* save of head macro */
938 struct mdoc_node
*body
; /* save of body macro */
944 enum margserr ac
, lac
;
948 nl
= MDOC_NEWLINE
& m
->flags
;
950 /* Close out prior implicit scope. */
952 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
)) {
953 if ( ! rew_sub(MDOC_BODY
, m
, tok
, line
, ppos
))
955 if ( ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
960 * This routine accomodates implicitly- and explicitly-scoped
961 * macro openings. Implicit ones first close out prior scope
962 * (seen above). Delay opening the head until necessary to
963 * allow leading punctuation to print. Special consideration
964 * for `It -column', which has phrase-part syntax instead of
965 * regular child nodes.
968 for (arg
= NULL
;; ) {
970 av
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
972 if (ARGV_WORD
== av
) {
986 if ( ! mdoc_block_alloc(m
, line
, ppos
, tok
, arg
))
992 * The `Nd' macro has all arguments in its body: it's a hybrid
993 * of block partial-explicit and full-implicit. Stupid.
996 if (MDOC_Nd
== tok
) {
997 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
1000 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1002 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1011 /* Initialise last-phrase-type with ARGS_PEND. */
1012 lac
= ARGS_ERROR
== ac
? ARGS_PEND
: ac
;
1013 ac
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1015 if (ARGS_ERROR
== ac
)
1018 if (ARGS_EOLN
== ac
) {
1019 if (ARGS_PPHRASE
!= lac
&& ARGS_PHRASE
!= lac
)
1022 * This is necessary: if the last token on a
1023 * line is a `Ta' or tab, then we'll get
1024 * ARGS_EOLN, so we must be smart enough to
1025 * reopen our scope if the last parse was a
1026 * phrase or partial phrase.
1028 if ( ! rew_sub(MDOC_BODY
, m
, tok
, line
, ppos
))
1030 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1037 * Emit leading punctuation (i.e., punctuation before
1038 * the MDOC_HEAD) for non-phrase types.
1043 ARGS_PHRASE
!= ac
&&
1044 ARGS_PPHRASE
!= ac
&&
1046 DELIM_OPEN
== mdoc_isdelim(p
)) {
1047 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1052 /* Open a head if one hasn't been opened. */
1055 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
1060 if (ARGS_PHRASE
== ac
||
1062 ARGS_PPHRASE
== ac
) {
1064 * If we haven't opened a body yet, rewind the
1065 * head; if we have, rewind that instead.
1068 mtt
= body
? MDOC_BODY
: MDOC_HEAD
;
1069 if ( ! rew_sub(mtt
, m
, tok
, line
, ppos
))
1072 /* Then allocate our body context. */
1074 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1079 * Process phrases: set whether we're in a
1080 * partial-phrase (this effects line handling)
1081 * then call down into the phrase parser.
1084 if (ARGS_PPHRASE
== ac
)
1085 m
->flags
|= MDOC_PPHRASE
;
1086 if (ARGS_PEND
== ac
&& ARGS_PPHRASE
== lac
)
1087 m
->flags
|= MDOC_PPHRASE
;
1089 if ( ! phrase(m
, line
, la
, buf
))
1092 m
->flags
&= ~MDOC_PPHRASE
;
1096 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
1098 if (MDOC_MAX
== ntok
) {
1099 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1104 if ( ! mdoc_macro(m
, ntok
, line
, la
, pos
, buf
))
1110 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
1115 if (nl
&& ! append_delims(m
, line
, pos
, buf
))
1118 /* If we've already opened our body, exit now. */
1125 * If there is an open (i.e., unvalidated) sub-block requiring
1126 * explicit close-out, postpone switching the current block from
1127 * head to body until the rew_sub() call closing out that
1130 for (n
= m
->last
; n
&& n
!= head
; n
= n
->parent
) {
1131 if (MDOC_BLOCK
== n
->type
&&
1132 MDOC_EXPLICIT
& mdoc_macros
[n
->tok
].flags
&&
1133 ! (MDOC_VALID
& n
->flags
)) {
1134 assert( ! (MDOC_ACTED
& n
->flags
));
1141 /* Close out scopes to remain in a consistent state. */
1143 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1145 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1149 if ( ! (MDOC_FREECOL
& m
->flags
))
1152 if ( ! rew_sub(MDOC_BODY
, m
, tok
, line
, ppos
))
1154 if ( ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
1157 m
->flags
&= ~MDOC_FREECOL
;
1163 blk_part_imp(MACRO_PROT_ARGS
)
1169 struct mdoc_node
*blk
; /* saved block context */
1170 struct mdoc_node
*body
; /* saved body context */
1171 struct mdoc_node
*n
;
1173 nl
= MDOC_NEWLINE
& m
->flags
;
1176 * A macro that spans to the end of the line. This is generally
1177 * (but not necessarily) called as the first macro. The block
1178 * has a head as the immediate child, which is always empty,
1179 * followed by zero or more opening punctuation nodes, then the
1180 * body (which may be empty, depending on the macro), then zero
1181 * or more closing punctuation nodes.
1184 if ( ! mdoc_block_alloc(m
, line
, ppos
, tok
, NULL
))
1189 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
1191 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1195 * Open the body scope "on-demand", that is, after we've
1196 * processed all our the leading delimiters (open parenthesis,
1200 for (body
= NULL
; ; ) {
1202 ac
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1204 if (ARGS_ERROR
== ac
)
1206 if (ARGS_EOLN
== ac
)
1208 if (ARGS_PUNCT
== ac
)
1211 if (NULL
== body
&& ARGS_QWORD
!= ac
&&
1212 DELIM_OPEN
== mdoc_isdelim(p
)) {
1213 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1219 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1224 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
1226 if (MDOC_MAX
== ntok
) {
1227 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1232 if ( ! mdoc_macro(m
, ntok
, line
, la
, pos
, buf
))
1237 /* Clean-ups to leave in a consistent state. */
1240 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1245 for (n
= body
->child
; n
&& n
->next
; n
= n
->next
)
1249 * End of sentence spacing: if the last node is a text node and
1250 * has a trailing period, then mark it as being end-of-sentence.
1253 if (n
&& MDOC_TEXT
== n
->type
&& n
->string
)
1254 if (mandoc_eos(n
->string
, strlen(n
->string
)))
1255 n
->flags
|= MDOC_EOS
;
1257 /* Up-propogate the end-of-space flag. */
1259 if (n
&& (MDOC_EOS
& n
->flags
)) {
1260 body
->flags
|= MDOC_EOS
;
1261 body
->parent
->flags
|= MDOC_EOS
;
1265 * If we can't rewind to our body, then our scope has already
1266 * been closed by another macro (like `Oc' closing `Op'). This
1267 * is ugly behaviour nodding its head to OpenBSD's overwhelming
1268 * crufty use of `Op' breakage.
1270 * FIXME - this should be ifdef'd OpenBSD?
1272 for (n
= m
->last
; n
; n
= n
->parent
)
1276 if (NULL
== n
&& ! mdoc_nmsg(m
, body
, MANDOCERR_SCOPE
))
1279 if (n
&& ! rew_last(m
, body
))
1282 /* Standard appending of delimiters. */
1284 if (nl
&& ! append_delims(m
, line
, pos
, buf
))
1287 /* Rewind scope, if applicable. */
1289 if (n
&& ! rew_last(m
, blk
))
1297 blk_part_exp(MACRO_PROT_ARGS
)
1301 struct mdoc_node
*head
; /* keep track of head */
1302 struct mdoc_node
*body
; /* keep track of body */
1306 nl
= MDOC_NEWLINE
& m
->flags
;
1309 * The opening of an explicit macro having zero or more leading
1310 * punctuation nodes; a head with optional single element (the
1311 * case of `Eo'); and a body that may be empty.
1314 if ( ! mdoc_block_alloc(m
, line
, ppos
, tok
, NULL
))
1317 for (head
= body
= NULL
; ; ) {
1319 ac
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1321 if (ARGS_ERROR
== ac
)
1323 if (ARGS_PUNCT
== ac
)
1325 if (ARGS_EOLN
== ac
)
1328 /* Flush out leading punctuation. */
1330 if (NULL
== head
&& ARGS_QWORD
!= ac
&&
1331 DELIM_OPEN
== mdoc_isdelim(p
)) {
1332 assert(NULL
== body
);
1333 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1339 assert(NULL
== body
);
1340 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
1346 * `Eo' gobbles any data into the head, but most other
1347 * macros just immediately close out and begin the body.
1352 /* No check whether it's a macro! */
1354 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1357 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1359 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1367 assert(NULL
!= head
&& NULL
!= body
);
1369 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
1371 if (MDOC_MAX
== ntok
) {
1372 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1377 if ( ! mdoc_macro(m
, ntok
, line
, la
, pos
, buf
))
1382 /* Clean-up to leave in a consistent state. */
1385 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
1391 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1393 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1398 /* Standard appending of delimiters. */
1402 return(append_delims(m
, line
, pos
, buf
));
1408 in_line_argn(MACRO_PROT_ARGS
)
1410 int la
, flushed
, j
, maxargs
, nl
;
1413 struct mdoc_arg
*arg
;
1417 nl
= MDOC_NEWLINE
& m
->flags
;
1420 * A line macro that has a fixed number of arguments (maxargs).
1421 * Only open the scope once the first non-leading-punctuation is
1422 * found (unless MDOC_IGNDELIM is noted, like in `Pf'), then
1423 * keep it open until the maximum number of arguments are
1445 for (arg
= NULL
; ; ) {
1447 av
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
1449 if (ARGV_WORD
== av
) {
1454 if (ARGV_EOLN
== av
)
1459 mdoc_argv_free(arg
);
1463 for (flushed
= j
= 0; ; ) {
1465 ac
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1467 if (ARGS_ERROR
== ac
)
1469 if (ARGS_PUNCT
== ac
)
1471 if (ARGS_EOLN
== ac
)
1474 if ( ! (MDOC_IGNDELIM
& mdoc_macros
[tok
].flags
) &&
1476 0 == j
&& DELIM_OPEN
== mdoc_isdelim(p
)) {
1477 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1481 if ( ! mdoc_elem_alloc(m
, line
, la
, tok
, arg
))
1484 if (j
== maxargs
&& ! flushed
) {
1485 if ( ! rew_elem(m
, tok
))
1490 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
1492 if (MDOC_MAX
!= ntok
) {
1493 if ( ! flushed
&& ! rew_elem(m
, tok
))
1496 if ( ! mdoc_macro(m
, ntok
, line
, la
, pos
, buf
))
1502 if ( ! (MDOC_IGNDELIM
& mdoc_macros
[tok
].flags
) &&
1505 DELIM_NONE
!= mdoc_isdelim(p
)) {
1506 if ( ! rew_elem(m
, tok
))
1512 * XXX: this is a hack to work around groff's ugliness
1513 * as regards `Xr' and extraneous arguments. It should
1514 * ideally be deprecated behaviour, but because this is
1515 * code is no here, it's unlikely to be removed.
1519 if (MDOC_Xr
== tok
&& j
== maxargs
) {
1520 if ( ! mdoc_elem_alloc(m
, line
, la
, MDOC_Ns
, NULL
))
1522 if ( ! rew_elem(m
, MDOC_Ns
))
1527 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1532 if (0 == j
&& ! mdoc_elem_alloc(m
, line
, la
, tok
, arg
))
1535 /* Close out in a consistent state. */
1537 if ( ! flushed
&& ! rew_elem(m
, tok
))
1541 return(append_delims(m
, line
, pos
, buf
));
1546 in_line_eoln(MACRO_PROT_ARGS
)
1551 struct mdoc_arg
*arg
;
1555 assert( ! (MDOC_PARSED
& mdoc_macros
[tok
].flags
));
1557 /* Parse macro arguments. */
1559 for (arg
= NULL
; ; ) {
1561 av
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
1563 if (ARGV_WORD
== av
) {
1567 if (ARGV_EOLN
== av
)
1572 mdoc_argv_free(arg
);
1576 /* Open element scope. */
1578 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
1581 /* Parse argument terms. */
1585 ac
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1587 if (ARGS_ERROR
== ac
)
1589 if (ARGS_EOLN
== ac
)
1592 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
1594 if (MDOC_MAX
== ntok
) {
1595 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1600 if ( ! rew_elem(m
, tok
))
1602 return(mdoc_macro(m
, ntok
, line
, la
, pos
, buf
));
1605 /* Close out (no delimiters). */
1607 return(rew_elem(m
, tok
));
1613 ctx_synopsis(MACRO_PROT_ARGS
)
1617 nl
= MDOC_NEWLINE
& m
->flags
;
1619 /* If we're not in the SYNOPSIS, go straight to in-line. */
1620 if (SEC_SYNOPSIS
!= m
->lastsec
)
1621 return(in_line(m
, tok
, line
, ppos
, pos
, buf
));
1623 /* If we're a nested call, same place. */
1625 return(in_line(m
, tok
, line
, ppos
, pos
, buf
));
1628 * XXX: this will open a block scope; however, if later we end
1629 * up formatting the block scope, then child nodes will inherit
1630 * the formatting. Be careful.
1633 return(blk_part_imp(m
, tok
, line
, ppos
, pos
, buf
));
1639 obsolete(MACRO_PROT_ARGS
)
1642 return(mdoc_pmsg(m
, line
, ppos
, MANDOCERR_MACROOBS
));
1647 * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
1648 * They're unusual because they're basically free-form text until a
1649 * macro is encountered.
1652 phrase(struct mdoc
*m
, int line
, int ppos
, char *buf
)
1659 for (pos
= ppos
; ; ) {
1662 ac
= mdoc_zargs(m
, line
, &pos
, buf
, 0, &p
);
1664 if (ARGS_ERROR
== ac
)
1666 if (ARGS_EOLN
== ac
)
1669 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup_raw(p
);
1671 if (MDOC_MAX
== ntok
) {
1672 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1677 if ( ! mdoc_macro(m
, ntok
, line
, la
, &pos
, buf
))
1679 return(append_delims(m
, line
, &pos
, buf
));
1688 phrase_ta(MACRO_PROT_ARGS
)
1696 * FIXME: this is overly restrictive: if the `Ta' is unexpected,
1697 * it should simply error out with ARGSLOST.
1700 if ( ! rew_sub(MDOC_BODY
, m
, MDOC_It
, line
, ppos
))
1702 if ( ! mdoc_body_alloc(m
, line
, ppos
, MDOC_It
))
1707 ac
= mdoc_zargs(m
, line
, pos
, buf
, 0, &p
);
1709 if (ARGS_ERROR
== ac
)
1711 if (ARGS_EOLN
== ac
)
1714 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup_raw(p
);
1716 if (MDOC_MAX
== ntok
) {
1717 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1722 if ( ! mdoc_macro(m
, ntok
, line
, la
, pos
, buf
))
1724 return(append_delims(m
, line
, pos
, buf
));