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