forked from vsinha/shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.cc
More file actions
442 lines (359 loc) · 10.3 KB
/
command.cc
File metadata and controls
442 lines (359 loc) · 10.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
/*
* 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!
*
*/
#include <fcntl.h>
#include <pwd.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "command.h"
#include "trace.h"
extern char **environ;
// ******** SimpleCommand ********
SimpleCommand::SimpleCommand()
{
// Creat available space for 5 arguments
_numberOfAvailableArguments = 5;
_numberOfArguments = 0;
_arguments = (char **) malloc( _numberOfAvailableArguments * sizeof( char * ) );
}
void
SimpleCommand::insertArgument( char * argument )
{
if ( _numberOfAvailableArguments == _numberOfArguments + 1 ) {
// Double the available space
_numberOfAvailableArguments *= 2;
_arguments = (char **) realloc( _arguments,
_numberOfAvailableArguments * sizeof( char * ) );
}
// check for env variable to expand
const char * pattern = "\\${.*}";
regex_t preg;
regmatch_t match;
if ( regcomp(&preg, pattern, 0) ) {
perror("regex failed to compile");
exit(1);
}
// expand a variable!
if ( !regexec(&preg, argument, 1, &match, 0) ) {
// regex matched ${var}, must look up at least one var
const unsigned int MAXARGLENGTH = 1024;
char * expandedArg = (char*)calloc(1, MAXARGLENGTH * sizeof(char));
// start copying non $ characters in the string
int i = 0; // position in argument
int m = 0; // position in expandedArg
while(argument[i] != 0 && i < MAXARGLENGTH ) {
if ( argument[i] != '$' ) { // as long as we havent encountered the var
expandedArg[m] = argument[i];
expandedArg[m + 1] = '\0'; //null terminator
i++;
m++;
} else { // argument[i] == '$', we must expand the variable
// find location of next '{' and '}' from where we are
char * start = strchr((char*)(argument + i), '{');
char * end = strchr((char*)(argument + i), '}');
// copy the environment var to expand,
// we want to be excluding '${' and '}'
char * var = (char*)calloc( 1, strlen(argument) * sizeof(char));
strncat(var, start + 1, end - start - 1);
// get the value
char* value = getenv(var);
if (value == NULL) { //nullcheck
strcat(expandedArg, "");
} else {
// add the variable's value to our expanded string
strcat(expandedArg, value);
}
//increment length of var + '${' and '}'
i += strlen(var) + 3;
m += strlen(value);
free(var);
}
}
argument = strdup(expandedArg);
}
// check for tilde to expand
if (argument[0] == '~') {
struct passwd *pw = getpwuid(getuid());
const char *homedir = pw->pw_dir;
// allocate space for expanding the tilde + the original argument
char * newArg = (char *)malloc( (strlen(homedir) + strlen(argument))*sizeof(char) );
newArg[0] = '\0';
strcat( newArg, homedir );
strcat( newArg, "/" );
strcat( newArg, (char *)(argument + 1) );
TRACE("new argument: %s\n", newArg);
argument = newArg;
}
// free the regex
regfree(&preg);
// add the argument
_arguments[ _numberOfArguments ] = argument;
// Add NULL argument at the end
_arguments[ _numberOfArguments + 1] = NULL;
_numberOfArguments++;
}
// ******** ArgCollector ********
// collects arguments from wildcard expansion
// sorts, and finally we add them to the simpleCommand
// initialize
ArgCollector::ArgCollector() {
maxArgs = 5;
nArgs = 0;
argArray = (char**)malloc(maxArgs * sizeof(char*));
}
// add argument to resizable array
void ArgCollector::addArg( char* arg ){
if ( maxArgs == nArgs ) {
//double available space
maxArgs *= 2;
if ( (argArray = (char**)realloc(argArray, maxArgs * sizeof(char*))) == NULL) {
perror("realloc arg buffer");
exit(1);
}
}
argArray[nArgs] = arg;
nArgs++;
}
int compare (const void * a, const void * b) {
return strcmp( *(const char**)a, *(const char**)b);
}
// sort the arguments
void ArgCollector::sortArgs(){
qsort(argArray, nArgs, sizeof(const char *), compare);
}
void ArgCollector::clear(){
//reset everything
maxArgs = 5;
nArgs = 0;
free(argArray);
argArray = (char**)malloc(maxArgs * sizeof(char*));
}
// ******** COMMAND ********
Command::Command()
{
// Create available space for one simple command
_numberOfAvailableSimpleCommands = 1;
_simpleCommands = (SimpleCommand **)
malloc( _numberOfSimpleCommands * sizeof( SimpleCommand * ) );
_numberOfSimpleCommands = 0;
_outFile = 0;
_inputFile = 0;
_errFile = 0;
_background = 0;
_openOptions = 0;
_ambiguous = 0;
}
void
Command::insertSimpleCommand( SimpleCommand * simpleCommand )
{
if ( _numberOfAvailableSimpleCommands == _numberOfSimpleCommands ) {
_numberOfAvailableSimpleCommands *= 2;
_simpleCommands = (SimpleCommand **) realloc( _simpleCommands,
_numberOfAvailableSimpleCommands * sizeof( SimpleCommand * ) );
}
_simpleCommands[ _numberOfSimpleCommands ] = simpleCommand;
_numberOfSimpleCommands++;
}
void
Command:: clear()
{
for ( int i = 0; i < _numberOfSimpleCommands; i++ ) {
for ( int j = 0; j < _simpleCommands[ i ]->_numberOfArguments; j ++ ) {
free ( _simpleCommands[ i ]->_arguments[ j ] );
}
free ( _simpleCommands[ i ]->_arguments );
free ( _simpleCommands[ i ] );
}
if ( _outFile ) {
free( _outFile );
}
if ( _inputFile ) {
free( _inputFile );
}
_numberOfSimpleCommands = 0;
_outFile = 0;
_inputFile = 0;
_errFile = 0;
_background = 0;
_ambiguous = 0;
}
void
Command::print()
{
printf("\n\n");
printf(" COMMAND TABLE \n");
printf("\n");
printf(" # Simple Commands\n");
printf(" --- ----------------------------------------------------------\n");
for ( int i = 0; i < _numberOfSimpleCommands; i++ ) {
printf(" %-3d ", i );
for ( int j = 0; j < _simpleCommands[i]->_numberOfArguments; j++ ) {
printf("\"%s\" \t", _simpleCommands[i]->_arguments[ j ] );
}
}
printf( "\n\n" );
printf( " Output Input Error Background\n" );
printf( " ------------ ------------ ------------ ------------\n" );
printf( " %-12s %-12s %-12s %-12s\n", _outFile?_outFile:"default",
_inputFile?_inputFile:"default", _errFile?_errFile:"default",
_background?"YES":"NO");
printf( "\n\n" );
}
void
Command::execute()
{
// Don't do anything if there are no simple commands
if ( _numberOfSimpleCommands == 0 ) {
prompt();
return;
}
if ( _ambiguous != 0 ) {
printf("Ambiguous output redirect.\n");
}
// save stdin / stdout / stderr
int tempIn = dup(0);
int tempOut = dup(1);
int tempErr = dup(2);
// set input
int fdIn;
if (_inputFile) { // open file for reading
fdIn = open(_inputFile, O_RDONLY);
} else { // use default input
fdIn = dup(tempIn);
}
int i;
int fdOut;
pid_t child;
for ( i = 0; i < _numberOfSimpleCommands; i++ ) {
// redirect input and close fdIn since we're done with it
dup2(fdIn, 0);
close(fdIn);
// setup output
if (i == _numberOfSimpleCommands - 1) { // last simple command
if (_outFile) { //redirect output
mode_t openMode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; // -rw-r-----
fdOut = open(_outFile, _openOptions, openMode);
} else { //use default output
fdOut = dup(tempOut);
}
} else { // not last simple command, so create a pipe
int fdPipe[2];
pipe(fdPipe);
fdOut = fdPipe[1];
fdIn = fdPipe[0];
}
dup2(fdOut, 1); // redirect output
if (_errFile) { // redirect error at same location as output if necessary
dup2( fdOut, 2);
}
close(fdOut); //close fdOut since we're done with it
//check for special commands
if ( !strcmp(_simpleCommands[i]->_arguments[0], "exit")
|| !strcmp(_simpleCommands[i]->_arguments[0], "quit") ){ // exit
exit(1);
} else if ( !strcmp(_simpleCommands[i]->_arguments[0], "setenv") ) { // setenv
setenv(_simpleCommands[i]->_arguments[1], _simpleCommands[i]->_arguments[2], 1);
child = 1;
continue; //skip the rest of this iteration
} else if ( !strcmp(_simpleCommands[i]->_arguments[0], "unsetenv") ) { // unsetenv
unsetenv(_simpleCommands[i]->_arguments[1]);
continue;
} else if ( !strcmp(_simpleCommands[i]->_arguments[0], "cd") ) { // cd
//call chdir
//if no argument is specified, cd to $HOME
//otherwise cd to the specified path
int ret;
if ( _simpleCommands[i]->_numberOfArguments > 1 ) {
ret = chdir( _simpleCommands[i]->_arguments[1] );
} else {
ret = chdir( getenv("HOME") );
}
if (ret != 0) {
fprintf(stderr, "No such file or directory\n");
}
continue;
} else { // else we sort the arguments and fork!
child = fork();
}
/* --- post-fork --- */
if (child == 0) { //child process
signal(SIGINT, SIG_DFL); //reset SIGINT for great good
execvp(_simpleCommands[i]->_arguments[0], _simpleCommands[i]->_arguments);
//if the child process reaches this point, execvp has failed
perror("execvp");
_exit(1);
} else if (child < 0) {
fprintf(stderr, "Fork failed\n");
exit(1);
}
if (!_background) {
waitpid(child, NULL, 0);
}
} // endfor
// restore in/out/err defaults
dup2(tempIn, 0);
dup2(tempOut, 1);
dup2(tempErr, 2);
close(tempIn);
close(tempOut);
close(tempErr);
// Clear to prepare for next command
clear();
// Print new prompt
prompt();
}
// Shell implementation
void
Command::prompt()
{
if (isatty(fileno(stdin))) {
printf("myshell $ ");
fflush(stdout);
}
}
Command Command::_currentCommand;
SimpleCommand * Command::_currentSimpleCommand;
ArgCollector * Command::_currentArgCollector;
int yyparse(void);
void sigint_handler(int sig) {
printf("\n");
Command::_currentCommand.clear();
Command::_currentCommand.prompt();
}
void sigchild_handler(int sig) {
while( waitpid(-1, NULL, WNOHANG) > 0); //lol ok this works i guess
}
int main()
{
// set the SIGINT handler
struct sigaction sa_int;
sa_int.sa_handler = sigint_handler;
sa_int.sa_flags = SA_RESTART; //restart any interrupted system calls
sigemptyset(&sa_int.sa_mask);
if (sigaction(SIGINT, &sa_int, NULL) == -1) {
perror("sigint action");
exit(1);
}
// set the SIGCHILD handler
struct sigaction sa_child;
sa_child.sa_handler = sigchild_handler;
sa_child.sa_flags = SA_RESTART;
sigemptyset(&sa_child.sa_mask);
if (sigaction(SIGCHLD, &sa_child, NULL) == -1) {
perror ("sigchild action");
}
Command::_currentCommand.prompt();
yyparse();
}