From 73e576f109cb53bdbee234a23af544109ae67f6d Mon Sep 17 00:00:00 2001 From: tomoron Date: Fri, 19 Apr 2024 14:55:01 +0200 Subject: [PATCH] fix and , or --- srcs/commands.c | 5 +++-- srcs/exec_bonus.c | 46 ++++++++++++++++++++++---------------- srcs/input_redirections.c | 4 ++-- srcs/minishell.h | 3 +-- srcs/output_redirections.c | 17 +++++++------- todo_list | 1 + 6 files changed, 42 insertions(+), 34 deletions(-) diff --git a/srcs/commands.c b/srcs/commands.c index 32c7892..0872192 100755 --- a/srcs/commands.c +++ b/srcs/commands.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 18:22:15 by marde-vr #+# #+# */ -/* Updated: 2024/04/19 10:40:13 by tomoron ### ########.fr */ +/* Updated: 2024/04/19 14:42:50 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -65,7 +65,8 @@ void remove_command_from_msh(t_msh *msh) free_token(msh->tokens); while (msh->cmds && !is_cmd_type(msh->cmds)) msh->cmds = msh->cmds->next; - while (msh->cmds && is_cmd_type(msh->cmds)) + while (msh->cmds && (is_cmd_type(msh->cmds) || is_input_type(msh->cmds) + || is_output_type(msh->cmds))) msh->cmds = msh->cmds->next; tmp = msh->cmds; while (tmp && !is_cmd_type(tmp)) diff --git a/srcs/exec_bonus.c b/srcs/exec_bonus.c index 32e9ad4..76ea957 100755 --- a/srcs/exec_bonus.c +++ b/srcs/exec_bonus.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/28 13:50:14 by tomoron #+# #+# */ -/* Updated: 2024/04/19 13:46:21 by marde-vr ### ########.fr */ +/* Updated: 2024/04/19 14:53:53 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,6 +28,22 @@ void get_redirections(t_msh *msh, t_cmd *cmds) } } +t_cmd *get_next_command(t_cmd *cmd) +{ + while(cmd) + { + while(cmd && !is_operand_type(cmd)) + cmd = cmd->next; + if(cmd && cmd->cmd_type == AND && !g_return_code) + return(cmd->next); + if(cmd && cmd->cmd_type == OR && g_return_code) + return(cmd->next); + if(cmd) + cmd = cmd->next; + } + return(0); +} + void exec_command_bonus(t_msh *msh, char *cmd_str) { t_cmd *cmds; @@ -45,22 +61,15 @@ void exec_command_bonus(t_msh *msh, char *cmd_str) } while (cmds) { - if ((cmds->cmd_type == AND && !g_return_code) || (cmds->cmd_type == OR - && g_return_code) || !is_operand_type(cmds)) - { - if (is_operand_type(cmds)) - cmds = cmds->next; - msh->tokens = parse_cmds_to_token(cmds, msh->env); - msh->cmds = cmds; - //print_msh_struct(msh); // debug - print_parsed_token(msh->tokens); // debug - exec_commands(msh); - msh->in_fd = 0; - msh->out_fd = 0; - } - while (cmds && (is_cmd_type(cmds) || cmds->cmd_type == PIPE - || is_output_type(cmds) || is_input_type(cmds))) - cmds = cmds->next; + print_parsed_cmd(cmds); // debug + msh->tokens = parse_cmds_to_token(cmds, msh->env); + msh->cmds = cmds; + //print_msh_struct(msh); // debug + // print_parsed_token(msh->tokens); // debug + exec_commands(msh); + msh->in_fd = 0; + msh->out_fd = 0; + cmds = get_next_command(cmds); } } @@ -131,8 +140,7 @@ void end_execution(t_msh *msh, int cmd_count) i = 0; while (i < cmd_count) - waitpid(msh->pids[i++], &status, 0); - if (!g_return_code && WIFEXITED(status)) + waitpid(msh->pids[i++], WIFEXITED(status)) g_return_code = WEXITSTATUS(status); if (WIFSIGNALED(status) && WTERMSIG(status) == SIGQUIT) printf("Quit\n"); diff --git a/srcs/input_redirections.c b/srcs/input_redirections.c index b0144da..861071c 100755 --- a/srcs/input_redirections.c +++ b/srcs/input_redirections.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 18:15:27 by marde-vr #+# #+# */ -/* Updated: 2024/04/19 13:46:32 by marde-vr ### ########.fr */ +/* Updated: 2024/04/19 14:49:43 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,7 +48,7 @@ int open_input_file(t_msh *msh, t_cmd **cur_token) ft_exit(msh, 1); msh->in_fd = open(filename->value, O_RDONLY); free_token(filename); - if (msh->in_fd == -1 && !g_return_code) + if (msh->in_fd == -1) { fprintf(stderr, "minishell: %s: ", (*cur_token)->next->value); perror(""); diff --git a/srcs/minishell.h b/srcs/minishell.h index 41515d3..77db57a 100755 --- a/srcs/minishell.h +++ b/srcs/minishell.h @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */ -/* Updated: 2024/04/19 13:51:04 by tomoron ### ########.fr */ +/* Updated: 2024/04/19 14:09:25 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -98,7 +98,6 @@ void exec_commands(t_msh *msh); void handle_here_doc(t_msh *msh, char *eof); int get_in_type(t_msh *msh, t_cmd *tokens); void signal_handler_interactive(int signum); -void get_out_type(t_msh *msh, t_cmd *cmds); int get_token_len(char *cmd, t_env *env); void print_syntax_error_bonus(t_cmd *cmd); void signal_handler_here_doc(int signum); diff --git a/srcs/output_redirections.c b/srcs/output_redirections.c index 88e1cbf..867af90 100755 --- a/srcs/output_redirections.c +++ b/srcs/output_redirections.c @@ -3,14 +3,10 @@ /* ::: :::::::: */ /* output_redirections.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: marde-vr +#+ +:+ +#+ */ +/* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2024/03/05 19:10:52 by marde-vr #+# #+# */ -<<<<<<< HEAD -/* Updated: 2024/04/19 09:45:21 by tomoron ### ########.fr */ -======= -/* Updated: 2024/04/19 13:33:50 by marde-vr ### ########.fr */ ->>>>>>> 7ae7a09 (fixed priorities and return codes) +/* Created: 2024/04/19 14:09:44 by tomoron #+# #+# */ +/* Updated: 2024/04/19 14:10:30 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -60,13 +56,15 @@ int open_out_file(t_msh *msh, t_cmd **cur_cmd, char *filename) return (0); } -void get_out_type(t_msh *msh, t_cmd *cmds) +int get_out_type(t_msh *msh, t_cmd *cmds) { t_cmd *cur_cmd; t_token *filename; + int ret; msh->out_type = CMD; msh->out_fd = 0; + ret = 0; cur_cmd = cmds; while (cur_cmd && !is_cmd_type(cur_cmd)) cur_cmd = cur_cmd->next; @@ -87,9 +85,10 @@ void get_out_type(t_msh *msh, t_cmd *cmds) filename = parse_tokens(cur_cmd->value, msh->env); if (!filename) ft_exit(msh, 1); - open_out_file(msh, &cur_cmd, filename->value); + ret = open_out_file(msh, &cur_cmd, filename->value); free_token(filename); } else if (cur_cmd && cur_cmd->cmd_type == PIPE) msh->out_type = PIPE; + return(ret); } diff --git a/todo_list b/todo_list index 51abd94..00b2ec2 100644 --- a/todo_list +++ b/todo_list @@ -6,6 +6,7 @@ fix exit | exit exec export in fork when piped here doc in fork +pipe with parenthesis LEAKS add cmds_head in msh_struct to free in runtime