-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyshell.cpp
More file actions
424 lines (415 loc) · 12.7 KB
/
myshell.cpp
File metadata and controls
424 lines (415 loc) · 12.7 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
#include "myshell.h"
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
void MyShell::type_prompt() { //print standarf format: ffosh:xxx$
char * pathname = getcwd(NULL, 0);
printf("ffosh:%s$ ", pathname);
free(pathname);
}
void MyShell::read_command() { //read command from cin
std::getline(cin, command);
}
vector<string> MyShell::
pause_command() { //split the command into few arguments, taking care of '\' and '"'
char * input = const_cast<char *>(command.c_str());
char c;
int i = 0;
string str;
vector<string> vec;
int counter = 0;
while (input[i] == ' ') { //omit all the white space in front of the first argument
i++;
}
if (input[i] == '\0') { //case: command only has white space
vec.push_back("");
return vec;
}
while (input[i] != '\0') {
if (input[i] == '"') { //case:the fist character is '"'
counter++;
while (input[++i] != '\0') {
if (input[i] == '\\') { //encounter '\'
c = input[++i];
str.push_back(c);
}
else {
if (input[i] == '"') { //the corresponding '"' is met
counter++;
vec.push_back(str);
str = "";
i = i + 1;
while (input[i] == ' ') { //omit white space until we reach the next argument
++i;
}
break;
}
else {
str.push_back(input[i]);
}
}
}
}
else { //case:the first character of the argument is not "
if (input[i] == '\\') { //encounter '\'
i++;
}
str.push_back(
input[i]); //push the fist character is not white space and not '\' into str
while (input[i] != '\0' && input[++i] != '\0') {
if (input[i] == '"') { //encounter '"' inside the argument
counter++;
if (str != "") {
vec.push_back(str);
str = ""; //clear str
}
while (input[++i] != '\0') {
if (input[i] == '\\') { //encounter '\'
c = input[++i];
}
else {
if (input[i] == '"') { //the corresponding '"' is met
counter++;
vec.push_back(str);
str = "";
while (input[i + 1] == ' ') { //omit white space until the next argument
i++;
}
break;
}
else {
str.push_back(input[i]);
}
}
}
}
else if (input[i] == ' ') { //encounter white space outside ""
vec.push_back(str);
str = "";
while (input[i] == ' ') {
i++;
}
break;
}
else {
if (input[i] == '\\') { // encounter '\'
i = i + 1;
}
str.push_back(input[i]);
}
}
}
}
if (counter % 2 != 0) { //having unclosed quotation in the arguments
cerr << "unclosed quotation" << endl;
command.clear();
command.shrink_to_fit();
vec.clear();
str.clear();
vec.shrink_to_fit();
str.shrink_to_fit();
}
if (command.size() == 0 || str != "") {
vec.push_back(str);
}
return vec;
}
int MyShell::find_dollar(
string & s) { //return $'s index in the string, if there is no $,return -1
int pos = -1;
for (size_t i = 0; i < s.size(); i++) {
if (s[i] == '$') {
pos = i;
break;
}
}
return pos;
}
vector<string> MyShell::pause_dollar(string & str) { //split the str by '$'
vector<string> res;
stringstream input(str);
string temp;
while (getline(input, temp, '$')) {
res.push_back(temp);
}
return res;
}
vector<string> MyShell::
replace_dollar() { //if there's $ in the argument, repalce it with the value in map
read_command();
vector<string> input = pause_command();
for (size_t i = 0; i < input.size(); i++) {
int pos = find_dollar(input[i]);
if (pos == -1) {
continue;
} //case: no $ in the argument,continue to the next argument
string str1 = input[i].substr(0, pos);
string str2 = input[i].substr(pos);
vector<string> res = pause_dollar(str2);
str2 = "";
for (size_t j = 1; j < res.size(); j++) {
size_t k = 0;
while (res[j][k] == '_' || isdigit(res[j][k]) || isalpha(res[j][k])) {
k++;
} //read legal characters to find key
string key = res[j].substr(0, k);
string value = v.find_key(key); //find the value in map
str2 = str2 + value + res[j].substr(k);
}
input[i] = str1 + str2; //replace the legal string after $
}
return input;
}
void MyShell::handle_command() { //handle the command
pid_t cpid, w;
int wstatus;
vector<char *>
env; //a temp vector for converting private member environmnet's type to char**
string env_path_name;
string env_value;
int env_index = 0;
while (environ[env_index] != NULL) { //put all environ into the environment field
string str(environ[env_index]);
env_index++;
environment.push_back(str);
str.clear();
}
environment.push_back(e.add_newenvp()); //add ece551path into environmnet
while (true) {
if (cin.eof()) { //case: CONTROL+D, need to free all things
env_path_name.clear();
env_path_name.shrink_to_fit();
env_value.clear();
env_value.shrink_to_fit();
env.clear();
delete_privatefields();
env.shrink_to_fit();
cout << "process finsh!" << endl;
exit(EXIT_SUCCESS);
}
type_prompt(); //print out "ffosh...$ "
vector<string> vec = replace_dollar();
int len = vec.size();
char ** newargv = new char *[len + 1]; //make space for newargv
if (vec[0] == "exit") { //case:exit, need to free all things
vec.clear();
vec.shrink_to_fit();
env_path_name.clear();
env_path_name.shrink_to_fit();
env_value.clear();
env_value.shrink_to_fit();
env.clear();
delete_privatefields();
env.shrink_to_fit();
delete[] newargv;
exit(EXIT_SUCCESS);
}
else if (vec[0] == "") { //case:\n
delete[] newargv;
continue;
}
else if (vec[0] == "set") { //case:set
delete[] newargv;
if (vec.size() < 3) { //need at least three arguments
cerr << "wrong input format!" << endl;
continue;
}
size_t i;
for (i = 0; i < vec[1].size(); i++) { //the key_name need to be legal
if (!(isalnum(vec[1][i]) || vec[1][i] == '_')) {
cerr << "wrong key format!" << endl;
break;
}
}
if (i != vec[1].size()) {
continue;
}
string set_value;
size_t sec_blank = 0;
int white_space = 0;
for (sec_blank = 0; sec_blank < command.size(); sec_blank++) {
if (command[sec_blank] == ' ') {
white_space++;
}
if (white_space == 2) { //find the second white space in the command
break;
}
}
set_value = command.substr(
sec_blank +
1); //put all the following string into the key's corresponding value
int p = -1;
if ((p = set_value.find(vec[1])) != -1 || command[0] != 's') {
cerr << "You should only type in one white space between set and key name,and no "
"white space before set!"
<< endl;
continue;
} //if the input format of set is mot met, should print this message
v.add_element(vec[1], set_value);
set_value.clear();
}
else if (vec[0] == "rev") { //case:rev
delete[] newargv;
if (vec.size() != 2) {
cerr << "wrong input format!" << endl;
continue;
}
size_t i;
for (i = 0; i < vec[1].size(); i++) {
if (!(isalnum(vec[1][i]) || vec[1][i] == '_')) {
cerr << "wrong key format!" << endl;
break;
}
}
if (i != vec[1].size()) {
continue;
}
v.rev_element(vec[1]);
}
else if (vec[0] == "export") { //case:export
delete[] newargv;
if (vec.size() != 2) {
cerr << "wrong input format!" << endl;
continue;
}
size_t ii;
for (ii = 0; ii < vec[1].size(); ii++) {
if (!(isalnum(vec[1][ii]) || vec[1][ii] == '_')) {
cerr << "wrong key format!" << endl;
break;
}
}
if (ii != vec[1].size()) {
continue;
}
string env_add = v.export_element(vec[1]);
size_t i;
for (i = 1; i < environment.size(); i++) {
size_t pos = environment[i].find("=");
env_path_name = environment[i].substr(0, pos);
if (vec[1] ==
env_path_name) { //if find the exist environment path name, just change the existing path
environment[i] = env_add;
break;
}
}
if (i == environment.size()) { //if not find, add another path
environment.push_back(env_add);
}
}
else if (vec[0] == "cd") { //case:cd
if (vec.size() == 1) { //cd without other argument
chdir("/home/ys270");
delete[] newargv;
}
else { //cd with other argument
int a = chdir(vec[1].c_str());
if (a == -1) {
perror("chdir");
delete_privatefields();
}
delete[] newargv;
}
}
else { //normal cases
cpid = fork();
if (cpid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
else if (cpid == 0) { //child process
if (vec[0][0] == '/' || (vec[0][0] == '.' && vec[0][1] == '/') ||
(vec[0][0] == '.' && vec[0][1] == '/' &&
vec[0][2] == '/')) { //if the user type in absolute path
char * filename = const_cast<char *>(vec[0].c_str());
for (int i = 0; i < len; i++) {
newargv[i] = const_cast<char *>(vec[i].c_str());
}
newargv[len] = NULL;
char * envp[] = {NULL};
execve(filename, newargv, envp);
cerr << "execve error" << endl;
delete[] newargv;
command.clear();
vec.clear();
vec.shrink_to_fit();
exit(EXIT_FAILURE);
}
else { //if the user did not type in the full path
for (size_t i = 0; i < environment.size(); i++) {
env.push_back(const_cast<char *>(environment[i].c_str()));
} //cast environment into env for type change
env.push_back(NULL);
char ** envp = env.data(); //envp's type is char** and can be put into execve
size_t ece551_i;
for (ece551_i = 0; ece551_i < environment.size(); ece551_i++) {
int pos = -1;
pos = environment[ece551_i].find("ECE551PATH");
if (pos != -1) {
break;
}
}
int pos = environment[ece551_i].find("=");
env_value = environment[ece551_i].substr(pos + 1);
vector<string> input = e.make_full_path(
env_value,
vec[0]); //search if there is matching path for the command
for (size_t i = 0; i < input.size(); i++) {
char * filename = const_cast<char *>((input[i]).c_str());
newargv[0] = filename;
for (int i = 1; i < len; i++) {
newargv[i] = const_cast<char *>(vec[i].c_str());
}
newargv[len] = NULL;
execve(filename,
newargv,
envp); //if any of the path matches,the program can execve
}
delete[] newargv;
cerr << "Command " << vec[0] << " not found"
<< endl; //if the command is not find
command.clear();
vec.clear();
vec.shrink_to_fit();
exit(EXIT_FAILURE);
}
}
else { //parent process
do {
delete[] newargv;
w = waitpid(-1, &wstatus, WUNTRACED | WCONTINUED);
if (w == -1) {
perror("waitpid");
exit(EXIT_FAILURE);
}
if (WIFEXITED(wstatus)) {
if (WEXITSTATUS(wstatus) == 0) {
printf("Program was successful\n");
}
else {
printf("Program failed with code %d\n", WEXITSTATUS(wstatus));
}
}
else if (WIFSIGNALED(wstatus)) {
printf("Terminated by signal %d\n", WTERMSIG(wstatus));
}
} while (!WIFEXITED(wstatus) && !WIFSIGNALED(wstatus));
}
}
}
}
void MyShell::
delete_privatefields() { //when exit or encouter eof, need to clear private fields
command.clear();
command.shrink_to_fit();
v.delete_all();
environment.clear();
environment.shrink_to_fit();
}