Annotation of mandoc/man_macro.c, Revision 1.108
1.108 ! schwarze 1: /* $Id: man_macro.c,v 1.107 2015/04/19 14:25:41 schwarze Exp $ */
1.1 kristaps 2: /*
1.65 schwarze 3: * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.93 schwarze 4: * Copyright (c) 2012, 2013, 2014, 2015 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.101 schwarze 11: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.14 kristaps 12: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.101 schwarze 13: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.14 kristaps 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.101 schwarze 28: #include "mandoc.h"
29: #include "roff.h"
1.58 kristaps 30: #include "man.h"
1.57 kristaps 31: #include "libmandoc.h"
1.106 schwarze 32: #include "roff_int.h"
1.1 kristaps 33: #include "libman.h"
34:
1.91 schwarze 35: static void blk_close(MACRO_PROT_ARGS);
36: static void blk_exp(MACRO_PROT_ARGS);
37: static void blk_imp(MACRO_PROT_ARGS);
38: static void in_line_eoln(MACRO_PROT_ARGS);
1.105 schwarze 39: static int man_args(struct roff_man *, int,
1.60 kristaps 40: int *, char *, char **);
1.105 schwarze 41: static void rew_scope(struct roff_man *, int);
1.19 kristaps 42:
43: const struct man_macro __man_macros[MAN_MAX] = {
1.31 kristaps 44: { in_line_eoln, MAN_NSCOPED }, /* br */
1.66 schwarze 45: { in_line_eoln, MAN_BSCOPE }, /* TH */
46: { blk_imp, MAN_BSCOPE | MAN_SCOPED }, /* SH */
47: { blk_imp, MAN_BSCOPE | MAN_SCOPED }, /* SS */
1.99 schwarze 48: { blk_imp, MAN_BSCOPE | MAN_SCOPED }, /* TP */
1.66 schwarze 49: { blk_imp, MAN_BSCOPE }, /* LP */
50: { blk_imp, MAN_BSCOPE }, /* PP */
51: { blk_imp, MAN_BSCOPE }, /* P */
52: { blk_imp, MAN_BSCOPE }, /* IP */
53: { blk_imp, MAN_BSCOPE }, /* HP */
1.90 schwarze 54: { in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* SM */
55: { in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* SB */
1.19 kristaps 56: { in_line_eoln, 0 }, /* BI */
57: { in_line_eoln, 0 }, /* IB */
58: { in_line_eoln, 0 }, /* BR */
59: { in_line_eoln, 0 }, /* RB */
1.90 schwarze 60: { in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* R */
61: { in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* B */
62: { in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* I */
1.19 kristaps 63: { in_line_eoln, 0 }, /* IR */
64: { in_line_eoln, 0 }, /* RI */
1.31 kristaps 65: { in_line_eoln, MAN_NSCOPED }, /* sp */
1.66 schwarze 66: { in_line_eoln, MAN_BSCOPE }, /* nf */
67: { in_line_eoln, MAN_BSCOPE }, /* fi */
1.92 schwarze 68: { blk_close, MAN_BSCOPE }, /* RE */
1.99 schwarze 69: { blk_exp, MAN_BSCOPE }, /* RS */
1.23 kristaps 70: { in_line_eoln, 0 }, /* DT */
1.28 kristaps 71: { in_line_eoln, 0 }, /* UC */
1.100 schwarze 72: { in_line_eoln, MAN_NSCOPED }, /* PD */
1.45 joerg 73: { in_line_eoln, 0 }, /* AT */
1.49 kristaps 74: { in_line_eoln, 0 }, /* in */
1.53 kristaps 75: { in_line_eoln, 0 }, /* ft */
1.71 kristaps 76: { in_line_eoln, 0 }, /* OP */
1.72 schwarze 77: { in_line_eoln, MAN_BSCOPE }, /* EX */
78: { in_line_eoln, MAN_BSCOPE }, /* EE */
1.99 schwarze 79: { blk_exp, MAN_BSCOPE }, /* UR */
1.92 schwarze 80: { blk_close, MAN_BSCOPE }, /* UE */
1.81 schwarze 81: { in_line_eoln, 0 }, /* ll */
1.19 kristaps 82: };
1.9 kristaps 83:
1.19 kristaps 84: const struct man_macro * const man_macros = __man_macros;
1.1 kristaps 85:
86:
1.91 schwarze 87: void
1.105 schwarze 88: man_unscope(struct roff_man *man, const struct roff_node *to)
1.1 kristaps 89: {
1.102 schwarze 90: struct roff_node *n;
1.1 kristaps 91:
1.83 schwarze 92: to = to->parent;
93: n = man->last;
94: while (n != to) {
1.87 schwarze 95:
96: /* Reached the end of the document? */
97:
98: if (to == NULL && ! (n->flags & MAN_VALID)) {
99: if (man->flags & (MAN_BLINE | MAN_ELINE) &&
100: man_macros[n->tok].flags & MAN_SCOPED) {
101: mandoc_vmsg(MANDOCERR_BLK_LINE,
102: man->parse, n->line, n->pos,
103: "EOF breaks %s",
104: man_macronames[n->tok]);
105: if (man->flags & MAN_ELINE)
106: man->flags &= ~MAN_ELINE;
107: else {
1.101 schwarze 108: assert(n->type == ROFFT_HEAD);
1.87 schwarze 109: n = n->parent;
110: man->flags &= ~MAN_BLINE;
111: }
112: man->last = n;
113: n = n->parent;
1.106 schwarze 114: roff_node_delete(man, man->last);
1.87 schwarze 115: continue;
116: }
1.101 schwarze 117: if (n->type == ROFFT_BLOCK &&
1.99 schwarze 118: man_macros[n->tok].fp == blk_exp)
1.87 schwarze 119: mandoc_msg(MANDOCERR_BLK_NOEND,
120: man->parse, n->line, n->pos,
121: man_macronames[n->tok]);
122: }
123:
1.55 kristaps 124: /*
1.83 schwarze 125: * We might delete the man->last node
126: * in the post-validation phase.
127: * Save a pointer to the parent such that
128: * we know where to continue the iteration.
1.55 kristaps 129: */
1.89 schwarze 130:
1.83 schwarze 131: man->last = n;
132: n = n->parent;
1.91 schwarze 133: man_valid_post(man);
1.19 kristaps 134: }
1.89 schwarze 135:
136: /*
137: * If we ended up at the parent of the node we were
138: * supposed to rewind to, that means the target node
139: * got deleted, so add the next node we parse as a child
140: * of the parent instead of as a sibling of the target.
141: */
142:
143: man->next = (man->last == to) ?
1.105 schwarze 144: ROFF_NEXT_CHILD : ROFF_NEXT_SIBLING;
1.19 kristaps 145: }
1.1 kristaps 146:
1.19 kristaps 147: /*
148: * Rewinding entails ascending the parse tree until a coherent point,
149: * for example, the `SH' macro will close out any intervening `SS'
150: * scopes. When a scope is closed, it must be validated and actioned.
151: */
1.91 schwarze 152: static void
1.105 schwarze 153: rew_scope(struct roff_man *man, int tok)
1.19 kristaps 154: {
1.102 schwarze 155: struct roff_node *n;
1.7 kristaps 156:
1.104 schwarze 157: /* Preserve empty paragraphs before RS. */
158:
159: n = man->last;
160: if (tok == MAN_RS && n->nchild == 0 &&
161: (n->tok == MAN_P || n->tok == MAN_PP || n->tok == MAN_LP))
162: return;
163:
164: for (;;) {
165: if (n->type == ROFFT_ROOT)
166: return;
167: if (n->flags & MAN_VALID) {
168: n = n->parent;
169: continue;
170: }
171: if (n->type != ROFFT_BLOCK) {
172: if (n->parent->type == ROFFT_ROOT) {
173: man_unscope(man, n);
174: return;
175: } else {
176: n = n->parent;
177: continue;
178: }
179: }
180: if (tok != MAN_SH && (n->tok == MAN_SH ||
181: (tok != MAN_SS && (n->tok == MAN_SS ||
182: man_macros[n->tok].fp == blk_exp))))
1.91 schwarze 183: return;
1.104 schwarze 184: man_unscope(man, n);
185: n = man->last;
1.6 kristaps 186: }
1.19 kristaps 187: }
188:
1.6 kristaps 189:
1.36 kristaps 190: /*
191: * Close out a generic explicit macro.
192: */
1.91 schwarze 193: void
1.21 kristaps 194: blk_close(MACRO_PROT_ARGS)
195: {
1.102 schwarze 196: int ntok;
197: const struct roff_node *nn;
1.93 schwarze 198: char *p;
199: int nrew, target;
1.21 kristaps 200:
1.93 schwarze 201: nrew = 1;
1.21 kristaps 202: switch (tok) {
1.82 schwarze 203: case MAN_RE:
1.21 kristaps 204: ntok = MAN_RS;
1.93 schwarze 205: if ( ! man_args(man, line, pos, buf, &p))
206: break;
207: for (nn = man->last->parent; nn; nn = nn->parent)
1.101 schwarze 208: if (nn->tok == ntok && nn->type == ROFFT_BLOCK)
1.93 schwarze 209: nrew++;
210: target = strtol(p, &p, 10);
211: if (*p != '\0')
212: mandoc_vmsg(MANDOCERR_ARG_EXCESS, man->parse,
213: line, p - buf, "RE ... %s", p);
214: if (target == 0)
215: target = 1;
216: nrew -= target;
217: if (nrew < 1) {
218: mandoc_vmsg(MANDOCERR_RE_NOTOPEN, man->parse,
219: line, ppos, "RE %d", target);
220: return;
221: }
1.76 schwarze 222: break;
1.82 schwarze 223: case MAN_UE:
1.76 schwarze 224: ntok = MAN_UR;
1.21 kristaps 225: break;
226: default:
227: abort();
228: /* NOTREACHED */
229: }
230:
1.75 schwarze 231: for (nn = man->last->parent; nn; nn = nn->parent)
1.101 schwarze 232: if (nn->tok == ntok && nn->type == ROFFT_BLOCK && ! --nrew)
1.21 kristaps 233: break;
234:
1.91 schwarze 235: if (nn == NULL) {
1.84 schwarze 236: mandoc_msg(MANDOCERR_BLK_NOTOPEN, man->parse,
237: line, ppos, man_macronames[tok]);
1.104 schwarze 238: rew_scope(man, MAN_PP);
1.95 schwarze 239: } else {
240: line = man->last->line;
241: ppos = man->last->pos;
242: ntok = man->last->tok;
1.83 schwarze 243: man_unscope(man, nn);
1.95 schwarze 244:
245: /* Move a trailing paragraph behind the block. */
246:
247: if (ntok == MAN_LP || ntok == MAN_PP || ntok == MAN_P) {
248: *pos = strlen(buf);
249: blk_imp(man, ntok, line, ppos, pos, buf);
250: }
251: }
1.21 kristaps 252: }
253:
1.91 schwarze 254: void
1.35 kristaps 255: blk_exp(MACRO_PROT_ARGS)
256: {
1.102 schwarze 257: struct roff_node *head;
1.98 schwarze 258: char *p;
1.59 kristaps 259: int la;
1.35 kristaps 260:
1.104 schwarze 261: rew_scope(man, tok);
1.108 ! schwarze 262: roff_block_alloc(man, line, ppos, tok);
1.106 schwarze 263: head = roff_head_alloc(man, line, ppos, tok);
1.35 kristaps 264:
1.98 schwarze 265: la = *pos;
266: if (man_args(man, line, pos, buf, &p))
1.107 schwarze 267: roff_word_alloc(man, line, la, p);
1.35 kristaps 268:
1.98 schwarze 269: if (buf[*pos] != '\0')
270: mandoc_vmsg(MANDOCERR_ARG_EXCESS,
271: man->parse, line, *pos, "%s ... %s",
272: man_macronames[tok], buf + *pos);
1.73 schwarze 273:
1.98 schwarze 274: man_unscope(man, head);
1.106 schwarze 275: roff_body_alloc(man, line, ppos, tok);
1.35 kristaps 276: }
277:
1.19 kristaps 278: /*
1.101 schwarze 279: * Parse an implicit-block macro. These contain a ROFFT_HEAD and a
280: * ROFFT_BODY contained within a ROFFT_BLOCK. Rules for closing out other
1.19 kristaps 281: * scopes, such as `SH' closing out an `SS', are defined in the rew
282: * routines.
283: */
1.91 schwarze 284: void
1.19 kristaps 285: blk_imp(MACRO_PROT_ARGS)
286: {
1.59 kristaps 287: int la;
1.19 kristaps 288: char *p;
1.102 schwarze 289: struct roff_node *n;
1.19 kristaps 290:
1.104 schwarze 291: rew_scope(man, tok);
1.108 ! schwarze 292: n = roff_block_alloc(man, line, ppos, tok);
1.106 schwarze 293: if (n->tok == MAN_SH || n->tok == MAN_SS)
294: man->flags &= ~MAN_LITERAL;
295: n = roff_head_alloc(man, line, ppos, tok);
1.25 kristaps 296:
1.19 kristaps 297: /* Add line arguments. */
1.3 kristaps 298:
1.19 kristaps 299: for (;;) {
300: la = *pos;
1.75 schwarze 301: if ( ! man_args(man, line, pos, buf, &p))
1.19 kristaps 302: break;
1.107 schwarze 303: roff_word_alloc(man, line, la, p);
1.6 kristaps 304: }
305:
1.99 schwarze 306: /*
307: * For macros having optional next-line scope,
308: * keep the head open if there were no arguments.
309: * For `TP', always keep the head open.
310: */
1.19 kristaps 311:
1.99 schwarze 312: if (man_macros[tok].flags & MAN_SCOPED &&
313: (tok == MAN_TP || n == man->last)) {
314: man->flags |= MAN_BLINE;
315: return;
1.27 kristaps 316: }
1.99 schwarze 317:
318: /* Close out the head and open the body. */
319:
1.103 schwarze 320: man_unscope(man, n);
1.106 schwarze 321: roff_body_alloc(man, line, ppos, tok);
1.4 kristaps 322: }
323:
1.91 schwarze 324: void
1.19 kristaps 325: in_line_eoln(MACRO_PROT_ARGS)
1.3 kristaps 326: {
1.59 kristaps 327: int la;
1.19 kristaps 328: char *p;
1.102 schwarze 329: struct roff_node *n;
1.3 kristaps 330:
1.108 ! schwarze 331: roff_elem_alloc(man, line, ppos, tok);
1.75 schwarze 332: n = man->last;
1.3 kristaps 333:
1.19 kristaps 334: for (;;) {
1.96 schwarze 335: if (buf[*pos] != '\0' && (tok == MAN_br ||
336: tok == MAN_fi || tok == MAN_nf)) {
337: mandoc_vmsg(MANDOCERR_ARG_SKIP,
338: man->parse, line, *pos, "%s %s",
1.97 schwarze 339: man_macronames[tok], buf + *pos);
340: break;
341: }
342: if (buf[*pos] != '\0' && man->last != n &&
343: (tok == MAN_PD || tok == MAN_ft || tok == MAN_sp)) {
344: mandoc_vmsg(MANDOCERR_ARG_EXCESS,
345: man->parse, line, *pos, "%s ... %s",
1.96 schwarze 346: man_macronames[tok], buf + *pos);
347: break;
348: }
1.19 kristaps 349: la = *pos;
1.75 schwarze 350: if ( ! man_args(man, line, pos, buf, &p))
1.19 kristaps 351: break;
1.90 schwarze 352: if (man_macros[tok].flags & MAN_JOIN &&
1.101 schwarze 353: man->last->type == ROFFT_TEXT)
1.107 schwarze 354: roff_word_append(man, p);
1.91 schwarze 355: else
1.107 schwarze 356: roff_word_alloc(man, line, la, p);
1.19 kristaps 357: }
1.78 schwarze 358:
359: /*
360: * Append MAN_EOS in case the last snipped argument
361: * ends with a dot, e.g. `.IR syslog (3).'
362: */
363:
364: if (n != man->last &&
1.80 schwarze 365: mandoc_eos(man->last->string, strlen(man->last->string)))
1.78 schwarze 366: man->last->flags |= MAN_EOS;
1.3 kristaps 367:
1.31 kristaps 368: /*
369: * If no arguments are specified and this is MAN_SCOPED (i.e.,
370: * next-line scoped), then set our mode to indicate that we're
371: * waiting for terms to load into our context.
372: */
373:
1.91 schwarze 374: if (n == man->last && man_macros[tok].flags & MAN_SCOPED) {
375: assert( ! (man_macros[tok].flags & MAN_NSCOPED));
1.75 schwarze 376: man->flags |= MAN_ELINE;
1.91 schwarze 377: return;
1.31 kristaps 378: }
1.64 kristaps 379:
1.101 schwarze 380: assert(man->last->type != ROFFT_ROOT);
1.105 schwarze 381: man->next = ROFF_NEXT_SIBLING;
1.82 schwarze 382:
1.19 kristaps 383: /*
1.31 kristaps 384: * Rewind our element scope. Note that when TH is pruned, we'll
385: * be back at the root, so make sure that we don't clobber as
386: * its sibling.
1.19 kristaps 387: */
1.3 kristaps 388:
1.75 schwarze 389: for ( ; man->last; man->last = man->last->parent) {
390: if (man->last == n)
1.19 kristaps 391: break;
1.101 schwarze 392: if (man->last->type == ROFFT_ROOT)
1.19 kristaps 393: break;
1.91 schwarze 394: man_valid_post(man);
1.19 kristaps 395: }
1.3 kristaps 396:
1.75 schwarze 397: assert(man->last);
1.3 kristaps 398:
399: /*
1.82 schwarze 400: * Same here regarding whether we're back at the root.
1.3 kristaps 401: */
402:
1.101 schwarze 403: if (man->last->type != ROFFT_ROOT)
1.91 schwarze 404: man_valid_post(man);
1.19 kristaps 405: }
1.3 kristaps 406:
407:
1.91 schwarze 408: void
1.105 schwarze 409: man_macroend(struct roff_man *man)
1.19 kristaps 410: {
1.22 kristaps 411:
1.91 schwarze 412: man_unscope(man, man->first);
1.19 kristaps 413: }
1.3 kristaps 414:
1.60 kristaps 415: static int
1.105 schwarze 416: man_args(struct roff_man *man, int line, int *pos, char *buf, char **v)
1.60 kristaps 417: {
418: char *start;
419:
420: assert(*pos);
421: *v = start = buf + *pos;
422: assert(' ' != *start);
423:
424: if ('\0' == *start)
425: return(0);
426:
1.75 schwarze 427: *v = mandoc_getarg(man->parse, v, line, pos);
1.60 kristaps 428: return(1);
429: }
CVSweb