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