commit 6846c4224dd3342c4cc67e08ac4910d44802b3ad
parent fec32b46a7e7f226df503b2c25b959ad7b3f59c8
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Tue, 1 Dec 2015 19:21:28 +0100
ed: improve regex/compile()
- use regerror for errors
- fix crash with input "/\n" (overflow, bp would be > cmdp).
- only match when compile() was successfull (WIP).
regex still has some bugs, more fixes coming soon.
Diffstat:
M | ed.c | | | 28 | ++++++++++++++++------------ |
1 file changed, 16 insertions(+), 12 deletions(-)
diff --git a/ed.c b/ed.c
@@ -230,11 +230,13 @@ setscratch()
modflag = 0;
}
-static void
+static int
compile()
{
+ static char regerrbuf[BUFSIZ];
char c, *bp, delim, buff[REGEXSIZE];
size_t len;
+ int ret;
for (delim = *cmdp++, bp = cmdp; c = *cmdp; ++cmdp) {
if (c == '\n')
@@ -242,21 +244,23 @@ compile()
if (c == '\n' || c == delim)
break;
}
- if (!(len = cmdp - bp)) {
+ if (bp >= cmdp) {
if (!pattern)
error("no previous pattern");
- return;
- }
- if (!pattern) {
- if (!(pattern = malloc(sizeof(*pattern))))
- error("no memory");
+ return 0;
}
+ if (!pattern && (!(pattern = malloc(sizeof(*pattern)))))
+ error("no memory");
+ len = cmdp - bp;
if (len >= sizeof(buff))
error("regular expression too long");
memcpy(buff, bp, len);
buff[len] = '\0';
- if (regcomp(pattern, buff, 0))
- error("incorrect regular expression"); /* TODO: call regerror */
+ if ((ret = regcomp(pattern, buff, REG_BASIC))) {
+ regerror(ret, pattern, regerrbuf, sizeof(regerrbuf));
+ error(regerrbuf);
+ }
+ return !ret;
}
static int
@@ -310,8 +314,8 @@ getnum(int *line)
sign = -sign;
case '/':
c = *cmdp;
- compile();
- ln = match(c);
+ if (compile())
+ ln = match(c);
break;
default:
if (isdigit(*cmdp)) {
@@ -896,7 +900,7 @@ chkglobal(void)
return 0;
}
deflines(nextln(0), lastln);
- compile();
+ compile(); /* TODO: check compile */
++cmdp; /* skip trailing '/' */
if (uflag)