ed

simple ed
git clone git://git.2f30.org/ed
Log | Files | Refs | LICENSE

commit 2dd7cf8673ea46255b65b7eb1000280ba452530f
parent 87c56d264c0f11c17339bf751e16a5d4020ecb71
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed,  9 Dec 2015 09:51:08 +0100

Convert chkglobal() into getchar() parser alike

This is the next step in our conversion of the input method.
At this point we already have fixed the problem related
to regular expressions, but we know that we have addede several
bugs, and we also want to remove the readcmd() function,
so there is a lot of job pending.

Diffstat:
Med.c | 68+++++++++++++++++++++++++++++++++++++++-----------------------------
1 file changed, 39 insertions(+), 29 deletions(-)

diff --git a/ed.c b/ed.c @@ -240,28 +240,35 @@ static void compile() { static char regerrbuf[BUFSIZ]; - char c, *bp, delim, buff[REGEXSIZE]; - size_t len; - int ret; + char *bp, delim, buff[REGEXSIZE]; + int c ,ret; - for (delim = *cmdp++, bp = cmdp; c = *cmdp; ++cmdp) { - if (c == '\n') - --cmdp; - if (c == '\n' || c == delim) + delim = getchar(); + for (bp = buff; bp < &buff[BUFSIZ-1]; *bp++ = c) { + if ((c = getchar()) == delim || c == '\n' || c == EOF) { + ungetc(c, stdin); break; + } + if (c == '\\') { + if (bp == &buff[BUFSIZ-2]) + break; + *bp++ = c; + c = getchar(); + } } - if (bp >= cmdp) { + if (bp == buff) { if (!pattern) error("no previous pattern"); return; } + if (bp == &buff[BUFSIZ-1]) { + /* TODO: Use addchar here! */ + error("too long regular expression"); + } + buff[buff - bp] = '\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'; + error("out of memory"); if ((ret = regcomp(pattern, buff, 0))) { regerror(ret, pattern, regerrbuf, sizeof(regerrbuf)); error(regerrbuf); @@ -332,7 +339,7 @@ invalid: static int linenum(int *line) { - int ln, c, sign = 1, ret = 1; + int ln, c, ret = 1; while (isspace(c = getchar())) /* nothing */; @@ -353,10 +360,10 @@ linenum(int *line) ln = lastln; break; case '?': - sign = -sign; case '/': - // compile(); - // ln = match(c); + ungetc(c, stdin); + compile(); + ln = match(c); break; case '-': case '+': @@ -923,12 +930,13 @@ docmd() static int chkglobal(void) { - int nmatch, i; + int c, nmatch, i; uflag = 1; - skipblank(); + while (isspace(c = getchar())) + /* nothing */; - switch (*cmdp++) { + switch (c) { case 'g': uflag = 0; case 'G': @@ -940,15 +948,12 @@ chkglobal(void) nmatch = 0; break; default: - --cmdp; + ungetc(c, stdin); return 0; } deflines(nextln(0), lastln); compile(); - ++cmdp; /* skip trailing '/' */ - - if (uflag) - chkprint(0); + getchar(); /* skip trailing '/' */ for (i = line1; i <= line2; i++) setglobal(i, nmatch); @@ -980,6 +985,9 @@ doglobal(void) int i, k; char *s, c; + if (uflag) + chkprint(0); + s = cmdp; for (i = 1; i <= lastln; i++) { k = getindex(i); @@ -1084,11 +1092,13 @@ main(int argc, char *argv[]) if (optprompt) fputs(prompt, stdout); getlst(); - readcmd(); - if (chkglobal()) + if (chkglobal()) { + readcmd(); doglobal(); - else + } else { + readcmd(); docmd(); + } } /* not reached */