1*260e9a87SYuri Pankov /* $Id: man_macro.c,v 1.98 2015/02/06 11:54:36 schwarze Exp $ */
295c635efSGarrett D'Amore /*
395c635efSGarrett D'Amore * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4*260e9a87SYuri Pankov * Copyright (c) 2012, 2013, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
5698f87a4SGarrett D'Amore * Copyright (c) 2013 Franco Fichtner <franco@lastsummer.de>
695c635efSGarrett D'Amore *
795c635efSGarrett D'Amore * Permission to use, copy, modify, and distribute this software for any
895c635efSGarrett D'Amore * purpose with or without fee is hereby granted, provided that the above
995c635efSGarrett D'Amore * copyright notice and this permission notice appear in all copies.
1095c635efSGarrett D'Amore *
1195c635efSGarrett D'Amore * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1295c635efSGarrett D'Amore * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1395c635efSGarrett D'Amore * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1495c635efSGarrett D'Amore * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1595c635efSGarrett D'Amore * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1695c635efSGarrett D'Amore * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1795c635efSGarrett D'Amore * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1895c635efSGarrett D'Amore */
1995c635efSGarrett D'Amore #include "config.h"
20*260e9a87SYuri Pankov
21*260e9a87SYuri Pankov #include <sys/types.h>
2295c635efSGarrett D'Amore
2395c635efSGarrett D'Amore #include <assert.h>
2495c635efSGarrett D'Amore #include <ctype.h>
2595c635efSGarrett D'Amore #include <stdlib.h>
2695c635efSGarrett D'Amore #include <string.h>
2795c635efSGarrett D'Amore
2895c635efSGarrett D'Amore #include "man.h"
2995c635efSGarrett D'Amore #include "mandoc.h"
3095c635efSGarrett D'Amore #include "libmandoc.h"
3195c635efSGarrett D'Amore #include "libman.h"
3295c635efSGarrett D'Amore
3395c635efSGarrett D'Amore enum rew {
3495c635efSGarrett D'Amore REW_REWIND,
3595c635efSGarrett D'Amore REW_NOHALT,
3695c635efSGarrett D'Amore REW_HALT
3795c635efSGarrett D'Amore };
3895c635efSGarrett D'Amore
39*260e9a87SYuri Pankov static void blk_close(MACRO_PROT_ARGS);
40*260e9a87SYuri Pankov static void blk_exp(MACRO_PROT_ARGS);
41*260e9a87SYuri Pankov static void blk_imp(MACRO_PROT_ARGS);
42*260e9a87SYuri Pankov static void in_line_eoln(MACRO_PROT_ARGS);
4395c635efSGarrett D'Amore static int man_args(struct man *, int,
4495c635efSGarrett D'Amore int *, char *, char **);
4595c635efSGarrett D'Amore
46*260e9a87SYuri Pankov static void rew_scope(enum man_type,
4795c635efSGarrett D'Amore struct man *, enum mant);
4895c635efSGarrett D'Amore static enum rew rew_dohalt(enum mant, enum man_type,
4995c635efSGarrett D'Amore const struct man_node *);
5095c635efSGarrett D'Amore static enum rew rew_block(enum mant, enum man_type,
5195c635efSGarrett D'Amore const struct man_node *);
5295c635efSGarrett D'Amore
5395c635efSGarrett D'Amore const struct man_macro __man_macros[MAN_MAX] = {
5495c635efSGarrett D'Amore { in_line_eoln, MAN_NSCOPED }, /* br */
5595c635efSGarrett D'Amore { in_line_eoln, MAN_BSCOPE }, /* TH */
5695c635efSGarrett D'Amore { blk_imp, MAN_BSCOPE | MAN_SCOPED }, /* SH */
5795c635efSGarrett D'Amore { blk_imp, MAN_BSCOPE | MAN_SCOPED }, /* SS */
5895c635efSGarrett D'Amore { blk_imp, MAN_BSCOPE | MAN_SCOPED | MAN_FSCOPED }, /* TP */
5995c635efSGarrett D'Amore { blk_imp, MAN_BSCOPE }, /* LP */
6095c635efSGarrett D'Amore { blk_imp, MAN_BSCOPE }, /* PP */
6195c635efSGarrett D'Amore { blk_imp, MAN_BSCOPE }, /* P */
6295c635efSGarrett D'Amore { blk_imp, MAN_BSCOPE }, /* IP */
6395c635efSGarrett D'Amore { blk_imp, MAN_BSCOPE }, /* HP */
64*260e9a87SYuri Pankov { in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* SM */
65*260e9a87SYuri Pankov { in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* SB */
6695c635efSGarrett D'Amore { in_line_eoln, 0 }, /* BI */
6795c635efSGarrett D'Amore { in_line_eoln, 0 }, /* IB */
6895c635efSGarrett D'Amore { in_line_eoln, 0 }, /* BR */
6995c635efSGarrett D'Amore { in_line_eoln, 0 }, /* RB */
70*260e9a87SYuri Pankov { in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* R */
71*260e9a87SYuri Pankov { in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* B */
72*260e9a87SYuri Pankov { in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* I */
7395c635efSGarrett D'Amore { in_line_eoln, 0 }, /* IR */
7495c635efSGarrett D'Amore { in_line_eoln, 0 }, /* RI */
7595c635efSGarrett D'Amore { in_line_eoln, MAN_NSCOPED }, /* sp */
7695c635efSGarrett D'Amore { in_line_eoln, MAN_BSCOPE }, /* nf */
7795c635efSGarrett D'Amore { in_line_eoln, MAN_BSCOPE }, /* fi */
78*260e9a87SYuri Pankov { blk_close, MAN_BSCOPE }, /* RE */
79698f87a4SGarrett D'Amore { blk_exp, MAN_BSCOPE | MAN_EXPLICIT }, /* RS */
8095c635efSGarrett D'Amore { in_line_eoln, 0 }, /* DT */
8195c635efSGarrett D'Amore { in_line_eoln, 0 }, /* UC */
8295c635efSGarrett D'Amore { in_line_eoln, 0 }, /* PD */
8395c635efSGarrett D'Amore { in_line_eoln, 0 }, /* AT */
8495c635efSGarrett D'Amore { in_line_eoln, 0 }, /* in */
8595c635efSGarrett D'Amore { in_line_eoln, 0 }, /* ft */
8695c635efSGarrett D'Amore { in_line_eoln, 0 }, /* OP */
87698f87a4SGarrett D'Amore { in_line_eoln, MAN_BSCOPE }, /* EX */
88698f87a4SGarrett D'Amore { in_line_eoln, MAN_BSCOPE }, /* EE */
89698f87a4SGarrett D'Amore { blk_exp, MAN_BSCOPE | MAN_EXPLICIT }, /* UR */
90*260e9a87SYuri Pankov { blk_close, MAN_BSCOPE }, /* UE */
91*260e9a87SYuri Pankov { in_line_eoln, 0 }, /* ll */
9295c635efSGarrett D'Amore };
9395c635efSGarrett D'Amore
9495c635efSGarrett D'Amore const struct man_macro * const man_macros = __man_macros;
9595c635efSGarrett D'Amore
9695c635efSGarrett D'Amore
97*260e9a87SYuri Pankov void
man_unscope(struct man * man,const struct man_node * to)98*260e9a87SYuri Pankov man_unscope(struct man *man, const struct man_node *to)
9995c635efSGarrett D'Amore {
10095c635efSGarrett D'Amore struct man_node *n;
10195c635efSGarrett D'Amore
102*260e9a87SYuri Pankov to = to->parent;
103*260e9a87SYuri Pankov n = man->last;
104*260e9a87SYuri Pankov while (n != to) {
10595c635efSGarrett D'Amore
106*260e9a87SYuri Pankov /* Reached the end of the document? */
10795c635efSGarrett D'Amore
108*260e9a87SYuri Pankov if (to == NULL && ! (n->flags & MAN_VALID)) {
109*260e9a87SYuri Pankov if (man->flags & (MAN_BLINE | MAN_ELINE) &&
110*260e9a87SYuri Pankov man_macros[n->tok].flags & MAN_SCOPED) {
111*260e9a87SYuri Pankov mandoc_vmsg(MANDOCERR_BLK_LINE,
112*260e9a87SYuri Pankov man->parse, n->line, n->pos,
113*260e9a87SYuri Pankov "EOF breaks %s",
114*260e9a87SYuri Pankov man_macronames[n->tok]);
115*260e9a87SYuri Pankov if (man->flags & MAN_ELINE)
116*260e9a87SYuri Pankov man->flags &= ~MAN_ELINE;
117*260e9a87SYuri Pankov else {
118*260e9a87SYuri Pankov assert(n->type == MAN_HEAD);
119*260e9a87SYuri Pankov n = n->parent;
120*260e9a87SYuri Pankov man->flags &= ~MAN_BLINE;
121*260e9a87SYuri Pankov }
122698f87a4SGarrett D'Amore man->last = n;
123*260e9a87SYuri Pankov n = n->parent;
124*260e9a87SYuri Pankov man_node_delete(man, man->last);
125*260e9a87SYuri Pankov continue;
126*260e9a87SYuri Pankov }
127*260e9a87SYuri Pankov if (n->type == MAN_BLOCK &&
128*260e9a87SYuri Pankov man_macros[n->tok].flags & MAN_EXPLICIT)
129*260e9a87SYuri Pankov mandoc_msg(MANDOCERR_BLK_NOEND,
130*260e9a87SYuri Pankov man->parse, n->line, n->pos,
131*260e9a87SYuri Pankov man_macronames[n->tok]);
13295c635efSGarrett D'Amore }
13395c635efSGarrett D'Amore
134*260e9a87SYuri Pankov /*
135*260e9a87SYuri Pankov * We might delete the man->last node
136*260e9a87SYuri Pankov * in the post-validation phase.
137*260e9a87SYuri Pankov * Save a pointer to the parent such that
138*260e9a87SYuri Pankov * we know where to continue the iteration.
139*260e9a87SYuri Pankov */
14095c635efSGarrett D'Amore
141*260e9a87SYuri Pankov man->last = n;
142*260e9a87SYuri Pankov n = n->parent;
143*260e9a87SYuri Pankov man_valid_post(man);
14495c635efSGarrett D'Amore }
14595c635efSGarrett D'Amore
146*260e9a87SYuri Pankov /*
147*260e9a87SYuri Pankov * If we ended up at the parent of the node we were
148*260e9a87SYuri Pankov * supposed to rewind to, that means the target node
149*260e9a87SYuri Pankov * got deleted, so add the next node we parse as a child
150*260e9a87SYuri Pankov * of the parent instead of as a sibling of the target.
151*260e9a87SYuri Pankov */
152*260e9a87SYuri Pankov
153*260e9a87SYuri Pankov man->next = (man->last == to) ?
154*260e9a87SYuri Pankov MAN_NEXT_CHILD : MAN_NEXT_SIBLING;
155*260e9a87SYuri Pankov }
15695c635efSGarrett D'Amore
15795c635efSGarrett D'Amore static enum rew
rew_block(enum mant ntok,enum man_type type,const struct man_node * n)15895c635efSGarrett D'Amore rew_block(enum mant ntok, enum man_type type, const struct man_node *n)
15995c635efSGarrett D'Amore {
16095c635efSGarrett D'Amore
161*260e9a87SYuri Pankov if (type == MAN_BLOCK && ntok == n->parent->tok &&
162*260e9a87SYuri Pankov n->parent->type == MAN_BODY)
16395c635efSGarrett D'Amore return(REW_REWIND);
16495c635efSGarrett D'Amore return(ntok == n->tok ? REW_HALT : REW_NOHALT);
16595c635efSGarrett D'Amore }
16695c635efSGarrett D'Amore
16795c635efSGarrett D'Amore /*
16895c635efSGarrett D'Amore * There are three scope levels: scoped to the root (all), scoped to the
16995c635efSGarrett D'Amore * section (all less sections), and scoped to subsections (all less
17095c635efSGarrett D'Amore * sections and subsections).
17195c635efSGarrett D'Amore */
17295c635efSGarrett D'Amore static enum rew
rew_dohalt(enum mant tok,enum man_type type,const struct man_node * n)17395c635efSGarrett D'Amore rew_dohalt(enum mant tok, enum man_type type, const struct man_node *n)
17495c635efSGarrett D'Amore {
17595c635efSGarrett D'Amore enum rew c;
17695c635efSGarrett D'Amore
17795c635efSGarrett D'Amore /* We cannot progress beyond the root ever. */
17895c635efSGarrett D'Amore if (MAN_ROOT == n->type)
17995c635efSGarrett D'Amore return(REW_HALT);
18095c635efSGarrett D'Amore
18195c635efSGarrett D'Amore assert(n->parent);
18295c635efSGarrett D'Amore
18395c635efSGarrett D'Amore /* Normal nodes shouldn't go to the level of the root. */
18495c635efSGarrett D'Amore if (MAN_ROOT == n->parent->type)
18595c635efSGarrett D'Amore return(REW_REWIND);
18695c635efSGarrett D'Amore
18795c635efSGarrett D'Amore /* Already-validated nodes should be closed out. */
18895c635efSGarrett D'Amore if (MAN_VALID & n->flags)
18995c635efSGarrett D'Amore return(REW_NOHALT);
19095c635efSGarrett D'Amore
19195c635efSGarrett D'Amore /* First: rewind to ourselves. */
192698f87a4SGarrett D'Amore if (type == n->type && tok == n->tok) {
193698f87a4SGarrett D'Amore if (MAN_EXPLICIT & man_macros[n->tok].flags)
194698f87a4SGarrett D'Amore return(REW_HALT);
195698f87a4SGarrett D'Amore else
19695c635efSGarrett D'Amore return(REW_REWIND);
197698f87a4SGarrett D'Amore }
19895c635efSGarrett D'Amore
19995c635efSGarrett D'Amore /*
20095c635efSGarrett D'Amore * Next follow the implicit scope-smashings as defined by man.7:
20195c635efSGarrett D'Amore * section, sub-section, etc.
20295c635efSGarrett D'Amore */
20395c635efSGarrett D'Amore
20495c635efSGarrett D'Amore switch (tok) {
205*260e9a87SYuri Pankov case MAN_SH:
20695c635efSGarrett D'Amore break;
207*260e9a87SYuri Pankov case MAN_SS:
20895c635efSGarrett D'Amore /* Rewind to a section, if a block. */
20995c635efSGarrett D'Amore if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
21095c635efSGarrett D'Amore return(c);
21195c635efSGarrett D'Amore break;
212*260e9a87SYuri Pankov case MAN_RS:
213698f87a4SGarrett D'Amore /* Preserve empty paragraphs before RS. */
214698f87a4SGarrett D'Amore if (0 == n->nchild && (MAN_P == n->tok ||
215698f87a4SGarrett D'Amore MAN_PP == n->tok || MAN_LP == n->tok))
216698f87a4SGarrett D'Amore return(REW_HALT);
21795c635efSGarrett D'Amore /* Rewind to a subsection, if a block. */
21895c635efSGarrett D'Amore if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
21995c635efSGarrett D'Amore return(c);
22095c635efSGarrett D'Amore /* Rewind to a section, if a block. */
22195c635efSGarrett D'Amore if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
22295c635efSGarrett D'Amore return(c);
22395c635efSGarrett D'Amore break;
22495c635efSGarrett D'Amore default:
22595c635efSGarrett D'Amore /* Rewind to an offsetter, if a block. */
22695c635efSGarrett D'Amore if (REW_NOHALT != (c = rew_block(MAN_RS, type, n)))
22795c635efSGarrett D'Amore return(c);
22895c635efSGarrett D'Amore /* Rewind to a subsection, if a block. */
22995c635efSGarrett D'Amore if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
23095c635efSGarrett D'Amore return(c);
23195c635efSGarrett D'Amore /* Rewind to a section, if a block. */
23295c635efSGarrett D'Amore if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
23395c635efSGarrett D'Amore return(c);
23495c635efSGarrett D'Amore break;
23595c635efSGarrett D'Amore }
23695c635efSGarrett D'Amore
23795c635efSGarrett D'Amore return(REW_NOHALT);
23895c635efSGarrett D'Amore }
23995c635efSGarrett D'Amore
24095c635efSGarrett D'Amore /*
24195c635efSGarrett D'Amore * Rewinding entails ascending the parse tree until a coherent point,
24295c635efSGarrett D'Amore * for example, the `SH' macro will close out any intervening `SS'
24395c635efSGarrett D'Amore * scopes. When a scope is closed, it must be validated and actioned.
24495c635efSGarrett D'Amore */
245*260e9a87SYuri Pankov static void
rew_scope(enum man_type type,struct man * man,enum mant tok)246698f87a4SGarrett D'Amore rew_scope(enum man_type type, struct man *man, enum mant tok)
24795c635efSGarrett D'Amore {
24895c635efSGarrett D'Amore struct man_node *n;
24995c635efSGarrett D'Amore enum rew c;
25095c635efSGarrett D'Amore
251698f87a4SGarrett D'Amore for (n = man->last; n; n = n->parent) {
25295c635efSGarrett D'Amore /*
25395c635efSGarrett D'Amore * Whether we should stop immediately (REW_HALT), stop
25495c635efSGarrett D'Amore * and rewind until this point (REW_REWIND), or keep
25595c635efSGarrett D'Amore * rewinding (REW_NOHALT).
25695c635efSGarrett D'Amore */
25795c635efSGarrett D'Amore c = rew_dohalt(tok, type, n);
25895c635efSGarrett D'Amore if (REW_HALT == c)
259*260e9a87SYuri Pankov return;
26095c635efSGarrett D'Amore if (REW_REWIND == c)
26195c635efSGarrett D'Amore break;
26295c635efSGarrett D'Amore }
26395c635efSGarrett D'Amore
26495c635efSGarrett D'Amore /*
26595c635efSGarrett D'Amore * Rewind until the current point. Warn if we're a roff
26695c635efSGarrett D'Amore * instruction that's mowing over explicit scopes.
26795c635efSGarrett D'Amore */
26895c635efSGarrett D'Amore
269*260e9a87SYuri Pankov man_unscope(man, n);
27095c635efSGarrett D'Amore }
27195c635efSGarrett D'Amore
27295c635efSGarrett D'Amore
27395c635efSGarrett D'Amore /*
27495c635efSGarrett D'Amore * Close out a generic explicit macro.
27595c635efSGarrett D'Amore */
276*260e9a87SYuri Pankov void
blk_close(MACRO_PROT_ARGS)27795c635efSGarrett D'Amore blk_close(MACRO_PROT_ARGS)
27895c635efSGarrett D'Amore {
27995c635efSGarrett D'Amore enum mant ntok;
28095c635efSGarrett D'Amore const struct man_node *nn;
281*260e9a87SYuri Pankov char *p;
282*260e9a87SYuri Pankov int nrew, target;
28395c635efSGarrett D'Amore
284*260e9a87SYuri Pankov nrew = 1;
28595c635efSGarrett D'Amore switch (tok) {
286*260e9a87SYuri Pankov case MAN_RE:
28795c635efSGarrett D'Amore ntok = MAN_RS;
288*260e9a87SYuri Pankov if ( ! man_args(man, line, pos, buf, &p))
28995c635efSGarrett D'Amore break;
290*260e9a87SYuri Pankov for (nn = man->last->parent; nn; nn = nn->parent)
291*260e9a87SYuri Pankov if (nn->tok == ntok && nn->type == MAN_BLOCK)
292*260e9a87SYuri Pankov nrew++;
293*260e9a87SYuri Pankov target = strtol(p, &p, 10);
294*260e9a87SYuri Pankov if (*p != '\0')
295*260e9a87SYuri Pankov mandoc_vmsg(MANDOCERR_ARG_EXCESS, man->parse,
296*260e9a87SYuri Pankov line, p - buf, "RE ... %s", p);
297*260e9a87SYuri Pankov if (target == 0)
298*260e9a87SYuri Pankov target = 1;
299*260e9a87SYuri Pankov nrew -= target;
300*260e9a87SYuri Pankov if (nrew < 1) {
301*260e9a87SYuri Pankov mandoc_vmsg(MANDOCERR_RE_NOTOPEN, man->parse,
302*260e9a87SYuri Pankov line, ppos, "RE %d", target);
303*260e9a87SYuri Pankov return;
304*260e9a87SYuri Pankov }
305*260e9a87SYuri Pankov break;
306*260e9a87SYuri Pankov case MAN_UE:
307698f87a4SGarrett D'Amore ntok = MAN_UR;
308698f87a4SGarrett D'Amore break;
30995c635efSGarrett D'Amore default:
31095c635efSGarrett D'Amore abort();
31195c635efSGarrett D'Amore /* NOTREACHED */
31295c635efSGarrett D'Amore }
31395c635efSGarrett D'Amore
314698f87a4SGarrett D'Amore for (nn = man->last->parent; nn; nn = nn->parent)
315*260e9a87SYuri Pankov if (nn->tok == ntok && nn->type == MAN_BLOCK && ! --nrew)
31695c635efSGarrett D'Amore break;
31795c635efSGarrett D'Amore
318*260e9a87SYuri Pankov if (nn == NULL) {
319*260e9a87SYuri Pankov mandoc_msg(MANDOCERR_BLK_NOTOPEN, man->parse,
320*260e9a87SYuri Pankov line, ppos, man_macronames[tok]);
321*260e9a87SYuri Pankov rew_scope(MAN_BLOCK, man, MAN_PP);
322*260e9a87SYuri Pankov } else {
323*260e9a87SYuri Pankov line = man->last->line;
324*260e9a87SYuri Pankov ppos = man->last->pos;
325*260e9a87SYuri Pankov ntok = man->last->tok;
326*260e9a87SYuri Pankov man_unscope(man, nn);
32795c635efSGarrett D'Amore
328*260e9a87SYuri Pankov /* Move a trailing paragraph behind the block. */
329*260e9a87SYuri Pankov
330*260e9a87SYuri Pankov if (ntok == MAN_LP || ntok == MAN_PP || ntok == MAN_P) {
331*260e9a87SYuri Pankov *pos = strlen(buf);
332*260e9a87SYuri Pankov blk_imp(man, ntok, line, ppos, pos, buf);
333*260e9a87SYuri Pankov }
334*260e9a87SYuri Pankov }
33595c635efSGarrett D'Amore }
33695c635efSGarrett D'Amore
337*260e9a87SYuri Pankov void
blk_exp(MACRO_PROT_ARGS)33895c635efSGarrett D'Amore blk_exp(MACRO_PROT_ARGS)
33995c635efSGarrett D'Amore {
340*260e9a87SYuri Pankov struct man_node *head;
34195c635efSGarrett D'Amore char *p;
342*260e9a87SYuri Pankov int la;
34395c635efSGarrett D'Amore
344*260e9a87SYuri Pankov rew_scope(MAN_BLOCK, man, tok);
345*260e9a87SYuri Pankov man_block_alloc(man, line, ppos, tok);
346*260e9a87SYuri Pankov man_head_alloc(man, line, ppos, tok);
347*260e9a87SYuri Pankov head = man->last;
34895c635efSGarrett D'Amore
34995c635efSGarrett D'Amore la = *pos;
350*260e9a87SYuri Pankov if (man_args(man, line, pos, buf, &p))
351*260e9a87SYuri Pankov man_word_alloc(man, line, la, p);
352*260e9a87SYuri Pankov
353*260e9a87SYuri Pankov if (buf[*pos] != '\0')
354*260e9a87SYuri Pankov mandoc_vmsg(MANDOCERR_ARG_EXCESS,
355*260e9a87SYuri Pankov man->parse, line, *pos, "%s ... %s",
356*260e9a87SYuri Pankov man_macronames[tok], buf + *pos);
357*260e9a87SYuri Pankov
358*260e9a87SYuri Pankov man_unscope(man, head);
359*260e9a87SYuri Pankov man_body_alloc(man, line, ppos, tok);
36095c635efSGarrett D'Amore }
36195c635efSGarrett D'Amore
36295c635efSGarrett D'Amore /*
36395c635efSGarrett D'Amore * Parse an implicit-block macro. These contain a MAN_HEAD and a
36495c635efSGarrett D'Amore * MAN_BODY contained within a MAN_BLOCK. Rules for closing out other
36595c635efSGarrett D'Amore * scopes, such as `SH' closing out an `SS', are defined in the rew
36695c635efSGarrett D'Amore * routines.
36795c635efSGarrett D'Amore */
368*260e9a87SYuri Pankov void
blk_imp(MACRO_PROT_ARGS)36995c635efSGarrett D'Amore blk_imp(MACRO_PROT_ARGS)
37095c635efSGarrett D'Amore {
37195c635efSGarrett D'Amore int la;
37295c635efSGarrett D'Amore char *p;
37395c635efSGarrett D'Amore struct man_node *n;
37495c635efSGarrett D'Amore
375*260e9a87SYuri Pankov rew_scope(MAN_BODY, man, tok);
376*260e9a87SYuri Pankov rew_scope(MAN_BLOCK, man, tok);
377*260e9a87SYuri Pankov man_block_alloc(man, line, ppos, tok);
378*260e9a87SYuri Pankov man_head_alloc(man, line, ppos, tok);
379698f87a4SGarrett D'Amore n = man->last;
38095c635efSGarrett D'Amore
38195c635efSGarrett D'Amore /* Add line arguments. */
38295c635efSGarrett D'Amore
38395c635efSGarrett D'Amore for (;;) {
38495c635efSGarrett D'Amore la = *pos;
385698f87a4SGarrett D'Amore if ( ! man_args(man, line, pos, buf, &p))
38695c635efSGarrett D'Amore break;
387*260e9a87SYuri Pankov man_word_alloc(man, line, la, p);
38895c635efSGarrett D'Amore }
38995c635efSGarrett D'Amore
39095c635efSGarrett D'Amore /* Close out head and open body (unless MAN_SCOPE). */
39195c635efSGarrett D'Amore
392*260e9a87SYuri Pankov if (man_macros[tok].flags & MAN_SCOPED) {
39395c635efSGarrett D'Amore /* If we're forcing scope (`TP'), keep it open. */
394*260e9a87SYuri Pankov if (man_macros[tok].flags & MAN_FSCOPED) {
395698f87a4SGarrett D'Amore man->flags |= MAN_BLINE;
396*260e9a87SYuri Pankov return;
397698f87a4SGarrett D'Amore } else if (n == man->last) {
398698f87a4SGarrett D'Amore man->flags |= MAN_BLINE;
399*260e9a87SYuri Pankov return;
40095c635efSGarrett D'Amore }
40195c635efSGarrett D'Amore }
402*260e9a87SYuri Pankov rew_scope(MAN_HEAD, man, tok);
403*260e9a87SYuri Pankov man_body_alloc(man, line, ppos, tok);
40495c635efSGarrett D'Amore }
40595c635efSGarrett D'Amore
406*260e9a87SYuri Pankov void
in_line_eoln(MACRO_PROT_ARGS)40795c635efSGarrett D'Amore in_line_eoln(MACRO_PROT_ARGS)
40895c635efSGarrett D'Amore {
40995c635efSGarrett D'Amore int la;
41095c635efSGarrett D'Amore char *p;
41195c635efSGarrett D'Amore struct man_node *n;
41295c635efSGarrett D'Amore
413*260e9a87SYuri Pankov man_elem_alloc(man, line, ppos, tok);
414698f87a4SGarrett D'Amore n = man->last;
41595c635efSGarrett D'Amore
41695c635efSGarrett D'Amore for (;;) {
417*260e9a87SYuri Pankov if (buf[*pos] != '\0' && (tok == MAN_br ||
418*260e9a87SYuri Pankov tok == MAN_fi || tok == MAN_nf)) {
419*260e9a87SYuri Pankov mandoc_vmsg(MANDOCERR_ARG_SKIP,
420*260e9a87SYuri Pankov man->parse, line, *pos, "%s %s",
421*260e9a87SYuri Pankov man_macronames[tok], buf + *pos);
422*260e9a87SYuri Pankov break;
423*260e9a87SYuri Pankov }
424*260e9a87SYuri Pankov if (buf[*pos] != '\0' && man->last != n &&
425*260e9a87SYuri Pankov (tok == MAN_PD || tok == MAN_ft || tok == MAN_sp)) {
426*260e9a87SYuri Pankov mandoc_vmsg(MANDOCERR_ARG_EXCESS,
427*260e9a87SYuri Pankov man->parse, line, *pos, "%s ... %s",
428*260e9a87SYuri Pankov man_macronames[tok], buf + *pos);
429*260e9a87SYuri Pankov break;
430*260e9a87SYuri Pankov }
43195c635efSGarrett D'Amore la = *pos;
432698f87a4SGarrett D'Amore if ( ! man_args(man, line, pos, buf, &p))
43395c635efSGarrett D'Amore break;
434*260e9a87SYuri Pankov if (man_macros[tok].flags & MAN_JOIN &&
435*260e9a87SYuri Pankov man->last->type == MAN_TEXT)
436*260e9a87SYuri Pankov man_word_append(man, p);
437*260e9a87SYuri Pankov else
438*260e9a87SYuri Pankov man_word_alloc(man, line, la, p);
43995c635efSGarrett D'Amore }
44095c635efSGarrett D'Amore
44195c635efSGarrett D'Amore /*
442698f87a4SGarrett D'Amore * Append MAN_EOS in case the last snipped argument
443698f87a4SGarrett D'Amore * ends with a dot, e.g. `.IR syslog (3).'
444698f87a4SGarrett D'Amore */
445698f87a4SGarrett D'Amore
446698f87a4SGarrett D'Amore if (n != man->last &&
447*260e9a87SYuri Pankov mandoc_eos(man->last->string, strlen(man->last->string)))
448698f87a4SGarrett D'Amore man->last->flags |= MAN_EOS;
449698f87a4SGarrett D'Amore
450698f87a4SGarrett D'Amore /*
45195c635efSGarrett D'Amore * If no arguments are specified and this is MAN_SCOPED (i.e.,
45295c635efSGarrett D'Amore * next-line scoped), then set our mode to indicate that we're
45395c635efSGarrett D'Amore * waiting for terms to load into our context.
45495c635efSGarrett D'Amore */
45595c635efSGarrett D'Amore
456*260e9a87SYuri Pankov if (n == man->last && man_macros[tok].flags & MAN_SCOPED) {
457*260e9a87SYuri Pankov assert( ! (man_macros[tok].flags & MAN_NSCOPED));
458698f87a4SGarrett D'Amore man->flags |= MAN_ELINE;
459*260e9a87SYuri Pankov return;
46095c635efSGarrett D'Amore }
46195c635efSGarrett D'Amore
462*260e9a87SYuri Pankov assert(man->last->type != MAN_ROOT);
463698f87a4SGarrett D'Amore man->next = MAN_NEXT_SIBLING;
46495c635efSGarrett D'Amore
46595c635efSGarrett D'Amore /*
46695c635efSGarrett D'Amore * Rewind our element scope. Note that when TH is pruned, we'll
46795c635efSGarrett D'Amore * be back at the root, so make sure that we don't clobber as
46895c635efSGarrett D'Amore * its sibling.
46995c635efSGarrett D'Amore */
47095c635efSGarrett D'Amore
471698f87a4SGarrett D'Amore for ( ; man->last; man->last = man->last->parent) {
472698f87a4SGarrett D'Amore if (man->last == n)
47395c635efSGarrett D'Amore break;
474698f87a4SGarrett D'Amore if (man->last->type == MAN_ROOT)
47595c635efSGarrett D'Amore break;
476*260e9a87SYuri Pankov man_valid_post(man);
47795c635efSGarrett D'Amore }
47895c635efSGarrett D'Amore
479698f87a4SGarrett D'Amore assert(man->last);
48095c635efSGarrett D'Amore
48195c635efSGarrett D'Amore /*
48295c635efSGarrett D'Amore * Same here regarding whether we're back at the root.
48395c635efSGarrett D'Amore */
48495c635efSGarrett D'Amore
485*260e9a87SYuri Pankov if (man->last->type != MAN_ROOT)
486*260e9a87SYuri Pankov man_valid_post(man);
48795c635efSGarrett D'Amore }
48895c635efSGarrett D'Amore
48995c635efSGarrett D'Amore
490*260e9a87SYuri Pankov void
man_macroend(struct man * man)491698f87a4SGarrett D'Amore man_macroend(struct man *man)
49295c635efSGarrett D'Amore {
49395c635efSGarrett D'Amore
494*260e9a87SYuri Pankov man_unscope(man, man->first);
49595c635efSGarrett D'Amore }
49695c635efSGarrett D'Amore
49795c635efSGarrett D'Amore static int
man_args(struct man * man,int line,int * pos,char * buf,char ** v)498698f87a4SGarrett D'Amore man_args(struct man *man, int line, int *pos, char *buf, char **v)
49995c635efSGarrett D'Amore {
50095c635efSGarrett D'Amore char *start;
50195c635efSGarrett D'Amore
50295c635efSGarrett D'Amore assert(*pos);
50395c635efSGarrett D'Amore *v = start = buf + *pos;
50495c635efSGarrett D'Amore assert(' ' != *start);
50595c635efSGarrett D'Amore
50695c635efSGarrett D'Amore if ('\0' == *start)
50795c635efSGarrett D'Amore return(0);
50895c635efSGarrett D'Amore
509698f87a4SGarrett D'Amore *v = mandoc_getarg(man->parse, v, line, pos);
51095c635efSGarrett D'Amore return(1);
51195c635efSGarrett D'Amore }
512