aboutsummaryrefslogtreecommitdiff
path: root/smash/parser.c
diff options
context:
space:
mode:
authorNathan P. Reiner <nathan@nathanreiner.xyz>2022-12-15 18:20:33 +0100
committerNathan P. Reiner <nathan@nathanreiner.xyz>2022-12-15 18:20:33 +0100
commit828dd435725ea315abd2ea9875325ee3b17041a9 (patch)
tree72d80411d5cecc8758fc87867521374e90caa44d /smash/parser.c
parent7536d000ac9a5188378f2749ecfd7f0ccb437573 (diff)
did this while lecture (builtins, parsing, exec and env by stdlib)
Diffstat (limited to 'smash/parser.c')
-rw-r--r--smash/parser.c65
1 files changed, 5 insertions, 60 deletions
diff --git a/smash/parser.c b/smash/parser.c
index 22b9ccf..55a6fd2 100644
--- a/smash/parser.c
+++ b/smash/parser.c
@@ -52,14 +52,16 @@ void free_expression(expression_list_t *expression)
}
-char **__new_argv(char *exp)
+char **new_argv(char *exp)
{
u64 n = cstr_split(exp, ' ');
- char **argv = malloc(sizeof(char*) * n + 1);
+ char **argv = malloc(sizeof(char*) * (n + 1));
u64 i = 0;
char *current = exp;
char *next;
+ argv[n] = 0;
+
for (; n > 0; --n) {
next = (char*)next_split(current);
strip_cstr(current, ' ');
@@ -70,68 +72,11 @@ char **__new_argv(char *exp)
current = next;
}
- argv[i] = 0;
-
return argv;
}
-void __free_argv(char **argv)
+void free_argv(char **argv)
{
free(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() {
- 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 };
-
- int pid[2];
- int pipefd[2];
-
- 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