]> git.dkaiser.de - 42/minishell.git/commitdiff
restored merge conflict error"
authorChristopher Uhlig <chuhlig@1-D-6.42heilbronn.de>
Fri, 25 Oct 2024 19:00:39 +0000 (21:00 +0200)
committerChristopher Uhlig <chuhlig@1-D-6.42heilbronn.de>
Fri, 25 Oct 2024 19:00:39 +0000 (21:00 +0200)
"

src/execute_cmd.c
src/interpreter.c

index fa7677f3463d709ef13b8b88e2953438560021c3..6386fc03bcc95d6aaabd35cf3bd24c1d9a804298 100644 (file)
@@ -3,10 +3,10 @@
 /*                                                        :::      ::::::::   */
 /*   execute_cmd.c                                      :+:      :+:    :+:   */
 /*                                                    +:+ +:+         +:+     */
-/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/10/21 13:58:56 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/10/25 13:31:16 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/10/25 20:59:22 by chuhlig          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -57,3 +57,20 @@ int  execute_cmd(t_cmd *cmd, t_env *env)
        result = execve(cmd->args[0], cmd->args, env_to_strlst(env));
        return (result);
 }
+
+static int     eval_cmd(t_cmd *cmd, t_env **env)
+{
+       if (ft_strncmp(cmd->args[0], "pwd", 4) == 0)
+               pwd(*env);
+       else if (ft_strncmp(cmd->args[0], "echo", 5) == 0)
+               echo(cmd->args);
+       else if (ft_strncmp(cmd->args[0], "env", 4) == 0)
+               ft_env(*env);
+       else if (ft_strncmp(cmd->args[0], "cd", 3) == 0)
+               cd(env, cmd->args);
+       else if (ft_strncmp(cmd->args[0], "unset", 6) == 0)
+               unset(cmd->args, env);
+       else if (ft_strncmp(cmd->args[0], "export", 7) == 0)
+               export(cmd->args, env);
+       return (0);
+}
\ No newline at end of file
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..c6a4a0074a3be6e281937ce26541dabe7b747c97 100644 (file)
@@ -0,0 +1,90 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   interpreter.c                                      :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   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       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#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(t_node *node, t_env *env)
+{
+       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
+       {
+               waitpid(pid, &result, 0);
+       }
+       return (result);
+}
+
+static int     eval_rec(t_node *node, t_env *env, int in_fd)
+{
+       pid_t   pid;
+       int             result;
+       int             p[2];
+
+       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);
+}
+