fds and sigint
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
25
srcs/main.c
25
srcs/main.c
@@ -15,6 +15,8 @@
|
||||
#include <readline/readline.h>
|
||||
#include <termios.h>
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user