minishell -> msh

This commit is contained in:
whaffman 2025-02-26 16:17:07 +01:00
parent c2efcca0fa
commit 5d01cf3086
68 changed files with 382 additions and 381 deletions

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/20 11:09:02 by whaffman #+# #+# */ /* Created: 2025/02/20 11:09:02 by whaffman #+# #+# */
/* Updated: 2025/02/20 11:38:58 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 15:44:47 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,13 +15,13 @@
# include "minishell.h" # include "minishell.h"
int is_builtin(char *cmd); int is_builtin(char *cmd);
int builtin_router(t_minishell *minishell, t_command *cmd); int builtin_router(t_minishell *msh, t_command *cmd);
int builtin_echo(t_minishell *minishell, t_command *cmd); int builtin_echo(t_minishell *msh, t_command *cmd);
int builtin_cd(t_minishell *minishell, t_command *cmd); int builtin_cd(t_minishell *msh, t_command *cmd);
int builtin_pwd(t_minishell *minishell, t_command *cmd); int builtin_pwd(t_minishell *msh, t_command *cmd);
int builtin_export(t_minishell *minishell, t_command *cmd); int builtin_export(t_minishell *msh, t_command *cmd);
int builtin_unset(t_minishell *minishell, t_command *cmd); int builtin_unset(t_minishell *msh, t_command *cmd);
int builtin_env(t_minishell *minishell, t_command *cmd); int builtin_env(t_minishell *msh, t_command *cmd);
int builtin_exit(t_minishell *minishell, t_command *cmd); int builtin_exit(t_minishell *msh, t_command *cmd);
#endif // BUILTIN_H #endif // BUILTIN_H

View File

@ -18,6 +18,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); void print_redirects(void *param);
void print_freelist(t_minishell *minishell); void print_freelist(t_minishell *msh);
#endif #endif

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/04 16:26:35 by whaffman #+# #+# */ /* Created: 2025/02/04 16:26:35 by whaffman #+# #+# */
/* Updated: 2025/02/25 15:19:01 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 15:44:49 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,14 +15,14 @@
# include "minishell.h" # include "minishell.h"
void environment_add(t_minishell *minishell, void environment_add(t_minishell *msh,
char *name, char *value); char *name, char *value);
void environment_print(t_minishell *minishell); void environment_print(t_minishell *msh);
t_environment *environment_get(t_minishell *minishell, char *name); t_environment *environment_get(t_minishell *msh, char *name);
void environment_free_list(t_minishell *minishell); void environment_free_list(t_minishell *msh);
int environment_parse(t_minishell *minishell, char **envp); int environment_parse(t_minishell *msh, char **envp);
char **environment_get_arr(t_minishell *minishell); char **environment_get_arr(t_minishell *msh);
void environment_del(t_minishell *minishell, char *name); void environment_del(t_minishell *msh, char *name);
void environment_free(t_minishell *minishell, void *content); void environment_free(t_minishell *msh, void *content);
#endif // ENVIRONMENT_H #endif // ENVIRONMENT_H

View File

@ -6,7 +6,7 @@
/* By: willem <willem@student.codam.nl> +#+ */ /* By: willem <willem@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/08 17:06:07 by willem #+# #+# */ /* Created: 2025/02/08 17:06:07 by willem #+# #+# */
/* Updated: 2025/02/25 13:52:05 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 15:45:17 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,14 +15,14 @@
# include "minishell.h" # include "minishell.h"
char *executor_absolute_path(t_minishell *minishell, char *cmd); char *executor_absolute_path(t_minishell *msh, char *cmd);
void executor_child(t_minishell *minishell, t_command *command); void executor_child(t_minishell *msh, t_command *command);
pid_t executor_fork(t_minishell *minishell, t_command *command); pid_t executor_fork(t_minishell *msh, t_command *command);
void executor_create_pipes(t_minishell *minishell); void executor_create_pipes(t_minishell *msh);
int executor_execute_pipeline(t_minishell *minishell); int executor_execute_pipeline(t_minishell *msh);
void executor_close_fds(int n_fds); void executor_close_fds(int n_fds);
int executor_count_fds(t_minishell *minishell); int executor_count_fds(t_minishell *msh);
int executor_open_fds(t_list *redirect_list, int og_fd); int executor_open_fds(t_list *redirect_list, int og_fd);
void executor_create_redirects(t_minishell *minishell); void executor_create_redirects(t_minishell *msh);
#endif // EXECUTOR_H #endif // EXECUTOR_H

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/18 19:00:51 by qmennen #+# #+# */ /* Created: 2025/02/18 19:00:51 by qmennen #+# #+# */
/* Updated: 2025/02/25 14:32:18 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 15:45:19 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,14 +16,14 @@
# include "minishell.h" # include "minishell.h"
t_environment *expander_get_var(const char *s, int idx, t_environment *expander_get_var(const char *s, int idx,
t_minishell *minishell); t_minishell *msh);
t_list *expander_parse_variables(const char *s, t_list *expander_parse_variables(const char *s,
t_minishell *minishell); t_minishell *msh);
char *expander_allocate_memory( char *expander_allocate_memory(
t_minishell *minishell, t_minishell *msh,
const char *s, const char *s,
t_list *variables); t_list *variables);
char *expander_parse_string(char *s, t_minishell *minishell); char *expander_parse_string(char *s, t_minishell *msh);
int expander_character_valid(const char c); int expander_character_valid(const char c);
int expander_expand_dollar(char *src, char *dest, int expander_expand_dollar(char *src, char *dest,
int *j, t_list *variables); int *j, t_list *variables);

View File

@ -15,9 +15,9 @@
# include "minishell.h" # include "minishell.h"
t_command *parser_command_new(t_minishell *minishell, char *cmd); t_command *parser_command_new(t_minishell *msh, char *cmd);
char **parser_get_arguments(t_list *list, t_minishell *minishell); char **parser_get_arguments(t_list *list, t_minishell *msh);
t_list *parser_get_commands(t_minishell *minishell); t_list *parser_get_commands(t_minishell *msh);
t_list *parser_get_input_redirects(t_list *list); t_list *parser_get_input_redirects(t_list *list);
#endif #endif

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/04 16:35:35 by whaffman #+# #+# */ /* Created: 2025/02/04 16:35:35 by whaffman #+# #+# */
/* Updated: 2025/02/12 12:54:01 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 15:45:22 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,7 +16,7 @@
# include "minishell.h" # include "minishell.h"
void print_banner(void); void print_banner(void);
char *ft_prompt(t_minishell *minishell); char *ft_prompt(t_minishell *msh);
void history_write(char *line); void history_write(char *line);
void history_load(void); void history_load(void);

View File

@ -14,9 +14,9 @@
# define REDIRECT_H # define REDIRECT_H
# include "minishell.h" # include "minishell.h"
t_redirect *redirect_new(t_minishell *minishell, t_token_type type, char *value); t_redirect *redirect_new(t_minishell *msh, t_token_type type, char *value);
t_list *redirect_get_inputs(t_minishell *minishell, t_list *list); t_list *redirect_get_inputs(t_minishell *msh, t_list *list);
t_list *redirect_get_outputs(t_minishell *minishell, 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 redirect_is_valid(t_list *lst, t_token *token);
int redirect_token_type(t_token *token); int redirect_token_type(t_token *token);
int redirect_is_delimiter(t_token *token); int redirect_is_delimiter(t_token *token);

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/05 12:36:00 by whaffman #+# #+# */ /* Created: 2025/02/05 12:36:00 by whaffman #+# #+# */
/* Updated: 2025/02/25 16:19:05 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 15:45:26 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,18 +16,18 @@
/** /**
* Lexer * Lexer
*/ */
t_lexer *ft_lexer_new(t_minishell *minishell); t_lexer *ft_lexer_new(t_minishell *msh);
void ft_lexer_free(t_minishell *minishell, t_lexer *lexer); void ft_lexer_free(t_minishell *msh, t_lexer *lexer);
void lexer_readchar(t_lexer *lexer); void lexer_readchar(t_lexer *lexer);
char *lexer_readword(t_minishell *minishell, t_lexer *lexer); char *lexer_readword(t_minishell *msh, t_lexer *lexer);
t_list *ft_parse_input(t_minishell *minishell); t_list *ft_parse_input(t_minishell *msh);
/** /**
* Token * Token
*/ */
t_token *ft_token_next(t_minishell *minishell, t_lexer *lexer); t_token *ft_token_next(t_minishell *msh, t_lexer *lexer);
t_token *token_new(t_minishell *minishell, t_token_type type, char *c, int pos); t_token *token_new(t_minishell *msh, t_token_type type, char *c, int pos);
void ft_token_free(t_minishell *minishell, t_token *token); void ft_token_free(t_minishell *msh, t_token *token);
void ft_clear_tokenlist(t_minishell *minishell, void *content); void ft_clear_tokenlist(t_minishell *msh, void *content);
t_token *token_parse(t_minishell *minishell, t_lexer *lexer); t_token *token_parse(t_minishell *msh, t_lexer *lexer);
#endif // TOKENIZER_H #endif // TOKENIZER_H

View File

