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