]> git.dkaiser.de - 42/minishell.git/commitdiff
Add pipe basics
authorDominik Kaiser <dkaiser@1-E-11.42heilbronn.de>
Tue, 22 Oct 2024 13:42:28 +0000 (15:42 +0200)
committerDominik Kaiser <dkaiser@1-E-11.42heilbronn.de>
Tue, 22 Oct 2024 13:42:28 +0000 (15:42 +0200)
TODO: Add actual piping

src/execute_cmd.c
src/interpreter.c

index 3730d4b02eb93a7d98f5ba1c0fcc1230e3fe3f6e..33ba11cd154abab3a14b3e07a6bf90fef76ab3df 100644 (file)
@@ -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       */
 /*                                                                            */
 /* ************************************************************************** */
 
 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);
 }
index 10826447cd7614781754ef55e1cafb06fb2ae981..f6757c422b64913e1f9f1e7959d7937c5b56ca17 100644 (file)
@@ -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);
 }