expansions refactoring WIP

This commit is contained in:
LuckyLaszlo
2021-12-15 13:42:41 +01:00
parent 5c1d8f527c
commit e5f033694b
4 changed files with 69 additions and 65 deletions

View File

@@ -6,13 +6,13 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/07 02:01:33 by lperrey #+# #+# */
/* Updated: 2021/12/05 18:14:24 by lperrey ### ########.fr */
/* Updated: 2021/12/15 13:33:18 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static t_list *ret_parameter_expansion(char *content, int *i);
static char *env_var_expansion(char *content, int *i);
t_list *expand_token(char *content)
{
@@ -30,13 +30,20 @@ t_list *expand_token(char *content)
{
if (content[i] == '$')
{
expand->next = ret_parameter_expansion(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
{
@@ -65,41 +72,35 @@ t_list *expand_token(char *content)
return (head.next);
}
// a voir si je retourne pas plutot un "char *".
// Malloc la lst dans la fonction est peut-être un peu superflu et pas super clair.
static t_list *ret_parameter_expansion(char *content, int *i)
static char *env_var_expansion(char *content, int *i)
{
t_list *expand;
char *expansion;
char *tmp;
int i_tmp;
int i_exp;
tmp = ft_calloc(ft_strlen(&content[*i]) + 1, 1);
if (!tmp)
return (NULL);
expand = ft_lstnew(NULL);
if (!expand)
return (ft_retp_free(NULL, tmp, free));
(*i)++; // skip '$'
if (content[*i] == '?')
{
(*i)++;
expand->content = ft_itoa(get_last_exit_status());
return (ft_retp_free(expand, tmp, free));
expansion = ft_itoa(get_last_exit_status());
return (expansion);
}
else if (content[*i] != '_' && !ft_isalpha(content[*i]))
{
tmp[0] = '$';
expand->content = tmp;
return (expand);
}
i_tmp = 0;
expansion = ft_calloc(ft_strlen(&content[*i - 1]) + 1, 1); // - 1 pour le premier skip
if (!expansion)
return (NULL);
expansion[0] = '$';
if (content[*i] != '_' && !ft_isalpha(content[*i]))
return (expansion);
i_exp = 0;
while (content[*i] == '_' || ft_isalnum(content[*i]))
tmp[i_tmp++] = content[(*i)++];
expand->content = getenv(tmp);
free(tmp);
if (expand->content)
expand->content = ft_strdup(expand->content);
return (expand);
expansion[i_exp++] = content[(*i)++];
tmp = getenv(expansion);
ft_free_null(&expansion);
if (tmp)
expansion = ft_strdup(tmp);
else // fix zob, a mieux faire
expansion = ft_calloc(1, 1);; //
return (expansion);
}
/*