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