diff options
Diffstat (limited to 'acccre-cmdparser.c')
-rw-r--r-- | acccre-cmdparser.c | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/acccre-cmdparser.c b/acccre-cmdparser.c new file mode 100644 index 0000000..4212296 --- /dev/null +++ b/acccre-cmdparser.c @@ -0,0 +1,62 @@ +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +/* TODO */ +static int run_cmd(const char *cmd, int argc, char *argv[]) { + puts(cmd); + for (int i = 0; i < argc; i++) { + puts(argv[i]); + } + + return 0; +} + +/* Tricks with pointers */ +int parse(char *str) { + char *cmd = str; + str += strcspn(str, separator); + if (*str) { + *str++ = 0; + } + + char *str_counter = str; + int i = 0; + for (; *str_counter; i++) { + str_counter += strspn(str, separator); + if (!*str_counter) { + break; + } + *str_counter++ = 0; + + str_counter += strcspn(str_counter, separator); + if(!str) { + return EINVAL; + } + *str++ = 0; + } + + char *args[i]; + for (int j = 0; *str; j++) { + str += strspn(str, separator); + if (!*str) { + break; + } + *str++ = 0; + + args[j] = str; + str += strcspn(str, separator); + if(!str) { + return EINVAL; + } + *str++ = 0; + + /* TODO: parse the string left to an array of arguments and pass command + * itself and arguments to the run_cmd() right after end of this loop + */ + } + + int response = run_cmd(cmd, i, args); + return response; +} |