From 0cca9cc79ea5a6ddea7bbb179df81fd3c3a0e71a Mon Sep 17 00:00:00 2001 From: Haeseong Kang Date: Thu, 30 Nov 2023 17:50:18 +0900 Subject: [PATCH] feat : sig --- includes/msh.h | 10 ++- sources/msh_builtin/msh_builtin_cd_chdir.c | 4 +- sources/msh_builtin/msh_builtin_env.c | 4 +- sources/msh_executor/msh_execute_first.c | 5 +- sources/msh_expander/msh_expand.c | 4 +- sources/msh_main/msh_main.c | 10 ++- sources/msh_signal/msh_signal.c | 83 ++++++++++++++++++++++ 7 files changed, 110 insertions(+), 10 deletions(-) create mode 100644 sources/msh_signal/msh_signal.c diff --git a/includes/msh.h b/includes/msh.h index ce40081..525b165 100644 --- a/includes/msh.h +++ b/includes/msh.h @@ -6,7 +6,7 @@ /* By: haekang +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/26 12:29:19 by jho #+# #+# */ -/* Updated: 2023/11/29 15:51:54 by jho ### ########.fr */ +/* Updated: 2023/11/30 17:13:44 by haekang ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,4 +36,12 @@ int g_exit_status; + + +void do_sigint(int signum); +void set_terminal_print_off(void); +void set_terminal_print_on(void); +void set_default_signal(void); +void set_signal(void); +void handle_signal_2(int signo); #endif diff --git a/sources/msh_builtin/msh_builtin_cd_chdir.c b/sources/msh_builtin/msh_builtin_cd_chdir.c index df5f78f..e5c36bd 100644 --- a/sources/msh_builtin/msh_builtin_cd_chdir.c +++ b/sources/msh_builtin/msh_builtin_cd_chdir.c @@ -6,7 +6,7 @@ /* By: haekang +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/29 16:14:44 by haekang #+# #+# */ -/* Updated: 2023/11/29 16:15:50 by haekang ### ########.fr */ +/* Updated: 2023/11/29 20:53:30 by haekang ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ int msh_builtin_cd_chdir(char *path, char *old_pwd, int pipe) printf("minishell: cd: %s: No such file or directory\n", path); g_exit_status = 1; if (pipe == 1) - exit (g_exit_status); + exit(g_exit_status); return (0); } return (1); diff --git a/sources/msh_builtin/msh_builtin_env.c b/sources/msh_builtin/msh_builtin_env.c index 6802000..dbe5657 100644 --- a/sources/msh_builtin/msh_builtin_env.c +++ b/sources/msh_builtin/msh_builtin_env.c @@ -6,7 +6,7 @@ /* By: haekang +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/10 20:07:51 by haekang #+# #+# */ -/* Updated: 2023/11/29 16:19:04 by haekang ### ########.fr */ +/* Updated: 2023/11/29 20:53:51 by haekang ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,6 +31,6 @@ int msh_builtin_env(int *fd, int pipe, char **cmd, t_env *env) } g_exit_status = 0; if (pipe == 1) - exit (g_exit_status); + exit(g_exit_status); return (0); } diff --git a/sources/msh_executor/msh_execute_first.c b/sources/msh_executor/msh_execute_first.c index cd9a229..97ac410 100644 --- a/sources/msh_executor/msh_execute_first.c +++ b/sources/msh_executor/msh_execute_first.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* msh_execute_first.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jho +#+ +:+ +#+ */ +/* By: haekang +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/23 14:24:03 by jho #+# #+# */ -/* Updated: 2023/11/29 17:03:15 by jho ### ########.fr */ +/* Updated: 2023/11/30 17:47:50 by haekang ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,6 +51,7 @@ int msh_execute_first(t_pipeline *pl, int *fd, t_env *env, int forked) return (0); if (pid == 0) { + set_default_signal(); param = msh_token_filter_sym(pl->tokens, WORD); if (param == NULL) exit(errno); diff --git a/sources/msh_expander/msh_expand.c b/sources/msh_expander/msh_expand.c index 7a7e3f5..1e54205 100644 --- a/sources/msh_expander/msh_expand.c +++ b/sources/msh_expander/msh_expand.c @@ -6,7 +6,7 @@ /* By: haekang +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/26 09:58:12 by jho #+# #+# */ -/* Updated: 2023/11/14 14:39:42 by haekang ### ########.fr */ +/* Updated: 2023/11/30 15:45:52 by haekang ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,6 +39,8 @@ char *msh_expand(char *s, t_env *env) t_token *tokens; char *expanded; + if (s == NULL) + return (NULL); tokens = msh_vqoutes(s); if (tokens == NULL) return (NULL); diff --git a/sources/msh_main/msh_main.c b/sources/msh_main/msh_main.c index 714a8d1..b46e4c3 100644 --- a/sources/msh_main/msh_main.c +++ b/sources/msh_main/msh_main.c @@ -6,7 +6,7 @@ /* By: haekang +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/12 12:03:25 by jho #+# #+# */ -/* Updated: 2023/11/29 19:41:13 by jho ### ########.fr */ +/* Updated: 2023/11/30 17:49:03 by haekang ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,10 +21,16 @@ int main(int argc, char *argv[], char *envp[]) (void) argc; (void) argv; + set_signal(); env = msh_env_new_list(envp); while (1) { input = readline("msh$> "); + if (input == NULL) + { + set_terminal_print_on(); + exit (g_exit_status);//여기서 ctrl + d 처리 + } expanded = msh_expand(input, env); pipelines = msh_lex(expanded); if (pipelines == NULL) @@ -38,7 +44,7 @@ int main(int argc, char *argv[], char *envp[]) msh_pipeline_free_list(pipelines); free(expanded); free(input); - system("leaks minishell"); + system("leaks minishell | grep leaks"); } return (0); } diff --git a/sources/msh_signal/msh_signal.c b/sources/msh_signal/msh_signal.c new file mode 100644 index 0000000..c662928 --- /dev/null +++ b/sources/msh_signal/msh_signal.c @@ -0,0 +1,83 @@ +#include + +void handle_signal_2(int signo) +{ + (void)signo; + g_exit_status = 1; +} + +void handle_signal(int signo) +{ + pid_t pid; + int status; + + pid = waitpid(-1, &status, WNOHANG); + if (signo == SIGINT) + { + if (pid == -1) + { + printf("\n"); + rl_on_new_line(); + rl_replace_line("", 0); + rl_redisplay(); + g_exit_status = 1; + } + else//자식 프로세스가 존재함 + { + printf("부모 시그널\n"); + rl_on_new_line(); + rl_replace_line("", 0); + rl_redisplay(); + g_exit_status = 1; + } + } + // else if (signo == SIGQUIT) + // { + // if (pid == -1) + // ft_putstr_fd("\b\b \b\b", STDOUT); + // else + // ft_putstr_fd("Quit: 3\n", STDOUT); + // } +} + +// void do_sigint(int signum) +// { +// (void)signum; +// printf("\n"); +// rl_on_new_line(); +// rl_replace_line("", 0); +// rl_redisplay(); +// g_exit_status = 1; +// } + +void set_terminal_print_off(void) +{ + struct termios term; + + tcgetattr(1, &term); + term.c_lflag &= ~(ECHOCTL); + tcsetattr(1, 0, &term); +} + +void set_terminal_print_on(void) +{ + struct termios term; + + tcgetattr(1, &term); + term.c_lflag |= (ECHOCTL); + tcsetattr(1, 0, &term); +} + +void set_default_signal(void) +{ + signal(SIGINT, SIG_DFL); + signal(SIGQUIT, SIG_DFL); +} + + +void set_signal(void) +{ + set_terminal_print_off(); + signal(SIGINT, handle_signal); + signal(SIGQUIT, SIG_IGN); +} \ No newline at end of file