ca marche a peu pres, il faut gerer la fermeture des fd entre pipes et redirections
This commit is contained in:
3
Makefile
3
Makefile
@@ -23,7 +23,8 @@ LIBFT = $(LIBFT_D)/libft.a
|
||||
|
||||
SRCS = main.c init.c free.c generic.c \
|
||||
env.c exit.c \
|
||||
pipes_hugo.c
|
||||
pipes_hugo.c \
|
||||
parser_hugo.c
|
||||
|
||||
DIR_OBJS = builds
|
||||
OBJS = $(SRCS:%.c=$(DIR_OBJS)/%.o)
|
||||
|
||||
14
file.txt
14
file.txt
@@ -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/16 15:20:32 by hulamy ### ########.fr */
|
||||
/* Updated: 2021/10/19 21:59:24 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -30,5 +30,9 @@ 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, int *fd_in, int *fd_out);
|
||||
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/19 21:58:02 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -20,4 +20,13 @@ typedef struct s_all
|
||||
char *prompt;
|
||||
} t_all;
|
||||
|
||||
typedef struct s_cmd
|
||||
{
|
||||
char **argv;
|
||||
pid_t pid;
|
||||
void *builtin;
|
||||
int fd_in;
|
||||
int fd_out;
|
||||
} t_cmd;
|
||||
|
||||
#endif
|
||||
|
||||
253
srcs/main.c
253
srcs/main.c
@@ -1,244 +1,47 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
|
||||
/* Updated: 2021/10/19 21:27:19 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
void print_tab(char **array);
|
||||
|
||||
typedef struct s_pipe
|
||||
{
|
||||
int nb_pipes;
|
||||
int **pipes_fd;
|
||||
char **input_split;
|
||||
} 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 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;
|
||||
|
||||
if (!nb)
|
||||
return (NULL);
|
||||
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);
|
||||
}
|
||||
|
||||
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 = calloc(1, sizeof(t_pipe));
|
||||
pipes->nb_pipes = count_pipes(input);
|
||||
pipes->pipes_fd = create_pipes(pipes->nb_pipes);
|
||||
pipes->input_split = split_pipes(input);
|
||||
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 exec_cmd(char **envp, t_list *cmd_list)
|
||||
{
|
||||
t_cmd *cmd;
|
||||
pid_t pid;
|
||||
pid_t wpid;
|
||||
int status;
|
||||
int i;
|
||||
|
||||
status = 0;
|
||||
i = 0;
|
||||
while (i <= pipes->nb_pipes)
|
||||
while(cmd_list)
|
||||
{
|
||||
cmd = cmd_list->content;
|
||||
pid = fork();
|
||||
if (pid == 0) // child
|
||||
if (pid == 0)
|
||||
{
|
||||
if (pipes->nb_pipes)
|
||||
if (cmd->fd_in != 0)
|
||||
{
|
||||
if (i == 0)
|
||||
dup2(pipes->pipes_fd[0][1], STDOUT_FILENO);
|
||||
if (i == 1)
|
||||
dup2(pipes->pipes_fd[0][0], STDIN_FILENO);
|
||||
close(pipes->pipes_fd[0][0]);
|
||||
close(pipes->pipes_fd[0][1]);
|
||||
dup2(cmd->fd_in, STDIN_FILENO);
|
||||
close(cmd->fd_in);
|
||||
}
|
||||
if (!ft_strncmp(pipes->input_split[i], "sleep ", 6))
|
||||
execve("/bin/sleep", ft_split(pipes->input_split[i], ' '), c->envp);
|
||||
if (!ft_strncmp(pipes->input_split[i], "ls ", 3))
|
||||
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 (cmd->fd_out != 1)
|
||||
{
|
||||
dup2(cmd->fd_out, STDOUT_FILENO);
|
||||
close(cmd->fd_out);
|
||||
}
|
||||
execve(cmd->argv[0], cmd->argv, envp);
|
||||
}
|
||||
if (pid > 0) // parent
|
||||
else
|
||||
{
|
||||
if (pipes->nb_pipes)
|
||||
{
|
||||
close(pipes->pipes_fd[0][0]);
|
||||
close(pipes->pipes_fd[0][1]);
|
||||
}
|
||||
if (cmd->fd_in != 0)
|
||||
close(cmd->fd_in);
|
||||
if (cmd->fd_out != 1)
|
||||
close(cmd->fd_out);
|
||||
}
|
||||
i++;
|
||||
while ((wpid = wait(&status)) > 0);
|
||||
cmd_list = cmd_list->next;
|
||||
}
|
||||
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)
|
||||
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->fd_in), &(element->fd_out));
|
||||
element->argv = ft_split(input_split[i], ' ');
|
||||
element->builtin = cmd_path(element->argv, envp);
|
||||
ft_lstadd_back(&cmd, ft_lstnew(element));
|
||||
i++;
|
||||
}
|
||||
return (cmd);
|
||||
}
|
||||
|
||||
void shell_loop(t_all *c)
|
||||
{
|
||||
char *line_input;
|
||||
t_list *cmd;
|
||||
t_list *tmp;
|
||||
t_cmd *tmp_cmd;
|
||||
// t_pipe *pipes;
|
||||
// int nb_pipes;
|
||||
// int **pipes_fd;
|
||||
// char **split;
|
||||
|
||||
line_input = NULL;
|
||||
while (1)
|
||||
@@ -249,19 +52,7 @@ void shell_loop(t_all *c)
|
||||
if (line_input && *line_input)
|
||||
{
|
||||
cmd = 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
|
||||
// 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);
|
||||
exec_cmd(c->envp, cmd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
75
srcs/parser_hugo.c
Normal file
75
srcs/parser_hugo.c
Normal file
@@ -0,0 +1,75 @@
|
||||
|
||||
#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, int *fd_in, int *fd_out)
|
||||
{
|
||||
int *pipes_fd;
|
||||
int next_in;
|
||||
char *tmp;
|
||||
|
||||
*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];
|
||||
}
|
||||
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;
|
||||
}
|
||||
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->fd_in), &(element->fd_out));
|
||||
element->argv = ft_split(input_split[i], ' ');
|
||||
element->builtin = cmd_path(element->argv, envp);
|
||||
ft_lstadd_back(&cmd, ft_lstnew(element));
|
||||
i++;
|
||||
}
|
||||
return (cmd);
|
||||
}
|
||||
Reference in New Issue
Block a user