42_minishell/redirection/heredoc.c

106 lines
2.4 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 #+# #+# */
2023-04-07 11:12:48 -04:00
/* Updated: 2023/04/07 15:11:17 by alouis-j ### ########.fr */
2023-02-17 10:33:52 -05:00
/* */
/* ************************************************************************** */
#include "./redirection_private.h"
2023-02-03 10:02:52 -05:00
2023-03-28 09:55:08 -04:00
int *ft_get_heredoc(void)
2023-02-23 09:14:08 -05:00
{
static int heredoc = -1;
2023-02-23 09:14:08 -05:00
return (&heredoc);
}
2023-03-29 13:07:57 -04:00
static int ft_format_and_write(t_data *data, const char *str, int fd)
{
char *line_clean;
line_clean = ft_env_filler(data, str);
if (line_clean == NULL)
return (1);
ft_putendl_fd(line_clean, fd);
free(line_clean);
return (0);
}
2023-04-07 11:12:48 -04:00
static int ft_heredoc3(char *stop, int fds[2])
{
if (*ft_get_heredoc() == -1)
{
ft_putchar_fd('\n', 1);
ft_closer(fds);
return (1);
}
else
{
2023-04-14 10:17:35 -04:00
ft_eprintf("bozoshell: warning: here-document at line");
2023-04-07 11:12:48 -04:00
ft_eprintf("1 delimited by end-of-file (wanted `%s')\n", stop);
return (2);
}
return (0);
}
2023-03-31 09:23:10 -04:00
static int ft_heredoc2(t_data *data, char *stop, int fds[2])
2023-03-29 13:07:57 -04:00
{
2023-03-31 09:23:10 -04:00
char *line;
2023-04-07 11:12:48 -04:00
int out;
2023-03-31 09:25:26 -04:00
2023-03-31 09:23:10 -04:00
ft_printf("> ");
line = get_next_line(*ft_get_heredoc());
if (line == NULL)
2023-03-31 09:29:06 -04:00
{
2023-04-07 11:12:48 -04:00
out = ft_heredoc3(stop, fds);
if (out)
return (out);
2023-03-31 09:29:06 -04:00
}
2023-03-29 13:07:57 -04:00
line[ft_strlen(line) - 1] = '\0';
if (ft_strcmp(line, stop) == 0)
{
free(line);
return (2);
}
if (ft_format_and_write(data, line, fds[1]))
{
ft_closer(fds);
free(line);
return (1);
}
free(line);
return (0);
}
int ft_heredoc(t_data *data, char *stop)
2023-02-03 10:02:52 -05:00
{
int fds[2];
2023-03-29 13:07:57 -04:00
int return_code;
2023-02-03 10:02:52 -05:00
if (pipe(fds) == -1)
2023-03-29 13:07:57 -04:00
return (-1);
*ft_get_heredoc() = dup(0);
2023-03-31 09:23:10 -04:00
while (true)
2023-02-03 10:02:52 -05:00
{
2023-03-31 09:23:10 -04:00
return_code = ft_heredoc2(data, stop, fds);
2023-03-29 13:07:57 -04:00
if (return_code == 2)
2023-02-03 10:02:52 -05:00
break ;
2023-04-05 08:13:56 -04:00
else if (return_code == 1)
2023-03-31 09:32:21 -04:00
{
2023-04-05 11:32:04 -04:00
if (*ft_get_heredoc() > 2)
close(*ft_get_heredoc());
2023-03-31 12:48:19 -04:00
*ft_get_heredoc() = -1;
return (-2);
2023-03-31 09:32:21 -04:00
}
2023-02-23 09:14:08 -05:00
}
2023-03-31 12:49:12 -04:00
close(*ft_get_heredoc());
*ft_get_heredoc() = -1;
2023-03-31 10:36:15 -04:00
close(fds[1]);
return (fds[0]);
2023-02-23 09:14:08 -05:00
}