-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
181 lines (173 loc) · 5.39 KB
/
main.c
File metadata and controls
181 lines (173 loc) · 5.39 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "headers.h"
int fg_present;
int prev_prom = 0;
int ps_no = 0;
char prev_prom_msg[255];
char bg_prom_msg[255];
void verify(char *input)
{
save_to_pastevents(input);
char *Total_entry_parts[100];
char *Total_commands[100];
char *piping_cmd[100];
int counter = 0;
int IO_flag;
Total_entry_parts[counter] = strtok(input, ";");
while (Total_entry_parts[counter] != NULL)
{
counter++;
Total_entry_parts[counter] = strtok(NULL, ";");
}
int count = 0;
int bg[100];
for (int i = 0; i < counter; i++)
{
Total_commands[count] = strtok(Total_entry_parts[i], "&");
while (Total_commands[count] != NULL)
{
bg[count] = 1;
count++;
Total_commands[count] = strtok(NULL, "&");
}
bg[count - 1] = 0;
}
for (int i = 0; i < count; i++)
{
if (!strcmp(Total_commands[i], "\n") || !strcmp(Total_commands[i], ""))
{
continue;
}
int pipe_count = 0;
piping_cmd[pipe_count] = strtok(Total_commands[i], "|");
while (piping_cmd[pipe_count] != NULL)
{
lstrip(piping_cmd[pipe_count]);
rstrip(piping_cmd[pipe_count]);
if (!strcmp(piping_cmd[pipe_count], ""))
{
printf("Invalid Use of pipe\n");
return;
}
pipe_count++;
piping_cmd[pipe_count] = strtok(NULL, "|");
}
for (int j = 0; j < pipe_count; j++)
{
int active_pipe = 0;
char *command_test[100] = {'\0'};
char *command[100] = {'\0'};
int cmd_count = 0;
int cmd_test_Count = 0;
char delimeters_test[] = "'\'''\"''\n'";
char *dummy;
command_test[cmd_test_Count] = strtok(piping_cmd[j], delimeters_test);
while (command_test[cmd_test_Count] != NULL)
{
lstrip(command_test[cmd_test_Count]);
rstrip(command_test[cmd_test_Count]);
cmd_test_Count++;
command_test[cmd_test_Count] = strtok(NULL, delimeters_test);
}
char delimeters[] = "'\t'' '";
for (int k = 0; k < cmd_test_Count; k++)
{
lstrip(command_test[k]);
rstrip(command_test[k]);
if (k == 1 || (strstr(command_test[k], "seek") > 0 && strstr(command_test[k], "warp") > 0 && strstr(command_test[k], "/") > 0))
{
command[cmd_count] = command_test[k];
cmd_count++;
continue;
}
IO_flag = 0;
command[cmd_count] = strtok(command_test[k], delimeters);
while (command[cmd_count] != NULL)
{
lstrip(command[cmd_count]);
rstrip(command[cmd_count]);
if (!(strcmp(command[cmd_count], "<")) || (!strcmp(command[cmd_count], ">")) || (!strcmp(command[cmd_count], ">>")))
{
IO_flag = 1;
}
cmd_count++;
command[cmd_count] = strtok(NULL, delimeters);
}
}
int pipe_fd[2];
int pipe_fd2[2];
if (pipe_count > 1)
{
if (j % 2 != 0)
{
pipe(pipe_fd);
}
else
{
pipe(pipe_fd2);
}
}
if (command[0])
{
if (!strcmp(command[0], "warp"))
{
warp(command, bg[i], cmd_count, pipe_fd, pipe_fd2, active_pipe);
}
else if (!strcmp(command[0], "fg"))
{
fg(command, bg[i], cmd_count, pipe_fd, pipe_fd2, &ps_no, active_pipe);
}
else if (!strcmp(command[0], "activities"))
{
activities();
}
else if (!strcmp(command[0], "seek"))
{
seek_flags(command, bg[i], cmd_count, pipe_fd, pipe_fd2, active_pipe);
}
else
{
execvp_func(command, bg[i], cmd_count, pipe_count, &prev_prom, prev_prom_msg, bg_prom_msg, &ps_no, pipe_fd, pipe_fd2, j, IO_flag);
}
}
}
}
}
void take_input()
{
fg_present = 0;
prompt(&prev_prom, prev_prom_msg);
char input[4096] = {'\0'};
if (fgets(input, 4096, stdin) == NULL)
{
printf("\nCtrl+D detected. Exiting the shell.\n");
exit(0);
}
verify(input);
}
int main()
{
signal(SIGINT, ctrlc_handle);
signal(SIGTSTP, ctrlz_handle);
while (1)
{
fg_present = 0;
int normality;
pid_t completed_pid = waitpid(-1, &normality, WNOHANG);
while (completed_pid > 0)
{
char norm[20];
if (WIFEXITED(normality))
{
strcpy(norm, "normally");
}
else
{
strcpy(norm, "abnormally");
}
ps_no--;
printf("Process with pid %d exited %s\n", completed_pid, norm);
completed_pid = waitpid(-1, &normality, WNOHANG);
}
take_input();
}
}