/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   process_helpers.c                                  :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: sede-san <sede-san@student.42madrid.com    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2026/02/11 00:00:00 by sede-san          #+#    #+#             */
/*   Updated: 2026/02/11 00:00:00 by sede-san         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "executor.h"

void	executor_child_process(
	t_list *current_command,
	t_pipeline *pipeline,
	t_minishell *minishell
)
{
	uint8_t		exit_status;
	t_command	*command;

	command = current_command->content;
	executor_setup_child_input(pipeline);
	executor_setup_child_output(current_command, pipeline);
	if (!executor_apply_redirections(command, NULL, NULL))
		exit(EXIT_FAILURE);
	exit_status = executor_execute_command(command, minishell);
	exit(exit_status);
}

void	executor_parent_cleanup(
	t_list *current_command,
	t_pipeline *pipeline
)
{
	if (pipeline->prev_read_fd != -1)
		close(pipeline->prev_read_fd);
	if (current_command->next)
	{
		close(pipeline->pipefd[WRITE_PIPE]);
		pipeline->prev_read_fd = pipeline->pipefd[READ_PIPE];
	}
	else
		pipeline->prev_read_fd = -1;
}

uint8_t	executor_wait_for_children(
	pid_t last_child_pid
)
{
	uint8_t	exit_status;
	int		status;
	pid_t	pid;

	exit_status = EXIT_SUCCESS;
	pid = wait(&status);
	while (last_child_pid > 0 && pid > 0)
	{
		if (pid == last_child_pid)
		{
			if (WIFEXITED(status))
				exit_status = WEXITSTATUS(status);
			else if (WIFSIGNALED(status))
				exit_status = 128 + WTERMSIG(status);
		}
		pid = wait(&status);
	}
	return (exit_status);
}

static void	cmdfree_argv(
	char **argv
)
{
	size_t	i;

	if (argv == NULL)
		return ;
	i = 0;
	while (argv[i] != NULL)
	{
		free(argv[i]);
		i++;
	}
	free(argv);
}

static void	cmdfree_redirection(
	t_redirection *redirection
)
{
	if (redirection == NULL)
		return ;
	free(redirection->target);
	free(redirection);
}

void	executor_cmdfree(
	t_command *command
)
{
	if (command == NULL)
		return ;
	cmdfree_argv(command->argv);
	free(command->path);
	ft_lstclear(&command->redirections,
		(void (*)(void *))cmdfree_redirection);
	ft_lstclear(&command->heredocs, (void (*)(void *))cmdfree_redirection);
	free(command);
}
