-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
99 lines (89 loc) · 2.93 KB
/
main.c
File metadata and controls
99 lines (89 loc) · 2.93 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ysantos- <ysantos-@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/04 17:02:58 by diosanto #+# #+# */
/* Updated: 2024/01/06 23:52:33 by ysantos- ### ########.fr */
/* */
/* ************************************************************************** */
#include "inc/minishell.h"
long long g_exit_status = 0;
void panic(t_data *data, char *error_msg, int exit_status)
{
if (data)
destroy(data);
ft_putendl_fd(error_msg, STDERR_FILENO);
exit(exit_status);
}
static char *get_input(void)
{
char *raw_input;
char *input;
bool save;
raw_input = readline("\e[0;94mminishell$\e[0m ");
save = false;
if (raw_input && !is_spaces(raw_input[0]) && ft_strlen(raw_input))
save = true;
input = trim_free(raw_input, " \t");
if (save)
add_history(input);
return (input);
}
/*Clean up the parsed list and reset data structure
1. Clear the parsed statement list
2. Set the pointer to the statement list to NULL
3. Set the head of the data structure to NULL*/
static void clean_parsed(t_statement **statement_list, t_data *data)
{
p_lstclear(statement_list);
*statement_list = NULL;
data->head = NULL;
}
void set_def_std(void)
{
ft_data()->default_stdin = dup(STDIN_FILENO);
ft_data()->default_stdout = dup(STDOUT_FILENO);
}
/*
1. check for args being provided upon execution(error)
2. start the shell environment
3. get the user input
4. check if valid
5. add the input to the history
6. expand the input and add special characters in the input
7. check if the modified input is empty, if so, free
8. parse the input into a list of statements(statement_list var)
9. set the head of the data structure to the parsed statement list
10. exec the parsed statements
11. clean the list and reset the data structure
12. exit with sucess(not sure if it will ever reach this)*/
int main(int ac, char **av, char **envp)
{
t_data data;
t_statement *statement_list;
char *input;
if (av && ac > 1)
panic(NULL, CL_ARGUMENTS_ERR, EXIT_FAILURE);
setup_shell(envp, &data, &statement_list);
set_def_std();
while (1)
{
input = get_input();
if (!valid_input(input, &data))
continue ;
input = expander(input, &data);
if (!input[0])
{
free(input);
continue ;
}
statement_list = parser(input, 0);
data.head = statement_list;
exec_type(statement_list, &data);
clean_parsed(&statement_list, &data);
}
return (EXIT_SUCCESS);
}