-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnpshell.cpp
362 lines (307 loc) · 10.5 KB
/
npshell.cpp
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
#include <csignal>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unistd.h>
#include <utility>
#include <vector>
#include <wait.h>
using namespace std;
struct Command {
vector<string> exec_command;
bool is_error_pipe = false;
// -1: no pipe after this command; 0:oridinary pipe; >0: number of commands need to be jumped
int pipe_number;
pair<int, int> input_pipe;
pair<int, int> output_pipe;
pair<int, int> error_pipe;
bool file_redirection = false;
string filename;
};
struct Job {
int job_id;
bool is_built_in_command = false;
vector<string> built_in_command;
queue<Command> command_queue;
};
pair<int, int> create_pipe() {
// pipefd[0]: read, pipefd[1]: write
// create pipe
int pipefd[2];
int create_pipe = pipe(pipefd);
// create pipe failed
while (create_pipe < 0) {
create_pipe = pipe(pipefd);
}
pair<int, int> temp;
temp.first = pipefd[0];
temp.second = pipefd[1];
return temp;
}
pair<int, int> create_numbered_pipe(int des_job_id, map<int, pair<int, int>> &numbered_pipe_map) {
pair<int, int> temp;
if ((numbered_pipe_map.find(des_job_id) != numbered_pipe_map.end())) {
temp.first = numbered_pipe_map[des_job_id].first;
temp.second = numbered_pipe_map[des_job_id].second;
} else {
temp = create_pipe();
}
numbered_pipe_map[des_job_id] = temp;
return temp;
}
void open_pipe(int in, int out) {
dup2(in, STDIN_FILENO);
dup2(out, STDOUT_FILENO);
return;
}
void close_pipe(pair<int, int> p) {
close(p.first);
close(p.second);
return;
}
void handle_built_in_command(vector<string> tokens) {
// Terminate npshell
if (tokens[0] == "exit") {
exit(0);
} else if (tokens[0] == "setenv") {
setenv(tokens[1].c_str(), tokens[2].c_str(), 1);
} else if (tokens[0] == "printenv") {
const char *environment_variable = getenv(tokens[1].c_str());
// check the variable exists or not
if (environment_variable)
cout << environment_variable << endl;
}
}
int parse(queue<Job> &job_queue, const string &str, const char &delimiter, int job_id) {
queue<Command> command_queue;
Job job;
Command command;
vector<string> exec_command;
stringstream ss(str);
string token;
while (getline(ss, token, delimiter)) {
// built-in command arguments
if (job.is_built_in_command) {
job.built_in_command.push_back(token);
}
// First built-in command
else if (token == "setenv" || token == "printenv" || token == "exit") {
job.is_built_in_command = true;
job.built_in_command.push_back(token);
}
// pipe or numbered pipe
else if (token[0] == '|' || (token[0] == '!' && token.length() > 1)) {
// oridinary pipe = 0
if (token == "|") {
command.pipe_number = 0;
command_queue.push(command);
command = Command();
}
// numbered pipe
else if (token[0] == '|') {
const char *t = token.c_str();
command.pipe_number = atoi(t + 1);
command_queue.push(command);
command = Command();
job_id++;
job.job_id = job_id;
job.command_queue = command_queue;
command_queue = queue<Command>();
job_queue.push(job);
job = Job();
}
// error pipe
else if (token[0] == '!') {
const char *t = token.c_str();
command.pipe_number = atoi(t + 1);
command.is_error_pipe = true;
command_queue.push(command);
command = Command();
job_id++;
job.job_id = job_id;
job.command_queue = command_queue;
command_queue = queue<Command>();
job_queue.push(job);
job = Job();
}
}
// get filename
else if (command.file_redirection) {
command.filename = token;
}
// file redirection
else if (token == ">") {
command.file_redirection = true;
}
// other commands
else {
command.exec_command.push_back(token);
}
}
if (job.is_built_in_command) {
job_id++;
job.job_id = job_id;
job_queue.push(job);
job = Job();
}
// last command
else if (command.exec_command.size() != 0) {
command.pipe_number = -1; // no pipe
command_queue.push(command);
command = Command();
job_id++;
// cout << "job_id = " << job_id << endl;
job.job_id = job_id;
job.command_queue = command_queue;
// cout << command_queue.size() << endl;
command_queue = queue<Command>();
job_queue.push(job);
job = Job();
}
// while (!job_queue.empty()) {
// Job c_job = job_queue.front();
// cout << "job id = " << c_job.job_id << endl;
// ;
// while (!c_job.command_queue.empty()) {
// Command c_command = c_job.command_queue.front();
// cout<<"exec command= ";
// for(int i=0; i<c_command.exec_command.size();i++)
// cout<<c_command.exec_command[i]<<" ";
// cout << " ; pipe number = " << c_command.pipe_number << endl;
// c_job.command_queue.pop();
// }
// job_queue.pop();
// }
return job_id;
}
char **to_char_array(vector<string> input) {
char **args;
args = new char *[input.size() + 1];
for (int i = 0; i < input.size(); i++) {
args[i] = strdup((input[i]).c_str());
// if (i == 0) {
// args[i] = strdup((input[i]).c_str());
// } else {
// args[i] = strdup((input[i]).c_str());
// }
}
args[input.size()] = NULL;
return args;
}
void execute(Command command, map<int, pair<int, int>> &numbered_pipe_map) {
// fork process
pid_t pid;
while (1) {
pid = fork();
if (pid >= 0) {
break;
}
}
// child process
if (pid == 0) {
// some data pass to this command through pipe
if (command.input_pipe.first != 0) {
dup2(command.input_pipe.first, STDIN_FILENO);
close_pipe(command.input_pipe);
}
// pipe after this command
if (command.output_pipe.second != 0) {
// if there is a pipe for stderr after this command, duplicate the file descriptor
if (command.is_error_pipe)
dup2(command.output_pipe.second, STDERR_FILENO);
dup2(command.output_pipe.second, STDOUT_FILENO);
close_pipe(command.output_pipe);
}
// deallocate the file descriptors stored in numbered_pipe_map
for (map<int, pair<int, int>>::iterator it = numbered_pipe_map.begin(); it != numbered_pipe_map.end(); ++it) {
close_pipe(it->second);
}
// exec
bool redireciton = false;
string file_name = "";
char **args = to_char_array(command.exec_command);
if (command.file_redirection) {
freopen(command.filename.c_str(), "w", stdout);
}
// string command(args[0]);
if (execvp(args[0], args) == -1) {
cerr << "Unknown command: [" << args[0] << "]." << endl;
exit(0);
}
}
// parent process
else {
// if there is a input pipe, deallocate the file descriptor
if (command.input_pipe.first != 0) {
close_pipe(command.input_pipe);
}
// if there is any pipe (including ordinary pipe and numbered pipe) after the command, don't wait
if (command.pipe_number >= 0) {
signal(SIGCHLD, SIG_IGN);
} else {
wait(NULL);
}
}
return;
}
int main() {
// set initial environment variable
setenv("PATH", "bin:.", 1);
string user_input;
map<int, pair<int, int>> numbered_pipe_map;
int job_id = 0;
while (1) {
cout << "% ";
getline(cin, user_input);
if (cin.eof()) {
exit(0);
}
// parse user input
char delimiter = ' ';
queue<Job> job_queue;
job_id = parse(job_queue, user_input, delimiter, job_id);
while (!job_queue.empty()) {
Job c_job = job_queue.front();
if (c_job.is_built_in_command) {
handle_built_in_command(c_job.built_in_command);
job_queue.pop();
continue;
}
// cout << "command queue size: " << c_job.command_queue.size() << endl;
while (!c_job.command_queue.empty()) {
Command c_command = c_job.command_queue.front();
c_job.command_queue.pop();
// cout << "current execute command: " << c_command.exec_command << endl;
// oridinary pipe after this command
if (c_command.pipe_number == 0) {
// read next command
Command *n_command = &c_job.command_queue.front();
// create pipe and assign it to current command output pipe
c_command.output_pipe = create_pipe();
// assign current command output pipe to next command input pipe
n_command->input_pipe = c_command.output_pipe;
}
// numbered pipe
else if (c_command.pipe_number > 0) {
c_command.output_pipe = create_numbered_pipe(c_job.job_id + c_command.pipe_number, numbered_pipe_map);
}
// if there exists numbered piped which send data to this command
if (numbered_pipe_map.find(c_job.job_id) != numbered_pipe_map.end()) {
for (map<int, pair<int, int>>::const_iterator it = numbered_pipe_map.begin();
it != numbered_pipe_map.end(); ++it) {
}
c_command.input_pipe = numbered_pipe_map[c_job.job_id];
numbered_pipe_map.erase(c_job.job_id);
}
// if no pipe after this command, execute directly
execute(c_command, numbered_pipe_map);
}
job_queue.pop();
}
}
return 0;
}