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