-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
96 lines (88 loc) · 2.46 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: heveline <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/14 14:40:33 by qcesar #+# #+# */
/* Updated: 2021/11/17 01:48:38 by heveline ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static void shlvl(t_env *env_list)
{
t_env *current;
int shlvl;
current = env_exists(env_list, "SHLVL");
if (current)
{
if (current->value)
{
shlvl = ft_atoi(current->value);
free(current->value);
}
if (!current->value || shlvl < 0)
shlvl = 0;
shlvl++;
current->value = ft_itoa(shlvl);
}
else
change_envlist("SHLVL=1", env_list);
}
static t_ctrl *ctrl_init(char **envp, int ac, char **av)
{
t_ctrl *control;
t_env *oldpwd;
(void)ac;
(void)av;
control = (t_ctrl *)malloc(sizeof(t_ctrl));
if (!control)
ft_err("Error: malloc hasn't worked");
control->mininame = ft_strdup("mini*hell: ");
control->env_list = envlist_init(envp);
control->fd_in = dup(STDIN_FILENO);
control->fd_out = dup(STDOUT_FILENO);
control->pid = 1;
control->print = 0;
shlvl(&(control->env_list));
oldpwd = env_exists(&(control->env_list), "OLDPWD");
if (oldpwd)
delete_env(oldpwd, &(control->env_list));
if (ac == 2 && ft_strequal(av[1], "-print"))
control->print = 1;
return (control);
}
static int clear_in_the_end(t_ctrl *control)
{
// rl_clear_history();
ctrl_free(control);
return (EXIT_SUCCESS);
}
int main(int ac, char **av, char **env)
{
char *line;
char **array;
t_ctrl *control;
control = ctrl_init(env, ac, av);
while (1)
{
sighandler();
line = readline(control->mininame);
if (!line)
{
ft_putstr_fd("exit\n", STDOUT_FILENO);
break ;
}
if (line[0] == '\0')
{
free(line);
continue ;
}
array = parsing(line, env);
if (!array || !array[0])
continue ;
tree(array, control, env);
}
return (clear_in_the_end(control));
}