diff options
| author | Dominik Kaiser | 2024-10-22 15:42:28 +0200 |
|---|---|---|
| committer | Dominik Kaiser | 2024-10-22 15:42:28 +0200 |
| commit | 0a5684e637cd6ec084575ddf9930cfde55d3cca7 (patch) | |
| tree | ce3fe9bcdde13b2fbe85ae44335876a3c9b325b5 | |
| parent | b23715a8077cff0241b721889f17b3c3665d38f1 (diff) | |
| download | minishell-0a5684e637cd6ec084575ddf9930cfde55d3cca7.tar.gz minishell-0a5684e637cd6ec084575ddf9930cfde55d3cca7.zip | |
Add pipe basics
TODO: Add actual piping
| -rw-r--r-- | src/execute_cmd.c | 21 | ||||
| -rw-r--r-- | src/interpreter.c | 79 |
2 files changed, 58 insertions, 42 deletions
diff --git a/src/execute_cmd.c b/src/execute_cmd.c index 3730d4b..33ba11c 100644 --- a/src/execute_cmd.c +++ b/src/execute_cmd.c @@ -6,7 +6,7 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/21 13:58:56 by dkaiser #+# #+# */ -/* Updated: 2024/10/21 15:07:37 by dkaiser ### ########.fr */ +/* Updated: 2024/10/22 15:42:17 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,21 +18,10 @@ int execute_cmd(t_cmd *cmd, t_env *env) { int result; - pid_t pid; + char *cmd_path; - pid = fork(); - if (pid < 0) - return (EXIT_FAILURE); - if (pid == 0) - { - result = execve(cmd->args[0], cmd->args, env_to_strlst(env)); - exit(result); - } - else - { - // only wait if cmd is on rightmost side of pipe? - // so in cmd1 | cmd2 | cmd3 | cmd4 only wait for cmd4 - waitpid(pid, &result, 0); - } + cmd_path = get_cmd_path(cmd->args[0], env); + cmd->args[0] = cmd_path; + result = execve(cmd->args[0], cmd->args, env_to_strlst(env)); return (result); } diff --git a/src/interpreter.c b/src/interpreter.c index 1082644..f6757c4 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -6,46 +6,73 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/05 13:15:24 by dkaiser #+# #+# */ -/* Updated: 2024/10/21 15:07:27 by dkaiser ### ########.fr */ +/* Updated: 2024/10/22 15:42:07 by dkaiser ### ########.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_pipe(t_pipe *pipe, t_env *env); -static int eval_cmd(t_cmd *cmd, t_env *env); +int eval_rec(t_node *node, t_env *env); int eval(t_node *node, t_env *env) { - if (node->type == PIPE_NODE) - return (eval_pipe(&node->content.pipe, env)); - else if (node->type == CMD_NODE) - return (eval_cmd(&node->content.cmd, env)); + pid_t pid; + int result; + + result = 0; + pid = fork(); + if (pid < 0) + { + return (EXIT_FAILURE); + } + if (pid == 0) + { + result = eval_rec(node, env); + exit(result); + } else { - panic(UNREACHABLE); - return (-1); + waitpid(pid, &result, 0); } + return (result); } -static int eval_pipe(t_pipe *pipe, t_env *env) -{ - dbg("TODO: PIPE"); - eval_cmd(&pipe->left->content.cmd, env); - eval_cmd(&pipe->right->content.cmd, env); - return (0); -} - -static int eval_cmd(t_cmd *cmd, t_env *env) +int eval_rec(t_node *node, t_env *env) { - char *cmd_path; + pid_t pid; + int result; - cmd_path = get_cmd_path(cmd->args[0], env); - if (cmd_path == NULL) - return (1); - free(cmd->args[0]); - cmd->args[0] = cmd_path; - execute_cmd(cmd, env); - return (0); + if (node->type == PIPE_NODE) + { + pid = fork(); + if (pid < 0) + { + return (EXIT_FAILURE); + } + if (pid == 0) + { + result = execute_cmd(&node->content.pipe.left->content.cmd, env); + exit(result); + } + else + { + result = eval(node->content.pipe.right, env); + } + } + else if (node->type == CMD_NODE) + { + result = execute_cmd(&node->content.cmd, env); + } + else + { + panic(UNREACHABLE); + return (EXIT_FAILURE); + } + return (result); } |
