Skip to content

Commit

Permalink
updates files from master branch 👻
Browse files Browse the repository at this point in the history
manually copied the changed files from the master branch; there are
still issues with macros and the new executor 📦
  • Loading branch information
paotsaq committed Oct 26, 2021
1 parent 0319d10 commit 06ae135
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 32 deletions.
1 change: 0 additions & 1 deletion minishell/headers/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ void del_env_var(void *env_var_void);
void free_env_and_paths(t_minishell *ms);

// REDIR_UTIL
bool is_redir_in(t_redir_type type);
int ft_set_dup(int old_fd);
bool ft_set_dup2(int dup_from_fd, int dup_to_fd);
bool ft_init_pipe_fd(int pipe_fd[2]);
Expand Down
2 changes: 1 addition & 1 deletion minishell/headers/executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


// EXECUTOR
int executor(t_minishell *ms, t_list *curr, int cmd_exit_code);
t_list *executor(t_minishell *ms, t_list *curr);

// EXEC_STD_CMD
int exec_std_cmd(t_cmd *cmd);
Expand Down
4 changes: 3 additions & 1 deletion minishell/headers/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ typedef enum e_inst_type
{
INSTR_CMD,
INSTR_OR,
INSTR_AND
INSTR_AND,
INSTR_GRP_START,
INSTR_GRP_END
} t_instr_type;

typedef struct s_instruction
Expand Down
7 changes: 0 additions & 7 deletions minishell/srcs/core/redir_util.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
#include "minishell.h"

bool is_redir_in(t_redir_type type)
{
if (type == REDIR_HERE_DOC || type == REDIR_IN)
return (true);
return (false);
}

