commit 74e966a35d5c673629afe47a1b79adba1aa9ddbf
parent 238f7f10768030d8a0bc7a0659e1427b127129e2
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Sat, 12 Dec 2015 16:52:07 +0100
Revert "Split compile into two different functions"
This reverts commit 91e4a06811bd9d9358b2154bbf060de5250dd5db.
Diffstat:
M | ed.c | | | 88 | ++++++++++++++++++++++++------------------------------------------------------- |
1 file changed, 27 insertions(+), 61 deletions(-)
diff --git a/ed.c b/ed.c
@@ -47,9 +47,7 @@ char *cmdline;
char *ocmdline;
size_t cmdsiz, cmdcap;
int repidx;
-
-int gsub;
-char *lhs, *rhs;
+char *rhs;
static void
error(char *msg)
@@ -276,14 +274,16 @@ setscratch()
modflag = 0;
}
-static char *
-getregex(int delim)
+static void
+compile(int delim)
{
- int n, c, bracket;
+ int n, ret, 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)
@@ -304,26 +304,17 @@ getregex(int delim)
lastre = addchar(c, lastre, &cap, &siz);
}
if (n == 0) {
- if (!lastre)
+ if (!pattern)
error("no previous pattern");
- } else {
- lastre = addchar('\0', lastre, &cap, &siz);
+ return;
}
-
- return lastre;
-}
-
-static void
-compile(char *s)
-{
- int ret;
- static char buf[BUFSIZ];
+ lastre = addchar('\0', lastre, &cap, &siz);
if (pattern)
regfree(pattern);
if (!pattern && (!(pattern = malloc(sizeof(*pattern)))))
error("out of memory");
- if ((ret = regcomp(pattern, s, 0))) {
+ if ((ret = regcomp(pattern, lastre, 0))) {
regerror(ret, pattern, buf, sizeof(buf));
error(buf);
}
@@ -397,7 +388,7 @@ linenum(int *line)
break;
case '?':
case '/':
- compile(getregex(c));
+ compile(c);
ln = match(c);
break;
case '^':
@@ -804,74 +795,49 @@ execsh(void)
}
static void
-getlhs(int delim)
-{
- int c;
- char *s;
-
- back(c = input());
- if (c == delim) {
- if ((s = lastre) == NULL)
- error("No previous substitution");
- } else {
- s = getregex(delim);
- }
- free(lhs);
- if ((lhs = strdup(s)) == NULL)
- error("out of memory");
-
- compile(lhs);
- if (input() != delim)
- error("invalid pattern delimiter");
-}
-
-static void
getrhs(int delim)
{
- char *repl;
int c;
size_t n, siz, cap;
+ static char *s;
- repl = NULL;
+ free(s);
+ s = NULL;
siz = cap = 0;
while ((c = input()) != '\n' && c != EOF && c != delim) {
if (c == '\\') {
if (isdigit(c = input()))
back(c);
}
- repl = addchar(c, repl, &siz, &cap);
+ s = addchar(c, s, &siz, &cap);
}
+ s = addchar('\0', s, &siz, &cap);
if (c == EOF)
error("invalid pattern delimiter");
+ if (c == '\n')
+ pflag = 'p';
back(c);
- repl = addchar('\0', repl, &siz, &cap);
- if (!strcmp("%", repl)) {
- free(repl);
+ if (!strcmp("%", s)) {
+ free(s);
if (!rhs)
error("no previous substitution");
} else {
free(rhs);
- rhs = repl;
+ rhs = s;
}
+ s = NULL;
}
static void
subs(void)
{
int c, delim;
+ int gsub;
- if ((delim = input()) == '\n') {
- if (!rhs || !lhs)
- error("no previous substitution");
- back('\n');
- } else {
- getlhs(delim);
- getrhs(delim);
- if ((c = input()) == '\n')
- pflag = 'p';
- back(c);
- }
+ delim = input();
+ compile(delim);
+ getrhs(delim);
if ((c = input()) == 'g') {
gsub = -1;
@@ -1121,7 +1087,7 @@ chkglobal(void)
gflag = 1;
deflines(nextln(0), lastln);
delim = input();
- compile(getregex(delim));
+ compile(delim);
for (i = line1; i <= line2; i++)
setglobal(i, nmatch);