commit 91e4a06811bd9d9358b2154bbf060de5250dd5db
parent 12bdc69e781567bac907074a1ef25ba90500b628
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Sat, 12 Dec 2015 10:41:19 +0100
Split compile into two different functions
This is important because we will want to compile
regular expressions that don't come from the input.
Diffstat:
M | ed.c | | | 29 | ++++++++++++++++++----------- |
1 file changed, 18 insertions(+), 11 deletions(-)
diff --git a/ed.c b/ed.c
@@ -273,16 +273,14 @@ setscratch()
modflag = 0;
}
-static void
-compile(int delim)
+static char *
+getregex(int delim)
{
- int n, ret, c,bracket;
+ int n, c, bracket;
static size_t siz, cap;
- static char buf[BUFSIZ];
if (!isgraph(delim))
error("invalid pattern delimiter");
-
bracket = siz = 0;
for (n = 0;; ++n) {
if ((c = input()) == delim && !bracket)
@@ -303,17 +301,26 @@ compile(int delim)
lastre = addchar(c, lastre, &cap, &siz);
}
if (n == 0) {
- if (!pattern)
+ if (!lastre)
error("no previous pattern");
- return;
+ } else {
+ lastre = addchar('\0', lastre, &cap, &siz);
}
- lastre = addchar('\0', lastre, &cap, &siz);
+
+ return lastre;
+}
+
+static void
+compile(char *s)
+{
+ int ret;
+ static char buf[BUFSIZ];
if (pattern)
regfree(pattern);
if (!pattern && (!(pattern = malloc(sizeof(*pattern)))))
error("out of memory");
- if ((ret = regcomp(pattern, lastre, 0))) {
+ if ((ret = regcomp(pattern, s, 0))) {
regerror(ret, pattern, buf, sizeof(buf));
error(buf);
}
@@ -387,7 +394,7 @@ linenum(int *line)
break;
case '?':
case '/':
- compile(c);
+ compile(getregex(c));
ln = match(c);
break;
case '^':
@@ -1026,7 +1033,7 @@ chkglobal(void)
gflag = 1;
deflines(nextln(0), lastln);
delim = input();
- compile(delim);
+ compile(getregex(delim));
for (i = line1; i <= line2; i++)
setglobal(i, nmatch);