1 /* $Id: mdoc_macro.c,v 1.115 2012/01/05 00:43:51 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010 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 obsolete(MACRO_PROT_ARGS);
52 static int phrase_ta(MACRO_PROT_ARGS);
54 static int dword(struct mdoc *, int, int,
55 const char *, enum mdelim);
56 static int append_delims(struct mdoc *,
58 static enum mdoct lookup(enum mdoct, const char *);
59 static enum mdoct lookup_raw(const char *);
60 static int make_pending(struct mdoc_node *, enum mdoct,
61 struct mdoc *, int, int);
62 static int phrase(struct mdoc *, int, int, char *);
63 static enum mdoct rew_alt(enum mdoct);
64 static enum rew rew_dohalt(enum mdoct, enum mdoc_type,
65 const struct mdoc_node *);
66 static int rew_elem(struct mdoc *, enum mdoct);
67 static int rew_last(struct mdoc *,
68 const struct mdoc_node *);
69 static int rew_sub(enum mdoc_type, struct mdoc *,
70 enum mdoct, int, int);
72 const struct mdoc_macro __mdoc_macros[MDOC_MAX] = {
73 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ap */
74 { in_line_eoln, MDOC_PROLOGUE }, /* Dd */
75 { in_line_eoln, MDOC_PROLOGUE }, /* Dt */
76 { in_line_eoln, MDOC_PROLOGUE }, /* Os */
77 { blk_full, MDOC_PARSED }, /* Sh */
78 { blk_full, MDOC_PARSED }, /* Ss */
79 { in_line_eoln, 0 }, /* Pp */
80 { blk_part_imp, MDOC_PARSED }, /* D1 */
81 { blk_part_imp, MDOC_PARSED }, /* Dl */
82 { blk_full, MDOC_EXPLICIT }, /* Bd */
83 { blk_exp_close, MDOC_EXPLICIT }, /* Ed */
84 { blk_full, MDOC_EXPLICIT }, /* Bl */
85 { blk_exp_close, MDOC_EXPLICIT }, /* El */
86 { blk_full, MDOC_PARSED }, /* It */
87 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ad */
88 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* An */
89 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ar */
90 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Cd */
91 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Cm */
92 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Dv */
93 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Er */
94 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ev */
95 { in_line_eoln, 0 }, /* Ex */
96 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fa */
97 { in_line_eoln, 0 }, /* Fd */
98 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fl */
99 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fn */
100 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ft */
101 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ic */
102 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* In */
103 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Li */
104 { blk_full, 0 }, /* Nd */
105 { ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED }, /* Nm */
106 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Op */
107 { obsolete, 0 }, /* Ot */
108 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Pa */
109 { in_line_eoln, 0 }, /* Rv */
110 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* St */
111 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Va */
112 { ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED }, /* Vt */
113 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Xr */
114 { in_line_eoln, 0 }, /* %A */
115 { in_line_eoln, 0 }, /* %B */
116 { in_line_eoln, 0 }, /* %D */
117 { in_line_eoln, 0 }, /* %I */
118 { in_line_eoln, 0 }, /* %J */
119 { in_line_eoln, 0 }, /* %N */
120 { in_line_eoln, 0 }, /* %O */
121 { in_line_eoln, 0 }, /* %P */
122 { in_line_eoln, 0 }, /* %R */
123 { in_line_eoln, 0 }, /* %T */
124 { in_line_eoln, 0 }, /* %V */
125 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Ac */
126 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Ao */
127 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Aq */
128 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* At */
129 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Bc */
130 { blk_full, MDOC_EXPLICIT }, /* Bf */
131 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Bo */
132 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Bq */
133 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bsx */
134 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bx */
135 { in_line_eoln, 0 }, /* Db */
136 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Dc */
137 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Do */
138 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Dq */
139 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Ec */
140 { blk_exp_close, MDOC_EXPLICIT }, /* Ef */
141 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Em */
142 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Eo */
143 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Fx */
144 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ms */
145 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_IGNDELIM }, /* No */
146 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_IGNDELIM }, /* Ns */
147 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Nx */
148 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ox */
149 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Pc */
150 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_IGNDELIM }, /* Pf */
151 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Po */
152 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Pq */
153 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Qc */
154 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Ql */
155 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Qo */
156 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Qq */
157 { blk_exp_close, MDOC_EXPLICIT }, /* Re */
158 { blk_full, MDOC_EXPLICIT }, /* Rs */
159 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Sc */
160 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* So */
161 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Sq */
162 { in_line_eoln, 0 }, /* Sm */
163 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Sx */
164 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Sy */
165 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Tn */
166 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ux */
167 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Xc */
168 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Xo */
169 { blk_full, MDOC_EXPLICIT | MDOC_CALLABLE }, /* Fo */
170 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Fc */
171 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Oo */
172 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Oc */
173 { blk_full, MDOC_EXPLICIT }, /* Bk */
174 { blk_exp_close, MDOC_EXPLICIT }, /* Ek */
175 { in_line_eoln, 0 }, /* Bt */
176 { in_line_eoln, 0 }, /* Hf */
177 { obsolete, 0 }, /* Fr */
178 { in_line_eoln, 0 }, /* Ud */
179 { in_line, 0 }, /* Lb */
180 { in_line_eoln, 0 }, /* Lp */
181 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Lk */
182 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Mt */
183 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Brq */
184 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Bro */
185 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Brc */
186 { in_line_eoln, 0 }, /* %C */
187 { obsolete, 0 }, /* Es */
188 { obsolete, 0 }, /* En */
189 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Dx */
190 { in_line_eoln, 0 }, /* %Q */
191 { in_line_eoln, 0 }, /* br */
192 { in_line_eoln, 0 }, /* sp */
193 { in_line_eoln, 0 }, /* %U */
194 { phrase_ta, MDOC_CALLABLE | MDOC_PARSED }, /* Ta */
197 const struct mdoc_macro * const mdoc_macros = __mdoc_macros;
201 * This is called at the end of parsing. It must traverse up the tree,
202 * closing out open [implicit] scopes. Obviously, open explicit scopes
206 mdoc_macroend(struct mdoc *m)
210 /* Scan for open explicit scopes. */
212 n = MDOC_VALID & m->last->flags ? m->last->parent : m->last;
214 for ( ; n; n = n->parent)
215 if (MDOC_BLOCK == n->type &&
216 MDOC_EXPLICIT & mdoc_macros[n->tok].flags)
217 mdoc_nmsg(m, n, MANDOCERR_SCOPEEXIT);
219 /* Rewind to the first. */
221 return(rew_last(m, m->first));
226 * Look up a macro from within a subsequent context.
229 lookup(enum mdoct from, const char *p)
232 if ( ! (MDOC_PARSED & mdoc_macros[from].flags))
234 return(lookup_raw(p));
239 * Lookup a macro following the initial line macro.
242 lookup_raw(const char *p)
246 if (MDOC_MAX == (res = mdoc_hash_find(p)))
248 if (MDOC_CALLABLE & mdoc_macros[res].flags)
255 rew_last(struct mdoc *mdoc, const struct mdoc_node *to)
257 struct mdoc_node *n, *np;
260 mdoc->next = MDOC_NEXT_SIBLING;
263 while (mdoc->last != to) {
265 * Save the parent here, because we may delete the
266 * m->last node in the post-validation phase and reset
267 * it to m->last->parent, causing a step in the closing
270 np = mdoc->last->parent;
271 if ( ! mdoc_valid_post(mdoc))
276 mdoc->last->last = n;
279 return(mdoc_valid_post(mdoc));
284 * For a block closing macro, return the corresponding opening one.
285 * Otherwise, return the macro itself.
288 rew_alt(enum mdoct tok)
331 * Rewinding to tok, how do we have to handle *p?
332 * REWIND_NONE: *p would delimit tok, but no tok scope is open
333 * inside *p, so there is no need to rewind anything at all.
334 * REWIND_THIS: *p matches tok, so rewind *p and nothing else.
335 * REWIND_MORE: *p is implicit, rewind it and keep searching for tok.
336 * REWIND_FORCE: *p is explicit, but tok is full, force rewinding *p.
337 * REWIND_LATER: *p is explicit and still open, postpone rewinding.
338 * REWIND_ERROR: No tok block is open at all.
341 rew_dohalt(enum mdoct tok, enum mdoc_type type,
342 const struct mdoc_node *p)
346 * No matching token, no delimiting block, no broken block.
347 * This can happen when full implicit macros are called for
348 * the first time but try to rewind their previous
351 if (MDOC_ROOT == p->type)
352 return(MDOC_BLOCK == type &&
353 MDOC_EXPLICIT & mdoc_macros[tok].flags ?
354 REWIND_ERROR : REWIND_NONE);
357 * When starting to rewind, skip plain text
358 * and nodes that have already been rewound.
360 if (MDOC_TEXT == p->type || MDOC_VALID & p->flags)
364 * The easiest case: Found a matching token.
365 * This applies to both blocks and elements.
369 return(p->end ? REWIND_NONE :
370 type == p->type ? REWIND_THIS : REWIND_MORE);
373 * While elements do require rewinding for themselves,
374 * they never affect rewinding of other nodes.
376 if (MDOC_ELEM == p->type)
380 * Blocks delimited by our target token get REWIND_MORE.
381 * Blocks delimiting our target token get REWIND_NONE.
385 if (MDOC_It == p->tok)
389 if (MDOC_BODY == p->type && MDOC_Bl == p->tok)
393 * XXX Badly nested block handling still fails badly
394 * when one block is breaking two blocks of the same type.
395 * This is an incomplete and extremely ugly workaround,
396 * required to let the OpenBSD tree build.
399 if (MDOC_Op == p->tok)
407 if (MDOC_BODY == p->type && MDOC_Sh == p->tok)
411 if (MDOC_Nd == p->tok || MDOC_Ss == p->tok ||
420 * Default block rewinding rules.
421 * In particular, always skip block end markers,
422 * and let all blocks rewind Nm children.
424 if (ENDBODY_NOT != p->end || MDOC_Nm == p->tok ||
425 (MDOC_BLOCK == p->type &&
426 ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)))
430 * By default, closing out full blocks
431 * forces closing of broken explicit blocks,
432 * while closing out partial blocks
433 * allows delayed rewinding by default.
435 return (&blk_full == mdoc_macros[tok].fp ?
436 REWIND_FORCE : REWIND_LATER);
441 rew_elem(struct mdoc *mdoc, enum mdoct tok)
446 if (MDOC_ELEM != n->type)
448 assert(MDOC_ELEM == n->type);
449 assert(tok == n->tok);
451 return(rew_last(mdoc, n));
456 * We are trying to close a block identified by tok,
457 * but the child block *broken is still open.
458 * Thus, postpone closing the tok block
459 * until the rew_sub call closing *broken.
462 make_pending(struct mdoc_node *broken, enum mdoct tok,
463 struct mdoc *m, int line, int ppos)
465 struct mdoc_node *breaker;
468 * Iterate backwards, searching for the block matching tok,
469 * that is, the block breaking the *broken block.
471 for (breaker = broken->parent; breaker; breaker = breaker->parent) {
474 * If the *broken block had already been broken before
475 * and we encounter its breaker, make the tok block
476 * pending on the inner breaker.
477 * Graphically, "[A breaker=[B broken=[C->B B] tok=A] C]"
478 * becomes "[A broken=[B [C->B B] tok=A] C]"
479 * and finally "[A [B->A [C->B B] A] C]".
481 if (breaker == broken->pending) {
486 if (REWIND_THIS != rew_dohalt(tok, MDOC_BLOCK, breaker))
488 if (MDOC_BODY == broken->type)
489 broken = broken->parent;
493 * If another, outer breaker is already pending on
494 * the *broken block, we must not clobber the link
495 * to the outer breaker, but make it pending on the
496 * new, now inner breaker.
497 * Graphically, "[A breaker=[B broken=[C->A A] tok=B] C]"
498 * becomes "[A breaker=[B->A broken=[C A] tok=B] C]"
499 * and finally "[A [B->A [C->B A] B] C]".
501 if (broken->pending) {
502 struct mdoc_node *taker;
505 * If the breaker had also been broken before,
506 * it cannot take on the outer breaker itself,
507 * but must hand it on to its own breakers.
508 * Graphically, this is the following situation:
509 * "[A [B breaker=[C->B B] broken=[D->A A] tok=C] D]"
510 * "[A taker=[B->A breaker=[C->B B] [D->C A] C] D]"
513 while (taker->pending)
514 taker = taker->pending;
515 taker->pending = broken->pending;
517 broken->pending = breaker;
518 mandoc_vmsg(MANDOCERR_SCOPENEST, m->parse, line, ppos,
519 "%s breaks %s", mdoc_macronames[tok],
520 mdoc_macronames[broken->tok]);
525 * Found no matching block for tok.
526 * Are you trying to close a block that is not open?
533 rew_sub(enum mdoc_type t, struct mdoc *m,
534 enum mdoct tok, int line, int ppos)
540 switch (rew_dohalt(tok, t, n)) {
546 mandoc_vmsg(MANDOCERR_SCOPEBROKEN, m->parse,
547 line, ppos, "%s breaks %s",
548 mdoc_macronames[tok],
549 mdoc_macronames[n->tok]);
555 if (make_pending(n, tok, m, line, ppos) ||
560 mdoc_pmsg(m, line, ppos, MANDOCERR_NOSCOPE);
567 if ( ! rew_last(m, n))
571 * The current block extends an enclosing block.
572 * Now that the current block ends, close the enclosing block, too.
574 while (NULL != (n = n->pending)) {
575 if ( ! rew_last(m, n))
577 if (MDOC_HEAD == n->type &&
578 ! mdoc_body_alloc(m, n->line, n->pos, n->tok))
586 * Allocate a word and check whether it's punctuation or not.
587 * Punctuation consists of those tokens found in mdoc_isdelim().
590 dword(struct mdoc *m, int line,
591 int col, const char *p, enum mdelim d)
597 if ( ! mdoc_word_alloc(m, line, col, p))
601 m->last->flags |= MDOC_DELIMO;
604 * Closing delimiters only suppress the preceding space
605 * when they follow something, not when they start a new
606 * block or element, and not when they follow `No'.
608 * XXX Explicitly special-casing MDOC_No here feels
609 * like a layering violation. Find a better way
610 * and solve this in the code related to `No'!
613 else if (DELIM_CLOSE == d && m->last->prev &&
614 m->last->prev->tok != MDOC_No)
615 m->last->flags |= MDOC_DELIMC;
621 append_delims(struct mdoc *m, int line, int *pos, char *buf)
627 if ('\0' == buf[*pos])
632 ac = mdoc_zargs(m, line, pos, buf, &p);
634 if (ARGS_ERROR == ac)
636 else if (ARGS_EOLN == ac)
639 dword(m, line, la, p, DELIM_MAX);
642 * If we encounter end-of-sentence symbols, then trigger
645 * XXX: it's easy to allow this to propagate outward to
646 * the last symbol, such that `. )' will cause the
647 * correct double-spacing. However, (1) groff isn't
648 * smart enough to do this and (2) it would require
649 * knowing which symbols break this behaviour, for
650 * example, `. ;' shouldn't propagate the double-space.
652 if (mandoc_eos(p, strlen(p), 0))
653 m->last->flags |= MDOC_EOS;
661 * Close out block partial/full explicit.
664 blk_exp_close(MACRO_PROT_ARGS)
666 struct mdoc_node *body; /* Our own body. */
667 struct mdoc_node *later; /* A sub-block starting later. */
668 struct mdoc_node *n; /* For searching backwards. */
670 int j, lastarg, maxargs, flushed, nl;
672 enum mdoct atok, ntok;
675 nl = MDOC_NEWLINE & m->flags;
687 * Search backwards for beginnings of blocks,
688 * both of our own and of pending sub-blocks.
692 for (n = m->last; n; n = n->parent) {
693 if (MDOC_VALID & n->flags)
696 /* Remember the start of our own body. */
697 if (MDOC_BODY == n->type && atok == n->tok) {
698 if (ENDBODY_NOT == n->end)
703 if (MDOC_BLOCK != n->type || MDOC_Nm == n->tok)
705 if (atok == n->tok) {
709 * Found the start of our own block.
710 * When there is no pending sub block,
711 * just proceed to closing out.
717 * When there is a pending sub block,
718 * postpone closing out the current block
719 * until the rew_sub() closing out the sub-block.
721 make_pending(later, tok, m, line, ppos);
724 * Mark the place where the formatting - but not
725 * the scope - of the current block ends.
727 if ( ! mdoc_endbody_alloc(m, line, ppos,
728 atok, body, ENDBODY_SPACE))
734 * When finding an open sub block, remember the last
735 * open explicit block, or, in case there are only
736 * implicit ones, the first open implicit block.
739 MDOC_EXPLICIT & mdoc_macros[later->tok].flags)
741 if (MDOC_CALLABLE & mdoc_macros[n->tok].flags)
745 if ( ! (MDOC_CALLABLE & mdoc_macros[tok].flags)) {
746 /* FIXME: do this in validate */
748 mdoc_pmsg(m, line, ppos, MANDOCERR_ARGSLOST);
750 if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
752 return(rew_sub(MDOC_BLOCK, m, tok, line, ppos));
755 if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
758 if (NULL == later && maxargs > 0)
759 if ( ! mdoc_tail_alloc(m, line, ppos, rew_alt(tok)))
762 for (flushed = j = 0; ; j++) {
765 if (j == maxargs && ! flushed) {
766 if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
771 ac = mdoc_args(m, line, pos, buf, tok, &p);
773 if (ARGS_ERROR == ac)
775 if (ARGS_PUNCT == ac)
780 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
782 if (MDOC_MAX == ntok) {
783 if ( ! dword(m, line, lastarg, p, DELIM_MAX))
789 if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
793 if ( ! mdoc_macro(m, ntok, line, lastarg, pos, buf))
798 if ( ! flushed && ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
803 return(append_delims(m, line, pos, buf));
808 in_line(MACRO_PROT_ARGS)
810 int la, scope, cnt, nc, nl;
815 struct mdoc_arg *arg;
818 nl = MDOC_NEWLINE & m->flags;
821 * Whether we allow ignored elements (those without content,
822 * usually because of reserved words) to squeak by.
844 for (arg = NULL;; ) {
846 av = mdoc_argv(m, line, tok, &arg, pos, buf);
848 if (ARGV_WORD == av) {
861 for (cnt = scope = 0;; ) {
863 ac = mdoc_args(m, line, pos, buf, tok, &p);
865 if (ARGS_ERROR == ac)
869 if (ARGS_PUNCT == ac)
872 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
875 * In this case, we've located a submacro and must
876 * execute it. Close out scope, if open. If no
877 * elements have been generated, either create one (nc)
878 * or raise a warning.
881 if (MDOC_MAX != ntok) {
882 if (scope && ! rew_elem(m, tok))
884 if (nc && 0 == cnt) {
885 if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
887 if ( ! rew_last(m, m->last))
889 } else if ( ! nc && 0 == cnt) {
891 mdoc_pmsg(m, line, ppos, MANDOCERR_MACROEMPTY);
894 if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
898 return(append_delims(m, line, pos, buf));
902 * Non-quote-enclosed punctuation. Set up our scope, if
903 * a word; rewind the scope, if a delimiter; then append
907 d = ARGS_QWORD == ac ? DELIM_NONE : mdoc_isdelim(p);
909 if (DELIM_NONE != d) {
911 * If we encounter closing punctuation, no word
912 * has been omitted, no scope is open, and we're
913 * allowed to have an empty element, then start
914 * a new scope. `Ar', `Fl', and `Li', only do
915 * this once per invocation. There may be more
916 * of these (all of them?).
918 if (0 == cnt && (nc || MDOC_Li == tok) &&
919 DELIM_CLOSE == d && ! scope) {
920 if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
922 if (MDOC_Ar == tok || MDOC_Li == tok ||
928 * Close out our scope, if one is open, before
931 if (scope && ! rew_elem(m, tok))
934 } else if ( ! scope) {
935 if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
943 if ( ! dword(m, line, la, p, d))
947 * `Fl' macros have their scope re-opened with each new
948 * word so that the `-' can be added to each one without
949 * having to parse out spaces.
951 if (scope && MDOC_Fl == tok) {
952 if ( ! rew_elem(m, tok))
958 if (scope && ! rew_elem(m, tok))
962 * If no elements have been collected and we're allowed to have
963 * empties (nc), open a scope and close it out. Otherwise,
967 if (nc && 0 == cnt) {
968 if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
970 if ( ! rew_last(m, m->last))
972 } else if ( ! nc && 0 == cnt) {
974 mdoc_pmsg(m, line, ppos, MANDOCERR_MACROEMPTY);
979 return(append_delims(m, line, pos, buf));
984 blk_full(MACRO_PROT_ARGS)
987 struct mdoc_arg *arg;
988 struct mdoc_node *head; /* save of head macro */
989 struct mdoc_node *body; /* save of body macro */
993 enum margserr ac, lac;
997 nl = MDOC_NEWLINE & m->flags;
999 /* Close out prior implicit scope. */
1001 if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)) {
1002 if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1004 if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1009 * This routine accommodates implicitly- and explicitly-scoped
1010 * macro openings. Implicit ones first close out prior scope
1011 * (seen above). Delay opening the head until necessary to
1012 * allow leading punctuation to print. Special consideration
1013 * for `It -column', which has phrase-part syntax instead of
1014 * regular child nodes.
1017 for (arg = NULL;; ) {
1019 av = mdoc_argv(m, line, tok, &arg, pos, buf);
1021 if (ARGV_WORD == av) {
1026 if (ARGV_EOLN == av)
1031 mdoc_argv_free(arg);
1035 if ( ! mdoc_block_alloc(m, line, ppos, tok, arg))
1041 * Exception: Heads of `It' macros in `-diag' lists are not
1042 * parsed, even though `It' macros in general are parsed.
1044 nparsed = MDOC_It == tok &&
1045 MDOC_Bl == m->last->parent->tok &&
1046 LIST_diag == m->last->parent->norm->Bl.type;
1049 * The `Nd' macro has all arguments in its body: it's a hybrid
1050 * of block partial-explicit and full-implicit. Stupid.
1053 if (MDOC_Nd == tok) {
1054 if ( ! mdoc_head_alloc(m, line, ppos, tok))
1057 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1059 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1068 /* Initialise last-phrase-type with ARGS_PEND. */
1069 lac = ARGS_ERROR == ac ? ARGS_PEND : ac;
1070 ac = mdoc_args(m, line, pos, buf, tok, &p);
1072 if (ARGS_PUNCT == ac)
1075 if (ARGS_ERROR == ac)
1078 if (ARGS_EOLN == ac) {
1079 if (ARGS_PPHRASE != lac && ARGS_PHRASE != lac)
1082 * This is necessary: if the last token on a
1083 * line is a `Ta' or tab, then we'll get
1084 * ARGS_EOLN, so we must be smart enough to
1085 * reopen our scope if the last parse was a
1086 * phrase or partial phrase.
1088 if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1090 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1097 * Emit leading punctuation (i.e., punctuation before
1098 * the MDOC_HEAD) for non-phrase types.
1103 ARGS_PHRASE != ac &&
1104 ARGS_PPHRASE != ac &&
1106 DELIM_OPEN == mdoc_isdelim(p)) {
1107 if ( ! dword(m, line, la, p, DELIM_OPEN))
1112 /* Open a head if one hasn't been opened. */
1115 if ( ! mdoc_head_alloc(m, line, ppos, tok))
1120 if (ARGS_PHRASE == ac ||
1122 ARGS_PPHRASE == ac) {
1124 * If we haven't opened a body yet, rewind the
1125 * head; if we have, rewind that instead.
1128 mtt = body ? MDOC_BODY : MDOC_HEAD;
1129 if ( ! rew_sub(mtt, m, tok, line, ppos))
1132 /* Then allocate our body context. */
1134 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1139 * Process phrases: set whether we're in a
1140 * partial-phrase (this effects line handling)
1141 * then call down into the phrase parser.
1144 if (ARGS_PPHRASE == ac)
1145 m->flags |= MDOC_PPHRASE;
1146 if (ARGS_PEND == ac && ARGS_PPHRASE == lac)
1147 m->flags |= MDOC_PPHRASE;
1149 if ( ! phrase(m, line, la, buf))
1152 m->flags &= ~MDOC_PPHRASE;
1156 ntok = nparsed || ARGS_QWORD == ac ?
1157 MDOC_MAX : lookup(tok, p);
1159 if (MDOC_MAX == ntok) {
1160 if ( ! dword(m, line, la, p, DELIM_MAX))
1165 if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1171 if ( ! mdoc_head_alloc(m, line, ppos, tok))
1176 if (nl && ! append_delims(m, line, pos, buf))
1179 /* If we've already opened our body, exit now. */
1185 * If there is an open (i.e., unvalidated) sub-block requiring
1186 * explicit close-out, postpone switching the current block from
1187 * head to body until the rew_sub() call closing out that
1190 for (n = m->last; n && n != head; n = n->parent) {
1191 if (MDOC_BLOCK == n->type &&
1192 MDOC_EXPLICIT & mdoc_macros[n->tok].flags &&
1193 ! (MDOC_VALID & n->flags)) {
1199 /* Close out scopes to remain in a consistent state. */
1201 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1203 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1207 if ( ! (MDOC_FREECOL & m->flags))
1210 if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1212 if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1215 m->flags &= ~MDOC_FREECOL;
1221 blk_part_imp(MACRO_PROT_ARGS)
1227 struct mdoc_node *blk; /* saved block context */
1228 struct mdoc_node *body; /* saved body context */
1229 struct mdoc_node *n;
1231 nl = MDOC_NEWLINE & m->flags;
1234 * A macro that spans to the end of the line. This is generally
1235 * (but not necessarily) called as the first macro. The block
1236 * has a head as the immediate child, which is always empty,
1237 * followed by zero or more opening punctuation nodes, then the
1238 * body (which may be empty, depending on the macro), then zero
1239 * or more closing punctuation nodes.
1242 if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL))
1247 if ( ! mdoc_head_alloc(m, line, ppos, tok))
1249 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1253 * Open the body scope "on-demand", that is, after we've
1254 * processed all our the leading delimiters (open parenthesis,
1258 for (body = NULL; ; ) {
1260 ac = mdoc_args(m, line, pos, buf, tok, &p);
1262 if (ARGS_ERROR == ac)
1264 if (ARGS_EOLN == ac)
1266 if (ARGS_PUNCT == ac)
1269 if (NULL == body && ARGS_QWORD != ac &&
1270 DELIM_OPEN == mdoc_isdelim(p)) {
1271 if ( ! dword(m, line, la, p, DELIM_OPEN))
1277 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1282 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1284 if (MDOC_MAX == ntok) {
1285 if ( ! dword(m, line, la, p, DELIM_MAX))
1290 if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1295 /* Clean-ups to leave in a consistent state. */
1298 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1303 for (n = body->child; n && n->next; n = n->next)
1307 * End of sentence spacing: if the last node is a text node and
1308 * has a trailing period, then mark it as being end-of-sentence.
1311 if (n && MDOC_TEXT == n->type && n->string)
1312 if (mandoc_eos(n->string, strlen(n->string), 1))
1313 n->flags |= MDOC_EOS;
1315 /* Up-propagate the end-of-space flag. */
1317 if (n && (MDOC_EOS & n->flags)) {
1318 body->flags |= MDOC_EOS;
1319 body->parent->flags |= MDOC_EOS;
1323 * If there is an open sub-block requiring explicit close-out,
1324 * postpone closing out the current block
1325 * until the rew_sub() call closing out the sub-block.
1327 for (n = m->last; n && n != body && n != blk->parent; n = n->parent) {
1328 if (MDOC_BLOCK == n->type &&
1329 MDOC_EXPLICIT & mdoc_macros[n->tok].flags &&
1330 ! (MDOC_VALID & n->flags)) {
1331 make_pending(n, tok, m, line, ppos);
1332 if ( ! mdoc_endbody_alloc(m, line, ppos,
1333 tok, body, ENDBODY_NOSPACE))
1340 * If we can't rewind to our body, then our scope has already
1341 * been closed by another macro (like `Oc' closing `Op'). This
1342 * is ugly behaviour nodding its head to OpenBSD's overwhelming
1343 * crufty use of `Op' breakage.
1346 mandoc_vmsg(MANDOCERR_SCOPENEST, m->parse, line, ppos,
1347 "%s broken", mdoc_macronames[tok]);
1349 if (n && ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1352 /* Standard appending of delimiters. */
1354 if (nl && ! append_delims(m, line, pos, buf))
1357 /* Rewind scope, if applicable. */
1359 if (n && ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1367 blk_part_exp(MACRO_PROT_ARGS)
1371 struct mdoc_node *head; /* keep track of head */
1372 struct mdoc_node *body; /* keep track of body */
1376 nl = MDOC_NEWLINE & m->flags;
1379 * The opening of an explicit macro having zero or more leading
1380 * punctuation nodes; a head with optional single element (the
1381 * case of `Eo'); and a body that may be empty.
1384 if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL))
1387 for (head = body = NULL; ; ) {
1389 ac = mdoc_args(m, line, pos, buf, tok, &p);
1391 if (ARGS_ERROR == ac)
1393 if (ARGS_PUNCT == ac)
1395 if (ARGS_EOLN == ac)
1398 /* Flush out leading punctuation. */
1400 if (NULL == head && ARGS_QWORD != ac &&
1401 DELIM_OPEN == mdoc_isdelim(p)) {
1402 assert(NULL == body);
1403 if ( ! dword(m, line, la, p, DELIM_OPEN))
1409 assert(NULL == body);
1410 if ( ! mdoc_head_alloc(m, line, ppos, tok))
1416 * `Eo' gobbles any data into the head, but most other
1417 * macros just immediately close out and begin the body.
1422 /* No check whether it's a macro! */
1424 if ( ! dword(m, line, la, p, DELIM_MAX))
1427 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1429 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1437 assert(NULL != head && NULL != body);
1439 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1441 if (MDOC_MAX == ntok) {
1442 if ( ! dword(m, line, la, p, DELIM_MAX))
1447 if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1452 /* Clean-up to leave in a consistent state. */
1455 if ( ! mdoc_head_alloc(m, line, ppos, tok))
1459 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1461 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1465 /* Standard appending of delimiters. */
1469 return(append_delims(m, line, pos, buf));
1475 in_line_argn(MACRO_PROT_ARGS)
1477 int la, flushed, j, maxargs, nl;
1480 struct mdoc_arg *arg;
1484 nl = MDOC_NEWLINE & m->flags;
1487 * A line macro that has a fixed number of arguments (maxargs).
1488 * Only open the scope once the first non-leading-punctuation is
1489 * found (unless MDOC_IGNDELIM is noted, like in `Pf'), then
1490 * keep it open until the maximum number of arguments are
1514 for (arg = NULL; ; ) {
1516 av = mdoc_argv(m, line, tok, &arg, pos, buf);
1518 if (ARGV_WORD == av) {
1523 if (ARGV_EOLN == av)
1528 mdoc_argv_free(arg);
1532 for (flushed = j = 0; ; ) {
1534 ac = mdoc_args(m, line, pos, buf, tok, &p);
1536 if (ARGS_ERROR == ac)
1538 if (ARGS_PUNCT == ac)
1540 if (ARGS_EOLN == ac)
1543 if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1544 ARGS_QWORD != ac && 0 == j &&
1545 DELIM_OPEN == mdoc_isdelim(p)) {
1546 if ( ! dword(m, line, la, p, DELIM_OPEN))
1550 if ( ! mdoc_elem_alloc(m, line, la, tok, arg))
1553 if (j == maxargs && ! flushed) {
1554 if ( ! rew_elem(m, tok))
1559 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1561 if (MDOC_MAX != ntok) {
1562 if ( ! flushed && ! rew_elem(m, tok))
1565 if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1571 if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1574 DELIM_NONE != mdoc_isdelim(p)) {
1575 if ( ! rew_elem(m, tok))
1580 if ( ! dword(m, line, la, p, DELIM_MAX))
1585 if (0 == j && ! mdoc_elem_alloc(m, line, la, tok, arg))
1588 /* Close out in a consistent state. */
1590 if ( ! flushed && ! rew_elem(m, tok))
1594 return(append_delims(m, line, pos, buf));
1599 in_line_eoln(MACRO_PROT_ARGS)
1604 struct mdoc_arg *arg;
1608 assert( ! (MDOC_PARSED & mdoc_macros[tok].flags));
1611 rew_sub(MDOC_BLOCK, m, MDOC_Nm, line, ppos);
1613 /* Parse macro arguments. */
1615 for (arg = NULL; ; ) {
1617 av = mdoc_argv(m, line, tok, &arg, pos, buf);
1619 if (ARGV_WORD == av) {
1623 if (ARGV_EOLN == av)
1628 mdoc_argv_free(arg);
1632 /* Open element scope. */
1634 if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1637 /* Parse argument terms. */
1641 ac = mdoc_args(m, line, pos, buf, tok, &p);
1643 if (ARGS_ERROR == ac)
1645 if (ARGS_EOLN == ac)
1648 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1650 if (MDOC_MAX == ntok) {
1651 if ( ! dword(m, line, la, p, DELIM_MAX))
1656 if ( ! rew_elem(m, tok))
1658 return(mdoc_macro(m, ntok, line, la, pos, buf));
1661 /* Close out (no delimiters). */
1663 return(rew_elem(m, tok));
1669 ctx_synopsis(MACRO_PROT_ARGS)
1673 nl = MDOC_NEWLINE & m->flags;
1675 /* If we're not in the SYNOPSIS, go straight to in-line. */
1676 if ( ! (MDOC_SYNOPSIS & m->flags))
1677 return(in_line(m, tok, line, ppos, pos, buf));
1679 /* If we're a nested call, same place. */
1681 return(in_line(m, tok, line, ppos, pos, buf));
1684 * XXX: this will open a block scope; however, if later we end
1685 * up formatting the block scope, then child nodes will inherit
1686 * the formatting. Be careful.
1689 return(blk_full(m, tok, line, ppos, pos, buf));
1690 assert(MDOC_Vt == tok);
1691 return(blk_part_imp(m, tok, line, ppos, pos, buf));
1697 obsolete(MACRO_PROT_ARGS)
1700 mdoc_pmsg(m, line, ppos, MANDOCERR_MACROOBS);
1706 * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
1707 * They're unusual because they're basically free-form text until a
1708 * macro is encountered.
1711 phrase(struct mdoc *m, int line, int ppos, char *buf)
1718 for (pos = ppos; ; ) {
1721 ac = mdoc_zargs(m, line, &pos, buf, &p);
1723 if (ARGS_ERROR == ac)
1725 if (ARGS_EOLN == ac)
1728 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup_raw(p);
1730 if (MDOC_MAX == ntok) {
1731 if ( ! dword(m, line, la, p, DELIM_MAX))
1736 if ( ! mdoc_macro(m, ntok, line, la, &pos, buf))
1738 return(append_delims(m, line, &pos, buf));
1747 phrase_ta(MACRO_PROT_ARGS)
1755 * FIXME: this is overly restrictive: if the `Ta' is unexpected,
1756 * it should simply error out with ARGSLOST.
1759 if ( ! rew_sub(MDOC_BODY, m, MDOC_It, line, ppos))
1761 if ( ! mdoc_body_alloc(m, line, ppos, MDOC_It))
1766 ac = mdoc_zargs(m, line, pos, buf, &p);
1768 if (ARGS_ERROR == ac)
1770 if (ARGS_EOLN == ac)
1773 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup_raw(p);
1775 if (MDOC_MAX == ntok) {
1776 if ( ! dword(m, line, la, p, DELIM_MAX))
1781 if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1783 return(append_delims(m, line, pos, buf));