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