/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   e_redirs.c                                         :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/14 17:19:27 by rbousset          #+#    #+#             */
/*   Updated: 2020/02/14 17:19:29 by rbousset         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <libft.h>
#include <stdint.h>
#include <stddef.h>
#include <fcntl.h>
#include <unistd.h>

#include "f_fail.h"
#include "s_destroy.h"
#include "s_line.h"
#include "s_struct.h"

static void	e_redir_minus_two(struct s_lredir *ptr)
{
	int32_t	fd[2];

	if (ptr->heredoc == NULL)
	{
		return ;
	}
	pipe(fd);
	dup2(fd[0], STDIN_FILENO);
	ft_dprintf(fd[1], "%s", ptr->heredoc);
	close(fd[1]);
	close(fd[0]);
}

static void	e_redir_minus_one(struct s_lredir *ptr, t_msh *msh)
{
	int32_t	fd;

	if ((fd = open(ptr->path, O_RDONLY)) == -1)
	{
		f_redir(ptr->path, msh);
		return ;
	}
	dup2(fd, STDIN_FILENO);
	close(fd);
}

static void	e_redir_plus_one(struct s_lredir *ptr, t_msh *msh)
{
	int32_t	fd;

	if ((fd = open(ptr->path, O_CREAT | O_TRUNC | O_WRONLY, 0644)) == -1)
	{
		f_redir(ptr->path, msh);
		return ;
	}
	dup2(fd, ptr->fd);
	close(fd);
}

static void	e_redir_plus_two(struct s_lredir *ptr, t_msh *msh)
{
	int32_t	fd;

	if ((fd = open(ptr->path, O_CREAT | O_APPEND | O_WRONLY, 0644)) == -1)
	{
		f_redir(ptr->path, msh);
		return ;
	}
	dup2(fd, ptr->fd);
	close(fd);
}

void		e_dup_redirs(const t_com *com, t_msh *msh)
{
	struct s_lredir	*ptr;

	ptr = com->rdr;
	while (ptr != NULL)
	{
		if (ptr->redir == -2 || ptr->redir == -3)
		{
			e_redir_minus_two(ptr);
		}
		else if (ptr->redir == -1)
		{
			e_redir_minus_one(ptr, msh);
		}
		else if (ptr->redir == 1)
		{
			e_redir_plus_one(ptr, msh);
		}
		else if (ptr->redir == 2)
		{
			e_redir_plus_two(ptr, msh);
		}
		ptr = ptr->next;
	}
}