From be1f7ebf8ce9c4ee21c5867ab16da1cb27781cf0 Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Fri, 7 Apr 2023 15:12:48 +0000 Subject: [PATCH] clean: norm --- cmd/cmd.c | 28 +++++++++++++++++++++++++++- cmd/cmd.h | 3 ++- main.c | 39 +++------------------------------------ redirection/check.c | 23 +++++++++++------------ redirection/heredoc.c | 34 ++++++++++++++++++++++------------ 5 files changed, 65 insertions(+), 62 deletions(-) diff --git a/cmd/cmd.c b/cmd/cmd.c index 461842a..3aff8eb 100644 --- a/cmd/cmd.c +++ b/cmd/cmd.c @@ -6,10 +6,11 @@ /* By: cchauvet fd_in); ft_closer(cmd->fd_out); } + +void ft_cmdwaiter(void *ptr) +{ + t_cmd *cmd; + int exit_status; + + cmd = ptr; + if (cmd->executable != NULL && cmd->own_cmd == 0 + && cmd->pid != -1 && cmd->fd_in[0] != -2 && cmd->fd_out[0] != -2) + { + waitpid(cmd->pid, &exit_status, 0); + if (WIFSIGNALED(exit_status)) + { + if (exit_status == 131) + { + ft_printf("Quit (core dumped)\n"); + *ft_get_exit_code() = 131; + } + else + *ft_get_exit_code() = 130; + } + else + *ft_get_exit_code() = WEXITSTATUS(exit_status); + } +} diff --git a/cmd/cmd.h b/cmd/cmd.h index 6bfd97c..ae87162 100644 --- a/cmd/cmd.h +++ b/cmd/cmd.h @@ -6,7 +6,7 @@ /* By: cchauvet cmds; - while (current != NULL) - { - cmd = current->content; - if (cmd->executable != NULL && cmd->own_cmd == 0 - && cmd->pid != -1 && cmd->fd_in[0] != -2 && cmd->fd_out[0] != -2) - { - waitpid(cmd->pid, &exit_status, 0); - if (WIFSIGNALED(exit_status)) - { - if (exit_status == 131) - { - ft_printf("Quit (core dumped)\n"); - *data->exit_code = 131; - } - else - { - ft_putchar_fd('\n', 1); - *data->exit_code = 130; - } - } - else - *data->exit_code = WEXITSTATUS(exit_status); - } - current = current->next; - } -} - static int ft_minishell(t_data *data, char *line) { char *line_clean; @@ -94,7 +61,7 @@ static int ft_minishell(t_data *data, char *line) free(line_clean); if (ft_cmds_executor(data) == 1) return (1); - ft_cmds_waiter(data); + ft_lstiter(*data->cmds, ft_cmdwaiter); ft_lstclear(data->cmds, ft_cmddel); return (0); } diff --git a/redirection/check.c b/redirection/check.c index a089045..cf2b3f6 100644 --- a/redirection/check.c +++ b/redirection/check.c @@ -6,11 +6,12 @@ /* By: cchauvet static bool ft_check_heredoc(t_data *data, t_cmd *cmd, char *redirection_identifier, char *redirection) @@ -120,8 +121,10 @@ bool ft_check_redirection(t_data *data, t_cmd *cmd, char *redirection_identifier, char *redirection) { char *str; + bool out; - if (ft_is_in("<>", redirection_identifier[0]) && ft_is_in("<>", redirection[0])) + if (ft_is_in("<>", redirection_identifier[0]) + && ft_is_in("<>", redirection[0])) { ft_eprintf("minishell: %s: invalid redirection file\n", redirection); return (1); @@ -133,16 +136,12 @@ bool ft_check_redirection(t_data *data, t_cmd *cmd, return (1); } ft_quote_remover(str); + out = 0; if (ft_check_heredoc(data, cmd, redirection_identifier, str) - || ft_check_infile(data, cmd, redirection_identifier, str) - || ft_check_outfile(data, cmd, redirection_identifier, str) - || ft_check_outfile_append(data, cmd, redirection_identifier, - str)) - { - free(str); - return (1); - } - ; + || ft_check_infile(data, cmd, redirection_identifier, str) + || ft_check_outfile(data, cmd, redirection_identifier, str) + || ft_check_outfile_append(data, cmd, redirection_identifier, str)) + out = 1; free(str); - return (0); + return (out); } diff --git a/redirection/heredoc.c b/redirection/heredoc.c index f1f707d..cbf8e25 100644 --- a/redirection/heredoc.c +++ b/redirection/heredoc.c @@ -6,7 +6,7 @@ /* By: cchauvet "); line = get_next_line(*ft_get_heredoc()); if (line == NULL) { - if (*ft_get_heredoc() == -1) - { - ft_putchar_fd('\n', 1); - ft_closer(fds); - return (1); - } - else - { - ft_eprintf("minishell: warning: here-document at line 1 delimited by end-of-file (wanted `%s')\n", stop); - return (2); - } + out = ft_heredoc3(stop, fds); + if (out) + return (out); } line[ft_strlen(line) - 1] = '\0'; if (ft_strcmp(line, stop) == 0)