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