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