From 5c7b8cde0d6d78f78d6639459276a69eab14668b Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Sun, 14 Jul 2024 11:26:15 +0200 Subject: [PATCH] pushed --- ast/Makefile | 4 +- exec/Makefile | 4 +- exec/src/run_ast.c | 212 +++++++++++++++++++++++++++++++------------ includes/app/state.h | 6 +- includes/minishell.h | 6 +- sources/ft_cmd.c | 4 +- sources/ft_exit.c | 4 +- sources/main.c | 8 +- 8 files changed, 174 insertions(+), 74 deletions(-) diff --git a/ast/Makefile b/ast/Makefile index 1d75e015..76e5a1b7 100644 --- a/ast/Makefile +++ b/ast/Makefile @@ -6,13 +6,13 @@ # By: maiboyer +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/03 13:20:01 by maiboyer #+# #+# # -# Updated: 2024/07/03 22:39:28 by maiboyer ### ########.fr # +# Updated: 2024/07/14 10:18:27 by maiboyer ### ########.fr # # # # **************************************************************************** # BUILD_DIR ?= ../build SRC_DIR = src -INCLUDE_DIR = include output/include ../includes ../output/include/ +INCLUDE_DIR = include output/include ../includes ../output/include/ ../stdme/output/include/ LIBS_DIR = . GENERIC_DIR = output/src GENERIC_INCLUDE = output/include diff --git a/exec/Makefile b/exec/Makefile index 0bced878..2fddd2b2 100644 --- a/exec/Makefile +++ b/exec/Makefile @@ -6,13 +6,13 @@ # By: maiboyer +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/03 13:20:01 by maiboyer #+# #+# # -# Updated: 2024/07/11 17:13:13 by maiboyer ### ########.fr # +# Updated: 2024/07/14 10:18:49 by maiboyer ### ########.fr # # # # **************************************************************************** # BUILD_DIR ?= ../build SRC_DIR = src -INCLUDE_DIR = include/ output/include/ ../includes/ ../output/include/ +INCLUDE_DIR = include/ output/include/ ../includes/ ../output/include/ ../stdme/output/include/ LIBS_DIR = . GENERIC_DIR = output/src GENERIC_INCLUDE = output/include diff --git a/exec/src/run_ast.c b/exec/src/run_ast.c index c240817a..c3f46852 100644 --- a/exec/src/run_ast.c +++ b/exec/src/run_ast.c @@ -6,75 +6,175 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/11 17:22:29 by maiboyer #+# #+# */ -/* Updated: 2024/07/12 11:52:23 by maiboyer ### ########.fr */ +/* Updated: 2024/07/14 10:38:32 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ +#include "app/state.h" #include "ast/ast.h" #include "exec/run.h" +#include "me/str/str.h" #include "me/types.h" #include #pragma clang diagnostic ignored "-Wunused-parameter" -/* -t_ast_arithmetic_expansion arithmetic_expansion; -t_ast_case case_; -t_ast_case_item case_item; -t_ast_command command; -t_ast_command_substitution command_substitution; -t_ast_compound_statement compound_statement; -t_ast_elif elif; -t_ast_else else_; -t_ast_empty empty; -t_ast_expansion expansion; -t_ast_extglob extglob; -t_ast_file_redirection file_redirection; -t_ast_for for_; -t_ast_function_definition function_definition; -t_ast_heredoc_redirection heredoc_redirection; -t_ast_if if_; -t_ast_list list; -t_ast_pipeline pipeline; -t_ast_program program; -t_ast_raw_string raw_string; -t_ast_regex regex; -t_ast_subshell subshell; -t_ast_until until; -t_ast_variable_assignment variable_assignment; -t_ast_while while_; -t_ast_word word; -*/ - #define NOT_DONE \ { \ printf("function `%s` isn't done !\n", __func__); \ return (ERROR); \ } -t_error run_arithmetic_expansion(t_ast_arithmetic_expansion arithmetic_expansion, void *arg) NOT_DONE; -t_error run_case_(t_ast_case case_, void *arg) NOT_DONE; -t_error run_case_item(t_ast_case_item case_item, void *arg) NOT_DONE; -t_error run_command(t_ast_command command, void *arg) NOT_DONE; -t_error run_command_substitution(t_ast_command_substitution command_substitution, void *arg) NOT_DONE; -t_error run_compound_statement(t_ast_compound_statement compound_statement, void *arg) NOT_DONE; -t_error run_elif(t_ast_elif elif, void *arg) NOT_DONE; -t_error run_else_(t_ast_else else_, void *arg) NOT_DONE; -t_error run_empty(t_ast_empty empty, void *arg) NOT_DONE; -t_error run_expansion(t_ast_expansion expansion, void *arg) NOT_DONE; -t_error run_extglob(t_ast_extglob extglob, void *arg) NOT_DONE; -t_error run_file_redirection(t_ast_file_redirection file_redirection, void *arg) NOT_DONE; -t_error run_for_(t_ast_for for_, void *arg) NOT_DONE; -t_error run_function_definition(t_ast_function_definition function_definition, void *arg) NOT_DONE; -t_error run_heredoc_redirection(t_ast_heredoc_redirection heredoc_redirection, void *arg) NOT_DONE; -t_error run_if_(t_ast_if if_, void *arg) NOT_DONE; -t_error run_list(t_ast_list list, void *arg) NOT_DONE; -t_error run_pipeline(t_ast_pipeline pipeline, void *arg) NOT_DONE; -t_error run_program(t_ast_program program, void *arg) NOT_DONE; -t_error run_raw_string(t_ast_raw_string raw_string, void *arg) NOT_DONE; -t_error run_regex(t_ast_regex regex, void *arg) NOT_DONE; -t_error run_subshell(t_ast_subshell subshell, void *arg) NOT_DONE; -t_error run_until(t_ast_until until, void *arg) NOT_DONE; -t_error run_variable_assignment(t_ast_variable_assignment variable_assignment, void *arg) NOT_DONE; -t_error run_while_(t_ast_while while_, void *arg) NOT_DONE; -t_error run_word(t_ast_word word, void *arg) NOT_DONE; +// Start Internals funcs + +bool _is_special_var(t_ast_expansion *self) +{ + char name; + + if (self->var_name == NULL) + return (true); + if (str_len(self->var_name) != 1) + return (false); + name = self->var_name[0]; + if (name == '*' || name == '@' || name == '?' || name == '!' || name == '#' || name == '-' || name == '$' || name == '0' || name == '_') + return (true); + return (false); +} + +t_str *_run_expansion_special_var(t_ast_expansion *expansion, t_state *state, t_str *storage) +{ + char name; + name = expansion->var_name[0]; + + if (name == '*') + ; // return all args exept argv[0] + if (name == '@') + ; // return all args with argv[0] + if (name == '?') + ; // return exit code of last run program + if (name == '!') + ; // return pid of last run program + if (name == '#') + ; // return argc -1 bc we don't care about argv[0] + if (name == '$') + ; // return pid of self (the shell) + + return (NULL); +} + +// End Internals funcs + +t_error run_command(t_ast_command *command, t_state *state, void *out) NOT_DONE; + +t_error run_expansion(t_ast_expansion *expansion, t_state *state, t_str **out) NOT_DONE; +t_error run_arithmetic_expansion(t_ast_arithmetic_expansion *arithmetic_expansion, t_state *state, void *out) NOT_DONE; +t_error run_case_(t_ast_case *case_, t_state *state, void *out) NOT_DONE; +t_error run_case_item(t_ast_case_item *case_item, t_state *state, void *out) NOT_DONE; +t_error run_command_substitution(t_ast_command_substitution *command_substitution, t_state *state, void *out) NOT_DONE; +t_error run_compound_statement(t_ast_compound_statement *compound_statement, t_state *state, void *out) NOT_DONE; +t_error run_elif(t_ast_elif *elif, t_state *state, void *out) NOT_DONE; +t_error run_else_(t_ast_else *else_, t_state *state, void *out) NOT_DONE; +t_error run_empty(t_ast_empty *empty, t_state *state, void *out) NOT_DONE; +t_error run_extglob(t_ast_extglob *extglob, t_state *state, void *out) NOT_DONE; +t_error run_file_redirection(t_ast_file_redirection *file_redirection, t_state *state, void *out) NOT_DONE; +t_error run_for_(t_ast_for *for_, t_state *state, void *out) NOT_DONE; +t_error run_function_definition(t_ast_function_definition *function_definition, t_state *state, void *out) NOT_DONE; +t_error run_heredoc_redirection(t_ast_heredoc_redirection *heredoc_redirection, t_state *state, void *out) NOT_DONE; +t_error run_if_(t_ast_if *if_, t_state *state, void *out) NOT_DONE; +t_error run_list(t_ast_list *list, t_state *state, void *out) NOT_DONE; +t_error run_pipeline(t_ast_pipeline *pipeline, t_state *state, void *out) NOT_DONE; +t_error run_program(t_ast_program *program, t_state *state, void *out) NOT_DONE; +t_error run_raw_string(t_ast_raw_string *raw_string, t_state *state, void *out) NOT_DONE; +t_error run_regex(t_ast_regex *regex, t_state *state, void *out) NOT_DONE; +t_error run_subshell(t_ast_subshell *subshell, t_state *state, void *out) NOT_DONE; +t_error run_until(t_ast_until *until, t_state *state, void *out) NOT_DONE; +t_error run_variable_assignment(t_ast_variable_assignment *variable_assignment, t_state *state, void *out) NOT_DONE; +t_error run_while_(t_ast_while *while_, t_state *state, void *out) NOT_DONE; +t_error run_word(t_ast_word *word, t_state *state, void *out) NOT_DONE; + +// FUNCTIONS + +// t_error run_command(t_ast_command *command, t_state *state, void *out) {} + +/* +/// this functons returns different things depending on the operator and/or the state of the shell +/// NULL != empty string for example +t_error run_expansion(t_ast_expansion *expansion, t_state *state, t_str **out) +{ + t_str backing_storage; + t_str *env_var; + + if (_is_special_var(expansion)) + { + backing_storage = _run_expansion_special_var(expansion, state); + env_var = &backing_storage; + } + else + env_var = hmap_env_get(state->env, &expansion->var_name); + + return (ERROR); +} +*/ + +// FUNCTIONS + +/* + +t_error run_node(t_ast_node self, t_state *state, void *out) +{ + if (self->kind == AST_ARITHMETIC_EXPANSION) + return (run_arithmetic_expansion(&self->data.arithmetic_expansion, state, out)); + if (self->kind == AST_CASE) + return (run_case_(&self->data.case_, state, out)); + if (self->kind == AST_CASE_ITEM) + return (run_case_item(&self->data.case_item, state, out)); + if (self->kind == AST_COMMAND) + return (run_command(&self->data.command, state, out)); + if (self->kind == AST_COMMAND_SUBSTITUTION) + return (run_command_substitution(&self->data.command_substitution, state, out)); + if (self->kind == AST_COMPOUND_STATEMENT) + return (run_compound_statement(&self->data.compound_statement, state, out)); + if (self->kind == AST_ELIF) + return (run_elif(&self->data.elif, state, out)); + if (self->kind == AST_ELSE) + return (run_else_(&self->data.else_, state, out)); + if (self->kind == AST_EMPTY) + return (run_empty(&self->data.empty, state, out)); + if (self->kind == AST_EXPANSION) + return (run_expansion(&self->data.expansion, state, out)); + if (self->kind == AST_EXTGLOB) + return (run_extglob(&self->data.extglob, state, out)); + if (self->kind == AST_FILE_REDIRECTION) + return (run_file_redirection(&self->data.file_redirection, state, out)); + if (self->kind == AST_FOR) + return (run_for_(&self->data.for_, state, out)); + if (self->kind == AST_FUNCTION_DEFINITION) + return (run_function_definition(&self->data.function_definition, state, out)); + if (self->kind == AST_HEREDOC_REDIRECTION) + return (run_heredoc_redirection(&self->data.heredoc_redirection, state, out)); + if (self->kind == AST_IF) + return (run_if_(&self->data.if_, state, out)); + if (self->kind == AST_LIST) + return (run_list(&self->data.list, state, out)); + if (self->kind == AST_PIPELINE) + return (run_pipeline(&self->data.pipeline, state, out)); + if (self->kind == AST_PROGRAM) + return (run_program(&self->data.program, state, out)); + if (self->kind == AST_RAW_STRING) + return (run_raw_string(&self->data.raw_string, state, out)); + if (self->kind == AST_REGEX) + return (run_regex(&self->data.regex, state, out)); + if (self->kind == AST_SUBSHELL) + return (run_subshell(&self->data.subshell, state, out)); + if (self->kind == AST_UNTIL) + return (run_until(&self->data.until, state, out)); + if (self->kind == AST_VARIABLE_ASSIGNMENT) + return (run_variable_assignment(&self->data.variable_assignment, state, out)); + if (self->kind == AST_WHILE) + return (run_while_(&self->data.while_, state, out)); + if (self->kind == AST_WORD) + return (run_word(&self->data.word, state, out)); + return (ERROR); +} + +*/ diff --git a/includes/app/state.h b/includes/app/state.h index df65a30f..54cfe795 100644 --- a/includes/app/state.h +++ b/includes/app/state.h @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/02 15:49:56 by maiboyer #+# #+# */ -/* Updated: 2024/05/09 16:23:54 by rparodi ### ########.fr */ +/* Updated: 2024/07/12 18:44:18 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,7 @@ typedef struct s_parser t_first_parser *parser; } t_parser; -typedef struct s_utils +typedef struct s_state { t_str name_shell; t_str str_input; @@ -33,6 +33,6 @@ typedef struct s_utils t_hashmap_env *env; t_node current_node; t_process ret; -} t_utils; +} t_state; #endif /* STATE_H */ diff --git a/includes/minishell.h b/includes/minishell.h index 5095437c..d40be919 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -25,7 +25,7 @@ #define PATH_FILES "/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games" t_i32 main(t_i32 argc, t_str argv[], t_str arge[]); -void ft_other_cmd(t_utils *shcat, t_usize i, t_usize prev_i); +void ft_other_cmd(t_state *shcat, t_usize i, t_usize prev_i); t_i32 ft_strcmp(const char *s1, const char *s2); t_i32 ft_check_type_operators(t_str operators); t_str *ft_split(t_const_str s, t_i8 c); @@ -39,9 +39,9 @@ void ft_bzero(void *s, t_usize n); void ft_free_strs(t_str *strs); void ft_pwd(void); void ft_echo(t_str txt, t_str flag); -void ft_exit(t_utils *maiboyerlpb, t_u8 exit_status); +void ft_exit(t_state *maiboyerlpb, t_u8 exit_status); void ft_free(void *ptr); -t_error handle_node_getstr(t_node *self, t_utils *shcat, t_str *out); +t_error handle_node_getstr(t_node *self, t_state *shcat, t_str *out); #endif diff --git a/sources/ft_cmd.c b/sources/ft_cmd.c index b5c175e9..3ac81c89 100644 --- a/sources/ft_cmd.c +++ b/sources/ft_cmd.c @@ -12,7 +12,7 @@ #include "../includes/minishell.h" -// void ft_exec_cmd(t_utils *utils, t_str cmd, t_str cmd_args[]) +// void ft_exec_cmd(t_state *utils, t_str cmd, t_str cmd_args[]) // { // if (execve(cmd, cmd_args, utils->envp) == -1) // { @@ -22,7 +22,7 @@ // } // } // -// void ft_other_cmd(t_utils *shcat, t_usize i, t_usize prev_i) +// void ft_other_cmd(t_state *shcat, t_usize i, t_usize prev_i) // { // pid_t pid; // t_i32 options; diff --git a/sources/ft_exit.c b/sources/ft_exit.c index 4c8134a5..4383af4d 100644 --- a/sources/ft_exit.c +++ b/sources/ft_exit.c @@ -34,7 +34,7 @@ void ft_free_strs(t_str *strs) ft_free(strs); } -void ft_free_utils(t_utils *s) +void ft_free_utils(t_state *s) { if (s->str_input) mem_free(s->str_input); @@ -45,7 +45,7 @@ void ft_free_utils(t_utils *s) ts_parser_delete(s->parser.parser); } -void ft_exit(t_utils *maiboyerlpb, t_u8 exit_status) +void ft_exit(t_state *maiboyerlpb, t_u8 exit_status) { if (maiboyerlpb != NULL) ft_free_utils(maiboyerlpb); diff --git a/sources/main.c b/sources/main.c index 804464cd..49f52805 100644 --- a/sources/main.c +++ b/sources/main.c @@ -116,13 +116,13 @@ t_node parse_str(t_parser *parser, t_const_str input) return (parse_to_nodes(parser->parser, input)); } -void exec_shcat(t_utils *shcat) +void exec_shcat(t_state *shcat) { print_node_data(&shcat->current_node, 0); free_node(shcat->current_node); } -void ft_take_args(t_utils *shcat) +void ft_take_args(t_state *shcat) { t_str cmd; @@ -163,14 +163,14 @@ void free_myparser(t_parser self) t_i32 main(t_i32 argc, t_str argv[], t_str envp[]) { - t_utils utils; + t_state utils; (void)argc; (void)argv; (void)envp; if (install_signal()) me_abort("Unable to install signals"); - utils = (t_utils){}; + utils = (t_state){}; utils.parser = create_myparser(); utils.env = create_env_map(); if (populate_env(utils.env, envp))