Annotation of mandoc/man_macro.c, Revision 1.20
1.20 ! kristaps 1: /* $Id: man_macro.c,v 1.19 2009/08/13 11:45:29 kristaps Exp $ */
1.1 kristaps 2: /*
1.15 kristaps 3: * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1 kristaps 4: *
5: * Permission to use, copy, modify, and distribute this software for any
1.14 kristaps 6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 8: *
1.14 kristaps 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.
1.1 kristaps 16: */
17: #include <assert.h>
18: #include <ctype.h>
19: #include <stdlib.h>
20: #include <string.h>
21:
22: #include "libman.h"
23:
1.19 kristaps 24: #define REW_REWIND (0) /* See rew_scope(). */
25: #define REW_NOHALT (1) /* See rew_scope(). */
26: #define REW_HALT (2) /* See rew_scope(). */
27:
28: static int in_line_eoln(MACRO_PROT_ARGS);
29: static int blk_imp(MACRO_PROT_ARGS);
30:
31: static int rew_scope(enum man_type, struct man *, int);
32: static int rew_dohalt(int, enum man_type,
33: const struct man_node *);
34:
35: const struct man_macro __man_macros[MAN_MAX] = {
36: { in_line_eoln, 0 }, /* br */
37: { in_line_eoln, 0 }, /* TH */
38: { blk_imp, 0 }, /* SH */
39: { blk_imp, 0 }, /* SS */
40: { blk_imp, MAN_SCOPED }, /* TP */
41: { blk_imp, 0 }, /* LP */
42: { blk_imp, 0 }, /* PP */
43: { blk_imp, 0 }, /* P */
44: { blk_imp, 0 }, /* IP */
45: { blk_imp, 0 }, /* HP */
46: { in_line_eoln, MAN_SCOPED }, /* SM */
47: { in_line_eoln, MAN_SCOPED }, /* SB */
48: { in_line_eoln, 0 }, /* BI */
49: { in_line_eoln, 0 }, /* IB */
50: { in_line_eoln, 0 }, /* BR */
51: { in_line_eoln, 0 }, /* RB */
52: { in_line_eoln, MAN_SCOPED }, /* R */
53: { in_line_eoln, MAN_SCOPED }, /* B */
54: { in_line_eoln, MAN_SCOPED }, /* I */
55: { in_line_eoln, 0 }, /* IR */
56: { in_line_eoln, 0 }, /* RI */
57: { in_line_eoln, 0 }, /* na */
58: { in_line_eoln, 0 }, /* i */
59: { in_line_eoln, 0 }, /* sp */
60: { in_line_eoln, 0 }, /* nf */
61: { in_line_eoln, 0 }, /* fi */
62: { in_line_eoln, 0 }, /* r */
63: };
1.9 kristaps 64:
1.19 kristaps 65: const struct man_macro * const man_macros = __man_macros;
1.1 kristaps 66:
67:
1.3 kristaps 68: int
1.19 kristaps 69: man_unscope(struct man *m, const struct man_node *n)
1.1 kristaps 70: {
71:
1.19 kristaps 72: assert(n);
73: m->next = MAN_NEXT_SIBLING;
74:
75: /* LINTED */
76: while (m->last != n) {
77: if ( ! man_valid_post(m))
78: return(0);
79: if ( ! man_action_post(m))
80: return(0);
81: m->last = m->last->parent;
82: assert(m->last);
83: }
84:
85: if ( ! man_valid_post(m))
1.1 kristaps 86: return(0);
1.19 kristaps 87: return(man_action_post(m));
88: }
1.1 kristaps 89:
90:
1.19 kristaps 91: /*
92: * There are three scope levels: scoped to the root (all), scoped to the
93: * section (all less sections), and scoped to subsections (all less
94: * sections and subsections).
95: */
96: static int
97: rew_dohalt(int tok, enum man_type type, const struct man_node *n)
98: {
1.1 kristaps 99:
1.19 kristaps 100: if (MAN_ROOT == n->type)
101: return(REW_HALT);
102: assert(n->parent);
103: if (MAN_ROOT == n->parent->type)
104: return(REW_REWIND);
105: if (MAN_VALID & n->flags)
106: return(REW_NOHALT);
107:
108: switch (tok) {
109: case (MAN_SH):
1.20 ! kristaps 110: /* Rewind to ourselves. */
1.19 kristaps 111: if (type == n->type && tok == n->tok)
112: return(REW_REWIND);
113: break;
114: case (MAN_SS):
1.20 ! kristaps 115: /* Rewind to ourselves. */
1.19 kristaps 116: if (type == n->type && tok == n->tok)
117: return(REW_REWIND);
1.20 ! kristaps 118: /* Rewind to a section, if a block. */
! 119: if (MAN_BLOCK == type && MAN_SH == n->parent->tok &&
! 120: MAN_BODY == n->parent->type)
! 121: return(REW_REWIND);
! 122: /* Don't go beyond a section. */
! 123: if (MAN_SH == n->tok)
1.19 kristaps 124: return(REW_HALT);
125: break;
126: default:
1.20 ! kristaps 127: /* Rewind to ourselves. */
1.19 kristaps 128: if (type == n->type && tok == n->tok)
129: return(REW_REWIND);
1.20 ! kristaps 130: /* Rewind to a subsection, if a block. */
! 131: if (MAN_BLOCK == type && MAN_SS == n->parent->tok &&
! 132: MAN_BODY == n->parent->type)
! 133: return(REW_REWIND);
! 134: /* Don't go beyond a subsection. */
! 135: if (MAN_SS == n->tok)
1.19 kristaps 136: return(REW_HALT);
1.20 ! kristaps 137: /* Rewind to a section, if a block. */
! 138: if (MAN_BLOCK == type && MAN_SH == n->parent->tok &&
! 139: MAN_BODY == n->parent->type)
! 140: return(REW_REWIND);
! 141: /* Don't go beyond a section. */
! 142: if (MAN_SH == n->tok)
1.19 kristaps 143: return(REW_HALT);
144: break;
1.3 kristaps 145: }
1.1 kristaps 146:
1.19 kristaps 147: return(REW_NOHALT);
148: }
1.9 kristaps 149:
150:
1.19 kristaps 151: /*
152: * Rewinding entails ascending the parse tree until a coherent point,
153: * for example, the `SH' macro will close out any intervening `SS'
154: * scopes. When a scope is closed, it must be validated and actioned.
155: */
156: static int
157: rew_scope(enum man_type type, struct man *m, int tok)
158: {
159: struct man_node *n;
160: int c;
1.7 kristaps 161:
1.19 kristaps 162: /* LINTED */
163: for (n = m->last; n; n = n->parent) {
164: /*
165: * Whether we should stop immediately (REW_HALT), stop
166: * and rewind until this point (REW_REWIND), or keep
167: * rewinding (REW_NOHALT).
168: */
169: c = rew_dohalt(tok, type, n);
170: if (REW_HALT == c)
171: return(1);
172: if (REW_REWIND == c)
1.7 kristaps 173: break;
1.6 kristaps 174: }
1.1 kristaps 175:
1.19 kristaps 176: /* Rewind until the current point. */
177:
178: assert(n);
179: return(man_unscope(m, n));
180: }
181:
1.6 kristaps 182:
1.19 kristaps 183: /*
184: * Parse an implicit-block macro. These contain a MAN_HEAD and a
185: * MAN_BODY contained within a MAN_BLOCK. Rules for closing out other
186: * scopes, such as `SH' closing out an `SS', are defined in the rew
187: * routines.
188: */
189: int
190: blk_imp(MACRO_PROT_ARGS)
191: {
192: int w, la;
193: char *p;
194:
195: /* Close out prior scopes. */
1.7 kristaps 196:
1.19 kristaps 197: if ( ! rew_scope(MAN_BODY, m, tok))
1.5 kristaps 198: return(0);
1.19 kristaps 199: if ( ! rew_scope(MAN_BLOCK, m, tok))
1.6 kristaps 200: return(0);
1.1 kristaps 201:
1.19 kristaps 202: /* Allocate new block & head scope. */
203:
204: if ( ! man_block_alloc(m, line, ppos, tok))
205: return(0);
206: if ( ! man_head_alloc(m, line, ppos, tok))
207: return(0);
1.1 kristaps 208:
1.19 kristaps 209: /* Add line arguments. */
1.3 kristaps 210:
1.19 kristaps 211: for (;;) {
212: la = *pos;
213: w = man_args(m, line, pos, buf, &p);
1.4 kristaps 214:
1.19 kristaps 215: if (-1 == w)
1.6 kristaps 216: return(0);
1.19 kristaps 217: if (0 == w)
218: break;
219:
220: if ( ! man_word_alloc(m, line, la, p))
1.6 kristaps 221: return(0);
1.19 kristaps 222: m->next = MAN_NEXT_SIBLING;
1.6 kristaps 223: }
224:
1.19 kristaps 225: /* Close out head and open body (unless MAN_SCOPE). */
226:
227: if (MAN_SCOPED & man_macros[tok].flags) {
228: m->flags |= MAN_BLINE;
229: return(1);
230: } else if ( ! rew_scope(MAN_HEAD, m, tok))
1.6 kristaps 231: return(0);
232:
1.19 kristaps 233: return(man_body_alloc(m, line, ppos, tok));
1.4 kristaps 234: }
235:
236:
1.19 kristaps 237: int
238: in_line_eoln(MACRO_PROT_ARGS)
1.3 kristaps 239: {
1.19 kristaps 240: int w, la;
241: char *p;
242: struct man_node *n;
1.3 kristaps 243:
1.19 kristaps 244: if ( ! man_elem_alloc(m, line, ppos, tok))
1.3 kristaps 245: return(0);
246:
1.19 kristaps 247: n = m->last;
248: m->next = MAN_NEXT_CHILD;
1.3 kristaps 249:
1.19 kristaps 250: for (;;) {
251: la = *pos;
252: w = man_args(m, line, pos, buf, &p);
1.3 kristaps 253:
1.19 kristaps 254: if (-1 == w)
255: return(0);
256: if (0 == w)
257: break;
1.3 kristaps 258:
1.19 kristaps 259: if ( ! man_word_alloc(m, line, la, p))
260: return(0);
261: m->next = MAN_NEXT_SIBLING;
262: }
1.3 kristaps 263:
1.19 kristaps 264: if (n == m->last && (MAN_SCOPED & man_macros[tok].flags)) {
265: m->flags |= MAN_ELINE;
266: return(1);
267: }
1.3 kristaps 268:
1.19 kristaps 269: /*
270: * Note that when TH is pruned, we'll be back at the root, so
271: * make sure that we don't clobber as its sibling.
272: */
1.3 kristaps 273:
1.19 kristaps 274: /* FIXME: clean this to use man_unscope(). */
1.3 kristaps 275:
1.19 kristaps 276: for ( ; m->last; m->last = m->last->parent) {
277: if (m->last == n)
278: break;
279: if (m->last->type == MAN_ROOT)
280: break;
281: if ( ! man_valid_post(m))
282: return(0);
283: if ( ! man_action_post(m))
284: return(0);
285: }
1.3 kristaps 286:
1.19 kristaps 287: assert(m->last);
1.3 kristaps 288:
289: /*
1.19 kristaps 290: * Same here regarding whether we're back at the root.
1.3 kristaps 291: */
292:
1.19 kristaps 293: if (m->last->type != MAN_ROOT && ! man_valid_post(m))
294: return(0);
295: if (m->last->type != MAN_ROOT && ! man_action_post(m))
296: return(0);
297: if (m->last->type != MAN_ROOT)
298: m->next = MAN_NEXT_SIBLING;
1.3 kristaps 299:
1.19 kristaps 300: return(1);
301: }
1.3 kristaps 302:
303:
1.19 kristaps 304: int
305: man_macroend(struct man *m)
306: {
1.3 kristaps 307:
1.19 kristaps 308: return(man_unscope(m, m->first));
309: }
1.3 kristaps 310:
CVSweb