From 118e2e5bef3b2a41cb668268c9e305545932e28d Mon Sep 17 00:00:00 2001 From: hugogogo Date: Sat, 30 Oct 2021 15:27:41 +0200 Subject: [PATCH] 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;