Annotation of mandoc/man_macro.c, Revision 1.107
1.107 ! schwarze 1: /* $Id: man_macro.c,v 1.106 2015/04/19 13:50:25 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.91 schwarze 262: man_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.91 schwarze 292: man_block_alloc(man, line, ppos, tok);
1.75 schwarze 293: n = man->last;
1.106 schwarze 294: if (n->tok == MAN_SH || n->tok == MAN_SS)
295: man->flags &= ~MAN_LITERAL;
296: n = roff_head_alloc(man, line, ppos, tok);
1.25 kristaps 297:
1.19 kristaps 298: /* Add line arguments. */
1.3 kristaps 299:
1.19 kristaps 300: for (;;) {
301: la = *pos;
1.75 schwarze 302: if ( ! man_args(man, line, pos, buf, &p))
1.19 kristaps 303: break;
1.107 ! schwarze 304: roff_word_alloc(man, line, la, p);
1.6 kristaps 305: }
306:
1.99 schwarze 307: /*
308: * For macros having optional next-line scope,
309: * keep the head open if there were no arguments.
310: * For `TP', always keep the head open.
311: */
1.19 kristaps 312:
1.99 schwarze 313: if (man_macros[tok].flags & MAN_SCOPED &&
314: (tok == MAN_TP || n == man->last)) {
315: man->flags |= MAN_BLINE;
316: return;
1.27 kristaps 317: }
1.99 schwarze 318:
319: /* Close out the head and open the body. */
320:
1.103 schwarze 321: man_unscope(man, n);
1.106 schwarze 322: roff_body_alloc(man, line, ppos, tok);
1.4 kristaps 323: }
324:
1.91 schwarze 325: void
1.19 kristaps 326: in_line_eoln(MACRO_PROT_ARGS)
1.3 kristaps 327: {
1.59 kristaps 328: int la;
1.19 kristaps 329: char *p;
1.102 schwarze 330: struct roff_node *n;
1.3 kristaps 331:
1.91 schwarze 332: man_elem_alloc(man, line, ppos, tok);
1.75 schwarze 333: n = man->last;
1.3 kristaps 334:
1.19 kristaps 335: for (;;) {
1.96 schwarze 336: if (buf[*pos] != '\0' && (tok == MAN_br ||
337: tok == MAN_fi || tok == MAN_nf)) {
338: mandoc_vmsg(MANDOCERR_ARG_SKIP,
339: man->parse, line, *pos, "%s %s",
1.97 schwarze 340: man_macronames[tok], buf + *pos);
341: break;
342: }
343: if (buf[*pos] != '\0' && man->last != n &&
344: (tok == MAN_PD || tok == MAN_ft || tok == MAN_sp)) {
345: mandoc_vmsg(MANDOCERR_ARG_EXCESS,
346: man->parse, line, *pos, "%s ... %s",
1.96 schwarze 347: man_macronames[tok], buf + *pos);
348: break;
349: }
1.19 kristaps 350: la = *pos;
1.75 schwarze 351: if ( ! man_args(man, line, pos, buf, &p))
1.19 kristaps 352: break;
1.90 schwarze 353: if (man_macros[tok].flags & MAN_JOIN &&
1.101 schwarze 354: man->last->type == ROFFT_TEXT)
1.107 ! schwarze 355: roff_word_append(man, p);
1.91 schwarze 356: else
1.107 ! schwarze 357: roff_word_alloc(man, line, la, p);
1.19 kristaps 358: }
1.78 schwarze 359:
360: /*
361: * Append MAN_EOS in case the last snipped argument
362: * ends with a dot, e.g. `.IR syslog (3).'
363: */
364:
365: if (n != man->last &&
1.80 schwarze 366: mandoc_eos(man->last->string, strlen(man->last->string)))
1.78 schwarze 367: man->last->flags |= MAN_EOS;
1.3 kristaps 368:
1.31 kristaps 369: /*
370: * If no arguments are specified and this is MAN_SCOPED (i.e.,
371: * next-line scoped), then set our mode to indicate that we're
372: * waiting for terms to load into our context.
373: */
374:
1.91 schwarze 375: if (n == man->last && man_macros[tok].flags & MAN_SCOPED) {
376: assert( ! (man_macros[tok].flags & MAN_NSCOPED));
1.75 schwarze 377: man->flags |= MAN_ELINE;
1.91 schwarze 378: return;
1.31 kristaps 379: }
1.64 kristaps 380:
1.101 schwarze 381: assert(man->last->type != ROFFT_ROOT);
1.105 schwarze 382: man->next = ROFF_NEXT_SIBLING;
1.82 schwarze 383:
1.19 kristaps 384: /*
1.31 kristaps 385: * Rewind our element scope. Note that when TH is pruned, we'll
386: * be back at the root, so make sure that we don't clobber as
387: * its sibling.
1.19 kristaps 388: */
1.3 kristaps 389:
1.75 schwarze 390: for ( ; man->last; man->last = man->last->parent) {
391: if (man->last == n)
1.19 kristaps 392: break;
1.101 schwarze 393: if (man->last->type == ROFFT_ROOT)
1.19 kristaps 394: break;
1.91 schwarze 395: man_valid_post(man);
1.19 kristaps 396: }
1.3 kristaps 397:
1.75 schwarze 398: assert(man->last);
1.3 kristaps 399:
400: /*
1.82 schwarze 401: * Same here regarding whether we're back at the root.
1.3 kristaps 402: */
403:
1.101 schwarze 404: if (man->last->type != ROFFT_ROOT)
1.91 schwarze 405: man_valid_post(man);
1.19 kristaps 406: }
1.3 kristaps 407:
408:
1.91 schwarze 409: void
1.105 schwarze 410: man_macroend(struct roff_man *man)
1.19 kristaps 411: {
1.22 kristaps 412:
1.91 schwarze 413: man_unscope(man, man->first);
1.19 kristaps 414: }
1.3 kristaps 415:
1.60 kristaps 416: static int
1.105 schwarze 417: man_args(struct roff_man *man, int line, int *pos, char *buf, char **v)
1.60 kristaps 418: {
419: char *start;
420:
421: assert(*pos);
422: *v = start = buf + *pos;
423: assert(' ' != *start);
424:
425: if ('\0' == *start)
426: return(0);
427:
1.75 schwarze 428: *v = mandoc_getarg(man->parse, v, line, pos);
1.60 kristaps 429: return(1);
430: }
CVSweb