]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_macro.c
1 /* $Id: mdoc_macro.c,v 1.135 2014/07/02 20:19:11 schwarze Exp $ */
3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2012, 2013 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
32 #include "libmandoc.h"
34 enum rew
{ /* see rew_dohalt() */
43 static int blk_full(MACRO_PROT_ARGS
);
44 static int blk_exp_close(MACRO_PROT_ARGS
);
45 static int blk_part_exp(MACRO_PROT_ARGS
);
46 static int blk_part_imp(MACRO_PROT_ARGS
);
47 static int ctx_synopsis(MACRO_PROT_ARGS
);
48 static int in_line_eoln(MACRO_PROT_ARGS
);
49 static int in_line_argn(MACRO_PROT_ARGS
);
50 static int in_line(MACRO_PROT_ARGS
);
51 static int phrase_ta(MACRO_PROT_ARGS
);
53 static int dword(struct mdoc
*, int, int, const char *,
55 static int append_delims(struct mdoc
*,
57 static enum mdoct
lookup(enum mdoct
, const char *);
58 static enum mdoct
lookup_raw(const char *);
59 static int make_pending(struct mdoc_node
*, enum mdoct
,
60 struct mdoc
*, int, int);
61 static int phrase(struct mdoc
*, int, int, char *);
62 static enum mdoct
rew_alt(enum mdoct
);
63 static enum rew
rew_dohalt(enum mdoct
, enum mdoc_type
,
64 const struct mdoc_node
*);
65 static int rew_elem(struct mdoc
*, enum mdoct
);
66 static int rew_last(struct mdoc
*,
67 const struct mdoc_node
*);
68 static int rew_sub(enum mdoc_type
, struct mdoc
*,
69 enum mdoct
, int, int);
71 const struct mdoc_macro __mdoc_macros
[MDOC_MAX
] = {
72 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_JOIN
}, /* Ap */
73 { in_line_eoln
, MDOC_PROLOGUE
}, /* Dd */
74 { in_line_eoln
, MDOC_PROLOGUE
}, /* Dt */
75 { in_line_eoln
, MDOC_PROLOGUE
}, /* Os */
76 { blk_full
, MDOC_PARSED
| MDOC_JOIN
}, /* Sh */
77 { blk_full
, MDOC_PARSED
| MDOC_JOIN
}, /* Ss */
78 { in_line_eoln
, 0 }, /* Pp */
79 { blk_part_imp
, MDOC_PARSED
| MDOC_JOIN
}, /* D1 */
80 { blk_part_imp
, MDOC_PARSED
| MDOC_JOIN
}, /* Dl */
81 { blk_full
, MDOC_EXPLICIT
}, /* Bd */
82 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_JOIN
}, /* Ed */
83 { blk_full
, MDOC_EXPLICIT
}, /* Bl */
84 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_JOIN
}, /* El */
85 { blk_full
, MDOC_PARSED
| MDOC_JOIN
}, /* It */
86 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ad */
87 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_JOIN
}, /* An */
88 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ar */
89 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_JOIN
}, /* Cd */
90 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Cm */
91 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Dv */
92 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Er */
93 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ev */
94 { in_line_eoln
, 0 }, /* Ex */
95 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fa */
96 { in_line_eoln
, 0 }, /* Fd */
97 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fl */
98 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fn */
99 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ft */
100 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_JOIN
}, /* Ic */
101 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* In */
102 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_JOIN
}, /* Li */
103 { blk_full
, MDOC_JOIN
}, /* Nd */
104 { ctx_synopsis
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Nm */
105 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Op */
106 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ot */
107 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Pa */
108 { in_line_eoln
, 0 }, /* Rv */
109 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* St */
110 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Va */
111 { ctx_synopsis
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Vt */
112 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Xr */
113 { in_line_eoln
, MDOC_JOIN
}, /* %A */
114 { in_line_eoln
, MDOC_JOIN
}, /* %B */
115 { in_line_eoln
, MDOC_JOIN
}, /* %D */
116 { in_line_eoln
, MDOC_JOIN
}, /* %I */
117 { in_line_eoln
, MDOC_JOIN
}, /* %J */
118 { in_line_eoln
, 0 }, /* %N */
119 { in_line_eoln
, MDOC_JOIN
}, /* %O */
120 { in_line_eoln
, 0 }, /* %P */
121 { in_line_eoln
, MDOC_JOIN
}, /* %R */
122 { in_line_eoln
, MDOC_JOIN
}, /* %T */
123 { in_line_eoln
, 0 }, /* %V */
124 { blk_exp_close
, MDOC_CALLABLE
| MDOC_PARSED
|
125 MDOC_EXPLICIT
| MDOC_JOIN
}, /* Ac */
126 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
|
127 MDOC_EXPLICIT
| MDOC_JOIN
}, /* Ao */
128 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_JOIN
}, /* Aq */
129 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* At */
130 { blk_exp_close
, MDOC_CALLABLE
| MDOC_PARSED
|
131 MDOC_EXPLICIT
| MDOC_JOIN
}, /* Bc */
132 { blk_full
, MDOC_EXPLICIT
}, /* Bf */
133 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
|
134 MDOC_EXPLICIT
| MDOC_JOIN
}, /* Bo */
135 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_JOIN
}, /* Bq */
136 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Bsx */
137 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Bx */
138 { in_line_eoln
, 0 }, /* Db */
139 { blk_exp_close
, MDOC_CALLABLE
| MDOC_PARSED
|
140 MDOC_EXPLICIT
| MDOC_JOIN
}, /* Dc */
141 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
|
142 MDOC_EXPLICIT
| MDOC_JOIN
}, /* Do */
143 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_JOIN
}, /* Dq */
144 { blk_exp_close
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Ec */
145 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_JOIN
}, /* Ef */
146 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_JOIN
}, /* Em */
147 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Eo */
148 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fx */
149 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ms */
150 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
|
151 MDOC_IGNDELIM
| MDOC_JOIN
}, /* No */
152 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
|
153 MDOC_IGNDELIM
| MDOC_JOIN
}, /* Ns */
154 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Nx */
155 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ox */
156 { blk_exp_close
, MDOC_CALLABLE
| MDOC_PARSED
|
157 MDOC_EXPLICIT
| MDOC_JOIN
}, /* Pc */
158 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_IGNDELIM
}, /* Pf */
159 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
|
160 MDOC_EXPLICIT
| MDOC_JOIN
}, /* Po */
161 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_JOIN
}, /* Pq */
162 { blk_exp_close
, MDOC_CALLABLE
| MDOC_PARSED
|
163 MDOC_EXPLICIT
| MDOC_JOIN
}, /* Qc */
164 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_JOIN
}, /* Ql */
165 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
|
166 MDOC_EXPLICIT
| MDOC_JOIN
}, /* Qo */
167 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_JOIN
}, /* Qq */
168 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_JOIN
}, /* Re */
169 { blk_full
, MDOC_EXPLICIT
}, /* Rs */
170 { blk_exp_close
, MDOC_CALLABLE
| MDOC_PARSED
|
171 MDOC_EXPLICIT
| MDOC_JOIN
}, /* Sc */
172 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
|
173 MDOC_EXPLICIT
| MDOC_JOIN
}, /* So */
174 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_JOIN
}, /* Sq */
175 { in_line_eoln
, 0 }, /* Sm */
176 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_JOIN
}, /* Sx */
177 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_JOIN
}, /* Sy */
178 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Tn */
179 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_JOIN
}, /* Ux */
180 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Xc */
181 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Xo */
182 { blk_full
, MDOC_EXPLICIT
| MDOC_CALLABLE
}, /* Fo */
183 { blk_exp_close
, MDOC_CALLABLE
| MDOC_PARSED
|
184 MDOC_EXPLICIT
| MDOC_JOIN
}, /* Fc */
185 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
|
186 MDOC_EXPLICIT
| MDOC_JOIN
}, /* Oo */
187 { blk_exp_close
, MDOC_CALLABLE
| MDOC_PARSED
|
188 MDOC_EXPLICIT
| MDOC_JOIN
}, /* Oc */
189 { blk_full
, MDOC_EXPLICIT
}, /* Bk */
190 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_JOIN
}, /* Ek */
191 { in_line_eoln
, 0 }, /* Bt */
192 { in_line_eoln
, 0 }, /* Hf */
193 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fr */
194 { in_line_eoln
, 0 }, /* Ud */
195 { in_line
, 0 }, /* Lb */
196 { in_line_eoln
, 0 }, /* Lp */
197 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Lk */
198 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Mt */
199 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_JOIN
}, /* Brq */
200 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
|
201 MDOC_EXPLICIT
| MDOC_JOIN
}, /* Bro */
202 { blk_exp_close
, MDOC_CALLABLE
| MDOC_PARSED
|
203 MDOC_EXPLICIT
| MDOC_JOIN
}, /* Brc */
204 { in_line_eoln
, MDOC_JOIN
}, /* %C */
205 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Es */
206 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_JOIN
}, /* En */
207 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Dx */
208 { in_line_eoln
, MDOC_JOIN
}, /* %Q */
209 { in_line_eoln
, 0 }, /* br */
210 { in_line_eoln
, 0 }, /* sp */
211 { in_line_eoln
, 0 }, /* %U */
212 { phrase_ta
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_JOIN
}, /* Ta */
213 { in_line_eoln
, 0 }, /* ll */
216 const struct mdoc_macro
* const mdoc_macros
= __mdoc_macros
;
220 * This is called at the end of parsing. It must traverse up the tree,
221 * closing out open [implicit] scopes. Obviously, open explicit scopes
225 mdoc_macroend(struct mdoc
*mdoc
)
229 /* Scan for open explicit scopes. */
231 n
= MDOC_VALID
& mdoc
->last
->flags
?
232 mdoc
->last
->parent
: mdoc
->last
;
234 for ( ; n
; n
= n
->parent
)
235 if (MDOC_BLOCK
== n
->type
&&
236 MDOC_EXPLICIT
& mdoc_macros
[n
->tok
].flags
)
237 mdoc_nmsg(mdoc
, n
, MANDOCERR_SCOPEEXIT
);
239 /* Rewind to the first. */
241 return(rew_last(mdoc
, mdoc
->first
));
245 * Look up a macro from within a subsequent context.
248 lookup(enum mdoct from
, const char *p
)
251 if ( ! (MDOC_PARSED
& mdoc_macros
[from
].flags
))
253 return(lookup_raw(p
));
257 * Lookup a macro following the initial line macro.
260 lookup_raw(const char *p
)
264 if (MDOC_MAX
== (res
= mdoc_hash_find(p
)))
266 if (MDOC_CALLABLE
& mdoc_macros
[res
].flags
)
272 rew_last(struct mdoc
*mdoc
, const struct mdoc_node
*to
)
274 struct mdoc_node
*n
, *np
;
277 mdoc
->next
= MDOC_NEXT_SIBLING
;
280 while (mdoc
->last
!= to
) {
282 * Save the parent here, because we may delete the
283 * mdoc->last node in the post-validation phase and reset
284 * it to mdoc->last->parent, causing a step in the closing
287 np
= mdoc
->last
->parent
;
288 if ( ! mdoc_valid_post(mdoc
))
293 mdoc
->last
->last
= n
;
296 return(mdoc_valid_post(mdoc
));
300 * For a block closing macro, return the corresponding opening one.
301 * Otherwise, return the macro itself.
304 rew_alt(enum mdoct tok
)
346 * Rewinding to tok, how do we have to handle *p?
347 * REWIND_NONE: *p would delimit tok, but no tok scope is open
348 * inside *p, so there is no need to rewind anything at all.
349 * REWIND_THIS: *p matches tok, so rewind *p and nothing else.
350 * REWIND_MORE: *p is implicit, rewind it and keep searching for tok.
351 * REWIND_FORCE: *p is explicit, but tok is full, force rewinding *p.
352 * REWIND_LATER: *p is explicit and still open, postpone rewinding.
353 * REWIND_ERROR: No tok block is open at all.
356 rew_dohalt(enum mdoct tok
, enum mdoc_type type
,
357 const struct mdoc_node
*p
)
361 * No matching token, no delimiting block, no broken block.
362 * This can happen when full implicit macros are called for
363 * the first time but try to rewind their previous
366 if (MDOC_ROOT
== p
->type
)
367 return(MDOC_BLOCK
== type
&&
368 MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
?
369 REWIND_ERROR
: REWIND_NONE
);
372 * When starting to rewind, skip plain text
373 * and nodes that have already been rewound.
375 if (MDOC_TEXT
== p
->type
|| MDOC_VALID
& p
->flags
)
379 * The easiest case: Found a matching token.
380 * This applies to both blocks and elements.
384 return(p
->end
? REWIND_NONE
:
385 type
== p
->type
? REWIND_THIS
: REWIND_MORE
);
388 * While elements do require rewinding for themselves,
389 * they never affect rewinding of other nodes.
391 if (MDOC_ELEM
== p
->type
)
395 * Blocks delimited by our target token get REWIND_MORE.
396 * Blocks delimiting our target token get REWIND_NONE.
400 if (MDOC_It
== p
->tok
)
404 if (MDOC_BODY
== p
->type
&& MDOC_Bl
== p
->tok
)
408 * XXX Badly nested block handling still fails badly
409 * when one block is breaking two blocks of the same type.
410 * This is an incomplete and extremely ugly workaround,
411 * required to let the OpenBSD tree build.
414 if (MDOC_Op
== p
->tok
)
422 if (MDOC_BODY
== p
->type
&& MDOC_Sh
== p
->tok
)
426 if (MDOC_Nd
== p
->tok
|| MDOC_Ss
== p
->tok
||
435 * Default block rewinding rules.
436 * In particular, always skip block end markers,
437 * and let all blocks rewind Nm children.
439 if (ENDBODY_NOT
!= p
->end
|| MDOC_Nm
== p
->tok
||
440 (MDOC_BLOCK
== p
->type
&&
441 ! (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
)))
445 * By default, closing out full blocks
446 * forces closing of broken explicit blocks,
447 * while closing out partial blocks
448 * allows delayed rewinding by default.
450 return (&blk_full
== mdoc_macros
[tok
].fp
?
451 REWIND_FORCE
: REWIND_LATER
);
455 rew_elem(struct mdoc
*mdoc
, enum mdoct tok
)
460 if (MDOC_ELEM
!= n
->type
)
462 assert(MDOC_ELEM
== n
->type
);
463 assert(tok
== n
->tok
);
465 return(rew_last(mdoc
, n
));
469 * We are trying to close a block identified by tok,
470 * but the child block *broken is still open.
471 * Thus, postpone closing the tok block
472 * until the rew_sub call closing *broken.
475 make_pending(struct mdoc_node
*broken
, enum mdoct tok
,
476 struct mdoc
*mdoc
, int line
, int ppos
)
478 struct mdoc_node
*breaker
;
481 * Iterate backwards, searching for the block matching tok,
482 * that is, the block breaking the *broken block.
484 for (breaker
= broken
->parent
; breaker
; breaker
= breaker
->parent
) {
487 * If the *broken block had already been broken before
488 * and we encounter its breaker, make the tok block
489 * pending on the inner breaker.
490 * Graphically, "[A breaker=[B broken=[C->B B] tok=A] C]"
491 * becomes "[A broken=[B [C->B B] tok=A] C]"
492 * and finally "[A [B->A [C->B B] A] C]".
494 if (breaker
== broken
->pending
) {
499 if (REWIND_THIS
!= rew_dohalt(tok
, MDOC_BLOCK
, breaker
))
501 if (MDOC_BODY
== broken
->type
)
502 broken
= broken
->parent
;
506 * If another, outer breaker is already pending on
507 * the *broken block, we must not clobber the link
508 * to the outer breaker, but make it pending on the
509 * new, now inner breaker.
510 * Graphically, "[A breaker=[B broken=[C->A A] tok=B] C]"
511 * becomes "[A breaker=[B->A broken=[C A] tok=B] C]"
512 * and finally "[A [B->A [C->B A] B] C]".
514 if (broken
->pending
) {
515 struct mdoc_node
*taker
;
518 * If the breaker had also been broken before,
519 * it cannot take on the outer breaker itself,
520 * but must hand it on to its own breakers.
521 * Graphically, this is the following situation:
522 * "[A [B breaker=[C->B B] broken=[D->A A] tok=C] D]"
523 * "[A taker=[B->A breaker=[C->B B] [D->C A] C] D]"
526 while (taker
->pending
)
527 taker
= taker
->pending
;
528 taker
->pending
= broken
->pending
;
530 broken
->pending
= breaker
;
531 mandoc_vmsg(MANDOCERR_BLOCK_NEST
, mdoc
->parse
, line
, ppos
,
532 "%s breaks %s", mdoc_macronames
[tok
],
533 mdoc_macronames
[broken
->tok
]);
538 * Found no matching block for tok.
539 * Are you trying to close a block that is not open?
545 rew_sub(enum mdoc_type t
, struct mdoc
*mdoc
,
546 enum mdoct tok
, int line
, int ppos
)
552 switch (rew_dohalt(tok
, t
, n
)) {
557 (MDOC_NEWLINE
& mdoc
->flags
&&
558 ! (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
));
561 mandoc_vmsg(MANDOCERR_SCOPEBROKEN
, mdoc
->parse
,
562 line
, ppos
, "%s breaks %s",
563 mdoc_macronames
[tok
],
564 mdoc_macronames
[n
->tok
]);
568 (MDOC_NEWLINE
& mdoc
->flags
? 1 : 0);
572 if (make_pending(n
, tok
, mdoc
, line
, ppos
) ||
577 mdoc_pmsg(mdoc
, line
, ppos
, MANDOCERR_NOSCOPE
);
584 if ( ! rew_last(mdoc
, n
))
588 * The current block extends an enclosing block.
589 * Now that the current block ends, close the enclosing block, too.
591 while (NULL
!= (n
= n
->pending
)) {
592 if ( ! rew_last(mdoc
, n
))
594 if (MDOC_HEAD
== n
->type
&&
595 ! mdoc_body_alloc(mdoc
, n
->line
, n
->pos
, n
->tok
))
603 * Allocate a word and check whether it's punctuation or not.
604 * Punctuation consists of those tokens found in mdoc_isdelim().
607 dword(struct mdoc
*mdoc
, int line
, int col
, const char *p
,
608 enum mdelim d
, int may_append
)
615 ! ((MDOC_SYNOPSIS
| MDOC_KEEP
| MDOC_SMOFF
) & mdoc
->flags
) &&
616 DELIM_NONE
== d
&& MDOC_TEXT
== mdoc
->last
->type
&&
617 DELIM_NONE
== mdoc_isdelim(mdoc
->last
->string
)) {
618 mdoc_word_append(mdoc
, p
);
622 if ( ! mdoc_word_alloc(mdoc
, line
, col
, p
))
626 mdoc
->last
->flags
|= MDOC_DELIMO
;
629 * Closing delimiters only suppress the preceding space
630 * when they follow something, not when they start a new
631 * block or element, and not when they follow `No'.
633 * XXX Explicitly special-casing MDOC_No here feels
634 * like a layering violation. Find a better way
635 * and solve this in the code related to `No'!
638 else if (DELIM_CLOSE
== d
&& mdoc
->last
->prev
&&
639 mdoc
->last
->prev
->tok
!= MDOC_No
&&
640 mdoc
->last
->parent
->tok
!= MDOC_Fd
)
641 mdoc
->last
->flags
|= MDOC_DELIMC
;
647 append_delims(struct mdoc
*mdoc
, int line
, int *pos
, char *buf
)
653 if ('\0' == buf
[*pos
])
658 ac
= mdoc_zargs(mdoc
, line
, pos
, buf
, &p
);
660 if (ARGS_ERROR
== ac
)
662 else if (ARGS_EOLN
== ac
)
665 dword(mdoc
, line
, la
, p
, DELIM_MAX
, 1);
668 * If we encounter end-of-sentence symbols, then trigger
671 * XXX: it's easy to allow this to propagate outward to
672 * the last symbol, such that `. )' will cause the
673 * correct double-spacing. However, (1) groff isn't
674 * smart enough to do this and (2) it would require
675 * knowing which symbols break this behaviour, for
676 * example, `. ;' shouldn't propagate the double-space.
678 if (mandoc_eos(p
, strlen(p
)))
679 mdoc
->last
->flags
|= MDOC_EOS
;
686 * Close out block partial/full explicit.
689 blk_exp_close(MACRO_PROT_ARGS
)
691 struct mdoc_node
*body
; /* Our own body. */
692 struct mdoc_node
*later
; /* A sub-block starting later. */
693 struct mdoc_node
*n
; /* For searching backwards. */
695 int j
, lastarg
, maxargs
, flushed
, nl
;
697 enum mdoct atok
, ntok
;
700 nl
= MDOC_NEWLINE
& mdoc
->flags
;
707 mdoc
->flags
&= ~MDOC_KEEP
;
714 * Search backwards for beginnings of blocks,
715 * both of our own and of pending sub-blocks.
719 for (n
= mdoc
->last
; n
; n
= n
->parent
) {
720 if (MDOC_VALID
& n
->flags
)
723 /* Remember the start of our own body. */
724 if (MDOC_BODY
== n
->type
&& atok
== n
->tok
) {
725 if (ENDBODY_NOT
== n
->end
)
730 if (MDOC_BLOCK
!= n
->type
|| MDOC_Nm
== n
->tok
)
732 if (atok
== n
->tok
) {
736 * Found the start of our own block.
737 * When there is no pending sub block,
738 * just proceed to closing out.
744 * When there is a pending sub block,
745 * postpone closing out the current block
746 * until the rew_sub() closing out the sub-block.
748 make_pending(later
, tok
, mdoc
, line
, ppos
);
751 * Mark the place where the formatting - but not
752 * the scope - of the current block ends.
754 if ( ! mdoc_endbody_alloc(mdoc
, line
, ppos
,
755 atok
, body
, ENDBODY_SPACE
))
761 * When finding an open sub block, remember the last
762 * open explicit block, or, in case there are only
763 * implicit ones, the first open implicit block.
766 MDOC_EXPLICIT
& mdoc_macros
[later
->tok
].flags
)
768 if (MDOC_It
!= n
->tok
)
772 if ( ! (MDOC_CALLABLE
& mdoc_macros
[tok
].flags
)) {
773 /* FIXME: do this in validate */
775 mdoc_pmsg(mdoc
, line
, ppos
, MANDOCERR_ARGSLOST
);
777 if ( ! rew_sub(MDOC_BODY
, mdoc
, tok
, line
, ppos
))
779 return(rew_sub(MDOC_BLOCK
, mdoc
, tok
, line
, ppos
));
782 if ( ! rew_sub(MDOC_BODY
, mdoc
, tok
, line
, ppos
))
785 if (NULL
== later
&& maxargs
> 0)
786 if ( ! mdoc_tail_alloc(mdoc
, line
, ppos
, rew_alt(tok
)))
789 for (flushed
= j
= 0; ; j
++) {
792 if (j
== maxargs
&& ! flushed
) {
793 if ( ! rew_sub(MDOC_BLOCK
, mdoc
, tok
, line
, ppos
))
798 ac
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
800 if (ARGS_ERROR
== ac
)
802 if (ARGS_PUNCT
== ac
)
807 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
809 if (MDOC_MAX
== ntok
) {
810 if ( ! dword(mdoc
, line
, lastarg
, p
, DELIM_MAX
,
811 MDOC_JOIN
& mdoc_macros
[tok
].flags
))
817 if ( ! rew_sub(MDOC_BLOCK
, mdoc
, tok
, line
, ppos
))
822 mdoc
->flags
&= ~MDOC_NEWLINE
;
824 if ( ! mdoc_macro(mdoc
, ntok
, line
, lastarg
, pos
, buf
))
829 if ( ! flushed
&& ! rew_sub(MDOC_BLOCK
, mdoc
, tok
, line
, ppos
))
834 return(append_delims(mdoc
, line
, pos
, buf
));
838 in_line(MACRO_PROT_ARGS
)
840 int la
, scope
, cnt
, nc
, nl
;
845 struct mdoc_arg
*arg
;
848 nl
= MDOC_NEWLINE
& mdoc
->flags
;
851 * Whether we allow ignored elements (those without content,
852 * usually because of reserved words) to squeak by.
874 for (arg
= NULL
;; ) {
876 av
= mdoc_argv(mdoc
, line
, tok
, &arg
, pos
, buf
);
878 if (ARGV_WORD
== av
) {
891 for (cnt
= scope
= 0;; ) {
893 ac
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
895 if (ARGS_ERROR
== ac
)
899 if (ARGS_PUNCT
== ac
)
902 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
905 * In this case, we've located a submacro and must
906 * execute it. Close out scope, if open. If no
907 * elements have been generated, either create one (nc)
908 * or raise a warning.
911 if (MDOC_MAX
!= ntok
) {
912 if (scope
&& ! rew_elem(mdoc
, tok
))
914 if (nc
&& 0 == cnt
) {
915 if ( ! mdoc_elem_alloc(mdoc
,
916 line
, ppos
, tok
, arg
))
918 if ( ! rew_last(mdoc
, mdoc
->last
))
920 } else if ( ! nc
&& 0 == cnt
) {
922 mandoc_msg(MANDOCERR_MACRO_EMPTY
,
923 mdoc
->parse
, line
, ppos
,
924 mdoc_macronames
[tok
]);
927 if ( ! mdoc_macro(mdoc
, ntok
, line
, la
, pos
, buf
))
931 return(append_delims(mdoc
, line
, pos
, buf
));
935 * Non-quote-enclosed punctuation. Set up our scope, if
936 * a word; rewind the scope, if a delimiter; then append
940 d
= ARGS_QWORD
== ac
? DELIM_NONE
: mdoc_isdelim(p
);
942 if (DELIM_NONE
!= d
) {
944 * If we encounter closing punctuation, no word
945 * has been omitted, no scope is open, and we're
946 * allowed to have an empty element, then start
947 * a new scope. `Ar', `Fl', and `Li', only do
948 * this once per invocation. There may be more
949 * of these (all of them?).
951 if (0 == cnt
&& (nc
|| MDOC_Li
== tok
) &&
952 DELIM_CLOSE
== d
&& ! scope
) {
953 if ( ! mdoc_elem_alloc(mdoc
,
954 line
, ppos
, tok
, arg
))
956 if (MDOC_Ar
== tok
|| MDOC_Li
== tok
||
962 * Close out our scope, if one is open, before
965 if (scope
&& ! rew_elem(mdoc
, tok
))
968 } else if ( ! scope
) {
969 if ( ! mdoc_elem_alloc(mdoc
, line
, ppos
, tok
, arg
))
977 if ( ! dword(mdoc
, line
, la
, p
, d
,
978 MDOC_JOIN
& mdoc_macros
[tok
].flags
))
982 * `Fl' macros have their scope re-opened with each new
983 * word so that the `-' can be added to each one without
984 * having to parse out spaces.
986 if (scope
&& MDOC_Fl
== tok
) {
987 if ( ! rew_elem(mdoc
, tok
))
993 if (scope
&& ! rew_elem(mdoc
, tok
))
997 * If no elements have been collected and we're allowed to have
998 * empties (nc), open a scope and close it out. Otherwise,
1002 if (nc
&& 0 == cnt
) {
1003 if ( ! mdoc_elem_alloc(mdoc
, line
, ppos
, tok
, arg
))
1005 if ( ! rew_last(mdoc
, mdoc
->last
))
1007 } else if ( ! nc
&& 0 == cnt
) {
1008 mdoc_argv_free(arg
);
1009 mandoc_msg(MANDOCERR_MACRO_EMPTY
, mdoc
->parse
,
1010 line
, ppos
, mdoc_macronames
[tok
]);
1015 return(append_delims(mdoc
, line
, pos
, buf
));
1019 blk_full(MACRO_PROT_ARGS
)
1021 int la
, nl
, nparsed
;
1022 struct mdoc_arg
*arg
;
1023 struct mdoc_node
*head
; /* save of head macro */
1024 struct mdoc_node
*body
; /* save of body macro */
1025 struct mdoc_node
*n
;
1028 enum margserr ac
, lac
;
1032 nl
= MDOC_NEWLINE
& mdoc
->flags
;
1034 /* Close out prior implicit scope. */
1036 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
)) {
1037 if ( ! rew_sub(MDOC_BODY
, mdoc
, tok
, line
, ppos
))
1039 if ( ! rew_sub(MDOC_BLOCK
, mdoc
, tok
, line
, ppos
))
1044 * This routine accommodates implicitly- and explicitly-scoped
1045 * macro openings. Implicit ones first close out prior scope
1046 * (seen above). Delay opening the head until necessary to
1047 * allow leading punctuation to print. Special consideration
1048 * for `It -column', which has phrase-part syntax instead of
1049 * regular child nodes.
1052 for (arg
= NULL
;; ) {
1054 av
= mdoc_argv(mdoc
, line
, tok
, &arg
, pos
, buf
);
1056 if (ARGV_WORD
== av
) {
1061 if (ARGV_EOLN
== av
)
1066 mdoc_argv_free(arg
);
1070 if ( ! mdoc_block_alloc(mdoc
, line
, ppos
, tok
, arg
))
1076 * Exception: Heads of `It' macros in `-diag' lists are not
1077 * parsed, even though `It' macros in general are parsed.
1079 nparsed
= MDOC_It
== tok
&&
1080 MDOC_Bl
== mdoc
->last
->parent
->tok
&&
1081 LIST_diag
== mdoc
->last
->parent
->norm
->Bl
.type
;
1084 * The `Nd' macro has all arguments in its body: it's a hybrid
1085 * of block partial-explicit and full-implicit. Stupid.
1088 if (MDOC_Nd
== tok
) {
1089 if ( ! mdoc_head_alloc(mdoc
, line
, ppos
, tok
))
1092 if ( ! rew_sub(MDOC_HEAD
, mdoc
, tok
, line
, ppos
))
1094 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
1100 mdoc
->flags
|= MDOC_KEEP
;
1106 /* Initialise last-phrase-type with ARGS_PEND. */
1107 lac
= ARGS_ERROR
== ac
? ARGS_PEND
: ac
;
1108 ac
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
1110 if (ARGS_PUNCT
== ac
)
1113 if (ARGS_ERROR
== ac
)
1116 if (ARGS_EOLN
== ac
) {
1117 if (ARGS_PPHRASE
!= lac
&& ARGS_PHRASE
!= lac
)
1120 * This is necessary: if the last token on a
1121 * line is a `Ta' or tab, then we'll get
1122 * ARGS_EOLN, so we must be smart enough to
1123 * reopen our scope if the last parse was a
1124 * phrase or partial phrase.
1126 if ( ! rew_sub(MDOC_BODY
, mdoc
, tok
, line
, ppos
))
1128 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
1135 * Emit leading punctuation (i.e., punctuation before
1136 * the MDOC_HEAD) for non-phrase types.
1141 ARGS_PHRASE
!= ac
&&
1142 ARGS_PPHRASE
!= ac
&&
1144 DELIM_OPEN
== mdoc_isdelim(p
)) {
1145 if ( ! dword(mdoc
, line
, la
, p
, DELIM_OPEN
, 0))
1150 /* Open a head if one hasn't been opened. */
1153 if ( ! mdoc_head_alloc(mdoc
, line
, ppos
, tok
))
1158 if (ARGS_PHRASE
== ac
||
1160 ARGS_PPHRASE
== ac
) {
1162 * If we haven't opened a body yet, rewind the
1163 * head; if we have, rewind that instead.
1166 mtt
= body
? MDOC_BODY
: MDOC_HEAD
;
1167 if ( ! rew_sub(mtt
, mdoc
, tok
, line
, ppos
))
1170 /* Then allocate our body context. */
1172 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
1177 * Process phrases: set whether we're in a
1178 * partial-phrase (this effects line handling)
1179 * then call down into the phrase parser.
1182 if (ARGS_PPHRASE
== ac
)
1183 mdoc
->flags
|= MDOC_PPHRASE
;
1184 if (ARGS_PEND
== ac
&& ARGS_PPHRASE
== lac
)
1185 mdoc
->flags
|= MDOC_PPHRASE
;
1187 if ( ! phrase(mdoc
, line
, la
, buf
))
1190 mdoc
->flags
&= ~MDOC_PPHRASE
;
1194 ntok
= nparsed
|| ARGS_QWORD
== ac
?
1195 MDOC_MAX
: lookup(tok
, p
);
1197 if (MDOC_MAX
== ntok
) {
1198 if ( ! dword(mdoc
, line
, la
, p
, DELIM_MAX
,
1199 MDOC_JOIN
& mdoc_macros
[tok
].flags
))
1204 if ( ! mdoc_macro(mdoc
, ntok
, line
, la
, pos
, buf
))
1210 if ( ! mdoc_head_alloc(mdoc
, line
, ppos
, tok
))
1215 if (nl
&& ! append_delims(mdoc
, line
, pos
, buf
))
1218 /* If we've already opened our body, exit now. */
1224 * If there is an open (i.e., unvalidated) sub-block requiring
1225 * explicit close-out, postpone switching the current block from
1226 * head to body until the rew_sub() call closing out that
1229 for (n
= mdoc
->last
; n
&& n
!= head
; n
= n
->parent
) {
1230 if (MDOC_BLOCK
== n
->type
&&
1231 MDOC_EXPLICIT
& mdoc_macros
[n
->tok
].flags
&&
1232 ! (MDOC_VALID
& n
->flags
)) {
1238 /* Close out scopes to remain in a consistent state. */
1240 if ( ! rew_sub(MDOC_HEAD
, mdoc
, tok
, line
, ppos
))
1242 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
1246 if ( ! (MDOC_FREECOL
& mdoc
->flags
))
1249 if ( ! rew_sub(MDOC_BODY
, mdoc
, tok
, line
, ppos
))
1251 if ( ! rew_sub(MDOC_BLOCK
, mdoc
, tok
, line
, ppos
))
1254 mdoc
->flags
&= ~MDOC_FREECOL
;
1259 blk_part_imp(MACRO_PROT_ARGS
)
1265 struct mdoc_node
*blk
; /* saved block context */
1266 struct mdoc_node
*body
; /* saved body context */
1267 struct mdoc_node
*n
;
1269 nl
= MDOC_NEWLINE
& mdoc
->flags
;
1272 * A macro that spans to the end of the line. This is generally
1273 * (but not necessarily) called as the first macro. The block
1274 * has a head as the immediate child, which is always empty,
1275 * followed by zero or more opening punctuation nodes, then the
1276 * body (which may be empty, depending on the macro), then zero
1277 * or more closing punctuation nodes.
1280 if ( ! mdoc_block_alloc(mdoc
, line
, ppos
, tok
, NULL
))
1285 if ( ! mdoc_head_alloc(mdoc
, line
, ppos
, tok
))
1287 if ( ! rew_sub(MDOC_HEAD
, mdoc
, tok
, line
, ppos
))
1291 * Open the body scope "on-demand", that is, after we've
1292 * processed all our the leading delimiters (open parenthesis,
1296 for (body
= NULL
; ; ) {
1298 ac
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
1300 if (ARGS_ERROR
== ac
)
1302 if (ARGS_EOLN
== ac
)
1304 if (ARGS_PUNCT
== ac
)
1307 if (NULL
== body
&& ARGS_QWORD
!= ac
&&
1308 DELIM_OPEN
== mdoc_isdelim(p
)) {
1309 if ( ! dword(mdoc
, line
, la
, p
, DELIM_OPEN
, 0))
1315 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
1320 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
1322 if (MDOC_MAX
== ntok
) {
1323 if ( ! dword(mdoc
, line
, la
, p
, DELIM_MAX
,
1324 MDOC_JOIN
& mdoc_macros
[tok
].flags
))
1329 if ( ! mdoc_macro(mdoc
, ntok
, line
, la
, pos
, buf
))
1334 /* Clean-ups to leave in a consistent state. */
1337 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
1343 * If there is an open sub-block requiring explicit close-out,
1344 * postpone closing out the current block
1345 * until the rew_sub() call closing out the sub-block.
1347 for (n
= mdoc
->last
; n
&& n
!= body
&& n
!= blk
->parent
;
1349 if (MDOC_BLOCK
== n
->type
&&
1350 MDOC_EXPLICIT
& mdoc_macros
[n
->tok
].flags
&&
1351 ! (MDOC_VALID
& n
->flags
)) {
1352 make_pending(n
, tok
, mdoc
, line
, ppos
);
1353 if ( ! mdoc_endbody_alloc(mdoc
, line
, ppos
,
1354 tok
, body
, ENDBODY_NOSPACE
))
1361 if ( ! rew_sub(MDOC_BODY
, mdoc
, tok
, line
, ppos
))
1364 /* Standard appending of delimiters. */
1366 if (nl
&& ! append_delims(mdoc
, line
, pos
, buf
))
1369 /* Rewind scope, if applicable. */
1371 if ( ! rew_sub(MDOC_BLOCK
, mdoc
, tok
, line
, ppos
))
1374 /* Move trailing .Ns out of scope. */
1376 for (n
= body
->child
; n
&& n
->next
; n
= n
->next
)
1378 if (n
&& MDOC_Ns
== n
->tok
)
1379 mdoc_node_relink(mdoc
, n
);
1385 blk_part_exp(MACRO_PROT_ARGS
)
1389 struct mdoc_node
*head
; /* keep track of head */
1390 struct mdoc_node
*body
; /* keep track of body */
1394 nl
= MDOC_NEWLINE
& mdoc
->flags
;
1397 * The opening of an explicit macro having zero or more leading
1398 * punctuation nodes; a head with optional single element (the
1399 * case of `Eo'); and a body that may be empty.
1402 if ( ! mdoc_block_alloc(mdoc
, line
, ppos
, tok
, NULL
))
1405 for (head
= body
= NULL
; ; ) {
1407 ac
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
1409 if (ARGS_ERROR
== ac
)
1411 if (ARGS_PUNCT
== ac
)
1413 if (ARGS_EOLN
== ac
)
1416 /* Flush out leading punctuation. */
1418 if (NULL
== head
&& ARGS_QWORD
!= ac
&&
1419 DELIM_OPEN
== mdoc_isdelim(p
)) {
1420 assert(NULL
== body
);
1421 if ( ! dword(mdoc
, line
, la
, p
, DELIM_OPEN
, 0))
1427 assert(NULL
== body
);
1428 if ( ! mdoc_head_alloc(mdoc
, line
, ppos
, tok
))
1434 * `Eo' gobbles any data into the head, but most other
1435 * macros just immediately close out and begin the body.
1440 /* No check whether it's a macro! */
1442 if ( ! dword(mdoc
, line
, la
, p
, DELIM_MAX
, 0))
1445 if ( ! rew_sub(MDOC_HEAD
, mdoc
, tok
, line
, ppos
))
1447 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
1455 assert(NULL
!= head
&& NULL
!= body
);
1457 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
1459 if (MDOC_MAX
== ntok
) {
1460 if ( ! dword(mdoc
, line
, la
, p
, DELIM_MAX
,
1461 MDOC_JOIN
& mdoc_macros
[tok
].flags
))
1466 if ( ! mdoc_macro(mdoc
, ntok
, line
, la
, pos
, buf
))
1471 /* Clean-up to leave in a consistent state. */
1474 if ( ! mdoc_head_alloc(mdoc
, line
, ppos
, tok
))
1478 if ( ! rew_sub(MDOC_HEAD
, mdoc
, tok
, line
, ppos
))
1480 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
1484 /* Standard appending of delimiters. */
1488 return(append_delims(mdoc
, line
, pos
, buf
));
1492 in_line_argn(MACRO_PROT_ARGS
)
1494 int la
, flushed
, j
, maxargs
, nl
;
1497 struct mdoc_arg
*arg
;
1501 nl
= MDOC_NEWLINE
& mdoc
->flags
;
1504 * A line macro that has a fixed number of arguments (maxargs).
1505 * Only open the scope once the first non-leading-punctuation is
1506 * found (unless MDOC_IGNDELIM is noted, like in `Pf'), then
1507 * keep it open until the maximum number of arguments are
1533 for (arg
= NULL
; ; ) {
1535 av
= mdoc_argv(mdoc
, line
, tok
, &arg
, pos
, buf
);
1537 if (ARGV_WORD
== av
) {
1542 if (ARGV_EOLN
== av
)
1547 mdoc_argv_free(arg
);
1551 for (flushed
= j
= 0; ; ) {
1553 ac
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
1555 if (ARGS_ERROR
== ac
)
1557 if (ARGS_PUNCT
== ac
)
1559 if (ARGS_EOLN
== ac
)
1562 if ( ! (MDOC_IGNDELIM
& mdoc_macros
[tok
].flags
) &&
1563 ARGS_QWORD
!= ac
&& 0 == j
&&
1564 DELIM_OPEN
== mdoc_isdelim(p
)) {
1565 if ( ! dword(mdoc
, line
, la
, p
, DELIM_OPEN
, 0))
1569 if ( ! mdoc_elem_alloc(mdoc
, line
, ppos
, tok
, arg
))
1572 if (j
== maxargs
&& ! flushed
) {
1573 if ( ! rew_elem(mdoc
, tok
))
1578 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
1580 if (MDOC_MAX
!= ntok
) {
1581 if ( ! flushed
&& ! rew_elem(mdoc
, tok
))
1584 if ( ! mdoc_macro(mdoc
, ntok
, line
, la
, pos
, buf
))
1590 if ( ! (MDOC_IGNDELIM
& mdoc_macros
[tok
].flags
) &&
1593 DELIM_NONE
!= mdoc_isdelim(p
)) {
1594 if ( ! rew_elem(mdoc
, tok
))
1599 if ( ! dword(mdoc
, line
, la
, p
, DELIM_MAX
,
1600 MDOC_JOIN
& mdoc_macros
[tok
].flags
))
1605 if (0 == j
&& ! mdoc_elem_alloc(mdoc
, line
, ppos
, tok
, arg
))
1608 /* Close out in a consistent state. */
1610 if ( ! flushed
&& ! rew_elem(mdoc
, tok
))
1614 return(append_delims(mdoc
, line
, pos
, buf
));
1618 in_line_eoln(MACRO_PROT_ARGS
)
1623 struct mdoc_arg
*arg
;
1627 assert( ! (MDOC_PARSED
& mdoc_macros
[tok
].flags
));
1630 rew_sub(MDOC_BLOCK
, mdoc
, MDOC_Nm
, line
, ppos
);
1632 /* Parse macro arguments. */
1634 for (arg
= NULL
; ; ) {
1636 av
= mdoc_argv(mdoc
, line
, tok
, &arg
, pos
, buf
);
1638 if (ARGV_WORD
== av
) {
1642 if (ARGV_EOLN
== av
)
1647 mdoc_argv_free(arg
);
1651 /* Open element scope. */
1653 if ( ! mdoc_elem_alloc(mdoc
, line
, ppos
, tok
, arg
))
1656 /* Parse argument terms. */
1660 ac
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
1662 if (ARGS_ERROR
== ac
)
1664 if (ARGS_EOLN
== ac
)
1667 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
1669 if (MDOC_MAX
== ntok
) {
1670 if ( ! dword(mdoc
, line
, la
, p
, DELIM_MAX
,
1671 MDOC_JOIN
& mdoc_macros
[tok
].flags
))
1676 if ( ! rew_elem(mdoc
, tok
))
1678 return(mdoc_macro(mdoc
, ntok
, line
, la
, pos
, buf
));
1681 /* Close out (no delimiters). */
1683 return(rew_elem(mdoc
, tok
));
1687 ctx_synopsis(MACRO_PROT_ARGS
)
1691 nl
= MDOC_NEWLINE
& mdoc
->flags
;
1693 /* If we're not in the SYNOPSIS, go straight to in-line. */
1694 if ( ! (MDOC_SYNOPSIS
& mdoc
->flags
))
1695 return(in_line(mdoc
, tok
, line
, ppos
, pos
, buf
));
1697 /* If we're a nested call, same place. */
1699 return(in_line(mdoc
, tok
, line
, ppos
, pos
, buf
));
1702 * XXX: this will open a block scope; however, if later we end
1703 * up formatting the block scope, then child nodes will inherit
1704 * the formatting. Be careful.
1707 return(blk_full(mdoc
, tok
, line
, ppos
, pos
, buf
));
1708 assert(MDOC_Vt
== tok
);
1709 return(blk_part_imp(mdoc
, tok
, line
, ppos
, pos
, buf
));
1713 * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
1714 * They're unusual because they're basically free-form text until a
1715 * macro is encountered.
1718 phrase(struct mdoc
*mdoc
, int line
, int ppos
, char *buf
)
1725 for (pos
= ppos
; ; ) {
1728 ac
= mdoc_zargs(mdoc
, line
, &pos
, buf
, &p
);
1730 if (ARGS_ERROR
== ac
)
1732 if (ARGS_EOLN
== ac
)
1735 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup_raw(p
);
1737 if (MDOC_MAX
== ntok
) {
1738 if ( ! dword(mdoc
, line
, la
, p
, DELIM_MAX
, 1))
1743 if ( ! mdoc_macro(mdoc
, ntok
, line
, la
, &pos
, buf
))
1745 return(append_delims(mdoc
, line
, &pos
, buf
));
1752 phrase_ta(MACRO_PROT_ARGS
)
1754 struct mdoc_node
*n
;
1760 /* Make sure we are in a column list or ignore this macro. */
1762 while (NULL
!= n
&& MDOC_Bl
!= n
->tok
)
1764 if (NULL
== n
|| LIST_column
!= n
->norm
->Bl
.type
) {
1765 mdoc_pmsg(mdoc
, line
, ppos
, MANDOCERR_STRAYTA
);
1769 /* Advance to the next column. */
1770 if ( ! rew_sub(MDOC_BODY
, mdoc
, MDOC_It
, line
, ppos
))
1772 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, MDOC_It
))
1777 ac
= mdoc_zargs(mdoc
, line
, pos
, buf
, &p
);
1779 if (ARGS_ERROR
== ac
)
1781 if (ARGS_EOLN
== ac
)
1784 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup_raw(p
);
1786 if (MDOC_MAX
== ntok
) {
1787 if ( ! dword(mdoc
, line
, la
, p
, DELIM_MAX
,
1788 MDOC_JOIN
& mdoc_macros
[tok
].flags
))
1793 if ( ! mdoc_macro(mdoc
, ntok
, line
, la
, pos
, buf
))
1795 return(append_delims(mdoc
, line
, pos
, buf
));