1 /* $Id: mdoc_macro.c,v 1.42 2010/02/17 19:28:11 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.
26 #define REWIND_REWIND (1 << 0)
27 #define REWIND_NOHALT (1 << 1)
28 #define REWIND_HALT (1 << 2)
30 static int ctx_synopsis(MACRO_PROT_ARGS
);
31 static int obsolete(MACRO_PROT_ARGS
);
32 static int blk_part_exp(MACRO_PROT_ARGS
);
33 static int in_line_eoln(MACRO_PROT_ARGS
);
34 static int in_line_argn(MACRO_PROT_ARGS
);
35 static int in_line(MACRO_PROT_ARGS
);
36 static int blk_full(MACRO_PROT_ARGS
);
37 static int blk_exp_close(MACRO_PROT_ARGS
);
38 static int blk_part_imp(MACRO_PROT_ARGS
);
40 static int phrase(struct mdoc
*, int, int, char *);
41 static int rew_dohalt(int, enum mdoc_type
,
42 const struct mdoc_node
*);
43 static int rew_alt(int);
44 static int rew_dobreak(int, const struct mdoc_node
*);
45 static int rew_elem(struct mdoc
*, int);
46 static int rew_sub(enum mdoc_type
, struct mdoc
*,
48 static int rew_last(struct mdoc
*,
49 const struct mdoc_node
*);
50 static int append_delims(struct mdoc
*, int, int *, char *);
51 static int lookup(int, const char *);
52 static int lookup_raw(const char *);
53 static int swarn(struct mdoc
*, enum mdoc_type
, int, int,
54 const struct mdoc_node
*);
56 /* Central table of library: who gets parsed how. */
58 const struct mdoc_macro __mdoc_macros
[MDOC_MAX
] = {
59 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ap */
60 { in_line_eoln
, MDOC_PROLOGUE
}, /* Dd */
61 { in_line_eoln
, MDOC_PROLOGUE
}, /* Dt */
62 { in_line_eoln
, MDOC_PROLOGUE
}, /* Os */
63 { blk_full
, 0 }, /* Sh */
64 { blk_full
, 0 }, /* Ss */
65 { in_line_eoln
, 0 }, /* Pp */
66 { blk_part_imp
, MDOC_PARSED
}, /* D1 */
67 { blk_part_imp
, MDOC_PARSED
}, /* Dl */
68 { blk_full
, MDOC_EXPLICIT
}, /* Bd */
69 { blk_exp_close
, MDOC_EXPLICIT
}, /* Ed */
70 { blk_full
, MDOC_EXPLICIT
}, /* Bl */
71 { blk_exp_close
, MDOC_EXPLICIT
}, /* El */
72 { blk_full
, MDOC_PARSED
}, /* It */
73 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ad */
74 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* An */
75 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ar */
76 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Cd */
77 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Cm */
78 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Dv */
79 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Er */
80 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ev */
81 { in_line_eoln
, 0 }, /* Ex */
82 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fa */
83 { in_line_eoln
, 0 }, /* Fd */
84 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fl */
85 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fn */
86 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ft */
87 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ic */
88 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* In */
89 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Li */
90 { blk_full
, 0 }, /* Nd */
91 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Nm */
92 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Op */
93 { obsolete
, 0 }, /* Ot */
94 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Pa */
95 { in_line_eoln
, 0 }, /* Rv */
96 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* St */
97 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Va */
98 { ctx_synopsis
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Vt */
99 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Xr */
100 { in_line_eoln
, 0 }, /* %A */
101 { in_line_eoln
, 0 }, /* %B */
102 { in_line_eoln
, 0 }, /* %D */
103 { in_line_eoln
, 0 }, /* %I */
104 { in_line_eoln
, 0 }, /* %J */
105 { in_line_eoln
, 0 }, /* %N */
106 { in_line_eoln
, 0 }, /* %O */
107 { in_line_eoln
, 0 }, /* %P */
108 { in_line_eoln
, 0 }, /* %R */
109 { in_line_eoln
, 0 }, /* %T */
110 { in_line_eoln
, 0 }, /* %V */
111 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Ac */
112 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Ao */
113 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Aq */
114 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* At */
115 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Bc */
116 { blk_full
, MDOC_EXPLICIT
}, /* Bf */
117 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Bo */
118 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Bq */
119 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Bsx */
120 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Bx */
121 { in_line_eoln
, 0 }, /* Db */
122 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Dc */
123 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Do */
124 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Dq */
125 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Ec */
126 { blk_exp_close
, MDOC_EXPLICIT
}, /* Ef */
127 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Em */
128 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Eo */
129 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fx */
130 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ms */
131 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* No */
132 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ns */
133 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Nx */
134 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ox */
135 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Pc */
136 { in_line_argn
, MDOC_PARSED
| MDOC_IGNDELIM
}, /* Pf */
137 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Po */
138 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Pq */
139 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Qc */
140 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ql */
141 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Qo */
142 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Qq */
143 { blk_exp_close
, MDOC_EXPLICIT
}, /* Re */
144 { blk_full
, MDOC_EXPLICIT
}, /* Rs */
145 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Sc */
146 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* So */
147 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Sq */
148 { in_line_eoln
, 0 }, /* Sm */
149 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Sx */
150 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Sy */
151 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Tn */
152 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ux */
153 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Xc */
154 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Xo */
155 { blk_full
, MDOC_EXPLICIT
| MDOC_CALLABLE
}, /* Fo */
156 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Fc */
157 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Oo */
158 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Oc */
159 { blk_full
, MDOC_EXPLICIT
}, /* Bk */
160 { blk_exp_close
, MDOC_EXPLICIT
}, /* Ek */
161 { in_line_eoln
, 0 }, /* Bt */
162 { in_line_eoln
, 0 }, /* Hf */
163 { obsolete
, 0 }, /* Fr */
164 { in_line_eoln
, 0 }, /* Ud */
165 { in_line_eoln
, 0 }, /* Lb */
166 { in_line_eoln
, 0 }, /* Lp */
167 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Lk */
168 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Mt */
169 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Brq */
170 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Bro */
171 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Brc */
172 { in_line_eoln
, 0 }, /* %C */
173 { obsolete
, 0 }, /* Es */
174 { obsolete
, 0 }, /* En */
175 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Dx */
176 { in_line_eoln
, 0 }, /* %Q */
177 { in_line_eoln
, 0 }, /* br */
178 { in_line_eoln
, 0 }, /* sp */
179 { in_line_eoln
, 0 }, /* %U */
182 const struct mdoc_macro
* const mdoc_macros
= __mdoc_macros
;
186 swarn(struct mdoc
*mdoc
, enum mdoc_type type
,
187 int line
, int pos
, const struct mdoc_node
*p
)
189 const char *n
, *t
, *tt
;
207 n
= mdoc_macronames
[p
->tok
];
211 n
= mdoc_macronames
[p
->tok
];
215 n
= mdoc_macronames
[p
->tok
];
222 if ( ! (MDOC_IGN_SCOPE
& mdoc
->pflags
))
223 return(mdoc_verr(mdoc
, line
, pos
,
224 "%s scope breaks %s scope of %s",
226 return(mdoc_vwarn(mdoc
, line
, pos
,
227 "%s scope breaks %s scope of %s",
233 * This is called at the end of parsing. It must traverse up the tree,
234 * closing out open [implicit] scopes. Obviously, open explicit scopes
238 mdoc_macroend(struct mdoc
*m
)
242 /* Scan for open explicit scopes. */
244 n
= MDOC_VALID
& m
->last
->flags
? m
->last
->parent
: m
->last
;
246 for ( ; n
; n
= n
->parent
) {
247 if (MDOC_BLOCK
!= n
->type
)
249 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[n
->tok
].flags
))
251 return(mdoc_nerr(m
, n
, EOPEN
));
254 /* Rewind to the first. */
256 return(rew_last(m
, m
->first
));
261 * Look up a macro from within a subsequent context.
264 lookup(int from
, const char *p
)
266 /* FIXME: make -diag lists be un-PARSED. */
268 if ( ! (MDOC_PARSED
& mdoc_macros
[from
].flags
))
270 return(lookup_raw(p
));
275 * Lookup a macro following the initial line macro.
278 lookup_raw(const char *p
)
282 if (MDOC_MAX
== (res
= mdoc_hash_find(p
)))
284 if (MDOC_CALLABLE
& mdoc_macros
[res
].flags
)
291 rew_last(struct mdoc
*mdoc
, const struct mdoc_node
*to
)
295 mdoc
->next
= MDOC_NEXT_SIBLING
;
298 while (mdoc
->last
!= to
) {
299 if ( ! mdoc_valid_post(mdoc
))
301 if ( ! mdoc_action_post(mdoc
))
303 mdoc
->last
= mdoc
->last
->parent
;
307 if ( ! mdoc_valid_post(mdoc
))
309 return(mdoc_action_post(mdoc
));
314 * Return the opening macro of a closing one, e.g., `Ec' has `Eo' as its
362 * Rewind rules. This indicates whether to stop rewinding
363 * (REWIND_HALT) without touching our current scope, stop rewinding and
364 * close our current scope (REWIND_REWIND), or continue (REWIND_NOHALT).
365 * The scope-closing and so on occurs in the various rew_* routines.
368 rew_dohalt(int tok
, enum mdoc_type type
, const struct mdoc_node
*p
)
371 if (MDOC_ROOT
== p
->type
)
373 if (MDOC_VALID
& p
->flags
)
374 return(REWIND_NOHALT
);
400 assert(MDOC_TAIL
!= type
);
401 if (type
== p
->type
&& tok
== p
->tok
)
402 return(REWIND_REWIND
);
405 assert(MDOC_TAIL
!= type
);
406 if (type
== p
->type
&& tok
== p
->tok
)
407 return(REWIND_REWIND
);
408 if (MDOC_BODY
== p
->type
&& MDOC_Bl
== p
->tok
)
412 if (type
== p
->type
&& tok
== p
->tok
)
413 return(REWIND_REWIND
);
418 assert(MDOC_TAIL
!= type
);
419 if (type
== p
->type
&& tok
== p
->tok
)
420 return(REWIND_REWIND
);
421 if (MDOC_BODY
== p
->type
&& MDOC_Sh
== p
->tok
)
455 if (type
== p
->type
&& tok
== p
->tok
)
456 return(REWIND_REWIND
);
458 /* Multi-line explicit scope close. */
490 if (type
== p
->type
&& rew_alt(tok
) == p
->tok
)
491 return(REWIND_REWIND
);
498 return(REWIND_NOHALT
);
503 * See if we can break an encountered scope (the rew_dohalt has returned
507 rew_dobreak(int tok
, const struct mdoc_node
*p
)
510 assert(MDOC_ROOT
!= p
->type
);
511 if (MDOC_ELEM
== p
->type
)
513 if (MDOC_TEXT
== p
->type
)
515 if (MDOC_VALID
& p
->flags
)
520 return(MDOC_It
== p
->tok
);
522 return(MDOC_Nd
== p
->tok
);
524 return(MDOC_Ss
== p
->tok
);
526 if (MDOC_Nd
== p
->tok
)
528 if (MDOC_Ss
== p
->tok
)
530 return(MDOC_Sh
== p
->tok
);
532 if (MDOC_It
== p
->tok
)
536 /* XXX - experimental! */
537 if (MDOC_Op
== p
->tok
)
544 if (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
)
545 return(p
->tok
== rew_alt(tok
));
546 else if (MDOC_BLOCK
== p
->type
)
549 return(tok
== p
->tok
);
554 rew_elem(struct mdoc
*mdoc
, int tok
)
559 if (MDOC_ELEM
!= n
->type
)
561 assert(MDOC_ELEM
== n
->type
);
562 assert(tok
== n
->tok
);
564 return(rew_last(mdoc
, n
));
569 rew_sub(enum mdoc_type t
, struct mdoc
*m
,
570 int tok
, int line
, int ppos
)
576 for (n
= m
->last
; n
; n
= n
->parent
) {
577 c
= rew_dohalt(tok
, t
, n
);
578 if (REWIND_HALT
== c
) {
581 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
))
583 return(mdoc_perr(m
, line
, ppos
, ENOCTX
));
585 if (REWIND_REWIND
== c
)
587 else if (rew_dobreak(tok
, n
))
589 if ( ! swarn(m
, t
, line
, ppos
, n
))
594 return(rew_last(m
, n
));
599 append_delims(struct mdoc
*mdoc
, int line
, int *pos
, char *buf
)
609 c
= mdoc_zargs(mdoc
, line
, pos
, buf
, ARGS_NOWARN
, &p
);
610 assert(ARGS_PHRASE
!= c
);
614 else if (ARGS_EOLN
== c
)
616 assert(mdoc_isdelim(p
));
617 if ( ! mdoc_word_alloc(mdoc
, line
, lastarg
, p
))
626 * Close out block partial/full explicit.
629 blk_exp_close(MACRO_PROT_ARGS
)
631 int j
, c
, lastarg
, maxargs
, flushed
;
643 if ( ! (MDOC_CALLABLE
& mdoc_macros
[tok
].flags
)) {
645 if ( ! mdoc_pwarn(m
, line
, ppos
, ENOLINE
))
648 if ( ! rew_sub(MDOC_BODY
, m
, tok
, line
, ppos
))
650 return(rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
));
653 if ( ! rew_sub(MDOC_BODY
, m
, tok
, line
, ppos
))
657 if ( ! mdoc_tail_alloc(m
, line
, ppos
, rew_alt(tok
)))
660 for (flushed
= j
= 0; ; j
++) {
663 if (j
== maxargs
&& ! flushed
) {
664 if ( ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
669 c
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
678 if (MDOC_MAX
!= (c
= lookup(tok
, p
))) {
680 if ( ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
684 if ( ! mdoc_macro(m
, c
, line
, lastarg
, pos
, buf
))
689 if ( ! mdoc_word_alloc(m
, line
, lastarg
, p
))
693 if ( ! flushed
&& ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
698 return(append_delims(m
, line
, pos
, buf
));
703 in_line(MACRO_PROT_ARGS
)
705 int la
, lastpunct
, c
, w
, cnt
, d
, nc
;
706 struct mdoc_arg
*arg
;
710 * Whether we allow ignored elements (those without content,
711 * usually because of reserved words) to squeak by.
732 for (arg
= NULL
;; ) {
734 c
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
736 if (ARGV_WORD
== c
) {
749 for (cnt
= 0, lastpunct
= 1;; ) {
751 w
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
760 /* Quoted words shouldn't be looked-up. */
762 c
= ARGS_QWORD
== w
? MDOC_MAX
: lookup(tok
, p
);
765 * In this case, we've located a submacro and must
766 * execute it. Close out scope, if open. If no
767 * elements have been generated, either create one (nc)
768 * or raise a warning.
772 if (0 == lastpunct
&& ! rew_elem(m
, tok
))
774 if (nc
&& 0 == cnt
) {
775 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
777 if ( ! rew_last(m
, m
->last
))
779 } else if ( ! nc
&& 0 == cnt
) {
781 if ( ! mdoc_pwarn(m
, line
, ppos
, EIGNE
))
784 c
= mdoc_macro(m
, c
, line
, la
, pos
, buf
);
789 return(append_delims(m
, line
, pos
, buf
));
793 * Non-quote-enclosed punctuation. Set up our scope, if
794 * a word; rewind the scope, if a delimiter; then append
800 if (ARGS_QWORD
!= w
&& d
) {
801 if (0 == lastpunct
&& ! rew_elem(m
, tok
))
804 } else if (lastpunct
) {
805 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
812 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
816 * `Fl' macros have their scope re-opened with each new
817 * word so that the `-' can be added to each one without
818 * having to parse out spaces.
820 if (0 == lastpunct
&& MDOC_Fl
== tok
) {
821 if ( ! rew_elem(m
, tok
))
827 if (0 == lastpunct
&& ! rew_elem(m
, tok
))
831 * If no elements have been collected and we're allowed to have
832 * empties (nc), open a scope and close it out. Otherwise,
836 if (nc
&& 0 == cnt
) {
837 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
839 if ( ! rew_last(m
, m
->last
))
841 } else if ( ! nc
&& 0 == cnt
) {
843 if ( ! mdoc_pwarn(m
, line
, ppos
, EIGNE
))
849 return(append_delims(m
, line
, pos
, buf
));
854 blk_full(MACRO_PROT_ARGS
)
856 int c
, lastarg
, reopen
, dohead
;
857 struct mdoc_arg
*arg
;
861 * Whether to process a block-head section. If this is
862 * non-zero, then a head will be opened for all line arguments.
863 * If not, then the head will always be empty and only a body
864 * will be opened, which will stay open at the eoln.
876 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
)) {
877 if ( ! rew_sub(MDOC_BODY
, m
, tok
, line
, ppos
))
879 if ( ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
883 for (arg
= NULL
;; ) {
885 c
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
887 if (ARGV_WORD
== c
) {
901 if ( ! mdoc_block_alloc(m
, line
, ppos
, tok
, arg
))
904 if (0 == buf
[*pos
]) {
905 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
907 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
909 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
914 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
917 /* Immediately close out head and enter body, if applicable. */
920 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
922 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
926 for (reopen
= 0;; ) {
928 c
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
934 if (ARGS_PHRASE
== c
) {
936 if (reopen
&& ! mdoc_head_alloc(m
, line
, ppos
, tok
))
939 * Phrases are self-contained macro phrases used
940 * in the columnar output of a macro. They need
943 if ( ! phrase(m
, line
, lastarg
, buf
))
945 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
952 if (MDOC_MAX
== (c
= lookup(tok
, p
))) {
953 if ( ! mdoc_word_alloc(m
, line
, lastarg
, p
))
958 if ( ! mdoc_macro(m
, c
, line
, lastarg
, pos
, buf
))
963 if (1 == ppos
&& ! append_delims(m
, line
, pos
, buf
))
966 /* If the body's already open, then just return. */
970 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
972 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
980 blk_part_imp(MACRO_PROT_ARGS
)
984 struct mdoc_node
*blk
, *body
, *n
;
986 /* If applicable, close out prior scopes. */
988 if ( ! mdoc_block_alloc(m
, line
, ppos
, tok
, NULL
))
990 /* Saved for later close-out. */
992 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
994 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
996 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
998 /* Saved for later close-out. */
1001 /* Body argument processing. */
1005 c
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1006 assert(ARGS_PHRASE
!= c
);
1008 if (ARGS_ERROR
== c
)
1010 if (ARGS_PUNCT
== c
)
1015 if (MDOC_MAX
== (c
= lookup(tok
, p
))) {
1016 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1021 if ( ! mdoc_macro(m
, c
, line
, la
, pos
, buf
))
1027 * If we can't rewind to our body, then our scope has already
1028 * been closed by another macro (like `Oc' closing `Op'). This
1029 * is ugly behaviour nodding its head to OpenBSD's overwhelming
1030 * crufty use of `Op' breakage--XXX, deprecate in time.
1032 for (n
= m
->last
; n
; n
= n
->parent
)
1035 if (NULL
== n
&& ! mdoc_nwarn(m
, body
, EIMPBRK
))
1037 if (n
&& ! rew_last(m
, body
))
1040 /* Standard appending of delimiters. */
1042 if (1 == ppos
&& ! append_delims(m
, line
, pos
, buf
))
1045 /* Rewind scope, if applicable. */
1047 if (n
&& ! rew_last(m
, blk
))
1055 blk_part_exp(MACRO_PROT_ARGS
)
1057 int la
, flushed
, j
, c
, maxargs
;
1060 /* Number of head arguments. Only `Eo' has these, */
1071 /* Begin the block scope. */
1073 if ( ! mdoc_block_alloc(m
, line
, ppos
, tok
, NULL
))
1077 * If no head arguments, open and then close out a head, noting
1078 * that we've flushed our terms. `flushed' means that we've
1079 * flushed out the head and the body is open.
1083 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
1085 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1087 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1091 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
1096 /* Process the head/head+body line arguments. */
1098 for (j
= 0; ; j
++) {
1100 if (j
== maxargs
&& ! flushed
) {
1101 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1104 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1108 c
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1109 assert(ARGS_PHRASE
!= c
);
1111 if (ARGS_ERROR
== c
)
1113 if (ARGS_PUNCT
== c
)
1118 if (MDOC_MAX
!= (c
= lookup(tok
, p
))) {
1120 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1123 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1126 if ( ! mdoc_macro(m
, c
, line
, la
, pos
, buf
))
1131 if ( ! flushed
&& mdoc_isdelim(p
)) {
1132 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1135 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1139 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1143 /* Close the head and open the body, if applicable. */
1146 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1148 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1152 /* Standard appending of delimiters. */
1156 return(append_delims(m
, line
, pos
, buf
));
1161 in_line_argn(MACRO_PROT_ARGS
)
1163 int la
, flushed
, j
, c
, maxargs
;
1164 struct mdoc_arg
*arg
;
1167 /* Fixed maximum arguments per macro, if applicable. */
1187 /* Macro argument processing. */
1189 for (arg
= NULL
;; ) {
1191 c
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
1193 if (ARGV_WORD
== c
) {
1203 mdoc_argv_free(arg
);
1207 /* Open the element scope. */
1209 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
1212 /* Process element arguments. */
1214 for (flushed
= j
= 0; ; j
++) {
1217 if (j
== maxargs
&& ! flushed
) {
1218 if ( ! rew_elem(m
, tok
))
1223 c
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1225 if (ARGS_ERROR
== c
)
1227 if (ARGS_PUNCT
== c
)
1232 if (MDOC_MAX
!= (c
= lookup(tok
, p
))) {
1233 if ( ! flushed
&& ! rew_elem(m
, tok
))
1236 if ( ! mdoc_macro(m
, c
, line
, la
, pos
, buf
))
1241 if ( ! (MDOC_IGNDELIM
& mdoc_macros
[tok
].flags
) &&
1242 ! flushed
&& mdoc_isdelim(p
)) {
1243 if ( ! rew_elem(m
, tok
))
1249 * XXX: this is a hack to work around groff's ugliness
1250 * as regards `Xr' and extraneous arguments. It should
1251 * ideally be deprecated behaviour, but because this is
1252 * code is no here, it's unlikely to be removed.
1254 if (MDOC_Xr
== tok
&& j
== maxargs
) {
1255 if ( ! mdoc_elem_alloc(m
, line
, ppos
, MDOC_Ns
, NULL
))
1257 if ( ! rew_elem(m
, MDOC_Ns
))
1261 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1265 /* Close out and append delimiters. */
1267 if ( ! flushed
&& ! rew_elem(m
, tok
))
1272 return(append_delims(m
, line
, pos
, buf
));
1277 in_line_eoln(MACRO_PROT_ARGS
)
1280 struct mdoc_arg
*arg
;
1283 assert( ! (MDOC_PARSED
& mdoc_macros
[tok
].flags
));
1285 /* Parse macro arguments. */
1287 for (arg
= NULL
; ; ) {
1289 c
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
1291 if (ARGV_WORD
== c
) {
1300 mdoc_argv_free(arg
);
1304 /* Open element scope. */
1306 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
1309 /* Parse argument terms. */
1313 w
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1315 if (ARGS_ERROR
== w
)
1320 c
= ARGS_QWORD
== w
? MDOC_MAX
: lookup(tok
, p
);
1322 if (MDOC_MAX
!= c
) {
1323 if ( ! rew_elem(m
, tok
))
1325 return(mdoc_macro(m
, c
, line
, la
, pos
, buf
));
1328 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1332 /* Close out (no delimiters). */
1334 return(rew_elem(m
, tok
));
1340 ctx_synopsis(MACRO_PROT_ARGS
)
1343 /* If we're not in the SYNOPSIS, go straight to in-line. */
1344 if (SEC_SYNOPSIS
!= m
->lastsec
)
1345 return(in_line(m
, tok
, line
, ppos
, pos
, buf
));
1347 /* If we're a nested call, same place. */
1349 return(in_line(m
, tok
, line
, ppos
, pos
, buf
));
1352 * XXX: this will open a block scope; however, if later we end
1353 * up formatting the block scope, then child nodes will inherit
1354 * the formatting. Be careful.
1357 return(blk_part_imp(m
, tok
, line
, ppos
, pos
, buf
));
1363 obsolete(MACRO_PROT_ARGS
)
1366 return(mdoc_pwarn(m
, line
, ppos
, EOBS
));
1371 * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
1372 * They're unusual because they're basically free-form text until a
1373 * macro is encountered.
1376 phrase(struct mdoc
*m
, int line
, int ppos
, char *buf
)
1381 for (pos
= ppos
; ; ) {
1384 /* Note: no calling context! */
1385 w
= mdoc_zargs(m
, line
, &pos
, buf
, 0, &p
);
1387 if (ARGS_ERROR
== w
)
1392 c
= ARGS_QWORD
== w
? MDOC_MAX
: lookup_raw(p
);
1394 if (MDOC_MAX
!= c
) {
1395 if ( ! mdoc_macro(m
, c
, line
, la
, &pos
, buf
))
1397 return(append_delims(m
, line
, &pos
, buf
));
1400 if ( ! mdoc_word_alloc(m
, line
, la
, p
))