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