-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathminishell.c
118 lines (108 loc) · 2.55 KB
/
minishell.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minishell.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: galetha <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/20 20:46:09 by wjonatho #+# #+# */
/* Updated: 2022/03/03 18:23:39 by galetha ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int ft_process4(char *str, t_envp **list_env)
{
t_comm *lst;
char **new_env;
lst = NULL;
new_env = ft_update_env(*list_env);
lst = ft_parser4(lst, str, *list_env);
if (lst == NULL)
{
if (new_env)
clean_env(new_env);
remove_all_list(lst);
return (0);
}
if (executor(&lst, new_env) == -1)
return (-1);
if (new_env)
clean_env(new_env);
*list_env = lst->e;
remove_all_list(lst);
return (0);
}
void ft_up_shlvl(t_envp *list_env)
{
t_envp *head;
int shlvl;
char *tmp;
int locate;
locate = 0;
head = list_env;
locate = locate_env_key(head, "SHLVL");
if (locate == -1)
add_to_env(list_env, "SHLVL", "1");
else
{
shlvl = ft_atoi(get_env_value(list_env, locate)) + 1;
tmp = ft_itoa(shlvl);
upd_env_value(list_env, tmp, locate);
if (tmp)
free(tmp);
}
}
int ft_support_circle(char *str, int *i, t_envp **list_env)
{
if (ft_is_space(str, i) == -1)
{
free (str);
return (-1);
}
if (ft_lexer(str) != -1)
{
if (ft_process4(str, list_env) == -1)
exit(0);
}
else
free (str);
return (0);
}
int ft_main_circle(char *str, t_envp **list_env)
{
int i;
i = 0;
str = readline("bash:");
if (str && *str)
{
if (ft_support_circle(str, &i, list_env) == -1)
return (0);
}
else if (str == NULL)
{
write(1, "exit\n", 5);
remove_all_env_list(*list_env);
exit(0);
}else{
free (str);
}
return (0);
}
int main(void)
{
char *str;
t_envp *list_env;
extern char **environ;
str = NULL;
list_env = NULL;
list_env = ft_node_env(list_env, environ);
ft_up_shlvl(list_env);
while (1)
{
signal(SIGINT, handler);
signal(SIGQUIT, SIG_IGN);
ft_main_circle(str, &list_env);
}
remove_all_env_list(list_env);
return (0);
}