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