From ab72bd5bbb22ff933bf4213329244bd843cf680d Mon Sep 17 00:00:00 2001 From: whaffman Date: Tue, 25 Feb 2025 14:54:17 +0100 Subject: [PATCH] malloc_safe - add freelist to minishell - add t_minishell *minishell to all function calling malloc_free - substituted all malloc calls but the first to malloc_safe --- inc/environment.h | 17 +++++----- inc/executor.h | 8 ++--- inc/expander.h | 7 ++-- inc/parser.h | 2 +- inc/redirect.h | 8 ++--- inc/tokenizer.h | 26 +++++++-------- inc/typedef.h | 15 +++++---- inc/utils.h | 8 ++--- sources.mk | 42 +++++++++++++----------- src/builtin/builtin_cd.c | 4 +-- src/builtin/builtin_env.c | 4 +-- src/builtin/builtin_export.c | 6 ++-- src/builtin/builtin_unset.c | 6 ++-- src/environment/environment_add.c | 8 +++-- src/environment/environment_del.c | 8 ++--- src/environment/environment_free_list.c | 6 ++-- src/environment/environment_get.c | 6 ++-- src/environment/environment_get_arr.c | 14 ++++---- src/environment/environment_parse.c | 8 ++--- src/environment/environment_print.c | 11 ++----- src/executor/executor_absolute_path.c | 10 +++--- src/executor/executor_child.c | 9 ++--- src/executor/executor_create_pipes.c | 4 +-- src/executor/executor_execute_pipeline.c | 4 +-- src/executor/executor_fork.c | 6 ++-- src/expander/expander_allocate_memory.c | 23 +++++++------ src/expander/expander_get_variable.c | 16 ++++----- src/expander/expander_parse_string.c | 16 ++++----- src/lexer/lexer_new.c | 27 +++++++-------- src/lexer/lexer_parse_input.c | 20 ++++++----- src/lexer/lexer_read_word.c | 24 +++++++------- src/lexer/lexer_token_next.c | 16 ++++----- src/main.c | 6 ++-- src/parser/parser_get_arguments.c | 9 ++--- src/parser/parser_get_commands.c | 6 ++-- src/parser/parser_new_command.c | 12 +++---- src/prompt/prompt.c | 35 ++++++++------------ src/redirect/redirect_get_inputs.c | 10 +++--- src/redirect/redirect_get_outputs.c | 8 ++--- src/redirect/redirect_new.c | 6 ++-- src/token/token_new.c | 23 +++++-------- src/token/token_parse.c | 18 +++++----- src/utils/check_malloc.c | 8 ++++- src/utils/free_freelist.c | 18 ++++++++++ src/utils/free_minishell.c | 4 +-- src/utils/ft_malloc_safe.c | 4 +-- src/utils/ft_strdup_safe.c | 4 +-- src/utils/ft_strjoin_safe.c | 4 +-- src/utils/init_minishell.c | 3 +- 49 files changed, 289 insertions(+), 278 deletions(-) create mode 100644 src/utils/free_freelist.c diff --git a/inc/environment.h b/inc/environment.h index d98c3c3..c8a467b 100644 --- a/inc/environment.h +++ b/inc/environment.h @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/04 16:26:35 by whaffman #+# #+# */ -/* Updated: 2025/02/11 15:39:15 by whaffman ######## odam.nl */ +/* Updated: 2025/02/25 13:31:55 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -15,13 +15,14 @@ # include "minishell.h" -void environment_add(t_list **environment, char *name, char *value); -void environment_print(t_list *environment); -t_environment *environment_get(t_list *environment, char *name); -void environment_free_list(t_list **environment); -int environment_parse(char **envp, t_list **environment); -char **environment_get_arr(t_list *environment); -void environment_del(t_list **environment, char *name); +void environment_add(t_minishell *minishell, + char *name, char *value); +void environment_print(t_minishell *minishell); +t_environment *environment_get(t_minishell *minishell, char *name); +void environment_free_list(t_minishell *minishell); +int environment_parse(t_minishell *minishell, char **envp); +char **environment_get_arr(t_minishell *minishell); +void environment_del(t_minishell *minishell, char *name); void environment_free(void *content); #endif // ENVIRONMENT_H diff --git a/inc/executor.h b/inc/executor.h index 82ec74f..d305646 100644 --- a/inc/executor.h +++ b/inc/executor.h @@ -6,7 +6,7 @@ /* By: willem +#+ */ /* +#+ */ /* Created: 2025/02/08 17:06:07 by willem #+# #+# */ -/* Updated: 2025/02/21 13:16:29 by whaffman ######## odam.nl */ +/* Updated: 2025/02/25 13:52:05 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -15,9 +15,9 @@ # include "minishell.h" -char *executor_absolute_path(t_list *env, char *cmd); -void executor_child(t_command *command); -pid_t executor_fork(t_command *command); +char *executor_absolute_path(t_minishell *minishell, char *cmd); +void executor_child(t_minishell *minishell, t_command *command); +pid_t executor_fork(t_minishell *minishell, t_command *command); void executor_create_pipes(t_minishell *minishell); int executor_execute_pipeline(t_minishell *minishell); void executor_close_fds(int n_fds); diff --git a/inc/expander.h b/inc/expander.h index e491c6e..db6158a 100644 --- a/inc/expander.h +++ b/inc/expander.h @@ -6,7 +6,7 @@ /* By: qmennen +#+ */ /* +#+ */ /* Created: 2025/02/18 19:00:51 by qmennen #+# #+# */ -/* Updated: 2025/02/21 16:13:32 by whaffman ######## odam.nl */ +/* Updated: 2025/02/25 14:32:18 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -19,7 +19,10 @@ t_environment *expander_get_var(const char *s, int idx, t_minishell *minishell); t_list *expander_parse_variables(const char *s, t_minishell *minishell); -char *expander_allocate_memory(const char *s, t_list *variables); +char *expander_allocate_memory( + t_minishell *minishell, + const char *s, + t_list *variables); char *expander_parse_string(char *s, t_minishell *minishell); int expander_character_valid(const char c); int expander_expand_dollar(char *src, char *dest, diff --git a/inc/parser.h b/inc/parser.h index aafa02a..13250e4 100644 --- a/inc/parser.h +++ b/inc/parser.h @@ -15,7 +15,7 @@ # include "minishell.h" -t_command *parser_command_new(char *cmd); +t_command *parser_command_new(t_minishell *minishell, char *cmd); char **parser_get_arguments(t_list *list, t_minishell *minishell); t_list *parser_get_commands(t_minishell *minishell); t_list *parser_get_input_redirects(t_list *list); diff --git a/inc/redirect.h b/inc/redirect.h index 5cffd36..d9b5911 100644 --- a/inc/redirect.h +++ b/inc/redirect.h @@ -14,11 +14,11 @@ # define REDIRECT_H # include "minishell.h" -t_redirect *redirect_new(t_token_type type, char *value); -t_list *redirect_get_inputs(t_list *list); -t_list *redirect_get_outputs(t_list *list); +t_redirect *redirect_new(t_minishell *minishell, t_token_type type, char *value); +t_list *redirect_get_inputs(t_minishell *minishell, t_list *list); +t_list *redirect_get_outputs(t_minishell *minishell, t_list *list); int redirect_is_valid(t_list *lst, t_token *token); int redirect_token_type(t_token *token); int redirect_is_delimiter(t_token *token); -#endif \ No newline at end of file +#endif diff --git a/inc/tokenizer.h b/inc/tokenizer.h index 306bc03..37ccacf 100644 --- a/inc/tokenizer.h +++ b/inc/tokenizer.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ -/* ::: :::::::: */ -/* tokenizer.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: qmennen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/02/05 12:36:00 by whaffman #+# #+# */ -/* Updated: 2025/02/18 17:02:17 by qmennen ### ########.fr */ +/* :::::::: */ +/* tokenizer.h :+: :+: */ +/* +:+ */ +/* By: qmennen +#+ */ +/* +#+ */ +/* Created: 2025/02/05 12:36:00 by whaffman #+# #+# */ +/* Updated: 2025/02/25 14:45:15 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -16,18 +16,18 @@ /** * Lexer */ -t_lexer *ft_lexer_new(const char *input); +t_lexer *ft_lexer_new(t_minishell *minishell); void ft_lexer_free(t_lexer *lexer); void lexer_readchar(t_lexer *lexer); -char *lexer_readword(t_lexer *lexer); -t_list *ft_parse_input(t_lexer *lexer); +char *lexer_readword(t_minishell *minishell, t_lexer *lexer); +t_list *ft_parse_input(t_minishell *minishell); /** * Token */ -t_token *ft_token_next(t_lexer *lexer); -t_token *token_new(t_token_type type, char *c, int pos); +t_token *ft_token_next(t_minishell *minishell, t_lexer *lexer); +t_token *token_new(t_minishell *minishell, t_token_type type, char *c, int pos); void ft_token_free(t_token *token); void ft_clear_tokenlist(void *content); -t_token *token_parse(t_lexer *lexer); +t_token *token_parse(t_minishell *minishell, t_lexer *lexer); #endif // TOKENIZER_H diff --git a/inc/typedef.h b/inc/typedef.h index a5e6799..53d848c 100644 --- a/inc/typedef.h +++ b/inc/typedef.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ -/* ::: :::::::: */ -/* typedef.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: qmennen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/02/05 12:36:08 by whaffman #+# #+# */ -/* Updated: 2025/02/20 16:45:55 by qmennen ### ########.fr */ +/* :::::::: */ +/* typedef.h :+: :+: */ +/* +:+ */ +/* By: qmennen +#+ */ +/* +#+ */ +/* Created: 2025/02/05 12:36:08 by whaffman #+# #+# */ +/* Updated: 2025/02/25 13:23:12 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -78,6 +78,7 @@ typedef struct s_minishell t_lexer *lexer; t_list *tokens; t_list *commands; + t_list *freelist; } t_minishell; typedef int (*t_builtin_fn)(t_minishell *, t_command *); diff --git a/inc/utils.h b/inc/utils.h index cf269ea..30a6dab 100644 --- a/inc/utils.h +++ b/inc/utils.h @@ -21,9 +21,9 @@ void print_banner(void); void print_list(void *content); void simple_builtins(t_minishell *minishell); void error_msg(char *func, char *msg); -void check_malloc(void *ptr); -char *ft_strdup_safe(const char *str); -char *ft_strjoin_safe(const char *s1, const char *s2); -void *ft_malloc_safe(size_t size); +void check_malloc(t_minishell *minishell, void *ptr); +char *ft_strdup_safe(t_minishell *minishell, const char *str); +char *ft_strjoin_safe(t_minishell *minishell, const char *s1, const char *s2); +void *malloc_safe(t_minishell *minishell, size_t size); #endif // UTILS_H diff --git a/sources.mk b/sources.mk index 862e5a7..391e82e 100644 --- a/sources.mk +++ b/sources.mk @@ -1,20 +1,22 @@ -VPATH = src:src/parser:src/environment:src/lexer:src/debug:src/token:src/signal:src/prompt:src/utils:src/expander:src/executor:src/builtin: -SOURCES = parser_get_arguments.c parser_new_command.c parser_get_commands.c \ -environment_get.c environment_get_arr.c environment_free_list.c \ -environment_add.c environment_print.c environment_del.c \ -environment_free.c environment_parse.c lexer_new.c lexer_read_word.c \ -lexer_read_char.c lexer_parse_input.c lexer_token_next.c \ -print_commands.c main.c token_new.c token_parse.c signal.c \ -history_write.c prompt.c history_load.c ft_malloc_safe.c \ -free_command_list.c free_minishell.c free_lexer.c print_banner.c \ -check_malloc.c error_msg.c free_token_list.c free_minishell_line.c \ -ft_strdup_safe.c free_token.c init_minishell.c ft_strjoin_safe.c \ -expander_allocate_memory.c expander_parse_string.c \ -expander_parse_variables.c expander_expand_dollar.c \ -expander_is_character.c expander_get_variable.c executor_open_fds.c \ -executor_count_fds.c executor_fork.c executor_absolute_path.c \ -executor_execute_pipeline.c executor_child.c executor_close_fds.c \ -executor_create_redirects.c executor_create_pipes.c builtin_export.c \ -simple_builtins.c builtin_router.c is_builtin.c builtin_unset.c \ -builtin_env.c builtin_cd.c builtin_pwd.c builtin_exit.c \ -builtin_echo.c \ +VPATH = src:src/prompt:src/utils:src/lexer:src/token:src/environment:src/executor:src/parser:src/signal:src/debug:src/expander:src/builtin:src/redirect: +SOURCES = history_load.c history_write.c prompt.c free_lexer.c free_token.c \ +free_token_list.c free_minishell.c free_minishell_line.c \ +init_minishell.c check_malloc.c error_msg.c free_command_list.c \ +ft_malloc_safe.c ft_strdup_safe.c ft_strjoin_safe.c print_banner.c \ +free_freelist.c lexer_read_char.c lexer_token_next.c \ +lexer_parse_input.c lexer_new.c lexer_read_word.c token_new.c \ +token_parse.c environment_free.c environment_del.c \ +environment_free_list.c environment_get.c environment_parse.c \ +environment_add.c environment_get_arr.c environment_print.c \ +executor_close_fds.c executor_child.c executor_fork.c \ +executor_absolute_path.c executor_execute_pipeline.c \ +executor_count_fds.c executor_create_redirects.c executor_open_fds.c \ +executor_create_pipes.c parser_get_arguments.c parser_new_command.c \ +parser_get_commands.c signal.c print_commands.c \ +expander_expand_dollar.c expander_get_variable.c \ +expander_is_character.c expander_parse_variables.c \ +expander_parse_string.c expander_allocate_memory.c builtin_cd.c \ +builtin_echo.c builtin_env.c builtin_export.c builtin_pwd.c \ +builtin_router.c builtin_unset.c simple_builtins.c builtin_exit.c \ +is_builtin.c main.c redirect_get_inputs.c redirect_get_outputs.c \ +redirect_new.c redirect_valid_type.c \ diff --git a/src/builtin/builtin_cd.c b/src/builtin/builtin_cd.c index 664cea8..1075101 100644 --- a/src/builtin/builtin_cd.c +++ b/src/builtin/builtin_cd.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/20 11:33:07 by whaffman #+# #+# */ -/* Updated: 2025/02/20 12:49:36 by whaffman ######## odam.nl */ +/* Updated: 2025/02/25 13:57:57 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ int builtin_cd(t_minishell *minishell, t_command *cmd) if (cmd->args[1] == NULL) { - env = environment_get(minishell->environment, "HOME"); + env = environment_get(minishell, "HOME"); if (env == NULL || env->value == NULL) { ft_putendl_fd("minishell: cd: HOME not set", STDERR_FILENO); diff --git a/src/builtin/builtin_env.c b/src/builtin/builtin_env.c index 42a119a..0888c66 100644 --- a/src/builtin/builtin_env.c +++ b/src/builtin/builtin_env.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/20 11:33:02 by whaffman #+# #+# */ -/* Updated: 2025/02/20 12:17:10 by whaffman ######## odam.nl */ +/* Updated: 2025/02/25 13:57:49 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -15,6 +15,6 @@ int builtin_env(t_minishell *minishell, t_command *cmd) { (void)cmd; - environment_print(minishell->environment); + environment_print(minishell); return (SUCCESS); } diff --git a/src/builtin/builtin_export.c b/src/builtin/builtin_export.c index 38dccbc..acdc31a 100644 --- a/src/builtin/builtin_export.c +++ b/src/builtin/builtin_export.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/20 11:32:53 by whaffman #+# #+# */ -/* Updated: 2025/02/20 12:30:21 by whaffman ######## odam.nl */ +/* Updated: 2025/02/25 13:57:08 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -28,14 +28,14 @@ int builtin_export(t_minishell *minishell, t_command *cmd) i++; continue ; } - env = environment_get(minishell->environment, arr[0]); + env = environment_get(minishell, arr[0]); if (env != NULL) { free(env->value); env->value = ft_strdup(arr[1]); //TODO: malloc check } else - environment_add(&(minishell->environment), arr[0], arr[1]); + environment_add(minishell, arr[0], arr[1]); ft_free_arr(arr); i++; } diff --git a/src/builtin/builtin_unset.c b/src/builtin/builtin_unset.c index 9bef151..474e2ea 100644 --- a/src/builtin/builtin_unset.c +++ b/src/builtin/builtin_unset.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/20 11:25:43 by whaffman #+# #+# */ -/* Updated: 2025/02/20 15:04:59 by whaffman ######## odam.nl */ +/* Updated: 2025/02/25 13:57:29 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -19,8 +19,8 @@ int builtin_unset(t_minishell *minishell, t_command *cmd) i = 1; while (cmd->args[i] != NULL) { - if (environment_get(minishell->environment, cmd->args[i]) != NULL) - environment_del(&(minishell->environment), cmd->args[i]); + if (environment_get(minishell, cmd->args[i]) != NULL) + environment_del(minishell, cmd->args[i]); i++; } return (SUCCESS); diff --git a/src/environment/environment_add.c b/src/environment/environment_add.c index 1f20b01..a2e395e 100644 --- a/src/environment/environment_add.c +++ b/src/environment/environment_add.c @@ -6,20 +6,22 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/04 16:13:33 by whaffman #+# #+# */ -/* Updated: 2025/02/12 21:33:20 by willem ######## odam.nl */ +/* Updated: 2025/02/25 14:28:43 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -void environment_add(t_list **environment, char *name, char *value) +void environment_add(t_minishell *minishell, char *name, char *value) { t_environment *new_environment; t_list *new_node; + t_list **environment; + environment = &minishell->environment; if (name != NULL && value != NULL) { - new_environment = malloc(sizeof(t_environment)); + new_environment = malloc_safe(minishell, sizeof(t_environment)); if (new_environment == NULL) return (perror("malloc")); new_environment->name = ft_strdup(name); diff --git a/src/environment/environment_del.c b/src/environment/environment_del.c index 6a7fd35..0238a1a 100644 --- a/src/environment/environment_del.c +++ b/src/environment/environment_del.c @@ -6,13 +6,13 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/11 14:17:53 by whaffman #+# #+# */ -/* Updated: 2025/02/11 17:20:26 by whaffman ######## odam.nl */ +/* Updated: 2025/02/25 13:41:06 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -void environment_del(t_list **environment, char *name) +void environment_del(t_minishell *minishell, char *name) { t_list *prev; t_list *current; @@ -21,7 +21,7 @@ void environment_del(t_list **environment, char *name) prev = NULL; next = NULL; - current = *environment; + current = minishell->environment; while (current != NULL) { env = (t_environment *)current->content; @@ -31,7 +31,7 @@ void environment_del(t_list **environment, char *name) environment_free(current->content); free(current); if (prev == NULL) - *environment = next; + minishell->environment = next; else prev->next = next; return ; diff --git a/src/environment/environment_free_list.c b/src/environment/environment_free_list.c index 2e1905c..022189e 100644 --- a/src/environment/environment_free_list.c +++ b/src/environment/environment_free_list.c @@ -6,13 +6,13 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/04 16:13:59 by whaffman #+# #+# */ -/* Updated: 2025/02/11 17:18:27 by whaffman ######## odam.nl */ +/* Updated: 2025/02/25 13:37:21 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -void environment_free_list(t_list **environment) +void environment_free_list(t_minishell *minishell) { - ft_lstclear(environment, environment_free); + ft_lstclear(&(minishell->environment), environment_free); } diff --git a/src/environment/environment_get.c b/src/environment/environment_get.c index 280698e..beb2315 100644 --- a/src/environment/environment_get.c +++ b/src/environment/environment_get.c @@ -6,16 +6,18 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/04 16:15:05 by whaffman #+# #+# */ -/* Updated: 2025/02/11 17:04:28 by whaffman ######## odam.nl */ +/* Updated: 2025/02/25 13:35:13 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -t_environment *environment_get(t_list *environment, char *name) +t_environment *environment_get(t_minishell *minishell, char *name) { t_environment *env; + t_list *environment; + environment = minishell->environment; while (environment != NULL) { env = (t_environment *)environment->content; diff --git a/src/environment/environment_get_arr.c b/src/environment/environment_get_arr.c index 8052e04..c7d9b68 100644 --- a/src/environment/environment_get_arr.c +++ b/src/environment/environment_get_arr.c @@ -6,28 +6,26 @@ /* By: willem +#+ */ /* +#+ */ /* Created: 2025/02/08 13:59:48 by willem #+# #+# */ -/* Updated: 2025/02/11 17:18:15 by whaffman ######## odam.nl */ +/* Updated: 2025/02/25 14:29:49 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -char **environment_get_arr(t_list *environment) +char **environment_get_arr(t_minishell *minishell) { char **arr; t_environment *env; int i; + t_list *environment; - arr = malloc(sizeof(char *) * (ft_lstsize(environment) + 1)); - if (arr == NULL) - return (NULL); + environment = minishell->environment; + arr = malloc_safe(minishell, sizeof(char *) * (ft_lstsize(environment) + 1)); i = 0; while (environment != NULL) { env = (t_environment *)environment->content; - arr[i] = malloc(ft_strlen(env->name) + ft_strlen(env->value) + 2); - if (arr[i] == NULL) - return (ft_free_arr(arr), NULL); + arr[i] = malloc_safe(minishell, ft_strlen(env->name) + ft_strlen(env->value) + 2); ft_strlcpy(arr[i], env->name, ft_strlen(env->name) + 1); ft_strlcat(arr[i], "=", ft_strlen(env->name) + 2); ft_strlcat(arr[i], env->value, diff --git a/src/environment/environment_parse.c b/src/environment/environment_parse.c index 3abd042..d4a436f 100644 --- a/src/environment/environment_parse.c +++ b/src/environment/environment_parse.c @@ -6,23 +6,23 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/05 15:52:33 by whaffman #+# #+# */ -/* Updated: 2025/02/11 17:18:43 by whaffman ######## odam.nl */ +/* Updated: 2025/02/25 13:42:22 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -int environment_parse(char **envp, t_list **environment) +int environment_parse(t_minishell *minishell, char **envp) { char **env; - *environment = NULL; + if (envp == NULL) return (FAILURE); while (*envp != NULL) { env = ft_split(*envp, '='); - environment_add(environment, env[0], env[1]); + environment_add(minishell, env[0], env[1]); ft_free_arr(env); envp++; } diff --git a/src/environment/environment_print.c b/src/environment/environment_print.c index e5c7b52..7720ec4 100644 --- a/src/environment/environment_print.c +++ b/src/environment/environment_print.c @@ -6,23 +6,18 @@ /* By: willem +#+ */ /* +#+ */ /* Created: 2025/02/08 13:52:08 by willem #+# #+# */ -/* Updated: 2025/02/11 17:05:10 by whaffman ######## odam.nl */ +/* Updated: 2025/02/25 13:39:45 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -void environment_print(t_list *environment) +void environment_print(t_minishell *minishell) { char **arr; int i; - arr = environment_get_arr(environment); - if (arr == NULL) - { - perror("malloc"); - return ; - } + arr = environment_get_arr(minishell); i = 0; while (arr[i] != NULL) { diff --git a/src/executor/executor_absolute_path.c b/src/executor/executor_absolute_path.c index 178d72a..e4af0d6 100644 --- a/src/executor/executor_absolute_path.c +++ b/src/executor/executor_absolute_path.c @@ -6,13 +6,13 @@ /* By: willem +#+ */ /* +#+ */ /* Created: 2025/02/08 17:00:24 by willem #+# #+# */ -/* Updated: 2025/02/13 15:04:09 by whaffman ######## odam.nl */ +/* Updated: 2025/02/25 14:30:18 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -char *executor_absolute_path(t_list *env, char *cmd) +char *executor_absolute_path(t_minishell *minishell, char *cmd) { char **path; t_environment *path_env; @@ -30,16 +30,14 @@ char *executor_absolute_path(t_list *env, char *cmd) } return (NULL); } - path_env = environment_get(env, "PATH"); + path_env = environment_get(minishell, "PATH"); if (!path_env) return (NULL); path = ft_split(path_env->value, ':'); i = 0; while (path[i] != NULL) { - executable = malloc(ft_strlen(path[i]) + ft_strlen(cmd) + 2); - if (!executable) - return (ft_free_arr(path), NULL); + executable = malloc_safe(minishell, ft_strlen(path[i]) + ft_strlen(cmd) + 2); ft_strlcpy(executable, path[i], ft_strlen(path[i]) + 1); ft_strlcat(executable, "/", ft_strlen(path[i]) + 2); ft_strlcat(executable, cmd, ft_strlen(path[i]) + ft_strlen(cmd) + 2); diff --git a/src/executor/executor_child.c b/src/executor/executor_child.c index b69206b..125acbb 100644 --- a/src/executor/executor_child.c +++ b/src/executor/executor_child.c @@ -6,21 +6,22 @@ /* By: willem +#+ */ /* +#+ */ /* Created: 2025/02/12 21:25:10 by willem #+# #+# */ -/* Updated: 2025/02/19 12:56:03 by whaffman ######## odam.nl */ +/* Updated: 2025/02/25 13:52:53 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -void executor_child(t_command *command) +void executor_child(t_minishell *minishell, t_command *command) { char *path; + if (command->fd_in != 0) dup2(command->fd_in, 0); if (command->fd_out != 1) dup2(command->fd_out, 1); executor_close_fds(command->n_fds); - path = executor_absolute_path(command->environment, command->command); + path = executor_absolute_path(minishell, command->command); if (path == NULL) { ft_putstr_fd(RED BOLD, 2); @@ -28,5 +29,5 @@ void executor_child(t_command *command) ft_putstr_fd(": " RESET "command not found\n", 2); return ; } - execve(path, command->args, environment_get_arr(command->environment)); + execve(path, command->args, environment_get_arr(minishell)); } diff --git a/src/executor/executor_create_pipes.c b/src/executor/executor_create_pipes.c index 9cff884..4a17eec 100644 --- a/src/executor/executor_create_pipes.c +++ b/src/executor/executor_create_pipes.c @@ -6,7 +6,7 @@ /* By: willem +#+ */ /* +#+ */ /* Created: 2025/02/12 21:25:22 by willem #+# #+# */ -/* Updated: 2025/02/22 22:25:28 by willem ######## odam.nl */ +/* Updated: 2025/02/25 14:19:57 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -38,7 +38,7 @@ void executor_create_pipes(t_minishell *minishell) command = (t_command *)current->content; if (current->next) { - if (!pipe(fd)) + if (pipe(fd) == -1) error_msg("pipe", "pipe creation failed"); command->fd_out = fd[1]; } diff --git a/src/executor/executor_execute_pipeline.c b/src/executor/executor_execute_pipeline.c index 9c6d49f..7e3335b 100644 --- a/src/executor/executor_execute_pipeline.c +++ b/src/executor/executor_execute_pipeline.c @@ -6,7 +6,7 @@ /* By: willem +#+ */ /* +#+ */ /* Created: 2025/02/12 21:25:02 by willem #+# #+# */ -/* Updated: 2025/02/21 13:11:17 by whaffman ######## odam.nl */ +/* Updated: 2025/02/25 13:52:35 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -27,7 +27,7 @@ int executor_execute_pipeline(t_minishell *minishell) { command = (t_command *)current->content; command->environment = minishell->environment; - last_pid = executor_fork(command); + last_pid = executor_fork(minishell, command); current = current->next; } waitpid(last_pid, &exit_status, 0); diff --git a/src/executor/executor_fork.c b/src/executor/executor_fork.c index 5bd2d22..5da3295 100644 --- a/src/executor/executor_fork.c +++ b/src/executor/executor_fork.c @@ -6,13 +6,13 @@ /* By: willem +#+ */ /* +#+ */ /* Created: 2025/02/12 21:24:52 by willem #+# #+# */ -/* Updated: 2025/02/19 13:40:19 by whaffman ######## odam.nl */ +/* Updated: 2025/02/25 13:51:48 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -pid_t executor_fork(t_command *command) +pid_t executor_fork(t_minishell *minishell, t_command *command) { pid_t pid; // int status; @@ -31,7 +31,7 @@ pid_t executor_fork(t_command *command) else if (pid == 0) { signal_init_child(); - executor_child(command); + executor_child(minishell, command); exit(127); } else diff --git a/src/expander/expander_allocate_memory.c b/src/expander/expander_allocate_memory.c index 286ed37..85689dc 100644 --- a/src/expander/expander_allocate_memory.c +++ b/src/expander/expander_allocate_memory.c @@ -1,18 +1,21 @@ /* ************************************************************************** */ /* */ -/* ::: :::::::: */ -/* expander_allocate_memory.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: qmennen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/02/19 13:57:19 by qmennen #+# #+# */ -/* Updated: 2025/02/19 13:57:36 by qmennen ### ########.fr */ +/* :::::::: */ +/* expander_allocate_memory.c :+: :+: */ +/* +:+ */ +/* By: qmennen +#+ */ +/* +#+ */ +/* Created: 2025/02/19 13:57:19 by qmennen #+# #+# */ +/* Updated: 2025/02/25 14:31:33 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -char *expander_allocate_memory(const char *s, t_list *variables) +char *expander_allocate_memory( + t_minishell *minishell, + const char *s, + t_list *variables) { int size; t_list *current; @@ -26,14 +29,14 @@ char *expander_allocate_memory(const char *s, t_list *variables) if (current->content == NULL) { current = current->next; - continue; + continue ; } env = (t_environment *)current->content; size += (ft_strlen(env->value) - ft_strlen(env->name)); current = current->next; } size += ft_strlen(s); - string = malloc(size); + string = malloc_safe(minishell, size); if (!string) perror("expander malloc"); return (string); diff --git a/src/expander/expander_get_variable.c b/src/expander/expander_get_variable.c index 924798a..edc2202 100644 --- a/src/expander/expander_get_variable.c +++ b/src/expander/expander_get_variable.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ -/* ::: :::::::: */ -/* expander_get_variable.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: qmennen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/02/19 13:59:03 by qmennen #+# #+# */ -/* Updated: 2025/02/19 13:59:14 by qmennen ### ########.fr */ +/* :::::::: */ +/* expander_get_variable.c :+: :+: */ +/* +:+ */ +/* By: qmennen +#+ */ +/* +#+ */ +/* Created: 2025/02/19 13:59:03 by qmennen #+# #+# */ +/* Updated: 2025/02/25 13:47:55 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -24,7 +24,7 @@ t_environment *expander_get_var(const char *s, int idx, t_minishell *minishell) name = ft_substr(s, idx, i); if (!name || !*name) return (NULL); - env = environment_get(minishell->environment, name); + env = environment_get(minishell, name); free(name); return (env); } diff --git a/src/expander/expander_parse_string.c b/src/expander/expander_parse_string.c index 25b7b27..762e2a2 100644 --- a/src/expander/expander_parse_string.c +++ b/src/expander/expander_parse_string.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ -/* ::: :::::::: */ -/* expander_parse_string.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: qmennen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/02/18 19:00:35 by qmennen #+# #+# */ -/* Updated: 2025/02/20 16:38:28 by qmennen ### ########.fr */ +/* :::::::: */ +/* expander_parse_string.c :+: :+: */ +/* +:+ */ +/* By: qmennen +#+ */ +/* +#+ */ +/* Created: 2025/02/18 19:00:35 by qmennen #+# #+# */ +/* Updated: 2025/02/25 14:32:59 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -35,7 +35,7 @@ char *expander_parse_string(char *s, t_minishell *minishell) int j; variables = expander_parse_variables(s, minishell); - string = expander_allocate_memory(s, variables); + string = expander_allocate_memory(minishell, s, variables); i = 0; j = 0; current = variables; diff --git a/src/lexer/lexer_new.c b/src/lexer/lexer_new.c index bbdc7c4..d346375 100644 --- a/src/lexer/lexer_new.c +++ b/src/lexer/lexer_new.c @@ -1,27 +1,24 @@ /* ************************************************************************** */ /* */ -/* ::: :::::::: */ -/* lexer_new.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: qmennen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/02/05 19:03:01 by qmennen #+# #+# */ -/* Updated: 2025/02/05 19:08:41 by qmennen ### ########.fr */ +/* :::::::: */ +/* lexer_new.c :+: :+: */ +/* +:+ */ +/* By: qmennen +#+ */ +/* +#+ */ +/* Created: 2025/02/05 19:03:01 by qmennen #+# #+# */ +/* Updated: 2025/02/25 14:35:35 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -t_lexer *ft_lexer_new(const char *input) +t_lexer *ft_lexer_new(t_minishell *minishell) { - t_lexer *lexer; + char *input; - lexer = malloc(sizeof(t_lexer)); - if (!lexer) - { - perror("failed assigning lexer memory"); - exit(EXIT_FAILURE); - } + t_lexer *lexer; + input = minishell->line; + lexer = malloc_safe(minishell, sizeof(t_lexer)); lexer->input = ft_strdup(input); lexer->pos = 0; lexer->n_pos = 1; diff --git a/src/lexer/lexer_parse_input.c b/src/lexer/lexer_parse_input.c index 2182c21..cfafe3c 100644 --- a/src/lexer/lexer_parse_input.c +++ b/src/lexer/lexer_parse_input.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ -/* ::: :::::::: */ -/* lexer_parse_input.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: qmennen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/02/05 19:09:20 by qmennen #+# #+# */ -/* Updated: 2025/02/20 16:42:39 by qmennen ### ########.fr */ +/* :::::::: */ +/* lexer_parse_input.c :+: :+: */ +/* +:+ */ +/* By: qmennen +#+ */ +/* +#+ */ +/* Created: 2025/02/05 19:09:20 by qmennen #+# #+# */ +/* Updated: 2025/02/25 14:39:37 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -23,16 +23,18 @@ * the input to be parsed. * @return A linked list of tokens parsed from the input. */ -t_list *ft_parse_input(t_lexer *lexer) +t_list *ft_parse_input(t_minishell *minishell) { t_list *list; t_token *token; + t_lexer *lexer; + lexer = minishell->lexer; list = NULL; while (TRUE) { //TODO: Check if unicode support is viable - token = ft_token_next(lexer); + token = ft_token_next(minishell, lexer); if (token->type == T_EOF || token->type == T_ERROR) //TODO T_ERROR removes the inf loop break ; ft_lstadd_back(&list, ft_lstnew(token)); diff --git a/src/lexer/lexer_read_word.c b/src/lexer/lexer_read_word.c index 1bbd225..5621d87 100644 --- a/src/lexer/lexer_read_word.c +++ b/src/lexer/lexer_read_word.c @@ -1,18 +1,18 @@ /* ************************************************************************** */ /* */ -/* ::: :::::::: */ -/* lexer_read_word.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: qmennen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/02/05 19:03:47 by qmennen #+# #+# */ -/* Updated: 2025/02/19 14:12:19 by qmennen ### ########.fr */ +/* :::::::: */ +/* lexer_read_word.c :+: :+: */ +/* +:+ */ +/* By: qmennen +#+ */ +/* +#+ */ +/* Created: 2025/02/05 19:03:47 by qmennen #+# #+# */ +/* Updated: 2025/02/25 14:40:33 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -static char *parse_quotes(t_lexer *lexer) +static char *parse_quotes(t_minishell *minishell, t_lexer *lexer) { int start; int len; @@ -26,7 +26,7 @@ static char *parse_quotes(t_lexer *lexer) while (lexer->current_char != '\0' && lexer->current_char != qc) lexer_readchar(lexer); len = lexer->pos - start; - word = malloc(sizeof(char) * len + 1); + word = malloc_safe(minishell, sizeof(char) * len + 1); ft_strlcpy(word, lexer->input + start, len + 1); if (lexer->current_char == qc) lexer_readchar(lexer); @@ -38,7 +38,7 @@ static char *parse_quotes(t_lexer *lexer) return (word); } -char *lexer_readword(t_lexer *lexer) +char *lexer_readword(t_minishell *minishell, t_lexer *lexer) { int start; int len; @@ -47,7 +47,7 @@ char *lexer_readword(t_lexer *lexer) start = lexer->pos; if (lexer->current_char == '"' || lexer->current_char == '\'') { - return (parse_quotes(lexer)); + return (parse_quotes(minishell, lexer)); } while (ft_isprint(lexer->current_char) && lexer->current_char != '<' && lexer->current_char != '>' && lexer->current_char != '|' @@ -57,7 +57,7 @@ char *lexer_readword(t_lexer *lexer) lexer_readchar(lexer); } len = lexer->pos - start; - word = malloc(sizeof(char) * len + 1); + word = malloc_safe(minishell, sizeof(char) * len + 1); ft_strlcpy(word, lexer->input + start, len + 1); return (word); } diff --git a/src/lexer/lexer_token_next.c b/src/lexer/lexer_token_next.c index d419c8c..f9343fd 100644 --- a/src/lexer/lexer_token_next.c +++ b/src/lexer/lexer_token_next.c @@ -6,7 +6,7 @@ /* By: qmennen +#+ */ /* +#+ */ /* Created: 2025/02/04 16:07:58 by qmennen #+# #+# */ -/* Updated: 2025/02/20 10:39:50 by whaffman ######## odam.nl */ +/* Updated: 2025/02/25 14:45:28 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -42,7 +42,7 @@ static t_token_type get_word_type(char c) * - If it is a printable character, reads the word and creates a word token. * - Otherwise, creates an error token. */ -t_token *ft_token_next(t_lexer *lexer) +t_token *ft_token_next(t_minishell *minishell, t_lexer *lexer) { t_token *token; t_token_type word_type; @@ -54,22 +54,22 @@ t_token *ft_token_next(t_lexer *lexer) lexer_readchar(lexer); current_pos = lexer->pos; if (lexer->current_char == '\0') - token = token_new(T_EOF, NULL, current_pos); + token = token_new(minishell, T_EOF, NULL, current_pos); else if (lexer->current_char == '<' || lexer->current_char == '>' || lexer->current_char == '|') - token = token_parse(lexer); + token = token_parse(minishell, lexer); else if (ft_isprint(lexer->current_char)) { word_type = get_word_type(lexer->current_char); - word = lexer_readword(lexer); + word = lexer_readword(minishell, lexer); if (!word) - return (token_new(T_ERROR, &lexer->current_char, current_pos)); - token = token_new(word_type, word, current_pos); + return (token_new(minishell, T_ERROR, &lexer->current_char, current_pos)); + token = token_new(minishell, word_type, word, current_pos); free(word); } else { - token = token_new(T_ERROR, NULL, current_pos); + token = token_new(minishell, T_ERROR, NULL, current_pos); printf("token->type: %d\n", token->type); } return (token); diff --git a/src/main.c b/src/main.c index 4b3e413..4ef0c29 100644 --- a/src/main.c +++ b/src/main.c @@ -24,14 +24,14 @@ int main(int argc, char **argv, char **envp) history_load(); minishell = init_minishell(); signal_init_minishell(); - environment_parse(envp, &(minishell->environment)); + environment_parse(minishell, envp); while (TRUE) { minishell->line = ft_prompt(minishell); if (minishell->line == NULL) break ; - minishell->lexer = ft_lexer_new(minishell->line); - minishell->tokens = ft_parse_input(minishell->lexer); + minishell->lexer = ft_lexer_new(minishell); + minishell->tokens = ft_parse_input(minishell); //ft_lstiter(minishell->tokens, token_print); minishell->commands = parser_get_commands(minishell); simple_builtins(minishell); diff --git a/src/parser/parser_get_arguments.c b/src/parser/parser_get_arguments.c index 0cdb42b..0eaec91 100644 --- a/src/parser/parser_get_arguments.c +++ b/src/parser/parser_get_arguments.c @@ -6,7 +6,7 @@ /* By: qmennen +#+ */ /* +#+ */ /* Created: 2025/02/11 16:20:09 by qmennen #+# #+# */ -/* Updated: 2025/02/12 12:58:42 by whaffman ######## odam.nl */ +/* Updated: 2025/02/25 14:41:06 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -50,12 +50,7 @@ char **parser_get_arguments(t_list *list, t_minishell *minishell) int i; cmds = count_cmds(list); - args = malloc((cmds + 1) * sizeof(char *)); - if (!args) - { - perror("malloc"); - exit(EXIT_FAILURE); - } + args = malloc_safe(minishell, (cmds + 1) * sizeof(char *)); current = list; i = -1; while ((++i) < cmds && current) diff --git a/src/parser/parser_get_commands.c b/src/parser/parser_get_commands.c index 6725b8c..f66f8bd 100644 --- a/src/parser/parser_get_commands.c +++ b/src/parser/parser_get_commands.c @@ -31,10 +31,10 @@ t_list *parser_get_commands(t_minishell *minishell) while (current) { token = (t_token *) current->content; - command = parser_command_new(ft_strdup(token->value)); + command = parser_command_new(minishell, ft_strdup(token->value)); command->args = parser_get_arguments(current, minishell); - command->redirect_in = redirect_get_inputs(current); - command->redirect_out = redirect_get_outputs(current); + command->redirect_in = redirect_get_inputs(minishell, current); + command->redirect_out = redirect_get_outputs(minishell, current); ft_lstadd_back(&command_list, ft_lstnew(command)); while (current && is_command_token((t_token *)current->content)) current = current->next; diff --git a/src/parser/parser_new_command.c b/src/parser/parser_new_command.c index 5d4ff99..7b4e45f 100644 --- a/src/parser/parser_new_command.c +++ b/src/parser/parser_new_command.c @@ -6,22 +6,18 @@ /* By: qmennen +#+ */ /* +#+ */ /* Created: 2025/02/11 16:18:21 by qmennen #+# #+# */ -/* Updated: 2025/02/21 13:07:47 by whaffman ######## odam.nl */ +/* Updated: 2025/02/25 14:41:57 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -t_command *parser_command_new(char *cmd) +t_command *parser_command_new(t_minishell *minishell, char *cmd) { t_command *command; - command = malloc(sizeof(t_command)); - if (!command) - { - perror("minishell malloc error"); - exit(EXIT_FAILURE); - } + 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 9f05c69..2bcefb4 100644 --- a/src/prompt/prompt.c +++ b/src/prompt/prompt.c @@ -6,13 +6,13 @@ /* By: qmennen +#+ */ /* +#+ */ /* Created: 2025/02/04 16:13:08 by whaffman #+# #+# */ -/* Updated: 2025/02/19 13:44:20 by whaffman ######## odam.nl */ +/* Updated: 2025/02/25 14:43:34 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -char *get_user(t_list *environment) +char *get_user(t_minishell *minishell) { const char guest[] = "guest"; int len; @@ -20,37 +20,32 @@ char *get_user(t_list *environment) char *username; char *result; - user = environment_get(environment, "USER"); + user = environment_get(minishell, "USER"); if (user == NULL) username = (char *)guest; else username = user->value; len = ft_strlen(username) + ft_strlen(PROMPT GREEN) + 1; - result = (char *)malloc(len); - if (result == NULL) - { - perror("malloc"); - return (NULL); - } + result = (char *)malloc_safe(minishell, len); ft_strlcpy(result, GREEN, ft_strlen(GREEN) + 1); ft_strlcat(result, username, ft_strlen(GREEN) + ft_strlen(username) + 1); ft_strlcat(result, PROMPT, len); return (result); } -int get_home_len(char *cwd, t_list *environment) +int get_home_len(t_minishell *minishell, char *cwd) { t_environment *home; int home_len; - home = environment_get(environment, "HOME"); + home = environment_get(minishell, "HOME"); home_len = 0; if (home && !ft_strncmp(cwd, home->value, ft_strlen(home->value))) home_len = ft_strlen(home->value) - 1; return (home_len); } -char *get_path_with_home(t_list *environment) +char *get_path_with_home(t_minishell *minishell) { char *result; char *cwd; @@ -60,11 +55,9 @@ char *get_path_with_home(t_list *environment) cwd = getcwd(NULL, 0); if (cwd == NULL) return (perror("getcwd"), NULL); - home_len = get_home_len(cwd, environment); + home_len = get_home_len(minishell, cwd); len = ft_strlen(cwd) - home_len + 1; - result = malloc(len); - if (result == NULL) - return (free(cwd), NULL); + result = malloc_safe(minishell, len); if (home_len) { ft_strlcpy(result, "~", 2); @@ -76,17 +69,17 @@ char *get_path_with_home(t_list *environment) return (result); } -char *get_path(t_list *environment) +char *get_path(t_minishell *minishell) { char *result; char *cwd; int len; - cwd = get_path_with_home(environment); + cwd = get_path_with_home(minishell); if (cwd == NULL) return (NULL); len = ft_strlen(cwd) + ft_strlen(BLUE RESET "> ") + 1; - result = malloc(len); + result = malloc_safe(minishell, len); if (result == NULL) { perror("malloc"); @@ -106,10 +99,10 @@ char *ft_prompt(t_minishell *minishell) char *prompt; char *user; - cwd = get_path(minishell->environment); + cwd = get_path(minishell); if (cwd == NULL) return (NULL); - user = get_user(minishell->environment); + user = get_user(minishell); if (user == NULL) return (free(cwd), NULL); prompt = ft_strjoin(user, cwd); diff --git a/src/redirect/redirect_get_inputs.c b/src/redirect/redirect_get_inputs.c index b6e0828..e3abc02 100644 --- a/src/redirect/redirect_get_inputs.c +++ b/src/redirect/redirect_get_inputs.c @@ -12,7 +12,7 @@ # include "redirect.h" -t_list *redirect_get_inputs(t_list *list) +t_list *redirect_get_inputs(t_minishell *minishell, t_list *list) { t_list *current; t_list *redirects; @@ -32,16 +32,18 @@ t_list *redirect_get_inputs(t_list *list) } if (redirect_is_valid(current, token)) { - ft_lstadd_front(&redirects, ft_lstnew(redirect_new(token->type, ft_strdup(((t_token *)current->next->content)->value)))); + ft_lstadd_front(&redirects, ft_lstnew( + redirect_new(minishell, token->type, + ft_strdup(((t_token *)current->next->content)->value)))); current = current->next; continue ; } else { - ft_lstadd_front(&redirects, ft_lstnew(redirect_new(T_ERROR, NULL))); + ft_lstadd_front(&redirects, ft_lstnew(redirect_new(minishell, T_ERROR, NULL))); break ; } current = current->next; } return (redirects); -} \ No newline at end of file +} diff --git a/src/redirect/redirect_get_outputs.c b/src/redirect/redirect_get_outputs.c index 8daee58..d65023e 100644 --- a/src/redirect/redirect_get_outputs.c +++ b/src/redirect/redirect_get_outputs.c @@ -12,7 +12,7 @@ #include "minishell.h" -t_list *redirect_get_outputs(t_list *list) +t_list *redirect_get_outputs(t_minishell *minishell, t_list *list) { t_list *current; t_list *redirects; @@ -32,16 +32,16 @@ t_list *redirect_get_outputs(t_list *list) } if (redirect_is_valid(current, token)) { - ft_lstadd_front(&redirects, ft_lstnew(redirect_new(token->type, ft_strdup(((t_token *)current->next->content)->value)))); + ft_lstadd_front(&redirects, ft_lstnew(redirect_new(minishell, token->type, ft_strdup(((t_token *)current->next->content)->value)))); current = current->next; continue ; } else { - ft_lstadd_front(&redirects, ft_lstnew(redirect_new(T_ERROR, NULL))); + ft_lstadd_front(&redirects, ft_lstnew(redirect_new(minishell, T_ERROR, NULL))); break ; } current = current->next; } return (redirects); -} \ No newline at end of file +} diff --git a/src/redirect/redirect_new.c b/src/redirect/redirect_new.c index 4983828..e6c9ccc 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_token_type type, char *value) +t_redirect *redirect_new(t_minishell * minishell, t_token_type type, char *value) { t_redirect *result; - result = ft_malloc_safe(sizeof(t_redirect)); + result = malloc_safe(minishell, sizeof(t_redirect)); result->type = type; result->value = NULL; if (value) result->value = value; return (result); -} \ No newline at end of file +} diff --git a/src/token/token_new.c b/src/token/token_new.c index 9626f44..4a35040 100644 --- a/src/token/token_new.c +++ b/src/token/token_new.c @@ -1,27 +1,22 @@ /* ************************************************************************** */ /* */ -/* ::: :::::::: */ -/* token_new.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: qmennen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/02/05 19:10:49 by qmennen #+# #+# */ -/* Updated: 2025/02/18 17:01:52 by qmennen ### ########.fr */ +/* :::::::: */ +/* token_new.c :+: :+: */ +/* +:+ */ +/* By: qmennen +#+ */ +/* +#+ */ +/* Created: 2025/02/05 19:10:49 by qmennen #+# #+# */ +/* Updated: 2025/02/25 14:43:56 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -t_token *token_new(t_token_type type, char *c, int pos) +t_token *token_new(t_minishell *minishell, t_token_type type, char *c, int pos) { t_token *token; - token = malloc(sizeof(t_token)); - if (!token) - { - perror("failed assigning token memory"); - exit(EXIT_FAILURE); - } + token = malloc_safe(minishell, sizeof(t_token)); token->type = type; token->position = pos; if (c) diff --git a/src/token/token_parse.c b/src/token/token_parse.c index 7a9d561..2031eda 100644 --- a/src/token/token_parse.c +++ b/src/token/token_parse.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ -/* ::: :::::::: */ -/* token_parse.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: qmennen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/02/05 19:10:17 by qmennen #+# #+# */ -/* Updated: 2025/02/18 17:02:17 by qmennen ### ########.fr */ +/* :::::::: */ +/* token_parse.c :+: :+: */ +/* +:+ */ +/* By: qmennen +#+ */ +/* +#+ */ +/* Created: 2025/02/05 19:10:17 by qmennen #+# #+# */ +/* Updated: 2025/02/25 14:45:08 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -56,7 +56,7 @@ static char *char_from_type(t_token_type type) return (NULL); } -t_token *token_parse(t_lexer *lexer) +t_token *token_parse(t_minishell *minishell, t_lexer *lexer) { int is_double; char c; @@ -66,7 +66,7 @@ t_token *token_parse(t_lexer *lexer) c = lexer->current_char; is_double = lexer->input[lexer->pos + 1] == c; type = token_from_char(c, is_double); - token = token_new(type, char_from_type(type), lexer->pos); + token = token_new(minishell, type, char_from_type(type), lexer->pos); if (is_double) lexer_readchar(lexer); lexer_readchar(lexer); diff --git a/src/utils/check_malloc.c b/src/utils/check_malloc.c index fff6fad..e78e62a 100644 --- a/src/utils/check_malloc.c +++ b/src/utils/check_malloc.c @@ -13,11 +13,17 @@ #include "minishell.h" -void check_malloc(void *ptr) +void check_malloc(t_minishell *minishell, void *ptr) { if (ptr == NULL) { error_msg("malloc", "can't allocate memory"); + + ft_lstclear(&(minishell->freelist), free); exit(1); } + else + { + ft_lstadd_front(&(minishell->freelist), ft_lstnew(ptr)); + } } diff --git a/src/utils/free_freelist.c b/src/utils/free_freelist.c new file mode 100644 index 0000000..5d3daa9 --- /dev/null +++ b/src/utils/free_freelist.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* free_freelist.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/02/25 13:23:30 by whaffman #+# #+# */ +/* Updated: 2025/02/25 13:24:18 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void free_freelist(t_minishell *minishell) +{ + ft_lstclear(&(minishell->freelist), free); +} diff --git a/src/utils/free_minishell.c b/src/utils/free_minishell.c index 9bb5dc3..4d09cb7 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/11 15:36:24 by whaffman ######## odam.nl */ +/* Updated: 2025/02/25 13:47:23 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -17,6 +17,6 @@ void free_minishell(t_minishell *minishell) if (minishell->line) free_minishell_line(minishell); if (minishell->environment) - environment_free_list(&minishell->environment); + environment_free_list(minishell); free(minishell); } diff --git a/src/utils/ft_malloc_safe.c b/src/utils/ft_malloc_safe.c index 5c601f2..4b21724 100644 --- a/src/utils/ft_malloc_safe.c +++ b/src/utils/ft_malloc_safe.c @@ -12,11 +12,11 @@ #include "minishell.h" -void *ft_malloc_safe(size_t size) +void *malloc_safe(t_minishell *minishell, size_t size) { void *ptr; ptr = malloc(size); - check_malloc(ptr); + check_malloc(minishell, ptr); return (ptr); } diff --git a/src/utils/ft_strdup_safe.c b/src/utils/ft_strdup_safe.c index bb52b05..581d3ce 100644 --- a/src/utils/ft_strdup_safe.c +++ b/src/utils/ft_strdup_safe.c @@ -12,11 +12,11 @@ #include "minishell.h" -char *ft_strdup_safe(const char *str) +char *ft_strdup_safe(t_minishell *minishell, const char *str) { char *new_str; new_str = ft_strdup(str); - check_malloc(new_str); + check_malloc(minishell, new_str); return (new_str); } diff --git a/src/utils/ft_strjoin_safe.c b/src/utils/ft_strjoin_safe.c index 8a41829..655c85b 100644 --- a/src/utils/ft_strjoin_safe.c +++ b/src/utils/ft_strjoin_safe.c @@ -12,12 +12,12 @@ #include "minishell.h" -char *ft_strjoin_safe(const char *s1, const char *s2) +char *ft_strjoin_safe(t_minishell *minishell, const char *s1, const char *s2) { char *new_str; new_str = ft_strjoin(s1, s2); - check_malloc(new_str); + check_malloc(minishell, new_str); return (new_str); } diff --git a/src/utils/init_minishell.c b/src/utils/init_minishell.c index 75a24b9..bc2cb13 100644 --- a/src/utils/init_minishell.c +++ b/src/utils/init_minishell.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/05 16:03:03 by whaffman #+# #+# */ -/* Updated: 2025/02/05 16:03:15 by whaffman ######## odam.nl */ +/* Updated: 2025/02/25 14:50:19 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -27,5 +27,6 @@ t_minishell *init_minishell(void) minishell->lexer = NULL; minishell->tokens = NULL; minishell->commands = NULL; + minishell->freelist = NULL; return (minishell); }