Compare commits
12 Commits
master
...
hugo_pipes
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e08ceeaffe | ||
|
|
f8c23f85b4 | ||
|
|
f2c8898fcc | ||
|
|
4d812cd346 | ||
|
|
5279c031fa | ||
|
|
1c5203f0ef | ||
|
|
c225a51199 | ||
|
|
25d159410f | ||
|
|
6e32b4ae47 | ||
|
|
7b817bb369 | ||
|
|
226829b891 | ||
|
|
98f247a378 |
4
Makefile
4
Makefile
@@ -22,7 +22,9 @@ LIBFT_D = ./libft
|
||||
LIBFT = $(LIBFT_D)/libft.a
|
||||
|
||||
SRCS = main.c init.c free.c generic.c \
|
||||
env.c exit.c
|
||||
env.c exit.c \
|
||||
pipes_hugo.c \
|
||||
parser_hugo.c
|
||||
|
||||
DIR_OBJS = builds
|
||||
OBJS = $(SRCS:%.c=$(DIR_OBJS)/%.o)
|
||||
|
||||
52
README.md
52
README.md
@@ -56,43 +56,59 @@
|
||||
|
||||
*[\go to sommaire](#markdown-header-sommaire)*
|
||||
|
||||
## 2. parsing :
|
||||
## 2. lexer (lexique analyser :
|
||||
---
|
||||
|
||||
[tutorial on fork and exec](https://www.youtube.com/watch?v=O1UOWScmqxg)
|
||||
[wait for all childs to terminate](https://stackoverflow.com/questions/19461744/how-to-make-parent-wait-for-all-child-processes-to-finish)
|
||||
[close pipes in child AND parents](https://stackoverflow.com/questions/33884291/pipes-dup2-and-exec)
|
||||
[how a shell works](https://www.cs.purdue.edu/homes/grr/SystemsProgrammingBook/Book/Chapter5-WritingYourOwnShell.pdf)
|
||||
|
||||
### 2.1 methode 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
|
||||
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 :
|
||||
---
|
||||
|
||||
@@ -306,6 +322,20 @@ _'___"___'___"_"___'___"___'_
|
||||
## 4. notes :
|
||||
---
|
||||
|
||||
**idea about malloc protection :**
|
||||
|
||||
have them(malloc and similar) done in a specific function that would check their return and accordingly exit the program or continue
|
||||
|
||||
-> so that it can be done in one line
|
||||
|
||||
void calloc_or_exit(int num, int size);
|
||||
|
||||
and possibly give it a pointer to a function that will clean before exit
|
||||
|
||||
void calloc_or_exit(int num, int size, void (*f)(void *ptr), void *ptr);
|
||||
|
||||
---
|
||||
|
||||
Ordre Interpreteur :
|
||||
1) Couper les mots (comment faire ? je ne vois pas comment gerer ce genre de bordel ci dessous)
|
||||
```
|
||||
|
||||
14
file.txt
Normal file
14
file.txt
Normal file
@@ -0,0 +1,14 @@
|
||||
builds
|
||||
echo
|
||||
file.txt
|
||||
headers
|
||||
libft
|
||||
Makefile
|
||||
minishell
|
||||
minishell.en.subject.pdf
|
||||
README.md
|
||||
ressources
|
||||
srcs
|
||||
tests
|
||||
test.txt
|
||||
valgrind_readline.supp
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */
|
||||
/* Updated: 2021/10/10 23:55:14 by lperrey ### ########.fr */
|
||||
/* Updated: 2021/10/19 23:41:25 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -28,4 +28,11 @@ char *ft_strjoinfree(char *s1, char *s2);
|
||||
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, t_all *c);
|
||||
// parser hugo
|
||||
void **cmd_path(char **argv, char **envp);
|
||||
int handle_fd(char **input, int i, int fdin, t_cmd *cmd);
|
||||
t_list *parser(char *input, char **envp);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/08 02:35:52 by lperrey #+# #+# */
|
||||
/* Updated: 2021/10/10 05:39:09 by lperrey ### ########.fr */
|
||||
/* Updated: 2021/10/20 09:11:41 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -20,4 +20,16 @@ typedef struct s_all
|
||||
char *prompt;
|
||||
} t_all;
|
||||
|
||||
typedef struct s_cmd
|
||||
{
|
||||
char **argv;
|
||||
pid_t pid;
|
||||
void *builtin;
|
||||
int pipe_in;
|
||||
int pipe_out;
|
||||
int fd_redirect;
|
||||
int fd_in;
|
||||
int fd_out;
|
||||
} t_cmd;
|
||||
|
||||
#endif
|
||||
|
||||
83
srcs/main.c
83
srcs/main.c
@@ -1,20 +1,64 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
|
||||
/* Updated: 2021/10/10 15:13:26 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
void close_fd(t_cmd *cmd, pid_t pid)
|
||||
{
|
||||
if (cmd->fd_in != 0)
|
||||
close(cmd->fd_in);
|
||||
if (pid == 0)
|
||||
{
|
||||
if (cmd->pipe_out != 1)
|
||||
{
|
||||
close(cmd->pipe_in);
|
||||
close(cmd->pipe_out);
|
||||
}
|
||||
if (cmd->fd_out != 1)
|
||||
close(cmd->fd_out);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (cmd->fd_out != 1 && cmd->fd_redirect == 1)
|
||||
close(cmd->fd_out);
|
||||
}
|
||||
}
|
||||
|
||||
void exec_cmd(char **envp, t_list *cmd_list)
|
||||
{
|
||||
t_cmd *cmd;
|
||||
pid_t pid;
|
||||
pid_t wpid;
|
||||
int status;
|
||||
|
||||
while(cmd_list)
|
||||
{
|
||||
cmd = cmd_list->content;
|
||||
pid = fork();
|
||||
if (pid == 0)
|
||||
{
|
||||
if (cmd->fd_in != 0)
|
||||
dup2(cmd->fd_in, STDIN_FILENO);
|
||||
if (cmd->fd_out != 1)
|
||||
dup2(cmd->fd_out, STDOUT_FILENO);
|
||||
close_fd(cmd, pid);
|
||||
execve(cmd->argv[0], cmd->argv, envp);
|
||||
}
|
||||
else
|
||||
{
|
||||
close_fd(cmd, pid);
|
||||
// if (cmd->fd_in != 0)
|
||||
// close(cmd->fd_in);
|
||||
// if (cmd->fd_out != 1 && cmd->fd_redirect == 1)
|
||||
// close(cmd->fd_out);
|
||||
}
|
||||
while ((wpid = wait(&status)) > 0);
|
||||
cmd_list = cmd_list->next;
|
||||
}
|
||||
}
|
||||
|
||||
void shell_loop(t_all *c)
|
||||
{
|
||||
char *line_input;
|
||||
t_list *cmd;
|
||||
|
||||
line_input = NULL;
|
||||
while (1)
|
||||
@@ -24,12 +68,8 @@ 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
|
||||
printf("echo: %s\n", line_input);
|
||||
cmd = parser(line_input, c->envp);
|
||||
exec_cmd(c->envp, cmd);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -71,12 +111,3 @@ int main(int argc, char *argv[], char *envp[])
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** idea about malloc protection :
|
||||
** have them(mallocand similar) done in a specific function that would check their return and accordingly exit the program or continue
|
||||
** -> so that it can be done in one line
|
||||
** void calloc_or_exit(int num, int size);
|
||||
** and possibly give it a pointer to a function that will clean before exit
|
||||
** void calloc_or_exit(int num, int size, void (*f)(void *ptr), void *ptr);
|
||||
*/
|
||||
|
||||
81
srcs/parser_hugo.c
Normal file
81
srcs/parser_hugo.c
Normal file
@@ -0,0 +1,81 @@
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
void **cmd_path(char **argv, char **envp)
|
||||
{
|
||||
int i;
|
||||
char **path;
|
||||
char *cmd;
|
||||
|
||||
i = 0;
|
||||
while (envp[i] && ft_strncmp(envp[i], "PATH=", 5))
|
||||
i++;
|
||||
path = ft_split(envp[i] + 5, ':'); // 5 = lentgh of "PATH="
|
||||
i = -1;
|
||||
while (*path && i != 0)
|
||||
{
|
||||
cmd = ft_strjoin(path[0], "/");
|
||||
cmd = ft_strjoin(cmd, argv[0]);
|
||||
i = access(cmd, X_OK);
|
||||
path++;
|
||||
}
|
||||
argv[0] = cmd;
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
int handle_fd(char **input, int i, int fdin, t_cmd *cmd)
|
||||
{
|
||||
int *pipes_fd;
|
||||
int next_in;
|
||||
char *tmp;
|
||||
|
||||
cmd->fd_in = fdin;
|
||||
cmd->fd_out = 1;
|
||||
cmd->fd_redirect = 1;
|
||||
cmd->pipe_in = 0;
|
||||
cmd->pipe_out = 1;
|
||||
next_in = 0;
|
||||
if (input[i + 1])
|
||||
{
|
||||
pipes_fd = calloc(2, sizeof(int));
|
||||
pipe(pipes_fd);
|
||||
cmd->fd_out = pipes_fd[1];
|
||||
next_in = pipes_fd[0];
|
||||
cmd->pipe_in = pipes_fd[0];
|
||||
cmd->pipe_out = pipes_fd[1];
|
||||
}
|
||||
tmp = ft_strchr(input[i], '>');
|
||||
if (tmp)
|
||||
{
|
||||
tmp[0] = '\0';
|
||||
tmp = ft_strtrim(tmp + 2, " "); // +2 for "> "
|
||||
cmd->fd_out = open(tmp, O_RDWR | O_TRUNC);
|
||||
next_in = cmd->fd_out;
|
||||
cmd->fd_redirect = cmd->fd_out;
|
||||
}
|
||||
return (next_in);
|
||||
}
|
||||
|
||||
t_list *parser(char *input, char **envp)
|
||||
{
|
||||
t_list *cmd;
|
||||
t_cmd *element;
|
||||
char **input_split;
|
||||
int i;
|
||||
int tmp_fd;
|
||||
|
||||
input_split = ft_split(input, '|');
|
||||
tmp_fd = 0;
|
||||
i = 0;
|
||||
cmd = NULL;
|
||||
while (input_split[i])
|
||||
{
|
||||
element = calloc(1, sizeof(t_cmd));
|
||||
tmp_fd = handle_fd(input_split, i, tmp_fd, element);
|
||||
element->argv = ft_split(input_split[i], ' ');
|
||||
element->builtin = cmd_path(element->argv, envp);
|
||||
ft_lstadd_back(&cmd, ft_lstnew(element));
|
||||
i++;
|
||||
}
|
||||
return (cmd);
|
||||
}
|
||||
53
srcs/pipes_hugo.c
Normal file
53
srcs/pipes_hugo.c
Normal file
@@ -0,0 +1,53 @@
|
||||
#include "minishell.h"
|
||||
|
||||
int size_tab(char **split)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (*split++)
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
|
||||
void print_tab(char **array)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (array[i])
|
||||
{
|
||||
printf("%i: [%s]\n", i, array[i]);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void cmd_execution(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;
|
||||
cmd_execution(split[i], c);
|
||||
}
|
||||
Reference in New Issue
Block a user