211 lines
4.6 KiB
C
211 lines
4.6 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* main.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
|
|
/* Updated: 2021/10/18 22:29:02 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;
|
|
|
|
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++;
|
|
}
|
|
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);
|
|
}
|
|
|
|
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);
|
|
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);
|
|
}
|
|
|
|
void execute_cmd(t_all *c, t_pipe *pipes)
|
|
{
|
|
pid_t pid;
|
|
int i;
|
|
int stdout_bak;
|
|
|
|
if (pipes->nb_pipes)
|
|
stdout_bak = dup(STDOUT_FILENO);
|
|
i = 0;
|
|
while (i <= pipes->nb_pipes)
|
|
{
|
|
if (pipes->nb_pipes)
|
|
{
|
|
printf("%i\n", stdout_bak);
|
|
if (i == 0)
|
|
{
|
|
dup2(pipes->pipes_fd[0][1], STDOUT_FILENO);
|
|
}
|
|
if (i == 1)
|
|
{
|
|
dup2(pipes->pipes_fd[0][0], STDIN_FILENO);
|
|
dup2(stdout_bak, STDOUT_FILENO);
|
|
}
|
|
close(pipes->pipes_fd[0][0]);
|
|
close(pipes->pipes_fd[0][1]);
|
|
close(stdout_bak);
|
|
}
|
|
pid = fork();
|
|
if (pid == 0) // child
|
|
{
|
|
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))
|
|
{
|
|
write(1, "\na\n\n", 4);
|
|
execve("/bin/ls", ft_split(pipes->input_split[i], ' '), c->envp);
|
|
}
|
|
}
|
|
if (pid > 0) // parent
|
|
{
|
|
wait(0);
|
|
}
|
|
i++;
|
|
}
|
|
}
|
|
|
|
void shell_loop(t_all *c)
|
|
{
|
|
char *line_input;
|
|
t_pipe *pipes;
|
|
// int nb_pipes;
|
|
// int **pipes_fd;
|
|
// char **split;
|
|
|
|
line_input = NULL;
|
|
while (1)
|
|
{
|
|
if (line_input)
|
|
free(line_input);
|
|
line_input = readline(c->prompt);
|
|
if (line_input && *line_input)
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
void wip_test()
|
|
{
|
|
char term_desc[2048];
|
|
char *term_type;
|
|
int term_width;
|
|
int term_height;
|
|
int ret;
|
|
|
|
term_type = getenv("TERM");
|
|
if (term_type == 0)
|
|
ft_putstr_fd("Specify a terminal type with `setenv TERM <yourtype>'.\n", 2);
|
|
ret = tgetent(term_desc, term_type);
|
|
if (ret < 0)
|
|
ft_putstr_fd("Could not access the termcap data base.\n", 2);
|
|
if (ret == 0)
|
|
ft_putstr_fd("Terminal type `%s' is not defined.\n", 2);
|
|
term_height = tgetnum ("li");
|
|
term_width = tgetnum ("co");
|
|
/* Extract information that termcap functions use. */
|
|
/* temp = tgetstr ("pc", BUFFADDR);
|
|
PC = temp ? *temp : 0;
|
|
BC = tgetstr ("le", BUFFADDR);
|
|
UP = tgetstr ("up", BUFFADDR); */
|
|
}
|
|
|
|
int main(int argc, char *argv[], char *envp[])
|
|
{
|
|
t_all c;
|
|
|
|
(void)argc;
|
|
(void)argv;
|
|
if (!init(&c, envp))
|
|
exit(EXIT_FAILURE);
|
|
shell_loop(&c);
|
|
return (0);
|
|
}
|
|
|