expansions refactoring done
This commit is contained in:
@@ -6,91 +6,115 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/11/07 02:01:33 by lperrey #+# #+# */
|
/* Created: 2021/11/07 02:01:33 by lperrey #+# #+# */
|
||||||
/* Updated: 2021/12/15 13:33:18 by lperrey ### ########.fr */
|
/* Updated: 2021/12/16 03:03:27 by lperrey ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
static char *env_var_expansion(char *content, int *i);
|
static t_list *content_copy(char *content, int *i, int *quotes_state);
|
||||||
|
static t_list *content_expand(char *content, int *i);
|
||||||
|
static char *env_var_expansion(char *content, int *i);
|
||||||
|
|
||||||
t_list *expand_token(char *content)
|
t_list *expand_token(char *content)
|
||||||
{
|
{
|
||||||
int in_quotes;
|
int quotes_state;
|
||||||
int i;
|
int i;
|
||||||
int i_exp;
|
|
||||||
t_list head;
|
t_list head;
|
||||||
t_list *expand;
|
t_list *expand;
|
||||||
|
|
||||||
in_quotes = 0;
|
|
||||||
i = 0;
|
|
||||||
head.next = NULL;
|
head.next = NULL;
|
||||||
expand = &head;
|
expand = &head;
|
||||||
|
quotes_state = 0;
|
||||||
|
i = 0;
|
||||||
while (content[i])
|
while (content[i])
|
||||||
{
|
{
|
||||||
if (content[i] == '$')
|
if (content[i] == '$')
|
||||||
{
|
expand->next = content_expand(content, &i);
|
||||||
expand->next = ft_lstnew(NULL);
|
|
||||||
expand = expand->next;
|
|
||||||
if (!expand)
|
|
||||||
{//todo wrap
|
|
||||||
perror("expand_token() error");
|
|
||||||
return (ft_lstclear(&head.next, free));
|
|
||||||
}
|
|
||||||
|
|
||||||
expand->content = env_var_expansion(content, &i);
|
|
||||||
if (!expand->content)
|
|
||||||
{//todo wrap
|
|
||||||
perror("expand_token() error");
|
|
||||||
return (ft_lstclear(&head.next, free));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
expand->next = content_copy(content, &i, "es_state);
|
||||||
expand->next = ft_lstnew_generic(sizeof(t_list), ft_strlen(&content[i]) + 1);
|
expand = expand->next;
|
||||||
expand = expand->next;
|
if (!expand)
|
||||||
i_exp = 0;
|
{//todo wrap
|
||||||
if (!expand)
|
perror("expand_token() error");
|
||||||
{//todo wrap
|
return (ft_lstclear(&head.next, free));
|
||||||
perror("expand_token() error");
|
|
||||||
return (ft_lstclear(&head.next, free));
|
|
||||||
}
|
|
||||||
while (content[i] && (content[i] != '$' || in_quotes == IN_QUOTES))
|
|
||||||
{
|
|
||||||
// quoting
|
|
||||||
if (content[i] == '\'' && in_quotes != IN_DQUOTES)
|
|
||||||
{
|
|
||||||
if (in_quotes == IN_QUOTES)
|
|
||||||
in_quotes = 0;
|
|
||||||
else
|
|
||||||
in_quotes = IN_QUOTES;
|
|
||||||
}
|
|
||||||
((char *)expand->content)[i_exp++] = content[i++];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (head.next);
|
return (head.next);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static t_list *content_copy(char *content, int *i, int *quotes_state)
|
||||||
|
{
|
||||||
|
int i_exp;
|
||||||
|
t_list *expand;
|
||||||
|
|
||||||
|
expand = ft_lstnew_generic(sizeof(t_list), ft_strlen(&content[*i]) + 1);
|
||||||
|
if (!expand)
|
||||||
|
return (NULL);
|
||||||
|
i_exp = 0;
|
||||||
|
while (content[*i] && (content[*i] != '$' || *quotes_state == IN_QUOTES))
|
||||||
|
{
|
||||||
|
if (content[*i] == '\'' && *quotes_state != IN_DQUOTES)
|
||||||
|
{
|
||||||
|
if (*quotes_state == IN_QUOTES)
|
||||||
|
*quotes_state = 0;
|
||||||
|
else
|
||||||
|
*quotes_state = IN_QUOTES;
|
||||||
|
}
|
||||||
|
((char *)expand->content)[i_exp++] = content[(*i)++];
|
||||||
|
}
|
||||||
|
return (expand);
|
||||||
|
}
|
||||||
|
|
||||||
|
static t_list *content_expand(char *content, int *i)
|
||||||
|
{
|
||||||
|
t_list *expand;
|
||||||
|
|
||||||
|
expand = ft_lstnew(NULL);
|
||||||
|
if (!expand)
|
||||||
|
return (NULL);
|
||||||
|
expand->content = env_var_expansion(content, i);
|
||||||
|
if (!expand->content)
|
||||||
|
{
|
||||||
|
free(expand);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
return (expand);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *retrieve_var(char *content, int *i);
|
||||||
|
|
||||||
static char *env_var_expansion(char *content, int *i)
|
static char *env_var_expansion(char *content, int *i)
|
||||||
{
|
{
|
||||||
char *expansion;
|
char *expansion;
|
||||||
char *tmp;
|
|
||||||
int i_exp;
|
|
||||||
|
|
||||||
(*i)++; // skip '$'
|
(*i)++; // skip '$'
|
||||||
if (content[*i] == '?')
|
if (content[*i] == '?')
|
||||||
{
|
{
|
||||||
(*i)++;
|
(*i)++;
|
||||||
expansion = ft_itoa(get_last_exit_status());
|
expansion = ft_itoa(get_last_exit_status());
|
||||||
return (expansion);
|
|
||||||
}
|
}
|
||||||
expansion = ft_calloc(ft_strlen(&content[*i - 1]) + 1, 1); // - 1 pour le premier skip
|
else if (content[*i] == '_' || ft_isalpha(content[*i]))
|
||||||
|
expansion = retrieve_var(content, i);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
expansion = ft_calloc(1 + 1, 1);
|
||||||
|
if (!expansion)
|
||||||
|
return (NULL);
|
||||||
|
expansion[0] = '$';
|
||||||
|
}
|
||||||
|
return (expansion);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *retrieve_var(char *content, int *i)
|
||||||
|
{
|
||||||
|
char *expansion;
|
||||||
|
char *tmp;
|
||||||
|
int i_exp;
|
||||||
|
|
||||||
|
expansion = ft_calloc(ft_strlen(&content[*i - 1]) + 1, 1); // *i - 1 for '$' skip
|
||||||
if (!expansion)
|
if (!expansion)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
expansion[0] = '$';
|
|
||||||
if (content[*i] != '_' && !ft_isalpha(content[*i]))
|
|
||||||
return (expansion);
|
|
||||||
i_exp = 0;
|
i_exp = 0;
|
||||||
while (content[*i] == '_' || ft_isalnum(content[*i]))
|
while (content[*i] == '_' || ft_isalnum(content[*i]))
|
||||||
expansion[i_exp++] = content[(*i)++];
|
expansion[i_exp++] = content[(*i)++];
|
||||||
@@ -98,8 +122,8 @@ static char *env_var_expansion(char *content, int *i)
|
|||||||
ft_free_null(&expansion);
|
ft_free_null(&expansion);
|
||||||
if (tmp)
|
if (tmp)
|
||||||
expansion = ft_strdup(tmp);
|
expansion = ft_strdup(tmp);
|
||||||
else // fix zob, a mieux faire
|
else
|
||||||
expansion = ft_calloc(1, 1);; //
|
expansion = ft_calloc(1, 1);
|
||||||
return (expansion);
|
return (expansion);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user