From bd6fba08fe7ede7f92629c1146ec2fbd4a567022 Mon Sep 17 00:00:00 2001 From: mdev9 Date: Sat, 30 Mar 2024 18:58:39 +0100 Subject: [PATCH] working on bonus execution --- srcs/builtins.c | 8 +-- srcs/commands.c | 35 ++++++----- srcs/exec.c | 5 +- srcs/exec_bonus.c | 116 +++++++++++++++++++++++++++++++++++-- srcs/exit.c | 6 +- srcs/export.c | 6 +- srcs/input_redirections.c | 53 +++++++++-------- srcs/main.c | 4 +- srcs/minishell.h | 33 ++++++----- srcs/output_redirections.c | 6 +- srcs/utils.c | 4 +- 11 files changed, 190 insertions(+), 86 deletions(-) diff --git a/srcs/builtins.c b/srcs/builtins.c index 59d5079..eaaf604 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/28 13:28:19 by tomoron ### ########.fr */ +/* Updated: 2024/03/30 18:40:49 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->cmds); + cd(msh->tokens); 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->cmds->next); + g_return_code = echo(msh->tokens->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->cmds); + g_return_code = cd(msh->tokens); else return (0); return (1); diff --git a/srcs/commands.c b/srcs/commands.c index 5c7bb38..b0e459a 100644 --- a/srcs/commands.c +++ b/srcs/commands.c @@ -6,46 +6,45 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 18:22:15 by marde-vr #+# #+# */ -/* Updated: 2024/03/28 13:35:09 by tomoron ### ########.fr */ +/* Updated: 2024/03/30 17:25:20 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int get_cmd_count(t_token *cmds) +int get_cmd_count(t_cmd *cmds) { int count; - t_token *cur_cmd; - + t_cmd *cur_cmd; + (void)cmds; count = 0; cur_cmd = cmds; while (cur_cmd->next != 0) { - if (cur_cmd->type != ARG) - if (/*cur_cmd->type == PIPE*/ 0) + if (cur_cmd->cmd_type != PIPE) count++; cur_cmd = cur_cmd->next; } - if (cur_cmd->type == ARG) + if (cur_cmd->cmd_type != PIPE) count++; return (count); } -int get_args_count(t_token *cmds) +int get_args_count(t_cmd *cmds) { int count; - t_token *cur_cmd; + t_cmd *cur_cmd; count = 0; cur_cmd = cmds; - if (cur_cmd->type == ARG) + if (cur_cmd->cmd_type == ARG) count++; while (cur_cmd->next) { if (/*cur_cmd->type == PIPE*/ 0) break ; cur_cmd = cur_cmd->next; - if (cur_cmd->type == ARG) + if (cur_cmd->cmd_type == ARG) count++; else if (/*cur_cmd->type != PIPE*/ 1) cur_cmd = cur_cmd->next; @@ -56,7 +55,7 @@ int get_args_count(t_token *cmds) char **get_cmd_args(t_msh *msh) { char **cmd_args; - t_token *cur_cmd; + t_cmd *cur_cmd; int args_count; int i; @@ -68,7 +67,7 @@ char **get_cmd_args(t_msh *msh) i = 0; while (i < args_count) { - if (cur_cmd->type == ARG) + if (cur_cmd->cmd_type == ARG) { if (!i) cmd_args[i++] = remove_path(cur_cmd->value); @@ -84,8 +83,8 @@ char **get_cmd_args(t_msh *msh) void remove_command_from_msh(t_msh *msh) { - t_token *cur_cmd; - t_token *cmd_tmp; + t_cmd *cur_cmd; + t_cmd *cmd_tmp; cur_cmd = msh->cmds; while (cur_cmd && cur_cmd->next) @@ -94,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->type; + //msh->in_type = cmd_tmp->cmd_type; free(cmd_tmp->value); free(cmd_tmp); msh->cmds = cur_cmd; @@ -102,10 +101,10 @@ void remove_command_from_msh(t_msh *msh) } cmd_tmp = cur_cmd; cur_cmd = cur_cmd->next; - msh->in_type = cur_cmd->type; + //msh->in_type = cur_cmd->type; free(cmd_tmp->value); free(cmd_tmp); msh->cmds = cur_cmd; } - msh->in_type = msh->cmds->type; + //msh->in_type = msh->cmds->type; } diff --git a/srcs/exec.c b/srcs/exec.c index e9d8119..d0d9b81 100755 --- a/srcs/exec.c +++ b/srcs/exec.c @@ -6,10 +6,11 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/07 14:12:49 by tomoron #+# #+# */ -/* Updated: 2024/03/27 16:52:02 by tomoron ### ########.fr */ +/* Updated: 2024/03/30 16:55:17 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ +/* #include "minishell.h" #include @@ -110,4 +111,4 @@ void exec_commands(t_msh *msh) while (++i < cmd_count) exec_command(msh, i, cmd_count); end_execution(msh, cmd_count); -} +}*/ diff --git a/srcs/exec_bonus.c b/srcs/exec_bonus.c index 390792c..74914c3 100644 --- a/srcs/exec_bonus.c +++ b/srcs/exec_bonus.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* exec_bonus.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: tomoron +#+ +:+ +#+ */ +/* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/28 13:50:14 by tomoron #+# #+# */ -/* Updated: 2024/03/29 14:47:40 by tomoron ### ########.fr */ +/* Updated: 2024/03/30 17:29:07 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,11 +14,115 @@ void exec_command_bonus(t_msh *msh, char *cmd_str) { - t_cmd *cmd; + t_cmd *cmds; (void)msh; printf("cmd : %s\n",cmd_str); - cmd = parsing_bonus(cmd_str); - printf("%p\n", cmd); - print_parsed_cmd(cmd); + 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); +} + +int exec(t_msh *msh, char **cmd_args, int i, int cmd_count) +{ + pid_t pid; + + if (i != cmd_count - 1) + { + if (pipe(msh->fds[i]) == -1) + { + perror("pipe"); + ft_exit(msh, 1); + } + } + pid = fork(); + if (pid == -1) + { + perror("fork"); + ft_exit(msh, 1); + } + if (pid == 0) + child(msh, cmd_args, i); + else + { + parent(msh, i, cmd_count); + msh->pids[i] = pid; + free(cmd_args); + } + return (0); +} + +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->tokens); + if (!g_return_code) + get_out_type(msh, msh->tokens); + } + else + { + get_out_type(msh, msh->tokens); + if (!g_return_code) + get_in_type(msh, msh->tokens); + } + if (!cmd_is_builtin(msh, msh->cmds->value)) + get_cmd_path(msh); + exec(msh, get_cmd_args(msh), i, cmd_count); + remove_command_from_msh(msh); +} + +void end_execution(t_msh *msh, int cmd_count) +{ + int i; + int status; + + i = 0; + while (i < cmd_count) + waitpid(msh->pids[i++], &status, 0); + if (!g_return_code && WIFEXITED(status)) + 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 + signal(SIGQUIT, signal_handler_interactive); + set_echoctl(0); +} + +void exec_commands(t_msh *msh) +{ + int cmd_count; + int i; + + i = -1; + ft_printf("yes\n"); + 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) + ft_exit(msh, 1); + while (++i < cmd_count) + exec_command(msh, i, cmd_count); + end_execution(msh, cmd_count); } diff --git a/srcs/exit.c b/srcs/exit.c index 44e469e..7816c66 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/27 16:24:45 by tomoron ### ########.fr */ +/* Updated: 2024/03/30 18:41:32 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->cmds->next; + cur_cmd = msh->tokens->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->cmds->next; + cur_cmd = msh->tokens->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 ae87291..d2b14c9 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/27 16:55:22 by tomoron ### ########.fr */ +/* Updated: 2024/03/30 18:45:02 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,7 +44,7 @@ int ft_export(t_msh *msh) char *value; int len; - cmd = msh->cmds; + cmd = msh->tokens; 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->cmds; + cmd = msh->tokens; if (cmd) cmd = cmd->next; while (cmd && cmd->type == ARG) diff --git a/srcs/input_redirections.c b/srcs/input_redirections.c index 6af2867..3abf7f6 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/28 13:26:42 by tomoron ### ########.fr */ +/* Updated: 2024/03/30 17:09:09 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,53 +27,52 @@ void redirect_input(t_msh *msh, int i) } } -void open_input_file(t_msh *msh, t_token **cur_cmd) +void open_input_file(t_msh *msh, t_token **cur_token) { - if ((*cur_cmd)->type == HERE_DOC) - handle_here_doc(msh, (*cur_cmd)->next->value); - if ((*cur_cmd)->type == RED_I) + if ((*cur_token)->type == HERE_DOC) + handle_here_doc(msh, (*cur_token)->next->value); + if ((*cur_token)->type == RED_I) { if (msh->in_fd != 0) close(msh->in_fd); - msh->in_fd = open((*cur_cmd)->next->value, O_RDONLY); + msh->in_fd = open((*cur_token)->next->value, O_RDONLY); if (msh->in_fd == -1 && !g_return_code) { - ft_printf_fd(2, "minishell: %s: ", (*cur_cmd)->next->value); + ft_printf_fd(2, "minishell: %s: ", (*cur_token)->next->value); perror(""); g_return_code = 1; } } } -void get_in_type(t_msh *msh, t_token *cmds) +void get_in_type(t_msh *msh, t_token *tokens) { - t_token *cur_cmd; + t_token *cur_token; - cur_cmd = cmds; - while (cur_cmd && cur_cmd->next && cur_cmd->type == ARG) - cur_cmd = cur_cmd->next; - if (cur_cmd->type) + cur_token = tokens; + while (cur_token && cur_token->next && cur_token->type == ARG) + cur_token = cur_token->next; + if (cur_token->type) { - msh->in_type = cur_cmd->type; - if (cur_cmd->type == HERE_DOC || cur_cmd->type == RED_I) - open_input_file(msh, &cur_cmd); + msh->in_type = cur_token->type; + if (cur_token->type == HERE_DOC || cur_token->type == RED_I) + open_input_file(msh, &cur_token); } - while (cur_cmd && cur_cmd->next && cur_cmd->next->type == ARG) - cur_cmd = cur_cmd->next; - if (cur_cmd->next && (cur_cmd->next->type == HERE_DOC - || cur_cmd->next->type == RED_I)) - get_in_type(msh, cur_cmd); + while (cur_token && cur_token->next && cur_token->next->type == ARG) + cur_token = cur_token->next; + if (cur_token->next && (cur_token->next->type == HERE_DOC + || cur_token->next->type == RED_I)) + get_in_type(msh, cur_token); } int first_is_in_type(t_msh *msh) { - t_token *cur_cmd; + t_token *cur_token; - cur_cmd = msh->cmds; - while (cur_cmd && cur_cmd->type == ARG && cur_cmd->next) - cur_cmd = cur_cmd->next; - if (/*cur_cmd->type == PIPE || */cur_cmd->type == RED_I - || cur_cmd->type == HERE_DOC) + cur_token = msh->tokens; + 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) return (1); return (0); } diff --git a/srcs/main.c b/srcs/main.c index ceee7bd..64bf65e 100755 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/02 21:59:20 by tomoron #+# #+# */ -/* Updated: 2024/03/29 14:45:43 by tomoron ### ########.fr */ +/* Updated: 2024/03/30 17:21:23 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -83,7 +83,7 @@ int init_minishell(t_msh **msh, int argc, char **argv, char **envp) (*msh)->env = get_env(envp); tcgetattr(1, &t_p); (*msh)->echoctl = t_p.c_lflag & ECHOCTL; - //signal(SIGINT, signal_handler_interactive); + //signal(SIGINT, signal_handler_interactive); //enables ctrl-C signal(SIGQUIT, signal_handler_interactive); if (set_echoctl(0)) ft_exit(*msh, 1); diff --git a/srcs/minishell.h b/srcs/minishell.h index 285cf6c..70f9811 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/29 14:36:18 by tomoron ### ########.fr */ +/* Updated: 2024/03/30 17:15:14 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,8 +36,8 @@ typedef enum e_cmd_type typedef struct s_cmd { - t_cmd_type cmd_type; - char *value; + t_cmd_type cmd_type; + char *value; struct s_cmd *next; } t_cmd; @@ -67,16 +67,17 @@ typedef struct s_env typedef struct s_msh { - struct s_env *env; - struct s_token *cmds; - int **fds; - int *pids; - enum e_token_type in_type; - enum e_token_type out_type; - int in_fd; - int out_fd; - int locked_return_code; - int echoctl; + t_env *env; + t_cmd *cmds; + t_token *tokens; + int **fds; + int *pids; + t_token_type in_type; + t_token_type out_type; + int in_fd; + int out_fd; + int locked_return_code; + int echoctl; } t_msh; extern int g_return_code; @@ -95,7 +96,7 @@ void parent(t_msh *msh, int i, int cmd_count); char *ft_get_env(t_env *env, char *var_name); void get_out_type(t_msh *msh, t_token *cmds); void handle_here_doc(t_msh *msh, char *eof); -void get_in_type(t_msh *msh, t_token *cmds); +void get_in_type(t_msh *msh, t_token *tokens); void signal_handler_interactive(int signum); int get_token_len(char *cmd, t_env *env); void signal_handler_here_doc(int signum); @@ -115,10 +116,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_token *cmds); +int get_args_count(t_cmd *cmds); char **env_to_char_tab(t_env *env); void print_parsed_cmd(t_cmd *cmd);//debug -int get_cmd_count(t_token *cmds); +int get_cmd_count(t_cmd *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 4a51594..af4f3c0 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/28 13:26:17 by tomoron ### ########.fr */ +/* Updated: 2024/03/30 18:44:30 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->cmds == cmds) + if (cmds->type && msh->tokens == cmds) { - while (msh->cmds->type != ARG && msh->cmds->next->next) + while (msh->cmds->cmd_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/utils.c b/srcs/utils.c index dab0b4d..13605c3 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/28 13:56:58 by tomoron ### ########.fr */ +/* Updated: 2024/03/30 18:45:27 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->cmds); + free_token(msh->tokens); set_echoctl(msh->echoctl); free(msh); }