/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   redirections.c                                     :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: lperrey <lperrey@student.42.fr>            +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2021/11/11 18:46:43 by lperrey           #+#    #+#             */
/*   Updated: 2021/12/10 10:25:34 by hulamy           ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "minishell.h"

int			here_doc(char *delimiter);

static int	redirect_cmd_input(t_token *t, t_cmd *cmd);
static int	redirect_cmd_output(t_token *t, t_cmd *cmd);

int	redirections(t_token *t, t_cmd **pipeline)
{
	int	i;

	i = 0;
	while (t)
	{
		if (!pipeline[i]->error)
		{
			if (t->id == '<' || t->id == T_DLESS)
			{
				if (!redirect_cmd_input(t, pipeline[i]))
					return (0);
			}
			else if (t->id == '>' || t->id == T_DGREAT)
			{
				if (!redirect_cmd_output(t, pipeline[i]))
					return (0);
			}
		}
		if (t->id == '|')
			i++;
		t = t->next;
	}
	return (1);
}

static int	redirect_cmd_input(t_token *t, t_cmd *cmd)
{
	if (cmd->fd_in != STDIN_FILENO)
		if (close(cmd->fd_in) == -1)
			perror("close()");
	if (t->id == '<')
	{
		// TODO : Expansion + quote removal sur le word t->next->content.
		// si plus d'un champ ou aucun champ aprés expansion,
		// message d'erreur comme bash "bash: $VAR: ambiguous redirect"
		// OU prise en compte seulement du premier champ.
		//EXPAND_AND_QUOTE_REMOVAL_PLACEHOLDER();
		cmd->fd_in = open(t->next->content, O_RDONLY);
		if (cmd->fd_in == -1)
		{
			ft_perror_io("open() ", t->next->content); // todo error
			cmd->error = EXIT_REDIRECTION;
		}
	}
	else if (t->id == T_DLESS)
	{
		cmd->fd_in = here_doc(t->next->content);
		if (cmd->fd_in == -1)
		{
			ft_putstr_fd("minishell: heredoc error\n", 2);
			cmd->error = EXIT_REDIRECTION;
		}
		else if (cmd->fd_in > EXIT_SIGNAL)
		{
			cmd->fd_in = 0;
			return (0);
		}
	}
	return (1);
}

static int	redirect_cmd_output(t_token *t, t_cmd *cmd)
{
	int	flags;

	if (cmd->fd_out != STDOUT_FILENO)
		if (close(cmd->fd_out) == -1)
			perror("close()");
	// TODO : Expansion + quote removal sur le word t->next->content.
	// si plus d'un champ ou aucun champ aprés expansion,
	// message d'erreur comme bash "bash: $VAR: ambiguous redirect"
	// OU prise en compte seulement du premier champ.
	//EXPAND_AND_QUOTE_REMOVAL_PLACEHOLDER();
	flags = O_WRONLY | O_CREAT;
	if (t->id == '>')
		flags = flags | O_TRUNC;
	else if (t->id == T_DGREAT)
		flags = flags | O_APPEND;
	cmd->fd_out = open(t->next->content, flags, S_IRWXU);
	if (cmd->fd_out == -1)
	{
		ft_perror_io("open() ", t->next->content);
		cmd->error = EXIT_REDIRECTION;
	}
	return (1);
}
