/* 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;
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);
}
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);
}