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> +#+ +:+ +#+ */ /* 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/05 18:14:24 by lperrey ### ########.fr */ /* Updated: 2021/12/15 13:33:18 by lperrey ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #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) t_list *expand_token(char *content)
{ {
@@ -30,13 +30,20 @@ t_list *expand_token(char *content)
{ {
if (content[i] == '$') if (content[i] == '$')
{ {
expand->next = ret_parameter_expansion(content, &i); expand->next = ft_lstnew(NULL);
expand = expand->next; expand = expand->next;
if (!expand) if (!expand)
{//todo wrap {//todo wrap
perror("expand_token() error"); perror("expand_token() error");
return (ft_lstclear(&head.next, free)); 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
{ {
@@ -65,41 +72,35 @@ t_list *expand_token(char *content)
return (head.next); return (head.next);
} }
// a voir si je retourne pas plutot un "char *". static char *env_var_expansion(char *content, int *i)
// 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)
{ {
t_list *expand; char *expansion;
char *tmp; 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 '$' (*i)++; // skip '$'
if (content[*i] == '?') if (content[*i] == '?')
{ {
(*i)++; (*i)++;
expand->content = ft_itoa(get_last_exit_status()); expansion = ft_itoa(get_last_exit_status());
return (ft_retp_free(expand, tmp, free)); return (expansion);
} }
else if (content[*i] != '_' && !ft_isalpha(content[*i])) expansion = ft_calloc(ft_strlen(&content[*i - 1]) + 1, 1); // - 1 pour le premier skip
{ if (!expansion)
tmp[0] = '$'; return (NULL);
expand->content = tmp; expansion[0] = '$';
return (expand); if (content[*i] != '_' && !ft_isalpha(content[*i]))
} return (expansion);
i_tmp = 0; i_exp = 0;
while (content[*i] == '_' || ft_isalnum(content[*i])) while (content[*i] == '_' || ft_isalnum(content[*i]))
tmp[i_tmp++] = content[(*i)++]; expansion[i_exp++] = content[(*i)++];
expand->content = getenv(tmp); tmp = getenv(expansion);
free(tmp); ft_free_null(&expansion);
if (expand->content) if (tmp)
expand->content = ft_strdup(expand->content); expansion = ft_strdup(tmp);
return (expand); 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> +#+ +:+ +#+ */ /* 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/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) if (!tmp_split)
return (0); return (0);
// 4 // 4
tmp = (char**)ft_dup_2d_arr(tmp_split, (t_dup_f)ft_strdup_quotes); tmp = tmp_split;
ft_free_2d_arr(tmp_split); tmp_split = ft_dup_2d_arr(tmp_split, (t_dup_f)ft_strdup_quotes);
tmp_split = tmp; ft_free_2d_arr(tmp);
if (!tmp_split) if (!tmp_split)
return (0); return (0);
// 5 // 5

View File

@@ -6,41 +6,14 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */ /* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/24 10:52:40 by lperrey #+# #+# */ /* 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" #include "minishell.h"
void save_redirections_words(t_token *t) void save_redirections_words(t_token *t);
{ void print_pipeline(t_cmd **pipeline);
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);
}
}
t_cmd **parsing(t_token *token_list) t_cmd **parsing(t_token *token_list)
{ {
@@ -80,6 +53,36 @@ t_cmd **parsing(t_token *token_list)
return (pipeline); 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 The grammar symbols
------------------------------------------------------- */ ------------------------------------------------------- */

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */ /* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/11 18:46:43 by lperrey #+# #+# */ /* 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) if (ft_lstsize((t_list *)head->next) != 1)
{ {
ret = 0; 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; ((t_token *)ft_lstlast((t_list *)head))->next = next_token;
return (ret); return (ret);