106 lines
2.4 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-07-15 14:27:43 +02:00
/* Updated: 2024/07/15 14:02:18 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
void sig_c(int code)
{
(void)code;
ft_putchar_fd('\n', STDOUT_FILENO);
rl_on_new_line();
rl_replace_line("", 0);
rl_redisplay();
2024-07-14 15:39:28 +02:00
get_exit_code(0);
}
2024-07-09 11:25:22 +02:00
bool run_checks(char *rl)
{
if (!*rl)
return (true);
if (check_syntax(rl))
return (true);
if (check_quote(rl))
return (true);
if (check_pipe(rl))
return (true);
return (false);
}
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-05-02 15:50:58 +02:00
char *prompt;
2024-07-09 16:24:25 +02:00
t_env *env_l;
t_list *cmd_list;
t_list *piped;
2024-04-25 11:46:41 +02:00
(void)ac;
2024-07-09 16:24:25 +02:00
rl = NULL;
get_program_name(av[0]);
2024-07-09 16:24:25 +02:00
env_l = env_init(env);
get_env(&env_l);
2024-07-09 16:24:25 +02:00
if (!env_l)
return (EXIT_FAILURE);
2024-07-15 14:27:43 +02:00
get_exit_code(0);
2024-04-25 11:46:41 +02:00
while (1)
{
signal(SIGINT, &sig_c);
2024-07-13 18:52:00 +02:00
signal(SIGQUIT, SIG_IGN);
prompt = get_prompt(env_l);
rl = readline(prompt);
2024-05-02 15:54:16 +02:00
free(prompt);
if (!rl)
2024-07-14 15:39:28 +02:00
{
get_exit_code(0);
2024-07-09 16:24:25 +02:00
break ;
2024-07-14 15:39:28 +02:00
}
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-14 15:39:28 +02:00
add_history(rl);
rl = env_var_replace(rl, env_l);
piped = tokenizer(rl);
get_list(&piped);
if (check_argv(piped))
{
2024-07-14 15:39:28 +02:00
free(rl);
ft_lstclear(&piped, &free_token);
continue ;
}
cmd_list = get_cmd_list(piped);
free(rl);
ft_lstclear(&piped, &free_token);
if (!cmd_list)
continue ;
if (format_quotes(cmd_list))
{
ft_lstclear(get_list(NULL), &free_cmd);
continue ;
}
get_list(&cmd_list);
if (check_redir(cmd_list))
{
ft_lstclear(get_list(NULL), &free_cmd);
continue ;
}
2024-07-09 16:24:25 +02:00
exec_split_cmd(cmd_list, env_l);
ft_lstclear(&cmd_list, &free_cmd);
2024-04-25 11:46:41 +02:00
}
2024-07-09 16:24:25 +02:00
free(rl);
rl_clear_history();
ft_envclear(&env_l, free);
2024-07-14 15:39:28 +02:00
return (get_exit_code(-1));
2024-07-09 16:24:25 +02:00
}