42_minishell/heredoc.c

61 lines
1.7 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* heredoc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/17 15:36:26 by cchauvet #+# #+# */
/* Updated: 2023/02/17 16:21:02 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftx/libftx.h"
#include "minishell.h"
#include <stdlib.h>
#include <unistd.h>
int *ft_get_heredoc()
{
static int heredoc = -1;
return (&heredoc);
}
int ft_heredoc(t_data *data, char *stop)
{
char *line;
char *line_clean;
int fds[2];
if (pipe(fds) == -1)
return (-2);
*ft_get_heredoc() = dup(0);
ft_printf("> ");
line = get_next_line(*ft_get_heredoc());
while (line != NULL)
{
line[ft_strlen(line) - 1] = '\0';
if (ft_strcmp(line, stop) == 0)
{
free(line);
break ;
}
line_clean = ft_env_filler(data, line);
free(line);
ft_putendl_fd(line_clean, fds[1]);
free(line_clean);
ft_printf("> ");
line = get_next_line(*ft_get_heredoc());
if (line == NULL && *ft_get_heredoc() == -1)
{
close(fds[0]);
close(fds[1]);
return (-2);
}
}
close(fds[1]);
*ft_get_heredoc() = -1;
return (fds[0]);
}