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); }