-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathminishell.c
117 lines (106 loc) · 2.71 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minishell.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tnard <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/03 10:48:16 by tnard #+# #+# */
/* Updated: 2022/01/03 10:48:16 by tnard ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "includes/minishell.h"
static char *ft_export_env(char **env)
{
char *str;
int x;
x = 1;
str = ft_strdup(env[0]);
while (env[x])
{
str = ft_strnjoin(str, "\n", 1);
str = ft_strnjoin(str, env[x], ft_strlen(env[x]));
x++;
}
return (str);
}
char *ft_readline(void)
{
char *str;
char *tmp;
str = ft_strdup("\001\e[0;32m\002minishell\001\e[0m\002:\001\e[0;34m\002");
tmp = getcwd(NULL, 0);
str = ft_strnjoin(str, tmp, ft_strlen(tmp));
free(tmp);
str = ft_strnjoin(str, "\001\e[0m\002$ ", ft_strlen("\001\e[0m\002$ "));
return (str);
}
void ft_add_shlvl(char **env)
{
char *tmp;
char *tmp1;
int x;
tmp = NULL;
tmp = ft_search_env(*env, "SHLVL");
if (tmp)
{
x = ft_atoi(tmp);
x++;
free(tmp);
tmp = ft_itoa(x);
ft_remove_env(env, "SHLVL", -1, 0);
tmp1 = ft_strjoin("SHLVL=", tmp);
free(tmp);
(*env) = ft_strnjoin((*env), "\n", 1);
(*env) = ft_strnjoin((*env), tmp1, ft_strlen(tmp1));
free(tmp1);
}
}
int ft_while(char **penv)
{
char **splitd;
char *readlin;
char *tmp;
signal(SIGINT, sig_quit);
signal(SIGQUIT, action);
readlin = ft_readline();
tmp = readline(readlin);
free(readlin);
if (!tmp)
{
ft_printf("exit\n");
return (0);
}
else if (ft_strlen(tmp) != 0)
{
add_history(tmp);
tmp = ft_penv((*penv), tmp, 0, 0);
splitd = ft_splitd((*penv), '\n');
ft_parse_command(tmp, splitd, penv, *penv);
ft_free_split(splitd);
free(tmp);
}
return (1);
}
int main(int argc, char **argv, char **envp)
{
char *penv;
(void)argc;
(void)argv;
g_exit = 0;
if (!envp[0])
penv = ft_strdup("MINISHELL=1");
else
penv = ft_export_env(envp);
ft_add_shlvl(&penv);
while (1)
{
if (ft_while(&penv))
continue ;
else
break ;
}
free(penv);
rl_clear_history();
return (0);
}