Skip to content

Commit

Permalink
Merge pull request #13 from dimadem/dimadem_tree_execution
Browse files Browse the repository at this point in the history
Dimadem tree execution
  • Loading branch information
dimadem authored Jun 28, 2024
2 parents 6a2c3c4 + 88024db commit 16a3e29
Showing 7 changed files with 76 additions and 26 deletions.
1 change: 0 additions & 1 deletion inc/tokens.h
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 5 additions & 5 deletions src/execute/execute.c
Original file line number Diff line number Diff line change
@@ -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);
Empty file added src/io_redirection/heredoc.c
Empty file.
52 changes: 52 additions & 0 deletions src/io_redirection/utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "shell.h"
#include <fcntl.h>
#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);
}
23 changes: 17 additions & 6 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -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);
}


}
}

10 changes: 0 additions & 10 deletions src/parser/parser.c
Original file line number Diff line number Diff line change
@@ -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);
6 changes: 2 additions & 4 deletions src/pipe/pipe.c
Original file line number Diff line number Diff line change
@@ -18,9 +18,7 @@
#include <sys/wait.h>
#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));
}

0 comments on commit 16a3e29

Please sign in to comment.