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