]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_macro.c
1 /* $Id: mdoc_macro.c,v 1.67 2010/05/15 08:54:04 schwarze 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.
29 #include "libmandoc.h"
37 static int blk_full(MACRO_PROT_ARGS
);
38 static int blk_exp_close(MACRO_PROT_ARGS
);
39 static int blk_part_exp(MACRO_PROT_ARGS
);
40 static int blk_part_imp(MACRO_PROT_ARGS
);
41 static int ctx_synopsis(MACRO_PROT_ARGS
);
42 static int in_line_eoln(MACRO_PROT_ARGS
);
43 static int in_line_argn(MACRO_PROT_ARGS
);
44 static int in_line(MACRO_PROT_ARGS
);
45 static int obsolete(MACRO_PROT_ARGS
);
47 static int append_delims(struct mdoc
*,
49 static enum mdoct
lookup(enum mdoct
, const char *);
50 static enum mdoct
lookup_raw(const char *);
51 static int phrase(struct mdoc
*, int, int,
52 char *, enum margserr
);
53 static enum mdoct
rew_alt(enum mdoct
);
54 static int rew_dobreak(enum mdoct
,
55 const struct mdoc_node
*);
56 static enum rew
rew_dohalt(enum mdoct
, enum mdoc_type
,
57 const struct mdoc_node
*);
58 static int rew_elem(struct mdoc
*, enum mdoct
);
59 static int rew_last(struct mdoc
*,
60 const struct mdoc_node
*);
61 static int rew_sub(enum mdoc_type
, struct mdoc
*,
62 enum mdoct
, int, int);
63 static int swarn(struct mdoc
*, enum mdoc_type
, int,
64 int, const struct mdoc_node
*);
66 const struct mdoc_macro __mdoc_macros
[MDOC_MAX
] = {
67 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ap */
68 { in_line_eoln
, MDOC_PROLOGUE
}, /* Dd */
69 { in_line_eoln
, MDOC_PROLOGUE
}, /* Dt */
70 { in_line_eoln
, MDOC_PROLOGUE
}, /* Os */
71 { blk_full
, 0 }, /* Sh */
72 { blk_full
, 0 }, /* Ss */
73 { in_line_eoln
, 0 }, /* Pp */
74 { blk_part_imp
, MDOC_PARSED
}, /* D1 */
75 { blk_part_imp
, MDOC_PARSED
}, /* Dl */
76 { blk_full
, MDOC_EXPLICIT
}, /* Bd */
77 { blk_exp_close
, MDOC_EXPLICIT
}, /* Ed */
78 { blk_full
, MDOC_EXPLICIT
}, /* Bl */
79 { blk_exp_close
, MDOC_EXPLICIT
}, /* El */
80 { blk_full
, MDOC_PARSED
}, /* It */
81 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ad */
82 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* An */
83 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ar */
84 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Cd */
85 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Cm */
86 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Dv */
87 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Er */
88 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ev */
89 { in_line_eoln
, 0 }, /* Ex */
90 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fa */
91 { in_line_eoln
, 0 }, /* Fd */
92 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fl */
93 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fn */
94 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ft */
95 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ic */
96 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* In */
97 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Li */
98 { blk_full
, 0 }, /* Nd */
99 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Nm */
100 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Op */
101 { obsolete
, 0 }, /* Ot */
102 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Pa */
103 { in_line_eoln
, 0 }, /* Rv */
104 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* St */
105 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Va */
106 { ctx_synopsis
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Vt */
107 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Xr */
108 { in_line_eoln
, 0 }, /* %A */
109 { in_line_eoln
, 0 }, /* %B */
110 { in_line_eoln
, 0 }, /* %D */
111 { in_line_eoln
, 0 }, /* %I */
112 { in_line_eoln
, 0 }, /* %J */
113 { in_line_eoln
, 0 }, /* %N */
114 { in_line_eoln
, 0 }, /* %O */
115 { in_line_eoln
, 0 }, /* %P */
116 { in_line_eoln
, 0 }, /* %R */
117 { in_line_eoln
, 0 }, /* %T */
118 { in_line_eoln
, 0 }, /* %V */
119 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Ac */
120 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Ao */
121 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Aq */
122 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* At */
123 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Bc */
124 { blk_full
, MDOC_EXPLICIT
}, /* Bf */
125 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Bo */
126 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Bq */
127 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Bsx */
128 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Bx */
129 { in_line_eoln
, 0 }, /* Db */
130 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Dc */
131 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Do */
132 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Dq */
133 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Ec */
134 { blk_exp_close
, MDOC_EXPLICIT
}, /* Ef */
135 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Em */
136 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Eo */
137 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fx */
138 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ms */
139 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* No */
140 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ns */
141 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Nx */
142 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ox */
143 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Pc */
144 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_IGNDELIM
}, /* Pf */
145 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Po */
146 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Pq */
147 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Qc */
148 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ql */
149 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Qo */
150 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Qq */
151 { blk_exp_close
, MDOC_EXPLICIT
}, /* Re */
152 { blk_full
, MDOC_EXPLICIT
}, /* Rs */
153 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Sc */
154 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* So */
155 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Sq */
156 { in_line_eoln
, 0 }, /* Sm */
157 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Sx */
158 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Sy */
159 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Tn */
160 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ux */
161 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Xc */
162 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Xo */
163 { blk_full
, MDOC_EXPLICIT
| MDOC_CALLABLE
}, /* Fo */
164 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Fc */
165 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Oo */
166 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Oc */
167 { blk_full
, MDOC_EXPLICIT
}, /* Bk */
168 { blk_exp_close
, MDOC_EXPLICIT
}, /* Ek */
169 { in_line_eoln
, 0 }, /* Bt */
170 { in_line_eoln
, 0 }, /* Hf */
171 { obsolete
, 0 }, /* Fr */
172 { in_line_eoln
, 0 }, /* Ud */
173 { in_line_eoln
, 0 }, /* Lb */
174 { in_line_eoln
, 0 }, /* Lp */
175 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Lk */
176 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Mt */
177 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Brq */
178 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Bro */
179 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Brc */
180 { in_line_eoln
, 0 }, /* %C */
181 { obsolete
, 0 }, /* Es */
182 { obsolete
, 0 }, /* En */
183 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Dx */
184 { in_line_eoln
, 0 }, /* %Q */
185 { in_line_eoln
, 0 }, /* br */
186 { in_line_eoln
, 0 }, /* sp */
187 { in_line_eoln
, 0 }, /* %U */
190 const struct mdoc_macro
* const mdoc_macros
= __mdoc_macros
;
194 swarn(struct mdoc
*mdoc
, enum mdoc_type type
,
195 int line
, int pos
, const struct mdoc_node
*p
)
197 const char *n
, *t
, *tt
;
215 n
= mdoc_macronames
[p
->tok
];
219 n
= mdoc_macronames
[p
->tok
];
223 n
= mdoc_macronames
[p
->tok
];
230 if ( ! (MDOC_IGN_SCOPE
& mdoc
->pflags
))
231 return(mdoc_verr(mdoc
, line
, pos
,
232 "%s scope breaks %s scope of %s",
234 return(mdoc_vwarn(mdoc
, line
, pos
,
235 "%s scope breaks %s scope of %s",
241 * This is called at the end of parsing. It must traverse up the tree,
242 * closing out open [implicit] scopes. Obviously, open explicit scopes
246 mdoc_macroend(struct mdoc
*m
)
250 /* Scan for open explicit scopes. */
252 n
= MDOC_VALID
& m
->last
->flags
? m
->last
->parent
: m
->last
;
254 for ( ; n
; n
= n
->parent
) {
255 if (MDOC_BLOCK
!= n
->type
)
257 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[n
->tok
].flags
))
259 return(mdoc_nerr(m
, n
, EOPEN
));
262 /* Rewind to the first. */
264 return(rew_last(m
, m
->first
));
269 * Look up a macro from within a subsequent context.
272 lookup(enum mdoct from
, const char *p
)
274 /* FIXME: make -diag lists be un-PARSED. */
276 if ( ! (MDOC_PARSED
& mdoc_macros
[from
].flags
))
278 return(lookup_raw(p
));
283 * Lookup a macro following the initial line macro.
286 lookup_raw(const char *p
)
290 if (MDOC_MAX
== (res
= mdoc_hash_find(p
)))
292 if (MDOC_CALLABLE
& mdoc_macros
[res
].flags
)
299 rew_last(struct mdoc
*mdoc
, const struct mdoc_node
*to
)
303 mdoc
->next
= MDOC_NEXT_SIBLING
;
306 while (mdoc
->last
!= to
) {
307 if ( ! mdoc_valid_post(mdoc
))
309 if ( ! mdoc_action_post(mdoc
))
311 mdoc
->last
= mdoc
->last
->parent
;
315 if ( ! mdoc_valid_post(mdoc
))
317 return(mdoc_action_post(mdoc
));
322 * Return the opening macro of a closing one, e.g., `Ec' has `Eo' as its
326 rew_alt(enum mdoct tok
)
370 * Rewind rules. This indicates whether to stop rewinding
371 * (REWIND_HALT) without touching our current scope, stop rewinding and
372 * close our current scope (REWIND_REWIND), or continue (REWIND_NOHALT).
373 * The scope-closing and so on occurs in the various rew_* routines.
376 rew_dohalt(enum mdoct tok
, enum mdoc_type type
,
377 const struct mdoc_node
*p
)
380 if (MDOC_ROOT
== p
->type
)
382 if (MDOC_VALID
& p
->flags
)
383 return(REWIND_NOHALT
);
409 assert(MDOC_TAIL
!= type
);
410 if (type
== p
->type
&& tok
== p
->tok
)
411 return(REWIND_REWIND
);
414 assert(MDOC_TAIL
!= type
);
415 if (type
== p
->type
&& tok
== p
->tok
)
416 return(REWIND_REWIND
);
417 if (MDOC_BODY
== p
->type
&& MDOC_Bl
== p
->tok
)
421 if (type
== p
->type
&& tok
== p
->tok
)
422 return(REWIND_REWIND
);
427 assert(MDOC_TAIL
!= type
);
428 if (type
== p
->type
&& tok
== p
->tok
)
429 return(REWIND_REWIND
);
430 if (MDOC_BODY
== p
->type
&& MDOC_Sh
== p
->tok
)
464 if (type
== p
->type
&& tok
== p
->tok
)
465 return(REWIND_REWIND
);
467 /* Multi-line explicit scope close. */
499 if (type
== p
->type
&& rew_alt(tok
) == p
->tok
)
500 return(REWIND_REWIND
);
507 return(REWIND_NOHALT
);
512 * See if we can break an encountered scope (the rew_dohalt has returned
516 rew_dobreak(enum mdoct tok
, const struct mdoc_node
*p
)
519 assert(MDOC_ROOT
!= p
->type
);
520 if (MDOC_ELEM
== p
->type
)
522 if (MDOC_TEXT
== p
->type
)
524 if (MDOC_VALID
& p
->flags
)
529 return(MDOC_It
== p
->tok
);
531 return(MDOC_Nd
== p
->tok
);
533 return(MDOC_Ss
== p
->tok
);
535 if (MDOC_Nd
== p
->tok
)
537 if (MDOC_Ss
== p
->tok
)
539 return(MDOC_Sh
== p
->tok
);
541 if (MDOC_It
== p
->tok
)
545 if (MDOC_Op
== p
->tok
)
552 if (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
)
553 return(p
->tok
== rew_alt(tok
));
554 else if (MDOC_BLOCK
== p
->type
)
557 return(tok
== p
->tok
);
562 rew_elem(struct mdoc
*mdoc
, enum mdoct tok
)
567 if (MDOC_ELEM
!= n
->type
)
569 assert(MDOC_ELEM
== n
->type
);
570 assert(tok
== n
->tok
);
572 return(rew_last(mdoc
, n
));
577 rew_sub(enum mdoc_type t
, struct mdoc
*m
,
578 enum mdoct tok
, int line
, int ppos
)
584 for (n
= m
->last
; n
; n
= n
->parent
) {
585 c
= rew_dohalt(tok
, t
, n
);
586 if (REWIND_HALT
== c
) {
589 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
))
591 return(mdoc_perr(m
, line
, ppos
, ENOCTX
));
593 if (REWIND_REWIND
== c
)
595 else if (rew_dobreak(tok
, n
))
597 if ( ! swarn(m
, t
, line
, ppos
, n
))
602 if ( ! rew_last(m
, n
))
607 * The current block extends an enclosing block beyond a line
608 * break. Now that the current block ends, close the enclosing
611 if (NULL
!= (n
= n
->pending
)) {
612 assert(MDOC_HEAD
== n
->type
);
613 if ( ! rew_last(m
, n
))
615 if ( ! mdoc_body_alloc(m
, n
->line
, n
->pos
, n
->tok
))
625 append_delims(struct mdoc
*m
, int line
, int *pos
, char *buf
)
631 if ('\0' == buf
[*pos
])
636 ac
= mdoc_zargs(m
, line
, pos
, buf
, ARGS_NOWARN
, &p
);
638 if (ARGS_ERROR
== ac
)
640 else if (ARGS_EOLN
== ac
)
643 assert(DELIM_NONE
!= mdoc_isdelim(p
));
644 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
648 * If we encounter end-of-sentence symbols, then trigger
651 * XXX: it's easy to allow this to propogate outward to
652 * the last symbol, such that `. )' will cause the
653 * correct double-spacing. However, (1) groff isn't
654 * smart enough to do this and (2) it would require
655 * knowing which symbols break this behaviour, for
656 * example, `. ;' shouldn't propogate the double-space.
658 if (mandoc_eos(p
, strlen(p
)))
659 m
->last
->flags
|= MDOC_EOS
;
667 * Close out block partial/full explicit.
670 blk_exp_close(MACRO_PROT_ARGS
)
672 int j
, lastarg
, maxargs
, flushed
, nl
;
677 nl
= MDOC_NEWLINE
& m
->flags
;
688 if ( ! (MDOC_CALLABLE
& mdoc_macros
[tok
].flags
)) {
690 if ( ! mdoc_pwarn(m
, line
, ppos
, ENOLINE
))
693 if ( ! rew_sub(MDOC_BODY
, m
, tok
, line
, ppos
))
695 return(rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
));
698 if ( ! rew_sub(MDOC_BODY
, m
, tok
, line
, ppos
))
702 if ( ! mdoc_tail_alloc(m
, line
, ppos
, rew_alt(tok
)))
705 for (flushed
= j
= 0; ; j
++) {
708 if (j
== maxargs
&& ! flushed
) {
709 if ( ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
714 ac
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
716 if (ARGS_ERROR
== ac
)
718 if (ARGS_PUNCT
== ac
)
723 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
725 if (MDOC_MAX
== ntok
) {
726 if ( ! mdoc_word_alloc(m
, line
, lastarg
, p
))
732 if ( ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
736 if ( ! mdoc_macro(m
, ntok
, line
, lastarg
, pos
, buf
))
741 if ( ! flushed
&& ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
746 return(append_delims(m
, line
, pos
, buf
));
751 in_line(MACRO_PROT_ARGS
)
753 int la
, lastpunct
, cnt
, nc
, nl
;
758 struct mdoc_arg
*arg
;
761 nl
= MDOC_NEWLINE
& m
->flags
;
764 * Whether we allow ignored elements (those without content,
765 * usually because of reserved words) to squeak by.
787 for (arg
= NULL
;; ) {
789 av
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
791 if (ARGV_WORD
== av
) {
804 for (cnt
= 0, lastpunct
= 1;; ) {
806 ac
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
808 if (ARGS_ERROR
== ac
)
812 if (ARGS_PUNCT
== ac
)
815 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
818 * In this case, we've located a submacro and must
819 * execute it. Close out scope, if open. If no
820 * elements have been generated, either create one (nc)
821 * or raise a warning.
824 if (MDOC_MAX
!= ntok
) {
825 if (0 == lastpunct
&& ! rew_elem(m
, tok
))
827 if (nc
&& 0 == cnt
) {
828 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
830 if ( ! rew_last(m
, m
->last
))
832 } else if ( ! nc
&& 0 == cnt
) {
834 if ( ! mdoc_pwarn(m
, line
, ppos
, EIGNE
))
837 if ( ! mdoc_macro(m
, ntok
, line
, la
, pos
, buf
))
841 return(append_delims(m
, line
, pos
, buf
));
845 * Non-quote-enclosed punctuation. Set up our scope, if
846 * a word; rewind the scope, if a delimiter; then append
850 d
= ARGS_QWORD
== ac
? DELIM_NONE
: mdoc_isdelim(p
);
852 if (ARGS_QWORD
!= ac
&& DELIM_NONE
!= d
) {
853 if (0 == lastpunct
&& ! rew_elem(m
, tok
))
856 } else if (lastpunct
) {
857 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
864 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
868 * `Fl' macros have their scope re-opened with each new
869 * word so that the `-' can be added to each one without
870 * having to parse out spaces.
872 if (0 == lastpunct
&& MDOC_Fl
== tok
) {
873 if ( ! rew_elem(m
, tok
))
879 if (0 == lastpunct
&& ! rew_elem(m
, tok
))
883 * If no elements have been collected and we're allowed to have
884 * empties (nc), open a scope and close it out. Otherwise,
888 if (nc
&& 0 == cnt
) {
889 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
891 if ( ! rew_last(m
, m
->last
))
893 } else if ( ! nc
&& 0 == cnt
) {
895 if ( ! mdoc_pwarn(m
, line
, ppos
, EIGNE
))
901 return(append_delims(m
, line
, pos
, buf
));
906 blk_full(MACRO_PROT_ARGS
)
909 struct mdoc_arg
*arg
;
910 struct mdoc_node
*head
; /* save of head macro */
911 struct mdoc_node
*body
; /* save of body macro */
916 enum margserr ac
, lac
;
920 nl
= MDOC_NEWLINE
& m
->flags
;
922 /* Close out prior implicit scope. */
924 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
)) {
925 if ( ! rew_sub(MDOC_BODY
, m
, tok
, line
, ppos
))
927 if ( ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
932 * This routine accomodates implicitly- and explicitly-scoped
933 * macro openings. Implicit ones first close out prior scope
934 * (seen above). Delay opening the head until necessary to
935 * allow leading punctuation to print. Special consideration
936 * for `It -column', which has phrase-part syntax instead of
937 * regular child nodes.
940 for (arg
= NULL
;; ) {
942 av
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
944 if (ARGV_WORD
== av
) {
958 if ( ! mdoc_block_alloc(m
, line
, ppos
, tok
, arg
))
964 * The `Nd' macro has all arguments in its body: it's a hybrid
965 * of block partial-explicit and full-implicit. Stupid.
968 if (MDOC_Nd
== tok
) {
969 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
972 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
974 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
984 ac
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
986 if (ARGS_ERROR
== ac
)
991 if (ARGS_PEND
== ac
) {
992 if (ARGS_PPHRASE
== lac
)
998 /* Don't emit leading punct. for phrases. */
1001 ARGS_PHRASE
!= ac
&&
1002 ARGS_PPHRASE
!= ac
&&
1004 DELIM_OPEN
== mdoc_isdelim(p
)) {
1005 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1010 /* Always re-open head for phrases. */
1013 ARGS_PHRASE
== ac
||
1014 ARGS_PPHRASE
== ac
) {
1015 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
1020 if (ARGS_PHRASE
== ac
|| ARGS_PPHRASE
== ac
) {
1021 if (ARGS_PPHRASE
== ac
)
1022 m
->flags
|= MDOC_PPHRASE
;
1023 if ( ! phrase(m
, line
, la
, buf
, ac
))
1025 m
->flags
&= ~MDOC_PPHRASE
;
1026 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1031 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
1033 if (MDOC_MAX
== ntok
) {
1034 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1039 if ( ! mdoc_macro(m
, ntok
, line
, la
, pos
, buf
))
1045 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
1050 if (nl
&& ! append_delims(m
, line
, pos
, buf
))
1053 /* If we've already opened our body, exit now. */
1060 * If there is an open (i.e., unvalidated) sub-block requiring
1061 * explicit close-out, postpone switching the current block from
1062 * head to body until the rew_sub() call closing out that
1065 for (n
= m
->last
; n
&& n
!= head
; n
= n
->parent
) {
1066 if (MDOC_BLOCK
== n
->type
&&
1067 MDOC_EXPLICIT
& mdoc_macros
[n
->tok
].flags
&&
1068 ! (MDOC_VALID
& n
->flags
)) {
1069 assert( ! (MDOC_ACTED
& n
->flags
));
1076 /* Close out scopes to remain in a consistent state. */
1078 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1080 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1088 blk_part_imp(MACRO_PROT_ARGS
)
1094 struct mdoc_node
*blk
; /* saved block context */
1095 struct mdoc_node
*body
; /* saved body context */
1096 struct mdoc_node
*n
;
1098 nl
= MDOC_NEWLINE
& m
->flags
;
1101 * A macro that spans to the end of the line. This is generally
1102 * (but not necessarily) called as the first macro. The block
1103 * has a head as the immediate child, which is always empty,
1104 * followed by zero or more opening punctuation nodes, then the
1105 * body (which may be empty, depending on the macro), then zero
1106 * or more closing punctuation nodes.
1109 if ( ! mdoc_block_alloc(m
, line
, ppos
, tok
, NULL
))
1114 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
1116 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1120 * Open the body scope "on-demand", that is, after we've
1121 * processed all our the leading delimiters (open parenthesis,
1125 for (body
= NULL
; ; ) {
1127 ac
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1129 if (ARGS_ERROR
== ac
)
1131 if (ARGS_EOLN
== ac
)
1133 if (ARGS_PUNCT
== ac
)
1136 if (NULL
== body
&& ARGS_QWORD
!= ac
&&
1137 DELIM_OPEN
== mdoc_isdelim(p
)) {
1138 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1144 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1149 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
1151 if (MDOC_MAX
== ntok
) {
1152 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1157 if ( ! mdoc_macro(m
, ntok
, line
, la
, pos
, buf
))
1162 /* Clean-ups to leave in a consistent state. */
1165 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1170 for (n
= body
->child
; n
&& n
->next
; n
= n
->next
)
1174 * End of sentence spacing: if the last node is a text node and
1175 * has a trailing period, then mark it as being end-of-sentence.
1178 if (n
&& MDOC_TEXT
== n
->type
&& n
->string
)
1179 if (mandoc_eos(n
->string
, strlen(n
->string
)))
1180 n
->flags
|= MDOC_EOS
;
1182 /* Up-propogate the end-of-space flag. */
1184 if (n
&& (MDOC_EOS
& n
->flags
)) {
1185 body
->flags
|= MDOC_EOS
;
1186 body
->parent
->flags
|= MDOC_EOS
;
1190 * If we can't rewind to our body, then our scope has already
1191 * been closed by another macro (like `Oc' closing `Op'). This
1192 * is ugly behaviour nodding its head to OpenBSD's overwhelming
1193 * crufty use of `Op' breakage.
1195 * FIXME - this should be ifdef'd OpenBSD?
1197 for (n
= m
->last
; n
; n
= n
->parent
)
1201 if (NULL
== n
&& ! mdoc_nwarn(m
, body
, EIMPBRK
))
1204 if (n
&& ! rew_last(m
, body
))
1207 /* Standard appending of delimiters. */
1209 if (nl
&& ! append_delims(m
, line
, pos
, buf
))
1212 /* Rewind scope, if applicable. */
1214 if (n
&& ! rew_last(m
, blk
))
1222 blk_part_exp(MACRO_PROT_ARGS
)
1226 struct mdoc_node
*head
; /* keep track of head */
1227 struct mdoc_node
*body
; /* keep track of body */
1231 nl
= MDOC_NEWLINE
& m
->flags
;
1234 * The opening of an explicit macro having zero or more leading
1235 * punctuation nodes; a head with optional single element (the
1236 * case of `Eo'); and a body that may be empty.
1239 if ( ! mdoc_block_alloc(m
, line
, ppos
, tok
, NULL
))
1242 for (head
= body
= NULL
; ; ) {
1244 ac
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1246 if (ARGS_ERROR
== ac
)
1248 if (ARGS_PUNCT
== ac
)
1250 if (ARGS_EOLN
== ac
)
1253 /* Flush out leading punctuation. */
1255 if (NULL
== head
&& ARGS_QWORD
!= ac
&&
1256 DELIM_OPEN
== mdoc_isdelim(p
)) {
1257 assert(NULL
== body
);
1258 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1264 assert(NULL
== body
);
1265 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
1271 * `Eo' gobbles any data into the head, but most other
1272 * macros just immediately close out and begin the body.
1277 /* No check whether it's a macro! */
1279 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1282 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1284 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1292 assert(NULL
!= head
&& NULL
!= body
);
1294 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
1296 if (MDOC_MAX
== ntok
) {
1297 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1302 if ( ! mdoc_macro(m
, ntok
, line
, la
, pos
, buf
))
1307 /* Clean-up to leave in a consistent state. */
1310 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
1316 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1318 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1323 /* Standard appending of delimiters. */
1327 return(append_delims(m
, line
, pos
, buf
));
1333 in_line_argn(MACRO_PROT_ARGS
)
1335 int la
, flushed
, j
, maxargs
, nl
;
1338 struct mdoc_arg
*arg
;
1342 nl
= MDOC_NEWLINE
& m
->flags
;
1345 * A line macro that has a fixed number of arguments (maxargs).
1346 * Only open the scope once the first non-leading-punctuation is
1347 * found (unless MDOC_IGNDELIM is noted, like in `Pf'), then
1348 * keep it open until the maximum number of arguments are
1370 for (arg
= NULL
; ; ) {
1372 av
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
1374 if (ARGV_WORD
== av
) {
1379 if (ARGV_EOLN
== av
)
1384 mdoc_argv_free(arg
);
1388 for (flushed
= j
= 0; ; ) {
1390 ac
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1392 if (ARGS_ERROR
== ac
)
1394 if (ARGS_PUNCT
== ac
)
1396 if (ARGS_EOLN
== ac
)
1399 if ( ! (MDOC_IGNDELIM
& mdoc_macros
[tok
].flags
) &&
1401 0 == j
&& DELIM_OPEN
== mdoc_isdelim(p
)) {
1402 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1406 if ( ! mdoc_elem_alloc(m
, line
, la
, tok
, arg
))
1409 if (j
== maxargs
&& ! flushed
) {
1410 if ( ! rew_elem(m
, tok
))
1415 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
1417 if (MDOC_MAX
!= ntok
) {
1418 if ( ! flushed
&& ! rew_elem(m
, tok
))
1421 if ( ! mdoc_macro(m
, ntok
, line
, la
, pos
, buf
))
1427 if ( ! (MDOC_IGNDELIM
& mdoc_macros
[tok
].flags
) &&
1430 DELIM_NONE
!= mdoc_isdelim(p
)) {
1431 if ( ! rew_elem(m
, tok
))
1437 * XXX: this is a hack to work around groff's ugliness
1438 * as regards `Xr' and extraneous arguments. It should
1439 * ideally be deprecated behaviour, but because this is
1440 * code is no here, it's unlikely to be removed.
1444 if (MDOC_Xr
== tok
&& j
== maxargs
) {
1445 if ( ! mdoc_elem_alloc(m
, line
, la
, MDOC_Ns
, NULL
))
1447 if ( ! rew_elem(m
, MDOC_Ns
))
1452 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1457 if (0 == j
&& ! mdoc_elem_alloc(m
, line
, la
, tok
, arg
))
1460 /* Close out in a consistent state. */
1462 if ( ! flushed
&& ! rew_elem(m
, tok
))
1466 return(append_delims(m
, line
, pos
, buf
));
1471 in_line_eoln(MACRO_PROT_ARGS
)
1476 struct mdoc_arg
*arg
;
1480 assert( ! (MDOC_PARSED
& mdoc_macros
[tok
].flags
));
1482 /* Parse macro arguments. */
1484 for (arg
= NULL
; ; ) {
1486 av
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
1488 if (ARGV_WORD
== av
) {
1492 if (ARGV_EOLN
== av
)
1497 mdoc_argv_free(arg
);
1501 /* Open element scope. */
1503 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
1506 /* Parse argument terms. */
1510 ac
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1512 if (ARGS_ERROR
== ac
)
1514 if (ARGS_EOLN
== ac
)
1517 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
1519 if (MDOC_MAX
== ntok
) {
1520 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1525 if ( ! rew_elem(m
, tok
))
1527 return(mdoc_macro(m
, ntok
, line
, la
, pos
, buf
));
1530 /* Close out (no delimiters). */
1532 return(rew_elem(m
, tok
));
1538 ctx_synopsis(MACRO_PROT_ARGS
)
1542 nl
= MDOC_NEWLINE
& m
->flags
;
1544 /* If we're not in the SYNOPSIS, go straight to in-line. */
1545 if (SEC_SYNOPSIS
!= m
->lastsec
)
1546 return(in_line(m
, tok
, line
, ppos
, pos
, buf
));
1548 /* If we're a nested call, same place. */
1550 return(in_line(m
, tok
, line
, ppos
, pos
, buf
));
1553 * XXX: this will open a block scope; however, if later we end
1554 * up formatting the block scope, then child nodes will inherit
1555 * the formatting. Be careful.
1558 return(blk_part_imp(m
, tok
, line
, ppos
, pos
, buf
));
1564 obsolete(MACRO_PROT_ARGS
)
1567 return(mdoc_pwarn(m
, line
, ppos
, EOBS
));
1572 * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
1573 * They're unusual because they're basically free-form text until a
1574 * macro is encountered.
1577 phrase(struct mdoc
*m
, int line
, int ppos
, char *buf
, enum margserr ac
)
1584 assert(ARGS_PHRASE
== ac
|| ARGS_PPHRASE
== ac
);
1586 for (pos
= ppos
; ; ) {
1589 aac
= mdoc_zargs(m
, line
, &pos
, buf
, 0, &p
);
1591 if (ARGS_ERROR
== aac
)
1593 if (ARGS_EOLN
== aac
)
1596 ntok
= ARGS_QWORD
== aac
? MDOC_MAX
: lookup_raw(p
);
1598 if (MDOC_MAX
== ntok
) {
1599 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1604 if ( ! mdoc_macro(m
, ntok
, line
, la
, &pos
, buf
))
1606 return(append_delims(m
, line
, &pos
, buf
));