diff --git a/includes/parser/tokenizer.h b/includes/parser/tokenizer.h index ad4d9a1..4e0cc5d 100644 --- a/includes/parser/tokenizer.h +++ b/includes/parser/tokenizer.h @@ -66,5 +66,7 @@ char *copy_meta(char *val, int *copied); char *copy_unmeta(char *val, int *copied); void debug_token_list(t_node *lst, char *msg); void set_token(t_node *head); +int goto_nxt_space(char *str); +void update_quote(char *quote, char cmp); #endif diff --git a/srcs/parsing/tokenizer/tokenizer.c b/srcs/parsing/tokenizer/tokenizer.c index e866dd5..dfb9f63 100644 --- a/srcs/parsing/tokenizer/tokenizer.c +++ b/srcs/parsing/tokenizer/tokenizer.c @@ -11,72 +11,6 @@ /* ************************************************************************** */ #include "../../../includes/minishell.h" -// -// char *get_til_nxt_quote(char **str, char quote) -// { -// char *base; -// char *tmp; -// char *out; -// int len; -// -// base = *str; -// len = 1; -// tmp = NULL; -// if (**str) -// (*str)++; -// while ((**str) && **str != quote) -// { -// len++; -// (*str)++; -// } -// out = ft_substr(base, 0, len + 1); -// if (**str) -// (*str)++; -// if (**str && *(*str) && (*(*str) == '"' || *(*str) == '\'')) -// { -// tmp = get_til_nxt_quote(str, **str); -// } -// out = ft_strfjoin(out, tmp); -// ft_free(&tmp); -// return (out); -// } -// - -bool is_space(char c) -{ - if ((c >= 9 && c <= 13) || c == 32) - return (true); - else - return (false); -} - -int skip_quote(char *str) -{ - int i; - - if (!str[1]) - return 0; - i = 1; - while(str[i] && str[i] != '\'' && str[i] != '"') - i++; - return (i); -} - -int goto_nxt_space(char *str) -{ - int i; - - i = 0; - while (is_space(str[i])) - i++; - while (str[i] && !is_space(str[i])) - { - if (str[i] == '\'' || str[i] == '"') - i += skip_quote(&(str[i])); - i++; - } - return (i); -} static t_node *tokenize_base(char *str) { @@ -99,6 +33,77 @@ static t_node *tokenize_base(char *str) return (lst); } +void unstick_main(t_node *it) +{ + char *first_str; + char *second_str; + int copied; + + if (is_meta(it->val[0])) + first_str = copy_meta(it->val, &copied); + else + first_str = copy_unmeta(it->val, &copied); + second_str = ft_substr(it->val, copied, ft_strlen(it->val) + - copied); + ft_free(&it->val); + it->val = ft_strdup(first_str); + create_node_after(it, second_str); + ft_free(&first_str); + ft_free(&second_str); +} + +bool unstick_quote(int count, t_node *it) +{ + char *first_str; + char *second_str; + + if (count == 0) + return (false); + first_str = ft_substr(it->val, 0, count + 1); + second_str = ft_substr(it->val, count + 1, ft_strlen(it->val)); + ft_free(&it->val); + it->val = ft_strdup(first_str); + create_node_after(it, second_str); + ft_free(&first_str); + ft_free(&second_str); + return (true); +} + +int quote_sticked(char *str) +{ + int i; + char quote; + + i = 1; + quote = 0; + update_quote("e, str[0]); + while (str[i]) + { + if (quote && str[i] == quote) + if (str[i + 1] && is_meta(str[i + 1])) + return(i); + update_quote("e, str[i]); + i++; + } + return (0); +} + +static int unstick_nodes(t_node *head) +{ + t_node *it; + + it = head; + while (it != NULL) + { + if (unstick_quote(quote_sticked(it->val), it)) + break ; + if (is_sticked(it->val)) + unstick_main(it); + it = it->next; + } + return (1); +} + static void del_void_nodes(t_node **head) { t_node *cpy; @@ -138,9 +143,13 @@ t_node *tokenize(char *str) debug_token_list(head, "trim"); del_void_nodes(&head); debug_token_list(head, "del void"); + unstick_nodes(head); + debug_token_list(head, "unstick"); + del_void_nodes(&head); + debug_token_list(head, "del void"); set_token(head); debug_token_list(head, "set token"); - // if (syntax_error(head)) - // return (free_linked_list(head), NULL); + if (syntax_error(head)) + return (free_linked_list(head), NULL); return (head); } diff --git a/srcs/parsing/tokenizer/tokenizer_utils.c b/srcs/parsing/tokenizer/tokenizer_utils.c index ec5a5f1..b96655a 100644 --- a/srcs/parsing/tokenizer/tokenizer_utils.c +++ b/srcs/parsing/tokenizer/tokenizer_utils.c @@ -37,3 +37,39 @@ int trim_nodes(t_node *head) } return (1); } + +static bool is_space(char c) +{ + if ((c >= 9 && c <= 13) || c == 32) + return (true); + else + return (false); +} + +static int skip_quote(char *str) +{ + int i; + + if (!str[1]) + return 0; + i = 1; + while(str[i] && str[i] != '\'' && str[i] != '"') + i++; + return (i); +} + +int goto_nxt_space(char *str) +{ + int i; + + i = 0; + while (is_space(str[i])) + i++; + while (str[i] && !is_space(str[i])) + { + if (str[i] == '\'' || str[i] == '"') + i += skip_quote(&(str[i])); + i++; + } + return (i); +} diff --git a/srcs/parsing/tokenizer/unstick_node_utils.c b/srcs/parsing/tokenizer/unstick_node_utils.c index 6f50b5a..f5a6330 100644 --- a/srcs/parsing/tokenizer/unstick_node_utils.c +++ b/srcs/parsing/tokenizer/unstick_node_utils.c @@ -68,27 +68,35 @@ int unic(int *meta) return (1); } +void update_quote(char *quote, char cmp) +{ + if (*quote == 0 && (cmp == '"' || cmp == '\'')) + *quote = cmp; + else if (*quote == cmp) + *quote = 0; + return ; +} + int is_sticked(char *val) { - int i; - int meta[100]; - int meta_it; - int unmeta; + int i; + int meta[1000]; + int meta_it; + int unmeta; + char quote; - i = 0; + i = -1; meta_it = 0; meta[0] = -1; unmeta = 0; - while (val[i]) + quote = 0; + while (val[++i]) { - if (is_meta(val[i])) - { - meta[meta_it] = val[i]; - meta_it++; - } + update_quote("e, val[i]); + if (is_meta(val[i]) && !quote) + meta[meta_it++] = val[i]; if (!is_meta(val[i])) unmeta = 1; - i++; } meta[meta_it] = -1; if ((meta[0] != -1 && unmeta) || !unic(meta) || (meta[0] == '('