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