1 /* $Id: mdoc_macro.c,v 1.99 2010/12/15 23:39:40 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010 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.
31 #include "libmandoc.h"
33 enum rew { /* see rew_dohalt() */
42 static int blk_full(MACRO_PROT_ARGS);
43 static int blk_exp_close(MACRO_PROT_ARGS);
44 static int blk_part_exp(MACRO_PROT_ARGS);
45 static int blk_part_imp(MACRO_PROT_ARGS);
46 static int ctx_synopsis(MACRO_PROT_ARGS);
47 static int in_line_eoln(MACRO_PROT_ARGS);
48 static int in_line_argn(MACRO_PROT_ARGS);
49 static int in_line(MACRO_PROT_ARGS);
50 static int obsolete(MACRO_PROT_ARGS);
51 static int phrase_ta(MACRO_PROT_ARGS);
53 static int append_delims(struct mdoc *,
55 static enum mdoct lookup(enum mdoct, const char *);
56 static enum mdoct lookup_raw(const char *);
57 static int make_pending(struct mdoc_node *, enum mdoct,
58 struct mdoc *, int, int);
59 static int phrase(struct mdoc *, int, int, char *);
60 static enum mdoct rew_alt(enum mdoct);
61 static enum rew rew_dohalt(enum mdoct, enum mdoc_type,
62 const struct mdoc_node *);
63 static int rew_elem(struct mdoc *, enum mdoct);
64 static int rew_last(struct mdoc *,
65 const struct mdoc_node *);
66 static int rew_sub(enum mdoc_type, struct mdoc *,
67 enum mdoct, int, int);
69 const struct mdoc_macro __mdoc_macros[MDOC_MAX] = {
70 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ap */
71 { in_line_eoln, MDOC_PROLOGUE }, /* Dd */
72 { in_line_eoln, MDOC_PROLOGUE }, /* Dt */
73 { in_line_eoln, MDOC_PROLOGUE }, /* Os */
74 { blk_full, 0 }, /* Sh */
75 { blk_full, 0 }, /* Ss */
76 { in_line_eoln, 0 }, /* Pp */
77 { blk_part_imp, MDOC_PARSED }, /* D1 */
78 { blk_part_imp, MDOC_PARSED }, /* Dl */
79 { blk_full, MDOC_EXPLICIT }, /* Bd */
80 { blk_exp_close, MDOC_EXPLICIT }, /* Ed */
81 { blk_full, MDOC_EXPLICIT }, /* Bl */
82 { blk_exp_close, MDOC_EXPLICIT }, /* El */
83 { blk_full, MDOC_PARSED }, /* It */
84 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ad */
85 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* An */
86 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ar */
87 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Cd */
88 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Cm */
89 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Dv */
90 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Er */
91 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ev */
92 { in_line_eoln, 0 }, /* Ex */
93 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fa */
94 { in_line_eoln, 0 }, /* Fd */
95 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fl */
96 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fn */
97 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ft */
98 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ic */
99 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* In */
100 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Li */
101 { blk_full, 0 }, /* Nd */
102 { ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED }, /* Nm */
103 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Op */
104 { obsolete, 0 }, /* Ot */
105 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Pa */
106 { in_line_eoln, 0 }, /* Rv */
107 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* St */
108 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Va */
109 { ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED }, /* Vt */
110 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Xr */
111 { in_line_eoln, 0 }, /* %A */
112 { in_line_eoln, 0 }, /* %B */
113 { in_line_eoln, 0 }, /* %D */
114 { in_line_eoln, 0 }, /* %I */
115 { in_line_eoln, 0 }, /* %J */
116 { in_line_eoln, 0 }, /* %N */
117 { in_line_eoln, 0 }, /* %O */
118 { in_line_eoln, 0 }, /* %P */
119 { in_line_eoln, 0 }, /* %R */
120 { in_line_eoln, 0 }, /* %T */
121 { in_line_eoln, 0 }, /* %V */
122 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Ac */
123 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Ao */
124 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Aq */
125 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* At */
126 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Bc */
127 { blk_full, MDOC_EXPLICIT }, /* Bf */
128 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Bo */
129 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Bq */
130 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bsx */
131 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bx */
132 { in_line_eoln, 0 }, /* Db */
133 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Dc */
134 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Do */
135 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Dq */
136 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Ec */
137 { blk_exp_close, MDOC_EXPLICIT }, /* Ef */
138 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Em */
139 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Eo */
140 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Fx */
141 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ms */
142 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_IGNDELIM }, /* No */
143 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_IGNDELIM }, /* Ns */
144 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Nx */
145 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ox */
146 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Pc */
147 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_IGNDELIM }, /* Pf */
148 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Po */
149 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Pq */
150 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Qc */
151 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Ql */
152 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Qo */
153 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Qq */
154 { blk_exp_close, MDOC_EXPLICIT }, /* Re */
155 { blk_full, MDOC_EXPLICIT }, /* Rs */
156 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Sc */
157 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* So */
158 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Sq */
159 { in_line_eoln, 0 }, /* Sm */
160 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Sx */
161 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Sy */
162 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Tn */
163 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ux */
164 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Xc */
165 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Xo */
166 { blk_full, MDOC_EXPLICIT | MDOC_CALLABLE }, /* Fo */
167 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Fc */
168 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Oo */
169 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Oc */
170 { blk_full, MDOC_EXPLICIT }, /* Bk */
171 { blk_exp_close, MDOC_EXPLICIT }, /* Ek */
172 { in_line_eoln, 0 }, /* Bt */
173 { in_line_eoln, 0 }, /* Hf */
174 { obsolete, 0 }, /* Fr */
175 { in_line_eoln, 0 }, /* Ud */
176 { in_line, 0 }, /* Lb */
177 { in_line_eoln, 0 }, /* Lp */
178 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Lk */
179 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Mt */
180 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Brq */
181 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Bro */
182 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Brc */
183 { in_line_eoln, 0 }, /* %C */
184 { obsolete, 0 }, /* Es */
185 { obsolete, 0 }, /* En */
186 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Dx */
187 { in_line_eoln, 0 }, /* %Q */
188 { in_line_eoln, 0 }, /* br */
189 { in_line_eoln, 0 }, /* sp */
190 { in_line_eoln, 0 }, /* %U */
191 { phrase_ta, MDOC_CALLABLE | MDOC_PARSED }, /* Ta */
194 const struct mdoc_macro * const mdoc_macros = __mdoc_macros;
198 * This is called at the end of parsing. It must traverse up the tree,
199 * closing out open [implicit] scopes. Obviously, open explicit scopes
203 mdoc_macroend(struct mdoc *m)
207 /* Scan for open explicit scopes. */
209 n = MDOC_VALID & m->last->flags ? m->last->parent : m->last;
211 for ( ; n; n = n->parent)
212 if (MDOC_BLOCK == n->type &&
213 MDOC_EXPLICIT & mdoc_macros[n->tok].flags)
214 mdoc_nmsg(m, n, MANDOCERR_SCOPEEXIT);
216 /* Rewind to the first. */
218 return(rew_last(m, m->first));
223 * Look up a macro from within a subsequent context.
226 lookup(enum mdoct from, const char *p)
228 /* FIXME: make -diag lists be un-PARSED. */
230 if ( ! (MDOC_PARSED & mdoc_macros[from].flags))
232 return(lookup_raw(p));
237 * Lookup a macro following the initial line macro.
240 lookup_raw(const char *p)
244 if (MDOC_MAX == (res = mdoc_hash_find(p)))
246 if (MDOC_CALLABLE & mdoc_macros[res].flags)
253 rew_last(struct mdoc *mdoc, const struct mdoc_node *to)
258 mdoc->next = MDOC_NEXT_SIBLING;
261 while (mdoc->last != to) {
262 if ( ! mdoc_valid_post(mdoc))
265 mdoc->last = mdoc->last->parent;
267 mdoc->last->last = n;
270 return(mdoc_valid_post(mdoc));
275 * For a block closing macro, return the corresponding opening one.
276 * Otherwise, return the macro itself.
279 rew_alt(enum mdoct tok)
322 * Rewinding to tok, how do we have to handle *p?
323 * REWIND_NONE: *p would delimit tok, but no tok scope is open
324 * inside *p, so there is no need to rewind anything at all.
325 * REWIND_THIS: *p matches tok, so rewind *p and nothing else.
326 * REWIND_MORE: *p is implicit, rewind it and keep searching for tok.
327 * REWIND_FORCE: *p is explicit, but tok is full, force rewinding *p.
328 * REWIND_LATER: *p is explicit and still open, postpone rewinding.
329 * REWIND_ERROR: No tok block is open at all.
332 rew_dohalt(enum mdoct tok, enum mdoc_type type,
333 const struct mdoc_node *p)
337 * No matching token, no delimiting block, no broken block.
338 * This can happen when full implicit macros are called for
339 * the first time but try to rewind their previous
342 if (MDOC_ROOT == p->type)
343 return(MDOC_BLOCK == type &&
344 MDOC_EXPLICIT & mdoc_macros[tok].flags ?
345 REWIND_ERROR : REWIND_NONE);
348 * When starting to rewind, skip plain text
349 * and nodes that have already been rewound.
351 if (MDOC_TEXT == p->type || MDOC_VALID & p->flags)
355 * The easiest case: Found a matching token.
356 * This applies to both blocks and elements.
360 return(p->end ? REWIND_NONE :
361 type == p->type ? REWIND_THIS : REWIND_MORE);
364 * While elements do require rewinding for themselves,
365 * they never affect rewinding of other nodes.
367 if (MDOC_ELEM == p->type)
371 * Blocks delimited by our target token get REWIND_MORE.
372 * Blocks delimiting our target token get REWIND_NONE.
376 if (MDOC_It == p->tok)
380 if (MDOC_BODY == p->type && MDOC_Bl == p->tok)
384 * XXX Badly nested block handling still fails badly
385 * when one block is breaking two blocks of the same type.
386 * This is an incomplete and extremely ugly workaround,
387 * required to let the OpenBSD tree build.
390 if (MDOC_Op == p->tok)
398 if (MDOC_BODY == p->type && MDOC_Sh == p->tok)
402 if (MDOC_Nd == p->tok || MDOC_Ss == p->tok ||
411 * Default block rewinding rules.
412 * In particular, always skip block end markers,
413 * and let all blocks rewind Nm children.
415 if (ENDBODY_NOT != p->end || MDOC_Nm == p->tok ||
416 (MDOC_BLOCK == p->type &&
417 ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)))
421 * By default, closing out full blocks
422 * forces closing of broken explicit blocks,
423 * while closing out partial blocks
424 * allows delayed rewinding by default.
426 return (&blk_full == mdoc_macros[tok].fp ?
427 REWIND_FORCE : REWIND_LATER);
432 rew_elem(struct mdoc *mdoc, enum mdoct tok)
437 if (MDOC_ELEM != n->type)
439 assert(MDOC_ELEM == n->type);
440 assert(tok == n->tok);
442 return(rew_last(mdoc, n));
447 * We are trying to close a block identified by tok,
448 * but the child block *broken is still open.
449 * Thus, postpone closing the tok block
450 * until the rew_sub call closing *broken.
453 make_pending(struct mdoc_node *broken, enum mdoct tok,
454 struct mdoc *m, int line, int ppos)
456 struct mdoc_node *breaker;
459 * Iterate backwards, searching for the block matching tok,
460 * that is, the block breaking the *broken block.
462 for (breaker = broken->parent; breaker; breaker = breaker->parent) {
465 * If the *broken block had already been broken before
466 * and we encounter its breaker, make the tok block
467 * pending on the inner breaker.
468 * Graphically, "[A breaker=[B broken=[C->B B] tok=A] C]"
469 * becomes "[A broken=[B [C->B B] tok=A] C]"
470 * and finally "[A [B->A [C->B B] A] C]".
472 if (breaker == broken->pending) {
477 if (REWIND_THIS != rew_dohalt(tok, MDOC_BLOCK, breaker))
479 if (MDOC_BODY == broken->type)
480 broken = broken->parent;
484 * If another, outer breaker is already pending on
485 * the *broken block, we must not clobber the link
486 * to the outer breaker, but make it pending on the
487 * new, now inner breaker.
488 * Graphically, "[A breaker=[B broken=[C->A A] tok=B] C]"
489 * becomes "[A breaker=[B->A broken=[C A] tok=B] C]"
490 * and finally "[A [B->A [C->B A] B] C]".
492 if (broken->pending) {
493 struct mdoc_node *taker;
496 * If the breaker had also been broken before,
497 * it cannot take on the outer breaker itself,
498 * but must hand it on to its own breakers.
499 * Graphically, this is the following situation:
500 * "[A [B breaker=[C->B B] broken=[D->A A] tok=C] D]"
501 * "[A taker=[B->A breaker=[C->B B] [D->C A] C] D]"
504 while (taker->pending)
505 taker = taker->pending;
506 taker->pending = broken->pending;
508 broken->pending = breaker;
509 mdoc_vmsg(m, MANDOCERR_SCOPENEST, line, ppos,
510 "%s breaks %s", mdoc_macronames[tok],
511 mdoc_macronames[broken->tok]);
516 * Found no matching block for tok.
517 * Are you trying to close a block that is not open?
524 rew_sub(enum mdoc_type t, struct mdoc *m,
525 enum mdoct tok, int line, int ppos)
531 switch (rew_dohalt(tok, t, n)) {
537 mdoc_vmsg(m, MANDOCERR_SCOPEBROKEN, line, ppos,
538 "%s breaks %s", mdoc_macronames[tok],
539 mdoc_macronames[n->tok]);
545 if (make_pending(n, tok, m, line, ppos) ||
550 mdoc_pmsg(m, line, ppos, MANDOCERR_NOSCOPE);
557 if ( ! rew_last(m, n))
561 * The current block extends an enclosing block.
562 * Now that the current block ends, close the enclosing block, too.
564 while (NULL != (n = n->pending)) {
565 if ( ! rew_last(m, n))
567 if (MDOC_HEAD == n->type &&
568 ! mdoc_body_alloc(m, n->line, n->pos, n->tok))
577 append_delims(struct mdoc *m, int line, int *pos, char *buf)
583 if ('\0' == buf[*pos])
588 ac = mdoc_zargs(m, line, pos, buf, ARGS_NOWARN, &p);
590 if (ARGS_ERROR == ac)
592 else if (ARGS_EOLN == ac)
595 assert(DELIM_NONE != mdoc_isdelim(p));
596 if ( ! mdoc_word_alloc(m, line, la, p))
600 * If we encounter end-of-sentence symbols, then trigger
603 * XXX: it's easy to allow this to propogate outward to
604 * the last symbol, such that `. )' will cause the
605 * correct double-spacing. However, (1) groff isn't
606 * smart enough to do this and (2) it would require
607 * knowing which symbols break this behaviour, for
608 * example, `. ;' shouldn't propogate the double-space.
610 if (mandoc_eos(p, strlen(p), 0))
611 m->last->flags |= MDOC_EOS;
619 * Close out block partial/full explicit.
622 blk_exp_close(MACRO_PROT_ARGS)
624 struct mdoc_node *body; /* Our own body. */
625 struct mdoc_node *later; /* A sub-block starting later. */
626 struct mdoc_node *n; /* For searching backwards. */
628 int j, lastarg, maxargs, flushed, nl;
630 enum mdoct atok, ntok;
633 nl = MDOC_NEWLINE & m->flags;
645 * Search backwards for beginnings of blocks,
646 * both of our own and of pending sub-blocks.
650 for (n = m->last; n; n = n->parent) {
651 if (MDOC_VALID & n->flags)
654 /* Remember the start of our own body. */
655 if (MDOC_BODY == n->type && atok == n->tok) {
656 if (ENDBODY_NOT == n->end)
661 if (MDOC_BLOCK != n->type || MDOC_Nm == n->tok)
663 if (atok == n->tok) {
667 * Found the start of our own block.
668 * When there is no pending sub block,
669 * just proceed to closing out.
675 * When there is a pending sub block,
676 * postpone closing out the current block
677 * until the rew_sub() closing out the sub-block.
679 make_pending(later, tok, m, line, ppos);
682 * Mark the place where the formatting - but not
683 * the scope - of the current block ends.
685 if ( ! mdoc_endbody_alloc(m, line, ppos,
686 atok, body, ENDBODY_SPACE))
692 * When finding an open sub block, remember the last
693 * open explicit block, or, in case there are only
694 * implicit ones, the first open implicit block.
697 MDOC_EXPLICIT & mdoc_macros[later->tok].flags)
699 if (MDOC_CALLABLE & mdoc_macros[n->tok].flags)
703 if ( ! (MDOC_CALLABLE & mdoc_macros[tok].flags)) {
704 /* FIXME: do this in validate */
706 if ( ! mdoc_pmsg(m, line, ppos, MANDOCERR_ARGSLOST))
709 if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
711 return(rew_sub(MDOC_BLOCK, m, tok, line, ppos));
714 if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
717 if (NULL == later && maxargs > 0)
718 if ( ! mdoc_tail_alloc(m, line, ppos, rew_alt(tok)))
721 for (flushed = j = 0; ; j++) {
724 if (j == maxargs && ! flushed) {
725 if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
730 ac = mdoc_args(m, line, pos, buf, tok, &p);
732 if (ARGS_ERROR == ac)
734 if (ARGS_PUNCT == ac)
739 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
741 if (MDOC_MAX == ntok) {
742 if ( ! mdoc_word_alloc(m, line, lastarg, p))
748 if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
752 if ( ! mdoc_macro(m, ntok, line, lastarg, pos, buf))
757 if ( ! flushed && ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
762 return(append_delims(m, line, pos, buf));
767 in_line(MACRO_PROT_ARGS)
769 int la, scope, cnt, nc, nl;
774 struct mdoc_arg *arg;
777 nl = MDOC_NEWLINE & m->flags;
780 * Whether we allow ignored elements (those without content,
781 * usually because of reserved words) to squeak by.
803 for (arg = NULL;; ) {
805 av = mdoc_argv(m, line, tok, &arg, pos, buf);
807 if (ARGV_WORD == av) {
820 for (cnt = scope = 0;; ) {
822 ac = mdoc_args(m, line, pos, buf, tok, &p);
824 if (ARGS_ERROR == ac)
828 if (ARGS_PUNCT == ac)
831 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
834 * In this case, we've located a submacro and must
835 * execute it. Close out scope, if open. If no
836 * elements have been generated, either create one (nc)
837 * or raise a warning.
840 if (MDOC_MAX != ntok) {
841 if (scope && ! rew_elem(m, tok))
843 if (nc && 0 == cnt) {
844 if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
846 if ( ! rew_last(m, m->last))
848 } else if ( ! nc && 0 == cnt) {
850 if ( ! mdoc_pmsg(m, line, ppos, MANDOCERR_MACROEMPTY))
853 if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
857 return(append_delims(m, line, pos, buf));
861 * Non-quote-enclosed punctuation. Set up our scope, if
862 * a word; rewind the scope, if a delimiter; then append
866 d = ARGS_QWORD == ac ? DELIM_NONE : mdoc_isdelim(p);
868 if (DELIM_NONE != d) {
870 * If we encounter closing punctuation, no word
871 * has been omitted, no scope is open, and we're
872 * allowed to have an empty element, then start
873 * a new scope. `Ar', `Fl', and `Li', only do
874 * this once per invocation. There may be more
875 * of these (all of them?).
877 if (0 == cnt && (nc || MDOC_Li == tok) &&
878 DELIM_CLOSE == d && ! scope) {
879 if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
881 if (MDOC_Ar == tok || MDOC_Li == tok ||
887 * Close out our scope, if one is open, before
890 if (scope && ! rew_elem(m, tok))
893 } else if ( ! scope) {
894 if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
901 if ( ! mdoc_word_alloc(m, line, la, p))
905 * `Fl' macros have their scope re-opened with each new
906 * word so that the `-' can be added to each one without
907 * having to parse out spaces.
909 if (scope && MDOC_Fl == tok) {
910 if ( ! rew_elem(m, tok))
916 if (scope && ! rew_elem(m, tok))
920 * If no elements have been collected and we're allowed to have
921 * empties (nc), open a scope and close it out. Otherwise,
925 if (nc && 0 == cnt) {
926 if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
928 if ( ! rew_last(m, m->last))
930 } else if ( ! nc && 0 == cnt) {
932 if ( ! mdoc_pmsg(m, line, ppos, MANDOCERR_MACROEMPTY))
938 return(append_delims(m, line, pos, buf));
943 blk_full(MACRO_PROT_ARGS)
946 struct mdoc_arg *arg;
947 struct mdoc_node *head; /* save of head macro */
948 struct mdoc_node *body; /* save of body macro */
952 enum margserr ac, lac;
956 nl = MDOC_NEWLINE & m->flags;
958 /* Close out prior implicit scope. */
960 if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)) {
961 if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
963 if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
968 * This routine accomodates implicitly- and explicitly-scoped
969 * macro openings. Implicit ones first close out prior scope
970 * (seen above). Delay opening the head until necessary to
971 * allow leading punctuation to print. Special consideration
972 * for `It -column', which has phrase-part syntax instead of
973 * regular child nodes.
976 for (arg = NULL;; ) {
978 av = mdoc_argv(m, line, tok, &arg, pos, buf);
980 if (ARGV_WORD == av) {
994 if ( ! mdoc_block_alloc(m, line, ppos, tok, arg))
1000 * The `Nd' macro has all arguments in its body: it's a hybrid
1001 * of block partial-explicit and full-implicit. Stupid.
1004 if (MDOC_Nd == tok) {
1005 if ( ! mdoc_head_alloc(m, line, ppos, tok))
1008 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1010 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1019 /* Initialise last-phrase-type with ARGS_PEND. */
1020 lac = ARGS_ERROR == ac ? ARGS_PEND : ac;
1021 ac = mdoc_args(m, line, pos, buf, tok, &p);
1023 if (ARGS_PUNCT == ac)
1026 if (ARGS_ERROR == ac)
1029 if (ARGS_EOLN == ac) {
1030 if (ARGS_PPHRASE != lac && ARGS_PHRASE != lac)
1033 * This is necessary: if the last token on a
1034 * line is a `Ta' or tab, then we'll get
1035 * ARGS_EOLN, so we must be smart enough to
1036 * reopen our scope if the last parse was a
1037 * phrase or partial phrase.
1039 if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1041 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1048 * Emit leading punctuation (i.e., punctuation before
1049 * the MDOC_HEAD) for non-phrase types.
1054 ARGS_PHRASE != ac &&
1055 ARGS_PPHRASE != ac &&
1057 DELIM_OPEN == mdoc_isdelim(p)) {
1058 if ( ! mdoc_word_alloc(m, line, la, p))
1063 /* Open a head if one hasn't been opened. */
1066 if ( ! mdoc_head_alloc(m, line, ppos, tok))
1071 if (ARGS_PHRASE == ac ||
1073 ARGS_PPHRASE == ac) {
1075 * If we haven't opened a body yet, rewind the
1076 * head; if we have, rewind that instead.
1079 mtt = body ? MDOC_BODY : MDOC_HEAD;
1080 if ( ! rew_sub(mtt, m, tok, line, ppos))
1083 /* Then allocate our body context. */
1085 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1090 * Process phrases: set whether we're in a
1091 * partial-phrase (this effects line handling)
1092 * then call down into the phrase parser.
1095 if (ARGS_PPHRASE == ac)
1096 m->flags |= MDOC_PPHRASE;
1097 if (ARGS_PEND == ac && ARGS_PPHRASE == lac)
1098 m->flags |= MDOC_PPHRASE;
1100 if ( ! phrase(m, line, la, buf))
1103 m->flags &= ~MDOC_PPHRASE;
1107 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1109 if (MDOC_MAX == ntok) {
1110 if ( ! mdoc_word_alloc(m, line, la, p))
1115 if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1121 if ( ! mdoc_head_alloc(m, line, ppos, tok))
1126 if (nl && ! append_delims(m, line, pos, buf))
1129 /* If we've already opened our body, exit now. */
1135 * If there is an open (i.e., unvalidated) sub-block requiring
1136 * explicit close-out, postpone switching the current block from
1137 * head to body until the rew_sub() call closing out that
1140 for (n = m->last; n && n != head; n = n->parent) {
1141 if (MDOC_BLOCK == n->type &&
1142 MDOC_EXPLICIT & mdoc_macros[n->tok].flags &&
1143 ! (MDOC_VALID & n->flags)) {
1149 /* Close out scopes to remain in a consistent state. */
1151 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1153 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1157 if ( ! (MDOC_FREECOL & m->flags))
1160 if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1162 if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1165 m->flags &= ~MDOC_FREECOL;
1171 blk_part_imp(MACRO_PROT_ARGS)
1177 struct mdoc_node *blk; /* saved block context */
1178 struct mdoc_node *body; /* saved body context */
1179 struct mdoc_node *n;
1181 nl = MDOC_NEWLINE & m->flags;
1184 * A macro that spans to the end of the line. This is generally
1185 * (but not necessarily) called as the first macro. The block
1186 * has a head as the immediate child, which is always empty,
1187 * followed by zero or more opening punctuation nodes, then the
1188 * body (which may be empty, depending on the macro), then zero
1189 * or more closing punctuation nodes.
1192 if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL))
1197 if ( ! mdoc_head_alloc(m, line, ppos, tok))
1199 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1203 * Open the body scope "on-demand", that is, after we've
1204 * processed all our the leading delimiters (open parenthesis,
1208 for (body = NULL; ; ) {
1210 ac = mdoc_args(m, line, pos, buf, tok, &p);
1212 if (ARGS_ERROR == ac)
1214 if (ARGS_EOLN == ac)
1216 if (ARGS_PUNCT == ac)
1219 if (NULL == body && ARGS_QWORD != ac &&
1220 DELIM_OPEN == mdoc_isdelim(p)) {
1221 if ( ! mdoc_word_alloc(m, line, la, p))
1227 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1232 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1234 if (MDOC_MAX == ntok) {
1235 if ( ! mdoc_word_alloc(m, line, la, p))
1240 if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1245 /* Clean-ups to leave in a consistent state. */
1248 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1253 for (n = body->child; n && n->next; n = n->next)
1257 * End of sentence spacing: if the last node is a text node and
1258 * has a trailing period, then mark it as being end-of-sentence.
1261 if (n && MDOC_TEXT == n->type && n->string)
1262 if (mandoc_eos(n->string, strlen(n->string), 1))
1263 n->flags |= MDOC_EOS;
1265 /* Up-propogate the end-of-space flag. */
1267 if (n && (MDOC_EOS & n->flags)) {
1268 body->flags |= MDOC_EOS;
1269 body->parent->flags |= MDOC_EOS;
1273 * If there is an open sub-block requiring explicit close-out,
1274 * postpone closing out the current block
1275 * until the rew_sub() call closing out the sub-block.
1277 for (n = m->last; n && n != body && n != blk->parent; n = n->parent) {
1278 if (MDOC_BLOCK == n->type &&
1279 MDOC_EXPLICIT & mdoc_macros[n->tok].flags &&
1280 ! (MDOC_VALID & n->flags)) {
1281 make_pending(n, tok, m, line, ppos);
1282 if ( ! mdoc_endbody_alloc(m, line, ppos,
1283 tok, body, ENDBODY_NOSPACE))
1290 * If we can't rewind to our body, then our scope has already
1291 * been closed by another macro (like `Oc' closing `Op'). This
1292 * is ugly behaviour nodding its head to OpenBSD's overwhelming
1293 * crufty use of `Op' breakage.
1295 if (n != body && ! mdoc_vmsg(m, MANDOCERR_SCOPENEST,
1296 line, ppos, "%s broken", mdoc_macronames[tok]))
1299 if (n && ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1302 /* Standard appending of delimiters. */
1304 if (nl && ! append_delims(m, line, pos, buf))
1307 /* Rewind scope, if applicable. */
1309 if (n && ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1317 blk_part_exp(MACRO_PROT_ARGS)
1321 struct mdoc_node *head; /* keep track of head */
1322 struct mdoc_node *body; /* keep track of body */
1326 nl = MDOC_NEWLINE & m->flags;
1329 * The opening of an explicit macro having zero or more leading
1330 * punctuation nodes; a head with optional single element (the
1331 * case of `Eo'); and a body that may be empty.
1334 if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL))
1337 for (head = body = NULL; ; ) {
1339 ac = mdoc_args(m, line, pos, buf, tok, &p);
1341 if (ARGS_ERROR == ac)
1343 if (ARGS_PUNCT == ac)
1345 if (ARGS_EOLN == ac)
1348 /* Flush out leading punctuation. */
1350 if (NULL == head && ARGS_QWORD != ac &&
1351 DELIM_OPEN == mdoc_isdelim(p)) {
1352 assert(NULL == body);
1353 if ( ! mdoc_word_alloc(m, line, la, p))
1359 assert(NULL == body);
1360 if ( ! mdoc_head_alloc(m, line, ppos, tok))
1366 * `Eo' gobbles any data into the head, but most other
1367 * macros just immediately close out and begin the body.
1372 /* No check whether it's a macro! */
1374 if ( ! mdoc_word_alloc(m, line, la, p))
1377 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1379 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1387 assert(NULL != head && NULL != body);
1389 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1391 if (MDOC_MAX == ntok) {
1392 if ( ! mdoc_word_alloc(m, line, la, p))
1397 if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1402 /* Clean-up to leave in a consistent state. */
1405 if ( ! mdoc_head_alloc(m, line, ppos, tok))
1411 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1413 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1418 /* Standard appending of delimiters. */
1422 return(append_delims(m, line, pos, buf));
1428 in_line_argn(MACRO_PROT_ARGS)
1430 int la, flushed, j, maxargs, nl;
1433 struct mdoc_arg *arg;
1437 nl = MDOC_NEWLINE & m->flags;
1440 * A line macro that has a fixed number of arguments (maxargs).
1441 * Only open the scope once the first non-leading-punctuation is
1442 * found (unless MDOC_IGNDELIM is noted, like in `Pf'), then
1443 * keep it open until the maximum number of arguments are
1465 for (arg = NULL; ; ) {
1467 av = mdoc_argv(m, line, tok, &arg, pos, buf);
1469 if (ARGV_WORD == av) {
1474 if (ARGV_EOLN == av)
1479 mdoc_argv_free(arg);
1483 for (flushed = j = 0; ; ) {
1485 ac = mdoc_args(m, line, pos, buf, tok, &p);
1487 if (ARGS_ERROR == ac)
1489 if (ARGS_PUNCT == ac)
1491 if (ARGS_EOLN == ac)
1494 if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1496 0 == j && DELIM_OPEN == mdoc_isdelim(p)) {
1497 if ( ! mdoc_word_alloc(m, line, la, p))
1501 if ( ! mdoc_elem_alloc(m, line, la, tok, arg))
1504 if (j == maxargs && ! flushed) {
1505 if ( ! rew_elem(m, tok))
1510 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1512 if (MDOC_MAX != ntok) {
1513 if ( ! flushed && ! rew_elem(m, tok))
1516 if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1522 if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1525 DELIM_NONE != mdoc_isdelim(p)) {
1526 if ( ! rew_elem(m, tok))
1532 * XXX: this is a hack to work around groff's ugliness
1533 * as regards `Xr' and extraneous arguments. It should
1534 * ideally be deprecated behaviour, but because this is
1535 * code is no here, it's unlikely to be removed.
1539 if (MDOC_Xr == tok && j == maxargs) {
1540 if ( ! mdoc_elem_alloc(m, line, la, MDOC_Ns, NULL))
1542 if ( ! rew_elem(m, MDOC_Ns))
1547 if ( ! mdoc_word_alloc(m, line, la, p))
1552 if (0 == j && ! mdoc_elem_alloc(m, line, la, tok, arg))
1555 /* Close out in a consistent state. */
1557 if ( ! flushed && ! rew_elem(m, tok))
1561 return(append_delims(m, line, pos, buf));
1566 in_line_eoln(MACRO_PROT_ARGS)
1571 struct mdoc_arg *arg;
1575 assert( ! (MDOC_PARSED & mdoc_macros[tok].flags));
1578 rew_sub(MDOC_BLOCK, m, MDOC_Nm, line, ppos);
1580 /* Parse macro arguments. */
1582 for (arg = NULL; ; ) {
1584 av = mdoc_argv(m, line, tok, &arg, pos, buf);
1586 if (ARGV_WORD == av) {
1590 if (ARGV_EOLN == av)
1595 mdoc_argv_free(arg);
1599 /* Open element scope. */
1601 if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1604 /* Parse argument terms. */
1608 ac = mdoc_args(m, line, pos, buf, tok, &p);
1610 if (ARGS_ERROR == ac)
1612 if (ARGS_EOLN == ac)
1615 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1617 if (MDOC_MAX == ntok) {
1618 if ( ! mdoc_word_alloc(m, line, la, p))
1623 if ( ! rew_elem(m, tok))
1625 return(mdoc_macro(m, ntok, line, la, pos, buf));
1628 /* Close out (no delimiters). */
1630 return(rew_elem(m, tok));
1636 ctx_synopsis(MACRO_PROT_ARGS)
1640 nl = MDOC_NEWLINE & m->flags;
1642 /* If we're not in the SYNOPSIS, go straight to in-line. */
1643 if ( ! (MDOC_SYNOPSIS & m->flags))
1644 return(in_line(m, tok, line, ppos, pos, buf));
1646 /* If we're a nested call, same place. */
1648 return(in_line(m, tok, line, ppos, pos, buf));
1651 * XXX: this will open a block scope; however, if later we end
1652 * up formatting the block scope, then child nodes will inherit
1653 * the formatting. Be careful.
1656 return(blk_full(m, tok, line, ppos, pos, buf));
1657 assert(MDOC_Vt == tok);
1658 return(blk_part_imp(m, tok, line, ppos, pos, buf));
1664 obsolete(MACRO_PROT_ARGS)
1667 return(mdoc_pmsg(m, line, ppos, MANDOCERR_MACROOBS));
1672 * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
1673 * They're unusual because they're basically free-form text until a
1674 * macro is encountered.
1677 phrase(struct mdoc *m, int line, int ppos, char *buf)
1684 for (pos = ppos; ; ) {
1687 ac = mdoc_zargs(m, line, &pos, buf, 0, &p);
1689 if (ARGS_ERROR == ac)
1691 if (ARGS_EOLN == ac)
1694 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup_raw(p);
1696 if (MDOC_MAX == ntok) {
1697 if ( ! mdoc_word_alloc(m, line, la, p))
1702 if ( ! mdoc_macro(m, ntok, line, la, &pos, buf))
1704 return(append_delims(m, line, &pos, buf));
1713 phrase_ta(MACRO_PROT_ARGS)
1721 * FIXME: this is overly restrictive: if the `Ta' is unexpected,
1722 * it should simply error out with ARGSLOST.
1725 if ( ! rew_sub(MDOC_BODY, m, MDOC_It, line, ppos))
1727 if ( ! mdoc_body_alloc(m, line, ppos, MDOC_It))
1732 ac = mdoc_zargs(m, line, pos, buf, 0, &p);
1734 if (ARGS_ERROR == ac)
1736 if (ARGS_EOLN == ac)
1739 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup_raw(p);
1741 if (MDOC_MAX == ntok) {
1742 if ( ! mdoc_word_alloc(m, line, la, p))
1747 if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1749 return(append_delims(m, line, pos, buf));