From 6498031d59c2416388d0c989bf511ed0db693446 Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Wed, 29 Mar 2023 19:07:57 +0200 Subject: [PATCH] clean: norm part2 (FINAL) --- Makefile | 4 +- execution/execution.c | 14 +++--- execution/execution_private.h | 5 +- format/format.c | 36 +++++++++------ main.c | 77 ++++++++----------------------- minishell.h | 3 +- redirection/check.c | 67 +++++++++++++++++++++++++++ redirection/heredoc.c | 62 +++++++++++++++++-------- redirection/redirection.c | 60 ++++++++---------------- redirection/redirection_private.h | 4 +- signal/signal.c | 65 ++++++++++++++++++++++++++ signal/signal.h | 19 ++++++++ signal/signal_private.h | 21 +++++++++ syntax/syntax.c | 27 ++++------- utils/fd.c | 8 +++- utils/ft_get_executable.c | 30 ++++++++---- utils/ft_quote_remover.c | 15 ++++-- utils/utils.h | 3 +- 18 files changed, 338 insertions(+), 182 deletions(-) create mode 100644 redirection/check.c create mode 100644 signal/signal.c create mode 100644 signal/signal.h create mode 100644 signal/signal_private.h diff --git a/Makefile b/Makefile index b42dc7f..4a20c38 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,9 @@ SRCS = ${BUILTINS_SRC} \ ./redirection/heredoc.c \ ./redirection/file.c \ ./redirection/redirection.c \ - ./parse/parse.c + ./redirection/check.c \ + ./parse/parse.c \ + ./signal/signal.c OBJS = ${SRCS:.c=.o} diff --git a/execution/execution.c b/execution/execution.c index 1059e5d..7d0e81d 100644 --- a/execution/execution.c +++ b/execution/execution.c @@ -6,17 +6,16 @@ /* By: cchauvet executable, "pwd") == 0) return_code = pwd(cmd->fd_out[0]); else if (ft_strcmp(cmd->executable, "env") == 0) @@ -29,7 +28,7 @@ static int ft_own_cmd(t_data *data, t_cmd *cmd) return_code = (unset(data->env, cmd->args, cmd->fd_out[0])); else if (ft_strcmp(cmd->executable, "echo") == 0) return_code = (echo(cmd->fd_out[0], cmd->args + 1)); - else if (ft_strcmp(cmd->executable, "exit") == 0) + else { return_code = ft_exit(cmd->args + 1); if (return_code >= 0) @@ -72,7 +71,7 @@ static int ft_cmd_executor(t_data *data, t_cmd *cmd, int fd) if (cmd->own_cmd == 1) { - exit_code = ft_own_cmd(data, cmd); + exit_code = ft_execute_own_cmd(data, cmd); ft_closer(cmd->fd_in); ft_closer(cmd->fd_out); if (exit_code == -1) @@ -104,7 +103,6 @@ int ft_cmds_executor(t_data *data) { content = current->content; fds[0] = -1; - fds[1] = -1; if (current->next != NULL) { if (pipe(fds) == -1) @@ -112,7 +110,9 @@ int ft_cmds_executor(t_data *data) ft_add_fd(content->fd_out, fds[1]); ft_add_fd(((t_cmd *)(current->next->content))->fd_in, fds[0]); } - if (ft_cmd_executor(data, content, fds[0])) + if (content->fd_in[0] == -2 || content->fd_out[0] == -2) + ft_mega_closer(content->fd_in, content->fd_out); + else if (ft_cmd_executor(data, content, fds[0])) return (1); current = current->next; } diff --git a/execution/execution_private.h b/execution/execution_private.h index 37e97b2..5b6aab0 100644 --- a/execution/execution_private.h +++ b/execution/execution_private.h @@ -6,7 +6,7 @@ /* By: cchauvet -#include -#include static char *ft_get_user_input(void) { @@ -55,7 +50,8 @@ static void ft_cmds_waiter(t_data *data) while (current != NULL) { content = current->content; - if (content->own_cmd == 0 && content->pid != -1) + if (content->own_cmd == 0 && content->pid != -1 + && content->fd_in[0] != -2 && content->fd_out[0] != -2) { waitpid(content->pid, &exit_status, 0); if (WIFSIGNALED(exit_status)) @@ -95,53 +91,25 @@ static int ft_minishell(t_data *data, char *line) return (0); } -void ft_ctrlc(int num) +int ft_init_data(t_data *data, char **env) { - t_data *data; - - if (num == SIGQUIT) - return ; - data = ft_get_data(); - data->exit_code = 130; - if (*ft_get_heredoc() != -1) + data->exit_code = 0; + data->child_pid = 0; + data->cmds = malloc(sizeof(t_cmd *)); + if (data->cmds == NULL) { - close(*ft_get_heredoc()); - *ft_get_heredoc() = -1; + ft_eprintf("minishell: malloc failed\n"); + return (1); } - else + *data->cmds = NULL; + data->env = init_env(env); + if (data->env == NULL) { - if (data->child_pid > 1) - { - data->child_pid = 0; - ft_putchar_fd('\n', 1); - } - else - { - rl_replace_line("", 0); - rl_on_new_line(); - ft_putchar_fd('\n', 1); - rl_redisplay(); - } - } -} - -void ft_quit(int num) -{ - t_data *data; - - (void) num; - data = ft_get_data(); - data->exit_code = 131; - if (data->child_pid > 1) - { - ft_printf("Quit (core dumped)\n"); - data->child_pid = 0; - } - else - { - rl_replace_line("", 0); - rl_redisplay(); + ft_eprintf("minishell: malloc failed\n"); + free(data->cmds); + return (1); } + return (0); } int main(int ac, char **av, char **env) @@ -154,14 +122,7 @@ int main(int ac, char **av, char **env) signal(SIGINT, ft_ctrlc); signal(SIGQUIT, ft_quit); data = ft_get_data(); - data->exit_code = 0; - data->child_pid = 0; - data->cmds = malloc(sizeof(t_cmd *)); - if (data->cmds == NULL) - return (1); - *data->cmds = NULL; - data->env = init_env(env); - if (data->env == NULL) + if (ft_init_data(data, env)) return (1); line = ft_get_user_input(); while (line != NULL) diff --git a/minishell.h b/minishell.h index 7a14123..5b8af0f 100644 --- a/minishell.h +++ b/minishell.h @@ -6,7 +6,7 @@ /* By: cchauvet # include # include diff --git a/redirection/check.c b/redirection/check.c new file mode 100644 index 0000000..f53f880 --- /dev/null +++ b/redirection/check.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* check.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet fd_in[0] = ft_heredoc(data, redirection); +} + +static void ft_check_infile(t_data *data, t_cmd *cmd, + char *redirection_identifier, char *redirection) +{ + if (ft_strcmp(redirection_identifier, "<") == 0) + { + if (ft_file_is_readable(data, redirection)) + cmd->fd_in[0] = open(redirection, O_RDONLY); + else + cmd->fd_in[0] = -2; + } +} + +static void ft_check_outfile(t_data *data, t_cmd *cmd, + char *redirection_identifier, char *redirection) +{ + if (ft_strcmp(redirection_identifier, ">") == 0) + { + if (ft_file_is_writable(data, redirection)) + cmd->fd_out[0] = open(redirection, + O_WRONLY | O_TRUNC | O_CREAT, 0644); + else + cmd->fd_out[0] = -2; + } +} + +static void ft_check_outfile_append(t_data *data, t_cmd *cmd, + char *redirection_identifier, char *redirection) +{ + if (ft_strcmp(redirection_identifier, ">>") == 0) + { + if (ft_file_is_writable(data, redirection)) + cmd->fd_out[0] = open(redirection, + O_WRONLY | O_APPEND | O_CREAT, 0644); + else + cmd->fd_out[0] = -2; + } +} + +void ft_check_redirection(t_data *data, t_cmd *cmd, + char *redirection_identifier, char *redirection) +{ + ft_check_heredoc(data, cmd, redirection_identifier, redirection); + ft_check_infile(data, cmd, redirection_identifier, redirection); + ft_check_outfile(data, cmd, redirection_identifier, redirection); + ft_check_outfile_append(data, cmd, redirection_identifier, redirection); +} diff --git a/redirection/heredoc.c b/redirection/heredoc.c index 7836840..cd5d133 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 && *ft_get_heredoc() == -1) + { + ft_closer(fds); + return (1); + } + return (0); +} + int ft_heredoc(t_data *data, char *stop) { char *line; - char *line_clean; int fds[2]; + int return_code; if (pipe(fds) == -1) - return (-2); + return (-1); *ft_get_heredoc() = dup(0); ft_printf("> "); line = get_next_line(*ft_get_heredoc()); while (line != NULL) { - line[ft_strlen(line) - 1] = '\0'; - if (ft_strcmp(line, stop) == 0) - { - free(line); + return_code = ft_heredoc2(data, stop, line, fds); + if (return_code == 2) 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]); + else if (return_code) return (-2); - } } close(fds[1]); *ft_get_heredoc() = -1; diff --git a/redirection/redirection.c b/redirection/redirection.c index 465562e..3f6c34d 100644 --- a/redirection/redirection.c +++ b/redirection/redirection.c @@ -6,7 +6,7 @@ /* By: cchauvet ", cmd_str[i])) start = i; - if (start == -1) + else continue ; - while (cmd_str[i] == cmd_str[start]) - i++; - while (cmd_str[i] == ' ') - i++; - while (cmd_str[i] != '\0' && (cmd_str[i] != ' ' - || ft_is_in_quote(cmd_str, i))) - i++; - stop = i - start; + ft_skip(cmd_str, &i, &start); if (start != -1) { - ft_strshift(cmd_str + start, -1 * stop); + ft_strshift(cmd_str + start, -1 * (i - start)); i = start; } } @@ -76,35 +79,8 @@ int ft_set_redirection(t_data *data, t_cmd *cmd, char **tab) while (tab[i + 1] != NULL) { ft_quote_remover(tab[i + 1]); - if (ft_strcmp(tab[i], "<<") == 0) - { - cmd->fd_in[0] = ft_heredoc(data, tab[i + 1]); - if (cmd->fd_in[0] == -2) - return (1); - } - else if (ft_strcmp(tab[i], "<") == 0) - { - if (ft_file_is_readable(data, tab[i + 1])) - cmd->fd_in[0] = open(tab[i + 1], O_RDONLY); - else - return (1); - } - else if (ft_strcmp(tab[i], ">") == 0) - { - if (ft_file_is_writable(data, tab[i + 1])) - cmd->fd_out[0] = open(tab[i + 1], - O_WRONLY | O_TRUNC | O_CREAT, 0644); - else - return (1); - } - else if (ft_strcmp(tab[i], ">>") == 0) - { - if (ft_file_is_appendable(data, tab[i + 1])) - cmd->fd_out[0] = open(tab[i + 1], - O_WRONLY | O_APPEND | O_CREAT, 0644); - else - return (1); - } + if (ft_check_redirection(data, cmd, tab[i], tab[i + 1]) == 2) + return (1); i++; } return (0); diff --git a/redirection/redirection_private.h b/redirection/redirection_private.h index 9d932ee..04a1761 100644 --- a/redirection/redirection_private.h +++ b/redirection/redirection_private.h @@ -6,7 +6,7 @@ /* By: cchauvet exit_code = 130; + if (*ft_get_heredoc() != -1) + { + close(*ft_get_heredoc()); + *ft_get_heredoc() = -1; + } + else + { + if (data->child_pid > 1) + { + data->child_pid = 0; + ft_putchar_fd('\n', 1); + } + else + ft_new_line(); + } +} + +void ft_quit(int num) +{ + t_data *data; + + (void) num; + data = ft_get_data(); + data->exit_code = 131; + if (data->child_pid > 1) + { + ft_printf("Quit (core dumped)\n"); + data->child_pid = 0; + } + else + { + rl_replace_line("", 0); + rl_redisplay(); + } +} diff --git a/signal/signal.h b/signal/signal.h new file mode 100644 index 0000000..aba255a --- /dev/null +++ b/signal/signal.h @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* signal.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet +# include +# include +# include "../libftx/libftx.h" +# include "../data/data.h" +# include "../redirection/redirection.h" +#endif diff --git a/syntax/syntax.c b/syntax/syntax.c index 3875582..6b28cf2 100644 --- a/syntax/syntax.c +++ b/syntax/syntax.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* syntatics.c :+: :+: :+: */ +/* syntax.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cchauvet