Merge branch 'willem' into quinten

This commit is contained in:
Quinten Mennen 2025-03-05 13:21:14 +01:00
commit c3cc79d662
19 changed files with 276 additions and 233 deletions

View File

@ -6,7 +6,7 @@
# By: qmennen <qmennen@student.codam.nl> +#+ #
# +#+ #
# Created: 2024/10/15 11:48:46 by whaffman #+# #+# #
# Updated: 2025/03/03 22:50:22 by whaffman ######## odam.nl #
# Updated: 2025/03/04 18:26:26 by whaffman ######## odam.nl #
# #
# **************************************************************************** #

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/04 16:26:35 by whaffman #+# #+# */
/* Updated: 2025/03/03 21:32:49 by whaffman ######## odam.nl */
/* Updated: 2025/03/04 16:30:03 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -17,7 +17,7 @@
void environment_add(t_minishell *msh,
char *name, char *value);
void environment_print(t_minishell *msh);
void environment_print(t_minishell *msh, int export);
t_environment *environment_get(t_minishell *msh, char *name);
void environment_free_list(t_minishell *msh);
int environment_parse(t_minishell *msh, char **envp);

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/11 14:03:03 by qmennen #+# #+# */
/* Updated: 2025/02/28 14:08:11 by whaffman ######## odam.nl */
/* Updated: 2025/03/05 12:50:03 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -16,7 +16,7 @@
# include "minishell.h"
char *parser_sanitize_string(t_minishell *msh, char *str, char c);
t_command *parser_alloc_command(t_minishell *msh, char *cmd);
t_command *parser_alloc_command(t_minishell *msh);
t_list *parser_get_commands(t_minishell *msh);
t_list *parser_get_input_redirects(t_list *list);
void parser_create_command(t_minishell *msh,

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/05 12:36:08 by whaffman #+# #+# */
/* Updated: 2025/02/26 17:26:27 by whaffman ######## odam.nl */
/* Updated: 2025/03/04 18:11:00 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -58,9 +58,9 @@ typedef struct s_redirect
typedef struct s_command
{
char *command;
//char *command;
char **args;
t_list *environment;
// t_list *environment;
t_list *redirect_in;
t_list *redirect_out;
int fd_in;

View File

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

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/20 11:33:05 by whaffman #+# #+# */
/* Updated: 2025/02/26 15:46:29 by whaffman ######## odam.nl */
/* Updated: 2025/03/04 16:16:02 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -34,5 +34,5 @@ int builtin_echo(t_minishell *msh, t_command *cmd)
}
if (!n_flag)
printf("\n");
return (SUCCESS);
return (EXIT_SUCCESS);
}

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/20 11:33:02 by whaffman #+# #+# */
/* Updated: 2025/02/26 15:46:55 by whaffman ######## odam.nl */
/* Updated: 2025/03/04 16:33:16 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -15,6 +15,6 @@
int builtin_env(t_minishell *msh, t_command *cmd)
{
(void)cmd;
environment_print(msh);
environment_print(msh, FALSE);
return (SUCCESS);
}

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/20 11:32:53 by whaffman #+# #+# */
/* Updated: 2025/03/03 23:00:38 by whaffman ######## odam.nl */
/* Updated: 2025/03/04 16:41:06 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -33,16 +33,16 @@ int builtin_export(t_minishell *msh, t_command *cmd)
i = 0;
arr = NULL;
if (cmd->args[1] == NULL)
environment_print(msh, TRUE);
while (cmd->args[++i] != NULL)
{
arr = ft_split_safe(msh, cmd->args[i], '=');
if (arr == NULL || arr[0] == NULL)
return (ft_free_arr_safe(msh, arr), EXIT_FAILURE);
else if (ft_isvalid_identifier(arr[0]) == TRUE)
if (arr != NULL && arr[0] != NULL && ft_isvalid_identifier(arr[0]) == TRUE)
environment_update(msh, arr[0], arr[1]);
else
{
err = ft_strjoin_safe(msh, arr[0], ": not a valid identifier");
err = ft_strjoin_safe(msh, cmd->args[1], ": not a valid identifier");
error_msg("export", err);
return (ft_free_arr_safe(msh, arr), EXIT_FAILURE);
}

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/20 11:12:38 by whaffman #+# #+# */
/* Updated: 2025/03/03 14:38:52 by whaffman ######## odam.nl */
/* Updated: 2025/03/04 16:19:15 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -23,8 +23,8 @@ int builtin_router(t_minishell *msh, t_command *cmd)
builtin_env,
builtin_exit};
if (!is_builtin(cmd->args[0]))
if (is_builtin(cmd->args[0]) < 0)
return (FALSE);
msh->exit_status = builtin_fn[is_builtin(cmd->args[0])](msh, cmd);
return (TRUE);
return (builtin_fn[is_builtin(cmd->args[0])](msh, cmd));
}

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/20 11:03:33 by whaffman #+# #+# */
/* Updated: 2025/02/26 14:42:49 by whaffman ######## odam.nl */
/* Updated: 2025/03/04 15:45:38 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -25,5 +25,5 @@ int is_builtin(char *cmd)
return (i);
i++;
}
return (FALSE);
return (-1);
}

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/05 16:21:39 by whaffman #+# #+# */
/* Updated: 2025/02/26 16:08:49 by whaffman ######## odam.nl */
/* Updated: 2025/03/04 15:43:53 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -16,6 +16,5 @@ void simple_builtins(t_minishell *msh)
{
if (msh->commands == NULL)
return ;
if (!builtin_router(msh, msh->commands->content))
executor_execute_pipeline(msh);
}

View File

@ -6,7 +6,7 @@
/* 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 */
/* Updated: 2025/03/05 13:03:54 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -22,16 +22,20 @@ void print_commands(void *param)
command = (t_command *)param;
if (!command)
return ;
printf("command: %s\n", command->command);
printf("-- Args: ");
printf(BOLD "\n==========================\n" RESET);
printf(GREEN BOLD "Command: " RESET "%s\n", command->args[0]);
printf(BOLD "-- Args: " RESET);
i = 0;
while (command->args[i])
printf("%s ", command->args[i++]);
printf("\n");
printf("-- Input redirects:\n");
printf(BOLD "-- Input redirects:\n" RESET);
ft_lstiter(command->redirect_in, print_redirects);
printf("-- Output redirects:\n");
printf(BOLD "-- Output redirects:\n" RESET);
ft_lstiter(command->redirect_out, print_redirects);
printf(BOLD "-- fd_in:" RESET " %i\n", command->fd_in);
printf(BOLD "-- fd_out:" RESET " %i\n", command->fd_out);
printf(BOLD "==========================" RESET "\n");
}
void print_redirects(void *param)
@ -47,14 +51,26 @@ void print_redirects(void *param)
void token_print(void *param)
{
t_token *token;
const char *token_type[] = {
"T_WORD",
"T_DQWORD",
"T_SQWORD",
"T_PIPE",
"T_REDIRECT_IN",
"T_REDIRECT_OUT",
"T_AND",
"T_OR",
"T_APPEND_OUT",
"T_HEREDOC",
"T_EOF",
"T_ERROR"
};
if (!DEBUG)
return ;
token = (t_token *)param;
if (token->type == T_ERROR)
{
printf("Error: %d\n", token->type);
return ;
}
printf("token type %i, value %s\n", token->type, token->value);
if (token->type == 11)
printf(RED BOLD "[ERROR]\t\t" RESET "(%s)\n", token_type[token->type]);
else
printf( GREEN BOLD "[%s]\t\t" RESET "{%s(%d)}\n", token->value, token_type[token->type], token->type);
}

View File

@ -6,23 +6,25 @@
/* By: willem <willem@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/08 13:52:08 by willem #+# #+# */
/* Updated: 2025/03/01 14:24:19 by whaffman ######## odam.nl */
/* Updated: 2025/03/04 16:29:49 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
void environment_print(t_minishell *msh)
void environment_print(t_minishell *msh, int export)
{
char **arr;
int i;
t_list *env_list;
t_environment *env;
arr = environment_get_arr(msh);
i = 0;
while (arr[i] != NULL)
env_list = msh->environment;
while(env_list != NULL)
{
printf("%s\n", arr[i]);
i++;
env = (t_environment *)env_list->content;
if (export)
printf("declare -x %s=\"%s\"\n", env->name, env->value);
else
printf("%s=%s\n", env->name, env->value);
env_list = env_list->next;
}
ft_free_arr_safe(msh, arr);
}

View File

@ -6,32 +6,63 @@
/* By: willem <willem@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/12 21:25:02 by willem #+# #+# */
/* Updated: 2025/02/26 17:42:46 by whaffman ######## odam.nl */
/* Updated: 2025/03/05 13:10:59 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int execute_builtin(t_minishell *msh, t_command *cmd)
{
int original_stdout;
int original_stdin;
int exit_status;
original_stdin = dup(STDIN_FILENO);
original_stdout = dup(STDOUT_FILENO);
dup2(cmd->fd_in, STDIN_FILENO);
dup2(cmd->fd_out, STDOUT_FILENO);
signal_init_child();
exit_status = builtin_router(msh, cmd);
signal_init_minishell();
close(cmd->fd_in);
close(cmd->fd_out);
dup2(original_stdin, STDIN_FILENO);
dup2(original_stdout, STDOUT_FILENO);
close(original_stdin);
close(original_stdout);
return (exit_status);
}
static void executor_execute_command(t_minishell *msh, t_command *cmd)
{
if (cmd->args[0] != NULL && is_builtin(cmd->args[0]) >= 0)
msh->exit_status = execute_builtin(msh, cmd);
else if (cmd->args[0] != NULL)
executor_fork(msh, cmd);
}
int executor_execute_pipeline(t_minishell *msh)
{
t_list *current;
t_command *command;
pid_t last_pid;
int exit_status;
executor_create_pipes(msh);
executor_create_redirects(msh);
current = msh->commands;
ft_lstiter(current, print_commands);
last_pid = 0;
while (current)
{
command = (t_command *)current->content;
command->environment = msh->environment;
last_pid = executor_fork(msh, command);
executor_execute_command(msh, (t_command *)current->content);
current = current->next;
}
if (last_pid != 0)
{
waitpid(last_pid, &exit_status, 0);
msh->exit_status = ((exit_status) & 0xff00) >> 8;
}
signal_init_minishell();
return (((exit_status) & 0xff00) >> 8);
return (msh->exit_status);
}

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/04 16:19:22 by whaffman #+# #+# */
/* Updated: 2025/03/03 14:25:02 by whaffman ######## odam.nl */
/* Updated: 2025/03/04 16:08:26 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -23,7 +23,7 @@ static void main_loop(t_minishell *msh)
msh->tokens = ft_parse_input(msh);
ft_lstiter(msh->tokens, token_print);
msh->commands = parser_get_commands(msh);
simple_builtins(msh);
executor_execute_pipeline(msh);
free_minishell_line(msh);
}
}

View File

@ -1,18 +1,18 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* parser_new_command.c :+: :+: */
/* parser_alloc_command.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/11 16:18:21 by qmennen #+# #+# */
/* Updated: 2025/02/26 16:14:31 by whaffman ######## odam.nl */
/* Updated: 2025/03/04 18:18:09 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
t_command *parser_alloc_command(t_minishell *msh, char *cmd)
t_command *parser_alloc_command(t_minishell *msh)
{
t_command *command;
@ -22,8 +22,7 @@ t_command *parser_alloc_command(t_minishell *msh, char *cmd)
command->fd_out = 1;
command->redirect_in = NULL;
command->redirect_out = NULL;
command->environment = NULL;
// command->environment = NULL;
command->n_fds = 0;
command->command = cmd;
return (command);
}

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser_get_commands.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* :::::::: */
/* parser_get_commands.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/11 14:06:02 by qmennen #+# #+# */
/* Updated: 2025/02/27 16:07:54 by qmennen ### ########.fr */
/* Updated: 2025/03/04 18:21:27 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -22,7 +22,6 @@ t_list *parser_get_commands(t_minishell *msh)
t_list *command_list;
t_list *current;
t_command *command;
t_token *token;
command_list = NULL;
if (!msh->tokens)
@ -30,8 +29,7 @@ t_list *parser_get_commands(t_minishell *msh)
current = msh->tokens;
while (current)
{
token = (t_token *) current->content;
command = parser_alloc_command(msh, ft_strdup_safe(msh, token->value));
command = parser_alloc_command(msh);
parser_create_command(msh, command, &current);
if (! parser_validate_command(command))
break ;
@ -41,6 +39,5 @@ t_list *parser_get_commands(t_minishell *msh)
if (current && ((t_token *)current->content)->type >= 3)
current = current->next;
}
ft_lstiter(command_list, print_commands);
return (command_list);
}

View File

@ -50,8 +50,6 @@ void free_command_list(t_minishell *msh, void *content)
t_command *command;
command = (t_command *)content;
if (command->command)
free_safe(msh, (void **)&(command->command));
if (command->args)
free_args(msh, command->args);
if (command->redirect_in)

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/20 18:06:46 by whaffman #+# #+# */
/* Updated: 2025/02/20 18:07:00 by whaffman ######## odam.nl */
/* Updated: 2025/03/04 16:38:10 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
@ -15,7 +15,8 @@
void *malloc_safe(t_minishell *msh, size_t size)
{
void *ptr;
if (size == 0)
return (NULL);
ptr = malloc(size);
check_malloc(msh, ptr);
return (ptr);