From babcc2ebdfdcf1c7128597adec5117d5086562ec Mon Sep 17 00:00:00 2001 From: tomoron Date: Wed, 3 Apr 2024 17:47:21 +0200 Subject: [PATCH] l'execution marche (un petit peu) ( je crois) --- srcs/builtins.c | 24 ++++----- srcs/cd.c | 0 srcs/commands.c | 14 +++--- srcs/debug.c | 0 srcs/env_utils.c | 0 srcs/exec.c | 18 +++---- srcs/exec_bonus.c | 99 ++++++++++++++++++++++++++++++-------- srcs/exit.c | 6 +-- srcs/export.c | 6 +-- srcs/here_doc.c | 8 +-- srcs/here_doc_utils.c | 0 srcs/input_redirections.c | 4 +- srcs/main.c | 8 +-- srcs/minishell.h | 9 ++-- srcs/output_redirections.c | 7 +-- srcs/parsing_bonus.c | 55 +++++++++++++-------- srcs/path.c | 16 +++--- srcs/pipe.c | 12 ++--- srcs/signal_handler.c | 0 srcs/utils.c | 14 +++--- srcs/utils_bonus.c | 0 21 files changed, 190 insertions(+), 110 deletions(-) mode change 100644 => 100755 srcs/builtins.c mode change 100644 => 100755 srcs/cd.c mode change 100644 => 100755 srcs/commands.c mode change 100644 => 100755 srcs/debug.c mode change 100644 => 100755 srcs/env_utils.c mode change 100644 => 100755 srcs/exec_bonus.c mode change 100644 => 100755 srcs/export.c mode change 100644 => 100755 srcs/here_doc.c mode change 100644 => 100755 srcs/here_doc_utils.c mode change 100644 => 100755 srcs/input_redirections.c mode change 100644 => 100755 srcs/output_redirections.c mode change 100644 => 100755 srcs/parsing_bonus.c mode change 100644 => 100755 srcs/path.c mode change 100644 => 100755 srcs/pipe.c mode change 100644 => 100755 srcs/signal_handler.c mode change 100644 => 100755 srcs/utils.c mode change 100644 => 100755 srcs/utils_bonus.c diff --git a/srcs/builtins.c b/srcs/builtins.c old mode 100644 new mode 100755 index d5037a4..197bc7f --- 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/04/01 13:22:14 by marde-vr ### ########.fr */ +/* Updated: 2024/04/03 15:46:27 by tomoron ### ########.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")) @@ -51,20 +51,20 @@ int cmd_is_builtin(t_msh *msh, char *cmd_token) int exec_builtin(t_msh *msh) { - if (!msh->cmds->value) + if (!msh->tokens->value) return (0); - if (!ft_strcmp(msh->cmds->value, "echo")) - 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")) + if (!ft_strcmp(msh->tokens->value, "echo")) + g_return_code = echo(msh->tokens->next); + else if (!ft_strcmp(msh->tokens->value, "ret")) + g_return_code = ft_atoi(msh->tokens->next->value); + else if (!ft_strcmp(msh->tokens->value, "env")) g_return_code = print_env(msh->env); - else if (!ft_strcmp(msh->cmds->value, "exit")) + else if (!ft_strcmp(msh->tokens->value, "exit")) exit_bt(msh); - else if (!ft_strcmp(msh->cmds->value, "pwd")) + else if (!ft_strcmp(msh->tokens->value, "pwd")) g_return_code = pwd(); - else if (!ft_strcmp(msh->cmds->value, "cd")) - g_return_code = cd(msh->cmds); + else if (!ft_strcmp(msh->tokens->value, "cd")) + g_return_code = cd(msh->tokens); else return (0); return (1); diff --git a/srcs/cd.c b/srcs/cd.c old mode 100644 new mode 100755 diff --git a/srcs/commands.c b/srcs/commands.c old mode 100644 new mode 100755 index 598b41b..26319f1 --- 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/02 13:48:48 by tomoron ### ########.fr */ +/* Updated: 2024/04/03 15:45:49 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,11 +36,11 @@ char **get_cmd_args(t_msh *msh) int args_count; int i; - args_count = get_args_count(msh->cmds); + args_count = get_args_count(msh->tokens); cmd_args = ft_calloc(args_count + 1, sizeof(char *)); if (!cmd_args) ft_exit(msh, 1); - cur_cmd = msh->cmds; + cur_cmd = msh->tokens; i = 0; while (i < args_count) { @@ -63,7 +63,7 @@ void remove_command_from_msh(t_msh *msh) t_token *cur_cmd; t_token *cmd_tmp; - cur_cmd = msh->cmds; + cur_cmd = msh->tokens; while (cur_cmd && cur_cmd->next) { if (/*cur_cmd->type == PIPE*/ 0) @@ -73,7 +73,7 @@ void remove_command_from_msh(t_msh *msh) //msh->in_type = cmd_tmp->type; free(cmd_tmp->value); free(cmd_tmp); - msh->cmds = cur_cmd; + msh->tokens = cur_cmd; return ; } cmd_tmp = cur_cmd; @@ -81,7 +81,7 @@ void remove_command_from_msh(t_msh *msh) //msh->in_type = cur_cmd->type; free(cmd_tmp->value); free(cmd_tmp); - msh->cmds = cur_cmd; + msh->tokens = cur_cmd; } - //msh->in_type = msh->cmds->type; + //msh->in_type = msh->tokens->type; } diff --git a/srcs/debug.c b/srcs/debug.c old mode 100644 new mode 100755 diff --git a/srcs/env_utils.c b/srcs/env_utils.c old mode 100644 new mode 100755 diff --git a/srcs/exec.c b/srcs/exec.c index d0d9b81..a4ecebc 100755 --- a/srcs/exec.c +++ b/srcs/exec.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/07 14:12:49 by tomoron #+# #+# */ -/* Updated: 2024/03/30 16:55:17 by marde-vr ### ########.fr */ +/* Updated: 2024/04/03 17:21:57 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,17 +51,17 @@ 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->cmds); + get_in_type(msh, msh->tokens); if (!g_return_code) - get_out_type(msh, msh->cmds); + get_out_type(msh, msh->tokens); } else { - get_out_type(msh, msh->cmds); + get_out_type(msh, msh->tokens); if (!g_return_code) - get_in_type(msh, msh->cmds); + get_in_type(msh, msh->tokens); } - if (!cmd_is_builtin(msh, msh->cmds->value)) + if (!cmd_is_builtin(msh, msh->tokens->value)) get_cmd_path(msh); exec(msh, get_cmd_args(msh), i, cmd_count); remove_command_from_msh(msh); @@ -90,7 +90,7 @@ void end_execution(t_msh *msh, int cmd_count) msh->fds = 0; free(msh->pids); msh->pids = 0; - signal(SIGINT, signal_handler_interactive); + //signal(SIGINT, signal_handler_interactive); signal(SIGQUIT, signal_handler_interactive); set_echoctl(0); } @@ -101,9 +101,9 @@ void exec_commands(t_msh *msh) int i; i = -1; - if (!msh->cmds) + if (!msh->tokens) return ; - cmd_count = get_cmd_count(msh->cmds); + cmd_count = get_cmd_count(msh->tokens); msh->fds = ft_calloc(cmd_count, sizeof(int **)); msh->pids = ft_calloc(cmd_count, sizeof(int *)); if (!msh->pids || !msh->fds) diff --git a/srcs/exec_bonus.c b/srcs/exec_bonus.c old mode 100644 new mode 100755 index d6c7365..26537cf --- 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/03 00:15:39 by tomoron ### ########.fr */ +/* Updated: 2024/04/03 17:46:14 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,7 @@ void get_redirections(t_msh *msh, t_cmd *cmds) } else { - get_out_type(msh,cmds); + get_out_type(msh, cmds); if (!g_return_code) get_in_type(msh, cmds); } @@ -33,28 +33,32 @@ void exec_command_bonus(t_msh *msh, char *cmd_str) t_cmd *cmds; t_cmd *tmp; - //printf("cmd : %s\n",cmd_str); cmds = parsing_bonus(cmd_str); + print_parsed_cmd(cmds); // debug tmp = check_cmds_syntax(cmds); - if(tmp) + if (tmp) { print_syntax_error_bonus(tmp); - printf("error\n"); + printf("error\n");//debug free_cmd(cmds); - return; + return ; } while (cmds) { - if (cmds->cmd_type == CMD) + if ((cmds->cmd_type == AND && !g_return_code) || (cmds->cmd_type == OR + && g_return_code) || !is_operand_type(cmds)) { - msh->cmds = parse_command(cmds->value, msh->env); + if(is_operand_type(cmds)) + cmds = cmds->next; + msh->tokens = parse_command(cmds->value, msh->env); + msh->cmds = cmds; get_redirections(msh, cmds); - print_msh_struct(msh); - print_parsed_cmd(cmds); - print_parsed_token(msh->cmds); -// exec_commands(msh); + print_msh_struct(msh); // debug + print_parsed_token(msh->tokens); // debug + exec_commands(msh); } - cmds = cmds->next; + while(cmds && (is_cmd_type(cmds) || cmds->cmd_type == PIPE)) + cmds = cmds->next; } } @@ -67,7 +71,7 @@ int exec(t_msh *msh, char **cmd_args, int i, int cmd_count) { if (pipe(fds) == -1) { - perror("pipe"); + perror("minishell: pipe"); ft_exit(msh, 1); } msh->in_fd = fds[0]; @@ -76,7 +80,7 @@ int exec(t_msh *msh, char **cmd_args, int i, int cmd_count) pid = fork(); if (pid == -1) { - perror("fork"); + perror("minishell: fork"); ft_exit(msh, 1); } if (pid == 0) @@ -93,7 +97,7 @@ 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; - if (!cmd_is_builtin(msh, msh->cmds->value)) + if (!cmd_is_builtin(msh, msh->tokens->value)) get_cmd_path(msh); exec(msh, get_cmd_args(msh), i, cmd_count); remove_command_from_msh(msh); @@ -110,11 +114,68 @@ void end_execution(t_msh *msh, int cmd_count) if (!g_return_code && WIFEXITED(status)) g_return_code = WEXITSTATUS(status); if (WIFSIGNALED(status) && WTERMSIG(status) == SIGQUIT) - printf("Quit\n"); - //TODO: (core dumped) WCOREDUMP + printf("Quit\n"); + // TODO: (core dumped) WCOREDUMP free(msh->pids); msh->pids = 0; - //signal(SIGINT, signal_handler_interactive); //enables ctrl-C + // signal(SIGINT, signal_handler_interactive); //enables ctrl-C signal(SIGQUIT, signal_handler_interactive); set_echoctl(0); } + +int get_cmd_count(t_cmd *cmds) +{ + int nb; + + nb = 0; + while(cmds && !is_operand_type(cmds)) + { + if(is_cmd_type(cmds)) + nb++; + cmds = cmds->next; + } + return(nb); +} + +void exec_commands(t_msh *msh) +{ + int cmd_count; + int i; + int status; + + if (!msh->tokens) + return ; + cmd_count = get_cmd_count(msh->cmds); + 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); + + i = 0; + while (i < cmd_count) + { + exec_command(msh, i, cmd_count); + i++; + } + i = 0; + while (i < cmd_count) + { + waitpid(msh->pids[i], &status, 0); + i++; + } + if (!g_return_code && WIFEXITED(status)) + g_return_code = WEXITSTATUS(status); + 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); + signal(SIGQUIT, signal_handler_interactive); +} diff --git a/srcs/exit.c b/srcs/exit.c index 50c50d0..9ebc43f 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/02 00:42:19 by tomoron ### ########.fr */ +/* Updated: 2024/04/03 15:44:06 by tomoron ### ########.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 && !ft_strisnbr(cur_cmd->value)) numeric_arg_err(cur_cmd->value, exit_code); else if (cur_cmd) @@ -38,7 +38,7 @@ int exit_bt(t_msh *msh) t_token *cur_cmd; int exit_code; - cur_cmd = msh->cmds->next; + cur_cmd = msh->tokens->next; ft_printf("exit\n"); if (cur_cmd && cur_cmd->next && ft_strisnbr(cur_cmd->value)) ft_putstr_fd("minishell: exit: too many arguments\n", 2); diff --git a/srcs/export.c b/srcs/export.c old mode 100644 new mode 100755 index fd4d184..fa03433 --- 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/04/02 02:16:18 by tomoron ### ########.fr */ +/* Updated: 2024/04/03 15:45:32 by tomoron ### ########.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) print_env_declare(msh->env); if (cmd && cmd->next && !cmd->next->next) @@ -95,7 +95,7 @@ int ft_unset(t_msh *msh) { t_token *cmd; - cmd = msh->cmds; + cmd = msh->tokens; if (cmd) cmd = cmd->next; while (cmd) diff --git a/srcs/here_doc.c b/srcs/here_doc.c old mode 100644 new mode 100755 index 454a7ca..fec8290 --- 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/04/02 17:04:44 by tomoron ### ########.fr */ +/* Updated: 2024/04/03 17:20:55 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,7 +40,7 @@ void here_doc_child(t_msh *msh, char *eof, char *here_doc_file) { here_doc_variables(1, 0, msh); here_doc_variables(1, 1, here_doc_file); - signal(SIGINT, signal_handler_here_doc); + //signal(SIGINT, signal_handler_here_doc); get_here_doc_input(msh, eof); close(msh->in_fd); free(here_doc_file); @@ -51,10 +51,10 @@ void here_doc_signal(t_msh *msh, int child_pid, char *here_doc_file) { int status; - signal(SIGINT, signal_handler_command); + //signal(SIGINT, signal_handler_command); signal(SIGQUIT, signal_handler_here_doc); waitpid(child_pid, &status, 0); - signal(SIGINT, signal_handler_interactive); + //signal(SIGINT, signal_handler_interactive); signal(SIGQUIT, signal_handler_interactive); close(msh->in_fd); if (WIFEXITED(status) && WEXITSTATUS(status)) diff --git a/srcs/here_doc_utils.c b/srcs/here_doc_utils.c old mode 100644 new mode 100755 diff --git a/srcs/input_redirections.c b/srcs/input_redirections.c old mode 100644 new mode 100755 index 9e73863..b7d2f0a --- 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/02 17:40:33 by tomoron ### ########.fr */ +/* Updated: 2024/04/03 12:32:30 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -64,7 +64,7 @@ void get_in_type(t_msh *msh, t_cmd *tokens) open_input_file(msh, &cur_token); } if(cur_token && cur_token->next && !is_operand_type(cur_token->next)) - get_in_type(msh, cur_token); + get_in_type(msh, cur_token->next); } int first_is_in_type(t_cmd *cmd) diff --git a/srcs/main.c b/srcs/main.c index 64bf65e..01b604d 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/30 17:21:23 by marde-vr ### ########.fr */ +/* Updated: 2024/04/03 15:46:12 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -107,11 +107,11 @@ int main(int argc, char **argv, char **envp) commands = readline(prompt); free(prompt); add_history(commands); - msh->cmds = parse_command(commands, msh->env); - print_parsed_cmd(msh->cmds); + msh->tokens = parse_command(commands, msh->env); + print_parsed_cmd(msh->tokens); free(commands); exec_commands(msh); - free_token(msh->cmds); + free_token(msh->tokens); } rl_clear_history(); diff --git a/srcs/minishell.h b/srcs/minishell.h index 38e59d0..570747e 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/03 01:24:10 by tomoron ### ########.fr */ +/* Updated: 2024/04/03 16:52:11 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -62,7 +62,9 @@ typedef struct s_env typedef struct s_msh { t_env *env; - t_token *cmds; + t_token *tokens; + t_cmd *cmds; + int **fds; int *pids; t_cmd_type in_type; t_cmd_type out_type; @@ -87,6 +89,7 @@ t_token *parse_command(char *command, t_env *env); 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_cmd *cmds); +void exec_commands(t_msh *msh); void handle_here_doc(t_msh *msh, char *eof); void get_in_type(t_msh *msh, t_cmd *tokens); void signal_handler_interactive(int signum); @@ -112,7 +115,7 @@ char *get_tmp_file_name(t_msh *msh); 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_token *cmds); +int get_cmd_count(t_cmd *cmds); int first_is_in_type(t_cmd *cmd); int contains_newline(char *str); int check_var_name(char *name); diff --git a/srcs/output_redirections.c b/srcs/output_redirections.c old mode 100644 new mode 100755 index 6d35ce3..588f3f5 --- 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/04/03 01:25:03 by tomoron ### ########.fr */ +/* Updated: 2024/04/03 15:09:52 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,7 +35,8 @@ void open_out_file(t_msh *msh, t_cmd **cur_cmd, char *filename) if (msh->out_fd == -1) { g_return_code = 1; - perror("open"); + ft_putstr_fd("minishell: ", 2); + perror(filename); return ; } if ((*cur_cmd)->cmd_type != PIPE) @@ -59,7 +60,7 @@ void get_out_type(t_msh *msh, t_cmd *cmds) cur_cmd = cur_cmd->next; if (cur_cmd->cmd_type == CMD || cur_cmd->cmd_type == PAREN) msh->out_type = 0; - else + else if(cur_cmd && !is_operand_type(cur_cmd)) { msh->out_type = cur_cmd->cmd_type; filename = parse_command(cur_cmd->value, msh->env); diff --git a/srcs/parsing_bonus.c b/srcs/parsing_bonus.c old mode 100644 new mode 100755 index 3365eab..769943d --- a/srcs/parsing_bonus.c +++ b/srcs/parsing_bonus.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/27 14:40:44 by tomoron #+# #+# */ -/* Updated: 2024/04/02 15:17:30 by tomoron ### ########.fr */ +/* Updated: 2024/04/03 15:00:00 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -140,29 +140,44 @@ void print_syntax_error_bonus(t_cmd *cmd) ft_printf_fd(2, "'\n"); } +int check_tokens_syntax(t_cmd *cmd, t_cmd *last) +{ + t_token *token; + + if (is_cmd_type(last)) + { + ft_putstr_fd("minishell : syntax error\n", 2); + return (0); + } + token = parse_command(cmd->value, 0); + if (!token) + return (0); + free_token(token); + return (1); +} + t_cmd *check_cmds_syntax(t_cmd *cmds) { - t_cmd_type last; - t_token *token; + t_cmd *last; - if (!cmds || cmds->cmd_type == OR || cmds->cmd_type == AND || cmds->cmd_type == PIPE) + if (!cmds || is_operand_type(cmds) || cmds->cmd_type == PIPE) return (cmds); - last = cmds->cmd_type; + if (!is_operand_type(cmds) && cmds->cmd_type != PIPE && cmds->value == 0) + return (cmds); + last = cmds; cmds = cmds->next; while (cmds) { - if (cmds->cmd_type == OR || cmds->cmd_type == AND - || cmds->cmd_type == PIPE) - if ((last != CMD && last != PAREN) || !cmds->next || (cmds->next->cmd_type != CMD - && cmds->next->cmd_type != PAREN)) + if (!is_operand_type(cmds) && cmds->cmd_type != PIPE + && cmds->value == 0) + return (cmds); + if (is_operand_type(cmds) || cmds->cmd_type == PIPE) + if (!is_cmd_type(last) || !cmds->next || !is_cmd_type(cmds->next)) return (cmds); - if (cmds->cmd_type == CMD || cmds->cmd_type == PAREN) - { - token = parse_command(cmds->value, 0); - if (!token) + if (is_cmd_type(cmds)) + if (!check_tokens_syntax(cmds, last)) return (cmds); - free_token(token); - } + last = cmds; cmds = cmds->next; } return (0); @@ -187,18 +202,18 @@ int get_next_arg_len(char *cmd) in_dquote = !in_dquote; len++; } - return(len); + return (len); } char *get_next_arg(char **cmd) { int len; char *res; - while(ft_isspace(**cmd)) + while (ft_isspace(**cmd)) (*cmd)++; len = get_next_arg_len(*cmd); - if(!len) - return(0); + if (!len) + return (0); res = ft_substr(*cmd, 0, len); *cmd += len; return (res); @@ -222,7 +237,7 @@ t_cmd *parsing_bonus(char *cmd) value = get_cmd_value(&cmd, type); else if (type == RED_O || type == RED_O_APP || type == RED_I || type == HERE_DOC) - value = get_next_arg(&cmd); + value = get_next_arg(&cmd); else value = 0; res = cmd_add_back(res, value, type); diff --git a/srcs/path.c b/srcs/path.c old mode 100644 new mode 100755 index 271bd62..014da01 --- a/srcs/path.c +++ b/srcs/path.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/21 21:47:15 by marde-vr #+# #+# */ -/* Updated: 2024/04/02 23:39:04 by tomoron ### ########.fr */ +/* Updated: 2024/04/03 15:46:04 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,15 +26,15 @@ void find_cmd_path(t_msh *msh, char **paths, int *found) tmp = ft_strjoin(path, "/"); if (!tmp) ft_exit(msh, 1); - path = ft_strjoin(tmp, msh->cmds->value); + path = ft_strjoin(tmp, msh->tokens->value); if (!path) ft_exit(msh, 1); free(tmp); if (access(path, X_OK) != -1) { *found = 1; - free(msh->cmds->value); - msh->cmds->value = path; + free(msh->tokens->value); + msh->tokens->value = path; break ; } free(path); @@ -81,7 +81,7 @@ void get_cmd_path(t_msh *msh) int found; found = 0; - if (ft_strchr(msh->cmds->value, '/')) + if (ft_strchr(msh->tokens->value, '/')) { if (!file_access(msh, &found)) return ; @@ -90,9 +90,9 @@ void get_cmd_path(t_msh *msh) get_path(msh, &found); if (!found) { - ft_printf_fd(2, "%s: command not found\n", msh->cmds->value); - free(msh->cmds->value); - msh->cmds->value = 0; + ft_printf_fd(2, "%s: command not found\n", msh->tokens->value); + free(msh->tokens->value); + msh->tokens->value = 0; g_return_code = 127; } } diff --git a/srcs/pipe.c b/srcs/pipe.c old mode 100644 new mode 100755 index aa5f7bb..2d877c9 --- 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/03 01:25:26 by tomoron ### ########.fr */ +/* Updated: 2024/04/03 17:21:41 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,17 +31,17 @@ 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))) + if (msh->tokens->value && (!ft_strcmp(msh->tokens->value, "cd") + || !ft_strcmp(msh->tokens->value, "exit") || exec_builtin(msh))) { free(cmd_args); ft_exit(msh, g_return_code); } - if (msh->cmds->value) + if (msh->tokens->value) { set_echoctl(msh->echoctl); env = env_to_char_tab(msh->env); - execve(msh->cmds->value, cmd_args, env); + execve(msh->tokens->value, cmd_args, env); ft_free_str_arr(env); } } @@ -66,7 +66,7 @@ void child(t_msh *msh, char **cmd_args, int i) void parent(t_msh *msh, int i, int cmd_count) { - signal(SIGINT, signal_handler_command); + //signal(SIGINT, signal_handler_command); signal(SIGQUIT, signal_handler_command); if (i != 0) { diff --git a/srcs/signal_handler.c b/srcs/signal_handler.c old mode 100644 new mode 100755 diff --git a/srcs/utils.c b/srcs/utils.c old mode 100644 new mode 100755 index ce0ae7a..e80d1a3 --- 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 20:06:36 by marde-vr ### ########.fr */ +/* Updated: 2024/04/03 15:46:38 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ void free_msh(t_msh *msh) { free_env(msh->env); free(msh->pids); - free_token(msh->cmds); + free_token(msh->tokens); set_echoctl(msh->echoctl); free(msh); } @@ -63,22 +63,22 @@ int file_access(t_msh *msh, int *found) { int fd; - fd = open(msh->cmds->value, O_DIRECTORY); + fd = open(msh->tokens->value, O_DIRECTORY); if (fd != -1) { close(fd); - ft_printf_fd(2, "minishell: %s: Is a directory\n", msh->cmds->value); + ft_printf_fd(2, "minishell: %s: Is a directory\n", msh->tokens->value); g_return_code = 126; return (0); } - if (access(msh->cmds->value, X_OK) != -1) + if (access(msh->tokens->value, X_OK) != -1) *found = 1; else { - ft_printf_fd(2, "minishell: %s: ", msh->cmds->value); + ft_printf_fd(2, "minishell: %s: ", msh->tokens->value); perror(""); g_return_code = 127; - if (access(msh->cmds->value, F_OK) != -1) + if (access(msh->tokens->value, F_OK) != -1) g_return_code = 126; return (0); } diff --git a/srcs/utils_bonus.c b/srcs/utils_bonus.c old mode 100644 new mode 100755