/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   redirect_valid_type.c                              :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: qmennen <qmennen@student.codam.nl>         +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2025/02/23 12:30:18 by Quinten           #+#    #+#             */
/*   Updated: 2025/02/26 17:40:44 by qmennen          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "minishell.h"

int	redirect_token_type(t_token *token)
{
	return (token->type == T_REDIRECT_IN
		|| token->type == T_HEREDOC
		|| token->type == T_REDIRECT_OUT
		|| token->type == T_APPEND_OUT);
}

int	redirect_is_valid(t_list *lst, t_token *token, int mode)
{
	t_token	*next;

	if (!lst->next)
		return (0);
	next = (t_token *)lst->next->content;
	if (!next)
	{
		return (0);
	}
	if (mode >= 0 && access(next->value, mode) != 0)
	{
		error_msg("minishell", "unable to write to temp file");
		return (0);
	}
	return (redirect_token_type(token) && next->type < 3);
}

int	redirect_is_delimiter(t_token *token)
{
	return (token->type == T_PIPE
		|| token->type == T_AND
		|| token->type == T_OR
		|| token->type == T_EOF
		|| token->type == T_ERROR);
}
