-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
118 lines (92 loc) · 1.8 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
#include "headers.h"
#include "prompt.h"
#include "ec.h"
#include "cd.h"
int cnt = 0;
int cnt_semi = 0;
void CTRL_C(int SIG);
int main()
{
CHILDPID = -1;
SHELLID = 0;
int std_in_saved = dup(0);
int std_out_saved = dup(1);
dup2(std_in_saved, STDIN_FILENO);
dup2(std_out_saved, STDOUT_FILENO);
char *home_dir = get_homedir();
signal(SIGINT, CTRL_C);
SHELLID = getpid();
while (1)
{
cnt_semi = 0;
char *input;
size_t size = 0;
prompt();
getline(&input, &size, stdin);
update_hist(input, strlen(input));
if (strlen(input) < 1)
continue;
if (input[0] == '\n')
continue;
if (strlen(input) == 1 && input[0] == ' ')
continue;
for (int i = 0; i < strlen(input); i++)
{
if (input[i] == ';')
cnt_semi++;
}
if (cnt_semi != 0)
{
char **cmd = extract_semicolon(input, &cnt_semi);
for (int j = 0; cmd[j] != NULL; j++)
{
cnt = 0;
int ret = piping(cmd[j], home_dir);
if (ret == 1)
continue;
red_flag_check(cmd[j]);
char **list;
list = extract_command(cmd[j], &cnt);
if (cnt == 0)
{
continue;
}
execute_command(list, home_dir, cnt, cmd[j]);
dup2(std_in_saved, STDIN_FILENO);
dup2(std_out_saved, STDOUT_FILENO);
}
}
else
{
char input_copy[1024];
strcpy(input_copy, input);
char **list;
cnt = 0;
list = extract_command(input_copy, &cnt);
if (cnt == 1)
continue;
int ret = piping(input, home_dir);
if (ret == 1)
continue;
red_flag_check(input);
if (cnt == 1)
continue;
execute_command(list, home_dir, cnt, input);
dup2(std_in_saved, STDIN_FILENO);
dup2(std_out_saved, STDOUT_FILENO);
}
}
}
void CTRL_C(int SIG)
{
int pid = getpid();
if (pid != SHELLID)
{
return;
}
if (CHILDPID != -1)
{
kill(CHILDPID, SIGINT);
signal(SIGINT, CTRL_C);
}
}