parsing input redirects

This commit is contained in:
Quinten 2025-02-23 12:20:19 +01:00
parent 24752c9d08
commit 45968e547e
7 changed files with 57 additions and 5 deletions

View File

@ -17,5 +17,6 @@
void print_commands(void *param); void print_commands(void *param);
void token_print(void *param); void token_print(void *param);
void print_redirects(void *param);
#endif #endif

View File

@ -18,5 +18,6 @@
t_command *parser_command_new(char *cmd); t_command *parser_command_new(char *cmd);
char **parser_get_arguments(t_list *list, t_minishell *minishell); char **parser_get_arguments(t_list *list, t_minishell *minishell);
t_list *parser_get_commands(t_minishell *minishell); t_list *parser_get_commands(t_minishell *minishell);
t_list *parser_get_input_redirects(t_list *list);
#endif #endif

View File

@ -28,6 +28,16 @@ void print_commands(void *param)
printf("\n"); 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) void token_print(void *param)
{ {
t_token *token; t_token *token;

View File

@ -36,7 +36,6 @@ int main(int argc, char **argv, char **envp)
minishell->commands = parser_get_commands(minishell); minishell->commands = parser_get_commands(minishell);
simple_builtins(minishell); simple_builtins(minishell);
free_minishell_line(minishell); free_minishell_line(minishell);
ft_lstclear(&minishell->commands, free_command_list);
} }
ft_lstclear(&minishell->commands, free_command_list); ft_lstclear(&minishell->commands, free_command_list);
free_minishell(minishell); free_minishell(minishell);

View File

@ -30,7 +30,7 @@ t_list *parser_get_commands(t_minishell *minishell)
command->args = parser_get_arguments(current, minishell); command->args = parser_get_arguments(current, minishell);
command->redirect_in = parser_get_input_redirects(current); command->redirect_in = parser_get_input_redirects(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) while (current && (((t_token *)current->content)->type < 3 || ((t_token *)current->content)->type == T_REDIRECT_IN))
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;

View File

@ -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); 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; 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 *current;
t_list *redirects; t_list *redirects;
@ -28,12 +35,24 @@ void parser_get_input_redirects(t_list *list)
while (current) while (current)
{ {
token = (t_token *)current->content; token = (t_token *)current->content;
if (token->type != T_REDIRECT_IN && token->type != T_HEREDOC)
{
current = current->next;
continue ;
}
if (valid_def(current, token)) 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 else
{
ft_lstadd_front(&redirects, ft_lstnew(redirect_new(T_ERROR, NULL)));
break ; break ;
}
current = current->next; current = current->next;
} }
ft_lstiter(redirects, print_redirects);
return (redirects);
} }

View File

@ -25,6 +25,26 @@ static void free_args(char **args)
free(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) void free_command_list(void *content)
{ {
t_command *command; t_command *command;
@ -34,5 +54,7 @@ void free_command_list(void *content)
free(command->command); free(command->command);
if (command->args) if (command->args)
free_args(command->args); free_args(command->args);
if (command->redirect_in)
free_redirects(command->redirect_in);
free(command); free(command);
} }