-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute.h
691 lines (662 loc) · 12.7 KB
/
execute.h
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
void removefromloop()
{
printf("Lo mai aaya %ld\n",getpid());
lekarle = 1;
}
void parse_it(char *argv)
{
char **tot = (char **)malloc(sizeof(char *)*512);
if(!tot)
{
perror("Malloc error");
exit(0);
}
for(ll k=0;k<200;k++)
{
tot[k] = (char *)malloc(sizeof(char)*100);
if(!tot[k])
{
perror("Malloc2 error");
exit(0);
}
}
ll start = 0;
ll end = 0;
ll ind = 0;
char ** er = (char **)malloc(sizeof(char *)*512);
if(!er)
{
perror("Malloc error");
exit(0);
}
for(ll k=0;k<200;k++)
{
er[k] = (char *)malloc(sizeof(char )*100);
if(!er[k])
{
perror("Malloc error");
exit(0);
}
}
ll di = 0;
ll op = 0;
while(ind < strlen(argv))
{
if((ind < strlen(argv)-1) && argv[ind]=='>' && argv[ind+1]=='>')
{
op = 43; // Random arbitary number
end = ind-1;
er[di] = (char *)malloc(sizeof(char )*100);
if(!er[di])
{
perror("Malloc error");
exit(0);
}
for(ll i=start;i<=end;i++)
{
er[di][i-start] = argv[i];
}
er[di][end-start+1] = '\0';
di++;
start = ind+2;
ind++;
}
else if((ind < strlen(argv)-1) && argv[ind]=='>' && argv[ind+1]!='>')
{
op = 45; // Random arbitary number
end = ind-1;
er[di] = (char *)malloc(sizeof(char )*100);
if(!er[di])
{
perror("Malloc error");
exit(0);
}
for(ll i=start;i<=end;i++)
{
er[di][i-start] = argv[i];
}
er[di][end-start+1] = '\0';
di++;
start = ind+1;
ind++;
}
else
{
// do nothing
}
ind++;
}
end = ind-1;
for(ll i=start;i<=end;i++)
{
er[di][i-start] = argv[i];
}
er[di][end-start+1] = '\0';
di++;
ll tin = 0;
ll lt_exists = 0;
for(ll f=0;f<di;f++)
{
// Parse each string by "<"
start = 0;
end = 0;
for(ll o=0;o<strlen(er[f]);o++)
{
if(er[f][o]=='<')
{
lt_exists = 1;
end = o-1;
tot[tin] = (char *)malloc(sizeof(char )*100);
if(!tot[tin])
{
perror("Malloc error");
exit(0);
}
for(ll b=start;b<=end;b++)
{
tot[tin][b-start] = er[f][b];
}
tot[tin][end-start+1] = '\0';
tin++;
start = o+1;
}
else
{
// printf("Na\n");
}
}
end = strlen(er[f])-1;
tot[tin] = (char *)malloc(sizeof(char )*100);
if(!tot[tin])
{
perror("Malloc error");
exit(0);
}
for(ll b=start;b<=end;b++)
{
tot[tin][b-start] = er[f][b];
}
tot[tin][end-start+1] = '\0';
tin++;
}
// op = 43 implies '>>'
if(!lt_exists && !op)
{
redirect(argv);
return;
}
int status;
// check read file for any spaces
ind = 0;
start = 0;
while(tot[1][ind]==' ')
{
ind++;
}
start = ind;
while(ind < strlen(tot[1]) && tot[1][ind]!=' ')
{
ind++;
}
char temp[3000];
for(ll k=start;k<ind;k++)
{
temp[k-start] = tot[1][k];
}
temp[ind-start] = '\0';
ll chk = 0;
for(ll p=ind;p<strlen(tot[1]);p++)
{
if(tot[1][p]!=' ')
{
chk = 1;
}
}
if(chk == 1)
{
printf("Please input the correct name of file\n");
return ;
}
for(ll h=0;h<strlen(temp);h++)
{
tot[1][h] = temp[h];
}
tot[1][strlen(temp)] = '\0';
// check write file for any spaces
ind = 0;
while(tot[2][ind]==' ')
{
ind++;
}
while(ind < strlen(tot[2]) && tot[2][ind] != ' ')
{
ind++;
}
char temp2[3000];
for(ll k=start;k<ind;k++)
{
temp2[k-start] = tot[2][k];
}
temp2[ind-start] = '\0';
chk = 0;
for(ll p=ind;p<strlen(tot[2]);p++)
{
if(tot[2][p]!=' ')
{
chk = 1;
}
}
if(chk == 1)
{
printf("Please provide correct name of file\n");
return ;
}
for(ll h=0;h<strlen(temp2);h++)
{
tot[2][h] = temp2[h];
}
tot[2][strlen(temp2)] = '\0';
pid_t pid = fork();
if(pid<0){perror("Forking error\n");}
if(pid==0)
{
if(lt_exists && op)
{
// Do both input and output
if(op==43)
{
int fd2 = open(tot[1],O_RDONLY); // Use this file for reading only
if(fd2<0)
{
perror("Can't open\n");
exit(0);
}
int fd3 = open(tot[2],O_WRONLY|O_APPEND|O_CREAT,0644); // Write to here
if(fd3<0)
{
perror("Can't open\n");
exit(0);
}
// Now redirect stdin of first command such that it reads from fd2 and appends to fd3
dup2(fd2,0); // Make it read from fd2
dup2(fd3,1); // Make it write to fd3
redirect(tot[0]);
close(fd2);
close(fd3);
}
else
{
int fd2 = open(tot[1],O_RDONLY); // Use this file for reading only
if(fd2<0)
{
perror("Can't open\n");
exit(0);
}
int fd3 = open(tot[2],O_WRONLY|O_TRUNC |O_CREAT,0644); // Write to here
if(fd3<0)
{
perror("Can't open\n");
exit(0);
}
// Now redirect stdin of first command such that it reads from fd2 and appends to fd3
dup2(fd2,0); // Make it read from fd2
close(fd2);
dup2(fd3,1); // Make it write to fd3
close(fd3);
redirect(tot[0]);
}
}
else if(lt_exists && !op)
{
// Do only input
int fd2 = open(tot[1],O_RDONLY); // Use this file for reading only
if(fd2<0)
{
perror("Cant open\n");
exit(0);
}
// Now redirect stdin of first command such that it reads from fd2 and appends to fd3
dup2(fd2,0);
close(fd2);
redirect(tot[0]);
}
else if(!lt_exists && op)
{
// Do only output
if(op==43)
{
int fd3 = open(tot[1],O_WRONLY|O_APPEND|O_CREAT,0644); // Write to here
if(fd3<0)
{
perror("Can't open\n");
exit(0);
}
dup2(fd3,1);
close(fd3);
redirect(tot[0]);
}
else
{
int fd3 = open(tot[1],O_WRONLY|O_TRUNC|O_CREAT,0644); // Write to here
if(fd3<0)
{
perror("Can't open\n");
exit(0);
}
dup2(fd3,1);
close(fd3);
redirect(tot[0]);
}
}
exit(0);
}
else
{
while (!(wait(&status) == pid)) ;
}
}
void redirect(char *argv)
{
if (!argv)
{
return;
}
char **tokens = (char **)malloc(sizeof(char *) * MAX_TOKENS);
if(!tokens)
{
perror("Malloc error");
exit(0);
}
for (ll o = 0; o < MAX_TOKENS; o++)
{
tokens[o] = NULL;
}
char *line = (char *)malloc(sizeof(char) * (MAX_TOKENS));
if(!line)
{
perror("Malloc error");
exit(0);
}
line = strtok(argv, " ");
ll i = 0;
while (line != NULL)
{
tokens[i++] = line;
line = strtok(NULL, " ");
}
ll n = 0;
signal(SIGINT,ctrlccross);
signal(SIGTSTP,ctrlzcross);
if (!strcmp(tokens[n], "vcsh")) // invoking shell
{
tokens[n] = (char *)malloc(sizeof(char) * 10);
if(!tokens[n])
{
perror("Malloc error");
exit(0);
}
strcpy(tokens[n], "./a.out");
}
if (!strcmp(tokens[n], "cd")) // execute cd
{
cd_vcsh(tokens);
}
else if (!strcmp(tokens[n], "quit")) // execute exit
{
quit();
}
else if (!strcmp(tokens[n], "pwd"))
{
pwd_vcsh(); // execute pwd
}
else if (!strcmp(tokens[n], "echo"))
{
echo_vcsh(i - 1, tokens); // execute echo
}
else if (!strcmp(tokens[n], "pinfo"))
{
pinfo_vcsh(tokens,parentid); // execute cd
}
else if (!strcmp(tokens[n], "pwd"))
{
pwd_vcsh(); // execute pwd
}
else if(!strcmp(tokens[n],"setenv"))
{
set_env(tokens);
}
else if(!strcmp(tokens[n],"unsetenv"))
{
unset(tokens);
}
else if(!(strcmp(tokens[n],"fg")))
{
fg(tokens);
}
else if(!(strcmp(tokens[n],"bg")))
{
bg(tokens);
}
else if(!(strcmp(tokens[n],"kjob")))
{
if(tokens[3])
{
printf("Please provide the correct number of arguments\n");
}
else if(!tokens[1]||!tokens[2])
{
printf("Please provide all arguments\n");
return;
}
ll jno = 0;
ll p = 1;
for(ll j=0;j<strlen(tokens[1]);j++)
{
jno += p*(tokens[1][strlen(tokens[1])-1-j]-'0');
p *= 10;
}
printf("Got jon as %lld\n",jno);
if(jno >= bgind){
printf("Please provide correct job number\n");
return ;
}
ll sig = 0;
p = 1;
for(ll j=0;j<strlen(tokens[2]);j++)
{
sig += p*(tokens[2][strlen(tokens[2])-1-j]-'0');
p *= 10;
}
printf("Got sig as %lld\n",sig);
kill_job(jno,sig);
}
else if(!(strcmp(tokens[n],"overkill")))
{
ll j = bgind;
ll prev = bgind;
while(j>0)
{
kill_job(0,9);
while(prev==bgind);
prev = bgind;
j--;
}
}
else
{
ll askedinbg = 0;
ll findind = 0;
for (ll n = 0; n < i && !askedinbg; n++)
{
if (tokens[n][0] == '&' && strlen(tokens[n]) == 1)
{
askedinbg = 1;
findind = n;
}
}
ll gh = 0;
ll find;
for (ll o = 0; !gh && o < strlen(argv); o++)
{
if (argv[o] == '&')
{
gh = 1;
find = o;
}
}
if (gh)
{
argv[find] = '\0';
}
if (askedinbg)
{
for (ll j = findind; j < i; j++)
{
tokens[j] = NULL;
}
i = findind;
}
if(!strcmp(tokens[n],"cronjob"))
{
iscronjob = 1;
askedinbg = 1; // execute in background
}
if (!askedinbg)
{
// asked to execute in foreground
signal(SIGCHLD, signal_handler);
int pid = fork();
if (pid == 0)
{
signal(SIGINT,SIG_DFL);
// printf("Mai hu chld meri pid hai %ld\n",getpid());
if (!strcmp(tokens[n], "ls"))
{
ls_vcsh(i - 1, tokens); // execute ls
}
else if(!strcmp(tokens[n],"jobs"))
{
showjobs();
}
else if (!strcmp(tokens[n], "history"))
{
char F[200]; // execute history
ll ind = 0;
for (ll q = 0; q < i; q++)
{
for (ll p = 0; p < strlen(tokens[q]); p++)
{
F[ind++] = tokens[q][p];
}
F[ind++] = ' ';
}
F[ind] = '\0';
// printf("%s\n",F);ls
history_vcsh(i, F, 1);
}
else
{
if (execvp(tokens[0], tokens) == -1)
{
perror("Erroor executing or wrong command\n");
}
}
exit(1);
}
else if (pid < 0)
{
perror("Error forking");
}
else
{
parentid = pid;
// printf("Parent ins %ld and child is %ld\n",parentid,pid);
int status;
do
{
waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status) && !nikal);
if(nikal)
{
printf("Caught signal %ld\n",pid);
pid_t pid = getpid();
if( (pid != shellid ) )
{
return;
}
else if(parentid>0)
{
pid = parentid;
procaarray[bgind] = parentid;
gidarray[bgind] = getpgid(parentid);
strcpy(characterarray[pid],curr_command);
printf("%lld+\tStopped\t%s\n",bgind,curr_command);
bgind++;
updatejobs();
}
}
//wait(NULL);
}
}
else
{
// execute in background
signal(SIGCHLD, signal_handler);
printf("%s\n", argv);
for (ll i = 0; i < strlen(argv); i++)
{
characterarray[bgind][i] = argv[i];
}
int pid = fork();
suspendedjobnumber++;
if (pid == 0)
{
signal(SIGINT,SIG_DFL);
//setpgid(0, 0);
signal(SIGTSTP,ctrlzcross);
// printf("Mai hun child meri pid hai %ld\n",getpid());
if (!strcmp(tokens[n], "ls"))
{
ls_vcsh(i - 1, tokens); // execute ls
}
else if (!strcmp(tokens[n], "pwd"))
{
pwd_vcsh(); // execute pwd
}
else if (!strcmp(tokens[n], "echo"))
{
echo_vcsh(i - 1, tokens); // execute echo
}
else if (!strcmp(tokens[n], "pinfo"))
{
pinfo_vcsh(tokens,parentid); // execute cd
}
else if (!strcmp(tokens[n], "pwd"))
{
pwd_vcsh(); // execute pwd
}
else if(!strcmp(tokens[n],"jobs"))
{
showjobs();
}
else if (!strcmp(tokens[n], "history"))
{
char F[200]; // execute history
ll ind = 0;
for (ll q = 0; q < i; q++)
{
for (ll p = 0; p < strlen(tokens[q]); p++)
{
F[ind++] = tokens[q][p];
}
F[ind++] = ' ';
}
F[ind] = '\0';
history_vcsh(i, F, 1);
}
else if(!strcmp(tokens[n],"cronjob"))
{
vcsh_cronjob(tokens);
}
else
{
signal(SIGINT,ctrlccross);
if (execvp(tokens[0], tokens) == -1)
{
perror("Erroor executing or wrong command\n");
}
}
exit(0);
}
else if (pid < 0)
{
perror("Error forking");
}
else
{
if(iscronjob)
{
pid_cronjob = pid;
}
parentid = pid;
procaarray[bgind] = pid;
gidarray[bgind] = getpgid(pid);
bgind++;
strcpy(characterarray[pid], argv);
int status;
kill(pid,SIGTTIN);
if(!strcmp(tokens[0],"sleep") )
{
kill(pid,SIGCONT);
}
printf("Done %lld\n",bgind-1);
}
}
}
if(iscronjob)
{
kill(pid_cronjob,SIGCONT);
}
free(tokens); // Free up the memory
free(line);
}