1993-06-18 13:00:14 +00:00
|
|
|
/* re.c: This file contains the regular expression interface routines for
|
|
|
|
the ed line editor. */
|
|
|
|
/*-
|
1994-02-01 00:36:28 +00:00
|
|
|
* Copyright (c) 1993 Andrew Moore, Talke Studio.
|
1993-06-18 13:00:14 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
1994-02-01 00:36:28 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
1993-06-18 13:00:14 +00:00
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
1994-02-01 00:36:28 +00:00
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
1993-06-18 13:00:14 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2002-06-30 05:13:54 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1993-06-18 13:00:14 +00:00
|
|
|
|
|
|
|
#include "ed.h"
|
|
|
|
|
1994-02-01 00:36:28 +00:00
|
|
|
|
1993-06-18 13:00:14 +00:00
|
|
|
extern int patlock;
|
|
|
|
|
2001-08-06 22:01:31 +00:00
|
|
|
const char *errmsg = "";
|
1993-06-18 13:00:14 +00:00
|
|
|
|
1995-05-30 00:07:29 +00:00
|
|
|
/* get_compiled_pattern: return pointer to compiled pattern from command
|
1994-02-01 00:36:28 +00:00
|
|
|
buffer */
|
1993-06-18 13:00:14 +00:00
|
|
|
pattern_t *
|
2002-02-02 06:36:49 +00:00
|
|
|
get_compiled_pattern(void)
|
1993-06-18 13:00:14 +00:00
|
|
|
{
|
|
|
|
static pattern_t *exp = NULL;
|
2001-08-06 22:01:31 +00:00
|
|
|
static char error[1024];
|
1993-06-18 13:00:14 +00:00
|
|
|
|
|
|
|
char *exps;
|
1994-02-01 00:36:28 +00:00
|
|
|
char delimiter;
|
1993-06-18 13:00:14 +00:00
|
|
|
int n;
|
|
|
|
|
1994-02-01 00:36:28 +00:00
|
|
|
if ((delimiter = *ibufp) == ' ') {
|
2001-08-06 22:01:31 +00:00
|
|
|
errmsg = "invalid pattern delimiter";
|
1993-06-18 13:00:14 +00:00
|
|
|
return NULL;
|
1994-02-01 00:36:28 +00:00
|
|
|
} else if (delimiter == '\n' || *++ibufp == '\n' || *ibufp == delimiter) {
|
2001-08-06 22:01:31 +00:00
|
|
|
if (!exp)
|
|
|
|
errmsg = "no previous pattern";
|
1993-06-18 13:00:14 +00:00
|
|
|
return exp;
|
1994-02-01 00:36:28 +00:00
|
|
|
} else if ((exps = extract_pattern(delimiter)) == NULL)
|
1993-06-18 13:00:14 +00:00
|
|
|
return NULL;
|
|
|
|
/* buffer alloc'd && not reserved */
|
|
|
|
if (exp && !patlock)
|
|
|
|
regfree(exp);
|
|
|
|
else if ((exp = (pattern_t *) malloc(sizeof(pattern_t))) == NULL) {
|
|
|
|
fprintf(stderr, "%s\n", strerror(errno));
|
2001-08-06 22:01:31 +00:00
|
|
|
errmsg = "out of memory";
|
1993-06-18 13:00:14 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
patlock = 0;
|
1995-03-19 13:29:28 +00:00
|
|
|
if ((n = regcomp(exp, exps, 0))) {
|
2001-08-06 22:01:31 +00:00
|
|
|
regerror(n, exp, error, sizeof error);
|
|
|
|
errmsg = error;
|
1993-08-16 09:30:18 +00:00
|
|
|
free(exp);
|
|
|
|
return exp = NULL;
|
1993-06-18 13:00:14 +00:00
|
|
|
}
|
|
|
|
return exp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-02-01 00:36:28 +00:00
|
|
|
/* extract_pattern: copy a pattern string from the command buffer; return
|
|
|
|
pointer to the copy */
|
1993-06-18 13:00:14 +00:00
|
|
|
char *
|
2002-02-02 06:36:49 +00:00
|
|
|
extract_pattern(int delimiter)
|
1993-06-18 13:00:14 +00:00
|
|
|
{
|
1994-02-01 00:36:28 +00:00
|
|
|
static char *lhbuf = NULL; /* buffer */
|
|
|
|
static int lhbufsz = 0; /* buffer size */
|
|
|
|
|
1993-06-18 13:00:14 +00:00
|
|
|
char *nd;
|
|
|
|
int len;
|
|
|
|
|
1994-02-01 00:36:28 +00:00
|
|
|
for (nd = ibufp; *nd != delimiter && *nd != '\n'; nd++)
|
1993-06-18 13:00:14 +00:00
|
|
|
switch (*nd) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case '[':
|
1994-02-01 00:36:28 +00:00
|
|
|
if ((nd = parse_char_class(++nd)) == NULL) {
|
2001-08-06 22:01:31 +00:00
|
|
|
errmsg = "unbalanced brackets ([])";
|
1993-06-18 13:00:14 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
if (*++nd == '\n') {
|
2001-08-06 22:01:31 +00:00
|
|
|
errmsg = "trailing backslash (\\)";
|
1993-06-18 13:00:14 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
len = nd - ibufp;
|
1994-02-01 00:36:28 +00:00
|
|
|
REALLOC(lhbuf, lhbufsz, len + 1, NULL);
|
1993-06-18 13:00:14 +00:00
|
|
|
memcpy(lhbuf, ibufp, len);
|
|
|
|
lhbuf[len] = '\0';
|
|
|
|
ibufp = nd;
|
1994-02-01 00:36:28 +00:00
|
|
|
return (isbinary) ? NUL_TO_NEWLINE(lhbuf, len) : lhbuf;
|
1993-06-18 13:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-02-01 00:36:28 +00:00
|
|
|
/* parse_char_class: expand a POSIX character class */
|
1993-06-18 13:00:14 +00:00
|
|
|
char *
|
2002-02-02 06:36:49 +00:00
|
|
|
parse_char_class(char *s)
|
1993-06-18 13:00:14 +00:00
|
|
|
{
|
|
|
|
int c, d;
|
|
|
|
|
|
|
|
if (*s == '^')
|
|
|
|
s++;
|
|
|
|
if (*s == ']')
|
|
|
|
s++;
|
|
|
|
for (; *s != ']' && *s != '\n'; s++)
|
|
|
|
if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '='))
|
|
|
|
for (s++, c = *++s; *s != ']' || c != d; s++)
|
|
|
|
if ((c = *s) == '\n')
|
|
|
|
return NULL;
|
|
|
|
return (*s == ']') ? s : NULL;
|
|
|
|
}
|