-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcommand.cc
More file actions
354 lines (289 loc) · 6.71 KB
/
command.cc
File metadata and controls
354 lines (289 loc) · 6.71 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
/*
* 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 "command.hh"
extern char **environ; //Holds enviroment variables
bool onError = false;
using namespace std;
Command::Command()
{
// Create available space for one simple command
_numOfAvailableSimpleCommands = 1;
_simpleCommands = (SimpleCommand **)
malloc( _numOfSimpleCommands * sizeof( SimpleCommand * ) );
_numOfSimpleCommands = 0;
_outFile = 0;
_inFile = 0;
_errFile = 0;
_append = 0;
_background = 0;
_inCounter = 0;
_outCounter = 0;
}
void Command::insertSimpleCommand( SimpleCommand * simpleCommand ) {
if ( _numOfAvailableSimpleCommands == _numOfSimpleCommands ) {
_numOfAvailableSimpleCommands *= 2;
_simpleCommands = (SimpleCommand **) realloc( _simpleCommands,
_numOfAvailableSimpleCommands * sizeof( SimpleCommand * ) );
}
_simpleCommands[ _numOfSimpleCommands ] = simpleCommand;
_numOfSimpleCommands++;
}
void Command:: clear() {
for ( int i = 0; i < _numOfSimpleCommands; i++ ) {
for ( int j = 0; j < _simpleCommands[ i ]->_numOfArguments; j ++ ) {
free ( _simpleCommands[ i ]->_arguments[ j ] );
}
free ( _simpleCommands[ i ]->_arguments );
free ( _simpleCommands[ i ] );
}
if ( _outFile ) {
free( _outFile );
}
if ( _inFile ) {
free( _inFile );
}
if ( _errFile ) {
free( _errFile );
}
_numOfSimpleCommands = 0;
_outFile = 0;
_inFile = 0;
_errFile = 0;
_background = 0;
_inCounter = 0;
_outCounter = 0;
_append = 0;
}
void Command::print() {
printf("\n\n");
printf(" COMMAND TABLE \n");
printf("\n");
printf(" # Simple Commands\n");
printf(" --- ----------------------------------------------------------\n");
for ( int i = 0; i < _numOfSimpleCommands; i++ ) {
printf(" %-3d ", i );
for ( int j = 0; j < _simpleCommands[i]->_numOfArguments; 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",
_inFile?_inFile:"default", _errFile?_errFile:"default",
_background?"YES":"NO");
printf( "\n\n" );
}
int Command::BuiltIn(int i) {
if(strcmp(_simpleCommands[i]->_arguments[0], "setenv") == 0){
int error = setenv(_simpleCommands[i]->_arguments[1], _simpleCommands[i]->_arguments[2], 1);
if(error) {
perror("setenv");
}
clear();
prompt();
return 1;
}
if(strcmp(_simpleCommands[i]->_arguments[0], "unsetenv") == 0){
int error = unsetenv(_simpleCommands[i]->_arguments[1]);
if(error) {
perror("unsetenv");
}
clear();
prompt();
return 1;
}
if(strcmp(_simpleCommands[i]->_arguments[0], "cd") == 0){
int error;
if(_simpleCommands[i]->_numOfArguments == 1){
error = chdir(getenv("HOME"));
} else {
error = chdir(_simpleCommands[i]->_arguments[1]);
}
if(error < 0){
perror("cd");
}
clear();
prompt();
return 1;
}
return 0;
}
void Command::execute() {
// Don't do anything if there are no simple commands
if ( _numOfSimpleCommands == 0 ) {
prompt();
return;
}
//
if(strcmp(_simpleCommands[0]->_arguments[0],"exit") == 0){
printf("Good bye!!\n");
exit(1);
}
if (_inCounter > 1 || _outCounter > 1) {
printf("Ambiguous output redirect.\n");
clear();
prompt();
return;
}
// Print contents of Command data structure
//print();
// Add execution here
// For every simple command fork a new process
// Setup i/o redirection
// and call exec
int tmpin = dup(0);
int tmpout = dup(1);
int tmperr = dup(2);
int fdin;
int fdout;
int fderr;
if(_inFile){
fdin = open(_inFile, O_RDONLY);
}
else {
fdin = dup(tmpin);
}
if(_errFile){
if(_append){
fderr = open(_errFile, O_WRONLY | O_APPEND | O_CREAT, 0600);
}
else {
fderr = open(_errFile, O_WRONLY | O_CREAT | O_TRUNC, 0600);
}
}
else {
fderr = dup(tmperr);
}
dup2(fderr,2);
close(fderr);
int pid;
for(int i = 0; i < _numOfSimpleCommands; i++){
//built in
if(BuiltIn(i)) return;
dup2(fdin,0);
close(fdin);
if(i == _numOfSimpleCommands-1){
if(_outFile){
if(_append){
fdout = open(_outFile, O_WRONLY | O_APPEND | O_CREAT, 0600);
}
else {
fdout = open(_outFile, O_WRONLY | O_CREAT | O_TRUNC, 0600);
}
}
else {
fdout = dup(tmpout);
}
} else {
int fdpipe[2];
pipe(fdpipe);
fdout = fdpipe[1];
fdin = fdpipe[0];
}
dup2(fdout,1);
close(fdout);
pid = fork();
if (pid == -1){
perror("fork\n");
exit(2);
}
if(pid == 0){
if(strcmp(_simpleCommands[i]->_arguments[0], "printenv") == 0){
char ** env = environ;
while(*env){
printf("%s\n", *env);
env++;
}
}
if(strcmp(_simpleCommands[i]->_arguments[0], "source") == 0){
FILE * fp = fopen(_simpleCommands[i]->_arguments[1], "r");
char cmdline [1024];
fgets(cmdline, 1023, fp);
fclose(fp);
int tmpin = dup(0);
int tmpout = dup(1);
int fdpipein[2];
int fdpipeout[2];
pipe(fdpipein);
pipe(fdpipeout);
write(fdpipein[1], cmdline, strlen(cmdline));
write(fdpipein[1], "\n", 1);
close(fdpipein[1]);
dup2(fdpipein[0], 0);
close(fdpipein[0]);
dup2(fdpipeout[1], 1);
close(fdpipeout[1]);
int ret = fork();
if (ret == 0) {
execvp("/proc/self/exe", NULL);
_exit(1);
} else if (ret < 0) {
perror("fork");
exit(1);
}
dup2(tmpin, 0);
dup2(tmpout, 1);
close(tmpin);
close(tmpout);
char ch;
char * buffer = (char *) malloc (100);
int i = 0;
// Read from the pipe the output of the subshell
while (read(fdpipeout[0], &ch, 1)) {
if (ch != '\n')
buffer[i++] = ch;
}
buffer[i] = '\0';
printf("%s\n",buffer);
}
else {
execvp(_simpleCommands[i]->_arguments[0],_simpleCommands[i]->_arguments);
// perror("execvp");
_exit(1);
}
}
} //for
dup2(tmpin,0);
dup2(tmpout,1);
dup2(tmperr,2);
close(tmpin);
close(tmpout);
close(tmperr);
if(!_background){
waitpid(pid,NULL,0);
}
// Clear to prepare for next command
clear();
// Print new prompt
prompt();
}
// Shell implementation
void Command::prompt() {
char * PROMPT = getenv("PROMPT");
char * ERR = getenv("onError");
if(isatty(0) && !PROMPT){
printf("myshell>");
fflush(stdout);
}
if(ERR == NULL){
onError = false;
}
if(isatty(0) && !onError && PROMPT){
printf("%s",PROMPT);
}
if(isatty(0) && onError && PROMPT){
printf("%s",ERR);
}
fflush(stdout);
onError = false;
}
Command Command::_currentCommand;
SimpleCommand * Command::_currentSimpleCommand;