2023-02-17 10:33:52 -05:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
|
|
|
/* 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 */
|
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
2023-03-10 06:32:39 -05:00
|
|
|
#include "./redirection_private.h"
|
2023-02-03 10:02:52 -05:00
|
|
|
|
2023-02-23 09:14:08 -05:00
|
|
|
int *ft_get_heredoc()
|
|
|
|
{
|
2023-03-07 11:48:18 -05:00
|
|
|
static int heredoc = -1;
|
2023-02-23 09:14:08 -05:00
|
|
|
|
|
|
|
return (&heredoc);
|
|
|
|
}
|
|
|
|
|
2023-03-07 11:48:18 -05:00
|
|
|
int ft_heredoc(t_data *data, char *stop)
|
2023-02-03 10:02:52 -05:00
|
|
|
{
|
|
|
|
char *line;
|
2023-03-07 11:48:18 -05:00
|
|
|
char *line_clean;
|
|
|
|
int fds[2];
|
2023-02-03 10:02:52 -05:00
|
|
|
|
2023-03-07 11:48:18 -05:00
|
|
|
if (pipe(fds) == -1)
|
|
|
|
return (-2);
|
|
|
|
*ft_get_heredoc() = dup(0);
|
|
|
|
ft_printf("> ");
|
|
|
|
line = get_next_line(*ft_get_heredoc());
|
2023-02-03 10:02:52 -05:00
|
|
|
while (line != NULL)
|
|
|
|
{
|
2023-03-07 11:48:18 -05:00
|
|
|
line[ft_strlen(line) - 1] = '\0';
|
2023-02-03 10:02:52 -05:00
|
|
|
if (ft_strcmp(line, stop) == 0)
|
2023-03-07 11:48:18 -05:00
|
|
|
{
|
|
|
|
free(line);
|
2023-02-03 10:02:52 -05:00
|
|
|
break ;
|
2023-03-07 11:48:18 -05:00
|
|
|
}
|
|
|
|
line_clean = ft_env_filler(data, line);
|
2023-02-03 10:02:52 -05:00
|
|
|
free(line);
|
2023-03-07 11:48:18 -05:00
|
|
|
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);
|
|
|
|
}
|
2023-02-23 09:14:08 -05:00
|
|
|
}
|
2023-03-07 11:48:18 -05:00
|
|
|
close(fds[1]);
|
|
|
|
*ft_get_heredoc() = -1;
|
|
|
|
return (fds[0]);
|
2023-02-23 09:14:08 -05:00
|
|
|
}
|