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