diff --git a/cmd/cmd.c b/cmd/cmd.c index fa4dd8a..3b834ec 100644 --- a/cmd/cmd.c +++ b/cmd/cmd.c @@ -6,7 +6,7 @@ /* By: cchauvet +#include "../signal/signal.h" +#include static bool ft_check_heredoc(t_data *data, t_cmd *cmd, char *redirection_identifier, char *redirection) @@ -22,7 +24,9 @@ static bool ft_check_heredoc(t_data *data, t_cmd *cmd, { if (cmd->fd_in[0] == -2) return (0); + signal(SIGINT, ft_ctrlc_heredoc); fd = ft_heredoc(data, redirection); + signal(SIGINT, ft_ctrlc); if (fd == -2) return (1); else diff --git a/redirection/heredoc.c b/redirection/heredoc.c index 7feb079..7b9fbef 100644 --- a/redirection/heredoc.c +++ b/redirection/heredoc.c @@ -6,17 +6,23 @@ /* By: cchauvet +#include -int *ft_get_heredoc(void) +static bool ft_fd_is_closed(int fd) { - static int heredoc = -1; + int fd2; - return (&heredoc); + fd2 = dup(fd); + if (fd2 == -1) + return (1); + close(fd2); + return (0); } static int ft_format_and_write(t_data *data, const char *str, int fd) @@ -31,75 +37,57 @@ static int ft_format_and_write(t_data *data, const char *str, int fd) return (0); } -static int ft_heredoc3(char *stop, int fds[2]) +int ft_heredoc2(t_data *data, char *line, char *stop, int fds[2]) { - if (*ft_get_heredoc() == -1) - { - ft_putchar_fd('\n', 1); - ft_closer(fds); - return (1); - } - else - { - ft_eprintf("bozoshell: warning: here-document at line"); - ft_eprintf("1 delimited by end-of-file (wanted `%s')\n", stop); - return (2); - } - return (0); -} - -static int ft_heredoc2(t_data *data, char *stop, int fds[2]) -{ - char *line; - int out; - - ft_printf("> "); - line = get_next_line(*ft_get_heredoc()); if (line == NULL) { - out = ft_heredoc3(stop, fds); - if (out) - return (out); + if (ft_fd_is_closed(0)) + { + close(fds[0]); + fds[0] = -2; + return (1); + } + else + { + ft_eprintf("\nbozoshell: warning: here-document at line 1%s", + "delimited by end-of-file (wanted `adfsd')\n"); + return (1); + } } - line[ft_strlen(line) - 1] = '\0'; if (ft_strcmp(line, stop) == 0) - { - free(line); - return (2); - } + return (1); if (ft_format_and_write(data, line, fds[1])) { - ft_closer(fds); - free(line); + close(fds[0]); + fds[0] = -2; return (1); } - free(line); return (0); } int ft_heredoc(t_data *data, char *stop) { int fds[2]; - int return_code; + int stdin_bak; + char *line; - if (pipe(fds) == -1) - return (-1); - *ft_get_heredoc() = dup(0); - while (true) + stdin_bak = dup(0); + if (stdin_bak == -1) + return (1); + if (pipe(fds)) { - return_code = ft_heredoc2(data, stop, fds); - if (return_code == 2) - break ; - else if (return_code == 1) - { - if (*ft_get_heredoc() > 2) - close(*ft_get_heredoc()); - *ft_get_heredoc() = -1; - return (-2); - } + close(stdin_bak); + return (1); } - close(*ft_get_heredoc()); - *ft_get_heredoc() = -1; + line = readline("> "); + while (ft_heredoc2(data, line, stop, fds) == 0) + { + free(line); + line = readline("> "); + } + free(line); close(fds[1]); + dup2(stdin_bak, 0); + close(stdin_bak); return (fds[0]); } diff --git a/signal/signal.c b/signal/signal.c index cc108a5..2aff3fa 100644 --- a/signal/signal.c +++ b/signal/signal.c @@ -6,26 +6,25 @@ /* By: cchauvet