-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
147 lines (137 loc) · 3.58 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: eusatiko <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/21 15:07:50 by auspensk #+# #+# */
/* Updated: 2024/10/16 12:12:21 by eusatiko ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
volatile sig_atomic_t g_lastsignal;
int main(int argc, char *argv[], char *envp[])
{
t_data data;
t_tok *head;
(void)argv;
if (argc > 1)
{
ft_putstr_fd ("no arguments required, only program name\n", 2);
exit(EXIT_FAILURE);
}
init_data(&data, envp);
main_loop(&data, &head);
printf("exit\n");
close (data.std_in);
exit(clean_exit(NULL, data.st_code, &data));
}
void main_loop(t_data *data, t_tok **head)
{
while (1)
{
sigaction(SIGINT, data->sa, NULL);
signal(SIGQUIT, SIG_IGN);
if (g_lastsignal == SIGINT)
printf("\n");
if (g_lastsignal == SIGQUIT)
write(2, "Quit\n", 5);
g_lastsignal = 0;
*head = read_input(data);
if (!(*head))
break ;
if ((*head)->type == END)
{
g_lastsignal = 0;
free_tokens(*head);
continue ;
}
data->cmd = parser(*head, data);
if (!data->cmd)
continue ;
signal(SIGINT, SIG_IGN);
execute_loop(data);
data->cmd = free_cmds(data->cmd);
}
}
t_tok *read_input(t_data *data)
{
char *input;
t_tok *tail;
t_tok *head;
input = readline("minishell> ");
if (g_lastsignal)
data->st_code = g_lastsignal + 128;
if (!input)
return (NULL);
if (*input)
add_history(input);
tail = lexer(input, NULL, data, 1);
if (tail && tail->type >= SQERR && tail->type <= PIPERR)
tail = accept_multiline_input(tail, &input, data);
if (input)
free(input);
if (!tail)
return (NULL);
head = tail->next;
tail->next = NULL;
return (head);
}
void handle_mltiline_sigint(int sig)
{
g_lastsignal = sig;
rl_replace_line("", 0);
rl_on_new_line();
ft_putstr_fd("\nminishell", 1);
rl_redisplay();
}
t_tok *accept_multiline_input(t_tok *tail, char **input, t_data *data)
{
char qt;
while (tail && tail->type >= SQERR && tail->type <= PIPERR)
{
g_lastsignal = 0;
signal(SIGINT, &handle_mltiline_sigint);
qt = '\0';
if (tail->type == SQERR)
qt = '\'';
else if (tail->type == DQERR)
qt = '\"';
free(*input);
*input = readline("> ");
if (!*input)
{
data->st_code = 2;
if (qt)
printf("unexpected EOF while looking for matching `%c\'\n", qt);
printf("syntax error: unexpected end of file\n");
free_tokens(tail->next);
tail = ft_calloc(1, sizeof(t_tok));
if (!tail)
return (NULL);
tail->word = ft_strdup("end of file");
if (!tail->word)
{
free(tail);
return (NULL);
}
tail->type = END;
tail->next = tail;
return (tail);
}
if (g_lastsignal)
{
if (**input)
add_history(*input);
data->st_code = g_lastsignal + 128;
free_tokens(tail->next);
tail = lexer(*input, NULL, data, 1);
if (tail && tail->type >= SQERR && tail->type <= PIPERR)
tail = accept_multiline_input(tail, input, data);
return (tail);
}
tail = lexer(*input, tail, data, 0);
}
return (tail);
}