/* ************************************************************************** */
/*                                                                            */
/*                                                        ::::::::            */
/*   parser_get_commands.c                              :+:    :+:            */
/*                                                     +:+                    */
/*   By: qmennen <qmennen@student.codam.nl>           +#+                     */
/*                                                   +#+                      */
/*   Created: 2025/02/11 14:06:02 by qmennen       #+#    #+#                 */
/*   Updated: 2025/03/04 18:21:27 by whaffman      ########   odam.nl         */
/*                                                                            */
/* ************************************************************************** */

#include "minishell.h"

static int	is_command_token(t_token *token)
{
	return (token->type < 3 || redirect_token_type(token));
}

t_list	*parser_get_commands(t_minishell *msh)
{
	t_list		*command_list;
	t_list		*current;
	t_command	*command;

	command_list = NULL;
	if (!msh->tokens)
		return (NULL);
	current = msh->tokens;
	while (current)
	{
		command = parser_alloc_command(msh);
		parser_create_command(msh, command, &current);
		if (! parser_validate_command(command))
			break ;
		ft_lstadd_back(&command_list, ft_lstnew_safe(msh, command));
		while (current && is_command_token((t_token *)current->content))
			current = current->next;
		if (current && ((t_token *)current->content)->type >= 3)
			current = current->next;
	}
	return (command_list);
}
