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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-08-07 21:33:47 +00:00
|
|
|
#if 0
|
1996-12-14 06:08:03 +00:00
|
|
|
static char * const rcsid = "@(#)re.c,v 1.6 1994/02/01 00:34:43 alm Exp";
|
1997-08-07 21:33:47 +00:00
|
|
|
#else
|
|
|
|
static char * const rcsid =
|
1999-08-27 23:15:48 +00:00
|
|
|
"$FreeBSD$";
|
1997-08-07 21:33:47 +00:00
|
|
|
#endif
|
1993-06-18 13:00:14 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include "ed.h"
|
|
|
|
|
1994-02-01 00:36:28 +00:00
|
|
|
|
1993-06-18 13:00:14 +00:00
|
|
|
extern int patlock;
|
|
|
|
|
1994-02-01 00:36:28 +00:00
|
|
|
char errmsg[MAXPATHLEN + 40] = "";
|
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 *
|
1994-02-01 00:36:28 +00:00
|
|
|
get_compiled_pattern()
|
1993-06-18 13:00:14 +00:00
|
|
|
{
|
|
|
|
static pattern_t *exp = NULL;
|
|
|
|
|
|
|
|
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) == ' ') {
|
1993-06-18 13:00:14 +00:00
|
|
|
sprintf(errmsg, "invalid pattern delimiter");
|
|
|
|
return NULL;
|
1994-02-01 00:36:28 +00:00
|
|
|
} else if (delimiter == '\n' || *++ibufp == '\n' || *ibufp == delimiter) {
|
1993-06-26 06:47:21 +00:00
|
|
|
if (!exp) sprintf(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));
|
|
|
|
sprintf(errmsg, "out of memory");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
patlock = 0;
|
1995-03-19 13:29:28 +00:00
|
|
|
if ((n = regcomp(exp, exps, 0))) {
|
1993-06-18 13:00:14 +00:00
|
|
|
regerror(n, exp, errmsg, sizeof errmsg);
|
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 *
|
1994-02-01 00:36:28 +00:00
|
|
|
extract_pattern(delimiter)
|
|
|
|
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) {
|
1993-06-18 13:00:14 +00:00
|
|
|
sprintf(errmsg, "unbalanced brackets ([])");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
if (*++nd == '\n') {
|
|
|
|
sprintf(errmsg, "trailing backslash (\\)");
|
|
|
|
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 *
|
1994-02-01 00:36:28 +00:00
|
|
|
parse_char_class(s)
|
1993-06-18 13:00:14 +00:00
|
|
|
char *s;
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|