creation struc pipes
This commit is contained in:
53
srcs/main.c
53
srcs/main.c
@@ -6,12 +6,21 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
|
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
|
||||||
/* Updated: 2021/10/18 08:59:59 by hulamy ### ########.fr */
|
/* Updated: 2021/10/18 11:25:43 by hulamy ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
|
void print_tab(char **array);
|
||||||
|
|
||||||
|
typedef struct s_pipe
|
||||||
|
{
|
||||||
|
int nb_pipe;
|
||||||
|
int **pipes_fd;
|
||||||
|
char **input_split;
|
||||||
|
} t_pipe;
|
||||||
|
|
||||||
int count_pipes(char *input)
|
int count_pipes(char *input)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
@@ -44,13 +53,35 @@ int **create_pipes(int nb)
|
|||||||
return (pipes_fd);
|
return (pipes_fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
void cmd_execution(t_all *c, int **pipes_fd, int nb_pipes, char *line_input)
|
char **split_pipes(char *input)
|
||||||
|
{
|
||||||
|
char **split;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
split = ft_split(input, '|');
|
||||||
|
i = -1;
|
||||||
|
while (split[++i])
|
||||||
|
split[i] = ft_strtrim(split[i], " ");
|
||||||
|
// print_tab(split);
|
||||||
|
return (split);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_pipe *fill_pipes(char *input)
|
||||||
|
{
|
||||||
|
t_pipe *pipes;
|
||||||
|
|
||||||
|
pipes->nb_pipe = count_pipes(input);
|
||||||
|
pipes->pipes_fd = create_pipes(pipes->nb_pipes);
|
||||||
|
pipes->input_split = split_pipes(input);
|
||||||
|
return (pipes);
|
||||||
|
}
|
||||||
|
|
||||||
|
void execute_cmd(t_all *c, char *line_input, t_all *pipes)
|
||||||
{
|
{
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
|
|
||||||
(void)pipes_fd;
|
while (nb_pipes-- >= 0)
|
||||||
if (!nb_pipes)
|
{
|
||||||
return ;
|
|
||||||
pid = fork();
|
pid = fork();
|
||||||
if (pid == 0) // child
|
if (pid == 0) // child
|
||||||
{
|
{
|
||||||
@@ -62,12 +93,15 @@ void cmd_execution(t_all *c, int **pipes_fd, int nb_pipes, char *line_input)
|
|||||||
wait(0);
|
wait(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void shell_loop(t_all *c)
|
void shell_loop(t_all *c)
|
||||||
{
|
{
|
||||||
char *line_input;
|
char *line_input;
|
||||||
int nb_pipes;
|
t_pipe *pipes;
|
||||||
int **pipes_fd;
|
// int nb_pipes;
|
||||||
|
// int **pipes_fd;
|
||||||
|
// char **split;
|
||||||
|
|
||||||
line_input = NULL;
|
line_input = NULL;
|
||||||
while (1)
|
while (1)
|
||||||
@@ -77,9 +111,8 @@ void shell_loop(t_all *c)
|
|||||||
line_input = readline(c->prompt);
|
line_input = readline(c->prompt);
|
||||||
if (line_input && *line_input)
|
if (line_input && *line_input)
|
||||||
{
|
{
|
||||||
nb_pipes = count_pipes(line_input);
|
pipes = fill_pipes(line_input);
|
||||||
pipes_fd = create_pipes(nb_pipes);
|
execute_cmd(c, line_input, pipes);
|
||||||
cmd_execution(c, pipes_fd, nb_pipes, line_input);
|
|
||||||
|
|
||||||
// if (!ft_strncmp(line_input, "env", 4)) // temp placeholder
|
// if (!ft_strncmp(line_input, "env", 4)) // temp placeholder
|
||||||
// builtin_env(0, NULL, c);
|
// builtin_env(0, NULL, c);
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ void print_tab(char **array)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void execute_cmd(char *cmd, t_all *c)
|
void cmd_execution(char *cmd, t_all *c)
|
||||||
{
|
{
|
||||||
const char *filename;
|
const char *filename;
|
||||||
char **argv;
|
char **argv;
|
||||||
@@ -49,5 +49,5 @@ void pipes_hugo(char *input, t_all *c)
|
|||||||
//printf("%i\n", nbr_pipes);
|
//printf("%i\n", nbr_pipes);
|
||||||
//print_tab(split);
|
//print_tab(split);
|
||||||
i = 0;
|
i = 0;
|
||||||
execute_cmd(split[i], c);
|
cmd_execution(split[i], c);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user