diff options
author | Ivan Davydov <lotigara@lotigara.ru> | 2025-06-29 00:08:38 +0300 |
---|---|---|
committer | Ivan Davydov <lotigara@lotigara.ru> | 2025-06-29 00:08:38 +0300 |
commit | 11714ed6c9adc5b6f90bffa1b23d5bb5b2e20bd5 (patch) | |
tree | 9d1831871da0f1906f01cade4440206b6f0f9b84 /acccre-cmdparser.c | |
parent | 2cc696105d9f3b8785d2a903211e8adbd707a3e3 (diff) |
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; +} |