ed

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

commit 3a265e90269f21d447251c7f305787f5da071b44
parent f37932bc4091a70b98fdf49dc76814bcec8ab2f5
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu Dec 10 16:13:22 +0100

Move getchar() and ungetc() to input() and back()

This is the first step to create a wrappers functions which
stores the full command line into an internal array, so
the command can be re applied later.

Diffstat:
ed.c | 78+++++++++++++++++++++++++++++++++++++++++++++---------------------------------
1 file changed, 45 insertions(+), 33 deletions(-)
diff --git a/ed.c b/ed.c @@ -55,6 +55,18 @@ error(char *msg) } static int +input(void) +{ + return getchar(); +} + +static int +back(int c) +{ + return ungetc(c, stdin); +} + +static int nextln(int line) { ++line; @@ -240,17 +252,17 @@ compile() char *bp, delim, buff[REGEXSIZE]; int c ,ret; - delim = getchar(); + delim = input(); for (bp = buff; bp < &buff[BUFSIZ-1]; *bp++ = c) { - if ((c = getchar()) == delim || c == '\n' || c == EOF) { - ungetc(c, stdin); + if ((c = input()) == delim || c == '\n' || c == EOF) { + back(c); break; } if (c == '\\') { if (bp == &buff[BUFSIZ-2]) break; *bp++ = c; - c = getchar(); + c = input(); } } if (bp == buff) { @@ -292,9 +304,9 @@ skipblank(void) { char c; - while ((c = getchar()) == ' ' || c == '\t') + while ((c = input()) == ' ' || c == '\t') /* nothing */; - ungetc(c, stdin); + back(c); } static int @@ -302,7 +314,7 @@ getnum(void) { int ln, n, c; - for (ln = 0; isdigit(c = getchar()); ln += n) { + for (ln = 0; isdigit(c = input()); ln += n) { if (ln > INT_MAX/10) goto invalid; n = c - '0'; @@ -310,7 +322,7 @@ getnum(void) if (INT_MAX - ln < n) goto invalid; } - ungetc(c, stdin); + back(c); return ln; invalid: @@ -324,7 +336,7 @@ linenum(int *line) skipblank(); - switch (c = getchar()) { + switch (c = input()) { case '.': ln = curln; break; @@ -340,17 +352,17 @@ linenum(int *line) break; case '?': case '/': - ungetc(c, stdin); + back(c); compile(); ln = match(c); break; case '-': case '+': ln = curln; - ungetc(c, stdin); + back(c); break; default: - ungetc(c, stdin); + back(c); if (isdigit(c)) ln = getnum(); else @@ -371,16 +383,16 @@ address(int *line) for (;;) { skipblank(); - if ((c = getchar()) != '+' && c != '-') + if ((c = input()) != '+' && c != '-') break; sign = c == '+'; - num = isdigit(ungetc(getchar(), stdin)) ? getnum() : 1; + num = isdigit(back(input())) ? getnum() : 1; num *= sign; if (INT_MAX - ln < num) goto invalid; ln += sign * num; } - ungetc(c, stdin); + back(c); if (ln < 0 || ln > lastln) error("invalid address"); @@ -396,13 +408,13 @@ getlst() { int ln, c; - if ((c = getchar()) == ',') { + if ((c = input()) == ',') { line1 = 1; line2 = lastln; nlines = lastln; return; } - ungetc(c, stdin); + back(c); line2 = curln; for (nlines = 0; address(&ln); ) { line1 = line2; @@ -410,8 +422,8 @@ getlst() ++nlines; skipblank(); - if ((c = getchar()) != ',' && c != ';') { - ungetc(c, stdin); + if ((c = input()) != ',' && c != ';') { + back(c); break; } if (c == ';') @@ -551,12 +563,12 @@ chkprint(int flag) char c; if (flag) { - if ((c = getchar()) == 'p' || c == 'l' || c == 'n') + if ((c = input()) == 'p' || c == 'l' || c == 'n') pflag = c; else - ungetc(c, stdin); + back(c); } - if (getchar() != '\n') + if (input() != '\n') error("invalid command suffix"); } @@ -567,12 +579,12 @@ getfname(void) char *bp; static char fname[FILENAME_MAX]; - if ((c = getchar()) != '\n' && c != ' ' && c != '\0') + if ((c = input()) != '\n' && c != ' ' && c != '\0') error("invalid command suffix"); skipblank(); for (bp = fname; bp < &fname[FILENAME_MAX]; *bp = c) { - if ((c = getchar()) != EOF || c == '\n') + if ((c = input()) != EOF || c == '\n') break; } if (bp == fname) { @@ -706,9 +718,9 @@ execsh(void) char c; skipblank(); - if ((c = getchar()) != '!') { + if ((c = input()) != '!') { siz = 0; - while ((c = getchar()) != EOF && c != '\n') + while ((c = input()) != EOF && c != '\n') cmd = addchar(c, cmd, &cap, &siz); cmd = addchar('\0', cmd, &cap, &siz); } else if (!cmd) { @@ -748,7 +760,7 @@ repeat: case 'l': case 'n': case 'p': - ungetc(cmd, stdin); + back(cmd); chkprint(1); deflines(curln, curln); goto print; @@ -836,7 +848,7 @@ repeat: case 'z': if (nlines > 1) goto bad_address; - if (isdigit(ungetc(getchar(), stdin))) + if (isdigit(back(input()))) num = getnum(); else num = 24; @@ -846,7 +858,7 @@ repeat: case 'k': if (nlines > 1) goto bad_address; - if (!islower(c = getchar())) + if (!islower(c = input())) error("invalid mark character"); chkprint(1); deflines(curln, curln); @@ -921,7 +933,7 @@ chkglobal(void) uflag = 1; skipblank(); - switch (c = getchar()) { + switch (c = input()) { case 'g': uflag = 0; case 'G': @@ -933,12 +945,12 @@ chkglobal(void) nmatch = 0; break; default: - ungetc(c, stdin); + back(c); return 0; } deflines(nextln(0), lastln); compile(); - getchar(); /* skip trailing '/' */ + input(); /* skip trailing '/' */ for (i = line1; i <= line2; i++) setglobal(i, nmatch); @@ -1049,7 +1061,7 @@ main(int argc, char *argv[]) fputs(prompt, stdout); getlst(); skipblank(); - chkglobal() ? doglobal() : docmd(getchar()); + chkglobal() ? doglobal() : docmd(input()); } /* not reached */