diff --git a/.vscode/settings.json b/.vscode/settings.json index ada2e55..29ba575 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,6 @@ { "files.associations": { "minishell.h": "c" - } + }, + "cmake.sourceDirectory": "/home/tchobert/GitHub_perso/Minishell/Unity" } diff --git a/Makefile b/Makefile index a0f7582..dbfe68e 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,10 @@ PATH_SRCS += srcs/history PATH_SRCS += srcs/syntax_analysis PATH_SRCS += srcs/lexical_analysis PATH_SRCS += srcs/syntax_analysis +PATH_SRCS += srcs/environment_management +PATH_SRCS += srcs/environment_building + +PATH_SRCS += srcs/functions_for_debug SRCS += main.c SRCS += exit_shell_routine.c @@ -29,14 +33,12 @@ SRCS += add_history.c # srcs/lexing -SRCS += lexical_analysis.c +SRCS += lexe_input.c SRCS += tokenizer.c SRCS += add_token_to_token_list.c SRCS += create_token.c SRCS += delete_token.c SRCS += delete_token_list.c -# SRCS += print_token.c -# SRCS += print_token_list.c SRCS += tokenize_operator.c SRCS += tokenize_word.c @@ -45,9 +47,32 @@ SRCS += lexer_utils.c # srcs/parsing -SRCS += syntax_analysis.c SRCS += parser.c +# srcs/environment_management + +SRCS += get_environment.c +SRCS += set_variable.c +SRCS += set_variable_from_keyvalue.c +SRCS += create_variable.c +SRCS += update_variable.c +SRCS += add_variable_to_environment.c +SRCS += delete_variable.c +SRCS += delete_variables_list.c +SRCS += find_variable_from_key.c +SRCS += get_variable_key.c +SRCS += get_variable_value.c + +# srcs/environment_building + +SRCS += build_environment.c + +# print_functions to use for debug and tests + +# SRCS += print_env.c +# SRCS += print_token.c +# SRCS += print_token_list.c + vpath %.c $(PATH_SRCS) ### TETS SRCS ################################################################ @@ -60,6 +85,10 @@ TESTS_SRCS_DIR += ./tests/tests_syntax_analysis TESTS_SRCS_DIR += ./tests/tests_lexical_analysis/BDD TESTS_SRCS_DIR += ./tests/tests_lexical_analysis/unit_tests TESTS_SRCS_DIR += ./tests/tests_syntax_analysis/BDD +TESTS_SRCS_DIR += ./tests/tests_environment_management +TESTS_SRCS_DIR += ./tests/tests_environment_management/unit_tests +TESTS_SRCS_DIR += ./tests/tests_environment_building +TESTS_SRCS_DIR += ./tests/tests_environment_building/unit_tests TESTS_SRCS += tests_main.c @@ -87,6 +116,19 @@ TESTS_SRCS += test_just_a_pipe.c TESTS_SRCS += test_two_pipes.c TESTS_SRCS += free_parser_test.c +# environment management + +TESTS_SRCS += unit_tests_environment.c +TESTS_SRCS += test_create_variable.c +TESTS_SRCS += test_add_variable_to_variables_list.c +TESTS_SRCS += delete_variables_list_tests_version.c +TESTS_SRCS += test_get_variable_key.c +TESTS_SRCS += test_get_variable_value.c + +# environemt building + +TESTS_SRCS += unit_tests_environment_building.c + # Unity UNITY_SRCS := Unity/src/unity.c diff --git a/Unity b/Unity index 73237c5..cbcd08f 160000 --- a/Unity +++ b/Unity @@ -1 +1 @@ -Subproject commit 73237c5d224169c7b4d2ec8321f9ac92e8071708 +Subproject commit cbcd08fa7de711053a3deec6339ee89cad5d2697 diff --git a/includes/environment.h b/includes/environment.h new file mode 100644 index 0000000..ca40e72 --- /dev/null +++ b/includes/environment.h @@ -0,0 +1,70 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* environment.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchobert +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/04 18:54:09 by tchobert #+# #+# */ +/* Updated: 2025/01/04 18:58:35 by tchobert ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ENVIRONMENT_H +# define ENVIRONMENT_H + +// INCLUDES + +# include "minishell.h" + +// DEFINES + +# define EQUAL_OPERATOR '=' +# define EXPORTABLE 1 + +// TYPEDEFS + +typedef t_list * t_variable_list; + +// ENUMS + +typedef enum e_status +{ + PROCESS_SUCCESS, + PROCESS_FAILURE +} t_status; + +// STRUCTURES + +typedef struct s_variable +{ + char *key; + char *value; + bool is_exportable; +} t_variable; + +// PROTOTYPES + +t_variable_list *get_environment(void); +t_status build_environment(char **variables); +t_status set_variable_from_keyvalue(const char *keyvalue, + bool make_it_exportable); +t_status set_variable(const char *key, const char *value, + bool make_it_exportable); +t_variable *create_variable(const char *key, const char *value, + bool is_exportable); +t_status update_variable(t_variable *variable, const char *value, + const bool is_exportable); +t_variable *find_variable_from_key(const t_variable_list *environment, + char *key); +bool is_variable_key_equal(void *variable_as_content, void *key); +t_status add_variable_to_environment(t_variable_list *environment, + t_variable *variable); +void delete_variable(void *data); +void delete_variables_list(void); +char *get_variable_key(const char *keyvalue); +char *get_variable_value(const char *keyvalue); + +void print_env(void); + +#endif \ No newline at end of file diff --git a/includes/minishell.h b/includes/minishell.h index c5d18fd..cd97cb1 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -20,6 +20,7 @@ # include "user_interface.h" # include "lexing.h" # include "parsing.h" +# include "environment.h" # include # include @@ -30,17 +31,22 @@ // STRUCTURES -typedef struct s_minishell +typedef struct s_command_session { char *user_input_line; t_token_list tokenized_user_input_line; -} t_minishell; +} t_command_session; + +typedef struct s_minishell_context +{ + t_command_session command_session; +} t_minishell_context; // PROTOTYPES int exit_shell_routine(void); char *prompt_gets_user_input(void); -t_lexing_status lexical_analysis(t_minishell *minishell_data); -t_syntax_status syntaxic_analysis(t_token_list token_list); +t_lexing_status lexe_input(t_command_session *current_command); +t_syntax_status parse_input(t_token_list token_list); #endif diff --git a/includes/parsing.h b/includes/parsing.h index 8c1b6e6..ddb32dd 100644 --- a/includes/parsing.h +++ b/includes/parsing.h @@ -17,10 +17,6 @@ # include "minishell.h" -// DEFINES - -# define POSSIBILITES_END -1 - // ENUMS typedef enum e_syntax_status @@ -30,8 +26,4 @@ typedef enum e_syntax_status INVALID_SYNTAX } t_syntax_status; -// PROTOTYPES - -t_syntax_status parser(t_token_list token_list); - #endif diff --git a/includes/tests.h b/includes/tests.h index ec92946..34a78ea 100644 --- a/includes/tests.h +++ b/includes/tests.h @@ -45,4 +45,15 @@ void test_input_just_a_pipe(void); void test_input_two_pipes(void); void test_free(void); -#endif +// ENVIRONMENT MANAGEMENT + +void unit_tests_environment_management(void); +void test_create_variable(void); +void test_add_variable_to_environment(void); +void delete_variables_list_tests_version(t_variable_list *variable_list); +void test_get_variable_key(void); +void test_get_variable_value(void); + +// ENVIRONMENT BUILDING + +#endif \ No newline at end of file diff --git a/libft b/libft index 910aff0..dd43fd8 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 910aff0e5c7fd4038140cc35dc58569b1755cf30 +Subproject commit dd43fd897fc816cba34108a9a2699031acbffd53 diff --git a/readline.supp b/readline.supp new file mode 100644 index 0000000..6eff1fd --- /dev/null +++ b/readline.supp @@ -0,0 +1,6 @@ +{ + ignore_libreadline_leaks + Memcheck:Leak + ... + obj:*/libreadline.so.* +} \ No newline at end of file diff --git a/srcs/environment_building/build_environment.c b/srcs/environment_building/build_environment.c new file mode 100644 index 0000000..bc1bfe0 --- /dev/null +++ b/srcs/environment_building/build_environment.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* build_environment.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchobert +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/09 16:26:17 by tchobert #+# #+# */ +/* Updated: 2025/01/09 16:26:28 by tchobert ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +t_status build_environment(char **variables) +{ + size_t i; + + i = 0; + while (variables[i] != NULL) + { + if (set_variable_from_keyvalue(variables[i], EXPORTABLE) + == PROCESS_FAILURE) + return (PROCESS_FAILURE); + ++i; + } + return (PROCESS_SUCCESS); +} diff --git a/srcs/environment_management/add_variable_to_environment.c b/srcs/environment_management/add_variable_to_environment.c new file mode 100644 index 0000000..62266dc --- /dev/null +++ b/srcs/environment_management/add_variable_to_environment.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* add_variable_to_variables_list.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchobert +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/06 21:48:23 by tchobert #+# #+# */ +/* Updated: 2025/01/06 21:48:51 by tchobert ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +t_status add_variable_to_environment(t_variable_list *environment, + t_variable *variable) +{ + t_variable_list new_node; + + new_node = ft_lstnew(variable); + if (new_node == NULL) + return (PROCESS_FAILURE); + ft_lstadd_back(environment, new_node); + return (PROCESS_SUCCESS); +} diff --git a/srcs/environment_management/create_variable.c b/srcs/environment_management/create_variable.c new file mode 100644 index 0000000..069ee16 --- /dev/null +++ b/srcs/environment_management/create_variable.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* create_variable.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchobert +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/06 21:46:37 by tchobert #+# #+# */ +/* Updated: 2025/01/06 21:46:48 by tchobert ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +t_variable *create_variable(const char *key, const char *value, + bool is_exportable) +{ + t_variable *new_variable; + + new_variable = (t_variable *)ft_calloc(1, sizeof(t_variable)); + if (new_variable == NULL) + return (NULL); + new_variable->key = ft_strdup(key); + if (new_variable->key == NULL + || update_variable(new_variable, value, is_exportable) + == PROCESS_FAILURE) + { + delete_variable(new_variable); + return (NULL); + } + return (new_variable); +} diff --git a/srcs/environment_management/delete_variable.c b/srcs/environment_management/delete_variable.c new file mode 100644 index 0000000..56b671b --- /dev/null +++ b/srcs/environment_management/delete_variable.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* delete_variable.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchobert +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/06 21:04:28 by tchobert #+# #+# */ +/* Updated: 2025/01/06 21:05:22 by tchobert ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void delete_variable(void *data) +{ + t_variable *variable_to_delete; + + variable_to_delete = (t_variable *)data; + free(variable_to_delete->key); + if (variable_to_delete->value != NULL) + free(variable_to_delete->value); + free(variable_to_delete); +} diff --git a/srcs/syntax_analysis/syntax_analysis.c b/srcs/environment_management/delete_variables_list.c similarity index 67% rename from srcs/syntax_analysis/syntax_analysis.c rename to srcs/environment_management/delete_variables_list.c index f656348..d375ca8 100644 --- a/srcs/syntax_analysis/syntax_analysis.c +++ b/srcs/environment_management/delete_variables_list.c @@ -1,21 +1,21 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* syntax_analysis.c :+: :+: :+: */ +/* delete_variables_list.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: tchobert +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2024/12/30 16:43:13 by tchobert #+# #+# */ -/* Updated: 2024/12/30 16:43:24 by tchobert ### ########.fr */ +/* Created: 2025/01/07 14:13:55 by tchobert #+# #+# */ +/* Updated: 2025/01/07 14:14:08 by tchobert ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -t_syntax_status syntaxic_analysis(const t_token_list token_list) +void delete_variables_list(void) { - t_syntax_status parser_output; + t_variable_list *env; - parser_output = parser(token_list); - return (parser_output); + env = get_environment(); + ft_lstclear(env, delete_variable); } diff --git a/srcs/environment_management/find_variable_from_key.c b/srcs/environment_management/find_variable_from_key.c new file mode 100644 index 0000000..d00e347 --- /dev/null +++ b/srcs/environment_management/find_variable_from_key.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* find_variable_from_key.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchobert +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/08 18:45:16 by tchobert #+# #+# */ +/* Updated: 2025/01/08 18:45:31 by tchobert ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +bool is_variable_key_equal(void *variable_as_content, void *key) +{ + const t_variable *variable = (t_variable *)variable_as_content; + + if (variable == NULL || variable->key == NULL || key == NULL) + return (false); + return (ft_strcmp(variable->key, key) == 0); +} + +t_variable *find_variable_from_key(const t_variable_list *environment, + char *key) +{ + return (ft_list_find_first_content_match((t_variable_list)(*environment), + key, is_variable_key_equal)); +} diff --git a/srcs/environment_management/get_environment.c b/srcs/environment_management/get_environment.c new file mode 100644 index 0000000..adcd36c --- /dev/null +++ b/srcs/environment_management/get_environment.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_environment.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchobert +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/07 20:39:36 by tchobert #+# #+# */ +/* Updated: 2025/01/07 20:39:48 by tchobert ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +t_variable_list *get_environment(void) +{ + static t_variable_list environment = NULL; + + return (&environment); +} diff --git a/srcs/environment_management/get_variable_key.c b/srcs/environment_management/get_variable_key.c new file mode 100644 index 0000000..1dd8434 --- /dev/null +++ b/srcs/environment_management/get_variable_key.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_variable_key.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchobert +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/09 17:27:49 by tchobert #+# #+# */ +/* Updated: 2025/01/09 17:28:01 by tchobert ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +char *get_variable_key(const char *keyvalue) +{ + char *key; + size_t i; + + i = 0; + while (keyvalue[i] != EQUAL_OPERATOR) + { + ++i; + } + key = ft_strndup(keyvalue, (i)); + return (key); +} diff --git a/srcs/environment_management/get_variable_value.c b/srcs/environment_management/get_variable_value.c new file mode 100644 index 0000000..4192969 --- /dev/null +++ b/srcs/environment_management/get_variable_value.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_variable_value.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchobert +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/09 17:34:36 by tchobert #+# #+# */ +/* Updated: 2025/01/09 17:34:52 by tchobert ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +char *get_variable_value(const char *keyvalue) +{ + char *value; + size_t i; + + i = 0; + while (keyvalue[i] != EQUAL_OPERATOR) + { + ++i; + } + value = ft_strdup(keyvalue + (i + 1)); + return (value); +} diff --git a/srcs/environment_management/set_variable.c b/srcs/environment_management/set_variable.c new file mode 100644 index 0000000..4818860 --- /dev/null +++ b/srcs/environment_management/set_variable.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* set_variable.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchobert +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/04 20:49:55 by tchobert #+# #+# */ +/* Updated: 2025/01/04 20:50:06 by tchobert ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +t_status set_variable(const char *key, const char *value, + bool make_it_exportable) +{ + const bool is_exportable = make_it_exportable == true; + t_variable_list *environment; + t_variable *variable; + char *key_to_find; + + environment = get_environment(); + key_to_find = (char *)key; + variable = find_variable_from_key(environment, key_to_find); + if (variable != NULL) + { + return (update_variable(variable, value, is_exportable)); + } + else + { + variable = create_variable(key, value, is_exportable); + return (add_variable_to_environment(environment, variable)); + } +} diff --git a/srcs/environment_management/set_variable_from_keyvalue.c b/srcs/environment_management/set_variable_from_keyvalue.c new file mode 100644 index 0000000..7dde94a --- /dev/null +++ b/srcs/environment_management/set_variable_from_keyvalue.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* set_variable_from_keyvalue.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchobert +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/07 19:14:42 by tchobert #+# #+# */ +/* Updated: 2025/01/07 19:14:53 by tchobert ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +t_status set_variable_from_keyvalue(const char *keyvalue, + bool make_it_exportable) +{ + t_status process_status; + char *key; + char *value; + + key = get_variable_key(keyvalue); + if (key == NULL) + return (process_status = PROCESS_FAILURE); + value = get_variable_value(keyvalue); + if (value == NULL) + { + free(key); + return (process_status = PROCESS_FAILURE); + } + process_status = set_variable(key, value, make_it_exportable); + free(key); + free(value); + return (process_status); +} diff --git a/srcs/environment_management/update_variable.c b/srcs/environment_management/update_variable.c new file mode 100644 index 0000000..68343cc --- /dev/null +++ b/srcs/environment_management/update_variable.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* update_variable.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchobert +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/08 13:45:48 by tchobert #+# #+# */ +/* Updated: 2025/01/08 13:46:03 by tchobert ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +t_status update_variable(t_variable *variable, const char *value, + const bool is_exportable) +{ + variable->is_exportable = is_exportable; + if (value != NULL) + { + free(variable->value); + variable->value = ft_strdup(value); + if (variable->value == NULL) + return (PROCESS_FAILURE); + } + return (PROCESS_SUCCESS); +} diff --git a/srcs/exit_shell_routine.c b/srcs/exit_shell_routine.c index f81dffb..6a2f8af 100644 --- a/srcs/exit_shell_routine.c +++ b/srcs/exit_shell_routine.c @@ -14,6 +14,7 @@ int exit_shell_routine(void) { + delete_variables_list(); printf("exit\n"); exit (EXIT_SUCCESS); } diff --git a/srcs/functions_for_debug/print_env.c b/srcs/functions_for_debug/print_env.c new file mode 100644 index 0000000..cbc86c6 --- /dev/null +++ b/srcs/functions_for_debug/print_env.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* print_env.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchobert +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/09 19:36:34 by tchobert #+# #+# */ +/* Updated: 2025/01/09 19:36:49 by tchobert ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +static void print_variable(void *content) +{ + const t_variable *variable = (t_variable *)content; + + printf("%s", variable->key); + printf("="); + printf("%s\n", variable->value); +} + +void print_env(void) +{ + t_variable_list *env = get_environment(); + + ft_lstiter(*env, print_variable); +} diff --git a/tests/tests_lexical_analysis/BDD/print_token.c b/srcs/functions_for_debug/print_token.c similarity index 100% rename from tests/tests_lexical_analysis/BDD/print_token.c rename to srcs/functions_for_debug/print_token.c diff --git a/tests/tests_lexical_analysis/BDD/print_token_list.c b/srcs/functions_for_debug/print_token_list.c similarity index 100% rename from tests/tests_lexical_analysis/BDD/print_token_list.c rename to srcs/functions_for_debug/print_token_list.c diff --git a/srcs/lexical_analysis/lexical_analysis.c b/srcs/lexical_analysis/lexe_input.c similarity index 82% rename from srcs/lexical_analysis/lexical_analysis.c rename to srcs/lexical_analysis/lexe_input.c index e6b87a3..bf187b9 100644 --- a/srcs/lexical_analysis/lexical_analysis.c +++ b/srcs/lexical_analysis/lexe_input.c @@ -12,11 +12,11 @@ #include "minishell.h" -t_lexing_status lexical_analysis(t_minishell *minishell_data) +t_lexing_status lexe_input(t_command_session *current_command) { - minishell_data->tokenized_user_input_line - = tokenize(minishell_data->user_input_line); - if (minishell_data->tokenized_user_input_line == NULL) + current_command->tokenized_user_input_line + = tokenize(current_command->user_input_line); + if (current_command->tokenized_user_input_line == NULL) return (LEXING_FAILURE); return (LEXING_SUCCESS); } diff --git a/srcs/main.c b/srcs/main.c index 0f5f078..cd9e4cf 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -13,16 +13,20 @@ #include "minishell.h" #ifndef TEST_MODE -static int main_process(t_minishell *minishell_data) +static int main_process(t_minishell_context *minishell_context) { - if (minishell_data->user_input_line == CTRL_D) + if (minishell_context->command_session.user_input_line == CTRL_D) + { exit_shell_routine(); - if (lexical_analysis(minishell_data) == LEXING_FAILURE) + } + if (lexe_input(&minishell_context->command_session) + == LEXING_FAILURE) { ft_dprintf(STDERR_FILENO, "Memory allocation failure during lexing.\n"); return (EXIT_FAILURE); } - if (syntaxic_analysis(minishell_data->tokenized_user_input_line) + if (parse_input( + minishell_context->command_session.tokenized_user_input_line) == INVALID_SYNTAX) { return (EXIT_FAILURE); @@ -30,30 +34,41 @@ static int main_process(t_minishell *minishell_data) return (EXIT_SUCCESS); } -static int core_routine(t_minishell *minishell_data) +static int core_routine(t_minishell_context *minishell_context) { + (void)minishell_context; while (MSH_LOOP) { - minishell_data->user_input_line = prompt_gets_user_input(); - main_process(minishell_data); - free(minishell_data->user_input_line); - delete_token_list(minishell_data->tokenized_user_input_line); + minishell_context->command_session.user_input_line + = prompt_gets_user_input(); + main_process(minishell_context); + free(minishell_context->command_session.user_input_line); + delete_token_list( + minishell_context->command_session.tokenized_user_input_line); } return (EXIT_SUCCESS); } -static int launch_shell(void) +static int launch_shell(char **env) { - t_minishell minishell_data; + t_minishell_context minishell_context; struct sigaction sa; - ft_bzero(&minishell_data, sizeof(minishell_data)); + ft_bzero(&minishell_context, sizeof(minishell_context)); + if (build_environment(env) == PROCESS_FAILURE) + { + ft_dprintf(STDERR_FILENO, "Fatal error while initializing minishell's" + " environment\n"); + exit(EXIT_FAILURE); + } setup_signals(&sa); - return (core_routine(&minishell_data)); + return (core_routine(&minishell_context)); } -int main(void) +int main(int ac, char **av, char **env) { - return (launch_shell()); + (void)ac; + (void)av; + return (launch_shell(env)); } -#endif +#endif \ No newline at end of file diff --git a/srcs/syntax_analysis/parser.c b/srcs/syntax_analysis/parser.c index 7159a6f..cc79527 100644 --- a/srcs/syntax_analysis/parser.c +++ b/srcs/syntax_analysis/parser.c @@ -85,7 +85,7 @@ static t_syntax_status check_tokens_sequence(t_token_list token) return (PARSING_IN_PROGRESS); } -t_syntax_status parser(t_token_list token_list) +t_syntax_status parse_input(t_token_list token_list) { t_token_list current_token; t_syntax_status parser_output; @@ -99,6 +99,10 @@ t_syntax_status parser(t_token_list token_list) current_token = current_token->next; } if (parser_output == INVALID_SYNTAX) + { display_syntax_error(current_token->next); + set_variable_from_keyvalue("?=2", false); + } + set_variable_from_keyvalue("?=0", false); return (parser_output); } diff --git a/test_minishell.sh b/test_minishell.sh new file mode 100644 index 0000000..f49467e --- /dev/null +++ b/test_minishell.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +dir=$(pwd) + +valgrind --leak-check=full --track-origins=yes --trace-children=yes --track-fds=yes --show-leak-kinds=all --suppressions=$dir/readline.supp ./minishell \ No newline at end of file diff --git a/tests/tests_environment_building/unit_tests/unit_tests_environment_building.c b/tests/tests_environment_building/unit_tests/unit_tests_environment_building.c new file mode 100644 index 0000000..beca864 --- /dev/null +++ b/tests/tests_environment_building/unit_tests/unit_tests_environment_building.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* unit_tests_environment_building.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchobert +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/09 17:24:36 by tchobert #+# #+# */ +/* Updated: 2025/01/09 17:24:44 by tchobert ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "tests.h" + +// void unit_tests_environment_building(void) +// { +// printf("Testing function \"build_environment\"\n\n"); + +// // ARRANGE + +// char env_test[][10] ={ +// {"VAR1=first"}, +// {"VAR2=second"}, +// {"VAR3=third"}, +// {"VAR4=fourth"}, +// {"VAR5=fifth"}, +// {"VAR6=sixth"}, +// {"VAR7=seventh"}, +// {"VAR8=eight"}, +// }; + +// // ACT + + + +// printf("\n"); +// } diff --git a/tests/tests_environment_management/unit_tests/delete_variables_list_tests_version.c b/tests/tests_environment_management/unit_tests/delete_variables_list_tests_version.c new file mode 100644 index 0000000..521a0bc --- /dev/null +++ b/tests/tests_environment_management/unit_tests/delete_variables_list_tests_version.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* delete_variables_list_tests_version.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchobert +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/08 19:09:27 by tchobert #+# #+# */ +/* Updated: 2025/01/08 19:09:39 by tchobert ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "tests.h" + +void delete_variables_list_tests_version(t_variable_list *variable_list) +{ + ft_lstclear(variable_list, delete_variable); +} \ No newline at end of file diff --git a/tests/tests_environment_management/unit_tests/test_add_variable_to_variables_list.c b/tests/tests_environment_management/unit_tests/test_add_variable_to_variables_list.c new file mode 100644 index 0000000..e48f08e --- /dev/null +++ b/tests/tests_environment_management/unit_tests/test_add_variable_to_variables_list.c @@ -0,0 +1,68 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_set_variable.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchobert +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/07 18:02:18 by tchobert #+# #+# */ +/* Updated: 2025/01/07 18:02:33 by tchobert ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "tests.h" + +void test_add_variable_to_environment(void) +{ + printf("Testing function \"add_variable_to_variable_list\"\n\n"); + + //ARRANGE + + char *key = "KEY"; + char *value = "VALUE"; + char *key_2 = "KEY_2"; + char *value_2 = "VALUE_2"; + t_variable_list variables_list = NULL; + + t_variable variable = {.key = "KEY", .value = "VALUE", .is_exportable = true}; + t_variable variable_2 = {.key = "KEY_2", .value = "VALUE_2", .is_exportable = false}; + + //ACT + + add_variable_to_environment(&variables_list, &variable); + add_variable_to_environment(&variables_list, &variable_2); + + //ASSERT + + TEST_ASSERT_EQUAL(0, ft_strcmp(((t_variable *)variables_list->content)->key, key)); + TEST_ASSERT_EQUAL(0, ft_strcmp(((t_variable *)variables_list->content)->value, value)); + TEST_ASSERT_EQUAL(true, ((t_variable *)variables_list->content)->is_exportable); + + printf("Variable_list first node key = %s\n", ((t_variable *)variables_list->content)->key); + printf("Variable list first node value = %s\n\n", ((t_variable *)variables_list->content)->value); + + t_variable_list second_node = (t_variable_list)variables_list->next; + + TEST_ASSERT_EQUAL(0, ft_strcmp(((t_variable *)second_node->content)->key, key_2)); + TEST_ASSERT_EQUAL(0, ft_strcmp(((t_variable *)second_node->content)->value, value_2)); + TEST_ASSERT_EQUAL(false, ((t_variable *)second_node->content)->is_exportable); + + printf("Variable_list second node key = %s\n", ((t_variable *)second_node->content)->key); + printf("Variable list second node value = %s\n\n", ((t_variable *)second_node->content)->value); + + printf("Testing function \"find_variable_from_key\"\n\n"); + + t_variable *search_result; + + search_result = find_variable_from_key(&variables_list, key_2); + + TEST_ASSERT_EQUAL(0, ft_strcmp(search_result->key, key_2)); + printf("Result found key = %s\n", search_result->key); + + //CLEAN + + free(variables_list); + free(second_node); + //delete_variables_list_tests_version(&variables_list); + printf("\n"); +} \ No newline at end of file diff --git a/tests/tests_environment_management/unit_tests/test_create_variable.c b/tests/tests_environment_management/unit_tests/test_create_variable.c new file mode 100644 index 0000000..713998b --- /dev/null +++ b/tests/tests_environment_management/unit_tests/test_create_variable.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_set_variable.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchobert +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/04 19:38:36 by tchobert #+# #+# */ +/* Updated: 2025/01/04 19:38:45 by tchobert ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "tests.h" + +void test_create_variable(void) +{ + printf("Testing function \"create_variable\"\n"); + + // ARRANGE + + const char *key = "VAR_NAME"; + const char *value = "test"; + const char *key_2 = "NAME_VAR"; + const char *value_2 = ""; + + // ACT + + t_variable *variable = create_variable(key, value, true); + t_variable *variable_2 = create_variable(key_2, value_2, false); + + // ASSERT + + TEST_ASSERT_EQUAL(0, ft_strncmp(key, variable->key, ft_strlen(key))); + printf("Variable key = %s\n", variable->key); + TEST_ASSERT_EQUAL(0, ft_strncmp(value, variable->value, ft_strlen(value))); + printf("Variable value = %s\n", variable->value); + TEST_ASSERT_EQUAL(true, variable->is_exportable); + + TEST_ASSERT_EQUAL(0,ft_strncmp(key_2, variable_2->key, ft_strlen(key_2))); + printf("Variable 2 key = %s\n", variable_2->key); + TEST_ASSERT_EQUAL(0, *variable_2->value); + printf("Variable 2 value = %s\n", variable_2->value); + TEST_ASSERT_EQUAL(false, variable_2->is_exportable); + + // CLEAN + + delete_variable(variable); + delete_variable(variable_2); + + printf("\n"); +} \ No newline at end of file diff --git a/tests/tests_environment_management/unit_tests/test_get_variable_key.c b/tests/tests_environment_management/unit_tests/test_get_variable_key.c new file mode 100644 index 0000000..48e5cd2 --- /dev/null +++ b/tests/tests_environment_management/unit_tests/test_get_variable_key.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_get_variable_key.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchobert +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/09 17:16:21 by tchobert #+# #+# */ +/* Updated: 2025/01/09 17:16:27 by tchobert ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "tests.h" + +void test_get_variable_key(void) +{ + printf("Testing function \"get_variable_key\"\n\n"); + + //ARRANGE + + const char *variable_keyvalue = "TEST=test"; + const char *expected_key = "TEST"; + char *key; + + // ACT + + key = get_variable_key(variable_keyvalue); + + // ASSERT + + TEST_ASSERT_EQUAL(0, ft_strcmp(key, expected_key)); + printf("Extracted key = %s\n\n", key); + free(key); +} diff --git a/tests/tests_environment_management/unit_tests/test_get_variable_value.c b/tests/tests_environment_management/unit_tests/test_get_variable_value.c new file mode 100644 index 0000000..63e405f --- /dev/null +++ b/tests/tests_environment_management/unit_tests/test_get_variable_value.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_get_variable_value.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchobert +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/09 17:55:28 by tchobert #+# #+# */ +/* Updated: 2025/01/09 17:55:38 by tchobert ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "tests.h" + +void test_get_variable_value(void) +{ + printf("Testing function \"get_variable_value\"\n\n"); + + //ARRANGE + + const char *variable_keyvalue = "TEST=test"; + const char *expected_value = "test"; + char *value; + + // ACT + + value = get_variable_value(variable_keyvalue); + + // ASSERT + + TEST_ASSERT_EQUAL(0, ft_strcmp(value, expected_value)); + printf("Extracted value = %s\n\n", value); + free(value); +} \ No newline at end of file diff --git a/tests/tests_environment_management/unit_tests/test_set_variable_from_keyvalue.c b/tests/tests_environment_management/unit_tests/test_set_variable_from_keyvalue.c new file mode 100644 index 0000000..7137f1b --- /dev/null +++ b/tests/tests_environment_management/unit_tests/test_set_variable_from_keyvalue.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_set_variable_from_keyvalue.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchobert +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/07 19:04:22 by tchobert #+# #+# */ +/* Updated: 2025/01/07 19:04:36 by tchobert ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "tests.h" + +void test_set_variable_from_keyvalue(void) +{ + //ARRANGE + + const char *keyvalue = "test=variable"; + const char *expected_key = "test"; + const char *expected_value = "variable"; + t_variable_list variables_list = NULL; + + //ACT + + set_variable_from_keyvalue(variables_list, keyvalue); + + //ASSERT + + TEST_ASSERT_EQUAL(0, ft_strncmp(((t_variable *)variables_list->content)->key, expected_key, ft_strlen(expected_key))); + TEST_ASSERT_EQUAL(0, ft_strncmp(((t_variable *)variables_list->content)->value, expected_value, ft_strlen(expected_value))); +} \ No newline at end of file diff --git a/tests/tests_environment_management/unit_tests/unit_tests_environment.c b/tests/tests_environment_management/unit_tests/unit_tests_environment.c new file mode 100644 index 0000000..787c333 --- /dev/null +++ b/tests/tests_environment_management/unit_tests/unit_tests_environment.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* unit_tests_environment.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchobert +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/04 19:58:30 by tchobert #+# #+# */ +/* Updated: 2025/01/04 19:58:40 by tchobert ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "tests.h" + +void unit_tests_environment_management(void) +{ + printf("\nTesting environment management -> unit tests\n\n"); + test_create_variable(); + test_add_variable_to_environment(); + test_get_variable_key(); + test_get_variable_value(); + printf("\n"); +} \ No newline at end of file diff --git a/tests/tests_main.c b/tests/tests_main.c index ee8445c..dbca38b 100644 --- a/tests/tests_main.c +++ b/tests/tests_main.c @@ -39,5 +39,9 @@ int main(void) printf("Testing minishell parser comportments\n\n"); RUN_TEST(parsing_bdd_tests); + // ENVIRONMENT MANAGEMENT + + RUN_TEST(unit_tests_environment_management); + return (UNITY_END()); } diff --git a/tests/tests_syntax_analysis/BDD/free_parser_test.c b/tests/tests_syntax_analysis/BDD/free_parser_test.c index 42e6183..05598a1 100644 --- a/tests/tests_syntax_analysis/BDD/free_parser_test.c +++ b/tests/tests_syntax_analysis/BDD/free_parser_test.c @@ -23,7 +23,6 @@ void test_free(void) scanf("%[^\n]", input); printf("\n"); - t_syntax_status parser_output; t_token_list token_list = tokenize(input); print_token_list(token_list); @@ -31,7 +30,7 @@ void test_free(void) printf("TOKEN LIST SIZE: %d\n\n", ft_lstsize(token_list)); //ACT - parser_output = parser(token_list); + parse_input(token_list); //print_token_list(token_list); //ASSERT diff --git a/tests/tests_syntax_analysis/BDD/test_empty_string.c b/tests/tests_syntax_analysis/BDD/test_empty_string.c index ea1a9e0..895908a 100644 --- a/tests/tests_syntax_analysis/BDD/test_empty_string.c +++ b/tests/tests_syntax_analysis/BDD/test_empty_string.c @@ -23,7 +23,7 @@ void test_input_empty_string(void) print_token_list(token_list); //ACT - parser_output = parser(token_list); + parser_output = parse_input(token_list); //print_token_list(token_list); //ASSERT diff --git a/tests/tests_syntax_analysis/BDD/test_just_a_pipe.c b/tests/tests_syntax_analysis/BDD/test_just_a_pipe.c index 7d526f4..ec346e5 100644 --- a/tests/tests_syntax_analysis/BDD/test_just_a_pipe.c +++ b/tests/tests_syntax_analysis/BDD/test_just_a_pipe.c @@ -23,7 +23,7 @@ void test_input_just_a_pipe(void) print_token_list(token_list); //ACT - parser_output = parser(token_list); + parser_output = parse_input(token_list); //print_token_list(token_list); //ASSERT diff --git a/tests/tests_syntax_analysis/BDD/test_two_pipes.c b/tests/tests_syntax_analysis/BDD/test_two_pipes.c index 6efd993..6bab1ce 100644 --- a/tests/tests_syntax_analysis/BDD/test_two_pipes.c +++ b/tests/tests_syntax_analysis/BDD/test_two_pipes.c @@ -23,7 +23,7 @@ void test_input_two_pipes(void) print_token_list(token_list); //ACT - parser_output = parser(token_list); + parser_output = parse_input(token_list); //print_token_list(token_list); //ASSERT