42_minishell/redirection/heredoc.c

58 lines
1.7 KiB
C
Raw Normal View History

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 */
/* */
/* ************************************************************************** */
#include "./redirection_private.h"
2023-02-03 10:02:52 -05:00
2023-02-23 09:14:08 -05:00
int *ft_get_heredoc()
{
static int heredoc = -1;
2023-02-23 09:14:08 -05:00
return (&heredoc);
}
int ft_heredoc(t_data *data, char *stop)
2023-02-03 10:02:52 -05:00
{
char *line;
char *line_clean;
int fds[2];
2023-02-03 10:02:52 -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)
{
line[ft_strlen(line) - 1] = '\0';
2023-02-03 10:02:52 -05:00
if (ft_strcmp(line, stop) == 0)
{
free(line);
2023-02-03 10:02:52 -05:00
break ;
}
line_clean = ft_env_filler(data, line);
2023-02-03 10:02:52 -05:00
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);
}
2023-02-23 09:14:08 -05:00
}
close(fds[1]);
*ft_get_heredoc() = -1;
return (fds[0]);
2023-02-23 09:14:08 -05:00
}