fatbase

portable OpenBSD tools
git clone git://git.2f30.org/fatbase
Log | Files | Refs

compile.c (20003B)


      1 /*	$OpenBSD: compile.c,v 1.36 2014/10/08 04:19:08 deraadt 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 #include <sys/types.h>
     37 #include <sys/stat.h>
     38 
     39 #include <ctype.h>
     40 #include <errno.h>
     41 #include <fcntl.h>
     42 #include <limits.h>
     43 #include <regex.h>
     44 #include <stdio.h>
     45 #include <stdlib.h>
     46 #include <string.h>
     47 
     48 #include "defs.h"
     49 #include "extern.h"
     50 #include "util.h"
     51 
     52 #define LHSZ	128
     53 #define	LHMASK	(LHSZ - 1)
     54 static struct labhash {
     55 	struct	labhash *lh_next;
     56 	u_int	lh_hash;
     57 	struct	s_command *lh_cmd;
     58 	int	lh_ref;
     59 } *labels[LHSZ];
     60 
     61 static char	 *compile_addr(char *, struct s_addr *);
     62 static char	 *compile_ccl(char **, char *);
     63 static char	 *compile_delimited(char *, char *, int);
     64 static char	 *compile_flags(char *, struct s_subst *);
     65 static char	 *compile_re(char *, regex_t **);
     66 static char	 *compile_subst(char *, struct s_subst *);
     67 static char	 *compile_text(void);
     68 static char	 *compile_tr(char *, char **);
     69 static struct s_command
     70 		**compile_stream(struct s_command **);
     71 static char	 *duptoeol(char *, char *, char **);
     72 static void	  enterlabel(struct s_command *);
     73 static struct s_command
     74 		 *findlabel(char *);
     75 static void	  fixuplabel(struct s_command *, struct s_command *);
     76 static void	  uselabel(void);
     77 
     78 /*
     79  * Command specification.  This is used to drive the command parser.
     80  */
     81 struct s_format {
     82 	char code;				/* Command code */
     83 	int naddr;				/* Number of address args */
     84 	enum e_args args;			/* Argument type */
     85 };
     86 
     87 static struct s_format cmd_fmts[] = {
     88 	{'{', 2, GROUP},
     89 	{'}', 0, ENDGROUP},
     90 	{'a', 1, TEXT},
     91 	{'b', 2, BRANCH},
     92 	{'c', 2, TEXT},
     93 	{'d', 2, EMPTY},
     94 	{'D', 2, EMPTY},
     95 	{'g', 2, EMPTY},
     96 	{'G', 2, EMPTY},
     97 	{'h', 2, EMPTY},
     98 	{'H', 2, EMPTY},
     99 	{'i', 1, TEXT},
    100 	{'l', 2, EMPTY},
    101 	{'n', 2, EMPTY},
    102 	{'N', 2, EMPTY},
    103 	{'p', 2, EMPTY},
    104 	{'P', 2, EMPTY},
    105 	{'q', 1, EMPTY},
    106 	{'r', 1, RFILE},
    107 	{'s', 2, SUBST},
    108 	{'t', 2, BRANCH},
    109 	{'w', 2, WFILE},
    110 	{'x', 2, EMPTY},
    111 	{'y', 2, TR},
    112 	{'!', 2, NONSEL},
    113 	{':', 0, LABEL},
    114 	{'#', 0, COMMENT},
    115 	{'=', 1, EMPTY},
    116 	{'\0', 0, COMMENT},
    117 };
    118 
    119 /* The compiled program. */
    120 struct s_command *prog;
    121 
    122 /*
    123  * Compile the program into prog.
    124  * Initialise appends.
    125  */
    126 void
    127 compile(void)
    128 {
    129 	*compile_stream(&prog) = NULL;
    130 	fixuplabel(prog, NULL);
    131 	uselabel();
    132 	appends = xreallocarray(NULL, appendnum, sizeof(struct s_appends));
    133 	match = xreallocarray(NULL, maxnsub + 1, sizeof(regmatch_t));
    134 }
    135 
    136 #define EATSPACE() do {							\
    137 	if (p)								\
    138 		while (isascii((unsigned char)*p) &&			\
    139 		    isspace((unsigned char)*p))				\
    140 			p++;						\
    141 	} while (0)
    142 
    143 static struct s_command **
    144 compile_stream(struct s_command **link)
    145 {
    146 	char *p;
    147 	static char *lbuf;	/* To avoid excessive malloc calls */
    148 	static size_t bufsize;
    149 	struct s_command *cmd, *cmd2, *stack;
    150 	struct s_format *fp;
    151 	int naddr;				/* Number of addresses */
    152 
    153 	stack = 0;
    154 	for (;;) {
    155 		if ((p = cu_fgets(&lbuf, &bufsize)) == NULL) {
    156 			if (stack != 0)
    157 				err(COMPILE, "unexpected EOF (pending }'s)");
    158 			return (link);
    159 		}
    160 
    161 semicolon:	EATSPACE();
    162 		if (*p == '#' || *p == '\0')
    163 			continue;
    164 		if (*p == ';') {
    165 			p++;
    166 			goto semicolon;
    167 		}
    168 		*link = cmd = xmalloc(sizeof(struct s_command));
    169 		link = &cmd->next;
    170 		cmd->nonsel = cmd->inrange = 0;
    171 		/* First parse the addresses */
    172 		naddr = 0;
    173 
    174 /* Valid characters to start an address */
    175 #define	addrchar(c)	(strchr("0123456789/\\$", (c)))
    176 		if (addrchar(*p)) {
    177 			naddr++;
    178 			cmd->a1 = xmalloc(sizeof(struct s_addr));
    179 			p = compile_addr(p, cmd->a1);
    180 			EATSPACE();				/* EXTENSION */
    181 			if (*p == ',') {
    182 				p++;
    183 				EATSPACE();			/* EXTENSION */
    184 				naddr++;
    185 				cmd->a2 = xmalloc(sizeof(struct s_addr));
    186 				p = compile_addr(p, cmd->a2);
    187 				EATSPACE();
    188 			} else {
    189 				cmd->a2 = 0;
    190 			}
    191 		} else {
    192 			cmd->a1 = cmd->a2 = 0;
    193 		}
    194 
    195 nonsel:		/* Now parse the command */
    196 		if (!*p)
    197 			err(COMPILE, "command expected");
    198 		cmd->code = *p;
    199 		for (fp = cmd_fmts; fp->code; fp++)
    200 			if (fp->code == *p)
    201 				break;
    202 		if (!fp->code)
    203 			err(COMPILE, "invalid command code %c", *p);
    204 		if (naddr > fp->naddr)
    205 			err(COMPILE,
    206 			    "command %c expects up to %d address(es), found %d",
    207 			    *p, fp->naddr, naddr);
    208 		switch (fp->args) {
    209 		case NONSEL:			/* ! */
    210 			p++;
    211 			EATSPACE();
    212 			cmd->nonsel = ! cmd->nonsel;
    213 			goto nonsel;
    214 		case GROUP:			/* { */
    215 			p++;
    216 			EATSPACE();
    217 			cmd->next = stack;
    218 			stack = cmd;
    219 			link = &cmd->u.c;
    220 			if (*p)
    221 				goto semicolon;
    222 			break;
    223 		case ENDGROUP:
    224 			/*
    225 			 * Short-circuit command processing, since end of
    226 			 * group is really just a noop.
    227 			 */
    228 			cmd->nonsel = 1;
    229 			if (stack == 0)
    230 				err(COMPILE, "unexpected }");
    231 			cmd2 = stack;
    232 			stack = cmd2->next;
    233 			cmd2->next = cmd;
    234 			/*FALLTHROUGH*/
    235 		case EMPTY:		/* d D g G h H l n N p P q x = \0 */
    236 			p++;
    237 			EATSPACE();
    238 			if (*p == ';') {
    239 				p++;
    240 				link = &cmd->next;
    241 				goto semicolon;
    242 			}
    243 			if (*p)
    244 				err(COMPILE,
    245 "extra characters at the end of %c command", cmd->code);
    246 			break;
    247 		case TEXT:			/* a c i */
    248 			p++;
    249 			EATSPACE();
    250 			if (*p != '\\')
    251 				err(COMPILE, "command %c expects \\ followed by"
    252 				    " text", cmd->code);
    253 			p++;
    254 			EATSPACE();
    255 			if (*p)
    256 				err(COMPILE, "extra characters after \\ at the"
    257 				    " end of %c command", cmd->code);
    258 			cmd->t = compile_text();
    259 			break;
    260 		case COMMENT:			/* \0 # */
    261 			break;
    262 		case WFILE:			/* w */
    263 			p++;
    264 			EATSPACE();
    265 			if (*p == '\0')
    266 				err(COMPILE, "filename expected");
    267 			cmd->t = duptoeol(p, "w command", NULL);
    268 			if (aflag)
    269 				cmd->u.fd = -1;
    270 			else if ((cmd->u.fd = open(p,
    271 			    O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
    272 			    DEFFILEMODE)) == -1)
    273 				err(FATAL, "%s: %s", p, strerror(errno));
    274 			break;
    275 		case RFILE:			/* r */
    276 			p++;
    277 			EATSPACE();
    278 			cmd->t = duptoeol(p, "read command", NULL);
    279 			break;
    280 		case BRANCH:			/* b t */
    281 			p++;
    282 			EATSPACE();
    283 			if (*p == '\0')
    284 				cmd->t = NULL;
    285 			else
    286 				cmd->t = duptoeol(p, "branch", &p);
    287 			if (*p == ';') {
    288 				p++;
    289 				goto semicolon;
    290 			}
    291 			break;
    292 		case LABEL:			/* : */
    293 			p++;
    294 			EATSPACE();
    295 			cmd->t = duptoeol(p, "label", &p);
    296 			if (strlen(cmd->t) == 0)
    297 				err(COMPILE, "empty label");
    298 			enterlabel(cmd);
    299 			if (*p == ';') {
    300 				p++;
    301 				goto semicolon;
    302 			}
    303 			break;
    304 		case SUBST:			/* s */
    305 			p++;
    306 			if (*p == '\0' || *p == '\\')
    307 				err(COMPILE, "substitute pattern can not be"
    308 				    " delimited by newline or backslash");
    309 			cmd->u.s = xmalloc(sizeof(struct s_subst));
    310 			p = compile_re(p, &cmd->u.s->re);
    311 			if (p == NULL)
    312 				err(COMPILE, "unterminated substitute pattern");
    313 			--p;
    314 			p = compile_subst(p, cmd->u.s);
    315 			p = compile_flags(p, cmd->u.s);
    316 			EATSPACE();
    317 			if (*p == ';') {
    318 				p++;
    319 				link = &cmd->next;
    320 				goto semicolon;
    321 			}
    322 			break;
    323 		case TR:			/* y */
    324 			p++;
    325 			p = compile_tr(p, (char **)&cmd->u.y);
    326 			EATSPACE();
    327 			if (*p == ';') {
    328 				p++;
    329 				link = &cmd->next;
    330 				goto semicolon;
    331 			}
    332 			if (*p)
    333 				err(COMPILE, "extra text at the end of a"
    334 				    " transform command");
    335 			break;
    336 		}
    337 	}
    338 }
    339 
    340 /*
    341  * Get a delimited string.  P points to the delimeter of the string; d points
    342  * to a buffer area.  Newline and delimiter escapes are processed; other
    343  * escapes are ignored.
    344  *
    345  * Returns a pointer to the first character after the final delimiter or NULL
    346  * in the case of a non-terminated string.  The character array d is filled
    347  * with the processed string.
    348  */
    349 static char *
    350 compile_delimited(char *p, char *d, int is_tr)
    351 {
    352 	char c;
    353 
    354 	c = *p++;
    355 	if (c == '\0')
    356 		return (NULL);
    357 	else if (c == '\\')
    358 		err(COMPILE, "\\ can not be used as a string delimiter");
    359 	else if (c == '\n')
    360 		err(COMPILE, "newline can not be used as a string delimiter");
    361 	while (*p) {
    362 		if (*p == '[' && *p != c) {
    363 			if ((d = compile_ccl(&p, d)) == NULL)
    364 				err(COMPILE, "unbalanced brackets ([])");
    365 			continue;
    366 		} else if (*p == '\\' && p[1] == '[') {
    367 			*d++ = *p++;
    368 		} else if (*p == '\\' && p[1] == c) {
    369 			p++;
    370 		} else if (*p == '\\' && p[1] == 'n') {
    371 			*d++ = '\n';
    372 			p += 2;
    373 			continue;
    374 		} else if (*p == '\\' && p[1] == '\\') {
    375 			if (is_tr)
    376 				p++;
    377 			else
    378 				*d++ = *p++;
    379 		} else if (*p == c) {
    380 			*d = '\0';
    381 			return (p + 1);
    382 		}
    383 		*d++ = *p++;
    384 	}
    385 	return (NULL);
    386 }
    387 
    388 
    389 /* compile_ccl: expand a POSIX character class */
    390 static char *
    391 compile_ccl(char **sp, char *t)
    392 {
    393 	int c, d;
    394 	char *s = *sp;
    395 
    396 	*t++ = *s++;
    397 	if (*s == '^')
    398 		*t++ = *s++;
    399 	if (*s == ']')
    400 		*t++ = *s++;
    401 	for (; *s && (*t = *s) != ']'; s++, t++)
    402 		if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) {
    403 			*++t = *++s, t++, s++;
    404 			for (c = *s; (*t = *s) != ']' || c != d; s++, t++)
    405 				if ((c = *s) == '\0')
    406 					return NULL;
    407 		} else if (*s == '\\' && s[1] == 'n') {
    408 			*t = '\n';
    409 			s++;
    410 		}
    411 	if (*s == ']') {
    412 		*sp = ++s;
    413 		return (++t);
    414 	} else {
    415 		return (NULL);
    416 	}
    417 }
    418 
    419 /*
    420  * Get a regular expression.  P points to the delimiter of the regular
    421  * expression; repp points to the address of a regexp pointer.  Newline
    422  * and delimiter escapes are processed; other escapes are ignored.
    423  * Returns a pointer to the first character after the final delimiter
    424  * or NULL in the case of a non terminated regular expression.  The regexp
    425  * pointer is set to the compiled regular expression.
    426  * Cflags are passed to regcomp.
    427  */
    428 static char *
    429 compile_re(char *p, regex_t **repp)
    430 {
    431 	int eval;
    432 	char *re;
    433 
    434 	re = xmalloc(strlen(p) + 1); /* strlen(re) <= strlen(p) */
    435 	p = compile_delimited(p, re, 0);
    436 	if (p && strlen(re) == 0) {
    437 		*repp = NULL;
    438 		free(re);
    439 		return (p);
    440 	}
    441 	*repp = xmalloc(sizeof(regex_t));
    442 	if (p && (eval = regcomp(*repp, re, Eflag ? REG_EXTENDED : 0)) != 0)
    443 		err(COMPILE, "RE error: %s", strregerror(eval, *repp));
    444 	if (maxnsub < (*repp)->re_nsub)
    445 		maxnsub = (*repp)->re_nsub;
    446 	free(re);
    447 	return (p);
    448 }
    449 
    450 /*
    451  * Compile the substitution string of a regular expression and set res to
    452  * point to a saved copy of it.  Nsub is the number of parenthesized regular
    453  * expressions.
    454  */
    455 static char *
    456 compile_subst(char *p, struct s_subst *s)
    457 {
    458 	static char *lbuf;
    459 	static size_t bufsize;
    460 	int asize, ref, size;
    461 	char c, *text, *op, *sp;
    462 	int sawesc = 0;
    463 
    464 	c = *p++;			/* Terminator character */
    465 	if (c == '\0')
    466 		return (NULL);
    467 
    468 	s->maxbref = 0;
    469 	s->linenum = linenum;
    470 	text = NULL;
    471 	asize = size = 0;
    472 	do {
    473 		size_t len = ROUNDLEN(strlen(p) + 1);
    474 		if (asize - size < len) {
    475 			do {
    476 				asize += len;
    477 			} while (asize - size < len);
    478 			text = xrealloc(text, asize);
    479 		}
    480 		op = sp = text + size;
    481 		for (; *p; p++) {
    482 			if (*p == '\\' || sawesc) {
    483 				/*
    484 				 * If this is a continuation from the last
    485 				 * buffer, we won't have a character to
    486 				 * skip over.
    487 				 */
    488 				if (sawesc)
    489 					sawesc = 0;
    490 				else
    491 					p++;
    492 
    493 				if (*p == '\0') {
    494 					/*
    495 					 * This escaped character is continued
    496 					 * in the next part of the line.  Note
    497 					 * this fact, then cause the loop to
    498 					 * exit w/ normal EOL case and reenter
    499 					 * above with the new buffer.
    500 					 */
    501 					sawesc = 1;
    502 					p--;
    503 					continue;
    504 				} else if (strchr("123456789", *p) != NULL) {
    505 					*sp++ = '\\';
    506 					ref = *p - '0';
    507 					if (s->re != NULL &&
    508 					    ref > s->re->re_nsub)
    509 						err(COMPILE,
    510 "\\%c not defined in the RE", *p);
    511 					if (s->maxbref < ref)
    512 						s->maxbref = ref;
    513 				} else if (*p == '&' || *p == '\\')
    514 					*sp++ = '\\';
    515 			} else if (*p == c) {
    516 				p++;
    517 				*sp++ = '\0';
    518 				size += sp - op;
    519 				s->new = xrealloc(text, size);
    520 				return (p);
    521 			} else if (*p == '\n') {
    522 				err(COMPILE,
    523 "unescaped newline inside substitute pattern");
    524 				/* NOTREACHED */
    525 			}
    526 			*sp++ = *p;
    527 		}
    528 		size += sp - op;
    529 	} while ((p = cu_fgets(&lbuf, &bufsize)));
    530 	err(COMPILE, "unterminated substitute in regular expression");
    531 	/* NOTREACHED */
    532 	return NULL;
    533 }
    534 
    535 /*
    536  * Compile the flags of the s command
    537  */
    538 static char *
    539 compile_flags(char *p, struct s_subst *s)
    540 {
    541 	int gn;			/* True if we have seen g or n */
    542 	long l;
    543 	char wfile[PATH_MAX], *q;
    544 
    545 	s->n = 1;				/* Default */
    546 	s->p = 0;
    547 	s->wfile = NULL;
    548 	s->wfd = -1;
    549 	for (gn = 0;;) {
    550 		EATSPACE();			/* EXTENSION */
    551 		switch (*p) {
    552 		case 'g':
    553 			if (gn)
    554 				err(COMPILE, "more than one number or 'g' in"
    555 				    " substitute flags");
    556 			gn = 1;
    557 			s->n = 0;
    558 			break;
    559 		case '\0':
    560 		case '\n':
    561 		case ';':
    562 			return (p);
    563 		case 'p':
    564 			s->p = 1;
    565 			break;
    566 		case '1': case '2': case '3':
    567 		case '4': case '5': case '6':
    568 		case '7': case '8': case '9':
    569 			if (gn)
    570 				err(COMPILE, "more than one number or 'g' in"
    571 				    " substitute flags");
    572 			gn = 1;
    573 			l = strtol(p, &p, 10);
    574 			if (l <= 0 || l >= INT_MAX)
    575 				err(COMPILE,
    576 				    "number in substitute flags out of range");
    577 			s->n = (int)l;
    578 			continue;
    579 		case 'w':
    580 			p++;
    581 #ifdef HISTORIC_PRACTICE
    582 			if (*p != ' ') {
    583 				err(WARNING, "space missing before w wfile");
    584 				return (p);
    585 			}
    586 #endif
    587 			EATSPACE();
    588 			q = wfile;
    589 			while (*p) {
    590 				if (*p == '\n')
    591 					break;
    592 				*q++ = *p++;
    593 			}
    594 			*q = '\0';
    595 			if (q == wfile)
    596 				err(COMPILE, "no wfile specified");
    597 			s->wfile = strdup(wfile);
    598 			if (!aflag && (s->wfd = open(wfile,
    599 			    O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
    600 			    DEFFILEMODE)) == -1)
    601 				err(FATAL, "%s: %s", wfile, strerror(errno));
    602 			return (p);
    603 		default:
    604 			err(COMPILE,
    605 			    "bad flag in substitute command: '%c'", *p);
    606 			break;
    607 		}
    608 		p++;
    609 	}
    610 }
    611 
    612 /*
    613  * Compile a translation set of strings into a lookup table.
    614  */
    615 static char *
    616 compile_tr(char *p, char **transtab)
    617 {
    618 	int i;
    619 	char *lt, *op, *np;
    620 	char *old = NULL, *new = NULL;
    621 
    622 	if (*p == '\0' || *p == '\\')
    623 		err(COMPILE,
    624 "transform pattern can not be delimited by newline or backslash");
    625 	old = xmalloc(strlen(p) + 1);
    626 	p = compile_delimited(p, old, 1);
    627 	if (p == NULL) {
    628 		err(COMPILE, "unterminated transform source string");
    629 		goto bad;
    630 	}
    631 	new = xmalloc(strlen(p) + 1);
    632 	p = compile_delimited(--p, new, 1);
    633 	if (p == NULL) {
    634 		err(COMPILE, "unterminated transform target string");
    635 		goto bad;
    636 	}
    637 	EATSPACE();
    638 	if (strlen(new) != strlen(old)) {
    639 		err(COMPILE, "transform strings are not the same length");
    640 		goto bad;
    641 	}
    642 	/* We assume characters are 8 bits */
    643 	lt = xmalloc(UCHAR_MAX + 1);
    644 	for (i = 0; i <= UCHAR_MAX; i++)
    645 		lt[i] = (char)i;
    646 	for (op = old, np = new; *op; op++, np++)
    647 		lt[(u_char)*op] = *np;
    648 	*transtab = lt;
    649 	free(old);
    650 	free(new);
    651 	return (p);
    652 bad:
    653 	free(old);
    654 	free(new);
    655 	return (NULL);
    656 }
    657 
    658 /*
    659  * Compile the text following an a, c, or i command.
    660  */
    661 static char *
    662 compile_text(void)
    663 {
    664 	int asize, esc_nl, size;
    665 	char *lbuf, *text, *p, *op, *s;
    666 	size_t bufsize;
    667 
    668 	lbuf = text = NULL;
    669 	asize = size = 0;
    670 	while ((p = cu_fgets(&lbuf, &bufsize))) {
    671 		size_t len = ROUNDLEN(strlen(p) + 1);
    672 		if (asize - size < len) {
    673 			do {
    674 				asize += len;
    675 			} while (asize - size < len);
    676 			text = xrealloc(text, asize);
    677 		}
    678 		op = s = text + size;
    679 		for (esc_nl = 0; *p != '\0'; p++) {
    680 			if (*p == '\\' && p[1] != '\0' && *++p == '\n')
    681 				esc_nl = 1;
    682 			*s++ = *p;
    683 		}
    684 		size += s - op;
    685 		if (!esc_nl) {
    686 			*s = '\0';
    687 			break;
    688 		}
    689 	}
    690 	free(lbuf);
    691 	text = xrealloc(text, size + 1);
    692 	text[size] = '\0';
    693 	return (text);
    694 }
    695 
    696 /*
    697  * Get an address and return a pointer to the first character after
    698  * it.  Fill the structure pointed to according to the address.
    699  */
    700 static char *
    701 compile_addr(char *p, struct s_addr *a)
    702 {
    703 	char *end;
    704 
    705 	switch (*p) {
    706 	case '\\':				/* Context address */
    707 		++p;
    708 		/* FALLTHROUGH */
    709 	case '/':				/* Context address */
    710 		p = compile_re(p, &a->u.r);
    711 		if (p == NULL)
    712 			err(COMPILE, "unterminated regular expression");
    713 		a->type = AT_RE;
    714 		return (p);
    715 
    716 	case '$':				/* Last line */
    717 		a->type = AT_LAST;
    718 		return (p + 1);
    719 						/* Line number */
    720 	case '0': case '1': case '2': case '3': case '4':
    721 	case '5': case '6': case '7': case '8': case '9':
    722 		a->type = AT_LINE;
    723 		a->u.l = strtoul(p, &end, 10);
    724 		return (end);
    725 	default:
    726 		err(COMPILE, "expected context address");
    727 		return (NULL);
    728 	}
    729 }
    730 
    731 /*
    732  * duptoeol --
    733  *	Return a copy of all the characters up to \n or \0.
    734  */
    735 static char *
    736 duptoeol(char *s, char *ctype, char **semi)
    737 {
    738 	size_t len;
    739 	int ws;
    740 	char *start;
    741 
    742 	ws = 0;
    743 	if (semi) {
    744 		for (start = s; *s != '\0' && *s != '\n' && *s != ';'; ++s)
    745 			ws = isspace((unsigned char)*s);
    746 	} else {
    747 		for (start = s; *s != '\0' && *s != '\n'; ++s)
    748 			ws = isspace((unsigned char)*s);
    749 		*s = '\0';
    750 	}
    751 	if (ws)
    752 		err(WARNING, "whitespace after %s", ctype);
    753 	len = s - start + 1;
    754 	if (semi)
    755 		*semi = s;
    756 	s = xmalloc(len);
    757 	strlcpy(s, start, len);
    758 	return (s);
    759 }
    760 
    761 /*
    762  * Convert goto label names to addresses, and count a and r commands, in
    763  * the given subset of the script.  Free the memory used by labels in b
    764  * and t commands (but not by :).
    765  *
    766  * TODO: Remove } nodes
    767  */
    768 static void
    769 fixuplabel(struct s_command *cp, struct s_command *end)
    770 {
    771 
    772 	for (; cp != end; cp = cp->next)
    773 		switch (cp->code) {
    774 		case 'a':
    775 		case 'r':
    776 			appendnum++;
    777 			break;
    778 		case 'b':
    779 		case 't':
    780 			/* Resolve branch target. */
    781 			if (cp->t == NULL) {
    782 				cp->u.c = NULL;
    783 				break;
    784 			}
    785 			if ((cp->u.c = findlabel(cp->t)) == NULL)
    786 				err(COMPILE2, "undefined label '%s'", cp->t);
    787 			free(cp->t);
    788 			break;
    789 		case '{':
    790 			/* Do interior commands. */
    791 			fixuplabel(cp->u.c, cp->next);
    792 			break;
    793 		}
    794 }
    795 
    796 /*
    797  * Associate the given command label for later lookup.
    798  */
    799 static void
    800 enterlabel(struct s_command *cp)
    801 {
    802 	struct labhash **lhp, *lh;
    803 	u_char *p;
    804 	u_int h, c;
    805 
    806 	for (h = 0, p = (u_char *)cp->t; (c = *p) != 0; p++)
    807 		h = (h << 5) + h + c;
    808 	lhp = &labels[h & LHMASK];
    809 	for (lh = *lhp; lh != NULL; lh = lh->lh_next)
    810 		if (lh->lh_hash == h && strcmp(cp->t, lh->lh_cmd->t) == 0)
    811 			err(COMPILE2, "duplicate label '%s'", cp->t);
    812 	lh = xmalloc(sizeof *lh);
    813 	lh->lh_next = *lhp;
    814 	lh->lh_hash = h;
    815 	lh->lh_cmd = cp;
    816 	lh->lh_ref = 0;
    817 	*lhp = lh;
    818 }
    819 
    820 /*
    821  * Find the label contained in the command l in the command linked
    822  * list cp.  L is excluded from the search.  Return NULL if not found.
    823  */
    824 static struct s_command *
    825 findlabel(char *name)
    826 {
    827 	struct labhash *lh;
    828 	u_char *p;
    829 	u_int h, c;
    830 
    831 	for (h = 0, p = (u_char *)name; (c = *p) != 0; p++)
    832 		h = (h << 5) + h + c;
    833 	for (lh = labels[h & LHMASK]; lh != NULL; lh = lh->lh_next) {
    834 		if (lh->lh_hash == h && strcmp(name, lh->lh_cmd->t) == 0) {
    835 			lh->lh_ref = 1;
    836 			return (lh->lh_cmd);
    837 		}
    838 	}
    839 	return (NULL);
    840 }
    841 
    842 /*
    843  * Warn about any unused labels.  As a side effect, release the label hash
    844  * table space.
    845  */
    846 static void
    847 uselabel(void)
    848 {
    849 	struct labhash *lh, *next;
    850 	int i;
    851 
    852 	for (i = 0; i < LHSZ; i++) {
    853 		for (lh = labels[i]; lh != NULL; lh = next) {
    854 			next = lh->lh_next;
    855 			if (!lh->lh_ref)
    856 				err(WARNING, "unused label '%s'",
    857 				    lh->lh_cmd->t);
    858 			free(lh);
    859 		}
    860 	}
    861 }