Annotation of mandoc/man_macro.c, Revision 1.73
1.73 ! schwarze 1: /* $Id: man_macro.c,v 1.72 2012/06/02 20:16:23 schwarze Exp $ */
1.1 kristaps 2: /*
1.65 schwarze 3: * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.73 ! schwarze 4: * Copyright (c) 2012 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 5: *
6: * Permission to use, copy, modify, and distribute this software for any
1.14 kristaps 7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 9: *
1.14 kristaps 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 kristaps 17: */
1.30 kristaps 18: #ifdef HAVE_CONFIG_H
19: #include "config.h"
20: #endif
21:
1.1 kristaps 22: #include <assert.h>
23: #include <ctype.h>
24: #include <stdlib.h>
25: #include <string.h>
26:
1.58 kristaps 27: #include "man.h"
1.46 kristaps 28: #include "mandoc.h"
1.57 kristaps 29: #include "libmandoc.h"
1.1 kristaps 30: #include "libman.h"
31:
1.34 kristaps 32: enum rew {
33: REW_REWIND,
34: REW_NOHALT,
1.40 kristaps 35: REW_HALT
1.34 kristaps 36: };
1.19 kristaps 37:
1.35 kristaps 38: static int blk_close(MACRO_PROT_ARGS);
39: static int blk_exp(MACRO_PROT_ARGS);
40: static int blk_imp(MACRO_PROT_ARGS);
1.19 kristaps 41: static int in_line_eoln(MACRO_PROT_ARGS);
1.60 kristaps 42: static int man_args(struct man *, int,
43: int *, char *, char **);
1.19 kristaps 44:
1.33 kristaps 45: static int rew_scope(enum man_type,
46: struct man *, enum mant);
1.34 kristaps 47: static enum rew rew_dohalt(enum mant, enum man_type,
1.19 kristaps 48: const struct man_node *);
1.34 kristaps 49: static enum rew rew_block(enum mant, enum man_type,
1.21 kristaps 50: const struct man_node *);
1.56 kristaps 51: static void rew_warn(struct man *,
1.46 kristaps 52: struct man_node *, enum mandocerr);
1.19 kristaps 53:
54: const struct man_macro __man_macros[MAN_MAX] = {
1.31 kristaps 55: { in_line_eoln, MAN_NSCOPED }, /* br */
1.66 schwarze 56: { in_line_eoln, MAN_BSCOPE }, /* TH */
57: { blk_imp, MAN_BSCOPE | MAN_SCOPED }, /* SH */
58: { blk_imp, MAN_BSCOPE | MAN_SCOPED }, /* SS */
59: { blk_imp, MAN_BSCOPE | MAN_SCOPED | MAN_FSCOPED }, /* TP */
60: { blk_imp, MAN_BSCOPE }, /* LP */
61: { blk_imp, MAN_BSCOPE }, /* PP */
62: { blk_imp, MAN_BSCOPE }, /* P */
63: { blk_imp, MAN_BSCOPE }, /* IP */
64: { blk_imp, MAN_BSCOPE }, /* HP */
1.19 kristaps 65: { in_line_eoln, MAN_SCOPED }, /* SM */
66: { in_line_eoln, MAN_SCOPED }, /* SB */
67: { in_line_eoln, 0 }, /* BI */
68: { in_line_eoln, 0 }, /* IB */
69: { in_line_eoln, 0 }, /* BR */
70: { in_line_eoln, 0 }, /* RB */
71: { in_line_eoln, MAN_SCOPED }, /* R */
72: { in_line_eoln, MAN_SCOPED }, /* B */
73: { in_line_eoln, MAN_SCOPED }, /* I */
74: { in_line_eoln, 0 }, /* IR */
75: { in_line_eoln, 0 }, /* RI */
1.31 kristaps 76: { in_line_eoln, MAN_NSCOPED }, /* na */
77: { in_line_eoln, MAN_NSCOPED }, /* sp */
1.66 schwarze 78: { in_line_eoln, MAN_BSCOPE }, /* nf */
79: { in_line_eoln, MAN_BSCOPE }, /* fi */
1.21 kristaps 80: { blk_close, 0 }, /* RE */
1.35 kristaps 81: { blk_exp, MAN_EXPLICIT }, /* RS */
1.23 kristaps 82: { in_line_eoln, 0 }, /* DT */
1.28 kristaps 83: { in_line_eoln, 0 }, /* UC */
1.29 kristaps 84: { in_line_eoln, 0 }, /* PD */
1.45 joerg 85: { in_line_eoln, 0 }, /* AT */
1.49 kristaps 86: { in_line_eoln, 0 }, /* in */
1.53 kristaps 87: { in_line_eoln, 0 }, /* ft */
1.71 kristaps 88: { in_line_eoln, 0 }, /* OP */
1.72 schwarze 89: { in_line_eoln, MAN_BSCOPE }, /* EX */
90: { in_line_eoln, MAN_BSCOPE }, /* EE */
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: */
1.56 kristaps 99: static void
1.46 kristaps 100: rew_warn(struct man *m, struct man_node *n, enum mandocerr er)
1.38 kristaps 101: {
102:
1.46 kristaps 103: if (er == MANDOCERR_MAX || MAN_BLOCK != n->type)
1.56 kristaps 104: return;
1.38 kristaps 105: if (MAN_VALID & n->flags)
1.56 kristaps 106: return;
1.38 kristaps 107: if ( ! (MAN_EXPLICIT & man_macros[n->tok].flags))
1.56 kristaps 108: return;
109:
110: assert(er < MANDOCERR_FATAL);
111: man_nmsg(m, n, er);
1.38 kristaps 112: }
113:
114:
115: /*
1.46 kristaps 116: * Rewind scope. If a code "er" != MANDOCERR_MAX has been provided, it
117: * will be used if an explicit block scope is being closed out.
1.38 kristaps 118: */
1.3 kristaps 119: int
1.55 kristaps 120: man_unscope(struct man *m, const struct man_node *to,
1.46 kristaps 121: enum mandocerr er)
1.1 kristaps 122: {
1.55 kristaps 123: struct man_node *n;
1.1 kristaps 124:
1.55 kristaps 125: assert(to);
1.19 kristaps 126:
1.64 kristaps 127: m->next = MAN_NEXT_SIBLING;
128:
1.19 kristaps 129: /* LINTED */
1.55 kristaps 130: while (m->last != to) {
131: /*
132: * Save the parent here, because we may delete the
133: * m->last node in the post-validation phase and reset
134: * it to m->last->parent, causing a step in the closing
135: * out to be lost.
136: */
137: n = m->last->parent;
1.56 kristaps 138: rew_warn(m, m->last, er);
1.19 kristaps 139: if ( ! man_valid_post(m))
140: return(0);
1.55 kristaps 141: m->last = n;
1.19 kristaps 142: assert(m->last);
143: }
144:
1.56 kristaps 145: rew_warn(m, m->last, er);
1.19 kristaps 146: if ( ! man_valid_post(m))
1.1 kristaps 147: return(0);
1.35 kristaps 148:
149: return(1);
1.19 kristaps 150: }
1.1 kristaps 151:
152:
1.34 kristaps 153: static enum rew
1.33 kristaps 154: rew_block(enum mant ntok, enum man_type type, const struct man_node *n)
1.21 kristaps 155: {
156:
157: if (MAN_BLOCK == type && ntok == n->parent->tok &&
158: MAN_BODY == n->parent->type)
159: return(REW_REWIND);
160: return(ntok == n->tok ? REW_HALT : REW_NOHALT);
161: }
162:
163:
1.19 kristaps 164: /*
165: * There are three scope levels: scoped to the root (all), scoped to the
166: * section (all less sections), and scoped to subsections (all less
167: * sections and subsections).
168: */
1.34 kristaps 169: static enum rew
1.33 kristaps 170: rew_dohalt(enum mant tok, enum man_type type, const struct man_node *n)
1.19 kristaps 171: {
1.34 kristaps 172: enum rew c;
1.1 kristaps 173:
1.38 kristaps 174: /* We cannot progress beyond the root ever. */
1.19 kristaps 175: if (MAN_ROOT == n->type)
176: return(REW_HALT);
1.38 kristaps 177:
1.19 kristaps 178: assert(n->parent);
1.38 kristaps 179:
180: /* Normal nodes shouldn't go to the level of the root. */
1.19 kristaps 181: if (MAN_ROOT == n->parent->type)
182: return(REW_REWIND);
1.38 kristaps 183:
184: /* Already-validated nodes should be closed out. */
1.19 kristaps 185: if (MAN_VALID & n->flags)
186: return(REW_NOHALT);
187:
1.38 kristaps 188: /* First: rewind to ourselves. */
1.73 ! schwarze 189: if (type == n->type && tok == n->tok) {
! 190: if (MAN_EXPLICIT & man_macros[n->tok].flags)
! 191: return(REW_HALT);
! 192: else
! 193: return(REW_REWIND);
! 194: }
1.21 kristaps 195:
1.38 kristaps 196: /*
197: * Next follow the implicit scope-smashings as defined by man.7:
198: * section, sub-section, etc.
199: */
200:
1.19 kristaps 201: switch (tok) {
202: case (MAN_SH):
203: break;
204: case (MAN_SS):
1.20 kristaps 205: /* Rewind to a section, if a block. */
1.21 kristaps 206: if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
207: return(c);
208: break;
209: case (MAN_RS):
1.73 ! schwarze 210: /* Preserve empty paragraphs before RS. */
! 211: if (0 == n->nchild && (MAN_P == n->tok ||
! 212: MAN_PP == n->tok || MAN_LP == n->tok))
! 213: return(REW_HALT);
1.21 kristaps 214: /* Rewind to a subsection, if a block. */
215: if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
216: return(c);
217: /* Rewind to a section, if a block. */
218: if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
219: return(c);
1.19 kristaps 220: break;
221: default:
1.21 kristaps 222: /* Rewind to an offsetter, if a block. */
223: if (REW_NOHALT != (c = rew_block(MAN_RS, type, n)))
224: return(c);
1.20 kristaps 225: /* Rewind to a subsection, if a block. */
1.21 kristaps 226: if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
227: return(c);
1.20 kristaps 228: /* Rewind to a section, if a block. */
1.21 kristaps 229: if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
230: return(c);
1.19 kristaps 231: break;
1.3 kristaps 232: }
1.1 kristaps 233:
1.19 kristaps 234: return(REW_NOHALT);
235: }
1.9 kristaps 236:
237:
1.19 kristaps 238: /*
239: * Rewinding entails ascending the parse tree until a coherent point,
240: * for example, the `SH' macro will close out any intervening `SS'
241: * scopes. When a scope is closed, it must be validated and actioned.
242: */
243: static int
1.33 kristaps 244: rew_scope(enum man_type type, struct man *m, enum mant tok)
1.19 kristaps 245: {
246: struct man_node *n;
1.34 kristaps 247: enum rew c;
1.7 kristaps 248:
1.19 kristaps 249: /* LINTED */
250: for (n = m->last; n; n = n->parent) {
251: /*
252: * Whether we should stop immediately (REW_HALT), stop
253: * and rewind until this point (REW_REWIND), or keep
254: * rewinding (REW_NOHALT).
255: */
256: c = rew_dohalt(tok, type, n);
257: if (REW_HALT == c)
258: return(1);
259: if (REW_REWIND == c)
1.7 kristaps 260: break;
1.6 kristaps 261: }
1.1 kristaps 262:
1.38 kristaps 263: /*
264: * Rewind until the current point. Warn if we're a roff
265: * instruction that's mowing over explicit scopes.
266: */
267: assert(n);
1.19 kristaps 268:
1.46 kristaps 269: return(man_unscope(m, n, MANDOCERR_MAX));
1.19 kristaps 270: }
271:
1.6 kristaps 272:
1.36 kristaps 273: /*
274: * Close out a generic explicit macro.
275: */
1.37 kristaps 276: /* ARGSUSED */
1.35 kristaps 277: int
1.21 kristaps 278: blk_close(MACRO_PROT_ARGS)
279: {
1.33 kristaps 280: enum mant ntok;
1.21 kristaps 281: const struct man_node *nn;
282:
283: switch (tok) {
284: case (MAN_RE):
285: ntok = MAN_RS;
286: break;
287: default:
288: abort();
289: /* NOTREACHED */
290: }
291:
292: for (nn = m->last->parent; nn; nn = nn->parent)
1.73 ! schwarze 293: if (ntok == nn->tok && MAN_BLOCK == nn->type)
1.21 kristaps 294: break;
295:
1.73 ! schwarze 296: if (NULL != nn)
! 297: man_unscope(m, nn, MANDOCERR_MAX);
! 298: else
1.56 kristaps 299: man_pmsg(m, line, ppos, MANDOCERR_NOSCOPE);
1.21 kristaps 300:
301: return(1);
302: }
303:
304:
1.48 kristaps 305: /* ARGSUSED */
1.35 kristaps 306: int
307: blk_exp(MACRO_PROT_ARGS)
308: {
1.73 ! schwarze 309: struct man_node *n;
1.59 kristaps 310: int la;
1.35 kristaps 311: char *p;
312:
1.73 ! schwarze 313: /* Close out prior implicit scopes. */
! 314:
! 315: if ( ! rew_scope(MAN_BLOCK, m, tok))
! 316: return(0);
1.35 kristaps 317:
318: if ( ! man_block_alloc(m, line, ppos, tok))
319: return(0);
320: if ( ! man_head_alloc(m, line, ppos, tok))
321: return(0);
322:
323: for (;;) {
324: la = *pos;
1.60 kristaps 325: if ( ! man_args(m, line, pos, buf, &p))
1.35 kristaps 326: break;
327: if ( ! man_word_alloc(m, line, la, p))
328: return(0);
329: }
330:
331: assert(m);
332: assert(tok != MAN_MAX);
333:
1.73 ! schwarze 334: for (n = m->last; n; n = n->parent) {
! 335: if (n->tok != tok)
! 336: continue;
! 337: assert(MAN_HEAD == n->type);
! 338: man_unscope(m, n, MANDOCERR_MAX);
! 339: break;
! 340: }
! 341:
1.35 kristaps 342: return(man_body_alloc(m, line, ppos, tok));
343: }
344:
345:
346:
1.19 kristaps 347: /*
348: * Parse an implicit-block macro. These contain a MAN_HEAD and a
349: * MAN_BODY contained within a MAN_BLOCK. Rules for closing out other
350: * scopes, such as `SH' closing out an `SS', are defined in the rew
351: * routines.
352: */
1.48 kristaps 353: /* ARGSUSED */
1.19 kristaps 354: int
355: blk_imp(MACRO_PROT_ARGS)
356: {
1.59 kristaps 357: int la;
1.19 kristaps 358: char *p;
1.25 kristaps 359: struct man_node *n;
1.19 kristaps 360:
361: /* Close out prior scopes. */
1.7 kristaps 362:
1.19 kristaps 363: if ( ! rew_scope(MAN_BODY, m, tok))
1.5 kristaps 364: return(0);
1.19 kristaps 365: if ( ! rew_scope(MAN_BLOCK, m, tok))
1.6 kristaps 366: return(0);
1.1 kristaps 367:
1.19 kristaps 368: /* Allocate new block & head scope. */
369:
370: if ( ! man_block_alloc(m, line, ppos, tok))
371: return(0);
372: if ( ! man_head_alloc(m, line, ppos, tok))
373: return(0);
1.1 kristaps 374:
1.25 kristaps 375: n = m->last;
376:
1.19 kristaps 377: /* Add line arguments. */
1.3 kristaps 378:
1.19 kristaps 379: for (;;) {
380: la = *pos;
1.60 kristaps 381: if ( ! man_args(m, line, pos, buf, &p))
1.19 kristaps 382: break;
383: if ( ! man_word_alloc(m, line, la, p))
1.6 kristaps 384: return(0);
385: }
386:
1.19 kristaps 387: /* Close out head and open body (unless MAN_SCOPE). */
388:
1.27 kristaps 389: if (MAN_SCOPED & man_macros[tok].flags) {
390: /* If we're forcing scope (`TP'), keep it open. */
391: if (MAN_FSCOPED & man_macros[tok].flags) {
392: m->flags |= MAN_BLINE;
393: return(1);
394: } else if (n == m->last) {
395: m->flags |= MAN_BLINE;
396: return(1);
397: }
398: }
399:
400: if ( ! rew_scope(MAN_HEAD, m, tok))
1.6 kristaps 401: return(0);
1.19 kristaps 402: return(man_body_alloc(m, line, ppos, tok));
1.4 kristaps 403: }
404:
405:
1.48 kristaps 406: /* ARGSUSED */
1.19 kristaps 407: int
408: in_line_eoln(MACRO_PROT_ARGS)
1.3 kristaps 409: {
1.59 kristaps 410: int la;
1.19 kristaps 411: char *p;
412: struct man_node *n;
1.3 kristaps 413:
1.19 kristaps 414: if ( ! man_elem_alloc(m, line, ppos, tok))
1.3 kristaps 415: return(0);
416:
1.19 kristaps 417: n = m->last;
1.3 kristaps 418:
1.19 kristaps 419: for (;;) {
420: la = *pos;
1.60 kristaps 421: if ( ! man_args(m, line, pos, buf, &p))
1.19 kristaps 422: break;
423: if ( ! man_word_alloc(m, line, la, p))
424: return(0);
425: }
1.3 kristaps 426:
1.31 kristaps 427: /*
428: * If no arguments are specified and this is MAN_SCOPED (i.e.,
429: * next-line scoped), then set our mode to indicate that we're
430: * waiting for terms to load into our context.
431: */
432:
1.25 kristaps 433: if (n == m->last && MAN_SCOPED & man_macros[tok].flags) {
1.31 kristaps 434: assert( ! (MAN_NSCOPED & man_macros[tok].flags));
1.19 kristaps 435: m->flags |= MAN_ELINE;
436: return(1);
437: }
1.3 kristaps 438:
1.31 kristaps 439: /* Set ignorable context, if applicable. */
440:
441: if (MAN_NSCOPED & man_macros[tok].flags) {
442: assert( ! (MAN_SCOPED & man_macros[tok].flags));
443: m->flags |= MAN_ILINE;
444: }
1.64 kristaps 445:
446: assert(MAN_ROOT != m->last->type);
447: m->next = MAN_NEXT_SIBLING;
1.31 kristaps 448:
1.19 kristaps 449: /*
1.31 kristaps 450: * Rewind our element scope. Note that when TH is pruned, we'll
451: * be back at the root, so make sure that we don't clobber as
452: * its sibling.
1.19 kristaps 453: */
1.3 kristaps 454:
1.19 kristaps 455: for ( ; m->last; m->last = m->last->parent) {
456: if (m->last == n)
457: break;
458: if (m->last->type == MAN_ROOT)
459: break;
460: if ( ! man_valid_post(m))
461: return(0);
462: }
1.3 kristaps 463:
1.19 kristaps 464: assert(m->last);
1.3 kristaps 465:
466: /*
1.19 kristaps 467: * Same here regarding whether we're back at the root.
1.3 kristaps 468: */
469:
1.19 kristaps 470: if (m->last->type != MAN_ROOT && ! man_valid_post(m))
471: return(0);
1.3 kristaps 472:
1.19 kristaps 473: return(1);
474: }
1.3 kristaps 475:
476:
1.19 kristaps 477: int
478: man_macroend(struct man *m)
479: {
1.22 kristaps 480:
1.46 kristaps 481: return(man_unscope(m, m->first, MANDOCERR_SCOPEEXIT));
1.19 kristaps 482: }
1.3 kristaps 483:
1.60 kristaps 484: static int
485: man_args(struct man *m, int line, int *pos, char *buf, char **v)
486: {
487: char *start;
488:
489: assert(*pos);
490: *v = start = buf + *pos;
491: assert(' ' != *start);
492:
493: if ('\0' == *start)
494: return(0);
495:
1.62 kristaps 496: *v = mandoc_getarg(m->parse, v, line, pos);
1.60 kristaps 497: return(1);
498: }
CVSweb