/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* main.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */ /* Updated: 2021/10/17 22:10:48 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ #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) { char *line_input; int nb_pipes; int **pipes_fd; // pid_t pid; line_input = NULL; while (1) { if (line_input) free(line_input); line_input = readline(c->prompt); if (line_input && *line_input) { nb_pipes = count_pipes(line_input); pipes_fd = create_pipes(nb_pipes); //pid = fork(); if (!ft_strncmp(line_input, "sleep ", 5)) execve("/bin/sleep", ft_split(line_input, ' '), c->envp); write(1, "t", 1); // 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 '.\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); }