]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_macro.c
1 /* $Id: mdoc_macro.c,v 1.144 2014/11/17 06:44:58 schwarze Exp $ */
3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2012, 2013, 2014 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.
20 #include <sys/types.h>
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
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_JOIN
}, /* No */
151 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
|
152 MDOC_IGNDELIM
| MDOC_JOIN
}, /* Ns */
153 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Nx */
154 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Ox */
155 { blk_exp_close
, MDOC_CALLABLE
| MDOC_PARSED
|
156 MDOC_EXPLICIT
| MDOC_JOIN
}, /* Pc */
157 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_IGNDELIM
}, /* Pf */
158 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
|
159 MDOC_EXPLICIT
| MDOC_JOIN
}, /* Po */
160 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_JOIN
}, /* Pq */
161 { blk_exp_close
, MDOC_CALLABLE
| MDOC_PARSED
|
162 MDOC_EXPLICIT
| MDOC_JOIN
}, /* Qc */
163 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_JOIN
}, /* Ql */
164 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
|
165 MDOC_EXPLICIT
| MDOC_JOIN
}, /* Qo */
166 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_JOIN
}, /* Qq */
167 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_JOIN
}, /* Re */
168 { blk_full
, MDOC_EXPLICIT
}, /* Rs */
169 { blk_exp_close
, MDOC_CALLABLE
| MDOC_PARSED
|
170 MDOC_EXPLICIT
| MDOC_JOIN
}, /* Sc */
171 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
|
172 MDOC_EXPLICIT
| MDOC_JOIN
}, /* So */
173 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_JOIN
}, /* Sq */
174 { in_line_eoln
, 0 }, /* Sm */
175 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_JOIN
}, /* Sx */
176 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_JOIN
}, /* Sy */
177 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Tn */
178 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_JOIN
}, /* Ux */
179 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_CALLABLE
| MDOC_PARSED
}, /* Xc */
180 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_EXPLICIT
}, /* Xo */
181 { blk_full
, MDOC_EXPLICIT
| MDOC_CALLABLE
}, /* Fo */
182 { blk_exp_close
, MDOC_CALLABLE
| MDOC_PARSED
|
183 MDOC_EXPLICIT
| MDOC_JOIN
}, /* Fc */
184 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
|
185 MDOC_EXPLICIT
| MDOC_JOIN
}, /* Oo */
186 { blk_exp_close
, MDOC_CALLABLE
| MDOC_PARSED
|
187 MDOC_EXPLICIT
| MDOC_JOIN
}, /* Oc */
188 { blk_full
, MDOC_EXPLICIT
}, /* Bk */
189 { blk_exp_close
, MDOC_EXPLICIT
| MDOC_JOIN
}, /* Ek */
190 { in_line_eoln
, 0 }, /* Bt */
191 { in_line_eoln
, 0 }, /* Hf */
192 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Fr */
193 { in_line_eoln
, 0 }, /* Ud */
194 { in_line
, 0 }, /* Lb */
195 { in_line_eoln
, 0 }, /* Lp */
196 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Lk */
197 { in_line
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Mt */
198 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_JOIN
}, /* Brq */
199 { blk_part_exp
, MDOC_CALLABLE
| MDOC_PARSED
|
200 MDOC_EXPLICIT
| MDOC_JOIN
}, /* Bro */
201 { blk_exp_close
, MDOC_CALLABLE
| MDOC_PARSED
|
202 MDOC_EXPLICIT
| MDOC_JOIN
}, /* Brc */
203 { in_line_eoln
, MDOC_JOIN
}, /* %C */
204 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Es */
205 { blk_part_imp
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_JOIN
}, /* En */
206 { in_line_argn
, MDOC_CALLABLE
| MDOC_PARSED
}, /* Dx */
207 { in_line_eoln
, MDOC_JOIN
}, /* %Q */
208 { in_line_eoln
, 0 }, /* br */
209 { in_line_eoln
, 0 }, /* sp */
210 { in_line_eoln
, 0 }, /* %U */
211 { phrase_ta
, MDOC_CALLABLE
| MDOC_PARSED
| MDOC_JOIN
}, /* Ta */
212 { in_line_eoln
, MDOC_PROLOGUE
}, /* ll */
215 const struct mdoc_macro
* const mdoc_macros
= __mdoc_macros
;
219 * This is called at the end of parsing. It must traverse up the tree,
220 * closing out open [implicit] scopes. Obviously, open explicit scopes
224 mdoc_macroend(struct mdoc
*mdoc
)
228 /* Scan for open explicit scopes. */
230 n
= MDOC_VALID
& mdoc
->last
->flags
?
231 mdoc
->last
->parent
: mdoc
->last
;
233 for ( ; n
; n
= n
->parent
)
234 if (MDOC_BLOCK
== n
->type
&&
235 MDOC_EXPLICIT
& mdoc_macros
[n
->tok
].flags
)
236 mandoc_msg(MANDOCERR_BLK_NOEND
, mdoc
->parse
,
237 n
->line
, n
->pos
, mdoc_macronames
[n
->tok
]);
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_ROOT
== p
->parent
->type
)
428 if (MDOC_Nd
== p
->tok
|| MDOC_Ss
== p
->tok
||
437 * Default block rewinding rules.
438 * In particular, always skip block end markers,
439 * and let all blocks rewind Nm children.
441 if (ENDBODY_NOT
!= p
->end
|| MDOC_Nm
== p
->tok
||
442 (MDOC_BLOCK
== p
->type
&&
443 ! (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
)))
447 * By default, closing out full blocks
448 * forces closing of broken explicit blocks,
449 * while closing out partial blocks
450 * allows delayed rewinding by default.
452 return (&blk_full
== mdoc_macros
[tok
].fp
?
453 REWIND_FORCE
: REWIND_LATER
);
457 rew_elem(struct mdoc
*mdoc
, enum mdoct tok
)
462 if (MDOC_ELEM
!= n
->type
)
464 assert(MDOC_ELEM
== n
->type
);
465 assert(tok
== n
->tok
);
467 return(rew_last(mdoc
, n
));
471 * We are trying to close a block identified by tok,
472 * but the child block *broken is still open.
473 * Thus, postpone closing the tok block
474 * until the rew_sub call closing *broken.
477 make_pending(struct mdoc_node
*broken
, enum mdoct tok
,
478 struct mdoc
*mdoc
, int line
, int ppos
)
480 struct mdoc_node
*breaker
;
483 * Iterate backwards, searching for the block matching tok,
484 * that is, the block breaking the *broken block.
486 for (breaker
= broken
->parent
; breaker
; breaker
= breaker
->parent
) {
489 * If the *broken block had already been broken before
490 * and we encounter its breaker, make the tok block
491 * pending on the inner breaker.
492 * Graphically, "[A breaker=[B broken=[C->B B] tok=A] C]"
493 * becomes "[A broken=[B [C->B B] tok=A] C]"
494 * and finally "[A [B->A [C->B B] A] C]".
496 if (breaker
== broken
->pending
) {
501 if (REWIND_THIS
!= rew_dohalt(tok
, MDOC_BLOCK
, breaker
))
503 if (MDOC_BODY
== broken
->type
)
504 broken
= broken
->parent
;
508 * If another, outer breaker is already pending on
509 * the *broken block, we must not clobber the link
510 * to the outer breaker, but make it pending on the
511 * new, now inner breaker.
512 * Graphically, "[A breaker=[B broken=[C->A A] tok=B] C]"
513 * becomes "[A breaker=[B->A broken=[C A] tok=B] C]"
514 * and finally "[A [B->A [C->B A] B] C]".
516 if (broken
->pending
) {
517 struct mdoc_node
*taker
;
520 * If the breaker had also been broken before,
521 * it cannot take on the outer breaker itself,
522 * but must hand it on to its own breakers.
523 * Graphically, this is the following situation:
524 * "[A [B breaker=[C->B B] broken=[D->A A] tok=C] D]"
525 * "[A taker=[B->A breaker=[C->B B] [D->C A] C] D]"
528 while (taker
->pending
)
529 taker
= taker
->pending
;
530 taker
->pending
= broken
->pending
;
532 broken
->pending
= breaker
;
533 mandoc_vmsg(MANDOCERR_BLK_NEST
, mdoc
->parse
, line
, ppos
,
534 "%s breaks %s", mdoc_macronames
[tok
],
535 mdoc_macronames
[broken
->tok
]);
540 * Found no matching block for tok.
541 * Are you trying to close a block that is not open?
547 rew_sub(enum mdoc_type t
, struct mdoc
*mdoc
,
548 enum mdoct tok
, int line
, int ppos
)
554 switch (rew_dohalt(tok
, t
, n
)) {
559 (MDOC_NEWLINE
& mdoc
->flags
&&
560 ! (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
));
563 mandoc_vmsg(MANDOCERR_BLK_BROKEN
, mdoc
->parse
,
564 line
, ppos
, "%s breaks %s",
565 mdoc_macronames
[tok
],
566 mdoc_macronames
[n
->tok
]);
570 (MDOC_NEWLINE
& mdoc
->flags
? 1 : 0);
574 if (make_pending(n
, tok
, mdoc
, line
, ppos
) ||
579 mandoc_msg(MANDOCERR_BLK_NOTOPEN
,
580 mdoc
->parse
, line
, ppos
,
581 mdoc_macronames
[tok
]);
588 if ( ! rew_last(mdoc
, n
))
592 * The current block extends an enclosing block.
593 * Now that the current block ends, close the enclosing block, too.
595 while (NULL
!= (n
= n
->pending
)) {
596 if ( ! rew_last(mdoc
, n
))
598 if (MDOC_HEAD
== n
->type
&&
599 ! mdoc_body_alloc(mdoc
, n
->line
, n
->pos
, n
->tok
))
607 * Allocate a word and check whether it's punctuation or not.
608 * Punctuation consists of those tokens found in mdoc_isdelim().
611 dword(struct mdoc
*mdoc
, int line
, int col
, const char *p
,
612 enum mdelim d
, int may_append
)
619 ! ((MDOC_SYNOPSIS
| MDOC_KEEP
| MDOC_SMOFF
) & mdoc
->flags
) &&
620 DELIM_NONE
== d
&& MDOC_TEXT
== mdoc
->last
->type
&&
621 DELIM_NONE
== mdoc_isdelim(mdoc
->last
->string
)) {
622 mdoc_word_append(mdoc
, p
);
626 if ( ! mdoc_word_alloc(mdoc
, line
, col
, p
))
630 * If the word consists of a bare delimiter,
631 * flag the new node accordingly,
632 * unless doing so was vetoed by the invoking macro.
633 * Always clear the veto, it is only valid for one word.
637 mdoc
->last
->flags
|= MDOC_DELIMO
;
638 else if (d
== DELIM_CLOSE
&&
639 ! (mdoc
->flags
& MDOC_NODELIMC
) &&
640 mdoc
->last
->parent
->tok
!= MDOC_Fd
)
641 mdoc
->last
->flags
|= MDOC_DELIMC
;
643 mdoc
->flags
&= ~MDOC_NODELIMC
;
649 append_delims(struct mdoc
*mdoc
, int line
, int *pos
, char *buf
)
655 if ('\0' == buf
[*pos
])
660 ac
= mdoc_zargs(mdoc
, line
, pos
, buf
, &p
);
662 if (ARGS_ERROR
== ac
)
664 else if (ARGS_EOLN
== ac
)
667 dword(mdoc
, line
, la
, p
, DELIM_MAX
, 1);
670 * If we encounter end-of-sentence symbols, then trigger
673 * XXX: it's easy to allow this to propagate outward to
674 * the last symbol, such that `. )' will cause the
675 * correct double-spacing. However, (1) groff isn't
676 * smart enough to do this and (2) it would require
677 * knowing which symbols break this behaviour, for
678 * example, `. ;' shouldn't propagate the double-space.
680 if (mandoc_eos(p
, strlen(p
)))
681 mdoc
->last
->flags
|= MDOC_EOS
;
688 * Close out block partial/full explicit.
691 blk_exp_close(MACRO_PROT_ARGS
)
693 struct mdoc_node
*body
; /* Our own body. */
694 struct mdoc_node
*later
; /* A sub-block starting later. */
695 struct mdoc_node
*n
; /* For searching backwards. */
697 int j
, lastarg
, maxargs
, flushed
, nl
;
699 enum mdoct atok
, ntok
;
702 nl
= MDOC_NEWLINE
& mdoc
->flags
;
709 mdoc
->flags
&= ~MDOC_KEEP
;
716 * Search backwards for beginnings of blocks,
717 * both of our own and of pending sub-blocks.
721 for (n
= mdoc
->last
; n
; n
= n
->parent
) {
722 if (MDOC_VALID
& n
->flags
)
725 /* Remember the start of our own body. */
726 if (MDOC_BODY
== n
->type
&& atok
== n
->tok
) {
727 if (ENDBODY_NOT
== n
->end
)
732 if (MDOC_BLOCK
!= n
->type
|| MDOC_Nm
== n
->tok
)
734 if (atok
== n
->tok
) {
738 * Found the start of our own block.
739 * When there is no pending sub block,
740 * just proceed to closing out.
746 * When there is a pending sub block,
747 * postpone closing out the current block
748 * until the rew_sub() closing out the sub-block.
750 make_pending(later
, tok
, mdoc
, line
, ppos
);
753 * Mark the place where the formatting - but not
754 * the scope - of the current block ends.
756 if ( ! mdoc_endbody_alloc(mdoc
, line
, ppos
,
757 atok
, body
, ENDBODY_SPACE
))
763 * When finding an open sub block, remember the last
764 * open explicit block, or, in case there are only
765 * implicit ones, the first open implicit block.
768 MDOC_EXPLICIT
& mdoc_macros
[later
->tok
].flags
)
770 if (MDOC_It
!= n
->tok
)
774 if ( ! (MDOC_PARSED
& mdoc_macros
[tok
].flags
)) {
775 if ('\0' != buf
[*pos
])
776 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
777 mdoc
->parse
, line
, ppos
,
778 "%s %s", mdoc_macronames
[tok
],
780 if ( ! rew_sub(MDOC_BODY
, mdoc
, tok
, line
, ppos
))
782 return(rew_sub(MDOC_BLOCK
, mdoc
, tok
, line
, ppos
));
785 if ( ! rew_sub(MDOC_BODY
, mdoc
, tok
, line
, ppos
))
788 if (NULL
== later
&& maxargs
> 0)
789 if ( ! mdoc_tail_alloc(mdoc
, line
, ppos
, rew_alt(tok
)))
792 for (flushed
= j
= 0; ; j
++) {
795 if (j
== maxargs
&& ! flushed
) {
796 if ( ! rew_sub(MDOC_BLOCK
, mdoc
, tok
, line
, ppos
))
801 ac
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
803 if (ARGS_ERROR
== ac
)
805 if (ARGS_PUNCT
== ac
)
810 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
812 if (MDOC_MAX
== ntok
) {
813 if ( ! dword(mdoc
, line
, lastarg
, p
, DELIM_MAX
,
814 MDOC_JOIN
& mdoc_macros
[tok
].flags
))
820 if ( ! rew_sub(MDOC_BLOCK
, mdoc
, tok
, line
, ppos
))
825 mdoc
->flags
&= ~MDOC_NEWLINE
;
827 if ( ! mdoc_macro(mdoc
, ntok
, line
, lastarg
, pos
, buf
))
832 if ( ! flushed
&& ! rew_sub(MDOC_BLOCK
, mdoc
, tok
, line
, ppos
))
837 return(append_delims(mdoc
, line
, pos
, buf
));
841 in_line(MACRO_PROT_ARGS
)
843 int la
, scope
, cnt
, firstarg
, mayopen
, nc
, nl
;
848 struct mdoc_arg
*arg
;
851 nl
= MDOC_NEWLINE
& mdoc
->flags
;
854 * Whether we allow ignored elements (those without content,
855 * usually because of reserved words) to squeak by.
877 for (arg
= NULL
;; ) {
879 av
= mdoc_argv(mdoc
, line
, tok
, &arg
, pos
, buf
);
881 if (ARGV_WORD
== av
) {
897 for (cnt
= scope
= 0;; ) {
899 ac
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
901 if (ac
== ARGS_ERROR
)
905 * At the end of a macro line,
906 * opening delimiters do not suppress spacing.
909 if (ac
== ARGS_EOLN
) {
911 mdoc
->last
->flags
&= ~MDOC_DELIMO
;
916 * The rest of the macro line is only punctuation,
917 * to be handled by append_delims().
918 * If there were no other arguments,
919 * do not allow the first one to suppress spacing,
920 * even if it turns out to be a closing one.
923 if (ac
== ARGS_PUNCT
) {
924 if (cnt
== 0 && nc
== 0)
925 mdoc
->flags
|= MDOC_NODELIMC
;
929 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
932 * In this case, we've located a submacro and must
933 * execute it. Close out scope, if open. If no
934 * elements have been generated, either create one (nc)
935 * or raise a warning.
938 if (MDOC_MAX
!= ntok
) {
939 if (scope
&& ! rew_elem(mdoc
, tok
))
941 if (nc
&& 0 == cnt
) {
942 if ( ! mdoc_elem_alloc(mdoc
,
943 line
, ppos
, tok
, arg
))
945 if ( ! rew_last(mdoc
, mdoc
->last
))
947 } else if ( ! nc
&& 0 == cnt
) {
949 mandoc_msg(MANDOCERR_MACRO_EMPTY
,
950 mdoc
->parse
, line
, ppos
,
951 mdoc_macronames
[tok
]);
954 if ( ! mdoc_macro(mdoc
, ntok
, line
, la
, pos
, buf
))
958 return(append_delims(mdoc
, line
, pos
, buf
));
962 * Non-quote-enclosed punctuation. Set up our scope, if
963 * a word; rewind the scope, if a delimiter; then append
967 d
= ARGS_QWORD
== ac
? DELIM_NONE
: mdoc_isdelim(p
);
969 if (DELIM_NONE
!= d
) {
971 * If we encounter closing punctuation, no word
972 * has been emitted, no scope is open, and we're
973 * allowed to have an empty element, then start
976 if ((d
== DELIM_CLOSE
||
977 (d
== DELIM_MIDDLE
&& tok
== MDOC_Fl
)) &&
978 !cnt
&& !scope
&& nc
&& mayopen
) {
979 if ( ! mdoc_elem_alloc(mdoc
,
980 line
, ppos
, tok
, arg
))
988 * Close out our scope, if one is open, before
991 if (scope
&& ! rew_elem(mdoc
, tok
))
994 } else if (mayopen
&& !scope
) {
995 if ( ! mdoc_elem_alloc(mdoc
, line
, ppos
, tok
, arg
))
1001 if ( ! dword(mdoc
, line
, la
, p
, d
,
1002 MDOC_JOIN
& mdoc_macros
[tok
].flags
))
1006 * If the first argument is a closing delimiter,
1007 * do not suppress spacing before it.
1010 if (firstarg
&& d
== DELIM_CLOSE
&& !nc
)
1011 mdoc
->last
->flags
&= ~MDOC_DELIMC
;
1015 * `Fl' macros have their scope re-opened with each new
1016 * word so that the `-' can be added to each one without
1017 * having to parse out spaces.
1019 if (scope
&& MDOC_Fl
== tok
) {
1020 if ( ! rew_elem(mdoc
, tok
))
1026 if (scope
&& ! rew_elem(mdoc
, tok
))
1030 * If no elements have been collected and we're allowed to have
1031 * empties (nc), open a scope and close it out. Otherwise,
1035 if (nc
&& 0 == cnt
) {
1036 if ( ! mdoc_elem_alloc(mdoc
, line
, ppos
, tok
, arg
))
1038 if ( ! rew_last(mdoc
, mdoc
->last
))
1040 } else if ( ! nc
&& 0 == cnt
) {
1041 mdoc_argv_free(arg
);
1042 mandoc_msg(MANDOCERR_MACRO_EMPTY
, mdoc
->parse
,
1043 line
, ppos
, mdoc_macronames
[tok
]);
1048 return(append_delims(mdoc
, line
, pos
, buf
));
1052 blk_full(MACRO_PROT_ARGS
)
1054 int la
, nl
, nparsed
;
1055 struct mdoc_arg
*arg
;
1056 struct mdoc_node
*head
; /* save of head macro */
1057 struct mdoc_node
*body
; /* save of body macro */
1058 struct mdoc_node
*n
;
1061 enum margserr ac
, lac
;
1065 nl
= MDOC_NEWLINE
& mdoc
->flags
;
1067 /* Skip items outside lists. */
1069 if (tok
== MDOC_It
) {
1070 for (n
= mdoc
->last
; n
; n
= n
->parent
)
1071 if (n
->tok
== MDOC_Bl
&&
1072 ! (n
->flags
& MDOC_VALID
))
1075 mandoc_vmsg(MANDOCERR_IT_STRAY
, mdoc
->parse
,
1076 line
, ppos
, "It %s", buf
+ *pos
);
1077 if ( ! mdoc_elem_alloc(mdoc
, line
, ppos
,
1080 return(rew_elem(mdoc
, MDOC_br
));
1084 /* Close out prior implicit scope. */
1086 if ( ! (MDOC_EXPLICIT
& mdoc_macros
[tok
].flags
)) {
1087 if ( ! rew_sub(MDOC_BODY
, mdoc
, tok
, line
, ppos
))
1089 if ( ! rew_sub(MDOC_BLOCK
, mdoc
, tok
, line
, ppos
))
1094 * This routine accommodates implicitly- and explicitly-scoped
1095 * macro openings. Implicit ones first close out prior scope
1096 * (seen above). Delay opening the head until necessary to
1097 * allow leading punctuation to print. Special consideration
1098 * for `It -column', which has phrase-part syntax instead of
1099 * regular child nodes.
1102 for (arg
= NULL
;; ) {
1104 av
= mdoc_argv(mdoc
, line
, tok
, &arg
, pos
, buf
);
1106 if (ARGV_WORD
== av
) {
1111 if (ARGV_EOLN
== av
)
1116 mdoc_argv_free(arg
);
1120 if ( ! mdoc_block_alloc(mdoc
, line
, ppos
, tok
, arg
))
1126 * Exception: Heads of `It' macros in `-diag' lists are not
1127 * parsed, even though `It' macros in general are parsed.
1129 nparsed
= MDOC_It
== tok
&&
1130 MDOC_Bl
== mdoc
->last
->parent
->tok
&&
1131 LIST_diag
== mdoc
->last
->parent
->norm
->Bl
.type
;
1134 * The `Nd' macro has all arguments in its body: it's a hybrid
1135 * of block partial-explicit and full-implicit. Stupid.
1138 if (MDOC_Nd
== tok
) {
1139 if ( ! mdoc_head_alloc(mdoc
, line
, ppos
, tok
))
1142 if ( ! rew_sub(MDOC_HEAD
, mdoc
, tok
, line
, ppos
))
1144 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
1150 mdoc
->flags
|= MDOC_KEEP
;
1156 /* Initialise last-phrase-type with ARGS_PEND. */
1157 lac
= ARGS_ERROR
== ac
? ARGS_PEND
: ac
;
1158 ac
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
1160 if (ARGS_PUNCT
== ac
)
1163 if (ARGS_ERROR
== ac
)
1166 if (ARGS_EOLN
== ac
) {
1167 if (ARGS_PPHRASE
!= lac
&& ARGS_PHRASE
!= lac
)
1170 * This is necessary: if the last token on a
1171 * line is a `Ta' or tab, then we'll get
1172 * ARGS_EOLN, so we must be smart enough to
1173 * reopen our scope if the last parse was a
1174 * phrase or partial phrase.
1176 if ( ! rew_sub(MDOC_BODY
, mdoc
, tok
, line
, ppos
))
1178 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
1185 * Emit leading punctuation (i.e., punctuation before
1186 * the MDOC_HEAD) for non-phrase types.
1191 ARGS_PHRASE
!= ac
&&
1192 ARGS_PPHRASE
!= ac
&&
1194 DELIM_OPEN
== mdoc_isdelim(p
)) {
1195 if ( ! dword(mdoc
, line
, la
, p
, DELIM_OPEN
, 0))
1200 /* Open a head if one hasn't been opened. */
1203 if ( ! mdoc_head_alloc(mdoc
, line
, ppos
, tok
))
1208 if (ARGS_PHRASE
== ac
||
1210 ARGS_PPHRASE
== ac
) {
1212 * If we haven't opened a body yet, rewind the
1213 * head; if we have, rewind that instead.
1216 mtt
= body
? MDOC_BODY
: MDOC_HEAD
;
1217 if ( ! rew_sub(mtt
, mdoc
, tok
, line
, ppos
))
1220 /* Then allocate our body context. */
1222 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
1227 * Process phrases: set whether we're in a
1228 * partial-phrase (this effects line handling)
1229 * then call down into the phrase parser.
1232 if (ARGS_PPHRASE
== ac
)
1233 mdoc
->flags
|= MDOC_PPHRASE
;
1234 if (ARGS_PEND
== ac
&& ARGS_PPHRASE
== lac
)
1235 mdoc
->flags
|= MDOC_PPHRASE
;
1237 if ( ! phrase(mdoc
, line
, la
, buf
))
1240 mdoc
->flags
&= ~MDOC_PPHRASE
;
1244 ntok
= nparsed
|| ARGS_QWORD
== ac
?
1245 MDOC_MAX
: lookup(tok
, p
);
1247 if (MDOC_MAX
== ntok
) {
1248 if ( ! dword(mdoc
, line
, la
, p
, DELIM_MAX
,
1249 MDOC_JOIN
& mdoc_macros
[tok
].flags
))
1254 if ( ! mdoc_macro(mdoc
, ntok
, line
, la
, pos
, buf
))
1260 if ( ! mdoc_head_alloc(mdoc
, line
, ppos
, tok
))
1265 if (nl
&& ! append_delims(mdoc
, line
, pos
, buf
))
1268 /* If we've already opened our body, exit now. */
1274 * If there is an open (i.e., unvalidated) sub-block requiring
1275 * explicit close-out, postpone switching the current block from
1276 * head to body until the rew_sub() call closing out that
1279 for (n
= mdoc
->last
; n
&& n
!= head
; n
= n
->parent
) {
1280 if (MDOC_BLOCK
== n
->type
&&
1281 MDOC_EXPLICIT
& mdoc_macros
[n
->tok
].flags
&&
1282 ! (MDOC_VALID
& n
->flags
)) {
1288 /* Close out scopes to remain in a consistent state. */
1290 if ( ! rew_sub(MDOC_HEAD
, mdoc
, tok
, line
, ppos
))
1292 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
1296 if ( ! (MDOC_FREECOL
& mdoc
->flags
))
1299 if ( ! rew_sub(MDOC_BODY
, mdoc
, tok
, line
, ppos
))
1301 if ( ! rew_sub(MDOC_BLOCK
, mdoc
, tok
, line
, ppos
))
1304 mdoc
->flags
&= ~MDOC_FREECOL
;
1309 blk_part_imp(MACRO_PROT_ARGS
)
1315 struct mdoc_node
*blk
; /* saved block context */
1316 struct mdoc_node
*body
; /* saved body context */
1317 struct mdoc_node
*n
;
1319 nl
= MDOC_NEWLINE
& mdoc
->flags
;
1322 * A macro that spans to the end of the line. This is generally
1323 * (but not necessarily) called as the first macro. The block
1324 * has a head as the immediate child, which is always empty,
1325 * followed by zero or more opening punctuation nodes, then the
1326 * body (which may be empty, depending on the macro), then zero
1327 * or more closing punctuation nodes.
1330 if ( ! mdoc_block_alloc(mdoc
, line
, ppos
, tok
, NULL
))
1335 if ( ! mdoc_head_alloc(mdoc
, line
, ppos
, tok
))
1337 if ( ! rew_sub(MDOC_HEAD
, mdoc
, tok
, line
, ppos
))
1341 * Open the body scope "on-demand", that is, after we've
1342 * processed all our the leading delimiters (open parenthesis,
1346 for (body
= NULL
; ; ) {
1348 ac
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
1350 if (ARGS_ERROR
== ac
)
1352 if (ARGS_EOLN
== ac
)
1354 if (ARGS_PUNCT
== ac
)
1357 if (NULL
== body
&& ARGS_QWORD
!= ac
&&
1358 DELIM_OPEN
== mdoc_isdelim(p
)) {
1359 if ( ! dword(mdoc
, line
, la
, p
, DELIM_OPEN
, 0))
1365 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
1370 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
1372 if (MDOC_MAX
== ntok
) {
1373 if ( ! dword(mdoc
, line
, la
, p
, DELIM_MAX
,
1374 MDOC_JOIN
& mdoc_macros
[tok
].flags
))
1379 if ( ! mdoc_macro(mdoc
, ntok
, line
, la
, pos
, buf
))
1384 /* Clean-ups to leave in a consistent state. */
1387 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
1393 * If there is an open sub-block requiring explicit close-out,
1394 * postpone closing out the current block
1395 * until the rew_sub() call closing out the sub-block.
1397 for (n
= mdoc
->last
; n
&& n
!= body
&& n
!= blk
->parent
;
1399 if (MDOC_BLOCK
== n
->type
&&
1400 MDOC_EXPLICIT
& mdoc_macros
[n
->tok
].flags
&&
1401 ! (MDOC_VALID
& n
->flags
)) {
1402 make_pending(n
, tok
, mdoc
, line
, ppos
);
1403 if ( ! mdoc_endbody_alloc(mdoc
, line
, ppos
,
1404 tok
, body
, ENDBODY_NOSPACE
))
1411 if ( ! rew_sub(MDOC_BODY
, mdoc
, tok
, line
, ppos
))
1414 /* Standard appending of delimiters. */
1416 if (nl
&& ! append_delims(mdoc
, line
, pos
, buf
))
1419 /* Rewind scope, if applicable. */
1421 if ( ! rew_sub(MDOC_BLOCK
, mdoc
, tok
, line
, ppos
))
1424 /* Move trailing .Ns out of scope. */
1426 for (n
= body
->child
; n
&& n
->next
; n
= n
->next
)
1428 if (n
&& MDOC_Ns
== n
->tok
)
1429 mdoc_node_relink(mdoc
, n
);
1435 blk_part_exp(MACRO_PROT_ARGS
)
1439 struct mdoc_node
*head
; /* keep track of head */
1440 struct mdoc_node
*body
; /* keep track of body */
1444 nl
= MDOC_NEWLINE
& mdoc
->flags
;
1447 * The opening of an explicit macro having zero or more leading
1448 * punctuation nodes; a head with optional single element (the
1449 * case of `Eo'); and a body that may be empty.
1452 if ( ! mdoc_block_alloc(mdoc
, line
, ppos
, tok
, NULL
))
1455 for (head
= body
= NULL
; ; ) {
1457 ac
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
1459 if (ARGS_ERROR
== ac
)
1461 if (ARGS_PUNCT
== ac
)
1463 if (ARGS_EOLN
== ac
)
1466 /* Flush out leading punctuation. */
1468 if (NULL
== head
&& ARGS_QWORD
!= ac
&&
1469 DELIM_OPEN
== mdoc_isdelim(p
)) {
1470 assert(NULL
== body
);
1471 if ( ! dword(mdoc
, line
, la
, p
, DELIM_OPEN
, 0))
1477 assert(NULL
== body
);
1478 if ( ! mdoc_head_alloc(mdoc
, line
, ppos
, tok
))
1484 * `Eo' gobbles any data into the head, but most other
1485 * macros just immediately close out and begin the body.
1490 /* No check whether it's a macro! */
1492 if ( ! dword(mdoc
, line
, la
, p
, DELIM_MAX
, 0))
1495 if ( ! rew_sub(MDOC_HEAD
, mdoc
, tok
, line
, ppos
))
1497 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
1505 assert(NULL
!= head
&& NULL
!= body
);
1507 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
1509 if (MDOC_MAX
== ntok
) {
1510 if ( ! dword(mdoc
, line
, la
, p
, DELIM_MAX
,
1511 MDOC_JOIN
& mdoc_macros
[tok
].flags
))
1516 if ( ! mdoc_macro(mdoc
, ntok
, line
, la
, pos
, buf
))
1521 /* Clean-up to leave in a consistent state. */
1524 if ( ! mdoc_head_alloc(mdoc
, line
, ppos
, tok
))
1528 if ( ! rew_sub(MDOC_HEAD
, mdoc
, tok
, line
, ppos
))
1530 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, tok
))
1534 /* Standard appending of delimiters. */
1538 return(append_delims(mdoc
, line
, pos
, buf
));
1542 in_line_argn(MACRO_PROT_ARGS
)
1544 int la
, flushed
, j
, maxargs
, nl
;
1547 struct mdoc_arg
*arg
;
1551 nl
= MDOC_NEWLINE
& mdoc
->flags
;
1554 * A line macro that has a fixed number of arguments (maxargs).
1555 * Only open the scope once the first non-leading-punctuation is
1556 * found (unless MDOC_IGNDELIM is noted, like in `Pf'), then
1557 * keep it open until the maximum number of arguments are
1581 for (arg
= NULL
; ; ) {
1583 av
= mdoc_argv(mdoc
, line
, tok
, &arg
, pos
, buf
);
1585 if (ARGV_WORD
== av
) {
1590 if (ARGV_EOLN
== av
)
1595 mdoc_argv_free(arg
);
1599 for (flushed
= j
= 0; ; ) {
1601 ac
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
1603 if (ARGS_ERROR
== ac
)
1605 if (ARGS_PUNCT
== ac
)
1607 if (ARGS_EOLN
== ac
)
1610 if ( ! (MDOC_IGNDELIM
& mdoc_macros
[tok
].flags
) &&
1611 ARGS_QWORD
!= ac
&& 0 == j
&&
1612 DELIM_OPEN
== mdoc_isdelim(p
)) {
1613 if ( ! dword(mdoc
, line
, la
, p
, DELIM_OPEN
, 0))
1617 if ( ! mdoc_elem_alloc(mdoc
, line
, ppos
, tok
, arg
))
1620 if (j
== maxargs
&& ! flushed
) {
1621 if ( ! rew_elem(mdoc
, tok
))
1626 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
1628 if (MDOC_MAX
!= ntok
) {
1629 if ( ! flushed
&& ! rew_elem(mdoc
, tok
))
1632 if ( ! mdoc_macro(mdoc
, ntok
, line
, la
, pos
, buf
))
1638 if ( ! (MDOC_IGNDELIM
& mdoc_macros
[tok
].flags
) &&
1641 DELIM_NONE
!= mdoc_isdelim(p
)) {
1642 if ( ! rew_elem(mdoc
, tok
))
1647 if ( ! dword(mdoc
, line
, la
, p
, DELIM_MAX
,
1648 MDOC_JOIN
& mdoc_macros
[tok
].flags
))
1653 if (0 == j
&& ! mdoc_elem_alloc(mdoc
, line
, ppos
, tok
, arg
))
1656 /* Close out in a consistent state. */
1658 if ( ! flushed
&& ! rew_elem(mdoc
, tok
))
1662 return(append_delims(mdoc
, line
, pos
, buf
));
1666 in_line_eoln(MACRO_PROT_ARGS
)
1671 struct mdoc_arg
*arg
;
1675 assert( ! (MDOC_PARSED
& mdoc_macros
[tok
].flags
));
1678 rew_sub(MDOC_BLOCK
, mdoc
, MDOC_Nm
, line
, ppos
);
1680 /* Parse macro arguments. */
1682 for (arg
= NULL
; ; ) {
1684 av
= mdoc_argv(mdoc
, line
, tok
, &arg
, pos
, buf
);
1686 if (ARGV_WORD
== av
) {
1690 if (ARGV_EOLN
== av
)
1695 mdoc_argv_free(arg
);
1699 /* Open element scope. */
1701 if ( ! mdoc_elem_alloc(mdoc
, line
, ppos
, tok
, arg
))
1704 /* Parse argument terms. */
1708 ac
= mdoc_args(mdoc
, line
, pos
, buf
, tok
, &p
);
1710 if (ARGS_ERROR
== ac
)
1712 if (ARGS_EOLN
== ac
)
1715 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup(tok
, p
);
1717 if (MDOC_MAX
== ntok
) {
1718 if ( ! dword(mdoc
, line
, la
, p
, DELIM_MAX
,
1719 MDOC_JOIN
& mdoc_macros
[tok
].flags
))
1724 if ( ! rew_elem(mdoc
, tok
))
1726 return(mdoc_macro(mdoc
, ntok
, line
, la
, pos
, buf
));
1729 /* Close out (no delimiters). */
1731 return(rew_elem(mdoc
, tok
));
1735 ctx_synopsis(MACRO_PROT_ARGS
)
1739 nl
= MDOC_NEWLINE
& mdoc
->flags
;
1741 /* If we're not in the SYNOPSIS, go straight to in-line. */
1742 if ( ! (MDOC_SYNOPSIS
& mdoc
->flags
))
1743 return(in_line(mdoc
, tok
, line
, ppos
, pos
, buf
));
1745 /* If we're a nested call, same place. */
1747 return(in_line(mdoc
, tok
, line
, ppos
, pos
, buf
));
1750 * XXX: this will open a block scope; however, if later we end
1751 * up formatting the block scope, then child nodes will inherit
1752 * the formatting. Be careful.
1755 return(blk_full(mdoc
, tok
, line
, ppos
, pos
, buf
));
1756 assert(MDOC_Vt
== tok
);
1757 return(blk_part_imp(mdoc
, tok
, line
, ppos
, pos
, buf
));
1761 * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
1762 * They're unusual because they're basically free-form text until a
1763 * macro is encountered.
1766 phrase(struct mdoc
*mdoc
, int line
, int ppos
, char *buf
)
1773 for (pos
= ppos
; ; ) {
1776 ac
= mdoc_zargs(mdoc
, line
, &pos
, buf
, &p
);
1778 if (ARGS_ERROR
== ac
)
1780 if (ARGS_EOLN
== ac
)
1783 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup_raw(p
);
1785 if (MDOC_MAX
== ntok
) {
1786 if ( ! dword(mdoc
, line
, la
, p
, DELIM_MAX
, 1))
1791 if ( ! mdoc_macro(mdoc
, ntok
, line
, la
, &pos
, buf
))
1793 return(append_delims(mdoc
, line
, &pos
, buf
));
1800 phrase_ta(MACRO_PROT_ARGS
)
1802 struct mdoc_node
*n
;
1808 /* Make sure we are in a column list or ignore this macro. */
1810 while (NULL
!= n
&& MDOC_Bl
!= n
->tok
)
1812 if (NULL
== n
|| LIST_column
!= n
->norm
->Bl
.type
) {
1813 mandoc_msg(MANDOCERR_TA_STRAY
, mdoc
->parse
,
1818 /* Advance to the next column. */
1819 if ( ! rew_sub(MDOC_BODY
, mdoc
, MDOC_It
, line
, ppos
))
1821 if ( ! mdoc_body_alloc(mdoc
, line
, ppos
, MDOC_It
))
1826 ac
= mdoc_zargs(mdoc
, line
, pos
, buf
, &p
);
1828 if (ARGS_ERROR
== ac
)
1830 if (ARGS_EOLN
== ac
)
1833 ntok
= ARGS_QWORD
== ac
? MDOC_MAX
: lookup_raw(p
);
1835 if (MDOC_MAX
== ntok
) {
1836 if ( ! dword(mdoc
, line
, la
, p
, DELIM_MAX
,
1837 MDOC_JOIN
& mdoc_macros
[tok
].flags
))
1842 if ( ! mdoc_macro(mdoc
, ntok
, line
, la
, pos
, buf
))
1844 return(append_delims(mdoc
, line
, pos
, buf
));