@ -13,23 +13,23 @@
#ifndef UTILS_H #ifndef UTILS_H
# define UTILS_H # define UTILS_H
void free_minishell_line(t_minishell *minishell); void free_minishell_line(t_minishell *msh);
void free_minishell(t_minishell **minishell); void free_minishell(t_minishell **msh);
void free_command_list(t_minishell *minishell, void *content); void free_command_list(t_minishell *msh, void *content);
t_minishell *init_minishell(void); t_minishell *init_minishell(void);
void print_banner(void); void print_banner(void);
void print_list(void *content); void print_list(void *content);
void simple_builtins(t_minishell *minishell); void simple_builtins(t_minishell *msh);
void error_msg(char *func, char *msg); void error_msg(char *func, char *msg);
void check_malloc(t_minishell *minishell, void *ptr); void check_malloc(t_minishell *msh, void *ptr);
char *ft_strdup_safe(t_minishell *minishell, const char *str); char *ft_strdup_safe(t_minishell *msh, const char *str);
char *ft_strjoin_safe(t_minishell *minishell, char *ft_strjoin_safe(t_minishell *msh,
const char *s1, const char *s2); const char *s1, const char *s2);
void *malloc_safe(t_minishell *minishell, size_t size); void *malloc_safe(t_minishell *msh, size_t size);
void free_safe(t_minishell *minishell, void **ptr); void free_safe(t_minishell *msh, void **ptr);
void free_freelist(t_minishell *minishell); void free_freelist(t_minishell *msh);
void ft_lstclear_safe(t_minishell *minishell, t_list **lst, void ft_lstclear_safe(t_minishell *msh, t_list **lst,
void (*del)(t_minishell *minishell, void *)); void (*del)(t_minishell *msh, void *));
t_list *ft_lstnew_safe(t_minishell *minishell, void *content); t_list *ft_lstnew_safe(t_minishell *msh, void *content);
#endif // UTILS_H #endif // UTILS_H

View File

@ -1,22 +1,23 @@
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: 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_load.c history_write.c prompt.c ft_lstclear_safe.c \ SOURCES = history_load.c history_write.c prompt.c init_minishell.c \
ft_lstnew_safe.c ft_strdup_safe.c ft_strjoin_safe.c init_minishell.c \ print_banner.c check_malloc.c free_command_list.c free_freelist.c \
malloc_safe.c free_minishell_line.c free_safe.c print_banner.c \ free_lexer.c free_minishell_line.c free_safe.c free_token.c \
error_msg.c check_malloc.c free_command_list.c free_freelist.c \ free_token_list.c ft_lstclear_safe.c ft_lstnew_safe.c \
free_lexer.c free_minishell.c free_token.c free_token_list.c \ ft_strdup_safe.c ft_strjoin_safe.c malloc_safe.c error_msg.c \
lexer_read_char.c lexer_new.c lexer_parse_input.c lexer_read_word.c \ free_minishell.c lexer_read_char.c lexer_new.c lexer_parse_input.c \
lexer_token_next.c token_new.c token_parse.c environment_add.c \ lexer_read_word.c lexer_token_next.c token_new.c token_parse.c \
environment_del.c environment_free.c environment_free_list.c \ environment_add.c environment_del.c environment_free.c \
environment_get.c environment_get_arr.c environment_print.c \ environment_free_list.c environment_get.c environment_get_arr.c \
environment_parse.c executor_close_fds.c executor_absolute_path.c \ environment_parse.c environment_print.c executor_close_fds.c \
executor_child.c executor_count_fds.c executor_create_pipes.c \ executor_open_fds.c executor_absolute_path.c executor_child.c \
executor_create_redirects.c executor_fork.c executor_open_fds.c \ executor_count_fds.c executor_create_pipes.c \
executor_execute_pipeline.c parser_get_commands.c \ executor_create_redirects.c executor_execute_pipeline.c \
parser_new_command.c parser_get_arguments.c signal.c print_freelist.c \ executor_fork.c parser_get_arguments.c parser_get_commands.c \
print_commands.c expander_expand_dollar.c expander_is_character.c \ parser_new_command.c signal.c signal_init.c.c print_commands.c \
expander_get_variable.c expander_parse_string.c \ print_freelist.c expander_expand_dollar.c expander_is_character.c \
expander_parse_variables.c expander_allocate_memory.c builtin_echo.c \ expander_allocate_memory.c expander_parse_string.c \
builtin_router.c simple_builtins.c is_builtin.c builtin_cd.c \ expander_parse_variables.c expander_get_variable.c is_builtin.c \
builtin_env.c builtin_export.c builtin_unset.c builtin_pwd.c \ builtin_cd.c builtin_echo.c builtin_env.c builtin_exit.c \
builtin_exit.c redirect_valid_type.c redirect_get_outputs.c \ builtin_export.c builtin_pwd.c builtin_router.c builtin_unset.c \
redirect_new.c redirect_get_inputs.c main.c \ simple_builtins.c redirect_valid_type.c redirect_new.c \
redirect_get_inputs.c redirect_get_outputs.c main.c \

View File

