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