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