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