/* ************************************************************************** */
/*                                                                            */
/*                                                        ::::::::            */
/*   free_command_list.c                                :+:    :+:            */
/*                                                     +:+                    */
/*   By: marvin <marvin@student.42.fr>                +#+                     */
/*                                                   +#+                      */
/*   Created: 2025/02/11 14:24:05 by qmennen       #+#    #+#                 */
/*   Updated: 2025/02/23 12:40:17 by Quinten       ########   odam.nl         */
/*                                                                            */
/* ************************************************************************** */

#include "minishell.h"

static void	free_args(t_minishell *msh, char **args)
{
	int	i;

	i = 0;
	while (args[i])
	{
		free_safe(msh, (void **)&args[i]);
		i++;
	}
	free_safe(msh, (void **)&args);
}

static void	free_redirects(t_minishell *msh, t_list *lst)
{
	t_redirect	*redir;
	t_list		*current;
	t_list		*last;

	current = lst;
	while (current)
	{
		last = current;
		redir = (t_redirect *)current->content;
		if (redir && redir->value)
			free_safe(msh, (void **)&redir->value);
		if (redir)
			free_safe(msh, (void **)&redir);
		current = current->next;
		free_safe(msh, (void **)&last);
	}
}

void	free_command_list(t_minishell *msh, void *content)
{
	t_command	*command;

	command = (t_command *)content;
	if (command->args)
		free_args(msh, command->args);
	if (command->redirect_in)
		free_redirects(msh, command->redirect_in);
	if (command->redirect_out)
		free_redirects(msh, command->redirect_out);
	free_safe(msh, (void **)&command);
}
