/* ::: :::::::: */
/* execution.c :+: :+: :+: */
/* +:+ +:+ +:+ */
-/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
-/* Created: 2024/05/03 15:59:58 by ljiriste #+# #+# */
-/* Updated: 2024/06/21 17:00:46 by ljiriste ### ########.fr */
+/* Created: 2024/07/21 08:57:54 by ljiriste #+# #+# */
+/* Updated: 2024/07/21 09:10:19 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
-int execute(__attribute__((unused))t_tree *parse_tree, __attribute__((unused))t_vars *vars)
+int ex_simple_command(t_parse_tree_node *simple_command, t_execution_info *info)
{
- ft_printf("Function execute has to be implemented.\n");
+ t_vec redirections;
+ t_vec assignments;
+ char **fields;
+
+ save_redirections(&redirections, simple_command);
+ save_assignments(&assignments, simple_command);
+ fields = expand(simple_command, info, &redirections, &assignments);
+ if (!fields)
+ {
+ ft_vec_free(&redirections, void_free_redirection);
+ ft_vec_free(&assignments, void_free_assignments);
+ return (1);
+ }
+ if (!*fields)
+ {
+ save_assignments(&assignments, info);
+ return (0);
+ }
+ ex_fields(fields, assignments, redirections, info);
+ ft_vec_free(&redirections, void_free_redirection);
+ ft_vec_free(&assignments, void_free_assignments);
+ return (0);
+}
+
+int ex_command(t_parse_tree_node *command, t_execution_info *info)
+{
+ if (is_token_type(ft_vec_caccess(&command->children, 0), "simple_command"))
+ return (ex_simple_command(ft_vec_access(&command->children, 0), info));
+ else
+ {
+ comp_command = ft_vec_access(&command->children, 0);
+ info_copy = copy_info(info);
+ if (!info_copy)
+ return (1);
+ res = ex_program(ft_vec_access(&comp_command->children, 1), info_copy);
+ free_info(info_copy);
+ return (res);
+ }
+}
+
+int ex_pipeline(t_parse_tree_node *pipeline, t_execution_info *info)
+{
+ if (pipeline->children.size == 1)
+ return (ex_command(ft_vec_access(&pipeline->children, 0), info));
+ else
+ {
+ if (pipe(pipe_fds))
+ return (1);
+ ft_swap_fd(&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);
+ close(pipe_fds[0]);
+ close(pipe_fds[1]);
+ }
return (0);
}
+
+int ex_program(t_parse_tree_node *program, t_execution_info *info)
+{
+ if (program->children.size == 1)
+ ex_pipeline(ft_vec_access(&program->children, 0), info);
+ 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);
+ }
+ else
+ {
+ ex_program(ft_vec_access(&program->children, 0), info);
+ if (info->ret_val != 0)
+ ex_pipeline(ft_vec_access(&program->children, 2), info);
+ }
+}
+
+int execute(t_tree *parse_tree, t_vars *vars)
+{
+ return (ex_program(parse_tree, env));
+}