2023-01-20 04:26:02 +03:00
|
|
|
/* $NetBSD: compile.c,v 1.51 2023/01/20 01:26:02 msaitoh Exp $ */
|
1997-01-09 23:18:21 +03:00
|
|
|
|
1993-04-14 03:49:12 +04:00
|
|
|
/*-
|
2014-06-06 04:13:13 +04:00
|
|
|
* Copyright (c) 1992 Diomidis Spinellis.
|
1994-02-04 02:32:10 +03:00
|
|
|
* Copyright (c) 1992, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-04-14 03:49:12 +04:00
|
|
|
*
|
|
|
|
* 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-08-07 15:13:06 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2006-06-18 09:16:41 +04:00
|
|
|
#if HAVE_NBTOOL_CONFIG_H
|
|
|
|
#include "nbtool_config.h"
|
|
|
|
#endif
|
|
|
|
|
1997-10-20 03:05:11 +04:00
|
|
|
#include <sys/cdefs.h>
|
2023-01-20 04:26:02 +03:00
|
|
|
__RCSID("$NetBSD: compile.c,v 1.51 2023/01/20 01:26:02 msaitoh Exp $");
|
2014-06-06 04:13:13 +04:00
|
|
|
#ifdef __FBSDID
|
|
|
|
__FBSDID("$FreeBSD: head/usr.bin/sed/compile.c 259132 2013-12-09 18:57:20Z eadler $");
|
|
|
|
#endif
|
|
|
|
|
2014-06-26 06:14:32 +04:00
|
|
|
#if 0
|
|
|
|
static const char sccsid[] = "@(#)compile.c 8.1 (Berkeley) 6/6/93";
|
|
|
|
#endif
|
|
|
|
|
1993-04-14 03:49:12 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
2014-06-06 04:13:13 +04:00
|
|
|
#include <err.h>
|
1993-04-14 03:49:12 +04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <regex.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-06-06 04:13:13 +04:00
|
|
|
#include <wchar.h>
|
1993-04-14 03:49:12 +04:00
|
|
|
|
|
|
|
#include "defs.h"
|
|
|
|
#include "extern.h"
|
|
|
|
|
1994-02-04 02:32:10 +03:00
|
|
|
#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-06-14 04:41:42 +04:00
|
|
|
static char *compile_addr(char *, struct s_addr *);
|
|
|
|
static char *compile_ccl(char **, char *);
|
2014-06-06 04:13:13 +04:00
|
|
|
static char *compile_delimited(char *, char *, int);
|
2002-06-14 04:41:42 +04:00
|
|
|
static char *compile_flags(char *, struct s_subst *);
|
2014-06-06 04:13:13 +04:00
|
|
|
static regex_t *compile_re(char *, int);
|
2002-06-14 04:41:42 +04:00
|
|
|
static char *compile_subst(char *, struct s_subst *);
|
|
|
|
static char *compile_text(void);
|
2014-06-06 04:13:13 +04:00
|
|
|
static char *compile_tr(char *, struct s_tr **);
|
1993-04-14 03:49:12 +04:00
|
|
|
static struct s_command
|
2002-06-14 04:41:42 +04:00
|
|
|
**compile_stream(struct s_command **);
|
2009-04-13 11:29:55 +04:00
|
|
|
static char *duptoeol(char *, const char *);
|
2002-06-14 04:41:42 +04:00
|
|
|
static void enterlabel(struct s_command *);
|
1993-04-14 03:49:12 +04:00
|
|
|
static struct s_command
|
2002-06-14 04:41:42 +04:00
|
|
|
*findlabel(char *);
|
|
|
|
static void fixuplabel(struct s_command *, struct s_command *);
|
|
|
|
static void uselabel(void);
|
2019-10-05 23:23:55 +03:00
|
|
|
static void parse_escapes(char *);
|
1993-04-14 03:49:12 +04: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},
|
1995-03-09 14:19:24 +03:00
|
|
|
{'}', 0, ENDGROUP},
|
1993-04-14 03:49:12 +04:00
|
|
|
{'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
|
2002-06-14 04:41:42 +04:00
|
|
|
compile(void)
|
1993-04-14 03:49:12 +04:00
|
|
|
{
|
1995-03-09 14:19:24 +03:00
|
|
|
*compile_stream(&prog) = NULL;
|
1994-02-04 02:32:10 +03:00
|
|
|
fixuplabel(prog, NULL);
|
|
|
|
uselabel();
|
2015-03-12 15:40:41 +03:00
|
|
|
if (appendnum > 0)
|
|
|
|
appends = xmalloc(sizeof(struct s_appends) * appendnum);
|
|
|
|
match = xmalloc((maxnsub + 1) * sizeof(regmatch_t));
|
1993-04-14 03:49:12 +04:00
|
|
|
}
|
|
|
|
|
2014-06-06 04:13:13 +04:00
|
|
|
#define EATSPACE() do { \
|
|
|
|
if (p) \
|
|
|
|
while (*p && isspace((unsigned char)*p)) \
|
|
|
|
p++; \
|
|
|
|
} while (0)
|
1993-04-14 03:49:12 +04:00
|
|
|
|
|
|
|
static struct s_command **
|
2002-06-14 04:41:42 +04:00
|
|
|
compile_stream(struct s_command **link)
|
1993-04-14 03:49:12 +04:00
|
|
|
{
|
1997-10-20 03:05:11 +04:00
|
|
|
char *p;
|
2014-06-06 04:13:13 +04:00
|
|
|
static char lbuf[_POSIX2_LINE_MAX + 1]; /* To save stack */
|
1995-03-09 14:19:24 +03:00
|
|
|
struct s_command *cmd, *cmd2, *stack;
|
1993-04-14 03:49:12 +04:00
|
|
|
struct s_format *fp;
|
2014-06-06 04:13:13 +04:00
|
|
|
char re[_POSIX2_LINE_MAX + 1];
|
1993-04-14 03:49:12 +04:00
|
|
|
int naddr; /* Number of addresses */
|
|
|
|
|
1995-03-09 14:19:24 +03:00
|
|
|
stack = 0;
|
1993-04-14 03:49:12 +04:00
|
|
|
for (;;) {
|
2014-06-06 04:13:13 +04:00
|
|
|
if ((p = cu_fgets(lbuf, sizeof(lbuf), NULL)) == NULL) {
|
1995-03-09 14:19:24 +03:00
|
|
|
if (stack != 0)
|
2014-06-06 04:13:13 +04:00
|
|
|
errx(1, "%lu: %s: unexpected EOF (pending }'s)",
|
|
|
|
linenum, fname);
|
1993-04-14 03:49:12 +04:00
|
|
|
return (link);
|
|
|
|
}
|
|
|
|
|
|
|
|
semicolon: EATSPACE();
|
2014-06-06 04:13:13 +04:00
|
|
|
if (p) {
|
|
|
|
if (*p == '#' || *p == '\0')
|
|
|
|
continue;
|
|
|
|
else if (*p == ';') {
|
|
|
|
p++;
|
|
|
|
goto semicolon;
|
|
|
|
}
|
2002-01-14 22:37:30 +03:00
|
|
|
}
|
1993-04-14 03:49:12 +04:00
|
|
|
*link = cmd = xmalloc(sizeof(struct s_command));
|
|
|
|
link = &cmd->next;
|
2014-06-06 04:13:13 +04:00
|
|
|
cmd->startline = cmd->nonsel = 0;
|
1993-04-14 03:49:12 +04:00
|
|
|
/* 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 */
|
1994-11-15 22:03:25 +03:00
|
|
|
naddr++;
|
1993-04-14 03:49:12 +04:00
|
|
|
cmd->a2 = xmalloc(sizeof(struct s_addr));
|
|
|
|
p = compile_addr(p, cmd->a2);
|
1994-11-15 22:03:25 +03:00
|
|
|
EATSPACE();
|
1994-11-09 22:30:32 +03:00
|
|
|
} else
|
|
|
|
cmd->a2 = 0;
|
|
|
|
} else
|
1994-11-15 22:03:25 +03:00
|
|
|
cmd->a1 = cmd->a2 = 0;
|
1993-04-14 03:49:12 +04:00
|
|
|
|
|
|
|
nonsel: /* Now parse the command */
|
|
|
|
if (!*p)
|
2014-06-06 04:13:13 +04:00
|
|
|
errx(1, "%lu: %s: command expected", linenum, fname);
|
1993-04-14 03:49:12 +04:00
|
|
|
cmd->code = *p;
|
|
|
|
for (fp = cmd_fmts; fp->code; fp++)
|
|
|
|
if (fp->code == *p)
|
|
|
|
break;
|
|
|
|
if (!fp->code)
|
2014-06-06 04:13:13 +04:00
|
|
|
errx(1, "%lu: %s: invalid command code %c", linenum, fname, *p);
|
1993-04-14 03:49:12 +04:00
|
|
|
if (naddr > fp->naddr)
|
2014-06-06 04:13:13 +04:00
|
|
|
errx(1,
|
|
|
|
"%lu: %s: command %c expects up to %d address(es), found %d",
|
|
|
|
linenum, fname, *p, fp->naddr, naddr);
|
1993-04-14 03:49:12 +04:00
|
|
|
switch (fp->args) {
|
|
|
|
case NONSEL: /* ! */
|
|
|
|
p++;
|
1994-11-15 22:03:25 +03:00
|
|
|
EATSPACE();
|
|
|
|
cmd->nonsel = ! cmd->nonsel;
|
1993-04-14 03:49:12 +04:00
|
|
|
goto nonsel;
|
|
|
|
case GROUP: /* { */
|
|
|
|
p++;
|
|
|
|
EATSPACE();
|
1995-03-09 14:19:24 +03:00
|
|
|
cmd->next = stack;
|
|
|
|
stack = cmd;
|
|
|
|
link = &cmd->u.c;
|
|
|
|
if (*p)
|
|
|
|
goto semicolon;
|
|
|
|
break;
|
|
|
|
case ENDGROUP:
|
1994-11-09 22:30:32 +03:00
|
|
|
/*
|
|
|
|
* Short-circuit command processing, since end of
|
|
|
|
* group is really just a noop.
|
|
|
|
*/
|
1995-03-09 14:19:24 +03:00
|
|
|
cmd->nonsel = 1;
|
|
|
|
if (stack == 0)
|
2014-06-06 04:13:13 +04:00
|
|
|
errx(1, "%lu: %s: unexpected }", linenum, fname);
|
1995-03-09 14:19:24 +03:00
|
|
|
cmd2 = stack;
|
|
|
|
stack = cmd2->next;
|
|
|
|
cmd2->next = cmd;
|
|
|
|
/*FALLTHROUGH*/
|
1993-04-14 03:49:12 +04:00
|
|
|
case EMPTY: /* d D g G h H l n N p P q x = \0 */
|
|
|
|
p++;
|
|
|
|
EATSPACE();
|
2014-05-05 21:12:11 +04:00
|
|
|
switch (*p) {
|
|
|
|
case ';':
|
1993-04-14 03:49:12 +04:00
|
|
|
p++;
|
|
|
|
link = &cmd->next;
|
|
|
|
goto semicolon;
|
2014-05-05 21:12:11 +04:00
|
|
|
case '}':
|
|
|
|
goto semicolon;
|
|
|
|
case '\0':
|
|
|
|
break;
|
|
|
|
default:
|
2014-06-06 04:13:13 +04:00
|
|
|
errx(1, "%lu: %s: extra characters at the end of %c command",
|
|
|
|
linenum, fname, cmd->code);
|
2014-05-05 21:12:11 +04:00
|
|
|
}
|
1993-04-14 03:49:12 +04:00
|
|
|
break;
|
|
|
|
case TEXT: /* a c i */
|
|
|
|
p++;
|
|
|
|
EATSPACE();
|
|
|
|
if (*p != '\\')
|
2014-06-06 04:13:13 +04:00
|
|
|
errx(1,
|
|
|
|
"%lu: %s: command %c expects \\ followed by text", linenum, fname, cmd->code);
|
1993-04-14 03:49:12 +04:00
|
|
|
p++;
|
|
|
|
EATSPACE();
|
|
|
|
if (*p)
|
2014-06-06 04:13:13 +04:00
|
|
|
errx(1,
|
|
|
|
"%lu: %s: extra characters after \\ at the end of %c command",
|
|
|
|
linenum, fname, cmd->code);
|
1993-04-14 03:49:12 +04:00
|
|
|
cmd->t = compile_text();
|
|
|
|
break;
|
|
|
|
case COMMENT: /* \0 # */
|
|
|
|
break;
|
|
|
|
case WFILE: /* w */
|
|
|
|
p++;
|
|
|
|
EATSPACE();
|
|
|
|
if (*p == '\0')
|
2014-06-06 04:13:13 +04:00
|
|
|
errx(1, "%lu: %s: filename expected", linenum, fname);
|
1994-02-04 02:32:10 +03:00
|
|
|
cmd->t = duptoeol(p, "w command");
|
1993-04-14 03:49:12 +04:00
|
|
|
if (aflag)
|
|
|
|
cmd->u.fd = -1;
|
2014-06-06 04:13:13 +04:00
|
|
|
else if ((cmd->u.fd = open(p,
|
1993-04-14 03:49:12 +04:00
|
|
|
O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
|
|
|
|
DEFFILEMODE)) == -1)
|
2014-06-06 04:13:13 +04:00
|
|
|
err(1, "%s", p);
|
1993-04-14 03:49:12 +04:00
|
|
|
break;
|
|
|
|
case RFILE: /* r */
|
|
|
|
p++;
|
|
|
|
EATSPACE();
|
|
|
|
if (*p == '\0')
|
2014-06-06 04:13:13 +04:00
|
|
|
errx(1, "%lu: %s: filename expected", linenum, fname);
|
1993-04-14 03:49:12 +04:00
|
|
|
else
|
1994-02-04 02:32:10 +03:00
|
|
|
cmd->t = duptoeol(p, "read command");
|
1993-04-14 03:49:12 +04:00
|
|
|
break;
|
|
|
|
case BRANCH: /* b t */
|
|
|
|
p++;
|
|
|
|
EATSPACE();
|
|
|
|
if (*p == '\0')
|
|
|
|
cmd->t = NULL;
|
|
|
|
else
|
1994-02-04 02:32:10 +03:00
|
|
|
cmd->t = duptoeol(p, "branch");
|
1993-04-14 03:49:12 +04:00
|
|
|
break;
|
|
|
|
case LABEL: /* : */
|
|
|
|
p++;
|
|
|
|
EATSPACE();
|
1994-02-04 02:32:10 +03:00
|
|
|
cmd->t = duptoeol(p, "label");
|
1993-04-14 03:49:12 +04:00
|
|
|
if (strlen(p) == 0)
|
2014-06-06 04:13:13 +04:00
|
|
|
errx(1, "%lu: %s: empty label", linenum, fname);
|
1994-02-04 02:32:10 +03:00
|
|
|
enterlabel(cmd);
|
1993-04-14 03:49:12 +04:00
|
|
|
break;
|
|
|
|
case SUBST: /* s */
|
|
|
|
p++;
|
|
|
|
if (*p == '\0' || *p == '\\')
|
2014-06-06 04:13:13 +04:00
|
|
|
errx(1,
|
|
|
|
"%lu: %s: substitute pattern can not be delimited by newline or backslash",
|
|
|
|
linenum, fname);
|
|
|
|
cmd->u.s = xcalloc(1, sizeof(struct s_subst));
|
|
|
|
p = compile_delimited(p, re, 0);
|
1993-04-14 03:49:12 +04:00
|
|
|
if (p == NULL)
|
2014-06-06 04:13:13 +04:00
|
|
|
errx(1,
|
|
|
|
"%lu: %s: unterminated substitute pattern", linenum, fname);
|
|
|
|
|
|
|
|
/* Compile RE with no case sensitivity temporarily */
|
|
|
|
if (*re == '\0')
|
|
|
|
cmd->u.s->re = NULL;
|
|
|
|
else
|
|
|
|
cmd->u.s->re = compile_re(re, 0);
|
1993-04-14 03:49:12 +04:00
|
|
|
--p;
|
|
|
|
p = compile_subst(p, cmd->u.s);
|
|
|
|
p = compile_flags(p, cmd->u.s);
|
2014-06-06 04:13:13 +04:00
|
|
|
|
|
|
|
/* Recompile RE with case sensitivity from "I" flag if any */
|
|
|
|
if (*re == '\0')
|
|
|
|
cmd->u.s->re = NULL;
|
|
|
|
else
|
|
|
|
cmd->u.s->re = compile_re(re, cmd->u.s->icase);
|
1993-04-14 03:49:12 +04:00
|
|
|
EATSPACE();
|
|
|
|
if (*p == ';') {
|
|
|
|
p++;
|
|
|
|
link = &cmd->next;
|
|
|
|
goto semicolon;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TR: /* y */
|
|
|
|
p++;
|
2014-06-06 04:13:13 +04:00
|
|
|
p = compile_tr(p, &cmd->u.y);
|
1993-04-14 03:49:12 +04:00
|
|
|
EATSPACE();
|
2014-05-05 21:12:11 +04:00
|
|
|
switch (*p) {
|
|
|
|
case ';':
|
1993-04-14 03:49:12 +04:00
|
|
|
p++;
|
|
|
|
link = &cmd->next;
|
|
|
|
goto semicolon;
|
2014-05-05 21:12:11 +04:00
|
|
|
case '}':
|
|
|
|
goto semicolon;
|
|
|
|
case '\0':
|
|
|
|
break;
|
|
|
|
default:
|
2014-06-06 04:13:13 +04:00
|
|
|
errx(1,
|
|
|
|
"%lu: %s: extra text at the end of a transform command", linenum, fname);
|
2014-05-05 21:12:11 +04:00
|
|
|
}
|
2014-06-06 04:13:13 +04:00
|
|
|
if (*p)
|
1993-04-14 03:49:12 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-01-20 04:26:02 +03:00
|
|
|
* Get a delimited string. P points to the delimiter of the string; d points
|
1993-04-14 03:49:12 +04: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 *
|
2014-06-06 04:13:13 +04:00
|
|
|
compile_delimited(char *p, char *d, int is_tr)
|
1993-04-14 03:49:12 +04:00
|
|
|
{
|
|
|
|
char c;
|
|
|
|
|
|
|
|
c = *p++;
|
|
|
|
if (c == '\0')
|
|
|
|
return (NULL);
|
|
|
|
else if (c == '\\')
|
2014-06-06 04:13:13 +04:00
|
|
|
errx(1, "%lu: %s: \\ can not be used as a string delimiter",
|
|
|
|
linenum, fname);
|
1993-04-14 03:49:12 +04:00
|
|
|
else if (c == '\n')
|
2014-06-06 04:13:13 +04:00
|
|
|
errx(1, "%lu: %s: newline can not be used as a string delimiter",
|
|
|
|
linenum, fname);
|
1993-04-14 03:49:12 +04:00
|
|
|
while (*p) {
|
2014-06-06 04:13:13 +04:00
|
|
|
if (*p == '[' && *p != c) {
|
1994-04-17 08:19:27 +04:00
|
|
|
if ((d = compile_ccl(&p, d)) == NULL)
|
2014-06-06 04:13:13 +04:00
|
|
|
errx(1, "%lu: %s: unbalanced brackets ([])", linenum, fname);
|
1994-04-17 08:19:27 +04:00
|
|
|
continue;
|
|
|
|
} else if (*p == '\\' && p[1] == '[') {
|
|
|
|
*d++ = *p++;
|
|
|
|
} else if (*p == '\\' && p[1] == c)
|
1993-04-14 03:49:12 +04:00
|
|
|
p++;
|
|
|
|
else if (*p == '\\' && p[1] == 'n') {
|
|
|
|
*d++ = '\n';
|
|
|
|
p += 2;
|
|
|
|
continue;
|
2014-06-06 04:13:13 +04:00
|
|
|
} else if (*p == '\\' && p[1] == '\\') {
|
|
|
|
if (is_tr)
|
|
|
|
p++;
|
|
|
|
else
|
|
|
|
*d++ = *p++;
|
|
|
|
} else if (*p == c) {
|
1993-04-14 03:49:12 +04:00
|
|
|
*d = '\0';
|
|
|
|
return (p + 1);
|
|
|
|
}
|
|
|
|
*d++ = *p++;
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
1994-04-17 08:19:27 +04:00
|
|
|
|
|
|
|
/* compile_ccl: expand a POSIX character class */
|
|
|
|
static char *
|
2002-06-14 04:41:42 +04:00
|
|
|
compile_ccl(char **sp, char *t)
|
1994-04-17 08:19:27 +04: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;
|
2014-06-06 04:13:13 +04:00
|
|
|
}
|
1994-04-17 08:19:27 +04:00
|
|
|
return (*s == ']') ? *sp = ++s, ++t : NULL;
|
|
|
|
}
|
|
|
|
|
1993-04-14 03:49:12 +04:00
|
|
|
/*
|
2014-06-06 04:13:13 +04:00
|
|
|
* Compiles the regular expression in RE and returns a pointer to the compiled
|
|
|
|
* regular expression.
|
1993-04-14 03:49:12 +04:00
|
|
|
* Cflags are passed to regcomp.
|
|
|
|
*/
|
2014-06-06 04:13:13 +04:00
|
|
|
static regex_t *
|
|
|
|
compile_re(char *re, int case_insensitive)
|
1993-04-14 03:49:12 +04:00
|
|
|
{
|
2014-06-06 04:13:13 +04:00
|
|
|
regex_t *rep;
|
|
|
|
int eval, flags;
|
|
|
|
|
|
|
|
|
|
|
|
flags = rflags;
|
|
|
|
if (case_insensitive)
|
|
|
|
flags |= REG_ICASE;
|
|
|
|
rep = xmalloc(sizeof(regex_t));
|
2019-10-05 23:23:55 +03:00
|
|
|
parse_escapes(re);
|
2014-06-06 04:13:13 +04:00
|
|
|
if ((eval = regcomp(rep, re, flags)) != 0)
|
|
|
|
errx(1, "%lu: %s: RE error: %s",
|
|
|
|
linenum, fname, strregerror(eval, rep));
|
|
|
|
if (maxnsub < rep->re_nsub)
|
|
|
|
maxnsub = rep->re_nsub;
|
|
|
|
return (rep);
|
1993-04-14 03:49:12 +04:00
|
|
|
}
|
|
|
|
|
2019-10-05 23:23:55 +03:00
|
|
|
static char
|
|
|
|
cton(char c, int base)
|
|
|
|
{
|
|
|
|
switch (c) {
|
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7':
|
|
|
|
return (char)(c - '0');
|
|
|
|
case '8': case '9':
|
|
|
|
return base == 8 ? '?' : (char)(c - '0');
|
|
|
|
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
|
|
|
|
return base == 16 ? (char)(c - 'a' + 10) : '?';
|
|
|
|
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
|
|
|
|
return base == 16 ? (char)(c - 'A' + 10) : '?';
|
|
|
|
default:
|
|
|
|
return '?';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ston(char **pp, char *sp, int base)
|
|
|
|
{
|
|
|
|
char *p = *pp, n;
|
|
|
|
int r = cton(p[1], base);
|
|
|
|
|
|
|
|
if (r == '?')
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
p++;
|
|
|
|
while ((n = cton(p[1], base)) != '?' && r < 255) {
|
|
|
|
r = r * base + n;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
*sp = (char)r;
|
|
|
|
*pp = p;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
unescape(char **pp, char **spp)
|
|
|
|
{
|
|
|
|
char *p = *pp;
|
|
|
|
char *sp = *spp;
|
|
|
|
|
|
|
|
switch (*p) {
|
|
|
|
case 'o':
|
|
|
|
if (!ston(&p, sp, 8))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
if (!ston(&p, sp, 10))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
if (!ston(&p, sp, 16))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
*sp = '\a';
|
|
|
|
break;
|
|
|
|
#if 0
|
|
|
|
// No, \b RE
|
|
|
|
case 'b':
|
|
|
|
*sp = '\b';
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case 'f':
|
|
|
|
*sp = '\f';
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
*sp = '\n';
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
*sp = '\r';
|
|
|
|
break;
|
2021-03-13 18:46:54 +03:00
|
|
|
case 't':
|
|
|
|
*sp = '\t';
|
|
|
|
break;
|
2019-10-05 23:23:55 +03:00
|
|
|
case 'v':
|
|
|
|
*sp = '\v';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*spp = sp + 1;
|
|
|
|
*pp = p;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
parse_escapes(char *buf)
|
|
|
|
{
|
|
|
|
char bracket = '\0';
|
|
|
|
char *p, *q;
|
|
|
|
|
|
|
|
p = q = buf;
|
|
|
|
|
|
|
|
for (p = q = buf; *p; p++) {
|
|
|
|
if (*p == '\\' && p[1] && !bracket) {
|
|
|
|
p++;
|
|
|
|
if (unescape(&p, &q))
|
|
|
|
continue;
|
|
|
|
*q++ = '\\';
|
|
|
|
}
|
|
|
|
switch (*p) {
|
|
|
|
case '[':
|
|
|
|
if (!bracket)
|
|
|
|
bracket = *p;
|
|
|
|
break;
|
|
|
|
case '.':
|
|
|
|
case ':':
|
|
|
|
case '=':
|
|
|
|
if (bracket == '[' && p[-1] == '[')
|
|
|
|
bracket = *p;
|
|
|
|
break;
|
|
|
|
case ']':
|
|
|
|
if (!bracket)
|
|
|
|
break;
|
|
|
|
if (bracket == '[')
|
|
|
|
bracket = '\0';
|
|
|
|
else if (p[-2] != bracket && p[-1] == bracket)
|
|
|
|
bracket = '[';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*q++ = *p;
|
|
|
|
}
|
|
|
|
*q = '\0';
|
|
|
|
}
|
|
|
|
|
1993-04-14 03:49:12 +04:00
|
|
|
/*
|
|
|
|
* 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 *
|
2002-06-14 04:41:42 +04:00
|
|
|
compile_subst(char *p, struct s_subst *s)
|
1993-04-14 03:49:12 +04:00
|
|
|
{
|
2014-06-06 04:13:13 +04:00
|
|
|
static char lbuf[_POSIX2_LINE_MAX + 1];
|
|
|
|
size_t asize, size;
|
|
|
|
u_char ref;
|
1993-04-14 03:49:12 +04:00
|
|
|
char c, *text, *op, *sp;
|
2014-06-06 04:13:13 +04:00
|
|
|
int more = 1, sawesc = 0;
|
1993-04-14 03:49:12 +04:00
|
|
|
|
|
|
|
c = *p++; /* Terminator character */
|
|
|
|
if (c == '\0')
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
s->maxbref = 0;
|
|
|
|
s->linenum = linenum;
|
2014-06-06 04:13:13 +04:00
|
|
|
asize = 2 * _POSIX2_LINE_MAX + 1;
|
|
|
|
text = xmalloc(asize);
|
|
|
|
size = 0;
|
1993-04-14 03:49:12 +04:00
|
|
|
do {
|
|
|
|
op = sp = text + size;
|
|
|
|
for (; *p; p++) {
|
2004-06-12 08:29:22 +04: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++;
|
|
|
|
|
2019-10-05 23:23:55 +03:00
|
|
|
switch (*p) {
|
|
|
|
case '\0':
|
2004-06-12 08:29:22 +04:00
|
|
|
/*
|
|
|
|
* 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;
|
2019-10-05 23:23:55 +03:00
|
|
|
case '0': case '1': case '2': case '3':
|
|
|
|
case '4': case '5': case '6': case '7':
|
|
|
|
case '8': case '9':
|
1993-04-14 03:49:12 +04:00
|
|
|
*sp++ = '\\';
|
2014-06-06 04:13:13 +04:00
|
|
|
ref = (u_char)(*p - '0');
|
1993-04-14 03:49:12 +04:00
|
|
|
if (s->re != NULL &&
|
2014-06-06 04:13:13 +04:00
|
|
|
ref > s->re->re_nsub)
|
|
|
|
errx(1, "%lu: %s: \\%c not defined in the RE",
|
|
|
|
linenum, fname, *p);
|
1993-04-14 03:49:12 +04:00
|
|
|
if (s->maxbref < ref)
|
|
|
|
s->maxbref = ref;
|
2019-10-05 23:23:55 +03:00
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
case '\\':
|
1993-04-14 03:49:12 +04:00
|
|
|
*sp++ = '\\';
|
2019-10-05 23:23:55 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (unescape(&p, &sp))
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
1993-04-14 03:49:12 +04:00
|
|
|
} else if (*p == c) {
|
2014-06-06 04:13:13 +04:00
|
|
|
if (*++p == '\0' && more) {
|
|
|
|
if (cu_fgets(lbuf, sizeof(lbuf), &more))
|
|
|
|
p = lbuf;
|
|
|
|
}
|
1993-04-14 03:49:12 +04:00
|
|
|
*sp++ = '\0';
|
2014-06-06 04:13:13 +04:00
|
|
|
size += (size_t)(sp - op);
|
1993-04-14 03:49:12 +04:00
|
|
|
s->new = xrealloc(text, size);
|
|
|
|
return (p);
|
|
|
|
} else if (*p == '\n') {
|
2014-06-06 04:13:13 +04:00
|
|
|
errx(1,
|
|
|
|
"%lu: %s: unescaped newline inside substitute pattern", linenum, fname);
|
1993-04-14 03:49:12 +04:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
*sp++ = *p;
|
|
|
|
}
|
2014-06-06 04:13:13 +04:00
|
|
|
size += (size_t)(sp - op);
|
|
|
|
if (asize - size < _POSIX2_LINE_MAX + 1) {
|
|
|
|
asize *= 2;
|
|
|
|
text = xrealloc(text, asize);
|
|
|
|
}
|
|
|
|
} while (cu_fgets(p = lbuf, sizeof(lbuf), &more));
|
|
|
|
errx(1, "%lu: %s: unterminated substitute in regular expression",
|
|
|
|
linenum, fname);
|
1993-04-14 03:49:12 +04:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compile the flags of the s command
|
|
|
|
*/
|
|
|
|
static char *
|
2002-06-14 04:41:42 +04:00
|
|
|
compile_flags(char *p, struct s_subst *s)
|
1993-04-14 03:49:12 +04:00
|
|
|
{
|
|
|
|
int gn; /* True if we have seen g or n */
|
2014-06-06 04:13:13 +04:00
|
|
|
unsigned long nval;
|
|
|
|
char wfile[_POSIX2_LINE_MAX + 1], *q;
|
1993-04-14 03:49:12 +04:00
|
|
|
|
|
|
|
s->n = 1; /* Default */
|
|
|
|
s->p = 0;
|
|
|
|
s->wfile = NULL;
|
|
|
|
s->wfd = -1;
|
2014-06-06 04:13:13 +04:00
|
|
|
s->icase = 0;
|
1993-04-14 03:49:12 +04:00
|
|
|
for (gn = 0;;) {
|
|
|
|
EATSPACE(); /* EXTENSION */
|
|
|
|
switch (*p) {
|
|
|
|
case 'g':
|
|
|
|
if (gn)
|
2014-06-06 04:13:13 +04:00
|
|
|
errx(1,
|
|
|
|
"%lu: %s: more than one number or 'g' in substitute flags", linenum, fname);
|
1993-04-14 03:49:12 +04:00
|
|
|
gn = 1;
|
|
|
|
s->n = 0;
|
|
|
|
break;
|
|
|
|
case '\0':
|
|
|
|
case '\n':
|
|
|
|
case ';':
|
|
|
|
return (p);
|
|
|
|
case 'p':
|
|
|
|
s->p = 1;
|
|
|
|
break;
|
2014-06-06 04:13:13 +04:00
|
|
|
case 'i':
|
|
|
|
case 'I':
|
|
|
|
s->icase = 1;
|
|
|
|
break;
|
1993-04-14 03:49:12 +04:00
|
|
|
case '1': case '2': case '3':
|
|
|
|
case '4': case '5': case '6':
|
|
|
|
case '7': case '8': case '9':
|
|
|
|
if (gn)
|
2014-06-06 04:13:13 +04:00
|
|
|
errx(1,
|
|
|
|
"%lu: %s: more than one number or 'g' in substitute flags", linenum, fname);
|
1993-04-14 03:49:12 +04:00
|
|
|
gn = 1;
|
2014-06-06 04:13:13 +04:00
|
|
|
errno = 0;
|
|
|
|
nval = strtoul(p, &p, 10);
|
|
|
|
if (errno == ERANGE || nval > INT_MAX)
|
|
|
|
errx(1,
|
|
|
|
"%lu: %s: overflow in the 'N' substitute flag", linenum, fname);
|
|
|
|
s->n = (int)nval;
|
2004-06-13 12:31:30 +04:00
|
|
|
p--;
|
1993-04-14 03:49:12 +04:00
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
p++;
|
1994-02-04 02:32:10 +03:00
|
|
|
#ifdef HISTORIC_PRACTICE
|
1993-04-14 03:49:12 +04:00
|
|
|
if (*p != ' ') {
|
2014-06-06 04:13:13 +04:00
|
|
|
warnx("%lu: %s: space missing before w wfile", linenum, fname);
|
1993-04-14 03:49:12 +04:00
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
EATSPACE();
|
|
|
|
q = wfile;
|
|
|
|
while (*p) {
|
|
|
|
if (*p == '\n')
|
|
|
|
break;
|
|
|
|
*q++ = *p++;
|
|
|
|
}
|
|
|
|
*q = '\0';
|
|
|
|
if (q == wfile)
|
2014-06-06 04:13:13 +04:00
|
|
|
errx(1, "%lu: %s: no wfile specified", linenum, fname);
|
1993-04-14 03:49:12 +04:00
|
|
|
s->wfile = strdup(wfile);
|
|
|
|
if (!aflag && (s->wfd = open(wfile,
|
|
|
|
O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
|
|
|
|
DEFFILEMODE)) == -1)
|
2014-06-06 04:13:13 +04:00
|
|
|
err(1, "%s", wfile);
|
1993-04-14 03:49:12 +04:00
|
|
|
return (p);
|
|
|
|
default:
|
2014-06-06 04:13:13 +04:00
|
|
|
errx(1, "%lu: %s: bad flag in substitute command: '%c'",
|
|
|
|
linenum, fname, *p);
|
1993-04-14 03:49:12 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compile a translation set of strings into a lookup table.
|
|
|
|
*/
|
|
|
|
static char *
|
2014-06-06 04:13:13 +04:00
|
|
|
compile_tr(char *p, struct s_tr **py)
|
1993-04-14 03:49:12 +04:00
|
|
|
{
|
2014-06-06 04:13:13 +04:00
|
|
|
struct s_tr *y;
|
|
|
|
size_t i;
|
|
|
|
const char *op, *np;
|
|
|
|
char old[_POSIX2_LINE_MAX + 1];
|
|
|
|
char new[_POSIX2_LINE_MAX + 1];
|
|
|
|
size_t oclen, oldlen, nclen, newlen;
|
|
|
|
mbstate_t mbs1, mbs2;
|
|
|
|
|
|
|
|
*py = y = xmalloc(sizeof(*y));
|
|
|
|
y->multis = NULL;
|
|
|
|
y->nmultis = 0;
|
1993-04-14 03:49:12 +04:00
|
|
|
|
|
|
|
if (*p == '\0' || *p == '\\')
|
2014-06-06 04:13:13 +04:00
|
|
|
errx(1,
|
|
|
|
"%lu: %s: transform pattern can not be delimited by newline or backslash",
|
|
|
|
linenum, fname);
|
|
|
|
p = compile_delimited(p, old, 1);
|
|
|
|
if (p == NULL)
|
|
|
|
errx(1, "%lu: %s: unterminated transform source string",
|
|
|
|
linenum, fname);
|
|
|
|
p = compile_delimited(p - 1, new, 1);
|
|
|
|
if (p == NULL)
|
|
|
|
errx(1, "%lu: %s: unterminated transform target string",
|
|
|
|
linenum, fname);
|
1993-04-14 03:49:12 +04:00
|
|
|
EATSPACE();
|
2014-06-06 04:13:13 +04:00
|
|
|
op = old;
|
|
|
|
oldlen = mbsrtowcs(NULL, &op, 0, NULL);
|
|
|
|
if (oldlen == (size_t)-1)
|
|
|
|
err(1, NULL);
|
|
|
|
np = new;
|
|
|
|
newlen = mbsrtowcs(NULL, &np, 0, NULL);
|
|
|
|
if (newlen == (size_t)-1)
|
|
|
|
err(1, NULL);
|
|
|
|
if (newlen != oldlen)
|
|
|
|
errx(1, "%lu: %s: transform strings are not the same length",
|
|
|
|
linenum, fname);
|
|
|
|
if (MB_CUR_MAX == 1) {
|
|
|
|
/*
|
|
|
|
* The single-byte encoding case is easy: generate a
|
|
|
|
* lookup table.
|
|
|
|
*/
|
|
|
|
for (i = 0; i <= UCHAR_MAX; i++)
|
|
|
|
y->bytetab[i] = (u_char)i;
|
|
|
|
for (; *op; op++, np++)
|
|
|
|
y->bytetab[(u_char)*op] = (u_char)*np;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Multi-byte encoding case: generate a lookup table as
|
|
|
|
* above, but only for single-byte characters. The first
|
|
|
|
* bytes of multi-byte characters have their lookup table
|
|
|
|
* entries set to 0, which causes do_tr() to search through
|
|
|
|
* an auxiliary vector of multi-byte mappings.
|
|
|
|
*/
|
|
|
|
memset(&mbs1, 0, sizeof(mbs1));
|
|
|
|
memset(&mbs2, 0, sizeof(mbs2));
|
|
|
|
for (i = 0; i <= UCHAR_MAX; i++)
|
|
|
|
y->bytetab[i] = (u_char)((btowc((int)i) != WEOF) ? i : 0);
|
|
|
|
while (*op != '\0') {
|
|
|
|
oclen = mbrlen(op, MB_LEN_MAX, &mbs1);
|
|
|
|
if (oclen == (size_t)-1 || oclen == (size_t)-2)
|
|
|
|
errc(1, EILSEQ, NULL);
|
|
|
|
nclen = mbrlen(np, MB_LEN_MAX, &mbs2);
|
|
|
|
if (nclen == (size_t)-1 || nclen == (size_t)-2)
|
|
|
|
errc(1, EILSEQ, NULL);
|
|
|
|
if (oclen == 1 && nclen == 1)
|
|
|
|
y->bytetab[(u_char)*op] = (u_char)*np;
|
|
|
|
else {
|
|
|
|
y->bytetab[(u_char)*op] = 0;
|
|
|
|
y->multis = xrealloc(y->multis,
|
|
|
|
(y->nmultis + 1) * sizeof(*y->multis));
|
|
|
|
i = y->nmultis++;
|
|
|
|
y->multis[i].fromlen = oclen;
|
|
|
|
memcpy(y->multis[i].from, op, oclen);
|
|
|
|
y->multis[i].tolen = nclen;
|
|
|
|
memcpy(y->multis[i].to, np, nclen);
|
|
|
|
}
|
|
|
|
op += oclen;
|
|
|
|
np += nclen;
|
|
|
|
}
|
1993-04-14 03:49:12 +04:00
|
|
|
}
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-06-06 04:13:13 +04:00
|
|
|
* Compile the text following an a or i command.
|
1993-04-14 03:49:12 +04:00
|
|
|
*/
|
|
|
|
static char *
|
2002-06-14 04:41:42 +04:00
|
|
|
compile_text(void)
|
1993-04-14 03:49:12 +04:00
|
|
|
{
|
2014-06-06 04:13:13 +04:00
|
|
|
size_t asize, size;
|
|
|
|
int esc_nl;
|
|
|
|
char *text, *p, *op, *s;
|
|
|
|
char lbuf[_POSIX2_LINE_MAX + 1];
|
|
|
|
|
|
|
|
asize = 2 * _POSIX2_LINE_MAX + 1;
|
|
|
|
text = xmalloc(asize);
|
|
|
|
size = 0;
|
|
|
|
while (cu_fgets(lbuf, sizeof(lbuf), NULL)) {
|
1993-04-14 03:49:12 +04:00
|
|
|
op = s = text + size;
|
2014-06-06 04:13:13 +04:00
|
|
|
p = lbuf;
|
|
|
|
for (esc_nl = 0; *p != '\0'; p++) {
|
|
|
|
if (*p == '\\' && p[1] != '\0' && *++p == '\n')
|
|
|
|
esc_nl = 1;
|
1993-04-14 03:49:12 +04:00
|
|
|
*s++ = *p;
|
|
|
|
}
|
2014-06-06 04:13:13 +04:00
|
|
|
size += (size_t)(s - op);
|
|
|
|
if (!esc_nl) {
|
1993-04-14 03:49:12 +04:00
|
|
|
*s = '\0';
|
|
|
|
break;
|
|
|
|
}
|
2014-06-06 04:13:13 +04:00
|
|
|
if (asize - size < _POSIX2_LINE_MAX + 1) {
|
|
|
|
asize *= 2;
|
|
|
|
text = xrealloc(text, asize);
|
|
|
|
}
|
1993-04-14 03:49:12 +04:00
|
|
|
}
|
2014-06-06 04:13:13 +04:00
|
|
|
text[size] = '\0';
|
|
|
|
p = xrealloc(text, size + 1);
|
|
|
|
return (p);
|
1993-04-14 03:49:12 +04: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 *
|
2002-06-14 04:41:42 +04:00
|
|
|
compile_addr(char *p, struct s_addr *a)
|
1993-04-14 03:49:12 +04:00
|
|
|
{
|
2014-06-06 04:13:13 +04:00
|
|
|
char *end, re[_POSIX2_LINE_MAX + 1];
|
|
|
|
int icase;
|
1993-04-14 03:49:12 +04:00
|
|
|
|
2014-06-06 04:13:13 +04:00
|
|
|
icase = 0;
|
|
|
|
|
|
|
|
a->type = 0;
|
1993-04-14 03:49:12 +04:00
|
|
|
switch (*p) {
|
|
|
|
case '\\': /* Context address */
|
|
|
|
++p;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case '/': /* Context address */
|
2014-06-06 04:13:13 +04:00
|
|
|
p = compile_delimited(p, re, 0);
|
1993-04-14 03:49:12 +04:00
|
|
|
if (p == NULL)
|
2014-06-06 04:13:13 +04:00
|
|
|
errx(1, "%lu: %s: unterminated regular expression", linenum, fname);
|
|
|
|
/* Check for case insensitive regexp flag */
|
|
|
|
if (*p == 'I') {
|
|
|
|
icase = 1;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
if (*re == '\0')
|
|
|
|
a->u.r = NULL;
|
|
|
|
else
|
|
|
|
a->u.r = compile_re(re, icase);
|
1993-04-14 03:49:12 +04:00
|
|
|
a->type = AT_RE;
|
|
|
|
return (p);
|
|
|
|
|
|
|
|
case '$': /* Last line */
|
|
|
|
a->type = AT_LAST;
|
|
|
|
return (p + 1);
|
2014-06-06 04:13:13 +04:00
|
|
|
|
|
|
|
case '+': /* Relative line number */
|
|
|
|
a->type = AT_RELLINE;
|
|
|
|
p++;
|
|
|
|
/* FALLTHROUGH */
|
1993-04-14 03:49:12 +04:00
|
|
|
/* Line number */
|
2014-06-06 04:13:13 +04:00
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
1993-04-14 03:49:12 +04:00
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
2014-06-06 04:13:13 +04:00
|
|
|
if (a->type == 0)
|
|
|
|
a->type = AT_LINE;
|
|
|
|
a->u.l = strtoul(p, &end, 10);
|
1993-04-14 03:49:12 +04:00
|
|
|
return (end);
|
|
|
|
default:
|
2014-06-06 04:13:13 +04:00
|
|
|
errx(1, "%lu: %s: expected context address", linenum, fname);
|
1993-04-14 03:49:12 +04:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1994-02-04 02:32:10 +03:00
|
|
|
* duptoeol --
|
|
|
|
* Return a copy of all the characters up to \n or \0.
|
1993-04-14 03:49:12 +04:00
|
|
|
*/
|
|
|
|
static char *
|
2009-04-13 11:29:55 +04:00
|
|
|
duptoeol(char *s, const char *ctype)
|
1993-04-14 03:49:12 +04:00
|
|
|
{
|
|
|
|
size_t len;
|
1994-02-04 02:32:10 +03:00
|
|
|
int ws;
|
2014-06-06 04:13:13 +04:00
|
|
|
char *p, *start;
|
1993-04-14 03:49:12 +04:00
|
|
|
|
1994-02-04 02:32:10 +03:00
|
|
|
ws = 0;
|
|
|
|
for (start = s; *s != '\0' && *s != '\n'; ++s)
|
1998-12-20 01:12:11 +03:00
|
|
|
ws = isspace((unsigned char)*s);
|
1993-04-14 03:49:12 +04:00
|
|
|
*s = '\0';
|
1994-02-04 02:32:10 +03:00
|
|
|
if (ws)
|
2014-06-06 04:13:13 +04:00
|
|
|
warnx("%lu: %s: whitespace after %s", linenum, fname, ctype);
|
|
|
|
len = (size_t)(s - start + 1);
|
|
|
|
p = xmalloc(len);
|
|
|
|
return (memmove(p, start, len));
|
1993-04-14 03:49:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1994-02-04 02:32:10 +03: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 :).
|
|
|
|
*
|
1993-04-14 03:49:12 +04:00
|
|
|
* TODO: Remove } nodes
|
|
|
|
*/
|
|
|
|
static void
|
2002-06-14 04:41:42 +04:00
|
|
|
fixuplabel(struct s_command *cp, struct s_command *end)
|
1993-04-14 03:49:12 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
for (; cp != end; cp = cp->next)
|
|
|
|
switch (cp->code) {
|
|
|
|
case 'a':
|
|
|
|
case 'r':
|
|
|
|
appendnum++;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
case 't':
|
1994-02-04 02:32:10 +03:00
|
|
|
/* Resolve branch target. */
|
1993-04-14 03:49:12 +04:00
|
|
|
if (cp->t == NULL) {
|
|
|
|
cp->u.c = NULL;
|
|
|
|
break;
|
|
|
|
}
|
1994-02-04 02:32:10 +03:00
|
|
|
if ((cp->u.c = findlabel(cp->t)) == NULL)
|
2014-06-06 04:13:13 +04:00
|
|
|
errx(1, "%lu: %s: undefined label '%s'", linenum, fname, cp->t);
|
1993-04-14 03:49:12 +04:00
|
|
|
free(cp->t);
|
|
|
|
break;
|
|
|
|
case '{':
|
1994-02-04 02:32:10 +03:00
|
|
|
/* Do interior commands. */
|
|
|
|
fixuplabel(cp->u.c, cp->next);
|
1993-04-14 03:49:12 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1994-02-04 02:32:10 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Associate the given command label for later lookup.
|
|
|
|
*/
|
|
|
|
static void
|
2002-06-14 04:41:42 +04:00
|
|
|
enterlabel(struct s_command *cp)
|
1994-02-04 02:32:10 +03:00
|
|
|
{
|
1997-10-20 03:05:11 +04:00
|
|
|
struct labhash **lhp, *lh;
|
|
|
|
u_char *p;
|
|
|
|
u_int h, c;
|
1994-02-04 02:32:10 +03: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)
|
2014-06-06 04:13:13 +04:00
|
|
|
errx(1, "%lu: %s: duplicate label '%s'", linenum, fname, cp->t);
|
1994-02-04 02:32:10 +03: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 *
|
2002-06-14 04:41:42 +04:00
|
|
|
findlabel(char *name)
|
1994-02-04 02:32:10 +03:00
|
|
|
{
|
1997-10-20 03:05:11 +04:00
|
|
|
struct labhash *lh;
|
|
|
|
u_char *p;
|
|
|
|
u_int h, c;
|
1994-02-04 02:32:10 +03: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);
|
|
|
|
}
|
|
|
|
|
2014-06-06 04:13:13 +04:00
|
|
|
/*
|
1994-02-04 02:32:10 +03:00
|
|
|
* Warn about any unused labels. As a side effect, release the label hash
|
|
|
|
* table space.
|
|
|
|
*/
|
|
|
|
static void
|
2002-06-14 04:41:42 +04:00
|
|
|
uselabel(void)
|
1994-02-04 02:32:10 +03:00
|
|
|
{
|
1997-10-20 03:05:11 +04:00
|
|
|
struct labhash *lh, *next;
|
|
|
|
int i;
|
1994-02-04 02:32:10 +03:00
|
|
|
|
|
|
|
for (i = 0; i < LHSZ; i++) {
|
|
|
|
for (lh = labels[i]; lh != NULL; lh = next) {
|
|
|
|
next = lh->lh_next;
|
|
|
|
if (!lh->lh_ref)
|
2014-06-06 04:13:13 +04:00
|
|
|
warnx("%lu: %s: unused label '%s'",
|
|
|
|
linenum, fname, lh->lh_cmd->t);
|
1994-02-04 02:32:10 +03:00
|
|
|
free(lh);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|