From 15bc4d2158fee7df4e62293a92e82613f52f4cc9 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Fri, 29 Oct 2021 13:26:38 +0200 Subject: [PATCH 1/6] 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); } } From 75987a117e3162e5b2026ad5c95d22fda2e8cff3 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Sat, 30 Oct 2021 10:20:43 +0200 Subject: [PATCH 2/6] cmd argv fill with word and without redirections --- srcs/parsing/parsing.c | 79 +++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 40 deletions(-) diff --git a/srcs/parsing/parsing.c b/srcs/parsing/parsing.c index cccb14d..b5f6452 100644 --- a/srcs/parsing/parsing.c +++ b/srcs/parsing/parsing.c @@ -51,65 +51,54 @@ t_cmd **create_cmd(t_token *token_list, size_t cmd_nbr) // T_DGREAT, //'>>' // T_WORD // count nbr word in cmd, minus redirection and heredoc -int next_cmd(t_token **token) +int next_cmd(t_token *token) { int i; i = 0; - while (*token && (*token)->id != T_PIPE) + while (token && token->id != T_PIPE) { - if ((*token)->id != T_WORD) + if (token->id != T_WORD) i--; else i++; - *token = (*token)->next; + token = token->next; } - if (*token && (*token)->id == T_PIPE) - *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) +void handle_argv(t_token *token, t_cmd **cmd, size_t cmd_nbr) { int argc; + int j; int i; - (void)cmd_arr; - (void)token; - (void)cmd_nbr; + i = 0; 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--; + argc = next_cmd(token); + cmd[i]->argv = calloc(argc + 1, sizeof(char *)); + cmd[i]->argv[argc] = NULL; + j = 0; + while (token && token->id != T_PIPE) + { + if (token->id == T_WORD) + { + cmd[i]->argv[j] = ft_strdup(token->content); + j++; + } + else if (token->id != T_PIPE) + token = token->next; + if (token->id != T_PIPE) + token = token->next; + } + if (token && token->id == T_PIPE) + token = token->next; + cmd_nbr--; + i++; } - -// 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) @@ -137,6 +126,16 @@ t_cmd **parsing(t_token *token_list, char **envp) // cmd_expansion(cmd_arr, envp); handle_argv(token_list, cmd_arr, cmd_nbr); + +int j; +j = 0; +while (cmd_arr[j]) +{ + printf("%i\n", j); + print_matrix(cmd_arr[j]->argv, " / "); + j++; +} + // if (!handle_builtin(token, cmd[i])) // handle_cmd(cmd[i]->argv, envp); From 0292425bc17ce427476b0f73548469d233447611 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Sat, 30 Oct 2021 14:43:06 +0200 Subject: [PATCH 3/6] path of cmd ok --- srcs/parsing/fill_cmd.c | 56 ++++++++++--------- srcs/parsing/parsing.c | 121 +++++++++++++++++++++++++++++++--------- srcs/shell_loop.c | 21 +++---- 3 files changed, 134 insertions(+), 64 deletions(-) diff --git a/srcs/parsing/fill_cmd.c b/srcs/parsing/fill_cmd.c index 6bc8e12..92ef03f 100644 --- a/srcs/parsing/fill_cmd.c +++ b/srcs/parsing/fill_cmd.c @@ -101,35 +101,37 @@ void handle_fd(t_token *token, t_cmd **cmd) cmd[1]->fd_in = pipes_fd[0]; } } +*/ -int handle_builtin(t_token *token, t_cmd *cmd) -{ - (void)token; - (void)cmd; - return (0); -} - -void handle_cmd(char **argv, char **envp) -{ - int i; - char **path; - char *cmd; - - i = 0; - while (envp[i] && ft_strncmp(envp[i], "PATH=", 5)) - i++; - path = ft_split(envp[i] + 5, ':'); // 5 = lentgh of "PATH=" - i = -1; - while (*path && i != 0) - { - cmd = ft_strjoin(path[0], "/"); - cmd = ft_strjoin(cmd, argv[0]); - i = access(cmd, X_OK); - path++; - } - argv[0] = cmd; -} +//int handle_builtin(t_token *token, t_cmd *cmd) +//{ +// (void)token; +// (void)cmd; +// return (0); +//} +// +//void handle_cmd(char **argv, char **envp) +//{ +// int i; +// char **path; +// char *cmd; +// +// i = 0; +// while (envp[i] && ft_strncmp(envp[i], "PATH=", 5)) +// i++; +// path = ft_split(envp[i] + 5, ':'); // 5 = lentgh of "PATH=" +// i = -1; +// while (*path && i != 0) +// { +// cmd = ft_strjoin(path[0], "/"); +// cmd = ft_strjoin(cmd, argv[0]); +// i = access(cmd, X_OK); +// path++; +// } +// argv[0] = cmd; +//} +/* t_cmd **fill_cmd(t_token *token, char **envp) { t_cmd **cmd; diff --git a/srcs/parsing/parsing.c b/srcs/parsing/parsing.c index b5f6452..36cd239 100644 --- a/srcs/parsing/parsing.c +++ b/srcs/parsing/parsing.c @@ -43,13 +43,20 @@ t_cmd **create_cmd(t_token *token_list, size_t cmd_nbr) return (cmd_arr); } -// T_TOKEN = 0, -// T_LESS = '<', -// T_GREAT = '>', -// T_PIPE = '|', -// T_DLESS, //'<<' -// T_DGREAT, //'>>' -// T_WORD +int is_redirection(enum e_token_id id) +{ + if (id == T_LESS) // < + return (1); + else if (id == T_GREAT) // > + return (1); + else if (id == T_DLESS) // << + return (1); + else if (id == T_DGREAT) // >> + return (1); + else + return (0); +} + // count nbr word in cmd, minus redirection and heredoc int next_cmd(t_token *token) { @@ -58,7 +65,7 @@ int next_cmd(t_token *token) i = 0; while (token && token->id != T_PIPE) { - if (token->id != T_WORD) + if (is_redirection(token->id)) i--; else i++; @@ -74,6 +81,7 @@ void handle_argv(t_token *token, t_cmd **cmd, size_t cmd_nbr) int argc; int j; int i; + int redirection; i = 0; while (cmd_nbr) @@ -82,17 +90,16 @@ void handle_argv(t_token *token, t_cmd **cmd, size_t cmd_nbr) cmd[i]->argv = calloc(argc + 1, sizeof(char *)); cmd[i]->argv[argc] = NULL; j = 0; + redirection = is_redirection(token->id); while (token && token->id != T_PIPE) { - if (token->id == T_WORD) + if (!redirection && token->id == T_WORD) { cmd[i]->argv[j] = ft_strdup(token->content); j++; } - else if (token->id != T_PIPE) - token = token->next; - if (token->id != T_PIPE) - token = token->next; + redirection = is_redirection(token->id); + token = token->next; } if (token && token->id == T_PIPE) token = token->next; @@ -101,12 +108,75 @@ void handle_argv(t_token *token, t_cmd **cmd, size_t cmd_nbr) } } +int fill_builtin(t_cmd *cmd, int (*builtin)(int, char **, t_all *)) +{ + cmd->builtin_command = &builtin; + return (1); +} + +int handle_builtin(t_cmd *cmd) +{ + if (!ft_strncmp(cmd->argv[0], "echo", 4)) + return (fill_builtin(cmd, &builtin_echo)); +// else if (!ft_strncmp(cmd->argv[0], "cd", 2)) +// return (fill_builtin(cmd, &builtin_cd)); +// else if (!ft_strncmp(cmd->argv[0], "pwd", 3)) +// return (fill_builtin(cmd, &builtin_pwd)); +// else if (!ft_strncmp(cmd->argv[0], "export", 6)) +// return (fill_builtin(cmd, &builtin_export)); +// else if (!ft_strncmp(cmd->argv[0], "unset", 5)) +// return (fill_builtin(cmd, &builtin_unset)); + else if (!ft_strncmp(cmd->argv[0], "env", 3)) + return (fill_builtin(cmd, &builtin_env)); + else if (!ft_strncmp(cmd->argv[0], "exit", 4)) + return (fill_builtin(cmd, &builtin_exit)); + return (0); +} + +void find_path(char **argv, char **envp) +{ + int i; + char **path; + char *cmd; + + i = 0; + while (envp[i] && ft_strncmp(envp[i], "PATH=", 5)) + i++; + path = ft_split(envp[i] + 5, ':'); // 5 = lentgh of "PATH=" + i = -1; + while (*path && i != 0) + { + cmd = ft_strjoin(path[0], "/"); + cmd = ft_strjoin(cmd, argv[0]); + i = access(cmd, X_OK); + path++; + } + if (!(*path)) + exit(0); // gerer erreur + argv[0] = cmd; +} + +void handle_path(t_cmd **cmd_arr, char **envp) +{ + int i; + + i = 0; + while (cmd_arr[i]) + { + if (!handle_builtin(cmd_arr[i])) + find_path(cmd_arr[i]->argv, envp); + i++; + } + +// if (!handle_builtin(token, cmd[i])) +// handle_cmd(cmd[i]->argv, envp); +} + 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); @@ -124,23 +194,20 @@ t_cmd **parsing(t_token *token_list, char **envp) // 2.9.1 - 2) Expansion // cmd_expansion(cmd_arr, envp); - handle_argv(token_list, cmd_arr, cmd_nbr); - -int j; -j = 0; -while (cmd_arr[j]) -{ - printf("%i\n", j); - print_matrix(cmd_arr[j]->argv, " / "); - j++; -} - -// if (!handle_builtin(token, cmd[i])) -// handle_cmd(cmd[i]->argv, envp); + handle_path(cmd_arr, envp); // 2.9.1 - 3) Redirection // handle_fd(token, cmd + i); + +//int j; +//j = 0; +//while (cmd_arr[j]) +//{ +// printf("%i\n", j); +// print_matrix(cmd_arr[j]->argv, " ; "); +// j++; +//} return (cmd_arr); } diff --git a/srcs/shell_loop.c b/srcs/shell_loop.c index fbca9e9..8bef7cb 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/29 12:49:08 by hulamy ### ########.fr */ +/* Updated: 2021/10/30 14:37:31 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -68,14 +68,15 @@ void shell_loop(t_all *c) // EXEC_PIPES_AND_CO() // temp placeholder - if (ft_strncmp(c->token_list->content, "env", 4) == 0) - builtin_env(0, NULL, c); - else if (ft_strncmp(c->token_list->content, "exit", 5) == 0) - builtin_exit(0, NULL, c); - else if (ft_strncmp(c->token_list->content, "echo", 5) == 0) - builtin_echo(ft_lstsize((t_list *)c->token_list) + 1, tokens_list_to_argv(c->token_list), c); - else - { + (void)tokens_list_to_argv; + // if (ft_strncmp(c->token_list->content, "env", 4) == 0) + // builtin_env(0, NULL, c); + // else if (ft_strncmp(c->token_list->content, "exit", 5) == 0) + // builtin_exit(0, NULL, c); + // else if (ft_strncmp(c->token_list->content, "echo", 5) == 0) + // builtin_echo(ft_lstsize((t_list *)c->token_list) + 1, tokens_list_to_argv(c->token_list), c); + // else + // { c->cmd_arr = parsing(c->token_list, c->envp); if (c->cmd_arr == NULL) ft_putstr_fd("Syntax KO:\n-----------\n", 1); @@ -85,7 +86,7 @@ void shell_loop(t_all *c) ft_lstprint((t_list *)c->token_list, 1); // execute_cmd(c->envp, c->cmd_arr); ft_lstclear((t_list **)&c->token_list, free); - } + // } } } } From 118e2e5bef3b2a41cb668268c9e305545932e28d Mon Sep 17 00:00:00 2001 From: hugogogo Date: Sat, 30 Oct 2021 15:27:41 +0200 Subject: [PATCH 4/6] handle fd --- srcs/parsing/fill_cmd.c | 102 ++++++++++++++++++++-------------------- srcs/parsing/parsing.c | 94 +++++++++++++++++++++++++----------- 2 files changed, 117 insertions(+), 79 deletions(-) diff --git a/srcs/parsing/fill_cmd.c b/srcs/parsing/fill_cmd.c index 92ef03f..e354814 100644 --- a/srcs/parsing/fill_cmd.c +++ b/srcs/parsing/fill_cmd.c @@ -50,58 +50,56 @@ int nbr_argv(t_token *token) // *token = (*token)->next; //} -/* -void handle_fd(t_token *token, t_cmd **cmd) -{ - int *pipes_fd; - - (*cmd)->fd_out = 1; - while (token && token->id != T_PIPE) - { - // T_LESS : '<', - if (token->id == T_LESS) - { - if ((*cmd)->fd_in != 0) - close((*cmd)->fd_in); - (*cmd)->fd_in = open(token->next->content, O_RDONLY | O_CREAT); - token->id = T_TOKEN; - token->next->id = T_TOKEN; - } - // T_GREAT : '>', - if (token->id == T_GREAT) - { - (*cmd)->fd_out = open(token->next->content, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU); - token->id = T_TOKEN; - token->next->id = T_TOKEN; - } - // T_DGREAT : '>>' - if (token->id == T_DGREAT) - { - (*cmd)->fd_out = open(token->next->content, O_WRONLY | O_CREAT | O_APPEND, S_IRWXU); - token->id = T_TOKEN; - token->next->id = T_TOKEN; - } - // T_DLESS : '<<' (heredoc) - // if (token->id == T_DGREAT) - // { - // cmd->fd_out = open(token->next->content, O_WRONLY | O_TMPFILE | O_APPEND, S_IRWXU); - // token->out = T_TOKEN; - // token->next->id = T_TOKEN; - // } - token = token->next; - } - if (token && token->id == T_PIPE) - { - pipes_fd = calloc(2, sizeof(int)); - pipe(pipes_fd); - if ((*cmd)->fd_out == 1) - (*cmd)->fd_out = pipes_fd[1]; - else - close(pipes_fd[1]); - cmd[1]->fd_in = pipes_fd[0]; - } -} -*/ +//void handle_fd(t_token *token, t_cmd **cmd) +//{ +// int *pipes_fd; +// +// (*cmd)->fd_out = 1; +// while (token && token->id != T_PIPE) +// { +// // T_LESS : '<', +// if (token->id == T_LESS) +// { +// if ((*cmd)->fd_in != 0) +// close((*cmd)->fd_in); +// (*cmd)->fd_in = open(token->next->content, O_RDONLY | O_CREAT); +// token->id = T_TOKEN; +// token->next->id = T_TOKEN; +// } +// // T_GREAT : '>', +// if (token->id == T_GREAT) +// { +// (*cmd)->fd_out = open(token->next->content, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU); +// token->id = T_TOKEN; +// token->next->id = T_TOKEN; +// } +// // T_DGREAT : '>>' +// if (token->id == T_DGREAT) +// { +// (*cmd)->fd_out = open(token->next->content, O_WRONLY | O_CREAT | O_APPEND, S_IRWXU); +// token->id = T_TOKEN; +// token->next->id = T_TOKEN; +// } +// // T_DLESS : '<<' (heredoc) +// // if (token->id == T_DGREAT) +// // { +// // cmd->fd_out = open(token->next->content, O_WRONLY | O_TMPFILE | O_APPEND, S_IRWXU); +// // token->out = T_TOKEN; +// // token->next->id = T_TOKEN; +// // } +// token = token->next; +// } +// if (token && token->id == T_PIPE) +// { +// pipes_fd = calloc(2, sizeof(int)); +// pipe(pipes_fd); +// if ((*cmd)->fd_out == 1) +// (*cmd)->fd_out = pipes_fd[1]; +// else +// close(pipes_fd[1]); +// cmd[1]->fd_in = pipes_fd[0]; +// } +//} //int handle_builtin(t_token *token, t_cmd *cmd) //{ diff --git a/srcs/parsing/parsing.c b/srcs/parsing/parsing.c index 36cd239..d961b5a 100644 --- a/srcs/parsing/parsing.c +++ b/srcs/parsing/parsing.c @@ -1,16 +1,6 @@ #include "minishell.h" -// A quoi bon un arbre binaire ? Je ne vois plus l'utilité. - -/* typedef struct s_binary_tree -{ - char *content; - struct s_binary_tree *sub; - struct s_binary_tree *sibling; - enum e_token_id id; -} t_binary_tree; */ - size_t count_pipes(t_token *token) { size_t nb; @@ -167,9 +157,73 @@ void handle_path(t_cmd **cmd_arr, char **envp) find_path(cmd_arr[i]->argv, envp); i++; } +} -// if (!handle_builtin(token, cmd[i])) -// handle_cmd(cmd[i]->argv, envp); +void fd_redirection(t_token *token, t_cmd *cmd) +{ + int flag; + + if (token->id == T_LESS) // '<' + { + flag = O_RDONLY | O_CREAT; + if (cmd->fd_in != 0) + close(cmd->fd_in); + cmd->fd_in = open(token->next->content, flag); + } + else if (token->id == T_GREAT) // '>' + { + flag = O_WRONLY | O_CREAT | O_TRUNC; + cmd->fd_out = open(token->next->content, flag, S_IRWXU); + } + else if (token->id == T_DGREAT) // '>>' + { + flag = O_WRONLY | O_CREAT | O_APPEND; + cmd->fd_out = open(token->next->content, flag, S_IRWXU); + } +} + +void fd_heredoc(t_token *token, t_cmd *cmd) +{ + (void)token; + (void)cmd; + // if (token->id == T_DGREAT) // << + // { + // cmd->fd_out = open(token->next->content, O_WRONLY | O_TMPFILE | O_APPEND, S_IRWXU); + // token->out = T_TOKEN; + // token->next->id = T_TOKEN; + // } +} + +void handle_fd(t_token *token, t_cmd **cmd_arr) +{ + int *pipes_fd; + int i; + + i = 0; + while (cmd_arr[i]) + { + cmd_arr[i]->fd_out = 1; + while (token && token->id != T_PIPE) + { + if (token->id == T_DGREAT) // '<<' + fd_heredoc(token, cmd_arr[i]); + else + fd_redirection(token, cmd_arr[i]); + token = token->next; + } + if (token && token->id == T_PIPE) + { + token = token->next; + pipes_fd = calloc(2, sizeof(int)); + pipe(pipes_fd); + if (cmd_arr[i]->fd_out == 1) + cmd_arr[i]->fd_out = pipes_fd[1]; + else + close(pipes_fd[1]); + cmd_arr[i + 1]->fd_in = pipes_fd[0]; + } + i++; + } } t_cmd **parsing(t_token *token_list, char **envp) @@ -177,28 +231,14 @@ t_cmd **parsing(t_token *token_list, char **envp) t_cmd **cmd_arr; size_t cmd_nbr; -/* t_binary_tree *syntax_tree; - - syntax_tree = ft_calloc(1, sizeof *syntax_tree); - if (!syntax_tree) - return (0); //WIP ERROR */ - if (!valid_syntax(token_list)) return (NULL); - // Pipes count (determine cmd_nbr) cmd_nbr = count_pipes(token_list); - - // Struct CMD alloc/fill 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); handle_path(cmd_arr, envp); - - // 2.9.1 - 3) Redirection -// handle_fd(token, cmd + i); + handle_fd(token_list, cmd_arr); //int j; //j = 0; From 698dcbe1ed8632dab1cf7b1996fa00ea473fe59d Mon Sep 17 00:00:00 2001 From: hugogogo Date: Sat, 30 Oct 2021 15:34:45 +0200 Subject: [PATCH 5/6] execute ok, il manque les heredocs --- fille.txt | 17 +++++++++++++++++ srcs/shell_loop.c | 15 +++++++++------ 2 files changed, 26 insertions(+), 6 deletions(-) create mode 100755 fille.txt diff --git a/fille.txt b/fille.txt new file mode 100755 index 0000000..9d5dd50 --- /dev/null +++ b/fille.txt @@ -0,0 +1,17 @@ +total 1376 +drwxrwxr-x 2 simplonco simplonco 4096 oct. 30 15:32 builds +-rw-rw-r-- 1 simplonco simplonco 0 oct. 28 21:00 cat +-rw-rw-r-- 1 simplonco simplonco 125 oct. 28 21:00 echo +-rwxrwxr-x 1 simplonco simplonco 150 oct. 28 21:03 file.txt +-rwx------ 1 simplonco simplonco 0 oct. 30 15:33 fille.txt +drwxrwxr-x 2 simplonco simplonco 4096 oct. 28 21:10 headers +drwxrwxr-x 8 simplonco simplonco 4096 oct. 23 20:48 libft +-rw-rw-r-- 1 simplonco simplonco 1412 oct. 29 12:32 Makefile +-rwxrwxr-x 1 simplonco simplonco 72256 oct. 30 15:32 minishell +-rw-rw-r-- 1 simplonco simplonco 1273353 oct. 7 21:29 minishell.en.subject.pdf +-rw-rw-r-- 1 simplonco simplonco 3365 oct. 27 14:47 parsing.txt +-rw-rw-r-- 1 simplonco simplonco 15216 oct. 28 21:00 README.md +drwxrwxr-x 2 simplonco simplonco 4096 oct. 28 21:00 ressources +drwxrwxr-x 4 simplonco simplonco 4096 oct. 30 15:32 srcs +drwxrwxr-x 2 simplonco simplonco 4096 oct. 27 14:47 tests +-rw-rw-r-- 1 simplonco simplonco 58 oct. 13 17:38 valgrind_readline.supp diff --git a/srcs/shell_loop.c b/srcs/shell_loop.c index 8bef7cb..4283afd 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/30 14:37:31 by hulamy ### ########.fr */ +/* Updated: 2021/10/30 15:32:49 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -77,14 +77,17 @@ void shell_loop(t_all *c) // builtin_echo(ft_lstsize((t_list *)c->token_list) + 1, tokens_list_to_argv(c->token_list), c); // else // { + 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); // } } From a926a817daa1e2b424ccaa9f0e735e20cf5615e1 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Tue, 2 Nov 2021 13:38:52 +0100 Subject: [PATCH 6/6] parsing et execution partielle de hugo --- Makefile | 1 - file.txt | 15 ---- fille.txt | 17 ---- srcs/parsing/cmd_expansion.c | 28 ------ srcs/parsing/fill_cmd.c | 159 ----------------------------------- srcs/parsing/parsing.c | 60 +++++++------ srcs/shell_loop.c | 137 +++++++++++------------------- 7 files changed, 78 insertions(+), 339 deletions(-) delete mode 100755 file.txt delete mode 100755 fille.txt delete mode 100644 srcs/parsing/cmd_expansion.c delete mode 100644 srcs/parsing/fill_cmd.c diff --git a/Makefile b/Makefile index eddb3c6..b239c10 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,6 @@ 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 cmd_expansion.c \ env.c exit.c echo.c DIR_OBJS = builds diff --git a/file.txt b/file.txt deleted file mode 100755 index 680d3ea..0000000 --- a/file.txt +++ /dev/null @@ -1,15 +0,0 @@ -builds -cat -echo -file.txt -headers -libft -Makefile -minishell -minishell.en.subject.pdf -parsing.txt -README.md -ressources -srcs -tests -valgrind_readline.supp diff --git a/fille.txt b/fille.txt deleted file mode 100755 index 9d5dd50..0000000 --- a/fille.txt +++ /dev/null @@ -1,17 +0,0 @@ -total 1376 -drwxrwxr-x 2 simplonco simplonco 4096 oct. 30 15:32 builds --rw-rw-r-- 1 simplonco simplonco 0 oct. 28 21:00 cat --rw-rw-r-- 1 simplonco simplonco 125 oct. 28 21:00 echo --rwxrwxr-x 1 simplonco simplonco 150 oct. 28 21:03 file.txt --rwx------ 1 simplonco simplonco 0 oct. 30 15:33 fille.txt -drwxrwxr-x 2 simplonco simplonco 4096 oct. 28 21:10 headers -drwxrwxr-x 8 simplonco simplonco 4096 oct. 23 20:48 libft --rw-rw-r-- 1 simplonco simplonco 1412 oct. 29 12:32 Makefile --rwxrwxr-x 1 simplonco simplonco 72256 oct. 30 15:32 minishell --rw-rw-r-- 1 simplonco simplonco 1273353 oct. 7 21:29 minishell.en.subject.pdf --rw-rw-r-- 1 simplonco simplonco 3365 oct. 27 14:47 parsing.txt --rw-rw-r-- 1 simplonco simplonco 15216 oct. 28 21:00 README.md -drwxrwxr-x 2 simplonco simplonco 4096 oct. 28 21:00 ressources -drwxrwxr-x 4 simplonco simplonco 4096 oct. 30 15:32 srcs -drwxrwxr-x 2 simplonco simplonco 4096 oct. 27 14:47 tests --rw-rw-r-- 1 simplonco simplonco 58 oct. 13 17:38 valgrind_readline.supp diff --git a/srcs/parsing/cmd_expansion.c b/srcs/parsing/cmd_expansion.c deleted file mode 100644 index de464db..0000000 --- a/srcs/parsing/cmd_expansion.c +++ /dev/null @@ -1,28 +0,0 @@ - -#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 deleted file mode 100644 index e354814..0000000 --- a/srcs/parsing/fill_cmd.c +++ /dev/null @@ -1,159 +0,0 @@ - -#include "minishell.h" - -int nbr_pipes(t_token *token) -{ - int nb; - - nb = 0; - while (token) - { - if (token->id == T_PIPE) - nb++; - token = token->next; - } - return (nb); -} - -int nbr_argv(t_token *token) -{ - int i; - - i = 0; - while (token && token->id != T_PIPE) - { - token = token->next; - i++; - } - return (i); -} - -//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; -// -// (*cmd)->fd_out = 1; -// while (token && token->id != T_PIPE) -// { -// // T_LESS : '<', -// if (token->id == T_LESS) -// { -// if ((*cmd)->fd_in != 0) -// close((*cmd)->fd_in); -// (*cmd)->fd_in = open(token->next->content, O_RDONLY | O_CREAT); -// token->id = T_TOKEN; -// token->next->id = T_TOKEN; -// } -// // T_GREAT : '>', -// if (token->id == T_GREAT) -// { -// (*cmd)->fd_out = open(token->next->content, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU); -// token->id = T_TOKEN; -// token->next->id = T_TOKEN; -// } -// // T_DGREAT : '>>' -// if (token->id == T_DGREAT) -// { -// (*cmd)->fd_out = open(token->next->content, O_WRONLY | O_CREAT | O_APPEND, S_IRWXU); -// token->id = T_TOKEN; -// token->next->id = T_TOKEN; -// } -// // T_DLESS : '<<' (heredoc) -// // if (token->id == T_DGREAT) -// // { -// // cmd->fd_out = open(token->next->content, O_WRONLY | O_TMPFILE | O_APPEND, S_IRWXU); -// // token->out = T_TOKEN; -// // token->next->id = T_TOKEN; -// // } -// token = token->next; -// } -// if (token && token->id == T_PIPE) -// { -// pipes_fd = calloc(2, sizeof(int)); -// pipe(pipes_fd); -// if ((*cmd)->fd_out == 1) -// (*cmd)->fd_out = pipes_fd[1]; -// else -// close(pipes_fd[1]); -// cmd[1]->fd_in = pipes_fd[0]; -// } -//} - -//int handle_builtin(t_token *token, t_cmd *cmd) -//{ -// (void)token; -// (void)cmd; -// return (0); -//} -// -//void handle_cmd(char **argv, char **envp) -//{ -// int i; -// char **path; -// char *cmd; -// -// i = 0; -// while (envp[i] && ft_strncmp(envp[i], "PATH=", 5)) -// i++; -// path = ft_split(envp[i] + 5, ':'); // 5 = lentgh of "PATH=" -// i = -1; -// while (*path && i != 0) -// { -// cmd = ft_strjoin(path[0], "/"); -// cmd = ft_strjoin(cmd, argv[0]); -// i = access(cmd, X_OK); -// path++; -// } -// argv[0] = cmd; -//} - -/* -t_cmd **fill_cmd(t_token *token, char **envp) -{ - t_cmd **cmd; - 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)); -// } - i = 0; - while (i <= pipes) - { - handle_fd(token, cmd + i); - handle_argv(&token, cmd[i]); - if (!handle_builtin(token, cmd[i])) - handle_cmd(cmd[i]->argv, envp); - i++; - } - return(cmd); -} -*/ diff --git a/srcs/parsing/parsing.c b/srcs/parsing/parsing.c index d961b5a..bbbd9db 100644 --- a/srcs/parsing/parsing.c +++ b/srcs/parsing/parsing.c @@ -1,6 +1,35 @@ #include "minishell.h" +size_t count_pipes(t_token *token); +t_cmd **create_cmd(t_token *token_list, size_t cmd_nbr); +void handle_argv(t_token *token, t_cmd **cmd, size_t cmd_nbr); +void handle_path(t_cmd **cmd_arr, char **envp); +void handle_fd(t_token *token, t_cmd **cmd_arr); +void fd_heredoc(t_token *token, t_cmd *cmd); +void fd_redirection(t_token *token, t_cmd *cmd); +void find_path(char **argv, char **envp); +int handle_builtin(t_cmd *cmd); +int fill_builtin(t_cmd *cmd, int (*builtin)(int, char **, t_all *)); +int next_cmd(t_token *token); +int is_redirection(enum e_token_id id); + +t_cmd **parsing(t_token *token_list, char **envp) +{ + t_cmd **cmd_arr; + size_t cmd_nbr; + + if (!valid_syntax(token_list)) + return (NULL); + cmd_nbr = count_pipes(token_list); + cmd_arr = create_cmd(token_list, cmd_nbr); +// cmd_expansion(cmd_arr, envp); + handle_argv(token_list, cmd_arr, cmd_nbr); + handle_path(cmd_arr, envp); + handle_fd(token_list, cmd_arr); + return (cmd_arr); +} + size_t count_pipes(t_token *token) { size_t nb; @@ -186,12 +215,6 @@ void fd_heredoc(t_token *token, t_cmd *cmd) { (void)token; (void)cmd; - // if (token->id == T_DGREAT) // << - // { - // cmd->fd_out = open(token->next->content, O_WRONLY | O_TMPFILE | O_APPEND, S_IRWXU); - // token->out = T_TOKEN; - // token->next->id = T_TOKEN; - // } } void handle_fd(t_token *token, t_cmd **cmd_arr) @@ -226,31 +249,6 @@ void handle_fd(t_token *token, t_cmd **cmd_arr) } } -t_cmd **parsing(t_token *token_list, char **envp) -{ - t_cmd **cmd_arr; - size_t cmd_nbr; - - if (!valid_syntax(token_list)) - return (NULL); - cmd_nbr = count_pipes(token_list); - cmd_arr = create_cmd(token_list, cmd_nbr); -// cmd_expansion(cmd_arr, envp); - handle_argv(token_list, cmd_arr, cmd_nbr); - handle_path(cmd_arr, envp); - handle_fd(token_list, cmd_arr); - -//int j; -//j = 0; -//while (cmd_arr[j]) -//{ -// printf("%i\n", j); -// print_matrix(cmd_arr[j]->argv, " ; "); -// j++; -//} - - return (cmd_arr); -} /* ------------------------------------------------------- The grammar symbols diff --git a/srcs/shell_loop.c b/srcs/shell_loop.c index 4283afd..f0fb765 100644 --- a/srcs/shell_loop.c +++ b/srcs/shell_loop.c @@ -6,13 +6,60 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */ -/* Updated: 2021/10/30 15:32:49 by hulamy ### ########.fr */ +/* Updated: 2021/11/02 13:37:12 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -static char **tokens_list_to_argv(t_token *t); // temp test +void close_fd(t_cmd *cmd); +void execute_cmd(char **envp, t_cmd **cmd_arr); + +void shell_loop(t_all *c) +{ + char *line_input; + + line_input = NULL; + while (1) + { + if (line_input) + free(line_input); + line_input = readline(c->prompt); + if (line_input && *line_input) + { + add_history(line_input); + c->token_list = input_to_tokens(line_input); + c->cmd_arr = parsing(c->token_list, c->envp); + execute_cmd(c->envp, c->cmd_arr); + ft_lstclear((t_list **)&c->token_list, free); + } + } +} + +void wip_test() +{ + char term_desc[2048]; + char *term_type; + int term_width; + int term_height; + int ret; + + term_type = getenv("TERM"); + if (term_type == 0) + ft_putstr_fd("Specify a terminal type with `setenv TERM '.\n", 2); + ret = tgetent(term_desc, term_type); + if (ret < 0) + ft_putstr_fd("Could not access the termcap data base.\n", 2); + if (ret == 0) + ft_putstr_fd("Terminal type `%s' is not defined.\n", 2); + term_height = tgetnum ("li"); + term_width = tgetnum ("co"); + /* Extract information that termcap functions use. */ +/* temp = tgetstr ("pc", BUFFADDR); + PC = temp ? *temp : 0; + BC = tgetstr ("le", BUFFADDR); + UP = tgetstr ("up", BUFFADDR); */ +} void close_fd(t_cmd *cmd) { @@ -49,89 +96,3 @@ void execute_cmd(char **envp, t_cmd **cmd_arr) // waitpid pour la derniere commande (pour '$?') while ((wpid = wait(&status)) > 0); } - -void shell_loop(t_all *c) -{ - char *line_input; - - line_input = NULL; - while (1) - { - if (line_input) - free(line_input); - line_input = readline(c->prompt); - if (line_input && *line_input) - { - add_history(line_input); - c->token_list = input_to_tokens(line_input); - - // EXEC_PIPES_AND_CO() - - // temp placeholder - (void)tokens_list_to_argv; - // if (ft_strncmp(c->token_list->content, "env", 4) == 0) - // builtin_env(0, NULL, c); - // else if (ft_strncmp(c->token_list->content, "exit", 5) == 0) - // builtin_exit(0, NULL, c); - // else if (ft_strncmp(c->token_list->content, "echo", 5) == 0) - // builtin_echo(ft_lstsize((t_list *)c->token_list) + 1, tokens_list_to_argv(c->token_list), c); - // else - // { - - 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); - ft_lstclear((t_list **)&c->token_list, free); - // } - } - } -} - -static char **tokens_list_to_argv(t_token *t) // temp test -{ - size_t i; - char **argv; - - i = ft_lstsize((t_list *)t); - argv = ft_calloc(i + 1, sizeof(char*)); - i = 0; - while (t) - { - argv[i] = t->content; - i++; - t = t->next; - } - return (argv); -} - -void wip_test() -{ - char term_desc[2048]; - char *term_type; - int term_width; - int term_height; - int ret; - - term_type = getenv("TERM"); - if (term_type == 0) - ft_putstr_fd("Specify a terminal type with `setenv TERM '.\n", 2); - ret = tgetent(term_desc, term_type); - if (ret < 0) - ft_putstr_fd("Could not access the termcap data base.\n", 2); - if (ret == 0) - ft_putstr_fd("Terminal type `%s' is not defined.\n", 2); - term_height = tgetnum ("li"); - term_width = tgetnum ("co"); - /* Extract information that termcap functions use. */ -/* temp = tgetstr ("pc", BUFFADDR); - PC = temp ? *temp : 0; - BC = tgetstr ("le", BUFFADDR); - UP = tgetstr ("up", BUFFADDR); */ -}