Annotation of mandoc/man_macro.c, Revision 1.82
1.82 ! schwarze 1: /* $Id: man_macro.c,v 1.81 2014/03/30 19:47:48 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.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: /*
1.46 kristaps 119: * Rewind scope. If a code "er" != MANDOCERR_MAX has been provided, it
120: * will be used if an explicit block scope is being closed out.
1.38 kristaps 121: */
1.3 kristaps 122: int
1.82 ! schwarze 123: man_unscope(struct man *man, const struct man_node *to,
1.46 kristaps 124: enum mandocerr er)
1.1 kristaps 125: {
1.55 kristaps 126: struct man_node *n;
1.1 kristaps 127:
1.55 kristaps 128: assert(to);
1.19 kristaps 129:
1.75 schwarze 130: man->next = MAN_NEXT_SIBLING;
1.64 kristaps 131:
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:
1.34 kristaps 154: static enum rew
1.33 kristaps 155: rew_block(enum mant ntok, enum man_type type, const struct man_node *n)
1.21 kristaps 156: {
157:
1.82 ! schwarze 158: if (MAN_BLOCK == type && ntok == n->parent->tok &&
! 159: MAN_BODY == n->parent->type)
1.21 kristaps 160: return(REW_REWIND);
161: return(ntok == n->tok ? REW_HALT : REW_NOHALT);
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.82 ! schwarze 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.82 ! schwarze 196: /*
1.38 kristaps 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) {
1.82 ! schwarze 202: case MAN_SH:
1.19 kristaps 203: break;
1.82 ! schwarze 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;
1.82 ! schwarze 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:
1.19 kristaps 237: /*
238: * Rewinding entails ascending the parse tree until a coherent point,
239: * for example, the `SH' macro will close out any intervening `SS'
240: * scopes. When a scope is closed, it must be validated and actioned.
241: */
242: static int
1.75 schwarze 243: rew_scope(enum man_type type, struct man *man, enum mant tok)
1.19 kristaps 244: {
245: struct man_node *n;
1.34 kristaps 246: enum rew c;
1.7 kristaps 247:
1.75 schwarze 248: for (n = man->last; n; n = n->parent) {
1.82 ! schwarze 249: /*
1.19 kristaps 250: * Whether we should stop immediately (REW_HALT), stop
251: * and rewind until this point (REW_REWIND), or keep
252: * rewinding (REW_NOHALT).
253: */
254: c = rew_dohalt(tok, type, n);
255: if (REW_HALT == c)
256: return(1);
257: if (REW_REWIND == c)
1.7 kristaps 258: break;
1.6 kristaps 259: }
1.1 kristaps 260:
1.82 ! schwarze 261: /*
1.38 kristaps 262: * Rewind until the current point. Warn if we're a roff
263: * instruction that's mowing over explicit scopes.
264: */
265: assert(n);
1.19 kristaps 266:
1.75 schwarze 267: return(man_unscope(man, n, MANDOCERR_MAX));
1.19 kristaps 268: }
269:
1.6 kristaps 270:
1.36 kristaps 271: /*
272: * Close out a generic explicit macro.
273: */
1.35 kristaps 274: int
1.21 kristaps 275: blk_close(MACRO_PROT_ARGS)
276: {
1.82 ! schwarze 277: enum mant ntok;
1.21 kristaps 278: const struct man_node *nn;
279:
280: switch (tok) {
1.82 ! schwarze 281: case MAN_RE:
1.21 kristaps 282: ntok = MAN_RS;
1.76 schwarze 283: break;
1.82 ! schwarze 284: case MAN_UE:
1.76 schwarze 285: ntok = MAN_UR;
1.21 kristaps 286: break;
287: default:
288: abort();
289: /* NOTREACHED */
290: }
291:
1.75 schwarze 292: for (nn = man->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.77 schwarze 296: if (NULL == nn) {
297: man_pmsg(man, line, ppos, MANDOCERR_NOSCOPE);
298: if ( ! rew_scope(MAN_BLOCK, man, MAN_PP))
299: return(0);
1.82 ! schwarze 300: } else
1.75 schwarze 301: man_unscope(man, nn, MANDOCERR_MAX);
1.21 kristaps 302:
303: return(1);
304: }
305:
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:
1.75 schwarze 315: if ( ! rew_scope(MAN_BLOCK, man, tok))
1.73 schwarze 316: return(0);
1.35 kristaps 317:
1.75 schwarze 318: if ( ! man_block_alloc(man, line, ppos, tok))
1.35 kristaps 319: return(0);
1.75 schwarze 320: if ( ! man_head_alloc(man, line, ppos, tok))
1.35 kristaps 321: return(0);
322:
323: for (;;) {
324: la = *pos;
1.75 schwarze 325: if ( ! man_args(man, line, pos, buf, &p))
1.35 kristaps 326: break;
1.75 schwarze 327: if ( ! man_word_alloc(man, line, la, p))
1.35 kristaps 328: return(0);
329: }
330:
1.75 schwarze 331: assert(man);
1.35 kristaps 332: assert(tok != MAN_MAX);
333:
1.75 schwarze 334: for (n = man->last; n; n = n->parent) {
1.73 schwarze 335: if (n->tok != tok)
336: continue;
337: assert(MAN_HEAD == n->type);
1.75 schwarze 338: man_unscope(man, n, MANDOCERR_MAX);
1.73 schwarze 339: break;
340: }
341:
1.75 schwarze 342: return(man_body_alloc(man, line, ppos, tok));
1.35 kristaps 343: }
344:
1.19 kristaps 345: /*
346: * Parse an implicit-block macro. These contain a MAN_HEAD and a
347: * MAN_BODY contained within a MAN_BLOCK. Rules for closing out other
348: * scopes, such as `SH' closing out an `SS', are defined in the rew
349: * routines.
350: */
351: int
352: blk_imp(MACRO_PROT_ARGS)
353: {
1.59 kristaps 354: int la;
1.19 kristaps 355: char *p;
1.25 kristaps 356: struct man_node *n;
1.19 kristaps 357:
358: /* Close out prior scopes. */
1.7 kristaps 359:
1.75 schwarze 360: if ( ! rew_scope(MAN_BODY, man, tok))
1.5 kristaps 361: return(0);
1.75 schwarze 362: if ( ! rew_scope(MAN_BLOCK, man, tok))
1.6 kristaps 363: return(0);
1.1 kristaps 364:
1.19 kristaps 365: /* Allocate new block & head scope. */
366:
1.75 schwarze 367: if ( ! man_block_alloc(man, line, ppos, tok))
1.19 kristaps 368: return(0);
1.75 schwarze 369: if ( ! man_head_alloc(man, line, ppos, tok))
1.19 kristaps 370: return(0);
1.1 kristaps 371:
1.75 schwarze 372: n = man->last;
1.25 kristaps 373:
1.19 kristaps 374: /* Add line arguments. */
1.3 kristaps 375:
1.19 kristaps 376: for (;;) {
377: la = *pos;
1.75 schwarze 378: if ( ! man_args(man, line, pos, buf, &p))
1.19 kristaps 379: break;
1.75 schwarze 380: if ( ! man_word_alloc(man, line, la, p))
1.6 kristaps 381: return(0);
382: }
383:
1.19 kristaps 384: /* Close out head and open body (unless MAN_SCOPE). */
385:
1.27 kristaps 386: if (MAN_SCOPED & man_macros[tok].flags) {
387: /* If we're forcing scope (`TP'), keep it open. */
388: if (MAN_FSCOPED & man_macros[tok].flags) {
1.75 schwarze 389: man->flags |= MAN_BLINE;
1.27 kristaps 390: return(1);
1.75 schwarze 391: } else if (n == man->last) {
392: man->flags |= MAN_BLINE;
1.27 kristaps 393: return(1);
394: }
395: }
396:
1.75 schwarze 397: if ( ! rew_scope(MAN_HEAD, man, tok))
1.6 kristaps 398: return(0);
1.75 schwarze 399: return(man_body_alloc(man, line, ppos, tok));
1.4 kristaps 400: }
401:
1.19 kristaps 402: int
403: in_line_eoln(MACRO_PROT_ARGS)
1.3 kristaps 404: {
1.59 kristaps 405: int la;
1.19 kristaps 406: char *p;
407: struct man_node *n;
1.3 kristaps 408:
1.75 schwarze 409: if ( ! man_elem_alloc(man, line, ppos, tok))
1.3 kristaps 410: return(0);
411:
1.75 schwarze 412: n = man->last;
1.3 kristaps 413:
1.19 kristaps 414: for (;;) {
415: la = *pos;
1.75 schwarze 416: if ( ! man_args(man, line, pos, buf, &p))
1.19 kristaps 417: break;
1.75 schwarze 418: if ( ! man_word_alloc(man, line, la, p))
1.19 kristaps 419: return(0);
420: }
1.78 schwarze 421:
422: /*
423: * Append MAN_EOS in case the last snipped argument
424: * ends with a dot, e.g. `.IR syslog (3).'
425: */
426:
427: if (n != man->last &&
1.80 schwarze 428: mandoc_eos(man->last->string, strlen(man->last->string)))
1.78 schwarze 429: man->last->flags |= MAN_EOS;
1.3 kristaps 430:
1.31 kristaps 431: /*
432: * If no arguments are specified and this is MAN_SCOPED (i.e.,
433: * next-line scoped), then set our mode to indicate that we're
434: * waiting for terms to load into our context.
435: */
436:
1.75 schwarze 437: if (n == man->last && MAN_SCOPED & man_macros[tok].flags) {
1.31 kristaps 438: assert( ! (MAN_NSCOPED & man_macros[tok].flags));
1.75 schwarze 439: man->flags |= MAN_ELINE;
1.19 kristaps 440: return(1);
1.82 ! schwarze 441: }
1.3 kristaps 442:
1.31 kristaps 443: /* Set ignorable context, if applicable. */
444:
445: if (MAN_NSCOPED & man_macros[tok].flags) {
446: assert( ! (MAN_SCOPED & man_macros[tok].flags));
1.75 schwarze 447: man->flags |= MAN_ILINE;
1.31 kristaps 448: }
1.64 kristaps 449:
1.75 schwarze 450: assert(MAN_ROOT != man->last->type);
451: man->next = MAN_NEXT_SIBLING;
1.82 ! schwarze 452:
1.19 kristaps 453: /*
1.31 kristaps 454: * Rewind our element scope. Note that when TH is pruned, we'll
455: * be back at the root, so make sure that we don't clobber as
456: * its sibling.
1.19 kristaps 457: */
1.3 kristaps 458:
1.75 schwarze 459: for ( ; man->last; man->last = man->last->parent) {
460: if (man->last == n)
1.19 kristaps 461: break;
1.75 schwarze 462: if (man->last->type == MAN_ROOT)
1.19 kristaps 463: break;
1.75 schwarze 464: if ( ! man_valid_post(man))
1.19 kristaps 465: return(0);
466: }
1.3 kristaps 467:
1.75 schwarze 468: assert(man->last);
1.3 kristaps 469:
470: /*
1.82 ! schwarze 471: * Same here regarding whether we're back at the root.
1.3 kristaps 472: */
473:
1.75 schwarze 474: if (man->last->type != MAN_ROOT && ! man_valid_post(man))
1.19 kristaps 475: return(0);
1.3 kristaps 476:
1.19 kristaps 477: return(1);
478: }
1.3 kristaps 479:
480:
1.19 kristaps 481: int
1.75 schwarze 482: man_macroend(struct man *man)
1.19 kristaps 483: {
1.22 kristaps 484:
1.75 schwarze 485: return(man_unscope(man, man->first, MANDOCERR_SCOPEEXIT));
1.19 kristaps 486: }
1.3 kristaps 487:
1.60 kristaps 488: static int
1.75 schwarze 489: man_args(struct man *man, int line, int *pos, char *buf, char **v)
1.60 kristaps 490: {
491: char *start;
492:
493: assert(*pos);
494: *v = start = buf + *pos;
495: assert(' ' != *start);
496:
497: if ('\0' == *start)
498: return(0);
499:
1.75 schwarze 500: *v = mandoc_getarg(man->parse, v, line, pos);
1.60 kristaps 501: return(1);
502: }
CVSweb