-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
364 lines (341 loc) · 10.7 KB
/
Copy pathparser.cpp
File metadata and controls
364 lines (341 loc) · 10.7 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
#include "parser.h"
#include "builtins.h"
#include <unistd.h>
#include <sys/wait.h>
#include <iostream>
#include <fstream>
#include <queue>
#include <fcntl.h>
std::set<std::string> extcmds = {
"ls",
"grep",
"pwd",
"echo",
"sort",
"cat",
"mv",
//"tree",
"clear",
"mkdir",
"rmdir",
"touch",
"cp",
"rm",
"date",
"whoami",
"man",
"rev"
};
std::set<std::string> builtincmds = {
"cd",
"quit"
};
bool* alive;
Node::Node(){}
Node::Node(Node* child1, Node* child2){
this->child1 = child1;
this->child2 = child2;
}
NodeType Node::getNodeType(){return NodeType::BASE;}
Node* Node::getChild1(){return child1;}
Node* Node::getChild2(){return child2;}
void Node::setChild1(Node* child1){this->child1 = child1;}
void Node::setChild2(Node* child2){this->child2 = child2;}
CommandNode::CommandNode(
std::string cmd,
std::vector<std::string> args
){
this->cmd = cmd;
this->args = args;
}
CommandNode::CommandNode(
Node* child1,
Node* child2,
std::string cmd,
std::vector<std::string> args
):Node(child1, child2){
this->cmd = cmd;
this->args = args;
}
std::string CommandNode::getCommand() {return cmd;}
std::vector<std::string> CommandNode::getArgs() {return args;}
NodeType CommandNode::getNodeType(){return NodeType::COMMAND;}
OperatorNode::OperatorNode(
std::string opr
){
this->opr = opr;
}
OperatorNode::OperatorNode(
Node* child1,
Node* child2,
std::string opr
):Node(child1, child2){
this->opr = opr;
}
std::string OperatorNode::getOperator() {return opr;}
NodeType OperatorNode::getNodeType(){
return NodeType::OPERATOR;
}
GenericNode::GenericNode(
std::string str
){
this->str = str;
}
GenericNode::GenericNode(
Node* child1,
Node* child2,
std::string str
):Node(child1, child2){
this->str = str;
}
std::string GenericNode::getString() {return str;}
NodeType GenericNode::getNodeType(){
return NodeType::GENERIC;
}
void GenericCommand::execute(){
std::cout << "This should never be reached" << std::endl;
}
GenericCommand::~GenericCommand(){
}
SeparateCommand::SeparateCommand(GenericCommand* cmd1, GenericCommand* cmd2){
this->cmd1 = cmd1;
this->cmd2 = cmd2;
}
SeparateCommand::~SeparateCommand(){
delete cmd1;
delete cmd2;
}
//already in a child process, just fork and run a new one
void SeparateCommand::execute(){
int c_pid = fork();
//child process
if(c_pid == 0){
cmd1->execute();
}
//parent process
else{
waitpid(c_pid, NULL, 0);
cmd2->execute();
}
}
PipeCommand::PipeCommand(GenericCommand* cmd1, GenericCommand* cmd2){
this->cmd1 = cmd1;
this->cmd2 = cmd2;
}
PipeCommand::~PipeCommand(){
delete cmd1;
delete cmd2;
}
void PipeCommand::execute(){
int pipefd[2]; //file descriptors of the pipe
//create a pipe between cmd1 and cmd2
pipe(pipefd);
//fork, then execute cmd1 and cmd2
int c_pid = fork();
//child process
//writes to the pipe
if(c_pid == 0){
dup2(pipefd[1], 1);
cmd1->execute();
}
//parent process
//reads from the pipe
else{
waitpid(c_pid, NULL, 0);
close(pipefd[1]);
dup2(pipefd[0], 0);
cmd2->execute();
}
}
ReoutCommand::ReoutCommand(GenericCommand* cmd, std::string output){
this->cmd = cmd;
this->output = output;
}
ReoutCommand::~ReoutCommand(){
delete cmd;
}
void ReoutCommand::execute(){
//assume in child process
dup2(open(&(output[0]), O_WRONLY), 1);
cmd->execute();
}
ReinCommand::ReinCommand(GenericCommand* cmd, std::string input){
this->cmd = cmd;
this->input = input;
}
ReinCommand::~ReinCommand(){
delete cmd;
}
void ReinCommand::execute(){
dup2(open(&(input[0]), O_RDONLY), 0);
cmd->execute();
}
ExecCommand::ExecCommand(CommandNode* cn){
this->cn = cn;
}
ExecCommand::~ExecCommand(){
}
void ExecCommand::execute(){
const auto& args = cn->getArgs();
std::vector<std::string> copiedArgs(args.begin(), args.end());
char* argsArray[cn->getArgs().size() + 1];
for(int i = 0; i < cn->getArgs().size(); i++) argsArray[i] = const_cast<char*>(copiedArgs[i].c_str());
argsArray[cn->getArgs().size()] = nullptr;
//builtin cmd
if(extcmds.count(cn->getArgs()[0]) == 0){
if(cn->getArgs()[0] == "cd"){
cd(cn->getArgs()[1]);
}
else if(cn->getArgs()[0] == "quit"){
quit(alive);
}
}
else{
execvp(argsArray[0], argsArray);
}
}
GenericCommand* parseExec(std::vector<Node*>::iterator start, std::vector<Node*>::iterator end, std::vector<Node*> &nodes){
//there should only be one command here whenever this is called
if(start == end && (*start)->getNodeType() == NodeType::COMMAND){
ExecCommand* ec = new ExecCommand(static_cast<CommandNode*>(*start));
return ec; //kind of scuffed, what if nodes gets deleted? maybe need to malloc
}
return nullptr;
}
GenericCommand* parseRedir(std::vector<Node*>::iterator start, std::vector<Node*>::iterator end, std::vector<Node*> &nodes){
std::vector<Node*>::iterator nodeIterator;
//parse to look for reout symbol
//GO IN REVERSE DIRECTION
for(nodeIterator = end; nodeIterator >= start; nodeIterator--){
switch((*nodeIterator)->getNodeType()){
case NodeType::OPERATOR:
{
if(static_cast<OperatorNode*>(*nodeIterator)->getOperator() == ">"){
ReoutCommand* rc = new ReoutCommand(
parseRedir(start, nodeIterator - 1, nodes),
static_cast<GenericNode*>(*(nodeIterator+1))->getString() //assume correct grammar and that this is a string
);
return rc;
}
else if(static_cast<OperatorNode*>(*nodeIterator)->getOperator() == "<"){
ReinCommand* rc = new ReinCommand(
parseRedir(start, nodeIterator - 1, nodes),
static_cast<GenericNode*>(*(nodeIterator+1))->getString()
);
return rc;
}
}
default:
break;
}
}
return parseExec(start, end, nodes);
}
/*
End is inclusive
*/
GenericCommand* parsePipe(std::vector<Node*>::iterator start, std::vector<Node*>::iterator end, std::vector<Node*> &nodes){
std::vector<Node*>::iterator nodeIterator;
//parse to look for pipe symbol
for(nodeIterator = start; nodeIterator <= end; nodeIterator++){
switch((*nodeIterator)->getNodeType()){
case NodeType::OPERATOR:
{
if((static_cast<OperatorNode*>(*nodeIterator))->getOperator() == "|"){
GenericCommand* pc = new PipeCommand(
parseRedir(start, nodeIterator - 1, nodes),
parsePipe(nodeIterator + 1, end, nodes)
);
return pc;
}
}
default:
break;
}
}
return parseRedir(start, end, nodes);
}
//search for semi-colons
GenericCommand* parseSeparate(std::vector<Node*>::iterator start, std::vector<Node*>::iterator end, std::vector<Node*> &nodes){
std::vector<Node*>::iterator nodeIterator;
//parse to look for pipe symbol for a node
for(nodeIterator = start; nodeIterator <= end; nodeIterator++){
switch((*nodeIterator)->getNodeType()){
case NodeType::OPERATOR:
{
if((static_cast<OperatorNode*>(*nodeIterator))->getOperator() == ";"){
GenericCommand* sc = new SeparateCommand(
parsePipe(start, nodeIterator - 1, nodes),
parseSeparate(nodeIterator + 1, end, nodes)
);
return sc;
}
}
default:
break;
}
}
return parsePipe(start, end, nodes);
}
void parse(std::vector<Token> tokens, bool* aliveShell){
alive = aliveShell;
//first check for symbols, then separate commands and arguments into nodes
//Nodes can be either a command or a file or an operator?
//need to add inferencing for generic/command as they may be confused
ParseState state = ParseState::START;
std::string cmd = "";
std::vector<std::string> args;
std::vector<Node*> nodes;
for(Token token : tokens){
if(state == ParseState::START && token.type != TokenType::SYMBOL){
if(extcmds.count(token.value) == 0 && builtincmds.count(token.value) == 0){
GenericNode* gn = new GenericNode(token.value);
nodes.push_back(gn);
}
else{
cmd = token.value;
args.push_back(token.value);
state = ParseState::ARGS;
}
}
else{
if(token.type == TokenType::SYMBOL){
//save this to a node
// TODO: Currently this has object slicing
CommandNode* cn = new CommandNode(cmd, args);
nodes.push_back(cn);
//clear cmd and args for next time
args.clear();
cmd = "";
//set parse state to check for cmd again
state = ParseState::START;
//add in an operator node
OperatorNode* on = new OperatorNode(token.value);
nodes.push_back(on);
}
else{
args.push_back(token.value);
}
}
}
//catch case where command args are not explicitly terminated with a symbol due to end of input
if(cmd != ""){
CommandNode* cn = new CommandNode(cmd, args);
nodes.push_back(cn);
}
GenericCommand* gc = parseSeparate(nodes.begin(), nodes.end() - 1, nodes);
int c_pid = fork();
//child process
if(c_pid == 0){
gc->execute();
}
//parent process
else{
waitpid(c_pid, NULL, 0);
}
// prevent memory leak
for(Node* node : nodes){
delete node;
}
delete gc;
}