-
Notifications
You must be signed in to change notification settings - Fork 1
/
minishell.c
97 lines (88 loc) · 2.72 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minishell.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jaesjeon <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/25 11:00:34 by jaesjeon #+# #+# */
/* Updated: 2022/10/03 11:53:25 by jaesjeon ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell_info.h"
#include "minishell.h"
#include "ft_alloc.h"
#include "ft_environ.h"
#include "ft_string.h"
#include "ft_library.h"
#include "ft_print.h"
#include "ft_tree.h"
void handle_shlvl_env(t_dict dict[])
{
char *cur_shlvl;
add_dict(dict, ft_strdup("OLDPWD"), NULL, NULL);
cur_shlvl = my_getenv(dict, "SHLVL");
if (cur_shlvl == NULL)
put_dict(dict, ft_strdup("SHLVL"), ft_strdup("1"));
else
put_dict(dict, ft_strdup("SHLVL"), ft_itoa(ft_atoi(cur_shlvl) + 1));
}
static void _minishell_init_setting(t_dict dict[], char *envp[])
{
int fd;
char ascii_art[2039];
set_init_signal();
terminal_off_control_chars();
set_exit_status(0);
envp_to_dict(dict, envp);
erase_dict(dict, "_");
erase_dict(dict, "OLDPWD");
handle_shlvl_env(dict);
fd = open("ascii_art", O_RDONLY);
ascii_art[2037] = '\n';
ascii_art[2038] = '\0';
if (fd != ERROR && read(fd, ascii_art, 2037) > 0)
{
ft_putstr_fd(ascii_art, STDOUT_FILENO);
close(fd);
}
else
ft_putendl_fd("Welcome to SUNSH!", STDOUT_FILENO);
}
static void _minishell_routine(t_dict dict[], char *full_line, t_oflag *oflag)
{
t_lx_token *token_list;
t_tree *root_tree;
token_list = lexer(full_line, oflag);
if (!token_list)
return ;
root_tree = parser(dict, token_list);
if (!root_tree)
return ;
terminal_on_control_chars();
executor(dict, root_tree, TRUE);
terminal_off_control_chars();
list_tree_free(NULL, root_tree);
}
int main(int argc, char *argv[], char *envp[])
{
char *full_line;
t_oflag oflag;
t_dict dict[DICT_MAX];
_minishell_init_setting(dict, envp);
while (TRUE)
{
full_line = liner(&oflag);
if (full_line && *full_line == '\0')
{
my_multi_free(full_line, NULL, NULL, NULL);
continue ;
}
add_history(full_line);
_minishell_routine(dict, full_line, &oflag);
my_multi_free(full_line, NULL, NULL, NULL);
}
(void)argc;
(void)argv;
return (0);
}