Annotation of mandoc/man_macro.c, Revision 1.84
1.84 ! schwarze 1: /* $Id: man_macro.c,v 1.83 2014/07/07 19:18:15 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)
1.84 ! schwarze 113: mandoc_msg(MANDOCERR_BLK_NOEND,
1.83 schwarze 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) {
1.84 ! schwarze 273: mandoc_msg(MANDOCERR_BLK_NOTOPEN, man->parse,
! 274: line, ppos, man_macronames[tok]);
1.77 schwarze 275: if ( ! rew_scope(MAN_BLOCK, man, MAN_PP))
276: return(0);
1.82 schwarze 277: } else
1.83 schwarze 278: man_unscope(man, nn);
1.21 kristaps 279:
280: return(1);
281: }
282:
1.35 kristaps 283: int
284: blk_exp(MACRO_PROT_ARGS)
285: {
1.73 schwarze 286: struct man_node *n;
1.59 kristaps 287: int la;
1.35 kristaps 288: char *p;
289:
1.73 schwarze 290: /* Close out prior implicit scopes. */
291:
1.75 schwarze 292: if ( ! rew_scope(MAN_BLOCK, man, tok))
1.73 schwarze 293: return(0);
1.35 kristaps 294:
1.75 schwarze 295: if ( ! man_block_alloc(man, line, ppos, tok))
1.35 kristaps 296: return(0);
1.75 schwarze 297: if ( ! man_head_alloc(man, line, ppos, tok))
1.35 kristaps 298: return(0);
299:
300: for (;;) {
301: la = *pos;
1.75 schwarze 302: if ( ! man_args(man, line, pos, buf, &p))
1.35 kristaps 303: break;
1.75 schwarze 304: if ( ! man_word_alloc(man, line, la, p))
1.35 kristaps 305: return(0);
306: }
307:
1.75 schwarze 308: assert(man);
1.35 kristaps 309: assert(tok != MAN_MAX);
310:
1.75 schwarze 311: for (n = man->last; n; n = n->parent) {
1.73 schwarze 312: if (n->tok != tok)
313: continue;
314: assert(MAN_HEAD == n->type);
1.83 schwarze 315: man_unscope(man, n);
1.73 schwarze 316: break;
317: }
318:
1.75 schwarze 319: return(man_body_alloc(man, line, ppos, tok));
1.35 kristaps 320: }
321:
1.19 kristaps 322: /*
323: * Parse an implicit-block macro. These contain a MAN_HEAD and a
324: * MAN_BODY contained within a MAN_BLOCK. Rules for closing out other
325: * scopes, such as `SH' closing out an `SS', are defined in the rew
326: * routines.
327: */
328: int
329: blk_imp(MACRO_PROT_ARGS)
330: {
1.59 kristaps 331: int la;
1.19 kristaps 332: char *p;
1.25 kristaps 333: struct man_node *n;
1.19 kristaps 334:
335: /* Close out prior scopes. */
1.7 kristaps 336:
1.75 schwarze 337: if ( ! rew_scope(MAN_BODY, man, tok))
1.5 kristaps 338: return(0);
1.75 schwarze 339: if ( ! rew_scope(MAN_BLOCK, man, tok))
1.6 kristaps 340: return(0);
1.1 kristaps 341:
1.19 kristaps 342: /* Allocate new block & head scope. */
343:
1.75 schwarze 344: if ( ! man_block_alloc(man, line, ppos, tok))
1.19 kristaps 345: return(0);
1.75 schwarze 346: if ( ! man_head_alloc(man, line, ppos, tok))
1.19 kristaps 347: return(0);
1.1 kristaps 348:
1.75 schwarze 349: n = man->last;
1.25 kristaps 350:
1.19 kristaps 351: /* Add line arguments. */
1.3 kristaps 352:
1.19 kristaps 353: for (;;) {
354: la = *pos;
1.75 schwarze 355: if ( ! man_args(man, line, pos, buf, &p))
1.19 kristaps 356: break;
1.75 schwarze 357: if ( ! man_word_alloc(man, line, la, p))
1.6 kristaps 358: return(0);
359: }
360:
1.19 kristaps 361: /* Close out head and open body (unless MAN_SCOPE). */
362:
1.27 kristaps 363: if (MAN_SCOPED & man_macros[tok].flags) {
364: /* If we're forcing scope (`TP'), keep it open. */
365: if (MAN_FSCOPED & man_macros[tok].flags) {
1.75 schwarze 366: man->flags |= MAN_BLINE;
1.27 kristaps 367: return(1);
1.75 schwarze 368: } else if (n == man->last) {
369: man->flags |= MAN_BLINE;
1.27 kristaps 370: return(1);
371: }
372: }
373:
1.75 schwarze 374: if ( ! rew_scope(MAN_HEAD, man, tok))
1.6 kristaps 375: return(0);
1.75 schwarze 376: return(man_body_alloc(man, line, ppos, tok));
1.4 kristaps 377: }
378:
1.19 kristaps 379: int
380: in_line_eoln(MACRO_PROT_ARGS)
1.3 kristaps 381: {
1.59 kristaps 382: int la;
1.19 kristaps 383: char *p;
384: struct man_node *n;
1.3 kristaps 385:
1.75 schwarze 386: if ( ! man_elem_alloc(man, line, ppos, tok))
1.3 kristaps 387: return(0);
388:
1.75 schwarze 389: n = man->last;
1.3 kristaps 390:
1.19 kristaps 391: for (;;) {
392: la = *pos;
1.75 schwarze 393: if ( ! man_args(man, line, pos, buf, &p))
1.19 kristaps 394: break;
1.75 schwarze 395: if ( ! man_word_alloc(man, line, la, p))
1.19 kristaps 396: return(0);
397: }
1.78 schwarze 398:
399: /*
400: * Append MAN_EOS in case the last snipped argument
401: * ends with a dot, e.g. `.IR syslog (3).'
402: */
403:
404: if (n != man->last &&
1.80 schwarze 405: mandoc_eos(man->last->string, strlen(man->last->string)))
1.78 schwarze 406: man->last->flags |= MAN_EOS;
1.3 kristaps 407:
1.31 kristaps 408: /*
409: * If no arguments are specified and this is MAN_SCOPED (i.e.,
410: * next-line scoped), then set our mode to indicate that we're
411: * waiting for terms to load into our context.
412: */
413:
1.75 schwarze 414: if (n == man->last && MAN_SCOPED & man_macros[tok].flags) {
1.31 kristaps 415: assert( ! (MAN_NSCOPED & man_macros[tok].flags));
1.75 schwarze 416: man->flags |= MAN_ELINE;
1.19 kristaps 417: return(1);
1.82 schwarze 418: }
1.3 kristaps 419:
1.31 kristaps 420: /* Set ignorable context, if applicable. */
421:
422: if (MAN_NSCOPED & man_macros[tok].flags) {
423: assert( ! (MAN_SCOPED & man_macros[tok].flags));
1.75 schwarze 424: man->flags |= MAN_ILINE;
1.31 kristaps 425: }
1.64 kristaps 426:
1.75 schwarze 427: assert(MAN_ROOT != man->last->type);
428: man->next = MAN_NEXT_SIBLING;
1.82 schwarze 429:
1.19 kristaps 430: /*
1.31 kristaps 431: * Rewind our element scope. Note that when TH is pruned, we'll
432: * be back at the root, so make sure that we don't clobber as
433: * its sibling.
1.19 kristaps 434: */
1.3 kristaps 435:
1.75 schwarze 436: for ( ; man->last; man->last = man->last->parent) {
437: if (man->last == n)
1.19 kristaps 438: break;
1.75 schwarze 439: if (man->last->type == MAN_ROOT)
1.19 kristaps 440: break;
1.75 schwarze 441: if ( ! man_valid_post(man))
1.19 kristaps 442: return(0);
443: }
1.3 kristaps 444:
1.75 schwarze 445: assert(man->last);
1.3 kristaps 446:
447: /*
1.82 schwarze 448: * Same here regarding whether we're back at the root.
1.3 kristaps 449: */
450:
1.75 schwarze 451: if (man->last->type != MAN_ROOT && ! man_valid_post(man))
1.19 kristaps 452: return(0);
1.3 kristaps 453:
1.19 kristaps 454: return(1);
455: }
1.3 kristaps 456:
457:
1.19 kristaps 458: int
1.75 schwarze 459: man_macroend(struct man *man)
1.19 kristaps 460: {
1.22 kristaps 461:
1.83 schwarze 462: return(man_unscope(man, man->first));
1.19 kristaps 463: }
1.3 kristaps 464:
1.60 kristaps 465: static int
1.75 schwarze 466: man_args(struct man *man, int line, int *pos, char *buf, char **v)
1.60 kristaps 467: {
468: char *start;
469:
470: assert(*pos);
471: *v = start = buf + *pos;
472: assert(' ' != *start);
473:
474: if ('\0' == *start)
475: return(0);
476:
1.75 schwarze 477: *v = mandoc_getarg(man->parse, v, line, pos);
1.60 kristaps 478: return(1);
479: }
CVSweb