-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
67 lines (62 loc) · 2.16 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abelayad <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/15 01:28:41 by abelayad #+# #+# */
/* Updated: 2023/06/18 17:48:55 by abelayad ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
t_minishell g_minishell;
static void ft_init_minishell(char **env)
{
ft_memset(&g_minishell, 0, sizeof(t_minishell));
g_minishell.environ = env;
ft_init_envlst();
g_minishell.stdin = dup(0);
g_minishell.stdout = dup(1);
tcgetattr(STDIN_FILENO, &g_minishell.original_term);
}
static void ft_start_execution(void)
{
signal(SIGQUIT, ft_sigquit_handler);
ft_init_tree(g_minishell.ast);
if (g_minishell.heredoc_sigint)
{
ft_clear_ast(&g_minishell.ast);
g_minishell.heredoc_sigint = false;
}
tcsetattr(STDIN_FILENO, TCSANOW, &g_minishell.original_term);
g_minishell.exit_s = ft_exec_node(g_minishell.ast, false);
ft_clear_ast(&g_minishell.ast);
}
int main(int argc, char **argv, char **env)
{
((void)argc, (void)argv);
ft_init_minishell(env);
while (1)
{
ft_init_signals();
g_minishell.line = readline(PROMPT);
if (!g_minishell.line)
(ft_clean_ms(),
ft_putstr_fd("exit\n", 1), exit(g_minishell.exit_s));
if (g_minishell.line[0])
add_history(g_minishell.line);
g_minishell.tokens = ft_tokenize();
if (!g_minishell.tokens)
continue ;
g_minishell.ast = ft_parse();
if (g_minishell.parse_err.type)
{
ft_handle_parse_err();
continue ;
}
ft_start_execution();
}
ft_garbage_collector(NULL, true);
return (ft_clean_ms(), g_minishell.exit_s);
}