xref: /csrg-svn/usr.bin/sed/compile.c (revision 56077)
1 /*-
2  * Copyright (c) 1992 Diomidis Spinellis.
3  * Copyright (c) 1992 The Regents of the University of California.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Diomidis Spinellis of Imperial College, University of London.
8  *
9  * %sccs.include.redist.c%
10  */
11 
12 #ifndef lint
13 static char sccsid[] = "@(#)compile.c	5.4 (Berkeley) 08/28/92";
14 #endif /* not lint */
15 
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 
19 #include <ctype.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <limits.h>
23 #include <regex.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #include "defs.h"
29 #include "extern.h"
30 
31 static char	 *compile_addr __P((char *, struct s_addr *));
32 static char	 *compile_delimited __P((char *, char *));
33 static char	 *compile_flags __P((char *, struct s_subst *));
34 static char	 *compile_re __P((char *, regex_t **));
35 static char	 *compile_subst __P((char *, struct s_subst *));
36 static char	 *compile_text __P((void));
37 static char	 *compile_tr __P((char *, char **));
38 static struct s_command
39 		**compile_stream __P((char *, struct s_command **, char *));
40 static char	 *duptoeol __P((char *));
41 static struct s_command
42 		 *findlabel __P((struct s_command *, struct s_command *));
43 static void	  fixuplabel __P((struct s_command *, struct s_command *));
44 
45 /*
46  * Command specification.  This is used to drive the command parser.
47  */
48 struct s_format {
49 	char code;				/* Command code */
50 	int naddr;				/* Number of address args */
51 	enum e_args args;			/* Argument type */
52 };
53 
54 static struct s_format cmd_fmts[] = {
55 	{'{', 2, GROUP},
56 	{'a', 1, TEXT},
57 	{'b', 2, BRANCH},
58 	{'c', 2, TEXT},
59 	{'d', 2, EMPTY},
60 	{'D', 2, EMPTY},
61 	{'g', 2, EMPTY},
62 	{'G', 2, EMPTY},
63 	{'h', 2, EMPTY},
64 	{'H', 2, EMPTY},
65 	{'i', 1, TEXT},
66 	{'l', 2, EMPTY},
67 	{'n', 2, EMPTY},
68 	{'N', 2, EMPTY},
69 	{'p', 2, EMPTY},
70 	{'P', 2, EMPTY},
71 	{'q', 1, EMPTY},
72 	{'r', 1, RFILE},
73 	{'s', 2, SUBST},
74 	{'t', 2, BRANCH},
75 	{'w', 2, WFILE},
76 	{'x', 2, EMPTY},
77 	{'y', 2, TR},
78 	{'!', 2, NONSEL},
79 	{':', 0, LABEL},
80 	{'#', 0, COMMENT},
81 	{'=', 1, EMPTY},
82 	{'\0', 0, COMMENT},
83 };
84 
85 /* The compiled program. */
86 struct s_command *prog;
87 
88 /*
89  * Compile the program into prog.
90  * Initialise appends.
91  */
92 void
93 compile()
94 {
95 	*compile_stream(NULL, &prog, NULL) = NULL;
96 	fixuplabel(prog, prog);
97 	appends = xmalloc(sizeof(struct s_appends) * appendnum);
98 	match = xmalloc((maxnsub + 1) * sizeof(regmatch_t));
99 }
100 
101 #define EATSPACE() do {							\
102 	if (p)								\
103 		while (*p && isascii(*p) && isspace(*p))		\
104 			p++;						\
105 	} while (0)
106 
107 static struct s_command **
108 compile_stream(terminator, link, p)
109 	char *terminator;
110 	struct s_command **link;
111 	register char *p;
112 {
113 	static char lbuf[_POSIX2_LINE_MAX + 1];	/* To save stack */
114 	struct s_command *cmd, *cmd2;
115 	struct s_format *fp;
116 	int naddr;				/* Number of addresses */
117 
118 	if (p != NULL)
119 		goto semicolon;
120 	for (;;) {
121 		if ((p = cu_fgets(lbuf, sizeof(lbuf))) == NULL) {
122 			if (terminator != NULL)
123 				err(COMPILE, "unexpected EOF (pending }'s)");
124 			return (link);
125 		}
126 
127 semicolon:	EATSPACE();
128 		if (p && (*p == '#' || *p == '\0'))
129 			continue;
130 		if (*p == '}') {
131 			if (terminator == NULL)
132 				err(COMPILE, "unexpected }");
133 			return (link);
134 		}
135 		*link = cmd = xmalloc(sizeof(struct s_command));
136 		link = &cmd->next;
137 		cmd->nonsel = cmd->inrange = 0;
138 		/* First parse the addresses */
139 		naddr = 0;
140 		cmd->a1 = cmd->a2 = NULL;
141 
142 /* Valid characters to start an address */
143 #define	addrchar(c)	(strchr("0123456789/\\$", (c)))
144 		if (addrchar(*p)) {
145 			naddr++;
146 			cmd->a1 = xmalloc(sizeof(struct s_addr));
147 			p = compile_addr(p, cmd->a1);
148 			EATSPACE();				/* EXTENSION */
149 			if (*p == ',') {
150 				naddr++;
151 				p++;
152 				EATSPACE();			/* EXTENSION */
153 				cmd->a2 = xmalloc(sizeof(struct s_addr));
154 				p = compile_addr(p, cmd->a2);
155 			}
156 		}
157 
158 nonsel:		/* Now parse the command */
159 		EATSPACE();
160 		if (!*p)
161 			err(COMPILE, "command expected");
162 		cmd->code = *p;
163 		for (fp = cmd_fmts; fp->code; fp++)
164 			if (fp->code == *p)
165 				break;
166 		if (!fp->code)
167 			err(COMPILE, "invalid command code %c", *p);
168 		if (naddr > fp->naddr)
169 			err(COMPILE,
170 "command %c expects up to %d address(es), found %d", *p, fp->naddr, naddr);
171 		switch (fp->args) {
172 		case NONSEL:			/* ! */
173 			cmd->nonsel = ! cmd->nonsel;
174 			p++;
175 			goto nonsel;
176 		case GROUP:			/* { */
177 			p++;
178 			EATSPACE();
179 			if (!*p)
180 				p = NULL;
181 			cmd2 = xmalloc(sizeof(struct s_command));
182 			cmd2->code = '}';
183 			*compile_stream("}", &cmd->u.c, p) = cmd2;
184 			cmd->next = cmd2;
185 			link = &cmd2->next;
186 			break;
187 		case EMPTY:		/* d D g G h H l n N p P q x = \0 */
188 			p++;
189 			EATSPACE();
190 			if (*p == ';') {
191 				p++;
192 				link = &cmd->next;
193 				goto semicolon;
194 			}
195 			if (*p)
196 				err(COMPILE,
197 "extra characters at the end of %c command", cmd->code);
198 			break;
199 		case TEXT:			/* a c i */
200 			p++;
201 			EATSPACE();
202 			if (*p != '\\')
203 				err(COMPILE,
204 "command %c expects \\ followed by text", cmd->code);
205 			p++;
206 			EATSPACE();
207 			if (*p)
208 				err(COMPILE,
209 "extra characters after \\ at the end of %c command", cmd->code);
210 			cmd->t = compile_text();
211 			break;
212 		case COMMENT:			/* \0 # */
213 			break;
214 		case WFILE:			/* w */
215 			p++;
216 			EATSPACE();
217 			if (*p == '\0')
218 				err(COMPILE, "filename expected");
219 			cmd->t = duptoeol(p);
220 			if (aflag)
221 				cmd->u.fd = -1;
222 			else if ((cmd->u.fd = open(p,
223 			    O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
224 			    DEFFILEMODE)) == -1)
225 				err(FATAL, "%s: %s\n", p, strerror(errno));
226 			break;
227 		case RFILE:			/* r */
228 			p++;
229 			EATSPACE();
230 			if (*p == '\0')
231 				err(COMPILE, "filename expected");
232 			else
233 				cmd->t = duptoeol(p);
234 			break;
235 		case BRANCH:			/* b t */
236 			p++;
237 			EATSPACE();
238 			if (*p == '\0')
239 				cmd->t = NULL;
240 			else
241 				cmd->t = duptoeol(p);
242 			break;
243 		case LABEL:			/* : */
244 			p++;
245 			EATSPACE();
246 			cmd->t = duptoeol(p);
247 			if (strlen(p) == 0)
248 				err(COMPILE, "empty label");
249 			break;
250 		case SUBST:			/* s */
251 			p++;
252 			if (*p == '\0' || *p == '\\')
253 				err(COMPILE,
254 "substitute pattern can not be delimited by newline or backslash");
255 			cmd->u.s = xmalloc(sizeof(struct s_subst));
256 			p = compile_re(p, &cmd->u.s->re);
257 			if (p == NULL)
258 				err(COMPILE, "unterminated substitute pattern");
259 			--p;
260 			p = compile_subst(p, cmd->u.s);
261 			p = compile_flags(p, cmd->u.s);
262 			EATSPACE();
263 			if (*p == ';') {
264 				p++;
265 				link = &cmd->next;
266 				goto semicolon;
267 			}
268 			break;
269 		case TR:			/* y */
270 			p++;
271 			p = compile_tr(p, (char **)&cmd->u.y);
272 			EATSPACE();
273 			if (*p == ';') {
274 				p++;
275 				link = &cmd->next;
276 				goto semicolon;
277 			}
278 			if (*p)
279 				err(COMPILE,
280 "extra text at the end of a transform command");
281 			break;
282 		}
283 	}
284 }
285 
286 /*
287  * Get a delimited string.  P points to the delimeter of the string; d points
288  * to a buffer area.  Newline and delimiter escapes are processed; other
289  * escapes are ignored.
290  *
291  * Returns a pointer to the first character after the final delimiter or NULL
292  * in the case of a non-terminated string.  The character array d is filled
293  * with the processed string.
294  */
295 static char *
296 compile_delimited(p, d)
297 	char *p, *d;
298 {
299 	char c;
300 
301 	c = *p++;
302 	if (c == '\0')
303 		return (NULL);
304 	else if (c == '\\')
305 		err(COMPILE, "\\ can not be used as a string delimiter");
306 	else if (c == '\n')
307 		err(COMPILE, "newline can not be used as a string delimiter");
308 	while (*p) {
309 		if (*p == '\\' && p[1] == c)
310 			p++;
311 		else if (*p == '\\' && p[1] == 'n') {
312 			*d++ = '\n';
313 			p += 2;
314 			continue;
315 		} else if (*p == c) {
316 			*d = '\0';
317 			return (p + 1);
318 		}
319 		*d++ = *p++;
320 	}
321 	return (NULL);
322 }
323 
324 /*
325  * Get a regular expression.  P points to the delimiter of the regular
326  * expression; repp points to the address of a regexp pointer.  Newline
327  * and delimiter escapes are processed; other escapes are ignored.
328  * Returns a pointer to the first character after the final delimiter
329  * or NULL in the case of a non terminated regular expression.  The regexp
330  * pointer is set to the compiled regular expression.
331  * Cflags are passed to regcomp.
332  */
333 static char *
334 compile_re(p, repp)
335 	char *p;
336 	regex_t **repp;
337 {
338 	int eval;
339 	char re[_POSIX2_LINE_MAX + 1];
340 
341 	p = compile_delimited(p, re);
342 	if (p && strlen(re) == 0) {
343 		*repp = NULL;
344 		return (p);
345 	}
346 	*repp = xmalloc(sizeof(regex_t));
347 	if (p && (eval = regcomp(*repp, re, 0)) != 0)
348 		err(COMPILE, "RE error: %s", strregerror(eval, *repp));
349 	if (maxnsub < (*repp)->re_nsub)
350 		maxnsub = (*repp)->re_nsub;
351 	return (p);
352 }
353 
354 /*
355  * Compile the substitution string of a regular expression and set res to
356  * point to a saved copy of it.  Nsub is the number of parenthesized regular
357  * expressions.
358  */
359 static char *
360 compile_subst(p, s)
361 	char *p;
362 	struct s_subst *s;
363 {
364 	static char lbuf[_POSIX2_LINE_MAX + 1];
365 	int asize, ref, size;
366 	char c, *text, *op, *sp;
367 
368 	c = *p++;			/* Terminator character */
369 	if (c == '\0')
370 		return (NULL);
371 
372 	s->maxbref = 0;
373 	s->linenum = linenum;
374 	asize = 2 * _POSIX2_LINE_MAX + 1;
375 	text = xmalloc(asize);
376 	size = 0;
377 	do {
378 		op = sp = text + size;
379 		for (; *p; p++) {
380 			if (*p == '\\') {
381 				p++;
382 				if (strchr("123456789", *p) != NULL) {
383 					*sp++ = '\\';
384 					ref = *p - '0';
385 					if (s->re != NULL &&
386 					    ref > s->re->re_nsub)
387 						err(COMPILE,
388 "\\%c not defined in the RE", *p);
389 					if (s->maxbref < ref)
390 						s->maxbref = ref;
391 				} else if (*p == '&')
392 					*sp++ = '\\';
393 			} else if (*p == c) {
394 				p++;
395 				*sp++ = '\0';
396 				size += sp - op;
397 				s->new = xrealloc(text, size);
398 				return (p);
399 			} else if (*p == '\n') {
400 				err(COMPILE,
401 "unescaped newline inside substitute pattern");
402 				/* NOTREACHED */
403 			}
404 			*sp++ = *p;
405 		}
406 		size += sp - op;
407 		if (asize - size < _POSIX2_LINE_MAX + 1) {
408 			asize *= 2;
409 			text = xmalloc(asize);
410 		}
411 	} while (cu_fgets(p = lbuf, sizeof(lbuf)));
412 	err(COMPILE, "unterminated substitute in regular expression");
413 	/* NOTREACHED */
414 }
415 
416 /*
417  * Compile the flags of the s command
418  */
419 static char *
420 compile_flags(p, s)
421 	char *p;
422 	struct s_subst *s;
423 {
424 	int gn;			/* True if we have seen g or n */
425 	char wfile[_POSIX2_LINE_MAX + 1], *q;
426 
427 	s->n = 1;				/* Default */
428 	s->p = 0;
429 	s->wfile = NULL;
430 	s->wfd = -1;
431 	for (gn = 0;;) {
432 		EATSPACE();			/* EXTENSION */
433 		switch (*p) {
434 		case 'g':
435 			if (gn)
436 				err(COMPILE,
437 "more than one number or 'g' in substitute flags");
438 			gn = 1;
439 			s->n = 0;
440 			break;
441 		case '\0':
442 		case '\n':
443 		case ';':
444 			return (p);
445 		case 'p':
446 			s->p = 1;
447 			break;
448 		case '1': case '2': case '3':
449 		case '4': case '5': case '6':
450 		case '7': case '8': case '9':
451 			if (gn)
452 				err(COMPILE,
453 "more than one number or 'g' in substitute flags");
454 			gn = 1;
455 			/* XXX Check for overflow */
456 			s->n = (int)strtol(p, &p, 10);
457 			break;
458 		case 'w':
459 			p++;
460 #ifdef HISTORIC_PRACTICE
461 			if (*p != ' ') {
462 				err(WARNING, "space missing before w wfile");
463 				return (p);
464 			}
465 #endif
466 			EATSPACE();
467 			q = wfile;
468 			while (*p) {
469 				if (*p == '\n')
470 					break;
471 				*q++ = *p++;
472 			}
473 			*q = '\0';
474 			if (q == wfile)
475 				err(COMPILE, "no wfile specified");
476 			s->wfile = strdup(wfile);
477 			if (!aflag && (s->wfd = open(wfile,
478 			    O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
479 			    DEFFILEMODE)) == -1)
480 				err(FATAL, "%s: %s\n", wfile, strerror(errno));
481 			return (p);
482 		default:
483 			err(COMPILE,
484 			    "bad flag in substitute command: '%c'", *p);
485 			break;
486 		}
487 		p++;
488 	}
489 }
490 
491 /*
492  * Compile a translation set of strings into a lookup table.
493  */
494 static char *
495 compile_tr(p, transtab)
496 	char *p;
497 	char **transtab;
498 {
499 	int i;
500 	char *lt, *op, *np;
501 	char old[_POSIX2_LINE_MAX + 1];
502 	char new[_POSIX2_LINE_MAX + 1];
503 
504 	if (*p == '\0' || *p == '\\')
505 		err(COMPILE,
506 "transform pattern can not be delimited by newline or backslash");
507 	p = compile_delimited(p, old);
508 	if (p == NULL) {
509 		err(COMPILE, "unterminated transform source string");
510 		return (NULL);
511 	}
512 	p = compile_delimited(--p, new);
513 	if (p == NULL) {
514 		err(COMPILE, "unterminated transform target string");
515 		return (NULL);
516 	}
517 	EATSPACE();
518 	if (strlen(new) != strlen(old)) {
519 		err(COMPILE, "transform strings are not the same length");
520 		return (NULL);
521 	}
522 	/* We assume characters are 8 bits */
523 	lt = xmalloc(UCHAR_MAX);
524 	for (i = 0; i <= UCHAR_MAX; i++)
525 		lt[i] = (char)i;
526 	for (op = old, np = new; *op; op++, np++)
527 		lt[(u_char)*op] = *np;
528 	*transtab = lt;
529 	return (p);
530 }
531 
532 /*
533  * Compile the text following an a or i command.
534  */
535 static char *
536 compile_text()
537 {
538 	int asize, size;
539 	char *text, *p, *op, *s;
540 	char lbuf[_POSIX2_LINE_MAX + 1];
541 
542 	asize = 2 * _POSIX2_LINE_MAX + 1;
543 	text = xmalloc(asize);
544 	size = 0;
545 	while (cu_fgets(lbuf, sizeof(lbuf))) {
546 		op = s = text + size;
547 		p = lbuf;
548 		EATSPACE();
549 		for (; *p; p++) {
550 			if (*p == '\\')
551 				p++;
552 			*s++ = *p;
553 		}
554 		size += s - op;
555 		if (p[-2] != '\\') {
556 			*s = '\0';
557 			break;
558 		}
559 		if (asize - size < _POSIX2_LINE_MAX + 1) {
560 			asize *= 2;
561 			text = xmalloc(asize);
562 		}
563 	}
564 	return (xrealloc(text, size + 1));
565 }
566 
567 /*
568  * Get an address and return a pointer to the first character after
569  * it.  Fill the structure pointed to according to the address.
570  */
571 static char *
572 compile_addr(p, a)
573 	char *p;
574 	struct s_addr *a;
575 {
576 	char *end;
577 
578 	switch (*p) {
579 	case '\\':				/* Context address */
580 		++p;
581 		/* FALLTHROUGH */
582 	case '/':				/* Context address */
583 		p = compile_re(p, &a->u.r);
584 		if (p == NULL)
585 			err(COMPILE, "unterminated regular expression");
586 		a->type = AT_RE;
587 		return (p);
588 
589 	case '$':				/* Last line */
590 		a->type = AT_LAST;
591 		return (p + 1);
592 						/* Line number */
593 	case '0': case '1': case '2': case '3': case '4':
594 	case '5': case '6': case '7': case '8': case '9':
595 		a->type = AT_LINE;
596 		a->u.l = strtol(p, &end, 10);
597 		return (end);
598 	default:
599 		err(COMPILE, "expected context address");
600 		return (NULL);
601 	}
602 }
603 
604 /*
605  * Return a copy of all the characters up to \n or \0
606  */
607 static char *
608 duptoeol(s)
609 	register char *s;
610 {
611 	size_t len;
612 	char *start;
613 
614 	for (start = s; *s != '\0' && *s != '\n'; ++s);
615 	*s = '\0';
616 	len = s - start + 1;
617 	return (memmove(xmalloc(len), start, len));
618 }
619 
620 /*
621  * Find the label contained in the command l in the command linked list cp.
622  * L is excluded from the search.  Return NULL if not found.
623  */
624 static struct s_command *
625 findlabel(l, cp)
626 	struct s_command *l, *cp;
627 {
628 	struct s_command *r;
629 
630 	for (; cp; cp = cp->next)
631 		if (cp->code == ':' && cp != l && strcmp(l->t, cp->t) == 0)
632 			return (cp);
633 		else if (cp->code == '{' && (r = findlabel(l, cp->u.c)))
634 			return (r);
635 	return (NULL);
636 }
637 
638 /*
639  * Convert goto label names to addresses.
640  * Detect duplicate labels.
641  * Set appendnum to the number of a and r commands in the script.
642  * Free the memory used by labels in b and t commands (but not by :)
643  * Root is a pointer to the script linked list; cp points to the
644  * search start.
645  * TODO: Remove } nodes
646  */
647 static void
648 fixuplabel(root, cp)
649 	struct s_command *root, *cp;
650 {
651 	struct s_command *cp2;
652 
653 	for (; cp; cp = cp->next)
654 		switch (cp->code) {
655 		case ':':
656 			if (findlabel(cp, root))
657 				err(COMPILE2, "duplicate label %s", cp->t);
658 			break;
659 		case 'a':
660 		case 'r':
661 			appendnum++;
662 			break;
663 		case 'b':
664 		case 't':
665 			if (cp->t == NULL) {
666 				cp->u.c = NULL;
667 				break;
668 			}
669 			if ((cp2 = findlabel(cp, root)) == NULL)
670 				err(COMPILE2, "undefined label '%s'", cp->t);
671 			free(cp->t);
672 			cp->u.c = cp2;
673 			break;
674 		case '{':
675 			fixuplabel(root, cp->u.c);
676 			break;
677 		}
678 }
679