xref: /freebsd/usr.bin/sed/compile.c (revision 6419bb52)
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
40 static 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)
63 static 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 
70 static char	 *compile_addr(char *, struct s_addr *);
71 static char	 *compile_ccl(char **, char *);
72 static char	 *compile_delimited(char *, char *, int);
73 static char	 *compile_flags(char *, struct s_subst *);
74 static regex_t	 *compile_re(char *, int);
75 static char	 *compile_subst(char *, struct s_subst *);
76 static char	 *compile_text(void);
77 static char	 *compile_tr(char *, struct s_tr **);
78 static struct s_command
79 		**compile_stream(struct s_command **);
80 static char	 *duptoeol(char *, const char *);
81 static void	  enterlabel(struct s_command *);
82 static struct s_command
83 		 *findlabel(char *);
84 static void	  fixuplabel(struct s_command *, struct s_command *);
85 static void	  uselabel(void);
86 
87 /*
88  * Command specification.  This is used to drive the command parser.
89  */
90 struct s_format {
91 	char code;				/* Command code */
92 	int naddr;				/* Number of address args */
93 	enum e_args args;			/* Argument type */
94 };
95 
96 static 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. */
129 struct s_command *prog;
130 
131 /*
132  * Compile the program into prog.
133  * Initialise appends.
134  */
135 void
136 compile(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 
156 static struct s_command **
157 compile_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 
175 semicolon:	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 
213 nonsel:		/* 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 
369 static int
370 hex2char(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 
387 static bool
388 hexdigit(char c)
389 {
390 	int lc;
391 
392 	lc = tolower(c);
393 	return isdigit(lc) || (lc >= 'a' && lc <= 'f');
394 }
395 
396 static bool
397 dohex(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  */
423 static char *
424 compile_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 ((d = compile_ccl(&p, d)) == NULL)
441 				errx(1, "%lu: %s: unbalanced brackets ([])", linenum, fname);
442 			continue;
443 		} else if (*p == '\\' && p[1] == '[') {
444 			*d++ = *p++;
445 		} else if (*p == '\\' && p[1] == c) {
446 			p++;
447 		} else if (*p == '\\' &&
448 		    (p[1] == 'n' || p[1] == 'r' || p[1] == 't')) {
449 			switch (p[1]) {
450 			case 'n':
451 				*d++ = '\n';
452 				break;
453 			case 'r':
454 				*d++ = '\r';
455 				break;
456 			case 't':
457 				*d++ = '\t';
458 				break;
459 			}
460 			p += 2;
461 			continue;
462 		} else if (*p == '\\' && p[1] == 'x') {
463 			if (dohex(&p[2], d, &hexlen)) {
464 				++d;
465 				p += hexlen + 2;
466 				continue;
467 			}
468 		} else if (*p == '\\' && p[1] == '\\') {
469 			if (is_tr)
470 				p++;
471 			else
472 				*d++ = *p++;
473 		} else if (*p == c) {
474 			*d = '\0';
475 			return (p + 1);
476 		}
477 		*d++ = *p++;
478 	}
479 	return (NULL);
480 }
481 
482 
483 /* compile_ccl: expand a POSIX character class */
484 static char *
485 compile_ccl(char **sp, char *t)
486 {
487 	int c, d, hexlen;
488 	char *s = *sp;
489 
490 	*t++ = *s++;
491 	if (*s == '^')
492 		*t++ = *s++;
493 	if (*s == ']')
494 		*t++ = *s++;
495 	for (; *s && (*t = *s) != ']'; s++, t++) {
496 		if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) {
497 			*++t = *++s, t++, s++;
498 			for (c = *s; (*t = *s) != ']' || c != d; s++, t++)
499 				if ((c = *s) == '\0')
500 					return NULL;
501 		} else if (*s == '\\') {
502 			switch (s[1]) {
503 			case 'n':
504 				*t = '\n';
505 				s++;
506 				break;
507 			case 'r':
508 				*t = '\r';
509 				s++;
510 				break;
511 			case 't':
512 				*t = '\t';
513 				s++;
514 				break;
515 			case 'x':
516 				if (dohex(&s[2], t, &hexlen))
517 					s += hexlen + 1;
518 				break;
519 			}
520 		}
521 	}
522 	return (*s == ']') ? *sp = ++s, ++t : NULL;
523 }
524 
525 /*
526  * Compiles the regular expression in RE and returns a pointer to the compiled
527  * regular expression.
528  * Cflags are passed to regcomp.
529  */
530 static regex_t *
531 compile_re(char *re, int case_insensitive)
532 {
533 	regex_t *rep;
534 	int eval, flags;
535 
536 
537 	flags = rflags;
538 	if (case_insensitive)
539 		flags |= REG_ICASE;
540 	if ((rep = malloc(sizeof(regex_t))) == NULL)
541 		err(1, "malloc");
542 	if ((eval = regcomp(rep, re, flags)) != 0)
543 		errx(1, "%lu: %s: RE error: %s",
544 				linenum, fname, strregerror(eval, rep));
545 	if (maxnsub < rep->re_nsub)
546 		maxnsub = rep->re_nsub;
547 	return (rep);
548 }
549 
550 /*
551  * Compile the substitution string of a regular expression and set res to
552  * point to a saved copy of it.  Nsub is the number of parenthesized regular
553  * expressions.
554  */
555 static char *
556 compile_subst(char *p, struct s_subst *s)
557 {
558 	static char lbuf[_POSIX2_LINE_MAX + 1];
559 	int asize, hexlen, size;
560 	u_char ref;
561 	char c, *text, *op, *sp;
562 	int more = 1, sawesc = 0;
563 
564 	c = *p++;			/* Terminator character */
565 	if (c == '\0')
566 		return (NULL);
567 
568 	s->maxbref = 0;
569 	s->linenum = linenum;
570 	asize = 2 * _POSIX2_LINE_MAX + 1;
571 	if ((text = malloc(asize)) == NULL)
572 		err(1, "malloc");
573 	size = 0;
574 	do {
575 		op = sp = text + size;
576 		for (; *p; p++) {
577 			if (*p == '\\' || sawesc) {
578 				/*
579 				 * If this is a continuation from the last
580 				 * buffer, we won't have a character to
581 				 * skip over.
582 				 */
583 				if (sawesc)
584 					sawesc = 0;
585 				else
586 					p++;
587 
588 				if (*p == '\0') {
589 					/*
590 					 * This escaped character is continued
591 					 * in the next part of the line.  Note
592 					 * this fact, then cause the loop to
593 					 * exit w/ normal EOL case and reenter
594 					 * above with the new buffer.
595 					 */
596 					sawesc = 1;
597 					p--;
598 					continue;
599 				} else if (strchr("123456789", *p) != NULL) {
600 					*sp++ = '\\';
601 					ref = *p - '0';
602 					if (s->re != NULL &&
603 					    ref > s->re->re_nsub)
604 						errx(1, "%lu: %s: \\%c not defined in the RE",
605 								linenum, fname, *p);
606 					if (s->maxbref < ref)
607 						s->maxbref = ref;
608 				} else {
609 					switch (*p) {
610 					case '&':
611 					case '\\':
612 						*sp++ = '\\';
613 						break;
614 					case 'n':
615 						*p = '\n';
616 						break;
617 					case 'r':
618 						*p = '\r';
619 						break;
620 					case 't':
621 						*p = '\t';
622 						break;
623 					case 'x':
624 #define	ADVANCE_N(s, n)					\
625 	do {						\
626 		char *adv = (s);			\
627 		while (*(adv + (n) - 1) != '\0') {	\
628 			*adv = *(adv + (n));		\
629 			++adv;				\
630 		}					\
631 		*adv = '\0';				\
632 	} while (0);
633 						if (dohex(&p[1], p, &hexlen)) {
634 							ADVANCE_N(p + 1,
635 							    hexlen);
636 						}
637 						break;
638 					}
639 				}
640 			} else if (*p == c) {
641 				if (*++p == '\0' && more) {
642 					if (cu_fgets(lbuf, sizeof(lbuf), &more))
643 						p = lbuf;
644 				}
645 				*sp++ = '\0';
646 				size += sp - op;
647 				if ((s->new = realloc(text, size)) == NULL)
648 					err(1, "realloc");
649 				return (p);
650 			} else if (*p == '\n') {
651 				errx(1,
652 "%lu: %s: unescaped newline inside substitute pattern", linenum, fname);
653 				/* NOTREACHED */
654 			}
655 			*sp++ = *p;
656 		}
657 		size += sp - op;
658 		if (asize - size < _POSIX2_LINE_MAX + 1) {
659 			asize *= 2;
660 			if ((text = realloc(text, asize)) == NULL)
661 				err(1, "realloc");
662 		}
663 	} while (cu_fgets(p = lbuf, sizeof(lbuf), &more) != NULL);
664 	errx(1, "%lu: %s: unterminated substitute in regular expression",
665 			linenum, fname);
666 	/* NOTREACHED */
667 }
668 
669 /*
670  * Compile the flags of the s command
671  */
672 static char *
673 compile_flags(char *p, struct s_subst *s)
674 {
675 	int gn;			/* True if we have seen g or n */
676 	unsigned long nval;
677 	char wfile[_POSIX2_LINE_MAX + 1], *q, *eq;
678 
679 	s->n = 1;				/* Default */
680 	s->p = 0;
681 	s->wfile = NULL;
682 	s->wfd = -1;
683 	s->icase = 0;
684 	for (gn = 0;;) {
685 		EATSPACE();			/* EXTENSION */
686 		switch (*p) {
687 		case 'g':
688 			if (gn)
689 				errx(1,
690 "%lu: %s: more than one number or 'g' in substitute flags", linenum, fname);
691 			gn = 1;
692 			s->n = 0;
693 			break;
694 		case '\0':
695 		case '\n':
696 		case ';':
697 			return (p);
698 		case 'p':
699 			s->p = 1;
700 			break;
701 		case 'i':
702 		case 'I':
703 			s->icase = 1;
704 			break;
705 		case '1': case '2': case '3':
706 		case '4': case '5': case '6':
707 		case '7': case '8': case '9':
708 			if (gn)
709 				errx(1,
710 "%lu: %s: more than one number or 'g' in substitute flags", linenum, fname);
711 			gn = 1;
712 			errno = 0;
713 			nval = strtol(p, &p, 10);
714 			if (errno == ERANGE || nval > INT_MAX)
715 				errx(1,
716 "%lu: %s: overflow in the 'N' substitute flag", linenum, fname);
717 			s->n = nval;
718 			p--;
719 			break;
720 		case 'w':
721 			p++;
722 #ifdef HISTORIC_PRACTICE
723 			if (*p != ' ') {
724 				warnx("%lu: %s: space missing before w wfile", linenum, fname);
725 				return (p);
726 			}
727 #endif
728 			EATSPACE();
729 			q = wfile;
730 			eq = wfile + sizeof(wfile) - 1;
731 			while (*p) {
732 				if (*p == '\n')
733 					break;
734 				if (q >= eq)
735 					err(1, "wfile too long");
736 				*q++ = *p++;
737 			}
738 			*q = '\0';
739 			if (q == wfile)
740 				errx(1, "%lu: %s: no wfile specified", linenum, fname);
741 			s->wfile = strdup(wfile);
742 			if (!aflag && (s->wfd = open(wfile,
743 			    O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
744 			    DEFFILEMODE)) == -1)
745 				err(1, "%s", wfile);
746 			return (p);
747 		default:
748 			errx(1, "%lu: %s: bad flag in substitute command: '%c'",
749 					linenum, fname, *p);
750 			break;
751 		}
752 		p++;
753 	}
754 }
755 
756 /*
757  * Compile a translation set of strings into a lookup table.
758  */
759 static char *
760 compile_tr(char *p, struct s_tr **py)
761 {
762 	struct s_tr *y;
763 	int i;
764 	const char *op, *np;
765 	char old[_POSIX2_LINE_MAX + 1];
766 	char new[_POSIX2_LINE_MAX + 1];
767 	size_t oclen, oldlen, nclen, newlen;
768 	mbstate_t mbs1, mbs2;
769 
770 	if ((*py = y = malloc(sizeof(*y))) == NULL)
771 		err(1, NULL);
772 	y->multis = NULL;
773 	y->nmultis = 0;
774 
775 	if (*p == '\0' || *p == '\\')
776 		errx(1,
777 	"%lu: %s: transform pattern can not be delimited by newline or backslash",
778 			linenum, fname);
779 	p = compile_delimited(p, old, 1);
780 	if (p == NULL)
781 		errx(1, "%lu: %s: unterminated transform source string",
782 				linenum, fname);
783 	p = compile_delimited(p - 1, new, 1);
784 	if (p == NULL)
785 		errx(1, "%lu: %s: unterminated transform target string",
786 				linenum, fname);
787 	EATSPACE();
788 	op = old;
789 	oldlen = mbsrtowcs(NULL, &op, 0, NULL);
790 	if (oldlen == (size_t)-1)
791 		err(1, NULL);
792 	np = new;
793 	newlen = mbsrtowcs(NULL, &np, 0, NULL);
794 	if (newlen == (size_t)-1)
795 		err(1, NULL);
796 	if (newlen != oldlen)
797 		errx(1, "%lu: %s: transform strings are not the same length",
798 				linenum, fname);
799 	if (MB_CUR_MAX == 1) {
800 		/*
801 		 * The single-byte encoding case is easy: generate a
802 		 * lookup table.
803 		 */
804 		for (i = 0; i <= UCHAR_MAX; i++)
805 			y->bytetab[i] = (char)i;
806 		for (; *op; op++, np++)
807 			y->bytetab[(u_char)*op] = *np;
808 	} else {
809 		/*
810 		 * Multi-byte encoding case: generate a lookup table as
811 		 * above, but only for single-byte characters. The first
812 		 * bytes of multi-byte characters have their lookup table
813 		 * entries set to 0, which causes do_tr() to search through
814 		 * an auxiliary vector of multi-byte mappings.
815 		 */
816 		memset(&mbs1, 0, sizeof(mbs1));
817 		memset(&mbs2, 0, sizeof(mbs2));
818 		for (i = 0; i <= UCHAR_MAX; i++)
819 			y->bytetab[i] = (btowc(i) != WEOF) ? i : 0;
820 		while (*op != '\0') {
821 			oclen = mbrlen(op, MB_LEN_MAX, &mbs1);
822 			if (oclen == (size_t)-1 || oclen == (size_t)-2)
823 				errc(1, EILSEQ, NULL);
824 			nclen = mbrlen(np, MB_LEN_MAX, &mbs2);
825 			if (nclen == (size_t)-1 || nclen == (size_t)-2)
826 				errc(1, EILSEQ, NULL);
827 			if (oclen == 1 && nclen == 1)
828 				y->bytetab[(u_char)*op] = *np;
829 			else {
830 				y->bytetab[(u_char)*op] = 0;
831 				y->multis = realloc(y->multis,
832 				    (y->nmultis + 1) * sizeof(*y->multis));
833 				if (y->multis == NULL)
834 					err(1, NULL);
835 				i = y->nmultis++;
836 				y->multis[i].fromlen = oclen;
837 				memcpy(y->multis[i].from, op, oclen);
838 				y->multis[i].tolen = nclen;
839 				memcpy(y->multis[i].to, np, nclen);
840 			}
841 			op += oclen;
842 			np += nclen;
843 		}
844 	}
845 	return (p);
846 }
847 
848 /*
849  * Compile the text following an a, c, or i command.
850  */
851 static char *
852 compile_text(void)
853 {
854 	int asize, esc_nl, size;
855 	char *text, *p, *op, *s;
856 	char lbuf[_POSIX2_LINE_MAX + 1];
857 
858 	asize = 2 * _POSIX2_LINE_MAX + 1;
859 	if ((text = malloc(asize)) == NULL)
860 		err(1, "malloc");
861 	size = 0;
862 	while (cu_fgets(lbuf, sizeof(lbuf), NULL) != NULL) {
863 		op = s = text + size;
864 		p = lbuf;
865 #ifdef LEGACY_BSDSED_COMPAT
866 		EATSPACE();
867 #endif
868 		for (esc_nl = 0; *p != '\0'; p++) {
869 			if (*p == '\\' && p[1] != '\0' && *++p == '\n')
870 				esc_nl = 1;
871 			*s++ = *p;
872 		}
873 		size += s - op;
874 		if (!esc_nl) {
875 			*s = '\0';
876 			break;
877 		}
878 		if (asize - size < _POSIX2_LINE_MAX + 1) {
879 			asize *= 2;
880 			if ((text = realloc(text, asize)) == NULL)
881 				err(1, "realloc");
882 		}
883 	}
884 	text[size] = '\0';
885 	if ((p = realloc(text, size + 1)) == NULL)
886 		err(1, "realloc");
887 	return (p);
888 }
889 
890 /*
891  * Get an address and return a pointer to the first character after
892  * it.  Fill the structure pointed to according to the address.
893  */
894 static char *
895 compile_addr(char *p, struct s_addr *a)
896 {
897 	char *end, re[_POSIX2_LINE_MAX + 1];
898 	int icase;
899 
900 	icase = 0;
901 
902 	a->type = 0;
903 	switch (*p) {
904 	case '\\':				/* Context address */
905 		++p;
906 		/* FALLTHROUGH */
907 	case '/':				/* Context address */
908 		p = compile_delimited(p, re, 0);
909 		if (p == NULL)
910 			errx(1, "%lu: %s: unterminated regular expression", linenum, fname);
911 		/* Check for case insensitive regexp flag */
912 		if (*p == 'I') {
913 			icase = 1;
914 			p++;
915 		}
916 		if (*re == '\0')
917 			a->u.r = NULL;
918 		else
919 			a->u.r = compile_re(re, icase);
920 		a->type = AT_RE;
921 		return (p);
922 
923 	case '$':				/* Last line */
924 		a->type = AT_LAST;
925 		return (p + 1);
926 
927 	case '+':				/* Relative line number */
928 		a->type = AT_RELLINE;
929 		p++;
930 		/* FALLTHROUGH */
931 						/* Line number */
932 	case '0': case '1': case '2': case '3': case '4':
933 	case '5': case '6': case '7': case '8': case '9':
934 		if (a->type == 0)
935 			a->type = AT_LINE;
936 		a->u.l = strtol(p, &end, 10);
937 		return (end);
938 	default:
939 		errx(1, "%lu: %s: expected context address", linenum, fname);
940 		return (NULL);
941 	}
942 }
943 
944 /*
945  * duptoeol --
946  *	Return a copy of all the characters up to \n or \0.
947  */
948 static char *
949 duptoeol(char *s, const char *ctype)
950 {
951 	size_t len;
952 	int ws;
953 	char *p, *start;
954 
955 	ws = 0;
956 	for (start = s; *s != '\0' && *s != '\n'; ++s)
957 		ws = isspace((unsigned char)*s);
958 	*s = '\0';
959 	if (ws)
960 		warnx("%lu: %s: whitespace after %s", linenum, fname, ctype);
961 	len = s - start + 1;
962 	if ((p = malloc(len)) == NULL)
963 		err(1, "malloc");
964 	return (memmove(p, start, len));
965 }
966 
967 /*
968  * Convert goto label names to addresses, and count a and r commands, in
969  * the given subset of the script.  Free the memory used by labels in b
970  * and t commands (but not by :).
971  *
972  * TODO: Remove } nodes
973  */
974 static void
975 fixuplabel(struct s_command *cp, struct s_command *end)
976 {
977 
978 	for (; cp != end; cp = cp->next)
979 		switch (cp->code) {
980 		case 'a':
981 		case 'r':
982 			appendnum++;
983 			break;
984 		case 'b':
985 		case 't':
986 			/* Resolve branch target. */
987 			if (cp->t == NULL) {
988 				cp->u.c = NULL;
989 				break;
990 			}
991 			if ((cp->u.c = findlabel(cp->t)) == NULL)
992 				errx(1, "%lu: %s: undefined label '%s'", linenum, fname, cp->t);
993 			free(cp->t);
994 			break;
995 		case '{':
996 			/* Do interior commands. */
997 			fixuplabel(cp->u.c, cp->next);
998 			break;
999 		}
1000 }
1001 
1002 /*
1003  * Associate the given command label for later lookup.
1004  */
1005 static void
1006 enterlabel(struct s_command *cp)
1007 {
1008 	struct labhash **lhp, *lh;
1009 	u_char *p;
1010 	u_int h, c;
1011 
1012 	for (h = 0, p = (u_char *)cp->t; (c = *p) != 0; p++)
1013 		h = (h << 5) + h + c;
1014 	lhp = &labels[h & LHMASK];
1015 	for (lh = *lhp; lh != NULL; lh = lh->lh_next)
1016 		if (lh->lh_hash == h && strcmp(cp->t, lh->lh_cmd->t) == 0)
1017 			errx(1, "%lu: %s: duplicate label '%s'", linenum, fname, cp->t);
1018 	if ((lh = malloc(sizeof *lh)) == NULL)
1019 		err(1, "malloc");
1020 	lh->lh_next = *lhp;
1021 	lh->lh_hash = h;
1022 	lh->lh_cmd = cp;
1023 	lh->lh_ref = 0;
1024 	*lhp = lh;
1025 }
1026 
1027 /*
1028  * Find the label contained in the command l in the command linked
1029  * list cp.  L is excluded from the search.  Return NULL if not found.
1030  */
1031 static struct s_command *
1032 findlabel(char *name)
1033 {
1034 	struct labhash *lh;
1035 	u_char *p;
1036 	u_int h, c;
1037 
1038 	for (h = 0, p = (u_char *)name; (c = *p) != 0; p++)
1039 		h = (h << 5) + h + c;
1040 	for (lh = labels[h & LHMASK]; lh != NULL; lh = lh->lh_next) {
1041 		if (lh->lh_hash == h && strcmp(name, lh->lh_cmd->t) == 0) {
1042 			lh->lh_ref = 1;
1043 			return (lh->lh_cmd);
1044 		}
1045 	}
1046 	return (NULL);
1047 }
1048 
1049 /*
1050  * Warn about any unused labels.  As a side effect, release the label hash
1051  * table space.
1052  */
1053 static void
1054 uselabel(void)
1055 {
1056 	struct labhash *lh, *next;
1057 	int i;
1058 
1059 	for (i = 0; i < LHSZ; i++) {
1060 		for (lh = labels[i]; lh != NULL; lh = next) {
1061 			next = lh->lh_next;
1062 			if (!lh->lh_ref)
1063 				warnx("%lu: %s: unused label '%s'",
1064 				    linenum, fname, lh->lh_cmd->t);
1065 			free(lh);
1066 		}
1067 	}
1068 }
1069