int ft_set_dup(int old_fd)
{
int new_fd;
Expand Down
57 changes: 40 additions & 17 deletions minishell/srcs/executor/executor.c
Original file line number Diff line number Diff line change
@@ -1,28 +1,51 @@
#include "minishell.h"

static int exec_instr(t_cmd *cmd, t_std_io *std_io, t_list **redirect);
static int exec_cmd(t_cmd *cmd);
static t_list *next_group_pos(t_list *curr);
static int exec_instr(t_cmd *cmd, t_std_io *std_io, t_list **redirect);
static int exec_cmd(t_cmd *cmd);

int executor(t_minishell *ms, t_list *curr, int cmd_exit_code)
t_list *executor(t_minishell *ms, t_list *curr)
{
t_instruction *instr;
int curr_group;

if (curr)
curr_group = ((t_instruction *)curr->content)->cmd->group;
while (curr)
if (!curr)
return (NULL);
instr = (t_instruction *)curr->content;
if (instr->type == INSTR_GRP_START || instr->type == INSTR_GRP_END)
return (executor(ms, curr->next));
else if (instr->type == INSTR_OR)
{
instr = (t_instruction *)curr->content;
if (instr->type == INSTR_CMD && instr->cmd->group != curr_group)
return (executor(ms, curr, cmd_exit_code));
else if (instr->type == INSTR_CMD)
cmd_exit_code = exec_instr(instr->cmd, &ms->streams, ms->redirect);
else if ((instr->type == INSTR_OR && cmd_exit_code == EXIT_SUCCESS)
|| (instr->type == INSTR_AND && cmd_exit_code != EXIT_SUCCESS))
return (cmd_exit_code);
curr = curr->next;
if (ms->exit_code == EXIT_SUCCESS)
return (executor(ms, next_group_pos(curr)));
else
return (executor(ms, curr->next));
}
else if (instr->type == INSTR_AND)
{
if (ms->exit_code != EXIT_SUCCESS)
return (executor(ms, next_group_pos(curr)));
else
return (executor(ms, curr->next));
}
else
{
while (curr && instr->type == INSTR_CMD)
{
get_minishell(NULL)->exit_code = exec_instr(instr->cmd,
&ms->streams, ms->redirect);
curr = curr->next;
if (curr)
instr = (t_instruction *)curr->content;
}
return (executor(ms, curr));
}
return (cmd_exit_code);
}

static t_list *next_group_pos(t_list *curr)
{
while (curr && ((t_instruction *)curr->content)->type != INSTR_GRP_END)
curr = curr->next;
return (curr);
}

static int exec_instr(t_cmd *cmd, t_std_io *std_io, t_list **redirect)
Expand Down
33 changes: 30 additions & 3 deletions minishell/srcs/executor/setup_redirect.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "minishell.h"

static bool setup_heredoc_redir(t_std_io *std_io, t_list *curr, int id,
t_has_redirs *has_redirs);
static bool setup_pipe_redir(t_std_io *std_io, t_list *curr, int id,
t_has_redirs *has_redirs);
static bool setup_other_redir(t_std_io *std_io, t_list *curr, int id,
Expand All @@ -11,6 +13,8 @@ bool setup_redirect(t_std_io *std_io, t_list **redirect, int id)

has_redirs.in = false;
has_redirs.out = false;
if (!setup_heredoc_redir(std_io, *redirect, id, &has_redirs))
return (false);
if (!setup_pipe_redir(std_io, *redirect, id, &has_redirs))
return (false);
if (!setup_other_redir(std_io, *redirect, id, &has_redirs))
Expand All @@ -19,6 +23,28 @@ bool setup_redirect(t_std_io *std_io, t_list **redirect, int id)
return (true);
}

static bool setup_heredoc_redir(t_std_io *std_io, t_list *curr, int id,
t_has_redirs *has_redirs)
{
t_redirect *curr_redir;

while (curr)
{
curr_redir = (t_redirect *)curr->content;
if (curr_redir->cmd_id == id
&& curr_redir->type == REDIR_HERE_DOC)
{
has_redirs->in = true;
if (!make_redir(std_io, curr_redir))
return (false);
}
else if (curr_redir->cmd_id > id)
break ;
curr = curr->next;
}
return (true);
}

static bool setup_pipe_redir(t_std_io *std_io, t_list *curr, int id,
t_has_redirs *has_redirs)
{
Expand Down Expand Up @@ -54,10 +80,11 @@ static bool setup_other_redir(t_std_io *std_io, t_list *curr, int id,
{
curr_redir = (t_redirect *)curr->content;
if (curr_redir->cmd_id == id
&& curr_redir->type != REDIR_PIPE_IN
&& curr_redir->type != REDIR_PIPE_OUT)
&& (curr_redir->type == REDIR_IN
|| curr_redir->type == REDIR_OUT
|| curr_redir->type == REDIR_OUT_APPEND))
{
if (is_redir_in(curr_redir->type))
if (curr_redir->type == REDIR_IN)
has_redirs->in = true;
else
has_redirs->out = true;
Expand Down
2 changes: 1 addition & 1 deletion minishell/srcs/non_interactive_mode.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void process_line(t_minishell *ms, char *line)
ms = parser(line, ms);
if (prog_state(TAKE_STATE) == PROG_OK)
{
ms->exit_code = executor(ms, *ms->instructions, ms->exit_code);
executor(ms, *ms->instructions);
restore_std_io(false, false);
}
else
Expand Down
11 changes: 10 additions & 1 deletion minishell/srcs/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ t_minishell *parser(char *line, t_minishell *minishell)
return (minishell);
perform_expansions(minishell->tokens);
parse_tokens(*minishell->tokens, 0, 0);
DEBUG(print_tokens(minishell->tokens);)
DEBUG(print_instructions(minishell->instructions);)
DEBUG(print_redirections(minishell->redirect);)
}
return (minishell);
}
Expand All @@ -22,14 +25,20 @@ void parse_tokens(t_list *curr_node, int cmd_id, int cmd_group)
t_token *curr_token;

if (!curr_node)
return ;
return;
curr_token = get_token(curr_node);
if (curr_token->op_type == OP_LOGIC)
parse_tokens(parse_logical_op(curr_node), cmd_id, cmd_group);
else if (curr_token->op_type == OP_PARENS_OPEN)
{
init_instruction(get_minishell(NULL), INSTR_GRP_START);
parse_tokens(curr_node->next, cmd_id, cmd_group + 1);
}
else if (curr_token->op_type == OP_PARENS_CLOSE)
{
init_instruction(get_minishell(NULL), INSTR_GRP_END);
parse_tokens(curr_node->next, cmd_id, cmd_group - 1);
}
else if (curr_token->op_type == OP_PIPE)
parse_tokens(parse_pipe(curr_node, cmd_id - 1), cmd_id, cmd_group);
else
Expand Down
2 changes: 2 additions & 0 deletions minishell/srcs/parser/parser_tasks.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ t_list *parse_pipe(t_list *curr_node, int cmd_id)
return (curr_node->next);
}

/* the function checks for consecutive redirections */
/* argument next_node is useless now and can be removed */
t_list *parse_redir(t_list *curr_node, int cmd_id)
{
t_token *token;
Expand Down

0 comments on commit 06ae135

Please sign in to comment.