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