-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyshell.c
More file actions
566 lines (477 loc) · 15.2 KB
/
myshell.c
File metadata and controls
566 lines (477 loc) · 15.2 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
/*
References:
- (General) : http://www.rozmichelle.com/pipes-forks-dups/
- (Main Piping) : https://stackoverflow.com/questions/13000917/c-programming-fork-multiple-pipe
- (Redirection) : http://www.sarathlakshman.com/2012/09/24/implementation-overview-of-redirection-and-pipe-operators-in-shell
- http://web.eecs.utk.edu/~jplank/plank/classes/cs360/360/notes/Dup/lecture.html
- https://stackoverflow.com/questions/13801175/classic-c-using-pipes-in-execvp-function-stdin-and-stdout-redirection
- https://www.geeksforgeeks.org/implementing-lswc-command/
- https://stackoverflow.com/questions/916900/having-trouble-with-fork-pipe-dup2-and-exec-in-c/
- https://stackoverflow.com/questions/8389033/implementation-of-multiple-pipes-in-c
*/
#define TRUE 1
#define FALSE 0
#define DEBUG 0
#define MAX_SIZE 512
char prefix[] = "myshell$ ";
char breakCharacters[] = {' ', '\n', '\0', '|', '<', '>', '&', ' '};
int breakCharactersLength = 8;
_Bool inputReceiving = TRUE;
_Bool printShellPrompt = TRUE;
/*
TO-DO List
- Trailing Ampersand/Meta characters checking may screw things up.
- I don't know how to implement & / Duplicate the entire shell process.
CheckList
[X] Error Checking for Commands / General Error Checking
[X] Check for Random Debugging Messages.
[X] Free Memory
- All memory up ubntil this point is being freed.
[X] Implement & (Ampersand);
BUGS
[✓] grep "c" doesn't work.
*/
//Function Declarations
void shellPrint();
int shellProcess(char *args[]);
char* getCommand(char inputLine[], char* command);
char** getArgs(char* trimmedInput, char* args[]);
char* trimWhiteSpace(char inputLine[]);
_Bool strCompare(char* command, char compare[]);
int getArgCount(char* args[]);
int getLength(char* input);
int* numCleaner(char** args, int* numberHelper);
/*
Main Class
- Gets Input from Command Line
- Sends Input to shellProcess Function
*/
int main(int argc, char** argv){
if(argc > 1){
if(strCompare(argv[1], "-n")){
printShellPrompt = FALSE;
}
}
if(printShellPrompt){
shellPrint();
}
char inputLine[MAX_SIZE];
while(inputReceiving){
inputLine[0] = '\0';
if(fgets(inputLine, MAX_SIZE, stdin) != NULL){
char *trimmedInput = malloc(MAX_SIZE);
trimmedInput = trimWhiteSpace(inputLine);
if(trimmedInput[0] != '\n'){
char **args = malloc(MAX_SIZE);
if(DEBUG){
printf("Original Line: {%s}\n", inputLine);
printf("Trimmed Line:{%s}\n", trimmedInput);
}
args = getArgs(trimmedInput, args);
int i = 0;
int argAmount = getArgCount(args);
/*
if(*args[0] < 'A' || *args[0] > 'z'){
//printf("-bash: syntax error near unexpected token \'%c\'\n", *args[0]);
if(printShellPrompt){
shellPrint();
}
}else{
shellProcess(args);
if(printShellPrompt){
shellPrint();
}
}
*/
shellProcess(args);
if(printShellPrompt){
shellPrint();
}
i = getArgCount(args);
for(i; i >= 0; i--){
if(DEBUG){
printf("Freeing: %s\n", (args)[i]);
}
free(args[i]);
}
free(trimmedInput);
}else{
if(printShellPrompt){
shellPrint();
}
}
}else{
printf("\n");
exit(EXIT_FAILURE);
}
}
}
char** getInfo(){
if(printShellPrompt){
shellPrint();
}
char inputLine[MAX_SIZE];
inputLine[0] = '\0';
if(fgets(inputLine, MAX_SIZE, stdin) != NULL){
char *trimmedInput = malloc(MAX_SIZE);
//memset(trimmedInput, 0, MAX_SIZE);
// printf("Here\n");
trimmedInput = trimWhiteSpace(inputLine);
// printf("String: %s\n", trimmedInput);
if(trimmedInput[0] != '\n'){
// printf("Here\n");
char **args = malloc(MAX_SIZE);
args = getArgs(trimmedInput, args);
//free(args);
free(trimmedInput);
return args;
//shellProcess(args);
}
}else{
exit(EXIT_FAILURE);
}
}
int getArgCount(char* args[]){
int returnN = 0;
//printf("Arg: %s\n", args[0]);
while(args[returnN] != NULL){
//printf("Arg: %s\n", args[returnN]);
returnN++;
}
return returnN;
}
int getLength(char* input){
int j = 0;
while(*input != '\0'){
input++;
j++;
}
return j;
}
_Bool strCompare(char* command, char compareString[]){
int index = 0;
while(*command != '\0' && compareString[index] != '\0'){
if(*command != compareString[index]){
return FALSE;
}
command++; index++;
}
return TRUE;
}
int* numCleaner(char** args, int* numberHelper){
if(DEBUG){
printf("Cleaning Number\n");
}
int i = 0;
int argCount = getArgCount(args);
int j = 0;
int commandCounter = 1;
char c;
numberHelper[0] = 1;
while(args[i] != NULL && i < argCount){
numberHelper[commandCounter] +=1;
j = 0;
c = args[i][j];
if(DEBUG){
printf("C: %c\n", c);
}
while(c != '\0'){
j++;
numberHelper[commandCounter] +=1;
if(c == '|' || c == '<' || c == '>' || c == '&' || c == '\n' || c == '\0'){
commandCounter++;
numberHelper[0] +=1;
}
c = args[i][j];
}
i++;
}
return numberHelper;
}
int shellProcess(char*args[]){
/*
numberHelper:
Index 0: Number of commands to execute.
Index X: Number of arguments in that command number.
*/
_Bool processingArgs = TRUE;
_Bool foundMeta = FALSE;
int argTracker = 0;
int argCounter = getArgCount(args);
int* numberHelper = calloc(sizeof(int), argCounter);
numberHelper = numCleaner(args, numberHelper);
int t;
numberHelper[0]+=1;
for(t = 0; t < numberHelper[0] + 1; t++){
numberHelper[t] -=1;
}
char*** cleanArgs = calloc(numberHelper[0] * sizeof(char*), sizeof(char*));
int j;
int z;
int cleanIndex = 0;
char* metaCharacters = malloc(sizeof(char) * (numberHelper[0] + 1));
/*
This code block is just cleaning up the args one more time. It is formatting them perfectly to be used
in the execvp commands. We also are creating an array of meta characters so we can reference them
These are both very important steps because it allows us to ensure that our inputs are being piped
properly.
*/
for(j = 0; j < numberHelper[0]; j++){
foundMeta = FALSE;
cleanArgs[j] = calloc(numberHelper[j+1] + 1, sizeof(char));
}
int cleanX, cleanY;
cleanX = 0;
cleanY = 0;
for(z = 0; z < argCounter; z++){
if(*args[z] == '|' || *args[z] == '>' || *args[z] == '<' || *args[z] == '&'){
if(DEBUG){
printf("Found Meta\n");
}
metaCharacters[cleanX] = *args[z];
cleanY++;
cleanArgs[cleanX][cleanY] = NULL;
cleanX++; cleanY = 0;
}else{
cleanArgs[cleanX][cleanY] = calloc(getLength(args[z]) * sizeof(char), sizeof(char));
cleanArgs[cleanX][cleanY] = args[z];
cleanY++;
}
}
/*
This is where we will handle the forking/piping/child handling processes.
Here is the main logic of the shell that is required for this assignment.
*/
int numPipes = numberHelper[0] - 1;
int pipefds[2*numPipes];
metaCharacters[numPipes +1] = '\0';
int r;
for( r = 0; r < numPipes; r++ ){
if( pipe(pipefds + r*2) < 0 ){
//perror and exit
}
}
int status;
int commandCounter = 0;
pid_t pid, wpid;
int new_pipe[2];
int old_pipe[2];
int tempPipe;
_Bool foundAmpersand = FALSE;
while(commandCounter <= numPipes){
if(commandCounter < numberHelper[0]){
pipe(new_pipe);
}
pid = fork();
if(pid < 0){
printf("ERROR: Could Not Fork\n");
}else{
//Child Stage
if(pid == 0){
if(DEBUG){
printf("\n\n");
printf("-------------------------\n");
printf("Command Counter: %d\n", commandCounter);
printf("Number of Pipes: %d\n", numPipes);
printf("-------------------------\n");
printf("\n\n");
}
//If Previous Commands Exist
if(commandCounter > 0){
tempPipe = (commandCounter-1) * 2;
//printf("Pipe (Previous) [Read]: %d\n", tempPipe);
if(dup2(pipefds[(commandCounter-1) * 2], 0) < 0){
perror(" dup2");///j-2 0 j+1 1
exit(EXIT_FAILURE);
}
close(pipefds[tempPipe]);
close(pipefds[tempPipe+1]);
}
//If there is a next command, we need to do those pipes.
if(commandCounter < numPipes){
tempPipe = commandCounter * 2 + 1;
if(metaCharacters[commandCounter] != '>' && metaCharacters[commandCounter] != '<'){
if(dup2(pipefds[tempPipe], 1) < 0){
perror("dup2");
exit(EXIT_FAILURE);
}
}else if(metaCharacters[commandCounter] == '>'){
int out = open(*cleanArgs[commandCounter+1], O_RDWR|O_CREAT, 0666); // Should also be symbolic values for access rights
dup2(out,STDOUT_FILENO);
close(out);
}else if(metaCharacters[commandCounter] == '<'){
int fd0 = open(*cleanArgs[commandCounter+1], O_RDONLY);
dup2(fd0, STDIN_FILENO);
close(fd0);
if(commandCounter + 1 < numPipes){
if(metaCharacters[commandCounter + 1] == '>'){
int out = open(*cleanArgs[commandCounter+2], O_RDWR|O_CREAT, 0666); // Should also be symbolic values for access rights
dup2(out,STDOUT_FILENO);
close(out);
}else{
if(dup2(pipefds[tempPipe+2], 1) < 0){
perror("dup2");
exit(EXIT_FAILURE);
}
}
}
}
close(pipefds[tempPipe]);
close(pipefds[tempPipe-1]);
}
if(commandCounter > 0){
if(metaCharacters[commandCounter-1] != '>' && metaCharacters[commandCounter-1] != '<'){
if(execvp(cleanArgs[commandCounter][0], &cleanArgs[commandCounter][0]) < 0){
printf("ERROR: Could Not execute command\n");
exit(0);
}
}else{
close(pipefds[tempPipe]);
close(pipefds[tempPipe-1]);
exit(0);
}
}else{
if(execvp(cleanArgs[commandCounter][0], &cleanArgs[commandCounter][0]) < 0){
printf("ERROR: Could Not execute command\n");
exit(0);
}
}
}else{
// In parent Call
if(DEBUG){
printf("In parent\n");
}
if(numPipes >= 1){
if(metaCharacters[numPipes-1] == '&' && !foundAmpersand){
char** tArgs = malloc(MAX_SIZE);
tArgs = getInfo();
//printf("Command: %d\n", commandCounter);
foundAmpersand = TRUE;
shellProcess(tArgs);
free(tArgs);
//printf("Here\n");
}
}
int e;
e = commandCounter*2 + 1;
wpid = waitpid(pid, &status, WUNTRACED); //Waited for child to finish
if(e < 2 * numPipes){
close(pipefds[e]);
}
if(commandCounter > 1){
close(pipefds[e-3]);
}
commandCounter++; // Increasing counter to know how many commands we execute.
}
}
}
free(numberHelper);
free(metaCharacters);
// FIX THIS SHITTY LINE OF CODE!!!!!
//free(cleanArgs);
return 0;
}
char* trimWhiteSpace(char inputLine[]){
char* trimmedInput = calloc(MAX_SIZE, sizeof(char));
// memset(trimmedInput, 0, getLength(trimmedInput));
int trimmedCounter = 0;
int inputLineCounter = 0;
if(inputLine[0] == ' '){
inputLineCounter++;
}
for(inputLineCounter; inputLineCounter < MAX_SIZE; inputLineCounter++){ // inputLine[inputLineCounter] != '\n'
if(inputLineCounter >= 1){
// Checking to see if we either find a double sspace, or a space before the newline character. This makes sure
// That there are almost no errors later on in the program
if(inputLine[inputLineCounter] == '\n' && inputLine[inputLineCounter-1] == ' '){
trimmedInput--; *(trimmedInput) = '\0';
trimmedInput-=trimmedCounter;
trimmedInput++;
return trimmedInput;
}
//Checking if we find 2 spaces in a row.
if(inputLine[inputLineCounter] == ' ' && inputLine[inputLineCounter-1] == ' '){
}else{
//Checking if we find newline
if(inputLine[inputLineCounter] == '\n'){
break;
}else{
//If we didn't find any violations, we can just keep adding the old characters to the newly trimmed output.
*trimmedInput = inputLine[inputLineCounter];
trimmedInput++;
trimmedCounter++;
}
}
}else{
*trimmedInput = inputLine[inputLineCounter]; trimmedInput++;
trimmedCounter++;
}
}
// printf("Trimmed\n");
*trimmedInput = '\0';
int x =0;
for(x; x < trimmedCounter; x++){
trimmedInput--;
}
return trimmedInput;
}
// I think getArgs is now bugfree and doing exactly what we want.
char** getArgs(char* trimmedInput, char* args[]){
_Bool scanning = TRUE;
int argCounter = 0;
int argLength = 0;
char *tempString = malloc(MAX_SIZE);;
int tempStringLength = 0;
int count = 0;
int stop = getLength(trimmedInput);
if(trimmedInput != NULL){
while(scanning && count < stop+2){
count++;
int j = 0;
for(j; j < breakCharactersLength; j++){
if(*trimmedInput == breakCharacters[j] && (argLength > 0 || *trimmedInput != ' ')){
if(argLength > 0){
args[argCounter] = malloc(sizeof(char) * argLength + 1);
*tempString = '\0';
tempString -= argLength;
int i = 0;
for(i; i < argLength; i++){
args[argCounter][i] = tempString[i];
}
args[argCounter][argLength] = '\0';
if(*trimmedInput == '\n' || *trimmedInput == '\0'){
scanning = FALSE;
}
argCounter++;
argLength = 0;
}
if(*trimmedInput == '|' || *trimmedInput == '&' || *trimmedInput == '<' || *trimmedInput == '>'){
args[argCounter] = malloc(sizeof(char) + 1);
args[argCounter][0]= *trimmedInput;
argCounter++;
args[argCounter-1][1] = '\0';
argLength = 0;
}
trimmedInput++;
}else{
if(j == (breakCharactersLength - 1)){
if(*trimmedInput != ' ' && *trimmedInput != '\0'){
*tempString = *trimmedInput;
tempString++; argLength++;
}
}
}
}
trimmedInput++;
}
}
return args;
}
void shellPrint(){
printf("%s", prefix);
}