Annotation of src/usr.bin/sed/compile.c, Revision 1.22
1.22 ! tedu 1: /* $OpenBSD: compile.c,v 1.21 2005/04/11 07:11:44 deraadt 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.22 ! tedu 38: static const char rcsid[] = "$OpenBSD: compile.c,v 1.21 2005/04/11 07:11:44 deraadt 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();
1.22 ! tedu 186: } else {
1.1 deraadt 187: cmd->a2 = 0;
1.22 ! tedu 188: }
! 189: } else {
1.1 deraadt 190: cmd->a1 = cmd->a2 = 0;
1.22 ! tedu 191: }
1.1 deraadt 192:
193: nonsel: /* Now parse the command */
194: if (!*p)
195: err(COMPILE, "command expected");
196: cmd->code = *p;
197: for (fp = cmd_fmts; fp->code; fp++)
198: if (fp->code == *p)
199: break;
200: if (!fp->code)
201: err(COMPILE, "invalid command code %c", *p);
202: if (naddr > fp->naddr)
203: err(COMPILE,
1.22 ! tedu 204: "command %c expects up to %d address(es), found %d",
! 205: *p, fp->naddr, naddr);
1.1 deraadt 206: switch (fp->args) {
207: case NONSEL: /* ! */
208: p++;
209: EATSPACE();
210: cmd->nonsel = ! cmd->nonsel;
211: goto nonsel;
212: case GROUP: /* { */
213: p++;
214: EATSPACE();
215: cmd->next = stack;
216: stack = cmd;
217: link = &cmd->u.c;
218: if (*p)
219: goto semicolon;
220: break;
221: case ENDGROUP:
222: /*
223: * Short-circuit command processing, since end of
224: * group is really just a noop.
225: */
226: cmd->nonsel = 1;
227: if (stack == 0)
228: err(COMPILE, "unexpected }");
229: cmd2 = stack;
230: stack = cmd2->next;
231: cmd2->next = cmd;
232: /*FALLTHROUGH*/
233: case EMPTY: /* d D g G h H l n N p P q x = \0 */
234: p++;
235: EATSPACE();
236: if (*p == ';') {
237: p++;
238: link = &cmd->next;
239: goto semicolon;
240: }
241: if (*p)
242: err(COMPILE,
243: "extra characters at the end of %c command", cmd->code);
244: break;
245: case TEXT: /* a c i */
246: p++;
247: EATSPACE();
248: if (*p != '\\')
1.22 ! tedu 249: err(COMPILE, "command %c expects \\ followed by"
! 250: " text", cmd->code);
1.1 deraadt 251: p++;
252: EATSPACE();
253: if (*p)
1.22 ! tedu 254: err(COMPILE, "extra characters after \\ at the"
! 255: " end of %c command", cmd->code);
1.1 deraadt 256: cmd->t = compile_text();
257: break;
258: case COMMENT: /* \0 # */
259: break;
260: case WFILE: /* w */
261: p++;
262: EATSPACE();
263: if (*p == '\0')
264: err(COMPILE, "filename expected");
1.16 deraadt 265: cmd->t = duptoeol(p, "w command", NULL);
1.1 deraadt 266: if (aflag)
267: cmd->u.fd = -1;
1.21 deraadt 268: else if ((cmd->u.fd = open(p,
1.1 deraadt 269: O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
270: DEFFILEMODE)) == -1)
1.12 jsyn 271: err(FATAL, "%s: %s", p, strerror(errno));
1.1 deraadt 272: break;
273: case RFILE: /* r */
274: p++;
275: EATSPACE();
1.16 deraadt 276: cmd->t = duptoeol(p, "read command", NULL);
1.1 deraadt 277: break;
278: case BRANCH: /* b t */
279: p++;
280: EATSPACE();
281: if (*p == '\0')
282: cmd->t = NULL;
283: else
1.16 deraadt 284: cmd->t = duptoeol(p, "branch", &p);
285: if (*p == ';') {
286: p++;
287: goto semicolon;
288: }
1.1 deraadt 289: break;
290: case LABEL: /* : */
291: p++;
292: EATSPACE();
1.16 deraadt 293: cmd->t = duptoeol(p, "label", &p);
294: if (strlen(cmd->t) == 0)
1.1 deraadt 295: err(COMPILE, "empty label");
296: enterlabel(cmd);
1.16 deraadt 297: if (*p == ';') {
298: p++;
299: goto semicolon;
300: }
1.1 deraadt 301: break;
302: case SUBST: /* s */
303: p++;
304: if (*p == '\0' || *p == '\\')
1.22 ! tedu 305: err(COMPILE, "substitute pattern can not be"
! 306: " delimited by newline or backslash");
1.1 deraadt 307: cmd->u.s = xmalloc(sizeof(struct s_subst));
308: p = compile_re(p, &cmd->u.s->re);
309: if (p == NULL)
310: err(COMPILE, "unterminated substitute pattern");
311: --p;
312: p = compile_subst(p, cmd->u.s);
313: p = compile_flags(p, cmd->u.s);
314: EATSPACE();
315: if (*p == ';') {
316: p++;
317: link = &cmd->next;
318: goto semicolon;
319: }
320: break;
321: case TR: /* y */
322: p++;
323: p = compile_tr(p, (char **)&cmd->u.y);
324: EATSPACE();
325: if (*p == ';') {
326: p++;
327: link = &cmd->next;
328: goto semicolon;
329: }
330: if (*p)
1.22 ! tedu 331: err(COMPILE, "extra text at the end of a"
! 332: " transform command");
1.1 deraadt 333: break;
334: }
335: }
336: }
337:
338: /*
339: * Get a delimited string. P points to the delimeter of the string; d points
340: * to a buffer area. Newline and delimiter escapes are processed; other
341: * escapes are ignored.
342: *
343: * Returns a pointer to the first character after the final delimiter or NULL
344: * in the case of a non-terminated string. The character array d is filled
345: * with the processed string.
346: */
347: static char *
1.15 deraadt 348: compile_delimited(char *p, char *d)
1.1 deraadt 349: {
350: char c;
351:
352: c = *p++;
353: if (c == '\0')
354: return (NULL);
355: else if (c == '\\')
356: err(COMPILE, "\\ can not be used as a string delimiter");
357: else if (c == '\n')
358: err(COMPILE, "newline can not be used as a string delimiter");
359: while (*p) {
360: if (*p == '[') {
361: if ((d = compile_ccl(&p, d)) == NULL)
362: err(COMPILE, "unbalanced brackets ([])");
363: continue;
364: } else if (*p == '\\' && p[1] == '[') {
365: *d++ = *p++;
1.22 ! tedu 366: } else if (*p == '\\' && p[1] == c) {
1.1 deraadt 367: p++;
1.22 ! tedu 368: } else if (*p == '\\' && p[1] == 'n') {
1.1 deraadt 369: *d++ = '\n';
370: p += 2;
371: continue;
1.22 ! tedu 372: } else if (*p == '\\' && p[1] == '\\') {
1.1 deraadt 373: *d++ = *p++;
1.22 ! tedu 374: } else if (*p == c) {
1.1 deraadt 375: *d = '\0';
376: return (p + 1);
377: }
378: *d++ = *p++;
379: }
380: return (NULL);
381: }
382:
383:
384: /* compile_ccl: expand a POSIX character class */
385: static char *
1.15 deraadt 386: compile_ccl(char **sp, char *t)
1.1 deraadt 387: {
388: int c, d;
389: char *s = *sp;
390:
391: *t++ = *s++;
392: if (*s == '^')
393: *t++ = *s++;
394: if (*s == ']')
395: *t++ = *s++;
396: for (; *s && (*t = *s) != ']'; s++, t++)
397: if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) {
398: *++t = *++s, t++, s++;
399: for (c = *s; (*t = *s) != ']' || c != d; s++, t++)
400: if ((c = *s) == '\0')
401: return NULL;
1.22 ! tedu 402: } else if (*s == '\\' && s[1] == 'n') {
! 403: *t = '\n';
! 404: s++;
! 405: }
! 406: if (*s == ']') {
! 407: *sp = ++s;
! 408: return (++t);
! 409: } else {
! 410: return (NULL);
! 411: }
1.1 deraadt 412: }
413:
414: /*
415: * Get a regular expression. P points to the delimiter of the regular
416: * expression; repp points to the address of a regexp pointer. Newline
417: * and delimiter escapes are processed; other escapes are ignored.
418: * Returns a pointer to the first character after the final delimiter
419: * or NULL in the case of a non terminated regular expression. The regexp
420: * pointer is set to the compiled regular expression.
421: * Cflags are passed to regcomp.
422: */
423: static char *
1.15 deraadt 424: compile_re(char *p, regex_t **repp)
1.1 deraadt 425: {
426: int eval;
427: char re[_POSIX2_LINE_MAX + 1];
428:
429: p = compile_delimited(p, re);
430: if (p && strlen(re) == 0) {
431: *repp = NULL;
432: return (p);
433: }
434: *repp = xmalloc(sizeof(regex_t));
1.6 millert 435: if (p && (eval = regcomp(*repp, re, 0)) != 0)
1.1 deraadt 436: err(COMPILE, "RE error: %s", strregerror(eval, *repp));
437: if (maxnsub < (*repp)->re_nsub)
438: maxnsub = (*repp)->re_nsub;
439: return (p);
440: }
441:
442: /*
443: * Compile the substitution string of a regular expression and set res to
444: * point to a saved copy of it. Nsub is the number of parenthesized regular
445: * expressions.
446: */
447: static char *
1.15 deraadt 448: compile_subst(char *p, struct s_subst *s)
1.1 deraadt 449: {
450: static char lbuf[_POSIX2_LINE_MAX + 1];
451: int asize, ref, size;
452: char c, *text, *op, *sp;
1.19 otto 453: int sawesc = 0;
1.1 deraadt 454:
455: c = *p++; /* Terminator character */
456: if (c == '\0')
457: return (NULL);
458:
459: s->maxbref = 0;
460: s->linenum = linenum;
461: asize = 2 * _POSIX2_LINE_MAX + 1;
462: text = xmalloc(asize);
463: size = 0;
464: do {
465: op = sp = text + size;
466: for (; *p; p++) {
1.19 otto 467: if (*p == '\\' || sawesc) {
468: /*
469: * If this is a continuation from the last
470: * buffer, we won't have a character to
471: * skip over.
472: */
473: if (sawesc)
474: sawesc = 0;
475: else
476: p++;
477:
478: if (*p == '\0') {
479: /*
480: * This escaped character is continued
481: * in the next part of the line. Note
482: * this fact, then cause the loop to
483: * exit w/ normal EOL case and reenter
484: * above with the new buffer.
485: */
486: sawesc = 1;
487: p--;
488: continue;
489: } else if (strchr("123456789", *p) != NULL) {
1.1 deraadt 490: *sp++ = '\\';
491: ref = *p - '0';
492: if (s->re != NULL &&
493: ref > s->re->re_nsub)
494: err(COMPILE,
495: "\\%c not defined in the RE", *p);
496: if (s->maxbref < ref)
497: s->maxbref = ref;
498: } else if (*p == '&' || *p == '\\')
499: *sp++ = '\\';
500: } else if (*p == c) {
501: p++;
502: *sp++ = '\0';
503: size += sp - op;
504: s->new = xrealloc(text, size);
505: return (p);
506: } else if (*p == '\n') {
507: err(COMPILE,
508: "unescaped newline inside substitute pattern");
509: /* NOTREACHED */
510: }
511: *sp++ = *p;
512: }
513: size += sp - op;
514: if (asize - size < _POSIX2_LINE_MAX + 1) {
515: asize *= 2;
1.20 otto 516: text = xrealloc(text, asize);
1.1 deraadt 517: }
518: } while (cu_fgets(p = lbuf, sizeof(lbuf)));
519: err(COMPILE, "unterminated substitute in regular expression");
520: /* NOTREACHED */
521: }
522:
523: /*
524: * Compile the flags of the s command
525: */
526: static char *
1.15 deraadt 527: compile_flags(char *p, struct s_subst *s)
1.1 deraadt 528: {
529: int gn; /* True if we have seen g or n */
1.17 otto 530: long l;
1.1 deraadt 531: char wfile[_POSIX2_LINE_MAX + 1], *q;
532:
533: s->n = 1; /* Default */
534: s->p = 0;
535: s->wfile = NULL;
536: s->wfd = -1;
537: for (gn = 0;;) {
538: EATSPACE(); /* EXTENSION */
539: switch (*p) {
540: case 'g':
541: if (gn)
1.22 ! tedu 542: err(COMPILE, "more than one number or 'g' in"
! 543: " substitute flags");
1.1 deraadt 544: gn = 1;
545: s->n = 0;
546: break;
547: case '\0':
548: case '\n':
549: case ';':
550: return (p);
551: case 'p':
552: s->p = 1;
553: break;
554: case '1': case '2': case '3':
555: case '4': case '5': case '6':
556: case '7': case '8': case '9':
557: if (gn)
1.22 ! tedu 558: err(COMPILE, "more than one number or 'g' in"
! 559: " substitute flags");
1.1 deraadt 560: gn = 1;
1.17 otto 561: l = strtol(p, &p, 10);
562: if (l <= 0 || l >= INT_MAX)
563: err(COMPILE,
564: "number in substitute flags out of range");
565: s->n = (int)l;
566: continue;
1.1 deraadt 567: case 'w':
568: p++;
569: #ifdef HISTORIC_PRACTICE
570: if (*p != ' ') {
571: err(WARNING, "space missing before w wfile");
572: return (p);
573: }
574: #endif
575: EATSPACE();
576: q = wfile;
577: while (*p) {
578: if (*p == '\n')
579: break;
580: *q++ = *p++;
581: }
582: *q = '\0';
583: if (q == wfile)
584: err(COMPILE, "no wfile specified");
585: s->wfile = strdup(wfile);
586: if (!aflag && (s->wfd = open(wfile,
587: O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
588: DEFFILEMODE)) == -1)
1.12 jsyn 589: err(FATAL, "%s: %s", wfile, strerror(errno));
1.1 deraadt 590: return (p);
591: default:
592: err(COMPILE,
593: "bad flag in substitute command: '%c'", *p);
594: break;
595: }
596: p++;
597: }
598: }
599:
600: /*
601: * Compile a translation set of strings into a lookup table.
602: */
603: static char *
1.15 deraadt 604: compile_tr(char *p, char **transtab)
1.1 deraadt 605: {
606: int i;
607: char *lt, *op, *np;
608: char old[_POSIX2_LINE_MAX + 1];
609: char new[_POSIX2_LINE_MAX + 1];
610:
611: if (*p == '\0' || *p == '\\')
612: err(COMPILE,
613: "transform pattern can not be delimited by newline or backslash");
614: p = compile_delimited(p, old);
615: if (p == NULL) {
616: err(COMPILE, "unterminated transform source string");
617: return (NULL);
618: }
619: p = compile_delimited(--p, new);
620: if (p == NULL) {
621: err(COMPILE, "unterminated transform target string");
622: return (NULL);
623: }
624: EATSPACE();
625: if (strlen(new) != strlen(old)) {
626: err(COMPILE, "transform strings are not the same length");
627: return (NULL);
628: }
629: /* We assume characters are 8 bits */
1.18 deraadt 630: lt = xmalloc(UCHAR_MAX + 1);
1.1 deraadt 631: for (i = 0; i <= UCHAR_MAX; i++)
632: lt[i] = (char)i;
633: for (op = old, np = new; *op; op++, np++)
634: lt[(u_char)*op] = *np;
635: *transtab = lt;
636: return (p);
637: }
638:
639: /*
1.7 deraadt 640: * Compile the text following an a, c, or i command.
1.1 deraadt 641: */
642: static char *
1.15 deraadt 643: compile_text(void)
1.1 deraadt 644: {
1.4 deraadt 645: int asize, esc_nl, size;
1.1 deraadt 646: char *text, *p, *op, *s;
647: char lbuf[_POSIX2_LINE_MAX + 1];
648:
649: asize = 2 * _POSIX2_LINE_MAX + 1;
650: text = xmalloc(asize);
651: size = 0;
652: while (cu_fgets(lbuf, sizeof(lbuf))) {
653: op = s = text + size;
654: p = lbuf;
655: EATSPACE();
1.4 deraadt 656: for (esc_nl = 0; *p != '\0'; p++) {
657: if (*p == '\\' && p[1] != '\0' && *++p == '\n')
658: esc_nl = 1;
1.1 deraadt 659: *s++ = *p;
660: }
661: size += s - op;
1.4 deraadt 662: if (!esc_nl) {
1.1 deraadt 663: *s = '\0';
664: break;
665: }
666: if (asize - size < _POSIX2_LINE_MAX + 1) {
667: asize *= 2;
668: text = xmalloc(asize);
669: }
670: }
1.8 brian 671: text[size] = '\0';
1.1 deraadt 672: return (xrealloc(text, size + 1));
673: }
674:
675: /*
676: * Get an address and return a pointer to the first character after
677: * it. Fill the structure pointed to according to the address.
678: */
679: static char *
1.15 deraadt 680: compile_addr(char *p, struct s_addr *a)
1.1 deraadt 681: {
682: char *end;
683:
684: switch (*p) {
685: case '\\': /* Context address */
686: ++p;
687: /* FALLTHROUGH */
688: case '/': /* Context address */
689: p = compile_re(p, &a->u.r);
690: if (p == NULL)
691: err(COMPILE, "unterminated regular expression");
692: a->type = AT_RE;
693: return (p);
694:
695: case '$': /* Last line */
696: a->type = AT_LAST;
697: return (p + 1);
698: /* Line number */
1.21 deraadt 699: case '0': case '1': case '2': case '3': case '4':
1.1 deraadt 700: case '5': case '6': case '7': case '8': case '9':
701: a->type = AT_LINE;
1.3 millert 702: a->u.l = strtoul(p, &end, 10);
1.1 deraadt 703: return (end);
704: default:
705: err(COMPILE, "expected context address");
706: return (NULL);
707: }
708: }
709:
710: /*
711: * duptoeol --
712: * Return a copy of all the characters up to \n or \0.
713: */
714: static char *
1.16 deraadt 715: duptoeol(char *s, char *ctype, char **semi)
1.1 deraadt 716: {
717: size_t len;
718: int ws;
719: char *start;
720:
721: ws = 0;
1.16 deraadt 722: if (semi) {
723: for (start = s; *s != '\0' && *s != '\n' && *s != ';'; ++s)
724: ws = isspace(*s);
725: } else {
726: for (start = s; *s != '\0' && *s != '\n'; ++s)
727: ws = isspace(*s);
728: *s = '\0';
729: }
1.1 deraadt 730: if (ws)
731: err(WARNING, "whitespace after %s", ctype);
732: len = s - start + 1;
1.16 deraadt 733: if (semi)
734: *semi = s;
735: s = xmalloc(len);
736: strlcpy(s, start, len);
737: return (s);
1.1 deraadt 738: }
739:
740: /*
741: * Convert goto label names to addresses, and count a and r commands, in
742: * the given subset of the script. Free the memory used by labels in b
743: * and t commands (but not by :).
744: *
745: * TODO: Remove } nodes
746: */
747: static void
1.15 deraadt 748: fixuplabel(struct s_command *cp, struct s_command *end)
1.1 deraadt 749: {
750:
751: for (; cp != end; cp = cp->next)
752: switch (cp->code) {
753: case 'a':
754: case 'r':
755: appendnum++;
756: break;
757: case 'b':
758: case 't':
759: /* Resolve branch target. */
760: if (cp->t == NULL) {
761: cp->u.c = NULL;
762: break;
763: }
764: if ((cp->u.c = findlabel(cp->t)) == NULL)
765: err(COMPILE2, "undefined label '%s'", cp->t);
766: free(cp->t);
767: break;
768: case '{':
769: /* Do interior commands. */
770: fixuplabel(cp->u.c, cp->next);
771: break;
772: }
773: }
774:
775: /*
776: * Associate the given command label for later lookup.
777: */
778: static void
1.15 deraadt 779: enterlabel(struct s_command *cp)
1.1 deraadt 780: {
1.10 mpech 781: struct labhash **lhp, *lh;
782: u_char *p;
783: u_int h, c;
1.1 deraadt 784:
785: for (h = 0, p = (u_char *)cp->t; (c = *p) != 0; p++)
786: h = (h << 5) + h + c;
787: lhp = &labels[h & LHMASK];
788: for (lh = *lhp; lh != NULL; lh = lh->lh_next)
789: if (lh->lh_hash == h && strcmp(cp->t, lh->lh_cmd->t) == 0)
790: err(COMPILE2, "duplicate label '%s'", cp->t);
791: lh = xmalloc(sizeof *lh);
792: lh->lh_next = *lhp;
793: lh->lh_hash = h;
794: lh->lh_cmd = cp;
795: lh->lh_ref = 0;
796: *lhp = lh;
797: }
798:
799: /*
800: * Find the label contained in the command l in the command linked
801: * list cp. L is excluded from the search. Return NULL if not found.
802: */
803: static struct s_command *
1.15 deraadt 804: findlabel(char *name)
1.1 deraadt 805: {
1.10 mpech 806: struct labhash *lh;
807: u_char *p;
808: u_int h, c;
1.1 deraadt 809:
810: for (h = 0, p = (u_char *)name; (c = *p) != 0; p++)
811: h = (h << 5) + h + c;
812: for (lh = labels[h & LHMASK]; lh != NULL; lh = lh->lh_next) {
813: if (lh->lh_hash == h && strcmp(name, lh->lh_cmd->t) == 0) {
814: lh->lh_ref = 1;
815: return (lh->lh_cmd);
816: }
817: }
818: return (NULL);
819: }
820:
1.21 deraadt 821: /*
1.1 deraadt 822: * Warn about any unused labels. As a side effect, release the label hash
823: * table space.
824: */
825: static void
1.15 deraadt 826: uselabel(void)
1.1 deraadt 827: {
1.10 mpech 828: struct labhash *lh, *next;
829: int i;
1.1 deraadt 830:
831: for (i = 0; i < LHSZ; i++) {
832: for (lh = labels[i]; lh != NULL; lh = next) {
833: next = lh->lh_next;
834: if (!lh->lh_ref)
835: err(WARNING, "unused label '%s'",
836: lh->lh_cmd->t);
837: free(lh);
838: }
839: }
840: }