CO-CODE Hugo-Luke
+ signals handling adjusted + wait_subshell() with last_exit_status + miscs
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
|
||||
/* Updated: 2021/11/16 21:22:32 by lperrey ### ########.fr */
|
||||
/* Updated: 2021/11/17 01:30:27 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -24,9 +24,13 @@ void shell_loop(t_all *c)
|
||||
{
|
||||
if (line_input)
|
||||
free(line_input);
|
||||
c->signal_behaviour.sa_handler = sigint_handler_interactive;
|
||||
sigaction(SIGINT, &c->signal_behaviour, NULL);
|
||||
line_input = readline(c->prompt);
|
||||
if (line_input && *line_input)
|
||||
{
|
||||
c->signal_behaviour.sa_handler = SIG_IGN;
|
||||
sigaction(SIGINT, &c->signal_behaviour, NULL);
|
||||
add_history(line_input);
|
||||
// Lexing
|
||||
c->token_list = input_to_tokens(line_input);
|
||||
@@ -43,105 +47,7 @@ void shell_loop(t_all *c)
|
||||
else if (!line_input)
|
||||
{
|
||||
write(1, "exit\n", 5);
|
||||
exit(0);
|
||||
free_exit(c, c->last_exit_status);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// WIP HUGO
|
||||
void close_fd(t_cmd *cmd)
|
||||
{
|
||||
if (cmd->fd_in != 0)
|
||||
close(cmd->fd_in);
|
||||
if (cmd->fd_out != 1)
|
||||
close(cmd->fd_out);
|
||||
}
|
||||
|
||||
// WIP HUGO
|
||||
void execute_cmd(char **envp, t_cmd **cmd_arr, t_all *c)
|
||||
{
|
||||
pid_t pid;
|
||||
pid_t wpid;
|
||||
int wstatus;
|
||||
int i;
|
||||
int argc;
|
||||
|
||||
// put signal handling for SIGINT to ignore so parent process will not activate signal_handling_executiv while childs are in process
|
||||
c->signal_behaviour.sa_handler = SIG_IGN;
|
||||
sigaction(SIGINT, &c->signal_behaviour, NULL);
|
||||
i = 0;
|
||||
while(cmd_arr[i])
|
||||
{
|
||||
pid = fork();
|
||||
if (pid == 0)
|
||||
{
|
||||
// activate singal handling for execution mode
|
||||
c->signal_behaviour.sa_handler = sigint_handler_execution;
|
||||
sigaction(SIGINT, &c->signal_behaviour, NULL);
|
||||
if (cmd_arr[i]->fd_in != 0)
|
||||
dup2(cmd_arr[i]->fd_in, STDIN_FILENO);
|
||||
if (cmd_arr[i]->fd_out != 1)
|
||||
dup2(cmd_arr[i]->fd_out, STDOUT_FILENO);
|
||||
close_fd(cmd_arr[i]);
|
||||
if (cmd_arr[i]->builtin_func)
|
||||
{
|
||||
argc = 0;
|
||||
while (cmd_arr[i]->argv[argc])
|
||||
argc++;
|
||||
cmd_arr[i]->builtin_func(argc, cmd_arr[i]->argv, c);
|
||||
exit(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
write(1, "1", 1);
|
||||
execve(cmd_arr[i]->argv[0], cmd_arr[i]->argv, envp);
|
||||
}
|
||||
}
|
||||
else
|
||||
close_fd(cmd_arr[i]);
|
||||
i++;
|
||||
}
|
||||
// waitpid pour la derniere commande (pour '$?')
|
||||
wpid = 1;
|
||||
while (wpid > 0)
|
||||
wpid = wait(&wstatus);
|
||||
// to print a \n after execve was terminated by a signal
|
||||
if (WIFSIGNALED(wstatus))
|
||||
write(1, "\n", 1);
|
||||
// put signal handling for sigint back to the signal handler for interactiv mode
|
||||
c->signal_behaviour.sa_handler = sigint_handler_interactiv;
|
||||
sigaction(SIGINT, &c->signal_behaviour, NULL);
|
||||
}
|
||||
|
||||
/* void test_signal(t_all *c)
|
||||
{
|
||||
// TEMP
|
||||
// A faire aprés être sortie du mode interactif
|
||||
// - Ignorer tout les signaux
|
||||
// - Remettre ori_termios
|
||||
c->signal_behaviour.sa_handler = SIG_IGN;
|
||||
sigaction(SIGINT, &c->signal_behaviour, NULL);
|
||||
sigaction(SIGQUIT, &c->signal_behaviour, NULL);
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &c->ori_termios);
|
||||
if (!fork())
|
||||
{
|
||||
char *arg_test[3];
|
||||
|
||||
arg_test[0] = ft_strdup("sleep");
|
||||
arg_test[1] = ft_strdup("3");
|
||||
arg_test[2] = NULL;
|
||||
sigaction(SIGQUIT, &c->ori_signal_behaviour, NULL);
|
||||
sigaction(SIGINT, &c->ori_signal_behaviour, NULL);
|
||||
execve("/bin/sleep", arg_test, c->envp);
|
||||
}
|
||||
else
|
||||
{
|
||||
int wait_test;
|
||||
wait(&wait_test);
|
||||
c->signal_behaviour.sa_handler = sigint_handler;
|
||||
sigaction(SIGINT, &c->signal_behaviour, NULL);
|
||||
c->signal_behaviour.sa_handler = SIG_IGN;
|
||||
sigaction(SIGQUIT, &c->signal_behaviour, NULL);
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &c->interactive_termios);
|
||||
}
|
||||
} */
|
||||
|
||||
Reference in New Issue
Block a user