From e05465348d60d83cbafbd87b9354ed29283a7e80 Mon Sep 17 00:00:00 2001 From: mdev9 Date: Mon, 1 Apr 2024 11:17:12 +0200 Subject: [PATCH 1/3] wip --- srcs/exec_bonus.c | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/srcs/exec_bonus.c b/srcs/exec_bonus.c index 74914c3..c91769b 100644 --- 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/03/30 17:29:07 by marde-vr ### ########.fr */ +/* Updated: 2024/04/01 11:16:05 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,16 +17,32 @@ void exec_command_bonus(t_msh *msh, char *cmd_str) t_cmd *cmds; (void)msh; - printf("cmd : %s\n",cmd_str); + //printf("cmd : %s\n",cmd_str); cmds = parsing_bonus(cmd_str); - printf("%p\n", cmds); - msh->tokens = parse_command(cmd_str, msh->env); - msh->cmds = cmds; - print_parsed_cmd(cmds); - exec_commands(msh); + //printf("%p\n", cmds); + while (cmds && cmds->next) + { + if (cmds->cmd_type == CMD) + { + msh->tokens = parse_command(cmds->value, msh->env); + print_parsed_cmd(cmds); + print_parsed_token(msh->tokens); + msh->cmds = cmds; + exec_commands(msh); + } + cmds = cmds->next; + } + if (cmds && cmds->cmd_type == CMD) + { + msh->tokens = parse_command(cmds->value, msh->env); + print_parsed_cmd(cmds); + print_parsed_token(msh->tokens); + msh->cmds = cmds; + exec_commands(msh); + } } -int exec(t_msh *msh, char **cmd_args, int i, int cmd_count) +int exec(t_msh *msh, char **cmd_args, int i, int cmd_count, int is_pipe) { pid_t pid; @@ -45,7 +61,7 @@ int exec(t_msh *msh, char **cmd_args, int i, int cmd_count) ft_exit(msh, 1); } if (pid == 0) - child(msh, cmd_args, i); + child(msh, cmd_args, i, is_pipe); else { parent(msh, i, cmd_count); @@ -75,7 +91,7 @@ void exec_command(t_msh *msh, int i, int cmd_count) } if (!cmd_is_builtin(msh, msh->cmds->value)) get_cmd_path(msh); - exec(msh, get_cmd_args(msh), i, cmd_count); + exec(msh, get_cmd_args(msh), i, cmd_count, 1); remove_command_from_msh(msh); } @@ -113,7 +129,6 @@ void exec_commands(t_msh *msh) int i; i = -1; - ft_printf("yes\n"); if (!msh->cmds) return ; cmd_count = get_cmd_count(msh->cmds); From 7d1fba69a9984278605f76e3646373335c697a55 Mon Sep 17 00:00:00 2001 From: mdev9 Date: Mon, 1 Apr 2024 13:59:34 +0200 Subject: [PATCH 2/3] modified cmds to tokens --- srcs/builtins.c | 8 ++++---- srcs/commands.c | 30 +++++++++++++++--------------- srcs/exec_bonus.c | 31 ++++++++++++++++--------------- srcs/exit.c | 6 +++--- srcs/export.c | 6 +++--- srcs/input_redirections.c | 4 ++-- srcs/minishell.h | 9 ++++----- srcs/output_redirections.c | 6 +++--- srcs/pipe.c | 2 +- srcs/utils.c | 4 ++-- 10 files changed, 53 insertions(+), 53 deletions(-) diff --git a/srcs/builtins.c b/srcs/builtins.c index eaaf604..d5037a4 100644 --- a/srcs/builtins.c +++ b/srcs/builtins.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 18:20:21 by marde-vr #+# #+# */ -/* Updated: 2024/03/30 18:40:49 by marde-vr ### ########.fr */ +/* Updated: 2024/04/01 13:22:14 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,7 +27,7 @@ int cmd_is_builtin(t_msh *msh, char *cmd_token) return (0); else if (!ft_strcmp(cmd_token, "cd")) { - cd(msh->tokens); + cd(msh->cmds); return (1); } else if (!ft_strcmp(cmd_token, "exit")) @@ -54,7 +54,7 @@ int exec_builtin(t_msh *msh) if (!msh->cmds->value) return (0); if (!ft_strcmp(msh->cmds->value, "echo")) - g_return_code = echo(msh->tokens->next); + g_return_code = echo(msh->cmds->next); else if (!ft_strcmp(msh->cmds->value, "ret")) g_return_code = ft_atoi(msh->cmds->next->value); else if (!ft_strcmp(msh->cmds->value, "env")) @@ -64,7 +64,7 @@ int exec_builtin(t_msh *msh) else if (!ft_strcmp(msh->cmds->value, "pwd")) g_return_code = pwd(); else if (!ft_strcmp(msh->cmds->value, "cd")) - g_return_code = cd(msh->tokens); + g_return_code = cd(msh->cmds); else return (0); return (1); diff --git a/srcs/commands.c b/srcs/commands.c index b0e459a..d3da7c0 100644 --- a/srcs/commands.c +++ b/srcs/commands.c @@ -6,45 +6,45 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 18:22:15 by marde-vr #+# #+# */ -/* Updated: 2024/03/30 17:25:20 by marde-vr ### ########.fr */ +/* Updated: 2024/04/01 13:27:40 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int get_cmd_count(t_cmd *cmds) +int get_cmd_count(t_token *cmds) { int count; - t_cmd *cur_cmd; - (void)cmds; + t_token *cur_cmd; + count = 0; cur_cmd = cmds; while (cur_cmd->next != 0) { - if (cur_cmd->cmd_type != PIPE) + if (cur_cmd->type != PIPE) count++; cur_cmd = cur_cmd->next; } - if (cur_cmd->cmd_type != PIPE) + if (cur_cmd->type != PIPE) count++; return (count); } -int get_args_count(t_cmd *cmds) +int get_args_count(t_token *cmds) { int count; - t_cmd *cur_cmd; + t_token *cur_cmd; count = 0; cur_cmd = cmds; - if (cur_cmd->cmd_type == ARG) + if (cur_cmd->type == ARG) count++; while (cur_cmd->next) { if (/*cur_cmd->type == PIPE*/ 0) break ; cur_cmd = cur_cmd->next; - if (cur_cmd->cmd_type == ARG) + if (cur_cmd->type == ARG) count++; else if (/*cur_cmd->type != PIPE*/ 1) cur_cmd = cur_cmd->next; @@ -55,7 +55,7 @@ int get_args_count(t_cmd *cmds) char **get_cmd_args(t_msh *msh) { char **cmd_args; - t_cmd *cur_cmd; + t_token *cur_cmd; int args_count; int i; @@ -67,7 +67,7 @@ char **get_cmd_args(t_msh *msh) i = 0; while (i < args_count) { - if (cur_cmd->cmd_type == ARG) + if (cur_cmd->type == ARG) { if (!i) cmd_args[i++] = remove_path(cur_cmd->value); @@ -83,8 +83,8 @@ char **get_cmd_args(t_msh *msh) void remove_command_from_msh(t_msh *msh) { - t_cmd *cur_cmd; - t_cmd *cmd_tmp; + t_token *cur_cmd; + t_token *cmd_tmp; cur_cmd = msh->cmds; while (cur_cmd && cur_cmd->next) @@ -93,7 +93,7 @@ void remove_command_from_msh(t_msh *msh) { cmd_tmp = cur_cmd; cur_cmd = cur_cmd->next; - //msh->in_type = cmd_tmp->cmd_type; + //msh->in_type = cmd_tmp->type; free(cmd_tmp->value); free(cmd_tmp); msh->cmds = cur_cmd; diff --git a/srcs/exec_bonus.c b/srcs/exec_bonus.c index c91769b..a198ca1 100644 --- 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/01 11:16:05 by marde-vr ### ########.fr */ +/* Updated: 2024/04/01 13:58:58 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,6 @@ void exec_command_bonus(t_msh *msh, char *cmd_str) { t_cmd *cmds; - (void)msh; //printf("cmd : %s\n",cmd_str); cmds = parsing_bonus(cmd_str); //printf("%p\n", cmds); @@ -24,25 +23,27 @@ void exec_command_bonus(t_msh *msh, char *cmd_str) { if (cmds->cmd_type == CMD) { - msh->tokens = parse_command(cmds->value, msh->env); + if (cmds->next->cmd_type == PIPE) + { + + } + msh->cmds = parse_command(cmds->value, msh->env); print_parsed_cmd(cmds); - print_parsed_token(msh->tokens); - msh->cmds = cmds; + print_parsed_token(msh->cmds); exec_commands(msh); } cmds = cmds->next; } if (cmds && cmds->cmd_type == CMD) { - msh->tokens = parse_command(cmds->value, msh->env); + msh->cmds = parse_command(cmds->value, msh->env); print_parsed_cmd(cmds); - print_parsed_token(msh->tokens); - msh->cmds = cmds; + print_parsed_token(msh->cmds); exec_commands(msh); } } -int exec(t_msh *msh, char **cmd_args, int i, int cmd_count, int is_pipe) +int exec(t_msh *msh, char **cmd_args, int i, int cmd_count) { pid_t pid; @@ -61,7 +62,7 @@ int exec(t_msh *msh, char **cmd_args, int i, int cmd_count, int is_pipe) ft_exit(msh, 1); } if (pid == 0) - child(msh, cmd_args, i, is_pipe); + child(msh, cmd_args, i); else { parent(msh, i, cmd_count); @@ -79,19 +80,19 @@ void exec_command(t_msh *msh, int i, int cmd_count) ft_exit(msh, 1); if (first_is_in_type(msh)) { - get_in_type(msh, msh->tokens); + get_in_type(msh, msh->cmds); if (!g_return_code) - get_out_type(msh, msh->tokens); + get_out_type(msh, msh->cmds); } else { - get_out_type(msh, msh->tokens); + get_out_type(msh, msh->cmds); if (!g_return_code) - get_in_type(msh, msh->tokens); + get_in_type(msh, msh->cmds); } if (!cmd_is_builtin(msh, msh->cmds->value)) get_cmd_path(msh); - exec(msh, get_cmd_args(msh), i, cmd_count, 1); + exec(msh, get_cmd_args(msh), i, cmd_count); remove_command_from_msh(msh); } diff --git a/srcs/exit.c b/srcs/exit.c index 7816c66..1685e59 100755 --- a/srcs/exit.c +++ b/srcs/exit.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/07 16:04:11 by tomoron #+# #+# */ -/* Updated: 2024/03/30 18:41:32 by marde-vr ### ########.fr */ +/* Updated: 2024/04/01 13:19:41 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,7 +24,7 @@ void get_exit_bt_return_code(t_msh *msh, int *exit_code) { t_token *cur_cmd; - cur_cmd = msh->tokens->next; + cur_cmd = msh->cmds->next; if (cur_cmd && cur_cmd->type == ARG && !ft_strisnbr(cur_cmd->value)) numeric_arg_err(cur_cmd->value, exit_code); else if (cur_cmd && cur_cmd->type == ARG) @@ -39,7 +39,7 @@ int exit_bt(t_msh *msh) int exit_code; int cmd_count; - cur_cmd = msh->tokens->next; + cur_cmd = msh->cmds->next; ft_printf("exit\n"); if (cur_cmd && cur_cmd->next && cur_cmd->next->type == ARG && ft_strisnbr(cur_cmd->value)) diff --git a/srcs/export.c b/srcs/export.c index d2b14c9..4227597 100644 --- a/srcs/export.c +++ b/srcs/export.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/18 18:29:20 by marde-vr #+# #+# */ -/* Updated: 2024/03/30 18:45:02 by marde-vr ### ########.fr */ +/* Updated: 2024/04/01 13:20:34 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,7 +44,7 @@ int ft_export(t_msh *msh) char *value; int len; - cmd = msh->tokens; + cmd = msh->cmds; if (cmd && (!cmd->next || (cmd->next && cmd->next->type != ARG))) print_env_declare(msh->env); if (cmd && cmd->next && cmd->next->type == ARG && (!cmd->next->next @@ -94,7 +94,7 @@ int ft_unset(t_msh *msh) { t_token *cmd; - cmd = msh->tokens; + cmd = msh->cmds; if (cmd) cmd = cmd->next; while (cmd && cmd->type == ARG) diff --git a/srcs/input_redirections.c b/srcs/input_redirections.c index 3abf7f6..a6ede37 100644 --- 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/03/30 17:09:09 by marde-vr ### ########.fr */ +/* Updated: 2024/04/01 13:20:54 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -69,7 +69,7 @@ int first_is_in_type(t_msh *msh) { t_token *cur_token; - cur_token = msh->tokens; + cur_token = msh->cmds; while (cur_token && cur_token->type == ARG && cur_token->next) cur_token = cur_token->next; if (cur_token->type == RED_I || cur_token->type == HERE_DOC) diff --git a/srcs/minishell.h b/srcs/minishell.h index 70f9811..e936734 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/03/30 17:15:14 by marde-vr ### ########.fr */ +/* Updated: 2024/04/01 13:24:39 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -68,8 +68,7 @@ typedef struct s_env typedef struct s_msh { t_env *env; - t_cmd *cmds; - t_token *tokens; + t_token *cmds; int **fds; int *pids; t_token_type in_type; @@ -116,10 +115,10 @@ void print_parsed_token(t_token *cmd);//debug int get_var_name_len(char *command); void handle_minishellrc(t_msh *msh); char *get_tmp_file_name(t_msh *msh); -int get_args_count(t_cmd *cmds); +int get_args_count(t_token *cmds); char **env_to_char_tab(t_env *env); void print_parsed_cmd(t_cmd *cmd);//debug -int get_cmd_count(t_cmd *cmds); +int get_cmd_count(t_token *cmds); int first_is_in_type(t_msh *msh); int contains_newline(char *str); int check_var_name(char *name); diff --git a/srcs/output_redirections.c b/srcs/output_redirections.c index af4f3c0..1188eea 100644 --- a/srcs/output_redirections.c +++ b/srcs/output_redirections.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 19:10:52 by marde-vr #+# #+# */ -/* Updated: 2024/03/30 18:44:30 by marde-vr ### ########.fr */ +/* Updated: 2024/04/01 13:21:28 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -58,9 +58,9 @@ void get_out_type(t_msh *msh, t_token *cmds) msh->out_type = ARG; msh->out_fd = 0; cur_cmd = cmds; - if (cmds->type && msh->tokens == cmds) + if (cmds->type && msh->cmds == cmds) { - while (msh->cmds->cmd_type != ARG && msh->cmds->next->next) + while (msh->cmds->type != ARG && msh->cmds->next->next) msh->cmds = msh->cmds->next->next; } while (cur_cmd && cur_cmd->next && (cur_cmd->type == ARG diff --git a/srcs/pipe.c b/srcs/pipe.c index 2b9015a..68d5307 100644 --- a/srcs/pipe.c +++ b/srcs/pipe.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 18:17:25 by marde-vr #+# #+# */ -/* Updated: 2024/03/28 13:18:35 by tomoron ### ########.fr */ +/* Updated: 2024/04/01 13:59:08 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/srcs/utils.c b/srcs/utils.c index 13605c3..881db1b 100644 --- a/srcs/utils.c +++ b/srcs/utils.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 18:19:26 by marde-vr #+# #+# */ -/* Updated: 2024/03/30 18:45:27 by marde-vr ### ########.fr */ +/* Updated: 2024/04/01 13:28:00 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ void free_msh(t_msh *msh) free_env(msh->env); free(msh->pids); free(msh->fds); - free_token(msh->tokens); + free_token(msh->cmds); set_echoctl(msh->echoctl); free(msh); } From dc73084edebd22c87ee14a56914352b348318f05 Mon Sep 17 00:00:00 2001 From: mdev9 Date: Mon, 1 Apr 2024 21:39:33 +0200 Subject: [PATCH 3/3] working on fixing pipes --- Makefile | 4 +-- srcs/commands.c | 4 +-- srcs/exec_bonus.c | 58 +++++++++++++------------------- srcs/exit.c | 12 +------ srcs/here_doc.c | 3 +- srcs/input_redirections.c | 6 ++-- srcs/minishell.h | 7 ++-- srcs/output_redirections.c | 6 ++-- srcs/pipe.c | 52 +++++++++++----------------- srcs/signal_handler.c | 3 +- srcs/utils.c | 3 +- srcs/{utils2.c => utils_bonus.c} | 0 12 files changed, 61 insertions(+), 97 deletions(-) rename srcs/{utils2.c => utils_bonus.c} (100%) diff --git a/Makefile b/Makefile index 7d1c2ef..7cb7d22 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: marde-vr +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/07/28 00:35:01 by tomoron #+# #+# # -# Updated: 2024/03/29 14:34:57 by tomoron ### ########.fr # +# Updated: 2024/04/01 20:09:57 by marde-vr ### ########.fr # # # # **************************************************************************** # @@ -34,7 +34,7 @@ SRCS_RAW = main.c\ commands.c\ pipe.c\ utils.c\ - utils2.c\ + utils_bonus.c\ signal_handler.c\ parsing_bonus.c\ exec_bonus.c diff --git a/srcs/commands.c b/srcs/commands.c index d3da7c0..b6daee8 100644 --- 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/01 13:27:40 by marde-vr ### ########.fr */ +/* Updated: 2024/04/01 20:01:21 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -61,7 +61,7 @@ char **get_cmd_args(t_msh *msh) args_count = get_args_count(msh->cmds); cmd_args = ft_calloc(args_count + 1, sizeof(char *)); - if (!cmd_args || !msh->fds) + if (!cmd_args) ft_exit(msh, 1); cur_cmd = msh->cmds; i = 0; diff --git a/srcs/exec_bonus.c b/srcs/exec_bonus.c index a198ca1..c8314df 100644 --- a/srcs/exec_bonus.c +++ b/srcs/exec_bonus.c @@ -6,12 +6,28 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/28 13:50:14 by tomoron #+# #+# */ -/* Updated: 2024/04/01 13:58:58 by marde-vr ### ########.fr */ +/* Updated: 2024/04/01 21:39:20 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" +void get_redirections(t_msh *msh) +{ + if (first_is_in_type(msh)) + { + get_in_type(msh, msh->cmds); + if (!g_return_code) + get_out_type(msh, msh->cmds); + } + else + { + get_out_type(msh, msh->cmds); + if (!g_return_code) + get_in_type(msh, msh->cmds); + } +} + void exec_command_bonus(t_msh *msh, char *cmd_str) { t_cmd *cmds; @@ -23,11 +39,8 @@ void exec_command_bonus(t_msh *msh, char *cmd_str) { if (cmds->cmd_type == CMD) { - if (cmds->next->cmd_type == PIPE) - { - - } msh->cmds = parse_command(cmds->value, msh->env); + get_redirections(msh); print_parsed_cmd(cmds); print_parsed_token(msh->cmds); exec_commands(msh); @@ -37,6 +50,7 @@ void exec_command_bonus(t_msh *msh, char *cmd_str) if (cmds && cmds->cmd_type == CMD) { msh->cmds = parse_command(cmds->value, msh->env); + get_redirections(msh); print_parsed_cmd(cmds); print_parsed_token(msh->cmds); exec_commands(msh); @@ -46,14 +60,17 @@ void exec_command_bonus(t_msh *msh, char *cmd_str) int exec(t_msh *msh, char **cmd_args, int i, int cmd_count) { pid_t pid; + int fds[2]; if (i != cmd_count - 1) { - if (pipe(msh->fds[i]) == -1) + if (pipe(fds) == -1) { perror("pipe"); ft_exit(msh, 1); } + msh->in_fd = fds[0]; + msh->out_fd = fds[1]; } pid = fork(); if (pid == -1) @@ -75,21 +92,6 @@ int exec(t_msh *msh, char **cmd_args, int i, int cmd_count) void exec_command(t_msh *msh, int i, int cmd_count) { g_return_code = 0; - msh->fds[i] = ft_calloc(2, sizeof(int *)); - if (!msh->fds[i]) - ft_exit(msh, 1); - if (first_is_in_type(msh)) - { - get_in_type(msh, msh->cmds); - if (!g_return_code) - get_out_type(msh, msh->cmds); - } - else - { - get_out_type(msh, msh->cmds); - if (!g_return_code) - get_in_type(msh, msh->cmds); - } if (!cmd_is_builtin(msh, msh->cmds->value)) get_cmd_path(msh); exec(msh, get_cmd_args(msh), i, cmd_count); @@ -108,15 +110,6 @@ void end_execution(t_msh *msh, int cmd_count) g_return_code = WEXITSTATUS(status); if (WIFSIGNALED(status) && WTERMSIG(status) == SIGQUIT) printf("Quit (core dumped)\n"); - i = 0; - while (i < cmd_count) - { - free(msh->fds[i]); - msh->fds[i] = 0; - i++; - } - free(msh->fds); - msh->fds = 0; free(msh->pids); msh->pids = 0; //signal(SIGINT, signal_handler_interactive); //enables ctrl-C @@ -130,13 +123,10 @@ void exec_commands(t_msh *msh) int i; i = -1; - if (!msh->cmds) - return ; cmd_count = get_cmd_count(msh->cmds); ft_printf("cmd_count: %d\n", cmd_count); - msh->fds = ft_calloc(cmd_count, sizeof(int **)); msh->pids = ft_calloc(cmd_count, sizeof(int *)); - if (!msh->pids || !msh->fds) + if (!msh->pids) ft_exit(msh, 1); while (++i < cmd_count) exec_command(msh, i, cmd_count); diff --git a/srcs/exit.c b/srcs/exit.c index 1685e59..fe6103c 100755 --- a/srcs/exit.c +++ b/srcs/exit.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/07 16:04:11 by tomoron #+# #+# */ -/* Updated: 2024/04/01 13:19:41 by marde-vr ### ########.fr */ +/* Updated: 2024/04/01 20:07:47 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,7 +37,6 @@ int exit_bt(t_msh *msh) { t_token *cur_cmd; int exit_code; - int cmd_count; cur_cmd = msh->cmds->next; ft_printf("exit\n"); @@ -47,15 +46,6 @@ int exit_bt(t_msh *msh) else { get_exit_bt_return_code(msh, &exit_code); - if (msh->fds) - { - cmd_count = get_cmd_count(msh->cmds); - while (cmd_count) - { - free(msh->fds[cmd_count - 1]); - cmd_count--; - } - } free_msh(msh); exit(exit_code); } diff --git a/srcs/here_doc.c b/srcs/here_doc.c index cf43ebe..d6fd2e8 100644 --- a/srcs/here_doc.c +++ b/srcs/here_doc.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/24 17:44:32 by marde-vr #+# #+# */ -/* Updated: 2024/03/26 17:52:26 by tomoron ### ########.fr */ +/* Updated: 2024/04/01 20:06:47 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,7 +43,6 @@ void here_doc_child(t_msh *msh, char *eof, char *here_doc_file) get_here_doc_input(msh, eof); close(msh->in_fd); free(here_doc_file); - free(msh->fds[0]); ft_exit(msh, 0); } diff --git a/srcs/input_redirections.c b/srcs/input_redirections.c index a6ede37..dd0933c 100644 --- a/srcs/input_redirections.c +++ b/srcs/input_redirections.c @@ -6,13 +6,13 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 18:15:27 by marde-vr #+# #+# */ -/* Updated: 2024/04/01 13:20:54 by marde-vr ### ########.fr */ +/* Updated: 2024/04/01 20:08:48 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void redirect_input(t_msh *msh, int i) +void redirect_input(t_msh *msh) { if (/*msh->in_type != PIPE*/ 1) { @@ -22,7 +22,7 @@ void redirect_input(t_msh *msh, int i) } else { - if (dup2(msh->fds[i - 1][0], 0) < 0) + if (dup2(msh->in_fd, 0) < 0) ft_exit(msh, 1); } } diff --git a/srcs/minishell.h b/srcs/minishell.h index e936734..36238ee 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/01 13:24:39 by marde-vr ### ########.fr */ +/* Updated: 2024/04/01 20:09:38 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -69,7 +69,6 @@ typedef struct s_msh { t_env *env; t_token *cmds; - int **fds; int *pids; t_token_type in_type; t_token_type out_type; @@ -106,10 +105,10 @@ void remove_command_from_msh(t_msh *msh); void ft_exit(t_msh *msh, int error_code); void signal_handler_command(int signum); void ft_exit(t_msh *msh, int exit_code); -void redirect_output(t_msh *msh, int i); +void redirect_output(t_msh *msh); char **split_paths_from_env(t_env *env); int add_return_code_to_str(char *res); -void redirect_input(t_msh *msh, int i); +void redirect_input(t_msh *msh); void parse_var(t_msh *msh, char *line); void print_parsed_token(t_token *cmd);//debug int get_var_name_len(char *command); diff --git a/srcs/output_redirections.c b/srcs/output_redirections.c index 1188eea..5157e45 100644 --- a/srcs/output_redirections.c +++ b/srcs/output_redirections.c @@ -6,13 +6,13 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 19:10:52 by marde-vr #+# #+# */ -/* Updated: 2024/04/01 13:21:28 by marde-vr ### ########.fr */ +/* Updated: 2024/04/01 20:09:27 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void redirect_output(t_msh *msh, int i) +void redirect_output(t_msh *msh) { if (/*msh->out_type != PIPE*/ 1) { @@ -21,7 +21,7 @@ void redirect_output(t_msh *msh, int i) } else { - if (dup2(msh->fds[i][1], 1) < 0) + if (dup2(msh->out_fd, 1) < 0) ft_exit(msh, 1); } } diff --git a/srcs/pipe.c b/srcs/pipe.c index 68d5307..8ab6174 100644 --- a/srcs/pipe.c +++ b/srcs/pipe.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 18:17:25 by marde-vr #+# #+# */ -/* Updated: 2024/04/01 13:59:08 by marde-vr ### ########.fr */ +/* Updated: 2024/04/01 20:09:14 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,30 +16,24 @@ void close_pipe_fds(t_msh *msh, int i) { if (i != 0) { - if (msh->fds[i - 1][0] > 2) - close(msh->fds[i - 1][0]); - if (msh->fds[i - 1][1] > 2) - close(msh->fds[i - 1][1]); + if (msh->in_fd > 2) + close(msh->in_fd); + if (msh->out_fd > 2) + close(msh->out_fd); } - if (msh->fds[i][0] > 2) - close(msh->fds[i][0]); - if (msh->fds[i][1] > 2) - close(msh->fds[i][1]); + if (msh->in_fd > 2) + close(msh->out_fd); + if (msh->in_fd > 2) + close(msh->in_fd); } -void execute_command(t_msh *msh, char **cmd_args, int i) +void execute_command(t_msh *msh, char **cmd_args) { char **env; if (msh->cmds->value && (!ft_strcmp(msh->cmds->value, "cd") || !ft_strcmp(msh->cmds->value, "exit") || exec_builtin(msh))) { - while (i >= 0) - { - free(msh->fds[i]); - msh->fds[i] = 0; - i--; - } free(cmd_args); ft_exit(msh, g_return_code); } @@ -56,21 +50,15 @@ void child(t_msh *msh, char **cmd_args, int i) { if ((msh->in_type != ARG/* && msh->in_type != PIPE*/) || (/*msh->in_type == PIPE &&*/ i > 0)) - redirect_input(msh, i); + redirect_input(msh); if (/*msh->out_type == PIPE ||*/ msh->out_type == RED_O || msh->out_type == RED_O_APP) redirect_output(msh, i); close_pipe_fds(msh, i); - execute_command(msh, cmd_args, i); + execute_command(msh, cmd_args); close(0); close(1); close(2); - while (i >= 0) - { - free(msh->fds[i]); - msh->fds[i] = 0; - i--; - } free(cmd_args); ft_exit(msh, g_return_code); } @@ -81,17 +69,17 @@ void parent(t_msh *msh, int i, int cmd_count) signal(SIGQUIT, signal_handler_command); if (i != 0) { - if (msh->fds[i - 1][0] > 2) - close(msh->fds[i - 1][0]); - if (msh->fds[i - 1][1] > 2) - close(msh->fds[i - 1][1]); + if (msh->in_fd > 2) + close(msh->in_fd); + if (msh->out_fd > 2) + close(msh->out_fd); } if (i == cmd_count - 1) { - if (msh->fds[i][0] > 2) - close(msh->fds[i][0]); - if (msh->fds[i][1] > 2) - close(msh->fds[i][1]); + if (msh->in_fd > 2) + close(msh->in_fd); + if (msh->out_fd > 2) + close(msh->out_fd); } if (msh->in_fd > 2) close(msh->in_fd); diff --git a/srcs/signal_handler.c b/srcs/signal_handler.c index 373f909..0b2f39c 100644 --- a/srcs/signal_handler.c +++ b/srcs/signal_handler.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/22 14:31:13 by tomoron #+# #+# */ -/* Updated: 2024/03/27 17:21:09 by tomoron ### ########.fr */ +/* Updated: 2024/04/01 20:10:09 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,7 +49,6 @@ void signal_handler_here_doc(int signum) here_doc_file = here_doc_variables(0, 1, 0); close(msh->in_fd); free(here_doc_file); - free(msh->fds[0]); ft_exit(msh, 1); } } diff --git a/srcs/utils.c b/srcs/utils.c index 881db1b..ce0ae7a 100644 --- a/srcs/utils.c +++ b/srcs/utils.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 18:19:26 by marde-vr #+# #+# */ -/* Updated: 2024/04/01 13:28:00 by marde-vr ### ########.fr */ +/* Updated: 2024/04/01 20:06:36 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,6 @@ void free_msh(t_msh *msh) { free_env(msh->env); free(msh->pids); - free(msh->fds); free_token(msh->cmds); set_echoctl(msh->echoctl); free(msh); diff --git a/srcs/utils2.c b/srcs/utils_bonus.c similarity index 100% rename from srcs/utils2.c rename to srcs/utils_bonus.c