Rename t_execution_info, add its definition
authorLukas Jiriste <ljiriste@student.42prague.com>
Sun, 21 Jul 2024 11:03:31 +0000 (13:03 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Sun, 21 Jul 2024 11:03:31 +0000 (13:03 +0200)
inc/minishell.h
src/execution.c

index a22dd23e58c75c6c2559a8e1cba14a7eae289a81..a5446b54fe804dd8cbd5acbaf964d731aed04d59 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/02 13:22:57 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/06/23 18:24:04 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/07/21 13:00:33 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -21,6 +21,21 @@ typedef struct s_vars
        t_vec   other;
 }                      t_vars;
 
+typedef struct s_redirection
+{
+       int     from_fd;
+       int     to_fd;
+}              t_redirection;
+
+typedef struct s_execution_env
+{
+       int             stdin_fd;
+       int             stdout_fd;
+       int             ret_val;
+       t_vars  *env_vars;
+       t_vec   redirections;
+}                      t_execution_env;
+
 typedef t_parse_tree_node      t_tree;
 
 int            init_vars(t_vars *vars, char **envp);
index 2b73dd97023f05dee889a301d3c2c9c717b69a6d..cb47adf329f4c9dfde5f90c6522d49d28aaeba4b 100644 (file)
@@ -6,13 +6,13 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/07/21 08:57:54 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/07/21 13:02:19 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/07/21 13:03:02 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "minishell.h"
 
-int    ex_simple_command(t_parse_tree_node *simple_command, t_execution_info *info)
+int    ex_simple_command(t_parse_tree_node *simple_command, t_execution_env *env)
 {
        t_vec   redirections;
        t_vec   assignments;
@@ -20,7 +20,7 @@ int   ex_simple_command(t_parse_tree_node *simple_command, t_execution_info *info)
 
        save_redirections(&redirections, simple_command);
        save_assignments(&assignments, simple_command);
-       fields = expand(simple_command, info, &redirections, &assignments);
+       fields = expand(simple_command, env, &redirections, &assignments);
        if (!fields)
        {
                ft_vec_free(&redirections, void_free_redirection);
@@ -29,84 +29,84 @@ int ex_simple_command(t_parse_tree_node *simple_command, t_execution_info *info)
        }
        if (!*fields)
        {
-               save_assignments(&assignments, info);
+               save_assignments(&assignments, env);
                ex_redirections(&redirections);
                return (0);
        }
-       ex_fields(fields, &assignments, &redirections, info);
+       ex_fields(fields, &assignments, &redirections, env);
        ft_vec_free(&redirections, void_free_redirection);
        ft_vec_free(&assignments, void_free_assignments);
        free_fields(fields);
        return (0);
 }
 
-int    subshell(t_parse_tree_node *program, const t_execution_info *info)
+int    subshell(t_parse_tree_node *program, const t_execution_env *env)
 {
        int                                     res;
-       t_execution_info        info_copy;
+       t_execution_env env_copy;
 
-       if (copy_info(&info_copy, info))
+       if (copy_env(&env_copy, env))
                return (1);
-       res = ex_program(program, &info_copy);
-       info->ret_val = info_copy->ret_val;
-       free_info(&info_copy);
+       res = ex_program(program, &env_copy);
+       env->ret_val = env_copy->ret_val;
+       free_env(&env_copy);
        return (res);
 }
 
-int    ex_command(t_parse_tree_node *command, t_execution_info *info)
+int    ex_command(t_parse_tree_node *command, t_execution_env *env)
 {
        if (is_token_type(ft_vec_caccess(&command->children, 0), "simple_command"))
-               return (ex_simple_command(ft_vec_access(&command->children, 0), info));
+               return (ex_simple_command(ft_vec_access(&command->children, 0), env));
        else
        {
                comp_command = ft_vec_access(&command->children, 0);
-               return (subshell(ft_vec_access(&comp_command->children, 1), info));
+               return (subshell(ft_vec_access(&comp_command->children, 1), env));
        }
 }
 
-int    ex_pipeline(t_parse_tree_node *pipeline, t_execution_info *info)
+int    ex_pipeline(t_parse_tree_node *pipeline, t_execution_env *env)
 {
        if (pipeline->children.size == 1)
-               return (ex_command(ft_vec_access(&pipeline->children, 0), info));
+               return (ex_command(ft_vec_access(&pipeline->children, 0), env));
        else
        {
                if (pipe(pipe_fds))
                        return (1);
-               ft_swap_int(&pipe_fds[1], &info->stdout_fd);
-               ex_pipeline(ft_vec_access(&pipeline->children, 0), info);
-               ft_swap_int(&pipe_fds[1], &info->stdout_fd);
-               ft_swap_int(&pipe_fds[0], &info->stdin_fd);
-               ex_command(ft_vec_access(&pipeline->children, 2), info);
-               ft_swap_int(&pipe_fds[0], &info->stdin_fd);
+               ft_swap_int(&pipe_fds[1], &env->stdout_fd);
+               ex_pipeline(ft_vec_access(&pipeline->children, 0), env);
+               ft_swap_int(&pipe_fds[1], &env->stdout_fd);
+               ft_swap_int(&pipe_fds[0], &env->stdin_fd);
+               ex_command(ft_vec_access(&pipeline->children, 2), env);
+               ft_swap_int(&pipe_fds[0], &env->stdin_fd);
                close(pipe_fds[0]);
                close(pipe_fds[1]);
        }
        return (0);
 }
 
-int    ex_program(t_parse_tree_node *program, t_execution_info *info)
+int    ex_program(t_parse_tree_node *program, t_execution_env *env)
 {
        if (program->children.size == 1)
-               ex_pipeline(ft_vec_access(&program->children, 0), info);
+               ex_pipeline(ft_vec_access(&program->children, 0), env);
        else if (is_token_type(ft_vec_caccess(&program->children, 1), "AND_IF"))
        {
-               ex_program(ft_vec_access(&program->children, 0), info);
-               if (info->ret_val == 0)
-                       ex_pipeline(ft_vec_access(&program->children, 2), info);
+               ex_program(ft_vec_access(&program->children, 0), env);
+               if (env->ret_val == 0)
+                       ex_pipeline(ft_vec_access(&program->children, 2), env);
        }
        else
        {
-               ex_program(ft_vec_access(&program->children, 0), info);
-               if (info->ret_val != 0)
-                       ex_pipeline(ft_vec_access(&program->children, 2), info);
+               ex_program(ft_vec_access(&program->children, 0), env);
+               if (env->ret_val != 0)
+                       ex_pipeline(ft_vec_access(&program->children, 2), env);
        }
 }
 
 int    execute(t_tree *parse_tree, t_vars *vars)
 {
-       t_execution_info        info;
+       t_execution_env env;
 
-       construct_info(&info, vars);
-       return (ex_program(parse_tree, &info));
-       free_info(&info);
+       construct_env(&env, vars);
+       return (ex_program(parse_tree, &env));
+       free_env(&env);
 }