diff --git a/src/subst/variable_subst.c b/src/subst/variable_subst.c index 01c4fe3..4605445 100644 --- a/src/subst/variable_subst.c +++ b/src/subst/variable_subst.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* variable_subst.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: khais +#+ +:+ +#+ */ +/* By: jguelen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2025/03/06 12:48/00 by khais #+# #+# */ -/* Updated: 2025/03/08 14:01:30 by jguelen ### ########.fr */ +/* Created: 2025/03/06 12:48:00 by khais #+# #+# */ +/* Updated: 2025/03/10 14:11:30 by jguelen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -135,7 +135,7 @@ t_worddesc *word_var_expansion(t_worddesc *word, t_minishell *app) i = 0; while (word->word[i] && word->word[i + 1]) { - if (word->mark_string[i] != '\'' && word->word[i] == '$') + if (word->marker[i] != '\'' && word->word[i] == '$') { rep = calculate_replacement(word, app, i, &id_len); if (!rep) diff --git a/src/subst/wildcard_exp.c b/src/subst/wildcard_exp.c index 76f34b6..30d6e35 100644 --- a/src/subst/wildcard_exp.c +++ b/src/subst/wildcard_exp.c @@ -6,7 +6,7 @@ /* By: jguelen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/23 15:02:59 by jguelen #+# #+# */ -/* Updated: 2025/03/08 17:25:03 by jguelen ### ########.fr */ +/* Updated: 2025/03/10 17:08:25 by jguelen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,6 @@ /* ** TODO Pre-process file_pattern worddesc for a congruent marker string -** TODO Post-process to sort the resulting list regarding ascii order */ /******************************************************************************/ /* NOTE: The use of errno and the setting of it was OKed by Alexandru in this */ @@ -52,7 +51,9 @@ DIR *open_current_dir(void) } /* -** +** TODO produce a clean marker string that denotes file_desc as issued from +** a pattern expansion or as quoted for possible spaces characters in the +** filename not to pose any possible issue. */ static t_wordlist *add_file_to_list(t_wordlist **list, char *filename) { @@ -62,7 +63,7 @@ static t_wordlist *add_file_to_list(t_wordlist **list, char *filename) copy = ft_strdup(filename); if (!copy) return (wordlist_destroy(*list), NULL); - file_desc = worddesc_create(copy, '\0', NULL); ///////////CHECK IF CONFLICT + file_desc = worddesc_create(copy, '\0', NULL); if (!file_desc) { wordlist_destroy(*list); @@ -96,9 +97,8 @@ static t_wordlist *add_file_to_list(t_wordlist **list, char *filename) ** that matches pattern->word if any file matches in the current directory. ** Otherwise return file_pattern itself if nothing matches the perceived ** pattern. This list should be alphabetically sorted. -** TODO sort the list in growing ascii order. (use of strcmp probable) */ -t_wordlist *expand_star(t_worddesc *file_pattern) +static t_wordlist *expand_star_core(t_worddesc *file_pattern) { struct dirent *new; DIR *current_dir; @@ -124,5 +124,39 @@ t_wordlist *expand_star(t_worddesc *file_pattern) return (wordlist_destroy(file_wordlist), NULL); if (!file_wordlist) wordlist_push(file_wordlist, file_pattern); - return (wordlist_quicksort(file_wordlist)); + return (wordlist_quicksort_full(file_wordlist)); +} + +/* +** TODO +** Pre-processes file_pattern to give it a proper marker chain and remove +** unquoted quotes from the pattern. +** Returns NULL in case of error. +** NOTE: This phase of pre-processing relies partly on processing done before +** it therefore checks more loosely (and quote removal still seems not to be +** done on string comming from a variable expansion) <-----TO RE-CHECK +*/ +t_wordlist *expand_star(t_worddesc *file_pattern) +{ + size_t i; + char *tmp; + + i = 0; + while (file_pattern->word[i]) + { + if (file_pattern->marker[i] != '\'' && file_pattern->marker[i] != '"' + && file_pattern->marker[i] != '&' && file_pattern->marker[i] != '$' + && (file_pattern->word[i] == '\'' || file_pattern->word[i] == '"')) + { + tmp = replace_in_str(file_pattern->word, i, i, NULL); + free(file_pattern->word); + file_pattern->word = tmp; + tmp = replace_in_str(file_pattern->marker, i, i, NULL); + free(file_pattern->marker); + file_pattern->marker = tmp; + continue; + } + i++; + } + return (expand_star_core(file_pattern)); } diff --git a/src/subst/wildcard_exp_utils.c b/src/subst/wildcard_exp_utils.c index 9381edd..7b68f93 100644 --- a/src/subst/wildcard_exp_utils.c +++ b/src/subst/wildcard_exp_utils.c @@ -6,7 +6,7 @@ /* By: jguelen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/07 17:10:01 by jguelen #+# #+# */ -/* Updated: 2025/03/08 17:21:48 by jguelen ### ########.fr */ +/* Updated: 2025/03/10 15:36:11 by jguelen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,6 +46,7 @@ static int same_character_or_one_char_wild(char *str, t_worddesc *pattern, || (pattern->word[j] == '?' && pattern->marker[j] != '\'' && pattern->marker != '"' + && pattern->marker != '&' && !(i == 1 && pattern->word[i] == '.'))); } @@ -78,8 +79,8 @@ void build_pattern_checks(char *str, t_worddesc *pattern, if (same_character_or_one_char_wild(str, pattern->word, i, j)) pattern_check[i][j] = pattern_check[i - 1][j - 1]; else if (pattern->word[j] == '*' && pattern->marker[j] != '\'' - && pattern->marker[j] != '"' && !(i == 1 - && pattern->word[i] == '.')) + && pattern->marker[j] != '"' && pattern->marker[j] != '&' + && !(i == 1 && pattern->word[i] == '.')) pattern_check[i][j] = !!(pattern_check[i - 1][j] + pattern_check[i][j - 1]); j++; @@ -122,3 +123,29 @@ char fits_pattern(char *str, t_worddesc *pattern) destroy_pattern_check(pattern_check, str_len + 1); return (ret); } + +/* +** Checks if a worddesc is to be considered a pattern as bash would in a +** reduced capacity, to wit if it contains at least one unquoted '?' or '*' +** character. +** Returns 1 if the worddesc pointed to by desc is to be considered a pattern, +** 0 otherwise. +*/ +int ispattern(t_worddesc *desc) +{ + size_t i; + + if (!desc) + return (0); + i = 0; + while (desc->word[i]) + { + if ((desc->word[i] == '*' || desc->word[i] == '?') + && (desc->marker[i] != '\'' && desc->marker[i] != '"' + && desc->marker[i] != '&')) + return (1); + i++; + } + return (0); +} +