diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2022-12-14 18:26:49 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2022-12-14 18:26:49 +0100 |
| commit | d179f1846f9372920ef02f08cfb4d3abe99b383f (patch) | |
| tree | 4b6ddc71566be95a1ce520ef9d957f4cac1af6c9 /smash/parser.c | |
first commit
Diffstat (limited to 'smash/parser.c')
| -rw-r--r-- | smash/parser.c | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/smash/parser.c b/smash/parser.c new file mode 100644 index 0000000..33fb410 --- /dev/null +++ b/smash/parser.c @@ -0,0 +1,76 @@ +#include "parser.h" + +#include "../lib/malloc/malloc.h" +#include "../lib/cstr/cstr.h" + +#include "../lib/sys/write.h" + +char **__new_argv(char *exp); +void __free_argv(char **argv); + +expression_list_t *new_expression_from_line(char *line) +{ + + u64 n = cstr_split(line, '|'); + char *current = line; + char *next = 0; + + + for (; n > 0; --n) { + next = (char*)next_split(current); + + strip_cstr(current, ' '); + + write(0, current, cstr_length(current)); + write(0, "\\n\n", 3); + + current = next; + } + return 0; +} + +char **__new_argv(char *exp) +{ + u64 n = cstr_split(exp, ' '); + char **argv = malloc(sizeof(char*) * n + 1); + u64 i = 0; + char *current = exp; + char *next; + + for (; n > 0; --n) { + next = (char*)next_split(current); + strip_cstr(current, ' '); + + if (cstr_length(current)) + argv[i++] = current; + + current = next; + } + + argv[i] = 0; + + return argv; +} + + +void __free_argv(char **argv) +{ + free(argv); +} + +#ifdef PARSER_UNIT_TEST + +#include "../lib/sys/execve.h" + +int main() { + const char exp[] = "/bin/ls -a -l -h"; + char **argv = __new_argv(exp); + char **p = argv; + char *const envp[] = { 0 }; + + execve(argv[0], argv, envp); + + free(argv); +} + +#endif |