77 lines
1.9 KiB
C
Raw Normal View History

2024-04-25 11:46:41 +02:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
2024-04-25 11:46:41 +02:00
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 11:18:04 by adjoly #+# #+# */
2024-08-06 17:28:32 +02:00
/* Updated: 2024/08/06 17:08:09 by adjoly ### ########.fr */
2024-04-25 11:46:41 +02:00
/* */
/* ************************************************************************** */
2024-04-30 12:36:16 +02:00
#include "minishell.h"
#include "parsing.h"
2024-04-25 11:46:41 +02:00
2024-07-18 13:32:09 +02:00
int free_end(char *rl, t_env *env_l, int exit_code, bool print)
2024-07-15 17:04:33 +02:00
{
2024-07-18 13:32:09 +02:00
free(rl);
rl_clear_history();
ft_envclear(&env_l, free);
if (print == true)
ft_putendl_fd("exit", STDOUT_FILENO);
return (get_exit_code(exit_code));
}
2024-07-15 17:04:33 +02:00
2024-07-18 13:32:09 +02:00
void __parse(char *rl, t_env *env_l)
{
2024-08-06 17:28:32 +02:00
t_list *token;
2024-07-18 13:32:09 +02:00
2024-08-06 17:28:32 +02:00
token = tokenizer(rl, env_l);
if (!token)
2024-07-18 13:32:09 +02:00
return ;
2024-08-06 17:28:32 +02:00
exec_split_cmd(token, env_l);
ft_lstclear(&token, free_cmd);
return ;
}
2024-07-18 13:32:09 +02:00
char *__rl(t_env *env_l)
2024-07-09 11:25:22 +02:00
{
2024-07-18 13:32:09 +02:00
char *rl;
char *prompt;
signal(SIGINT, &sig_c);
signal(SIGQUIT, SIG_IGN);
prompt = get_prompt(env_l);
rl = readline(prompt);
free(prompt);
return (rl);
2024-07-09 11:25:22 +02:00
}
2024-07-09 16:24:25 +02:00
int main(int ac, char **av, char **env)
2024-04-25 11:46:41 +02:00
{
char *rl;
2024-07-09 16:24:25 +02:00
t_env *env_l;
2024-04-25 11:46:41 +02:00
(void)ac;
get_program_name(av[0]);
2024-07-09 16:24:25 +02:00
env_l = env_init(env);
if (!env_l)
return (EXIT_FAILURE);
2024-07-18 13:32:09 +02:00
get_env(&env_l);
2024-07-18 17:10:53 +02:00
get_exit_code(0);
2024-04-25 11:46:41 +02:00
while (1)
{
2024-07-18 13:32:09 +02:00
rl = __rl(env_l);
if (!rl)
2024-07-18 13:32:09 +02:00
return (free_end(rl, env_l, -1, true));
if (run_checks(rl))
2024-07-09 16:24:25 +02:00
{
free(rl);
2024-07-09 16:24:25 +02:00
continue ;
}
2024-07-18 13:32:09 +02:00
__parse(rl, env_l);
2024-04-25 11:46:41 +02:00
}
2024-07-18 13:32:09 +02:00
return (free_end(rl, env_l, -1, false));
2024-07-09 16:24:25 +02:00
}