-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.cpp
120 lines (114 loc) · 3.17 KB
/
shell.cpp
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
#include "parser.h"
#include "bundle.h"
#include "functions.h"
int main(){
Bundle bundle = Bundle();
std::vector<Bundle> bundles;
char buffer[257];
parsed_input *parsedInput = (parsed_input*)malloc(257);
int isBundleCreation = 0;
int oneStepBefore = 0;
memset(buffer, 0, sizeof(char)*257);
std::cin.getline(buffer,257);
buffer[strlen(buffer)] = '\n';
int temp = 0;
while(1){
oneStepBefore = isBundleCreation;
temp = parse(buffer, isBundleCreation, parsedInput);
//If command is not pbs
if(!temp){
if(parsedInput->command.type == PROCESS_BUNDLE_EXECUTION){
isBundleCreation=0;
int n = parsedInput->command.bundle_count;
std::vector<int> queue;
for(int i=0;i<n;i++){
int index = findBundle(bundles, parsedInput->command.bundles[i].name);
//If the executed bundle is created before
if(index != -1){
queue.push_back(index);
if(parsedInput->command.bundles[i].input){
bundles[index].set_inFile(parsedInput->command.bundles[i].input);
}
else{
bundles[index].set_inFile("");
}
if(parsedInput->command.bundles[i].output){
bundles[index].set_outFile(parsedInput->command.bundles[i].output);
}
else{
bundles[index].set_outFile("");
}
}
}
//Single bundle execution
if(n==1){
bundles[queue[0]].execute();
}
//Pipe execution
else{
init_pipe(bundles, queue);
}
queue.clear();
}
else if(parsedInput->command.type == PROCESS_BUNDLE_STOP){
isBundleCreation=0;
}
else if(parsedInput->command.type == QUIT){
isBundleCreation=0;
}
//PROCESS_BUNDLE_CREATE;
else{
isBundleCreation=1;
}
}
//If command is pbs
else{
isBundleCreation=0;
}
//Infer if there exists any command or sequence of shell commands (arguments of bundle)
//pbc command
if(!oneStepBefore && isBundleCreation){
//std::cout<<"PBC: "<<parsedInput->command.type<<std::endl;
//std::string n(parsedInput->command.bundle_name);
//std::cout<<"BName: "<<parsedInput->command.bundle_name<<std::endl;
bundle.set_name(parsedInput->command.bundle_name);
}
//pbs command
else if(oneStepBefore && !isBundleCreation){
//std::cout<<"PBS: "<<parsedInput->command.type<<std::endl;
bundles.push_back(bundle);
bundle = Bundle();
}
//taking shell commands (arguments of bundle)
else if(oneStepBefore && isBundleCreation){
/*std::string cmd = "";
for(int i=0;parsedInput->argv[i];i++){
//std::cout<<"ARGV: "<<parsedInput->argv[i]<<std::endl;
if(i){
cmd += " ";
}
cmd += parsedInput->argv[i];
}*/
bundle.add_command(parsedInput->argv);
//std::cout<<"CMD: \""<<cmd<<"\""<<std::endl;
}
//executing bundle or quit
else if(!oneStepBefore && !isBundleCreation){
//std::cout<<"E | Q: "<<parsedInput->command.type<<std::endl;
if(parsedInput->command.type == QUIT){
break;
}
}
//std::cout<<std::endl;
memset(parsedInput,0,sizeof(char)*257);
memset(buffer, 0, sizeof(char)*257);
std::cin.getline(buffer,sizeof(char)*257);
buffer[strlen(buffer)] = '\n';
}
//Print existing bundles
/*for(int i=0;i<bundles.size();i++){
bundles[i].print();
std::cout << std::endl;
}*/
return 0;
}