-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell-code.c
More file actions
372 lines (335 loc) · 7.45 KB
/
shell-code.c
File metadata and controls
372 lines (335 loc) · 7.45 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include <stdio.h>
#include <stdbool.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#define MAX_INPUT_SIZE 1024
#define MAX_TOKEN_SIZE 64
#define MAX_NUM_TOKENS 64
int BackGround[64];
int Para[64];
bool doExit = false;
int curCmd = -1;
bool stop = false;
/* Splits the string by space and returns the array of tokens
*
*/
char **tokenize(char *line)
{
char **tokens = (char **)malloc(MAX_NUM_TOKENS * sizeof(char *));
char *token = (char *)malloc(MAX_TOKEN_SIZE * sizeof(char));
int i, tokenIndex = 0, tokenNo = 0;
for(i =0; i < strlen(line); i++){
char readChar = line[i];
if (readChar == ' ' || readChar == '\n' || readChar == '\t'){
token[tokenIndex] = '\0';
if (tokenIndex != 0){
tokens[tokenNo] = (char*)malloc(MAX_TOKEN_SIZE*sizeof(char));
strcpy(tokens[tokenNo++], token);
tokenIndex = 0;
}
} else {
token[tokenIndex++] = readChar;
}
}
free(token);
tokens[tokenNo] = NULL ;
return tokens;
}
void HandleBack(char []); //handles back-ground processes
void HandleCd(char**);//handle cd command
void signalHandler();//handles ctrl+c
void HandleSeries(char**);//handles series commands
int HandlePara(char**);//handles parallel commands
void Perform(int, char *[], int, int);//execute command
void HandleExit();//handles exit command to exit the shell program
int main(int argc, char* argv[]) {
doExit = false;
char line[MAX_INPUT_SIZE];
char **tokens;
//char *com = (char *)malloc(MAX_TOKEN_SIZE*sizeof(char));
int i;
bool isBack = false, isSerial = false, isPara = false;
int pid;
int totalTokens = 0;
FILE* fp;
if(argc == 2) {
fp = fopen(argv[1],"r");
if(fp < 0) {
printf("File doesn't exists.");
return -1;
}
}
for(i=0;i<64 ;i++){
BackGround[i] = -1;
Para[i] = -1;
}
signal(SIGINT, signalHandler);
while(1) {
totalTokens = 0;
/* BEGIN: TAKING INPUT */
bzero(line, sizeof(line));
if(argc == 2) { // batch mode
if(fgets(line, sizeof(line), fp) == NULL) { // file reading finished
break;
}
line[strlen(line) - 1] = '\0';
} else { // interactive mode
printf("$ ");
scanf("%[^\n]", line);
getchar();
}
/* END: TAKING INPUT */
line[strlen(line)] = '\n'; //terminate with new line
tokens = tokenize(line);
//NO COMMAND CASE
if(!tokens[0]){
goto FreeThings;
}
//find out whether series or parallel or background
isBack = false, isSerial = false, isPara = false;
for(i=0; tokens[i]!=NULL; i++){
if(!strcmp(tokens[i], "&")){
isBack = true;
break;
}
if(!strcmp(tokens[i], "&&")){
isSerial = true;
break;
}
if(!strcmp(tokens[i], "&&&")){
isPara = true;
break;
}
//totalTokens++;
}
// Backgroud process case
if(isBack){
HandleBack(line);
goto FreeThings;
}
// Serial Execution case
if(isSerial){
HandleSeries(tokens);
goto FreeThings;
}
// Parallel Execution case
if(isPara){
int p = HandlePara(tokens);
while(p--){
wait(NULL);
}
for(i = 0; i<64; i++) Para[i] = -1;
goto FreeThings;
}
// cd CASE
if(!strcmp(tokens[0], "cd")){
HandleCd(tokens);
goto FreeThings;
}
// exit case
if(!strcmp(tokens[0], "exit")){
HandleExit();
goto FreeThings;
}
//NORMAL COMMANDS CASE
pid = fork();
if(pid<0){
printf("fork failed\n");
}
else if(pid==0){
//strcpy(com, line);
if(execvp(tokens[0], tokens) < 0){
printf("ERROR: command not found or incorrect command or incorrect arguments\n");
exit(0);
}
}
else{
curCmd = pid;
waitpid(pid, NULL, 0);
}
// Freeing the allocated memory
FreeThings:
for(int i=0;tokens[i]!=NULL;i++){
free(tokens[i]);
}
free(tokens);
//Reaping finished Back Ground processes
for(i =0; i<64; i++){
if(BackGround[i]> 0){
int b = waitpid(BackGround[i], NULL, WNOHANG);
if(BackGround[i] == b){
BackGround[i] = -1;
printf("Shell: Back ground process finished\n");
}
}
}
stop = false;
//exit the shell
if(doExit) break;
}
return 0;
}
void HandleBack(char line[]){
char* Line = strtok(line, "&");
char** Tokens = tokenize(Line);
int pid = fork();
if(pid<0){
printf("ERROR: Problem while forking the child\n");
}
if(pid==0){
setpgid(0, 0);
if(execvp(Tokens[0], Tokens) < 0){
printf("ERROR: wrong commnad\n");
exit(0);
}
}
else{
for(int i=0; i<64; i++){
if(BackGround[i] == -1){
BackGround[i] = pid;
break;
}
}
}
for(int i=0;Tokens[i]!=NULL;i++){
free(Tokens[i]);
}
free(Tokens);
return;
}
void Perform(int tokensLength, char *commands[64], int k, int ins){
char *command[tokensLength + 1];
for(int j=0;j<tokensLength; j++){
command[j] = commands[j];
}
command[tokensLength] = NULL;
int pid = fork();
if(pid<0){
printf("ERROR: Problem while forking the child\n");
}
else if(pid == 0){
if(execvp(command[0], command)< 0){
printf("ERROR: wrong command\n");
exit(0);
}
}
else if(pid>0){
if(k==1) {
curCmd = pid;
waitpid(pid, NULL, 0);
}
else{
if(ins) Para[ins -1] = pid;
}
}
return;
}
void HandleSeries(char** tokens){
int tokensLength = 0;
char *commands[64];
if(stop == true) return;
for(int i=0; tokens[i]!= NULL; i++){
//printf("%s.....2\n", tokens[i]);
if(!strcmp(tokens[i], "&&")){
if(!strcmp(commands[0], "cd")){
HandleCd(commands);
continue;
}
if(!strcmp(commands[0], "exit")){
HandleExit();
return;
}
Perform(tokensLength, commands, 1, 0);
if(stop == true) return;
tokensLength = 0;
}
else{
commands[tokensLength] = tokens[i];
tokensLength++;
}
}
if(!strcmp(commands[0], "cd")) HandleCd(commands);
else if(!strcmp(commands[0], "exit")) HandleExit();
else Perform(tokensLength, commands, 1, 0);
return ;
}
int HandlePara(char** tokens){
int tokensLength = 0, totalIns = 0;
char *commands[64];
for(int i=0; tokens[i]!= NULL; i++){
//printf("%s.....2\n", tokens[i]);
if(!strcmp(tokens[i], "&&&")){
if(!strcmp(commands[0], "cd")){
HandleCd(commands);
}
if(!strcmp(commands[0], "exit")){
HandleExit();
return 0;
}
else{
totalIns++;
Perform(tokensLength, commands, 0, totalIns);
}
tokensLength = 0;
}
else{
commands[tokensLength] = tokens[i];
tokensLength++;
}
}
if(!strcmp(commands[0], "cd")) HandleCd(commands);
else if(!strcmp(commands[0], "exit")){
HandleExit();
return 0;
}
else{
totalIns++;
Perform(tokensLength, commands, 0, totalIns);
}
return totalIns;
}
void HandleCd(char** tokens){
if(tokens[2]!=NULL){
printf("Error: expected only one argument\n");
}
else{
if(chdir(tokens[1]) < 0){
printf("Shell: Incorrect Command\n");
};
}
return;
}
void signalHandler(){
for(int i=0; i<64; i++){
if(Para[i] > -1){
kill(Para[i], SIGKILL);
wait(0);
}
Para[i] = -1;
}
if(curCmd > -1){
kill(curCmd, SIGKILL);
wait(0);
}
stop = true;
printf("\n");
return;
}
void HandleExit(){
for(int i=0; i<64; i++){
if(BackGround[i] > -1){
kill(BackGround[i], SIGKILL);
wait(0);
}
if(Para[i] > -1){
kill(Para[i], SIGKILL);
wait(0);
}
BackGround[i] = -1;
}
doExit = true;
return;
}