xref: /freebsd/usr.bin/sed/compile.c (revision 8e33c0a0f6baf1964c3607c363a9411a119acdcb)
19b50d902SRodney W. Grimes /*-
29b50d902SRodney W. Grimes  * Copyright (c) 1992 Diomidis Spinellis.
39b50d902SRodney W. Grimes  * Copyright (c) 1992, 1993
49b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
59b50d902SRodney W. Grimes  *
69b50d902SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
79b50d902SRodney W. Grimes  * Diomidis Spinellis of Imperial College, University of London.
89b50d902SRodney W. Grimes  *
99b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
109b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
119b50d902SRodney W. Grimes  * are met:
129b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
149b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
159b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
169b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
179b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
189b50d902SRodney W. Grimes  *    must display the following acknowledgement:
199b50d902SRodney W. Grimes  *	This product includes software developed by the University of
209b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
219b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
229b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
239b50d902SRodney W. Grimes  *    without specific prior written permission.
249b50d902SRodney W. Grimes  *
259b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
269b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
279b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
289b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
299b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
309b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
319b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
329b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
339b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
349b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
359b50d902SRodney W. Grimes  * SUCH DAMAGE.
369b50d902SRodney W. Grimes  */
379b50d902SRodney W. Grimes 
389b50d902SRodney W. Grimes #ifndef lint
3973a08bb2SPhilippe Charnier #if 0
409b50d902SRodney W. Grimes static char sccsid[] = "@(#)compile.c	8.1 (Berkeley) 6/6/93";
4173a08bb2SPhilippe Charnier #endif
4273a08bb2SPhilippe Charnier static const char rcsid[] =
43c3aac50fSPeter Wemm   "$FreeBSD$";
449b50d902SRodney W. Grimes #endif /* not lint */
459b50d902SRodney W. Grimes 
469b50d902SRodney W. Grimes #include <sys/types.h>
479b50d902SRodney W. Grimes #include <sys/stat.h>
489b50d902SRodney W. Grimes 
499b50d902SRodney W. Grimes #include <ctype.h>
5073a08bb2SPhilippe Charnier #include <err.h>
519b50d902SRodney W. Grimes #include <fcntl.h>
529b50d902SRodney W. Grimes #include <limits.h>
539b50d902SRodney W. Grimes #include <regex.h>
549b50d902SRodney W. Grimes #include <stdio.h>
559b50d902SRodney W. Grimes #include <stdlib.h>
569b50d902SRodney W. Grimes #include <string.h>
579b50d902SRodney W. Grimes 
589b50d902SRodney W. Grimes #include "defs.h"
599b50d902SRodney W. Grimes #include "extern.h"
609b50d902SRodney W. Grimes 
619b50d902SRodney W. Grimes #define LHSZ	128
629b50d902SRodney W. Grimes #define	LHMASK	(LHSZ - 1)
639b50d902SRodney W. Grimes static struct labhash {
649b50d902SRodney W. Grimes 	struct	labhash *lh_next;
659b50d902SRodney W. Grimes 	u_int	lh_hash;
669b50d902SRodney W. Grimes 	struct	s_command *lh_cmd;
679b50d902SRodney W. Grimes 	int	lh_ref;
689b50d902SRodney W. Grimes } *labels[LHSZ];
699b50d902SRodney W. Grimes 
709b50d902SRodney W. Grimes static char	 *compile_addr __P((char *, struct s_addr *));
71ce19262dSJordan K. Hubbard static char	 *compile_ccl __P((char **, char *));
729b50d902SRodney W. Grimes static char	 *compile_delimited __P((char *, char *));
739b50d902SRodney W. Grimes static char	 *compile_flags __P((char *, struct s_subst *));
749b50d902SRodney W. Grimes static char	 *compile_re __P((char *, regex_t **));
759b50d902SRodney W. Grimes static char	 *compile_subst __P((char *, struct s_subst *));
769b50d902SRodney W. Grimes static char	 *compile_text __P((void));
779b50d902SRodney W. Grimes static char	 *compile_tr __P((char *, char **));
789b50d902SRodney W. Grimes static struct s_command
79ce19262dSJordan K. Hubbard 		**compile_stream __P((struct s_command **));
809b50d902SRodney W. Grimes static char	 *duptoeol __P((char *, char *));
819b50d902SRodney W. Grimes static void	  enterlabel __P((struct s_command *));
829b50d902SRodney W. Grimes static struct s_command
839b50d902SRodney W. Grimes 		 *findlabel __P((char *));
849b50d902SRodney W. Grimes static void	  fixuplabel __P((struct s_command *, struct s_command *));
859b50d902SRodney W. Grimes static void	  uselabel __P((void));
869b50d902SRodney W. Grimes 
879b50d902SRodney W. Grimes /*
889b50d902SRodney W. Grimes  * Command specification.  This is used to drive the command parser.
899b50d902SRodney W. Grimes  */
909b50d902SRodney W. Grimes struct s_format {
919b50d902SRodney W. Grimes 	char code;				/* Command code */
929b50d902SRodney W. Grimes 	int naddr;				/* Number of address args */
939b50d902SRodney W. Grimes 	enum e_args args;			/* Argument type */
949b50d902SRodney W. Grimes };
959b50d902SRodney W. Grimes 
969b50d902SRodney W. Grimes static struct s_format cmd_fmts[] = {
979b50d902SRodney W. Grimes 	{'{', 2, GROUP},
98ce19262dSJordan K. Hubbard 	{'}', 0, ENDGROUP},
999b50d902SRodney W. Grimes 	{'a', 1, TEXT},
1009b50d902SRodney W. Grimes 	{'b', 2, BRANCH},
1019b50d902SRodney W. Grimes 	{'c', 2, TEXT},
1029b50d902SRodney W. Grimes 	{'d', 2, EMPTY},
1039b50d902SRodney W. Grimes 	{'D', 2, EMPTY},
1049b50d902SRodney W. Grimes 	{'g', 2, EMPTY},
1059b50d902SRodney W. Grimes 	{'G', 2, EMPTY},
1069b50d902SRodney W. Grimes 	{'h', 2, EMPTY},
1079b50d902SRodney W. Grimes 	{'H', 2, EMPTY},
1089b50d902SRodney W. Grimes 	{'i', 1, TEXT},
1099b50d902SRodney W. Grimes 	{'l', 2, EMPTY},
1109b50d902SRodney W. Grimes 	{'n', 2, EMPTY},
1119b50d902SRodney W. Grimes 	{'N', 2, EMPTY},
1129b50d902SRodney W. Grimes 	{'p', 2, EMPTY},
1139b50d902SRodney W. Grimes 	{'P', 2, EMPTY},
1149b50d902SRodney W. Grimes 	{'q', 1, EMPTY},
1159b50d902SRodney W. Grimes 	{'r', 1, RFILE},
1169b50d902SRodney W. Grimes 	{'s', 2, SUBST},
1179b50d902SRodney W. Grimes 	{'t', 2, BRANCH},
1189b50d902SRodney W. Grimes 	{'w', 2, WFILE},
1199b50d902SRodney W. Grimes 	{'x', 2, EMPTY},
1209b50d902SRodney W. Grimes 	{'y', 2, TR},
1219b50d902SRodney W. Grimes 	{'!', 2, NONSEL},
1229b50d902SRodney W. Grimes 	{':', 0, LABEL},
1239b50d902SRodney W. Grimes 	{'#', 0, COMMENT},
1249b50d902SRodney W. Grimes 	{'=', 1, EMPTY},
1259b50d902SRodney W. Grimes 	{'\0', 0, COMMENT},
1269b50d902SRodney W. Grimes };
1279b50d902SRodney W. Grimes 
1289b50d902SRodney W. Grimes /* The compiled program. */
1299b50d902SRodney W. Grimes struct s_command *prog;
1309b50d902SRodney W. Grimes 
1319b50d902SRodney W. Grimes /*
1329b50d902SRodney W. Grimes  * Compile the program into prog.
1339b50d902SRodney W. Grimes  * Initialise appends.
1349b50d902SRodney W. Grimes  */
1359b50d902SRodney W. Grimes void
1369b50d902SRodney W. Grimes compile()
1379b50d902SRodney W. Grimes {
138ce19262dSJordan K. Hubbard 	*compile_stream(&prog) = NULL;
1399b50d902SRodney W. Grimes 	fixuplabel(prog, NULL);
1409b50d902SRodney W. Grimes 	uselabel();
1418e33c0a0SDavid E. O'Brien 	if ((appends = malloc(sizeof(struct s_appends) * appendnum)) == NULL)
1428e33c0a0SDavid E. O'Brien 		err(1, "malloc");
1438e33c0a0SDavid E. O'Brien 	if ((match = malloc((maxnsub + 1) * sizeof(regmatch_t))) == NULL)
1448e33c0a0SDavid E. O'Brien 		err(1, "malloc");
1459b50d902SRodney W. Grimes }
1469b50d902SRodney W. Grimes 
1479b50d902SRodney W. Grimes #define EATSPACE() do {							\
1489b50d902SRodney W. Grimes 	if (p)								\
149726aebe5SAndrey A. Chernov 		while (*p && isspace((unsigned char)*p))                \
1509b50d902SRodney W. Grimes 			p++;						\
1519b50d902SRodney W. Grimes 	} while (0)
1529b50d902SRodney W. Grimes 
1539b50d902SRodney W. Grimes static struct s_command **
154ce19262dSJordan K. Hubbard compile_stream(link)
1559b50d902SRodney W. Grimes 	struct s_command **link;
1569b50d902SRodney W. Grimes {
157ce19262dSJordan K. Hubbard 	register char *p;
1589b50d902SRodney W. Grimes 	static char lbuf[_POSIX2_LINE_MAX + 1];	/* To save stack */
159ce19262dSJordan K. Hubbard 	struct s_command *cmd, *cmd2, *stack;
1609b50d902SRodney W. Grimes 	struct s_format *fp;
1619b50d902SRodney W. Grimes 	int naddr;				/* Number of addresses */
1629b50d902SRodney W. Grimes 
163ce19262dSJordan K. Hubbard 	stack = 0;
1649b50d902SRodney W. Grimes 	for (;;) {
165c56690efSArchie Cobbs 		if ((p = cu_fgets(lbuf, sizeof(lbuf), NULL)) == NULL) {
166ce19262dSJordan K. Hubbard 			if (stack != 0)
16773a08bb2SPhilippe Charnier 				errx(1, "%lu: %s: unexpected EOF (pending }'s)",
16873a08bb2SPhilippe Charnier 							linenum, fname);
1699b50d902SRodney W. Grimes 			return (link);
1709b50d902SRodney W. Grimes 		}
1719b50d902SRodney W. Grimes 
1729b50d902SRodney W. Grimes semicolon:	EATSPACE();
1739b50d902SRodney W. Grimes 		if (p && (*p == '#' || *p == '\0'))
1749b50d902SRodney W. Grimes 			continue;
1758e33c0a0SDavid E. O'Brien 		if ((*link = cmd = malloc(sizeof(struct s_command))) == NULL)
1768e33c0a0SDavid E. O'Brien 			err(1, "malloc");
1779b50d902SRodney W. Grimes 		link = &cmd->next;
1789b50d902SRodney W. Grimes 		cmd->nonsel = cmd->inrange = 0;
1799b50d902SRodney W. Grimes 		/* First parse the addresses */
1809b50d902SRodney W. Grimes 		naddr = 0;
1819b50d902SRodney W. Grimes 
1829b50d902SRodney W. Grimes /* Valid characters to start an address */
1839b50d902SRodney W. Grimes #define	addrchar(c)	(strchr("0123456789/\\$", (c)))
1849b50d902SRodney W. Grimes 		if (addrchar(*p)) {
1859b50d902SRodney W. Grimes 			naddr++;
1868e33c0a0SDavid E. O'Brien 			if ((cmd->a1 = malloc(sizeof(struct s_addr))) == NULL)
1878e33c0a0SDavid E. O'Brien 				err(1, "malloc");
1889b50d902SRodney W. Grimes 			p = compile_addr(p, cmd->a1);
1899b50d902SRodney W. Grimes 			EATSPACE();				/* EXTENSION */
1909b50d902SRodney W. Grimes 			if (*p == ',') {
1919b50d902SRodney W. Grimes 				p++;
1929b50d902SRodney W. Grimes 				EATSPACE();			/* EXTENSION */
193ce19262dSJordan K. Hubbard 				naddr++;
1948e33c0a0SDavid E. O'Brien 				if ((cmd->a2 = malloc(sizeof(struct s_addr)))
1958e33c0a0SDavid E. O'Brien 				    == NULL)
1968e33c0a0SDavid E. O'Brien 					err(1, "malloc");
1979b50d902SRodney W. Grimes 				p = compile_addr(p, cmd->a2);
198ce19262dSJordan K. Hubbard 				EATSPACE();
199ce19262dSJordan K. Hubbard 			} else
200ce19262dSJordan K. Hubbard 				cmd->a2 = 0;
201ce19262dSJordan K. Hubbard 		} else
202ce19262dSJordan K. Hubbard 			cmd->a1 = cmd->a2 = 0;
2039b50d902SRodney W. Grimes 
2049b50d902SRodney W. Grimes nonsel:		/* Now parse the command */
2059b50d902SRodney W. Grimes 		if (!*p)
20673a08bb2SPhilippe Charnier 			errx(1, "%lu: %s: command expected", linenum, fname);
2079b50d902SRodney W. Grimes 		cmd->code = *p;
2089b50d902SRodney W. Grimes 		for (fp = cmd_fmts; fp->code; fp++)
2099b50d902SRodney W. Grimes 			if (fp->code == *p)
2109b50d902SRodney W. Grimes 				break;
2119b50d902SRodney W. Grimes 		if (!fp->code)
21273a08bb2SPhilippe Charnier 			errx(1, "%lu: %s: invalid command code %c", linenum, fname, *p);
2139b50d902SRodney W. Grimes 		if (naddr > fp->naddr)
21473a08bb2SPhilippe Charnier 			errx(1,
21573a08bb2SPhilippe Charnier 				"%lu: %s: command %c expects up to %d address(es), found %d",
21673a08bb2SPhilippe Charnier 				linenum, fname, *p, fp->naddr, naddr);
2179b50d902SRodney W. Grimes 		switch (fp->args) {
2189b50d902SRodney W. Grimes 		case NONSEL:			/* ! */
2199b50d902SRodney W. Grimes 			p++;
220ce19262dSJordan K. Hubbard 			EATSPACE();
221ce19262dSJordan K. Hubbard 			cmd->nonsel = ! cmd->nonsel;
2229b50d902SRodney W. Grimes 			goto nonsel;
2239b50d902SRodney W. Grimes 		case GROUP:			/* { */
2249b50d902SRodney W. Grimes 			p++;
2259b50d902SRodney W. Grimes 			EATSPACE();
226ce19262dSJordan K. Hubbard 			cmd->next = stack;
227ce19262dSJordan K. Hubbard 			stack = cmd;
228ce19262dSJordan K. Hubbard 			link = &cmd->u.c;
229ce19262dSJordan K. Hubbard 			if (*p)
230ce19262dSJordan K. Hubbard 				goto semicolon;
2319b50d902SRodney W. Grimes 			break;
232ce19262dSJordan K. Hubbard 		case ENDGROUP:
233ce19262dSJordan K. Hubbard 			/*
234ce19262dSJordan K. Hubbard 			 * Short-circuit command processing, since end of
235ce19262dSJordan K. Hubbard 			 * group is really just a noop.
236ce19262dSJordan K. Hubbard 			 */
237ce19262dSJordan K. Hubbard 			cmd->nonsel = 1;
238ce19262dSJordan K. Hubbard 			if (stack == 0)
23973a08bb2SPhilippe Charnier 				errx(1, "%lu: %s: unexpected }", linenum, fname);
240ce19262dSJordan K. Hubbard 			cmd2 = stack;
241ce19262dSJordan K. Hubbard 			stack = cmd2->next;
242ce19262dSJordan K. Hubbard 			cmd2->next = cmd;
243ce19262dSJordan K. Hubbard 			/*FALLTHROUGH*/
2449b50d902SRodney W. Grimes 		case EMPTY:		/* d D g G h H l n N p P q x = \0 */
2459b50d902SRodney W. Grimes 			p++;
2469b50d902SRodney W. Grimes 			EATSPACE();
2479b50d902SRodney W. Grimes 			if (*p == ';') {
2489b50d902SRodney W. Grimes 				p++;
2499b50d902SRodney W. Grimes 				link = &cmd->next;
2509b50d902SRodney W. Grimes 				goto semicolon;
2519b50d902SRodney W. Grimes 			}
2529b50d902SRodney W. Grimes 			if (*p)
25373a08bb2SPhilippe Charnier 				errx(1, "%lu: %s: extra characters at the end of %c command",
25473a08bb2SPhilippe Charnier 						linenum, fname, cmd->code);
2559b50d902SRodney W. Grimes 			break;
2569b50d902SRodney W. Grimes 		case TEXT:			/* a c i */
2579b50d902SRodney W. Grimes 			p++;
2589b50d902SRodney W. Grimes 			EATSPACE();
2599b50d902SRodney W. Grimes 			if (*p != '\\')
26073a08bb2SPhilippe Charnier 				errx(1,
26173a08bb2SPhilippe Charnier "%lu: %s: command %c expects \\ followed by text", linenum, fname, cmd->code);
2629b50d902SRodney W. Grimes 			p++;
2639b50d902SRodney W. Grimes 			EATSPACE();
2649b50d902SRodney W. Grimes 			if (*p)
26573a08bb2SPhilippe Charnier 				errx(1,
26673a08bb2SPhilippe Charnier 				"%lu: %s: extra characters after \\ at the end of %c command",
26773a08bb2SPhilippe Charnier 				linenum, fname, cmd->code);
2689b50d902SRodney W. Grimes 			cmd->t = compile_text();
2699b50d902SRodney W. Grimes 			break;
2709b50d902SRodney W. Grimes 		case COMMENT:			/* \0 # */
2719b50d902SRodney W. Grimes 			break;
2729b50d902SRodney W. Grimes 		case WFILE:			/* w */
2739b50d902SRodney W. Grimes 			p++;
2749b50d902SRodney W. Grimes 			EATSPACE();
2759b50d902SRodney W. Grimes 			if (*p == '\0')
27673a08bb2SPhilippe Charnier 				errx(1, "%lu: %s: filename expected", linenum, fname);
2779b50d902SRodney W. Grimes 			cmd->t = duptoeol(p, "w command");
2789b50d902SRodney W. Grimes 			if (aflag)
2799b50d902SRodney W. Grimes 				cmd->u.fd = -1;
2809b50d902SRodney W. Grimes 			else if ((cmd->u.fd = open(p,
2819b50d902SRodney W. Grimes 			    O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
2829b50d902SRodney W. Grimes 			    DEFFILEMODE)) == -1)
28373a08bb2SPhilippe Charnier 				err(1, "%s", p);
2849b50d902SRodney W. Grimes 			break;
2859b50d902SRodney W. Grimes 		case RFILE:			/* r */
2869b50d902SRodney W. Grimes 			p++;
2879b50d902SRodney W. Grimes 			EATSPACE();
2889b50d902SRodney W. Grimes 			if (*p == '\0')
28973a08bb2SPhilippe Charnier 				errx(1, "%lu: %s: filename expected", linenum, fname);
2909b50d902SRodney W. Grimes 			else
2919b50d902SRodney W. Grimes 				cmd->t = duptoeol(p, "read command");
2929b50d902SRodney W. Grimes 			break;
2939b50d902SRodney W. Grimes 		case BRANCH:			/* b t */
2949b50d902SRodney W. Grimes 			p++;
2959b50d902SRodney W. Grimes 			EATSPACE();
2969b50d902SRodney W. Grimes 			if (*p == '\0')
2979b50d902SRodney W. Grimes 				cmd->t = NULL;
2989b50d902SRodney W. Grimes 			else
2999b50d902SRodney W. Grimes 				cmd->t = duptoeol(p, "branch");
3009b50d902SRodney W. Grimes 			break;
3019b50d902SRodney W. Grimes 		case LABEL:			/* : */
3029b50d902SRodney W. Grimes 			p++;
3039b50d902SRodney W. Grimes 			EATSPACE();
3049b50d902SRodney W. Grimes 			cmd->t = duptoeol(p, "label");
3059b50d902SRodney W. Grimes 			if (strlen(p) == 0)
30673a08bb2SPhilippe Charnier 				errx(1, "%lu: %s: empty label", linenum, fname);
3079b50d902SRodney W. Grimes 			enterlabel(cmd);
3089b50d902SRodney W. Grimes 			break;
3099b50d902SRodney W. Grimes 		case SUBST:			/* s */
3109b50d902SRodney W. Grimes 			p++;
3119b50d902SRodney W. Grimes 			if (*p == '\0' || *p == '\\')
31273a08bb2SPhilippe Charnier 				errx(1,
31373a08bb2SPhilippe Charnier "%lu: %s: substitute pattern can not be delimited by newline or backslash",
31473a08bb2SPhilippe Charnier 					linenum, fname);
3158e33c0a0SDavid E. O'Brien 			if ((cmd->u.s = malloc(sizeof(struct s_subst))) == NULL)
3168e33c0a0SDavid E. O'Brien 				err(1, "malloc");
3179b50d902SRodney W. Grimes 			p = compile_re(p, &cmd->u.s->re);
3189b50d902SRodney W. Grimes 			if (p == NULL)
31973a08bb2SPhilippe Charnier 				errx(1,
32073a08bb2SPhilippe Charnier 				"%lu: %s: unterminated substitute pattern", linenum, fname);
3219b50d902SRodney W. Grimes 			--p;
3229b50d902SRodney W. Grimes 			p = compile_subst(p, cmd->u.s);
3239b50d902SRodney W. Grimes 			p = compile_flags(p, cmd->u.s);
3249b50d902SRodney W. Grimes 			EATSPACE();
3259b50d902SRodney W. Grimes 			if (*p == ';') {
3269b50d902SRodney W. Grimes 				p++;
3279b50d902SRodney W. Grimes 				link = &cmd->next;
3289b50d902SRodney W. Grimes 				goto semicolon;
3299b50d902SRodney W. Grimes 			}
3309b50d902SRodney W. Grimes 			break;
3319b50d902SRodney W. Grimes 		case TR:			/* y */
3329b50d902SRodney W. Grimes 			p++;
3339b50d902SRodney W. Grimes 			p = compile_tr(p, (char **)&cmd->u.y);
3349b50d902SRodney W. Grimes 			EATSPACE();
3359b50d902SRodney W. Grimes 			if (*p == ';') {
3369b50d902SRodney W. Grimes 				p++;
3379b50d902SRodney W. Grimes 				link = &cmd->next;
3389b50d902SRodney W. Grimes 				goto semicolon;
3399b50d902SRodney W. Grimes 			}
3409b50d902SRodney W. Grimes 			if (*p)
34173a08bb2SPhilippe Charnier 				errx(1,
34273a08bb2SPhilippe Charnier "%lu: %s: extra text at the end of a transform command", linenum, fname);
3439b50d902SRodney W. Grimes 			break;
3449b50d902SRodney W. Grimes 		}
3459b50d902SRodney W. Grimes 	}
3469b50d902SRodney W. Grimes }
3479b50d902SRodney W. Grimes 
3489b50d902SRodney W. Grimes /*
3499b50d902SRodney W. Grimes  * Get a delimited string.  P points to the delimeter of the string; d points
3509b50d902SRodney W. Grimes  * to a buffer area.  Newline and delimiter escapes are processed; other
3519b50d902SRodney W. Grimes  * escapes are ignored.
3529b50d902SRodney W. Grimes  *
3539b50d902SRodney W. Grimes  * Returns a pointer to the first character after the final delimiter or NULL
3549b50d902SRodney W. Grimes  * in the case of a non-terminated string.  The character array d is filled
3559b50d902SRodney W. Grimes  * with the processed string.
3569b50d902SRodney W. Grimes  */
3579b50d902SRodney W. Grimes static char *
3589b50d902SRodney W. Grimes compile_delimited(p, d)
3599b50d902SRodney W. Grimes 	char *p, *d;
3609b50d902SRodney W. Grimes {
3619b50d902SRodney W. Grimes 	char c;
3629b50d902SRodney W. Grimes 
3639b50d902SRodney W. Grimes 	c = *p++;
3649b50d902SRodney W. Grimes 	if (c == '\0')
3659b50d902SRodney W. Grimes 		return (NULL);
3669b50d902SRodney W. Grimes 	else if (c == '\\')
36773a08bb2SPhilippe Charnier 		errx(1, "%lu: %s: \\ can not be used as a string delimiter",
36873a08bb2SPhilippe Charnier 				linenum, fname);
3699b50d902SRodney W. Grimes 	else if (c == '\n')
37073a08bb2SPhilippe Charnier 		errx(1, "%lu: %s: newline can not be used as a string delimiter",
37173a08bb2SPhilippe Charnier 				linenum, fname);
3729b50d902SRodney W. Grimes 	while (*p) {
373ce19262dSJordan K. Hubbard 		if (*p == '[') {
374ce19262dSJordan K. Hubbard 			if ((d = compile_ccl(&p, d)) == NULL)
37573a08bb2SPhilippe Charnier 				errx(1, "%lu: %s: unbalanced brackets ([])", linenum, fname);
376ce19262dSJordan K. Hubbard 			continue;
377ce19262dSJordan K. Hubbard 		} else if (*p == '\\' && p[1] == '[') {
378ce19262dSJordan K. Hubbard 			*d++ = *p++;
379ce19262dSJordan K. Hubbard 		} else if (*p == '\\' && p[1] == c)
3809b50d902SRodney W. Grimes 			p++;
3819b50d902SRodney W. Grimes 		else if (*p == '\\' && p[1] == 'n') {
3829b50d902SRodney W. Grimes 			*d++ = '\n';
3839b50d902SRodney W. Grimes 			p += 2;
3849b50d902SRodney W. Grimes 			continue;
3859b50d902SRodney W. Grimes 		} else if (*p == '\\' && p[1] == '\\')
3869b50d902SRodney W. Grimes 			*d++ = *p++;
3879b50d902SRodney W. Grimes 		else if (*p == c) {
3889b50d902SRodney W. Grimes 			*d = '\0';
3899b50d902SRodney W. Grimes 			return (p + 1);
3909b50d902SRodney W. Grimes 		}
3919b50d902SRodney W. Grimes 		*d++ = *p++;
3929b50d902SRodney W. Grimes 	}
3939b50d902SRodney W. Grimes 	return (NULL);
3949b50d902SRodney W. Grimes }
3959b50d902SRodney W. Grimes 
396ce19262dSJordan K. Hubbard 
397ce19262dSJordan K. Hubbard /* compile_ccl: expand a POSIX character class */
398ce19262dSJordan K. Hubbard static char *
399ce19262dSJordan K. Hubbard compile_ccl(sp, t)
400ce19262dSJordan K. Hubbard 	char **sp;
401ce19262dSJordan K. Hubbard 	char *t;
402ce19262dSJordan K. Hubbard {
403ce19262dSJordan K. Hubbard 	int c, d;
404ce19262dSJordan K. Hubbard 	char *s = *sp;
405ce19262dSJordan K. Hubbard 
406ce19262dSJordan K. Hubbard 	*t++ = *s++;
407ce19262dSJordan K. Hubbard 	if (*s == '^')
408ce19262dSJordan K. Hubbard 		*t++ = *s++;
409ce19262dSJordan K. Hubbard 	if (*s == ']')
410ce19262dSJordan K. Hubbard 		*t++ = *s++;
411ce19262dSJordan K. Hubbard 	for (; *s && (*t = *s) != ']'; s++, t++)
412ce19262dSJordan K. Hubbard 		if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) {
413ce19262dSJordan K. Hubbard 			*++t = *++s, t++, s++;
414ce19262dSJordan K. Hubbard 			for (c = *s; (*t = *s) != ']' || c != d; s++, t++)
415ce19262dSJordan K. Hubbard 				if ((c = *s) == '\0')
416ce19262dSJordan K. Hubbard 					return NULL;
417ce19262dSJordan K. Hubbard 		} else if (*s == '\\' && s[1] == 'n')
418ce19262dSJordan K. Hubbard 			    *t = '\n', s++;
419ce19262dSJordan K. Hubbard 	return (*s == ']') ? *sp = ++s, ++t : NULL;
420ce19262dSJordan K. Hubbard }
421ce19262dSJordan K. Hubbard 
4229b50d902SRodney W. Grimes /*
4239b50d902SRodney W. Grimes  * Get a regular expression.  P points to the delimiter of the regular
4249b50d902SRodney W. Grimes  * expression; repp points to the address of a regexp pointer.  Newline
4259b50d902SRodney W. Grimes  * and delimiter escapes are processed; other escapes are ignored.
4269b50d902SRodney W. Grimes  * Returns a pointer to the first character after the final delimiter
4279b50d902SRodney W. Grimes  * or NULL in the case of a non terminated regular expression.  The regexp
4289b50d902SRodney W. Grimes  * pointer is set to the compiled regular expression.
4299b50d902SRodney W. Grimes  * Cflags are passed to regcomp.
4309b50d902SRodney W. Grimes  */
4319b50d902SRodney W. Grimes static char *
4329b50d902SRodney W. Grimes compile_re(p, repp)
4339b50d902SRodney W. Grimes 	char *p;
4349b50d902SRodney W. Grimes 	regex_t **repp;
4359b50d902SRodney W. Grimes {
4369b50d902SRodney W. Grimes 	int eval;
4379b50d902SRodney W. Grimes 	char re[_POSIX2_LINE_MAX + 1];
4389b50d902SRodney W. Grimes 
4399b50d902SRodney W. Grimes 	p = compile_delimited(p, re);
4409b50d902SRodney W. Grimes 	if (p && strlen(re) == 0) {
4419b50d902SRodney W. Grimes 		*repp = NULL;
4429b50d902SRodney W. Grimes 		return (p);
4439b50d902SRodney W. Grimes 	}
4448e33c0a0SDavid E. O'Brien 	if ((*repp = malloc(sizeof(regex_t))) == NULL)
4458e33c0a0SDavid E. O'Brien 		err(1, "malloc");
446175de1e6SBrian Feldman 	if (p && (eval = regcomp(*repp, re, rflags)) != 0)
44773a08bb2SPhilippe Charnier 		errx(1, "%lu: %s: RE error: %s",
44873a08bb2SPhilippe Charnier 				linenum, fname, strregerror(eval, *repp));
4499b50d902SRodney W. Grimes 	if (maxnsub < (*repp)->re_nsub)
4509b50d902SRodney W. Grimes 		maxnsub = (*repp)->re_nsub;
4519b50d902SRodney W. Grimes 	return (p);
4529b50d902SRodney W. Grimes }
4539b50d902SRodney W. Grimes 
4549b50d902SRodney W. Grimes /*
4559b50d902SRodney W. Grimes  * Compile the substitution string of a regular expression and set res to
4569b50d902SRodney W. Grimes  * point to a saved copy of it.  Nsub is the number of parenthesized regular
4579b50d902SRodney W. Grimes  * expressions.
4589b50d902SRodney W. Grimes  */
4599b50d902SRodney W. Grimes static char *
4609b50d902SRodney W. Grimes compile_subst(p, s)
4619b50d902SRodney W. Grimes 	char *p;
4629b50d902SRodney W. Grimes 	struct s_subst *s;
4639b50d902SRodney W. Grimes {
4649b50d902SRodney W. Grimes 	static char lbuf[_POSIX2_LINE_MAX + 1];
4659b50d902SRodney W. Grimes 	int asize, ref, size;
4669b50d902SRodney W. Grimes 	char c, *text, *op, *sp;
467c56690efSArchie Cobbs 	int more = 0;
4689b50d902SRodney W. Grimes 
4699b50d902SRodney W. Grimes 	c = *p++;			/* Terminator character */
4709b50d902SRodney W. Grimes 	if (c == '\0')
4719b50d902SRodney W. Grimes 		return (NULL);
4729b50d902SRodney W. Grimes 
4739b50d902SRodney W. Grimes 	s->maxbref = 0;
4749b50d902SRodney W. Grimes 	s->linenum = linenum;
4759b50d902SRodney W. Grimes 	asize = 2 * _POSIX2_LINE_MAX + 1;
4768e33c0a0SDavid E. O'Brien 	if ((text = malloc(asize)) == NULL)
4778e33c0a0SDavid E. O'Brien 		err(1, "malloc");
4789b50d902SRodney W. Grimes 	size = 0;
4799b50d902SRodney W. Grimes 	do {
4809b50d902SRodney W. Grimes 		op = sp = text + size;
4819b50d902SRodney W. Grimes 		for (; *p; p++) {
4829b50d902SRodney W. Grimes 			if (*p == '\\') {
4839b50d902SRodney W. Grimes 				p++;
4849b50d902SRodney W. Grimes 				if (strchr("123456789", *p) != NULL) {
4859b50d902SRodney W. Grimes 					*sp++ = '\\';
4869b50d902SRodney W. Grimes 					ref = *p - '0';
4879b50d902SRodney W. Grimes 					if (s->re != NULL &&
4889b50d902SRodney W. Grimes 					    ref > s->re->re_nsub)
48973a08bb2SPhilippe Charnier 						errx(1, "%lu: %s: \\%c not defined in the RE",
49073a08bb2SPhilippe Charnier 								linenum, fname, *p);
4919b50d902SRodney W. Grimes 					if (s->maxbref < ref)
4929b50d902SRodney W. Grimes 						s->maxbref = ref;
4939b50d902SRodney W. Grimes 				} else if (*p == '&' || *p == '\\')
4949b50d902SRodney W. Grimes 					*sp++ = '\\';
4959b50d902SRodney W. Grimes 			} else if (*p == c) {
496c56690efSArchie Cobbs 				if (*++p == '\0' && more) {
497c56690efSArchie Cobbs 					if (cu_fgets(lbuf, sizeof(lbuf), &more))
4981a6583daSArchie Cobbs 						p = lbuf;
4991a6583daSArchie Cobbs 				}
5009b50d902SRodney W. Grimes 				*sp++ = '\0';
5019b50d902SRodney W. Grimes 				size += sp - op;
5028e33c0a0SDavid E. O'Brien 				if ((s->new = realloc(text, size)) == NULL)
5038e33c0a0SDavid E. O'Brien 					err(1, "realloc");
5049b50d902SRodney W. Grimes 				return (p);
5059b50d902SRodney W. Grimes 			} else if (*p == '\n') {
50673a08bb2SPhilippe Charnier 				errx(1,
50773a08bb2SPhilippe Charnier "%lu: %s: unescaped newline inside substitute pattern", linenum, fname);
5089b50d902SRodney W. Grimes 				/* NOTREACHED */
5099b50d902SRodney W. Grimes 			}
5109b50d902SRodney W. Grimes 			*sp++ = *p;
5119b50d902SRodney W. Grimes 		}
5129b50d902SRodney W. Grimes 		size += sp - op;
5139b50d902SRodney W. Grimes 		if (asize - size < _POSIX2_LINE_MAX + 1) {
5149b50d902SRodney W. Grimes 			asize *= 2;
5158e33c0a0SDavid E. O'Brien 			if ((text = realloc(text, asize)) == NULL)
5168e33c0a0SDavid E. O'Brien 				err(1, "realloc");
5179b50d902SRodney W. Grimes 		}
518c56690efSArchie Cobbs 	} while (cu_fgets(p = lbuf, sizeof(lbuf), &more));
51973a08bb2SPhilippe Charnier 	errx(1, "%lu: %s: unterminated substitute in regular expression",
52073a08bb2SPhilippe Charnier 			linenum, fname);
5219b50d902SRodney W. Grimes 	/* NOTREACHED */
5229b50d902SRodney W. Grimes }
5239b50d902SRodney W. Grimes 
5249b50d902SRodney W. Grimes /*
5259b50d902SRodney W. Grimes  * Compile the flags of the s command
5269b50d902SRodney W. Grimes  */
5279b50d902SRodney W. Grimes static char *
5289b50d902SRodney W. Grimes compile_flags(p, s)
5299b50d902SRodney W. Grimes 	char *p;
5309b50d902SRodney W. Grimes 	struct s_subst *s;
5319b50d902SRodney W. Grimes {
5329b50d902SRodney W. Grimes 	int gn;			/* True if we have seen g or n */
5339b50d902SRodney W. Grimes 	char wfile[_POSIX2_LINE_MAX + 1], *q;
5349b50d902SRodney W. Grimes 
5359b50d902SRodney W. Grimes 	s->n = 1;				/* Default */
5369b50d902SRodney W. Grimes 	s->p = 0;
5379b50d902SRodney W. Grimes 	s->wfile = NULL;
5389b50d902SRodney W. Grimes 	s->wfd = -1;
5399b50d902SRodney W. Grimes 	for (gn = 0;;) {
5409b50d902SRodney W. Grimes 		EATSPACE();			/* EXTENSION */
5419b50d902SRodney W. Grimes 		switch (*p) {
5429b50d902SRodney W. Grimes 		case 'g':
5439b50d902SRodney W. Grimes 			if (gn)
54473a08bb2SPhilippe Charnier 				errx(1,
54573a08bb2SPhilippe Charnier "%lu: %s: more than one number or 'g' in substitute flags", linenum, fname);
5469b50d902SRodney W. Grimes 			gn = 1;
5479b50d902SRodney W. Grimes 			s->n = 0;
5489b50d902SRodney W. Grimes 			break;
5499b50d902SRodney W. Grimes 		case '\0':
5509b50d902SRodney W. Grimes 		case '\n':
5519b50d902SRodney W. Grimes 		case ';':
5529b50d902SRodney W. Grimes 			return (p);
5539b50d902SRodney W. Grimes 		case 'p':
5549b50d902SRodney W. Grimes 			s->p = 1;
5559b50d902SRodney W. Grimes 			break;
5569b50d902SRodney W. Grimes 		case '1': case '2': case '3':
5579b50d902SRodney W. Grimes 		case '4': case '5': case '6':
5589b50d902SRodney W. Grimes 		case '7': case '8': case '9':
5599b50d902SRodney W. Grimes 			if (gn)
56073a08bb2SPhilippe Charnier 				errx(1,
56173a08bb2SPhilippe Charnier "%lu: %s: more than one number or 'g' in substitute flags", linenum, fname);
5629b50d902SRodney W. Grimes 			gn = 1;
5639b50d902SRodney W. Grimes 			/* XXX Check for overflow */
5649b50d902SRodney W. Grimes 			s->n = (int)strtol(p, &p, 10);
5659b50d902SRodney W. Grimes 			break;
5669b50d902SRodney W. Grimes 		case 'w':
5679b50d902SRodney W. Grimes 			p++;
5689b50d902SRodney W. Grimes #ifdef HISTORIC_PRACTICE
5699b50d902SRodney W. Grimes 			if (*p != ' ') {
57073a08bb2SPhilippe Charnier 				warnx("%lu: %s: space missing before w wfile", linenum, fname);
5719b50d902SRodney W. Grimes 				return (p);
5729b50d902SRodney W. Grimes 			}
5739b50d902SRodney W. Grimes #endif
5749b50d902SRodney W. Grimes 			EATSPACE();
5759b50d902SRodney W. Grimes 			q = wfile;
5769b50d902SRodney W. Grimes 			while (*p) {
5779b50d902SRodney W. Grimes 				if (*p == '\n')
5789b50d902SRodney W. Grimes 					break;
5799b50d902SRodney W. Grimes 				*q++ = *p++;
5809b50d902SRodney W. Grimes 			}
5819b50d902SRodney W. Grimes 			*q = '\0';
5829b50d902SRodney W. Grimes 			if (q == wfile)
58373a08bb2SPhilippe Charnier 				errx(1, "%lu: %s: no wfile specified", linenum, fname);
5849b50d902SRodney W. Grimes 			s->wfile = strdup(wfile);
5859b50d902SRodney W. Grimes 			if (!aflag && (s->wfd = open(wfile,
5869b50d902SRodney W. Grimes 			    O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
5879b50d902SRodney W. Grimes 			    DEFFILEMODE)) == -1)
58873a08bb2SPhilippe Charnier 				err(1, "%s", wfile);
5899b50d902SRodney W. Grimes 			return (p);
5909b50d902SRodney W. Grimes 		default:
59173a08bb2SPhilippe Charnier 			errx(1, "%lu: %s: bad flag in substitute command: '%c'",
59273a08bb2SPhilippe Charnier 					linenum, fname, *p);
5939b50d902SRodney W. Grimes 			break;
5949b50d902SRodney W. Grimes 		}
5959b50d902SRodney W. Grimes 		p++;
5969b50d902SRodney W. Grimes 	}
5979b50d902SRodney W. Grimes }
5989b50d902SRodney W. Grimes 
5999b50d902SRodney W. Grimes /*
6009b50d902SRodney W. Grimes  * Compile a translation set of strings into a lookup table.
6019b50d902SRodney W. Grimes  */
6029b50d902SRodney W. Grimes static char *
6039b50d902SRodney W. Grimes compile_tr(p, transtab)
6049b50d902SRodney W. Grimes 	char *p;
6059b50d902SRodney W. Grimes 	char **transtab;
6069b50d902SRodney W. Grimes {
6079b50d902SRodney W. Grimes 	int i;
6089b50d902SRodney W. Grimes 	char *lt, *op, *np;
6099b50d902SRodney W. Grimes 	char old[_POSIX2_LINE_MAX + 1];
6109b50d902SRodney W. Grimes 	char new[_POSIX2_LINE_MAX + 1];
6119b50d902SRodney W. Grimes 
6129b50d902SRodney W. Grimes 	if (*p == '\0' || *p == '\\')
61373a08bb2SPhilippe Charnier 		errx(1,
61473a08bb2SPhilippe Charnier 	"%lu: %s: transform pattern can not be delimited by newline or backslash",
61573a08bb2SPhilippe Charnier 			linenum, fname);
6169b50d902SRodney W. Grimes 	p = compile_delimited(p, old);
61773a08bb2SPhilippe Charnier 	if (p == NULL)
61873a08bb2SPhilippe Charnier 		errx(1, "%lu: %s: unterminated transform source string",
61973a08bb2SPhilippe Charnier 				linenum, fname);
6209b50d902SRodney W. Grimes 	p = compile_delimited(--p, new);
62173a08bb2SPhilippe Charnier 	if (p == NULL)
62273a08bb2SPhilippe Charnier 		errx(1, "%lu: %s: unterminated transform target string",
62373a08bb2SPhilippe Charnier 				linenum, fname);
6249b50d902SRodney W. Grimes 	EATSPACE();
62573a08bb2SPhilippe Charnier 	if (strlen(new) != strlen(old))
62673a08bb2SPhilippe Charnier 		errx(1, "%lu: %s: transform strings are not the same length",
62773a08bb2SPhilippe Charnier 				linenum, fname);
6289b50d902SRodney W. Grimes 	/* We assume characters are 8 bits */
6298e33c0a0SDavid E. O'Brien 	if ((lt = malloc(UCHAR_MAX)) == NULL)
6308e33c0a0SDavid E. O'Brien 		err(1, "malloc");
6319b50d902SRodney W. Grimes 	for (i = 0; i <= UCHAR_MAX; i++)
6329b50d902SRodney W. Grimes 		lt[i] = (char)i;
6339b50d902SRodney W. Grimes 	for (op = old, np = new; *op; op++, np++)
6349b50d902SRodney W. Grimes 		lt[(u_char)*op] = *np;
6359b50d902SRodney W. Grimes 	*transtab = lt;
6369b50d902SRodney W. Grimes 	return (p);
6379b50d902SRodney W. Grimes }
6389b50d902SRodney W. Grimes 
6399b50d902SRodney W. Grimes /*
6409b50d902SRodney W. Grimes  * Compile the text following an a or i command.
6419b50d902SRodney W. Grimes  */
6429b50d902SRodney W. Grimes static char *
6439b50d902SRodney W. Grimes compile_text()
6449b50d902SRodney W. Grimes {
64549e65599SBruce Evans 	int asize, esc_nl, size;
6469b50d902SRodney W. Grimes 	char *text, *p, *op, *s;
6479b50d902SRodney W. Grimes 	char lbuf[_POSIX2_LINE_MAX + 1];
6489b50d902SRodney W. Grimes 
6499b50d902SRodney W. Grimes 	asize = 2 * _POSIX2_LINE_MAX + 1;
6508e33c0a0SDavid E. O'Brien 	if ((text = malloc(asize)) == NULL)
6518e33c0a0SDavid E. O'Brien 		err(1, "malloc");
6529b50d902SRodney W. Grimes 	size = 0;
653c56690efSArchie Cobbs 	while (cu_fgets(lbuf, sizeof(lbuf), NULL)) {
6549b50d902SRodney W. Grimes 		op = s = text + size;
6559b50d902SRodney W. Grimes 		p = lbuf;
6569b50d902SRodney W. Grimes 		EATSPACE();
65749e65599SBruce Evans 		for (esc_nl = 0; *p != '\0'; p++) {
65849e65599SBruce Evans 			if (*p == '\\' && p[1] != '\0' && *++p == '\n')
65949e65599SBruce Evans 				esc_nl = 1;
6609b50d902SRodney W. Grimes 			*s++ = *p;
6619b50d902SRodney W. Grimes 		}
6629b50d902SRodney W. Grimes 		size += s - op;
66349e65599SBruce Evans 		if (!esc_nl) {
6649b50d902SRodney W. Grimes 			*s = '\0';
6659b50d902SRodney W. Grimes 			break;
6669b50d902SRodney W. Grimes 		}
6679b50d902SRodney W. Grimes 		if (asize - size < _POSIX2_LINE_MAX + 1) {
6689b50d902SRodney W. Grimes 			asize *= 2;
6698e33c0a0SDavid E. O'Brien 			if ((text = realloc(text, asize)) == NULL)
6708e33c0a0SDavid E. O'Brien 				err(1, "realloc");
6719b50d902SRodney W. Grimes 		}
6729b50d902SRodney W. Grimes 	}
67313ede3c0SBrian Somers 	text[size] = '\0';
6748e33c0a0SDavid E. O'Brien 	if ((p = realloc(text, size + 1)) == NULL)
6758e33c0a0SDavid E. O'Brien 		err(1, "realloc");
6768e33c0a0SDavid E. O'Brien 	return (p);
6779b50d902SRodney W. Grimes }
6789b50d902SRodney W. Grimes 
6799b50d902SRodney W. Grimes /*
6809b50d902SRodney W. Grimes  * Get an address and return a pointer to the first character after
6819b50d902SRodney W. Grimes  * it.  Fill the structure pointed to according to the address.
6829b50d902SRodney W. Grimes  */
6839b50d902SRodney W. Grimes static char *
6849b50d902SRodney W. Grimes compile_addr(p, a)
6859b50d902SRodney W. Grimes 	char *p;
6869b50d902SRodney W. Grimes 	struct s_addr *a;
6879b50d902SRodney W. Grimes {
6889b50d902SRodney W. Grimes 	char *end;
6899b50d902SRodney W. Grimes 
6909b50d902SRodney W. Grimes 	switch (*p) {
6919b50d902SRodney W. Grimes 	case '\\':				/* Context address */
6929b50d902SRodney W. Grimes 		++p;
6939b50d902SRodney W. Grimes 		/* FALLTHROUGH */
6949b50d902SRodney W. Grimes 	case '/':				/* Context address */
6959b50d902SRodney W. Grimes 		p = compile_re(p, &a->u.r);
6969b50d902SRodney W. Grimes 		if (p == NULL)
69773a08bb2SPhilippe Charnier 			errx(1, "%lu: %s: unterminated regular expression", linenum, fname);
6989b50d902SRodney W. Grimes 		a->type = AT_RE;
6999b50d902SRodney W. Grimes 		return (p);
7009b50d902SRodney W. Grimes 
7019b50d902SRodney W. Grimes 	case '$':				/* Last line */
7029b50d902SRodney W. Grimes 		a->type = AT_LAST;
7039b50d902SRodney W. Grimes 		return (p + 1);
7049b50d902SRodney W. Grimes 						/* Line number */
7059b50d902SRodney W. Grimes 	case '0': case '1': case '2': case '3': case '4':
7069b50d902SRodney W. Grimes 	case '5': case '6': case '7': case '8': case '9':
7079b50d902SRodney W. Grimes 		a->type = AT_LINE;
7089b50d902SRodney W. Grimes 		a->u.l = strtol(p, &end, 10);
7099b50d902SRodney W. Grimes 		return (end);
7109b50d902SRodney W. Grimes 	default:
71173a08bb2SPhilippe Charnier 		errx(1, "%lu: %s: expected context address", linenum, fname);
7129b50d902SRodney W. Grimes 		return (NULL);
7139b50d902SRodney W. Grimes 	}
7149b50d902SRodney W. Grimes }
7159b50d902SRodney W. Grimes 
7169b50d902SRodney W. Grimes /*
7179b50d902SRodney W. Grimes  * duptoeol --
7189b50d902SRodney W. Grimes  *	Return a copy of all the characters up to \n or \0.
7199b50d902SRodney W. Grimes  */
7209b50d902SRodney W. Grimes static char *
7219b50d902SRodney W. Grimes duptoeol(s, ctype)
7229b50d902SRodney W. Grimes 	register char *s;
7239b50d902SRodney W. Grimes 	char *ctype;
7249b50d902SRodney W. Grimes {
7259b50d902SRodney W. Grimes 	size_t len;
7269b50d902SRodney W. Grimes 	int ws;
7278e33c0a0SDavid E. O'Brien 	char *p, *start;
7289b50d902SRodney W. Grimes 
7299b50d902SRodney W. Grimes 	ws = 0;
7309b50d902SRodney W. Grimes 	for (start = s; *s != '\0' && *s != '\n'; ++s)
731726aebe5SAndrey A. Chernov 		ws = isspace((unsigned char)*s);
7329b50d902SRodney W. Grimes 	*s = '\0';
7339b50d902SRodney W. Grimes 	if (ws)
73473a08bb2SPhilippe Charnier 		warnx("%lu: %s: whitespace after %s", linenum, fname, ctype);
7359b50d902SRodney W. Grimes 	len = s - start + 1;
7368e33c0a0SDavid E. O'Brien 	if ((p = malloc(len)) == NULL)
7378e33c0a0SDavid E. O'Brien 		err(1, "malloc");
7388e33c0a0SDavid E. O'Brien 	return (memmove(p, start, len));
7399b50d902SRodney W. Grimes }
7409b50d902SRodney W. Grimes 
7419b50d902SRodney W. Grimes /*
7429b50d902SRodney W. Grimes  * Convert goto label names to addresses, and count a and r commands, in
7439b50d902SRodney W. Grimes  * the given subset of the script.  Free the memory used by labels in b
7449b50d902SRodney W. Grimes  * and t commands (but not by :).
7459b50d902SRodney W. Grimes  *
7469b50d902SRodney W. Grimes  * TODO: Remove } nodes
7479b50d902SRodney W. Grimes  */
7489b50d902SRodney W. Grimes static void
7499b50d902SRodney W. Grimes fixuplabel(cp, end)
7509b50d902SRodney W. Grimes 	struct s_command *cp, *end;
7519b50d902SRodney W. Grimes {
7529b50d902SRodney W. Grimes 
7539b50d902SRodney W. Grimes 	for (; cp != end; cp = cp->next)
7549b50d902SRodney W. Grimes 		switch (cp->code) {
7559b50d902SRodney W. Grimes 		case 'a':
7569b50d902SRodney W. Grimes 		case 'r':
7579b50d902SRodney W. Grimes 			appendnum++;
7589b50d902SRodney W. Grimes 			break;
7599b50d902SRodney W. Grimes 		case 'b':
7609b50d902SRodney W. Grimes 		case 't':
7619b50d902SRodney W. Grimes 			/* Resolve branch target. */
7629b50d902SRodney W. Grimes 			if (cp->t == NULL) {
7639b50d902SRodney W. Grimes 				cp->u.c = NULL;
7649b50d902SRodney W. Grimes 				break;
7659b50d902SRodney W. Grimes 			}
7669b50d902SRodney W. Grimes 			if ((cp->u.c = findlabel(cp->t)) == NULL)
76773a08bb2SPhilippe Charnier 				errx(1, "%lu: %s: undefined label '%s'", linenum, fname, cp->t);
7689b50d902SRodney W. Grimes 			free(cp->t);
7699b50d902SRodney W. Grimes 			break;
7709b50d902SRodney W. Grimes 		case '{':
7719b50d902SRodney W. Grimes 			/* Do interior commands. */
7729b50d902SRodney W. Grimes 			fixuplabel(cp->u.c, cp->next);
7739b50d902SRodney W. Grimes 			break;
7749b50d902SRodney W. Grimes 		}
7759b50d902SRodney W. Grimes }
7769b50d902SRodney W. Grimes 
7779b50d902SRodney W. Grimes /*
7789b50d902SRodney W. Grimes  * Associate the given command label for later lookup.
7799b50d902SRodney W. Grimes  */
7809b50d902SRodney W. Grimes static void
7819b50d902SRodney W. Grimes enterlabel(cp)
7829b50d902SRodney W. Grimes 	struct s_command *cp;
7839b50d902SRodney W. Grimes {
7849b50d902SRodney W. Grimes 	register struct labhash **lhp, *lh;
7859b50d902SRodney W. Grimes 	register u_char *p;
7869b50d902SRodney W. Grimes 	register u_int h, c;
7879b50d902SRodney W. Grimes 
7889b50d902SRodney W. Grimes 	for (h = 0, p = (u_char *)cp->t; (c = *p) != 0; p++)
7899b50d902SRodney W. Grimes 		h = (h << 5) + h + c;
7909b50d902SRodney W. Grimes 	lhp = &labels[h & LHMASK];
7919b50d902SRodney W. Grimes 	for (lh = *lhp; lh != NULL; lh = lh->lh_next)
7929b50d902SRodney W. Grimes 		if (lh->lh_hash == h && strcmp(cp->t, lh->lh_cmd->t) == 0)
79373a08bb2SPhilippe Charnier 			errx(1, "%lu: %s: duplicate label '%s'", linenum, fname, cp->t);
7948e33c0a0SDavid E. O'Brien 	if ((lh = malloc(sizeof *lh)) == NULL)
7958e33c0a0SDavid E. O'Brien 		err(1, "malloc");
7969b50d902SRodney W. Grimes 	lh->lh_next = *lhp;
7979b50d902SRodney W. Grimes 	lh->lh_hash = h;
7989b50d902SRodney W. Grimes 	lh->lh_cmd = cp;
7999b50d902SRodney W. Grimes 	lh->lh_ref = 0;
8009b50d902SRodney W. Grimes 	*lhp = lh;
8019b50d902SRodney W. Grimes }
8029b50d902SRodney W. Grimes 
8039b50d902SRodney W. Grimes /*
8049b50d902SRodney W. Grimes  * Find the label contained in the command l in the command linked
8059b50d902SRodney W. Grimes  * list cp.  L is excluded from the search.  Return NULL if not found.
8069b50d902SRodney W. Grimes  */
8079b50d902SRodney W. Grimes static struct s_command *
8089b50d902SRodney W. Grimes findlabel(name)
8099b50d902SRodney W. Grimes 	char *name;
8109b50d902SRodney W. Grimes {
8119b50d902SRodney W. Grimes 	register struct labhash *lh;
8129b50d902SRodney W. Grimes 	register u_char *p;
8139b50d902SRodney W. Grimes 	register u_int h, c;
8149b50d902SRodney W. Grimes 
8159b50d902SRodney W. Grimes 	for (h = 0, p = (u_char *)name; (c = *p) != 0; p++)
8169b50d902SRodney W. Grimes 		h = (h << 5) + h + c;
8179b50d902SRodney W. Grimes 	for (lh = labels[h & LHMASK]; lh != NULL; lh = lh->lh_next) {
8189b50d902SRodney W. Grimes 		if (lh->lh_hash == h && strcmp(name, lh->lh_cmd->t) == 0) {
8199b50d902SRodney W. Grimes 			lh->lh_ref = 1;
8209b50d902SRodney W. Grimes 			return (lh->lh_cmd);
8219b50d902SRodney W. Grimes 		}
8229b50d902SRodney W. Grimes 	}
8239b50d902SRodney W. Grimes 	return (NULL);
8249b50d902SRodney W. Grimes }
8259b50d902SRodney W. Grimes 
8269b50d902SRodney W. Grimes /*
8279b50d902SRodney W. Grimes  * Warn about any unused labels.  As a side effect, release the label hash
8289b50d902SRodney W. Grimes  * table space.
8299b50d902SRodney W. Grimes  */
8309b50d902SRodney W. Grimes static void
8319b50d902SRodney W. Grimes uselabel()
8329b50d902SRodney W. Grimes {
8339b50d902SRodney W. Grimes 	register struct labhash *lh, *next;
8349b50d902SRodney W. Grimes 	register int i;
8359b50d902SRodney W. Grimes 
8369b50d902SRodney W. Grimes 	for (i = 0; i < LHSZ; i++) {
8379b50d902SRodney W. Grimes 		for (lh = labels[i]; lh != NULL; lh = next) {
8389b50d902SRodney W. Grimes 			next = lh->lh_next;
8399b50d902SRodney W. Grimes 			if (!lh->lh_ref)
84073a08bb2SPhilippe Charnier 				warnx("%lu: %s: unused label '%s'",
84173a08bb2SPhilippe Charnier 				    linenum, fname, lh->lh_cmd->t);
8429b50d902SRodney W. Grimes 			free(lh);
8439b50d902SRodney W. Grimes 		}
8449b50d902SRodney W. Grimes 	}
8459b50d902SRodney W. Grimes }
846