From 24752c9d08ac27d9be0b5a4408d5dd053ce90869 Mon Sep 17 00:00:00 2001 From: Quinten Date: Sun, 23 Feb 2025 11:58:13 +0100 Subject: [PATCH 1/8] temp --- src/parser/parser_get_commands.c | 79 +++++++++++++------------ src/parser/parser_get_input_redirects.c | 39 ++++++++++++ 2 files changed, 79 insertions(+), 39 deletions(-) create mode 100644 src/parser/parser_get_input_redirects.c diff --git a/src/parser/parser_get_commands.c b/src/parser/parser_get_commands.c index 74c21ff..1001485 100644 --- a/src/parser/parser_get_commands.c +++ b/src/parser/parser_get_commands.c @@ -1,39 +1,40 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parser_get_commands.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: qmennen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/02/11 14:06:02 by qmennen #+# #+# */ -/* Updated: 2025/02/18 20:36:01 by qmennen ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -t_list *parser_get_commands(t_minishell *minishell) -{ - t_list *command_list; - t_list *current; - t_command *command; - t_token *token; - - command_list = NULL; - if (!minishell->tokens) - return (NULL); - current = minishell->tokens; - while (current) - { - token = (t_token *) current->content; - command = parser_command_new(ft_strdup(token->value)); - command->args = parser_get_arguments(current, minishell); - ft_lstadd_back(&command_list, ft_lstnew(command)); - while (current && ((t_token *)current->content)->type < 3) - current = current->next; - if (current && ((t_token *)current->content)->type >= 3) - current = current->next; - } -// ft_lstiter(command_list, print_commands); - return (command_list); -} +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parser_get_commands.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/11 14:06:02 by qmennen #+# #+# */ +/* Updated: 2025/02/18 20:36:01 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +t_list *parser_get_commands(t_minishell *minishell) +{ + t_list *command_list; + t_list *current; + t_command *command; + t_token *token; + + command_list = NULL; + if (!minishell->tokens) + return (NULL); + current = minishell->tokens; + while (current) + { + token = (t_token *) current->content; + command = parser_command_new(ft_strdup(token->value)); + command->args = parser_get_arguments(current, minishell); + command->redirect_in = parser_get_input_redirects(current); + ft_lstadd_back(&command_list, ft_lstnew(command)); + while (current && ((t_token *)current->content)->type < 3) + current = current->next; + if (current && ((t_token *)current->content)->type >= 3) + current = current->next; + } +// ft_lstiter(command_list, print_commands); + return (command_list); +} diff --git a/src/parser/parser_get_input_redirects.c b/src/parser/parser_get_input_redirects.c new file mode 100644 index 0000000..83b011d --- /dev/null +++ b/src/parser/parser_get_input_redirects.c @@ -0,0 +1,39 @@ +# include "minishell.h" + +static int valid_def(t_list *list, t_token *token) +{ + t_token *next; + + if (!list->next) + return (0); + next = (t_token *)list->next->content; + if (!next) + return (0); + return ((token->type == T_REDIRECT_IN || token->type == T_HEREDOC) && next->type < 3); +} + +static t_redirect *redirect_new() +{ + t_redirect *result; +} + +void parser_get_input_redirects(t_list *list) +{ + t_list *current; + t_list *redirects; + t_token *token; + + redirects = NULL; + current = list; + while (current) + { + token = (t_token *)current->content; + if (valid_def(current, token)) + { + ft_lstaddfront(&redirects, ft_lstnew()); + } + else + break ; + current = current->next; + } +} \ No newline at end of file From 45968e547ed22078431eb56c8af98aa3aeec9832 Mon Sep 17 00:00:00 2001 From: Quinten Date: Sun, 23 Feb 2025 12:20:19 +0100 Subject: [PATCH 2/8] parsing input redirects --- inc/debug.h | 1 + inc/parser.h | 1 + src/debug/print_commands.c | 10 ++++++++++ src/main.c | 1 - src/parser/parser_get_commands.c | 2 +- src/parser/parser_get_input_redirects.c | 25 ++++++++++++++++++++++--- src/utils/free_command_list.c | 22 ++++++++++++++++++++++ 7 files changed, 57 insertions(+), 5 deletions(-) diff --git a/inc/debug.h b/inc/debug.h index f26f569..174135e 100644 --- a/inc/debug.h +++ b/inc/debug.h @@ -17,5 +17,6 @@ void print_commands(void *param); void token_print(void *param); +void print_redirects(void *param); #endif diff --git a/inc/parser.h b/inc/parser.h index f757caa..8a8d05e 100644 --- a/inc/parser.h +++ b/inc/parser.h @@ -18,5 +18,6 @@ t_command *parser_command_new(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); #endif diff --git a/src/debug/print_commands.c b/src/debug/print_commands.c index ed094e9..2ddf0cf 100644 --- a/src/debug/print_commands.c +++ b/src/debug/print_commands.c @@ -28,6 +28,16 @@ void print_commands(void *param) printf("\n"); } +void print_redirects(void *param) +{ + t_redirect *redirect; + + redirect = (t_redirect *)param; + if (!redirect) + return ; + printf("redirect %i value %s\n", redirect->type, redirect->value); +} + void token_print(void *param) { t_token *token; diff --git a/src/main.c b/src/main.c index 2a82a97..dec9a74 100644 --- a/src/main.c +++ b/src/main.c @@ -36,7 +36,6 @@ int main(int argc, char **argv, char **envp) minishell->commands = parser_get_commands(minishell); simple_builtins(minishell); free_minishell_line(minishell); - ft_lstclear(&minishell->commands, free_command_list); } ft_lstclear(&minishell->commands, free_command_list); free_minishell(minishell); diff --git a/src/parser/parser_get_commands.c b/src/parser/parser_get_commands.c index 1001485..c6af1fd 100644 --- a/src/parser/parser_get_commands.c +++ b/src/parser/parser_get_commands.c @@ -30,7 +30,7 @@ t_list *parser_get_commands(t_minishell *minishell) command->args = parser_get_arguments(current, minishell); command->redirect_in = parser_get_input_redirects(current); ft_lstadd_back(&command_list, ft_lstnew(command)); - while (current && ((t_token *)current->content)->type < 3) + while (current && (((t_token *)current->content)->type < 3 || ((t_token *)current->content)->type == T_REDIRECT_IN)) current = current->next; if (current && ((t_token *)current->content)->type >= 3) current = current->next; diff --git a/src/parser/parser_get_input_redirects.c b/src/parser/parser_get_input_redirects.c index 83b011d..aee0376 100644 --- a/src/parser/parser_get_input_redirects.c +++ b/src/parser/parser_get_input_redirects.c @@ -12,12 +12,19 @@ static int valid_def(t_list *list, t_token *token) return ((token->type == T_REDIRECT_IN || token->type == T_HEREDOC) && next->type < 3); } -static t_redirect *redirect_new() +static t_redirect *redirect_new(t_token_type type, char *value) { t_redirect *result; + + result = ft_malloc_safe(sizeof(t_redirect)); + result->type = type; + result->value = NULL; + if (value) + result->value = value; + return (result); } -void parser_get_input_redirects(t_list *list) +t_list *parser_get_input_redirects(t_list *list) { t_list *current; t_list *redirects; @@ -28,12 +35,24 @@ void parser_get_input_redirects(t_list *list) while (current) { token = (t_token *)current->content; + if (token->type != T_REDIRECT_IN && token->type != T_HEREDOC) + { + current = current->next; + continue ; + } if (valid_def(current, token)) { - ft_lstaddfront(&redirects, ft_lstnew()); + ft_lstadd_front(&redirects, ft_lstnew(redirect_new(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))); break ; + } current = current->next; } + ft_lstiter(redirects, print_redirects); + return (redirects); } \ No newline at end of file diff --git a/src/utils/free_command_list.c b/src/utils/free_command_list.c index 684f713..76316c0 100644 --- a/src/utils/free_command_list.c +++ b/src/utils/free_command_list.c @@ -25,6 +25,26 @@ static void free_args(char **args) free(args); } +static void free_redirects(t_list *lst) +{ + t_redirect *redir; + t_list *current; + t_list *last; + + current = lst; + while (current) + { + last = current; + redir = (t_redirect *)current->content; + if (redir && redir->value) + free(redir->value); + if (redir) + free(redir); + current = current->next; + free(last); + } +} + void free_command_list(void *content) { t_command *command; @@ -34,5 +54,7 @@ void free_command_list(void *content) free(command->command); if (command->args) free_args(command->args); + if (command->redirect_in) + free_redirects(command->redirect_in); free(command); } From 3c14325711885dc314f2cbac07f74b68d6b1d1f1 Mon Sep 17 00:00:00 2001 From: Quinten Date: Sun, 23 Feb 2025 12:34:34 +0100 Subject: [PATCH 3/8] some more structure --- Makefile | 5 ++- inc/minishell.h | 5 ++- inc/redirect.h | 20 +++++++++ src/parser/parser_get_commands.c | 2 +- src/parser/parser_get_input_redirects.c | 58 ------------------------- src/redirect/redirect_get_inputs.c | 46 ++++++++++++++++++++ src/redirect/redirect_new.c | 25 +++++++++++ src/redirect/redirect_valid_type.c | 31 +++++++++++++ 8 files changed, 129 insertions(+), 63 deletions(-) create mode 100644 inc/redirect.h delete mode 100644 src/parser/parser_get_input_redirects.c create mode 100644 src/redirect/redirect_get_inputs.c create mode 100644 src/redirect/redirect_new.c create mode 100644 src/redirect/redirect_valid_type.c diff --git a/Makefile b/Makefile index 634f3cc..8e3044a 100644 --- a/Makefile +++ b/Makefile @@ -3,10 +3,10 @@ # :::::::: # # Makefile :+: :+: # # +:+ # -# By: qmennen +#+ # +# By: marvin +#+ # # +#+ # # Created: 2024/10/15 11:48:46 by whaffman #+# #+# # -# Updated: 2025/02/20 11:10:42 by whaffman ######## odam.nl # +# Updated: 2025/02/23 12:28:40 by Quinten ######## odam.nl # # # # **************************************************************************** # @@ -24,6 +24,7 @@ OBJ_PATH = obj VPATH = src:src/environment:src/prompt:src/lexer:src/token:src/utils: VPATH += src/executor:src/parser:src/expander:src/debug:src/signal:src/builtin +VPATH += src/redirect SOURCES = $(shell basename -a $(shell find $(SRC_PATH) -type f -name "*.c")) OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o)) diff --git a/inc/minishell.h b/inc/minishell.h index 27bca6a..6b60f96 100644 --- a/inc/minishell.h +++ b/inc/minishell.h @@ -3,10 +3,10 @@ /* :::::::: */ /* minishell.h :+: :+: */ /* +:+ */ -/* By: whaffman +#+ */ +/* By: marvin +#+ */ /* +#+ */ /* Created: 2025/02/04 16:13:13 by whaffman #+# #+# */ -/* Updated: 2025/02/20 11:35:24 by whaffman ######## odam.nl */ +/* Updated: 2025/02/23 12:28:23 by Quinten ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -24,6 +24,7 @@ # include "executor.h" # include "parser.h" # include "expander.h" +# include "redirect.h" # include "debug.h" # include "utils.h" diff --git a/inc/redirect.h b/inc/redirect.h new file mode 100644 index 0000000..4f6f0c4 --- /dev/null +++ b/inc/redirect.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* redirect.h :+: :+: */ +/* +:+ */ +/* By: Quinten +#+ */ +/* +#+ */ +/* Created: 2025/02/23 12:26:29 by Quinten #+# #+# */ +/* Updated: 2025/02/23 12:26:29 by Quinten ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#ifndef REDIRECT_H +# define REDIRECT_H +# include "minishell.h" + +t_redirect *redirect_new(t_token_type type, char *value); +t_list *redirect_get_inputs(t_list *list); +int redirect_is_valid(t_list *lst, t_token *token); +#endif \ No newline at end of file diff --git a/src/parser/parser_get_commands.c b/src/parser/parser_get_commands.c index c6af1fd..e49dfb1 100644 --- a/src/parser/parser_get_commands.c +++ b/src/parser/parser_get_commands.c @@ -28,7 +28,7 @@ t_list *parser_get_commands(t_minishell *minishell) token = (t_token *) current->content; command = parser_command_new(ft_strdup(token->value)); command->args = parser_get_arguments(current, minishell); - command->redirect_in = parser_get_input_redirects(current); + command->redirect_in = redirect_get_inputs(current); ft_lstadd_back(&command_list, ft_lstnew(command)); while (current && (((t_token *)current->content)->type < 3 || ((t_token *)current->content)->type == T_REDIRECT_IN)) current = current->next; diff --git a/src/parser/parser_get_input_redirects.c b/src/parser/parser_get_input_redirects.c deleted file mode 100644 index aee0376..0000000 --- a/src/parser/parser_get_input_redirects.c +++ /dev/null @@ -1,58 +0,0 @@ -# include "minishell.h" - -static int valid_def(t_list *list, t_token *token) -{ - t_token *next; - - if (!list->next) - return (0); - next = (t_token *)list->next->content; - if (!next) - return (0); - return ((token->type == T_REDIRECT_IN || token->type == T_HEREDOC) && next->type < 3); -} - -static t_redirect *redirect_new(t_token_type type, char *value) -{ - t_redirect *result; - - result = ft_malloc_safe(sizeof(t_redirect)); - result->type = type; - result->value = NULL; - if (value) - result->value = value; - return (result); -} - -t_list *parser_get_input_redirects(t_list *list) -{ - t_list *current; - t_list *redirects; - t_token *token; - - redirects = NULL; - current = list; - while (current) - { - token = (t_token *)current->content; - if (token->type != T_REDIRECT_IN && token->type != T_HEREDOC) - { - current = current->next; - continue ; - } - if (valid_def(current, token)) - { - ft_lstadd_front(&redirects, ft_lstnew(redirect_new(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))); - break ; - } - current = current->next; - } - ft_lstiter(redirects, print_redirects); - return (redirects); -} \ No newline at end of file diff --git a/src/redirect/redirect_get_inputs.c b/src/redirect/redirect_get_inputs.c new file mode 100644 index 0000000..4caf8b8 --- /dev/null +++ b/src/redirect/redirect_get_inputs.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* redirect_get_inputs.c :+: :+: */ +/* +:+ */ +/* By: Quinten +#+ */ +/* +#+ */ +/* Created: 2025/02/23 12:29:05 by Quinten #+# #+# */ +/* Updated: 2025/02/23 12:29:05 by Quinten ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +# include "redirect.h" + +t_list *redirect_get_inputs(t_list *list) +{ + t_list *current; + t_list *redirects; + t_token *token; + + redirects = NULL; + current = list; + while (current) + { + token = (t_token *)current->content; + if (token->type != T_REDIRECT_IN && token->type != T_HEREDOC) + { + current = current->next; + continue ; + } + if (redirect_is_valid(current, token)) + { + ft_lstadd_front(&redirects, ft_lstnew(redirect_new(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))); + break ; + } + current = current->next; + } + ft_lstiter(redirects, print_redirects); + return (redirects); +} \ No newline at end of file diff --git a/src/redirect/redirect_new.c b/src/redirect/redirect_new.c new file mode 100644 index 0000000..4983828 --- /dev/null +++ b/src/redirect/redirect_new.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* redirect_new.c :+: :+: */ +/* +:+ */ +/* By: Quinten +#+ */ +/* +#+ */ +/* Created: 2025/02/23 12:27:33 by Quinten #+# #+# */ +/* Updated: 2025/02/23 12:27:33 by Quinten ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +t_redirect *redirect_new(t_token_type type, char *value) +{ + t_redirect *result; + + result = ft_malloc_safe(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/redirect/redirect_valid_type.c b/src/redirect/redirect_valid_type.c new file mode 100644 index 0000000..49d0674 --- /dev/null +++ b/src/redirect/redirect_valid_type.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* redirect_valid_type.c :+: :+: */ +/* +:+ */ +/* By: Quinten +#+ */ +/* +#+ */ +/* Created: 2025/02/23 12:30:18 by Quinten #+# #+# */ +/* Updated: 2025/02/23 12:30:18 by Quinten ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +static int is_redirection(t_token *token) +{ + 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) +{ + t_token *next; + + if (!lst->next) + return (0); + next = (t_token *)lst->next->content; + if (!next) + return (0); + return (is_redirection(token) && next->type < 3); +} \ No newline at end of file From 2117a6538e8d862c56415f4f0b1a6b250b47d201 Mon Sep 17 00:00:00 2001 From: Quinten Date: Sun, 23 Feb 2025 12:44:31 +0100 Subject: [PATCH 4/8] redirects --- inc/redirect.h | 2 ++ src/debug/print_commands.c | 22 ++++++++------ src/parser/parser_get_commands.c | 10 +++++-- src/redirect/redirect_get_inputs.c | 1 - src/redirect/redirect_get_outputs.c | 45 +++++++++++++++++++++++++++++ src/redirect/redirect_valid_type.c | 4 +-- src/utils/free_command_list.c | 18 +++++++----- 7 files changed, 80 insertions(+), 22 deletions(-) create mode 100644 src/redirect/redirect_get_outputs.c diff --git a/inc/redirect.h b/inc/redirect.h index 4f6f0c4..98a86e3 100644 --- a/inc/redirect.h +++ b/inc/redirect.h @@ -16,5 +16,7 @@ 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); int redirect_is_valid(t_list *lst, t_token *token); +int redirect_token_type(t_token *token); #endif \ No newline at end of file diff --git a/src/debug/print_commands.c b/src/debug/print_commands.c index 2ddf0cf..7884a8d 100644 --- a/src/debug/print_commands.c +++ b/src/debug/print_commands.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ -/* ::: :::::::: */ -/* print_commands.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: qmennen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/02/18 20:06:37 by qmennen #+# #+# */ -/* Updated: 2025/02/18 20:11:45 by qmennen ### ########.fr */ +/* :::::::: */ +/* print_commands.c :+: :+: */ +/* +:+ */ +/* By: marvin +#+ */ +/* +#+ */ +/* Created: 2025/02/18 20:06:37 by qmennen #+# #+# */ +/* Updated: 2025/02/23 12:43:35 by Quinten ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -21,11 +21,15 @@ void print_commands(void *param) if (!command) return ; printf("command: %s\n", command->command); - printf("args: "); + printf("-- Args: "); i = 0; while (command->args[i++]) printf("%s ", command->args[i]); printf("\n"); + printf("-- Input redirects:\n"); + ft_lstiter(command->redirect_in, print_redirects); + printf("-- Output redirects:\n"); + ft_lstiter(command->redirect_out, print_redirects); } void print_redirects(void *param) @@ -35,7 +39,7 @@ void print_redirects(void *param) redirect = (t_redirect *)param; if (!redirect) return ; - printf("redirect %i value %s\n", redirect->type, redirect->value); + printf(" Redirect %i value %s\n", redirect->type, redirect->value); } void token_print(void *param) diff --git a/src/parser/parser_get_commands.c b/src/parser/parser_get_commands.c index e49dfb1..6725b8c 100644 --- a/src/parser/parser_get_commands.c +++ b/src/parser/parser_get_commands.c @@ -12,6 +12,11 @@ #include "minishell.h" +static int is_command_token(t_token *token) +{ + return (token->type < 3 || redirect_token_type(token)); +} + t_list *parser_get_commands(t_minishell *minishell) { t_list *command_list; @@ -29,12 +34,13 @@ t_list *parser_get_commands(t_minishell *minishell) command = parser_command_new(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); ft_lstadd_back(&command_list, ft_lstnew(command)); - while (current && (((t_token *)current->content)->type < 3 || ((t_token *)current->content)->type == T_REDIRECT_IN)) + while (current && is_command_token((t_token *)current->content)) current = current->next; if (current && ((t_token *)current->content)->type >= 3) current = current->next; } -// ft_lstiter(command_list, print_commands); + ft_lstiter(command_list, print_commands); return (command_list); } diff --git a/src/redirect/redirect_get_inputs.c b/src/redirect/redirect_get_inputs.c index 4caf8b8..1ea9f8b 100644 --- a/src/redirect/redirect_get_inputs.c +++ b/src/redirect/redirect_get_inputs.c @@ -41,6 +41,5 @@ t_list *redirect_get_inputs(t_list *list) } current = current->next; } - ft_lstiter(redirects, print_redirects); return (redirects); } \ No newline at end of file diff --git a/src/redirect/redirect_get_outputs.c b/src/redirect/redirect_get_outputs.c new file mode 100644 index 0000000..18cc0dc --- /dev/null +++ b/src/redirect/redirect_get_outputs.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* redirect_get_outputs.c :+: :+: */ +/* +:+ */ +/* By: Quinten +#+ */ +/* +#+ */ +/* Created: 2025/02/23 12:37:24 by Quinten #+# #+# */ +/* Updated: 2025/02/23 12:37:24 by Quinten ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +t_list *redirect_get_outputs(t_list *list) +{ + t_list *current; + t_list *redirects; + t_token *token; + + redirects = NULL; + current = list; + while (current) + { + token = (t_token *)current->content; + if (token->type != T_REDIRECT_OUT && token->type != T_APPEND_OUT) + { + current = current->next; + continue ; + } + if (redirect_is_valid(current, token)) + { + ft_lstadd_front(&redirects, ft_lstnew(redirect_new(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))); + break ; + } + current = current->next; + } + return (redirects); +} \ No newline at end of file diff --git a/src/redirect/redirect_valid_type.c b/src/redirect/redirect_valid_type.c index 49d0674..2df0a3d 100644 --- a/src/redirect/redirect_valid_type.c +++ b/src/redirect/redirect_valid_type.c @@ -12,7 +12,7 @@ #include "minishell.h" -static int is_redirection(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); @@ -27,5 +27,5 @@ int redirect_is_valid(t_list *lst, t_token *token) next = (t_token *)lst->next->content; if (!next) return (0); - return (is_redirection(token) && next->type < 3); + return (redirect_token_type(token) && next->type < 3); } \ No newline at end of file diff --git a/src/utils/free_command_list.c b/src/utils/free_command_list.c index 76316c0..4654515 100644 --- a/src/utils/free_command_list.c +++ b/src/utils/free_command_list.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ -/* ::: :::::::: */ -/* free_command_list.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: qmennen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/02/11 14:24:05 by qmennen #+# #+# */ -/* Updated: 2025/02/11 14:27:02 by qmennen ### ########.fr */ +/* :::::::: */ +/* free_command_list.c :+: :+: */ +/* +:+ */ +/* By: marvin +#+ */ +/* +#+ */ +/* Created: 2025/02/11 14:24:05 by qmennen #+# #+# */ +/* Updated: 2025/02/23 12:40:17 by Quinten ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -55,6 +55,8 @@ void free_command_list(void *content) if (command->args) free_args(command->args); if (command->redirect_in) - free_redirects(command->redirect_in); + free_redirects(command->redirect_in); + if (command->redirect_out) + free_redirects(command->redirect_out); free(command); } From ea90a00c33e8f5528ddfb61aa4b5442ad01eb6bf Mon Sep 17 00:00:00 2001 From: Quinten Date: Sun, 23 Feb 2025 12:46:02 +0100 Subject: [PATCH 5/8] merge main into quitnen --- Makefile | 156 +++++++++++++++++----------------- inc/debug.h | 44 +++++----- inc/minishell.h | 100 +++++++++++----------- inc/parser.h | 46 +++++----- inc/utils.h | 58 ++++++------- src/debug/print_commands.c | 104 +++++++++++------------ src/main.c | 86 +++++++++---------- src/utils/check_malloc.c | 46 +++++----- src/utils/error_msg.c | 68 +++++++-------- src/utils/free_command_list.c | 124 +++++++++++++-------------- src/utils/ft_malloc_safe.c | 44 +++++----- src/utils/ft_strdup_safe.c | 44 +++++----- src/utils/ft_strjoin_safe.c | 46 +++++----- 13 files changed, 483 insertions(+), 483 deletions(-) diff --git a/Makefile b/Makefile index 8e3044a..7a679b7 100644 --- a/Makefile +++ b/Makefile @@ -1,78 +1,78 @@ -# **************************************************************************** # -# # -# :::::::: # -# Makefile :+: :+: # -# +:+ # -# By: marvin +#+ # -# +#+ # -# Created: 2024/10/15 11:48:46 by whaffman #+# #+# # -# Updated: 2025/02/23 12:28:40 by Quinten ######## odam.nl # -# # -# **************************************************************************** # - -NAME = minishell - -SRC_PATH = src -INC_PATH = inc -LIB_PATH = lib - -LIBFT_PATH = $(LIB_PATH)/libft -LIBFT_INC_PATH = $(LIBFT_PATH)/inc -LIBFT = $(LIBFT_PATH)/libft.a - -OBJ_PATH = obj - -VPATH = src:src/environment:src/prompt:src/lexer:src/token:src/utils: -VPATH += src/executor:src/parser:src/expander:src/debug:src/signal:src/builtin -VPATH += src/redirect -SOURCES = $(shell basename -a $(shell find $(SRC_PATH) -type f -name "*.c")) - -OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o)) -DEPENDS = ${OBJECTS:.o=.d} - -CC = cc -RM = rm -rf - -INCLUDES = -I./$(INC_PATH) -I./$(LIBFT_INC_PATH) -CFLAGS = -Wall -Wextra -Werror -fsanitize=address,undefined -MMD -g3 - -UNAME_S := $(shell uname -s) -ifeq ($(UNAME_S),Linux) - LDLIBS := -L$(LIBFT_PATH) -lft -lreadline -endif - -all: $(NAME) - echo $(SOURCES) - -$(NAME): $(LIBFT) $(OBJECTS) - $(CC) $(CFLAGS) $(OBJECTS) $(LDLIBS) -o $(NAME) - --include ${DEPENDS} - -$(LIBFT): $(LIBFT_PATH) - $(MAKE) -C $(LIBFT_PATH) - -$(LIBFT_PATH): - git submodule add https://gitea.duinvoetje.nl/willem/libft.git $(LIBFT_PATH) - -$(OBJ_PATH): - mkdir -p $@ - -$(OBJ_PATH)/%.o: %.c $(LIBFT) | $(OBJ_PATH) - $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@ - -clean: - $(RM) $(OBJECTS) $(OBJ_PATH) - $(MAKE) -C $(LIBFT_PATH) clean - - -fclean: clean - $(RM) $(NAME) - $(MAKE) -C $(LIBFT_PATH) fclean - -re: fclean all - -run: all - ./$(NAME) - -.PHONY: all clean fclean re run +# **************************************************************************** # +# # +# :::::::: # +# Makefile :+: :+: # +# +:+ # +# By: marvin +#+ # +# +#+ # +# Created: 2024/10/15 11:48:46 by whaffman #+# #+# # +# Updated: 2025/02/23 12:28:40 by Quinten ######## odam.nl # +# # +# **************************************************************************** # + +NAME = minishell + +SRC_PATH = src +INC_PATH = inc +LIB_PATH = lib + +LIBFT_PATH = $(LIB_PATH)/libft +LIBFT_INC_PATH = $(LIBFT_PATH)/inc +LIBFT = $(LIBFT_PATH)/libft.a + +OBJ_PATH = obj + +VPATH = src:src/environment:src/prompt:src/lexer:src/token:src/utils: +VPATH += src/executor:src/parser:src/expander:src/debug:src/signal:src/builtin +VPATH += src/redirect +SOURCES = $(shell basename -a $(shell find $(SRC_PATH) -type f -name "*.c")) + +OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o)) +DEPENDS = ${OBJECTS:.o=.d} + +CC = cc +RM = rm -rf + +INCLUDES = -I./$(INC_PATH) -I./$(LIBFT_INC_PATH) +CFLAGS = -Wall -Wextra -Werror -fsanitize=address,undefined -MMD -g3 + +UNAME_S := $(shell uname -s) +ifeq ($(UNAME_S),Linux) + LDLIBS := -L$(LIBFT_PATH) -lft -lreadline +endif + +all: $(NAME) + echo $(SOURCES) + +$(NAME): $(LIBFT) $(OBJECTS) + $(CC) $(CFLAGS) $(OBJECTS) $(LDLIBS) -o $(NAME) + +-include ${DEPENDS} + +$(LIBFT): $(LIBFT_PATH) + $(MAKE) -C $(LIBFT_PATH) + +$(LIBFT_PATH): + git submodule add https://gitea.duinvoetje.nl/willem/libft.git $(LIBFT_PATH) + +$(OBJ_PATH): + mkdir -p $@ + +$(OBJ_PATH)/%.o: %.c $(LIBFT) | $(OBJ_PATH) + $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@ + +clean: + $(RM) $(OBJECTS) $(OBJ_PATH) + $(MAKE) -C $(LIBFT_PATH) clean + + +fclean: clean + $(RM) $(NAME) + $(MAKE) -C $(LIBFT_PATH) fclean + +re: fclean all + +run: all + ./$(NAME) + +.PHONY: all clean fclean re run diff --git a/inc/debug.h b/inc/debug.h index 174135e..9c11cdd 100644 --- a/inc/debug.h +++ b/inc/debug.h @@ -1,22 +1,22 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* debug.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: qmennen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/02/18 20:06:53 by qmennen #+# #+# */ -/* Updated: 2025/02/18 20:10:49 by qmennen ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef DEBUG_H -# define DEBUG_H - -# include "minishell.h" - -void print_commands(void *param); -void token_print(void *param); -void print_redirects(void *param); - -#endif +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* debug.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/18 20:06:53 by qmennen #+# #+# */ +/* Updated: 2025/02/18 20:10:49 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef DEBUG_H +# define DEBUG_H + +# include "minishell.h" + +void print_commands(void *param); +void token_print(void *param); +void print_redirects(void *param); + +#endif diff --git a/inc/minishell.h b/inc/minishell.h index 6b60f96..78bc2cf 100644 --- a/inc/minishell.h +++ b/inc/minishell.h @@ -1,50 +1,50 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* minishell.h :+: :+: */ -/* +:+ */ -/* By: marvin +#+ */ -/* +#+ */ -/* Created: 2025/02/04 16:13:13 by whaffman #+# #+# */ -/* Updated: 2025/02/23 12:28:23 by Quinten ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#ifndef MINISHELL_H -# define MINISHELL_H - -# include "allowed.h" -# include "libft.h" -# include "typedef.h" -# include "signals.h" -# include "environment.h" -# include "prompt.h" -# include "tokenizer.h" -# include "builtin.h" -# include "executor.h" -# include "parser.h" -# include "expander.h" -# include "redirect.h" -# include "debug.h" -# include "utils.h" - -# define TRUE 1 -# define FALSE 0 - -# define SUCCESS 1 -# define FAILURE 0 - -# define BOLD "\001\033[1m\002" -# define RED "\001\033[0;31m\002" -# define GREEN "\001\033[0;32m\002" -# define YELLOW "\001\033[0;33m\002" -# define BLUE "\001\033[0;34m\002" -# define MAGENTA "\001\033[0;35m\002" -# define CYAN "\001\033[0;36m\002" -# define RESET "\001\033[0m\002" -# define PROMPT RESET "🐚" GREEN "minishell" RESET ": " -# define PROMPT_LEN 51 - -void token_print(void *param); - -#endif +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* minishell.h :+: :+: */ +/* +:+ */ +/* By: marvin +#+ */ +/* +#+ */ +/* Created: 2025/02/04 16:13:13 by whaffman #+# #+# */ +/* Updated: 2025/02/23 12:28:23 by Quinten ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#ifndef MINISHELL_H +# define MINISHELL_H + +# include "allowed.h" +# include "libft.h" +# include "typedef.h" +# include "signals.h" +# include "environment.h" +# include "prompt.h" +# include "tokenizer.h" +# include "builtin.h" +# include "executor.h" +# include "parser.h" +# include "expander.h" +# include "redirect.h" +# include "debug.h" +# include "utils.h" + +# define TRUE 1 +# define FALSE 0 + +# define SUCCESS 1 +# define FAILURE 0 + +# define BOLD "\001\033[1m\002" +# define RED "\001\033[0;31m\002" +# define GREEN "\001\033[0;32m\002" +# define YELLOW "\001\033[0;33m\002" +# define BLUE "\001\033[0;34m\002" +# define MAGENTA "\001\033[0;35m\002" +# define CYAN "\001\033[0;36m\002" +# define RESET "\001\033[0m\002" +# define PROMPT RESET "🐚" GREEN "minishell" RESET ": " +# define PROMPT_LEN 51 + +void token_print(void *param); + +#endif diff --git a/inc/parser.h b/inc/parser.h index 8a8d05e..aafa02a 100644 --- a/inc/parser.h +++ b/inc/parser.h @@ -1,23 +1,23 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* parser.h :+: :+: */ -/* +:+ */ -/* By: qmennen +#+ */ -/* +#+ */ -/* Created: 2025/02/11 14:03:03 by qmennen #+# #+# */ -/* Updated: 2025/02/11 17:19:01 by whaffman ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#ifndef PARSER_H -# define PARSER_H - -# include "minishell.h" - -t_command *parser_command_new(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); - -#endif +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* parser.h :+: :+: */ +/* +:+ */ +/* By: qmennen +#+ */ +/* +#+ */ +/* Created: 2025/02/11 14:03:03 by qmennen #+# #+# */ +/* Updated: 2025/02/11 17:19:01 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#ifndef PARSER_H +# define PARSER_H + +# include "minishell.h" + +t_command *parser_command_new(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); + +#endif diff --git a/inc/utils.h b/inc/utils.h index 36087dc..cf269ea 100644 --- a/inc/utils.h +++ b/inc/utils.h @@ -1,29 +1,29 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* utils.h :+: :+: */ -/* +:+ */ -/* By: whaffman +#+ */ -/* +#+ */ -/* Created: 2025/02/05 16:06:35 by whaffman #+# #+# */ -/* Updated: 2025/02/20 18:07:19 by whaffman ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#ifndef UTILS_H -# define UTILS_H - -void free_minishell_line(t_minishell *minishell); -void free_minishell(t_minishell *minishell); -void free_command_list(void *content); -t_minishell *init_minishell(void); -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); - -#endif // UTILS_H +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* utils.h :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/02/05 16:06:35 by whaffman #+# #+# */ +/* Updated: 2025/02/20 18:07:19 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#ifndef UTILS_H +# define UTILS_H + +void free_minishell_line(t_minishell *minishell); +void free_minishell(t_minishell *minishell); +void free_command_list(void *content); +t_minishell *init_minishell(void); +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); + +#endif // UTILS_H diff --git a/src/debug/print_commands.c b/src/debug/print_commands.c index 7884a8d..cd5cc23 100644 --- a/src/debug/print_commands.c +++ b/src/debug/print_commands.c @@ -1,52 +1,52 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* print_commands.c :+: :+: */ -/* +:+ */ -/* By: marvin +#+ */ -/* +#+ */ -/* Created: 2025/02/18 20:06:37 by qmennen #+# #+# */ -/* Updated: 2025/02/23 12:43:35 by Quinten ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -void print_commands(void *param) -{ - t_command *command; - int i; - - command = (t_command *)param; - if (!command) - return ; - printf("command: %s\n", command->command); - printf("-- Args: "); - i = 0; - while (command->args[i++]) - printf("%s ", command->args[i]); - printf("\n"); - printf("-- Input redirects:\n"); - ft_lstiter(command->redirect_in, print_redirects); - printf("-- Output redirects:\n"); - ft_lstiter(command->redirect_out, print_redirects); -} - -void print_redirects(void *param) -{ - t_redirect *redirect; - - redirect = (t_redirect *)param; - if (!redirect) - return ; - printf(" Redirect %i value %s\n", redirect->type, redirect->value); -} - -void token_print(void *param) -{ - t_token *token; - - token = (t_token *)param; - printf("token type %i, value %s\n", token->type, token->value); -} - +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* print_commands.c :+: :+: */ +/* +:+ */ +/* By: marvin +#+ */ +/* +#+ */ +/* Created: 2025/02/18 20:06:37 by qmennen #+# #+# */ +/* Updated: 2025/02/23 12:43:35 by Quinten ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void print_commands(void *param) +{ + t_command *command; + int i; + + command = (t_command *)param; + if (!command) + return ; + printf("command: %s\n", command->command); + printf("-- Args: "); + i = 0; + while (command->args[i++]) + printf("%s ", command->args[i]); + printf("\n"); + printf("-- Input redirects:\n"); + ft_lstiter(command->redirect_in, print_redirects); + printf("-- Output redirects:\n"); + ft_lstiter(command->redirect_out, print_redirects); +} + +void print_redirects(void *param) +{ + t_redirect *redirect; + + redirect = (t_redirect *)param; + if (!redirect) + return ; + printf(" Redirect %i value %s\n", redirect->type, redirect->value); +} + +void token_print(void *param) +{ + t_token *token; + + token = (t_token *)param; + printf("token type %i, value %s\n", token->type, token->value); +} + diff --git a/src/main.c b/src/main.c index dec9a74..4b3e413 100644 --- a/src/main.c +++ b/src/main.c @@ -1,43 +1,43 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* main.c :+: :+: */ -/* +:+ */ -/* By: whaffman +#+ */ -/* +#+ */ -/* Created: 2025/02/04 16:19:22 by whaffman #+# #+# */ -/* Updated: 2025/02/19 17:59:24 by whaffman ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" -#include "minishell.h" -#include "utils.h" - -int main(int argc, char **argv, char **envp) -{ - t_minishell *minishell; - - (void)argc; - (void)argv; - print_banner(); - history_load(); - minishell = init_minishell(); - signal_init_minishell(); - environment_parse(envp, &(minishell->environment)); - 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); - //ft_lstiter(minishell->tokens, token_print); - minishell->commands = parser_get_commands(minishell); - simple_builtins(minishell); - free_minishell_line(minishell); - } - ft_lstclear(&minishell->commands, free_command_list); - free_minishell(minishell); - return (EXIT_SUCCESS); -} +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* main.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/02/04 16:19:22 by whaffman #+# #+# */ +/* Updated: 2025/02/19 17:59:24 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "minishell.h" +#include "utils.h" + +int main(int argc, char **argv, char **envp) +{ + t_minishell *minishell; + + (void)argc; + (void)argv; + print_banner(); + history_load(); + minishell = init_minishell(); + signal_init_minishell(); + environment_parse(envp, &(minishell->environment)); + 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); + //ft_lstiter(minishell->tokens, token_print); + minishell->commands = parser_get_commands(minishell); + simple_builtins(minishell); + free_minishell_line(minishell); + } + ft_lstclear(&minishell->commands, free_command_list); + free_minishell(minishell); + return (EXIT_SUCCESS); +} diff --git a/src/utils/check_malloc.c b/src/utils/check_malloc.c index a7ae1d5..fff6fad 100644 --- a/src/utils/check_malloc.c +++ b/src/utils/check_malloc.c @@ -1,23 +1,23 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* check_malloc.c :+: :+: */ -/* +:+ */ -/* By: whaffman +#+ */ -/* +#+ */ -/* Created: 2025/02/20 18:00:10 by whaffman #+# #+# */ -/* Updated: 2025/02/20 18:01:08 by whaffman ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - - -void check_malloc(void *ptr) -{ - if (ptr == NULL) - { - error_msg("malloc", "can't allocate memory"); - exit(1); - } -} +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* check_malloc.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/02/20 18:00:10 by whaffman #+# #+# */ +/* Updated: 2025/02/20 18:01:08 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + + +void check_malloc(void *ptr) +{ + if (ptr == NULL) + { + error_msg("malloc", "can't allocate memory"); + exit(1); + } +} diff --git a/src/utils/error_msg.c b/src/utils/error_msg.c index bb985e4..6442019 100644 --- a/src/utils/error_msg.c +++ b/src/utils/error_msg.c @@ -1,34 +1,34 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* error_msg.c :+: :+: */ -/* +:+ */ -/* By: whaffman +#+ */ -/* +#+ */ -/* Created: 2025/02/20 17:03:13 by whaffman #+# #+# */ -/* Updated: 2025/02/20 17:59:36 by whaffman ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - - -void error_msg(char *func, char *msg) -{ - if (errno) - perror(RED BOLD "minishell" RESET); - else - { - ft_putstr_fd(RED BOLD "minishell" RESET ": ", 2); - if (func != NULL) - { - ft_putstr_fd(func, 2); - ft_putstr_fd(": ", 2); - } - if (msg != NULL) - ft_putstr_fd(msg, 2); - else - ft_putstr_fd("general error", 2); - ft_putstr_fd("\n", 2); - } -} +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* error_msg.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/02/20 17:03:13 by whaffman #+# #+# */ +/* Updated: 2025/02/20 17:59:36 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + + +void error_msg(char *func, char *msg) +{ + if (errno) + perror(RED BOLD "minishell" RESET); + else + { + ft_putstr_fd(RED BOLD "minishell" RESET ": ", 2); + if (func != NULL) + { + ft_putstr_fd(func, 2); + ft_putstr_fd(": ", 2); + } + if (msg != NULL) + ft_putstr_fd(msg, 2); + else + ft_putstr_fd("general error", 2); + ft_putstr_fd("\n", 2); + } +} diff --git a/src/utils/free_command_list.c b/src/utils/free_command_list.c index 4654515..5ed7a59 100644 --- a/src/utils/free_command_list.c +++ b/src/utils/free_command_list.c @@ -1,62 +1,62 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* free_command_list.c :+: :+: */ -/* +:+ */ -/* By: marvin +#+ */ -/* +#+ */ -/* Created: 2025/02/11 14:24:05 by qmennen #+# #+# */ -/* Updated: 2025/02/23 12:40:17 by Quinten ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -static void free_args(char **args) -{ - int i; - - i = 0; - while (args[i]) - { - free(args[i]); - i++; - } - free(args); -} - -static void free_redirects(t_list *lst) -{ - t_redirect *redir; - t_list *current; - t_list *last; - - current = lst; - while (current) - { - last = current; - redir = (t_redirect *)current->content; - if (redir && redir->value) - free(redir->value); - if (redir) - free(redir); - current = current->next; - free(last); - } -} - -void free_command_list(void *content) -{ - t_command *command; - - command = (t_command *)content; - if (command->command) - free(command->command); - if (command->args) - free_args(command->args); - if (command->redirect_in) - free_redirects(command->redirect_in); - if (command->redirect_out) - free_redirects(command->redirect_out); - free(command); -} +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* free_command_list.c :+: :+: */ +/* +:+ */ +/* By: marvin +#+ */ +/* +#+ */ +/* Created: 2025/02/11 14:24:05 by qmennen #+# #+# */ +/* Updated: 2025/02/23 12:40:17 by Quinten ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +static void free_args(char **args) +{ + int i; + + i = 0; + while (args[i]) + { + free(args[i]); + i++; + } + free(args); +} + +static void free_redirects(t_list *lst) +{ + t_redirect *redir; + t_list *current; + t_list *last; + + current = lst; + while (current) + { + last = current; + redir = (t_redirect *)current->content; + if (redir && redir->value) + free(redir->value); + if (redir) + free(redir); + current = current->next; + free(last); + } +} + +void free_command_list(void *content) +{ + t_command *command; + + command = (t_command *)content; + if (command->command) + free(command->command); + if (command->args) + free_args(command->args); + if (command->redirect_in) + free_redirects(command->redirect_in); + if (command->redirect_out) + free_redirects(command->redirect_out); + free(command); +} diff --git a/src/utils/ft_malloc_safe.c b/src/utils/ft_malloc_safe.c index eeb6923..5c601f2 100644 --- a/src/utils/ft_malloc_safe.c +++ b/src/utils/ft_malloc_safe.c @@ -1,22 +1,22 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* ft_malloc_safe.c :+: :+: */ -/* +:+ */ -/* By: whaffman +#+ */ -/* +#+ */ -/* Created: 2025/02/20 18:06:46 by whaffman #+# #+# */ -/* Updated: 2025/02/20 18:07:00 by whaffman ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -void *ft_malloc_safe(size_t size) -{ - void *ptr; - - ptr = malloc(size); - check_malloc(ptr); - return (ptr); -} +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_malloc_safe.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/02/20 18:06:46 by whaffman #+# #+# */ +/* Updated: 2025/02/20 18:07:00 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void *ft_malloc_safe(size_t size) +{ + void *ptr; + + ptr = malloc(size); + check_malloc(ptr); + return (ptr); +} diff --git a/src/utils/ft_strdup_safe.c b/src/utils/ft_strdup_safe.c index c0fe7fb..bb52b05 100644 --- a/src/utils/ft_strdup_safe.c +++ b/src/utils/ft_strdup_safe.c @@ -1,22 +1,22 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* ft_strdup_safe.c :+: :+: */ -/* +:+ */ -/* By: whaffman +#+ */ -/* +#+ */ -/* Created: 2025/02/20 18:01:27 by whaffman #+# #+# */ -/* Updated: 2025/02/20 18:04:53 by whaffman ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -char *ft_strdup_safe(const char *str) -{ - char *new_str; - - new_str = ft_strdup(str); - check_malloc(new_str); - return (new_str); -} +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_strdup_safe.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/02/20 18:01:27 by whaffman #+# #+# */ +/* Updated: 2025/02/20 18:04:53 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +char *ft_strdup_safe(const char *str) +{ + char *new_str; + + new_str = ft_strdup(str); + check_malloc(new_str); + return (new_str); +} diff --git a/src/utils/ft_strjoin_safe.c b/src/utils/ft_strjoin_safe.c index 27c8d0f..8a41829 100644 --- a/src/utils/ft_strjoin_safe.c +++ b/src/utils/ft_strjoin_safe.c @@ -1,23 +1,23 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* ft_strjoin_safe.c :+: :+: */ -/* +:+ */ -/* By: whaffman +#+ */ -/* +#+ */ -/* Created: 2025/02/20 18:02:31 by whaffman #+# #+# */ -/* Updated: 2025/02/20 18:04:23 by whaffman ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -char *ft_strjoin_safe(const char *s1, const char *s2) -{ - char *new_str; - - new_str = ft_strjoin(s1, s2); - check_malloc(new_str); - return (new_str); -} - +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_strjoin_safe.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/02/20 18:02:31 by whaffman #+# #+# */ +/* Updated: 2025/02/20 18:04:23 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +char *ft_strjoin_safe(const char *s1, const char *s2) +{ + char *new_str; + + new_str = ft_strjoin(s1, s2); + check_malloc(new_str); + return (new_str); +} + From fd72fbb0d31d9742f17022569b1abe7a9d3a36d6 Mon Sep 17 00:00:00 2001 From: Quinten Date: Sun, 23 Feb 2025 13:11:06 +0100 Subject: [PATCH 6/8] general fixes to redirects --- src/parser/parser_get_commands.c | 2 +- src/redirect/redirect_get_inputs.c | 2 ++ src/redirect/redirect_get_outputs.c | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/parser/parser_get_commands.c b/src/parser/parser_get_commands.c index 6725b8c..cbf115e 100644 --- a/src/parser/parser_get_commands.c +++ b/src/parser/parser_get_commands.c @@ -14,7 +14,7 @@ static int is_command_token(t_token *token) { - return (token->type < 3 || redirect_token_type(token)); + return (token->type < 3 || token->type == T_REDIRECT_IN || token->type ==T_HEREDOC || token->type == T_REDIRECT_OUT || token->type == T_APPEND_OUT); } t_list *parser_get_commands(t_minishell *minishell) diff --git a/src/redirect/redirect_get_inputs.c b/src/redirect/redirect_get_inputs.c index 1ea9f8b..a44a6fe 100644 --- a/src/redirect/redirect_get_inputs.c +++ b/src/redirect/redirect_get_inputs.c @@ -23,6 +23,8 @@ t_list *redirect_get_inputs(t_list *list) while (current) { token = (t_token *)current->content; + if (token->type == T_PIPE || token->type == T_EOF) + break ; if (token->type != T_REDIRECT_IN && token->type != T_HEREDOC) { current = current->next; diff --git a/src/redirect/redirect_get_outputs.c b/src/redirect/redirect_get_outputs.c index 18cc0dc..9383bf6 100644 --- a/src/redirect/redirect_get_outputs.c +++ b/src/redirect/redirect_get_outputs.c @@ -23,6 +23,8 @@ t_list *redirect_get_outputs(t_list *list) while (current) { token = (t_token *)current->content; + if (token->type == T_PIPE || token->type == T_EOF) + break; if (token->type != T_REDIRECT_OUT && token->type != T_APPEND_OUT) { current = current->next; From e13eff066921d2f58ffbdb419221f73f82d006b9 Mon Sep 17 00:00:00 2001 From: Quinten Date: Sun, 23 Feb 2025 13:12:06 +0100 Subject: [PATCH 7/8] util --- src/parser/parser_get_commands.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parser/parser_get_commands.c b/src/parser/parser_get_commands.c index cbf115e..6725b8c 100644 --- a/src/parser/parser_get_commands.c +++ b/src/parser/parser_get_commands.c @@ -14,7 +14,7 @@ static int is_command_token(t_token *token) { - return (token->type < 3 || token->type == T_REDIRECT_IN || token->type ==T_HEREDOC || token->type == T_REDIRECT_OUT || token->type == T_APPEND_OUT); + return (token->type < 3 || redirect_token_type(token)); } t_list *parser_get_commands(t_minishell *minishell) From 8b33185add356fb6df2e7ada7b1b4b04aadcd108 Mon Sep 17 00:00:00 2001 From: Quinten Date: Sun, 23 Feb 2025 13:14:48 +0100 Subject: [PATCH 8/8] readability --- inc/redirect.h | 2 ++ src/redirect/redirect_get_inputs.c | 2 +- src/redirect/redirect_get_outputs.c | 2 +- src/redirect/redirect_valid_type.c | 8 +++++++- 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/inc/redirect.h b/inc/redirect.h index 98a86e3..5cffd36 100644 --- a/inc/redirect.h +++ b/inc/redirect.h @@ -19,4 +19,6 @@ t_list *redirect_get_inputs(t_list *list); t_list *redirect_get_outputs(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 diff --git a/src/redirect/redirect_get_inputs.c b/src/redirect/redirect_get_inputs.c index a44a6fe..b6e0828 100644 --- a/src/redirect/redirect_get_inputs.c +++ b/src/redirect/redirect_get_inputs.c @@ -23,7 +23,7 @@ t_list *redirect_get_inputs(t_list *list) while (current) { token = (t_token *)current->content; - if (token->type == T_PIPE || token->type == T_EOF) + if (redirect_is_delimiter(token)) break ; if (token->type != T_REDIRECT_IN && token->type != T_HEREDOC) { diff --git a/src/redirect/redirect_get_outputs.c b/src/redirect/redirect_get_outputs.c index 9383bf6..8daee58 100644 --- a/src/redirect/redirect_get_outputs.c +++ b/src/redirect/redirect_get_outputs.c @@ -23,7 +23,7 @@ t_list *redirect_get_outputs(t_list *list) while (current) { token = (t_token *)current->content; - if (token->type == T_PIPE || token->type == T_EOF) + if (redirect_is_delimiter(token)) break; if (token->type != T_REDIRECT_OUT && token->type != T_APPEND_OUT) { diff --git a/src/redirect/redirect_valid_type.c b/src/redirect/redirect_valid_type.c index 2df0a3d..2554d32 100644 --- a/src/redirect/redirect_valid_type.c +++ b/src/redirect/redirect_valid_type.c @@ -28,4 +28,10 @@ int redirect_is_valid(t_list *lst, t_token *token) if (!next) return (0); return (redirect_token_type(token) && next->type < 3); -} \ No newline at end of file +} + +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); +} \ No newline at end of file