-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell_s1086730_s1046913.cpp
412 lines (371 loc) · 11.9 KB
/
shell_s1086730_s1046913.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
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
/**
* Shell
* Operating Systems
* v20.08.28
*/
/**
Hint: Control-click on a functionname to go to the definition
Hint: Ctrl-space to auto complete functions and variables
*/
// function/class definitions you are going to use
#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/param.h>
#include <signal.h>
#include <string.h>
#include <fcntl.h>
#include <vector>
#include <array>
// although it is good habit, you don't have to type 'std' before many objects by including this line
using namespace std;
struct Command {
vector<string> parts = {};
};
struct Expression {
vector<Command> commands;
string inputFromFile;
string outputToFile;
bool background = false;
};
// Parses a string to form a vector of arguments. The seperator is a space char (' ').
vector<string> splitString(const string& str, char delimiter = ' ') {
vector<string> retval;
for (size_t pos = 0; pos < str.length(); ) {
// look for the next space
size_t found = str.find(delimiter, pos);
// if no space was found, this is the last word
if (found == string::npos) {
retval.push_back(str.substr(pos));
break;
}
// filter out consequetive spaces
if (found != pos)
retval.push_back(str.substr(pos, found-pos));
pos = found+1;
}
return retval;
}
// wrapper around the C execvp so it can be called with C++ strings (easier to work with)
// always start with the command itself
// always terminate with a NULL pointer
// DO NOT CHANGE THIS FUNCTION UNDER ANY CIRCUMSTANCE
int execvp(const vector<string>& args) {
// build argument list
const char** c_args = new const char*[args.size()+1];
for (size_t i = 0; i < args.size(); ++i) {
c_args[i] = args[i].c_str();
}
c_args[args.size()] = nullptr;
// replace current process with new process as specified
::execvp(c_args[0], const_cast<char**>(c_args));
// if we got this far, there must be an error
int retval = errno;
// in case of failure, clean up memory
delete[] c_args;
return retval;
}
// Executes a command with arguments. In case of failure, returns error code.
int executeCommand(const Command& cmd) {
auto& parts = cmd.parts;
if (parts.size() == 0)
return EINVAL;
// execute external commands
int retval = execvp(parts);
return retval;
}
void displayPrompt() {
char buffer[512];
char* dir = getcwd(buffer, sizeof(buffer));
if (dir) {
cout << "\e[32m" << dir << "\e[39m"; // the strings starting with '\e' are escape codes, that the terminal application interpets in this case as "set color to green"/"set color to default"
}
cout << "$ ";
flush(cout);
}
string requestCommandLine(bool showPrompt) {
if (showPrompt) {
displayPrompt();
}
string retval;
getline(cin, retval);
return retval;
}
// note: For such a simple shell, there is little need for a full blown parser (as in an LL or LR capable parser).
// Here, the user input can be parsed using the following approach.
// First, divide the input into the distinct commands (as they can be chained, separated by `|`).
// Next, these commands are parsed separately. The first command is checked for the `<` operator, and the last command for the `>` operator.
Expression parseCommandLine(string commandLine) {
Expression expression;
vector<string> commands = splitString(commandLine, '|');
for (size_t i = 0; i < commands.size(); ++i) {
string& line = commands[i];
vector<string> args = splitString(line, ' ');
if (i == commands.size() - 1 && args.size() > 1 && args[args.size()-1] == "&") {
expression.background = true;
args.resize(args.size()-1);
}
if (i == commands.size() - 1 && args.size() > 2 && args[args.size()-2] == ">") {
expression.outputToFile = args[args.size()-1];
args.resize(args.size()-2);
}
if (i == 0 && args.size() > 2 && args[args.size()-2] == "<") {
expression.inputFromFile = args[args.size()-1];
args.resize(args.size()-2);
}
expression.commands.push_back({args});
}
return expression;
}
int executeExpression(Expression& expression) {
// Check for empty expression
string strEmpty = "..";
if (expression.commands.size() == 0){
return EINVAL;
}
bool inputFile = !(expression.inputFromFile.empty());
bool outputFile = !(expression.outputToFile.empty());
// Handle intern commands (like 'cd' and 'exit')
for(std::vector<int>::size_type i = 0; i < expression.commands.size(); i++){
for (std::vector<int>::size_type j = 0; j < expression.commands[i].parts.size(); j++){
if (expression.commands[i].parts[j] == "exit"){
//ckecks for an exit command
printf("Exit successfull!\n");
exit(0);
}
else if (expression.commands[i].parts[j] == "cd"){
//change the directory acordingly
int ch = chdir(expression.commands[i].parts[j+1].c_str());
//and gives an error if the directory is not found.
if (ch < 0){
printf("Error when changing directory\n");
}
if(expression.commands[i].parts[j+1] == strEmpty){
chdir("..");
}
}
}
}
// External commands, executed with fork():
// create a list of 2 file descriptors. Iterate through number of pipes an create pairs with pipe(). Add them to pipesRead and pipesWrite vectors.
int ends[2];
std::vector<int> pipesRead;
std::vector<int> pipesWrite;
for (std::vector<int>::size_type i = 0; i < expression.commands.size() - 1; i++) {
if (pipe(ends) == -1){
printf("error when opening a pipe");
return 1;
} else {
//printf("LINE 168");
pipesRead.push_back(ends[0]);
pipesWrite.push_back(ends[1]);
}
}
//create list of process ids for each command.
std::vector<pid_t> children;
pid_t process;
if (expression.commands.size() >= 2){
for (std::vector<int>::size_type i = 0; i < expression.commands.size(); i++) {
process = fork();
if (process > 0) { //in parent process, add remaining children to children list.
children.push_back(process);
} else if (process == 0) {
if (i == 0) {
if (inputFile) {
int input = open(expression.inputFromFile.c_str(), O_RDWR | O_CREAT);
if (input == -1) {
fprintf(stderr, "File could not open.");
}
else {
dup2(input, STDIN_FILENO);
close(input);
}
}
//printf("line 200");
close(pipesRead[0]);
dup2(pipesWrite[0], STDOUT_FILENO);
close(pipesWrite[0]);
executeCommand(expression.commands[0]);
}
else if (i == expression.commands.size() - 1) {
if (outputFile) {
int output = open(expression.outputToFile.c_str(), O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
if (output == -1) {
fprintf(stderr, "File could not open.");
}
else {
dup2(output, STDOUT_FILENO);
close(output);
}
}
close(pipesWrite[i-1]);
dup2(pipesRead[i-1], STDIN_FILENO);
close(pipesRead[i-1]);
executeCommand(expression.commands[i]);
}
else {
dup2(pipesRead[i-1], STDIN_FILENO);
close(pipesRead[i-1]);
dup2(pipesWrite[i], STDOUT_FILENO);
close(pipesWrite[i]);
executeCommand(expression.commands[i]);
}
}
}
} else {
if (inputFile) {
int input = open(expression.inputFromFile.c_str(), O_RDWR | O_CREAT);
if (input == -1) {
fprintf(stderr, "File could not open.");
}
else {
dup2(input, STDIN_FILENO);
close(input);
}
}
if (outputFile) {
int output = open(expression.outputToFile.c_str(), O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
if (output == -1) {
fprintf(stderr, "File could not open.");
}
else {
dup2(output, STDOUT_FILENO);
close(output);
}
}
process = fork();
if (process == 0) {
executeCommand(expression.commands[0]);
}
else {
waitpid(process, nullptr, 0);
}
return 0; //if only one command, execute it and return
}
for (std::vector<int>::size_type i = 0; i < expression.commands.size() - 1; i++) {
close(pipesRead[i]);
close(pipesWrite[i]);
}
if(!expression.background){
for(std::vector<int>::size_type i = 0; i < expression.commands.size(); i++){
waitpid(children[i], NULL, 0);
}
}
return 0;
// Loop over all commandos, and connect the output and input of the forked processes
//Iterate through children processes. Only redirect output for first child and only iput for last child.
/*for (std::vector<int>::size_type i = 0; i < children.size() - 1; i++) {
pid_t processID;
processID=getpid();
if (processID == children[i]) {
//printf("line 210");
dup2(pipesRead[i], STDIN_FILENO);
close(pipesRead[i]);
dup2(pipesWrite[i], STDOUT_FILENO);
close(pipesWrite[i]);
waitpid(children[i-1], NULL, 0);
executeCommand(expression.commands[i]);
}
}
if (getpid() == children[children.size() - 1]) {
//printf("line 221 last");
int n = children.size() - 1; // last child
dup2(pipesRead[n], STDIN_FILENO);
close(pipesWrite[n]);
close(pipesWrite[n]);
waitpid(children[children.size()-2], NULL, 0);
executeCommand(expression.commands[expression.commands.size()-1]);
}
*/
//for(std::vector<int>::size_type i = 0; i < expression.commands.size(); i++){
// for (std::vector<int>::size_type j = 0; j < expression.commands[i].parts.size(); j++){
// if(pid == 0){
// //child process
// dup2(fd[1],STDOUT_FILENO);
// close(fd[0]);
// close(fd[1]);
// executeCommand(expression.commands[i]);
// }
// else {
// //parent process
// wait(NULL);
// dup2(fd[0], STDIN_FILENO);
// close(fd[0]);
// close(fd[1]);
// executeCommand(expression.commands[j]);
// }
// }
//}
// For now, we just execute the first command in the expression. Disable.
// executeCommand(expression.commands[0]);
}
int normal(bool showPrompt) {
while (cin.good()) {
string commandLine = requestCommandLine(showPrompt);
Expression expression = parseCommandLine(commandLine);
int rc = executeExpression(expression);
if (rc != 0)
cerr << strerror(rc) << endl;
}
return 0;
}
// framework for executing "date | tail -c 5" using raw commands
// two processes are created, and connected to each other
int step1(bool showPrompt) {
// create communication channel shared between the two processes
// ...
int fd[2];
if (pipe(fd) == -1){
printf("error when opening the pipe");
return 1;
}
pid_t child1 = fork();
if(child1 < 0){
printf("Error when forking child 1");
return 2;
}
if (child1 == 0) {
// redirect standard output (STDOUT_FILENO) to the input of the shared communication channel
dup2(fd[1],STDOUT_FILENO);
close(fd[0]);
close(fd[1]);
// free non used resources (why?)
Command cmd = {{string("date")}};
executeCommand(cmd);
// display nice warning that the executable could not be found
if (executeCommand(cmd) == -1){
printf("Error,executable not found");
return 3;
}
abort(); // if the executable is not found, we should abort. (why?) -- because the pipe will have no input to take in.
}
pid_t child2 = fork();
if (child2 == 0) {
// redirect the output of the shared communication channel to the standard input (STDIN_FILENO).
dup2(fd[0], STDIN_FILENO);
close(fd[0]);
close(fd[1]);
// free non used resources (why?) -- if we don't close the pipes after we're done with them the processes won't know when to stop reading/writring
Command cmd = {{string("tail"), string("-c"), string("5")}};
executeCommand(cmd);
abort(); // if the executable is not found, we should abort. (why?)
}
// free non used resources (why?)
//if we don't close the main pipe, the child2 process will not know when to stop reading, because the pipe from the main process is not closed
close(fd[0]);
close(fd[1]);
// wait on child processes to finish (why both?)
waitpid(child1, nullptr, 0);
waitpid(child2, nullptr, 0);
return 0;
}
int shell(bool showPrompt) {
return normal(showPrompt);
//return step1(showPrompt);
if(step1(showPrompt) !=0){
printf("Step 1 error");
}
}