-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmyshell.cc
More file actions
306 lines (269 loc) · 9.53 KB
/
myshell.cc
File metadata and controls
306 lines (269 loc) · 9.53 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
#include <string>
#include <iostream>
#include <vector>
#include <list>
#include <sstream>
#include <sys/wait.h>
#include <errno.h> // man errno for information
#include <cassert>
//#include <thread>
#include <unistd.h>
#include <cerrno>
#include <cstring>
#include <stdlib.h>
#include <stdlib.h>
#include <cstring>
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "thread.h"
#include <mutex>
#include "filesystem.h"
using namespace filesystem;
using namespace std;
#define each(I) for( typeof((I).begin()) it=(I).begin(); it!=(I).end(); ++it )
/*
void testCompleteMe(){
char *complete = readline("");
cout << string(complete);
//printf("%s\n", complete);
delete complete;
}
*/
int doit( vector<string> tok );
struct Devices{
int deviceNumber;
string driverName;
};
struct openFileTable{
Devices *ptr; //pointer to device
bool write;
bool read;
};
struct processTable{
pid_t pid;
pid_t *ppid;
openFileTable opfile[32];
void setid(pid_t p, pid_t pp){pid = p; ppid = &pp;}
void print(){
cout << "[PID: " << pid << "][PPID: " << *ppid << "]" << endl;
}
};
thread_local processTable tmp;
int doit( vector<string> tok );
class shellThread : public Thread {
vector<string> tok;
int priority() {return Thread::priority(); }
void action (){
bool redirection = 0; // flag to see if we are doing redirection
int redirection_index = 0;
int saved_stdout = 0;
// Option processing: (1) redirect I/O as requested and (2) build
// a C-style list of arguments, i.e., an array of pointers to
// C-strings, terminated by an occurrence of the null poiinter.
//
//
string progname = tok[0];
tmp.setid(getpid(), getppid());
cout << "PID: " << tmp.pid << " PPID: " << *tmp.ppid << endl;
//testCompleteMe();
char* arglist[ 1 + tok.size() ]; // "1+" for a terminating null ptr.
int argct = 0;
for ( int i = 0; i != tok.size(); ++i ) {
string progname = tok[0];
char* arglist[ 1 + tok.size() ]; // "1+" for a terminating null ptr.
int argct = 0;
for ( int i = 0; i != tok.size(); ++i ) {
if ( tok[i] == "&" || tok[i] == ";" ) break; // arglist done.
else if ( tok[i] == "<" ) freopen( tok[++i].c_str(), "r", stdin );
else if ( tok[i] == ">" ) freopen( tok[++i].c_str(), "w", stdout );
else if ( tok[i] == ">>" )
{
cerr << ">>: Entering\n";
redirection = 1;
redirection_index = i;
saved_stdout = dup(STDOUT_FILENO);
freopen( tok[++i].c_str(), "a", stdout );
cerr << ">>: Exiting\n";
}
else if ( tok[i] == "2>" ) freopen( tok[++i].c_str(), "w", stderr );
else if ( tok[i] == "|" ) { // create a pipeline.
int mypipe[2];
int& pipe_out = mypipe[0];
int& pipe_in = mypipe[1];
// Find two available ports and create a pipe between them, and
// store output portuntitled folder# into pipe_out and input port# to pipe_in.
if ( pipe( mypipe ) ) { // All that is done here by pipe().
//cerr << "myshell: " << strerror(errno) << endl; // report err
return;
} else if ( fork() ) { // you're the parent and consumer here.
dup2( pipe_out, STDIN_FILENO ); // connect pipe_out to stdin.
close( pipe_out ); // close original pipe connections.
close( pipe_in );
while ( tok.front() != "|" ) tok.erase( tok.begin() );
tok.erase(tok.begin()); // get rid of "|".
exit( doit( tok ) ); // recurse on what's left of tok.
} else { // you're the child and producer here.
dup2( pipe_in, STDOUT_FILENO ); // connect pipe_in to stdout.
close( pipe_out ); // close original pipe connections.
close( pipe_in );
break; // exec with the current arglist.
}
}
else { // add this token a C-style argv for execvp().
// Append tok[i].c_str() to arglist
arglist[argct] = new char[1+tok[i].size()];
strcpy( arglist[argct], tok[i].c_str() );
// arglist[argct] = const_cast<char*>( tok[i].c_str() );
// Per C++2003, Section 21.3.7: "Nor shall the program treat
// the returned value [ of .c_str() ] as a valid pointer value
// after any subsequent call to a non-const member function of
// basic_string that designates the same object as this."
// And, there are no subsequent operations on these strings.
arglist[++argct] = 0; // C-lists of strings end w null pointer.
}
}
// tilde expansion
if ( progname[0] == '~' ) progname = getenv("HOME")+progname.substr(1);
// execvp( progname.c_str(), arglist ); // execute the command.
Inode<App>* junk = static_cast<Inode<App>*>((dynamic_cast<Inode<Directory>*>(root->file->theMap["bin"])->file->theMap)[tok[0]] ); //Update to put apps in a directory
if ( ! junk ) {
(dynamic_cast<Inode<Directory>*>(root->file->theMap["bin"])->file->theMap).erase(tok[0]);
cerr << "shell: " << tok[0] << " command not found\n";
continue;
}
App* thisApp = static_cast<App*>(junk->file);
if ( thisApp != 0 ) {
if(redirection)
{
vector<string> newTok;
for (int j = 0; j < redirection_index; ++j)
{
newTok.push_back(tok[j]);
}
thisApp(newTok);
fflush(stdout);
//fclose(stdout);
dup2(saved_stdout, STDOUT_FILENO);
close(saved_stdout);
//stdout = fdopen(STDOUT_FILENO, "w");
return;
}
else
{
thisApp(tok); // if possible, apply cmd to its args
return;
}
}
else {
cerr << "Instruction " << tok[0] << " not implemented.\n";
}
// If we get here, an error occurred in the child's attempt to exec.
//cerr << "myshell: " << strerror(errno) << endl; // report error.
//exit(0); // child must not return, so must die now.
}
}
/*
void action()
{
char* argv[exec_data.size()+1];
int i = 0;
for ( i = 0; i < exec_data.size(); ++i)
{
argv[i] = new char ('\0');
strcpy(argv[i], exec_data[i].c_str());
}
argv[i] = 0; // Null terminating argv for execvp
int pid;
if ((pid = fork()) < 0) { // Fork
cerr << "Fork failed.\n";
exit(1);
}
else if (pid == 0) { // Child
if (execvp(argv[0], argv) < 0) {
cerr << "Exec failed.\n";
exit(1);
}
}
else // Parent
{
//Delete all the dynamically allocated pointers for this argv
for (int i = 0 ; i < exec_data.size() ; ++i)
{
delete argv[i];
}
cerr << Me() << " has executed the command \n";
//threadGraveyard.thread_cancel(); // cancel parent thread;
}
}
*/
public:
shellThread(string name, int priority, vector<string> v)
:tok(v), Thread(name, priority)
{}
};
int doit( vector<string> tok ) {
// Executes a parsed command line returning command's exit status.
if ( tok.size() == 0 ) return 0; // nothing to be done.
string progname = tok[0];
assert( progname != "" );
// A child process can't cd for its parent.
/*
if ( progname == "cd" ) { // chdir() and return.
chdir( tok.size() > 1 ? tok[1].c_str() : getenv("HOME") );
if ( ! errno ) return 0;
cerr << "myshell: cd: " << strerror(errno) << endl;
return -1;
}
*/
// fork. And, wait if child to run in foreground.
/*if ( pid_t kidpid = fork() )
{
if ( errno || tok.back() == "&") return 0;
int temp = 0;
waitpid( kidpid, &temp, 0 );
return ( WIFEXITED(temp) ) ? WEXITSTATUS(temp) : -1;
}*/
// You're the child.
//cerr << "Thread starting\n";
shellThread thread1 ("Temp name", INT_MAX,tok);
//cerr << "thread exiting\n";
thread1.join();
//cerr << "returning\n";
return 0;
}
int main( int argc, char* argv[] ) {
///*
FSInit("info.txt");
while ( ! cin.eof() ) {
cout << "? " ; // prompt.
//testCompleteMe();
// testCompleteMe();
string temp = "";
getline( cin, temp );
cout.flush();
stringstream ss(temp); // split temp at white spaces into v.
while ( ss ) {
vector<string> v;
string s;
while ( ss >> s ) {
v.push_back(s);
if ( s == "&" || s == ";" ) break;
}
// thread t(do_work);
//cerr <<"Entering doit\n";
int status = doit( v ); // FIX make status available.
//cerr << "Exiting doit\n";
//if ( errno ) cerr << "myshell: " << strerror(errno) << endl;
}
}
//cerr << "exit" << endl;
return 0; // exit.
//*/
// testCompleteMe();
}
///////////////// Diagnostic Tools /////////////////////////
// cout.flush();
// if ( WIFEXITED(status) ) { // reports exit status of proc.
// cout << "exit status = " << WEXITSTATUS(status) << endl;
// }