Annotation of mandoc/man_macro.c, Revision 1.33
1.33 ! kristaps 1: /* $Id: man_macro.c,v 1.32 2010/03/23 11:30:48 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: */
1.30 kristaps 17: #ifdef HAVE_CONFIG_H
18: #include "config.h"
19: #endif
20:
1.1 kristaps 21: #include <assert.h>
22: #include <ctype.h>
23: #include <stdlib.h>
24: #include <string.h>
25:
26: #include "libman.h"
27:
1.19 kristaps 28: #define REW_REWIND (0) /* See rew_scope(). */
29: #define REW_NOHALT (1) /* See rew_scope(). */
30: #define REW_HALT (2) /* See rew_scope(). */
31:
32: static int in_line_eoln(MACRO_PROT_ARGS);
33: static int blk_imp(MACRO_PROT_ARGS);
1.21 kristaps 34: static int blk_close(MACRO_PROT_ARGS);
1.19 kristaps 35:
1.33 ! kristaps 36: static int rew_scope(enum man_type,
! 37: struct man *, enum mant);
! 38: static int rew_dohalt(enum mant, enum man_type,
1.19 kristaps 39: const struct man_node *);
1.33 ! kristaps 40: static int rew_block(enum mant, enum man_type,
1.21 kristaps 41: const struct man_node *);
1.19 kristaps 42:
43: const struct man_macro __man_macros[MAN_MAX] = {
1.31 kristaps 44: { in_line_eoln, MAN_NSCOPED }, /* br */
1.19 kristaps 45: { in_line_eoln, 0 }, /* TH */
1.24 kristaps 46: { blk_imp, MAN_SCOPED }, /* SH */
47: { blk_imp, MAN_SCOPED }, /* SS */
1.27 kristaps 48: { blk_imp, MAN_SCOPED | MAN_FSCOPED }, /* TP */
1.19 kristaps 49: { blk_imp, 0 }, /* LP */
50: { blk_imp, 0 }, /* PP */
51: { blk_imp, 0 }, /* P */
52: { blk_imp, 0 }, /* IP */
53: { blk_imp, 0 }, /* HP */
54: { in_line_eoln, MAN_SCOPED }, /* SM */
55: { in_line_eoln, MAN_SCOPED }, /* SB */
56: { in_line_eoln, 0 }, /* BI */
57: { in_line_eoln, 0 }, /* IB */
58: { in_line_eoln, 0 }, /* BR */
59: { in_line_eoln, 0 }, /* RB */
60: { in_line_eoln, MAN_SCOPED }, /* R */
61: { in_line_eoln, MAN_SCOPED }, /* B */
62: { in_line_eoln, MAN_SCOPED }, /* I */
63: { in_line_eoln, 0 }, /* IR */
64: { in_line_eoln, 0 }, /* RI */
1.31 kristaps 65: { in_line_eoln, MAN_NSCOPED }, /* na */
1.19 kristaps 66: { in_line_eoln, 0 }, /* i */
1.31 kristaps 67: { in_line_eoln, MAN_NSCOPED }, /* sp */
1.19 kristaps 68: { in_line_eoln, 0 }, /* nf */
69: { in_line_eoln, 0 }, /* fi */
70: { in_line_eoln, 0 }, /* r */
1.21 kristaps 71: { blk_close, 0 }, /* RE */
1.22 kristaps 72: { blk_imp, MAN_EXPLICIT }, /* RS */
1.23 kristaps 73: { in_line_eoln, 0 }, /* DT */
1.28 kristaps 74: { in_line_eoln, 0 }, /* UC */
1.29 kristaps 75: { in_line_eoln, 0 }, /* PD */
1.32 kristaps 76: { in_line_eoln, MAN_NSCOPED }, /* Sp */
77: { in_line_eoln, 0 }, /* Vb */
78: { in_line_eoln, 0 }, /* Ve */
1.19 kristaps 79: };
1.9 kristaps 80:
1.19 kristaps 81: const struct man_macro * const man_macros = __man_macros;
1.1 kristaps 82:
83:
1.3 kristaps 84: int
1.19 kristaps 85: man_unscope(struct man *m, const struct man_node *n)
1.1 kristaps 86: {
87:
1.19 kristaps 88: assert(n);
89: m->next = MAN_NEXT_SIBLING;
90:
91: /* LINTED */
92: while (m->last != n) {
93: if ( ! man_valid_post(m))
94: return(0);
95: if ( ! man_action_post(m))
96: return(0);
97: m->last = m->last->parent;
98: assert(m->last);
99: }
100:
101: if ( ! man_valid_post(m))
1.1 kristaps 102: return(0);
1.19 kristaps 103: return(man_action_post(m));
104: }
1.1 kristaps 105:
106:
1.21 kristaps 107: static int
1.33 ! kristaps 108: rew_block(enum mant ntok, enum man_type type, const struct man_node *n)
1.21 kristaps 109: {
110:
111: if (MAN_BLOCK == type && ntok == n->parent->tok &&
112: MAN_BODY == n->parent->type)
113: return(REW_REWIND);
114: return(ntok == n->tok ? REW_HALT : REW_NOHALT);
115: }
116:
117:
1.19 kristaps 118: /*
119: * There are three scope levels: scoped to the root (all), scoped to the
120: * section (all less sections), and scoped to subsections (all less
121: * sections and subsections).
122: */
123: static int
1.33 ! kristaps 124: rew_dohalt(enum mant tok, enum man_type type, const struct man_node *n)
1.19 kristaps 125: {
1.21 kristaps 126: int c;
1.1 kristaps 127:
1.19 kristaps 128: if (MAN_ROOT == n->type)
129: return(REW_HALT);
130: assert(n->parent);
131: if (MAN_ROOT == n->parent->type)
132: return(REW_REWIND);
133: if (MAN_VALID & n->flags)
134: return(REW_NOHALT);
135:
1.21 kristaps 136: /* Rewind to ourselves, first. */
137: if (type == n->type && tok == n->tok)
138: return(REW_REWIND);
139:
1.19 kristaps 140: switch (tok) {
141: case (MAN_SH):
142: break;
143: case (MAN_SS):
1.20 kristaps 144: /* Rewind to a section, if a block. */
1.21 kristaps 145: if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
146: return(c);
147: break;
148: case (MAN_RS):
149: /* Rewind to a subsection, if a block. */
150: if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
151: return(c);
152: /* Rewind to a section, if a block. */
153: if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
154: return(c);
1.19 kristaps 155: break;
156: default:
1.21 kristaps 157: /* Rewind to an offsetter, if a block. */
158: if (REW_NOHALT != (c = rew_block(MAN_RS, type, n)))
159: return(c);
1.20 kristaps 160: /* Rewind to a subsection, if a block. */
1.21 kristaps 161: if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
162: return(c);
1.20 kristaps 163: /* Rewind to a section, if a block. */
1.21 kristaps 164: if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
165: return(c);
1.19 kristaps 166: break;
1.3 kristaps 167: }
1.1 kristaps 168:
1.19 kristaps 169: return(REW_NOHALT);
170: }
1.9 kristaps 171:
172:
1.19 kristaps 173: /*
174: * Rewinding entails ascending the parse tree until a coherent point,
175: * for example, the `SH' macro will close out any intervening `SS'
176: * scopes. When a scope is closed, it must be validated and actioned.
177: */
178: static int
1.33 ! kristaps 179: rew_scope(enum man_type type, struct man *m, enum mant tok)
1.19 kristaps 180: {
181: struct man_node *n;
182: int c;
1.7 kristaps 183:
1.19 kristaps 184: /* LINTED */
185: for (n = m->last; n; n = n->parent) {
186: /*
187: * Whether we should stop immediately (REW_HALT), stop
188: * and rewind until this point (REW_REWIND), or keep
189: * rewinding (REW_NOHALT).
190: */
191: c = rew_dohalt(tok, type, n);
192: if (REW_HALT == c)
193: return(1);
194: if (REW_REWIND == c)
1.7 kristaps 195: break;
1.6 kristaps 196: }
1.1 kristaps 197:
1.19 kristaps 198: /* Rewind until the current point. */
199:
200: assert(n);
201: return(man_unscope(m, n));
202: }
203:
1.6 kristaps 204:
1.21 kristaps 205: /* ARGSUSED */
206: int
207: blk_close(MACRO_PROT_ARGS)
208: {
1.33 ! kristaps 209: enum mant ntok;
1.21 kristaps 210: const struct man_node *nn;
211:
212: switch (tok) {
213: case (MAN_RE):
214: ntok = MAN_RS;
215: break;
216: default:
217: abort();
218: /* NOTREACHED */
219: }
220:
221: for (nn = m->last->parent; nn; nn = nn->parent)
222: if (ntok == nn->tok)
223: break;
224:
225: if (NULL == nn)
226: if ( ! man_pwarn(m, line, ppos, WNOSCOPE))
227: return(0);
228:
229: if ( ! rew_scope(MAN_BODY, m, ntok))
230: return(0);
231: if ( ! rew_scope(MAN_BLOCK, m, ntok))
232: return(0);
233: m->next = MAN_NEXT_SIBLING;
234: return(1);
235: }
236:
237:
1.19 kristaps 238: /*
239: * Parse an implicit-block macro. These contain a MAN_HEAD and a
240: * MAN_BODY contained within a MAN_BLOCK. Rules for closing out other
241: * scopes, such as `SH' closing out an `SS', are defined in the rew
242: * routines.
243: */
244: int
245: blk_imp(MACRO_PROT_ARGS)
246: {
247: int w, la;
248: char *p;
1.25 kristaps 249: struct man_node *n;
1.19 kristaps 250:
251: /* Close out prior scopes. */
1.7 kristaps 252:
1.19 kristaps 253: if ( ! rew_scope(MAN_BODY, m, tok))
1.5 kristaps 254: return(0);
1.19 kristaps 255: if ( ! rew_scope(MAN_BLOCK, m, tok))
1.6 kristaps 256: return(0);
1.1 kristaps 257:
1.19 kristaps 258: /* Allocate new block & head scope. */
259:
260: if ( ! man_block_alloc(m, line, ppos, tok))
261: return(0);
262: if ( ! man_head_alloc(m, line, ppos, tok))
263: return(0);
1.1 kristaps 264:
1.25 kristaps 265: n = m->last;
266:
1.19 kristaps 267: /* Add line arguments. */
1.3 kristaps 268:
1.19 kristaps 269: for (;;) {
270: la = *pos;
271: w = man_args(m, line, pos, buf, &p);
1.4 kristaps 272:
1.19 kristaps 273: if (-1 == w)
1.6 kristaps 274: return(0);
1.19 kristaps 275: if (0 == w)
276: break;
277:
278: if ( ! man_word_alloc(m, line, la, p))
1.6 kristaps 279: return(0);
280: }
281:
1.19 kristaps 282: /* Close out head and open body (unless MAN_SCOPE). */
283:
1.27 kristaps 284: if (MAN_SCOPED & man_macros[tok].flags) {
285: /* If we're forcing scope (`TP'), keep it open. */
286: if (MAN_FSCOPED & man_macros[tok].flags) {
287: m->flags |= MAN_BLINE;
288: return(1);
289: } else if (n == m->last) {
290: m->flags |= MAN_BLINE;
291: return(1);
292: }
293: }
294:
295: if ( ! rew_scope(MAN_HEAD, m, tok))
1.6 kristaps 296: return(0);
297:
1.19 kristaps 298: return(man_body_alloc(m, line, ppos, tok));
1.4 kristaps 299: }
300:
301:
1.19 kristaps 302: int
303: in_line_eoln(MACRO_PROT_ARGS)
1.3 kristaps 304: {
1.19 kristaps 305: int w, la;
306: char *p;
307: struct man_node *n;
1.3 kristaps 308:
1.19 kristaps 309: if ( ! man_elem_alloc(m, line, ppos, tok))
1.3 kristaps 310: return(0);
311:
1.19 kristaps 312: n = m->last;
1.3 kristaps 313:
1.19 kristaps 314: for (;;) {
315: la = *pos;
316: w = man_args(m, line, pos, buf, &p);
1.3 kristaps 317:
1.19 kristaps 318: if (-1 == w)
319: return(0);
320: if (0 == w)
321: break;
1.32 kristaps 322:
323: /* XXX ignore Vb arguments for now */
324: if (MAN_Vb == tok)
325: continue;
1.3 kristaps 326:
1.19 kristaps 327: if ( ! man_word_alloc(m, line, la, p))
328: return(0);
329: }
1.3 kristaps 330:
1.31 kristaps 331: /*
332: * If no arguments are specified and this is MAN_SCOPED (i.e.,
333: * next-line scoped), then set our mode to indicate that we're
334: * waiting for terms to load into our context.
335: */
336:
1.25 kristaps 337: if (n == m->last && MAN_SCOPED & man_macros[tok].flags) {
1.31 kristaps 338: assert( ! (MAN_NSCOPED & man_macros[tok].flags));
1.19 kristaps 339: m->flags |= MAN_ELINE;
340: return(1);
341: }
1.3 kristaps 342:
1.31 kristaps 343: /* Set ignorable context, if applicable. */
344:
345: if (MAN_NSCOPED & man_macros[tok].flags) {
346: assert( ! (MAN_SCOPED & man_macros[tok].flags));
347: m->flags |= MAN_ILINE;
348: }
349:
1.19 kristaps 350: /*
1.31 kristaps 351: * Rewind our element scope. Note that when TH is pruned, we'll
352: * be back at the root, so make sure that we don't clobber as
353: * its sibling.
1.19 kristaps 354: */
1.3 kristaps 355:
1.19 kristaps 356: for ( ; m->last; m->last = m->last->parent) {
357: if (m->last == n)
358: break;
359: if (m->last->type == MAN_ROOT)
360: break;
361: if ( ! man_valid_post(m))
362: return(0);
363: if ( ! man_action_post(m))
364: return(0);
365: }
1.3 kristaps 366:
1.19 kristaps 367: assert(m->last);
1.3 kristaps 368:
369: /*
1.19 kristaps 370: * Same here regarding whether we're back at the root.
1.3 kristaps 371: */
372:
1.19 kristaps 373: if (m->last->type != MAN_ROOT && ! man_valid_post(m))
374: return(0);
375: if (m->last->type != MAN_ROOT && ! man_action_post(m))
376: return(0);
377: if (m->last->type != MAN_ROOT)
378: m->next = MAN_NEXT_SIBLING;
1.3 kristaps 379:
1.19 kristaps 380: return(1);
381: }
1.3 kristaps 382:
383:
1.19 kristaps 384: int
385: man_macroend(struct man *m)
386: {
1.22 kristaps 387: struct man_node *n;
388:
389: n = MAN_VALID & m->last->flags ?
390: m->last->parent : m->last;
391:
392: for ( ; n; n = n->parent) {
393: if (MAN_BLOCK != n->type)
394: continue;
395: if ( ! (MAN_EXPLICIT & man_macros[n->tok].flags))
396: continue;
1.26 kristaps 397: if ( ! man_nwarn(m, n, WEXITSCOPE))
398: return(0);
1.22 kristaps 399: }
1.3 kristaps 400:
1.19 kristaps 401: return(man_unscope(m, m->first));
402: }
1.3 kristaps 403:
CVSweb