xref: /csrg-svn/usr.bin/sed/compile.c (revision 69057)
155992Sbostic /*-
255992Sbostic  * Copyright (c) 1992 Diomidis Spinellis.
362229Sbostic  * Copyright (c) 1992, 1993
462229Sbostic  *	The Regents of the University of California.  All rights reserved.
555992Sbostic  *
655992Sbostic  * This code is derived from software contributed to Berkeley by
755992Sbostic  * Diomidis Spinellis of Imperial College, University of London.
855992Sbostic  *
955992Sbostic  * %sccs.include.redist.c%
1055992Sbostic  */
1155992Sbostic 
1255992Sbostic #ifndef lint
13*69057Sbostic static char sccsid[] = "@(#)compile.c	8.2 (Berkeley) 04/28/95";
1455992Sbostic #endif /* not lint */
1555992Sbostic 
1655992Sbostic #include <sys/types.h>
1756019Sbostic #include <sys/stat.h>
1855992Sbostic 
1955992Sbostic #include <ctype.h>
2055992Sbostic #include <errno.h>
2155992Sbostic #include <fcntl.h>
2255992Sbostic #include <limits.h>
2355992Sbostic #include <regex.h>
2455992Sbostic #include <stdio.h>
2555992Sbostic #include <stdlib.h>
2655992Sbostic #include <string.h>
2755992Sbostic 
2855992Sbostic #include "defs.h"
2955992Sbostic #include "extern.h"
3055992Sbostic 
3159141Storek #define LHSZ	128
3259141Storek #define	LHMASK	(LHSZ - 1)
3359141Storek static struct labhash {
3459141Storek 	struct	labhash *lh_next;
3559141Storek 	u_int	lh_hash;
3659141Storek 	struct	s_command *lh_cmd;
3759141Storek 	int	lh_ref;
3859141Storek } *labels[LHSZ];
3959141Storek 
4055992Sbostic static char	 *compile_addr __P((char *, struct s_addr *));
4155992Sbostic static char	 *compile_delimited __P((char *, char *));
4255992Sbostic static char	 *compile_flags __P((char *, struct s_subst *));
4356077Sbostic static char	 *compile_re __P((char *, regex_t **));
4456019Sbostic static char	 *compile_subst __P((char *, struct s_subst *));
4555992Sbostic static char	 *compile_text __P((void));
4655992Sbostic static char	 *compile_tr __P((char *, char **));
4755992Sbostic static struct s_command
4855992Sbostic 		**compile_stream __P((char *, struct s_command **, char *));
4958540Sbostic static char	 *duptoeol __P((char *, char *));
5059141Storek static void	  enterlabel __P((struct s_command *));
5155992Sbostic static struct s_command
5259141Storek 		 *findlabel __P((char *));
5359141Storek static void	  fixuplabel __P((struct s_command *, struct s_command *));
5459141Storek static void	  uselabel __P((void));
5555992Sbostic 
5655992Sbostic /*
5755992Sbostic  * Command specification.  This is used to drive the command parser.
5855992Sbostic  */
5955992Sbostic struct s_format {
6055992Sbostic 	char code;				/* Command code */
6155992Sbostic 	int naddr;				/* Number of address args */
6255992Sbostic 	enum e_args args;			/* Argument type */
6355992Sbostic };
6455992Sbostic 
6555992Sbostic static struct s_format cmd_fmts[] = {
6655992Sbostic 	{'{', 2, GROUP},
6755992Sbostic 	{'a', 1, TEXT},
6855992Sbostic 	{'b', 2, BRANCH},
6955992Sbostic 	{'c', 2, TEXT},
7055992Sbostic 	{'d', 2, EMPTY},
7155992Sbostic 	{'D', 2, EMPTY},
7255992Sbostic 	{'g', 2, EMPTY},
7355992Sbostic 	{'G', 2, EMPTY},
7455992Sbostic 	{'h', 2, EMPTY},
7555992Sbostic 	{'H', 2, EMPTY},
7655992Sbostic 	{'i', 1, TEXT},
7755992Sbostic 	{'l', 2, EMPTY},
7855992Sbostic 	{'n', 2, EMPTY},
7955992Sbostic 	{'N', 2, EMPTY},
8055992Sbostic 	{'p', 2, EMPTY},
8155992Sbostic 	{'P', 2, EMPTY},
8255992Sbostic 	{'q', 1, EMPTY},
8355992Sbostic 	{'r', 1, RFILE},
8455992Sbostic 	{'s', 2, SUBST},
8555992Sbostic 	{'t', 2, BRANCH},
8655992Sbostic 	{'w', 2, WFILE},
8755992Sbostic 	{'x', 2, EMPTY},
8855992Sbostic 	{'y', 2, TR},
8955992Sbostic 	{'!', 2, NONSEL},
9055992Sbostic 	{':', 0, LABEL},
9155992Sbostic 	{'#', 0, COMMENT},
9255992Sbostic 	{'=', 1, EMPTY},
9355992Sbostic 	{'\0', 0, COMMENT},
9455992Sbostic };
9555992Sbostic 
9656019Sbostic /* The compiled program. */
9755992Sbostic struct s_command *prog;
9855992Sbostic 
9955992Sbostic /*
10055992Sbostic  * Compile the program into prog.
10156019Sbostic  * Initialise appends.
10255992Sbostic  */
10355992Sbostic void
compile()10455992Sbostic compile()
10555992Sbostic {
10655992Sbostic 	*compile_stream(NULL, &prog, NULL) = NULL;
10759141Storek 	fixuplabel(prog, NULL);
10859141Storek 	uselabel();
10955992Sbostic 	appends = xmalloc(sizeof(struct s_appends) * appendnum);
11056077Sbostic 	match = xmalloc((maxnsub + 1) * sizeof(regmatch_t));
11155992Sbostic }
11255992Sbostic 
11355992Sbostic #define EATSPACE() do {							\
11455992Sbostic 	if (p)								\
11555992Sbostic 		while (*p && isascii(*p) && isspace(*p))		\
11655992Sbostic 			p++;						\
11755992Sbostic 	} while (0)
11855992Sbostic 
11955992Sbostic static struct s_command **
compile_stream(terminator,link,p)12055992Sbostic compile_stream(terminator, link, p)
12155992Sbostic 	char *terminator;
12255992Sbostic 	struct s_command **link;
12355992Sbostic 	register char *p;
12455992Sbostic {
12555992Sbostic 	static char lbuf[_POSIX2_LINE_MAX + 1];	/* To save stack */
12655992Sbostic 	struct s_command *cmd, *cmd2;
12755992Sbostic 	struct s_format *fp;
12855992Sbostic 	int naddr;				/* Number of addresses */
12955992Sbostic 
13055992Sbostic 	if (p != NULL)
13155992Sbostic 		goto semicolon;
13255992Sbostic 	for (;;) {
13355992Sbostic 		if ((p = cu_fgets(lbuf, sizeof(lbuf))) == NULL) {
13455992Sbostic 			if (terminator != NULL)
13555992Sbostic 				err(COMPILE, "unexpected EOF (pending }'s)");
13655992Sbostic 			return (link);
13755992Sbostic 		}
13855992Sbostic 
13955992Sbostic semicolon:	EATSPACE();
14055992Sbostic 		if (p && (*p == '#' || *p == '\0'))
14155992Sbostic 			continue;
14255992Sbostic 		if (*p == '}') {
14355992Sbostic 			if (terminator == NULL)
14455992Sbostic 				err(COMPILE, "unexpected }");
14555992Sbostic 			return (link);
14655992Sbostic 		}
14755992Sbostic 		*link = cmd = xmalloc(sizeof(struct s_command));
14855992Sbostic 		link = &cmd->next;
14959141Storek 		cmd->nonsel = cmd->inrange = 0;
15055992Sbostic 		/* First parse the addresses */
15155992Sbostic 		naddr = 0;
15255992Sbostic 
15355992Sbostic /* Valid characters to start an address */
15455992Sbostic #define	addrchar(c)	(strchr("0123456789/\\$", (c)))
15555992Sbostic 		if (addrchar(*p)) {
15655992Sbostic 			naddr++;
15755992Sbostic 			cmd->a1 = xmalloc(sizeof(struct s_addr));
15855992Sbostic 			p = compile_addr(p, cmd->a1);
15955992Sbostic 			EATSPACE();				/* EXTENSION */
16055992Sbostic 			if (*p == ',') {
16155992Sbostic 				p++;
16255992Sbostic 				EATSPACE();			/* EXTENSION */
163*69057Sbostic 				naddr++;
16455992Sbostic 				cmd->a2 = xmalloc(sizeof(struct s_addr));
16555992Sbostic 				p = compile_addr(p, cmd->a2);
166*69057Sbostic 				EATSPACE();
167*69057Sbostic 			} else
168*69057Sbostic 				cmd->a2 = 0;
169*69057Sbostic 		} else
170*69057Sbostic 			cmd->a1 = cmd->a2 = 0;
17155992Sbostic 
17255992Sbostic nonsel:		/* Now parse the command */
17355992Sbostic 		if (!*p)
17455992Sbostic 			err(COMPILE, "command expected");
17555992Sbostic 		cmd->code = *p;
17655992Sbostic 		for (fp = cmd_fmts; fp->code; fp++)
17755992Sbostic 			if (fp->code == *p)
17855992Sbostic 				break;
17955992Sbostic 		if (!fp->code)
18055992Sbostic 			err(COMPILE, "invalid command code %c", *p);
18155992Sbostic 		if (naddr > fp->naddr)
18255992Sbostic 			err(COMPILE,
18355992Sbostic "command %c expects up to %d address(es), found %d", *p, fp->naddr, naddr);
18455992Sbostic 		switch (fp->args) {
18555992Sbostic 		case NONSEL:			/* ! */
186*69057Sbostic 			p++;
187*69057Sbostic 			EATSPACE();
18855992Sbostic 			cmd->nonsel = ! cmd->nonsel;
18955992Sbostic 			goto nonsel;
19055992Sbostic 		case GROUP:			/* { */
19155992Sbostic 			p++;
19255992Sbostic 			EATSPACE();
19355992Sbostic 			if (!*p)
19455992Sbostic 				p = NULL;
19555992Sbostic 			cmd2 = xmalloc(sizeof(struct s_command));
19655992Sbostic 			cmd2->code = '}';
19755992Sbostic 			*compile_stream("}", &cmd->u.c, p) = cmd2;
19855992Sbostic 			cmd->next = cmd2;
19955992Sbostic 			link = &cmd2->next;
200*69057Sbostic 			/*
201*69057Sbostic 			 * Short-circuit command processing, since end of
202*69057Sbostic 			 * group is really just a noop.
203*69057Sbostic 			 */
204*69057Sbostic 			cmd2->nonsel = 1;
205*69057Sbostic 			cmd2->a1 = cmd2->a2 = 0;
20655992Sbostic 			break;
20755992Sbostic 		case EMPTY:		/* d D g G h H l n N p P q x = \0 */
20855992Sbostic 			p++;
20955992Sbostic 			EATSPACE();
21055992Sbostic 			if (*p == ';') {
21155992Sbostic 				p++;
21255992Sbostic 				link = &cmd->next;
21355992Sbostic 				goto semicolon;
21455992Sbostic 			}
21555992Sbostic 			if (*p)
21655992Sbostic 				err(COMPILE,
21755992Sbostic "extra characters at the end of %c command", cmd->code);
21855992Sbostic 			break;
21955992Sbostic 		case TEXT:			/* a c i */
22055992Sbostic 			p++;
22155992Sbostic 			EATSPACE();
22255992Sbostic 			if (*p != '\\')
22355992Sbostic 				err(COMPILE,
22455992Sbostic "command %c expects \\ followed by text", cmd->code);
22555992Sbostic 			p++;
22655992Sbostic 			EATSPACE();
22755992Sbostic 			if (*p)
22855992Sbostic 				err(COMPILE,
22955992Sbostic "extra characters after \\ at the end of %c command", cmd->code);
23055992Sbostic 			cmd->t = compile_text();
23155992Sbostic 			break;
23255992Sbostic 		case COMMENT:			/* \0 # */
23355992Sbostic 			break;
23455992Sbostic 		case WFILE:			/* w */
23555992Sbostic 			p++;
23655992Sbostic 			EATSPACE();
23755992Sbostic 			if (*p == '\0')
23855992Sbostic 				err(COMPILE, "filename expected");
23958540Sbostic 			cmd->t = duptoeol(p, "w command");
24055992Sbostic 			if (aflag)
24155992Sbostic 				cmd->u.fd = -1;
24255992Sbostic 			else if ((cmd->u.fd = open(p,
24355992Sbostic 			    O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
24455992Sbostic 			    DEFFILEMODE)) == -1)
24555992Sbostic 				err(FATAL, "%s: %s\n", p, strerror(errno));
24655992Sbostic 			break;
24755992Sbostic 		case RFILE:			/* r */
24855992Sbostic 			p++;
24955992Sbostic 			EATSPACE();
25055992Sbostic 			if (*p == '\0')
25155992Sbostic 				err(COMPILE, "filename expected");
25255992Sbostic 			else
25358540Sbostic 				cmd->t = duptoeol(p, "read command");
25455992Sbostic 			break;
25555992Sbostic 		case BRANCH:			/* b t */
25655992Sbostic 			p++;
25755992Sbostic 			EATSPACE();
25855992Sbostic 			if (*p == '\0')
25955992Sbostic 				cmd->t = NULL;
26055992Sbostic 			else
26158540Sbostic 				cmd->t = duptoeol(p, "branch");
26255992Sbostic 			break;
26355992Sbostic 		case LABEL:			/* : */
26455992Sbostic 			p++;
26555992Sbostic 			EATSPACE();
26658540Sbostic 			cmd->t = duptoeol(p, "label");
26755992Sbostic 			if (strlen(p) == 0)
26855992Sbostic 				err(COMPILE, "empty label");
26959141Storek 			enterlabel(cmd);
27055992Sbostic 			break;
27155992Sbostic 		case SUBST:			/* s */
27255992Sbostic 			p++;
27355992Sbostic 			if (*p == '\0' || *p == '\\')
27455992Sbostic 				err(COMPILE,
27555992Sbostic "substitute pattern can not be delimited by newline or backslash");
27655992Sbostic 			cmd->u.s = xmalloc(sizeof(struct s_subst));
27756077Sbostic 			p = compile_re(p, &cmd->u.s->re);
27855992Sbostic 			if (p == NULL)
27956004Sbostic 				err(COMPILE, "unterminated substitute pattern");
28056019Sbostic 			--p;
28156019Sbostic 			p = compile_subst(p, cmd->u.s);
28255992Sbostic 			p = compile_flags(p, cmd->u.s);
28355992Sbostic 			EATSPACE();
28455992Sbostic 			if (*p == ';') {
28555992Sbostic 				p++;
28655992Sbostic 				link = &cmd->next;
28755992Sbostic 				goto semicolon;
28855992Sbostic 			}
28955992Sbostic 			break;
29055992Sbostic 		case TR:			/* y */
29155992Sbostic 			p++;
29255992Sbostic 			p = compile_tr(p, (char **)&cmd->u.y);
29355992Sbostic 			EATSPACE();
29455992Sbostic 			if (*p == ';') {
29555992Sbostic 				p++;
29655992Sbostic 				link = &cmd->next;
29755992Sbostic 				goto semicolon;
29855992Sbostic 			}
29955992Sbostic 			if (*p)
30055992Sbostic 				err(COMPILE,
30155992Sbostic "extra text at the end of a transform command");
30255992Sbostic 			break;
30355992Sbostic 		}
30455992Sbostic 	}
30555992Sbostic }
30655992Sbostic 
30755992Sbostic /*
30855992Sbostic  * Get a delimited string.  P points to the delimeter of the string; d points
30955992Sbostic  * to a buffer area.  Newline and delimiter escapes are processed; other
31055992Sbostic  * escapes are ignored.
31155992Sbostic  *
31255992Sbostic  * Returns a pointer to the first character after the final delimiter or NULL
31355992Sbostic  * in the case of a non-terminated string.  The character array d is filled
31455992Sbostic  * with the processed string.
31555992Sbostic  */
31655992Sbostic static char *
compile_delimited(p,d)31755992Sbostic compile_delimited(p, d)
31855992Sbostic 	char *p, *d;
31955992Sbostic {
32055992Sbostic 	char c;
32155992Sbostic 
32255992Sbostic 	c = *p++;
32355992Sbostic 	if (c == '\0')
32455992Sbostic 		return (NULL);
32555992Sbostic 	else if (c == '\\')
32655992Sbostic 		err(COMPILE, "\\ can not be used as a string delimiter");
32755992Sbostic 	else if (c == '\n')
32855992Sbostic 		err(COMPILE, "newline can not be used as a string delimiter");
32955992Sbostic 	while (*p) {
33055992Sbostic 		if (*p == '\\' && p[1] == c)
33156019Sbostic 			p++;
33255992Sbostic 		else if (*p == '\\' && p[1] == 'n') {
33356019Sbostic 			*d++ = '\n';
33456019Sbostic 			p += 2;
33556019Sbostic 			continue;
33656663Sbostic 		} else if (*p == '\\' && p[1] == '\\')
33756663Sbostic 			*d++ = *p++;
33856663Sbostic 		else if (*p == c) {
33955992Sbostic 			*d = '\0';
34055992Sbostic 			return (p + 1);
34155992Sbostic 		}
34255992Sbostic 		*d++ = *p++;
34355992Sbostic 	}
34455992Sbostic 	return (NULL);
34555992Sbostic }
34655992Sbostic 
34755992Sbostic /*
34856019Sbostic  * Get a regular expression.  P points to the delimiter of the regular
34956019Sbostic  * expression; repp points to the address of a regexp pointer.  Newline
35056019Sbostic  * and delimiter escapes are processed; other escapes are ignored.
35155992Sbostic  * Returns a pointer to the first character after the final delimiter
35256019Sbostic  * or NULL in the case of a non terminated regular expression.  The regexp
35356019Sbostic  * pointer is set to the compiled regular expression.
35455992Sbostic  * Cflags are passed to regcomp.
35555992Sbostic  */
35655992Sbostic static char *
compile_re(p,repp)35756077Sbostic compile_re(p, repp)
35855992Sbostic 	char *p;
35956019Sbostic 	regex_t **repp;
36055992Sbostic {
36155992Sbostic 	int eval;
36255992Sbostic 	char re[_POSIX2_LINE_MAX + 1];
36355992Sbostic 
36455992Sbostic 	p = compile_delimited(p, re);
36556019Sbostic 	if (p && strlen(re) == 0) {
36656019Sbostic 		*repp = NULL;
36756019Sbostic 		return (p);
36856019Sbostic 	}
36956019Sbostic 	*repp = xmalloc(sizeof(regex_t));
37056077Sbostic 	if (p && (eval = regcomp(*repp, re, 0)) != 0)
37156019Sbostic 		err(COMPILE, "RE error: %s", strregerror(eval, *repp));
37256077Sbostic 	if (maxnsub < (*repp)->re_nsub)
37356077Sbostic 		maxnsub = (*repp)->re_nsub;
37455992Sbostic 	return (p);
37555992Sbostic }
37655992Sbostic 
37755992Sbostic /*
37855992Sbostic  * Compile the substitution string of a regular expression and set res to
37955992Sbostic  * point to a saved copy of it.  Nsub is the number of parenthesized regular
38055992Sbostic  * expressions.
38155992Sbostic  */
38255992Sbostic static char *
compile_subst(p,s)38356019Sbostic compile_subst(p, s)
38456019Sbostic 	char *p;
38556019Sbostic 	struct s_subst *s;
38655992Sbostic {
38755992Sbostic 	static char lbuf[_POSIX2_LINE_MAX + 1];
38856019Sbostic 	int asize, ref, size;
38956019Sbostic 	char c, *text, *op, *sp;
39055992Sbostic 
39155992Sbostic 	c = *p++;			/* Terminator character */
39255992Sbostic 	if (c == '\0')
39355992Sbostic 		return (NULL);
39455992Sbostic 
39556019Sbostic 	s->maxbref = 0;
39656019Sbostic 	s->linenum = linenum;
39755992Sbostic 	asize = 2 * _POSIX2_LINE_MAX + 1;
39855992Sbostic 	text = xmalloc(asize);
39955992Sbostic 	size = 0;
40055992Sbostic 	do {
40156019Sbostic 		op = sp = text + size;
40255992Sbostic 		for (; *p; p++) {
40355992Sbostic 			if (*p == '\\') {
40455992Sbostic 				p++;
40555992Sbostic 				if (strchr("123456789", *p) != NULL) {
40656019Sbostic 					*sp++ = '\\';
40756019Sbostic 					ref = *p - '0';
40856019Sbostic 					if (s->re != NULL &&
40956019Sbostic 					    ref > s->re->re_nsub)
41055992Sbostic 						err(COMPILE,
41156019Sbostic "\\%c not defined in the RE", *p);
41256077Sbostic 					if (s->maxbref < ref)
41356077Sbostic 						s->maxbref = ref;
41456663Sbostic 				} else if (*p == '&' || *p == '\\')
41556019Sbostic 					*sp++ = '\\';
41655992Sbostic 			} else if (*p == c) {
41755992Sbostic 				p++;
41856019Sbostic 				*sp++ = '\0';
41956019Sbostic 				size += sp - op;
42056019Sbostic 				s->new = xrealloc(text, size);
42155992Sbostic 				return (p);
42255992Sbostic 			} else if (*p == '\n') {
42355992Sbostic 				err(COMPILE,
42455992Sbostic "unescaped newline inside substitute pattern");
42556019Sbostic 				/* NOTREACHED */
42655992Sbostic 			}
42756019Sbostic 			*sp++ = *p;
42855992Sbostic 		}
42956019Sbostic 		size += sp - op;
43055992Sbostic 		if (asize - size < _POSIX2_LINE_MAX + 1) {
43155992Sbostic 			asize *= 2;
43255992Sbostic 			text = xmalloc(asize);
43355992Sbostic 		}
43455992Sbostic 	} while (cu_fgets(p = lbuf, sizeof(lbuf)));
43556019Sbostic 	err(COMPILE, "unterminated substitute in regular expression");
43656019Sbostic 	/* NOTREACHED */
43755992Sbostic }
43855992Sbostic 
43955992Sbostic /*
44055992Sbostic  * Compile the flags of the s command
44155992Sbostic  */
44255992Sbostic static char *
compile_flags(p,s)44355992Sbostic compile_flags(p, s)
44455992Sbostic 	char *p;
44555992Sbostic 	struct s_subst *s;
44655992Sbostic {
44755992Sbostic 	int gn;			/* True if we have seen g or n */
44855992Sbostic 	char wfile[_POSIX2_LINE_MAX + 1], *q;
44955992Sbostic 
45055992Sbostic 	s->n = 1;				/* Default */
45155992Sbostic 	s->p = 0;
45255992Sbostic 	s->wfile = NULL;
45355992Sbostic 	s->wfd = -1;
45455992Sbostic 	for (gn = 0;;) {
45555992Sbostic 		EATSPACE();			/* EXTENSION */
45655992Sbostic 		switch (*p) {
45755992Sbostic 		case 'g':
45855992Sbostic 			if (gn)
45956004Sbostic 				err(COMPILE,
46056004Sbostic "more than one number or 'g' in substitute flags");
46155992Sbostic 			gn = 1;
46255992Sbostic 			s->n = 0;
46355992Sbostic 			break;
46455992Sbostic 		case '\0':
46555992Sbostic 		case '\n':
46655992Sbostic 		case ';':
46755992Sbostic 			return (p);
46855992Sbostic 		case 'p':
46955992Sbostic 			s->p = 1;
47055992Sbostic 			break;
47155992Sbostic 		case '1': case '2': case '3':
47255992Sbostic 		case '4': case '5': case '6':
47355992Sbostic 		case '7': case '8': case '9':
47455992Sbostic 			if (gn)
47556004Sbostic 				err(COMPILE,
47656004Sbostic "more than one number or 'g' in substitute flags");
47755992Sbostic 			gn = 1;
47855992Sbostic 			/* XXX Check for overflow */
47955992Sbostic 			s->n = (int)strtol(p, &p, 10);
48055992Sbostic 			break;
48155992Sbostic 		case 'w':
48255992Sbostic 			p++;
48355992Sbostic #ifdef HISTORIC_PRACTICE
48455992Sbostic 			if (*p != ' ') {
48555992Sbostic 				err(WARNING, "space missing before w wfile");
48655992Sbostic 				return (p);
48755992Sbostic 			}
48855992Sbostic #endif
48955992Sbostic 			EATSPACE();
49055992Sbostic 			q = wfile;
49155992Sbostic 			while (*p) {
49255992Sbostic 				if (*p == '\n')
49355992Sbostic 					break;
49455992Sbostic 				*q++ = *p++;
49555992Sbostic 			}
49655992Sbostic 			*q = '\0';
49755992Sbostic 			if (q == wfile)
49856004Sbostic 				err(COMPILE, "no wfile specified");
49955992Sbostic 			s->wfile = strdup(wfile);
50055992Sbostic 			if (!aflag && (s->wfd = open(wfile,
50155992Sbostic 			    O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
50255992Sbostic 			    DEFFILEMODE)) == -1)
50355992Sbostic 				err(FATAL, "%s: %s\n", wfile, strerror(errno));
50455992Sbostic 			return (p);
50555992Sbostic 		default:
50655992Sbostic 			err(COMPILE,
50756004Sbostic 			    "bad flag in substitute command: '%c'", *p);
50855992Sbostic 			break;
50955992Sbostic 		}
51055992Sbostic 		p++;
51155992Sbostic 	}
51255992Sbostic }
51355992Sbostic 
51455992Sbostic /*
51555992Sbostic  * Compile a translation set of strings into a lookup table.
51655992Sbostic  */
51755992Sbostic static char *
compile_tr(p,transtab)51855992Sbostic compile_tr(p, transtab)
51955992Sbostic 	char *p;
52055992Sbostic 	char **transtab;
52155992Sbostic {
52255992Sbostic 	int i;
52355992Sbostic 	char *lt, *op, *np;
52455992Sbostic 	char old[_POSIX2_LINE_MAX + 1];
52555992Sbostic 	char new[_POSIX2_LINE_MAX + 1];
52655992Sbostic 
52755992Sbostic 	if (*p == '\0' || *p == '\\')
52855992Sbostic 		err(COMPILE,
52955992Sbostic "transform pattern can not be delimited by newline or backslash");
53055992Sbostic 	p = compile_delimited(p, old);
53155992Sbostic 	if (p == NULL) {
53255992Sbostic 		err(COMPILE, "unterminated transform source string");
53355992Sbostic 		return (NULL);
53455992Sbostic 	}
53555992Sbostic 	p = compile_delimited(--p, new);
53655992Sbostic 	if (p == NULL) {
53755992Sbostic 		err(COMPILE, "unterminated transform target string");
53855992Sbostic 		return (NULL);
53955992Sbostic 	}
54055992Sbostic 	EATSPACE();
54155992Sbostic 	if (strlen(new) != strlen(old)) {
54255992Sbostic 		err(COMPILE, "transform strings are not the same length");
54355992Sbostic 		return (NULL);
54455992Sbostic 	}
54555992Sbostic 	/* We assume characters are 8 bits */
54655992Sbostic 	lt = xmalloc(UCHAR_MAX);
54755992Sbostic 	for (i = 0; i <= UCHAR_MAX; i++)
54855992Sbostic 		lt[i] = (char)i;
54955992Sbostic 	for (op = old, np = new; *op; op++, np++)
55055992Sbostic 		lt[(u_char)*op] = *np;
55155992Sbostic 	*transtab = lt;
55255992Sbostic 	return (p);
55355992Sbostic }
55455992Sbostic 
55555992Sbostic /*
55655992Sbostic  * Compile the text following an a or i command.
55755992Sbostic  */
55855992Sbostic static char *
compile_text()55955992Sbostic compile_text()
56055992Sbostic {
56155992Sbostic 	int asize, size;
56255992Sbostic 	char *text, *p, *op, *s;
56355992Sbostic 	char lbuf[_POSIX2_LINE_MAX + 1];
56455992Sbostic 
56555992Sbostic 	asize = 2 * _POSIX2_LINE_MAX + 1;
56655992Sbostic 	text = xmalloc(asize);
56755992Sbostic 	size = 0;
56855992Sbostic 	while (cu_fgets(lbuf, sizeof(lbuf))) {
56955992Sbostic 		op = s = text + size;
57055992Sbostic 		p = lbuf;
57155992Sbostic 		EATSPACE();
57255992Sbostic 		for (; *p; p++) {
57355992Sbostic 			if (*p == '\\')
57455992Sbostic 				p++;
57555992Sbostic 			*s++ = *p;
57655992Sbostic 		}
57755992Sbostic 		size += s - op;
57855992Sbostic 		if (p[-2] != '\\') {
57955992Sbostic 			*s = '\0';
58055992Sbostic 			break;
58155992Sbostic 		}
58255992Sbostic 		if (asize - size < _POSIX2_LINE_MAX + 1) {
58355992Sbostic 			asize *= 2;
58455992Sbostic 			text = xmalloc(asize);
58555992Sbostic 		}
58655992Sbostic 	}
58755992Sbostic 	return (xrealloc(text, size + 1));
58855992Sbostic }
58955992Sbostic 
59055992Sbostic /*
59155992Sbostic  * Get an address and return a pointer to the first character after
59255992Sbostic  * it.  Fill the structure pointed to according to the address.
59355992Sbostic  */
59455992Sbostic static char *
compile_addr(p,a)59555992Sbostic compile_addr(p, a)
59655992Sbostic 	char *p;
59755992Sbostic 	struct s_addr *a;
59855992Sbostic {
59955992Sbostic 	char *end;
60055992Sbostic 
60155992Sbostic 	switch (*p) {
60255992Sbostic 	case '\\':				/* Context address */
60356019Sbostic 		++p;
60456019Sbostic 		/* FALLTHROUGH */
60555992Sbostic 	case '/':				/* Context address */
60656077Sbostic 		p = compile_re(p, &a->u.r);
60755992Sbostic 		if (p == NULL)
60855992Sbostic 			err(COMPILE, "unterminated regular expression");
60955992Sbostic 		a->type = AT_RE;
61055992Sbostic 		return (p);
61156019Sbostic 
61255992Sbostic 	case '$':				/* Last line */
61355992Sbostic 		a->type = AT_LAST;
61455992Sbostic 		return (p + 1);
61555992Sbostic 						/* Line number */
61655992Sbostic 	case '0': case '1': case '2': case '3': case '4':
61755992Sbostic 	case '5': case '6': case '7': case '8': case '9':
61855992Sbostic 		a->type = AT_LINE;
61955992Sbostic 		a->u.l = strtol(p, &end, 10);
62055992Sbostic 		return (end);
62155992Sbostic 	default:
62255992Sbostic 		err(COMPILE, "expected context address");
62355992Sbostic 		return (NULL);
62455992Sbostic 	}
62555992Sbostic }
62655992Sbostic 
62755992Sbostic /*
62858540Sbostic  * duptoeol --
62958540Sbostic  *	Return a copy of all the characters up to \n or \0.
63055992Sbostic  */
63155992Sbostic static char *
duptoeol(s,ctype)63258540Sbostic duptoeol(s, ctype)
63355992Sbostic 	register char *s;
63458540Sbostic 	char *ctype;
63555992Sbostic {
63655992Sbostic 	size_t len;
63758540Sbostic 	int ws;
63855992Sbostic 	char *start;
63955992Sbostic 
64058540Sbostic 	ws = 0;
64158540Sbostic 	for (start = s; *s != '\0' && *s != '\n'; ++s)
64258540Sbostic 		ws = isspace(*s);
64355992Sbostic 	*s = '\0';
64458540Sbostic 	if (ws)
64558540Sbostic 		err(WARNING, "whitespace after %s", ctype);
64655992Sbostic 	len = s - start + 1;
64755992Sbostic 	return (memmove(xmalloc(len), start, len));
64855992Sbostic }
64955992Sbostic 
65055992Sbostic /*
65159141Storek  * Convert goto label names to addresses, and count a and r commands, in
65259141Storek  * the given subset of the script.  Free the memory used by labels in b
65359141Storek  * and t commands (but not by :).
65458540Sbostic  *
65555992Sbostic  * TODO: Remove } nodes
65655992Sbostic  */
65755992Sbostic static void
fixuplabel(cp,end)65859141Storek fixuplabel(cp, end)
65959141Storek 	struct s_command *cp, *end;
66055992Sbostic {
66155992Sbostic 
66256093Sbostic 	for (; cp != end; cp = cp->next)
66355992Sbostic 		switch (cp->code) {
66455992Sbostic 		case 'a':
66555992Sbostic 		case 'r':
66655992Sbostic 			appendnum++;
66755992Sbostic 			break;
66855992Sbostic 		case 'b':
66955992Sbostic 		case 't':
67059141Storek 			/* Resolve branch target. */
67155992Sbostic 			if (cp->t == NULL) {
67255992Sbostic 				cp->u.c = NULL;
67355992Sbostic 				break;
67455992Sbostic 			}
67559141Storek 			if ((cp->u.c = findlabel(cp->t)) == NULL)
67656004Sbostic 				err(COMPILE2, "undefined label '%s'", cp->t);
67755992Sbostic 			free(cp->t);
67855992Sbostic 			break;
67955992Sbostic 		case '{':
68059141Storek 			/* Do interior commands. */
68159141Storek 			fixuplabel(cp->u.c, cp->next);
68255992Sbostic 			break;
68355992Sbostic 		}
68455992Sbostic }
68558540Sbostic 
68658540Sbostic /*
68759141Storek  * Associate the given command label for later lookup.
68859141Storek  */
68959141Storek static void
enterlabel(cp)69059141Storek enterlabel(cp)
69159141Storek 	struct s_command *cp;
69259141Storek {
69359141Storek 	register struct labhash **lhp, *lh;
69459141Storek 	register u_char *p;
69559141Storek 	register u_int h, c;
69659141Storek 
69759141Storek 	for (h = 0, p = (u_char *)cp->t; (c = *p) != 0; p++)
69859141Storek 		h = (h << 5) + h + c;
69959141Storek 	lhp = &labels[h & LHMASK];
70059141Storek 	for (lh = *lhp; lh != NULL; lh = lh->lh_next)
70159141Storek 		if (lh->lh_hash == h && strcmp(cp->t, lh->lh_cmd->t) == 0)
70259141Storek 			err(COMPILE2, "duplicate label '%s'", cp->t);
70359141Storek 	lh = xmalloc(sizeof *lh);
70459141Storek 	lh->lh_next = *lhp;
70559141Storek 	lh->lh_hash = h;
70659141Storek 	lh->lh_cmd = cp;
70759141Storek 	lh->lh_ref = 0;
70859141Storek 	*lhp = lh;
70959141Storek }
71059141Storek 
71159141Storek /*
71258540Sbostic  * Find the label contained in the command l in the command linked
71358540Sbostic  * list cp.  L is excluded from the search.  Return NULL if not found.
71458540Sbostic  */
71558540Sbostic static struct s_command *
findlabel(name)71659141Storek findlabel(name)
71759141Storek 	char *name;
71858540Sbostic {
71959141Storek 	register struct labhash *lh;
72059141Storek 	register u_char *p;
72159141Storek 	register u_int h, c;
72258540Sbostic 
72359141Storek 	for (h = 0, p = (u_char *)name; (c = *p) != 0; p++)
72459141Storek 		h = (h << 5) + h + c;
72559141Storek 	for (lh = labels[h & LHMASK]; lh != NULL; lh = lh->lh_next) {
72659141Storek 		if (lh->lh_hash == h && strcmp(name, lh->lh_cmd->t) == 0) {
72759141Storek 			lh->lh_ref = 1;
72859141Storek 			return (lh->lh_cmd);
72959141Storek 		}
73058540Sbostic 	}
73158540Sbostic 	return (NULL);
73258540Sbostic }
73358540Sbostic 
73458540Sbostic /*
73559141Storek  * Warn about any unused labels.  As a side effect, release the label hash
73659141Storek  * table space.
73758540Sbostic  */
73858540Sbostic static void
uselabel()73959141Storek uselabel()
74058540Sbostic {
74159141Storek 	register struct labhash *lh, *next;
74259141Storek 	register int i;
74359141Storek 
74459141Storek 	for (i = 0; i < LHSZ; i++) {
74559141Storek 		for (lh = labels[i]; lh != NULL; lh = next) {
74659141Storek 			next = lh->lh_next;
74759141Storek 			if (!lh->lh_ref)
74859141Storek 				err(WARNING, "unused label '%s'",
74959141Storek 				    lh->lh_cmd->t);
75059141Storek 			free(lh);
75159141Storek 		}
75258540Sbostic 	}
75358540Sbostic }
754