Author | Jamozed <[email protected]> |
Date | 2021-03-30 08:08:56 |
Commit | 6fb6cdf3c2a00b9992af388fdb40c9dab63f6caf |
Parent | 8b9271124ffd98418df623614c09c301ed86ca4e |
Reimplement basic quote parsing
Diffstat
M | src/lineread.c | | | 7 | ++----- |
M | src/parse.c | | | 55 | +++++++++++++++++++++++++++---------------------------- |
2 files changed, 29 insertions, 33 deletions
diff --git a/src/lineread.c b/src/lineread.c index 5b60696..c8241b5 100644 --- a/src/lineread.c +++ b/src/lineread.c @@ -103,10 +103,7 @@ static char *lineedit(void) { case '\x1B': switch ((c = fgetc(stdin))) { case '[': switch ((c = fgetc(stdin))) { case '2': switch ((c = fgetc(stdin))) { - case '~': { /* TODO */ - printf("%c", fgetc(stdin)); - continue; - } // INSERT + case '~': { /* TODO */ continue; } // INSERT } case '3': switch ((c = fgetc(stdin))) { case '~': { lineDelete(&l); continue; } // DELETE @@ -126,7 +123,7 @@ static char *lineedit(void) { } } case '\x7F': { lineBackspace(&l); continue; } // BACKSPACE - default: { lineInsert(&l, c); } + default: { lineInsert(&l, c); continue; } } } diff --git a/src/parse.c b/src/parse.c index c5b09b5..b4de9b6 100644 --- a/src/parse.c +++ b/src/parse.c @@ -42,18 +42,18 @@ struct lex { char *s; size_t sp; }; struct arr { char **a; size_t al, ac; }; struct str { char *s; size_t sl, sc; }; -static struct lex l; - -static inline char *lex(void); +static inline char *lex(struct lex *l); static inline void spush(struct str *s, char c); /* Parse a string into an argument vector */ char **parse(char *s) { - struct arr a; a.al = 0; a.ac = 64; l.s = s; l.sp = 0; + struct lex l; l.s = s; l.sp = 0; + struct arr a; a.al = 0; a.ac = 64; + if (!(a.a = malloc(a.ac * sizeof (*a.a)))) { return NULL; } - for (char *s = lex(); s; s = lex()) { + for (char *s = lex(&l); s; s = lex(&l)) { // TODO dynamic expansion of a a.a[a.al] = s; ++a.al; } @@ -61,37 +61,36 @@ char **parse(char *s) { a.a[a.al] = NULL; return a.a; } -#define c l.s[l.sp] +#define c l->s[l->sp] /* Lex the next token from the string */ -static inline char *lex(void) { +static inline char *lex(struct lex *l) { struct str s; s.sl = 0; s.sc = 64; skip:; - for (; isspace(c); ++l.sp) {} if (c == 0) { return NULL; } - if (c == '#') { for (++l.sp; !(c == '\n'); ++l.sp) {} goto skip; } + for (; isspace(c); ++l->sp) {} if (!c) { return NULL; } + if (c == '#') { for (++l->sp; !(c == '\n'); ++l->sp) {} goto skip; } if (!(s.s = malloc(s.sc * sizeof (*s.s)))) { return NULL; } - goto word; for (;;) { ++l.sp; -word:; - // if (c == '\\') { ++l.sp; - // if (c == '\n') { continue; } - // else { spush(&s, '\\'); } - // } - // else if (c == '\'') for (++l.sp;;) { - // if (c == '\'') { break; } spush(&s, c); - // } - // else if (c == '\"') for (++l.sp;;) { - // if (c == '\\') { ++l.sp; - // if (c == '$' || c == '`' || c == '\"' || c == '\\'); - // else if (c == '\n') { continue; } - // else { spush(&s, '\\'); } spush(&s, c); - // } - // else if (c == '\"') { break; } spush(&s, c); - // } - if (isspace(c) || c == 0) { break; } - else { spush(&s, c); } + for (; c; ++l->sp) { + if (isspace(c)) { ++l->sp; break; } + + switch (c) { + case '\\': switch (l->s[++l->sp]) { + case '\n': { continue; } + default: { spush(&s, c); continue; } + } + case '\'': { // SINGLE QUOTES + for (++l->sp; c != '\''; ++l->sp) { spush(&s, c); } continue; + } + case '\"': { // DOUBLE QUOTES + for (++l->sp; c != '\"'; ++l->sp) { + /* TODO */ spush(&s, c); + } continue; + } + default: { spush(&s, c); continue; } + } } return s.s;