diff --git a/inc/tokens.h b/inc/tokens.h index 9bdfb83a..739e0d70 100644 --- a/inc/tokens.h +++ b/inc/tokens.h @@ -48,7 +48,6 @@ typedef struct s_token typedef struct s_ast { t_token_type type; - bool incomplete; char **args; struct s_ast *left; struct s_ast *right; diff --git a/src/execute/execute.c b/src/execute/execute.c index c61ec445..600b1dba 100644 --- a/src/execute/execute.c +++ b/src/execute/execute.c @@ -42,27 +42,27 @@ int execute_ast(t_ast *node, t_minishell_data *data) data->args = node->args; return (execute(data)); } - else if (node->type == ENV_VAR) + else if (node->type == ENV_VAR) // "$()" { printf(BLU"ENV_VAR\n"RESET); //execute_redirect(node, data); } else if (node->type == REDIR_IN) { - printf(CYA"REDIR_IN\n"RESET); + printf(CYA"REDIR_IN\n"RESET); // "<" //execute_sequence(node, data); } - else if (node->type == REDIR_OUT) + else if (node->type == REDIR_OUT) // ">" { printf(CYA"REDIR_OUT\n"RESET); //execute_sequence(node, data); } - else if (node->type == REDIR_APPEND) + else if (node->type == REDIR_APPEND) // ">>" { printf(CYA"REDIR_APPEND\n"RESET); //execute_sequence(node, data); } - else if (node->type == REDIR_HEREDOC) + else if (node->type == REDIR_HEREDOC) // "<<" { printf(MAG"REDIR_HEREDOC\n"RESET); //execute_sequence(node, data); diff --git a/src/io_redirection/heredoc.c b/src/io_redirection/heredoc.c new file mode 100644 index 00000000..e69de29b diff --git a/src/io_redirection/utils.c b/src/io_redirection/utils.c new file mode 100644 index 00000000..ec55417b --- /dev/null +++ b/src/io_redirection/utils.c @@ -0,0 +1,52 @@ +#include "shell.h" +#include +#include "libft.h" +#include "tokens.h" + +int open(t_minishell_data *data, char *direction); +void redirect(t_minishell_data *data, int fd, char *direction); +int execute_redirection(t_ast *node, t_minishell_data *data); + +int open(t_minishell_data *data, char *direction) +{ + int fd; + + if (ft_strcmp(direction, ">") == 0) + fd = open(data->args[0], O_WRONLY | O_CREAT | O_TRUNC, 0644); + else if (ft_strcmp(direction, ">>") == 0) + fd = open(data->args[0], O_WRONLY | O_CREAT | O_APPEND, 0644); + else if (ft_strcmp(direction, "<") == 0) + fd = open(data->args[0], O_RDONLY); + else + fd = -1; + return (fd); +} + +void redirect(t_minishell_data *data, int fd, char *direction) +{ + if (ft_strcmp(direction, ">") == 0 || ft_strcmp(direction, ">>") == 0) + dup2(fd, STDOUT_FILENO); + else if (ft_strcmp(direction, "<") == 0) + dup2(fd, STDIN_FILENO); + close(fd); +} + +int execute_redirection(t_ast *node, t_minishell_data *data) +{ + int fd; + size_t i; + + i = 0; + while (node->args[i] != NULL) + { + if (ft_strcmp(node->args[i], ">") == 0 || ft_strcmp(node->args[i], ">>") == 0 || ft_strcmp(node->args[i], "<") == 0) + { + fd = open(data, node->args[i]); + if (fd == -1) + return (1); + redirect(data, fd, node->args[i]); + } + i++; + } + return (0); +} diff --git a/src/main.c b/src/main.c index d8745201..daeba35b 100644 --- a/src/main.c +++ b/src/main.c @@ -15,6 +15,17 @@ #include "shell.h" #include "pipe.h" +int status_handler(int status, t_loop_data *loop_data) +{ + if (status == WAIT_NEXT_COMMAND) + { + loop_cleanup(loop_data->trimmed_input, loop_data->tokens, \ + loop_data->prompt, loop_data->tree); + return (0); + } + return (1); +} + void main_loop(t_minishell_data *data, t_loop_data *loop_data) { int status; @@ -28,16 +39,16 @@ void main_loop(t_minishell_data *data, t_loop_data *loop_data) // input_error_checks(loop_data->trimmed_input); loop_data->tokens = tokenise(loop_data->trimmed_input); loop_data->tree = parse_tokens(&loop_data->tokens); + print_ast_root(loop_data->tree); status = execute_ast(loop_data->tree, data); - if (status == WAIT_NEXT_COMMAND) + if (status_handler(status, loop_data)) { + handle_temp_fd(data); loop_cleanup(loop_data->trimmed_input, loop_data->tokens, \ - loop_data->prompt, loop_data->tree); - continue ; - } - handle_temp_fd(data); - loop_cleanup(loop_data->trimmed_input, loop_data->tokens, \ loop_data->prompt, loop_data->tree); + } + + } } diff --git a/src/parser/parser.c b/src/parser/parser.c index 2947216f..caee36bd 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -12,10 +12,6 @@ #include "tokens.h" -/* - REFACTORING -*/ - t_ast *clr_node(t_token **tokens, t_token *next_token, t_ast *redirect_node); t_ast *manage_redirs(t_token **tokens); t_ast *create_redir_node(t_token *token); @@ -102,15 +98,9 @@ t_ast *manage_pipe(t_token **tokens) (*tokens)->next = NULL; pipe_node->left = manage_redirs(&tmp); if (next_token->next == NULL) - { pipe_node->right = NULL; - pipe_node->incomplete = true; - } else - { pipe_node->right = manage_pipe(&(next_token->next)); - pipe_node->incomplete = false; - } free(next_token->data); free(next_token); return (pipe_node); diff --git a/src/pipe/pipe.c b/src/pipe/pipe.c index b2facc41..dbd495d8 100644 --- a/src/pipe/pipe.c +++ b/src/pipe/pipe.c @@ -18,9 +18,7 @@ #include #include "pipe.h" -int ft_perror(char *str); int builtin_pipe(t_ast *node, t_minishell_data *data); -void close_fds(int fds[2]); pid_t execute_child(t_ast *node, t_minishell_data *data, \ int fd[2], int direction); @@ -35,7 +33,7 @@ int builtin_pipe(t_ast *node, t_minishell_data *data) if (pipe(fd) == -1) ft_perror("pipe"); pid_1 = execute_child(node->left, data, fd, 0); - if (!node->incomplete) + if (node->right != NULL) pid_2 = execute_child(node->right, data, fd, 1); else { @@ -46,7 +44,7 @@ int builtin_pipe(t_ast *node, t_minishell_data *data) close_fds(fd); if (pid_1 > 0) waitpid(pid_1, &status, 0); - if (!node->incomplete && pid_2 > 0) + if (node->right != NULL && pid_2 > 0) waitpid(pid_2, &status, 0); return (WEXITSTATUS(status)); }