Author | Jamozed <[email protected]> |
Date | 2021-04-01 07:37:00 |
Commit | 6fe231e405317b6c8c2c66c2dcb5db265ac9ce6e |
Parent | ba8ec312979ec7102c224dff6130d4ec2ad78218 |
Add non-terminal input handling to lineread
Diffstat
M | src/lineread.c | | | 30 | ++++++++++++++++++++++++++++-- |
M | src/parse.c | | | 6 | +++--- |
2 files changed, 31 insertions, 5 deletions
diff --git a/src/lineread.c b/src/lineread.c index ce20d9b..27357f1 100644 --- a/src/lineread.c +++ b/src/lineread.c @@ -52,6 +52,7 @@ struct line { static struct termios tco, tcn; static bool rawflag = false, ateflag = false; +static char *linentty(void); static char *lineedit(void); static void lineESC(struct line *l, register int c); @@ -68,6 +69,7 @@ static void lineMoveWordHome(struct line *l); static void lineMoveWordEnd(struct line *l); static void lineMoveHome(struct line *l); static void lineMoveEnd(struct line *l); +static void linePush(struct line *l, char c); static int lineInsert(struct line *l, char c); static void lineBackspace(struct line *l); static void lineDelete(struct line *l); @@ -78,11 +80,28 @@ static void lineDeleteEnd(struct line *l); /* Read a line from stdin */ char *lineread(void) { - if (errno) { warn("%s", serr()); } - if (!isatty(STDIN_FILENO)) { return NULL; } // TODO + if (!isatty(STDIN_FILENO)) { errno = 0; return linentty(); } else { return lineedit(); } } +/* Read from a non-terminal stdin */ +static char *linentty(void) { + struct line l; register char *r; + l.sp = 0; l.sl = 0; l.sc = 1024; + + if (!(l.s = malloc(l.sc * sizeof (*l.s)))) { return NULL; } + + for (register int c; (c = fgetc(stdin));) { + if (c == EOF) { if (l.sl) { break; } r = NULL; goto ret; } + else { linePush(&l, c); continue; } + } + +end:; + r = strndup(l.s, l.sl); +ret:; + free(l.s); return r; +} + /* Dynamically read a line from stdin */ static char *lineedit(void) { struct line l; register char *r; @@ -249,6 +268,13 @@ static void lineMoveEnd(struct line *l) { l->sp = l->sl; lineRefresh(l); return; } +/* Push character onto end of line */ +static void linePush(struct line *l, char c) { + if (l->sl + 1 > l->sc) { l->sc *= 2; + l->s = realloc(l->s, l->sc * sizeof (*l->s)); + } l->s[l->sl] = c; l->s[++l->sl] = 0; return; +} + /* Insert character at cursor */ static int lineInsert(struct line *l, char c) { if (l->sl + 1 == l->sc) { /* TODO expand string */ } diff --git a/src/parse.c b/src/parse.c index 2a676a2..b357220 100644 --- a/src/parse.c +++ b/src/parse.c @@ -66,14 +66,14 @@ static inline char *lex(struct lex *l) { struct str s; s.sl = 0; s.sc = 64; skip:; - for (; isspace(c); ++l->sp) {} if (!c) { return NULL; } + for (; isspace(c) && c != '\n'; ++l->sp) {} if (!c) { return NULL; } - if (c == ';') { ++l->sp; return NULL; } + if (c == '\n' || c == ';') { ++l->sp; return NULL; } if (c == '#') { for (++l->sp; !(c == '\n'); ++l->sp) {} goto skip; } if (!(s.s = malloc(s.sc * sizeof (*s.s)))) { return NULL; } - for (; c && c != ';'; ++l->sp) { + for (; c && c != '\n' && c != ';'; ++l->sp) { if (isspace(c)) { ++l->sp; break; } switch (c) {