words_expansions() complete

+ TODO : need refactoring and fix to valgrind invalid read size
+ redirections() WIP
+ Generic ft_dup_2d_arr(), ft_split_quotes(), ft_strdup_quotes()
+ shell_loop() continue on error
+ various small fix
This commit is contained in:
LuckyLaszlo
2021-11-14 00:09:42 +01:00
parent 86707f9fc6
commit 106af37b58
15 changed files with 653 additions and 51 deletions

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/07 02:01:33 by lperrey #+# #+# */
/* Updated: 2021/11/07 04:36:12 by lperrey ### ########.fr */
/* Updated: 2021/11/11 21:04:41 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -21,9 +21,9 @@ enum e_in_quote_state
IN_DQUOTES = '\"'
};
static t_list *ret_parameter_expansion(t_token *t, int *i);
static t_list *ret_parameter_expansion(char *content, int *i);
t_list *expand_token(t_token *t)
t_list *expand_token(char *content)
{
int in_quotes;
int i;
@@ -35,11 +35,11 @@ t_list *expand_token(t_token *t)
i = 0;
head.next = NULL;
expand = &head;
while (t->content[i])
while (content[i])
{
if (t->content[i] == '$')
if (content[i] == '$')
{
expand->next = ret_parameter_expansion(t, &i);
expand->next = ret_parameter_expansion(content, &i);
expand = expand->next;
if (!expand)
{//todo wrap
@@ -49,7 +49,7 @@ t_list *expand_token(t_token *t)
}
else
{
expand->next = ft_lstnew_generic(sizeof(t_list), ft_strlen(&t->content[i]) + 1);
expand->next = ft_lstnew_generic(sizeof(t_list), ft_strlen(&content[i]) + 1);
expand = expand->next;
i_exp = 0;
if (!expand)
@@ -57,17 +57,17 @@ t_list *expand_token(t_token *t)
perror("expand_token() error");
return (ft_lstclear(&head.next, free));
}
while (t->content[i] && (t->content[i] != '$' || in_quotes == IN_QUOTES))
while (content[i] && (content[i] != '$' || in_quotes == IN_QUOTES))
{
// quoting
if (t->content[i] == '\'' && in_quotes != IN_DQUOTES)
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++] = t->content[i++];
((char *)expand->content)[i_exp++] = content[i++];
}
}
}
@@ -76,35 +76,34 @@ t_list *expand_token(t_token *t)
// 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.
// et aussi changer "t_token *t" en "char *content", inutile d'avoir le token entier.
static t_list *ret_parameter_expansion(t_token *t, int *i)
static t_list *ret_parameter_expansion(char *content, int *i)
{
t_list *expand;
char *tmp;
int i_tmp;
tmp = ft_calloc(ft_strlen(&t->content[*i]) + 1, 1);
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 (t->content[*i] == '?')
if (content[*i] == '?')
{
(*i)++;
expand->content = ft_itoa(g_all->last_exit_status);
return (ft_retp_free(expand, tmp, free));
}
else if (t->content[*i] != '_' && !ft_isalpha(t->content[*i]))
else if (content[*i] != '_' && !ft_isalpha(content[*i]))
{
tmp[0] = '$';
expand->content = tmp;
return (expand);
}
i_tmp = 0;
while (t->content[*i] == '_' || ft_isalnum(t->content[*i]))
tmp[i_tmp++] = t->content[(*i)++];
while (content[*i] == '_' || ft_isalnum(content[*i]))
tmp[i_tmp++] = content[(*i)++];
expand->content = getenv(tmp);
free(tmp);
if (expand->content)