@ -6,20 +6,20 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/20 11:33:07 by whaffman #+# #+# */ /* Created: 2025/02/20 11:33:07 by whaffman #+# #+# */
/* Updated: 2025/02/25 13:57:57 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 15:45:55 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int builtin_cd(t_minishell *minishell, t_command *cmd) int builtin_cd(t_minishell *msh, t_command *cmd)
{ {
t_environment *env; t_environment *env;
char *path; char *path;
if (cmd->args[1] == NULL) if (cmd->args[1] == NULL)
{ {
env = environment_get(minishell, "HOME"); env = environment_get(msh, "HOME");
if (env == NULL || env->value == NULL) if (env == NULL || env->value == NULL)
{ {
ft_putendl_fd("minishell: cd: HOME not set", STDERR_FILENO); ft_putendl_fd("minishell: cd: HOME not set", STDERR_FILENO);

View File

@ -6,18 +6,18 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/20 11:33:05 by whaffman #+# #+# */ /* Created: 2025/02/20 11:33:05 by whaffman #+# #+# */
/* Updated: 2025/02/20 12:37:42 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 15:46:29 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int builtin_echo(t_minishell *minishell, t_command *cmd) int builtin_echo(t_minishell *msh, t_command *cmd)
{ {
int i; int i;
int n_flag; int n_flag;
(void)minishell; (void)msh;
i = 1; i = 1;
n_flag = 0; n_flag = 0;
if (cmd->args[i] != NULL && ft_strncmp(cmd->args[i], "-n", 3) == 0) if (cmd->args[i] != NULL && ft_strncmp(cmd->args[i], "-n", 3) == 0)

View File

@ -6,15 +6,15 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/20 11:33:02 by whaffman #+# #+# */ /* Created: 2025/02/20 11:33:02 by whaffman #+# #+# */
/* Updated: 2025/02/25 13:57:49 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 15:46:55 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int builtin_env(t_minishell *minishell, t_command *cmd) int builtin_env(t_minishell *msh, t_command *cmd)
{ {
(void)cmd; (void)cmd;
environment_print(minishell); environment_print(msh);
return (SUCCESS); return (SUCCESS);
} }

View File

@ -6,14 +6,14 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/20 11:32:59 by whaffman #+# #+# */ /* Created: 2025/02/20 11:32:59 by whaffman #+# #+# */
/* Updated: 2025/02/26 15:19:51 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 15:47:14 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
//TODO EXIT last exit status r:20 //TODO EXIT last exit status r:20
int builtin_exit(t_minishell *minishell, t_command *cmd) int builtin_exit(t_minishell *msh, t_command *cmd)
{ {
int exit_status; int exit_status;
@ -36,7 +36,7 @@ int builtin_exit(t_minishell *minishell, t_command *cmd)
} }
exit_status = ft_atoi(cmd->args[1]); exit_status = ft_atoi(cmd->args[1]);
} }
free_minishell(&minishell); free_minishell(&msh);
rl_clear_history(); rl_clear_history();
exit(exit_status); exit(exit_status);
return (FAILURE); return (FAILURE);

View File

@ -6,13 +6,13 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/20 11:32:53 by whaffman #+# #+# */ /* Created: 2025/02/20 11:32:53 by whaffman #+# #+# */
/* Updated: 2025/02/25 16:39:13 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 15:49:16 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int builtin_export(t_minishell *minishell, t_command *cmd) int builtin_export(t_minishell *msh, t_command *cmd)
{ {
t_environment *env; t_environment *env;
char **arr; char **arr;
@ -28,14 +28,14 @@ int builtin_export(t_minishell *minishell, t_command *cmd)
i++; i++;
continue ; continue ;
} }
env = environment_get(minishell, arr[0]); env = environment_get(msh, arr[0]);
if (env != NULL) if (env != NULL)
{ {
free_safe(minishell, (void **)&(env->value)); free_safe(msh, (void **)&(env->value));
env->value = ft_strdup_safe(minishell, arr[1]); env->value = ft_strdup_safe(msh, arr[1]);
} }
else else
environment_add(minishell, arr[0], arr[1]); environment_add(msh, arr[0], arr[1]);
ft_free_arr(arr); ft_free_arr(arr);
i++; i++;
} }

View File

@ -1,25 +1,25 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* :::::::: */
/* builtin_pwd.c :+: :+: :+: */ /* builtin_pwd.c :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+ */
/* Created: 2025/02/20 11:32:28 by whaffman #+# #+# */ /* Created: 2025/02/20 11:32:28 by whaffman #+# #+# */
/* Updated: 2025/02/25 20:07:22 by qmennen ### ########.fr */ /* Updated: 2025/02/26 16:06:09 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int builtin_pwd(t_minishell *minishell, t_command *cmd) int builtin_pwd(t_minishell *msh, t_command *cmd)
{ {
char *cwd; char *cwd;
(void)minishell; (void)msh;
(void)cmd; (void)cmd;
cwd = getcwd(NULL, 0); cwd = getcwd(NULL, 0);
printf("%s\n", cwd); printf("%s\n", cwd);
free_safe(minishell, (void **)&(cwd)); free_safe(msh, (void **)&(cwd));
return (SUCCESS); return (SUCCESS);
} }

View File

@ -6,13 +6,13 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/20 11:12:38 by whaffman #+# #+# */ /* Created: 2025/02/20 11:12:38 by whaffman #+# #+# */
/* Updated: 2025/02/20 13:56:10 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:06:18 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int builtin_router(t_minishell *minishell, t_command *cmd) int builtin_router(t_minishell *msh, t_command *cmd)
{ {
const t_builtin_fn builtin_fn[] = { const t_builtin_fn builtin_fn[] = {
builtin_echo, builtin_echo,
@ -25,6 +25,6 @@ int builtin_router(t_minishell *minishell, t_command *cmd)
if (!is_builtin(cmd->args[0])) if (!is_builtin(cmd->args[0]))
return (FALSE); return (FALSE);
builtin_fn[is_builtin(cmd->args[0])](minishell, cmd); builtin_fn[is_builtin(cmd->args[0])](msh, cmd);
return (TRUE); return (TRUE);
} }

View File

@ -6,21 +6,21 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/20 11:25:43 by whaffman #+# #+# */ /* Created: 2025/02/20 11:25:43 by whaffman #+# #+# */
/* Updated: 2025/02/25 13:57:29 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:07:21 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int builtin_unset(t_minishell *minishell, t_command *cmd) int builtin_unset(t_minishell *msh, t_command *cmd)
{ {
int i; int i;
i = 1; i = 1;
while (cmd->args[i] != NULL) while (cmd->args[i] != NULL)
{ {
if (environment_get(minishell, cmd->args[i]) != NULL) if (environment_get(msh, cmd->args[i]) != NULL)
environment_del(minishell, cmd->args[i]); environment_del(msh, cmd->args[i]);
i++; i++;
} }
return (SUCCESS); return (SUCCESS);

View File

@ -6,16 +6,16 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/05 16:21:39 by whaffman #+# #+# */ /* Created: 2025/02/05 16:21:39 by whaffman #+# #+# */
/* Updated: 2025/02/26 14:42:54 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:08:49 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void simple_builtins(t_minishell *minishell) void simple_builtins(t_minishell *msh)
{ {
if (minishell->commands == NULL) if (msh->commands == NULL)
return ; return ;
if (!builtin_router(minishell, minishell->commands->content)) if (!builtin_router(msh, msh->commands->content))
executor_execute_pipeline(minishell); executor_execute_pipeline(msh);
} }

View File

@ -6,13 +6,13 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/25 15:46:03 by whaffman #+# #+# */ /* Created: 2025/02/25 15:46:03 by whaffman #+# #+# */
/* Updated: 2025/02/26 14:44:46 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:08:53 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void print_freelist(t_minishell *minishell) void print_freelist(t_minishell *msh)
{ {
t_list *freelist; t_list *freelist;
int i; int i;
@ -20,7 +20,7 @@ void print_freelist(t_minishell *minishell)
if (!DEBUG) if (!DEBUG)
return ; return ;
i = 0; i = 0;
freelist = minishell->freelist; freelist = msh->freelist;
while (freelist) while (freelist)
{ {
printf("%d: %p\n", i, freelist->content); printf("%d: %p\n", i, freelist->content);

View File

@ -6,32 +6,32 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/04 16:13:33 by whaffman #+# #+# */ /* Created: 2025/02/04 16:13:33 by whaffman #+# #+# */
/* Updated: 2025/02/25 16:50:12 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:09:07 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void environment_add(t_minishell *minishell, char *name, char *value) void environment_add(t_minishell *msh, char *name, char *value)
{ {
t_environment *new_environment; t_environment *new_environment;
t_list *new_node; t_list *new_node;
t_list **environment; t_list **environment;
environment = &minishell->environment; environment = &msh->environment;
if (name != NULL && value != NULL) if (name != NULL && value != NULL)
{ {
new_environment = malloc_safe(minishell, sizeof(t_environment)); new_environment = malloc_safe(msh, sizeof(t_environment));
if (new_environment == NULL) if (new_environment == NULL)
return (perror("malloc")); return (perror("malloc"));
new_environment->name = ft_strdup_safe(minishell, name); new_environment->name = ft_strdup_safe(msh, name);
new_environment->value = ft_strdup_safe(minishell, value); new_environment->value = ft_strdup_safe(msh, value);
new_node = ft_lstnew_safe(minishell, new_environment); new_node = ft_lstnew_safe(msh, new_environment);
if (new_node == NULL if (new_node == NULL
|| new_environment->name == NULL || new_environment->name == NULL
|| new_environment->value == NULL) || new_environment->value == NULL)
{ {
environment_free(minishell, new_environment); environment_free(msh, new_environment);
return (perror("malloc")); return (perror("malloc"));
} }
ft_lstadd_back(environment, new_node); ft_lstadd_back(environment, new_node);

View File

@ -6,13 +6,13 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/11 14:17:53 by whaffman #+# #+# */ /* Created: 2025/02/11 14:17:53 by whaffman #+# #+# */
/* Updated: 2025/02/25 15:19:14 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:09:12 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void environment_del(t_minishell *minishell, char *name) void environment_del(t_minishell *msh, char *name)
{ {
t_list *prev; t_list *prev;
t_list *current; t_list *current;
@ -21,17 +21,17 @@ void environment_del(t_minishell *minishell, char *name)
prev = NULL; prev = NULL;
next = NULL; next = NULL;
current = minishell->environment; current = msh->environment;
while (current != NULL) while (current != NULL)
{ {
env = (t_environment *)current->content; env = (t_environment *)current->content;
if (ft_strncmp(env->name, name, ft_strlen(name) + 1) == 0) if (ft_strncmp(env->name, name, ft_strlen(name) + 1) == 0)
{ {
next = current->next; next = current->next;
environment_free(minishell, current->content); environment_free(msh, current->content);
free_safe(minishell, (void **)&(current)); free_safe(msh, (void **)&(current));
if (prev == NULL) if (prev == NULL)
minishell->environment = next; msh->environment = next;
else else
prev->next = next; prev->next = next;
return ; return ;

View File

@ -6,18 +6,18 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/11 15:38:43 by whaffman #+# #+# */ /* Created: 2025/02/11 15:38:43 by whaffman #+# #+# */
/* Updated: 2025/02/25 15:18:45 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:09:17 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void environment_free(t_minishell *minishell, void *content) void environment_free(t_minishell *msh, void *content)
{ {
t_environment *env; t_environment *env;
env = (t_environment *)content; env = (t_environment *)content;
free_safe(minishell, (void **)&(env->name)); free_safe(msh, (void **)&(env->name));
free_safe(minishell, (void **)&(env->value)); free_safe(msh, (void **)&(env->value));
free_safe(minishell, (void **)&(env)); free_safe(msh, (void **)&(env));
} }

View File

@ -6,18 +6,18 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/04 16:13:59 by whaffman #+# #+# */ /* Created: 2025/02/04 16:13:59 by whaffman #+# #+# */
/* Updated: 2025/02/25 15:24:08 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:09:15 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void environment_free_list(t_minishell *minishell) void environment_free_list(t_minishell *msh)
{ {
t_list **lst; t_list **lst;
t_list *next; t_list *next;
lst = &minishell->environment; lst = &msh->environment;
if (!lst) if (!lst)
return ; return ;
while (*lst) while (*lst)
@ -26,7 +26,7 @@ void environment_free_list(t_minishell *minishell)
next = (*lst)->next; next = (*lst)->next;
else else
next = NULL; next = NULL;
environment_free(minishell, (*lst)->content); environment_free(msh, (*lst)->content);
free(*lst); free(*lst);
*lst = next; *lst = next;
} }

View File

@ -6,18 +6,18 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/04 16:15:05 by whaffman #+# #+# */ /* Created: 2025/02/04 16:15:05 by whaffman #+# #+# */
/* Updated: 2025/02/25 13:35:13 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:09:22 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
t_environment *environment_get(t_minishell *minishell, char *name) t_environment *environment_get(t_minishell *msh, char *name)
{ {
t_environment *env; t_environment *env;
t_list *environment; t_list *environment;
environment = minishell->environment; environment = msh->environment;
while (environment != NULL) while (environment != NULL)
{ {
env = (t_environment *)environment->content; env = (t_environment *)environment->content;

View File

@ -6,27 +6,27 @@
/* By: willem <willem@student.codam.nl> +#+ */ /* By: willem <willem@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/08 13:59:48 by willem #+# #+# */ /* Created: 2025/02/08 13:59:48 by willem #+# #+# */
/* Updated: 2025/02/26 14:45:35 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:09:20 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
char **environment_get_arr(t_minishell *minishell) char **environment_get_arr(t_minishell *msh)
{ {
char **arr; char **arr;
t_environment *env; t_environment *env;
int i; int i;
t_list *environment; t_list *environment;
environment = minishell->environment; environment = msh->environment;
arr = malloc_safe(minishell, arr = malloc_safe(msh,
sizeof(char *) * (ft_lstsize(environment) + 1)); sizeof(char *) * (ft_lstsize(environment) + 1));
i = 0; i = 0;
while (environment != NULL) while (environment != NULL)
{ {
env = (t_environment *)environment->content; env = (t_environment *)environment->content;
arr[i] = malloc_safe(minishell, arr[i] = malloc_safe(msh,
ft_strlen(env->name) + ft_strlen(env->value) + 2); ft_strlen(env->name) + ft_strlen(env->value) + 2);
ft_strlcpy(arr[i], env->name, ft_strlen(env->name) + 1); ft_strlcpy(arr[i], env->name, ft_strlen(env->name) + 1);
ft_strlcat(arr[i], "=", ft_strlen(env->name) + 2); ft_strlcat(arr[i], "=", ft_strlen(env->name) + 2);

View File

@ -6,13 +6,13 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/05 15:52:33 by whaffman #+# #+# */ /* Created: 2025/02/05 15:52:33 by whaffman #+# #+# */
/* Updated: 2025/02/26 15:20:09 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:09:26 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int environment_parse(t_minishell *minishell, char **envp) int environment_parse(t_minishell *msh, char **envp)
{ {
char **env; char **env;
@ -23,7 +23,7 @@ int environment_parse(t_minishell *minishell, char **envp)
env = ft_split(*envp, '='); env = ft_split(*envp, '=');
if (!env) if (!env)
return (FAILURE); return (FAILURE);
environment_add(minishell, env[0], env[1]); environment_add(msh, env[0], env[1]);
ft_free_arr(env); ft_free_arr(env);
envp++; envp++;
} }

View File

@ -6,18 +6,18 @@
/* By: willem <willem@student.codam.nl> +#+ */ /* By: willem <willem@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/08 13:52:08 by willem #+# #+# */ /* Created: 2025/02/08 13:52:08 by willem #+# #+# */
/* Updated: 2025/02/25 13:39:45 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:09:35 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void environment_print(t_minishell *minishell) void environment_print(t_minishell *msh)
{ {
char **arr; char **arr;
int i; int i;
arr = environment_get_arr(minishell); arr = environment_get_arr(msh);
i = 0; i = 0;
while (arr[i] != NULL) while (arr[i] != NULL)
{ {

View File

@ -6,13 +6,13 @@
/* By: willem <willem@student.codam.nl> +#+ */ /* By: willem <willem@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/08 17:00:24 by willem #+# #+# */ /* Created: 2025/02/08 17:00:24 by willem #+# #+# */
/* Updated: 2025/02/25 16:39:59 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:09:42 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
char *executor_absolute_path(t_minishell *minishell, char *cmd) char *executor_absolute_path(t_minishell *msh, char *cmd)
{ {
char **path; char **path;
t_environment *path_env; t_environment *path_env;
@ -23,21 +23,21 @@ char *executor_absolute_path(t_minishell *minishell, char *cmd)
{ {
if (access(cmd, F_OK) == 0) if (access(cmd, F_OK) == 0)
{ {
executable = ft_strdup_safe(minishell, cmd); executable = ft_strdup_safe(msh, cmd);
if (!executable) if (!executable)
return (NULL); return (NULL);
return (executable); return (executable);
} }
return (NULL); return (NULL);
} }
path_env = environment_get(minishell, "PATH"); path_env = environment_get(msh, "PATH");
if (!path_env) if (!path_env)
return (NULL); return (NULL);
path = ft_split(path_env->value, ':'); path = ft_split(path_env->value, ':');
i = 0; i = 0;
while (path[i] != NULL) while (path[i] != NULL)
{ {
executable = malloc_safe(minishell, ft_strlen(path[i]) + ft_strlen(cmd) + 2); executable = malloc_safe(msh, ft_strlen(path[i]) + ft_strlen(cmd) + 2);
ft_strlcpy(executable, path[i], ft_strlen(path[i]) + 1); ft_strlcpy(executable, path[i], ft_strlen(path[i]) + 1);
ft_strlcat(executable, "/", ft_strlen(path[i]) + 2); ft_strlcat(executable, "/", ft_strlen(path[i]) + 2);
ft_strlcat(executable, cmd, ft_strlen(path[i]) + ft_strlen(cmd) + 2); ft_strlcat(executable, cmd, ft_strlen(path[i]) + ft_strlen(cmd) + 2);
@ -46,7 +46,7 @@ char *executor_absolute_path(t_minishell *minishell, char *cmd)
ft_free_arr(path); ft_free_arr(path);
return (executable); return (executable);
} }
free_safe(minishell, (void **)&executable); free_safe(msh, (void **)&executable);
i++; i++;
} }
return (ft_free_arr(path), NULL); return (ft_free_arr(path), NULL);

View File

@ -6,13 +6,13 @@
/* By: willem <willem@student.codam.nl> +#+ */ /* By: willem <willem@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 15:20:25 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:09:44 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void executor_child(t_minishell *minishell, t_command *command) void executor_child(t_minishell *msh, t_command *command)
{ {
char *path; char *path;
@ -21,7 +21,7 @@ void executor_child(t_minishell *minishell, 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(minishell, command->command); path = executor_absolute_path(msh, command->command);
if (path == NULL) if (path == NULL)
{ {
ft_putstr_fd(RED BOLD, 2); ft_putstr_fd(RED BOLD, 2);
@ -29,5 +29,5 @@ void executor_child(t_minishell *minishell, t_command *command)
ft_putstr_fd(": " RESET "command not found\n", 2); ft_putstr_fd(": " RESET "command not found\n", 2);
return ; return ;
} }
execve(path, command->args, environment_get_arr(minishell)); execve(path, command->args, environment_get_arr(msh));
} }

View File

@ -6,19 +6,19 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/21 12:42:26 by whaffman #+# #+# */ /* Created: 2025/02/21 12:42:26 by whaffman #+# #+# */
/* Updated: 2025/02/26 15:20:33 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:09:45 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int executor_count_fds(t_minishell *minishell) int executor_count_fds(t_minishell *msh)
{ {
int count; int count;
t_list *current; t_list *current;
t_command *command; t_command *command;
current = minishell->commands; current = msh->commands;
count = (ft_lstsize(current) - 1) * 2; count = (ft_lstsize(current) - 1) * 2;
while (current) while (current)
{ {

View File

@ -6,7 +6,7 @@
/* By: willem <willem@student.codam.nl> +#+ */ /* By: willem <willem@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/12 21:25:22 by willem #+# #+# */ /* Created: 2025/02/12 21:25:22 by willem #+# #+# */
/* Updated: 2025/02/25 14:19:57 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:10:19 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -6,18 +6,18 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/21 13:00:00 by whaffman #+# #+# */ /* Created: 2025/02/21 13:00:00 by whaffman #+# #+# */
/* Updated: 2025/02/21 13:16:03 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:10:35 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void executor_create_redirects(t_minishell *minishell) void executor_create_redirects(t_minishell *msh)
{ {
t_list *current; t_list *current;
t_command *cmd; t_command *cmd;
current = minishell->commands; current = msh->commands;
while (current) while (current)
{ {
cmd = (t_command *)current->content; cmd = (t_command *)current->content;

View File

@ -6,28 +6,28 @@
/* By: willem <willem@student.codam.nl> +#+ */ /* By: willem <willem@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/12 21:25:02 by willem #+# #+# */ /* Created: 2025/02/12 21:25:02 by willem #+# #+# */
/* Updated: 2025/02/25 13:52:35 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:10:39 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int executor_execute_pipeline(t_minishell *minishell) int executor_execute_pipeline(t_minishell *msh)
{ {
t_list *current; t_list *current;
t_command *command; t_command *command;
pid_t last_pid; pid_t last_pid;
int exit_status; int exit_status;
executor_create_pipes(minishell); executor_create_pipes(msh);
executor_create_redirects(minishell); executor_create_redirects(msh);
current = minishell->commands; current = msh->commands;
last_pid = 0; last_pid = 0;
while (current) while (current)
{ {
command = (t_command *)current->content; command = (t_command *)current->content;
command->environment = minishell->environment; command->environment = msh->environment;
last_pid = executor_fork(minishell, command); last_pid = executor_fork(msh, command);
current = current->next; current = current->next;
} }
waitpid(last_pid, &exit_status, 0); waitpid(last_pid, &exit_status, 0);

View File

@ -6,14 +6,14 @@
/* By: willem <willem@student.codam.nl> +#+ */ /* By: willem <willem@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/12 21:24:52 by willem #+# #+# */ /* Created: 2025/02/12 21:24:52 by willem #+# #+# */
/* Updated: 2025/02/26 15:21:42 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:10:40 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
//TODO should parent close the fds? //TODO should parent close the fds?
pid_t executor_fork(t_minishell *minishell, t_command *command) pid_t executor_fork(t_minishell *msh, t_command *command)
{ {
pid_t pid; pid_t pid;
@ -29,7 +29,7 @@ pid_t executor_fork(t_minishell *minishell, t_command *command)
else if (pid == 0) else if (pid == 0)
{ {
signal_init_child(); signal_init_child();
executor_child(minishell, command); executor_child(msh, command);
exit(127); exit(127);
} }
else else

View File

@ -6,14 +6,14 @@
/* 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 15:22:11 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:10:42 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
char *expander_allocate_memory( char *expander_allocate_memory(
t_minishell *minishell, t_minishell *msh,
const char *s, const char *s,
t_list *variables) t_list *variables)
{ {
@ -36,6 +36,6 @@ char *expander_allocate_memory(
current = current->next; current = current->next;
} }
size += ft_strlen(s); size += ft_strlen(s);
string = malloc_safe(minishell, size); string = malloc_safe(msh, size);
return (string); return (string);
} }

View File

@ -6,13 +6,13 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/19 13:59:03 by qmennen #+# #+# */ /* Created: 2025/02/19 13:59:03 by qmennen #+# #+# */
/* Updated: 2025/02/25 15:26:36 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:12:29 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
t_environment *expander_get_var(const char *s, int idx, t_minishell *minishell) t_environment *expander_get_var(const char *s, int idx, t_minishell *msh)
{ {
int i; int i;
t_environment *env; t_environment *env;
@ -24,7 +24,7 @@ t_environment *expander_get_var(const char *s, int idx, t_minishell *minishell)
name = ft_substr(s, idx, i); name = ft_substr(s, idx, i);
if (!name || !*name) if (!name || !*name)
return (NULL); return (NULL);
env = environment_get(minishell, name); env = environment_get(msh, name);
free_safe(minishell, (void **)&name); free_safe(msh, (void **)&name);
return (env); return (env);
} }

View File

@ -6,13 +6,13 @@
/* 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 15:22:54 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:13:40 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
static void free_variables(t_minishell *minishell, t_list *variables) static void free_variables(t_minishell *msh, t_list *variables)
{ {
t_list *current; t_list *current;
t_list *last; t_list *last;
@ -22,12 +22,12 @@ static void free_variables(t_minishell *minishell, t_list *variables)
{ {
last = current; last = current;
current = current->next; current = current->next;
free_safe(minishell, (void **)&last); free_safe(msh, (void **)&last);
} }
} }
//TODO: Figure out why echo "> echo "\as"" breaks //TODO: Figure out why echo "> echo "\as"" breaks
char *expander_parse_string(char *s, t_minishell *minishell) char *expander_parse_string(char *s, t_minishell *msh)
{ {
t_list *variables; t_list *variables;
t_list *current; t_list *current;
@ -35,8 +35,8 @@ char *expander_parse_string(char *s, t_minishell *minishell)
int i; int i;
int j; int j;
variables = expander_parse_variables(s, minishell); variables = expander_parse_variables(s, msh);
string = expander_allocate_memory(minishell, s, variables); string = expander_allocate_memory(msh, s, variables);
i = 0; i = 0;
j = 0; j = 0;
current = variables; current = variables;
@ -53,6 +53,6 @@ char *expander_parse_string(char *s, t_minishell *minishell)
string[j++] = s[i++]; string[j++] = s[i++];
} }
string[j] = 0; string[j] = 0;
free_variables(minishell, variables); free_variables(msh, variables);
return (string); return (string);
} }

View File

@ -6,13 +6,13 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/19 13:58:13 by qmennen #+# #+# */ /* Created: 2025/02/19 13:58:13 by qmennen #+# #+# */
/* Updated: 2025/02/26 15:23:17 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:13:42 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
t_list *expander_parse_variables(const char *s, t_minishell *minishell) t_list *expander_parse_variables(const char *s, t_minishell *msh)
{ {
int i; int i;
t_list *var_list; t_list *var_list;
@ -24,11 +24,11 @@ t_list *expander_parse_variables(const char *s, t_minishell *minishell)
{ {
if (s[i] == '$') if (s[i] == '$')
{ {
env = expander_get_var(s, i + 1, minishell); env = expander_get_var(s, i + 1, msh);
if (env) if (env)
ft_lstadd_back(&var_list, ft_lstnew_safe(minishell, env)); ft_lstadd_back(&var_list, ft_lstnew_safe(msh, env));
else else
ft_lstadd_back(&var_list, ft_lstnew_safe(minishell, NULL)); ft_lstadd_back(&var_list, ft_lstnew_safe(msh, NULL));
} }
i++; i++;
} }

View File

@ -6,20 +6,20 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/05 19:03:01 by qmennen #+# #+# */ /* Created: 2025/02/05 19:03:01 by qmennen #+# #+# */
/* Updated: 2025/02/26 15:23:52 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:13:50 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
t_lexer *ft_lexer_new(t_minishell *minishell) t_lexer *ft_lexer_new(t_minishell *msh)
{ {
char *input; char *input;
t_lexer *lexer; t_lexer *lexer;
input = minishell->line; input = msh->line;
lexer = malloc_safe(minishell, sizeof(t_lexer)); lexer = malloc_safe(msh, sizeof(t_lexer));
lexer->input = ft_strdup_safe(minishell, input); lexer->input = ft_strdup_safe(msh, input);
lexer->pos = 0; lexer->pos = 0;
lexer->n_pos = 1; lexer->n_pos = 1;
lexer->current_char = '\0'; lexer->current_char = '\0';

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/05 19:09:20 by qmennen #+# #+# */ /* Created: 2025/02/05 19:09:20 by qmennen #+# #+# */
/* Updated: 2025/02/26 15:24:39 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:13:54 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,21 +24,21 @@
* the input to be parsed. * the input to be parsed.
* @return A linked list of tokens parsed from the input. * @return A linked list of tokens parsed from the input.
*/ */
t_list *ft_parse_input(t_minishell *minishell) t_list *ft_parse_input(t_minishell *msh)
{ {
t_list *list; t_list *list;
t_token *token; t_token *token;
t_lexer *lexer; t_lexer *lexer;
lexer = minishell->lexer; lexer = msh->lexer;
list = NULL; list = NULL;
while (TRUE) while (TRUE)
{ {
token = ft_token_next(minishell, lexer); token = ft_token_next(msh, lexer);
if (token->type == T_EOF || token->type == T_ERROR) if (token->type == T_EOF || token->type == T_ERROR)
break ; break ;
ft_lstadd_back(&list, ft_lstnew_safe(minishell, token)); ft_lstadd_back(&list, ft_lstnew_safe(msh, token));
} }
ft_token_free(minishell, token); ft_token_free(msh, token);
return (list); return (list);
} }

