CO-CODE Hugo-Luke

+ signals handling adjusted
+ wait_subshell() with last_exit_status
+ miscs
This commit is contained in:
LuckyLaszlo
2021-11-17 01:35:06 +01:00
parent 965cf99ca5
commit 26993144cc
8 changed files with 185 additions and 128 deletions

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */
/* Updated: 2021/11/16 21:06:00 by lperrey ### ########.fr */
/* Updated: 2021/11/16 22:58:35 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -80,7 +80,6 @@ char **ft_split_quotes(char const *s, char c);
char *ft_strdup_quotes(const char *s);
// signals.c
void sigint_handler_interactiv(int signum);
void sigint_handler_execution(int signum);
void sigint_handler_interactive(int signum);
#endif

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */
/* Updated: 2021/11/16 08:42:01 by lperrey ### ########.fr */
/* Updated: 2021/11/17 01:08:38 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -18,9 +18,10 @@
int pipeline(t_all *c);
int open_pipes(t_cmd *pipeline[]);
int pipeline_access_cmd(t_cmd *pipeline[], char *path[]);
void pipeline_exec(t_cmd *pipeline[], t_all *c);
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)
{
@@ -45,9 +46,8 @@ int pipeline(t_all *c)
}
else
{
pipeline_exec(c->cmd_arr, c);
wait_subshell(pipeline_exec(c->cmd_arr, c), &c->last_exit_status);
free_pipeline(&c->cmd_arr);
// TODO wait process here
}
return (1);
}
@@ -95,7 +95,7 @@ int simple_cmd_builtin(t_cmd *cmd, t_all *c)
// TODO : Change exit status as in documentation :
// https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_08_02
void pipeline_exec(t_cmd *pipeline[], t_all *c)
pid_t pipeline_exec(t_cmd *pipeline[], t_all *c)
{
int i;
int ret;
@@ -103,7 +103,7 @@ void pipeline_exec(t_cmd *pipeline[], t_all *c)
i = 0;
while (pipeline[i])
{
if (pipeline[i]->pid == 0) // Pid var as error mark, WIP
if (!pipeline[i]->error)
{
ret = cmd_exec_in_subshell(pipeline[i], c);
if (ret != EXIT_SUCCESS)
@@ -111,6 +111,49 @@ void pipeline_exec(t_cmd *pipeline[], t_all *c)
}
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)
@@ -120,6 +163,8 @@ int cmd_exec_in_subshell(t_cmd *cmd, t_all *c)
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()"));

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/10 23:53:17 by lperrey #+# #+# */
/* Updated: 2021/11/16 22:24:36 by lperrey ### ########.fr */
/* Updated: 2021/11/17 01:25:35 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -18,6 +18,7 @@ int free_exit(t_all *c, int exit_status)
free(c->prompt);
ft_lstclear((t_list **)&c->token_list, free); // a voir avec Hugo, il y a un truc qui me semble superflu dans la fonction
ft_free_2d_arr(c->envp);
ft_free_2d_arr(c->path);
free_pipeline(&c->cmd_arr);
//if (c->termios_changed)
// tcsetattr(STDIN_FILENO, TCSANOW, &c->ori_termios);
@@ -55,11 +56,17 @@ void close_pipeline_fd(t_cmd *pipeline[])
while (pipeline[i])
{
if (pipeline[i]->fd_in != STDIN_FILENO && pipeline[i]->fd_in > 0)
{
if (close(pipeline[i]->fd_in) == -1)
perror("close()");
pipeline[i]->fd_in = 0;
}
if (pipeline[i]->fd_out != STDOUT_FILENO && pipeline[i]->fd_out > 0)
{
if (close(pipeline[i]->fd_out) == -1)
perror("close()");
pipeline[i]->fd_out = 0;
}
i++;
}
}

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
/* Updated: 2021/11/02 14:03:31 by hulamy ### ########.fr */
/* Updated: 2021/11/08 04:05:41 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -20,9 +20,10 @@ int main(int argc, char *argv[], char *envp[])
(void)argv;
if (!init(&c, envp))
free_exit(&c, EXIT_FAILURE);
if (isatty(STDIN_FILENO))
shell_loop(&c);
else
shell_script(&c);
putenv("VAR=W1 W2 W3"); // TEMP TEST
//if (isatty(STDIN_FILENO))
shell_loop(&c);
//else
// shell_script(&c);
return (0);
}

View File

@@ -73,7 +73,7 @@ int fill_builtin(t_cmd *cmd, int (*builtin)(int, char **, t_all *))
}
int handle_builtin(t_cmd *cmd)
{
{ // Il faut penser à comparer un char de plus (\0 inclus)
if (!ft_strncmp(cmd->argv[0], "echo", 4))
return (fill_builtin(cmd, &builtin_echo));
// else if (!ft_strncmp(cmd->argv[0], "cd", 2))
@@ -133,7 +133,7 @@ void fd_redirection(t_token *token, t_cmd *cmd)
if (token->id == T_LESS) // '<'
{
flag = O_RDONLY | O_CREAT;
flag = O_RDONLY | O_CREAT; // O_CREAT ? Pourquoi donc ?
if (cmd->fd_in != 0)
close(cmd->fd_in);
cmd->fd_in = open(token->next->content, flag);

View File

@@ -0,0 +1,106 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split_MODIF.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/13 07:08:40 by lperrey #+# #+# */
/* Updated: 2021/11/13 20:24:33 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
// FT_SPLIT LEGEREMENT REMANIER.
// A REMPLACER DANS LA LIBFT
#include "libft.h"
static size_t count_word(char const *s, char c);
static char **alloc_words(char const *s, char c, char **str_arr,
size_t words_count);
static void fill_arr(char const *s, char c, char **str_arr);
char **ft_split(char const *s, char c)
{
char **str_arr;
size_t words_count;
if (s == NULL)
return (NULL);
words_count = count_word(s, c);
str_arr = ft_calloc(words_count + 1, sizeof(char *));
if (!str_arr)
return (NULL);
if (!(alloc_words(s, c, str_arr, words_count)))
{
ft_free_2d_arr(str_arr);
return (NULL);
}
fill_arr(s, c, str_arr);
return (str_arr);
}
static size_t count_word(char const *s, char c)
{
unsigned int i;
size_t count;
count = 0;
i = 0;
while (s[i])
{
while (s[i] == c)
i++;
if (s[i])
count++;
while (s[i] && s[i] != c)
i++;
}
return (count);
}
static char **alloc_words(char const *s, char c, char **str_arr,
size_t words_count)
{
unsigned int i;
size_t len;
unsigned int arr_i;
i = 0;
arr_i = 0;
while (arr_i < words_count)
{
len = 0;
while (s[i] == c)
i++;
while (s[i] && s[i] != c)
{
len++;
i++;
}
str_arr[arr_i] = malloc(len + 1);
if (!str_arr[arr_i])
return (NULL);
arr_i++;
}
return (str_arr);
}
static void fill_arr(char const *s, char c, char **str_arr)
{
unsigned int i;
unsigned int arr_i;
unsigned int char_i;
i = 0;
arr_i = 0;
while (str_arr[arr_i])
{
while (s[i] == c)
i++;
char_i = 0;
while (s[i] && s[i] != c)
str_arr[arr_i][char_i++] = s[i++];
str_arr[arr_i][char_i] = '\0';
arr_i++;
}
}

View File

@@ -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);
}
} */

View File

@@ -6,31 +6,24 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/23 18:56:53 by lperrey #+# #+# */
/* Updated: 2021/11/15 20:08:26 by hulamy ### ########.fr */
/* Updated: 2021/11/16 22:58:29 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void sigint_handler_interactiv(int signum)
void sigint_handler_interactive(int signum)
{
(void)signum;
write(1, "\n", 1);
rl_replace_line("", 1);
rl_on_new_line();
rl_replace_line("", 1);
rl_redisplay();
}
void sigint_handler_execution(int signum)
{
(void)signum;
write(1, "\n", 1);
// exit(0);
}
int set_signals_handling(struct sigaction *signal_behaviour)
{
signal_behaviour->sa_handler = sigint_handler_interactiv;
signal_behaviour->sa_handler = sigint_handler_interactive;
sigaction(SIGINT, signal_behaviour, NULL);
signal_behaviour->sa_handler = SIG_IGN;
sigaction(SIGQUIT, signal_behaviour, NULL);