/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   redirect_new.c                                     :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: qmennen <qmennen@student.codam.nl>         +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2025/02/23 12:27:33 by Quinten           #+#    #+#             */
/*   Updated: 2025/02/27 18:11:25 by qmennen          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "minishell.h"

t_redirect	*redirect_new(t_minishell *msh, t_token_type type, char *value)
{
	t_redirect	*result;

	result = malloc_safe(msh, sizeof(t_redirect));
	result->type = type;
	result->value = NULL;
	if (value)
		result->value = value;
	return (result);
}

int	redirect_create(t_minishell *msh, t_list **tokens, t_list **redirects,
t_token_type type)
{
	t_list		*new;
	t_redirect	*redir;
	t_token		*file_token;
	char		*file_name;

	file_token = (t_token *)((*tokens)->next->content);
	file_name = NULL;
	if (file_token)
		file_name = ft_strdup_safe(msh, file_token->value);
	redir = redirect_new(msh, type, file_name);
	new = ft_lstnew_safe(msh, redir);
	ft_lstadd_back(redirects, new);
	if (tokens)
		*tokens = (*tokens)->next;
	return (1);
}
