-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
123 lines (111 loc) · 5.01 KB
/
Copy pathmain.c
File metadata and controls
123 lines (111 loc) · 5.01 KB
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
114
115
116
117
118
119
120
121
122
123
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zguellou <zguellou@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/06 14:19:38 by ctoujana #+# #+# */
/* Updated: 2025/05/25 15:06:57 by zguellou ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int g_sig = 0;
static void handle_sigint(int sig)
{
(void)sig;
if (g_sig == 1)
{
write(1, "\n", 1);
rl_on_new_line();
rl_replace_line("", 0);
rl_redisplay();
ft_exit_status(130, 1);
}
}
static void setup_signals(void)
{
signal(SIGINT, handle_sigint);
signal(SIGQUIT, SIG_IGN);
}
static void shell_animation(void)
{
printf("\033[2J\033[H");
fflush(stdout);
printf("\033[H\n");
printf("%s", BLUE);
printf(" /\\\\\\\\\\\\\\\\\\\\\\ /\\\\\\ /\\\\\\ /\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ /\\\\\\ /\\\\\\ /\\\\\\\\\\\\\\\\\\\\\\ /\\\\\\\\ /\\\\\\\\ /\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ /\\\\\\\\\\\\\\\\\\\\\\ /\\\\\\\\\\ /\\\\\\ \n");
usleep(300000);
printf(" /\\\\\\/////////\\\\\\ \\/\\\\\\ \\/\\\\\\ \\/\\\\\\/////////// \\/\\\\\\ \\/\\\\\\ \\/////\\\\\\/// \\/\\\\\\\\\\\\ /\\\\\\\\\\\\ \\////////////\\\\\\ \\/////\\\\\\/// \\/\\\\\\\\\\\\ \\/\\\\\\ \n");
usleep(300000);
printf(" \\//\\\\\\ \\/// \\/\\\\\\ \\/\\\\\\ \\/\\\\\\ \\/\\\\\\ \\/\\\\\\ \\/\\\\\\ \\/\\\\\\//\\\\\\ /\\\\\\//\\\\\\ /\\\\\\/ \\/\\\\\\ \\/\\\\\\/\\\\\\ \\/\\\\\\ \n");
usleep(300000);
printf("%s", GREEN);
printf(" \\////\\\\\\ \\/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\/\\\\\\\\\\\\\\\\\\\\\\ \\/\\\\\\ \\/\\\\\\ \\/\\\\\\ \\/\\\\\\\\///\\\\\\/\\\\\\/ \\/\\\\\\ /\\\\\\/ \\/\\\\\\ \\/\\\\\\//\\\\\\ \\/\\\\\\ \n");
usleep(300000);
printf(" \\////\\\\\\ \\/\\\\\\/////////\\\\\\ \\/\\\\\\/////// \\/\\\\\\ \\/\\\\\\ \\/\\\\\\ \\/\\\\\\ \\///\\\\\\/ \\/\\\\\\ /\\\\\\/ \\/\\\\\\ \\/\\\\\\\\//\\\\\\\\/\\\\\\ \n");
usleep(300000);
printf(" \\////\\\\\\ \\/\\\\\\ \\/\\\\\\ \\/\\\\\\ \\/\\\\\\ \\/\\\\\\ \\/\\\\\\ \\/\\\\\\ \\/// \\/\\\\\\ /\\\\\\/ \\/\\\\\\ \\/\\\\\\ \\//\\\\\\/\\\\\\ \n");
usleep(300000);
printf("%s", YELLOW);
printf(" /\\\\\\ \\//\\\\\\ \\/\\\\\\ \\/\\\\\\ \\/\\\\\\ \\/\\\\\\ \\/\\\\\\ \\/\\\\\\ \\/\\\\\\ \\/\\\\\\ /\\\\\\/ \\/\\\\\\ \\/\\\\\\ \\//\\\\\\\\\\\\ \n");
usleep(300000);
printf(" \\///\\\\\\\\\\\\\\\\\\\\\\/ \\/\\\\\\ \\/\\\\\\ \\/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ /\\\\\\\\\\\\\\\\\\\\\\ \\/\\\\\\ \\/\\\\\\ /\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ /\\\\\\\\\\\\\\\\\\\\\\ \\/\\\\\\ \\//\\\\\\\\\\ \n");
usleep(300000);
printf(" \\/////////// \\/// \\/// \\/////////////// \\/////////////// \\/////////////// \\/////////// \\/// \\/// \\/////////////// \\/////////// \\/// \\///// \n");
usleep(300000);
printf("%s", RESET);
printf("\n");
usleep(300000);
}
static void shell_loop(t_env **my_env, t_free **free_nodes)
{
char *line;
static int first_run = 1;
line = NULL;
if (first_run)
{
shell_animation();
first_run = 0;
}
while (1)
{
(setup_signals(), env_and_1byte_alloc(my_env, free_nodes));
g_sig = 1;
line = readline(GREEN "→ " PROMPT "minishell>$ " RESET);
if (!line)
{
ft_putstr_fd("exit\n", 1, 0);
if (free_nodes)
ft_lstclear(free_nodes);
close_all_fds();
break ;
}
g_sig = 0;
if (line[0])
add_history(line);
if (process_line(&line, free_nodes))
continue ;
process_execution(line, free_nodes, my_env);
ft_lstclear(free_nodes);
*free_nodes = NULL;
}
}
int main(int ac, char **av, char **env)
{
t_free *free_nodes;
t_env *my_env;
free_nodes = NULL;
(void)av;
if (!isatty(1) || !isatty(0))
return (1);
if (ac == 1)
{
my_env = env_generator(env, &free_nodes);
free_nodes->my_env = &my_env;
shell_loop(&my_env, &free_nodes);
free_env(&my_env);
}
rl_clear_history();
return (ft_exit_status(0, 0));
}