-
Notifications
You must be signed in to change notification settings - Fork 0
/
minishell.c
111 lines (101 loc) · 2.69 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minishell.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: shongou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/04 23:02:59 by shongou #+# #+# */
/* Updated: 2022/11/04 23:03:02 by shongou ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
t_sh_var g_sh_var = {};
static void signal_handler(int sig)
{
if (sig == SIGINT)
{
g_sh_var.signal = sig;
}
}
void init(char **line, t_token **tok, t_node **node)
{
char *pwd;
errno = 0;
g_sh_var.signal = 0;
g_sh_var.environ = create_env();
pwd = getcwd(NULL, 0);
if (pwd == NULL)
{
ft_putstr_fd("init: error retrieving current directory: ", \
STDERR_FILENO);
ft_putstr_fd("getcwd: cannot access parent directories: ", \
STDERR_FILENO);
ft_putendl_fd("No such file or directory", \
STDERR_FILENO);
}
update_or_add_value(&g_sh_var.environ, "PWD", pwd);
free(pwd);
update_or_add_value(&g_sh_var.environ, "OLDPWD", NULL);
*line = NULL;
*tok = NULL;
*node = NULL;
}
char *readline_wrapper(int *x)
{
char *line;
*x = get_print_start();
rl_event_hook = check_state;
line = readline("minishell> ");
if (g_sh_var.signal != 0)
{
g_sh_var.exit_status = 1;
// ft_putchar_fd('\n', 2);
// rl_on_new_line();
// rl_replace_line("", 0);
// rl_redisplay();
}
if (line == NULL)
{
display_exit(*x);
exit(g_sh_var.exit_status);
}
if (ft_strlen(line) != 0)
add_history(line);
return (line);
}
void loop_init(char **line, t_token **tok, t_node **node)
{
errno = 0;
g_sh_var.signal = 0;
set_signal_handler(SIGINT, signal_handler);
set_signal_handler(SIGQUIT, SIG_IGN);
all_free(*line, *tok, *node);
*line = NULL;
*tok = NULL;
*node = NULL;
}
void minishell(void)
{
char *line;
t_token *tok;
t_node *node;
int x;
init(&line, &tok, &node);
while (1)
{
loop_init(&line, &tok, &node);
line = readline_wrapper(&x);
set_signal_handler(SIGINT, SIG_IGN);
if (ft_strlen(line) == 0)
continue ;
tok = lexer(line);
if (tok == NULL)
continue ;
node = parse(&tok);
if (g_sh_var.signal != 0 || node == NULL)
continue ;
node = expansion(node);
exec(node, 0);
}
}