-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPipeCommand.cc
669 lines (592 loc) · 18.3 KB
/
PipeCommand.cc
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
/*
* CS252: Shell project
*
* Template file.
* You will need to add more code here to execute the command table.
*
* NOTE: You are responsible for fixing any bugs this code may have!
*
* DO NOT PUT THIS PROJECT IN A PUBLIC REPOSITORY LIKE GIT. IF YOU WANT
* TO MAKE IT PUBLICALLY AVAILABLE YOU NEED TO REMOVE ANY SKELETON CODE
* AND REWRITE YOUR PROJECT SO IT IMPLEMENTS FUNCTIONALITY DIFFERENT THAN
* WHAT IS SPECIFIED IN THE HANDOUT. WE OFTEN REUSE PART OF THE PROJECTS FROM
* SEMESTER TO SEMESTER AND PUTTING YOUR CODE IN A PUBLIC REPOSITORY
* MAY FACILITATE ACADEMIC DISHONESTY.
*/
#include <cstdio>
#include <cstdlib>
#include <dirent.h>
#include <fcntl.h>
#include <iostream>
#include <limits.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <stdio.h>
#include <string.h>
#include <regex>
#include <regex.h>
#include <unistd.h>
#include "PipeCommand.hh"
#include "Shell.hh"
int last_return_code = 0;
pid_t last_background_pid = 0;
std::string last_argument;
std::string shell_path;
extern char * path;
PipeCommand::PipeCommand() {
// Initialize a new vector of Simple PipeCommands
_simpleCommands = std::vector<SimpleCommand *>();
_outFile = NULL;
_inFile = NULL;
_errFile = NULL;
_append = false;
_background = false;
}
void PipeCommand::insertSimpleCommand( SimpleCommand * simplePipeCommand ) {
// add the simple command to the vector
_simpleCommands.push_back(simplePipeCommand);
}
void PipeCommand::clear() {
// deallocate all the simple commands in the command vector
for (auto simplePipeCommand : _simpleCommands) {
delete simplePipeCommand;
}
// remove all references to the simple commands we've deallocated
// (basically just sets the size to 0)
_simpleCommands.clear();
if ( _outFile ) {
delete _outFile;
}
_outFile = NULL;
if ( _inFile ) {
delete _inFile;
}
_inFile = NULL;
if ( _errFile ) {
delete _errFile;
}
_errFile = NULL;
_append = false;
_background = false;
}
void PipeCommand::print() {
printf("\n\n");
//printf(" COMMAND TABLE \n");
printf("\n");
printf(" # Simple PipeCommands\n");
printf(" --- ----------------------------------------------------------\n");
int i = 0;
// iterate over the simple commands and print them nicely
for ( auto & simplePipeCommand : _simpleCommands ) {
printf(" %-3d ", i++ );
simplePipeCommand->print();
}
printf( "\n\n" );
printf( " Output Input Error Background\n" );
printf( " ------------ ------------ ------------ ------------\n" );
printf( " %-12s %-12s %-12s %-12s\n",
_outFile?_outFile->c_str():"default",
_inFile?_inFile->c_str():"default",
_errFile?_errFile->c_str():"default",
_background?"YES":"NO");
printf( "\n\n" );
}
void PipeCommand::execute() {
// Don't do anything if there are no simple commands
if ( _simpleCommands.size() == 0 ) {
Shell::TheShell->prompt();
return;
}
// Print contents of PipeCommand data structure
//print();
//save stdin/stdout
int tempin = dup(0);
int tempout = dup(1);
int temperr = dup(2);
//create pipe
int fdin = 0;
if (_inFile) {
//read infile
fdin = open(_inFile->c_str(), O_RDONLY);
} else {
//use default input
fdin = dup(tempin);
}
int fderr = 0;
if (_errFile) {
if (_append) {
//read append to error file if bool append
fderr = open(_errFile->c_str(), O_WRONLY | O_APPEND | O_CREAT, 0666);
} else {
//write over error file
fderr = open(_errFile->c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0666);
}
} else {
fderr = dup(temperr);
}
dup2(fderr, 2);
close(fderr);
int ret = 0;
int fdout = 0;
unsigned long size = _simpleCommands.size();
int temp = 0;
for (unsigned long i = 0; i < size; i++) {
std::vector<std::string*> arguments;
if (_simpleCommands[i]->_arguments.empty() == false) {
arguments = _simpleCommands[i]->_arguments;
}
//redirect input
dup2(fdin, 0);
close(fdin);
//setup output
int fdpipe[2];
if (i == size - 1) {
//last simple command
if (_outFile) {
if (_append) {
fdout = open(_outFile->c_str(), O_WRONLY | O_APPEND | O_CREAT, 0666);
} else {
fdout = open(_outFile->c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0666);
}
} else {
//use default output
fdout=dup(tempout);
}
} else {
// not last simple commmand
// create pipe
pipe(fdpipe);
fdin = fdpipe[0];
fdout=fdpipe[1];
//dup2(temp, fdin);
}
char ** args = this->expandEnvVarsAndWildcards(_simpleCommands[i]);
if (args == NULL) {
clear();
return;
}
setenv("_lastarg", arguments.back()->c_str(), 1);
//redirect
dup2(fdout, 1);
close(fdout);
//create child process
ret = fork();
if (ret == -1) {
perror("fork\n");
exit(2);
}
if (ret == 0) {
if (!strcmp(_simpleCommands[i]->_arguments[0]->c_str(), "printenv")) {
char **p = environ;
while (*p != NULL) {
if (!strcmp(*p, "_lastarg")) {
continue;
} else {
printf("%s\n", *p);
p++;
}
}
exit(0);
}
execvp(args[0], args);
for (unsigned long i = 0; args[i] != nullptr; i++) {
delete[] args[i];
}
delete[] args;
perror("execvp");
exit(1);
}
if (i < size - 1) {
close(fdpipe[1]);
}
dup2(fdpipe[0], temp);
}
dup2(tempin, 0);
dup2(tempout, 1);
dup2(temperr, 2);
close(tempin);
close(tempout);
close(temperr);
if (_background == false) {
int status;
waitpid(ret, &status, 0);
if (WIFEXITED(status)) {
last_return_code = WEXITSTATUS(status);
} else {
last_background_pid = ret;
}
}
// For every simple command fork a new process
// Setup i/o redirection
// and call exec
// Clear to prepare for next command
clear();
// Print new prompt
//Shell::TheShell->prompt();
}
std::string PipeCommand::subShell(std::string& command) {
//if command is of type subshell and meets regex requirements then create subshell
std::string argo;
if (command.find("$(") != std::string::npos) {
argo = command.substr(2); //get rid of $(
argo.resize(argo.size() - 1); //get rid of )
} else {
argo = command.substr(1); //get rid of first backtick
argo.resize(argo.size() - 1); //get rid of second backtick
}
int tempin = dup(0);
int tempout = dup(1);
int pin[2];
pipe(pin);
int pout[2];
pipe(pout);
write(pin[1], argo.c_str(), argo.length());
write(pin[1], "\n", 1);
close(pin[1]);
char * buffer = (char *) malloc(4096);
strncpy(buffer, (char *)"", 10);
int ret = fork();
if (ret == 0) {
close(pin[1]);
close(pout[0]);
dup2(pin[0], 0);
dup2(pout[1], 1);
char *args[2] = {const_cast<char*>("/proc/self/exe"), NULL};
execvp(args[0], args);
//execvp closes pin[1] and pout[0]
_exit(1);
} else if (ret < 0) {
perror("fork");
exit(1);
} else {
//parent
waitpid(ret, NULL, 0);
close(pin[1]);
close(pout[1]);
close(pin[0]);
char c;
int i = 0;
while(read(pout[0], &c, 1)) {
if (c == '\n' || c == ' ') {
buffer[i++] = ' ';
} else {
buffer[i++] = c;
}
}
buffer[i] = '\0';
dup2(tempin, 0);
dup2(tempout, 1);
close(tempin);
close(tempout);
}
waitpid(ret, NULL, 0);
std::string output(buffer);
free(buffer);
return output;
}
std::string PipeCommand::envExpansion(std::string& command) {
//if command is of type env var then expands and return
std::string argo = command.substr(2); //get rid of ${
argo.resize(argo.size() - 1); //get rid of }
if (argo == "$") {
argo = std::to_string(getpid());
} else if (argo == "?") {
argo = std::to_string(last_return_code);
} else if (argo == "!") {
argo = std::to_string(last_background_pid);
} else if (argo == "_") {
//not working
argo = getenv("_lastarg");
//std::cout << "Hello" <<std::endl;
} else if (argo == "SHELL") {
char realPath[PATH_MAX];
if (realpath(path, realPath) != NULL) {
argo = std::string(realPath);
} else {
argo = "error";
}
} else {
argo = getenv(argo.c_str());
}
return argo;
}
std::string PipeCommand::tildeExpansion(std::string& command) {
while (true) {
std::string::iterator found = std::find(command.begin(), command.end(), '~');
if (found == command.end()) {
break;
}
if (command.length() == 1 || *(found + 1) == '/') {
struct passwd* pwd = getpwnam(getenv("USER"));
if (pwd) {
command.replace(found, found + 1, pwd->pw_dir);
} else {
break;
}
} else {
std::string::iterator slash = std::find(found, command.end(), '/');
std::string user(found + 1, slash);
struct passwd* pwd = getpwnam(user.c_str());
if (pwd) {
command.replace(found, slash, pwd->pw_dir);
} else {
break;
}
}
}
return command;
}
std::vector<std::string> PipeCommand::wildCards(std::string& command) {
std::string regex = regConvert(command);
std::regex pattern(regex);
std::vector<std::string> matched_files;
DIR *dir;
struct dirent* ent;
if ((dir = opendir(".")) != nullptr) {
while ((ent = readdir(dir)) != nullptr) {
std::string file = ent->d_name;
if (std::regex_match(file, pattern)) {
if (file[0] == '.') {
if (command[0] == '.') {
matched_files.push_back(file);
}
} else {
matched_files.push_back(file);
}
}
}
closedir(dir);
} else {
perror("oopendir");
return matched_files;
}
if (matched_files.empty()) {
matched_files.push_back(regex);
}
return matched_files;
}
void PipeCommand::expandWildcards(std::string& prefix, std::string& suffix, std::vector <std::string> &matched_files, bool multilevel) {
if (suffix == "") {
size_t lastSlash = prefix.find_last_of('/');
if (prefix[lastSlash + 1] != '.') {
matched_files.push_back(prefix);
}
return;
}
if (multilevel == true) {
size_t star = suffix.find('*');
// find first star
size_t firstSlash = suffix.find_last_of('/', star);
// find slash before star
size_t secondSlash = suffix.find_first_of('/', firstSlash + 1);
// find slash after star
std::string fakeSuffix; //segment the has the first regex
// /etc/r*/*s* -> fakeSuffix would be r*
std::string nextSuffix; //segment the rest of the suffix
// /etc/r*/*s* -> nextSuffix would be *s*
if (secondSlash != std::string::npos) {
//if secondSlash exists
fakeSuffix = suffix.substr(firstSlash + 1, secondSlash);
nextSuffix = suffix.substr(secondSlash + 1);
} else {
//if secondSlash doesnt exist
multilevel = false; // becomes false because there is no more levels
fakeSuffix = suffix.substr(firstSlash + 1);
nextSuffix = "";
}
DIR * dir;
struct dirent *ent;
//create regex of fakeSuffix -> this will be what we are matching
std::string regex = regConvert(fakeSuffix);
std::regex pattern(regex);
if ((dir = opendir(prefix.c_str())) != nullptr) {
while ((ent = readdir(dir)) != nullptr) {
if (regex_match(ent->d_name, pattern)) {
std::string full_arg = prefix + "/" + ent->d_name;
if (ent->d_name[0] == '.') {
if (fakeSuffix[0] == '.') {
matched_files.push_back(full_arg);
}
}
expandWildcards(full_arg, nextSuffix, matched_files, multilevel);
}
}
}
}
return;
}
std::string PipeCommand::regConvert(std::string s) {
std::string regex = "^";
for (char c: s) {
switch (c) {
case '*':
regex+= ".*";
break;
case '?':
regex+= ".";
break;
case '.':
regex+="\\.";
break;
default:
regex+=c;
break;
}
}
regex += "$";
return regex;
}
bool PipeCommand::needsWildcardExpansion(std::string& command) {
return command.find('*') != std::string::npos || command.find('?') != std::string::npos;
}
// Expands environment vars and wildcards of a SimpleCommand and
// returns the arguments to pass to execvp.
char **
PipeCommand::expandEnvVarsAndWildcards(SimpleCommand * simpleCommandNumber)
{
//print();
//simpleCommandNumber->print();
std::regex envVar(R"(\$\{[^}]+\})");
std::regex sub(R"(\$\(([^)]+)\))");
std::regex sub2(R"(`([^`]*)`)");
std::regex tilde(R"(^~[^ |>\t\n]*)");
std::vector <std::string> arg_list;
if (strcmp(simpleCommandNumber->_arguments[0]->c_str(), "setenv") == 0) {
//setenv
if (setenv(simpleCommandNumber->_arguments[1]->c_str(), simpleCommandNumber->_arguments[2]->c_str(), 1) < 0) {
perror("setenv");
}
return NULL;
} else if (strcmp(simpleCommandNumber->_arguments[0]->c_str(), "unsetenv") == 0) {
//unsetenv
if (unsetenv(simpleCommandNumber->_arguments[1]->c_str()) < 0) {
perror("unsetenv");
}
return NULL;
} else if (strcmp(simpleCommandNumber->_arguments[0]->c_str(), "cd") == 0) {
//cd
int error;
if (simpleCommandNumber->_arguments.size() == 1) {
error = chdir(getenv("HOME"));
if (error < 0) {
std::string a = getenv("HOME");
std::string b = "cd: can't cd to ";
std::string c = b + a;
perror(c.c_str());
}
} else {
if (std::regex_search(simpleCommandNumber->_arguments[1]->c_str(), envVar)) {
//expand before cd ing
std::string com = simpleCommandNumber->_arguments[1]->c_str();
std::string full_arg = "";
while (com != "") {
size_t dollar = com.find('$');
size_t closer = com.find('}');
if (dollar == std::string::npos || closer == std::string::npos) {
full_arg += com;
com = "";
} else {
full_arg += com.substr(0, dollar);
com = com.substr(dollar, com.length() - dollar);
closer = com.find('}');
std::string temp = com.substr(0, closer + 1);
full_arg += envExpansion(temp);
com = com.substr(closer + 1, com.length() - closer);
}
}
delete simpleCommandNumber->_arguments[1];
simpleCommandNumber->_arguments[1] = new std::string(full_arg);
}
error = chdir(simpleCommandNumber->_arguments[1]->c_str());
if (error < 0) {
std::string a = simpleCommandNumber->_arguments[1]->c_str();
std::string b = "cd: can't cd to ";
std::string c = b + a;
perror(c.c_str());
}
}
return NULL;
} else {
for (unsigned long j = 0; j < simpleCommandNumber->_arguments.size(); j++) {
std::string argo = simpleCommandNumber->_arguments[j]->c_str();
if (std::regex_search(argo, envVar)) {
//envExpansion
std::string com = argo;
std::string full_arg = "";
while (com != "") {
size_t dollar = com.find('$');
size_t closer = com.find('}');
if (dollar == std::string::npos || closer == std::string::npos) {
full_arg += com;
com = "";
} else {
full_arg += com.substr(0, dollar);
com = com.substr(dollar, com.length() - dollar);
closer = com.find('}');
std::string temp = com.substr(0, closer + 1);
full_arg += envExpansion(temp);
com = com.substr(closer + 1, com.length() - closer);
}
}
argo = full_arg;
arg_list.push_back(argo);
} else if (std::regex_search(argo, sub) || std::regex_search(argo, sub2)) {
//subshell
std::string output = subShell(argo);
argo = subShell(argo);
//split the single argument into a vector of strings on ' '
//since subshell already gets rid of all the \n's
std::istringstream iss(output);
std::string token;
while (iss >> token) {
arg_list.push_back(token);
}
} else if (std::regex_search(argo, tilde)) {
//tilde
argo = tildeExpansion(argo);
arg_list.push_back(argo);
} else {
//check if wildcards needed
if (needsWildcardExpansion(argo) && argo.find("??") == std::string::npos) {
std::vector<std::string> matched_files = wildCards(argo);
//use own arg list since i am looping through simpleCommand's args
if (matched_files.size() == 1 && needsWildcardExpansion(matched_files[0])) {
//if wildCards function did not change the expression
//every test except for first
bool multilevel = false;
//for multi level regex expressions
size_t star = argo.find('*');
size_t slash = argo.find_last_of('/', star);
// finds the first slash between 0 and star position
size_t starAfterSlash = argo.find('*', slash + 1);
//finds the first star after slash position
if (starAfterSlash != std::string::npos) {
multilevel = true;
}
std::string pre = argo.substr(0, slash);
std::string suf = argo.substr(slash + 1);
matched_files.clear();
expandWildcards(pre, suf, matched_files, multilevel);
}
std::sort(matched_files.begin(), matched_files.end());
for (unsigned long k = 0; k < matched_files.size(); k++) {
arg_list.push_back(matched_files[k]);
}
} else { //matched_files empty
arg_list.push_back(argo);
}
}
}
char ** args = new char * [arg_list.size() + 1];
for (unsigned long i = 0; i < arg_list.size(); i++) {
args[i] = new char[arg_list[i].size() + 1];
std::strcpy(args[i], arg_list[i].c_str());
}
args[arg_list.size()] = NULL;
arg_list.clear();
return args;
}
return NULL;
}