From 2af75c9497526375bbbb640d5a94ddd11737210d Mon Sep 17 00:00:00 2001 From: tomoron Date: Thu, 25 Apr 2024 18:41:02 +0200 Subject: [PATCH] heredoc ctrl+C --- srcs/exec_bonus.c | 4 +++- srcs/here_doc.c | 8 +++++++- srcs/input_redirections.c | 6 +++--- srcs/main.c | 8 +------- srcs/output_redirections.c | 4 ++-- srcs/signal_handler.c | 14 +++++++++++--- 6 files changed, 27 insertions(+), 17 deletions(-) diff --git a/srcs/exec_bonus.c b/srcs/exec_bonus.c index 0196843..aaec259 100755 --- 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/25 13:44:26 by tomoron ### ########.fr */ +/* Updated: 2024/04/25 18:21:20 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -72,6 +72,8 @@ int exec(t_msh *msh, char **cmd_args, int i, int cmd_count) void exec_command(t_msh *msh, int i, int cmd_count) { + if(msh->in_fd == -1 || msh->out_fd == -1) + return ; g_return_code = 0; msh->fds[i] = ft_calloc(2, sizeof(int *)); if (!msh->fds[i]) diff --git a/srcs/here_doc.c b/srcs/here_doc.c index 8e06763..c938a7c 100755 --- 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/25 10:16:53 by tomoron ### ########.fr */ +/* Updated: 2024/04/25 18:38:34 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,6 +37,7 @@ void get_here_doc_input(t_msh *msh, char *eof) void here_doc_child(t_msh *msh, char *eof) { + rl_catch_signals = 1; here_doc_variables(1, msh); signal(SIGINT, signal_handler_here_doc); get_here_doc_input(msh, eof); @@ -55,7 +56,12 @@ void here_doc_signal(t_msh *msh, int child_pid, char *here_doc_file) signal(SIGQUIT, signal_handler_interactive); close(msh->in_fd); if (WIFEXITED(status) && WEXITSTATUS(status)) + { unlink(here_doc_file); + msh->in_fd = -1; + g_return_code = 130; + return ; + } msh->in_fd = open(here_doc_file, O_RDWR, 0644); if (msh->in_fd == -1 && !(WIFEXITED(status) && WEXITSTATUS(status))) perror("open"); diff --git a/srcs/input_redirections.c b/srcs/input_redirections.c index 95872e2..25ed912 100755 --- 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/24 21:30:46 by tomoron ### ########.fr */ +/* Updated: 2024/04/25 18:39:48 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,7 +36,7 @@ void redirect_input(t_msh *msh, int i, char **cmd_args) void ambiguous_redirect(char *str, t_msh *msh) { ft_printf_fd(2, "minishell: %s: ambiguous redirect\n", str); - msh->in_fd = -2; + msh->in_fd = -1; g_return_code = 1; } @@ -66,7 +66,7 @@ int open_input_file(t_msh *msh, t_cmd **cur_token) } free_token(filename); } - return (msh->in_fd == -2); + return (msh->in_fd == -1); } int get_in_type(t_msh *msh, t_cmd *t_strt, t_cmd *tokens, int here_doc) diff --git a/srcs/main.c b/srcs/main.c index d8dd876..a4af8c8 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/04/25 13:40:02 by tomoron ### ########.fr */ +/* Updated: 2024/04/25 18:38:02 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -90,8 +90,6 @@ t_env *add_shlvl(t_env *env) int init_minishell(t_msh **msh, int argc, char **argv, char **envp) { - struct termios t_p; - *msh = ft_calloc(1, sizeof(t_msh)); if (!*msh) exit(1); @@ -99,13 +97,9 @@ int init_minishell(t_msh **msh, int argc, char **argv, char **envp) (void)argv; (*msh)->env = get_env(envp); (*msh)->env = add_shlvl((*msh)->env); - tcgetattr(1, &t_p); - //(*msh)->echoctl = t_p.c_lflag & ECHOCTL; rl_catch_signals = 0; signal(SIGINT, signal_handler_interactive); signal(SIGQUIT, signal_handler_interactive); - //if (set_echoctl(0)) - // ft_exit(*msh, 1); return (0); } diff --git a/srcs/output_redirections.c b/srcs/output_redirections.c index d11a654..ab23f48 100755 --- a/srcs/output_redirections.c +++ b/srcs/output_redirections.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/19 14:09:44 by tomoron #+# #+# */ -/* Updated: 2024/04/24 21:31:01 by tomoron ### ########.fr */ +/* Updated: 2024/04/25 18:39:52 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -91,5 +91,5 @@ int get_out_type(t_msh *msh, t_cmd *cur_cmd) } else if (cur_cmd && cur_cmd->cmd_type == PIPE) msh->out_type = PIPE; - return (ret || msh->in_fd == -2); + return (ret || msh->in_fd == -1); } diff --git a/srcs/signal_handler.c b/srcs/signal_handler.c index d55ca96..1f547ad 100755 --- a/srcs/signal_handler.c +++ b/srcs/signal_handler.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/22 14:31:13 by tomoron #+# #+# */ -/* Updated: 2024/04/25 13:46:40 by tomoron ### ########.fr */ +/* Updated: 2024/04/25 18:39:18 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,18 +37,24 @@ void *here_doc_variables(int write, void *data) void signal_handler_here_doc(int signum) { - t_msh *msh; + t_msh *msh; if (signum == SIGINT) { msh = here_doc_variables(0, 0); - close(msh->in_fd); + rl_on_new_line(); + printf("\n"); + if(msh->in_fd > 2) + close(msh->in_fd); ft_exit(msh, 1); } } int set_echoctl(int value) { + printf("nope"); +(void)value; +/* struct termios t_p; if (!isatty(1)) @@ -64,6 +70,8 @@ int set_echoctl(int value) if (tcsetattr(1, TCSANOW, &t_p)) return (1); return (0); +*/ + return(0); } void signal_handler_command(int signum)