From 7536d000ac9a5188378f2749ecfd7f0ccb437573 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Wed, 14 Dec 2022 22:12:20 +0100 Subject: first try to pipe stuff --- smash/Makefile | 2 +- smash/parser.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++------- smash/parser.h | 7 +---- smash/test | Bin 0 -> 26656 bytes 4 files changed, 73 insertions(+), 17 deletions(-) create mode 100755 smash/test (limited to 'smash') diff --git a/smash/Makefile b/smash/Makefile index 3f684cd..4deb244 100644 --- a/smash/Makefile +++ b/smash/Makefile @@ -1,5 +1,5 @@ unit_test_parser: - gcc parser.c ../lib/cstr/cstr.c ../lib/malloc/malloc.c ../lib/sys/start.S -static -nostdlib -fno-stack-protector -o test -DPARSER_UNIT_TEST -g + gcc parser.c ../lib/cstr/cstr.c ../lib/malloc/malloc.c ../lib/sys/start.S -static -nostdlib -fno-stack-protector -Wno-implicit-function-declaration -o test -DPARSER_UNIT_TEST -g ./test unit_test_variables: diff --git a/smash/parser.c b/smash/parser.c index 33fb410..22b9ccf 100644 --- a/smash/parser.c +++ b/smash/parser.c @@ -14,21 +14,44 @@ expression_list_t *new_expression_from_line(char *line) u64 n = cstr_split(line, '|'); char *current = line; char *next = 0; - + expression_list_t *head = 0; + expression_list_t *expression = 0; for (; n > 0; --n) { next = (char*)next_split(current); strip_cstr(current, ' '); - write(0, current, cstr_length(current)); - write(0, "\\n\n", 3); + if (!expression) { + expression = malloc(sizeof(expression_list_t)); + head = expression; + } else { + expression->next = malloc(sizeof(expression_list_t)); + expression = expression->next; + } + + expression->call = current; + expression->next = 0; current = next; } - return 0; + + return head; } + +void free_expression(expression_list_t *expression) +{ + expression_list_t *next; + + while (expression) { + next = expression->next; + free(expression); + expression = next; + } +} + + char **__new_argv(char *exp) { u64 n = cstr_split(exp, ' '); @@ -61,16 +84,54 @@ void __free_argv(char **argv) #ifdef PARSER_UNIT_TEST #include "../lib/sys/execve.h" +#include "../lib/sys/fork.h" +#include "../lib/sys/wait4.h" +#include "../lib/sys/pipe.h" +#include "../lib/sys/dup2.h" +#include "../lib/sys/close.h" +#include "../lib/sys/io.h" +#include "../lib/sys/exit.h" int main() { - const char exp[] = "/bin/ls -a -l -h"; - char **argv = __new_argv(exp); - char **p = argv; - char *const envp[] = { 0 }; + char in[] = "/bin/ls -a | /bin/wc -l"; + expression_list_t *exps = new_expression_from_line(in); + expression_list_t *exp = exps; + char **envp = { 0 }; - execve(argv[0], argv, envp); + int pid[2]; + int pipefd[2]; - free(argv); + char **argv1 = __new_argv(exp->call); + char **argv2 = __new_argv(exp->next->call); + + pipe(pipefd); + pid[0] = fork(); + + if (pid[0] == 0) { + close(pipefd[0]); + dup2(pipefd[1], STDOUT_FD); + close(pipefd[1]); + + execve(argv1[0], argv1, envp); + } else { + pid[1] = fork(); + + + close(pipefd[1]); + + if (pid[1] == 0) { + dup2(pipefd[0], STDIN_FD); + close(pipefd[0]); + + execve(argv2[0], argv2, envp); + } else { + close(pipefd[0]); + wait4(pid[0], 0, 0); + wait4(pid[1], 0, 0); + } + } + + return 0; } #endif diff --git a/smash/parser.h b/smash/parser.h index 9c23e0f..23eee88 100644 --- a/smash/parser.h +++ b/smash/parser.h @@ -3,13 +3,8 @@ #include "../lib/sys/sizes.h" -typedef struct { - const char *head; - const char **args; -} expression_t; - typedef struct expression_list_t__ { - expression_t expression; + char *call; struct expression_list_t__ *next; } expression_list_t; diff --git a/smash/test b/smash/test new file mode 100755 index 0000000..a76addd Binary files /dev/null and b/smash/test differ -- cgit v1.2.3-70-g09d2