From 842e31c68f450e1f8e7603a8f61c5d5b4f7b1278 Mon Sep 17 00:00:00 2001 From: Loic Deridder Date: Thu, 13 Feb 2025 09:10:54 +0100 Subject: [PATCH] fds and sigint --- srcs/execution/exec_cmd.c | 4 ++++ srcs/execution/exec_pline.c | 8 +++++++- srcs/main.c | 25 ++++++++++++++++++++++++- srcs/msh_struct.c | 1 + srcs/parsing/ast/ast.c | 4 ++-- srcs/parsing/ast/utils/free_ast.c | 4 ++++ srcs/sig.c | 9 --------- 7 files changed, 42 insertions(+), 13 deletions(-) diff --git a/srcs/execution/exec_cmd.c b/srcs/execution/exec_cmd.c index a279352..1491cfa 100644 --- a/srcs/execution/exec_cmd.c +++ b/srcs/execution/exec_cmd.c @@ -65,6 +65,10 @@ int exec(t_ast_n *node) char *path; expand_node(node); + if (node->msh->here_fd != -1) + close(node->msh->here_fd); + if (node->msh->hist != -1) + close(node->msh->hist); path = find_path(node->cmd, node->msh->env); if (!path) return_error(node->cmd, "command not found", 127, node); diff --git a/srcs/execution/exec_pline.c b/srcs/execution/exec_pline.c index 5f86bcf..1884ae4 100644 --- a/srcs/execution/exec_pline.c +++ b/srcs/execution/exec_pline.c @@ -16,6 +16,13 @@ void exec_pcmd(t_ast_n *pcmd) { int ret; + if (pcmd->msh->here_fd != -1) + close(pcmd->msh->here_fd); + close(pcmd->msh->hist); + if (pcmd->save_stdo != -1) + close(pcmd->save_stdo); + if (pcmd->save_stdi != -1) + close(pcmd->save_stdi); if (!pcmd->cmd) exit(0); if (is_builtin(pcmd->cmd)) @@ -35,7 +42,6 @@ void exec_pchild(int *pipes, int index, t_ast_n *pcmd, int cmds) ret = 0; if (index < cmds - 1) dup2(pipes[1], STDOUT_FILENO); - close(pcmd->msh->hist); close(pipes[0]); close(pipes[1]); handle_redir(pcmd); diff --git a/srcs/main.c b/srcs/main.c index c563002..6750cef 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -15,6 +15,8 @@ #include #include +int g_sig = 0; + static void add_prevhistory(t_msh *msh) { char *str; @@ -31,11 +33,27 @@ static void add_prevhistory(t_msh *msh) } } +void handle_sigint(int sig) +{ + (void)sig; + write(2, "\n\n", 2); + rl_on_new_line(); + rl_replace_line("", 0); + rl_redisplay(); + g_sig = sig; +} + static void exit_manual(t_msh *msh) { int ret; - ret = msh->ex_code; + if (g_sig == SIGINT) + { + ret = 130; + g_sig = 0; + } + else + ret = msh->ex_code; free_msh(msh); ft_fprintf(2, "exit\n"); exit(ret); @@ -54,6 +72,11 @@ int interactive_mode(t_msh *msh) } if (!msh->input) return (1); + if (g_sig == SIGINT) + { + msh->ex_code = 130; + g_sig = 0; + } interpret_cmd(&msh->input, msh); return (1); } diff --git a/srcs/msh_struct.c b/srcs/msh_struct.c index acd549f..ff0f37b 100644 --- a/srcs/msh_struct.c +++ b/srcs/msh_struct.c @@ -37,6 +37,7 @@ t_msh *init_msh(char **envp) close(fd); msh->hist = open(".mmoat_history", O_RDWR | O_CREAT | O_APPEND, 0666); msh->ex_code = 0; + msh->here_fd = -1; msh->input = NULL; if (!msh) return (NULL); diff --git a/srcs/parsing/ast/ast.c b/srcs/parsing/ast/ast.c index ffd6c77..9828564 100644 --- a/srcs/parsing/ast/ast.c +++ b/srcs/parsing/ast/ast.c @@ -26,8 +26,8 @@ t_ast_n *create_ast_n(t_node *lst, t_ast_n *parent, t_msh *msh, bool subsh) node->parent = parent; node->_stdout = 1; node->_stdin = 0; - node->save_stdo = 1; - node->save_stdi = 0; + node->save_stdo = -1; + node->save_stdi = -1; node->sh = subsh; if (node->state == _AND || node->state == _OR) create_and_or(node, lst, token, msh); diff --git a/srcs/parsing/ast/utils/free_ast.c b/srcs/parsing/ast/utils/free_ast.c index be3dab9..c6884da 100644 --- a/srcs/parsing/ast/utils/free_ast.c +++ b/srcs/parsing/ast/utils/free_ast.c @@ -38,6 +38,10 @@ static void free_pline(t_ast_n *node) void free_ast(t_ast_n *node) { + if (node->save_stdi != -1) + close(node->save_stdi); + if (node->save_stdo != -1) + close(node->save_stdo); if (node->state == _AND || node->state == _OR) { free_ast(node->left); diff --git a/srcs/sig.c b/srcs/sig.c index 68444ea..69d55f2 100644 --- a/srcs/sig.c +++ b/srcs/sig.c @@ -31,15 +31,6 @@ void init_sig(void) sigaction(SIGQUIT, &(sa[1]), NULL); } -void handle_sigint(int sig) -{ - (void)sig; - write(2, "\n\n", 2); - rl_on_new_line(); - rl_replace_line("", 0); - rl_redisplay(); -} - void handle_sigquit(int sig) { (void)sig;