tests de pipes et fork et execve et wait
This commit is contained in:
33
README.md
33
README.md
@@ -56,7 +56,7 @@
|
|||||||
|
|
||||||
*[\go to sommaire](#markdown-header-sommaire)*
|
*[\go to sommaire](#markdown-header-sommaire)*
|
||||||
|
|
||||||
## 2. parsing :
|
## 2. lexer (lexique analyser :
|
||||||
---
|
---
|
||||||
|
|
||||||
### 2.1 methode arbre binaire :
|
### 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)
|
[transformer arbre normal en arbre binaire](https://fr.wikipedia.org/wiki/Arbre_binaire#Transformation_d'un_arbre_quelconque_en_un_arbre_binaire)
|
||||||
|
|
||||||
```text
|
```text
|
||||||
ARCHITECTURE :
|
arbre lexical :
|
||||||
all
|
all
|
||||||
expend $
|
|
||||||
. pipes
|
. pipes
|
||||||
. . redirections (program function or file)
|
. . redirections (program function or file)
|
||||||
. . . arguments (nom arg arg arg ...)
|
. . . arguments (nom arg arg arg ...)
|
||||||
. . . .
|
. . . .
|
||||||
EXEMPLE : . .
|
EXEMPLE : . .
|
||||||
[sort < ./prgrm 'arg1 '$VARIABLE" arg3" | grep "word.$EXTENSION" | wc -l > file]
|
[< file ./prgrm 'arg1 '$VARIABLE" arg3" | grep "word.$EXTENSION" | wc -l > file]
|
||||||
[sort < ./prgrm 'arg1 'arg2" arg3" | grep "word.md" | wc -l > file]
|
. [< file ./prgrm 'arg1 'arg2" arg3"]
|
||||||
. [sort < ./prgrm 'arg1 'arg2" arg3"]
|
. . [file]
|
||||||
. . [sort]
|
|
||||||
. . [./prgrm 'arg1 'arg2" arg3"]
|
. . [./prgrm 'arg1 'arg2" arg3"]
|
||||||
. . . [./prgrm]
|
. . . [./prgrm]
|
||||||
. . . ['arg1 ']
|
. . . ['arg1 'arg2" arg3"]
|
||||||
. . . [arg2]
|
|
||||||
. . . [" arg3"]
|
|
||||||
. [grep "word.md"]
|
. [grep "word.md"]
|
||||||
. . . [grep]
|
. . . [grep]
|
||||||
. . . ["word.md"]
|
. . . ["word.md"]
|
||||||
. [wc -l > file]
|
. [wc -l > file]
|
||||||
|
. . [file]
|
||||||
. . [wc -l]
|
. . [wc -l]
|
||||||
. . . [wc]
|
. . . [wc]
|
||||||
. . . [-l]
|
. . . [-l]
|
||||||
. . [file]
|
|
||||||
```
|
```
|
||||||
|
|
||||||
*[\go to sommaire](#markdown-header-sommaire)*
|
*[\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 :
|
## 3. gerer les quotes et la separation des arguments :
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* 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/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);
|
char *ft_strjoinfree_s2(const char *s1, char *s2);
|
||||||
|
|
||||||
// pipes hugo
|
// pipes hugo
|
||||||
void pipes_hugo(char *input);
|
void pipes_hugo(char *input, t_all *c);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
59
srcs/main.c
59
srcs/main.c
@@ -6,15 +6,48 @@
|
|||||||
/* 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/15 17:33:46 by hulamy ### ########.fr */
|
/* Updated: 2021/10/17 22:10:48 by hulamy ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#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)
|
void shell_loop(t_all *c)
|
||||||
{
|
{
|
||||||
char *line_input;
|
char *line_input;
|
||||||
|
int nb_pipes;
|
||||||
|
int **pipes_fd;
|
||||||
|
// pid_t pid;
|
||||||
|
|
||||||
line_input = NULL;
|
line_input = NULL;
|
||||||
while (1)
|
while (1)
|
||||||
@@ -24,14 +57,22 @@ 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)
|
||||||
{
|
{
|
||||||
if (!ft_strncmp(line_input, "env", 4)) // temp placeholder
|
nb_pipes = count_pipes(line_input);
|
||||||
builtin_env(0, NULL, c);
|
pipes_fd = create_pipes(nb_pipes);
|
||||||
else if (!ft_strncmp(line_input, "exit", 5)) // temp placeholder
|
//pid = fork();
|
||||||
builtin_exit(0, NULL, c);
|
if (!ft_strncmp(line_input, "sleep ", 5))
|
||||||
else if (!ft_strncmp(line_input, "pipe ", 5)) // temp temp temp
|
execve("/bin/sleep", ft_split(line_input, ' '), c->envp);
|
||||||
pipes_hugo(line_input);
|
write(1, "t", 1);
|
||||||
else
|
|
||||||
printf("echo: %s\n", 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
|
||||||
|
// else if (!ft_strncmp(line_input, "pipe ", 5)) // temp temp temp
|
||||||
|
// pipes_hugo(line_input, c);
|
||||||
|
// else
|
||||||
|
// printf("echo: %s\n", line_input);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,20 +17,37 @@ void print_tab(char **array)
|
|||||||
i = 0;
|
i = 0;
|
||||||
while (array[i])
|
while (array[i])
|
||||||
{
|
{
|
||||||
printf("%i: %s\n", i, array[i]);
|
printf("%i: [%s]\n", i, array[i]);
|
||||||
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;
|
char **split;
|
||||||
int nbr_pipes;
|
int nbr_pipes;
|
||||||
|
int i;
|
||||||
|
|
||||||
split = ft_split(input, '|');
|
split = ft_split(input, '|');
|
||||||
split[0] += 5; // pour sauter la premiere entree qui est "pipe"
|
split[0] += 5; // pour sauter la premiere entree qui est "pipe"
|
||||||
nbr_pipes = size_tab(split);
|
nbr_pipes = size_tab(split);
|
||||||
//printf("%i\n", nbr_pipes);
|
//printf("%i\n", nbr_pipes);
|
||||||
//print_tab(split);
|
//print_tab(split);
|
||||||
|
i = 0;
|
||||||
|
execute_cmd(split[i], c);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user