commit pour merge
This commit is contained in:
@@ -6,100 +6,125 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/11/07 02:01:33 by lperrey #+# #+# */
|
||||
/* Updated: 2021/12/10 10:27:50 by hulamy ### ########.fr */
|
||||
/* Updated: 2021/12/16 14:51:33 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
static t_list *ret_parameter_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)
|
||||
{
|
||||
int in_quotes;
|
||||
int quotes_state;
|
||||
int i;
|
||||
int i_exp;
|
||||
t_list head;
|
||||
t_list *expand;
|
||||
|
||||
in_quotes = 0;
|
||||
i = 0;
|
||||
head.next = NULL;
|
||||
expand = &head;
|
||||
quotes_state = 0;
|
||||
i = 0;
|
||||
while (content[i])
|
||||
{
|
||||
if (content[i] == '$')
|
||||
{
|
||||
expand->next = ret_parameter_expansion(content, &i);
|
||||
expand = expand->next;
|
||||
if (!expand)
|
||||
{//todo wrap
|
||||
perror("expand_token() error");
|
||||
return (ft_lstclear(&head.next, free));
|
||||
}
|
||||
}
|
||||
expand->next = content_expand(content, &i);
|
||||
else
|
||||
{
|
||||
expand->next = ft_lstnew_generic(sizeof(t_list), ft_strlen(&content[i]) + 1);
|
||||
expand = expand->next;
|
||||
i_exp = 0;
|
||||
if (!expand)
|
||||
{//todo wrap
|
||||
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++];
|
||||
}
|
||||
expand->next = content_copy(content, &i, "es_state);
|
||||
expand = expand->next;
|
||||
if (!expand)
|
||||
{//todo wrap
|
||||
perror("expand_token() error");
|
||||
return (ft_lstclear(&head.next, free));
|
||||
}
|
||||
}
|
||||
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 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;
|
||||
char *tmp;
|
||||
int i_tmp;
|
||||
|
||||
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));
|
||||
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)
|
||||
{
|
||||
char *expansion;
|
||||
|
||||
(*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());
|
||||
}
|
||||
else if (content[*i] != '_' && !ft_isalpha(content[*i]))
|
||||
else if (content[*i] == '_' || ft_isalpha(content[*i]))
|
||||
expansion = retrieve_var(content, i);
|
||||
else
|
||||
{
|
||||
tmp[0] = '$';
|
||||
expand->content = tmp;
|
||||
return (expand);
|
||||
expansion = ft_calloc(1 + 1, 1);
|
||||
if (!expansion)
|
||||
return (NULL);
|
||||
expansion[0] = '$';
|
||||
}
|
||||
i_tmp = 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)
|
||||
return (NULL);
|
||||
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
|
||||
expansion = ft_calloc(1, 1);
|
||||
return (expansion);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* words_expansions.c :+: :+: :+: */
|
||||
/* expansions.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/11/07 02:01:33 by lperrey #+# #+# */
|
||||
/* Updated: 2021/12/10 10:27:01 by hulamy ### ########.fr */
|
||||
/* Updated: 2021/12/16 14:50:58 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
t_list *expand_token(char *content);
|
||||
char *rejoin_after_expand(t_list *expand_lst);
|
||||
int new_token_for_each_field(char **fields, t_token **t);
|
||||
int new_token_for_each_field(char **fields, t_token *t);
|
||||
|
||||
// 1 - chaque bout dans un element d'une t_list
|
||||
// (telle quelle si non expand, ou VARIABLE de env)
|
||||
@@ -25,28 +25,43 @@ int new_token_for_each_field(char **fields, t_token **t);
|
||||
// (ft_lstadd_front() sur le token original, puis set le token orignal à :
|
||||
// t->id = 0 ; free(t->content) ; t->content = NULL ; pour qu'il soit ignoré sur la suite du parsing)
|
||||
|
||||
int words_expansions(t_token *token_list)
|
||||
int expansions(t_token *t, t_cmd **pipeline)
|
||||
{
|
||||
while (token_list)
|
||||
int i;
|
||||
t_token *next_token;
|
||||
|
||||
i = 0;
|
||||
while (t)
|
||||
{
|
||||
if (token_list->id == T_WORD)
|
||||
token_expansions(&token_list);
|
||||
token_list = token_list->next;
|
||||
if (t->id == '|')
|
||||
i++;
|
||||
if (!pipeline[i]->error && t->id == T_WORD)
|
||||
{
|
||||
next_token = t->next;
|
||||
if (!token_expansions(t))
|
||||
{
|
||||
pipeline[i]->error = EXIT_EXPANSION;
|
||||
}
|
||||
while (t != next_token)
|
||||
t = t->next;
|
||||
}
|
||||
else
|
||||
t = t->next;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
int token_expansions(t_token **t)
|
||||
int token_expansions(t_token *t)
|
||||
{
|
||||
void *tmp;
|
||||
char **tmp_split;
|
||||
|
||||
// 1
|
||||
tmp = expand_token((*t)->content);
|
||||
tmp = (t_list*)expand_token(t->content);
|
||||
if (!tmp)
|
||||
return (0);
|
||||
// 2
|
||||
tmp = rejoin_after_expand(tmp);
|
||||
tmp = (char*)rejoin_after_expand(tmp);
|
||||
if (!tmp)
|
||||
return (0);
|
||||
// 3
|
||||
@@ -55,9 +70,9 @@ int token_expansions(t_token **t)
|
||||
if (!tmp_split)
|
||||
return (0);
|
||||
// 4
|
||||
tmp = 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
|
||||
@@ -1,106 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_split_MODIF.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/11/13 07:08:40 by lperrey #+# #+# */
|
||||
/* Updated: 2021/11/13 20:24:33 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
// FT_SPLIT LEGEREMENT REMANIER.
|
||||
// A REMPLACER DANS LA LIBFT
|
||||
#include "libft.h"
|
||||
|
||||
static size_t count_word(char const *s, char c);
|
||||
static char **alloc_words(char const *s, char c, char **str_arr,
|
||||
size_t words_count);
|
||||
static void fill_arr(char const *s, char c, char **str_arr);
|
||||
|
||||
char **ft_split(char const *s, char c)
|
||||
{
|
||||
char **str_arr;
|
||||
size_t words_count;
|
||||
|
||||
if (s == NULL)
|
||||
return (NULL);
|
||||
words_count = count_word(s, c);
|
||||
str_arr = ft_calloc(words_count + 1, sizeof(char *));
|
||||
if (!str_arr)
|
||||
return (NULL);
|
||||
if (!(alloc_words(s, c, str_arr, words_count)))
|
||||
{
|
||||
ft_free_2d_arr(str_arr);
|
||||
return (NULL);
|
||||
}
|
||||
fill_arr(s, c, str_arr);
|
||||
return (str_arr);
|
||||
}
|
||||
|
||||
static size_t count_word(char const *s, char c)
|
||||
{
|
||||
unsigned int i;
|
||||
size_t count;
|
||||
|
||||
count = 0;
|
||||
i = 0;
|
||||
while (s[i])
|
||||
{
|
||||
while (s[i] == c)
|
||||
i++;
|
||||
if (s[i])
|
||||
count++;
|
||||
while (s[i] && s[i] != c)
|
||||
i++;
|
||||
}
|
||||
return (count);
|
||||
}
|
||||
|
||||
static char **alloc_words(char const *s, char c, char **str_arr,
|
||||
size_t words_count)
|
||||
{
|
||||
unsigned int i;
|
||||
size_t len;
|
||||
unsigned int arr_i;
|
||||
|
||||
i = 0;
|
||||
arr_i = 0;
|
||||
while (arr_i < words_count)
|
||||
{
|
||||
len = 0;
|
||||
while (s[i] == c)
|
||||
i++;
|
||||
while (s[i] && s[i] != c)
|
||||
{
|
||||
len++;
|
||||
i++;
|
||||
}
|
||||
str_arr[arr_i] = malloc(len + 1);
|
||||
if (!str_arr[arr_i])
|
||||
return (NULL);
|
||||
arr_i++;
|
||||
}
|
||||
return (str_arr);
|
||||
}
|
||||
|
||||
static void fill_arr(char const *s, char c, char **str_arr)
|
||||
{
|
||||
unsigned int i;
|
||||
unsigned int arr_i;
|
||||
unsigned int char_i;
|
||||
|
||||
i = 0;
|
||||
arr_i = 0;
|
||||
while (str_arr[arr_i])
|
||||
{
|
||||
while (s[i] == c)
|
||||
i++;
|
||||
char_i = 0;
|
||||
while (s[i] && s[i] != c)
|
||||
str_arr[arr_i][char_i++] = s[i++];
|
||||
str_arr[arr_i][char_i] = '\0';
|
||||
arr_i++;
|
||||
}
|
||||
}
|
||||
@@ -1,179 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_split_quotes.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/11/13 07:08:40 by lperrey #+# #+# */
|
||||
/* Updated: 2021/11/26 21:27:10 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
static size_t count_word(char const *s, char c);
|
||||
static char **alloc_words(char const *s, char c, char **str_arr,
|
||||
size_t words_count);
|
||||
static void fill_arr(char const *s, char c, char **str_arr);
|
||||
static int quote_state_change(int *quote_state, const char *s);
|
||||
|
||||
char **ft_split_quotes(char const *s, char c)
|
||||
{
|
||||
char **str_arr;
|
||||
size_t words_count;
|
||||
|
||||
if (s == NULL)
|
||||
return (NULL);
|
||||
words_count = count_word(s, c);
|
||||
str_arr = ft_calloc(words_count + 1, sizeof(char *));
|
||||
if (!str_arr)
|
||||
return (NULL);
|
||||
if (!(alloc_words(s, c, str_arr, words_count)))
|
||||
{
|
||||
ft_free_2d_arr(str_arr);
|
||||
return (NULL);
|
||||
}
|
||||
fill_arr(s, c, str_arr);
|
||||
return (str_arr);
|
||||
}
|
||||
|
||||
static size_t count_word(char const *s, char c)
|
||||
{
|
||||
unsigned int i;
|
||||
size_t count;
|
||||
int quote_state;
|
||||
|
||||
i = 0;
|
||||
count = 0;
|
||||
quote_state = 0;
|
||||
while (s[i])
|
||||
{
|
||||
while (s[i] == c)
|
||||
i++;
|
||||
if (s[i])
|
||||
count++;
|
||||
while (s[i] && (s[i] != c || quote_state))
|
||||
{
|
||||
while (quote_state_change("e_state, &s[i]))
|
||||
i++;
|
||||
if (s[i] && (s[i] != c || quote_state))
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return (count);
|
||||
}
|
||||
|
||||
static char **alloc_words(char const *s, char c, char **str_arr,
|
||||
size_t words_count)
|
||||
{
|
||||
unsigned int i;
|
||||
size_t len;
|
||||
unsigned int arr_i;
|
||||
int quote_state;
|
||||
|
||||
i = 0;
|
||||
arr_i = 0;
|
||||
quote_state = 0;
|
||||
while (arr_i < words_count)
|
||||
{
|
||||
len = 0;
|
||||
while (s[i] == c)
|
||||
i++;
|
||||
while (s[i + len]
|
||||
&& (quote_state_change("e_state, &s[i + len])
|
||||
|| (s[i + len] != c || quote_state)))
|
||||
len++;
|
||||
i = i + len;
|
||||
str_arr[arr_i] = ft_calloc(len + 1, 1);
|
||||
if (!str_arr[arr_i])
|
||||
return (NULL);
|
||||
arr_i++;
|
||||
}
|
||||
return (str_arr);
|
||||
}
|
||||
|
||||
// Plus clair, plus de 25 lignes :(
|
||||
/* static char **alloc_words(char const *s, char c, char **str_arr,
|
||||
size_t words_count)
|
||||
{
|
||||
unsigned int i;
|
||||
size_t len;
|
||||
unsigned int arr_i;
|
||||
int quote_state;
|
||||
|
||||
i = 0;
|
||||
arr_i = 0;
|
||||
quote_state = 0;
|
||||
while (arr_i < words_count)
|
||||
{
|
||||
len = 0;
|
||||
while (s[i] == c)
|
||||
i++;
|
||||
while (s[i + len] && (s[i + len] != c || quote_state))
|
||||
{
|
||||
while (quote_state_change("e_state, &s[i + len]))
|
||||
len++;
|
||||
if (s[i + len] != c || quote_state)
|
||||
len++;
|
||||
}
|
||||
i = i + len;
|
||||
str_arr[arr_i] = ft_calloc(len + 1, 1);
|
||||
if (!str_arr[arr_i])
|
||||
return (NULL);
|
||||
arr_i++;
|
||||
}
|
||||
return (str_arr);
|
||||
} */
|
||||
|
||||
static void fill_arr(char const *s, char c, char **str_arr)
|
||||
{
|
||||
unsigned int i;
|
||||
unsigned int arr_i;
|
||||
unsigned int char_i;
|
||||
int quote_state;
|
||||
|
||||
i = 0;
|
||||
arr_i = 0;
|
||||
quote_state = 0;
|
||||
while (str_arr[arr_i])
|
||||
{
|
||||
while (s[i] == c)
|
||||
i++;
|
||||
char_i = 0;
|
||||
while (s[i] && (s[i] != c || quote_state))
|
||||
{
|
||||
while (quote_state_change("e_state, &s[i]))
|
||||
str_arr[arr_i][char_i++] = s[i++];
|
||||
if (s[i] && (s[i] != c || quote_state))
|
||||
str_arr[arr_i][char_i++] = s[i++];
|
||||
}
|
||||
str_arr[arr_i][char_i] = '\0'; //superflu si ft_calloc
|
||||
arr_i++;
|
||||
}
|
||||
}
|
||||
|
||||
static int quote_state_change(int *quote_state, const char *s)
|
||||
{
|
||||
if (s[0] == '\'' && *quote_state != IN_DQUOTES)
|
||||
{
|
||||
if (*quote_state == IN_QUOTES)
|
||||
*quote_state = 0;
|
||||
else if (ft_strchr(&s[1], '\'')) // if closed quotes
|
||||
*quote_state = IN_QUOTES;
|
||||
else
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
else if (s[0] == '\"' && *quote_state != IN_QUOTES)
|
||||
{
|
||||
if (*quote_state == IN_DQUOTES)
|
||||
*quote_state = 0;
|
||||
else if (ft_strchr(&s[1], '\"')) // if closed quotes
|
||||
*quote_state = IN_DQUOTES;
|
||||
else
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strdup_quotes.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/11/13 04:35:06 by lperrey #+# #+# */
|
||||
/* Updated: 2021/11/26 21:27:05 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
static int quote_state_change(int *quote_state, const char *s);
|
||||
|
||||
/* Duplicate a string minus the quoting characters ['] and ["]*/
|
||||
char *ft_strdup_quotes(const char *s)
|
||||
{
|
||||
unsigned int i;
|
||||
unsigned int i_dup;
|
||||
char *dup;
|
||||
int quote_state;
|
||||
|
||||
dup = ft_calloc(ft_strlen(s) + 1, 1);
|
||||
if (!dup)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
i_dup = 0;
|
||||
quote_state = 0;
|
||||
while (s[i])
|
||||
{
|
||||
while (quote_state_change("e_state, &s[i]))
|
||||
i++;
|
||||
if (s[i])
|
||||
dup[i_dup++] = s[i++];
|
||||
}
|
||||
return (dup);
|
||||
}
|
||||
|
||||
static int quote_state_change(int *quote_state, const char *s)
|
||||
{
|
||||
if (s[0] == '\'' && *quote_state != IN_DQUOTES)
|
||||
{
|
||||
if (*quote_state == IN_QUOTES)
|
||||
*quote_state = 0;
|
||||
else if (ft_strchr(&s[1], '\'')) // if closed quotes
|
||||
*quote_state = IN_QUOTES;
|
||||
else
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
else if (s[0] == '\"' && *quote_state != IN_QUOTES)
|
||||
{
|
||||
if (*quote_state == IN_DQUOTES)
|
||||
*quote_state = 0;
|
||||
else if (ft_strchr(&s[1], '\"')) // if closed quotes
|
||||
*quote_state = IN_DQUOTES;
|
||||
else
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
@@ -6,15 +6,15 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/11/07 02:01:33 by lperrey #+# #+# */
|
||||
/* Updated: 2021/12/10 10:27:26 by hulamy ### ########.fr */
|
||||
/* Updated: 2021/12/16 14:50:36 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
static t_token *insert_tokens(t_token *t, t_token *insert_lst);
|
||||
static void insert_tokens(t_token *t, t_token *insert_lst);
|
||||
|
||||
int new_token_for_each_field(char **fields, t_token **t)
|
||||
int new_token_for_each_field(char **fields, t_token *t)
|
||||
{
|
||||
t_token head;
|
||||
t_token *insert_lst;
|
||||
@@ -38,11 +38,11 @@ int new_token_for_each_field(char **fields, t_token **t)
|
||||
i++;
|
||||
}
|
||||
free(fields);
|
||||
*t = insert_tokens(*t, head.next);
|
||||
insert_tokens(t, head.next);
|
||||
return (1);
|
||||
}
|
||||
|
||||
static t_token *insert_tokens(t_token *t, t_token *insert_lst)
|
||||
static void insert_tokens(t_token *t, t_token *insert_lst)
|
||||
{
|
||||
t_token *tmp;
|
||||
t_token *insert_lst_last;
|
||||
@@ -50,12 +50,12 @@ static t_token *insert_tokens(t_token *t, t_token *insert_lst)
|
||||
t->id = 0;
|
||||
ft_free_null(&t->content);
|
||||
if (!insert_lst)
|
||||
return (t);
|
||||
return ;
|
||||
|
||||
tmp = t->next;
|
||||
t->next = insert_lst;
|
||||
insert_lst_last = (t_token *)ft_lstlast((t_list *)insert_lst);
|
||||
insert_lst_last->next = tmp;
|
||||
|
||||
return (insert_lst_last);
|
||||
//return (insert_lst_last); // return inutile pour reusinage
|
||||
}
|
||||
|
||||
@@ -6,12 +6,39 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/24 10:52:40 by lperrey #+# #+# */
|
||||
/* Updated: 2021/12/10 10:26:31 by hulamy ### ########.fr */
|
||||
/* Updated: 2021/12/16 14:50:16 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
void save_redirections_words(t_token *t);
|
||||
|
||||
t_cmd **parsing(t_token *token_list)
|
||||
{
|
||||
t_cmd **pipeline;
|
||||
|
||||
if (!valid_syntax(token_list))
|
||||
return (NULL);
|
||||
|
||||
// 2.9.1 - 1) Save Words
|
||||
save_redirections_words(token_list);
|
||||
// Struct CMD alloc
|
||||
pipeline = pipeline_alloc(1 + count_pipes(token_list));
|
||||
if (!pipeline)
|
||||
return (NULL);
|
||||
// 2.9.1 - 2) Expansion
|
||||
if (!expansions(token_list, pipeline))
|
||||
return (ft_retp_free(NULL, &pipeline, (t_free_f)free_pipeline));
|
||||
// 2.9.1 - 3) Redirection
|
||||
if (!redirections(token_list, pipeline))
|
||||
return (ft_retp_free(NULL, &pipeline, (t_free_f)free_pipeline));
|
||||
// Struct CMD fill
|
||||
if (!pipeline_fill_argv(token_list, pipeline))
|
||||
return (ft_retp_free(NULL, &pipeline, (t_free_f)free_pipeline));
|
||||
return (pipeline);
|
||||
}
|
||||
|
||||
void save_redirections_words(t_token *t)
|
||||
{
|
||||
while (t)
|
||||
@@ -26,62 +53,20 @@ void save_redirections_words(t_token *t)
|
||||
}
|
||||
}
|
||||
|
||||
void print_pipeline(t_cmd **pipeline)
|
||||
{
|
||||
int i;
|
||||
/*
|
||||
2.9.1 Simple Commands
|
||||
2.9.1 - 1) Save Words
|
||||
2.9.1 - 2) Expansion
|
||||
2.9.1 - 3) Redirection
|
||||
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
|
||||
#tag_18_09_01
|
||||
*/
|
||||
|
||||
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 **pipeline;
|
||||
|
||||
if (!valid_syntax(token_list))
|
||||
return (NULL);
|
||||
|
||||
// 2.9.1 - 1) Save Words
|
||||
save_redirections_words(token_list);
|
||||
|
||||
// 2.9.1 - 2) Expansion
|
||||
// TEST TOKENS PRINT
|
||||
//ft_putstr_fd("TOKENS LIST :\n-----------\n", 1);
|
||||
//ft_lstprint((t_list *)token_list, 1);
|
||||
//
|
||||
// getenv() ne va pas fonctionner avec le changement d'environnement prévu jusqu'ici.
|
||||
// TODO : Revoir le changement d'environnement (avec extern char **environ ?)
|
||||
// OU Integrer un equivalent perso comme dans pipex
|
||||
if (!words_expansions(token_list))
|
||||
return (NULL);
|
||||
//
|
||||
//ft_putstr_fd("TOKENS LIST EXPANDED :\n-----------\n", 1);
|
||||
//ft_lstprint((t_list *)token_list, 1);
|
||||
|
||||
// Struct CMD alloc
|
||||
pipeline = pipeline_alloc(1 + count_pipes(token_list));
|
||||
if (!pipeline)
|
||||
return (NULL);
|
||||
|
||||
// 2.9.1 - 3) Redirection
|
||||
if (!redirections(token_list, pipeline))
|
||||
return (ft_retp_free(NULL, &pipeline, (t_free_f)free_pipeline));
|
||||
|
||||
// Struct CMD fill
|
||||
if (!pipeline_fill_argv(token_list, pipeline))
|
||||
return (ft_retp_free(NULL, &pipeline, (t_free_f)free_pipeline));
|
||||
//print_pipeline(pipeline);
|
||||
|
||||
return (pipeline);
|
||||
}
|
||||
/*
|
||||
2.10 Shell Grammar
|
||||
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
|
||||
#tag_18_10
|
||||
*/
|
||||
|
||||
/* -------------------------------------------------------
|
||||
The grammar symbols
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/11/11 18:46:43 by lperrey #+# #+# */
|
||||
/* Updated: 2021/12/10 10:25:54 by hulamy ### ########.fr */
|
||||
/* Updated: 2021/12/16 14:49:53 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -37,7 +37,7 @@ int here_doc(char *delimiter)
|
||||
ret = here_doc_write(delimiter, here_doc);
|
||||
free(delimiter);
|
||||
if (close(here_doc) == -1)
|
||||
ft_perror_io("close() ", TMP_HERE_DOC);
|
||||
shell_perror(TMP_HERE_DOC, ": ", "", 0);
|
||||
if (ret != 0)
|
||||
{
|
||||
if (unlink(TMP_HERE_DOC) == -1)
|
||||
@@ -46,7 +46,7 @@ int here_doc(char *delimiter)
|
||||
}
|
||||
here_doc = open(TMP_HERE_DOC, O_RDONLY);
|
||||
if (here_doc == -1)
|
||||
ft_perror_io("open() ", TMP_HERE_DOC);
|
||||
shell_perror(TMP_HERE_DOC, ": ", "", 0);
|
||||
if (unlink(TMP_HERE_DOC) == -1)
|
||||
return (ft_reti_perror_io(-1, "unlink() ", TMP_HERE_DOC));
|
||||
return (here_doc);
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/11/11 18:46:43 by lperrey #+# #+# */
|
||||
/* Updated: 2021/12/10 10:25:34 by hulamy ### ########.fr */
|
||||
/* Updated: 2021/12/16 14:49:28 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -16,6 +16,7 @@ int here_doc(char *delimiter);
|
||||
|
||||
static int redirect_cmd_input(t_token *t, t_cmd *cmd);
|
||||
static int redirect_cmd_output(t_token *t, t_cmd *cmd);
|
||||
static int expand_redirection(t_token *t);
|
||||
|
||||
int redirections(t_token *t, t_cmd **pipeline)
|
||||
{
|
||||
@@ -24,6 +25,8 @@ int redirections(t_token *t, t_cmd **pipeline)
|
||||
i = 0;
|
||||
while (t)
|
||||
{
|
||||
if (t->id == '|')
|
||||
i++;
|
||||
if (!pipeline[i]->error)
|
||||
{
|
||||
if (t->id == '<' || t->id == T_DLESS)
|
||||
@@ -37,8 +40,6 @@ int redirections(t_token *t, t_cmd **pipeline)
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
if (t->id == '|')
|
||||
i++;
|
||||
t = t->next;
|
||||
}
|
||||
return (1);
|
||||
@@ -51,15 +52,15 @@ static int redirect_cmd_input(t_token *t, t_cmd *cmd)
|
||||
perror("close()");
|
||||
if (t->id == '<')
|
||||
{
|
||||
// TODO : Expansion + quote removal sur le word t->next->content.
|
||||
// si plus d'un champ ou aucun champ aprés expansion,
|
||||
// message d'erreur comme bash "bash: $VAR: ambiguous redirect"
|
||||
// OU prise en compte seulement du premier champ.
|
||||
//EXPAND_AND_QUOTE_REMOVAL_PLACEHOLDER();
|
||||
if (!expand_redirection(t))
|
||||
{
|
||||
cmd->error = EXIT_REDIRECTION;
|
||||
return (1);
|
||||
}
|
||||
cmd->fd_in = open(t->next->content, O_RDONLY);
|
||||
if (cmd->fd_in == -1)
|
||||
{
|
||||
ft_perror_io("open() ", t->next->content); // todo error
|
||||
shell_perror(t->next->content, ": ", "", 0);
|
||||
cmd->error = EXIT_REDIRECTION;
|
||||
}
|
||||
}
|
||||
@@ -68,7 +69,7 @@ static int redirect_cmd_input(t_token *t, t_cmd *cmd)
|
||||
cmd->fd_in = here_doc(t->next->content);
|
||||
if (cmd->fd_in == -1)
|
||||
{
|
||||
ft_putstr_fd("minishell: heredoc error\n", 2);
|
||||
shell_error("heredoc error", ": ", "", 0);
|
||||
cmd->error = EXIT_REDIRECTION;
|
||||
}
|
||||
else if (cmd->fd_in > EXIT_SIGNAL)
|
||||
@@ -87,11 +88,11 @@ static int redirect_cmd_output(t_token *t, t_cmd *cmd)
|
||||
if (cmd->fd_out != STDOUT_FILENO)
|
||||
if (close(cmd->fd_out) == -1)
|
||||
perror("close()");
|
||||
// TODO : Expansion + quote removal sur le word t->next->content.
|
||||
// si plus d'un champ ou aucun champ aprés expansion,
|
||||
// message d'erreur comme bash "bash: $VAR: ambiguous redirect"
|
||||
// OU prise en compte seulement du premier champ.
|
||||
//EXPAND_AND_QUOTE_REMOVAL_PLACEHOLDER();
|
||||
if (!expand_redirection(t))
|
||||
{
|
||||
cmd->error = EXIT_REDIRECTION;
|
||||
return (1);
|
||||
}
|
||||
flags = O_WRONLY | O_CREAT;
|
||||
if (t->id == '>')
|
||||
flags = flags | O_TRUNC;
|
||||
@@ -100,8 +101,37 @@ static int redirect_cmd_output(t_token *t, t_cmd *cmd)
|
||||
cmd->fd_out = open(t->next->content, flags, S_IRWXU);
|
||||
if (cmd->fd_out == -1)
|
||||
{
|
||||
ft_perror_io("open() ", t->next->content);
|
||||
shell_perror(t->next->content, ": ", "", EXIT_REDIRECTION);
|
||||
cmd->error = EXIT_REDIRECTION;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
static int expand_redirection(t_token *t)
|
||||
{
|
||||
t_token *head;
|
||||
t_token *next_token;
|
||||
int ret;
|
||||
|
||||
ret = 1;
|
||||
head = t;
|
||||
t = t->next;
|
||||
next_token = t->next;
|
||||
t->next = NULL;
|
||||
if (!token_expansions(t))
|
||||
{
|
||||
((t_token *)ft_lstlast((t_list *)head))->next = next_token;
|
||||
return (0);
|
||||
}
|
||||
head->next = t->next;
|
||||
free(t);
|
||||
if (head->next)
|
||||
head->next->id = T_REDIRECTION_WORD; // Eventuellement a integrer dans token_expansions()
|
||||
if (ft_lstsize((t_list *)head->next) != 1)
|
||||
{
|
||||
ret = 0;
|
||||
shell_error("", "", "ambiguous redirect", 0);
|
||||
}
|
||||
((t_token *)ft_lstlast((t_list *)head))->next = next_token;
|
||||
return (ret);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user