From: Lukas Jiriste Date: Sun, 21 Jul 2024 17:40:50 +0000 (+0200) Subject: Implement expansion inside execution.c X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=7f4010bf29c0e51d87733a6aaf53d83655e7d4c6;p=42%2Fminishell.git Implement expansion inside execution.c Due to that the expansion.c file is removed. --- diff --git a/Makefile b/Makefile index 43bd564..fc1e0a8 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,6 @@ SOURCES := main.c \ input_handling.c \ tokenization.c \ parsing.c \ - expansion.c \ execution.c \ helpers.c \ diff --git a/inc/minishell.h b/inc/minishell.h index 0835940..d708866 100644 --- a/inc/minishell.h +++ b/inc/minishell.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/02 13:22:57 by ljiriste #+# #+# */ -/* Updated: 2024/07/21 17:19:11 by ljiriste ### ########.fr */ +/* Updated: 2024/07/21 17:46:51 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -47,7 +47,6 @@ void handle_input(char **line, t_vars *vars); int tokenize(char **line, t_vec *tokens); int parse(t_vec *tokens, t_tree **parse_tree); -int expand(t_tree *parse_tree, t_vars *vars); int execute(t_tree *parse_tree, t_vars *vars); int init_tree(t_tree *tree); diff --git a/src/execution.c b/src/execution.c index 4033c23..b730c6f 100644 --- a/src/execution.c +++ b/src/execution.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/21 08:57:54 by ljiriste #+# #+# */ -/* Updated: 2024/07/21 18:29:22 by ljiriste ### ########.fr */ +/* Updated: 2024/07/21 19:45:29 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -360,6 +360,81 @@ int save_redirections(t_vec *redirections, t_parse_tree_node *simple_command, co return (0); } +int add_word(t_vec *exp_str, const char *word, const t_execution_env *env) +{ + size_t i; + char *var; + const char *value; + + while (word[i]) + { + if (word[i] == '$') + { + ++i; + var = get_var_name(word + i); + if (!var) + return (1); + value = get_env_var_value(env, var); + i += ft_strlen(var); + free(var); + if (value) + if (ft_vec_append_range(exp_str, value, ft_strlen(value)) != success) + return (1); + } + else + if (ft_vec_append(exp_str, word + (i++)) != success) + return (1); + } + return (0); +} + +int expand_cmd(t_vec *exp_str, const t_parse_tree_node *node, const t_execution_env *env) +{ + size_t i; + const t_parse_tree_node *subnode; + + i = 0; + while (i < node->children.size) + { + subnode = ft_vec_caccess(&node->children, i); + if (is_token_type(subnode, "cmd_suffix")) + { + if (expand_cmd(exp_str, subnode, env)) + return (1); + } + else if (is_token_type(subnode, "WORD")) + if (add_word(exp_str, subnode->token.str, env)) + return (1); + ++i; + } + return (0); +} + +char **expand(t_parse_tree_node *simple_command, const t_execution_env *env) +{ + size_t i; + t_vec expanded_str; + t_parse_tree_node *subnode; + + ft_vec_init(&expanded_str, sizeof(char)); + ft_vec_reserve(&expanded_str, 64); + i = 0; + while (i < simple_command->children.size) + { + subnode = ft_vec_access(&simple_command->children, i); + if (is_token_type(subnode, "cmd_name") + || is_token_type(subnode, "cmd_word") + || is_token_type(subnode, "cmd_suffix")) + if (expand_cmd(&expanded_str, subnode, env)) + { + ft_vec_free(&expanded_str, NULL); + return (NULL); + } + ++i; + } + return (expanded_str.vec); +} + int ex_simple_command(t_parse_tree_node *simple_command, t_execution_env *env) { t_vec redirections; @@ -370,7 +445,7 @@ int ex_simple_command(t_parse_tree_node *simple_command, t_execution_env *env) res = save_redirections(&redirections, simple_command, env); res = res || save_assignments(&assignments, simple_command); if (!res) - fields = expand(simple_command, env, &redirections, &assignments); + fields = expand(simple_command, env); if (!fields || res) { ft_vec_free(&redirections, void_free_redirection); @@ -379,7 +454,7 @@ int ex_simple_command(t_parse_tree_node *simple_command, t_execution_env *env) } if (!*fields) { - save_assignments(&assignments, env); + assignments_to_env(&assignments, env); ex_redirections(&redirections); ft_vec_free(&redirections, void_free_redirection); ft_vec_free(&assignments, void_free_assignments); diff --git a/src/expansion.c b/src/expansion.c deleted file mode 100644 index 697241e..0000000 --- a/src/expansion.c +++ /dev/null @@ -1,19 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* expansion.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: ljiriste +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/06/21 16:29:48 by ljiriste #+# #+# */ -/* Updated: 2024/06/21 16:31:57 by ljiriste ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -int expand(__attribute__((unused))t_tree *parse_tree, __attribute__((unused))t_vars *vars) -{ - ft_printf("Variable expansion has yet to be implemented.\n"); - return (0); -}