/* ************************************************************************** */
/*                                                                            */
/*                                                        ::::::::            */
/*   redirect_get_inputs.c                              :+:    :+:            */
/*                                                     +:+                    */
/*   By: qmennen <qmennen@student.codam.nl>           +#+                     */
/*                                                   +#+                      */
/*   Created: 2025/02/23 12:29:05 by Quinten       #+#    #+#                 */
/*   Updated: 2025/02/28 14:00:39 by whaffman      ########   odam.nl         */
/*                                                                            */
/* ************************************************************************** */

#include "redirect.h"

static void	check_heredoc(t_minishell *msh, t_list *current, t_token *token)
{
	if (token->type == T_HEREDOC && redirect_is_valid(current, token, -1))
		process_heredoc(msh, token, current->next->content);
}

t_list	*redirect_get_inputs(t_minishell *msh, t_list *list)
{
	t_list	*current;
	t_list	*redirects;
	t_token	*token;
	int		flag;

	redirects = NULL;
	current = list;
	flag = 1;
	token = (t_token *)current->content;
	while (current && flag && !redirect_is_delimiter(token))
	{
		token = (t_token *)current->content;
		if (token->type != T_REDIRECT_IN && token->type != T_HEREDOC)
		{
			current = current->next;
			continue ;
		}
		check_heredoc(msh, current, token);
		flag = redirect_is_valid(current, token, F_OK | R_OK);
		if (flag)
			redirect_create(msh, &current, &redirects, token->type);
		current = current->next;
	}
	redirect_new_error(msh, &redirects, flag);
	return (redirects);
}
