tout est casse je recommence avec un parser un peu mieux
This commit is contained in:
@@ -59,8 +59,10 @@
|
|||||||
## 2. lexer (lexique analyser :
|
## 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)
|
[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)
|
[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 :
|
### 2.1 methode arbre binaire :
|
||||||
|
|
||||||
|
|||||||
151
srcs/main.c
151
srcs/main.c
@@ -6,7 +6,7 @@
|
|||||||
/* 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 23:44:34 by hulamy ### ########.fr */
|
/* Updated: 2021/10/19 21:16:21 by hulamy ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -21,6 +21,16 @@ typedef struct s_pipe
|
|||||||
char **input_split;
|
char **input_split;
|
||||||
} t_pipe;
|
} t_pipe;
|
||||||
|
|
||||||
|
typedef struct s_cmd
|
||||||
|
{
|
||||||
|
char **argv;
|
||||||
|
// t_pid pid;
|
||||||
|
int pid;
|
||||||
|
void *builtin;
|
||||||
|
int fd_in;
|
||||||
|
int fd_out;
|
||||||
|
} t_cmd;
|
||||||
|
|
||||||
int count_pipes(char *input)
|
int count_pipes(char *input)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
@@ -47,17 +57,9 @@ int **create_pipes(int nb)
|
|||||||
{
|
{
|
||||||
pipes_fd[i] = calloc(2, sizeof(int));
|
pipes_fd[i] = calloc(2, sizeof(int));
|
||||||
pipe(pipes_fd[i]);
|
pipe(pipes_fd[i]);
|
||||||
printf("%i - %i\n", pipes_fd[i][0],pipes_fd[i][1]);
|
//printf("%i - %i\n", pipes_fd[i][0],pipes_fd[i][1]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
int j;
|
|
||||||
j = -1;
|
|
||||||
while (++j < nb)
|
|
||||||
{
|
|
||||||
printf("A\n");
|
|
||||||
printf("%i: %i - %i\n", j, pipes_fd[j][0], pipes_fd[j][1]);
|
|
||||||
printf("B\n");
|
|
||||||
}
|
|
||||||
return (pipes_fd);
|
return (pipes_fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,28 +84,34 @@ t_pipe *fill_pipes(char *input)
|
|||||||
pipes->nb_pipes = count_pipes(input);
|
pipes->nb_pipes = count_pipes(input);
|
||||||
pipes->pipes_fd = create_pipes(pipes->nb_pipes);
|
pipes->pipes_fd = create_pipes(pipes->nb_pipes);
|
||||||
pipes->input_split = split_pipes(input);
|
pipes->input_split = split_pipes(input);
|
||||||
int i;
|
|
||||||
i = -1;
|
|
||||||
while (++i < pipes->nb_pipes)
|
|
||||||
{
|
|
||||||
printf("C\n");
|
|
||||||
printf("%i: %i - %i\n", i, pipes->pipes_fd[i][0], pipes->pipes_fd[i][1]);
|
|
||||||
printf("D\n");
|
|
||||||
}
|
|
||||||
return (pipes);
|
return (pipes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int redirection(char **input)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while ((*input)[i] != '\0' && (*input)[i] != '>')
|
||||||
|
i++;
|
||||||
|
if ((*input)[i] == '>')
|
||||||
|
{
|
||||||
|
*input = ft_substr(*input, 0, i);
|
||||||
|
return (open("file.txt", O_WRONLY | O_TRUNC));
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// pipe[0] read end
|
||||||
|
// pipe[1] write end
|
||||||
void execute_cmd(t_all *c, t_pipe *pipes)
|
void execute_cmd(t_all *c, t_pipe *pipes)
|
||||||
{
|
{
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
pid_t wpid;
|
pid_t wpid;
|
||||||
int status;
|
int status;
|
||||||
int i;
|
int i;
|
||||||
int stdout_bak;
|
|
||||||
|
|
||||||
status = 0;
|
status = 0;
|
||||||
if (pipes->nb_pipes)
|
|
||||||
stdout_bak = dup(STDOUT_FILENO);
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i <= pipes->nb_pipes)
|
while (i <= pipes->nb_pipes)
|
||||||
{
|
{
|
||||||
@@ -113,24 +121,22 @@ void execute_cmd(t_all *c, t_pipe *pipes)
|
|||||||
if (pipes->nb_pipes)
|
if (pipes->nb_pipes)
|
||||||
{
|
{
|
||||||
if (i == 0)
|
if (i == 0)
|
||||||
{
|
|
||||||
dup2(pipes->pipes_fd[0][1], STDOUT_FILENO);
|
dup2(pipes->pipes_fd[0][1], STDOUT_FILENO);
|
||||||
}
|
|
||||||
if (i == 1)
|
if (i == 1)
|
||||||
{
|
|
||||||
dup2(pipes->pipes_fd[0][0], STDIN_FILENO);
|
dup2(pipes->pipes_fd[0][0], STDIN_FILENO);
|
||||||
dup2(stdout_bak, STDOUT_FILENO);
|
|
||||||
}
|
|
||||||
close(pipes->pipes_fd[0][0]);
|
close(pipes->pipes_fd[0][0]);
|
||||||
close(pipes->pipes_fd[0][1]);
|
close(pipes->pipes_fd[0][1]);
|
||||||
close(stdout_bak);
|
|
||||||
}
|
}
|
||||||
if (!ft_strncmp(pipes->input_split[i], "sleep ", 6))
|
if (!ft_strncmp(pipes->input_split[i], "sleep ", 6))
|
||||||
execve("/bin/sleep", ft_split(pipes->input_split[i], ' '), c->envp);
|
execve("/bin/sleep", ft_split(pipes->input_split[i], ' '), c->envp);
|
||||||
if (!ft_strncmp(pipes->input_split[i], "ls ", 3))
|
if (!ft_strncmp(pipes->input_split[i], "ls ", 3))
|
||||||
{
|
|
||||||
execve("/bin/ls", ft_split(pipes->input_split[i], ' '), c->envp);
|
execve("/bin/ls", ft_split(pipes->input_split[i], ' '), c->envp);
|
||||||
}
|
if (!ft_strncmp(pipes->input_split[i], "cat ", 4))
|
||||||
|
execve("/bin/cat", ft_split(pipes->input_split[i], ' '), c->envp);
|
||||||
|
if (!ft_strncmp(pipes->input_split[i], "wc ", 3))
|
||||||
|
execve("/usr/bin/wc", ft_split(pipes->input_split[i], ' '), c->envp);
|
||||||
|
if (!ft_strncmp(pipes->input_split[i], "sort ", 5))
|
||||||
|
execve("/usr/bin/sort", ft_split(pipes->input_split[i], ' '), c->envp);
|
||||||
}
|
}
|
||||||
if (pid > 0) // parent
|
if (pid > 0) // parent
|
||||||
{
|
{
|
||||||
@@ -138,7 +144,6 @@ void execute_cmd(t_all *c, t_pipe *pipes)
|
|||||||
{
|
{
|
||||||
close(pipes->pipes_fd[0][0]);
|
close(pipes->pipes_fd[0][0]);
|
||||||
close(pipes->pipes_fd[0][1]);
|
close(pipes->pipes_fd[0][1]);
|
||||||
close(stdout_bak);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
@@ -146,10 +151,87 @@ void execute_cmd(t_all *c, t_pipe *pipes)
|
|||||||
while ((wpid = wait(&status)) > 0);
|
while ((wpid = wait(&status)) > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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, int *fd_in, int *fd_out)
|
||||||
|
{
|
||||||
|
int *pipes_fd;
|
||||||
|
int next_in;
|
||||||
|
char *tmp;
|
||||||
|
|
||||||
|
//printf("cmd:%i", i);
|
||||||
|
*fd_in = fdin;
|
||||||
|
*fd_out = 1;
|
||||||
|
next_in = 0;
|
||||||
|
if (input[i + 1])
|
||||||
|
{
|
||||||
|
pipes_fd = calloc(2, sizeof(int));
|
||||||
|
pipe(pipes_fd);
|
||||||
|
*fd_out = pipes_fd[1];
|
||||||
|
next_in = pipes_fd[0];
|
||||||
|
//printf(" - pipes:[%i][%i]", pipes_fd[0], pipes_fd[1]);
|
||||||
|
}
|
||||||
|
tmp = ft_strchr(input[i], '>');
|
||||||
|
if (tmp)
|
||||||
|
{
|
||||||
|
tmp[0] = '\0';
|
||||||
|
tmp = ft_strtrim(tmp + 2, " "); // +2 for "> "
|
||||||
|
*fd_out = open(tmp, O_WRONLY | O_TRUNC);
|
||||||
|
next_in = *fd_out;
|
||||||
|
//printf(" - file:[%i]", *fd_out);
|
||||||
|
}
|
||||||
|
//printf(" - final_fd:[%i][%i]\n", *fd_in, *fd_out);
|
||||||
|
return (next_in);
|
||||||
|
}
|
||||||
|
|
||||||
|
//t_list *parser(char *input)
|
||||||
|
void 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;
|
||||||
|
while (input_split[i])
|
||||||
|
{
|
||||||
|
element = calloc(1, sizeof(t_cmd));
|
||||||
|
tmp_fd = handle_fd(input_split, i, tmp_fd, &(element->fd_in), &(element->fd_out));
|
||||||
|
element->argv = ft_split(input_split[i], ' ');
|
||||||
|
element->builtin = cmd_path(element->argv, envp);
|
||||||
|
cmd = ft_lstnew(element);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void shell_loop(t_all *c)
|
void shell_loop(t_all *c)
|
||||||
{
|
{
|
||||||
char *line_input;
|
char *line_input;
|
||||||
t_pipe *pipes;
|
// t_list *cmd;
|
||||||
|
// t_pipe *pipes;
|
||||||
// int nb_pipes;
|
// int nb_pipes;
|
||||||
// int **pipes_fd;
|
// int **pipes_fd;
|
||||||
// char **split;
|
// char **split;
|
||||||
@@ -162,8 +244,11 @@ 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)
|
||||||
{
|
{
|
||||||
pipes = fill_pipes(line_input);
|
// cmd = parser(line_input);
|
||||||
execute_cmd(c, pipes);
|
parser(line_input, c->envp);
|
||||||
|
// exec_cmd(c, cmd);
|
||||||
|
// pipes = fill_pipes(line_input);
|
||||||
|
// execute_cmd(c, pipes);
|
||||||
|
|
||||||
// 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);
|
||||||
|
|||||||
Reference in New Issue
Block a user