aboutsummaryrefslogtreecommitdiff
path: root/src/interpreter.c
diff options
context:
space:
mode:
authorDominik Kaiser2025-01-20 18:46:39 +0100
committerGitHub2025-01-20 18:46:39 +0100
commit9fa887da20e409c2f25fac44b57f999e508a30ea (patch)
tree7e3fee3edea18d0d81abddb89104858cfc0ec5e3 /src/interpreter.c
parentaf9d1a9b39daaf1b86cf94ee629e06503d8ab6d4 (diff)
parent62845ce01f222fad126372c574c7ab084478adf0 (diff)
downloadminishell-9fa887da20e409c2f25fac44b57f999e508a30ea.tar.gz
minishell-9fa887da20e409c2f25fac44b57f999e508a30ea.zip
Merge pull request #30 from dpu-kaiser/merge_format_redir
Merge format redir
Diffstat (limited to 'src/interpreter.c')
-rw-r--r--src/interpreter.c89
1 files changed, 15 insertions, 74 deletions
diff --git a/src/interpreter.c b/src/interpreter.c
index c7fe67c..f31e965 100644
--- a/src/interpreter.c
+++ b/src/interpreter.c
@@ -6,100 +6,41 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/17 19:15:49 by chuhlig #+# #+# */
-/* Updated: 2025/01/16 18:44:39 by dkaiser ### ########.fr */
+/* Updated: 2025/01/20 12:48:49 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
#include "debug_tools.h"
#include "minishell.h"
-int handle_redirections(t_redirection *redirs)
+int eval_rec(t_node *node, t_env **env, int in_fd);
+
+int open_file(char *path, int flags, int mode)
{
int fd;
- if (redirs[0].type == INPUT_FILE)
- {
- fd = open(redirs[0].specifier, O_RDONLY);
- if (fd < 0)
- {
- perror("open");
- return (-1);
- }
- dup2(fd, STDIN_FILENO);
- close(fd);
- }
- else if (redirs[0].type == INPUT_LIMITER)
- {
- fd = open("/tmp/heredoc_tmp", O_WRONLY | O_TRUNC, 0644);
- if (fd < 0)
- {
- perror("open");
- return (-1);
- }
- write(fd, redirs[0].specifier, ft_strlen(redirs[0].specifier));
- close(fd);
- fd = open("/tmp/heredoc_tmp", O_RDONLY);
- if (fd < 0)
- {
- perror("open");
- return (-1);
- }
- dup2(fd, STDIN_FILENO);
- close(fd);
- }
- if (redirs[1].type == OUTPUT_OVERRIDE)
- {
- fd = open(redirs[1].specifier, O_WRONLY | O_TRUNC, 0644);
- if (fd < 0)
- {
- perror("open");
- return (-1);
- }
- dup2(fd, STDOUT_FILENO);
- close(fd);
- }
- else if (redirs[1].type == OUTPUT_APPEND)
- {
- fd = open(redirs[1].specifier, O_WRONLY | O_APPEND, 0644);
- if (fd < 0)
- {
- perror("open");
- return (-1);
- }
- dup2(fd, STDOUT_FILENO);
- close(fd);
- }
- return (0);
+ fd = open(path, flags, mode);
+ if (fd < 0)
+ perror("open");
+ return (fd);
}
int eval_rec(t_node *node, t_env **env, int in_fd)
{
- pid_t pid;
int p[2];
+ pid_t pid;
int result;
- int original_stdin;
if (node->type == PIPE_NODE)
{
- pipe(p);
+ if (pipe(p) == -1)
+ return (perror("pipe"), EXIT_FAILURE);
pid = fork();
+ 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 = eval_rec(node->content.pipe.left, env, in_fd);
- exit(result);
- }
- else
- {
- close(p[1]);
- original_stdin = dup(STDIN_FILENO);
- dup2(p[0], STDIN_FILENO);
- result = eval_rec(node->content.pipe.right, env, p[0]);
- dup2(original_stdin, STDIN_FILENO);
- close(original_stdin);
- }
+ 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);