2024-07-17 13:57:15 -07:00
|
|
|
/* $OpenBSD: compile.c,v 1.53 2024/07/17 20:57:15 millert Exp $ */
|
1996-06-25 22:31:08 -07:00
|
|
|
|
1995-10-18 01:37:01 -07:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1992 Diomidis Spinellis.
|
|
|
|
* Copyright (c) 1992, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Diomidis Spinellis of Imperial College, University of London.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-06-02 19:56:05 -07:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1995-10-18 01:37:01 -07:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
2024-07-17 13:57:15 -07:00
|
|
|
#include <err.h>
|
1995-10-18 01:37:01 -07:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <regex.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "defs.h"
|
|
|
|
#include "extern.h"
|
|
|
|
|
|
|
|
#define LHSZ 128
|
|
|
|
#define LHMASK (LHSZ - 1)
|
|
|
|
static struct labhash {
|
|
|
|
struct labhash *lh_next;
|
|
|
|
u_int lh_hash;
|
|
|
|
struct s_command *lh_cmd;
|
|
|
|
int lh_ref;
|
|
|
|
} *labels[LHSZ];
|
|
|
|
|
2002-02-16 13:27:05 -08:00
|
|
|
static char *compile_addr(char *, struct s_addr *);
|
|
|
|
static char *compile_ccl(char **, char *);
|
2017-12-11 05:25:57 -08:00
|
|
|
static char *compile_delimited(char *, char *);
|
2002-02-16 13:27:05 -08:00
|
|
|
static char *compile_flags(char *, struct s_subst *);
|
|
|
|
static char *compile_re(char *, regex_t **);
|
|
|
|
static char *compile_subst(char *, struct s_subst *);
|
|
|
|
static char *compile_text(void);
|
|
|
|
static char *compile_tr(char *, char **);
|
1995-10-18 01:37:01 -07:00
|
|
|
static struct s_command
|
2002-02-16 13:27:05 -08:00
|
|
|
**compile_stream(struct s_command **);
|
2003-10-07 10:56:26 -07:00
|
|
|
static char *duptoeol(char *, char *, char **);
|
2002-02-16 13:27:05 -08:00
|
|
|
static void enterlabel(struct s_command *);
|
1995-10-18 01:37:01 -07:00
|
|
|
static struct s_command
|
2002-02-16 13:27:05 -08:00
|
|
|
*findlabel(char *);
|
|
|
|
static void fixuplabel(struct s_command *, struct s_command *);
|
|
|
|
static void uselabel(void);
|
1995-10-18 01:37:01 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Command specification. This is used to drive the command parser.
|
|
|
|
*/
|
|
|
|
struct s_format {
|
|
|
|
char code; /* Command code */
|
|
|
|
int naddr; /* Number of address args */
|
|
|
|
enum e_args args; /* Argument type */
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct s_format cmd_fmts[] = {
|
|
|
|
{'{', 2, GROUP},
|
|
|
|
{'}', 0, ENDGROUP},
|
|
|
|
{'a', 1, TEXT},
|
|
|
|
{'b', 2, BRANCH},
|
|
|
|
{'c', 2, TEXT},
|
|
|
|
{'d', 2, EMPTY},
|
|
|
|
{'D', 2, EMPTY},
|
|
|
|
{'g', 2, EMPTY},
|
|
|
|
{'G', 2, EMPTY},
|
|
|
|
{'h', 2, EMPTY},
|
|
|
|
{'H', 2, EMPTY},
|
|
|
|
{'i', 1, TEXT},
|
|
|
|
{'l', 2, EMPTY},
|
|
|
|
{'n', 2, EMPTY},
|
|
|
|
{'N', 2, EMPTY},
|
|
|
|
{'p', 2, EMPTY},
|
|
|
|
{'P', 2, EMPTY},
|
|
|
|
{'q', 1, EMPTY},
|
|
|
|
{'r', 1, RFILE},
|
|
|
|
{'s', 2, SUBST},
|
|
|
|
{'t', 2, BRANCH},
|
|
|
|
{'w', 2, WFILE},
|
|
|
|
{'x', 2, EMPTY},
|
|
|
|
{'y', 2, TR},
|
|
|
|
{'!', 2, NONSEL},
|
|
|
|
{':', 0, LABEL},
|
|
|
|
{'#', 0, COMMENT},
|
|
|
|
{'=', 1, EMPTY},
|
|
|
|
{'\0', 0, COMMENT},
|
|
|
|
};
|
|
|
|
|
|
|
|
/* The compiled program. */
|
|
|
|
struct s_command *prog;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compile the program into prog.
|
|
|
|
* Initialise appends.
|
|
|
|
*/
|
|
|
|
void
|
2003-06-10 15:20:44 -07:00
|
|
|
compile(void)
|
1995-10-18 01:37:01 -07:00
|
|
|
{
|
|
|
|
*compile_stream(&prog) = NULL;
|
|
|
|
fixuplabel(prog, NULL);
|
|
|
|
uselabel();
|
2014-10-07 21:19:08 -07:00
|
|
|
appends = xreallocarray(NULL, appendnum, sizeof(struct s_appends));
|
|
|
|
match = xreallocarray(NULL, maxnsub + 1, sizeof(regmatch_t));
|
1995-10-18 01:37:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#define EATSPACE() do { \
|
|
|
|
if (p) \
|
2013-11-28 10:24:54 -08:00
|
|
|
while (isascii((unsigned char)*p) && \
|
|
|
|
isspace((unsigned char)*p)) \
|
1995-10-18 01:37:01 -07:00
|
|
|
p++; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
static struct s_command **
|
2003-06-10 15:20:44 -07:00
|
|
|
compile_stream(struct s_command **link)
|
1995-10-18 01:37:01 -07:00
|
|
|
{
|
2001-11-19 11:02:13 -08:00
|
|
|
char *p;
|
2008-10-08 10:26:47 -07:00
|
|
|
static char *lbuf; /* To avoid excessive malloc calls */
|
2008-10-16 09:34:32 -07:00
|
|
|
static size_t bufsize;
|
1995-10-18 01:37:01 -07:00
|
|
|
struct s_command *cmd, *cmd2, *stack;
|
|
|
|
struct s_format *fp;
|
|
|
|
int naddr; /* Number of addresses */
|
|
|
|
|
|
|
|
stack = 0;
|
|
|
|
for (;;) {
|
2024-06-17 17:32:22 -07:00
|
|
|
if ((p = cu_getline(&lbuf, &bufsize)) == NULL) {
|
1995-10-18 01:37:01 -07:00
|
|
|
if (stack != 0)
|
2024-07-17 13:57:15 -07:00
|
|
|
error("unexpected EOF (pending }'s)");
|
1995-10-18 01:37:01 -07:00
|
|
|
return (link);
|
|
|
|
}
|
|
|
|
|
|
|
|
semicolon: EATSPACE();
|
2007-01-09 00:37:44 -08:00
|
|
|
if (*p == '#' || *p == '\0')
|
1995-10-18 01:37:01 -07:00
|
|
|
continue;
|
2007-01-09 00:37:44 -08:00
|
|
|
if (*p == ';') {
|
|
|
|
p++;
|
|
|
|
goto semicolon;
|
|
|
|
}
|
1995-10-18 01:37:01 -07:00
|
|
|
*link = cmd = xmalloc(sizeof(struct s_command));
|
|
|
|
link = &cmd->next;
|
|
|
|
cmd->nonsel = cmd->inrange = 0;
|
|
|
|
/* First parse the addresses */
|
|
|
|
naddr = 0;
|
|
|
|
|
|
|
|
/* Valid characters to start an address */
|
|
|
|
#define addrchar(c) (strchr("0123456789/\\$", (c)))
|
|
|
|
if (addrchar(*p)) {
|
|
|
|
naddr++;
|
|
|
|
cmd->a1 = xmalloc(sizeof(struct s_addr));
|
|
|
|
p = compile_addr(p, cmd->a1);
|
|
|
|
EATSPACE(); /* EXTENSION */
|
|
|
|
if (*p == ',') {
|
|
|
|
p++;
|
|
|
|
EATSPACE(); /* EXTENSION */
|
|
|
|
naddr++;
|
|
|
|
cmd->a2 = xmalloc(sizeof(struct s_addr));
|
|
|
|
p = compile_addr(p, cmd->a2);
|
|
|
|
EATSPACE();
|
2006-10-08 17:23:56 -07:00
|
|
|
} else {
|
1995-10-18 01:37:01 -07:00
|
|
|
cmd->a2 = 0;
|
2006-10-08 17:23:56 -07:00
|
|
|
}
|
|
|
|
} else {
|
1995-10-18 01:37:01 -07:00
|
|
|
cmd->a1 = cmd->a2 = 0;
|
2006-10-08 17:23:56 -07:00
|
|
|
}
|
1995-10-18 01:37:01 -07:00
|
|
|
|
|
|
|
nonsel: /* Now parse the command */
|
|
|
|
if (!*p)
|
2024-07-17 13:57:15 -07:00
|
|
|
error("command expected");
|
1995-10-18 01:37:01 -07:00
|
|
|
cmd->code = *p;
|
|
|
|
for (fp = cmd_fmts; fp->code; fp++)
|
|
|
|
if (fp->code == *p)
|
|
|
|
break;
|
|
|
|
if (!fp->code)
|
2024-07-17 13:57:15 -07:00
|
|
|
error("invalid command code %c", *p);
|
1995-10-18 01:37:01 -07:00
|
|
|
if (naddr > fp->naddr)
|
2024-07-17 13:57:15 -07:00
|
|
|
error("command %c expects up to %d address(es),"
|
|
|
|
" found %d", *p, fp->naddr, naddr);
|
1995-10-18 01:37:01 -07:00
|
|
|
switch (fp->args) {
|
|
|
|
case NONSEL: /* ! */
|
|
|
|
p++;
|
|
|
|
EATSPACE();
|
2015-06-23 15:52:55 -07:00
|
|
|
cmd->nonsel = 1;
|
1995-10-18 01:37:01 -07:00
|
|
|
goto nonsel;
|
|
|
|
case GROUP: /* { */
|
|
|
|
p++;
|
|
|
|
EATSPACE();
|
|
|
|
cmd->next = stack;
|
|
|
|
stack = cmd;
|
|
|
|
link = &cmd->u.c;
|
|
|
|
if (*p)
|
|
|
|
goto semicolon;
|
|
|
|
break;
|
|
|
|
case ENDGROUP:
|
|
|
|
/*
|
|
|
|
* Short-circuit command processing, since end of
|
|
|
|
* group is really just a noop.
|
|
|
|
*/
|
|
|
|
cmd->nonsel = 1;
|
|
|
|
if (stack == 0)
|
2024-07-17 13:57:15 -07:00
|
|
|
error("unexpected }");
|
1995-10-18 01:37:01 -07:00
|
|
|
cmd2 = stack;
|
|
|
|
stack = cmd2->next;
|
|
|
|
cmd2->next = cmd;
|
|
|
|
/*FALLTHROUGH*/
|
|
|
|
case EMPTY: /* d D g G h H l n N p P q x = \0 */
|
|
|
|
p++;
|
|
|
|
EATSPACE();
|
|
|
|
if (*p == ';') {
|
|
|
|
p++;
|
|
|
|
link = &cmd->next;
|
|
|
|
goto semicolon;
|
|
|
|
}
|
|
|
|
if (*p)
|
2024-07-17 13:57:15 -07:00
|
|
|
error("extra characters at the end of %c"
|
|
|
|
" command", cmd->code);
|
1995-10-18 01:37:01 -07:00
|
|
|
break;
|
|
|
|
case TEXT: /* a c i */
|
|
|
|
p++;
|
|
|
|
EATSPACE();
|
|
|
|
if (*p != '\\')
|
2024-07-17 13:57:15 -07:00
|
|
|
error("command %c expects \\ followed by text",
|
|
|
|
cmd->code);
|
1995-10-18 01:37:01 -07:00
|
|
|
p++;
|
|
|
|
EATSPACE();
|
|
|
|
if (*p)
|
2024-07-17 13:57:15 -07:00
|
|
|
error("extra characters after \\ at the"
|
2006-10-08 17:23:56 -07:00
|
|
|
" end of %c command", cmd->code);
|
1995-10-18 01:37:01 -07:00
|
|
|
cmd->t = compile_text();
|
|
|
|
break;
|
|
|
|
case COMMENT: /* \0 # */
|
|
|
|
break;
|
|
|
|
case WFILE: /* w */
|
|
|
|
p++;
|
|
|
|
EATSPACE();
|
|
|
|
if (*p == '\0')
|
2024-07-17 13:57:15 -07:00
|
|
|
error("filename expected");
|
2003-10-07 10:56:26 -07:00
|
|
|
cmd->t = duptoeol(p, "w command", NULL);
|
2017-08-01 11:05:53 -07:00
|
|
|
if (aflag) {
|
1995-10-18 01:37:01 -07:00
|
|
|
cmd->u.fd = -1;
|
2017-08-01 11:05:53 -07:00
|
|
|
pledge_wpath = 1;
|
|
|
|
}
|
2005-04-11 00:11:44 -07:00
|
|
|
else if ((cmd->u.fd = open(p,
|
1995-10-18 01:37:01 -07:00
|
|
|
O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
|
|
|
|
DEFFILEMODE)) == -1)
|
2024-07-17 13:57:15 -07:00
|
|
|
err(1, "%s", p);
|
1995-10-18 01:37:01 -07:00
|
|
|
break;
|
|
|
|
case RFILE: /* r */
|
2017-08-01 11:05:53 -07:00
|
|
|
pledge_rpath = 1;
|
1995-10-18 01:37:01 -07:00
|
|
|
p++;
|
|
|
|
EATSPACE();
|
2017-12-08 10:41:59 -08:00
|
|
|
if (*p == '\0')
|
2024-07-17 13:57:15 -07:00
|
|
|
error("filename expected");
|
2003-10-07 10:56:26 -07:00
|
|
|
cmd->t = duptoeol(p, "read command", NULL);
|
1995-10-18 01:37:01 -07:00
|
|
|
break;
|
|
|
|
case BRANCH: /* b t */
|
|
|
|
p++;
|
|
|
|
EATSPACE();
|
2018-12-07 06:45:40 -08:00
|
|
|
if (*p == '\0' || *p == ';')
|
1995-10-18 01:37:01 -07:00
|
|
|
cmd->t = NULL;
|
|
|
|
else
|
2003-10-07 10:56:26 -07:00
|
|
|
cmd->t = duptoeol(p, "branch", &p);
|
|
|
|
if (*p == ';') {
|
|
|
|
p++;
|
|
|
|
goto semicolon;
|
|
|
|
}
|
1995-10-18 01:37:01 -07:00
|
|
|
break;
|
|
|
|
case LABEL: /* : */
|
|
|
|
p++;
|
|
|
|
EATSPACE();
|
2003-10-07 10:56:26 -07:00
|
|
|
cmd->t = duptoeol(p, "label", &p);
|
|
|
|
if (strlen(cmd->t) == 0)
|
2024-07-17 13:57:15 -07:00
|
|
|
error("empty label");
|
1995-10-18 01:37:01 -07:00
|
|
|
enterlabel(cmd);
|
2003-10-07 10:56:26 -07:00
|
|
|
if (*p == ';') {
|
|
|
|
p++;
|
|
|
|
goto semicolon;
|
|
|
|
}
|
1995-10-18 01:37:01 -07:00
|
|
|
break;
|
|
|
|
case SUBST: /* s */
|
|
|
|
p++;
|
|
|
|
if (*p == '\0' || *p == '\\')
|
2024-07-17 13:57:15 -07:00
|
|
|
error("substitute pattern can not be"
|
2006-10-08 17:23:56 -07:00
|
|
|
" delimited by newline or backslash");
|
1995-10-18 01:37:01 -07:00
|
|
|
cmd->u.s = xmalloc(sizeof(struct s_subst));
|
|
|
|
p = compile_re(p, &cmd->u.s->re);
|
|
|
|
if (p == NULL)
|
2024-07-17 13:57:15 -07:00
|
|
|
error("unterminated substitute pattern");
|
1995-10-18 01:37:01 -07:00
|
|
|
--p;
|
|
|
|
p = compile_subst(p, cmd->u.s);
|
|
|
|
p = compile_flags(p, cmd->u.s);
|
|
|
|
EATSPACE();
|
|
|
|
if (*p == ';') {
|
|
|
|
p++;
|
|
|
|
link = &cmd->next;
|
|
|
|
goto semicolon;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TR: /* y */
|
|
|
|
p++;
|
|
|
|
p = compile_tr(p, (char **)&cmd->u.y);
|
|
|
|
EATSPACE();
|
|
|
|
if (*p == ';') {
|
|
|
|
p++;
|
|
|
|
link = &cmd->next;
|
|
|
|
goto semicolon;
|
|
|
|
}
|
|
|
|
if (*p)
|
2024-07-17 13:57:15 -07:00
|
|
|
error("extra text at the end of a"
|
2006-10-08 17:23:56 -07:00
|
|
|
" transform command");
|
1995-10-18 01:37:01 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-12-26 11:16:00 -08:00
|
|
|
* Get a delimited string. P points to the delimiter of the string; d points
|
1995-10-18 01:37:01 -07:00
|
|
|
* to a buffer area. Newline and delimiter escapes are processed; other
|
|
|
|
* escapes are ignored.
|
|
|
|
*
|
|
|
|
* Returns a pointer to the first character after the final delimiter or NULL
|
|
|
|
* in the case of a non-terminated string. The character array d is filled
|
|
|
|
* with the processed string.
|
|
|
|
*/
|
|
|
|
static char *
|
2017-12-11 05:25:57 -08:00
|
|
|
compile_delimited(char *p, char *d)
|
1995-10-18 01:37:01 -07:00
|
|
|
{
|
|
|
|
char c;
|
|
|
|
|
|
|
|
c = *p++;
|
|
|
|
if (c == '\0')
|
|
|
|
return (NULL);
|
|
|
|
else if (c == '\\')
|
2024-07-17 13:57:15 -07:00
|
|
|
error("\\ can not be used as a string delimiter");
|
1995-10-18 01:37:01 -07:00
|
|
|
else if (c == '\n')
|
2024-07-17 13:57:15 -07:00
|
|
|
error("newline can not be used as a string delimiter");
|
2018-08-14 11:10:09 -07:00
|
|
|
|
|
|
|
while (p[0]) {
|
|
|
|
/* Unescaped delimiter: We are done. */
|
|
|
|
if (p[0] == c) {
|
1995-10-18 01:37:01 -07:00
|
|
|
*d = '\0';
|
2018-08-14 11:10:09 -07:00
|
|
|
return p + 1;
|
1995-10-18 01:37:01 -07:00
|
|
|
}
|
2018-08-14 11:10:09 -07:00
|
|
|
if (p[0] == '\\') {
|
|
|
|
/* Escaped delimiter: Skip the backslash. */
|
|
|
|
if (p[1] == c) {
|
|
|
|
p++;
|
|
|
|
} else {
|
|
|
|
/* Backslash-n: Match linefeed. */
|
|
|
|
if (p[1] == 'n') {
|
|
|
|
*d++ = '\n';
|
|
|
|
p += 2;
|
|
|
|
/* Other escapes remain unchanged. */
|
|
|
|
} else {
|
|
|
|
*d++ = *p++;
|
|
|
|
*d++ = *p++;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p[0] != '[')
|
|
|
|
*d++ = *p++;
|
|
|
|
/*
|
|
|
|
* Bracket expression:
|
|
|
|
* It may contain the delimiter without escaping.
|
|
|
|
*/
|
|
|
|
else if ((d = compile_ccl(&p, d)) == NULL)
|
2024-07-17 13:57:15 -07:00
|
|
|
error("unbalanced brackets ([])");
|
1995-10-18 01:37:01 -07:00
|
|
|
}
|
2018-08-14 11:10:09 -07:00
|
|
|
return NULL;
|
1995-10-18 01:37:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* compile_ccl: expand a POSIX character class */
|
|
|
|
static char *
|
2003-06-10 15:20:44 -07:00
|
|
|
compile_ccl(char **sp, char *t)
|
1995-10-18 01:37:01 -07:00
|
|
|
{
|
|
|
|
int c, d;
|
|
|
|
char *s = *sp;
|
|
|
|
|
|
|
|
*t++ = *s++;
|
|
|
|
if (*s == '^')
|
|
|
|
*t++ = *s++;
|
|
|
|
if (*s == ']')
|
|
|
|
*t++ = *s++;
|
|
|
|
for (; *s && (*t = *s) != ']'; s++, t++)
|
|
|
|
if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) {
|
|
|
|
*++t = *++s, t++, s++;
|
|
|
|
for (c = *s; (*t = *s) != ']' || c != d; s++, t++)
|
|
|
|
if ((c = *s) == '\0')
|
|
|
|
return NULL;
|
2006-10-08 17:23:56 -07:00
|
|
|
} else if (*s == '\\' && s[1] == 'n') {
|
|
|
|
*t = '\n';
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
if (*s == ']') {
|
|
|
|
*sp = ++s;
|
|
|
|
return (++t);
|
|
|
|
} else {
|
|
|
|
return (NULL);
|
|
|
|
}
|
1995-10-18 01:37:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get a regular expression. P points to the delimiter of the regular
|
|
|
|
* expression; repp points to the address of a regexp pointer. Newline
|
|
|
|
* and delimiter escapes are processed; other escapes are ignored.
|
|
|
|
* Returns a pointer to the first character after the final delimiter
|
|
|
|
* or NULL in the case of a non terminated regular expression. The regexp
|
|
|
|
* pointer is set to the compiled regular expression.
|
|
|
|
* Cflags are passed to regcomp.
|
|
|
|
*/
|
|
|
|
static char *
|
2003-06-10 15:20:44 -07:00
|
|
|
compile_re(char *p, regex_t **repp)
|
1995-10-18 01:37:01 -07:00
|
|
|
{
|
|
|
|
int eval;
|
2008-10-08 10:26:47 -07:00
|
|
|
char *re;
|
1995-10-18 01:37:01 -07:00
|
|
|
|
2008-10-08 10:26:47 -07:00
|
|
|
re = xmalloc(strlen(p) + 1); /* strlen(re) <= strlen(p) */
|
2017-12-11 05:25:57 -08:00
|
|
|
p = compile_delimited(p, re);
|
1995-10-18 01:37:01 -07:00
|
|
|
if (p && strlen(re) == 0) {
|
|
|
|
*repp = NULL;
|
2008-10-08 10:26:47 -07:00
|
|
|
free(re);
|
1995-10-18 01:37:01 -07:00
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
*repp = xmalloc(sizeof(regex_t));
|
2009-08-06 20:30:56 -07:00
|
|
|
if (p && (eval = regcomp(*repp, re, Eflag ? REG_EXTENDED : 0)) != 0)
|
2024-07-17 13:57:15 -07:00
|
|
|
error("RE error: %s", strregerror(eval, *repp));
|
1995-10-18 01:37:01 -07:00
|
|
|
if (maxnsub < (*repp)->re_nsub)
|
|
|
|
maxnsub = (*repp)->re_nsub;
|
2008-10-08 10:26:47 -07:00
|
|
|
free(re);
|
1995-10-18 01:37:01 -07:00
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compile the substitution string of a regular expression and set res to
|
|
|
|
* point to a saved copy of it. Nsub is the number of parenthesized regular
|
|
|
|
* expressions.
|
|
|
|
*/
|
|
|
|
static char *
|
2003-06-10 15:20:44 -07:00
|
|
|
compile_subst(char *p, struct s_subst *s)
|
1995-10-18 01:37:01 -07:00
|
|
|
{
|
2008-10-08 10:26:47 -07:00
|
|
|
static char *lbuf;
|
2008-10-16 09:34:32 -07:00
|
|
|
static size_t bufsize;
|
2017-12-13 08:06:34 -08:00
|
|
|
size_t asize, ref, size;
|
1995-10-18 01:37:01 -07:00
|
|
|
char c, *text, *op, *sp;
|
2004-07-09 12:39:40 -07:00
|
|
|
int sawesc = 0;
|
1995-10-18 01:37:01 -07:00
|
|
|
|
|
|
|
c = *p++; /* Terminator character */
|
|
|
|
if (c == '\0')
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
s->maxbref = 0;
|
|
|
|
s->linenum = linenum;
|
2008-10-16 09:34:32 -07:00
|
|
|
text = NULL;
|
|
|
|
asize = size = 0;
|
1995-10-18 01:37:01 -07:00
|
|
|
do {
|
2008-10-16 09:34:32 -07:00
|
|
|
size_t len = ROUNDLEN(strlen(p) + 1);
|
|
|
|
if (asize - size < len) {
|
2008-10-08 10:26:47 -07:00
|
|
|
do {
|
2008-10-16 09:34:32 -07:00
|
|
|
asize += len;
|
|
|
|
} while (asize - size < len);
|
2008-10-08 10:26:47 -07:00
|
|
|
text = xrealloc(text, asize);
|
|
|
|
}
|
1995-10-18 01:37:01 -07:00
|
|
|
op = sp = text + size;
|
|
|
|
for (; *p; p++) {
|
2004-07-09 12:39:40 -07:00
|
|
|
if (*p == '\\' || sawesc) {
|
|
|
|
/*
|
|
|
|
* If this is a continuation from the last
|
|
|
|
* buffer, we won't have a character to
|
|
|
|
* skip over.
|
|
|
|
*/
|
|
|
|
if (sawesc)
|
|
|
|
sawesc = 0;
|
|
|
|
else
|
|
|
|
p++;
|
|
|
|
|
|
|
|
if (*p == '\0') {
|
|
|
|
/*
|
|
|
|
* This escaped character is continued
|
|
|
|
* in the next part of the line. Note
|
|
|
|
* this fact, then cause the loop to
|
|
|
|
* exit w/ normal EOL case and reenter
|
|
|
|
* above with the new buffer.
|
|
|
|
*/
|
|
|
|
sawesc = 1;
|
|
|
|
p--;
|
|
|
|
continue;
|
|
|
|
} else if (strchr("123456789", *p) != NULL) {
|
1995-10-18 01:37:01 -07:00
|
|
|
*sp++ = '\\';
|
|
|
|
ref = *p - '0';
|
|
|
|
if (s->re != NULL &&
|
|
|
|
ref > s->re->re_nsub)
|
2024-07-17 13:57:15 -07:00
|
|
|
error("\\%c not defined in the"
|
|
|
|
" RE", *p);
|
1995-10-18 01:37:01 -07:00
|
|
|
if (s->maxbref < ref)
|
|
|
|
s->maxbref = ref;
|
|
|
|
} else if (*p == '&' || *p == '\\')
|
|
|
|
*sp++ = '\\';
|
|
|
|
} else if (*p == c) {
|
|
|
|
p++;
|
|
|
|
*sp++ = '\0';
|
|
|
|
size += sp - op;
|
|
|
|
s->new = xrealloc(text, size);
|
|
|
|
return (p);
|
|
|
|
} else if (*p == '\n') {
|
2024-07-17 13:57:15 -07:00
|
|
|
error("unescaped newline inside substitute"
|
|
|
|
" pattern");
|
1995-10-18 01:37:01 -07:00
|
|
|
}
|
|
|
|
*sp++ = *p;
|
|
|
|
}
|
|
|
|
size += sp - op;
|
2024-06-17 17:32:22 -07:00
|
|
|
} while ((p = cu_getline(&lbuf, &bufsize)));
|
2024-07-17 13:57:15 -07:00
|
|
|
error("unterminated substitute in regular expression");
|
1995-10-18 01:37:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compile the flags of the s command
|
|
|
|
*/
|
|
|
|
static char *
|
2003-06-10 15:20:44 -07:00
|
|
|
compile_flags(char *p, struct s_subst *s)
|
1995-10-18 01:37:01 -07:00
|
|
|
{
|
|
|
|
int gn; /* True if we have seen g or n */
|
2004-02-17 08:13:33 -08:00
|
|
|
long l;
|
1995-10-18 01:37:01 -07:00
|
|
|
|
|
|
|
s->n = 1; /* Default */
|
|
|
|
s->p = 0;
|
|
|
|
s->wfile = NULL;
|
|
|
|
s->wfd = -1;
|
|
|
|
for (gn = 0;;) {
|
|
|
|
EATSPACE(); /* EXTENSION */
|
|
|
|
switch (*p) {
|
|
|
|
case 'g':
|
|
|
|
if (gn)
|
2024-07-17 13:57:15 -07:00
|
|
|
error("more than one number or 'g' in"
|
2006-10-08 17:23:56 -07:00
|
|
|
" substitute flags");
|
1995-10-18 01:37:01 -07:00
|
|
|
gn = 1;
|
|
|
|
s->n = 0;
|
|
|
|
break;
|
|
|
|
case '\0':
|
|
|
|
case '\n':
|
|
|
|
case ';':
|
|
|
|
return (p);
|
|
|
|
case 'p':
|
|
|
|
s->p = 1;
|
|
|
|
break;
|
|
|
|
case '1': case '2': case '3':
|
|
|
|
case '4': case '5': case '6':
|
|
|
|
case '7': case '8': case '9':
|
|
|
|
if (gn)
|
2024-07-17 13:57:15 -07:00
|
|
|
error("more than one number or 'g' in"
|
2006-10-08 17:23:56 -07:00
|
|
|
" substitute flags");
|
1995-10-18 01:37:01 -07:00
|
|
|
gn = 1;
|
2004-02-17 08:13:33 -08:00
|
|
|
l = strtol(p, &p, 10);
|
|
|
|
if (l <= 0 || l >= INT_MAX)
|
2024-07-17 13:57:15 -07:00
|
|
|
error("number in substitute flags out of"
|
|
|
|
" range");
|
2004-02-17 08:13:33 -08:00
|
|
|
s->n = (int)l;
|
|
|
|
continue;
|
1995-10-18 01:37:01 -07:00
|
|
|
case 'w':
|
|
|
|
p++;
|
|
|
|
EATSPACE();
|
2017-12-08 10:41:59 -08:00
|
|
|
if (*p == '\0')
|
2024-07-17 13:57:15 -07:00
|
|
|
error("filename expected");
|
2017-12-08 10:41:59 -08:00
|
|
|
s->wfile = duptoeol(p, "s command w flag", NULL);
|
|
|
|
*p = '\0';
|
2017-08-01 11:05:53 -07:00
|
|
|
if (aflag)
|
|
|
|
pledge_wpath = 1;
|
2017-12-08 10:41:59 -08:00
|
|
|
else if ((s->wfd = open(s->wfile,
|
1995-10-18 01:37:01 -07:00
|
|
|
O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
|
|
|
|
DEFFILEMODE)) == -1)
|
2024-07-17 13:57:15 -07:00
|
|
|
err(1, "%s", s->wfile);
|
1995-10-18 01:37:01 -07:00
|
|
|
return (p);
|
|
|
|
default:
|
2024-07-17 13:57:15 -07:00
|
|
|
error("bad flag in substitute command: '%c'", *p);
|
1995-10-18 01:37:01 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compile a translation set of strings into a lookup table.
|
|
|
|
*/
|
|
|
|
static char *
|
2017-12-11 05:25:57 -08:00
|
|
|
compile_tr(char *old, char **transtab)
|
1995-10-18 01:37:01 -07:00
|
|
|
{
|
|
|
|
int i;
|
2017-12-12 04:52:01 -08:00
|
|
|
char delimiter, check[UCHAR_MAX + 1];
|
2017-12-11 05:25:57 -08:00
|
|
|
char *new, *end;
|
|
|
|
|
|
|
|
memset(check, 0, sizeof(check));
|
|
|
|
delimiter = *old;
|
|
|
|
if (delimiter == '\\')
|
2024-07-17 13:57:15 -07:00
|
|
|
error("\\ can not be used as a string delimiter");
|
2017-12-11 05:25:57 -08:00
|
|
|
else if (delimiter == '\n' || delimiter == '\0')
|
2024-07-17 13:57:15 -07:00
|
|
|
error("newline can not be used as a string delimiter");
|
2017-12-11 05:25:57 -08:00
|
|
|
|
|
|
|
new = old++;
|
|
|
|
do {
|
|
|
|
if ((new = strchr(new + 1, delimiter)) == NULL)
|
2024-07-17 13:57:15 -07:00
|
|
|
error("unterminated transform source string");
|
2017-12-11 05:25:57 -08:00
|
|
|
} while (*(new - 1) == '\\' && *(new -2) != '\\');
|
|
|
|
*new = '\0';
|
|
|
|
end = new++;
|
|
|
|
do {
|
|
|
|
if ((end = strchr(end + 1, delimiter)) == NULL)
|
2024-07-17 13:57:15 -07:00
|
|
|
error("unterminated transform target string");
|
2017-12-11 05:25:57 -08:00
|
|
|
} while (*(end -1) == '\\' && *(end -2) != '\\');
|
|
|
|
*end = '\0';
|
|
|
|
|
1995-10-18 01:37:01 -07:00
|
|
|
/* We assume characters are 8 bits */
|
2017-12-11 05:25:57 -08:00
|
|
|
*transtab = xmalloc(UCHAR_MAX + 1);
|
1995-10-18 01:37:01 -07:00
|
|
|
for (i = 0; i <= UCHAR_MAX; i++)
|
2017-12-11 05:25:57 -08:00
|
|
|
(*transtab)[i] = (char)i;
|
|
|
|
|
|
|
|
while (*old != '\0' && *new != '\0') {
|
|
|
|
if (*old == '\\') {
|
|
|
|
old++;
|
|
|
|
if (*old == 'n')
|
|
|
|
*old = '\n';
|
|
|
|
else if (*old != delimiter && *old != '\\')
|
2024-07-17 13:57:15 -07:00
|
|
|
error("Unexpected character after backslash");
|
2017-12-11 05:25:57 -08:00
|
|
|
}
|
|
|
|
if (*new == '\\') {
|
|
|
|
new++;
|
|
|
|
if (*new == 'n')
|
|
|
|
*new = '\n';
|
|
|
|
else if (*new != delimiter && *new != '\\')
|
2024-07-17 13:57:15 -07:00
|
|
|
error("Unexpected character after backslash");
|
2017-12-11 05:25:57 -08:00
|
|
|
}
|
2017-12-13 08:07:54 -08:00
|
|
|
if (check[(u_char) *old] == 1)
|
2024-07-17 13:57:15 -07:00
|
|
|
error("Repeated character in source string");
|
2017-12-13 08:07:54 -08:00
|
|
|
check[(u_char) *old] = 1;
|
2017-12-12 04:52:01 -08:00
|
|
|
(*transtab)[(u_char) *old++] = *new++;
|
2017-12-11 05:25:57 -08:00
|
|
|
}
|
|
|
|
if (*old != '\0' || *new != '\0')
|
2024-07-17 13:57:15 -07:00
|
|
|
error("transform strings are not the same length");
|
2017-12-11 05:25:57 -08:00
|
|
|
return end + 1;
|
1995-10-18 01:37:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1998-02-15 02:38:35 -08:00
|
|
|
* Compile the text following an a, c, or i command.
|
1995-10-18 01:37:01 -07:00
|
|
|
*/
|
|
|
|
static char *
|
2003-06-10 15:20:44 -07:00
|
|
|
compile_text(void)
|
1995-10-18 01:37:01 -07:00
|
|
|
{
|
2017-12-13 08:06:34 -08:00
|
|
|
size_t asize, size, bufsize;
|
2008-10-08 10:26:47 -07:00
|
|
|
char *lbuf, *text, *p, *op, *s;
|
2017-12-13 08:06:34 -08:00
|
|
|
int esc_nl;
|
2008-10-16 09:34:32 -07:00
|
|
|
|
|
|
|
lbuf = text = NULL;
|
|
|
|
asize = size = 0;
|
2024-06-17 17:32:22 -07:00
|
|
|
while ((p = cu_getline(&lbuf, &bufsize))) {
|
2008-10-16 09:34:32 -07:00
|
|
|
size_t len = ROUNDLEN(strlen(p) + 1);
|
|
|
|
if (asize - size < len) {
|
2008-10-08 10:26:47 -07:00
|
|
|
do {
|
2008-10-16 09:34:32 -07:00
|
|
|
asize += len;
|
|
|
|
} while (asize - size < len);
|
2008-10-08 10:26:47 -07:00
|
|
|
text = xrealloc(text, asize);
|
|
|
|
}
|
1995-10-18 01:37:01 -07:00
|
|
|
op = s = text + size;
|
1997-11-13 19:59:25 -08:00
|
|
|
for (esc_nl = 0; *p != '\0'; p++) {
|
|
|
|
if (*p == '\\' && p[1] != '\0' && *++p == '\n')
|
|
|
|
esc_nl = 1;
|
1995-10-18 01:37:01 -07:00
|
|
|
*s++ = *p;
|
|
|
|
}
|
|
|
|
size += s - op;
|
1997-11-13 19:59:25 -08:00
|
|
|
if (!esc_nl) {
|
1995-10-18 01:37:01 -07:00
|
|
|
*s = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-10-08 10:26:47 -07:00
|
|
|
free(lbuf);
|
2010-11-15 12:26:00 -08:00
|
|
|
text = xrealloc(text, size + 1);
|
1998-09-22 14:21:43 -07:00
|
|
|
text[size] = '\0';
|
2010-11-15 12:26:00 -08:00
|
|
|
return (text);
|
1995-10-18 01:37:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get an address and return a pointer to the first character after
|
|
|
|
* it. Fill the structure pointed to according to the address.
|
|
|
|
*/
|
|
|
|
static char *
|
2003-06-10 15:20:44 -07:00
|
|
|
compile_addr(char *p, struct s_addr *a)
|
1995-10-18 01:37:01 -07:00
|
|
|
{
|
|
|
|
char *end;
|
|
|
|
|
|
|
|
switch (*p) {
|
|
|
|
case '\\': /* Context address */
|
|
|
|
++p;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case '/': /* Context address */
|
|
|
|
p = compile_re(p, &a->u.r);
|
|
|
|
if (p == NULL)
|
2024-07-17 13:57:15 -07:00
|
|
|
error("unterminated regular expression");
|
1995-10-18 01:37:01 -07:00
|
|
|
a->type = AT_RE;
|
|
|
|
return (p);
|
|
|
|
|
|
|
|
case '$': /* Last line */
|
|
|
|
a->type = AT_LAST;
|
|
|
|
return (p + 1);
|
|
|
|
/* Line number */
|
2005-04-11 00:11:44 -07:00
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
1995-10-18 01:37:01 -07:00
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
|
|
|
a->type = AT_LINE;
|
1997-04-28 13:23:19 -07:00
|
|
|
a->u.l = strtoul(p, &end, 10);
|
1995-10-18 01:37:01 -07:00
|
|
|
return (end);
|
|
|
|
default:
|
2024-07-17 13:57:15 -07:00
|
|
|
error("expected context address");
|
1995-10-18 01:37:01 -07:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* duptoeol --
|
|
|
|
* Return a copy of all the characters up to \n or \0.
|
|
|
|
*/
|
|
|
|
static char *
|
2003-10-07 10:56:26 -07:00
|
|
|
duptoeol(char *s, char *ctype, char **semi)
|
1995-10-18 01:37:01 -07:00
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
int ws;
|
|
|
|
char *start;
|
|
|
|
|
|
|
|
ws = 0;
|
2003-10-07 10:56:26 -07:00
|
|
|
if (semi) {
|
|
|
|
for (start = s; *s != '\0' && *s != '\n' && *s != ';'; ++s)
|
2013-11-28 10:24:54 -08:00
|
|
|
ws = isspace((unsigned char)*s);
|
2003-10-07 10:56:26 -07:00
|
|
|
} else {
|
|
|
|
for (start = s; *s != '\0' && *s != '\n'; ++s)
|
2013-11-28 10:24:54 -08:00
|
|
|
ws = isspace((unsigned char)*s);
|
2003-10-07 10:56:26 -07:00
|
|
|
*s = '\0';
|
|
|
|
}
|
1995-10-18 01:37:01 -07:00
|
|
|
if (ws)
|
2017-01-20 02:26:16 -08:00
|
|
|
warning("whitespace after %s", ctype);
|
1995-10-18 01:37:01 -07:00
|
|
|
len = s - start + 1;
|
2003-10-07 10:56:26 -07:00
|
|
|
if (semi)
|
|
|
|
*semi = s;
|
|
|
|
s = xmalloc(len);
|
|
|
|
strlcpy(s, start, len);
|
|
|
|
return (s);
|
1995-10-18 01:37:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert goto label names to addresses, and count a and r commands, in
|
|
|
|
* the given subset of the script. Free the memory used by labels in b
|
|
|
|
* and t commands (but not by :).
|
|
|
|
*
|
|
|
|
* TODO: Remove } nodes
|
|
|
|
*/
|
|
|
|
static void
|
2003-06-10 15:20:44 -07:00
|
|
|
fixuplabel(struct s_command *cp, struct s_command *end)
|
1995-10-18 01:37:01 -07:00
|
|
|
{
|
|
|
|
|
|
|
|
for (; cp != end; cp = cp->next)
|
|
|
|
switch (cp->code) {
|
|
|
|
case 'a':
|
|
|
|
case 'r':
|
|
|
|
appendnum++;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
case 't':
|
|
|
|
/* Resolve branch target. */
|
|
|
|
if (cp->t == NULL) {
|
|
|
|
cp->u.c = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((cp->u.c = findlabel(cp->t)) == NULL)
|
2024-07-17 13:57:15 -07:00
|
|
|
error("undefined label '%s'", cp->t);
|
1995-10-18 01:37:01 -07:00
|
|
|
free(cp->t);
|
|
|
|
break;
|
|
|
|
case '{':
|
|
|
|
/* Do interior commands. */
|
|
|
|
fixuplabel(cp->u.c, cp->next);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Associate the given command label for later lookup.
|
|
|
|
*/
|
|
|
|
static void
|
2003-06-10 15:20:44 -07:00
|
|
|
enterlabel(struct s_command *cp)
|
1995-10-18 01:37:01 -07:00
|
|
|
{
|
2001-11-19 11:02:13 -08:00
|
|
|
struct labhash **lhp, *lh;
|
|
|
|
u_char *p;
|
|
|
|
u_int h, c;
|
1995-10-18 01:37:01 -07:00
|
|
|
|
|
|
|
for (h = 0, p = (u_char *)cp->t; (c = *p) != 0; p++)
|
|
|
|
h = (h << 5) + h + c;
|
|
|
|
lhp = &labels[h & LHMASK];
|
|
|
|
for (lh = *lhp; lh != NULL; lh = lh->lh_next)
|
|
|
|
if (lh->lh_hash == h && strcmp(cp->t, lh->lh_cmd->t) == 0)
|
2024-07-17 13:57:15 -07:00
|
|
|
error("duplicate label '%s'", cp->t);
|
1995-10-18 01:37:01 -07:00
|
|
|
lh = xmalloc(sizeof *lh);
|
|
|
|
lh->lh_next = *lhp;
|
|
|
|
lh->lh_hash = h;
|
|
|
|
lh->lh_cmd = cp;
|
|
|
|
lh->lh_ref = 0;
|
|
|
|
*lhp = lh;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the label contained in the command l in the command linked
|
|
|
|
* list cp. L is excluded from the search. Return NULL if not found.
|
|
|
|
*/
|
|
|
|
static struct s_command *
|
2003-06-10 15:20:44 -07:00
|
|
|
findlabel(char *name)
|
1995-10-18 01:37:01 -07:00
|
|
|
{
|
2001-11-19 11:02:13 -08:00
|
|
|
struct labhash *lh;
|
|
|
|
u_char *p;
|
|
|
|
u_int h, c;
|
1995-10-18 01:37:01 -07:00
|
|
|
|
|
|
|
for (h = 0, p = (u_char *)name; (c = *p) != 0; p++)
|
|
|
|
h = (h << 5) + h + c;
|
|
|
|
for (lh = labels[h & LHMASK]; lh != NULL; lh = lh->lh_next) {
|
|
|
|
if (lh->lh_hash == h && strcmp(name, lh->lh_cmd->t) == 0) {
|
|
|
|
lh->lh_ref = 1;
|
|
|
|
return (lh->lh_cmd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2005-04-11 00:11:44 -07:00
|
|
|
/*
|
1995-10-18 01:37:01 -07:00
|
|
|
* Warn about any unused labels. As a side effect, release the label hash
|
|
|
|
* table space.
|
|
|
|
*/
|
|
|
|
static void
|
2003-06-10 15:20:44 -07:00
|
|
|
uselabel(void)
|
1995-10-18 01:37:01 -07:00
|
|
|
{
|
2001-11-19 11:02:13 -08:00
|
|
|
struct labhash *lh, *next;
|
|
|
|
int i;
|
1995-10-18 01:37:01 -07:00
|
|
|
|
|
|
|
for (i = 0; i < LHSZ; i++) {
|
|
|
|
for (lh = labels[i]; lh != NULL; lh = next) {
|
|
|
|
next = lh->lh_next;
|
|
|
|
if (!lh->lh_ref)
|
2017-01-20 02:26:16 -08:00
|
|
|
warning("unused label '%s'",
|
1995-10-18 01:37:01 -07:00
|
|
|
lh->lh_cmd->t);
|
|
|
|
free(lh);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|