-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtoyshell.cpp
More file actions
1601 lines (1367 loc) · 46.3 KB
/
toyshell.cpp
File metadata and controls
1601 lines (1367 loc) · 46.3 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
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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "toyshell.h"
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <time.h>
#include <sys/types.h>
#include <cstdio>
#include <dirent.h>
#include <fcntl.h>
/* Constructor called on inital creation of toyshell object
* Opens two files to get name and terminator of shell
* If files do not exist create them and fill in default values
*/
ToyShell::ToyShell()
{
int aliasLimit = 10;
int jobLimit = 10;
int jobSize = 0;
int jobStored = 1;
int aliasSizeX=0;
count = 0;
history = new string[10];
historySize=0;
historyArraySize=10;
oldpwd = getenv ("PWD");
aInput=dup(0);
aOutput=dup(1);
input=dup(0);
output=dup(1);
jobs = new job[10];
//get shellname and terminator from file
fstream read;
read.open("shellName.txt");
if(read.is_open()){
while(!read.eof())
read>>name;
read.close();
}
else{
//create shellName.txt and set default name
read.open("shellName.txt", fstream::out);
read << "Toyshell";
name = "Toyshell";
read.close();
}
fstream read2;
read2.open("shellTerminator.txt");
if(read2.is_open()){
while(!read2.eof())
read2>>terminator;
read2.close();
}
else{
//create shellTerminator.txt and set default terminator
read2.open("shellTerminator.txt", fstream::out);
read2 << "->";
terminator = "->";
read2.close();
}
}
/* Deconstructor called at end of program
* Clears all memory allocated by the new and strup commands
*
*/
ToyShell::~ToyShell(){
if(workCommand->size !=0){
for (int i=0; i<workCommand->size; i++)
free(workCommand->token[i]); //frees up each space in memory
//Clean up the array of words
delete [] workCommand->token; // cleans up words allocated space
}
close(input);
close(output);
close(aInput);
close(aOutput);
}
/* IncreaseCount is called at the end on loop in main
* Keeps track of how many commands have been entered
*
*/
void ToyShell::increaseCount(){
count++;
}
/* Tokenize is called when a command is entered
* It converts the passed string into a cstring
* This cstring is then broken apart into seperate words using strtok
* The seperated command is stored as well as its size.
*/
void ToyShell::tokenize(string commandLine){
if(!commandLine.empty()){
workCommand = new command;
char delim[]=" ,;";
char *workCommandLine = new char [commandLine.length() + 1]; //c-string use$
workCommand->token= new char*[commandLine.length()];
strcpy(workCommandLine, commandLine.c_str());
int i=0; //initilize the counter
workCommand->token[i] = strdup(strtok(workCommandLine, delim));
char *test;
do
{
i++;
test=strtok(NULL, delim);
if(test==NULL)
break;
workCommand->token[i]=strdup(test);
} while(test!=NULL);
workCommand->size = i;
}
}
void ToyShell::tokenizeTemp(string commandLine){
if(!commandLine.empty()){
tempCommand = new command;
char delim[]=" ,;";
char *workCommandLine = new char [commandLine.length() + 1]; //c-string use$
tempCommand->token= new char*[commandLine.length()];
strcpy(workCommandLine, commandLine.c_str());
int i=0; //initilize the counter
tempCommand->token[i] = strdup(strtok(workCommandLine, delim));
char *test;
do
{
i++;
test=strtok(NULL, delim);
if(test==NULL)
break;
tempCommand->token[i]=strdup(test);
} while(test!=NULL);
tempCommand->size = i;
}
}
/* TokenizePath is called when a unix command needs to be executed
* It converts the passed string into a cstring
* This cstring is then broken apart into seperate words using strtok
* The seperated command is stored as well as its size.
*/
void ToyShell::tokenizePath(char* commandLine){
path = new command;
char delim[]=" :";
char *workCommandLine = new char [strlen(commandLine) + 1]; //c-string use$
path->token= new char*[strlen(commandLine)];
strcpy(workCommandLine, commandLine);
int i=0; //initilize the counter
path->token[i] = strdup(strtok(workCommandLine, delim));
char *test;
do
{
i++;
test=strtok(NULL, delim);
if(test==NULL)
break;
path->token[i]=strdup(test);
} while(test!=NULL);
path->size = i;
}
/* Alias called before the execution of a command
* Each part of the command is looped through to check if any word matchs
* the list of given aliases
* if a match is made the actual command is substituted in for the alias
* the allocated memory is then cleared and the command is sent to tokenize
* to be seperated again
*Returns true if an alias was found to continue alias loop in main
*Returns false if there is no aliases
*/
bool ToyShell::alias(){
string fullCommand = "";
string command = workCommand->token[0];
if(aliasSizeX<=0)
return false;
if(!command.compare("newname")|| !command.compare("NEWNAME"))
return false;
//loop through entire command->each word
for(int i=0; i<workCommand->size; i++)
{
//loop through entire alias list
for(int j=0; j<aliasSizeX; j++)
{
//compare current word with the alias->stored in the first column of 2d array
if(!storedA[j][0].compare(workCommand->token[i])){
//grabs any part of the command before the alias
for(int k=0; k<i; k++)
fullCommand += string(workCommand->token[k])+" ";
//adds the alias
fullCommand += storedA[j][1]+" ";
//adds any part of the command after the alias
for(int l=i+1; l<workCommand->size; l++)
fullCommand += string(workCommand->token[l])+" ";
//frees up each space in memory->clears out tokenize
for (int i=0; i<workCommand->size; i++)
free(workCommand->token[i]);
//Clean up the array of words
delete [] workCommand->token; // cleans up words allocated space
//sends the string to be seperated and stored
tokenize(fullCommand);
return true; //this may have to be outside the first for loop
}
}
}
return false;
}
/* Execute checks the given command and calls the corresonding function
*Returns 0 if all executed
*Returns any other int as an error code
*/
int ToyShell::execute( ){
//input=dup(0);
//output=dup(1);
//aInput=dup(0);
//aOutput=dup(1);
int status = 0;
//just set command to make life easier
string command = workCommand->token[0];
//Check if command is to repeat previous command
//Then fetch previous command to continue executing
if(!command.compare("!")){
if(workCommand->size == 2){
int test = getHistoryCommand(workCommand->token[1]);
//reset command since it has changed
if(test == 1)
return status;
command = workCommand->token[0];
}
else{
cout<<"Missing Parameter: history line number"<<endl;
return status;
}
}
//make all lowercase
for(int i=0; i<command.length(); i++)
command[i] = tolower(command[i]);
//if command is to stop program NOTE: compare returns 0 if equal
if(!command.compare("stop"))
return 10;
//sets shell name
else if(!command.compare("setshellname")){
if(workCommand->size>1)
setShellName(workCommand->token[1]);
else{
cout<<"Missing Parameter: new shell name"<<endl;
status=1;
}
}
//sets terminator
else if( !command.compare("setterminator")){
if(workCommand->size>1)
setShellTerminator(workCommand->token[1]);
else{
cout<<"Missing Parameter: new shell terminator"<<endl;
status =1;
}
}
//lists current history -> default array of 10
else if( !command.compare("history")){
outputHistory();
}
//new alias for command
else if( !command.compare("newname")){
newAlias();
}
//output all aliases that have been defined
else if( !command.compare("newnames")){
outputAlias();
}
// savenewnames store all aliases in file
else if( !command.compare("savenewnames")){
if(workCommand->size>1)
saveAlias(workCommand->token[1]);
else{
cout<<"Missing Parameter: file name"<<endl;
status = 1;
}
}
//readnewnames read all aliases from file
else if( !command.compare("readnewnames")){
if(workCommand->size>1)
readAlias(workCommand->token[1]);
else{
cout<<"Missing Parameter: file name"<<endl;
status =1;
}
}
//output all background process
else if( !command.compare("backjobs")){
backJobs();
}
// moves background job to foreground
else if( !command.compare("frontjob")){
if(workCommand->size>1)
frontJob(workCommand->token[1]);
else{
cout<<"Missing Parameter: job id"<<endl;
status=1;
}
}
// moves kill background job
else if(!command.compare("cull")){
if(workCommand->size>1)
status=cull(workCommand->token[1]);
else{
cout<<"Missing Parameter: job id"<<endl;
status=1;
}
}
else if (!command.compare("back")){
if(workCommand->size>1){
cout << "Too may parameters" << endl;
status=1;
}
else
backCommand();
}
else if (!command.compare("cd")){
changeDirectories();
}
//conditional excecution command
else if ( !command.compare("cond")){
if (workCommand->size >= 6) {//must be at least this big
int status = 0;
bool temp = false;
temp = condition();
status = conditionHelper(temp);
}
else{
cout << "Missing Parameters" << endl;
status=1;
}
}
//conditional excecution command
else if ( !command.compare("notcond")){
if (workCommand->size >= 5) {//must be at least this big
int status = 0;
bool temp = false;
temp = condition();
status = conditionHelper(!temp);
}
else{
cout << "Missing Parameters" << endl;
status=1;
}
}
else if ( !command.compare("output")){
//print any text after display
for(int i=1; i<workCommand->size; i++)
{
cout<<workCommand->token[i]<<" ";
}
cout<<endl;
}
else if ( !command.compare("usescript")){
if(workCommand->size<2){
cout << "Missing Parameters" << endl;
status=1;
}
else
status= executeScript();
}
//if not a shell command try and execute as UNIX Command
else{
status = unixCommand();
}
return status;
}
string ToyShell::errorMessage(int status){
switch(status){
case 1:
return "This command is not reconginzed";
case 2:
return "New alias could not be made";
case 3:
return "File could not be written to";
case 4:
return "Aliases could not be read from file";
default:
return "";
}
}
/* Setshellname opens default file and stores the new shell name
* it also sets the variable name to the new shell name
*/
void ToyShell::setShellName(string newName){
//set class variable as new name
name = newName;
//open shell name file and rewrite shell with new name
ofstream ofs;
ofs.open("shellName.txt", std::ofstream::out | std::ofstream::trunc);
ofs<<name;
ofs.close();
}
/* Setshellterminator opens default file and stores the new shell terminator
* it also sets the variable terminator to the new shell terminator
*/
void ToyShell::setShellTerminator(string newTerminator){
//set class variable as new terminator
terminator = newTerminator;
//open shell name file and rewrite shell with new name
ofstream ofs;
ofs.open("shellTerminator.txt", std::ofstream::out | std::ofstream::trunc);
ofs<<terminator;
ofs.close();
}
/* saveHistory saves each command entered
* The default array size is 10, if more then 10 commands are entered
* Then array is shifted to remove first element
*/
void ToyShell::saveHistory(){
//dynamically add onto history array
string command;
for(int i=0; i<workCommand->size; i++)
command += string(workCommand->token[i])+" ";
if(historySize>=historyArraySize){
/*If we want to add the dynamic allocation back
string* grownArray = new string[historyArraySize+10];
for (int i=0; i < historyArraySize; ++i)
grownArray[i] = history[i];
// enlarge newly allocated array:
historyArraySize+= 10;
// release old memory
delete[] history;
// reassign history pointer to point to expanded array
history = grownArray;*/
for(int i=0; i<historySize; i++){
if(i<historySize-1)
history[i] = history[i+1];
}
history[historySize-1]=command;
}
else{
history[historySize]=command;
historySize++;
}
}
/* Gethistorycommand takes the line number given
* converts it into an int and then finds the corresponding
* location in the history array
*/
int ToyShell::getHistoryCommand(string line){
// object from the class stringstream
stringstream convert(line);
int lineNum = 0;
//convert string into integer
convert >> lineNum;
lineNum--;
if(lineNum>= historySize){
cout<<"Line number entered was greater then amount of history"<<endl;
return 1;
}
if(lineNum< 0){
cout<<"Line number entered was less then 0"<<endl;
return 1;
}
//get requested command
string command = history[lineNum];
//clear out current command for replacement
if(workCommand->size !=0){
for (int i=0; i<workCommand->size; i++)
free(workCommand->token[i]); //frees up each space in memory
//Clean up the array of words
delete [] workCommand->token; // cleans up words allocated space
}
//original=command;
//call tokenize to repeat process
tokenize(command);
return 0;
}
/* outputHistory outputs all history saved
*
*/
void ToyShell::outputHistory(){
if(historySize==0)
cout<<"There is no command history";
else{
for(int i=0; i<historySize; i++)
cout<<"Line "<<i+1<<" "<<history[i]<<endl;
}
}
/* newAlias has three different processes
* Deleting an alias
* Replacing an alias
* Adding a brand new alias
*/
void ToyShell::newAlias(){
if(workCommand->size >= 2){ //more than 2 tokens
//frees up each space in memory->clears out tokenize
/* for (int i=0; i<workCommand->size; i++)
free(workCommand->token[i]);
//Clean up the array of words
delete [] workCommand->token; // cleans up words allocated space
//sends the string to be seperated and stored
tokenize(original);*/
bool changed = false; //used to see if anything changes in the for loop
bool found = false;
string temp = "";
if(workCommand->size>2){
for (int i = 2; i < workCommand->size; i++){ //create a string out of workCommand
if(workCommand->size != i+1)
temp+= string(workCommand->token[i])+" ";
else
temp += string(workCommand->token[i]);
}
}
//make all lowercase
for(int i=0; i<temp.length(); i++)
temp[i] = tolower(temp[i]);
string alias = workCommand->token[1];
//make all lowercase
for(int i=0; i<alias.length(); i++)
alias[i] = tolower(alias[i]);
if(temp == alias) //if alias is the same as the command
cout<<"Alias is same as a command, alias not created"<<endl;
else{
for(int i = 0; i < aliasSizeX; i++){ //loop for the size of the alias array
if (alias == storedA[i][0] && workCommand->size == 2){ //if any of the aliases match
if(aliasSizeX > 1){
storedA[i][0] = storedA[aliasSizeX-1][0]; //set the matching to NULL
storedA[i][1] = storedA[aliasSizeX-1][1]; //set the matching to NULL
}
else{
storedA[i][0] = ""; //set the matching to NULL
storedA[i][1] = ""; //set the matching to NULL
}
aliasSizeX--;
changed = true; //set the changed to true
return;
}
if (storedA[i][1] == temp && workCommand->size > 2){ //compare alias to stored
storedA[i][0] = alias;
return;
}
//if replacing existing alias with new command
if (storedA[i][0] == alias && workCommand->size > 2){ //compare alias to stored
storedA[i][1] = temp;
return;
}
}
if(!found){ //doesn't exist
if (aliasSizeX == 10){
cout << "no spots open to store alias" << endl;
return;
}
aliasSizeX++;
storedA[aliasSizeX-1][0] = workCommand->token[1];
storedA[aliasSizeX-1][1] = temp;
}
}
}
else { //only 1 token was specified and nothing else
cout << "You didn't specify any alias" << endl;
}
}
void ToyShell::outputAlias(){
if(aliasSizeX<=0)
cout<<"There is no declared aliases" << endl;
else{
//go through 2d array print out new name and the actual name
for(int i=0; i<aliasSizeX; i++)
cout<<storedA[i][0]<< " | "<<storedA[i][1]<<endl;
}
}
/* saveAlias opens given file and stores all aliases
* if file does not exist it is created
*/
int ToyShell::saveAlias(string fileName){
//open shell name file and rewrite shell with new name
ofstream ofs;
ofs.open(fileName.c_str(), std::ofstream::out | std::ofstream::trunc);
if(ofs.is_open()){
for(int i=0; i<aliasSizeX; i++){
ofs<<storedA[i][0]<<endl;
ofs<<storedA[i][1]<<endl;
}
ofs.close();
}
else{
cout<<"Error: Could not open file"<<endl;
return 1;
}
return 0;
}
/* readAlias opens a given file and reads in all aliases from file
* it saves existing aliases and appends new aliases to the array
*/
int ToyShell::readAlias(string fileName){
string newAlias="";
string command="";
fstream read;
read.open(fileName.c_str());
if(read.is_open()){
while(!read.eof()){
//read entire line in from file
//ASSUMPTIONS IS THAT ALIAS IS PLACED ON FIRST LINE AND THE COMMAND IS PLACED ON THE SECOND
getline(read, newAlias);
//incase file not formated correctly
if(read.eof()){
return 0;
}
//get command for alias
getline(read, command);
if (aliasSizeX == 10){
cout << "no spots open to store alias" << endl;
return 1;
}
bool found =false;
for(int i = 0; i < aliasSizeX; i++){ //loop for the size of the alias array
if (newAlias == storedA[i][0]){
found = true;
break;
} //if any of the aliases match
}
if(!found){
storedA[aliasSizeX][0] = newAlias;
storedA[aliasSizeX][1] = command;
aliasSizeX++;
}
}
read.close();
}
else{
cout<<"Error: Could not open file"<<endl;
return 1;
}
return 0;
}
int ToyShell::unixCommand(){
pid_t childPid = 0;
pid_t waitPid;
int status;
//aInput=dup(0);
//aOutput=dup(1);
//check for any input file
for(int i=0; i<workCommand->size; i++){
string temp = workCommand->token[i];
if(!temp.compare("[")){
if(i+1 < workCommand->size){
string filename = workCommand->token[i+1];
status = inputFile(filename);
dup2( input, 0);
for(int j = i; j< workCommand->size; j++)
{
if(j<workCommand->size-1)
workCommand->token[j] = workCommand->token[j+1];
else
workCommand->token[workCommand->size-1]= '\0';
}
workCommand->size--;
}
else{
cout<< "Error: missing file name"<<endl;
return 1;
}
break;
}
}
//check for any output file
for(int i=0; i<workCommand->size; i++){
string temp = workCommand->token[i];
if(!temp.compare("]")){
if(i+1 < workCommand->size){
string filename = workCommand->token[i+1];
status = outputFile(filename);
dup2( output, 1);
for(int j = i; j< workCommand->size; j++)
{
if(j<workCommand->size-2)
workCommand->token[j] = workCommand->token[j+2];
else
workCommand->token[workCommand->size-1]= '\0';
workCommand->token[workCommand->size-2]= '\0';
}
workCommand->size = workCommand->size-2;
}
else{
cout<< "Error: missing file name"<<endl;
return 1;
}
break;
}
}
//Check for any pipe commands
for(int i=0; i<workCommand->size; i++){
string temp = workCommand->token[i];
if(!temp.compare("@")){
int status = piping();
dup2( aInput, 0);
dup2( aOutput, 1);
return status;
}
}
//first get full path
char* pPath;
pPath = getenv ("PATH");
bool found = false;
bool isWait = true;
string spath="";
string command = workCommand->token[workCommand->size-1];
//if process is not supposed to wait
if(!command.compare("-")){
isWait = false;
//remove - sign
workCommand->token[workCommand->size-1]= '\0';
workCommand->size=workCommand->size-1 ;
}
if (pPath!=NULL){
//then seperate and tokenize the path by :
tokenizePath(pPath);
//loop through each name in path/
for(int i=0; i<path->size; i++){
//append command to the end
spath= path->token[i];
spath +="/";
spath +=workCommand->token[0];
//check if file is there
//and if it is executable
if((access(spath.c_str(), X_OK))==0){
found=true;
break;
}
}
if(found){
childPid = fork ();
if (childPid == -1)
{
fprintf (stderr, "Process %d failed to fork!\n", getpid ());
return 1 ;
}
//in child process
if (childPid == 0)
{
unixExecution(spath);
//return 10 to stop shell if error has occurred with execve
return 10;
}
//in parent
else
{
//if parent should wait for child to return
if(isWait){
do
{
waitPid = wait (&status);
} while (waitPid != childPid);
}
else{
//store not waited for job
storeBackJob(childPid);
}
//otherwise continue
dup2( aInput, 0);
dup2( aOutput, 1);
return 0;
}
}
else
cout<<"Error: Command entered not recongized"<<endl;
//frees up each space in memory->clears out tokenize
for (int i=0; i<path->size; i++)
free(path->token[i]);
//Clean up the array of words
delete [] path->token;
}
dup2( aInput, 0);
dup2( aOutput, 1);
}
void ToyShell::unixExecution(string spath){
//make sure last character is null
//there is extra space here since in
//tokenize workCommand allocates
//enough space for all the characters
workCommand->token[workCommand->size]= '\0';
//then use excev
execve(spath.c_str(),workCommand->token, environ);
}
void ToyShell::storeBackJob(int processId){
dup2(aOutput, fileno(stdout));
if(jobLimit-1 < jobSize){
cout<<"Error: Maximum number of jobs being executed, please wait for process to finish then try again"<<endl;
return;
}
time_t rawtime;
time (&rawtime);
string command="";
for(int i=0; i<workCommand->size; i++){
command+= workCommand->token[i];
command +=" ";
}
jobs[jobSize].jobId = jobStored;
jobs[jobSize].processId = processId;
jobs[jobSize].line = command;
jobs[jobSize].timeInfo = localtime (&rawtime);
cout<<"Job has been added to background process"<<endl;
cout<<"Job Id "<<"Process Id "<<"Command "<<"Time Created "<<endl;
cout<<jobs[jobSize].jobId<<" "<<jobs[jobSize].processId<<" "<< jobs[jobSize].line<<" "<<asctime(jobs[jobSize].timeInfo)<<" "<<endl;
jobSize++;
jobStored++;
dup2(output, fileno(stdout));
}
void ToyShell::backJobs(){
pid_t waitPid;
pid_t pid;
int status;
if(jobSize==0){
cout<<"There are no background jobs executing"<<endl;
return;
}
cout<<"Status "<<"Job Id "<<"Process Id "<<"Command "<<"Time Created "<<endl;
for(int i=0; i<jobSize; i++){
pid= jobs[i].processId;
waitPid = waitpid(pid, NULL, WNOHANG);
if(waitPid==0)
cout<<"Running ";
else
cout<<"Done ";
cout<<jobs[i].jobId<<" "<<jobs[i].processId<<" "<< jobs[i].line<<" "<<asctime(jobs[i].timeInfo)<<" "<<endl;
//mark job for deletion from list
if(waitPid!=0)
jobs[i].jobId=0;
}
//removes executing job from the array.
for(int i=0; i<jobSize; i++){
if(jobs[i].jobId==0)
{
if(i!=jobSize-1){
jobs[i].jobId=jobs[jobSize-1].jobId;
jobs[i].processId=jobs[jobSize-1].processId;
jobs[i].line=jobs[jobSize-1].line;
jobs[i].timeInfo=jobs[jobSize-1].timeInfo;
}
jobSize--;
}
}
}
void ToyShell::frontJob(string temp){
int status;
pid_t waitPid;
int j=0;
int processId=0;
int found = false;
// object from the class stringstream
stringstream convert(temp);
int jobId = 0;
//convert string into integer
convert >> jobId;
if(jobId <0 || jobId>jobStored){
cout<<"Error: invalid job id entered"<<endl;
return;
}
//find process id for corresponding job id
for(int i=0; i<jobSize; i++){
if(jobs[i].jobId==jobId)
{
processId = jobs[i].processId;
j = i;