]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_macro.c
1 /* $Id: mdoc_macro.c,v 1.44 2010/03/29 19:45:42 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 #define REWIND_REWIND (1 << 0)
31 #define REWIND_NOHALT (1 << 1)
32 #define REWIND_HALT (1 << 2)
34 static int ctx_synopsis(MACRO_PROT_ARGS
);
35 static int obsolete(MACRO_PROT_ARGS
);
36 static int blk_part_exp(MACRO_PROT_ARGS
);
37 static int in_line_eoln(MACRO_PROT_ARGS
);
38 static int in_line_argn(MACRO_PROT_ARGS
);
39 static int in_line(MACRO_PROT_ARGS
);
40 static int blk_full(MACRO_PROT_ARGS
);
41 static int blk_exp_close(MACRO_PROT_ARGS
);
42 static int blk_part_imp(MACRO_PROT_ARGS
);
44 static int phrase(struct mdoc
*, int, int, char *);
45 static int rew_dohalt(int, enum mdoc_type
,
46 const struct mdoc_node
*);
47 static int rew_alt(int);
48 static int rew_dobreak(int, const struct mdoc_node
*);
49 static int rew_elem(struct mdoc
*, int);
50 static int rew_sub(enum mdoc_type
, struct mdoc
*,
52 static int rew_last(struct mdoc
*,
53 const struct mdoc_node
*);
54 static int append_delims(struct mdoc
*, int, int *, char *);
55 static int lookup(int, const char *);
56 static int lookup_raw(const char *);
57 static int swarn(struct mdoc
*, enum mdoc_type
, int, int,
58 const struct mdoc_node
*);
60 /* Central table of library: who gets parsed how. */
62 const struct mdoc_macro __mdoc_macros
[MDOC_MAX
] = {
63 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ap */
64 { in_line_eoln
, MDOC_PROLOGUE
}, /* Dd */
65 { in_line_eoln
, MDOC_PROLOGUE
}, /* Dt */
66 { in_line_eoln
, MDOC_PROLOGUE
}, /* Os */
67 { blk_full
, 0 }, /* Sh */
68 { blk_full
, 0 }, /* Ss */
69 { in_line_eoln
, 0 }, /* Pp */
70 { blk_part_imp
, MDOC_PARSED
}, /* D1 */
71 { blk_part_imp
, MDOC_PARSED
}, /* Dl */
72 { blk_full
, MDOC_EXPLICIT
}, /* Bd */
73 { blk_exp_close
, MDOC_EXPLICIT
}, /* Ed */
74 { blk_full
, MDOC_EXPLICIT
}, /* Bl */
75 { blk_exp_close
, MDOC_EXPLICIT
}, /* El */
76 { blk_full
, MDOC_PARSED
}, /* It */
77 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ad */
78 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* An */
79 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ar */
80 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Cd */
81 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Cm */
82 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Dv */
83 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Er */
84 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ev */
85 { in_line_eoln
, 0 }, /* Ex */
86 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fa */
87 { in_line_eoln
, 0 }, /* Fd */
88 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fl */
89 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fn */
90 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ft */
91 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ic */
92 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* In */
93 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Li */
94 { blk_full
, 0 }, /* Nd */
95 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Nm */
96 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Op */
97 { obsolete
, 0 }, /* Ot */
98 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Pa */
99 { in_line_eoln
, 0 }, /* Rv */
100 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* St */
101 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Va */
102 { ctx_synopsis
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Vt */
103 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Xr */
104 { in_line_eoln
, 0 }, /* %A */
105 { in_line_eoln
, 0 }, /* %B */
106 { in_line_eoln
, 0 }, /* %D */
107 { in_line_eoln
, 0 }, /* %I */
108 { in_line_eoln
, 0 }, /* %J */
109 { in_line_eoln
, 0 }, /* %N */
110 { in_line_eoln
, 0 }, /* %O */
111 { in_line_eoln
, 0 }, /* %P */
112 { in_line_eoln
, 0 }, /* %R */
113 { in_line_eoln
, 0 }, /* %T */
114 { in_line_eoln
, 0 }, /* %V */
115 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Ac */
116 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Ao */
117 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Aq */
118 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* At */
119 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Bc */
120 { blk_full
, MDOC_EXPLICIT
}, /* Bf */
121 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Bo */
122 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Bq */
123 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Bsx */
124 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Bx */
125 { in_line_eoln
, 0 }, /* Db */
126 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Dc */
127 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Do */
128 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Dq */
129 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Ec */
130 { blk_exp_close
, MDOC_EXPLICIT
}, /* Ef */
131 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Em */
132 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Eo */
133 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fx */
134 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ms */
135 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* No */
136 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ns */
137 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Nx */
138 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ox */
139 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Pc */
140 { in_line_argn
, MDOC_PARSED
| MDOC_IGNDELIM
}, /* Pf */
141 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Po */
142 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Pq */
143 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Qc */
144 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ql */
145 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Qo */
146 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Qq */
147 { blk_exp_close
, MDOC_EXPLICIT
}, /* Re */
148 { blk_full
, MDOC_EXPLICIT
}, /* Rs */
149 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Sc */
150 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* So */
151 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Sq */
152 { in_line_eoln
, 0 }, /* Sm */
153 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Sx */
154 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Sy */
155 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Tn */
156 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ux */
157 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Xc */
158 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Xo */
159 { blk_full
, MDOC_EXPLICIT
| MDOC_CALLABLE
}, /* Fo */
160 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Fc */
161 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Oo */
162 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Oc */
163 { blk_full
, MDOC_EXPLICIT
}, /* Bk */
164 { blk_exp_close
, MDOC_EXPLICIT
}, /* Ek */
165 { in_line_eoln
, 0 }, /* Bt */
166 { in_line_eoln
, 0 }, /* Hf */
167 { obsolete
, 0 }, /* Fr */
168 { in_line_eoln
, 0 }, /* Ud */
169 { in_line_eoln
, 0 }, /* Lb */
170 { in_line_eoln
, 0 }, /* Lp */
171 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Lk */
172 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Mt */
173 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Brq */
174 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Bro */
175 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Brc */
176 { in_line_eoln
, 0 }, /* %C */
177 { obsolete
, 0 }, /* Es */
178 { obsolete
, 0 }, /* En */
179 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Dx */
180 { in_line_eoln
, 0 }, /* %Q */
181 { in_line_eoln
, 0 }, /* br */
182 { in_line_eoln
, 0 }, /* sp */
183 { in_line_eoln
, 0 }, /* %U */
186 const struct mdoc_macro
* const mdoc_macros
= __mdoc_macros
;
190 swarn(struct mdoc
*mdoc
, enum mdoc_type type
,
191 int line
, int pos
, const struct mdoc_node
*p
)
193 const char *n
, *t
, *tt
;
211 n
= mdoc_macronames
[p
->tok
];
215 n
= mdoc_macronames
[p
->tok
];
219 n
= mdoc_macronames
[p
->tok
];
226 if ( ! (MDOC_IGN_SCOPE
& mdoc
->pflags
))
227 return(mdoc_verr(mdoc
, line
, pos
,
228 "%s scope breaks %s scope of %s",
230 return(mdoc_vwarn(mdoc
, line
, pos
,
231 "%s scope breaks %s scope of %s",
237 * This is called at the end of parsing. It must traverse up the tree,
238 * closing out open [implicit] scopes. Obviously, open explicit scopes
242 mdoc_macroend(struct mdoc
*m
)
246 /* Scan for open explicit scopes. */
248 n
= MDOC_VALID
& m
->last
->flags
? m
->last
->parent
: m
->last
;
250 for ( ; n
; n
= n
->parent
) {
251 if (MDOC_BLOCK
!= n
->type
)
253 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[n
->tok
].flags
))
255 return(mdoc_nerr(m
, n
, EOPEN
));
258 /* Rewind to the first. */
260 return(rew_last(m
, m
->first
));
265 * Look up a macro from within a subsequent context.
268 lookup(int from
, const char *p
)
270 /* FIXME: make -diag lists be un-PARSED. */
272 if ( ! (MDOC_PARSED
& mdoc_macros
[from
].flags
))
274 return(lookup_raw(p
));
279 * Lookup a macro following the initial line macro.
282 lookup_raw(const char *p
)
286 if (MDOC_MAX
== (res
= mdoc_hash_find(p
)))
288 if (MDOC_CALLABLE
& mdoc_macros
[res
].flags
)
295 rew_last(struct mdoc
*mdoc
, const struct mdoc_node
*to
)
299 mdoc
->next
= MDOC_NEXT_SIBLING
;
302 while (mdoc
->last
!= to
) {
303 if ( ! mdoc_valid_post(mdoc
))
305 if ( ! mdoc_action_post(mdoc
))
307 mdoc
->last
= mdoc
->last
->parent
;
311 if ( ! mdoc_valid_post(mdoc
))
313 return(mdoc_action_post(mdoc
));
318 * Return the opening macro of a closing one, e.g., `Ec' has `Eo' as its
366 * Rewind rules. This indicates whether to stop rewinding
367 * (REWIND_HALT) without touching our current scope, stop rewinding and
368 * close our current scope (REWIND_REWIND), or continue (REWIND_NOHALT).
369 * The scope-closing and so on occurs in the various rew_* routines.
372 rew_dohalt(int tok
, enum mdoc_type type
, const struct mdoc_node
*p
)
375 if (MDOC_ROOT
== p
->type
)
377 if (MDOC_VALID
& p
->flags
)
378 return(REWIND_NOHALT
);
404 assert(MDOC_TAIL
!= type
);
405 if (type
== p
->type
&& tok
== p
->tok
)
406 return(REWIND_REWIND
);
409 assert(MDOC_TAIL
!= type
);
410 if (type
== p
->type
&& tok
== p
->tok
)
411 return(REWIND_REWIND
);
412 if (MDOC_BODY
== p
->type
&& MDOC_Bl
== p
->tok
)
416 if (type
== p
->type
&& tok
== p
->tok
)
417 return(REWIND_REWIND
);
422 assert(MDOC_TAIL
!= type
);
423 if (type
== p
->type
&& tok
== p
->tok
)
424 return(REWIND_REWIND
);
425 if (MDOC_BODY
== p
->type
&& MDOC_Sh
== p
->tok
)
459 if (type
== p
->type
&& tok
== p
->tok
)
460 return(REWIND_REWIND
);
462 /* Multi-line explicit scope close. */
494 if (type
== p
->type
&& rew_alt(tok
) == p
->tok
)
495 return(REWIND_REWIND
);
502 return(REWIND_NOHALT
);
507 * See if we can break an encountered scope (the rew_dohalt has returned
511 rew_dobreak(int tok
, const struct mdoc_node
*p
)
514 assert(MDOC_ROOT
!= p
->type
);
515 if (MDOC_ELEM
== p
->type
)
517 if (MDOC_TEXT
== p
->type
)
519 if (MDOC_VALID
& p
->flags
)
524 return(MDOC_It
== p
->tok
);
526 return(MDOC_Nd
== p
->tok
);
528 return(MDOC_Ss
== p
->tok
);
530 if (MDOC_Nd
== p
->tok
)
532 if (MDOC_Ss
== p
->tok
)
534 return(MDOC_Sh
== p
->tok
);
536 if (MDOC_It
== p
->tok
)
540 /* XXX - experimental! */
541 if (MDOC_Op
== p
->tok
)
548 if (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
)
549 return(p
->tok
== rew_alt(tok
));
550 else if (MDOC_BLOCK
== p
->type
)
553 return(tok
== p
->tok
);
558 rew_elem(struct mdoc
*mdoc
, int tok
)
563 if (MDOC_ELEM
!= n
->type
)
565 assert(MDOC_ELEM
== n
->type
);
566 assert(tok
== n
->tok
);
568 return(rew_last(mdoc
, n
));
573 rew_sub(enum mdoc_type t
, struct mdoc
*m
,
574 int tok
, int line
, int ppos
)
580 for (n
= m
->last
; n
; n
= n
->parent
) {
581 c
= rew_dohalt(tok
, t
, n
);
582 if (REWIND_HALT
== c
) {
585 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
))
587 return(mdoc_perr(m
, line
, ppos
, ENOCTX
));
589 if (REWIND_REWIND
== c
)
591 else if (rew_dobreak(tok
, n
))
593 if ( ! swarn(m
, t
, line
, ppos
, n
))
598 return(rew_last(m
, n
));
603 append_delims(struct mdoc
*mdoc
, int line
, int *pos
, char *buf
)
613 c
= mdoc_zargs(mdoc
, line
, pos
, buf
, ARGS_NOWARN
, &p
);
614 assert(ARGS_PHRASE
!= c
);
618 else if (ARGS_EOLN
== c
)
620 assert(mdoc_isdelim(p
));
621 if ( ! mdoc_word_alloc(mdoc
, line
, lastarg
, p
))
630 * Close out block partial/full explicit.
633 blk_exp_close(MACRO_PROT_ARGS
)
635 int j
, c
, lastarg
, maxargs
, flushed
;
647 if ( ! (MDOC_CALLABLE
& mdoc_macros
[tok
].flags
)) {
649 if ( ! mdoc_pwarn(m
, line
, ppos
, ENOLINE
))
652 if ( ! rew_sub(MDOC_BODY
, m
, tok
, line
, ppos
))
654 return(rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
));
657 if ( ! rew_sub(MDOC_BODY
, m
, tok
, line
, ppos
))
661 if ( ! mdoc_tail_alloc(m
, line
, ppos
, rew_alt(tok
)))
664 for (flushed
= j
= 0; ; j
++) {
667 if (j
== maxargs
&& ! flushed
) {
668 if ( ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
673 c
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
682 if (MDOC_MAX
!= (c
= lookup(tok
, p
))) {
684 if ( ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
688 if ( ! mdoc_macro(m
, c
, line
, lastarg
, pos
, buf
))
693 if ( ! mdoc_word_alloc(m
, line
, lastarg
, p
))
697 if ( ! flushed
&& ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
702 return(append_delims(m
, line
, pos
, buf
));
707 in_line(MACRO_PROT_ARGS
)
709 int la
, lastpunct
, c
, w
, cnt
, d
, nc
;
710 struct mdoc_arg
*arg
;
714 * Whether we allow ignored elements (those without content,
715 * usually because of reserved words) to squeak by.
736 for (arg
= NULL
;; ) {
738 c
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
740 if (ARGV_WORD
== c
) {
753 for (cnt
= 0, lastpunct
= 1;; ) {
755 w
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
764 /* Quoted words shouldn't be looked-up. */
766 c
= ARGS_QWORD
== w
? MDOC_MAX
: lookup(tok
, p
);
769 * In this case, we've located a submacro and must
770 * execute it. Close out scope, if open. If no
771 * elements have been generated, either create one (nc)
772 * or raise a warning.
776 if (0 == lastpunct
&& ! rew_elem(m
, tok
))
778 if (nc
&& 0 == cnt
) {
779 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
781 if ( ! rew_last(m
, m
->last
))
783 } else if ( ! nc
&& 0 == cnt
) {
785 if ( ! mdoc_pwarn(m
, line
, ppos
, EIGNE
))
788 c
= mdoc_macro(m
, c
, line
, la
, pos
, buf
);
793 return(append_delims(m
, line
, pos
, buf
));
797 * Non-quote-enclosed punctuation. Set up our scope, if
798 * a word; rewind the scope, if a delimiter; then append
804 if (ARGS_QWORD
!= w
&& d
) {
805 if (0 == lastpunct
&& ! rew_elem(m
, tok
))
808 } else if (lastpunct
) {
809 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
816 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
820 * `Fl' macros have their scope re-opened with each new
821 * word so that the `-' can be added to each one without
822 * having to parse out spaces.
824 if (0 == lastpunct
&& MDOC_Fl
== tok
) {
825 if ( ! rew_elem(m
, tok
))
831 if (0 == lastpunct
&& ! rew_elem(m
, tok
))
835 * If no elements have been collected and we're allowed to have
836 * empties (nc), open a scope and close it out. Otherwise,
840 if (nc
&& 0 == cnt
) {
841 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
843 if ( ! rew_last(m
, m
->last
))
845 } else if ( ! nc
&& 0 == cnt
) {
847 if ( ! mdoc_pwarn(m
, line
, ppos
, EIGNE
))
853 return(append_delims(m
, line
, pos
, buf
));
858 blk_full(MACRO_PROT_ARGS
)
860 int c
, lastarg
, reopen
, dohead
;
861 struct mdoc_arg
*arg
;
865 * Whether to process a block-head section. If this is
866 * non-zero, then a head will be opened for all line arguments.
867 * If not, then the head will always be empty and only a body
868 * will be opened, which will stay open at the eoln.
880 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
)) {
881 if ( ! rew_sub(MDOC_BODY
, m
, tok
, line
, ppos
))
883 if ( ! rew_sub(MDOC_BLOCK
, m
, tok
, line
, ppos
))
887 for (arg
= NULL
;; ) {
889 c
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
891 if (ARGV_WORD
== c
) {
905 if ( ! mdoc_block_alloc(m
, line
, ppos
, tok
, arg
))
907 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
910 if ('\0' == buf
[*pos
]) {
911 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
913 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
918 /* Immediately close out head and enter body, if applicable. */
921 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
923 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
927 for (reopen
= 0;; ) {
929 c
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
935 if (ARGS_PHRASE
== c
) {
937 if (reopen
&& ! mdoc_head_alloc(m
, line
, ppos
, tok
))
940 * Phrases are self-contained macro phrases used
941 * in the columnar output of a macro. They need
944 if ( ! phrase(m
, line
, lastarg
, buf
))
946 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
953 if (MDOC_MAX
== (c
= lookup(tok
, p
))) {
954 if ( ! mdoc_word_alloc(m
, line
, lastarg
, p
))
959 if ( ! mdoc_macro(m
, c
, line
, lastarg
, pos
, buf
))
964 if (1 == ppos
&& ! append_delims(m
, line
, pos
, buf
))
967 /* If the body's already open, then just return. */
971 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
973 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
981 blk_part_imp(MACRO_PROT_ARGS
)
985 struct mdoc_node
*blk
; /* saved block context */
986 struct mdoc_node
*body
; /* saved body context */
990 * A macro that spans to the end of the line. This is generally
991 * (but not necessarily) called as the first macro. The block
992 * has a head as the immediate child, which is always empty,
993 * followed by zero or more opening punctuation nodes, then the
994 * body (which may be empty, depending on the macro), then zero
995 * or more closing punctuation nodes.
998 if ( ! mdoc_block_alloc(m
, line
, ppos
, tok
, NULL
))
1003 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
1005 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1009 * Open the body scope "on-demand", that is, after we've
1010 * processed all our the leading delimiters (open parenthesis,
1014 for (body
= NULL
; ; ) {
1016 c
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1018 assert(ARGS_PHRASE
!= c
);
1020 if (ARGS_ERROR
== c
)
1024 if (ARGS_PUNCT
== c
)
1027 if (1 == mdoc_isdelim(p
) && NULL
== body
) {
1028 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1034 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1039 if (MDOC_MAX
!= (c
= lookup(tok
, p
))) {
1040 if ( ! mdoc_macro(m
, c
, line
, la
, pos
, buf
))
1045 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1049 /* Clean-ups to leave in a consistent state. */
1052 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1058 * If we can't rewind to our body, then our scope has already
1059 * been closed by another macro (like `Oc' closing `Op'). This
1060 * is ugly behaviour nodding its head to OpenBSD's overwhelming
1061 * crufty use of `Op' breakage. XXX: DEPRECATE.
1064 for (n
= m
->last
; n
; n
= n
->parent
)
1068 if (NULL
== n
&& ! mdoc_nwarn(m
, body
, EIMPBRK
))
1071 if (n
&& ! rew_last(m
, body
))
1074 /* Standard appending of delimiters. */
1076 if (1 == ppos
&& ! append_delims(m
, line
, pos
, buf
))
1079 /* Rewind scope, if applicable. */
1081 if (n
&& ! rew_last(m
, blk
))
1089 blk_part_exp(MACRO_PROT_ARGS
)
1092 struct mdoc_node
*head
; /* keep track of head */
1093 struct mdoc_node
*body
; /* keep track of body */
1097 * The opening of an explicit macro having zero or more leading
1098 * punctuation nodes; a head with optional single element (the
1099 * case of `Eo'); and a body that may be empty.
1102 if ( ! mdoc_block_alloc(m
, line
, ppos
, tok
, NULL
))
1105 for (head
= body
= NULL
; ; ) {
1107 c
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1109 if (ARGS_ERROR
== c
)
1111 if (ARGS_PUNCT
== c
)
1116 assert(ARGS_PHRASE
!= c
);
1118 /* Flush out leading punctuation. */
1120 if (1 == mdoc_isdelim(p
) && NULL
== head
) {
1121 assert(NULL
== body
);
1122 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1128 assert(NULL
== body
);
1129 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
1135 * `Eo' gobbles any data into the head, but most other
1136 * macros just immediately close out and begin the body.
1141 /* No check whether it's a macro! */
1143 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1146 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1148 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1156 assert(NULL
!= head
&& NULL
!= body
);
1158 if (MDOC_MAX
!= (c
= lookup(tok
, p
))) {
1159 if ( ! mdoc_macro(m
, c
, line
, la
, pos
, buf
))
1164 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1168 /* Clean-up to leave in a consistent state. */
1171 if ( ! mdoc_head_alloc(m
, line
, ppos
, tok
))
1177 if ( ! rew_sub(MDOC_HEAD
, m
, tok
, line
, ppos
))
1179 if ( ! mdoc_body_alloc(m
, line
, ppos
, tok
))
1184 /* Standard appending of delimiters. */
1189 return(append_delims(m
, line
, pos
, buf
));
1194 in_line_argn(MACRO_PROT_ARGS
)
1196 int la
, flushed
, j
, c
, maxargs
;
1197 struct mdoc_arg
*arg
;
1200 /* Fixed maximum arguments per macro, if applicable. */
1220 /* Macro argument processing. */
1222 for (arg
= NULL
;; ) {
1224 c
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
1226 if (ARGV_WORD
== c
) {
1236 mdoc_argv_free(arg
);
1240 /* Open the element scope. */
1242 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
1245 /* Process element arguments. */
1247 for (flushed
= j
= 0; ; j
++) {
1250 if (j
== maxargs
&& ! flushed
) {
1251 if ( ! rew_elem(m
, tok
))
1256 c
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1258 if (ARGS_ERROR
== c
)
1260 if (ARGS_PUNCT
== c
)
1265 if (MDOC_MAX
!= (c
= lookup(tok
, p
))) {
1266 if ( ! flushed
&& ! rew_elem(m
, tok
))
1269 if ( ! mdoc_macro(m
, c
, line
, la
, pos
, buf
))
1274 if ( ! (MDOC_IGNDELIM
& mdoc_macros
[tok
].flags
) &&
1275 ! flushed
&& mdoc_isdelim(p
)) {
1276 if ( ! rew_elem(m
, tok
))
1282 * XXX: this is a hack to work around groff's ugliness
1283 * as regards `Xr' and extraneous arguments. It should
1284 * ideally be deprecated behaviour, but because this is
1285 * code is no here, it's unlikely to be removed.
1288 if (MDOC_Xr
== tok
&& j
== maxargs
) {
1289 if ( ! mdoc_elem_alloc(m
, line
, ppos
, MDOC_Ns
, NULL
))
1291 if ( ! rew_elem(m
, MDOC_Ns
))
1295 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1299 /* Close out and append delimiters. */
1301 if ( ! flushed
&& ! rew_elem(m
, tok
))
1306 return(append_delims(m
, line
, pos
, buf
));
1311 in_line_eoln(MACRO_PROT_ARGS
)
1314 struct mdoc_arg
*arg
;
1317 assert( ! (MDOC_PARSED
& mdoc_macros
[tok
].flags
));
1319 /* Parse macro arguments. */
1321 for (arg
= NULL
; ; ) {
1323 c
= mdoc_argv(m
, line
, tok
, &arg
, pos
, buf
);
1325 if (ARGV_WORD
== c
) {
1334 mdoc_argv_free(arg
);
1338 /* Open element scope. */
1340 if ( ! mdoc_elem_alloc(m
, line
, ppos
, tok
, arg
))
1343 /* Parse argument terms. */
1347 w
= mdoc_args(m
, line
, pos
, buf
, tok
, &p
);
1349 if (ARGS_ERROR
== w
)
1354 c
= ARGS_QWORD
== w
? MDOC_MAX
: lookup(tok
, p
);
1356 if (MDOC_MAX
!= c
) {
1357 if ( ! rew_elem(m
, tok
))
1359 return(mdoc_macro(m
, c
, line
, la
, pos
, buf
));
1362 if ( ! mdoc_word_alloc(m
, line
, la
, p
))
1366 /* Close out (no delimiters). */
1368 return(rew_elem(m
, tok
));
1374 ctx_synopsis(MACRO_PROT_ARGS
)
1377 /* If we're not in the SYNOPSIS, go straight to in-line. */
1378 if (SEC_SYNOPSIS
!= m
->lastsec
)
1379 return(in_line(m
, tok
, line
, ppos
, pos
, buf
));
1381 /* If we're a nested call, same place. */
1383 return(in_line(m
, tok
, line
, ppos
, pos
, buf
));
1386 * XXX: this will open a block scope; however, if later we end
1387 * up formatting the block scope, then child nodes will inherit
1388 * the formatting. Be careful.
1391 return(blk_part_imp(m
, tok
, line
, ppos
, pos
, buf
));
1397 obsolete(MACRO_PROT_ARGS
)
1400 return(mdoc_pwarn(m
, line
, ppos
, EOBS
));
1405 * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
1406 * They're unusual because they're basically free-form text until a
1407 * macro is encountered.
1410 phrase(struct mdoc
*m
, int line
, int ppos
, char *buf
)
1415 for (pos
= ppos
; ; ) {
1418 /* Note: no calling context! */
1419 w
= mdoc_zargs(m
, line
, &pos
, buf
, 0, &p
);
1421 if (ARGS_ERROR
== w
)
1426 c
= ARGS_QWORD
== w
? MDOC_MAX
: lookup_raw(p
);
1428 if (MDOC_MAX
!= c
) {
1429 if ( ! mdoc_macro(m
, c
, line
, la
, &pos
, buf
))
1431 return(append_delims(m
, line
, &pos
, buf
));
1434 if ( ! mdoc_word_alloc(m
, line
, la
, p
))