Annotation of src/usr.bin/sed/compile.c, Revision 1.18
1.18 ! deraadt 1: /* $OpenBSD: compile.c,v 1.17 2004/02/17 16:13:33 otto Exp $ */
1.2 deraadt 2:
1.1 deraadt 3: /*-
4: * Copyright (c) 1992 Diomidis Spinellis.
5: * Copyright (c) 1992, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * This code is derived from software contributed to Berkeley by
9: * Diomidis Spinellis of Imperial College, University of London.
10: *
11: * Redistribution and use in source and binary forms, with or without
12: * modification, are permitted provided that the following conditions
13: * are met:
14: * 1. Redistributions of source code must retain the above copyright
15: * notice, this list of conditions and the following disclaimer.
16: * 2. Redistributions in binary form must reproduce the above copyright
17: * notice, this list of conditions and the following disclaimer in the
18: * documentation and/or other materials provided with the distribution.
1.14 millert 19: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
36: #ifndef lint
1.9 millert 37: /* from: static char sccsid[] = "@(#)compile.c 8.2 (Berkeley) 4/28/95"; */
1.18 ! deraadt 38: static char *rcsid = "$OpenBSD: compile.c,v 1.17 2004/02/17 16:13:33 otto Exp $";
1.1 deraadt 39: #endif /* not lint */
40:
41: #include <sys/types.h>
42: #include <sys/stat.h>
43:
44: #include <ctype.h>
45: #include <errno.h>
46: #include <fcntl.h>
47: #include <limits.h>
48: #include <regex.h>
49: #include <stdio.h>
50: #include <stdlib.h>
51: #include <string.h>
52:
53: #include "defs.h"
54: #include "extern.h"
55:
56: #define LHSZ 128
57: #define LHMASK (LHSZ - 1)
58: static struct labhash {
59: struct labhash *lh_next;
60: u_int lh_hash;
61: struct s_command *lh_cmd;
62: int lh_ref;
63: } *labels[LHSZ];
64:
1.11 millert 65: static char *compile_addr(char *, struct s_addr *);
66: static char *compile_ccl(char **, char *);
67: static char *compile_delimited(char *, char *);
68: static char *compile_flags(char *, struct s_subst *);
69: static char *compile_re(char *, regex_t **);
70: static char *compile_subst(char *, struct s_subst *);
71: static char *compile_text(void);
72: static char *compile_tr(char *, char **);
1.1 deraadt 73: static struct s_command
1.11 millert 74: **compile_stream(struct s_command **);
1.16 deraadt 75: static char *duptoeol(char *, char *, char **);
1.11 millert 76: static void enterlabel(struct s_command *);
1.1 deraadt 77: static struct s_command
1.11 millert 78: *findlabel(char *);
79: static void fixuplabel(struct s_command *, struct s_command *);
80: static void uselabel(void);
1.1 deraadt 81:
82: /*
83: * Command specification. This is used to drive the command parser.
84: */
85: struct s_format {
86: char code; /* Command code */
87: int naddr; /* Number of address args */
88: enum e_args args; /* Argument type */
89: };
90:
91: static struct s_format cmd_fmts[] = {
92: {'{', 2, GROUP},
93: {'}', 0, ENDGROUP},
94: {'a', 1, TEXT},
95: {'b', 2, BRANCH},
96: {'c', 2, TEXT},
97: {'d', 2, EMPTY},
98: {'D', 2, EMPTY},
99: {'g', 2, EMPTY},
100: {'G', 2, EMPTY},
101: {'h', 2, EMPTY},
102: {'H', 2, EMPTY},
103: {'i', 1, TEXT},
104: {'l', 2, EMPTY},
105: {'n', 2, EMPTY},
106: {'N', 2, EMPTY},
107: {'p', 2, EMPTY},
108: {'P', 2, EMPTY},
109: {'q', 1, EMPTY},
110: {'r', 1, RFILE},
111: {'s', 2, SUBST},
112: {'t', 2, BRANCH},
113: {'w', 2, WFILE},
114: {'x', 2, EMPTY},
115: {'y', 2, TR},
116: {'!', 2, NONSEL},
117: {':', 0, LABEL},
118: {'#', 0, COMMENT},
119: {'=', 1, EMPTY},
120: {'\0', 0, COMMENT},
121: };
122:
123: /* The compiled program. */
124: struct s_command *prog;
125:
126: /*
127: * Compile the program into prog.
128: * Initialise appends.
129: */
130: void
1.15 deraadt 131: compile(void)
1.1 deraadt 132: {
133: *compile_stream(&prog) = NULL;
134: fixuplabel(prog, NULL);
135: uselabel();
136: appends = xmalloc(sizeof(struct s_appends) * appendnum);
137: match = xmalloc((maxnsub + 1) * sizeof(regmatch_t));
138: }
139:
140: #define EATSPACE() do { \
141: if (p) \
142: while (*p && isascii(*p) && isspace(*p)) \
143: p++; \
144: } while (0)
145:
146: static struct s_command **
1.15 deraadt 147: compile_stream(struct s_command **link)
1.1 deraadt 148: {
1.10 mpech 149: char *p;
1.1 deraadt 150: static char lbuf[_POSIX2_LINE_MAX + 1]; /* To save stack */
151: struct s_command *cmd, *cmd2, *stack;
152: struct s_format *fp;
153: int naddr; /* Number of addresses */
154:
155: stack = 0;
156: for (;;) {
157: if ((p = cu_fgets(lbuf, sizeof(lbuf))) == NULL) {
158: if (stack != 0)
159: err(COMPILE, "unexpected EOF (pending }'s)");
160: return (link);
161: }
162:
163: semicolon: EATSPACE();
164: if (p && (*p == '#' || *p == '\0'))
165: continue;
166: *link = cmd = xmalloc(sizeof(struct s_command));
167: link = &cmd->next;
168: cmd->nonsel = cmd->inrange = 0;
169: /* First parse the addresses */
170: naddr = 0;
171:
172: /* Valid characters to start an address */
173: #define addrchar(c) (strchr("0123456789/\\$", (c)))
174: if (addrchar(*p)) {
175: naddr++;
176: cmd->a1 = xmalloc(sizeof(struct s_addr));
177: p = compile_addr(p, cmd->a1);
178: EATSPACE(); /* EXTENSION */
179: if (*p == ',') {
180: p++;
181: EATSPACE(); /* EXTENSION */
182: naddr++;
183: cmd->a2 = xmalloc(sizeof(struct s_addr));
184: p = compile_addr(p, cmd->a2);
185: EATSPACE();
186: } else
187: cmd->a2 = 0;
188: } else
189: cmd->a1 = cmd->a2 = 0;
190:
191: nonsel: /* Now parse the command */
192: if (!*p)
193: err(COMPILE, "command expected");
194: cmd->code = *p;
195: for (fp = cmd_fmts; fp->code; fp++)
196: if (fp->code == *p)
197: break;
198: if (!fp->code)
199: err(COMPILE, "invalid command code %c", *p);
200: if (naddr > fp->naddr)
201: err(COMPILE,
202: "command %c expects up to %d address(es), found %d", *p, fp->naddr, naddr);
203: switch (fp->args) {
204: case NONSEL: /* ! */
205: p++;
206: EATSPACE();
207: cmd->nonsel = ! cmd->nonsel;
208: goto nonsel;
209: case GROUP: /* { */
210: p++;
211: EATSPACE();
212: cmd->next = stack;
213: stack = cmd;
214: link = &cmd->u.c;
215: if (*p)
216: goto semicolon;
217: break;
218: case ENDGROUP:
219: /*
220: * Short-circuit command processing, since end of
221: * group is really just a noop.
222: */
223: cmd->nonsel = 1;
224: if (stack == 0)
225: err(COMPILE, "unexpected }");
226: cmd2 = stack;
227: stack = cmd2->next;
228: cmd2->next = cmd;
229: /*FALLTHROUGH*/
230: case EMPTY: /* d D g G h H l n N p P q x = \0 */
231: p++;
232: EATSPACE();
233: if (*p == ';') {
234: p++;
235: link = &cmd->next;
236: goto semicolon;
237: }
238: if (*p)
239: err(COMPILE,
240: "extra characters at the end of %c command", cmd->code);
241: break;
242: case TEXT: /* a c i */
243: p++;
244: EATSPACE();
245: if (*p != '\\')
246: err(COMPILE,
247: "command %c expects \\ followed by text", cmd->code);
248: p++;
249: EATSPACE();
250: if (*p)
251: err(COMPILE,
252: "extra characters after \\ at the end of %c command", cmd->code);
253: cmd->t = compile_text();
254: break;
255: case COMMENT: /* \0 # */
256: break;
257: case WFILE: /* w */
258: p++;
259: EATSPACE();
260: if (*p == '\0')
261: err(COMPILE, "filename expected");
1.16 deraadt 262: cmd->t = duptoeol(p, "w command", NULL);
1.1 deraadt 263: if (aflag)
264: cmd->u.fd = -1;
265: else if ((cmd->u.fd = open(p,
266: O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
267: DEFFILEMODE)) == -1)
1.12 jsyn 268: err(FATAL, "%s: %s", p, strerror(errno));
1.1 deraadt 269: break;
270: case RFILE: /* r */
271: p++;
272: EATSPACE();
1.16 deraadt 273: cmd->t = duptoeol(p, "read command", NULL);
1.1 deraadt 274: break;
275: case BRANCH: /* b t */
276: p++;
277: EATSPACE();
278: if (*p == '\0')
279: cmd->t = NULL;
280: else
1.16 deraadt 281: cmd->t = duptoeol(p, "branch", &p);
282: if (*p == ';') {
283: p++;
284: goto semicolon;
285: }
1.1 deraadt 286: break;
287: case LABEL: /* : */
288: p++;
289: EATSPACE();
1.16 deraadt 290: cmd->t = duptoeol(p, "label", &p);
291: if (strlen(cmd->t) == 0)
1.1 deraadt 292: err(COMPILE, "empty label");
293: enterlabel(cmd);
1.16 deraadt 294: if (*p == ';') {
295: p++;
296: goto semicolon;
297: }
1.1 deraadt 298: break;
299: case SUBST: /* s */
300: p++;
301: if (*p == '\0' || *p == '\\')
302: err(COMPILE,
303: "substitute pattern can not be delimited by newline or backslash");
304: cmd->u.s = xmalloc(sizeof(struct s_subst));
305: p = compile_re(p, &cmd->u.s->re);
306: if (p == NULL)
307: err(COMPILE, "unterminated substitute pattern");
308: --p;
309: p = compile_subst(p, cmd->u.s);
310: p = compile_flags(p, cmd->u.s);
311: EATSPACE();
312: if (*p == ';') {
313: p++;
314: link = &cmd->next;
315: goto semicolon;
316: }
317: break;
318: case TR: /* y */
319: p++;
320: p = compile_tr(p, (char **)&cmd->u.y);
321: EATSPACE();
322: if (*p == ';') {
323: p++;
324: link = &cmd->next;
325: goto semicolon;
326: }
327: if (*p)
328: err(COMPILE,
329: "extra text at the end of a transform command");
330: break;
331: }
332: }
333: }
334:
335: /*
336: * Get a delimited string. P points to the delimeter of the string; d points
337: * to a buffer area. Newline and delimiter escapes are processed; other
338: * escapes are ignored.
339: *
340: * Returns a pointer to the first character after the final delimiter or NULL
341: * in the case of a non-terminated string. The character array d is filled
342: * with the processed string.
343: */
344: static char *
1.15 deraadt 345: compile_delimited(char *p, char *d)
1.1 deraadt 346: {
347: char c;
348:
349: c = *p++;
350: if (c == '\0')
351: return (NULL);
352: else if (c == '\\')
353: err(COMPILE, "\\ can not be used as a string delimiter");
354: else if (c == '\n')
355: err(COMPILE, "newline can not be used as a string delimiter");
356: while (*p) {
357: if (*p == '[') {
358: if ((d = compile_ccl(&p, d)) == NULL)
359: err(COMPILE, "unbalanced brackets ([])");
360: continue;
361: } else if (*p == '\\' && p[1] == '[') {
362: *d++ = *p++;
363: } else if (*p == '\\' && p[1] == c)
364: p++;
365: else if (*p == '\\' && p[1] == 'n') {
366: *d++ = '\n';
367: p += 2;
368: continue;
369: } else if (*p == '\\' && p[1] == '\\')
370: *d++ = *p++;
371: else if (*p == c) {
372: *d = '\0';
373: return (p + 1);
374: }
375: *d++ = *p++;
376: }
377: return (NULL);
378: }
379:
380:
381: /* compile_ccl: expand a POSIX character class */
382: static char *
1.15 deraadt 383: compile_ccl(char **sp, char *t)
1.1 deraadt 384: {
385: int c, d;
386: char *s = *sp;
387:
388: *t++ = *s++;
389: if (*s == '^')
390: *t++ = *s++;
391: if (*s == ']')
392: *t++ = *s++;
393: for (; *s && (*t = *s) != ']'; s++, t++)
394: if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) {
395: *++t = *++s, t++, s++;
396: for (c = *s; (*t = *s) != ']' || c != d; s++, t++)
397: if ((c = *s) == '\0')
398: return NULL;
399: } else if (*s == '\\' && s[1] == 'n')
400: *t = '\n', s++;
401: return (*s == ']') ? *sp = ++s, ++t : NULL;
402: }
403:
404: /*
405: * Get a regular expression. P points to the delimiter of the regular
406: * expression; repp points to the address of a regexp pointer. Newline
407: * and delimiter escapes are processed; other escapes are ignored.
408: * Returns a pointer to the first character after the final delimiter
409: * or NULL in the case of a non terminated regular expression. The regexp
410: * pointer is set to the compiled regular expression.
411: * Cflags are passed to regcomp.
412: */
413: static char *
1.15 deraadt 414: compile_re(char *p, regex_t **repp)
1.1 deraadt 415: {
416: int eval;
417: char re[_POSIX2_LINE_MAX + 1];
418:
419: p = compile_delimited(p, re);
420: if (p && strlen(re) == 0) {
421: *repp = NULL;
422: return (p);
423: }
424: *repp = xmalloc(sizeof(regex_t));
1.6 millert 425: if (p && (eval = regcomp(*repp, re, 0)) != 0)
1.1 deraadt 426: err(COMPILE, "RE error: %s", strregerror(eval, *repp));
427: if (maxnsub < (*repp)->re_nsub)
428: maxnsub = (*repp)->re_nsub;
429: return (p);
430: }
431:
432: /*
433: * Compile the substitution string of a regular expression and set res to
434: * point to a saved copy of it. Nsub is the number of parenthesized regular
435: * expressions.
436: */
437: static char *
1.15 deraadt 438: compile_subst(char *p, struct s_subst *s)
1.1 deraadt 439: {
440: static char lbuf[_POSIX2_LINE_MAX + 1];
441: int asize, ref, size;
442: char c, *text, *op, *sp;
443:
444: c = *p++; /* Terminator character */
445: if (c == '\0')
446: return (NULL);
447:
448: s->maxbref = 0;
449: s->linenum = linenum;
450: asize = 2 * _POSIX2_LINE_MAX + 1;
451: text = xmalloc(asize);
452: size = 0;
453: do {
454: op = sp = text + size;
455: for (; *p; p++) {
456: if (*p == '\\') {
457: p++;
458: if (strchr("123456789", *p) != NULL) {
459: *sp++ = '\\';
460: ref = *p - '0';
461: if (s->re != NULL &&
462: ref > s->re->re_nsub)
463: err(COMPILE,
464: "\\%c not defined in the RE", *p);
465: if (s->maxbref < ref)
466: s->maxbref = ref;
467: } else if (*p == '&' || *p == '\\')
468: *sp++ = '\\';
469: } else if (*p == c) {
470: p++;
471: *sp++ = '\0';
472: size += sp - op;
473: s->new = xrealloc(text, size);
474: return (p);
475: } else if (*p == '\n') {
476: err(COMPILE,
477: "unescaped newline inside substitute pattern");
478: /* NOTREACHED */
479: }
480: *sp++ = *p;
481: }
482: size += sp - op;
483: if (asize - size < _POSIX2_LINE_MAX + 1) {
484: asize *= 2;
485: text = xmalloc(asize);
486: }
487: } while (cu_fgets(p = lbuf, sizeof(lbuf)));
488: err(COMPILE, "unterminated substitute in regular expression");
489: /* NOTREACHED */
490: }
491:
492: /*
493: * Compile the flags of the s command
494: */
495: static char *
1.15 deraadt 496: compile_flags(char *p, struct s_subst *s)
1.1 deraadt 497: {
498: int gn; /* True if we have seen g or n */
1.17 otto 499: long l;
1.1 deraadt 500: char wfile[_POSIX2_LINE_MAX + 1], *q;
501:
502: s->n = 1; /* Default */
503: s->p = 0;
504: s->wfile = NULL;
505: s->wfd = -1;
506: for (gn = 0;;) {
507: EATSPACE(); /* EXTENSION */
508: switch (*p) {
509: case 'g':
510: if (gn)
511: err(COMPILE,
512: "more than one number or 'g' in substitute flags");
513: gn = 1;
514: s->n = 0;
515: break;
516: case '\0':
517: case '\n':
518: case ';':
519: return (p);
520: case 'p':
521: s->p = 1;
522: break;
523: case '1': case '2': case '3':
524: case '4': case '5': case '6':
525: case '7': case '8': case '9':
526: if (gn)
527: err(COMPILE,
528: "more than one number or 'g' in substitute flags");
529: gn = 1;
1.17 otto 530: l = strtol(p, &p, 10);
531: if (l <= 0 || l >= INT_MAX)
532: err(COMPILE,
533: "number in substitute flags out of range");
534: s->n = (int)l;
535: continue;
1.1 deraadt 536: case 'w':
537: p++;
538: #ifdef HISTORIC_PRACTICE
539: if (*p != ' ') {
540: err(WARNING, "space missing before w wfile");
541: return (p);
542: }
543: #endif
544: EATSPACE();
545: q = wfile;
546: while (*p) {
547: if (*p == '\n')
548: break;
549: *q++ = *p++;
550: }
551: *q = '\0';
552: if (q == wfile)
553: err(COMPILE, "no wfile specified");
554: s->wfile = strdup(wfile);
555: if (!aflag && (s->wfd = open(wfile,
556: O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
557: DEFFILEMODE)) == -1)
1.12 jsyn 558: err(FATAL, "%s: %s", wfile, strerror(errno));
1.1 deraadt 559: return (p);
560: default:
561: err(COMPILE,
562: "bad flag in substitute command: '%c'", *p);
563: break;
564: }
565: p++;
566: }
567: }
568:
569: /*
570: * Compile a translation set of strings into a lookup table.
571: */
572: static char *
1.15 deraadt 573: compile_tr(char *p, char **transtab)
1.1 deraadt 574: {
575: int i;
576: char *lt, *op, *np;
577: char old[_POSIX2_LINE_MAX + 1];
578: char new[_POSIX2_LINE_MAX + 1];
579:
580: if (*p == '\0' || *p == '\\')
581: err(COMPILE,
582: "transform pattern can not be delimited by newline or backslash");
583: p = compile_delimited(p, old);
584: if (p == NULL) {
585: err(COMPILE, "unterminated transform source string");
586: return (NULL);
587: }
588: p = compile_delimited(--p, new);
589: if (p == NULL) {
590: err(COMPILE, "unterminated transform target string");
591: return (NULL);
592: }
593: EATSPACE();
594: if (strlen(new) != strlen(old)) {
595: err(COMPILE, "transform strings are not the same length");
596: return (NULL);
597: }
598: /* We assume characters are 8 bits */
1.18 ! deraadt 599: lt = xmalloc(UCHAR_MAX + 1);
1.1 deraadt 600: for (i = 0; i <= UCHAR_MAX; i++)
601: lt[i] = (char)i;
602: for (op = old, np = new; *op; op++, np++)
603: lt[(u_char)*op] = *np;
604: *transtab = lt;
605: return (p);
606: }
607:
608: /*
1.7 deraadt 609: * Compile the text following an a, c, or i command.
1.1 deraadt 610: */
611: static char *
1.15 deraadt 612: compile_text(void)
1.1 deraadt 613: {
1.4 deraadt 614: int asize, esc_nl, size;
1.1 deraadt 615: char *text, *p, *op, *s;
616: char lbuf[_POSIX2_LINE_MAX + 1];
617:
618: asize = 2 * _POSIX2_LINE_MAX + 1;
619: text = xmalloc(asize);
620: size = 0;
621: while (cu_fgets(lbuf, sizeof(lbuf))) {
622: op = s = text + size;
623: p = lbuf;
624: EATSPACE();
1.4 deraadt 625: for (esc_nl = 0; *p != '\0'; p++) {
626: if (*p == '\\' && p[1] != '\0' && *++p == '\n')
627: esc_nl = 1;
1.1 deraadt 628: *s++ = *p;
629: }
630: size += s - op;
1.4 deraadt 631: if (!esc_nl) {
1.1 deraadt 632: *s = '\0';
633: break;
634: }
635: if (asize - size < _POSIX2_LINE_MAX + 1) {
636: asize *= 2;
637: text = xmalloc(asize);
638: }
639: }
1.8 brian 640: text[size] = '\0';
1.1 deraadt 641: return (xrealloc(text, size + 1));
642: }
643:
644: /*
645: * Get an address and return a pointer to the first character after
646: * it. Fill the structure pointed to according to the address.
647: */
648: static char *
1.15 deraadt 649: compile_addr(char *p, struct s_addr *a)
1.1 deraadt 650: {
651: char *end;
652:
653: switch (*p) {
654: case '\\': /* Context address */
655: ++p;
656: /* FALLTHROUGH */
657: case '/': /* Context address */
658: p = compile_re(p, &a->u.r);
659: if (p == NULL)
660: err(COMPILE, "unterminated regular expression");
661: a->type = AT_RE;
662: return (p);
663:
664: case '$': /* Last line */
665: a->type = AT_LAST;
666: return (p + 1);
667: /* Line number */
668: case '0': case '1': case '2': case '3': case '4':
669: case '5': case '6': case '7': case '8': case '9':
670: a->type = AT_LINE;
1.3 millert 671: a->u.l = strtoul(p, &end, 10);
1.1 deraadt 672: return (end);
673: default:
674: err(COMPILE, "expected context address");
675: return (NULL);
676: }
677: }
678:
679: /*
680: * duptoeol --
681: * Return a copy of all the characters up to \n or \0.
682: */
683: static char *
1.16 deraadt 684: duptoeol(char *s, char *ctype, char **semi)
1.1 deraadt 685: {
686: size_t len;
687: int ws;
688: char *start;
689:
690: ws = 0;
1.16 deraadt 691: if (semi) {
692: for (start = s; *s != '\0' && *s != '\n' && *s != ';'; ++s)
693: ws = isspace(*s);
694: } else {
695: for (start = s; *s != '\0' && *s != '\n'; ++s)
696: ws = isspace(*s);
697: *s = '\0';
698: }
1.1 deraadt 699: if (ws)
700: err(WARNING, "whitespace after %s", ctype);
701: len = s - start + 1;
1.16 deraadt 702: if (semi)
703: *semi = s;
704: s = xmalloc(len);
705: strlcpy(s, start, len);
706: return (s);
1.1 deraadt 707: }
708:
709: /*
710: * Convert goto label names to addresses, and count a and r commands, in
711: * the given subset of the script. Free the memory used by labels in b
712: * and t commands (but not by :).
713: *
714: * TODO: Remove } nodes
715: */
716: static void
1.15 deraadt 717: fixuplabel(struct s_command *cp, struct s_command *end)
1.1 deraadt 718: {
719:
720: for (; cp != end; cp = cp->next)
721: switch (cp->code) {
722: case 'a':
723: case 'r':
724: appendnum++;
725: break;
726: case 'b':
727: case 't':
728: /* Resolve branch target. */
729: if (cp->t == NULL) {
730: cp->u.c = NULL;
731: break;
732: }
733: if ((cp->u.c = findlabel(cp->t)) == NULL)
734: err(COMPILE2, "undefined label '%s'", cp->t);
735: free(cp->t);
736: break;
737: case '{':
738: /* Do interior commands. */
739: fixuplabel(cp->u.c, cp->next);
740: break;
741: }
742: }
743:
744: /*
745: * Associate the given command label for later lookup.
746: */
747: static void
1.15 deraadt 748: enterlabel(struct s_command *cp)
1.1 deraadt 749: {
1.10 mpech 750: struct labhash **lhp, *lh;
751: u_char *p;
752: u_int h, c;
1.1 deraadt 753:
754: for (h = 0, p = (u_char *)cp->t; (c = *p) != 0; p++)
755: h = (h << 5) + h + c;
756: lhp = &labels[h & LHMASK];
757: for (lh = *lhp; lh != NULL; lh = lh->lh_next)
758: if (lh->lh_hash == h && strcmp(cp->t, lh->lh_cmd->t) == 0)
759: err(COMPILE2, "duplicate label '%s'", cp->t);
760: lh = xmalloc(sizeof *lh);
761: lh->lh_next = *lhp;
762: lh->lh_hash = h;
763: lh->lh_cmd = cp;
764: lh->lh_ref = 0;
765: *lhp = lh;
766: }
767:
768: /*
769: * Find the label contained in the command l in the command linked
770: * list cp. L is excluded from the search. Return NULL if not found.
771: */
772: static struct s_command *
1.15 deraadt 773: findlabel(char *name)
1.1 deraadt 774: {
1.10 mpech 775: struct labhash *lh;
776: u_char *p;
777: u_int h, c;
1.1 deraadt 778:
779: for (h = 0, p = (u_char *)name; (c = *p) != 0; p++)
780: h = (h << 5) + h + c;
781: for (lh = labels[h & LHMASK]; lh != NULL; lh = lh->lh_next) {
782: if (lh->lh_hash == h && strcmp(name, lh->lh_cmd->t) == 0) {
783: lh->lh_ref = 1;
784: return (lh->lh_cmd);
785: }
786: }
787: return (NULL);
788: }
789:
790: /*
791: * Warn about any unused labels. As a side effect, release the label hash
792: * table space.
793: */
794: static void
1.15 deraadt 795: uselabel(void)
1.1 deraadt 796: {
1.10 mpech 797: struct labhash *lh, *next;
798: int i;
1.1 deraadt 799:
800: for (i = 0; i < LHSZ; i++) {
801: for (lh = labels[i]; lh != NULL; lh = next) {
802: next = lh->lh_next;
803: if (!lh->lh_ref)
804: err(WARNING, "unused label '%s'",
805: lh->lh_cmd->t);
806: free(lh);
807: }
808: }
809: }