quote done

This commit is contained in:
gazhonsepaskwa
2025-02-17 15:52:35 +01:00
parent a9cffbe6e3
commit 5826dfd43e
3 changed files with 89 additions and 98 deletions

View File

@@ -86,6 +86,7 @@ void ft_error(char *e);
int ft_printf(const char *fstr, ...); int ft_printf(const char *fstr, ...);
int ft_fprintf(int fd, const char *str, ...); int ft_fprintf(int fd, const char *str, ...);
char *ft_sprintf(const char *str, ...); char *ft_sprintf(const char *str, ...);
char *ft_strfjoin(char *s1, char *s2);
int ft_debug(const char *fstr, ...); int ft_debug(const char *fstr, ...);
char *rep_c(char c, int count); char *rep_c(char c, int count);

View File

@@ -11,76 +11,92 @@
/* ************************************************************************** */ /* ************************************************************************** */
#include "../../../includes/minishell.h" #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) static t_node *tokenize_base(char *str)
{ {
t_node *lst;
char *tmp;
int i; int i;
t_node *head; int len;
char **tab;
tab = ft_split_keep(str, " \t\n"); lst = NULL;
if (!tab) tmp = NULL;
return (NULL);
head = NULL;
i = 0; i = 0;
while (tab[i]) while (str[i])
{ {
if (!add_node_back(&head, tab[i], 0, 0)) len = goto_nxt_space(&(str[i]));
return (free_tab(tab), NULL); tmp = ft_substr(&str[i], 0, len);
i++; add_node_back(&lst, tmp, 0, 0);
ft_free(&tmp);
i += len;
} }
free_tab(tab); return (lst);
return (head);
}
static int unstick_nodes(t_node *head)
{
t_node *it;
char *first_str;
char *second_str;
int copied;
it = head;
while (it != NULL)
{
if (is_sticked(it->val))
{
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);
}
it = it->next;
}
return (1);
}
static int stick_quote_node(t_node *head, char q)
{
t_node *it;
it = head;
while (it != NULL)
{
if (ft_strchr(it->val, q) && ft_strchr(it->val,
q) == ft_strrchr(it->val, q))
{
while (it->next && !ft_strchr(it->next->val, q))
if (!merge_with_next_node(it))
return (0);
if (it->next && !merge_with_next_node(it))
return (0);
}
it = it->next;
}
return (1);
} }
static void del_void_nodes(t_node **head) static void del_void_nodes(t_node **head)
@@ -116,18 +132,15 @@ t_node *tokenize(char *str)
head = tokenize_base(str); head = tokenize_base(str);
if (!head) if (!head)
return (NULL); return (NULL);
debug_token_list(head, "base");
if (!trim_nodes(head)) if (!trim_nodes(head))
return (NULL); return (NULL);
if (!unstick_nodes(head)) debug_token_list(head, "trim");
return (NULL);
stick_quote_node(head, 39);
stick_quote_node(head, '"');
set_token(head);
if (!trim_nodes(head))
return (NULL);
del_void_nodes(&head); del_void_nodes(&head);
debug_token_list(head, "tokenizer"); debug_token_list(head, "del void");
if (syntax_error(head)) set_token(head);
return (free_linked_list(head), NULL); debug_token_list(head, "set token");
// if (syntax_error(head))
// return (free_linked_list(head), NULL);
return (head); return (head);
} }

View File

@@ -20,19 +20,6 @@ int is_meta(char c)
return (0); return (0);
} }
int ft_str_count(char *s, char c)
{
int i;
int count;
i = -1;
count = 0;
while (s[++i])
if (s[i] == c)
count++;
return (count);
}
int trim_nodes(t_node *head) int trim_nodes(t_node *head)
{ {
t_node *it; t_node *it;
@@ -43,19 +30,9 @@ int trim_nodes(t_node *head)
in_quote = 0; in_quote = 0;
while (it != NULL) while (it != NULL)
{ {
if (ft_str_count(it->val, 39) == 1 || ft_str_count(it->val, '"') == 1) tmp = ft_strtrim(it->val, " \t\n");
{ free(it->val);
if (!in_quote) it->val = tmp;
in_quote = it->val[0];
else if (it->val[0] == in_quote)
in_quote = 0;
}
if (!in_quote)
{
tmp = ft_strtrim(it->val, " \t\n");
free(it->val);
it->val = tmp;
}
it = it->next; it = it->next;
} }
return (1); return (1);