commit pour merge

This commit is contained in:
hugogogo
2021-12-17 12:00:32 +01:00
32 changed files with 621 additions and 565 deletions

View File

@@ -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, &quotes_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);
}
/*

View File

@@ -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

View File

@@ -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++;
}
}

View File

@@ -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(&quote_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(&quote_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(&quote_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(&quote_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);
}

View File

@@ -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(&quote_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);
}

View File

@@ -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
}