aboutsummaryrefslogtreecommitdiff
path: root/src/interpreter.c
diff options
context:
space:
mode:
authorDominik Kaiser2025-01-14 16:40:30 +0100
committerGitHub2025-01-14 16:40:30 +0100
commit398b0d39cbbe2cdabbfae00f799181a37754d5c1 (patch)
tree397ae613dd39a35a6c91e7e30426f86ade4e24bb /src/interpreter.c
parent00ad7429f223c85e99da6ffa8f7dade0c73c97b5 (diff)
parent553204e584dd08987902c7693e47744192e6bd85 (diff)
downloadminishell-398b0d39cbbe2cdabbfae00f799181a37754d5c1.tar.gz
minishell-398b0d39cbbe2cdabbfae00f799181a37754d5c1.zip
Merge branch 'main' into echo-builtint
Diffstat (limited to 'src/interpreter.c')
-rw-r--r--src/interpreter.c85
1 files changed, 68 insertions, 17 deletions
diff --git a/src/interpreter.c b/src/interpreter.c
index 2a09e6d..c6a4a00 100644
--- a/src/interpreter.c
+++ b/src/interpreter.c
@@ -3,37 +3,88 @@
/* ::: :::::::: */
/* interpreter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
-/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/05 13:15:24 by dkaiser #+# #+# */
-/* Updated: 2024/08/05 13:33:16 by dkaiser ### ########.fr */
+/* Updated: 2024/10/25 20:59:16 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_pipe(t_pipe *pipe);
-static int eval_cmd(t_cmd *cmd);
+static int eval_rec(t_node *node, t_env *env, int in_fd);
-int eval(t_node *node)
+int eval(t_node *node, t_env *env)
{
- if (node->type == PIPE_NODE)
- return (eval_pipe(&node->content.pipe));
- else if (node->type == CMD_NODE)
- return (eval_cmd(&node->content.cmd));
+ pid_t pid;
+ int result;
+
+ 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
{
- panic(UNREACHABLE);
- return (-1);
+ waitpid(pid, &result, 0);
}
+ return (result);
}
-static int eval_pipe(t_pipe *pipe)
+static int eval_rec(t_node *node, t_env *env, int in_fd)
{
- return (0);
-}
+ pid_t pid;
+ int result;
+ int p[2];
-static int eval_cmd(t_cmd *cmd)
-{
- return (0);
+ result = pipe(p);
+ if (result == -1)
+ return (EXIT_FAILURE);
+ if (node->type == PIPE_NODE)
+ {
+ pid = fork();
+ if (pid < 0)
+ {
+ return (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]);
+ }
+ }
+ else if (node->type == CMD_NODE)
+ {
+ result = execute_cmd(&node->content.cmd, env);
+ }
+ else
+ {
+ panic(UNREACHABLE);
+ return (EXIT_FAILURE);
+ }
+ return (result);
}
+