1 /* $Id: man_macro.c,v 1.8 2009/09/18 22:46:14 schwarze Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 #define REW_REWIND (0) /* See rew_scope(). */
25 #define REW_NOHALT (1) /* See rew_scope(). */
26 #define REW_HALT (2) /* See rew_scope(). */
28 static int in_line_eoln(MACRO_PROT_ARGS);
29 static int blk_imp(MACRO_PROT_ARGS);
30 static int blk_close(MACRO_PROT_ARGS);
32 static int rew_scope(enum man_type, struct man *, int);
33 static int rew_dohalt(int, enum man_type,
34 const struct man_node *);
35 static int rew_block(int, enum man_type,
36 const struct man_node *);
38 const struct man_macro __man_macros[MAN_MAX] = {
39 { in_line_eoln, 0 }, /* br */
40 { in_line_eoln, 0 }, /* TH */
41 { blk_imp, MAN_SCOPED }, /* SH */
42 { blk_imp, MAN_SCOPED }, /* SS */
43 { blk_imp, MAN_SCOPED | MAN_FSCOPED }, /* TP */
44 { blk_imp, 0 }, /* LP */
45 { blk_imp, 0 }, /* PP */
46 { blk_imp, 0 }, /* P */
47 { blk_imp, 0 }, /* IP */
48 { blk_imp, 0 }, /* HP */
49 { in_line_eoln, MAN_SCOPED }, /* SM */
50 { in_line_eoln, MAN_SCOPED }, /* SB */
51 { in_line_eoln, 0 }, /* BI */
52 { in_line_eoln, 0 }, /* IB */
53 { in_line_eoln, 0 }, /* BR */
54 { in_line_eoln, 0 }, /* RB */
55 { in_line_eoln, MAN_SCOPED }, /* R */
56 { in_line_eoln, MAN_SCOPED }, /* B */
57 { in_line_eoln, MAN_SCOPED }, /* I */
58 { in_line_eoln, 0 }, /* IR */
59 { in_line_eoln, 0 }, /* RI */
60 { in_line_eoln, 0 }, /* na */
61 { in_line_eoln, 0 }, /* i */
62 { in_line_eoln, 0 }, /* sp */
63 { in_line_eoln, 0 }, /* nf */
64 { in_line_eoln, 0 }, /* fi */
65 { in_line_eoln, 0 }, /* r */
66 { blk_close, 0 }, /* RE */
67 { blk_imp, MAN_EXPLICIT }, /* RS */
68 { in_line_eoln, 0 }, /* DT */
69 { in_line_eoln, 0 }, /* UC */
72 const struct man_macro * const man_macros = __man_macros;
76 man_unscope(struct man *m, const struct man_node *n)
80 m->next = MAN_NEXT_SIBLING;
83 while (m->last != n) {
84 if ( ! man_valid_post(m))
86 if ( ! man_action_post(m))
88 m->last = m->last->parent;
92 if ( ! man_valid_post(m))
94 return(man_action_post(m));
99 rew_block(int ntok, enum man_type type, const struct man_node *n)
102 if (MAN_BLOCK == type && ntok == n->parent->tok &&
103 MAN_BODY == n->parent->type)
105 return(ntok == n->tok ? REW_HALT : REW_NOHALT);
110 * There are three scope levels: scoped to the root (all), scoped to the
111 * section (all less sections), and scoped to subsections (all less
112 * sections and subsections).
115 rew_dohalt(int tok, enum man_type type, const struct man_node *n)
119 if (MAN_ROOT == n->type)
122 if (MAN_ROOT == n->parent->type)
124 if (MAN_VALID & n->flags)
127 /* Rewind to ourselves, first. */
128 if (type == n->type && tok == n->tok)
135 /* Rewind to a section, if a block. */
136 if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
140 /* Rewind to a subsection, if a block. */
141 if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
143 /* Rewind to a section, if a block. */
144 if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
148 /* Rewind to an offsetter, if a block. */
149 if (REW_NOHALT != (c = rew_block(MAN_RS, type, n)))
151 /* Rewind to a subsection, if a block. */
152 if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
154 /* Rewind to a section, if a block. */
155 if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
165 * Rewinding entails ascending the parse tree until a coherent point,
166 * for example, the `SH' macro will close out any intervening `SS'
167 * scopes. When a scope is closed, it must be validated and actioned.
170 rew_scope(enum man_type type, struct man *m, int tok)
176 for (n = m->last; n; n = n->parent) {
178 * Whether we should stop immediately (REW_HALT), stop
179 * and rewind until this point (REW_REWIND), or keep
180 * rewinding (REW_NOHALT).
182 c = rew_dohalt(tok, type, n);
189 /* Rewind until the current point. */
192 return(man_unscope(m, n));
198 blk_close(MACRO_PROT_ARGS)
201 const struct man_node *nn;
212 for (nn = m->last->parent; nn; nn = nn->parent)
217 if ( ! man_pwarn(m, line, ppos, WNOSCOPE))
220 if ( ! rew_scope(MAN_BODY, m, ntok))
222 if ( ! rew_scope(MAN_BLOCK, m, ntok))
224 m->next = MAN_NEXT_SIBLING;
230 * Parse an implicit-block macro. These contain a MAN_HEAD and a
231 * MAN_BODY contained within a MAN_BLOCK. Rules for closing out other
232 * scopes, such as `SH' closing out an `SS', are defined in the rew
236 blk_imp(MACRO_PROT_ARGS)
242 /* Close out prior scopes. */
244 if ( ! rew_scope(MAN_BODY, m, tok))
246 if ( ! rew_scope(MAN_BLOCK, m, tok))
249 /* Allocate new block & head scope. */
251 if ( ! man_block_alloc(m, line, ppos, tok))
253 if ( ! man_head_alloc(m, line, ppos, tok))
258 /* Add line arguments. */
262 w = man_args(m, line, pos, buf, &p);
269 if ( ! man_word_alloc(m, line, la, p))
273 /* Close out head and open body (unless MAN_SCOPE). */
275 if (MAN_SCOPED & man_macros[tok].flags) {
276 /* If we're forcing scope (`TP'), keep it open. */
277 if (MAN_FSCOPED & man_macros[tok].flags) {
278 m->flags |= MAN_BLINE;
280 } else if (n == m->last) {
281 m->flags |= MAN_BLINE;
286 if ( ! rew_scope(MAN_HEAD, m, tok))
289 return(man_body_alloc(m, line, ppos, tok));
294 in_line_eoln(MACRO_PROT_ARGS)
300 if ( ! man_elem_alloc(m, line, ppos, tok))
307 w = man_args(m, line, pos, buf, &p);
314 if ( ! man_word_alloc(m, line, la, p))
318 if (n == m->last && MAN_SCOPED & man_macros[tok].flags) {
319 m->flags |= MAN_ELINE;
324 * Note that when TH is pruned, we'll be back at the root, so
325 * make sure that we don't clobber as its sibling.
328 for ( ; m->last; m->last = m->last->parent) {
331 if (m->last->type == MAN_ROOT)
333 if ( ! man_valid_post(m))
335 if ( ! man_action_post(m))
342 * Same here regarding whether we're back at the root.
345 if (m->last->type != MAN_ROOT && ! man_valid_post(m))
347 if (m->last->type != MAN_ROOT && ! man_action_post(m))
349 if (m->last->type != MAN_ROOT)
350 m->next = MAN_NEXT_SIBLING;
357 man_macroend(struct man *m)
361 n = MAN_VALID & m->last->flags ?
362 m->last->parent : m->last;
364 for ( ; n; n = n->parent) {
365 if (MAN_BLOCK != n->type)
367 if ( ! (MAN_EXPLICIT & man_macros[n->tok].flags))
369 if ( ! man_nwarn(m, n, WEXITSCOPE))
373 return(man_unscope(m, m->first));