View File

@ -6,13 +6,13 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/05 19:03:47 by qmennen #+# #+# */ /* Created: 2025/02/05 19:03:47 by qmennen #+# #+# */
/* Updated: 2025/02/25 15:27:01 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:13:59 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
static char *parse_quotes(t_minishell *minishell, t_lexer *lexer) static char *parse_quotes(t_minishell *msh, t_lexer *lexer)
{ {
int start; int start;
int len; int len;
@ -26,19 +26,19 @@ static char *parse_quotes(t_minishell *minishell, t_lexer *lexer)
while (lexer->current_char != '\0' && lexer->current_char != qc) while (lexer->current_char != '\0' && lexer->current_char != qc)
lexer_readchar(lexer); lexer_readchar(lexer);
len = lexer->pos - start; len = lexer->pos - start;
word = malloc_safe(minishell, sizeof(char) * len + 1); word = malloc_safe(msh, sizeof(char) * len + 1);
ft_strlcpy(word, lexer->input + start, len + 1); ft_strlcpy(word, lexer->input + start, len + 1);
if (lexer->current_char == qc) if (lexer->current_char == qc)
lexer_readchar(lexer); lexer_readchar(lexer);
else else
{ {
free_safe(minishell, (void **)&word); free_safe(msh, (void **)&word);
return (NULL); return (NULL);
} }
return (word); return (word);
} }
char *lexer_readword(t_minishell *minishell, t_lexer *lexer) char *lexer_readword(t_minishell *msh, t_lexer *lexer)
{ {
int start; int start;
int len; int len;
@ -47,7 +47,7 @@ char *lexer_readword(t_minishell *minishell, t_lexer *lexer)
start = lexer->pos; start = lexer->pos;
if (lexer->current_char == '"' || lexer->current_char == '\'') if (lexer->current_char == '"' || lexer->current_char == '\'')
{ {
return (parse_quotes(minishell, lexer)); return (parse_quotes(msh, lexer));
} }
while (ft_isprint(lexer->current_char) && lexer->current_char != '<' while (ft_isprint(lexer->current_char) && lexer->current_char != '<'
&& lexer->current_char != '>' && lexer->current_char != '|' && lexer->current_char != '>' && lexer->current_char != '|'
@ -57,7 +57,7 @@ char *lexer_readword(t_minishell *minishell, t_lexer *lexer)
lexer_readchar(lexer); lexer_readchar(lexer);
} }
len = lexer->pos - start; len = lexer->pos - start;
word = malloc_safe(minishell, sizeof(char) * len + 1); word = malloc_safe(msh, sizeof(char) * len + 1);
ft_strlcpy(word, lexer->input + start, len + 1); ft_strlcpy(word, lexer->input + start, len + 1);
return (word); return (word);
} }

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/04 16:07:58 by qmennen #+# #+# */ /* Created: 2025/02/04 16:07:58 by qmennen #+# #+# */
/* Updated: 2025/02/25 15:27:11 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:14:10 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -42,7 +42,7 @@ static t_token_type get_word_type(char c)
* - If it is a printable character, reads the word and creates a word token. * - If it is a printable character, reads the word and creates a word token.
* - Otherwise, creates an error token. * - Otherwise, creates an error token.
*/ */
t_token *ft_token_next(t_minishell *minishell, t_lexer *lexer) t_token *ft_token_next(t_minishell *msh, t_lexer *lexer)
{ {
t_token *token; t_token *token;
t_token_type word_type; t_token_type word_type;
@ -54,22 +54,22 @@ t_token *ft_token_next(t_minishell *minishell, t_lexer *lexer)
lexer_readchar(lexer); lexer_readchar(lexer);
current_pos = lexer->pos; current_pos = lexer->pos;
if (lexer->current_char == '\0') if (lexer->current_char == '\0')
token = token_new(minishell, T_EOF, NULL, current_pos); token = token_new(msh, T_EOF, NULL, current_pos);
else if (lexer->current_char == '<' || lexer->current_char == '>' else if (lexer->current_char == '<' || lexer->current_char == '>'
|| lexer->current_char == '|') || lexer->current_char == '|')
token = token_parse(minishell, lexer); token = token_parse(msh, lexer);
else if (ft_isprint(lexer->current_char)) else if (ft_isprint(lexer->current_char))
{ {
word_type = get_word_type(lexer->current_char); word_type = get_word_type(lexer->current_char);
word = lexer_readword(minishell, lexer); word = lexer_readword(msh, lexer);
if (!word) if (!word)
return (token_new(minishell, T_ERROR, &lexer->current_char, current_pos)); return (token_new(msh, T_ERROR, &lexer->current_char, current_pos));
token = token_new(minishell, word_type, word, current_pos); token = token_new(msh, word_type, word, current_pos);
free_safe(minishell, (void **)&word); free_safe(msh, (void **)&word);
} }
else else
{ {
token = token_new(minishell, T_ERROR, NULL, current_pos); token = token_new(msh, T_ERROR, NULL, current_pos);
printf("token->type: %d\n", token->type); printf("token->type: %d\n", token->type);
} }
return (token); return (token);

View File

@ -16,29 +16,29 @@
int main(int argc, char **argv, char **envp) int main(int argc, char **argv, char **envp)
{ {
t_minishell *minishell; t_minishell *msh;
(void)argc; (void)argc;
(void)argv; (void)argv;
print_banner(); print_banner();
history_load(); history_load();
minishell = init_minishell(); msh = init_minishell();
signal_init_minishell(); signal_init_minishell();
environment_parse(minishell, envp); environment_parse(msh, envp);
while (TRUE) while (TRUE)
{ {
minishell->line = ft_prompt(minishell); msh->line = ft_prompt(msh);
if (minishell->line == NULL) if (msh->line == NULL)
break ; break ;
minishell->lexer = ft_lexer_new(minishell); msh->lexer = ft_lexer_new(msh);
minishell->tokens = ft_parse_input(minishell); msh->tokens = ft_parse_input(msh);
ft_lstiter(minishell->tokens, token_print); ft_lstiter(msh->tokens, token_print);
minishell->commands = parser_get_commands(minishell); msh->commands = parser_get_commands(msh);
simple_builtins(minishell); simple_builtins(msh);
free_minishell_line(minishell); free_minishell_line(msh);
} }
ft_lstclear_safe(minishell, &minishell->commands, free_command_list); ft_lstclear_safe(msh, &msh->commands, free_command_list);
free_minishell(&minishell); free_minishell(&msh);
rl_clear_history(); rl_clear_history();
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
} }

View File

@ -6,7 +6,7 @@
/* 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 15:26:32 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:14:14 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -44,7 +44,7 @@ static int parser_should_expand(t_list *value)
&& token->value[1])); && token->value[1]));
} }
char **parser_get_arguments(t_list *list, t_minishell *minishell) char **parser_get_arguments(t_list *list, t_minishell *msh)
{ {
t_list *current; t_list *current;
char **args; char **args;
@ -52,17 +52,17 @@ char **parser_get_arguments(t_list *list, t_minishell *minishell)
int i; int i;
cmds = count_cmds(list); cmds = count_cmds(list);
args = malloc_safe(minishell, (cmds + 1) * sizeof(char *)); args = malloc_safe(msh, (cmds + 1) * sizeof(char *));
current = list; current = list;
i = -1; i = -1;
while ((++i) < cmds && current) while ((++i) < cmds && current)
{ {
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, minishell); ((t_token *)current->content)->value, msh);
else if (((t_token *)current->content)->type == T_WORD else if (((t_token *)current->content)->type == T_WORD
|| ((t_token *)current->content)->type == T_SQWORD) || ((t_token *)current->content)->type == T_SQWORD)
args[i] = ft_strdup_safe(minishell, args[i] = ft_strdup_safe(msh,
((t_token *)current->content)->value); ((t_token *)current->content)->value);
current = current->next; current = current->next;
} }

View File

@ -17,7 +17,7 @@ static int is_command_token(t_token *token)
return (token->type < 3 || redirect_token_type(token)); return (token->type < 3 || redirect_token_type(token));
} }
t_list *parser_get_commands(t_minishell *minishell) t_list *parser_get_commands(t_minishell *msh)
{ {
t_list *command_list; t_list *command_list;
t_list *current; t_list *current;
@ -25,18 +25,18 @@ t_list *parser_get_commands(t_minishell *minishell)
t_token *token; t_token *token;
command_list = NULL; command_list = NULL;
if (!minishell->tokens) if (!msh->tokens)
return (NULL); return (NULL);
current = minishell->tokens; current = msh->tokens;
while (current) while (current)
{ {
token = (t_token *) current->content; token = (t_token *) current->content;
command = parser_command_new(minishell, command = parser_command_new(msh,
ft_strdup_safe(minishell, token->value)); ft_strdup_safe(msh, token->value));
command->args = parser_get_arguments(current, minishell); command->args = parser_get_arguments(current, msh);
command->redirect_in = redirect_get_inputs(minishell, current); command->redirect_in = redirect_get_inputs(msh, current);
command->redirect_out = redirect_get_outputs(minishell, current); command->redirect_out = redirect_get_outputs(msh, current);
ft_lstadd_back(&command_list, ft_lstnew_safe(minishell, command)); ft_lstadd_back(&command_list, ft_lstnew_safe(msh, command));
while (current && is_command_token((t_token *)current->content)) while (current && is_command_token((t_token *)current->content))
current = current->next; current = current->next;
if (current && ((t_token *)current->content)->type >= 3) if (current && ((t_token *)current->content)->type >= 3)

View File

@ -6,17 +6,17 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/11 16:18:21 by qmennen #+# #+# */ /* Created: 2025/02/11 16:18:21 by qmennen #+# #+# */
/* Updated: 2025/02/26 15:32:45 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:14:31 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
t_command *parser_command_new(t_minishell *minishell, char *cmd) t_command *parser_command_new(t_minishell *msh, char *cmd)
{ {
t_command *command; t_command *command;
command = malloc_safe(minishell, sizeof(t_command)); command = malloc_safe(msh, sizeof(t_command));
command->args = NULL; command->args = NULL;
command->fd_in = 0; command->fd_in = 0;
command->fd_out = 1; command->fd_out = 1;

View File

@ -6,13 +6,13 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/04 16:13:08 by whaffman #+# #+# */ /* Created: 2025/02/04 16:13:08 by whaffman #+# #+# */
/* Updated: 2025/02/26 15:33:15 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:15:04 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
char *get_user(t_minishell *minishell) char *get_user(t_minishell *msh)
{ {
const char guest[] = "guest"; const char guest[] = "guest";
int len; int len;
@ -20,32 +20,32 @@ char *get_user(t_minishell *minishell)
char *username; char *username;
char *result; char *result;
user = environment_get(minishell, "USER"); user = environment_get(msh, "USER");
if (user == NULL) if (user == NULL)
username = (char *)guest; username = (char *)guest;
else else
username = user->value; username = user->value;
len = ft_strlen(username) + ft_strlen(PROMPT GREEN) + 1; len = ft_strlen(username) + ft_strlen(PROMPT GREEN) + 1;
result = (char *)malloc_safe(minishell, len); result = (char *)malloc_safe(msh, len);
ft_strlcpy(result, GREEN, ft_strlen(GREEN) + 1); ft_strlcpy(result, GREEN, ft_strlen(GREEN) + 1);
ft_strlcat(result, username, ft_strlen(GREEN) + ft_strlen(username) + 1); ft_strlcat(result, username, ft_strlen(GREEN) + ft_strlen(username) + 1);
ft_strlcat(result, PROMPT, len); ft_strlcat(result, PROMPT, len);
return (result); return (result);
} }
int get_home_len(t_minishell *minishell, char *cwd) int get_home_len(t_minishell *msh, char *cwd)
{ {
t_environment *home; t_environment *home;
int home_len; int home_len;
home = environment_get(minishell, "HOME"); home = environment_get(msh, "HOME");
home_len = 0; home_len = 0;
if (home && !ft_strncmp(cwd, home->value, ft_strlen(home->value))) if (home && !ft_strncmp(cwd, home->value, ft_strlen(home->value)))
home_len = ft_strlen(home->value) - 1; home_len = ft_strlen(home->value) - 1;
return (home_len); return (home_len);
} }
char *get_path_with_home(t_minishell *minishell) char *get_path_with_home(t_minishell *msh)
{ {
char *result; char *result;
char *cwd; char *cwd;
@ -55,9 +55,9 @@ char *get_path_with_home(t_minishell *minishell)
cwd = getcwd(NULL, 0); cwd = getcwd(NULL, 0);
if (cwd == NULL) if (cwd == NULL)
return (perror("getcwd"), NULL); return (perror("getcwd"), NULL);
home_len = get_home_len(minishell, cwd); home_len = get_home_len(msh, cwd);
len = ft_strlen(cwd) - home_len + 1; len = ft_strlen(cwd) - home_len + 1;
result = malloc_safe(minishell, len); result = malloc_safe(msh, len);
if (home_len) if (home_len)
{ {
ft_strlcpy(result, "~", 2); ft_strlcpy(result, "~", 2);
@ -69,43 +69,43 @@ char *get_path_with_home(t_minishell *minishell)
return (result); return (result);
} }
char *get_path(t_minishell *minishell) char *get_path(t_minishell *msh)
{ {
char *result; char *result;
char *cwd; char *cwd;
int len; int len;
cwd = get_path_with_home(minishell); cwd = get_path_with_home(msh);
if (cwd == NULL) if (cwd == NULL)
return (NULL); return (NULL);
len = ft_strlen(cwd) + ft_strlen(BLUE RESET "> ") + 1; len = ft_strlen(cwd) + ft_strlen(BLUE RESET "> ") + 1;
result = malloc_safe(minishell, len); result = malloc_safe(msh, len);
ft_strlcpy(result, BLUE, len); ft_strlcpy(result, BLUE, len);
ft_strlcat(result, cwd, len); ft_strlcat(result, cwd, len);
ft_strlcat(result, RESET "> ", len); ft_strlcat(result, RESET "> ", len);
free_safe(minishell, (void **) &cwd); free_safe(msh, (void **) &cwd);
return (result); return (result);
} }
char *ft_prompt(t_minishell *minishell) char *ft_prompt(t_minishell *msh)
{ {
char *line; char *line;
char *cwd; char *cwd;
char *prompt; char *prompt;
char *user; char *user;
cwd = get_path(minishell); cwd = get_path(msh);
if (cwd == NULL) if (cwd == NULL)
return (NULL); return (NULL);
user = get_user(minishell); user = get_user(msh);
if (user == NULL) if (user == NULL)
return (free(cwd), NULL); return (free(cwd), NULL);
prompt = ft_strjoin_safe(minishell, user, cwd); prompt = ft_strjoin_safe(msh, user, cwd);
free_safe(minishell, (void **)&user); free_safe(msh, (void **)&user);
free_safe(minishell, (void **)&cwd); free_safe(msh, (void **)&cwd);
rl_on_new_line(); rl_on_new_line();
line = readline(prompt); line = readline(prompt);
free_safe(minishell, (void **)&prompt); free_safe(msh, (void **)&prompt);
if (line == NULL) if (line == NULL)
return (NULL); return (NULL);
history_write(line); history_write(line);

View File

@ -35,7 +35,7 @@ static void process_heredoc(t_minishell *msh, t_token *heredoc, t_token *delim)
delim->value = ft_strdup_safe(msh, ".ms_heredoc"); delim->value = ft_strdup_safe(msh, ".ms_heredoc");
} }
t_list *redirect_get_inputs(t_minishell *minishell, 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;
@ -54,19 +54,19 @@ t_list *redirect_get_inputs(t_minishell *minishell, t_list *list)
continue ; continue ;
} }
if (token->type == T_HEREDOC && redirect_is_valid(current, token)) if (token->type == T_HEREDOC && redirect_is_valid(current, token))
process_heredoc(minishell, token, current->next->content); process_heredoc(msh, token, current->next->content);
if (redirect_is_valid(current, token)) if (redirect_is_valid(current, token))
{ {
ft_lstadd_back(&redirects, ft_lstnew_safe(minishell, ft_lstadd_back(&redirects, ft_lstnew_safe(msh,
redirect_new(minishell, token->type, redirect_new(msh, token->type,
ft_strdup_safe(minishell, ft_strdup_safe(msh,
((t_token *)current->next->content)->value)))); ((t_token *)current->next->content)->value))));
current = current->next; current = current->next;
continue ; continue ;
} }
else else
{ {
ft_lstadd_front(&redirects, ft_lstnew_safe(minishell, redirect_new(minishell, T_ERROR, NULL))); ft_lstadd_front(&redirects, ft_lstnew_safe(msh, redirect_new(msh, T_ERROR, NULL)));
break ; break ;
} }
current = current->next; current = current->next;

View File

@ -6,21 +6,21 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/05 19:10:49 by qmennen #+# #+# */ /* Created: 2025/02/05 19:10:49 by qmennen #+# #+# */
/* Updated: 2025/02/25 16:42:52 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:15:17 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
t_token *token_new(t_minishell *minishell, t_token_type type, char *c, int pos) t_token *token_new(t_minishell *msh, t_token_type type, char *c, int pos)
{ {
t_token *token; t_token *token;
token = malloc_safe(minishell, sizeof(t_token)); token = malloc_safe(msh, sizeof(t_token));
token->type = type; token->type = type;
token->position = pos; token->position = pos;
if (c) if (c)
token->value = ft_strdup_safe(minishell, c); token->value = ft_strdup_safe(msh, c);
else else
token->value = NULL; token->value = NULL;
return (token); return (token);

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/05 19:10:17 by qmennen #+# #+# */ /* Created: 2025/02/05 19:10:17 by qmennen #+# #+# */
/* Updated: 2025/02/25 14:45:08 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:15:19 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -56,7 +56,7 @@ static char *char_from_type(t_token_type type)
return (NULL); return (NULL);
} }
t_token *token_parse(t_minishell *minishell, t_lexer *lexer) t_token *token_parse(t_minishell *msh, t_lexer *lexer)
{ {
int is_double; int is_double;
char c; char c;
@ -66,7 +66,7 @@ t_token *token_parse(t_minishell *minishell, t_lexer *lexer)
c = lexer->current_char; c = lexer->current_char;
is_double = lexer->input[lexer->pos + 1] == c; is_double = lexer->input[lexer->pos + 1] == c;
type = token_from_char(c, is_double); type = token_from_char(c, is_double);
token = token_new(minishell, type, char_from_type(type), lexer->pos); token = token_new(msh, type, char_from_type(type), lexer->pos);
if (is_double) if (is_double)
lexer_readchar(lexer); lexer_readchar(lexer);
lexer_readchar(lexer); lexer_readchar(lexer);

View File

@ -12,14 +12,14 @@
#include "minishell.h" #include "minishell.h"
void check_malloc(t_minishell *minishell, void *ptr) void check_malloc(t_minishell *msh, void *ptr)
{ {
t_list *new; t_list *new;
if (ptr == NULL) if (ptr == NULL)
{ {
error_msg("malloc", "can't allocate memory"); error_msg("malloc", "can't allocate memory");
ft_lstclear(&(minishell->freelist), free); ft_lstclear(&(msh->freelist), free);
exit(1); exit(1);
} }
else else
@ -28,10 +28,10 @@ void check_malloc(t_minishell *minishell, void *ptr)
if (new == NULL) if (new == NULL)
{ {
error_msg("malloc", "can't allocate memory"); error_msg("malloc", "can't allocate memory");
ft_lstclear(&(minishell->freelist), free); ft_lstclear(&(msh->freelist), free);
exit(1); exit(1);
} }
else else
ft_lstadd_front(&(minishell->freelist), new); ft_lstadd_front(&(msh->freelist), new);
} }
} }

View File

@ -15,10 +15,10 @@
void error_msg(char *func, char *msg) void error_msg(char *func, char *msg)
{ {
if (errno) if (errno)
perror(RED BOLD "minishell" RESET); perror(RED BOLD "msh" RESET);
else else
{ {
ft_putstr_fd(RED BOLD "minishell" RESET ": ", 2); ft_putstr_fd(RED BOLD "msh" RESET ": ", 2);
if (func != NULL) if (func != NULL)
{ {
ft_putstr_fd(func, 2); ft_putstr_fd(func, 2);

View File

@ -12,20 +12,20 @@
#include "minishell.h" #include "minishell.h"
static void free_args(t_minishell *minishell, char **args) static void free_args(t_minishell *msh, char **args)
{ {
int i; int i;
i = 0; i = 0;
while (args[i]) while (args[i])
{ {
free_safe(minishell, (void **)&args[i]); free_safe(msh, (void **)&args[i]);
i++; i++;
} }
free_safe(minishell, (void **)&args); free_safe(msh, (void **)&args);
} }
static void free_redirects(t_minishell *minishell, t_list *lst) static void free_redirects(t_minishell *msh, t_list *lst)
{ {
t_redirect *redir; t_redirect *redir;
t_list *current; t_list *current;
@ -37,26 +37,26 @@ static void free_redirects(t_minishell *minishell, t_list *lst)
last = current; last = current;
redir = (t_redirect *)current->content; redir = (t_redirect *)current->content;
if (redir && redir->value) if (redir && redir->value)
free_safe(minishell, (void **)&redir->value); free_safe(msh, (void **)&redir->value);
if (redir) if (redir)
free_safe(minishell, (void **)&redir); free_safe(msh, (void **)&redir);
current = current->next; current = current->next;
free_safe(minishell, (void **)&last); free_safe(msh, (void **)&last);
} }
} }
void free_command_list(t_minishell *minishell, void *content) void free_command_list(t_minishell *msh, void *content)
{ {
t_command *command; t_command *command;
command = (t_command *)content; command = (t_command *)content;
if (command->command) if (command->command)
free_safe(minishell, (void **)&(command->command)); free_safe(msh, (void **)&(command->command));
if (command->args) if (command->args)
free_args(minishell, command->args); free_args(msh, command->args);
if (command->redirect_in) if (command->redirect_in)
free_redirects(minishell, command->redirect_in); free_redirects(msh, command->redirect_in);
if (command->redirect_out) if (command->redirect_out)
free_redirects(minishell, command->redirect_out); free_redirects(msh, command->redirect_out);
free_safe(minishell, (void **)&command); free_safe(msh, (void **)&command);
} }

View File

@ -6,18 +6,18 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/25 13:23:30 by whaffman #+# #+# */ /* Created: 2025/02/25 13:23:30 by whaffman #+# #+# */
/* Updated: 2025/02/25 15:56:40 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:15:29 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void free_freelist(t_minishell *minishell) void free_freelist(t_minishell *msh)
{ {
t_list *freelist; t_list *freelist;
t_list *next; t_list *next;
freelist = minishell->freelist; freelist = msh->freelist;
while (freelist) while (freelist)
{ {
next = freelist->next; next = freelist->next;
@ -25,5 +25,5 @@ void free_freelist(t_minishell *minishell)
free(freelist); free(freelist);
freelist = next; freelist = next;
} }
minishell->freelist = NULL; msh->freelist = NULL;
} }

View File

@ -6,15 +6,15 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/05 19:07:01 by qmennen #+# #+# */ /* Created: 2025/02/05 19:07:01 by qmennen #+# #+# */
/* Updated: 2025/02/25 16:16:01 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:15:31 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void ft_lexer_free(t_minishell *minishell, t_lexer *lexer) void ft_lexer_free(t_minishell *msh, t_lexer *lexer)
{ {
if (lexer->input) if (lexer->input)
free_safe(minishell, (void **)&(lexer->input)); free_safe(msh, (void **)&(lexer->input));
free_safe(minishell, (void **)&lexer); free_safe(msh, (void **)&lexer);
} }

View File

@ -6,15 +6,15 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/05 16:03:57 by whaffman #+# #+# */ /* Created: 2025/02/05 16:03:57 by whaffman #+# #+# */
/* Updated: 2025/02/26 15:43:00 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:15:42 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void free_minishell(t_minishell **minishell) void free_minishell(t_minishell **msh)
{ {
free_freelist(*minishell); free_freelist(*msh);
free(*minishell); free(*msh);
*minishell = NULL; *msh = NULL;
} }

View File

@ -6,20 +6,20 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/05 16:01:44 by whaffman #+# #+# */ /* Created: 2025/02/05 16:01:44 by whaffman #+# #+# */
/* Updated: 2025/02/25 17:40:32 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:15:38 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void free_minishell_line(t_minishell *minishell) void free_minishell_line(t_minishell *msh)
{ {
if (minishell->line) if (msh->line)
free(minishell->line); free(msh->line);
if (minishell->lexer) if (msh->lexer)
ft_lexer_free(minishell, minishell->lexer); ft_lexer_free(msh, msh->lexer);
if (minishell->tokens) if (msh->tokens)
ft_lstclear_safe(minishell, &minishell->tokens, ft_clear_tokenlist); ft_lstclear_safe(msh, &msh->tokens, ft_clear_tokenlist);
if (minishell->commands) if (msh->commands)
ft_lstclear_safe(minishell, &minishell->commands, free_command_list); ft_lstclear_safe(msh, &msh->commands, free_command_list);
} }

View File

@ -6,20 +6,20 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/25 15:09:34 by whaffman #+# #+# */ /* Created: 2025/02/25 15:09:34 by whaffman #+# #+# */
/* Updated: 2025/02/26 15:43:08 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:15:46 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void free_safe(t_minishell *minishell, void **ptr) void free_safe(t_minishell *msh, void **ptr)
{ {
t_list *prev; t_list *prev;
t_list *current; t_list *current;
prev = NULL; prev = NULL;
current = minishell->freelist; current = msh->freelist;
if (*ptr) if (*ptr)
{ {
while (current) while (current)
@ -29,7 +29,7 @@ void free_safe(t_minishell *minishell, void **ptr)
if (prev) if (prev)
prev->next = current->next; prev->next = current->next;
else else
minishell->freelist = current->next; msh->freelist = current->next;
free(*ptr); free(*ptr);
free(current); free(current);
return ; return ;

View File

@ -6,15 +6,15 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/05 19:07:45 by qmennen #+# #+# */ /* Created: 2025/02/05 19:07:45 by qmennen #+# #+# */
/* Updated: 2025/02/25 16:17:52 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:16:01 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void ft_token_free(t_minishell *minishell, t_token *token) void ft_token_free(t_minishell *msh, t_token *token)
{ {
if (token->value) if (token->value)
free_safe(minishell, (void **)&token->value); free_safe(msh, (void **)&token->value);
free_safe(minishell, (void **)&token); free_safe(msh, (void **)&token);
} }

View File

@ -6,16 +6,16 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/05 19:08:14 by qmennen #+# #+# */ /* Created: 2025/02/05 19:08:14 by qmennen #+# #+# */
/* Updated: 2025/02/25 16:18:44 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:15:58 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void ft_clear_tokenlist(t_minishell *minishell, void *content) void ft_clear_tokenlist(t_minishell *msh, void *content)
{ {
t_token *token; t_token *token;
token = (t_token *)content; token = (t_token *)content;
ft_token_free(minishell, token); ft_token_free(msh, token);
} }

View File

@ -1,19 +1,19 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* :::::::: */
/* minishell_lstclear.c :+: :+: */ /* ft_lstclear_safe.c :+: :+: */
/* +:+ */ /* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/25 16:21:16 by whaffman #+# #+# */ /* Created: 2025/02/25 16:21:16 by whaffman #+# #+# */
/* Updated: 2025/02/25 16:48:43 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:16:21 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void ft_lstclear_safe(t_minishell *minishell, t_list **lst, void ft_lstclear_safe(t_minishell *msh, t_list **lst,
void (*del)(t_minishell *minishell, void *)) void (*del)(t_minishell *msh, void *))
{ {
t_list *current; t_list *current;
t_list *next; t_list *next;
@ -22,8 +22,8 @@ void ft_lstclear_safe(t_minishell *minishell, t_list **lst,
while (current) while (current)
{ {
next = current->next; next = current->next;
del(minishell, current->content); del(msh, current->content);
free_safe(minishell, (void **)&current); free_safe(msh, (void **)&current);
current = next; current = next;
} }
*lst = NULL; *lst = NULL;

View File

@ -1,22 +1,22 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* :::::::: */
/* minishell_lstnew.c :+: :+: */ /* ft_lstnew_safe.c :+: :+: */
/* +:+ */ /* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/25 16:47:09 by whaffman #+# #+# */ /* Created: 2025/02/25 16:47:09 by whaffman #+# #+# */
/* Updated: 2025/02/25 16:49:11 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:16:29 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
t_list *ft_lstnew_safe(t_minishell *minishell, void *content) t_list *ft_lstnew_safe(t_minishell *msh, void *content)
{ {
t_list *new; t_list *new;
new = malloc_safe(minishell, sizeof(t_list)); new = malloc_safe(msh, sizeof(t_list));
new->content = content; new->content = content;
new->next = NULL; new->next = NULL;
return (new); return (new);

View File

@ -12,11 +12,11 @@
#include "minishell.h" #include "minishell.h"
char *ft_strdup_safe(t_minishell *minishell, const char *str) char *ft_strdup_safe(t_minishell *msh, const char *str)
{ {
char *new_str; char *new_str;
new_str = ft_strdup(str); new_str = ft_strdup(str);
check_malloc(minishell, new_str); check_malloc(msh, new_str);
return (new_str); return (new_str);
} }

View File

@ -12,11 +12,11 @@
#include "minishell.h" #include "minishell.h"
char *ft_strjoin_safe(t_minishell *minishell, const char *s1, const char *s2) char *ft_strjoin_safe(t_minishell *msh, const char *s1, const char *s2)
{ {
char *new_str; char *new_str;
new_str = ft_strjoin(s1, s2); new_str = ft_strjoin(s1, s2);
check_malloc(minishell, new_str); check_malloc(msh, new_str);
return (new_str); return (new_str);
} }

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/05 16:03:03 by whaffman #+# #+# */ /* Created: 2025/02/05 16:03:03 by whaffman #+# #+# */
/* Updated: 2025/02/25 14:50:19 by whaffman ######## odam.nl */ /* Updated: 2025/02/26 16:16:37 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,19 +14,19 @@
t_minishell *init_minishell(void) t_minishell *init_minishell(void)
{ {
t_minishell *minishell; t_minishell *msh;
minishell = malloc(sizeof(t_minishell)); msh = malloc(sizeof(t_minishell));
if (!minishell) if (!msh)
{ {
perror("failed assigning minishell memory"); perror("failed assigning msh memory");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
minishell->environment = NULL; msh->environment = NULL;
minishell->line = NULL; msh->line = NULL;
minishell->lexer = NULL; msh->lexer = NULL;
minishell->tokens = NULL; msh->tokens = NULL;
minishell->commands = NULL; msh->commands = NULL;
minishell->freelist = NULL; msh->freelist = NULL;
return (minishell); return (msh);
} }

View File

@ -12,11 +12,11 @@
#include "minishell.h" #include "minishell.h"
void *malloc_safe(t_minishell *minishell, size_t size) void *malloc_safe(t_minishell *msh, size_t size)
{ {
void *ptr; void *ptr;
ptr = malloc(size); ptr = malloc(size);
check_malloc(minishell, ptr); check_malloc(msh, ptr);
return (ptr); return (ptr);
} }