1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1992 Diomidis Spinellis.
5 * Copyright (c) 1992, 1993
6 *	The Regents of the University of California.  All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Diomidis Spinellis of Imperial College, University of London.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD$");
38
39#ifndef lint
40static const char sccsid[] = "@(#)compile.c	8.1 (Berkeley) 6/6/93";
41#endif
42
43#include <sys/types.h>
44#include <sys/stat.h>
45
46#include <ctype.h>
47#include <err.h>
48#include <errno.h>
49#include <fcntl.h>
50#include <limits.h>
51#include <regex.h>
52#include <stdbool.h>
53#include <stdio.h>
54#include <stdlib.h>
55#include <string.h>
56#include <wchar.h>
57
58#include "defs.h"
59#include "extern.h"
60
61#define LHSZ	128
62#define	LHMASK	(LHSZ - 1)
63static struct labhash {
64	struct	labhash *lh_next;
65	u_int	lh_hash;
66	struct	s_command *lh_cmd;
67	int	lh_ref;
68} *labels[LHSZ];
69
70static char	 *compile_addr(char *, struct s_addr *);
71static char	 *compile_ccl(char **, char *);
72static char	 *compile_delimited(char *, char *, int);
73static char	 *compile_flags(char *, struct s_subst *);
74static regex_t	 *compile_re(char *, int);
75static char	 *compile_subst(char *, struct s_subst *);
76static char	 *compile_text(void);
77static char	 *compile_tr(char *, struct s_tr **);
78static struct s_command
79		**compile_stream(struct s_command **);
80static char	 *duptoeol(char *, const char *);
81static void	  enterlabel(struct s_command *);
82static struct s_command
83		 *findlabel(char *);
84static void	  fixuplabel(struct s_command *, struct s_command *);
85static void	  uselabel(void);
86
87/*
88 * Command specification.  This is used to drive the command parser.
89 */
90struct s_format {
91	char code;				/* Command code */
92	int naddr;				/* Number of address args */
93	enum e_args args;			/* Argument type */
94};
95
96static struct s_format cmd_fmts[] = {
97	{'{', 2, GROUP},
98	{'}', 0, ENDGROUP},
99	{'a', 1, TEXT},
100	{'b', 2, BRANCH},
101	{'c', 2, TEXT},
102	{'d', 2, EMPTY},
103	{'D', 2, EMPTY},
104	{'g', 2, EMPTY},
105	{'G', 2, EMPTY},
106	{'h', 2, EMPTY},
107	{'H', 2, EMPTY},
108	{'i', 1, TEXT},
109	{'l', 2, EMPTY},
110	{'n', 2, EMPTY},
111	{'N', 2, EMPTY},
112	{'p', 2, EMPTY},
113	{'P', 2, EMPTY},
114	{'q', 1, EMPTY},
115	{'r', 1, RFILE},
116	{'s', 2, SUBST},
117	{'t', 2, BRANCH},
118	{'w', 2, WFILE},
119	{'x', 2, EMPTY},
120	{'y', 2, TR},
121	{'!', 2, NONSEL},
122	{':', 0, LABEL},
123	{'#', 0, COMMENT},
124	{'=', 1, EMPTY},
125	{'\0', 0, COMMENT},
126};
127
128/* The compiled program. */
129struct s_command *prog;
130
131/*
132 * Compile the program into prog.
133 * Initialise appends.
134 */
135void
136compile(void)
137{
138	*compile_stream(&prog) = NULL;
139	fixuplabel(prog, NULL);
140	uselabel();
141	if (appendnum == 0)
142		appends = NULL;
143	else if ((appends = malloc(sizeof(struct s_appends) * appendnum)) ==
144	    NULL)
145		err(1, "malloc");
146	if ((match = malloc((maxnsub + 1) * sizeof(regmatch_t))) == NULL)
147		err(1, "malloc");
148}
149
150#define EATSPACE() do {							\
151	if (p)								\
152		while (*p && isspace((unsigned char)*p))                \
153			p++;						\
154	} while (0)
155
156static struct s_command **
157compile_stream(struct s_command **link)
158{
159	char *p;
160	static char lbuf[_POSIX2_LINE_MAX + 1];	/* To save stack */
161	struct s_command *cmd, *cmd2, *stack;
162	struct s_format *fp;
163	char re[_POSIX2_LINE_MAX + 1];
164	int naddr;				/* Number of addresses */
165
166	stack = NULL;
167	for (;;) {
168		if ((p = cu_fgets(lbuf, sizeof(lbuf), NULL)) == NULL) {
169			if (stack != NULL)
170				errx(1, "%lu: %s: unexpected EOF (pending }'s)",
171							linenum, fname);
172			return (link);
173		}
174
175semicolon:	EATSPACE();
176		if (p) {
177			if (*p == '#' || *p == '\0')
178				continue;
179			else if (*p == ';') {
180				p++;
181				goto semicolon;
182			}
183		}
184		if ((*link = cmd = malloc(sizeof(struct s_command))) == NULL)
185			err(1, "malloc");
186		link = &cmd->next;
187		cmd->startline = cmd->nonsel = 0;
188		/* First parse the addresses */
189		naddr = 0;
190
191/* Valid characters to start an address */
192#define	addrchar(c)	(strchr("0123456789/\\$", (c)))
193		if (addrchar(*p)) {
194			naddr++;
195			if ((cmd->a1 = malloc(sizeof(struct s_addr))) == NULL)
196				err(1, "malloc");
197			p = compile_addr(p, cmd->a1);
198			EATSPACE();				/* EXTENSION */
199			if (*p == ',') {
200				p++;
201				EATSPACE();			/* EXTENSION */
202				naddr++;
203				if ((cmd->a2 = malloc(sizeof(struct s_addr)))
204				    == NULL)
205					err(1, "malloc");
206				p = compile_addr(p, cmd->a2);
207				EATSPACE();
208			} else
209				cmd->a2 = NULL;
210		} else
211			cmd->a1 = cmd->a2 = NULL;
212
213nonsel:		/* Now parse the command */
214		if (!*p)
215			errx(1, "%lu: %s: command expected", linenum, fname);
216		cmd->code = *p;
217		for (fp = cmd_fmts; fp->code; fp++)
218			if (fp->code == *p)
219				break;
220		if (!fp->code)
221			errx(1, "%lu: %s: invalid command code %c", linenum, fname, *p);
222		if (naddr > fp->naddr)
223			errx(1,
224				"%lu: %s: command %c expects up to %d address(es), found %d",
225				linenum, fname, *p, fp->naddr, naddr);
226		switch (fp->args) {
227		case NONSEL:			/* ! */
228			p++;
229			EATSPACE();
230			cmd->nonsel = 1;
231			goto nonsel;
232		case GROUP:			/* { */
233			p++;
234			EATSPACE();
235			cmd->next = stack;
236			stack = cmd;
237			link = &cmd->u.c;
238			if (*p)
239				goto semicolon;
240			break;
241		case ENDGROUP:
242			/*
243			 * Short-circuit command processing, since end of
244			 * group is really just a noop.
245			 */
246			cmd->nonsel = 1;
247			if (stack == NULL)
248				errx(1, "%lu: %s: unexpected }", linenum, fname);
249			cmd2 = stack;
250			stack = cmd2->next;
251			cmd2->next = cmd;
252			/*FALLTHROUGH*/
253		case EMPTY:		/* d D g G h H l n N p P q x = \0 */
254			p++;
255			EATSPACE();
256			if (*p == ';') {
257				p++;
258				link = &cmd->next;
259				goto semicolon;
260			}
261			if (*p)
262				errx(1, "%lu: %s: extra characters at the end of %c command",
263						linenum, fname, cmd->code);
264			break;
265		case TEXT:			/* a c i */
266			p++;
267			EATSPACE();
268			if (*p != '\\')
269				errx(1,
270"%lu: %s: command %c expects \\ followed by text", linenum, fname, cmd->code);
271			p++;
272			EATSPACE();
273			if (*p)
274				errx(1,
275				"%lu: %s: extra characters after \\ at the end of %c command",
276				linenum, fname, cmd->code);
277			cmd->t = compile_text();
278			break;
279		case COMMENT:			/* \0 # */
280			break;
281		case WFILE:			/* w */
282			p++;
283			EATSPACE();
284			if (*p == '\0')
285				errx(1, "%lu: %s: filename expected", linenum, fname);
286			cmd->t = duptoeol(p, "w command");
287			if (aflag)
288				cmd->u.fd = -1;
289			else if ((cmd->u.fd = open(p,
290			    O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
291			    DEFFILEMODE)) == -1)
292				err(1, "%s", p);
293			break;
294		case RFILE:			/* r */
295			p++;
296			EATSPACE();
297			if (*p == '\0')
298				errx(1, "%lu: %s: filename expected", linenum, fname);
299			else
300				cmd->t = duptoeol(p, "read command");
301			break;
302		case BRANCH:			/* b t */
303			p++;
304			EATSPACE();
305			if (*p == '\0')
306				cmd->t = NULL;
307			else
308				cmd->t = duptoeol(p, "branch");
309			break;
310		case LABEL:			/* : */
311			p++;
312			EATSPACE();
313			cmd->t = duptoeol(p, "label");
314			if (strlen(p) == 0)
315				errx(1, "%lu: %s: empty label", linenum, fname);
316			enterlabel(cmd);
317			break;
318		case SUBST:			/* s */
319			p++;
320			if (*p == '\0' || *p == '\\')
321				errx(1,
322"%lu: %s: substitute pattern can not be delimited by newline or backslash",
323					linenum, fname);
324			if ((cmd->u.s = calloc(1, sizeof(struct s_subst))) == NULL)
325				err(1, "malloc");
326			p = compile_delimited(p, re, 0);
327			if (p == NULL)
328				errx(1,
329				"%lu: %s: unterminated substitute pattern", linenum, fname);
330
331			/* Compile RE with no case sensitivity temporarily */
332			if (*re == '\0')
333				cmd->u.s->re = NULL;
334			else
335				cmd->u.s->re = compile_re(re, 0);
336			--p;
337			p = compile_subst(p, cmd->u.s);
338			p = compile_flags(p, cmd->u.s);
339
340			/* Recompile RE with case sensitivity from "I" flag if any */
341			if (*re == '\0')
342				cmd->u.s->re = NULL;
343			else
344				cmd->u.s->re = compile_re(re, cmd->u.s->icase);
345			EATSPACE();
346			if (*p == ';') {
347				p++;
348				link = &cmd->next;
349				goto semicolon;
350			}
351			break;
352		case TR:			/* y */
353			p++;
354			p = compile_tr(p, &cmd->u.y);
355			EATSPACE();
356			if (*p == ';') {
357				p++;
358				link = &cmd->next;
359				goto semicolon;
360			}
361			if (*p)
362				errx(1,
363"%lu: %s: extra text at the end of a transform command", linenum, fname);
364			break;
365		}
366	}
367}
368
369static int
370hex2char(const char *in, char *out, int len)
371{
372	long ord;
373	char *endptr, hexbuf[3];
374
375	hexbuf[0] = in[0];
376	hexbuf[1] = len > 1 ? in[1] : '\0';
377	hexbuf[2] = '\0';
378
379	errno = 0;
380	ord = strtol(hexbuf, &endptr, 16);
381	if (*endptr != '\0' || errno != 0)
382		return (ERANGE);
383	*out = (char)ord;
384	return (0);
385}
386
387static bool
388hexdigit(char c)
389{
390	int lc;
391
392	lc = tolower(c);
393	return isdigit(lc) || (lc >= 'a' && lc <= 'f');
394}
395
396static bool
397dohex(const char *in, char *out, int *len)
398{
399	int tmplen;
400
401	if (!hexdigit(in[0]))
402		return (false);
403	tmplen = 1;
404	if (hexdigit(in[1]))
405		++tmplen;
406	if (hex2char(in, out, tmplen) == 0) {
407		*len = tmplen;
408		return (true);
409	}
410
411	return (false);
412}
413
414/*
415 * Get a delimited string.  P points to the delimiter of the string; d points
416 * to a buffer area.  Newline and delimiter escapes are processed; other
417 * escapes are ignored.
418 *
419 * Returns a pointer to the first character after the final delimiter or NULL
420 * in the case of a non-terminated string.  The character array d is filled
421 * with the processed string.
422 */
423static char *
424compile_delimited(char *p, char *d, int is_tr)
425{
426	int hexlen;
427	char c;
428
429	c = *p++;
430	if (c == '\0')
431		return (NULL);
432	else if (c == '\\')
433		errx(1, "%lu: %s: \\ can not be used as a string delimiter",
434				linenum, fname);
435	else if (c == '\n')
436		errx(1, "%lu: %s: newline can not be used as a string delimiter",
437				linenum, fname);
438	while (*p) {
439		if (*p == '[' && *p != c) {
440			if (!is_tr) {
441				if ((d = compile_ccl(&p, d)) == NULL) {
442					errx(1,
443					    "%lu: %s: unbalanced brackets ([])",
444					    linenum, fname);
445				}
446				continue;
447			}
448		} else if (*p == '\\' && p[1] == '[') {
449			if (is_tr)
450				p++;
451			else
452				*d++ = *p++;
453		} else if (*p == '\\' && p[1] == c) {
454			p++;
455		} else if (*p == '\\' &&
456		    (p[1] == 'n' || p[1] == 'r' || p[1] == 't')) {
457			switch (p[1]) {
458			case 'n':
459				*d++ = '\n';
460				break;
461			case 'r':
462				*d++ = '\r';
463				break;
464			case 't':
465				*d++ = '\t';
466				break;
467			}
468			p += 2;
469			continue;
470		} else if (*p == '\\' && p[1] == 'x') {
471			if (dohex(&p[2], d, &hexlen)) {
472				++d;
473				p += hexlen + 2;
474				continue;
475			}
476		} else if (*p == '\\' && p[1] == '\\') {
477			if (is_tr)
478				p++;
479			else
480				*d++ = *p++;
481		} else if (*p == c) {
482			*d = '\0';
483			return (p + 1);
484		}
485		*d++ = *p++;
486	}
487	return (NULL);
488}
489
490
491/* compile_ccl: expand a POSIX character class */
492static char *
493compile_ccl(char **sp, char *t)
494{
495	int c, d, hexlen;
496	char *s = *sp;
497
498	*t++ = *s++;
499	if (*s == '^')
500		*t++ = *s++;
501	if (*s == ']')
502		*t++ = *s++;
503	for (; *s && (*t = *s) != ']'; s++, t++) {
504		if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) {
505			*++t = *++s, t++, s++;
506			for (c = *s; (*t = *s) != ']' || c != d; s++, t++)
507				if ((c = *s) == '\0')
508					return NULL;
509		} else if (*s == '\\') {
510			switch (s[1]) {
511			case 'n':
512				*t = '\n';
513				s++;
514				break;
515			case 'r':
516				*t = '\r';
517				s++;
518				break;
519			case 't':
520				*t = '\t';
521				s++;
522				break;
523			case 'x':
524				if (dohex(&s[2], t, &hexlen))
525					s += hexlen + 1;
526				break;
527			}
528		}
529	}
530	return (*s == ']') ? *sp = ++s, ++t : NULL;
531}
532
533/*
534 * Compiles the regular expression in RE and returns a pointer to the compiled
535 * regular expression.
536 * Cflags are passed to regcomp.
537 */
538static regex_t *
539compile_re(char *re, int case_insensitive)
540{
541	regex_t *rep;
542	int eval, flags;
543
544
545	flags = rflags;
546	if (case_insensitive)
547		flags |= REG_ICASE;
548	if ((rep = malloc(sizeof(regex_t))) == NULL)
549		err(1, "malloc");
550	if ((eval = regcomp(rep, re, flags)) != 0)
551		errx(1, "%lu: %s: RE error: %s",
552				linenum, fname, strregerror(eval, rep));
553	if (maxnsub < rep->re_nsub)
554		maxnsub = rep->re_nsub;
555	return (rep);
556}
557
558/*
559 * Compile the substitution string of a regular expression and set res to
560 * point to a saved copy of it.  Nsub is the number of parenthesized regular
561 * expressions.
562 */
563static char *
564compile_subst(char *p, struct s_subst *s)
565{
566	static char lbuf[_POSIX2_LINE_MAX + 1];
567	int asize, hexlen, size;
568	u_char ref;
569	char c, *text, *op, *sp;
570	int more = 1, sawesc = 0;
571
572	c = *p++;			/* Terminator character */
573	if (c == '\0')
574		return (NULL);
575
576	s->maxbref = 0;
577	s->linenum = linenum;
578	asize = 2 * _POSIX2_LINE_MAX + 1;
579	if ((text = malloc(asize)) == NULL)
580		err(1, "malloc");
581	size = 0;
582	do {
583		op = sp = text + size;
584		for (; *p; p++) {
585			if (*p == '\\' || sawesc) {
586				/*
587				 * If this is a continuation from the last
588				 * buffer, we won't have a character to
589				 * skip over.
590				 */
591				if (sawesc)
592					sawesc = 0;
593				else
594					p++;
595
596				if (*p == '\0') {
597					/*
598					 * This escaped character is continued
599					 * in the next part of the line.  Note
600					 * this fact, then cause the loop to
601					 * exit w/ normal EOL case and reenter
602					 * above with the new buffer.
603					 */
604					sawesc = 1;
605					p--;
606					continue;
607				} else if (strchr("123456789", *p) != NULL) {
608					*sp++ = '\\';
609					ref = *p - '0';
610					if (s->re != NULL &&
611					    ref > s->re->re_nsub)
612						errx(1, "%lu: %s: \\%c not defined in the RE",
613								linenum, fname, *p);
614					if (s->maxbref < ref)
615						s->maxbref = ref;
616				} else {
617					switch (*p) {
618					case '&':
619					case '\\':
620						*sp++ = '\\';
621						break;
622					case 'n':
623						*p = '\n';
624						break;
625					case 'r':
626						*p = '\r';
627						break;
628					case 't':
629						*p = '\t';
630						break;
631					case 'x':
632#define	ADVANCE_N(s, n)					\
633	do {						\
634		char *adv = (s);			\
635		while (*(adv + (n) - 1) != '\0') {	\
636			*adv = *(adv + (n));		\
637			++adv;				\
638		}					\
639		*adv = '\0';				\
640	} while (0);
641						if (dohex(&p[1], p, &hexlen)) {
642							ADVANCE_N(p + 1,
643							    hexlen);
644						}
645						break;
646					}
647				}
648			} else if (*p == c) {
649				if (*++p == '\0' && more) {
650					if (cu_fgets(lbuf, sizeof(lbuf), &more))
651						p = lbuf;
652				}
653				*sp++ = '\0';
654				size += sp - op;
655				if ((s->new = realloc(text, size)) == NULL)
656					err(1, "realloc");
657				return (p);
658			} else if (*p == '\n') {
659				errx(1,
660"%lu: %s: unescaped newline inside substitute pattern", linenum, fname);
661				/* NOTREACHED */
662			}
663			*sp++ = *p;
664		}
665		size += sp - op;
666		if (asize - size < _POSIX2_LINE_MAX + 1) {
667			asize *= 2;
668			if ((text = realloc(text, asize)) == NULL)
669				err(1, "realloc");
670		}
671	} while (cu_fgets(p = lbuf, sizeof(lbuf), &more) != NULL);
672	errx(1, "%lu: %s: unterminated substitute in regular expression",
673			linenum, fname);
674	/* NOTREACHED */
675}
676
677/*
678 * Compile the flags of the s command
679 */
680static char *
681compile_flags(char *p, struct s_subst *s)
682{
683	int gn;			/* True if we have seen g or n */
684	unsigned long nval;
685	char wfile[_POSIX2_LINE_MAX + 1], *q, *eq;
686
687	s->n = 1;				/* Default */
688	s->p = 0;
689	s->wfile = NULL;
690	s->wfd = -1;
691	s->icase = 0;
692	for (gn = 0;;) {
693		EATSPACE();			/* EXTENSION */
694		switch (*p) {
695		case 'g':
696			if (gn)
697				errx(1,
698"%lu: %s: more than one number or 'g' in substitute flags", linenum, fname);
699			gn = 1;
700			s->n = 0;
701			break;
702		case '\0':
703		case '\n':
704		case ';':
705			return (p);
706		case 'p':
707			s->p = 1;
708			break;
709		case 'i':
710		case 'I':
711			s->icase = 1;
712			break;
713		case '1': case '2': case '3':
714		case '4': case '5': case '6':
715		case '7': case '8': case '9':
716			if (gn)
717				errx(1,
718"%lu: %s: more than one number or 'g' in substitute flags", linenum, fname);
719			gn = 1;
720			errno = 0;
721			nval = strtol(p, &p, 10);
722			if (errno == ERANGE || nval > INT_MAX)
723				errx(1,
724"%lu: %s: overflow in the 'N' substitute flag", linenum, fname);
725			s->n = nval;
726			p--;
727			break;
728		case 'w':
729			p++;
730#ifdef HISTORIC_PRACTICE
731			if (*p != ' ') {
732				warnx("%lu: %s: space missing before w wfile", linenum, fname);
733				return (p);
734			}
735#endif
736			EATSPACE();
737			q = wfile;
738			eq = wfile + sizeof(wfile) - 1;
739			while (*p) {
740				if (*p == '\n')
741					break;
742				if (q >= eq)
743					err(1, "wfile too long");
744				*q++ = *p++;
745			}
746			*q = '\0';
747			if (q == wfile)
748				errx(1, "%lu: %s: no wfile specified", linenum, fname);
749			s->wfile = strdup(wfile);
750			if (!aflag && (s->wfd = open(wfile,
751			    O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
752			    DEFFILEMODE)) == -1)
753				err(1, "%s", wfile);
754			return (p);
755		default:
756			errx(1, "%lu: %s: bad flag in substitute command: '%c'",
757					linenum, fname, *p);
758			break;
759		}
760		p++;
761	}
762}
763
764/*
765 * Compile a translation set of strings into a lookup table.
766 */
767static char *
768compile_tr(char *p, struct s_tr **py)
769{
770	struct s_tr *y;
771	int i;
772	const char *op, *np;
773	char old[_POSIX2_LINE_MAX + 1];
774	char new[_POSIX2_LINE_MAX + 1];
775	size_t oclen, oldlen, nclen, newlen;
776	mbstate_t mbs1, mbs2;
777
778	if ((*py = y = malloc(sizeof(*y))) == NULL)
779		err(1, NULL);
780	y->multis = NULL;
781	y->nmultis = 0;
782
783	if (*p == '\0' || *p == '\\')
784		errx(1,
785	"%lu: %s: transform pattern can not be delimited by newline or backslash",
786			linenum, fname);
787	p = compile_delimited(p, old, 1);
788	if (p == NULL)
789		errx(1, "%lu: %s: unterminated transform source string",
790				linenum, fname);
791	p = compile_delimited(p - 1, new, 1);
792	if (p == NULL)
793		errx(1, "%lu: %s: unterminated transform target string",
794				linenum, fname);
795	EATSPACE();
796	op = old;
797	oldlen = mbsrtowcs(NULL, &op, 0, NULL);
798	if (oldlen == (size_t)-1)
799		err(1, NULL);
800	np = new;
801	newlen = mbsrtowcs(NULL, &np, 0, NULL);
802	if (newlen == (size_t)-1)
803		err(1, NULL);
804	if (newlen != oldlen)
805		errx(1, "%lu: %s: transform strings are not the same length",
806				linenum, fname);
807	if (MB_CUR_MAX == 1) {
808		/*
809		 * The single-byte encoding case is easy: generate a
810		 * lookup table.
811		 */
812		for (i = 0; i <= UCHAR_MAX; i++)
813			y->bytetab[i] = (char)i;
814		for (; *op; op++, np++)
815			y->bytetab[(u_char)*op] = *np;
816	} else {
817		/*
818		 * Multi-byte encoding case: generate a lookup table as
819		 * above, but only for single-byte characters. The first
820		 * bytes of multi-byte characters have their lookup table
821		 * entries set to 0, which causes do_tr() to search through
822		 * an auxiliary vector of multi-byte mappings.
823		 */
824		memset(&mbs1, 0, sizeof(mbs1));
825		memset(&mbs2, 0, sizeof(mbs2));
826		for (i = 0; i <= UCHAR_MAX; i++)
827			y->bytetab[i] = (btowc(i) != WEOF) ? i : 0;
828		while (*op != '\0') {
829			oclen = mbrlen(op, MB_LEN_MAX, &mbs1);
830			if (oclen == (size_t)-1 || oclen == (size_t)-2)
831				errc(1, EILSEQ, NULL);
832			nclen = mbrlen(np, MB_LEN_MAX, &mbs2);
833			if (nclen == (size_t)-1 || nclen == (size_t)-2)
834				errc(1, EILSEQ, NULL);
835			if (oclen == 1 && nclen == 1)
836				y->bytetab[(u_char)*op] = *np;
837			else {
838				y->bytetab[(u_char)*op] = 0;
839				y->multis = realloc(y->multis,
840				    (y->nmultis + 1) * sizeof(*y->multis));
841				if (y->multis == NULL)
842					err(1, NULL);
843				i = y->nmultis++;
844				y->multis[i].fromlen = oclen;
845				memcpy(y->multis[i].from, op, oclen);
846				y->multis[i].tolen = nclen;
847				memcpy(y->multis[i].to, np, nclen);
848			}
849			op += oclen;
850			np += nclen;
851		}
852	}
853	return (p);
854}
855
856/*
857 * Compile the text following an a, c, or i command.
858 */
859static char *
860compile_text(void)
861{
862	int asize, esc_nl, size;
863	char *text, *p, *op, *s;
864	char lbuf[_POSIX2_LINE_MAX + 1];
865
866	asize = 2 * _POSIX2_LINE_MAX + 1;
867	if ((text = malloc(asize)) == NULL)
868		err(1, "malloc");
869	size = 0;
870	while (cu_fgets(lbuf, sizeof(lbuf), NULL) != NULL) {
871		op = s = text + size;
872		p = lbuf;
873#ifdef LEGACY_BSDSED_COMPAT
874		EATSPACE();
875#endif
876		for (esc_nl = 0; *p != '\0'; p++) {
877			if (*p == '\\' && p[1] != '\0' && *++p == '\n')
878				esc_nl = 1;
879			*s++ = *p;
880		}
881		size += s - op;
882		if (!esc_nl) {
883			*s = '\0';
884			break;
885		}
886		if (asize - size < _POSIX2_LINE_MAX + 1) {
887			asize *= 2;
888			if ((text = realloc(text, asize)) == NULL)
889				err(1, "realloc");
890		}
891	}
892	text[size] = '\0';
893	if ((p = realloc(text, size + 1)) == NULL)
894		err(1, "realloc");
895	return (p);
896}
897
898/*
899 * Get an address and return a pointer to the first character after
900 * it.  Fill the structure pointed to according to the address.
901 */
902static char *
903compile_addr(char *p, struct s_addr *a)
904{
905	char *end, re[_POSIX2_LINE_MAX + 1];
906	int icase;
907
908	icase = 0;
909
910	a->type = 0;
911	switch (*p) {
912	case '\\':				/* Context address */
913		++p;
914		/* FALLTHROUGH */
915	case '/':				/* Context address */
916		p = compile_delimited(p, re, 0);
917		if (p == NULL)
918			errx(1, "%lu: %s: unterminated regular expression", linenum, fname);
919		/* Check for case insensitive regexp flag */
920		if (*p == 'I') {
921			icase = 1;
922			p++;
923		}
924		if (*re == '\0')
925			a->u.r = NULL;
926		else
927			a->u.r = compile_re(re, icase);
928		a->type = AT_RE;
929		return (p);
930
931	case '$':				/* Last line */
932		a->type = AT_LAST;
933		return (p + 1);
934
935	case '+':				/* Relative line number */
936		a->type = AT_RELLINE;
937		p++;
938		/* FALLTHROUGH */
939						/* Line number */
940	case '0': case '1': case '2': case '3': case '4':
941	case '5': case '6': case '7': case '8': case '9':
942		if (a->type == 0)
943			a->type = AT_LINE;
944		a->u.l = strtol(p, &end, 10);
945		return (end);
946	default:
947		errx(1, "%lu: %s: expected context address", linenum, fname);
948		return (NULL);
949	}
950}
951
952/*
953 * duptoeol --
954 *	Return a copy of all the characters up to \n or \0.
955 */
956static char *
957duptoeol(char *s, const char *ctype)
958{
959	size_t len;
960	int ws;
961	char *p, *start;
962
963	ws = 0;
964	for (start = s; *s != '\0' && *s != '\n'; ++s)
965		ws = isspace((unsigned char)*s);
966	*s = '\0';
967	if (ws)
968		warnx("%lu: %s: whitespace after %s", linenum, fname, ctype);
969	len = s - start + 1;
970	if ((p = malloc(len)) == NULL)
971		err(1, "malloc");
972	return (memmove(p, start, len));
973}
974
975/*
976 * Convert goto label names to addresses, and count a and r commands, in
977 * the given subset of the script.  Free the memory used by labels in b
978 * and t commands (but not by :).
979 *
980 * TODO: Remove } nodes
981 */
982static void
983fixuplabel(struct s_command *cp, struct s_command *end)
984{
985
986	for (; cp != end; cp = cp->next)
987		switch (cp->code) {
988		case 'a':
989		case 'r':
990			appendnum++;
991			break;
992		case 'b':
993		case 't':
994			/* Resolve branch target. */
995			if (cp->t == NULL) {
996				cp->u.c = NULL;
997				break;
998			}
999			if ((cp->u.c = findlabel(cp->t)) == NULL)
1000				errx(1, "%lu: %s: undefined label '%s'", linenum, fname, cp->t);
1001			free(cp->t);
1002			break;
1003		case '{':
1004			/* Do interior commands. */
1005			fixuplabel(cp->u.c, cp->next);
1006			break;
1007		}
1008}
1009
1010/*
1011 * Associate the given command label for later lookup.
1012 */
1013static void
1014enterlabel(struct s_command *cp)
1015{
1016	struct labhash **lhp, *lh;
1017	u_char *p;
1018	u_int h, c;
1019
1020	for (h = 0, p = (u_char *)cp->t; (c = *p) != 0; p++)
1021		h = (h << 5) + h + c;
1022	lhp = &labels[h & LHMASK];
1023	for (lh = *lhp; lh != NULL; lh = lh->lh_next)
1024		if (lh->lh_hash == h && strcmp(cp->t, lh->lh_cmd->t) == 0)
1025			errx(1, "%lu: %s: duplicate label '%s'", linenum, fname, cp->t);
1026	if ((lh = malloc(sizeof *lh)) == NULL)
1027		err(1, "malloc");
1028	lh->lh_next = *lhp;
1029	lh->lh_hash = h;
1030	lh->lh_cmd = cp;
1031	lh->lh_ref = 0;
1032	*lhp = lh;
1033}
1034
1035/*
1036 * Find the label contained in the command l in the command linked
1037 * list cp.  L is excluded from the search.  Return NULL if not found.
1038 */
1039static struct s_command *
1040findlabel(char *name)
1041{
1042	struct labhash *lh;
1043	u_char *p;
1044	u_int h, c;
1045
1046	for (h = 0, p = (u_char *)name; (c = *p) != 0; p++)
1047		h = (h << 5) + h + c;
1048	for (lh = labels[h & LHMASK]; lh != NULL; lh = lh->lh_next) {
1049		if (lh->lh_hash == h && strcmp(name, lh->lh_cmd->t) == 0) {
1050			lh->lh_ref = 1;
1051			return (lh->lh_cmd);
1052		}
1053	}
1054	return (NULL);
1055}
1056
1057/*
1058 * Warn about any unused labels.  As a side effect, release the label hash
1059 * table space.
1060 */
1061static void
1062uselabel(void)
1063{
1064	struct labhash *lh, *next;
1065	int i;
1066
1067	for (i = 0; i < LHSZ; i++) {
1068		for (lh = labels[i]; lh != NULL; lh = next) {
1069			next = lh->lh_next;
1070			if (!lh->lh_ref)
1071				warnx("%lu: %s: unused label '%s'",
1072				    linenum, fname, lh->lh_cmd->t);
1073			free(lh);
1074		}
1075	}
1076}
1077