WIP parsing expansions
This commit is contained in:
113
srcs/parsing/expansions/expand_token.c
Normal file
113
srcs/parsing/expansions/expand_token.c
Normal file
@@ -0,0 +1,113 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* expand_token.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
t_list *ft_lstnew_generic(size_t lst_sizse, size_t content_size);
|
||||
|
||||
enum e_in_quote_state
|
||||
{
|
||||
NOT_IN = 0,
|
||||
IN_QUOTES = '\'',
|
||||
IN_DQUOTES = '\"'
|
||||
};
|
||||
|
||||
static t_list *ret_parameter_expansion(t_token *t, int *i);
|
||||
|
||||
t_list *expand_token(t_token *t)
|
||||
{
|
||||
int in_quotes;
|
||||
int i;
|
||||
t_list head;
|
||||
t_list *expand;
|
||||
|
||||
in_quotes = 0;
|
||||
i = 0;
|
||||
head.next = NULL;
|
||||
expand = &head;
|
||||
while (t->content[i])
|
||||
{
|
||||
if (t->content[i] == '$')
|
||||
{
|
||||
expand->next = ret_parameter_expansion(t, &i);
|
||||
if (!expand->next)
|
||||
{//todo wrap
|
||||
perror("expand_token() error");
|
||||
return (ft_lstclear(&head.next, free));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
expand->next = ft_lstnew_generic(sizeof(t_list), ft_strlen(&t->content[i]) + 1);
|
||||
if (!expand->next)
|
||||
{//todo wrap
|
||||
perror("expand_token() error");
|
||||
return (ft_lstclear(&head.next, free));
|
||||
}
|
||||
while (t->content[i] && (t->content[i] != '$' && in_quotes != IN_QUOTES))
|
||||
{
|
||||
// quoting
|
||||
if (t->content[i] == '\'' && in_quotes != IN_DQUOTES)
|
||||
{
|
||||
if (in_quotes == IN_QUOTES)
|
||||
in_quotes = 0;
|
||||
else
|
||||
in_quotes = IN_QUOTES;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
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.
|
||||
// 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)
|
||||
{
|
||||
t_list *expand;
|
||||
char *tmp;
|
||||
int i_tmp;
|
||||
|
||||
tmp = ft_calloc(ft_strlen(&t->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] == '?')
|
||||
{
|
||||
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]))
|
||||
{
|
||||
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++];
|
||||
expand->content = getenv(tmp);
|
||||
free(tmp);
|
||||
if (expand->content)
|
||||
expand->content = ft_strdup(expand->content);
|
||||
else
|
||||
expand->content = ft_calloc(1, 1);
|
||||
if (!expand->content)
|
||||
return (ft_retp_free(NULL, expand, free));
|
||||
return (expand);
|
||||
}
|
||||
61
srcs/parsing/expansions/new_token_for_each_field.c
Normal file
61
srcs/parsing/expansions/new_token_for_each_field.c
Normal file
@@ -0,0 +1,61 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* new_token_for_each_field.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/11/07 02:01:33 by lperrey #+# #+# */
|
||||
/* Updated: 2021/11/07 04:33:04 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
t_list *ft_lstnew_generic(size_t lst_sizse, size_t content_size);
|
||||
|
||||
static t_token *insert_tokens(t_token *t, t_token *insert_lst);
|
||||
|
||||
int new_token_for_each_field(char **fields, t_token **t)
|
||||
{
|
||||
t_token head;
|
||||
t_token *insert_lst;
|
||||
int i;
|
||||
|
||||
head.next = NULL;
|
||||
insert_lst = &head;
|
||||
i = 0;
|
||||
while (fields[i])
|
||||
{
|
||||
insert_lst->next = (t_token *)ft_lstnew_generic(sizeof(*insert_lst), 0);
|
||||
if (!insert_lst->next)
|
||||
{//todo wrap
|
||||
perror("insert_token_for_each_field() error");
|
||||
ft_free_2d_arr(fields);
|
||||
return ((int)ft_lstclear((t_list **)&head.next, NULL));
|
||||
}
|
||||
insert_lst = insert_lst->next;
|
||||
insert_lst->content = fields[i];
|
||||
insert_lst->id = T_WORD;
|
||||
}
|
||||
free(fields);
|
||||
*t = insert_tokens(*t, insert_lst);
|
||||
return (1);
|
||||
}
|
||||
|
||||
static t_token *insert_tokens(t_token *t, t_token *insert_lst)
|
||||
{
|
||||
t_token *tmp;
|
||||
t_token *insert_lst_last;
|
||||
|
||||
t->id = 0;
|
||||
free(t->content);
|
||||
t->content = NULL;
|
||||
|
||||
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);
|
||||
}
|
||||
39
srcs/parsing/expansions/rejoin_after_expand.c
Normal file
39
srcs/parsing/expansions/rejoin_after_expand.c
Normal file
@@ -0,0 +1,39 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* rejoin_after_expand.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/11/07 02:01:33 by lperrey #+# #+# */
|
||||
/* Updated: 2021/11/07 04:03:02 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
char *rejoin_after_expand(t_list *expand_lst)
|
||||
{
|
||||
t_list *head;
|
||||
char *result;
|
||||
|
||||
head = expand_lst;
|
||||
result = ft_calloc(1, 1);
|
||||
if (!result)
|
||||
{//todo wrap
|
||||
perror("rejoin_after_expand() error");
|
||||
return (ft_lstclear(&head, free));
|
||||
}
|
||||
while (expand_lst)
|
||||
{
|
||||
result = ft_strjoinfree_s1(result, expand_lst->content);
|
||||
if (!result)
|
||||
{//todo wrap
|
||||
perror("rejoin_after_expand() error");
|
||||
return (ft_lstclear(&head, free));
|
||||
}
|
||||
expand_lst = expand_lst->next;
|
||||
}
|
||||
ft_lstclear(&head, free);
|
||||
return (result);
|
||||
}
|
||||
69
srcs/parsing/expansions/words_expansions.c
Normal file
69
srcs/parsing/expansions/words_expansions.c
Normal file
@@ -0,0 +1,69 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* words_expansions.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/11/07 02:01:33 by lperrey #+# #+# */
|
||||
/* Updated: 2021/11/07 04:36:52 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
enum e_in_quote_state
|
||||
{
|
||||
NOT_IN = 0,
|
||||
IN_QUOTES = '\'',
|
||||
IN_DQUOTES = '\"'
|
||||
};
|
||||
|
||||
t_list *ft_lstnew_generic(size_t lst_sizse, size_t content_size);
|
||||
t_list *expand_token(t_token *t);
|
||||
char *rejoin_after_expand(t_list *expand_lst);
|
||||
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)
|
||||
// 2 - strjoin() le tout
|
||||
// 3 - split avec un ft_strplit() modifié (ne splitant pas dans les quotes)
|
||||
// 4 - creer un token T_WORD pour chaque *string du **split_arr
|
||||
// (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 *t)
|
||||
{
|
||||
void *tmp_expand;
|
||||
char **tmp_split;
|
||||
|
||||
while (t)
|
||||
{
|
||||
if (t->id == T_WORD)
|
||||
{
|
||||
// 1
|
||||
tmp_expand = expand_token(t);
|
||||
if (!tmp_expand)
|
||||
return (0);
|
||||
if (((t_list*)tmp_expand)->next)
|
||||
{
|
||||
// 2
|
||||
tmp_expand = rejoin_after_expand(tmp_expand);
|
||||
if (!tmp_expand)
|
||||
return (0);
|
||||
// 3 WIP PLACEHOLDER, MUST WRITE A ft_split_quoted() FOR NO SPLIT IN QUOTES
|
||||
tmp_split = ft_split(tmp_expand, ' ');
|
||||
free(tmp_expand);
|
||||
if (!tmp_split)
|
||||
return (0);
|
||||
// 4
|
||||
if (!new_token_for_each_field(tmp_split, &t))
|
||||
return (0);
|
||||
}
|
||||
else
|
||||
ft_lstclear((t_list **)&tmp_expand, free);
|
||||
}
|
||||
t = t->next;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
Reference in New Issue
Block a user