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 90eaaea..3b1c54e 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/30 16:30:51 by jho ### ########.fr */ +/* Updated: 2023/11/30 17:47:50 by haekang ### ########.fr */ /* */ /* ************************************************************************** */ @@ -60,7 +60,7 @@ int msh_execute_first(t_pipeline *pl, int *fd, t_env *env, int forked) return (-1); if (pid == 0) { - if (fd[0] != 0 && close(fd[0]) == -1) + if (fd[0] != 0 && close(fd[0]) == -1) return (-1); msh_execute_first_child(pl, local_fd, env, forked); } 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 f60ce80..289c101 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/30 16:15:28 by jho ### ########.fr */ +/* Updated: 2023/11/30 17:49:03 by haekang ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,6 +33,11 @@ int main(int argc, char *argv[], char *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) 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