From c2efcca0fa55307b498e5b70d5f2ce654c759ba9 Mon Sep 17 00:00:00 2001 From: whaffman Date: Wed, 26 Feb 2025 15:45:10 +0100 Subject: [PATCH] norm 2 --- src/parser/parser_get_commands.c | 5 ++-- src/parser/parser_new_command.c | 3 +-- src/prompt/prompt.c | 18 +++++++------- src/redirect/redirect_get_inputs.c | 11 ++++----- src/redirect/redirect_get_outputs.c | 12 +++++----- src/redirect/redirect_new.c | 16 ++++++------- src/redirect/redirect_valid_type.c | 37 ++++++++++++++++------------- src/signal/signal.c | 30 +---------------------- src/signal/signal_init.c.c | 37 +++++++++++++++++++++++++++++ src/utils/check_malloc.c | 5 ++-- src/utils/error_msg.c | 1 - src/utils/free_minishell.c | 6 +---- src/utils/free_safe.c | 6 +---- src/utils/ft_strjoin_safe.c | 1 - 14 files changed, 95 insertions(+), 93 deletions(-) create mode 100644 src/signal/signal_init.c.c diff --git a/src/parser/parser_get_commands.c b/src/parser/parser_get_commands.c index 724db81..7d67d06 100644 --- a/src/parser/parser_get_commands.c +++ b/src/parser/parser_get_commands.c @@ -12,7 +12,7 @@ #include "minishell.h" -static int is_command_token(t_token *token) +static int is_command_token(t_token *token) { return (token->type < 3 || redirect_token_type(token)); } @@ -31,7 +31,8 @@ t_list *parser_get_commands(t_minishell *minishell) while (current) { token = (t_token *) current->content; - command = parser_command_new(minishell, ft_strdup_safe(minishell, token->value)); + command = parser_command_new(minishell, + ft_strdup_safe(minishell, token->value)); command->args = parser_get_arguments(current, minishell); command->redirect_in = redirect_get_inputs(minishell, current); command->redirect_out = redirect_get_outputs(minishell, current); diff --git a/src/parser/parser_new_command.c b/src/parser/parser_new_command.c index 7b4e45f..f162d67 100644 --- a/src/parser/parser_new_command.c +++ b/src/parser/parser_new_command.c @@ -6,7 +6,7 @@ /* By: qmennen +#+ */ /* +#+ */ /* Created: 2025/02/11 16:18:21 by qmennen #+# #+# */ -/* Updated: 2025/02/25 14:41:57 by whaffman ######## odam.nl */ +/* Updated: 2025/02/26 15:32:45 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -17,7 +17,6 @@ t_command *parser_command_new(t_minishell *minishell, char *cmd) t_command *command; command = malloc_safe(minishell, sizeof(t_command)); -\ command->args = NULL; command->fd_in = 0; command->fd_out = 1; diff --git a/src/prompt/prompt.c b/src/prompt/prompt.c index 8e98282..e0f9195 100644 --- a/src/prompt/prompt.c +++ b/src/prompt/prompt.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ -/* ::: :::::::: */ -/* prompt.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: qmennen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/02/04 16:13:08 by whaffman #+# #+# */ -/* Updated: 2025/02/25 18:41:56 by qmennen ### ########.fr */ +/* :::::::: */ +/* prompt.c :+: :+: */ +/* +:+ */ +/* By: qmennen +#+ */ +/* +#+ */ +/* Created: 2025/02/04 16:13:08 by whaffman #+# #+# */ +/* Updated: 2025/02/26 15:33:15 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -33,7 +33,7 @@ char *get_user(t_minishell *minishell) return (result); } -int get_home_len(t_minishell *minishell, char *cwd) +int get_home_len(t_minishell *minishell, char *cwd) { t_environment *home; int home_len; @@ -83,7 +83,7 @@ char *get_path(t_minishell *minishell) ft_strlcpy(result, BLUE, len); ft_strlcat(result, cwd, len); ft_strlcat(result, RESET "> ", len); - free_safe(minishell,(void **) &cwd); + free_safe(minishell, (void **) &cwd); return (result); } diff --git a/src/redirect/redirect_get_inputs.c b/src/redirect/redirect_get_inputs.c index f68d9af..6e298fb 100644 --- a/src/redirect/redirect_get_inputs.c +++ b/src/redirect/redirect_get_inputs.c @@ -12,7 +12,7 @@ #include "redirect.h" -static void process_heredoc(t_minishell *ms, t_token *heredoc, t_token *delim) +static void process_heredoc(t_minishell *msh, t_token *heredoc, t_token *delim) { char *line; int fd; @@ -20,7 +20,6 @@ static void process_heredoc(t_minishell *ms, t_token *heredoc, t_token *delim) fd = open(".ms_heredoc", O_WRONLY | O_CREAT | O_TRUNC, 0644); if (fd < 0) //TODO: Will this work? return ; - while (TRUE) { line = readline(">"); @@ -33,14 +32,14 @@ static void process_heredoc(t_minishell *ms, t_token *heredoc, t_token *delim) } close(fd); heredoc->type = T_REDIRECT_IN; - delim->value = ft_strdup_safe(ms, ".ms_heredoc"); + delim->value = ft_strdup_safe(msh, ".ms_heredoc"); } t_list *redirect_get_inputs(t_minishell *minishell, t_list *list) { - t_list *current; - t_list *redirects; - t_token *token; + t_list *current; + t_list *redirects; + t_token *token; redirects = NULL; current = list; diff --git a/src/redirect/redirect_get_outputs.c b/src/redirect/redirect_get_outputs.c index f1f884c..e2cca75 100644 --- a/src/redirect/redirect_get_outputs.c +++ b/src/redirect/redirect_get_outputs.c @@ -12,11 +12,11 @@ #include "minishell.h" -t_list *redirect_get_outputs(t_minishell *minishell, t_list *list) +t_list *redirect_get_outputs(t_minishell *msh, t_list *list) { - t_list *current; - t_list *redirects; - t_token *token; + t_list *current; + t_list *redirects; + t_token *token; redirects = NULL; current = list; @@ -33,13 +33,13 @@ t_list *redirect_get_outputs(t_minishell *minishell, t_list *list) if (redirect_is_valid(current, token)) { ft_lstadd_front(&redirects, - ft_lstnew_safe(minishell, redirect_new(minishell, token->type, ft_strdup_safe(minishell, ((t_token *)current->next->content)->value)))); + ft_lstnew_safe(msh, redirect_new(msh, token->type, ft_strdup_safe(msh, ((t_token *)current->next->content)->value)))); current = current->next; continue ; } else { - ft_lstadd_front(&redirects, ft_lstnew_safe(minishell, redirect_new(minishell, T_ERROR, NULL))); + ft_lstadd_front(&redirects, ft_lstnew_safe(msh, redirect_new(msh, T_ERROR, NULL))); break ; } current = current->next; diff --git a/src/redirect/redirect_new.c b/src/redirect/redirect_new.c index e6c9ccc..662d337 100644 --- a/src/redirect/redirect_new.c +++ b/src/redirect/redirect_new.c @@ -12,14 +12,14 @@ #include "minishell.h" -t_redirect *redirect_new(t_minishell * minishell, t_token_type type, char *value) +t_redirect *redirect_new(t_minishell *msh, t_token_type type, char *value) { - t_redirect *result; + t_redirect *result; - result = malloc_safe(minishell, sizeof(t_redirect)); - result->type = type; - result->value = NULL; - if (value) - result->value = value; - return (result); + result = malloc_safe(msh, sizeof(t_redirect)); + result->type = type; + result->value = NULL; + if (value) + result->value = value; + return (result); } diff --git a/src/redirect/redirect_valid_type.c b/src/redirect/redirect_valid_type.c index 2554d32..5f5f9de 100644 --- a/src/redirect/redirect_valid_type.c +++ b/src/redirect/redirect_valid_type.c @@ -12,26 +12,31 @@ #include "minishell.h" -int redirect_token_type(t_token *token) +int redirect_token_type(t_token *token) { - return (token->type == T_REDIRECT_IN || token->type == T_HEREDOC || - token->type == T_REDIRECT_OUT || token->type == T_APPEND_OUT); + return (token->type == T_REDIRECT_IN + || token->type == T_HEREDOC + || token->type == T_REDIRECT_OUT + || token->type == T_APPEND_OUT); } -int redirect_is_valid(t_list *lst, t_token *token) +int redirect_is_valid(t_list *lst, t_token *token) { - t_token *next; - - if (!lst->next) - return (0); - next = (t_token *)lst->next->content; - if (!next) - return (0); - return (redirect_token_type(token) && next->type < 3); -} + t_token *next; -int redirect_is_delimiter(t_token *token) + if (!lst->next) + return (0); + next = (t_token *)lst->next->content; + if (!next) + return (0); + return (redirect_token_type(token) && next->type < 3); +} + +int redirect_is_delimiter(t_token *token) { - return (token->type == T_PIPE || token->type == T_AND || - token->type == T_OR || token->type == T_EOF || token->type == T_ERROR); + return (token->type == T_PIPE + || token->type == T_AND + || token->type == T_OR + || token->type == T_EOF + || token->type == T_ERROR); } \ No newline at end of file diff --git a/src/signal/signal.c b/src/signal/signal.c index 0b81082..edc72ca 100644 --- a/src/signal/signal.c +++ b/src/signal/signal.c @@ -6,13 +6,12 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/19 12:18:47 by whaffman #+# #+# */ -/* Updated: 2025/02/19 17:30:12 by whaffman ######## odam.nl */ +/* Updated: 2025/02/26 15:42:30 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" - void sigint_minishell_handler(int signum) { (void)signum; @@ -36,30 +35,3 @@ void sig_parent_handler(int signum) (void)signum; ft_putstr_fd("\n", 1); } - - -void signal_init_minishell(void) -{ - signal(SIGINT, sigint_minishell_handler); - signal(SIGQUIT, SIG_IGN); -} - -void signal_init_parent(void) -{ - signal(SIGINT, sig_parent_handler); - signal(SIGQUIT, sig_parent_handler); -} - - -void signal_init_child(void) -{ - signal(SIGINT, SIG_DFL); - signal(SIGQUIT, SIG_DFL); -} - - -void signal_init_heredoc(void) -{ - signal(SIGINT, sigint_heredoc_handler); - signal(SIGQUIT, SIG_IGN); -} diff --git a/src/signal/signal_init.c.c b/src/signal/signal_init.c.c new file mode 100644 index 0000000..0f5133c --- /dev/null +++ b/src/signal/signal_init.c.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* signal_init.c.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/02/26 15:39:01 by whaffman #+# #+# */ +/* Updated: 2025/02/26 15:39:46 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void signal_init_minishell(void) +{ + signal(SIGINT, sigint_minishell_handler); + signal(SIGQUIT, SIG_IGN); +} + +void signal_init_parent(void) +{ + signal(SIGINT, sig_parent_handler); + signal(SIGQUIT, sig_parent_handler); +} + +void signal_init_child(void) +{ + signal(SIGINT, SIG_DFL); + signal(SIGQUIT, SIG_DFL); +} + +void signal_init_heredoc(void) +{ + signal(SIGINT, sigint_heredoc_handler); + signal(SIGQUIT, SIG_IGN); +} diff --git a/src/utils/check_malloc.c b/src/utils/check_malloc.c index 4607767..72cff69 100644 --- a/src/utils/check_malloc.c +++ b/src/utils/check_malloc.c @@ -12,10 +12,9 @@ #include "minishell.h" - -void check_malloc(t_minishell *minishell, void *ptr) +void check_malloc(t_minishell *minishell, void *ptr) { - t_list *new; + t_list *new; if (ptr == NULL) { diff --git a/src/utils/error_msg.c b/src/utils/error_msg.c index 6442019..2ce1ed1 100644 --- a/src/utils/error_msg.c +++ b/src/utils/error_msg.c @@ -12,7 +12,6 @@ #include "minishell.h" - void error_msg(char *func, char *msg) { if (errno) diff --git a/src/utils/free_minishell.c b/src/utils/free_minishell.c index 1b8e567..fc4db5a 100644 --- a/src/utils/free_minishell.c +++ b/src/utils/free_minishell.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/05 16:03:57 by whaffman #+# #+# */ -/* Updated: 2025/02/25 16:02:57 by whaffman ######## odam.nl */ +/* Updated: 2025/02/26 15:43:00 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,10 +14,6 @@ void free_minishell(t_minishell **minishell) { - // if ((*minishell)->line) - // free_minishell_line(*minishell); - // if ((*minishell)->environment) - // environment_free_list(*minishell); free_freelist(*minishell); free(*minishell); *minishell = NULL; diff --git a/src/utils/free_safe.c b/src/utils/free_safe.c index bae7ef2..53faa28 100644 --- a/src/utils/free_safe.c +++ b/src/utils/free_safe.c @@ -6,16 +6,12 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/25 15:09:34 by whaffman #+# #+# */ -/* Updated: 2025/02/25 18:06:06 by whaffman ######## odam.nl */ +/* Updated: 2025/02/26 15:43:08 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -#include -#include -#include - void free_safe(t_minishell *minishell, void **ptr) { diff --git a/src/utils/ft_strjoin_safe.c b/src/utils/ft_strjoin_safe.c index 655c85b..cf596c8 100644 --- a/src/utils/ft_strjoin_safe.c +++ b/src/utils/ft_strjoin_safe.c @@ -20,4 +20,3 @@ char *ft_strjoin_safe(t_minishell *minishell, const char *s1, const char *s2) check_malloc(minishell, new_str); return (new_str); } -