-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
103 lines (94 loc) · 2.88 KB
/
main.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fvieira <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/23 17:22:56 by fvieira #+# #+# */
/* Updated: 2023/02/27 22:13:08 by fvieira ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
t_prompt g_everything;
void handle_signals(int signo)
{
if (signo == SIGINT)
{
ft_printf(1, "\n");
rl_on_new_line();
rl_replace_line("", 0);
rl_redisplay();
g_everything.exit_stat = 130;
}
}
void signals(void)
{
struct termios termios_save;
struct termios termios_new;
if (signal(SIGQUIT, handle_signals) == SIG_ERR)
ft_printf(1, "failed to register interrupts with kernel\n");
if (signal(SIGINT, handle_signals) == SIG_ERR)
ft_printf(1, "failed to register interrupts with kernel\n");
if (tcgetattr(0, &termios_save) == -1)
{
perror("tcgetattr");
exit(1);
}
termios_new = termios_save;
termios_new.c_lflag &= ~ECHOCTL;
if (tcsetattr(0, 0, &termios_new) == -1)
{
perror("tcsetattr");
exit(1);
}
}
void delete_everything(t_prompt *everything)
{
if (everything->cmd)
freesplit(everything->cmd);
if (everything->sep)
freesplit(everything->sep);
if (everything->st_arg)
freesplit(everything->st_arg);
if (everything->order)
free(everything->order);
}
void ft_exit(t_prompt *everything)
{
int saida;
if (!everything->st_arg)
saida = (everything->exit_stat);
else
saida = (ft_atoi(everything->st_arg[0]));
change_directory(everything, "altura de dar free a isto!@#%$^");
delete_everything(everything);
freesplit(everything->new_env);
ft_printf(1, "exit\n");
exit(saida);
}
int main(int argc, char **argv, char **envp)
{
(void)argv;
(void)argc;
signals();
g_everything.new_env = set_new_env(envp);
g_everything.exit_stat = 0;
g_everything.herein = 0;
while (1)
{
if (!g_everything.herein)
g_everything.prompt = readline("prompt% ");
if (g_everything.prompt)
sanitize(&g_everything);
if (!g_everything.prompt || !ft_strcmp(g_everything.cmd[0], "exit"))
ft_exit(&g_everything);
if (g_everything.prompt[0] != '\0')
add_history(g_everything.prompt);
if (!catch_input_errors(&g_everything)
&& g_everything.prompt[0] != 0 && ft_strlen(g_everything.cmd[0]))
g_everything.fd = parser(&g_everything);
free(g_everything.prompt);
}
return (0);
}