diff --git a/srcs/minishell.h b/srcs/minishell.h index 8bedfee..8d5b8d2 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/03 14:16:24 by tomoron ### ########.fr */ +/* Updated: 2024/05/04 14:11:35 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -81,7 +81,7 @@ typedef struct s_msh 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 *is_var); +int add_var_to_str(char *res, char **command, t_env *env); 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); @@ -115,7 +115,8 @@ char *ft_get_env(t_env *env, char *var_name); int get_out_type(t_msh *msh, t_cmd *cmds); void handle_here_doc(t_msh *msh, char *eof); void signal_handler_interactive(int signum); -int get_token_len(char *cmd, t_env *env); +int get_token_len(char *cmd); +int get_var_len(char **command, t_env *env); void signal_handler_here_doc(int signum); t_token *parsing_syntax_error(t_token *res); int file_access(t_msh *msh, int *found); diff --git a/srcs/parsing.c b/srcs/parsing.c index 747b107..1afa008 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/04/24 20:39:35 by marde-vr ### ########.fr */ +/* Updated: 2024/05/04 14:30:22 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" @@ -31,7 +31,7 @@ int add_home_to_str(char *res) return (i); } -char *get_token(char **cmd, int *quotes[2], t_env *env, int *is_var) +char *get_token(char **cmd, int quotes[2]) { char *res; int i; @@ -39,19 +39,17 @@ char *get_token(char **cmd, int *quotes[2], t_env *env, int *is_var) i = 0; while (ft_isspace(**cmd)) (*cmd)++; - res = ft_calloc(get_token_len(*cmd, env) + 1, 1); - while (res && **cmd && (is_cmd_char(**cmd) || *(quotes[0]) || *(quotes[1]))) + res = ft_calloc(get_token_len(*cmd) + 1, 1); + while (res && **cmd && (is_cmd_char(**cmd) || quotes[0] || quotes[1])) { - if (**cmd == '"' && !*(quotes[0])) - *(quotes[1]) = !*(quotes[1]); - if (**cmd == '\'' && !*(quotes[1])) - *(quotes[0]) = !*(quotes[0]); - if (**cmd == '$' && !*(quotes[0])) - i += add_var_to_str(res + i, cmd, env, is_var); - else if (**cmd == '~' && !*(quotes[0]) && !*(quotes[1])) + if (**cmd == '"' && !quotes[0]) + quotes[1] = !quotes[1]; + if (**cmd == '\'' && !quotes[1]) + 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)++; @@ -59,29 +57,85 @@ char *get_token(char **cmd, int *quotes[2], t_env *env, int *is_var) 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); + else + i++; + command++; + } + return(i); +} + +char *expand_variables(char *command, t_env *env) +{ + char *res; + char *start; + int i; + int in_dquote; + int in_quote; + + if(!command) + return(0); + res = ft_calloc(get_variable_expantion_len(command, env) + 1, 1); + start = command; + in_quote = 0; + in_dquote = 0; + 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); + else + res[i++] = *command; + command++; + } + return(res); +} + t_token *parse_tokens(char *command, t_env *env) { - int in_quote; - int in_dquote; + int quotes[2]; + char *tmp; t_token *res; char *value; int is_var; - in_quote = 0; - in_dquote = 0; + quotes[0] = 0; + quotes[1] = 0; res = 0; is_var = 0; + command = expand_variables(command, env); + tmp = command; while (command && *command) { - value = get_token(&command, (int *[2]){&in_quote, &in_dquote}, - env, &is_var); + value = get_token(&command, quotes); if (!value) return (free_token(res)); res = expand_wildcards(res, value, is_var); while (ft_isspace(*command)) command++; } - if (command && (in_quote || in_dquote)) + free(tmp); + if (tmp && (quotes[0] || quotes[1])) return (parsing_syntax_error(res)); return (res); } diff --git a/srcs/parsing_var.c b/srcs/parsing_var.c index 29d1c61..88727b9 100755 --- a/srcs/parsing_var.c +++ b/srcs/parsing_var.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/09 15:24:36 by tomoron #+# #+# */ -/* Updated: 2024/05/03 12:52:45 by tomoron ### ########.fr */ +/* Updated: 2024/05/04 14:11:54 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,7 +45,7 @@ int get_var_len(char **command, t_env *env) return (ft_strlen(env_var)); } -int get_token_len(char *command, t_env *env) +int get_token_len(char *command) { int in_quote; int in_dquote; @@ -60,8 +60,6 @@ int get_token_len(char *command, t_env *env) in_dquote = !in_dquote; if (*command == '\'' && !in_dquote) in_quote = !in_quote; - if (*command == '$' && !in_quote) - res += get_var_len(&command, env); else if (*command == '~' && !in_quote && !in_dquote) res += ft_strlen(getenv("HOME")); else if (*command != '\'' && *command != '"') @@ -81,7 +79,7 @@ int invalid_variable_char(char *res, char c) return (2); } -int add_var_to_str(char *res, char **command, t_env *env, int *is_var) +int add_var_to_str(char *res, char **command, t_env *env) { char *var_name; char *var; @@ -89,7 +87,6 @@ int add_var_to_str(char *res, char **command, t_env *env, int *is_var) i = 0; (*command)++; - *is_var = 1; if (**command == '\'' || **command == '"' || !**command) { if (**command != '\'' && **command != '"')