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);
}
/*

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/07 02:01:33 by lperrey #+# #+# */
/* Updated: 2021/12/11 20:10:37 by lperrey ### ########.fr */
/* Updated: 2021/12/15 00:20:47 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -70,9 +70,9 @@ int token_expansions(t_token *t)
if (!tmp_split)
return (0);
// 4
tmp = (char**)ft_dup_2d_arr(tmp_split, (t_dup_f)ft_strdup_quotes);
ft_free_2d_arr(tmp_split);
tmp_split = tmp;
tmp = tmp_split;
tmp_split = ft_dup_2d_arr(tmp_split, (t_dup_f)ft_strdup_quotes);
ft_free_2d_arr(tmp);
if (!tmp_split)
return (0);
// 5

View File

@@ -6,41 +6,14 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/24 10:52:40 by lperrey #+# #+# */
/* Updated: 2021/12/10 19:58:17 by lperrey ### ########.fr */
/* Updated: 2021/12/12 21:18:52 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void save_redirections_words(t_token *t)
{
while (t)
{
if (t->id == '>' || t->id == T_DGREAT
|| t->id == '<' || t->id == T_DLESS)
{
t = t->next;
t->id = T_REDIRECTION_WORD;
}
t = t->next;
}
}
void print_pipeline(t_cmd **pipeline)
{
int i;
i = 0;
while (pipeline[i])
{
printf("CMD %i, fd_in=%i, fd_out=%i\n", i, pipeline[i]->fd_in, pipeline[i]->fd_out);
ft_putstr_fd(" |", 1);
print_matrix(pipeline[i]->argv, "|\n |");
i++;
if (pipeline[i])
ft_putstr_fd("----------------\n", 1);
}
}
void save_redirections_words(t_token *t);
void print_pipeline(t_cmd **pipeline);
t_cmd **parsing(t_token *token_list)
{
@@ -80,6 +53,36 @@ t_cmd **parsing(t_token *token_list)
return (pipeline);
}
void save_redirections_words(t_token *t)
{
while (t)
{
if (t->id == '>' || t->id == T_DGREAT
|| t->id == '<' || t->id == T_DLESS)
{
t = t->next;
t->id = T_REDIRECTION_WORD;
}
t = t->next;
}
}
void print_pipeline(t_cmd **pipeline)
{
int i;
i = 0;
while (pipeline[i])
{
printf("CMD %i, fd_in=%i, fd_out=%i\n", i, pipeline[i]->fd_in, pipeline[i]->fd_out);
ft_putstr_fd(" |", 1);
print_matrix(pipeline[i]->argv, "|\n |");
i++;
if (pipeline[i])
ft_putstr_fd("----------------\n", 1);
}
}
/* -------------------------------------------------------
The grammar symbols
------------------------------------------------------- */

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/11 18:46:43 by lperrey #+# #+# */
/* Updated: 2021/12/11 21:15:03 by lperrey ### ########.fr */
/* Updated: 2021/12/11 21:32:31 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -130,7 +130,7 @@ static int expand_redirection(t_token *t)
if (ft_lstsize((t_list *)head->next) != 1)
{
ret = 0;
ft_putstr_fd("minishell: ambiguous redirect\n", STDERR_FILENO); // tmp message
shell_error("", "", "ambiguous redirect", 0);
}
((t_token *)ft_lstlast((t_list *)head))->next = next_token;
return (ret);