redirects

This commit is contained in:
Quinten 2025-02-23 12:44:31 +01:00
parent 3c14325711
commit 2117a6538e
7 changed files with 80 additions and 22 deletions

View File

@ -16,5 +16,7 @@
t_redirect *redirect_new(t_token_type type, char *value); t_redirect *redirect_new(t_token_type type, char *value);
t_list *redirect_get_inputs(t_list *list); 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_is_valid(t_list *lst, t_token *token);
int redirect_token_type(t_token *token);
#endif #endif

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* :::::::: */
/* print_commands.c :+: :+: :+: */ /* print_commands.c :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: marvin <marvin@student.42.fr> +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+ */
/* Created: 2025/02/18 20:06:37 by qmennen #+# #+# */ /* Created: 2025/02/18 20:06:37 by qmennen #+# #+# */
/* Updated: 2025/02/18 20:11:45 by qmennen ### ########.fr */ /* Updated: 2025/02/23 12:43:35 by Quinten ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,11 +21,15 @@ void print_commands(void *param)
if (!command) if (!command)
return ; return ;
printf("command: %s\n", command->command); printf("command: %s\n", command->command);
printf("args: "); printf("-- Args: ");
i = 0; i = 0;
while (command->args[i++]) while (command->args[i++])
printf("%s ", command->args[i]); printf("%s ", command->args[i]);
printf("\n"); 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) void print_redirects(void *param)
@ -35,7 +39,7 @@ void print_redirects(void *param)
redirect = (t_redirect *)param; redirect = (t_redirect *)param;
if (!redirect) if (!redirect)
return ; 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) void token_print(void *param)

View File

@ -12,6 +12,11 @@
#include "minishell.h" #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 *parser_get_commands(t_minishell *minishell)
{ {
t_list *command_list; 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 = parser_command_new(ft_strdup(token->value));
command->args = parser_get_arguments(current, minishell); command->args = parser_get_arguments(current, minishell);
command->redirect_in = redirect_get_inputs(current); command->redirect_in = redirect_get_inputs(current);
command->redirect_out = redirect_get_outputs(current);
ft_lstadd_back(&command_list, ft_lstnew(command)); 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; current = current->next;
if (current && ((t_token *)current->content)->type >= 3) if (current && ((t_token *)current->content)->type >= 3)
current = current->next; current = current->next;
} }
// ft_lstiter(command_list, print_commands); ft_lstiter(command_list, print_commands);
return (command_list); return (command_list);
} }

View File

@ -41,6 +41,5 @@ t_list *redirect_get_inputs(t_list *list)
} }
current = current->next; current = current->next;
} }
ft_lstiter(redirects, print_redirects);
return (redirects); return (redirects);
} }

View File

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* redirect_get_outputs.c :+: :+: */
/* +:+ */
/* By: Quinten <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* 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);
}

View File

@ -12,7 +12,7 @@
#include "minishell.h" #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 || return (token->type == T_REDIRECT_IN || token->type == T_HEREDOC ||
token->type == T_REDIRECT_OUT || token->type == T_APPEND_OUT); 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; next = (t_token *)lst->next->content;
if (!next) if (!next)
return (0); return (0);
return (is_redirection(token) && next->type < 3); return (redirect_token_type(token) && next->type < 3);
} }

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* :::::::: */
/* free_command_list.c :+: :+: :+: */ /* free_command_list.c :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: marvin <marvin@student.42.fr> +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+ */
/* Created: 2025/02/11 14:24:05 by qmennen #+# #+# */ /* Created: 2025/02/11 14:24:05 by qmennen #+# #+# */
/* Updated: 2025/02/11 14:27:02 by qmennen ### ########.fr */ /* Updated: 2025/02/23 12:40:17 by Quinten ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -56,5 +56,7 @@ void free_command_list(void *content)
free_args(command->args); free_args(command->args);
if (command->redirect_in) 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); free(command);
} }