fixed quotes handling in expand token

+ split expand_token.c in multiples files
This commit is contained in:
lperrey
2021-12-21 01:22:38 +01:00
parent 80917cae7c
commit e4c4589d3c
5 changed files with 141 additions and 97 deletions

View File

@@ -38,7 +38,9 @@ SRCS = main.c \
lexing.c fill_token.c check_operators.c \
parsing.c create_pipeline.c \
valid_syntax.c valid_pipeline.c valid_command.c rules_command.c valid_io_redirect.c \
expansions.c expand_token.c rejoin_after_expand.c new_token_for_each_field.c \
expansions.c \
expand_token.c content_expand.c content_copy.c \
rejoin_after_expand.c new_token_for_each_field.c \
redirections.c here_doc.c \
exec_cmd_line.c pipeline.c \
find_access.c subshell_exec.c subshell_wait.c simple_cmd_builtin.c \

View File

@@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* content_copy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/07 02:01:33 by lperrey #+# #+# */
/* Updated: 2021/12/21 01:04:58 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static void quotes_handling(char c, int *quotes_state);
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])
{
if (content[*i] == '$' && *quotes_state != IN_QUOTES)
break ;
quotes_handling(content[*i], quotes_state);
((char *)expand->content)[i_exp++] = content[(*i)++];
}
return (expand);
}
static void quotes_handling(char c, int *quotes_state)
{
if (c == '\'' && *quotes_state != IN_DQUOTES)
{
if (*quotes_state == IN_QUOTES)
*quotes_state = 0;
else
*quotes_state = IN_QUOTES;
}
else if (c == '\"' && *quotes_state != IN_QUOTES)
{
if (*quotes_state == IN_DQUOTES)
*quotes_state = 0;
else
*quotes_state = IN_DQUOTES;
}
}

View File

@@ -0,0 +1,82 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* content_expand.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/07 02:01:33 by lperrey #+# #+# */
/* Updated: 2021/12/21 01:20:40 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static char *env_var_expansion(char *content, int *i);
static char *retrieve_var(char *content, int *i);
t_list *content_expand(char *content, int *i)
{
t_list *expand;
expand = ft_lstnew(NULL);
if (!expand)
return (NULL);
expand->content = env_var_expansion(content, i);
if (!expand->content)
{
free(expand);
return (NULL);
}
return (expand);
}
static char *env_var_expansion(char *content, int *i)
{
char *expansion;
(*i)++;
if (content[*i] == '?')
{
(*i)++;
expansion = ft_itoa(get_last_exit_status());
}
else if (content[*i] == '_' || ft_isalpha(content[*i]))
expansion = retrieve_var(content, i);
else
{
expansion = ft_calloc(1 + 1, 1);
if (!expansion)
return (NULL);
expansion[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);
if (!expansion)
return (NULL);
i_exp = 0;
while (content[*i] == '_' || ft_isalnum(content[*i]))
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);
}
/*
environment variables must be POSIX NAME :
3.235 Name
https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html
#tag_03_235
*/

View File

@@ -6,15 +6,14 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/07 02:01:33 by lperrey #+# #+# */
/* Updated: 2021/12/20 14:47:04 by hulamy ### ########.fr */
/* Updated: 2021/12/21 01:12:42 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
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 *content_copy(char *content, int *i, int *quotes_state);
t_list *content_expand(char *content, int *i);
t_list *expand_token(char *content)
{
@@ -42,94 +41,3 @@ t_list *expand_token(char *content)
}
return (head.next);
}
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;
expand = ft_lstnew(NULL);
if (!expand)
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)++;
if (content[*i] == '?')
{
(*i)++;
expansion = ft_itoa(get_last_exit_status());
}
else if (content[*i] == '_' || ft_isalpha(content[*i]))
expansion = retrieve_var(content, i);
else
{
expansion = ft_calloc(1 + 1, 1);
if (!expansion)
return (NULL);
expansion[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);
if (!expansion)
return (NULL);
i_exp = 0;
while (content[*i] == '_' || ft_isalnum(content[*i]))
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);
}
/*
environment variables must be POSIX NAME :
3.235 Name
https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html
#tag_03_235
*/