/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* main.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cchauvet static char *ft_get_user_input(t_list **env) { char *line; char *prompt; char *pwd; pwd = get_pwd(2); if (pwd == NULL) return (NULL); prompt = ft_strmerger(2, pwd, "$ "); free(pwd); if (prompt == NULL) { ft_eprintf("minishell: malloc failed\n"); return (NULL); } line = readline(prompt); add_history(line); free(prompt); return (line); } static int ft_minishell(t_data *data, char *line) { t_list **cmds; int code; char *line_clean; int infile; int outfile; if (ft_syntatic_verif(line)) return (1); line_clean = ft_normalizer(line); if (line_clean == NULL) return (1); outfile = ft_outfile(line_clean); if (outfile == -2) { free(line_clean); return (1); } infile = ft_infile(line_clean); if (infile == -2) { close(outfile); free(line_clean); return (1); } cmds = ft_parse_cmds(line_clean, data->env, infile, outfile); if (cmds == NULL) { close(outfile); close(infile); ft_lstclear(cmds, ft_cmddel); free(cmds); free(line_clean); return (1); } code = ft_cmds_executor(data, cmds); ft_lstclear(cmds, ft_cmddel); free(cmds); free(line_clean); return (code); } #if DEBUG int main(int ac, char **av, char **env) { t_data data; if (ac == 1) return (1); data.env = init_env(env); if (data.env == NULL) return (1); if (ft_minishell(data.env, av[1]) == 1) { ft_lstclear(data.env, env_del); free(data.env); return (1); } ft_lstclear(data.env, env_del); free(data.env); return (0); } #else void ft_ctrlc(int num) { if (*ft_get_heredoc()) { kill(*ft_get_heredoc(), SIGQUIT); ft_putchar_fd('\n', 1); } else { rl_on_new_line(); ft_putchar_fd('\n', 1); rl_redisplay(); } } int main(int ac, char **av, char **env) { t_data data; char *line; signal(SIGINT, ft_ctrlc); data.env = init_env(env); if (data.env == NULL) return (1); if (create_value_by_key_dup("", "$", data.env) == 1 || create_value_by_key_dup("?", "0", data.env) == 1) { ft_lstclear(data.env, env_del); free(data.env); return (1); } line = ft_get_user_input(data.env); if (line == NULL) { ft_lstclear(data.env, env_del); free(data.env); return (1); } while (line != NULL) { if (ft_minishell(&data, line) == -1) break ; free(line); line = ft_get_user_input(data.env); if (line == NULL) { ft_lstclear(data.env, env_del); free(data.env); return (1); } } ft_lstclear(data.env, env_del); free(data.env); return (data.exit_code); } #endif