From 6cfc2beff445f37f1419ecd555b5252c11893cb1 Mon Sep 17 00:00:00 2001 From: tomoron Date: Mon, 6 May 2024 10:58:42 +0200 Subject: [PATCH] norme --- srcs/exec_utils.c | 10 +----- srcs/lst_env.c | 6 ++-- srcs/minishell.h | 4 ++- srcs/parsing.c | 58 ++-------------------------------- srcs/variable_expantion.c | 65 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 74 insertions(+), 69 deletions(-) create mode 100644 srcs/variable_expantion.c diff --git a/srcs/exec_utils.c b/srcs/exec_utils.c index 623b78d..a03f27c 100644 --- a/srcs/exec_utils.c +++ b/srcs/exec_utils.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/24 10:46:28 by marde-vr #+# #+# */ -/* Updated: 2024/05/06 10:13:22 by tomoron ### ########.fr */ +/* Updated: 2024/05/06 10:56:52 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -53,14 +53,6 @@ int get_cmd_count(t_cmd *cmds) nb = 1; while (cmds && !is_operand_type(cmds)) { - /*while (cmds && (is_output_type(cmds) || is_input_type(cmds))) - cmds = cmds->next; - if (cmds && is_cmd_type(cmds)) - nb++; - while (cmds && (is_output_type(cmds) || is_input_type(cmds) - || is_cmd_type(cmds))) - cmds = cmds->next; - */ if (cmds && cmds->cmd_type == PIPE) { nb++; diff --git a/srcs/lst_env.c b/srcs/lst_env.c index 84e5e9b..3fbfa06 100755 --- a/srcs/lst_env.c +++ b/srcs/lst_env.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/06 20:46:19 by tomoron #+# #+# */ -/* Updated: 2024/05/03 14:20:04 by tomoron ### ########.fr */ +/* Updated: 2024/05/06 10:56:39 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,13 +17,13 @@ t_env *env_add_back(t_env *env, char *name, char *value, int empty) t_env *res; t_env *current; - if(empty) + if (empty) free(value); res = ft_calloc(1, sizeof(t_env)); if (!res) return (env); res->name = name; - if(!empty) + if (!empty) res->value = value; if (!env) return (res); diff --git a/srcs/minishell.h b/srcs/minishell.h index 0333881..2670073 100755 --- a/srcs/minishell.h +++ b/srcs/minishell.h @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */ -/* Updated: 2024/05/06 10:54:21 by tomoron ### ########.fr */ +/* Updated: 2024/05/06 10:57:52 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -82,6 +82,8 @@ extern int g_return_code; int get_in_type(t_msh *msh, t_cmd *t_strt, t_cmd *tokens, int here_doc); int add_var_to_str(char *res, char **command, t_env *env, int dquote); +int get_variable_expantion_len(char *command, t_env *env); +char *expand_variables(char *command, t_env *env, int *is_var); t_env *export_set_env(t_env *env, char *name, char *value, int append); void parent(t_msh *msh, int i, int cmd_count, char **cmd_args); t_token *expand_wildcards(t_token *res, char *value, int is_var); diff --git a/srcs/parsing.c b/srcs/parsing.c index 838243f..ff30538 100755 --- a/srcs/parsing.c +++ b/srcs/parsing.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/09 15:26:01 by tomoron #+# #+# */ -/* Updated: 2024/05/06 10:36:45 by tomoron ### ########.fr */ +/* Updated: 2024/05/06 10:58:08 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" @@ -48,8 +48,7 @@ char *get_token(char **cmd, int quotes[2]) quotes[0] = !quotes[0]; else if (**cmd == '~' && !quotes[0] && !quotes[1]) i += add_home_to_str(res + i); - else if (((**cmd == '\'' && quotes[1]) - || (**cmd == '"' && quotes[0])) + else if (((**cmd == '\'' && quotes[1]) || (**cmd == '"' && quotes[0])) || (**cmd != '\'' && **cmd != '"')) res[i++] = **cmd; (*cmd)++; @@ -57,59 +56,6 @@ char *get_token(char **cmd, int quotes[2]) return (res); } -int get_variable_expantion_len(char *command , t_env *env) -{ - int in_quote; - int in_dquote; - int i; - - i = 0; - in_dquote = 0; - in_quote = 0; - while(*command) - { - if(*command == '\'' && !in_dquote) - in_quote = !in_quote; - if(*command == '"' && !in_quote) - in_dquote = !in_dquote; - if(*command == '$' && !in_quote) - i+= get_var_len(&command, env, in_dquote); - else - i++; - command++; - } - return(i); -} - -char *expand_variables(char *command, t_env *env, int *is_var) -{ - char *res; - int i; - int in_dquote; - int in_quote; - - if(!command) - return(0); - res = ft_calloc(get_variable_expantion_len(command, env) + 1, 1); - in_quote = 0; - in_dquote = 0; - *is_var = 1; - i = 0; - while(res && *command) - { - if(*command == '\'' && !in_dquote) - in_quote = !in_quote; - if(*command == '"' && !in_quote) - in_dquote = !in_dquote; - if(*command == '$' && !in_quote) - i+= add_var_to_str(res + i, &command ,env, in_dquote); - else - res[i++] = *command; - command++; - } - return(res); -} - t_token *parse_tokens(char *command, t_env *env) { int quotes[2]; diff --git a/srcs/variable_expantion.c b/srcs/variable_expantion.c new file mode 100644 index 0000000..a66f659 --- /dev/null +++ b/srcs/variable_expantion.c @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* variable_expantion.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/06 10:57:33 by tomoron #+# #+# */ +/* Updated: 2024/05/06 10:57:39 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "minishell.h" + +int get_variable_expantion_len(char *command, t_env *env) +{ + int in_quote; + int in_dquote; + int i; + + i = 0; + in_dquote = 0; + in_quote = 0; + while (*command) + { + if (*command == '\'' && !in_dquote) + in_quote = !in_quote; + if (*command == '"' && !in_quote) + in_dquote = !in_dquote; + if (*command == '$' && !in_quote) + i += get_var_len(&command, env, in_dquote); + else + i++; + command++; + } + return (i); +} + +char *expand_variables(char *command, t_env *env, int *is_var) +{ + char *res; + int i; + int in_dquote; + int in_quote; + + if (!command) + return (0); + res = ft_calloc(get_variable_expantion_len(command, env) + 1, 1); + in_quote = 0; + in_dquote = 0; + *is_var = 1; + i = 0; + while (res && *command) + { + if (*command == '\'' && !in_dquote) + in_quote = !in_quote; + if (*command == '"' && !in_quote) + in_dquote = !in_dquote; + if (*command == '$' && !in_quote) + i += add_var_to_str(res + i, &command, env, in_dquote); + else + res[i++] = *command; + command++; + } + return (res); +}