From 15bc4d2158fee7df4e62293a92e82613f52f4cc9 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Fri, 29 Oct 2021 13:26:38 +0200 Subject: [PATCH] refonte de la gestion des cmd dans parsing --- Makefile | 2 +- headers/minishell_prototypes.h | 3 +- srcs/main.c | 2 +- srcs/parsing/cmd_expansion.c | 28 ++++++++ srcs/parsing/fill_cmd.c | 70 ++++++++++---------- srcs/parsing/parsing.c | 113 ++++++++++++++++++++++++++++++++- srcs/shell_loop.c | 12 ++-- 7 files changed, 182 insertions(+), 48 deletions(-) create mode 100644 srcs/parsing/cmd_expansion.c diff --git a/Makefile b/Makefile index e46605e..eddb3c6 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,7 @@ SRCS = main.c init.c free.c generic.c \ lexing.c \ parsing.c \ valid_syntax.c valid_pipeline.c valid_command.c valid_io_redirect.c \ - fill_cmd.c \ + fill_cmd.c cmd_expansion.c \ env.c exit.c echo.c DIR_OBJS = builds diff --git a/headers/minishell_prototypes.h b/headers/minishell_prototypes.h index 00b7150..9c80db6 100644 --- a/headers/minishell_prototypes.h +++ b/headers/minishell_prototypes.h @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */ -/* Updated: 2021/10/28 20:49:30 by hulamy ### ########.fr */ +/* Updated: 2021/10/28 21:10:45 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,6 +28,7 @@ int valid_syntax(t_token *token_list); int valid_token(t_token **token_list, enum e_token_id token_id); int valid_command_separator(const t_token *token_list); t_cmd **fill_cmd(t_token *token, char **envp); +void cmd_expansion(t_cmd **cmd_arr, char **envp); // Builtins int builtin_env(int argc, char *argv[], t_all *c); diff --git a/srcs/main.c b/srcs/main.c index eb745a5..b79a5a5 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */ -/* Updated: 2021/10/28 15:12:04 by hulamy ### ########.fr */ +/* Updated: 2021/10/29 01:10:02 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/srcs/parsing/cmd_expansion.c b/srcs/parsing/cmd_expansion.c new file mode 100644 index 0000000..de464db --- /dev/null +++ b/srcs/parsing/cmd_expansion.c @@ -0,0 +1,28 @@ + +#include "minishell.h" + +void cmd_expansion(t_cmd **cmd_arr, char **envp) +{ + char **str; + char *var; + int i; + int j; + + (void)envp; + i = 0; + while (cmd_arr[i]) + { + j = 0; + str = cmd_arr[i]->argv; + while (str[j]) + { + var = ft_strchr(str[j], '$'); + if (var != NULL) + { + printf("%s\n", var); + } + j++; + } + i++; + } +} diff --git a/srcs/parsing/fill_cmd.c b/srcs/parsing/fill_cmd.c index dfc3eb1..6bc8e12 100644 --- a/srcs/parsing/fill_cmd.c +++ b/srcs/parsing/fill_cmd.c @@ -28,35 +28,29 @@ int nbr_argv(t_token *token) return (i); } -// T_T -// T_L -// T_G -// T_P -// T_D -// T_D -// T_W -void handle_argv(t_token **token, t_cmd *cmd) -{ - int argc; - int i; - - argc = nbr_argv(*token); - cmd->argv = calloc(argc + 1, sizeof(char *)); - cmd->argv[argc] = NULL; - i = 0; - while (*token && (*token)->id != T_PIPE) - { - if ((*token)->id == T_WORD) - { - cmd->argv[i] = ft_strdup((*token)->content); - i++; - } - *token = (*token)->next; - } - if (*token && (*token)->id == T_PIPE) - *token = (*token)->next; -} +//void handle_argv(t_token **token, t_cmd *cmd) +//{ +// int argc; +// int i; +// +// argc = nbr_argv(*token); +// cmd->argv = calloc(argc + 1, sizeof(char *)); +// cmd->argv[argc] = NULL; +// i = 0; +// while (*token && (*token)->id != T_PIPE) +// { +// if ((*token)->id == T_WORD) +// { +// cmd->argv[i] = ft_strdup((*token)->content); +// i++; +// } +// *token = (*token)->next; +// } +// if (*token && (*token)->id == T_PIPE) +// *token = (*token)->next; +//} +/* void handle_fd(t_token *token, t_cmd **cmd) { int *pipes_fd; @@ -142,15 +136,15 @@ t_cmd **fill_cmd(t_token *token, char **envp) int pipes; int i; - pipes = nbr_pipes(token); - cmd = ft_calloc(pipes + 2, sizeof(t_cmd*)); - cmd[pipes + 1] = NULL; - i = -1; - while (++i <= pipes) - { - cmd[i] = ft_calloc(1, sizeof(t_cmd)); - ft_bzero(cmd[i], sizeof(t_cmd)); - } +// pipes = nbr_pipes(token); +// cmd = ft_calloc(pipes + 2, sizeof(t_cmd*)); +// cmd[pipes + 1] = NULL; +// i = -1; +// while (++i <= pipes) +// { +// cmd[i] = ft_calloc(1, sizeof(t_cmd)); +// ft_bzero(cmd[i], sizeof(t_cmd)); +// } i = 0; while (i <= pipes) { @@ -162,4 +156,4 @@ t_cmd **fill_cmd(t_token *token, char **envp) } return(cmd); } - +*/ diff --git a/srcs/parsing/parsing.c b/srcs/parsing/parsing.c index 96fc2fa..cccb14d 100644 --- a/srcs/parsing/parsing.c +++ b/srcs/parsing/parsing.c @@ -11,10 +11,113 @@ enum e_token_id id; } t_binary_tree; */ +size_t count_pipes(t_token *token) +{ + size_t nb; + + nb = 0; + while (token) + { + if (token->id == T_PIPE) + nb++; + token = token->next; + } + return (nb + 1); +} + +t_cmd **create_cmd(t_token *token_list, size_t cmd_nbr) +{ + t_cmd **cmd_arr; + size_t i; + + (void)token_list; + cmd_arr = ft_calloc(cmd_nbr + 1, sizeof(t_cmd *)); + cmd_arr[cmd_nbr] = NULL; + i = 0; + while (i < cmd_nbr) + { + cmd_arr[i] = ft_calloc(1, sizeof(t_cmd)); + ft_bzero(cmd_arr[i], sizeof(t_cmd)); + i++; + } + return (cmd_arr); +} + +// T_TOKEN = 0, +// T_LESS = '<', +// T_GREAT = '>', +// T_PIPE = '|', +// T_DLESS, //'<<' +// T_DGREAT, //'>>' +// T_WORD +// count nbr word in cmd, minus redirection and heredoc +int next_cmd(t_token **token) +{ + int i; + + i = 0; + while (*token && (*token)->id != T_PIPE) + { + if ((*token)->id != T_WORD) + i--; + else + i++; + *token = (*token)->next; + } + if (*token && (*token)->id == T_PIPE) + *token = (*token)->next; + return (i); +} + +void handle_argv(t_token *token, t_cmd **cmd_arr, size_t cmd_nbr) +{ + int argc; + int i; + + (void)cmd_arr; + (void)token; + (void)cmd_nbr; + while (cmd_nbr) + { + argc = next_cmd(&token); + cmd->argv = calloc(argc + 1, sizeof(char *)); + cmd->argv[argc] = NULL; +// i = 0; +// while (token && token->id != T_PIPE) +// { +// if (token->id == T_WORD) +// { +// cmd->argv[i] = ft_strdup(token->content); +// i++; +// } +// token = token->next; +// } +// cmd_nbr--; + } + +// cmd->argv = calloc(argc + 1, sizeof(char *)); +// cmd->argv[argc] = NULL; + +// i = 0; +// while (*token && (*token)->id != T_PIPE) +// { +// if ((*token)->id == T_WORD) +// { +// cmd->argv[i] = ft_strdup((*token)->content); +// i++; +// } +// *token = (*token)->next; +// } +// if (*token && (*token)->id == T_PIPE) +// *token = (*token)->next; +} + t_cmd **parsing(t_token *token_list, char **envp) { t_cmd **cmd_arr; + size_t cmd_nbr; + (void)envp; /* t_binary_tree *syntax_tree; syntax_tree = ft_calloc(1, sizeof *syntax_tree); @@ -24,13 +127,21 @@ t_cmd **parsing(t_token *token_list, char **envp) if (!valid_syntax(token_list)) return (NULL); // Pipes count (determine cmd_nbr) + cmd_nbr = count_pipes(token_list); // Struct CMD alloc/fill - cmd_arr = fill_cmd(token_list, envp); + cmd_arr = create_cmd(token_list, cmd_nbr); +// cmd_arr = fill_cmd(token_list, envp); // 2.9.1 - 2) Expansion +// cmd_expansion(cmd_arr, envp); + + handle_argv(token_list, cmd_arr, cmd_nbr); +// if (!handle_builtin(token, cmd[i])) +// handle_cmd(cmd[i]->argv, envp); // 2.9.1 - 3) Redirection +// handle_fd(token, cmd + i); return (cmd_arr); } diff --git a/srcs/shell_loop.c b/srcs/shell_loop.c index 188e909..fbca9e9 100644 --- a/srcs/shell_loop.c +++ b/srcs/shell_loop.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */ -/* Updated: 2021/10/28 20:45:09 by hulamy ### ########.fr */ +/* Updated: 2021/10/29 12:49:08 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -79,11 +79,11 @@ void shell_loop(t_all *c) c->cmd_arr = parsing(c->token_list, c->envp); if (c->cmd_arr == NULL) ft_putstr_fd("Syntax KO:\n-----------\n", 1); - // else - // ft_putstr_fd("Syntax OK:\n-----------\n", 1); - // ft_putstr_fd("TOKENS LIST :\n-----------\n", 1); - // ft_lstprint((t_list *)c->token_list, 1); - execute_cmd(c->envp, c->cmd_arr); + else + ft_putstr_fd("Syntax OK:\n-----------\n", 1); + ft_putstr_fd("TOKENS LIST :\n-----------\n", 1); + ft_lstprint((t_list *)c->token_list, 1); +// execute_cmd(c->envp, c->cmd_arr); ft_lstclear((t_list **)&c->token_list, free); } }