From 67718cbcda566127b8c5b38ecda83cbd469dbc3f Mon Sep 17 00:00:00 2001 From: dzwdz Date: Wed, 27 Jul 2022 20:02:50 +0200 Subject: user/shell: actual parsing, multiple argument support --- src/user/app/shell/parser.c | 64 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/user/app/shell/parser.c (limited to 'src/user/app/shell/parser.c') diff --git a/src/user/app/shell/parser.c b/src/user/app/shell/parser.c new file mode 100644 index 0000000..bd70350 --- /dev/null +++ b/src/user/app/shell/parser.c @@ -0,0 +1,64 @@ +#include "shell.h" +#include +#include + +static bool isspace(char c) { + return c == ' ' || c == '\t' || c == '\n'; +} + +static char skipspace(char **sp) { + char *s = *sp; + while (*s && isspace(*s)) s++; + *sp = s; + return *s; +} + +static char *parg(char **sp) { + char *s = *sp; + char *res = NULL; + + if (skipspace(&s)) { + switch (*s) { + case '"': + s++; + res = s; + while (*s && *s != '"') + s++; + break; + default: + res = s; + while (*s && !isspace(*s) && *s != '>') + s++; + break; + } + if (*s) *s++ = '\0'; + } + + *sp = s; + return res; +} + +int parse(char *s, char **argv, size_t argvlen, char **redir) { + if (argvlen == 0) return -1; + size_t argc = 0; + char *arg; + + *argv = NULL; + *redir = NULL; + + while (skipspace(&s)) { + switch (*s) { + case '>': + s++; + *redir = parg(&s); + break; + default: + arg = parg(&s); + argv[argc++] = arg; + if (argc >= argvlen) + return -1; + } + } + argv[argc] = NULL; + return argc; +} -- cgit v1.2.3