2002-01-23 22:07:33 +03:00
|
|
|
/* $NetBSD: re.c,v 1.17 2002/01/23 19:07:33 atatat Exp $ */
|
1995-03-21 12:01:59 +03:00
|
|
|
|
1993-04-08 05:07:16 +04:00
|
|
|
/* re.c: This file contains the regular expression interface routines for
|
|
|
|
the ed line editor. */
|
|
|
|
/*-
|
1993-11-12 13:48:43 +03:00
|
|
|
* Copyright (c) 1993 Andrew Moore, Talke Studio.
|
1993-04-08 05:07:16 +04: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.
|
|
|
|
*
|
1993-11-12 13:48:43 +03:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
1993-04-08 05:07:16 +04:00
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
1993-11-12 13:48:43 +03:00
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
1993-04-08 05:07:16 +04: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.
|
|
|
|
*/
|
|
|
|
|
1997-07-20 10:35:35 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-04-08 05:07:16 +04:00
|
|
|
#ifndef lint
|
1995-03-21 12:01:59 +03:00
|
|
|
#if 0
|
1994-02-20 09:56:16 +03:00
|
|
|
static char *rcsid = "@(#)re.c,v 1.6 1994/02/01 00:34:43 alm Exp";
|
1995-03-21 12:01:59 +03:00
|
|
|
#else
|
2002-01-23 22:07:33 +03:00
|
|
|
__RCSID("$NetBSD: re.c,v 1.17 2002/01/23 19:07:33 atatat Exp $");
|
1995-03-21 12:01:59 +03:00
|
|
|
#endif
|
1993-04-08 05:07:16 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include "ed.h"
|
|
|
|
|
1994-02-20 09:56:16 +03:00
|
|
|
|
|
|
|
char errmsg[MAXPATHLEN + 40] = "";
|
1993-04-08 05:07:16 +04:00
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* get_compiled_pattern: return pointer to compiled pattern from command
|
|
|
|
buffer */
|
1993-04-08 05:07:16 +04:00
|
|
|
pattern_t *
|
1993-11-23 07:41:44 +03:00
|
|
|
get_compiled_pattern()
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
|
|
|
static pattern_t *exp = NULL;
|
|
|
|
|
|
|
|
char *exps;
|
1993-11-23 07:41:44 +03:00
|
|
|
char delimiter;
|
1993-04-08 05:07:16 +04:00
|
|
|
int n;
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
if ((delimiter = *ibufp) == ' ') {
|
1993-04-08 05:07:16 +04:00
|
|
|
sprintf(errmsg, "invalid pattern delimiter");
|
|
|
|
return NULL;
|
1993-11-23 07:41:44 +03:00
|
|
|
} else if (delimiter == '\n' || *++ibufp == '\n' || *ibufp == delimiter) {
|
1993-07-02 14:02:26 +04:00
|
|
|
if (!exp) sprintf(errmsg, "no previous pattern");
|
1993-04-08 05:07:16 +04:00
|
|
|
return exp;
|
1993-11-23 07:41:44 +03:00
|
|
|
} else if ((exps = extract_pattern(delimiter)) == NULL)
|
1993-04-08 05:07:16 +04:00
|
|
|
return NULL;
|
|
|
|
/* buffer alloc'd && not reserved */
|
|
|
|
if (exp && !patlock)
|
|
|
|
regfree(exp);
|
|
|
|
else if ((exp = (pattern_t *) malloc(sizeof(pattern_t))) == NULL) {
|
1993-04-14 16:22:14 +04:00
|
|
|
fprintf(stderr, "%s\n", strerror(errno));
|
1993-04-10 15:46:38 +04:00
|
|
|
sprintf(errmsg, "out of memory");
|
1993-04-08 05:07:16 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
patlock = 0;
|
2002-01-23 22:07:33 +03:00
|
|
|
if ((n = regcomp(exp, exps, ere)) != 0) {
|
1993-04-08 05:07:16 +04:00
|
|
|
regerror(n, exp, errmsg, sizeof errmsg);
|
1993-08-30 06:20:18 +04:00
|
|
|
free(exp);
|
|
|
|
return exp = NULL;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
return exp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* extract_pattern: copy a pattern string from the command buffer; return
|
|
|
|
pointer to the copy */
|
1993-04-08 05:07:16 +04:00
|
|
|
char *
|
1993-11-23 07:41:44 +03:00
|
|
|
extract_pattern(delimiter)
|
|
|
|
int delimiter;
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
1994-02-20 09:56:16 +03:00
|
|
|
static char *lhbuf = NULL; /* buffer */
|
|
|
|
static int lhbufsz = 0; /* buffer size */
|
|
|
|
|
1993-04-08 05:07:16 +04:00
|
|
|
char *nd;
|
1993-05-08 14:49:52 +04:00
|
|
|
int len;
|
1993-04-08 05:07:16 +04:00
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
for (nd = ibufp; *nd != delimiter && *nd != '\n'; nd++)
|
1993-04-08 05:07:16 +04:00
|
|
|
switch (*nd) {
|
|
|
|
default:
|
|
|
|
break;
|
1993-05-08 14:49:52 +04:00
|
|
|
case '[':
|
1993-11-23 07:41:44 +03:00
|
|
|
if ((nd = parse_char_class(++nd)) == NULL) {
|
1993-04-08 05:07:16 +04:00
|
|
|
sprintf(errmsg, "unbalanced brackets ([])");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
break;
|
1993-05-08 14:49:52 +04:00
|
|
|
case '\\':
|
1993-04-08 05:07:16 +04:00
|
|
|
if (*++nd == '\n') {
|
|
|
|
sprintf(errmsg, "trailing backslash (\\)");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
1993-05-08 14:49:52 +04:00
|
|
|
len = nd - ibufp;
|
1994-02-20 09:56:16 +03:00
|
|
|
REALLOC(lhbuf, lhbufsz, len + 1, NULL);
|
1993-05-08 14:49:52 +04:00
|
|
|
memcpy(lhbuf, ibufp, len);
|
|
|
|
lhbuf[len] = '\0';
|
1993-04-08 05:07:16 +04:00
|
|
|
ibufp = nd;
|
1993-11-23 07:41:44 +03:00
|
|
|
return (isbinary) ? NUL_TO_NEWLINE(lhbuf, len) : lhbuf;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-11-23 07:41:44 +03:00
|
|
|
/* parse_char_class: expand a POSIX character class */
|
1993-04-08 05:07:16 +04:00
|
|
|
char *
|
1993-11-23 07:41:44 +03:00
|
|
|
parse_char_class(s)
|
1993-05-12 12:22:03 +04:00
|
|
|
char *s;
|
1993-04-08 05:07:16 +04:00
|
|
|
{
|
1993-05-12 12:22:03 +04: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;
|
1993-04-08 05:07:16 +04:00
|
|
|
}
|