From dfe27312852491c391af4105470ff57ccea3012a Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Sat, 31 Aug 2024 13:36:40 +0200 Subject: [PATCH] Make some functions comply with the 42 Norm Make functions inside tokenization.c and wildcards.c comply with the 42 Norm. --- src/tokenization.c | 93 +++++++++++++++++++++++----------------------- src/wildcards.c | 81 +++++++++++++++++++++------------------- 2 files changed, 89 insertions(+), 85 deletions(-) diff --git a/src/tokenization.c b/src/tokenization.c index ccabffc..505774a 100644 --- a/src/tokenization.c +++ b/src/tokenization.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* tokenization.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: lnikolov +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/21 16:34:43 by ljiriste #+# #+# */ -/* Updated: 2024/08/31 11:58:17 by ljiriste ### ########.fr */ +/* Updated: 2024/08/31 12:55:57 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -164,47 +164,74 @@ int is_redirection_operator(const t_token *token) || token->type == g_tokens[DGREAT]); } +int assignment_may_follow(const char *type) +{ + return (type == g_tokens[ASSIGNMENT_WORD] + || type == g_tokens[AND_IF] + || type == g_tokens[OR_IF] + || type == g_tokens[LPARA]); +} + void filter_assignment_word(t_vec *tokens) { size_t i; t_token *token; const t_token *prev_token; - i = 0; - while (i < tokens->size) + i = -1; + while (i + 1 < tokens->size) { + ++i; token = ft_vec_access(tokens, i); if (i == 0 || token->type != g_tokens[ASSIGNMENT_WORD]) - { - ++i; continue ; - } prev_token = ft_vec_caccess(tokens, i - 1); - if (prev_token->type == g_tokens[ASSIGNMENT_WORD] - || prev_token->type == g_tokens[AND_IF] - || prev_token->type == g_tokens[OR_IF] - || prev_token->type == g_tokens[LPARA]) - { - ++i; + if (assignment_may_follow(prev_token->type)) continue ; - } if (i == 1 || prev_token->type != g_tokens[WORD]) { - ++i; token->type = (char *)g_tokens[WORD]; continue ; } prev_token = ft_vec_caccess(tokens, i - 2); if (!is_redirection_operator(prev_token)) token->type = (char *)g_tokens[WORD]; - ++i; } } +int categorization_step(char *line, size_t *i, + t_vec *current_token, t_vec *tokens) +{ + int res; + + if (is_operator_start(current_token->vec, current_token->size) + && can_expand_operator(current_token, line[*i])) + res = (ft_vec_append(current_token, line + (*i)++) != success); + else if (is_operator(current_token)) + res = finish_token(tokens, current_token, '\0'); + else if (line[*i] == '\'') + res = handle_quote(current_token, line, '\'', i); + else if (line[*i] == '"' ) + res = handle_quote(current_token, line, '"', i); + else if (is_operator_start(line + *i, 1) || ft_isspace(line[*i])) + { + if (current_token->size > 0) + res = finish_token(tokens, current_token, line[*i]); + if (!ft_isspace(line[*i])) + res = res || ft_vec_append(current_token, line + *i) + != success; + ++*i; + } + else if (current_token->size > 0) + res = ft_vec_append(current_token, line + (*i)++) != success; + else + res = ft_vec_append(current_token, line + (*i)++) != success; + return (res); +} + int tokenize(char *line, t_vec *tokens) { t_vec current_token; - t_token token; size_t i; int res; @@ -213,38 +240,12 @@ int tokenize(char *line, t_vec *tokens) i = 0; while (line[i] && res == 0) { - if (is_operator_start(current_token.vec, current_token.size) - && can_expand_operator(¤t_token, line[i])) - res = (ft_vec_append(¤t_token, line + (i++)) != success); - else if (is_operator(¤t_token)) - res = finish_token(tokens, ¤t_token, '\0'); - else if (line[i] == '\'') - res = handle_quote(¤t_token, line, '\'', &i); - else if (line[i] == '"' ) - res = handle_quote(¤t_token, line, '"', &i); - else if (is_operator_start(line + i, 1) || ft_isspace(line[i])) - { - if (current_token.size > 0) - res = finish_token(tokens, ¤t_token, line[i]); - if (!ft_isspace(line[i])) - res = res || ft_vec_append(¤t_token, line + i) - != success; - ++i; - } - else if (current_token.size > 0) - res = ft_vec_append(¤t_token, line + (i++)) != success; - else if (line[i] == '#') + if (line[i] == '#') break ; - else - res = ft_vec_append(¤t_token, line + (i++)) != success; + res = categorization_step(line, &i, ¤t_token, tokens); } if (current_token.size > 0 && !res) - { - ft_vec_append(¤t_token, ""); - token.type = (char *)get_token_type(current_token.vec, '\0'); - token.str = current_token.vec; - ft_vec_append(tokens, &token); - } + res = finish_token(tokens, ¤t_token, '\0'); if (res) ft_vec_free(tokens, free_token); ft_vec_free(¤t_token, NULL); diff --git a/src/wildcards.c b/src/wildcards.c index 89adcfb..3613608 100644 --- a/src/wildcards.c +++ b/src/wildcards.c @@ -6,7 +6,7 @@ /* By: lnikolov current_expand_char == '\0' && *info->current_entry_char == '\0') - return (add_entry(expanded, info)); - if (*info->current_expand_char == '/' && *info->current_entry_char == '\0') - { - ++info->current_expand_char; - return (expand_further(expanded, *info)); - } - if (*info->current_expand_char == '/' && info->current_entry_char - == info->entry) - { - ++info->current_expand_char; - return (add_conformant(expanded, info, quote)); - } if (*info->current_expand_char == '\'' || *info->current_expand_char == '"') { if (!quote) @@ -118,6 +103,26 @@ int add_conformant(t_vec *expanded, t_wildcard_info *info, char quote) return (0); } +int add_conformant(t_vec *expanded, t_wildcard_info *info, char quote) +{ + if (g_last_signal != 0) + return (2); + if (*info->current_expand_char == '\0' && *info->current_entry_char == '\0') + return (add_entry(expanded, info)); + if (*info->current_expand_char == '/' && *info->current_entry_char == '\0') + { + ++info->current_expand_char; + return (expand_further(expanded, *info)); + } + if (*info->current_expand_char == '/' && info->current_entry_char + == info->entry) + { + ++info->current_expand_char; + return (add_conformant(expanded, info, quote)); + } + return (add_conformant_part2(expanded, info, quote)); +} + int expand_dir(t_vec *expanded, t_wildcard_info *info) { DIR *dir; @@ -181,17 +186,15 @@ int replace_str_by_joint_quoted_split(char **str, char **expanded_split) size_t len; size_t i; - tmp = *str; len = 0; i = 0; while (expanded_split[i]) len += ft_strlen(expanded_split[i++]) + 2 + 1; - *str = malloc(len + 1 + 1); - if (!str) - { - *str = tmp; + tmp = malloc(len + 1 + 1); + if (!tmp) return (1); - } + free(*str); + *str = tmp; str[0][0] = '\0'; i = 0; while (expanded_split[i]) @@ -201,7 +204,6 @@ int replace_str_by_joint_quoted_split(char **str, char **expanded_split) ft_strlcat(*str, "'", len + 1 + 1); ft_strlcat(*str, " ", len + 1 + 1); } - free(tmp); return (0); } @@ -249,17 +251,13 @@ char *get_start_path(const char *str, const t_execution_env *env) return (res); } -int expand_word(char **str, const t_execution_env *env) +int expand_word_inner(char **str, const t_execution_env *env) { const char *nullptr = NULL; t_vec matched; t_wildcard_info info; int res; - char last_char; - last_char = str[0][ft_strlen(*str) - 1]; - if (last_char == '\n') - str[0][ft_strlen(*str) - 1] = '\0'; if (ft_vec_init(&matched, sizeof(char *)) != success) return (1); info.to_expand = *str; @@ -276,6 +274,18 @@ int expand_word(char **str, const t_execution_env *env) res = res || replace_str_by_joint_quoted_split(str, matched.vec); ft_vec_free(&matched, free_str); free((char *)info.path); + return (res); +} + +int expand_word(char **str, const t_execution_env *env) +{ + int res; + char last_char; + + last_char = str[0][ft_strlen(*str) - 1]; + if (last_char == '\n') + str[0][ft_strlen(*str) - 1] = '\0'; + res = expand_word_inner(str, env); if (last_char == '\n') { str[0][ft_strlen(*str) + 1] = '\0'; @@ -296,18 +306,11 @@ int should_be_expanded(const char *word) if ((word[i] == '*' || (word[i] == '?' && !(i && word[i - 1] == '$'))) && quote_char == '\0') return (1); - if (word[i] == '"') - { - if (quote_char == '\0') - quote_char = '"'; - else if (quote_char == '"') - quote_char = '\0'; - } - if (word[i] == '\'') + if (word[i] == '"' || word[i] == '\'') { if (quote_char == '\0') - quote_char = '\''; - else if (quote_char == '\'') + quote_char = word[i]; + else if (quote_char == word[i]) quote_char = '\0'; } ++i; -- 2.30.2