Author | Jamozed <[email protected]> |
Date | 2021-03-31 11:57:02 |
Commit | 0f6aa82e55e57d88314704fb7c1ce079b8176a7a |
Parent | 89d88c31e4685e1dedbcb1d4ffcb123f00febf52 |
Implement semicolon handling
Diffstat
M | src/main.c | | | 15 | ++++++++------- |
M | src/parse.c | | | 21 | ++++++++++++++------- |
M | src/parse.h | | | 2 | +- |
3 files changed, 23 insertions, 15 deletions
diff --git a/src/main.c b/src/main.c index 27df818..364fa63 100644 --- a/src/main.c +++ b/src/main.c @@ -62,13 +62,14 @@ int main(int ac, char *av[]) { (void)(ac); char *line = lineread(); fputc('\n', stdout); if (!line) { if (errno) { warn("%s", serr()); } break; } - char **args = parse(line); free(line); - if (!args) { if (errno) { warn("%s", serr()); } goto end; } - - _ret = execute(args); - -end:; - for (size_t i = 0; args[i]; ++i) { free(args[i]); } free(args); + for (char *e = line; *e;) { + char **args = parse(e, &e); + if (!args) { if (errno) { warn("%s", serr()); } break; } + + _ret = execute(args); + + for (size_t i = 0; args[i]; ++i) { free(args[i]); } free(args); + } free(line); } while (_loop); return warned; diff --git a/src/parse.c b/src/parse.c index b4de9b6..2a676a2 100644 --- a/src/parse.c +++ b/src/parse.c @@ -44,21 +44,19 @@ struct str { char *s; size_t sl, sc; }; static inline char *lex(struct lex *l); +static inline void apush(struct arr *a, char *s); static inline void spush(struct str *s, char c); /* Parse a string into an argument vector */ -char **parse(char *s) { +char **parse(char *s, char **e) { 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(&l); s; s = lex(&l)) { - // TODO dynamic expansion of a - a.a[a.al] = s; ++a.al; - } + for (char *s = lex(&l); s; s = lex(&l)) { apush(&a, s); } - a.a[a.al] = NULL; return a.a; + a.a[a.al] = NULL; *e = s + l.sp; return a.a; } #define c l->s[l->sp] @@ -69,11 +67,13 @@ static inline char *lex(struct lex *l) { skip:; for (; isspace(c); ++l->sp) {} if (!c) { return NULL; } + + if (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; ++l->sp) { + for (; c && c != ';'; ++l->sp) { if (isspace(c)) { ++l->sp; break; } switch (c) { @@ -98,6 +98,13 @@ skip:; #undef c // l.s[l.sp] +/* Push a string to the end of an array */ +static inline void apush(struct arr *a, char *s) { + if (a->al + 1 > a->ac) { a->ac *= 2; + a->a = realloc(a->a, a->ac * sizeof (*a->a)); + } a->a[a->al] = s; a->a[++a->al] = 0; return; +} + /* Push a character to the end of a string */ static inline void spush(struct str *s, char c) { if (s->sl + 1 > s->sc) { s->sc *= 2; diff --git a/src/parse.h b/src/parse.h index 18202b8..fa26606 100644 --- a/src/parse.h +++ b/src/parse.h @@ -33,6 +33,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. #ifndef OMKOV_OSH_PARSE_H_G9TJ04KR #define OMKOV_OSH_PARSE_H_G9TJ04KR -extern char **parse(char *s); +extern char **parse(char *s, char **e); #endif // OMKOV_OSH_PARSE_H_G9TJ04KR