From e4c4589d3cd9cfc1ec78ed02158206cd3a5a20a2 Mon Sep 17 00:00:00 2001 From: lperrey Date: Tue, 21 Dec 2021 01:22:38 +0100 Subject: [PATCH] fixed quotes handling in expand token + split expand_token.c in multiples files --- Makefile | 4 +- minishell_tests | 2 +- srcs/parsing/expansions/content_copy.c | 52 +++++++++++++ srcs/parsing/expansions/content_expand.c | 82 ++++++++++++++++++++ srcs/parsing/expansions/expand_token.c | 98 +----------------------- 5 files changed, 141 insertions(+), 97 deletions(-) create mode 100644 srcs/parsing/expansions/content_copy.c create mode 100644 srcs/parsing/expansions/content_expand.c diff --git a/Makefile b/Makefile index 4ac6af1..249d187 100644 --- a/Makefile +++ b/Makefile @@ -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 \ diff --git a/minishell_tests b/minishell_tests index c2e0c0b..d5c24e2 160000 --- a/minishell_tests +++ b/minishell_tests @@ -1 +1 @@ -Subproject commit c2e0c0bdcbb9446608e7a1062a89533f391fd10b +Subproject commit d5c24e28c96ee44629c91b5ef4ecd34584f12a7f diff --git a/srcs/parsing/expansions/content_copy.c b/srcs/parsing/expansions/content_copy.c new file mode 100644 index 0000000..32b4836 --- /dev/null +++ b/srcs/parsing/expansions/content_copy.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* content_copy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lperrey +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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; + } +} diff --git a/srcs/parsing/expansions/content_expand.c b/srcs/parsing/expansions/content_expand.c new file mode 100644 index 0000000..82a1693 --- /dev/null +++ b/srcs/parsing/expansions/content_expand.c @@ -0,0 +1,82 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* content_expand.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lperrey +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 +*/ diff --git a/srcs/parsing/expansions/expand_token.c b/srcs/parsing/expansions/expand_token.c index f0f242e..7bce58e 100644 --- a/srcs/parsing/expansions/expand_token.c +++ b/srcs/parsing/expansions/expand_token.c @@ -6,15 +6,14 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 -*/