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); - } + // } } } }