-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.cc
457 lines (378 loc) · 11.2 KB
/
command.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
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <pwd.h>
#include "y.tab.hh"
#include "command.hh"
#include "shell.hh"
//#include "tty-raw-mode.c"
extern char **environ;
extern "C" void tty_term_mode(void);
extern "C" void setNextCommand(char *command);
Command::Command() {
// Initialize a new vector of Simple Commands
_simpleCommands = std::vector<SimpleCommand *>();
// Initialize a new vector of full Command parts
fullCommand = std::vector<std::string>();
// Input, Output, Error Files
_outFile = NULL;
_inFile = NULL;
_errFile = NULL;
// Background Command Variable
_background = false;
// Append output and error variables
_append = false;
_appendErr = false;
// Ambiguous Redirection variable
_ambiguousRedirect = false;
// bool to check for source command
_src = false;
// bool to check for subshell
_subshell = false;
// Return Code of previous command
_returnCode = 0;
// PID of previous background command
_pidBackground = 0;
// Index used in building command for history table
_index = 0;
}
void Command::insertSimpleCommand( SimpleCommand * simpleCommand ) {
// add the simple command to the vector
_simpleCommands.push_back(simpleCommand);
}
void Command::clear() {
// deallocate all the simple commands in the command vector
for (auto simpleCommand : _simpleCommands) {
delete simpleCommand;
}
// remove all references to the simple commands we've deallocated
// (basically just sets the size to 0)
_simpleCommands.clear();
// Clear output, input, and error files
if ( _outFile == _errFile ) {
delete _outFile;
_outFile = NULL;
_errFile = NULL;
} else {
if ( _outFile ) {
delete _outFile;
}
_outFile = NULL;
if ( _errFile ) {
delete _errFile;
}
_errFile = NULL;
}
if ( _inFile ) {
delete _inFile;
}
_inFile = NULL;
_background = false;
}
void Command::print() {
printf("\n\n");
printf(" COMMAND TABLE \n");
printf("\n");
printf(" # Simple Commands\n");
printf(" --- ----------------------------------------------------------\n");
int i = 0;
// iterate over the simple commands and print them nicely
for ( auto & simpleCommand : _simpleCommands ) {
printf(" %-3d ", i++ );
simpleCommand->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 Command::execute() {
// Variable for builtin commands
bool builtin = false;
// Don't do anything if there are no simple commands
if ( _simpleCommands.size() == 0 ) {
if (isatty(0)) {
Shell::prompt();
}
return;
}
// Don't do anything if there is ambiguous redirection
if ( _ambiguousRedirect ) {
return;
}
// Print contents of Command data structure
if (isatty(0)) {
//print();
}
//Storing stdin, stdout, and stderr
int defaultin = dup( 0 );
int defaultout = dup( 1 );
int defaulterr = dup( 2 );
int fdin;
int fdout;
int fderr;
//Opening _inFile if needed
if ( _inFile ) {
fdin = open((char *) _inFile->c_str(), O_CREAT|O_RDWR, 0663);
} else {
fdin = dup( defaultin );
close(fdin);
}
//Opening _errFile if needed
if( _errFile ) {
if( _appendErr ) {
fderr = open((char *) _errFile->c_str(), O_CREAT|O_RDWR|O_APPEND, 0663);
} else {
fderr = open((char *) _errFile->c_str(), O_CREAT|O_RDWR|O_TRUNC, 0663);
}
} else {
fderr = dup( defaulterr );
}
dup2(fderr, 2);
close(fderr);
// Insert into the history table
if(!_src && !_subshell) {
insertHistory();
} else if (strcmp(_simpleCommands[0]->_arguments[0]->c_str(), "source")) {
insertHistory();
}
int x = 0;
int ret;
int fdpipe[2];
// Checks whether it is a builtin command or exit command.
if(strcmp(_simpleCommands[0]->_arguments[0]->c_str(), "exit") == 0) {
if(isatty(0)) {
printf("Goodbye\n");
}
// Closing all file descriptors before exiting
dup2(defaultin, 0);
dup2(defaultout, 1);
dup2(defaulterr, 2);
close(defaultin);
close(defaultout);
close(defaulterr);
close(fdin);
clear();
// Restore original terminal mode
// ReturnCode is set appropriately
// LastArg is set appropriately
tty_term_mode();
exit(0);
} else if(strcmp(_simpleCommands[0]->_arguments[0]->c_str(), "setenv") == 0) {
char *name = (char *) _simpleCommands[0]->_arguments[1]->c_str();
char *value = (char *) _simpleCommands[0]->_arguments[2]->c_str();
Command::_returnCode = 0;
if(setenv(name, value, 1)) {
Command::_returnCode = 2;
perror("setenv\n");
}
Command::_lastArg = *(_simpleCommands[0]->_arguments[2]);
builtin = true;
} else if(strcmp(_simpleCommands[0]->_arguments[0]->c_str(), "unsetenv") == 0) {
Command::_returnCode = 0;
if(unsetenv(_simpleCommands[0]->_arguments[1]->c_str())) {
Command::_returnCode = 2;
perror("unsetenv\n");
}
Command::_lastArg = *(_simpleCommands[0]->_arguments[1]);
builtin = true;
} else if(strcmp(_simpleCommands[0]->_arguments[0]->c_str(), "cd") == 0) {
Command::_returnCode = 0;
if(_simpleCommands[0]->_arguments.size() == 1) {
if(chdir(getenv("HOME"))) {
perror("cd");
Command::_returnCode = 2;
}
Command::_lastArg = *(_simpleCommands[0]->_arguments[0]);
} else {
Command::_returnCode = 0;
if(chdir(_simpleCommands[0]->_arguments[1]->c_str())) {
fprintf(stderr, "cd: can't cd to %s\n", _simpleCommands[0]->_arguments[1]->c_str());
Command::_returnCode = 2;
}
Command::_lastArg = *(_simpleCommands[0]->_arguments[1]);
}
builtin = true;
}
// Iterate over simple commands if it isn't a builtin command
if(!builtin) {
for ( auto & simpleCommand : _simpleCommands ) {
//Sets stdin
dup2(fdin, 0);
close(fdin);
//Check if it is the last command and opens _outFile if needed
if ( x == _simpleCommands.size() - 1) {
if( _outFile ) {
if( _append ) {
fdout = open((char *) _outFile->c_str(), O_CREAT|O_RDWR|O_APPEND, 0663);
} else {
fdout = open((char *) _outFile->c_str(), O_CREAT|O_RDWR|O_TRUNC, 0663);
}
} else {
fdout = dup( defaultout );
}
} else {
//Creates a new pipe
pipe(fdpipe);
fdout = fdpipe[1];
fdin = fdpipe[0];
}
//Sets stdout
dup2(fdout, 1);
close(fdout);
//Gets argv from simpleCommand for execv
std::vector<std::string *> arguments = returnArgs(simpleCommand->_arguments, simpleCommand->_arguments.size());
const char *argv[simpleCommand->_arguments.size()+1];
for(int x = 0; x < simpleCommand->_arguments.size(); x++) {
argv[x] = arguments.at(x)->c_str();
}
argv[simpleCommand->_arguments.size()] = NULL;
Command::_lastArg = *(simpleCommand->_arguments.at(simpleCommand->_arguments.size()-1));
ret = fork();
if (ret == 0) {
// Printenv command
if(strcmp(argv[0], "printenv") == 0) {
int i = 0;
while(environ[i]) {
printf("%s\n", environ[i++]);
}
fflush(stdout);
Command::_returnCode = 0;
Command::_lastArg = "printenv";
dup2(defaultin, 0);
dup2(defaultout, 1);
dup2(defaulterr, 2);
close(defaultin);
close(defaultout);
close(defaulterr);
exit(0);
} else {
execvp(argv[0], (char *const *)argv);
perror("execvp");
_exit(1);
}
} else if (ret < 0) {
perror("fork");
exit(2);
}
x++;
}
}
// Resetting stdin, stdout, stderr
// and closing unecessary file descriptors
dup2(defaultin, 0);
dup2(defaultout, 1);
dup2(defaulterr, 2);
close(defaultin);
close(defaultout);
close(defaulterr);
int status;
if(!builtin) {
//Waitpid if background is not set
if ( !_background ) {
waitpid(ret, &status, 0);
Command::_returnCode = WEXITSTATUS(status);
if(isatty(0)) {
if(Command::_returnCode != 0) {
printf("%s\n", getenv("ON_ERROR"));
fflush(stdout);
}
Shell::prompt();
}
} else {
// Set the _pidBackground variable
Shell::changePID(ret);
_pidBackground = ret;
}
} else {
// Do nothign if it is a builtin
builtin = false;
if(isatty(0)) {
Shell::prompt();
}
}
// Clear to prepare for next command
clear();
}
// Returns vector of Arguments from simpleCommand
std::vector<std::string *> Command::returnArgs(std::vector<std::string *> args, int size) {
std::vector<std::string *> argv = std::vector<std::string *>();
std::string env = "";
std::string fullArg = "";
char *env1;
for ( auto & arg : args ) {
// Perform Tilde expansion
if(arg->at(0) == '~') {
if(arg->size() == 1) {
passwd *pass = getpwnam(getenv("USER"));
fullArg += pass->pw_dir;
} else {
std::string user = "";
for(int i = 1; i < arg->size(); i++) {
if (arg->at(i) == '/' || i == arg->size()-1) {
fullArg += "/homes/";
fullArg += user;
fullArg += arg->substr(i, arg->size()-i+1);
break;
}
user += arg->at(i);
}
}
} else {
for(int j = 0; j < arg->size(); j++) {
fullArg += arg->at(j);
}
}
*arg = fullArg;
argv.push_back(arg);
fullArg = "";
}
argv.push_back(NULL);;
return argv;
}
// Builds the full command to be inserted
void Command::buildCommand(std::string insert) {
if(!_src && !_subshell) {
Command::fullCommand.push_back(insert);
}
}
// Inserts the command into the history table
void Command::insertHistory() {
std::string command = "";
for(std::string s : Command::fullCommand) {
command += s;
command += " ";
}
setNextCommand(strdup(command.substr(0, command.size() - 1).c_str()));
fullCommand.clear();
}
// Set PID of last background process run
void Command::setPID(int newPid) {
Command::_pidBackground = newPid;
}
// Set relative path
void Command::setRelativePath(char *relativePath) {
Command::_relativePath = relativePath;
}
SimpleCommand * Command::_currentSimpleCommand;
int Command::_pidBackground;
std::string Command::_lastArg;
std::string Command::_relativePath;
std::vector<std::string> Command::fullCommand;
int Command::_returnCode;
int Command::_index;
bool Command::_src;
bool Command::_subshell;