fds and sigint

This commit is contained in:
Loic Deridder
2025-02-13 09:10:54 +01:00
parent b57118d0ae
commit 842e31c68f
7 changed files with 42 additions and 13 deletions

View File

@@ -65,6 +65,10 @@ int exec(t_ast_n *node)
char *path; char *path;
expand_node(node); 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); path = find_path(node->cmd, node->msh->env);
if (!path) if (!path)
return_error(node->cmd, "command not found", 127, node); return_error(node->cmd, "command not found", 127, node);

View File

@@ -16,6 +16,13 @@ void exec_pcmd(t_ast_n *pcmd)
{ {
int ret; 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) if (!pcmd->cmd)
exit(0); exit(0);
if (is_builtin(pcmd->cmd)) if (is_builtin(pcmd->cmd))
@@ -35,7 +42,6 @@ void exec_pchild(int *pipes, int index, t_ast_n *pcmd, int cmds)
ret = 0; ret = 0;
if (index < cmds - 1) if (index < cmds - 1)
dup2(pipes[1], STDOUT_FILENO); dup2(pipes[1], STDOUT_FILENO);
close(pcmd->msh->hist);
close(pipes[0]); close(pipes[0]);
close(pipes[1]); close(pipes[1]);
handle_redir(pcmd); handle_redir(pcmd);

View File

@@ -15,6 +15,8 @@
#include <readline/readline.h> #include <readline/readline.h>
#include <termios.h> #include <termios.h>
int g_sig = 0;
static void add_prevhistory(t_msh *msh) static void add_prevhistory(t_msh *msh)
{ {
char *str; char *str;
@@ -31,10 +33,26 @@ 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) static void exit_manual(t_msh *msh)
{ {
int ret; int ret;
if (g_sig == SIGINT)
{
ret = 130;
g_sig = 0;
}
else
ret = msh->ex_code; ret = msh->ex_code;
free_msh(msh); free_msh(msh);
ft_fprintf(2, "exit\n"); ft_fprintf(2, "exit\n");
@@ -54,6 +72,11 @@ int interactive_mode(t_msh *msh)
} }
if (!msh->input) if (!msh->input)
return (1); return (1);
if (g_sig == SIGINT)
{
msh->ex_code = 130;
g_sig = 0;
}
interpret_cmd(&msh->input, msh); interpret_cmd(&msh->input, msh);
return (1); return (1);
} }

View File

@@ -37,6 +37,7 @@ t_msh *init_msh(char **envp)
close(fd); close(fd);
msh->hist = open(".mmoat_history", O_RDWR | O_CREAT | O_APPEND, 0666); msh->hist = open(".mmoat_history", O_RDWR | O_CREAT | O_APPEND, 0666);
msh->ex_code = 0; msh->ex_code = 0;
msh->here_fd = -1;
msh->input = NULL; msh->input = NULL;
if (!msh) if (!msh)
return (NULL); return (NULL);

View File

@@ -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->parent = parent;
node->_stdout = 1; node->_stdout = 1;
node->_stdin = 0; node->_stdin = 0;
node->save_stdo = 1; node->save_stdo = -1;
node->save_stdi = 0; node->save_stdi = -1;
node->sh = subsh; node->sh = subsh;
if (node->state == _AND || node->state == _OR) if (node->state == _AND || node->state == _OR)
create_and_or(node, lst, token, msh); create_and_or(node, lst, token, msh);

View File

@@ -38,6 +38,10 @@ static void free_pline(t_ast_n *node)
void free_ast(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) if (node->state == _AND || node->state == _OR)
{ {
free_ast(node->left); free_ast(node->left);

View File

@@ -31,15 +31,6 @@ void init_sig(void)
sigaction(SIGQUIT, &(sa[1]), NULL); 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 handle_sigquit(int sig)
{ {
(void)sig; (void)sig;