-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminishell.c
113 lines (104 loc) · 2.87 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minishell.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abenamar <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/30 18:52:49 by abenamar #+# #+# */
/* Updated: 2023/09/27 13:26:27 by abenamar ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static t_list *ft_env(char **ep)
{
t_list *env;
size_t i;
char *var;
env = NULL;
i = 0;
while (ep[i])
{
var = ft_strdup(ep[i]);
if (var)
ft_lstadd_front(&env, ft_lstnew(var));
++i;
}
ft_env_puts(&env, "?", "0");
ft_env_puts(&env, "OLDPWD", ft_env_gets(&env, "OLDPWD"));
return (env);
}
static char *ft_prompt(t_list **env)
{
const char *home = ft_env_gets(env, "HOME");
size_t len;
char *cwd;
char *str;
char *prompt;
len = 0;
if (home)
len = ft_strlen(home);
if (ft_env_gets(env, "PWD"))
cwd = ft_strdup(ft_env_gets(env, "PWD"));
else
cwd = getcwd(NULL, 0);
if (!cwd)
return (NULL);
if (len && !ft_strncmp(cwd, home, len))
str = ft_strjoin("~", cwd + len);
else
str = ft_strdup(cwd);
(free(cwd), cwd = ft_strjoin(str, " $ \033[00m"), free(str));
if (ft_atoi(ft_env_gets(env, "?")))
prompt = ft_strjoin(__FAILURE __PROMPT, cwd);
else
prompt = ft_strjoin(__SUCCESS __PROMPT, cwd);
return (free(cwd), prompt);
}
static char *ft_readline(t_list **env)
{
char *line;
char *str;
if (isatty(STDIN_FILENO))
{
line = ft_prompt(env);
str = readline(line);
if (!str)
return (printf("exit\n"), g_signum = SIGTERM, free(line), NULL);
}
else
{
line = get_next_line(STDIN_FILENO);
str = ft_strtrim(line, "\n");
if (!str)
return (g_signum = SIGTERM, free(line), NULL);
}
if (*str)
add_history(str);
(free(line), line = ft_strtrim(str, " "), free(str));
if (!g_signum && line && *line)
ft_env_puti(env, "?", ft_line_process(&line, env));
else if (g_signum == SIGINT)
ft_env_puts(env, "?", "130");
return (line);
}
int main(int ac, char **av, char **ep)
{
t_list *env;
char *line;
int code;
if (!ft_signals())
return (EXIT_FAILURE);
if (ac > 1 && !ft_redirect_input(av[1], NULL, 1))
return (EXIT_FAILURE);
env = ft_env(ep);
line = NULL;
while (g_signum != SIGTERM)
{
free(line);
g_signum = 0;
line = ft_readline(&env);
}
code = ft_env_geti(&env, "?");
return (rl_clear_history(), ft_lstclear(&env, &free), code);
}