diff options
| author | Dominik Kaiser | 2025-01-20 20:14:32 +0100 |
|---|---|---|
| committer | GitHub | 2025-01-20 20:14:32 +0100 |
| commit | bd8c817797d5f2b1affe6957ffc51846a38e70ec (patch) | |
| tree | 2fc0f567b1c4f2f168a931ad0bff69e52c6c226c /src/interpreter.c | |
| parent | a9aba07b52cbf98eb9c52cd8ee0cd5f5021d2931 (diff) | |
| parent | dc6a4f2d0de92984c2584ef905011e2a60792850 (diff) | |
| download | minishell-bd8c817797d5f2b1affe6957ffc51846a38e70ec.tar.gz minishell-bd8c817797d5f2b1affe6957ffc51846a38e70ec.zip | |
Merge interpreter changes into main
Miau
Diffstat (limited to 'src/interpreter.c')
| -rw-r--r-- | src/interpreter.c | 79 |
1 files changed, 22 insertions, 57 deletions
diff --git a/src/interpreter.c b/src/interpreter.c index c6a4a00..9cd5292 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -5,86 +5,51 @@ /* +:+ +:+ +:+ */ /* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2024/08/05 13:15:24 by dkaiser #+# #+# */ -/* Updated: 2024/10/25 20:59:16 by chuhlig ### ########.fr */ +/* Created: 2024/12/17 19:15:49 by chuhlig #+# #+# */ +/* Updated: 2025/01/20 20:07:11 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "debug_tools.h" #include "minishell.h" -#include <stdlib.h> -#include <sys/_types/_pid_t.h> -#include <sys/cdefs.h> -#include <sys/wait.h> -#include <unistd.h> -static int eval_rec(t_node *node, t_env *env, int in_fd); +int eval_rec(t_node *node, t_env **env, int in_fd); -int eval(t_node *node, t_env *env) +int open_file(char *path, int flags, int mode) { - pid_t pid; - int result; + int fd; - if (node == NULL) - return (EXIT_FAILURE); - result = 0; - pid = fork(); - if (pid < 0) - { - return (EXIT_FAILURE); - } - if (pid == 0) - { - result = eval_rec(node, env, STDIN_FILENO); - exit(result); - } - else - { - waitpid(pid, &result, 0); - } - return (result); + fd = open(path, flags, mode); + if (fd < 0) + perror(path); + return (fd); } -static int eval_rec(t_node *node, t_env *env, int in_fd) +int eval_rec(t_node *node, t_env **env, int in_fd) { + int p[2]; pid_t pid; int result; - int p[2]; - result = pipe(p); - if (result == -1) - return (EXIT_FAILURE); if (node->type == PIPE_NODE) { + if (pipe(p) == -1) + return (perror("pipe"), EXIT_FAILURE); pid = fork(); - if (pid < 0) - { - return (EXIT_FAILURE); - } + if (pid == -1) + return (perror("fork"), close(p[0]), close(p[1]), EXIT_FAILURE); if (pid == 0) - { - close(p[0]); - dup2(in_fd, STDIN_FILENO); - dup2(p[1], STDOUT_FILENO); - result = execute_cmd(&node->content.pipe.left->content.cmd, env); - exit(result); - } - else - { - close(p[1]); - dup2(p[0], STDIN_FILENO); - result = eval_rec(node->content.pipe.right, env, p[0]); - } + handle_pipe_child(p, node, env, in_fd); + result = handle_pipe_parent(p, node, env); } else if (node->type == CMD_NODE) - { result = execute_cmd(&node->content.cmd, env); - } else - { - panic(UNREACHABLE); - return (EXIT_FAILURE); - } + result = EXIT_FAILURE; return (result); } +int eval(t_node *node, t_env **env) +{ + return (eval_rec(node, env, STDIN_FILENO)); +} |
