From 226829b8913451e153aa8f4cc9745a2a5176c03d Mon Sep 17 00:00:00 2001 From: hugogogo Date: Sun, 17 Oct 2021 22:12:55 +0200 Subject: [PATCH] tests de pipes et fork et execve et wait --- README.md | 33 ++++++++++++------- headers/minishell_prototypes.h | 4 +-- srcs/main.c | 59 ++++++++++++++++++++++++++++------ srcs/pipes_hugo.c | 23 +++++++++++-- 4 files changed, 94 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 07c0fcf..b63a2f3 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ *[\go to sommaire](#markdown-header-sommaire)* -## 2. parsing : +## 2. lexer (lexique analyser : --- ### 2.1 methode arbre binaire : @@ -64,35 +64,46 @@ [transformer arbre normal en arbre binaire](https://fr.wikipedia.org/wiki/Arbre_binaire#Transformation_d'un_arbre_quelconque_en_un_arbre_binaire) ```text -ARCHITECTURE : +arbre lexical : all - expend $ . pipes . . redirections (program function or file) . . . arguments (nom arg arg arg ...) . . . . EXEMPLE : . . - [sort < ./prgrm 'arg1 '$VARIABLE" arg3" | grep "word.$EXTENSION" | wc -l > file] - [sort < ./prgrm 'arg1 'arg2" arg3" | grep "word.md" | wc -l > file] - . [sort < ./prgrm 'arg1 'arg2" arg3"] - . . [sort] + [< file ./prgrm 'arg1 '$VARIABLE" arg3" | grep "word.$EXTENSION" | wc -l > file] + . [< file ./prgrm 'arg1 'arg2" arg3"] + . . [file] . . [./prgrm 'arg1 'arg2" arg3"] . . . [./prgrm] - . . . ['arg1 '] - . . . [arg2] - . . . [" arg3"] + . . . ['arg1 'arg2" arg3"] . [grep "word.md"] . . . [grep] . . . ["word.md"] . [wc -l > file] + . . [file] . . [wc -l] . . . [wc] . . . [-l] - . . [file] ``` *[\go to sommaire](#markdown-header-sommaire)* +> export TEST="" +> ./test_argv $TEST foo +argv[0] : [./test_argv] +argv[1] : [foo] + +> export TEST=" bar " +> ./test_argv foo"$TEST"foo +argv[0] : [./test_argv] +argv[1] : [foo bar foo] + +> export TEST="bar" +> ./test_argv "foo "$TEST" foo" +argv[0] : [./test_argv] +argv[1] : [foo bar foo] + ## 3. gerer les quotes et la separation des arguments : --- diff --git a/headers/minishell_prototypes.h b/headers/minishell_prototypes.h index 16739e6..79e46a2 100644 --- a/headers/minishell_prototypes.h +++ b/headers/minishell_prototypes.h @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */ -/* Updated: 2021/10/15 17:18:14 by hulamy ### ########.fr */ +/* Updated: 2021/10/16 15:20:32 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,6 +29,6 @@ char *ft_strjoinfree_s1(char *s1, const char *s2); char *ft_strjoinfree_s2(const char *s1, char *s2); // pipes hugo -void pipes_hugo(char *input); +void pipes_hugo(char *input, t_all *c); #endif diff --git a/srcs/main.c b/srcs/main.c index 3938a4c..3479912 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,15 +6,48 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */ -/* Updated: 2021/10/15 17:33:46 by hulamy ### ########.fr */ +/* Updated: 2021/10/17 22:10:48 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" +int count_pipes(char *input) +{ + int i; + int nb; + + i = -1; + nb = 0; + while (input[++i]) + if (input[i] == '|') + nb++; + return (nb); +} + +int **create_pipes(int nb) +{ + int **pipes_fd; + int i; + + pipes_fd = calloc(nb, sizeof(int *)); + i = 0; + while(i < nb) + { + pipes_fd[i] = calloc(2, sizeof(int)); + pipe(pipes_fd[i]); + printf("%i - %i\n", pipes_fd[i][0],pipes_fd[i][1]); + i++; + } + return (pipes_fd); +} + void shell_loop(t_all *c) { char *line_input; + int nb_pipes; + int **pipes_fd; +// pid_t pid; line_input = NULL; while (1) @@ -24,14 +57,22 @@ void shell_loop(t_all *c) line_input = readline(c->prompt); if (line_input && *line_input) { - if (!ft_strncmp(line_input, "env", 4)) // temp placeholder - builtin_env(0, NULL, c); - else if (!ft_strncmp(line_input, "exit", 5)) // temp placeholder - builtin_exit(0, NULL, c); - else if (!ft_strncmp(line_input, "pipe ", 5)) // temp temp temp - pipes_hugo(line_input); - else - printf("echo: %s\n", line_input); + nb_pipes = count_pipes(line_input); + pipes_fd = create_pipes(nb_pipes); + //pid = fork(); + if (!ft_strncmp(line_input, "sleep ", 5)) + execve("/bin/sleep", ft_split(line_input, ' '), c->envp); + write(1, "t", 1); + +// if (!ft_strncmp(line_input, "env", 4)) // temp placeholder +// builtin_env(0, NULL, c); +// else if (!ft_strncmp(line_input, "exit", 5)) // temp placeholder +// builtin_exit(0, NULL, c); +// else if (!ft_strncmp(line_input, "pipe ", 5)) // temp temp temp +// else if (!ft_strncmp(line_input, "pipe ", 5)) // temp temp temp +// pipes_hugo(line_input, c); +// else +// printf("echo: %s\n", line_input); } } } diff --git a/srcs/pipes_hugo.c b/srcs/pipes_hugo.c index b663829..d11109d 100644 --- a/srcs/pipes_hugo.c +++ b/srcs/pipes_hugo.c @@ -17,20 +17,37 @@ void print_tab(char **array) i = 0; while (array[i]) { - printf("%i: %s\n", i, array[i]); + printf("%i: [%s]\n", i, array[i]); i++; } } -void pipes_hugo(char *input) +void execute_cmd(char *cmd, t_all *c) +{ + const char *filename; + char **argv; + char **envp; + +// (void)cmd; + filename = ft_strtrim(cmd, " "); + printf("[%s]\n", filename); +// filename = "/bin/ls"; + argv = ft_split(filename, ' '); + envp = c->envp; + execve(filename, argv, envp); +} + +void pipes_hugo(char *input, t_all *c) { char **split; int nbr_pipes; + int i; split = ft_split(input, '|'); split[0] += 5; // pour sauter la premiere entree qui est "pipe" nbr_pipes = size_tab(split); //printf("%i\n", nbr_pipes); //print_tab(split); - + i = 0; + execute_cmd(split[i], c); }