Annotation of mandoc/man_macro.c, Revision 1.39
1.39 ! kristaps 1: /* $Id: man_macro.c,v 1.38 2010/03/27 10:04:56 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:
1.35 kristaps 34: static int blk_close(MACRO_PROT_ARGS);
35: static int blk_dotted(MACRO_PROT_ARGS);
36: static int blk_exp(MACRO_PROT_ARGS);
37: static int blk_imp(MACRO_PROT_ARGS);
1.19 kristaps 38: static int in_line_eoln(MACRO_PROT_ARGS);
39:
1.33 kristaps 40: static int rew_scope(enum man_type,
41: struct man *, enum mant);
1.34 kristaps 42: static enum rew rew_dohalt(enum mant, enum man_type,
1.19 kristaps 43: const struct man_node *);
1.34 kristaps 44: static enum rew rew_block(enum mant, enum man_type,
1.21 kristaps 45: const struct man_node *);
1.38 kristaps 46: static int rew_warn(struct man *,
47: struct man_node *, enum merr);
1.19 kristaps 48:
49: const struct man_macro __man_macros[MAN_MAX] = {
1.31 kristaps 50: { in_line_eoln, MAN_NSCOPED }, /* br */
1.19 kristaps 51: { in_line_eoln, 0 }, /* TH */
1.24 kristaps 52: { blk_imp, MAN_SCOPED }, /* SH */
53: { blk_imp, MAN_SCOPED }, /* SS */
1.27 kristaps 54: { blk_imp, MAN_SCOPED | MAN_FSCOPED }, /* TP */
1.19 kristaps 55: { blk_imp, 0 }, /* LP */
56: { blk_imp, 0 }, /* PP */
57: { blk_imp, 0 }, /* P */
58: { blk_imp, 0 }, /* IP */
59: { blk_imp, 0 }, /* HP */
60: { in_line_eoln, MAN_SCOPED }, /* SM */
61: { in_line_eoln, MAN_SCOPED }, /* SB */
62: { in_line_eoln, 0 }, /* BI */
63: { in_line_eoln, 0 }, /* IB */
64: { in_line_eoln, 0 }, /* BR */
65: { in_line_eoln, 0 }, /* RB */
66: { in_line_eoln, MAN_SCOPED }, /* R */
67: { in_line_eoln, MAN_SCOPED }, /* B */
68: { in_line_eoln, MAN_SCOPED }, /* I */
69: { in_line_eoln, 0 }, /* IR */
70: { in_line_eoln, 0 }, /* RI */
1.31 kristaps 71: { in_line_eoln, MAN_NSCOPED }, /* na */
1.19 kristaps 72: { in_line_eoln, 0 }, /* i */
1.31 kristaps 73: { in_line_eoln, MAN_NSCOPED }, /* sp */
1.19 kristaps 74: { in_line_eoln, 0 }, /* nf */
75: { in_line_eoln, 0 }, /* fi */
76: { in_line_eoln, 0 }, /* r */
1.21 kristaps 77: { blk_close, 0 }, /* RE */
1.35 kristaps 78: { blk_exp, MAN_EXPLICIT }, /* RS */
1.23 kristaps 79: { in_line_eoln, 0 }, /* DT */
1.28 kristaps 80: { in_line_eoln, 0 }, /* UC */
1.29 kristaps 81: { in_line_eoln, 0 }, /* PD */
1.32 kristaps 82: { in_line_eoln, MAN_NSCOPED }, /* Sp */
83: { in_line_eoln, 0 }, /* Vb */
84: { in_line_eoln, 0 }, /* Ve */
1.35 kristaps 85: { blk_exp, MAN_EXPLICIT | MAN_NOCLOSE}, /* de */
86: { blk_exp, MAN_EXPLICIT | MAN_NOCLOSE}, /* dei */
87: { blk_exp, MAN_EXPLICIT | MAN_NOCLOSE}, /* am */
88: { blk_exp, MAN_EXPLICIT | MAN_NOCLOSE}, /* ami */
89: { blk_exp, MAN_EXPLICIT | MAN_NOCLOSE}, /* ig */
90: { blk_dotted, 0 }, /* . */
1.19 kristaps 91: };
1.9 kristaps 92:
1.19 kristaps 93: const struct man_macro * const man_macros = __man_macros;
1.1 kristaps 94:
95:
1.38 kristaps 96: /*
97: * Warn when "n" is an explicit non-roff macro.
98: */
99: static int
100: rew_warn(struct man *m, struct man_node *n, enum merr er)
101: {
102:
103: if (er == WERRMAX || MAN_BLOCK != n->type)
104: return(1);
105: if (MAN_VALID & n->flags)
106: return(1);
107: if ( ! (MAN_EXPLICIT & man_macros[n->tok].flags))
108: return(1);
109: if (MAN_NOCLOSE & man_macros[n->tok].flags)
110: return(1);
111: return(man_nwarn(m, n, er));
112: }
113:
114:
115: /*
116: * Rewind scope. If a code "er" != WERRMAX has been provided, it will
117: * be used if an explicit block scope is being closed out.
118: */
1.3 kristaps 119: int
1.38 kristaps 120: man_unscope(struct man *m, const struct man_node *n, enum merr er)
1.1 kristaps 121: {
122:
1.19 kristaps 123: assert(n);
124:
125: /* LINTED */
126: while (m->last != n) {
1.38 kristaps 127: if ( ! rew_warn(m, m->last, er))
128: return(0);
1.19 kristaps 129: if ( ! man_valid_post(m))
130: return(0);
131: if ( ! man_action_post(m))
132: return(0);
133: m->last = m->last->parent;
134: assert(m->last);
135: }
136:
1.38 kristaps 137: if ( ! rew_warn(m, m->last, er))
138: return(0);
1.19 kristaps 139: if ( ! man_valid_post(m))
1.1 kristaps 140: return(0);
1.35 kristaps 141: if ( ! man_action_post(m))
142: return(0);
143:
144: m->next = MAN_ROOT == m->last->type ?
145: MAN_NEXT_CHILD : MAN_NEXT_SIBLING;
146:
147: return(1);
1.19 kristaps 148: }
1.1 kristaps 149:
150:
1.34 kristaps 151: static enum rew
1.33 kristaps 152: rew_block(enum mant ntok, enum man_type type, const struct man_node *n)
1.21 kristaps 153: {
154:
155: if (MAN_BLOCK == type && ntok == n->parent->tok &&
156: MAN_BODY == n->parent->type)
157: return(REW_REWIND);
158: return(ntok == n->tok ? REW_HALT : REW_NOHALT);
159: }
160:
161:
1.19 kristaps 162: /*
163: * There are three scope levels: scoped to the root (all), scoped to the
164: * section (all less sections), and scoped to subsections (all less
165: * sections and subsections).
166: */
1.34 kristaps 167: static enum rew
1.33 kristaps 168: rew_dohalt(enum mant tok, enum man_type type, const struct man_node *n)
1.19 kristaps 169: {
1.34 kristaps 170: enum rew c;
1.1 kristaps 171:
1.38 kristaps 172: /* We cannot progress beyond the root ever. */
1.19 kristaps 173: if (MAN_ROOT == n->type)
174: return(REW_HALT);
1.38 kristaps 175:
1.19 kristaps 176: assert(n->parent);
1.38 kristaps 177:
178: /* Normal nodes shouldn't go to the level of the root. */
1.19 kristaps 179: if (MAN_ROOT == n->parent->type)
180: return(REW_REWIND);
1.38 kristaps 181:
182: /* Already-validated nodes should be closed out. */
1.19 kristaps 183: if (MAN_VALID & n->flags)
184: return(REW_NOHALT);
185:
1.38 kristaps 186: /* First: rewind to ourselves. */
1.21 kristaps 187: if (type == n->type && tok == n->tok)
188: return(REW_REWIND);
189:
1.38 kristaps 190: /*
191: * If we're a roff macro, then we can close out anything that
192: * stands between us and our parent context.
193: */
194: if (MAN_NOCLOSE & man_macros[tok].flags)
195: return(REW_NOHALT);
196:
197: /*
198: * Don't clobber roff macros: this is a bit complicated. If the
199: * current macro is a roff macro, halt immediately and don't
200: * rewind. If it's not, and the parent is, then close out the
201: * current scope and halt at the parent.
202: */
203: if (MAN_NOCLOSE & man_macros[n->tok].flags)
204: return(REW_HALT);
205: if (MAN_NOCLOSE & man_macros[n->parent->tok].flags)
206: return(REW_REWIND);
207:
208: /*
209: * Next follow the implicit scope-smashings as defined by man.7:
210: * section, sub-section, etc.
211: */
212:
1.19 kristaps 213: switch (tok) {
214: case (MAN_SH):
215: break;
216: case (MAN_SS):
1.20 kristaps 217: /* Rewind to a section, if a block. */
1.21 kristaps 218: if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
219: return(c);
220: break;
221: case (MAN_RS):
222: /* Rewind to a subsection, if a block. */
223: if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
224: return(c);
225: /* Rewind to a section, if a block. */
226: if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
227: return(c);
1.19 kristaps 228: break;
229: default:
1.21 kristaps 230: /* Rewind to an offsetter, if a block. */
231: if (REW_NOHALT != (c = rew_block(MAN_RS, type, n)))
232: return(c);
1.20 kristaps 233: /* Rewind to a subsection, if a block. */
1.21 kristaps 234: if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
235: return(c);
1.20 kristaps 236: /* Rewind to a section, if a block. */
1.21 kristaps 237: if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
238: return(c);
1.19 kristaps 239: break;
1.3 kristaps 240: }
1.1 kristaps 241:
1.19 kristaps 242: return(REW_NOHALT);
243: }
1.9 kristaps 244:
245:
1.19 kristaps 246: /*
247: * Rewinding entails ascending the parse tree until a coherent point,
248: * for example, the `SH' macro will close out any intervening `SS'
249: * scopes. When a scope is closed, it must be validated and actioned.
250: */
251: static int
1.33 kristaps 252: rew_scope(enum man_type type, struct man *m, enum mant tok)
1.19 kristaps 253: {
254: struct man_node *n;
1.34 kristaps 255: enum rew c;
1.7 kristaps 256:
1.19 kristaps 257: /* LINTED */
258: for (n = m->last; n; n = n->parent) {
259: /*
260: * Whether we should stop immediately (REW_HALT), stop
261: * and rewind until this point (REW_REWIND), or keep
262: * rewinding (REW_NOHALT).
263: */
264: c = rew_dohalt(tok, type, n);
265: if (REW_HALT == c)
266: return(1);
267: if (REW_REWIND == c)
1.7 kristaps 268: break;
1.6 kristaps 269: }
1.1 kristaps 270:
1.38 kristaps 271: /*
272: * Rewind until the current point. Warn if we're a roff
273: * instruction that's mowing over explicit scopes.
274: */
275: assert(n);
276: if (MAN_NOCLOSE & man_macros[tok].flags)
277: return(man_unscope(m, n, WROFFSCOPE));
1.19 kristaps 278:
1.38 kristaps 279: return(man_unscope(m, n, WERRMAX));
1.19 kristaps 280: }
281:
1.6 kristaps 282:
1.36 kristaps 283: /*
284: * Closure for dotted macros (de, dei, am, ami, ign). This must handle
285: * any of these as the parent node, so it needs special handling.
286: * Beyond this, it's the same as blk_close().
287: */
1.37 kristaps 288: /* ARGSUSED */
1.21 kristaps 289: int
1.35 kristaps 290: blk_dotted(MACRO_PROT_ARGS)
291: {
292: enum mant ntok;
293: struct man_node *nn;
294:
1.38 kristaps 295: /* Check for any of the following parents... */
296:
1.35 kristaps 297: for (nn = m->last->parent; nn; nn = nn->parent)
298: if (nn->tok == MAN_de || nn->tok == MAN_dei ||
299: nn->tok == MAN_am ||
300: nn->tok == MAN_ami ||
301: nn->tok == MAN_ig) {
302: ntok = nn->tok;
303: break;
304: }
305:
306: if (NULL == nn) {
307: if ( ! man_pwarn(m, line, ppos, WNOSCOPE))
308: return(0);
309: return(1);
310: }
311:
312: if ( ! rew_scope(MAN_BODY, m, ntok))
313: return(0);
314: if ( ! rew_scope(MAN_BLOCK, m, ntok))
315: return(0);
316:
1.38 kristaps 317: /*
318: * XXX: manually adjust our next-line status. roff macros are,
319: * for the moment, ignored, so we don't want to close out bodies
320: * and so on.
321: */
322:
323: switch (m->last->type) {
324: case (MAN_BODY):
325: m->next = MAN_NEXT_CHILD;
326: break;
327: default:
328: break;
329: }
330:
1.35 kristaps 331: return(1);
332: }
333:
334:
1.36 kristaps 335: /*
336: * Close out a generic explicit macro.
337: */
1.37 kristaps 338: /* ARGSUSED */
1.35 kristaps 339: int
1.21 kristaps 340: blk_close(MACRO_PROT_ARGS)
341: {
1.33 kristaps 342: enum mant ntok;
1.21 kristaps 343: const struct man_node *nn;
344:
345: switch (tok) {
346: case (MAN_RE):
347: ntok = MAN_RS;
348: break;
349: default:
350: abort();
351: /* NOTREACHED */
352: }
353:
354: for (nn = m->last->parent; nn; nn = nn->parent)
355: if (ntok == nn->tok)
356: break;
357:
358: if (NULL == nn)
359: if ( ! man_pwarn(m, line, ppos, WNOSCOPE))
360: return(0);
361:
362: if ( ! rew_scope(MAN_BODY, m, ntok))
363: return(0);
364: if ( ! rew_scope(MAN_BLOCK, m, ntok))
365: return(0);
1.35 kristaps 366:
1.21 kristaps 367: return(1);
368: }
369:
370:
1.35 kristaps 371: int
372: blk_exp(MACRO_PROT_ARGS)
373: {
374: int w, la;
375: char *p;
376:
377: /*
378: * Close out prior scopes. "Regular" explicit macros cannot be
379: * nested, but we allow roff macros to be placed just about
380: * anywhere.
381: */
382:
383: if ( ! (MAN_NOCLOSE & man_macros[tok].flags)) {
384: if ( ! rew_scope(MAN_BODY, m, tok))
385: return(0);
386: if ( ! rew_scope(MAN_BLOCK, m, tok))
387: return(0);
388: }
389:
390: if ( ! man_block_alloc(m, line, ppos, tok))
391: return(0);
392: if ( ! man_head_alloc(m, line, ppos, tok))
393: return(0);
394:
395: for (;;) {
396: la = *pos;
397: w = man_args(m, line, pos, buf, &p);
398:
399: if (-1 == w)
400: return(0);
401: if (0 == w)
402: break;
403:
404: if ( ! man_word_alloc(m, line, la, p))
405: return(0);
406: }
407:
408: assert(m);
409: assert(tok != MAN_MAX);
410:
411: if ( ! rew_scope(MAN_HEAD, m, tok))
412: return(0);
413: return(man_body_alloc(m, line, ppos, tok));
414: }
415:
416:
417:
1.19 kristaps 418: /*
419: * Parse an implicit-block macro. These contain a MAN_HEAD and a
420: * MAN_BODY contained within a MAN_BLOCK. Rules for closing out other
421: * scopes, such as `SH' closing out an `SS', are defined in the rew
422: * routines.
423: */
424: int
425: blk_imp(MACRO_PROT_ARGS)
426: {
427: int w, la;
428: char *p;
1.25 kristaps 429: struct man_node *n;
1.19 kristaps 430:
431: /* Close out prior scopes. */
1.7 kristaps 432:
1.19 kristaps 433: if ( ! rew_scope(MAN_BODY, m, tok))
1.5 kristaps 434: return(0);
1.19 kristaps 435: if ( ! rew_scope(MAN_BLOCK, m, tok))
1.6 kristaps 436: return(0);
1.1 kristaps 437:
1.19 kristaps 438: /* Allocate new block & head scope. */
439:
440: if ( ! man_block_alloc(m, line, ppos, tok))
441: return(0);
442: if ( ! man_head_alloc(m, line, ppos, tok))
443: return(0);
1.1 kristaps 444:
1.25 kristaps 445: n = m->last;
446:
1.19 kristaps 447: /* Add line arguments. */
1.3 kristaps 448:
1.19 kristaps 449: for (;;) {
450: la = *pos;
451: w = man_args(m, line, pos, buf, &p);
1.4 kristaps 452:
1.19 kristaps 453: if (-1 == w)
1.6 kristaps 454: return(0);
1.19 kristaps 455: if (0 == w)
456: break;
457:
458: if ( ! man_word_alloc(m, line, la, p))
1.6 kristaps 459: return(0);
460: }
461:
1.19 kristaps 462: /* Close out head and open body (unless MAN_SCOPE). */
463:
1.27 kristaps 464: if (MAN_SCOPED & man_macros[tok].flags) {
465: /* If we're forcing scope (`TP'), keep it open. */
466: if (MAN_FSCOPED & man_macros[tok].flags) {
467: m->flags |= MAN_BLINE;
468: return(1);
469: } else if (n == m->last) {
470: m->flags |= MAN_BLINE;
471: return(1);
472: }
473: }
474:
475: if ( ! rew_scope(MAN_HEAD, m, tok))
1.6 kristaps 476: return(0);
1.19 kristaps 477: return(man_body_alloc(m, line, ppos, tok));
1.4 kristaps 478: }
479:
480:
1.19 kristaps 481: int
482: in_line_eoln(MACRO_PROT_ARGS)
1.3 kristaps 483: {
1.19 kristaps 484: int w, la;
485: char *p;
486: struct man_node *n;
1.3 kristaps 487:
1.19 kristaps 488: if ( ! man_elem_alloc(m, line, ppos, tok))
1.3 kristaps 489: return(0);
490:
1.19 kristaps 491: n = m->last;
1.3 kristaps 492:
1.19 kristaps 493: for (;;) {
494: la = *pos;
495: w = man_args(m, line, pos, buf, &p);
1.3 kristaps 496:
1.19 kristaps 497: if (-1 == w)
498: return(0);
499: if (0 == w)
500: break;
501: if ( ! man_word_alloc(m, line, la, p))
502: return(0);
503: }
1.3 kristaps 504:
1.31 kristaps 505: /*
506: * If no arguments are specified and this is MAN_SCOPED (i.e.,
507: * next-line scoped), then set our mode to indicate that we're
508: * waiting for terms to load into our context.
509: */
510:
1.25 kristaps 511: if (n == m->last && MAN_SCOPED & man_macros[tok].flags) {
1.31 kristaps 512: assert( ! (MAN_NSCOPED & man_macros[tok].flags));
1.19 kristaps 513: m->flags |= MAN_ELINE;
514: return(1);
515: }
1.3 kristaps 516:
1.31 kristaps 517: /* Set ignorable context, if applicable. */
518:
519: if (MAN_NSCOPED & man_macros[tok].flags) {
520: assert( ! (MAN_SCOPED & man_macros[tok].flags));
521: m->flags |= MAN_ILINE;
522: }
523:
1.19 kristaps 524: /*
1.31 kristaps 525: * Rewind our element scope. Note that when TH is pruned, we'll
526: * be back at the root, so make sure that we don't clobber as
527: * its sibling.
1.19 kristaps 528: */
1.3 kristaps 529:
1.19 kristaps 530: for ( ; m->last; m->last = m->last->parent) {
531: if (m->last == n)
532: break;
533: if (m->last->type == MAN_ROOT)
534: break;
535: if ( ! man_valid_post(m))
536: return(0);
537: if ( ! man_action_post(m))
538: return(0);
539: }
1.3 kristaps 540:
1.19 kristaps 541: assert(m->last);
1.3 kristaps 542:
543: /*
1.19 kristaps 544: * Same here regarding whether we're back at the root.
1.3 kristaps 545: */
546:
1.19 kristaps 547: if (m->last->type != MAN_ROOT && ! man_valid_post(m))
548: return(0);
549: if (m->last->type != MAN_ROOT && ! man_action_post(m))
550: return(0);
1.35 kristaps 551:
552: m->next = MAN_ROOT == m->last->type ?
553: MAN_NEXT_CHILD : MAN_NEXT_SIBLING;
1.3 kristaps 554:
1.19 kristaps 555: return(1);
556: }
1.3 kristaps 557:
558:
1.19 kristaps 559: int
560: man_macroend(struct man *m)
561: {
1.22 kristaps 562:
1.38 kristaps 563: return(man_unscope(m, m->first, WEXITSCOPE));
1.19 kristaps 564: }
1.3 kristaps 565:
CVSweb