Merge remote-tracking branch 'origin/quinten' into willem

This commit is contained in:
whaffman 2025-02-26 22:56:31 +01:00
commit bc019e4c04
11 changed files with 107 additions and 105 deletions

View File

@ -15,6 +15,8 @@
# include "minishell.h" # include "minishell.h"
t_redirect *redirect_new(t_minishell *msh, t_token_type type, char *value); t_redirect *redirect_new(t_minishell *msh, t_token_type type, char *value);
void redirect_new_error(t_minishell *msh, t_list **redirects);
void redirect_create(t_minishell *msh, t_list **tokens, t_list **redirects, t_token_type type);
t_list *redirect_get_inputs(t_minishell *msh, t_list *list); t_list *redirect_get_inputs(t_minishell *msh, t_list *list);
t_list *redirect_get_outputs(t_minishell *msh, t_list *list); t_list *redirect_get_outputs(t_minishell *msh, t_list *list);
int redirect_is_valid(t_list *lst, t_token *token, int mode); int redirect_is_valid(t_list *lst, t_token *token, int mode);

View File

@ -1,24 +0,0 @@
VPATH = src:src/prompt:src/utils:src/lexer:src/token:src/environment:src/executor:src/parser:src/debug:src/expander:src/builtin:src/signal:src/redirect:
SOURCES = history_load.c history_write.c prompt.c print_banner.c \
init_minishell.c ft_substr_safe.c check_malloc.c error_msg.c \
free_command_list.c free_freelist.c free_lexer.c free_minishell.c \
free_minishell_line.c free_safe.c free_token.c free_token_list.c \
ft_lstclear_safe.c ft_lstnew_safe.c ft_strdup_safe.c \
ft_strjoin_safe.c malloc_safe.c lexer_read_char.c lexer_new.c \
lexer_parse_input.c lexer_read_word.c lexer_token_next.c token_new.c \
token_parse.c environment_add.c environment_del.c environment_free.c \
environment_free_list.c environment_get.c environment_get_arr.c \
environment_parse.c environment_print.c executor_close_fds.c \
executor_absolute_path.c executor_child.c executor_count_fds.c \
executor_create_pipes.c executor_create_redirects.c \
executor_execute_pipeline.c executor_fork.c executor_open_fds.c \
parser_get_arguments.c parser_new_command.c parser_get_commands.c \
print_commands.c print_freelist.c expander_is_character.c \
expander_expand_dollar.c expander_allocate_memory.c \
expander_parse_string.c expander_parse_variables.c \
expander_get_variable.c builtin_cd.c builtin_echo.c builtin_env.c \
builtin_exit.c builtin_export.c builtin_pwd.c builtin_router.c \
builtin_unset.c is_builtin.c simple_builtins.c signal.c \
signal_init.c.c redirect_get_outputs.c redirect_new.c \
redirect_get_inputs.c redirect_valid_type.c \
redirect_process_heredoc.c main.c \

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* executor_child.c :+: :+: */ /* executor_child.c :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: willem <willem@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/12 21:25:10 by willem #+# #+# */ /* Created: 2025/02/12 21:25:10 by willem #+# #+# */
/* Updated: 2025/02/26 16:09:44 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 18:20:34 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,11 +21,11 @@ void executor_child(t_minishell *msh, t_command *command)
if (command->fd_out != 1) if (command->fd_out != 1)
dup2(command->fd_out, 1); dup2(command->fd_out, 1);
executor_close_fds(command->n_fds); executor_close_fds(command->n_fds);
path = executor_absolute_path(msh, command->command); path = executor_absolute_path(msh, command->args[0]);
if (path == NULL) if (path == NULL)
{ {
ft_putstr_fd(RED BOLD, 2); ft_putstr_fd(RED BOLD, 2);
ft_putstr_fd(command->command, 2); ft_putstr_fd(command->args[0], 2);
ft_putstr_fd(": " RESET "command not found\n", 2); ft_putstr_fd(": " RESET "command not found\n", 2);
return ; return ;
} }

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* expander_allocate_memory.c :+: :+: */ /* expander_allocate_memory.c :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/19 13:57:19 by qmennen #+# #+# */ /* Created: 2025/02/19 13:57:19 by qmennen #+# #+# */
/* Updated: 2025/02/26 16:10:42 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 18:05:17 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -37,5 +37,6 @@ char *expander_allocate_memory(
} }
size += ft_strlen(s); size += ft_strlen(s);
string = malloc_safe(msh, size); string = malloc_safe(msh, size);
string[0] = 0;
return (string); return (string);
} }

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/18 19:00:35 by qmennen #+# #+# */ /* Created: 2025/02/18 19:00:35 by qmennen #+# #+# */
/* Updated: 2025/02/26 17:23:30 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 22:56:05 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -37,16 +37,15 @@ char *expander_parse_string(char *s, t_minishell *msh)
variables = expander_parse_variables(s, msh); variables = expander_parse_variables(s, msh);
string = expander_allocate_memory(msh, s, variables); string = expander_allocate_memory(msh, s, variables);
current = variables;
i = 0; i = 0;
j = 0; j = 0;
current = variables; while (s[i] && current)
while (s[i])
{ {
if (s[i] == '$' && s[i + 1]) if (s[i] == '$' && s[i + 1])
{ {
i++; i++;
i += expander_expand_dollar(s + i, string, &j, current); i += expander_expand_dollar(s + i, string, &j, current);
if (current)
current = current->next; current = current->next;
} }
else else

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* parser_get_arguments.c :+: :+: */ /* parser_get_arguments.c :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/11 16:20:09 by qmennen #+# #+# */ /* Created: 2025/02/11 16:20:09 by qmennen #+# #+# */
/* Updated: 2025/02/26 16:14:14 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 18:35:27 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -38,7 +38,7 @@ static int parser_should_expand(t_list *value)
token = (t_token *)value->content; token = (t_token *)value->content;
if (!token) if (!token)
return (0); return (0);
return (token->type == T_DQWORD return ((token->type == T_DQWORD && ft_strchr(token->value, '$'))
|| (token->type == T_WORD || (token->type == T_WORD
&& token->value[0] == '$' && token->value[0] == '$'
&& token->value[1])); && token->value[1]));
@ -60,8 +60,7 @@ char **parser_get_arguments(t_list *list, t_minishell *msh)
if (parser_should_expand(current)) if (parser_should_expand(current))
args[i] = expander_parse_string( args[i] = expander_parse_string(
((t_token *)current->content)->value, msh); ((t_token *)current->content)->value, msh);
else if (((t_token *)current->content)->type == T_WORD else if (((t_token *)current->content)->type < 3)
|| ((t_token *)current->content)->type == T_SQWORD)
args[i] = ft_strdup_safe(msh, args[i] = ft_strdup_safe(msh,
((t_token *)current->content)->value); ((t_token *)current->content)->value);
current = current->next; current = current->next;

View File

@ -6,53 +6,43 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/23 12:29:05 by Quinten #+# #+# */ /* Created: 2025/02/23 12:29:05 by Quinten #+# #+# */
/* Updated: 2025/02/26 16:57:02 by qmennen ### ########.fr */ /* Updated: 2025/02/26 17:38:59 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "redirect.h" #include "redirect.h"
static void check_heredoc(t_minishell *msh, t_list *current, t_token *token)
{
if (token->type == T_HEREDOC && redirect_is_valid(current, token, -1))
process_heredoc(msh, token, current->next->content);
}
t_list *redirect_get_inputs(t_minishell *msh, t_list *list) t_list *redirect_get_inputs(t_minishell *msh, t_list *list)
{ {
t_list *current; t_list *current;
t_list *redirects; t_list *redirects;
t_token *token; t_token *token;
int result; int flag;
redirects = NULL; redirects = NULL;
current = list; current = list;
result = 1; flag = 1;
while (current && result) token = (t_token *)current->content;
while (current && flag && !redirect_is_delimiter(token))
{ {
token = (t_token *)current->content; token = (t_token *)current->content;
if (redirect_is_delimiter(token))
break ;
if (token->type != T_REDIRECT_IN && token->type != T_HEREDOC) if (token->type != T_REDIRECT_IN && token->type != T_HEREDOC)
{ {
current = current->next; current = current->next;
continue ; continue ;
} }
if (token->type == T_HEREDOC && redirect_is_valid(current, token, -1)) check_heredoc(msh, current, token);
result = process_heredoc(msh, token, current->next->content); flag = redirect_is_valid(current, token, F_OK | R_OK);
if (redirect_is_valid(current, token, F_OK | R_OK)) redirect_create(msh, &current, &redirects, token->type);
{
ft_lstadd_back(&redirects, ft_lstnew_safe(msh,
redirect_new(msh, token->type,
ft_strdup_safe(msh,
((t_token *)current->next->content)->value))));
current = current->next;
continue;
}
else
{
ft_lstadd_front(&redirects, ft_lstnew_safe(msh, redirect_new(msh, T_ERROR, NULL)));
break;
}
current = current->next; current = current->next;
} }
if (result < 0) if (flag <= 0)
{ redirect_new_error(msh, &redirects);
ft_lstadd_front(&redirects, ft_lstnew_safe(msh, redirect_new(msh, T_ERROR, NULL)));
}
return (redirects); return (redirects);
} }

View File

@ -17,32 +17,25 @@ t_list *redirect_get_outputs(t_minishell *msh, t_list *list)
t_list *current; t_list *current;
t_list *redirects; t_list *redirects;
t_token *token; t_token *token;
int flag;
flag = 1;
redirects = NULL; redirects = NULL;
current = list; current = list;
while (current) token = (t_token *)current->content;
while (current && flag && !redirect_is_delimiter(token))
{ {
token = (t_token *)current->content; token = (t_token *)current->content;
if (redirect_is_delimiter(token))
break ;
if (token->type != T_REDIRECT_OUT && token->type != T_APPEND_OUT) if (token->type != T_REDIRECT_OUT && token->type != T_APPEND_OUT)
{ {
current = current->next; current = current->next;
continue ; continue ;
} }
if (redirect_is_valid(current, token, -1)) flag = redirect_is_valid(current, token, -1);
{ redirect_create(msh, &current, &redirects, token->type);
ft_lstadd_front(&redirects,
ft_lstnew_safe(msh, redirect_new(msh, token->type, ft_strdup_safe(msh, ((t_token *)current->next->content)->value))));
current = current->next;
continue ;
}
else
{
ft_lstadd_front(&redirects, ft_lstnew_safe(msh, redirect_new(msh, T_ERROR, NULL)));
break ;
}
current = current->next; current = current->next;
} }
if (flag <= 0)
redirect_new_error(msh, &redirects);
return (redirects); return (redirects);
} }

View File

@ -23,3 +23,22 @@ t_redirect *redirect_new(t_minishell *msh, t_token_type type, char *value)
result->value = value; result->value = value;
return (result); return (result);
} }
void redirect_create(t_minishell *msh, t_list **tokens, t_list **redirects,
t_token_type type)
{
t_list *new;
t_redirect *redir;
t_token *file_token;
char *file_name;
file_token = (t_token *)((*tokens)->next->content);
file_name = NULL;
if (file_token)
file_name = ft_strdup_safe(msh, file_token->value);
redir = redirect_new(msh, type, file_name);
new = ft_lstnew_safe(msh, redir);
ft_lstadd_back(redirects, new);
if (tokens)
*tokens = (*tokens)->next;
}

View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* redirect_new_error.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/26 17:37:12 by qmennen #+# #+# */
/* Updated: 2025/02/26 17:44:49 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void redirect_new_error(t_minishell *msh, t_list **redirects)
{
t_list *new;
t_redirect *redir;
redir = redirect_new(msh, T_ERROR, NULL);
new = ft_lstnew_safe(msh, redir);
ft_lstadd_back(redirects, new);
}

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/23 12:30:18 by Quinten #+# #+# */ /* Created: 2025/02/23 12:30:18 by Quinten #+# #+# */
/* Updated: 2025/02/26 16:56:51 by qmennen ### ########.fr */ /* Updated: 2025/02/26 17:40:44 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */