-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminishell.c
83 lines (76 loc) · 2.09 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minishell.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mrafik <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/26 22:49:02 by wboutzou #+# #+# */
/* Updated: 2022/10/21 01:20:11 by mrafik ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void free_parse(t_parsing *parse)
{
free(parse->str);
free(parse->lexer);
freetoken(&parse->head);
freeall(&(parse->cmd));
free(parse->error);
free(parse);
}
int parsing_error(t_parsing *parse)
{
if ((parse->herdoc == 0 && parse->res == -2) || \
(parse->res == -1) || (parse->res == -3))
{
if ((parse->herdoc == 0 && parse->res == -2))
printf("%s\n", parse->error);
else if (parse->res == -1)
printf("syntax error near unexpected token `|'\n");
return (0);
}
return (1);
}
void sig_handler(int signum)
{
if (signum == SIGINT)
{
write(2, "\n", 1);
rl_replace_line("", 0);
rl_on_new_line();
rl_redisplay();
g_code = 1;
}
}
int main(int argc, char **argv, char **envp)
{
t_parsing *parse;
t_ex execu;
(void ) argc;
(void ) argv;
signal(SIGINT, sig_handler);
signal(SIGQUIT, SIG_IGN);
g_code = 0;
execu.env = ft_dup (envp);
execu.ex_save = ft_dup(envp);
while (1)
{
parse = init_parse(execu.env);
parse->str = readline("minishell> ");
if (parse->str)
{
parsing(parse);
add_history(parse->str);
if (parsing_error(parse))
ft_execution(parse->cmd, &execu);
free_parse(parse);
}
else
{
write(2, "exit", 5);
exit(g_code);
}
}
return (0);
}