/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* shell_loop.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */ /* Updated: 2021/11/10 13:43:10 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" void close_fd(t_cmd *cmd); void execute_cmd(char **envp, t_cmd **cmd_arr); void shell_loop(t_all *c) { char *line_input; line_input = NULL; while (1) { if (line_input) free(line_input); line_input = readline(c->prompt); if (line_input && *line_input) { add_history(line_input); c->token_list = input_to_tokens(line_input); c->cmd_arr = parsing(c->token_list, c->envp); execute_cmd(c->envp, c->cmd_arr); ft_lstclear((t_list **)&c->token_list, free); } else if (!line_input) { write(1, "exit\n", 5); exit(0); } } } void close_fd(t_cmd *cmd) { if (cmd->fd_in != 0) close(cmd->fd_in); if (cmd->fd_out != 1) close(cmd->fd_out); } void execute_cmd(char **envp, t_cmd **cmd_arr) { pid_t pid; pid_t wpid; int status; int i; i = 0; while(cmd_arr[i]) { pid = fork(); if (pid == 0) { if (cmd_arr[i]->fd_in != 0) dup2(cmd_arr[i]->fd_in, STDIN_FILENO); if (cmd_arr[i]->fd_out != 1) dup2(cmd_arr[i]->fd_out, STDOUT_FILENO); close_fd(cmd_arr[i]); execve(cmd_arr[i]->argv[0], cmd_arr[i]->argv, envp); } else close_fd(cmd_arr[i]); i++; } // waitpid pour la derniere commande (pour '$?') while ((wpid = wait(&status)) > 0); }