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