-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
125 lines (114 loc) · 3.26 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: amentag <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/14 02:47:24 by amentag #+# #+# */
/* Updated: 2023/04/14 02:47:24 by amentag ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int g_err_code = 0;
t_env_node *ft_get_node(char *var, char *value)
{
t_env_node *node;
node = malloc(sizeof(t_env_node));
if (!node)
return (NULL);
var = ft_sp_strdup(var, "");
if (!var)
return (NULL);
value = ft_sp_strdup(value, "");
if (!value)
return (NULL);
node->var = var;
node->value = value;
node->next = NULL;
node->index = -1;
node->type = ASSIGNED;
return (node);
}
t_env_node *ft_copy_i_env(char *current)
{
t_env_node *head;
t_env_node *node;
head = NULL;
node = ft_get_node("PWD", current);
if (!node)
return (NULL);
ft_envadd_back(&head, node);
node = ft_get_node("SHLVL", "1");
if (!node)
return (NULL);
ft_envadd_back(&head, node);
node = ft_get_node("_", "/usr/bin/env");
if (!node)
return (NULL);
ft_envadd_back(&head, node);
return (head);
}
void init_head_env(t_console *con, char **env)
{
if (!env[0])
{
con->secure = 1;
con->secure_path = \
ft_sp_strdup("/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:.", "");
con->head_env = ft_copy_i_env(con->current_dir);
}
else
con->head_env = ft_copy_env(env);
if (!con->head_env)
return (ft_env_clear(&(con->head_env)), free(con), exit(1));
ft_sort_head_env(con->head_env);
}
t_console *initial(char **env)
{
t_console *con;
con = malloc(sizeof(t_console));
if (!con)
exit(1);
con->heredoc_file_name = NULL;
con->secure_path = NULL;
con->secure = 0;
con->commands = NULL;
getcwd(con->current_dir, sizeof(con->current_dir));
init_head_env(con, env);
con->definer = malloc(sizeof(t_definer));
if (!con->definer)
return (ft_env_clear(&(con->head_env)), free(con), exit(1), NULL);
con->definer->in = 0;
con->definer->out = 1;
con->env = env;
con->telda = get_telda(con->head_env);
if (!con->telda)
return (ft_env_clear(&(con->head_env)), free(con->definer), \
free(con), exit(1), NULL);
con->exit_code = 0;
con->heredoc_file_name = NULL;
return (con);
}
int main(int ac, char **av, char **env)
{
t_console *console;
char *str;
(void)ac;
(void)av;
console = initial(env);
while (1)
{
ft_handle_sig();
rl_catch_signals = 0;
str = readline("minishell >> ");
console->exit_code = g_err_code;
g_err_code = 0;
collect(str);
if (!str)
free_console(console, 0);
if (str[0] != '\0')
add_history(str);
ft_run(console, str);
}
}