Merge remote-tracking branch 'origin/main' into willem
This commit is contained in:
commit
1d4316cbf1
2
Makefile
2
Makefile
@ -6,7 +6,7 @@
|
||||
# By: qmennen <qmennen@student.codam.nl> +#+ #
|
||||
# +#+ #
|
||||
# Created: 2024/10/15 11:48:46 by whaffman #+# #+# #
|
||||
# Updated: 2025/02/23 12:30:21 by willem ######## odam.nl #
|
||||
# Updated: 2025/02/23 13:34:57 by willem ######## odam.nl #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
|
||||
@ -17,5 +17,6 @@
|
||||
|
||||
void print_commands(void *param);
|
||||
void token_print(void *param);
|
||||
void print_redirects(void *param);
|
||||
|
||||
#endif
|
||||
|
||||
@ -3,10 +3,10 @@
|
||||
/* :::::::: */
|
||||
/* minishell.h :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* By: marvin <marvin@student.42.fr> +#+ */
|
||||
/* +#+ */
|
||||
/* 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"
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
24
inc/redirect.h
Normal file
24
inc/redirect.h
Normal file
@ -0,0 +1,24 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* redirect.h :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: Quinten <qmennen@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* 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);
|
||||
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
|
||||
@ -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,25 @@ 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)
|
||||
{
|
||||
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)
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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;
|
||||
@ -28,12 +33,14 @@ 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 = 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)
|
||||
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);
|
||||
}
|
||||
|
||||
47
src/redirect/redirect_get_inputs.c
Normal file
47
src/redirect/redirect_get_inputs.c
Normal file
@ -0,0 +1,47 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* redirect_get_inputs.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: Quinten <qmennen@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* 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 (redirect_is_delimiter(token))
|
||||
break ;
|
||||
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;
|
||||
}
|
||||
return (redirects);
|
||||
}
|
||||
47
src/redirect/redirect_get_outputs.c
Normal file
47
src/redirect/redirect_get_outputs.c
Normal file
@ -0,0 +1,47 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* 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 (redirect_is_delimiter(token))
|
||||
break;
|
||||
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);
|
||||
}
|
||||
25
src/redirect/redirect_new.c
Normal file
25
src/redirect/redirect_new.c
Normal file
@ -0,0 +1,25 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* redirect_new.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: Quinten <qmennen@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* 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);
|
||||
}
|
||||
37
src/redirect/redirect_valid_type.c
Normal file
37
src/redirect/redirect_valid_type.c
Normal file
@ -0,0 +1,37 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* redirect_valid_type.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: Quinten <qmennen@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/02/23 12:30:18 by Quinten #+# #+# */
|
||||
/* Updated: 2025/02/23 12:30:18 by Quinten ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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 (redirect_token_type(token) && next->type < 3);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
@ -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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -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,9 @@ 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);
|
||||
if (command->redirect_out)
|
||||
free_redirects(command->redirect_out);
|
||||
free(command);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user