gestion sigint dans heredoc fonctionne basiquement

This commit is contained in:
hugogogo
2021-11-25 10:06:48 +01:00
parent ef12d6ba4b
commit 80410e6d81
11 changed files with 39 additions and 201 deletions

View File

@@ -133,6 +133,8 @@ test
[bash]$ [bash]$
``` ```
7. [force readline return without pressing ctrl-c](https://stackoverflow.com/questions/53165704/readline-c-force-return-of-certain-text-in-readline)
## schema d'execution : ## schema d'execution :
```text ```text

View File

@@ -6,14 +6,15 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */ /* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */ /* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */
/* Updated: 2021/11/18 21:32:54 by hulamy ### ########.fr */ /* Updated: 2021/11/24 18:38:11 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef MINISHELL_PROTOTYPES_H #ifndef MINISHELL_PROTOTYPES_H
# define MINISHELL_PROTOTYPES_H # define MINISHELL_PROTOTYPES_H
int // variable globale
int switch_heredoc_sigint;
// Init // Init
int init(t_all *c, char *envp[]); int init(t_all *c, char *envp[]);

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */ /* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */ /* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */
/* Updated: 2021/11/18 14:12:36 by lperrey ### ########.fr */ /* Updated: 2021/11/18 23:08:16 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */ /* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */ /* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */
/* Updated: 2021/11/18 13:37:17 by lperrey ### ########.fr */ /* Updated: 2021/11/18 23:05:26 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */ /* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */ /* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */
/* Updated: 2021/11/18 14:29:31 by lperrey ### ########.fr */ /* Updated: 2021/11/18 23:09:46 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@@ -1,183 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_cmd_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */
/* Updated: 2021/11/18 10:28:00 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
// https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_01_01
// Bien penser à mettre les ptr à NULL aprés free en cas d'erreur (pour ne pas double free si appel à free_exit())
int pipeline(t_all *c);
int open_pipes(t_cmd *pipeline[]);
int pipeline_access_cmd(t_cmd *pipeline[], char *path[]);
pid_t pipeline_exec(t_cmd *pipeline[], t_all *c);
int cmd_exec_in_subshell(t_cmd *cmd, t_all *c);
int simple_cmd_builtin(t_cmd *cmd, t_all *c);
void wait_subshell(pid_t last_cmd_pid, int *last_exit_status);
int exec_cmd_line(t_all *c)
{
if (!pipeline(c))
{
free_pipeline(&c->cmd_arr);
return (0);
}
return (1);
}
int pipeline(t_all *c)
{
if (!open_pipes(c->cmd_arr))
return (0);
if (!pipeline_access_cmd(c->cmd_arr, c->path))
return (0);
if (ft_2d_arrlen(pipeline) == 1 && c->cmd_arr[0]->builtin_func)
{
simple_cmd_builtin(c->cmd_arr[0], c);
free_pipeline(&c->cmd_arr);
}
else
{
wait_subshell(pipeline_exec(c->cmd_arr, c), &c->last_exit_status);
free_pipeline(&c->cmd_arr);
}
return (1);
}
int open_pipes(t_cmd *pipeline[])
{
int i;
int pipes[2];
i = 0;
while (pipeline[i] && pipeline[i + 1])
{
if (pipe(pipes) == -1)
return (ft_reti_perror(0, "pipe()"));
if (pipeline[i]->fd_out == STDOUT_FILENO)
pipeline[i]->fd_out = pipes[STDOUT_FILENO];
else
if (close(pipes[STDOUT_FILENO]) == -1)
perror("close()");
if (pipeline[i]->fd_in == STDIN_FILENO)
pipeline[i + 1]->fd_in = pipes[STDIN_FILENO];
else
if (close(pipes[STDIN_FILENO]) == -1)
perror("close()");
i++;
}
return (1);
}
int pipeline_access_cmd(t_cmd *pipeline[], char *path[])
{
// TODO
// Penser à : path = strdup(argv[0]);
// et non : path = argv[0];
// pour ne pas double free
return (1);
}
int simple_cmd_builtin(t_cmd *cmd, t_all *c)
{
// TODO
return (1);
}
// TODO : Change exit status as in documentation :
// https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_08_02
pid_t pipeline_exec(t_cmd *pipeline[], t_all *c)
{
int i;
int ret;
i = 0;
while (pipeline[i])
{
if (!pipeline[i]->error)
{
ret = cmd_exec_in_subshell(pipeline[i], c);
if (ret != EXIT_SUCCESS)
free_exit(c, ret);
}
i++;
}
close_pipeline_fd(c->cmd_arr);
i -= 1;
if (pipeline[i]->error)
c->last_exit_status = pipeline[i]->error;
return (pipeline[i]->pid);
}
int handle_wait_error(void)
{
if (errno == ECHILD)
return (-1);
else if (errno == EINTR)
return (0);
else
perror("wait()");
return (-1);
}
void wait_subshell(pid_t last_cmd_pid, int *last_exit_status)
{
int wstatus;
int ret;
//wstatus = 0;
if (last_cmd_pid > 0)
{
if (waitpid(last_cmd_pid, &wstatus, 0) == -1)
perror("waitpid()");
if (WIFEXITED(wstatus))
*last_exit_status = WEXITSTATUS(wstatus);
}
ret = 0;
while (ret != -1)
{
ret = wait(&wstatus);
if (ret == -1)
ret = handle_wait_error();
}
if (WIFSIGNALED(wstatus))
{
write(STDIN_FILENO, "\n", 1);
*last_exit_status = 128 + WTERMSIG(wstatus);
}
}
int cmd_exec_in_subshell(t_cmd *cmd, t_all *c)
{
cmd->pid = fork();
if (cmd->pid == -1)
perror("fork()");
if (cmd->pid == 0)
{
c->signal_behaviour.sa_handler = SIG_DFL;
sigaction(SIGINT, &c->signal_behaviour, NULL);
if (cmd->fd_in != STDIN_FILENO)
if (dup2(cmd->fd_in, STDIN_FILENO) == -1)
return (ft_reti_perror(EXIT_FAILURE, "dup2()"));
if (cmd->fd_out != STDOUT_FILENO)
if (dup2(cmd->fd_out, STDOUT_FILENO) == -1)
return (ft_reti_perror(EXIT_FAILURE, "dup2()"));
close_pipeline_fd(c->cmd_arr);
if (cmd->builtin_func)
free_exit(c, cmd->builtin_func(ft_2d_arrlen(cmd->argv), cmd->argv, c));
else if (execve("/bin/echo", cmd->argv, c->envp) == -1) // WIP, TEST
return (ft_reti_perror_io(EXIT_FAILURE, "execve() ", cmd->argv[0]));
//else if (execve(cmd->path, cmd->argv, c->envp) == -1)
// return (ft_reti_perror_io(EXIT_FAILURE, "execve() ", cmd->argv[0]));
}
return (EXIT_SUCCESS);
}

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */ /* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */ /* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
/* Updated: 2021/11/16 21:06:18 by lperrey ### ########.fr */ /* Updated: 2021/11/18 21:56:06 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */ /* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/24 10:52:40 by lperrey #+# #+# */ /* Created: 2021/10/24 10:52:40 by lperrey #+# #+# */
/* Updated: 2021/11/16 21:18:27 by lperrey ### ########.fr */ /* Updated: 2021/11/18 22:35:07 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -81,14 +81,12 @@ t_cmd **parsing(t_token *token_list)
// 2.9.1 - 3) Redirection // 2.9.1 - 3) Redirection
if (!redirections(token_list, cmd_arr)) if (!redirections(token_list, cmd_arr))
return (ft_retp_free(NULL, &cmd_arr, (t_free_f)free_pipeline)); return (ft_retp_free(NULL, &cmd_arr, (t_free_f)free_pipeline));
// Struct CMD fill // Struct CMD fill
if (!cmd_array_fill_argv(token_list, cmd_arr)) if (!cmd_array_fill_argv(token_list, cmd_arr))
return (ft_retp_free(NULL, &cmd_arr, (t_free_f)free_pipeline)); return (ft_retp_free(NULL, &cmd_arr, (t_free_f)free_pipeline));
print_cmd_array(cmd_arr); print_cmd_array(cmd_arr);
// HUGO WIP
// handle_path(cmd_arr, envp);
return (cmd_arr); return (cmd_arr);
} }

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */ /* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/11 18:46:43 by lperrey #+# #+# */ /* Created: 2021/11/11 18:46:43 by lperrey #+# #+# */
/* Updated: 2021/11/18 17:32:32 by hulamy ### ########.fr */ /* Updated: 2021/11/25 10:05:44 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -39,7 +39,9 @@ int here_doc(char *delimiter)
if (!here_doc_write(delimiter, here_doc)) if (!here_doc_write(delimiter, here_doc))
{ {
free(delimiter); free(delimiter);
return (0); if (unlink(TMP_HERE_DOC) == -1)
return (ft_reti_perror_io(-1, "unlink() ", TMP_HERE_DOC));
return (-1);
} }
free(delimiter); free(delimiter);
if (close(here_doc) == -1) if (close(here_doc) == -1)
@@ -52,6 +54,11 @@ int here_doc(char *delimiter)
return (here_doc); return (here_doc);
} }
int void_func_return_readline(void)
{
return (0);
}
static int here_doc_write(char *delimiter, int doc_fd) static int here_doc_write(char *delimiter, int doc_fd)
{ {
char *line; char *line;
@@ -60,12 +67,19 @@ static int here_doc_write(char *delimiter, int doc_fd)
signal_action.sa_handler = sigint_handler_heredoc; signal_action.sa_handler = sigint_handler_heredoc;
sigaction(SIGINT, &signal_action, NULL); sigaction(SIGINT, &signal_action, NULL);
switch_heredoc_sigint = 0;
rl_event_hook = void_func_return_readline;
line_count = 0; line_count = 0;
while (1) while (1)
{ {
line_count++; line_count++;
line = NULL; line = NULL;
line = readline("> "); line = readline("> ");
if (switch_heredoc_sigint == 1)
{
// todo : gerer le statut exit 130
return (1);
}
if (!line) if (!line)
{ // TODO : error print wrapper { // TODO : error print wrapper
ft_putstr_fd("minishell: ", 2); ft_putstr_fd("minishell: ", 2);

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */ /* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/11 18:46:43 by lperrey #+# #+# */ /* Created: 2021/11/11 18:46:43 by lperrey #+# #+# */
/* Updated: 2021/11/16 22:29:52 by lperrey ### ########.fr */ /* Updated: 2021/11/18 22:17:44 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */ /* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/23 18:56:53 by lperrey #+# #+# */ /* Created: 2021/10/23 18:56:53 by lperrey #+# #+# */
/* Updated: 2021/11/18 17:02:16 by hulamy ### ########.fr */ /* Updated: 2021/11/25 10:02:53 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -24,10 +24,16 @@ void sigint_handler_interactive(int signum)
void sigint_handler_heredoc(int signum) void sigint_handler_heredoc(int signum)
{ {
(void)signum; (void)signum;
write(1, "\n", 1); switch_heredoc_sigint = 1;
rl_on_new_line(); rl_done = 1;
rl_replace_line("", 1); // write (1, rl_line_buffer, ft_strlen(rl_line_buffer) + 1);
rl_redisplay(); // rl_line_buffer = "\004";
// write(1, "\004", 1);
// rl_on_new_line();
// rl_replace_line((char *)NULL, 1);
// rl_replace_line("\004", 1);
// rl_replace_line("", 1);
// rl_redisplay();
} }
int set_signals_handling(struct sigaction *signal_behaviour) int set_signals_handling(struct sigaction *signal_behaviour)