-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
92 lines (84 loc) · 2.6 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hokim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/09 19:49:02 by hokim #+# #+# */
/* Updated: 2021/05/23 18:53:33 by hokim ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
t_sig g_sig;
void shell_init(t_datas *datas, t_cursor *cursor, char **envv)
{
set_terminal(&cursor->cm, &cursor->dc, &cursor->ce);
cursor->history = ft_new_deck();
cursor->cur_history = cursor->history->tail;
datas->env_list = array_to_list(envv, 0);
datas->export_list = array_to_list(envv, 1);
ft_rm_env(datas, "OLDPWD");
ft_export_env(datas, NULL, "OLDPWD");
datas->envv = envv;
datas->fd.read = 0;
datas->fd.write = 1;
datas->ori_fd.err = dup(2);
datas->status = 0;
}
void new_input_init(t_cursor *cursor, char *buf, int *i)
{
set_again_terminal();
write(1, HEADER, ft_strlen(HEADER));
get_cursor_position(&(cursor->term_offset), &(cursor->v));
buf[0] = '\0';
cursor->max = 0;
*i = 0;
cursor->c = 0;
signal(SIGINT, sig_ft);
signal(SIGQUIT, sig_ft);
}
void make_blocks(char *buf, t_datas *datas)
{
int i;
char **blocks;
blocks = ft_split(buf, ';');
i = -1;
while (blocks[++i])
{
if (g_sig.is_exit == 1)
{
printf("#\n");
break ;
}
pipe_process(blocks[i], datas);
}
free_str_array(blocks);
}
int main(int argc, char **argv, char **envv)
{
char buf[4096];
t_datas datas;
t_cursor cursor;
int i;
shell_init(&datas, &cursor, envv);
new_input_init(&cursor, buf, &i);
while (argc && argv[0] && read(0, &cursor.c, sizeof(int)) > 0)
{
if (cursor.c != '\n')
{
read_char_process(buf, &cursor, &i);
continue ;
}
back_terminal();
ft_lstadd(cursor.history, ft_new_list(buf));
cursor.cur_history = cursor.history->tail;
if (buf[0] != '\n')
write(1, "\n", 1);
if (buf[0] != '\0' && buf[0] != '\n')
if (syntax_error_check(datas.ori_fd.err, buf, &(datas.status)))
make_blocks(buf, &datas);
new_input_init(&cursor, buf, &i);
}
return (datas.status);
}