-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbashProcessor
516 lines (427 loc) · 13.1 KB
/
bashProcessor
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
#include "process.h"
#include "parse.h"
// process.c created by Gabriel Dos Santos on 10/18/2021
struct dirStack{
char *name;
struct dirStack *last;
};
typedef struct dirStack *DirStack;
DirStack top = NULL;
#define REGULAR (0)
#define BACKGROUND (1)
#define PARENT (0)
#define CHILD (1)
int simpleProcess(const CMD *cmd);
int pipelineProcess(const CMD *cmd);
int conditionalProcess(const CMD *cmd, int type);
int backgroundProcess(const CMD *cmd, int bg);
int subcommandProcess(const CMD * cmd);
int myopen(char *pathname, int flags, mode_t mode, const CMD * cmd, int tofrom);
int dirProcess(const CMD *cmd, int location);
int myfork(pid_t *pid, const CMD *cmd);
int myprocess(const CMD *cmd);
void fileRedir(const CMD *cmd);
void argPrint(const CMD *cmd);
void saveEnviron(const CMD *cmd, char ** memLoc);
void restoreEnviron(const CMD *cmd, char ** memLoc);
void mywaitpid(pid_t pid, int * status, int flags);
int process(const CMD *cmd){
//reaping zombie processes
int zombie_status;
pid_t zombie_pid = 0;
zombie_pid = waitpid(-1, &zombie_status, WNOHANG);
for(; zombie_pid > 0 ; zombie_pid = waitpid(-1, &zombie_status, WNOHANG)){
fprintf(stderr, "Completed: %u (%d)\n", zombie_pid, STATUS(zombie_status));
}
return myprocess(cmd);
}
int myprocess(const CMD *cmd){
int child_status;
switch(cmd->type){
case PIPE :
child_status = pipelineProcess(cmd);
break;
case SEP_AND :
child_status = conditionalProcess(cmd, SEP_AND);
break;
case SEP_OR :
child_status = conditionalProcess(cmd, SEP_OR);
break;
case SEP_END :
child_status = backgroundProcess(cmd, REGULAR);
break;
case SEP_BG :
child_status = backgroundProcess(cmd, REGULAR);
break;
case SUBCMD :
child_status = subcommandProcess(cmd);
break;
default : ;
char * memoryLocals[cmd->nLocal];
saveEnviron(cmd, memoryLocals);
child_status = simpleProcess(cmd);
restoreEnviron(cmd, memoryLocals);
break;
}
//change child_status into a string
char string[16];
sprintf(string, "%d", child_status);
setenv("?", string, 1);
return child_status;
}
int backgroundProcess(const CMD* cmd, int bg){
pid_t pid;
int leftstatus, forked;
if(cmd->type == SEP_BG){
if((cmd->left->type == SEP_BG) || (cmd->left->type == SEP_END)){
//dont want to background the left child yet
leftstatus = backgroundProcess(cmd->left, BACKGROUND);
} else {
//simple case: left-child just background it
if((forked = myfork(&pid, cmd))){
return forked;
}
if(pid == 0){
signal(SIGINT, SIG_IGN);
exit(myprocess(cmd->left));
} else {
fprintf(stderr, "Backgrounded: %d\n", pid);
}
}
if(bg){
//bg signifies that right child needs to be backgrounded
if((forked = myfork(&pid, cmd))){
return forked;
}
if(pid == 0){
signal(SIGINT, SIG_IGN);
exit(myprocess(cmd->right));
} else {
fprintf(stderr, "Backgrounded: %d\n", pid);
return 0; //both left and right are BG'd so assume 0
}
} else {
//right child is a non-bg
if(cmd->right){
return myprocess(cmd->right);
} else {
return 0; //left was bg and right doesn't exist so assume 0
}
}
} else {
//Case where Type = SEP_END
leftstatus = myprocess(cmd->left);
//checking if right child needs to be backgrounded from recursive call
if(bg){
if((forked = myfork(&pid, cmd))){
return forked;
}
if(pid == 0){
signal(SIGINT, SIG_IGN);
exit(myprocess(cmd->right));
} else {
fprintf(stderr, "Backgrounded: %d\n", pid);
return leftstatus;
}
} else {
//not backgrounding case
if(cmd->right){
return myprocess(cmd->right);
} else {
return leftstatus;
}
}
}
}
int subcommandProcess(const CMD* cmd){
pid_t pid;
int forked, status = 0;
if((forked = myfork(&pid, cmd))){
return forked;
}
// Creates a subshell to run the process
if(pid == 0){
signal(SIGINT, SIG_DFL);
fileRedir(cmd); //locals dealt here as well
exit(myprocess(cmd->left));
}
mywaitpid(pid, &status , 0);
return STATUS(status);
}
int conditionalProcess(const CMD *cmd, int type){
int leftstatus = myprocess(cmd->left);
if(((type == SEP_OR) && leftstatus) || ((type == SEP_AND) && !leftstatus)){
return myprocess(cmd->right);
} else {
return leftstatus;
}
}
int pipelineProcess(const CMD *cmd){
pid_t leftpid, rightpid;
int leftstatus, rightstatus, forked;
int pipefd[2]; //Note: pipefd[1] is write end, and pipefd[0] is read end
if(pipe(pipefd) == -1){
int myerrno = errno;
perror(cmd->argv[0]);
return myerrno;
}
if((forked = myfork(&leftpid, cmd))){
close(pipefd[0]);
close(pipefd[1]);
return forked;
}
if(leftpid == 0){
signal(SIGINT, SIG_DFL);
dup2(pipefd[1], STDOUT_FILENO);
close(pipefd[0]);
close(pipefd[1]);
exit(myprocess(cmd->left));
}
if((forked = myfork(&rightpid, cmd))){
close(pipefd[0]);
close(pipefd[1]);
return forked;
}
if(rightpid == 0){
signal(SIGINT, SIG_DFL);
dup2(pipefd[0], STDIN_FILENO);
close(pipefd[0]);
close(pipefd[1]);
//right child is taking input from left child until it is closed
exit(myprocess(cmd->right));
}
close(pipefd[0]);
close(pipefd[1]);
mywaitpid(leftpid, &leftstatus, 0);
mywaitpid(rightpid, &rightstatus, 0);
if(STATUS(rightstatus) == 0){
return STATUS(leftstatus);
} else {
return STATUS(rightstatus);
}
}
int simpleProcess(const CMD *cmd){
int forked, status = 0;
pid_t pid;
if((forked = myfork(&pid, cmd))){
return forked;
}
if(pid == 0) {
fileRedir(cmd);
//Exit on a special case
if(!strcmp(cmd->argv[0], "popd") || !strcmp(cmd->argv[0], "pushd") || ! strcmp(cmd->argv[0], "cd")){
exit(dirProcess(cmd, CHILD));
}
signal(SIGINT, SIG_DFL);
//Execute the text and continues upon failure
execvp(cmd->argv[0], cmd->argv);
int myerrno = errno;
perror(cmd->argv[0]);
exit(myerrno);
}
mywaitpid(pid, &status, 0);
if(!STATUS(status)){
return dirProcess(cmd, PARENT);
}
return STATUS(status);
}
//adds the proper status for CRTL-C around the waitpid
void mywaitpid(pid_t pid, int * status, int flags){
signal(SIGINT, SIG_IGN);
waitpid(pid, status, flags);
signal(SIGINT, SIG_DFL);
}
int dirProcess(const CMD * cmd, int location){
int cdVal;
if(!strcmp(cmd->argv[0], "cd")){
if(cmd->argc == 1){
//simple cd back to home page
char *home = getenv("HOME");
if(!home){
fprintf(stderr, "cd: no home directory\n");
return 1;
}
cdVal = chdir(home);
if(cdVal){
int myerrno = errno;
perror(cmd->argv[0]);
return myerrno;
} else {
return cdVal;
}
} else if (cmd->argc == 2) {
//cd into the argument; returns 0 upon success
cdVal = chdir(cmd->argv[1]);
if(cdVal){
int myerrno = errno;
perror("cd: chdir fail");
return myerrno;
} else {
return cdVal;
}
} else {
//improper arguments
fprintf(stderr, "usage: cd OR cd <dirName>\n");
return 1;
}
} else if (!strcmp(cmd->argv[0], "popd")) {
//remove top of dirStack and cd down a level
if(cmd->argc != 1) {
fprintf(stderr, "usage: popd\n");
return 1;
}
if(top){
cdVal = chdir(top->name);
int space = 0;
if(location == CHILD){
for(DirStack i = top; i != NULL; i = i->last){
if(space)
putchar(' ');
printf("%s", i->name);
space = 1;
}
putchar('\n');
}
DirStack temp = top->last;
free(top->name);
free(top);
top = temp;
return cdVal;
} else {
if(location == CHILD)
fprintf(stderr, "popd: dir stack empty\n");
return 1;
}
} else if (!strcmp(cmd->argv[0], "pushd")){
//add a new layer onto dirStack and cd into it
if(cmd->argc != 2){
fprintf(stderr, "usage: pushd <dirName>\n");
return 1;
}
char * eltName = getcwd(NULL, 0);
if(eltName == NULL){
int myerrno = errno;
perror("");
return myerrno;
}
if((cdVal = chdir(cmd->argv[1])) == -1){
//the directory doesn't exist
int myerrno = errno;
perror("pushd: chdir fail");
free(eltName);
return myerrno;
}
char * curDirName = getcwd(NULL, 0);
if(curDirName == NULL){
int myerrno = errno;
perror("");
free(eltName);
return myerrno;
} else {
if(location == CHILD)
printf("%s", curDirName);
free(curDirName);
}
DirStack newtop = malloc(sizeof(struct dirStack));
newtop->name = eltName;
newtop->last = top;
top = newtop;
if(location == CHILD){
for(DirStack i = top; i != NULL; i = i->last){
printf(" %s", i->name);
}
putchar('\n');
}
return cdVal;
} else {
//not a built-in
return 0;
}
}
void fileRedir(const CMD * cmd){
for(int i = 0; i < cmd->nLocal; i++){
setenv(cmd->locVar[i], cmd->locVal[i], 1);
}
int fd;
switch(cmd->fromType){
case RED_IN : ;
//redirect to the file
fd = myopen(cmd->fromFile, O_RDONLY, 0, cmd, 0);
dup2(fd, STDIN_FILENO);
close(fd);
break;
case RED_IN_HERE : ;
char template[] = "/tmp/heredoc-XXXXXX";
fd = mkstemp(template); //if fd = -1 on errors check that
if(write(fd, cmd->fromFile, strlen(cmd->fromFile)) == -1)
exit(errno); //WIP if write returns -1 on error
lseek(fd, 0, 0);
dup2(fd, STDIN_FILENO);
unlink(template);
close(fd);
break;
default :
break;
}
switch(cmd->toType){
case RED_OUT : ;
//redirect to the file
fd = myopen(cmd->toFile, O_CREAT | O_TRUNC | O_WRONLY, S_IRWXU, cmd, 1);
dup2(fd, STDOUT_FILENO);
close(fd);
break;
case RED_OUT_APP : ;
//need to traverse to the end of the file
fd = myopen(cmd->toFile, O_APPEND | O_CREAT | O_WRONLY, S_IRWXU, cmd, 1);
dup2(fd, STDOUT_FILENO);
close(fd);
break;
default :
break;
}
}
int myopen(char *pathname, int flags, mode_t mode, const CMD * cmd, int tofrom){
int fd;
if((fd = open(pathname, flags, mode)) == -1){
int myerrno = errno;
if(tofrom){
perror(cmd->toFile);
} else {
perror(cmd->fromFile);
}
exit(myerrno);
} else {
return fd;
}
}
int myfork(pid_t *pid, const CMD *cmd){
if((*pid = fork()) < 0){
int myerrno = errno;
perror(cmd->argv[0]);
return myerrno;
} else {
return 0;
}
}
void saveEnviron(const CMD *cmd, char ** memLoc){
//saves any overwritten vars
for(int i = 0; i < cmd->nLocal; i ++){
memLoc[i] = getenv(cmd->locVar[i]);
}
//sets new environment vars
for(int i = 0; i < cmd->nLocal; i++){
setenv(cmd->locVar[i], cmd->locVal[i], 1);
}
}
void restoreEnviron(const CMD *cmd, char ** memLoc){
//wipes environment clean
for(int i = 0; i < cmd->nLocal; i++){
unsetenv(cmd->locVar[i]);
}
//restores any of the previous environment variables
for(int i = 0; i < cmd->nLocal; i++){
if(memLoc[i]){
setenv(cmd->locVar[i], memLoc[i], 1);
}
}
}