forked from FabianRapp/minishell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
107 lines (99 loc) · 3.24 KB
/
main.c
File metadata and controls
107 lines (99 loc) · 3.24 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mevangel <mevangel@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/17 11:00:27 by frapp #+# #+# */
/* Updated: 2024/03/27 22:55:21 by mevangel ### ########.fr */
/* */
/* ************************************************************************** */
#include "headers/minishell.h"
void add_global_data(t_ast *ast, t_shared_data *shared_data)
{
if (!ast)
return ;
add_global_data(ast->left, shared_data);
add_global_data(ast->right, shared_data);
ast->fd_to_close = INIT_VAL;
ast->fd_to_close_read = INIT_VAL;
ast->fd_to_close_write = INIT_VAL;
ast->shared_data = shared_data;
ast->exit_status = DEFAULT_EXIT_STATUS;
}
static bool init_shared_data(t_shared_data *shared_data,
char **base_env, char ***env_list, char ***exp_list)
{
if (!shared_data)
return (false);
shared_data->stop_execution = false;
*env_list = ft_initialize_env(base_env);
if (*env_list == NULL)
return (get_last_exit());
*exp_list = ft_initialize_env(base_env);
if (*exp_list == NULL)
return (ft_free_2darr(*env_list), get_last_exit());
get_env_list(env_list);
shared_data->envs = env_list;
shared_data->env_exp = exp_list;
return (true);
}
t_result init_main(int ac, t_shared_data *shared_data)
{
errno = 0;
set_signals();
set_last_exit(0);
shared_data->env_exp = NULL;
shared_data->envs = NULL;
shared_data->main_pid = get_pid();
if (ac > 2)
{
print_error(true, NULL, NULL, "max one arg allowed");
exit(1);
}
if (!shared_data->main_pid)
return (1);
return (SUCCESS);
}
void main_loop(t_cleanup_data *cleanup_data, t_shared_data *shared_data)
{
t_ast *ast;
ast = get_input(cleanup_data);
if (TESTER && !cleanup_data->input)
main_exit(cleanup_data, true, false);
if (!ast)
main_exit(cleanup_data, full_exit_status(false) == true, false);
while (1)
{
if (ast)
{
add_global_data(ast, shared_data);
ast->shared_data->cleanup_data = cleanup_data;
run_node(ast);
main_exit(cleanup_data, full_exit_status(false) == true, false);
}
ast = get_input(cleanup_data);
if (!ast)
main_exit(cleanup_data, full_exit_status(false) == true, false);
}
}
int main(int ac, char **av, char **base_env)
{
t_cleanup_data cleanup_data;
t_shared_data shared_data;
char **env_list;
char **exp_list;
if (isatty(0) && tcgetattr(0, &shared_data.base_term_settings) == -1)
return (errno);
get_base_term(true, &shared_data.base_term_settings);
cleanup_data.shared_data = &shared_data;
if (init_main(ac, &shared_data) == ERROR)
return (1);
if (!init_shared_data(&shared_data, base_env, &env_list, &exp_list))
return (ERROR);
(void)av;
shared_data.cleanup_data = &cleanup_data;
main_loop(&cleanup_data, &shared_data);
return (1);
}