From 7f0aef0f167fc0d01d6899e0844351f2793dbf35 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Thu, 22 Aug 2024 14:40:00 +0200 Subject: [PATCH] Make wildcards work with variables and quotes --- inc/minishell.h | 6 ++-- src/execution.c | 10 +++--- src/wildcards.c | 88 ++++++++++++++++++++++++++++++++++++++----------- 3 files changed, 77 insertions(+), 27 deletions(-) diff --git a/inc/minishell.h b/inc/minishell.h index 4fb3e46..43f0a29 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/08/08 15:10:56 by ljiriste ### ########.fr */ +/* Updated: 2024/08/22 14:25:59 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -54,7 +54,9 @@ void clean_env(t_execution_env *env); void handle_input(char **line, t_execution_env *env); -int add_conformant(t_vec *expanded, t_wildcard_info *info); +void unquote_field(char *field); +int add_word(t_vec *exp_str, const char *word, const t_execution_env *env); +int add_conformant(t_vec *expanded, t_wildcard_info *info, char quote); int expand_dir(t_vec *expanded, t_wildcard_info *info); int expand_wildcards(char **input, const t_execution_env *env); diff --git a/src/execution.c b/src/execution.c index 819fcf1..6a703b4 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/08/02 18:35:53 by ljiriste ### ########.fr */ +/* Updated: 2024/08/22 14:27:10 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,8 +59,6 @@ void clear_old(t_vec *assignments, const char *new) int add_word(t_vec *exp_str, const char *word, const t_execution_env *env); -void unquote_field(char *field); - int add_assignment(t_vec *assignments, const char *assignment, const t_execution_env *env) { t_vec advanced_copy; @@ -426,7 +424,9 @@ int add_word(t_vec *exp_str, const char *word, const t_execution_env *env) i += ft_strlen(var); free(var); if (value) - if (ft_vec_append_range(exp_str, value, ft_strlen(value)) != success) + if (ft_vec_append(exp_str, "'") != success + || ft_vec_append_range(exp_str, value, ft_strlen(value)) != success + || ft_vec_append(exp_str, "'") != success) return (1); } else @@ -490,7 +490,7 @@ void unquote_field(char *field) quote = 0; i = 0; - while (field[i]) + while (field && field[i]) { if (field[i] == '"' || field[i] == '\'') { diff --git a/src/wildcards.c b/src/wildcards.c index 8274f49..d516f9d 100644 --- a/src/wildcards.c +++ b/src/wildcards.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/08 10:50:26 by ljiriste #+# #+# */ -/* Updated: 2024/08/22 13:02:23 by ljiriste ### ########.fr */ +/* Updated: 2024/08/22 14:35:25 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,13 +36,13 @@ int branch_at_star(t_vec *expanded, t_wildcard_info *info) const size_t start_size = expanded->size; ++info->current_expand_char; - if (add_conformant(expanded, info)) + if (add_conformant(expanded, info, '\0')) return (1); if (start_size != expanded->size || *info->current_entry_char == '\0') return (0); --info->current_expand_char; ++info->current_entry_char; - return (add_conformant(expanded, info)); + return (add_conformant(expanded, info, '\0')); } int expand_further(t_vec *expanded, t_wildcard_info info) @@ -66,7 +66,7 @@ int expand_further(t_vec *expanded, t_wildcard_info info) return (res); } -int add_conformant(t_vec *expanded, t_wildcard_info *info) +int add_conformant(t_vec *expanded, t_wildcard_info *info, char quote) { if (*info->current_expand_char == '\0' && *info->current_entry_char == '\0') return (add_entry(expanded, info)); @@ -78,14 +78,25 @@ int add_conformant(t_vec *expanded, t_wildcard_info *info) if (*info->current_expand_char == '/' && info->current_entry_char == info->entry) { ++info->current_expand_char; - return (add_conformant(expanded, info)); + return (add_conformant(expanded, info, quote)); + } + if (*info->current_expand_char == '\'' || *info->current_expand_char == '"') + { + if (!quote) + quote = *info->current_expand_char; + else if (quote == *info->current_expand_char) + quote = '\0'; + else + --info->current_expand_char; + ++info->current_expand_char; + return (add_conformant(expanded, info, quote)); } if ((*info->current_expand_char == '?' && *info->current_entry_char != '\0') - || *info->current_expand_char == *info->current_entry_char) + || (*info->current_expand_char == *info->current_entry_char && (*info->current_expand_char != '*' || quote))) { ++info->current_expand_char; ++info->current_entry_char; - return (add_conformant(expanded, info)); + return (add_conformant(expanded, info, quote)); } if (*info->current_expand_char == '*' && (*info->current_entry_char != '.' || info->current_entry_char != info->entry)) return (branch_at_star(expanded, info)); @@ -109,7 +120,7 @@ int expand_dir(t_vec *expanded, t_wildcard_info *info) info->entry = dir_entry->d_name; info->current_entry_char = info->entry; info->current_expand_char = info->to_expand; - if (add_conformant(expanded, info)) + if (add_conformant(expanded, info, '\0')) { closedir(dir); return (1); @@ -179,6 +190,48 @@ int replace_str_by_joint_quoted_split(char **str, char **expanded_split) return (0); } +int expand_vars(char **str, const t_execution_env *env) +{ + t_vec expanded; + + if (ft_vec_init(&expanded, sizeof(char)) != success) + return (1); + if (add_word(&expanded, *str, env)) + { + ft_vec_free(&expanded, NULL); + return (1); + } + *(char *)ft_vec_access(&expanded, expanded.size - 1) = '\0'; + free(*str); + *str = expanded.vec; + return (0); +} + +char *get_start_path(const char *str, const t_execution_env *env) +{ + char *unquoted_str; + char *res; + + unquoted_str = ft_strdup(str); + unquote_field(unquoted_str); + if (unquoted_str[0] == '/') + res = ft_strdup("/"); + else if (unquoted_str[0] == '~' && (ft_isalpha(unquoted_str[1]) || unquoted_str[1] == '/' || unquoted_str[1] == '~' || unquoted_str[1] == '+' || unquoted_str[1] == '-')) + { + if (unquoted_str[1] == '/' || unquoted_str[1] == '\0') + res = ft_strjoin(get_env_var_value(env, "HOME"), "/"); + else + { + free(unquoted_str); + return (NULL); + } + } + else + res = ft_strdup(""); + free(unquoted_str); + return (res); +} + int expand_word(char **str, const t_execution_env *env) { const char *nullptr = NULL; @@ -192,19 +245,12 @@ int expand_word(char **str, const t_execution_env *env) if (ft_vec_init(&matched, sizeof(char *)) != success) return (1); info.to_expand = *str; - if (str[0][0] == '/') - info.path = ft_strdup("/"); - else if (str[0][0] == '~' && (ft_isalpha(str[0][1]) || str[0][1] == '/' || str[0][1] == '~' || str[0][1] == '+' || str[0][1] == '-')) - { - if (str[0][1] == '/' || str[0][1] == '\0') - info.path = ft_strdup(get_env_var_value(env, "HOME")); - else - return (1); - } - else - info.path = ft_strdup(""); + info.path = get_start_path(*str, env); if (!info.path) + { + ft_vec_free(&matched, free_str); return (1); + } if (info.path[0] && ft_strchr("/~", info.path[0]) != NULL) ++info.to_expand; res = expand_dir(&matched, &info); @@ -263,11 +309,13 @@ int expand_wildcards(char **input, const t_execution_env *env) while (split[i]) { if (should_be_expanded(split[i])) - if (expand_word(split + i, env)) + { + if (expand_vars(split + i, env) || expand_word(split + i, env)) { ft_free_split(split); return (1); } + } ++i; } res = replace_str_by_joint_split(input, split); -- 2.30.2