diff --git a/.nfs00000000098c82b80000065f b/.nfs00000000098c82b80000065f new file mode 100755 index 0000000..baed54c Binary files /dev/null and b/.nfs00000000098c82b80000065f differ diff --git a/.nfs00000000098c82ed00000153 b/.nfs00000000098c82ed00000153 deleted file mode 100755 index 1699e31..0000000 Binary files a/.nfs00000000098c82ed00000153 and /dev/null differ diff --git a/heredoc.c b/heredoc.c index cc9ae71..3c58229 100644 --- a/heredoc.c +++ b/heredoc.c @@ -13,54 +13,48 @@ #include "libftx/libftx.h" #include "minishell.h" #include +#include int *ft_get_heredoc() { - static int heredoc; + static int heredoc = -1; return (&heredoc); } -int ft_heredoc_creator(char *stop, int fd) +int ft_heredoc(t_data *data, char *stop) { char *line; - - line = readline("> "); - while (line != NULL) - { - if (ft_strcmp(line, stop) == 0) - break ; - ft_putendl_fd(line, fd); - free(line); - line = readline("> "); - if (line == NULL) - return (1); - } - return (0); -} - -int ft_heredoc(char *stop) -{ - int pid; + char *line_clean; int fds[2]; - int exit_code; if (pipe(fds) == -1) - return (-1); - pid = fork(); - if (pid == -1) - return (-1); - if (pid == 0) + return (-2); + *ft_get_heredoc() = dup(0); + ft_printf("> "); + line = get_next_line(*ft_get_heredoc()); + while (line != NULL) { - exit_code = ft_heredoc_creator(stop, fds[1]); - exit (exit_code + 1); - } - else - { - *ft_get_heredoc() = pid; - waitpid(pid, &exit_code, 0); - *ft_get_heredoc() = 0; - close(fds[1]); - return (fds[0]); + 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]); } diff --git a/infile.c b/infile.c index 2f17bc8..a0a10dc 100644 --- a/infile.c +++ b/infile.c @@ -10,16 +10,14 @@ /* */ /* ************************************************************************** */ -#include "libftx/libftx.h" #include "minishell.h" -#include "utils/utils.h" -#include static int ft_infile_is_valid(t_data *data, const char *line) { char **tab; char *path; ssize_t i; + int return_code; tab = ft_split_quoted(line, ' '); if (tab == NULL) @@ -29,34 +27,34 @@ static int ft_infile_is_valid(t_data *data, const char *line) } if (tab[0] == NULL) return (1); + return_code = 1; i = -1; - while (tab[++i] != NULL) + while (tab[++i] != NULL && return_code == 1) { if (tab[i][0] == '<') { - path = ft_env_filler(data, tab[i + 1]); - if (path == NULL) - return (0); - ft_quote_remover(path); - if (ft_file_is_readable(path) == 0) + if (ft_strcmp(tab[i], "<") == 0) { - data->exit_code = 1; - free(path); - ft_freer_tab_ultimate(1, tab); - return (0); + path = ft_env_filler(data, tab[i + 1]); + if (path == NULL) + return_code = 0; + ft_quote_remover(path); + free(tab[i + 1]); + tab[i + 1] = path; + if (ft_file_is_readable(tab[i + 1]) == 0) + { + data->exit_code = 1; + return_code = -1; + } } - if (ft_contain_only_str(path, "| <>")) - { - free(path); - ft_eprintf("minishell: %s: must be followed by an infile\n", path); - ft_freer_tab_ultimate(1, tab); - return (0); - } - free(path); + if (ft_contain_only_str(tab[i + 1], "| <>")) + return_code = 0; } } + if (return_code == 0) + ft_eprintf("minishell: %s: must be followed by an infile\n", tab[i]); ft_freer_tab_ultimate(1, tab); - return (1); + return (return_code); } static int ft_get_infile(t_data *data, const char *line) @@ -89,7 +87,7 @@ static int ft_get_infile(t_data *data, const char *line) free(path); } else if (ft_strcmp("<<", tab[i]) == 0) - fd = ft_heredoc(ft_quote_remover(tab[i + 1])); + fd = ft_heredoc(data, ft_quote_remover(tab[i + 1])); i++; } ft_freer_tab_ultimate(1, tab); diff --git a/main.c b/main.c index 333f523..9b0ce18 100644 --- a/main.c +++ b/main.c @@ -39,7 +39,6 @@ static char *ft_get_user_input() static int ft_minishell(t_data *data, char *line) { - t_list **cmds; char *line_clean; int infile; @@ -107,9 +106,11 @@ static int ft_minishell(t_data *data, char *line) void ft_ctrlc(int num) { (void) num; - if (*ft_get_heredoc()) + + if (*ft_get_heredoc() != -1) { - kill(*ft_get_heredoc(), SIGQUIT); + close(*ft_get_heredoc()); + *ft_get_heredoc() = -1; } else { diff --git a/minishell.h b/minishell.h index 3550bb8..0b6ee63 100644 --- a/minishell.h +++ b/minishell.h @@ -40,7 +40,7 @@ int ft_file_is_appendable(const char *path); char *ft_get_file_path(const char *infile); int ft_infile(t_data *data, char *line); int ft_outfile(t_data *data, char *line); -int ft_heredoc(char *stop); +int ft_heredoc(t_data *data, char *stop); int *ft_get_heredoc(); size_t ft_seglen_quoted(const char *str, char c); int ft_cmds_executor(t_data *data, t_list **cmds);