redirects
This commit is contained in:
parent
3c14325711
commit
2117a6538e
@ -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
|
||||
@ -1,12 +1,12 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* print_commands.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/18 20:06:37 by qmennen #+# #+# */
|
||||
/* Updated: 2025/02/18 20:11:45 by qmennen ### ########.fr */
|
||||
/* :::::::: */
|
||||
/* print_commands.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: marvin <marvin@student.42.fr> +#+ */
|
||||
/* +#+ */
|
||||
/* 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)
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -41,6 +41,5 @@ t_list *redirect_get_inputs(t_list *list)
|
||||
}
|
||||
current = current->next;
|
||||
}
|
||||
ft_lstiter(redirects, print_redirects);
|
||||
return (redirects);
|
||||
}
|
||||
45
src/redirect/redirect_get_outputs.c
Normal file
45
src/redirect/redirect_get_outputs.c
Normal 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);
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
@ -1,12 +1,12 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* free_command_list.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <marvin@student.42.fr> +#+ */
|
||||
/* +#+ */
|
||||
/* 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);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user