From 7b6c75b5c39d054daac93ba4e051efd1b4a02de1 Mon Sep 17 00:00:00 2001 From: tomoron Date: Fri, 22 Mar 2024 20:12:25 +0100 Subject: [PATCH] debut signal --- Makefile | 5 +++-- srcs/commands.c | 7 +++++-- srcs/exec.c | 4 +++- srcs/main.c | 4 +++- srcs/minishell.h | 5 ++++- srcs/path.c | 9 +++------ srcs/pipe.c | 4 +++- srcs/signal_handler.c | 35 +++++++++++++++++++++++++++++++++++ 8 files changed, 59 insertions(+), 14 deletions(-) create mode 100644 srcs/signal_handler.c diff --git a/Makefile b/Makefile index dd94e27..960a38e 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: marde-vr +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/07/28 00:35:01 by tomoron #+# #+# # -# Updated: 2024/03/05 19:11:56 by marde-vr ### ########.fr # +# Updated: 2024/03/22 14:32:18 by tomoron ### ########.fr # # # # **************************************************************************** # @@ -36,7 +36,8 @@ SRCS_RAW = main.c\ builtins.c\ commands.c\ pipe.c\ - utils.c + utils.c\ + signal_handler.c OBJS_DIR = objs/ SRCS_DIR = srcs/ diff --git a/srcs/commands.c b/srcs/commands.c index 5372d93..c20a1fe 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/03/21 13:51:22 by marde-vr ### ########.fr */ +/* Updated: 2024/03/22 14:07:57 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -70,7 +70,10 @@ char **get_cmd_args(t_msh *msh) { if (cur_cmd->type == ARG) { - cmd_args[i] = remove_path(cur_cmd->token); + if(!i) + cmd_args[i] = remove_path(cur_cmd->token); + else + cmd_args[i] = cur_cmd->token; i++; } else diff --git a/srcs/exec.c b/srcs/exec.c index ce558ab..74141bd 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/21 13:42:56 by tomoron ### ########.fr */ +/* Updated: 2024/03/22 17:48:45 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -93,4 +93,6 @@ void exec_commands(t_msh *msh) free(msh->fds); msh->fds = 0; free(msh->pids); + signal(SIGINT, signal_handler_interactive); + signal(SIGQUIT, signal_handler_interactive); } diff --git a/srcs/main.c b/srcs/main.c index c5c81ab..a9c7bee 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/22 13:35:19 by tomoron ### ########.fr */ +/* Updated: 2024/03/22 16:45:49 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -77,6 +77,8 @@ int init_minishell(t_msh **msh, int argc, char **argv, char **envp) (void)argv; (*msh)->env = get_env(envp); (*msh)->aliases = 0; + signal(SIGINT, signal_handler_interactive); + signal(SIGQUIT, signal_handler_interactive); return (0); } diff --git a/srcs/minishell.h b/srcs/minishell.h index 245cd16..761e58d 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/21 13:30:59 by marde-vr ### ########.fr */ +/* Updated: 2024/03/22 16:44:54 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,7 @@ # include "../libft/libft.h" # include "fcntl.h" # include +# include typedef enum e_token_type { @@ -118,5 +119,7 @@ char **get_cmd_args(t_msh *msh); void remove_command_from_msh(t_msh *msh); int file_access(t_msh *msh, int *found); char *remove_path(char *token); +void signal_handler_interactive(int signum); +void signal_handler_command(int signum); #endif diff --git a/srcs/path.c b/srcs/path.c index f0c4f95..26958f1 100644 --- 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/03/21 16:39:06 by marde-vr ### ########.fr */ +/* Updated: 2024/03/22 15:45:44 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,10 +30,7 @@ char **split_paths_from_env(t_env *env) cur_env_var = cur_env_var->next; } if (!path_in_envp) - { - ft_printf_fd(2, "minishell: error: PATH not found\n"); return (0); - } return (ft_split(cur_env_var->value, ':')); } @@ -71,7 +68,7 @@ void free_paths(char **paths) int i; i = 0; - while (paths[i]) + while (paths && paths[i]) { free(paths[i]); i++; @@ -87,7 +84,7 @@ void get_path(t_msh *msh, int *found) if (!paths) { free_paths(paths); - ft_exit(msh, 1); + return; } find_cmd_path(msh, paths, found); free_paths(paths); diff --git a/srcs/pipe.c b/srcs/pipe.c index f2d93a8..bb8a7e5 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/21 13:50:47 by tomoron ### ########.fr */ +/* Updated: 2024/03/22 17:45:02 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -77,6 +77,8 @@ 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(SIGQUIT, signal_handler_command); if (i != 0) { if (msh->fds[i - 1][0] > 2) diff --git a/srcs/signal_handler.c b/srcs/signal_handler.c new file mode 100644 index 0000000..e239e0d --- /dev/null +++ b/srcs/signal_handler.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* signal_handler.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/22 14:31:13 by tomoron #+# #+# */ +/* Updated: 2024/03/22 19:53:02 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void signal_handler_interactive(int signum) +{ + if(signum == 2) + { + g_return_code = 130; + ft_putstr_fd("\n", 1); + rl_replace_line("", 0); + rl_on_new_line(); + rl_redisplay(); + } + if(signum == 3) + { + rl_redisplay(); + ft_putstr_fd("filsdeup", 1); + } +} + +void signal_handler_command(int signum) +{ + (void)signum; +}