2 * Copyright (c) 1992 Diomidis Spinellis.
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Diomidis Spinellis of Imperial College, University of London.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static char sccsid[] = "@(#)compile.c 8.1 (Berkeley) 6/6/93";
42 #include <sys/types.h>
58 #define LHMASK (LHSZ - 1)
59 static struct labhash {
60 struct labhash *lh_next;
62 struct s_command *lh_cmd;
66 static char *compile_addr __P((char *, struct s_addr *));
67 static char *compile_delimited __P((char *, char *));
68 static char *compile_flags __P((char *, struct s_subst *));
69 static char *compile_re __P((char *, regex_t **));
70 static char *compile_subst __P((char *, struct s_subst *));
71 static char *compile_text __P((void));
72 static char *compile_tr __P((char *, char **));
73 static struct s_command
74 **compile_stream __P((char *, struct s_command **, char *));
75 static char *duptoeol __P((char *, char *));
76 static void enterlabel __P((struct s_command *));
77 static struct s_command
78 *findlabel __P((char *));
79 static void fixuplabel __P((struct s_command *, struct s_command *));
80 static void uselabel __P((void));
83 * Command specification. This is used to drive the command parser.
86 char code; /* Command code */
87 int naddr; /* Number of address args */
88 enum e_args args; /* Argument type */
91 static struct s_format cmd_fmts[] = {
122 /* The compiled program. */
123 struct s_command *prog;
126 * Compile the program into prog.
127 * Initialise appends.
132 *compile_stream(NULL, &prog, NULL) = NULL;
133 fixuplabel(prog, NULL);
135 appends = xmalloc(sizeof(struct s_appends) * appendnum);
136 match = xmalloc((maxnsub + 1) * sizeof(regmatch_t));
139 #define EATSPACE() do { \
141 while (*p && isascii(*p) && isspace(*p)) \
145 static struct s_command **
146 compile_stream(terminator, link, p)
148 struct s_command **link;
151 static char lbuf[_POSIX2_LINE_MAX + 1]; /* To save stack */
152 struct s_command *cmd, *cmd2;
154 int naddr; /* Number of addresses */
159 if ((p = cu_fgets(lbuf, sizeof(lbuf))) == NULL) {
160 if (terminator != NULL)
161 err(COMPILE, "unexpected EOF (pending }'s)");
165 semicolon: EATSPACE();
166 if (p && (*p == '#' || *p == '\0'))
169 if (terminator == NULL)
170 err(COMPILE, "unexpected }");
173 *link = cmd = xmalloc(sizeof(struct s_command));
175 cmd->nonsel = cmd->inrange = 0;
176 /* First parse the addresses */
178 cmd->a1 = cmd->a2 = NULL;
180 /* Valid characters to start an address */
181 #define addrchar(c) (strchr("0123456789/\\$", (c)))
184 cmd->a1 = xmalloc(sizeof(struct s_addr));
185 p = compile_addr(p, cmd->a1);
186 EATSPACE(); /* EXTENSION */
190 EATSPACE(); /* EXTENSION */
191 cmd->a2 = xmalloc(sizeof(struct s_addr));
192 p = compile_addr(p, cmd->a2);
196 nonsel: /* Now parse the command */
199 err(COMPILE, "command expected");
201 for (fp = cmd_fmts; fp->code; fp++)
205 err(COMPILE, "invalid command code %c", *p);
206 if (naddr > fp->naddr)
208 "command %c expects up to %d address(es), found %d", *p, fp->naddr, naddr);
211 cmd->nonsel = ! cmd->nonsel;
219 cmd2 = xmalloc(sizeof(struct s_command));
221 *compile_stream("}", &cmd->u.c, p) = cmd2;
225 case EMPTY: /* d D g G h H l n N p P q x = \0 */
235 "extra characters at the end of %c command", cmd->code);
237 case TEXT: /* a c i */
242 "command %c expects \\ followed by text", cmd->code);
247 "extra characters after \\ at the end of %c command", cmd->code);
248 cmd->t = compile_text();
250 case COMMENT: /* \0 # */
256 err(COMPILE, "filename expected");
257 cmd->t = duptoeol(p, "w command");
260 else if ((cmd->u.fd = open(p,
261 O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
263 err(FATAL, "%s: %s\n", p, strerror(errno));
269 err(COMPILE, "filename expected");
271 cmd->t = duptoeol(p, "read command");
273 case BRANCH: /* b t */
279 cmd->t = duptoeol(p, "branch");
284 cmd->t = duptoeol(p, "label");
286 err(COMPILE, "empty label");
291 if (*p == '\0' || *p == '\\')
293 "substitute pattern can not be delimited by newline or backslash");
294 cmd->u.s = xmalloc(sizeof(struct s_subst));
295 p = compile_re(p, &cmd->u.s->re);
297 err(COMPILE, "unterminated substitute pattern");
299 p = compile_subst(p, cmd->u.s);
300 p = compile_flags(p, cmd->u.s);
310 p = compile_tr(p, (char **)&cmd->u.y);
319 "extra text at the end of a transform command");
326 * Get a delimited string. P points to the delimeter of the string; d points
327 * to a buffer area. Newline and delimiter escapes are processed; other
328 * escapes are ignored.
330 * Returns a pointer to the first character after the final delimiter or NULL
331 * in the case of a non-terminated string. The character array d is filled
332 * with the processed string.
335 compile_delimited(p, d)
344 err(COMPILE, "\\ can not be used as a string delimiter");
346 err(COMPILE, "newline can not be used as a string delimiter");
348 if (*p == '\\' && p[1] == c)
350 else if (*p == '\\' && p[1] == 'n') {
354 } else if (*p == '\\' && p[1] == '\\')
366 * Get a regular expression. P points to the delimiter of the regular
367 * expression; repp points to the address of a regexp pointer. Newline
368 * and delimiter escapes are processed; other escapes are ignored.
369 * Returns a pointer to the first character after the final delimiter
370 * or NULL in the case of a non terminated regular expression. The regexp
371 * pointer is set to the compiled regular expression.
372 * Cflags are passed to regcomp.
380 char re[_POSIX2_LINE_MAX + 1];
382 p = compile_delimited(p, re);
383 if (p && strlen(re) == 0) {
387 *repp = xmalloc(sizeof(regex_t));
388 if (p && (eval = regcomp(*repp, re, 0)) != 0)
389 err(COMPILE, "RE error: %s", strregerror(eval, *repp));
390 if (maxnsub < (*repp)->re_nsub)
391 maxnsub = (*repp)->re_nsub;
396 * Compile the substitution string of a regular expression and set res to
397 * point to a saved copy of it. Nsub is the number of parenthesized regular
405 static char lbuf[_POSIX2_LINE_MAX + 1];
406 int asize, ref, size;
407 char c, *text, *op, *sp;
409 c = *p++; /* Terminator character */
414 s->linenum = linenum;
415 asize = 2 * _POSIX2_LINE_MAX + 1;
416 text = xmalloc(asize);
419 op = sp = text + size;
423 if (strchr("123456789", *p) != NULL) {
427 ref > s->re->re_nsub)
429 "\\%c not defined in the RE", *p);
430 if (s->maxbref < ref)
432 } else if (*p == '&' || *p == '\\')
434 } else if (*p == c) {
438 s->new = xrealloc(text, size);
440 } else if (*p == '\n') {
442 "unescaped newline inside substitute pattern");
448 if (asize - size < _POSIX2_LINE_MAX + 1) {
450 text = xmalloc(asize);
452 } while (cu_fgets(p = lbuf, sizeof(lbuf)));
453 err(COMPILE, "unterminated substitute in regular expression");
458 * Compile the flags of the s command
465 int gn; /* True if we have seen g or n */
466 char wfile[_POSIX2_LINE_MAX + 1], *q;
468 s->n = 1; /* Default */
473 EATSPACE(); /* EXTENSION */
478 "more than one number or 'g' in substitute flags");
489 case '1': case '2': case '3':
490 case '4': case '5': case '6':
491 case '7': case '8': case '9':
494 "more than one number or 'g' in substitute flags");
496 /* XXX Check for overflow */
497 s->n = (int)strtol(p, &p, 10);
501 #ifdef HISTORIC_PRACTICE
503 err(WARNING, "space missing before w wfile");
516 err(COMPILE, "no wfile specified");
517 s->wfile = strdup(wfile);
518 if (!aflag && (s->wfd = open(wfile,
519 O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
521 err(FATAL, "%s: %s\n", wfile, strerror(errno));
525 "bad flag in substitute command: '%c'", *p);
533 * Compile a translation set of strings into a lookup table.
536 compile_tr(p, transtab)
542 char old[_POSIX2_LINE_MAX + 1];
543 char new[_POSIX2_LINE_MAX + 1];
545 if (*p == '\0' || *p == '\\')
547 "transform pattern can not be delimited by newline or backslash");
548 p = compile_delimited(p, old);
550 err(COMPILE, "unterminated transform source string");
553 p = compile_delimited(--p, new);
555 err(COMPILE, "unterminated transform target string");
559 if (strlen(new) != strlen(old)) {
560 err(COMPILE, "transform strings are not the same length");
563 /* We assume characters are 8 bits */
564 lt = xmalloc(UCHAR_MAX);
565 for (i = 0; i <= UCHAR_MAX; i++)
567 for (op = old, np = new; *op; op++, np++)
568 lt[(u_char)*op] = *np;
574 * Compile the text following an a or i command.
580 char *text, *p, *op, *s;
581 char lbuf[_POSIX2_LINE_MAX + 1];
583 asize = 2 * _POSIX2_LINE_MAX + 1;
584 text = xmalloc(asize);
586 while (cu_fgets(lbuf, sizeof(lbuf))) {
587 op = s = text + size;
600 if (asize - size < _POSIX2_LINE_MAX + 1) {
602 text = xmalloc(asize);
605 return (xrealloc(text, size + 1));
609 * Get an address and return a pointer to the first character after
610 * it. Fill the structure pointed to according to the address.
620 case '\\': /* Context address */
623 case '/': /* Context address */
624 p = compile_re(p, &a->u.r);
626 err(COMPILE, "unterminated regular expression");
630 case '$': /* Last line */
634 case '0': case '1': case '2': case '3': case '4':
635 case '5': case '6': case '7': case '8': case '9':
637 a->u.l = strtol(p, &end, 10);
640 err(COMPILE, "expected context address");
647 * Return a copy of all the characters up to \n or \0.
659 for (start = s; *s != '\0' && *s != '\n'; ++s)
663 err(WARNING, "whitespace after %s", ctype);
665 return (memmove(xmalloc(len), start, len));
669 * Convert goto label names to addresses, and count a and r commands, in
670 * the given subset of the script. Free the memory used by labels in b
671 * and t commands (but not by :).
673 * TODO: Remove } nodes
677 struct s_command *cp, *end;
680 for (; cp != end; cp = cp->next)
688 /* Resolve branch target. */
693 if ((cp->u.c = findlabel(cp->t)) == NULL)
694 err(COMPILE2, "undefined label '%s'", cp->t);
698 /* Do interior commands. */
699 fixuplabel(cp->u.c, cp->next);
705 * Associate the given command label for later lookup.
709 struct s_command *cp;
711 register struct labhash **lhp, *lh;
715 for (h = 0, p = (u_char *)cp->t; (c = *p) != 0; p++)
716 h = (h << 5) + h + c;
717 lhp = &labels[h & LHMASK];
718 for (lh = *lhp; lh != NULL; lh = lh->lh_next)
719 if (lh->lh_hash == h && strcmp(cp->t, lh->lh_cmd->t) == 0)
720 err(COMPILE2, "duplicate label '%s'", cp->t);
721 lh = xmalloc(sizeof *lh);
730 * Find the label contained in the command l in the command linked
731 * list cp. L is excluded from the search. Return NULL if not found.
733 static struct s_command *
737 register struct labhash *lh;
741 for (h = 0, p = (u_char *)name; (c = *p) != 0; p++)
742 h = (h << 5) + h + c;
743 for (lh = labels[h & LHMASK]; lh != NULL; lh = lh->lh_next) {
744 if (lh->lh_hash == h && strcmp(name, lh->lh_cmd->t) == 0) {
753 * Warn about any unused labels. As a side effect, release the label hash
759 register struct labhash *lh, *next;
762 for (i = 0; i < LHSZ; i++) {
763 for (lh = labels[i]; lh != NULL; lh = next) {
766 err(WARNING, "unused label '%s'",