-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
107 lines (99 loc) · 2.83 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
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: ysoroko <ysoroko@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/03/25 13:52:17 by ysoroko #+# #+# */
/* Updated: 2021/11/03 14:05:48 by ysoroko ### ########.fr */
/* */
/* ************************************************************************** */
#include "./include/minishell.h"
/*
** FT_CLEANUP_AND_FREE
** This function is responsible for freeing everything that was malloc'd
** during a single user's input
** It frees the input saved as a string, and also the entire t_list and
** all of its content
*/
static void ft_cleanup_and_free(char **str, t_dl_lst *lst)
{
if (str)
ft_free_str(str);
if (lst)
ft_dl_lstclear(lst, &ft_free_t_command);
}
/// Possible "errors":
// 1) CTRL + D sends an EOF to the STDIN, so the input becomes a NULL string
// 2) Only spaces in input
// 3) Unclosed quotes in input
int ft_user_input_error(char *str)
{
if (!str)
{
ft_putendl_fd("exit", STDOUT);
exit(EXIT_SUCCESS);
}
if (!str[0])
return (1);
if (ft_str_has_unclosed_quotes(str))
{
ft_err(str, NULL, "Unclosed quotes error", 1);
return (0);
}
return (0);
}
void ft_prompt(char *path)
{
char *user_input_str;
t_dl_lst *input_as_dl_command_list;
user_input_str = readline(PROMPT_NAME);
if (ft_user_input_error(user_input_str))
{
free(user_input_str);
return ;
}
add_history(user_input_str);
if (ft_str_only_has_chars_from_charset(user_input_str, SPACES))
{
free(user_input_str);
return ;
}
else if (ft_str_has_unclosed_quotes(user_input_str))
{
free(user_input_str);
return ;
}
input_as_dl_command_list = ft_input_parsing(user_input_str, path);
if (!ft_cmd_list_error(input_as_dl_command_list))
ft_execute(input_as_dl_command_list);
ft_cleanup_and_free(&user_input_str, input_as_dl_command_list);
}
int main(int ac, char **av, char **env)
{
char origin[1024];
char *path;
char *builtins_path;
if (!env)
return (1);
(void)ac;
(void)av;
builtins_path = NULL;
g_glob.fork_ret = 0;
ft_duplicate_env(env);
getcwd(origin, 1024);
ft_up_shlvl();
path = ft_strjoin_exit(origin, "/builtins/");
if (ft_env_index("EXIT_STATUS") != -1)
ft_unset("EXIT_STATUS");
ft_export("EXIT_STATUS=0");
while (1)
{
g_glob.fork_ret = 0;
ft_setup_signals();
ft_prompt(path);
}
free(path);
return (1);
}