-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.cpp
More file actions
498 lines (431 loc) · 13.1 KB
/
Copy pathshell.cpp
File metadata and controls
498 lines (431 loc) · 13.1 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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#include <iostream>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <fcntl.h>
#include <termios.h>
#include <dirent.h>
#include <vector>
#include <string>
#include <sstream>
#include "Tokenizer.h"
// all the basic colours for a shell prompt
#define RED "\033[1;31m"
#define GREEN "\033[1;32m"
#define YELLOW "\033[1;33m"
#define BLUE "\033[1;34m"
#define WHITE "\033[1;37m"
#define NC "\033[0m"
#define INPUT_FD 0
#define OUTPUT_FD 1
#define MIN(A, B) ((A) < (B) ? (A) : (B))
#define DUP_IF_DIFFERENT(fd, orig_fd) \
{ \
if (fd != orig_fd) \
{ \
cout << "Duping " << fd << " orig " << orig_fd << endl; \
dup2(fd, orig_fd); \
} \
}
using namespace std;
void set_raw_mode()
{
struct termios t;
tcgetattr(STDIN_FILENO, &t); // get the current terminal I/O structure
t.c_lflag &= ~ICANON; // Manipulate the flag bits to do what you want it to do
tcsetattr(STDIN_FILENO, TCSANOW, &t); // Apply the new settings
}
class Entry
{
bool is_dir;
string name;
public:
Entry(bool is_dir, string name) : is_dir(is_dir), name(name){};
bool is_directory() const { return is_dir; };
string to_string() const { return name; };
};
vector<Entry>
directory_iterator(const string path)
{
DIR *d;
struct dirent *dir;
vector<Entry> entries;
d = opendir(path.c_str());
if (d)
{
while ((dir = readdir(d)) != NULL)
{
if (strcmp(dir->d_name, ".") != 0 && strcmp(dir->d_name, "..") != 0)
entries.push_back(Entry(dir->d_type == DT_DIR, path + string(dir->d_name)));
}
closedir(d);
}
return entries;
}
vector<string> autocomplete(string word, bool first)
{
vector<string> result;
if (word.rfind("./", 0) == 0 || word.rfind("/", 0) == 0 || !first)
{
// Local search
if (word == "")
word = "./";
size_t s = word.rfind("/", word.size());
bool remove_prefix = false;
string directory;
if (s != string::npos)
directory = word.substr(0, s + 1);
else
{
directory = "./";
remove_prefix = true;
}
for (const auto &entry : directory_iterator(directory))
{
string e = entry.to_string();
if (remove_prefix)
e = e.substr(2);
if (e.rfind(word, 0) == 0)
{
if (entry.is_directory())
e += '/';
result.push_back(e);
}
}
}
else
{
// Search in PATH environment variable
stringstream path = stringstream(string(getenv("PATH")));
string s;
while (getline(path, s, ':'))
{
for (const auto &entry_v : directory_iterator(s))
{
// Get section after last /
string entry = entry_v.to_string();
size_t s = entry.rfind("/", entry.size());
string v = entry.substr(s + 1);
if (v.rfind(word, 0) == 0)
result.push_back(v);
}
}
}
return result;
}
void print_prompt()
{
char buf[1024];
char wd[1024];
time_t now = time(NULL);
struct tm *t = localtime(&now);
strftime(buf, sizeof(buf) - 1, "%b %d %H:%M:%S", t);
cout << WHITE << buf << " " << YELLOW << getenv("USER") << ":" << BLUE << getcwd(wd, 1024) << GREEN << "$ " << NC;
}
void clear_text(size_t i)
{
while (i > 0)
{
printf("\b \b");
i--;
}
}
string handle_input(const vector<string> &history)
{
string res;
char c;
size_t current_command = history.size();
while ((c = getchar()) != '\n')
{
if (c == 27)
{
// ESC key, likely arrow keys
char a, b;
a = getchar();
b = getchar();
if (a == 91 && b == 65)
{
// Up arrow
clear_text(res.size() + 4);
if (current_command > 0)
current_command--;
if (history.size() > 0)
// If there is any, update the command
res = history[current_command];
cout << res;
}
else if (a == 91 && b == 66)
{
// Down arrow
clear_text(res.size() + 4);
current_command = MIN(current_command + 1, history.size() + 1);
if (current_command < history.size())
{
res = history[current_command];
cout << res;
}
else
{
// Default case, no text
res = "";
}
}
else
{
res += c;
res += a;
res += b;
}
}
else if (c == '\t')
{
// Autocomplete
// The idea is that the terminal should try to find a possible solution for the current word
string last_word;
size_t s;
bool first_word = false;
if ((s = res.rfind(' ')) != string::npos)
{
last_word = res.substr(s + 1);
}
else
{
last_word = res;
first_word = true;
}
// Get possible names
vector<string> completions = autocomplete(last_word, first_word);
if (completions.size() == 1)
{
if (last_word != completions[0])
{
res += completions[0].substr(last_word.size());
cout << endl;
print_prompt();
cout << res;
}
}
else if (completions.size() != 0)
{
cout << endl;
for (const auto &v : completions)
{
cout << v << " ";
}
cout << endl;
print_prompt();
cout << res;
}
}
else if (c == 127)
{
// Remove the backspace character from the terminal window
printf("\b\b \b\b");
// Backspace
if (res.size() > 0)
{
// Remove the character if it exists
res.pop_back();
printf("\b \b");
}
}
else
{
res += c;
}
}
return res;
}
int main()
{
char *wd = (char *)malloc(sizeof(char) * 1024);
char *last_directory = (char *)malloc(sizeof(char) * 1024);
last_directory[0] = 0;
bool last_dir_exists = false;
pid_t wpid;
int status = 0;
vector<string> history;
set_raw_mode();
for (;;)
{
// Wait for all children to remove zombie children
while ((wpid = waitpid(-1, &status, WNOHANG)) > 0)
{
if (status > 1)
{ // exit if child didn't exec properly
cout << "Error in child" << endl;
exit(status);
}
}
getcwd(wd, 1024);
print_prompt();
// get user inputted command
// string input;
// getline(cin, input);
string input = handle_input(history);
if (input == "exit" || cin.eof())
{ // print exit message and break out of infinite loop
break;
}
history.push_back(input);
// get tokenized commands from user input
Tokenizer tknr(input);
if (tknr.hasError())
{ // continue to next prompt if input had an error
continue;
}
// Keep track of the input and outputs to be used
int new_fds[2], old_fds[2];
for (size_t i = 0; i < tknr.commands.size(); i++)
{
auto cmd = tknr.commands[i];
// Handle specific situations: pwd, cd
if (cmd->args[0] == "cd")
{
string dst;
if (cmd->args[1][0] == '/')
{
dst = string(cmd->args[1]);
strcpy(last_directory, wd);
}
else if (cmd->args[1][0] == '-')
{
if (!last_dir_exists)
{
cout << "No previous directory available." << endl;
continue;
}
// swap directory with last directory
char *tmp = last_directory;
last_directory = wd;
wd = tmp;
cmd->args[1] = tmp;
}
else
{
dst += string(wd);
dst += "/";
dst += string(cmd->args[1]);
strcpy(last_directory, wd);
}
last_dir_exists = true;
chdir(cmd->args[1].c_str());
continue;
}
else if (cmd->args[0] == "pwd")
{
cout << wd << endl;
continue;
}
// if it's not the last command, create a pipe
if (i != tknr.commands.size() - 1)
{
// Create a pipe
if (pipe(new_fds) == -1)
{
cout << "Failed to create pipe" << endl;
exit(1);
}
}
// fork to create child
pid_t pid = fork();
if (pid < 0)
{ // error check
perror("fork");
exit(2);
}
if (pid == 0)
{ // if child, exec to run command
// run single commands with no arguments
std::vector<char *> arguments;
for (auto s : cmd->args)
{
arguments.push_back(strdup(s.c_str()));
}
arguments.push_back(nullptr);
char **args = arguments.data();
// If there is a previous command
if (i != 0)
{
// Get input from pipe to stdin
dup2(old_fds[0], 0);
close(old_fds[0]);
// Close output side
close(old_fds[1]);
}
// If there is a next command
if (i != tknr.commands.size() - 1)
{
// Close input side
close(new_fds[0]);
// Get output from pipe to stdout
dup2(new_fds[1], 1);
close(new_fds[1]);
}
// Check if input/output redirection
if (cmd->hasInput())
{
// Open the input file
int fd = open(cmd->in_file.c_str(), O_RDONLY);
if (fd < 0)
{
cerr << "Failed to open in file" << endl;
exit(1);
}
close(INPUT_FD);
dup(fd);
close(fd);
}
if (cmd->hasOutput())
{
int fd = open(cmd->out_file.c_str(), (O_RDWR | O_CREAT), 0777);
if (fd < 0)
{
cerr << "Failed to open output file " << endl;
exit(1);
}
close(OUTPUT_FD);
dup(fd);
close(fd);
}
if (execvp(args[0], args) < 0)
{ // error check
perror("execvp");
exit(2);
}
}
else
{ // if parent, wait for child to finish
// if there is a previous command
if (i != 0)
{
// close the previous pipe
close(old_fds[0]);
close(old_fds[1]);
}
if (i != tknr.commands.size() - 1)
{
old_fds[0] = new_fds[0];
old_fds[1] = new_fds[1];
}
int status = 0;
if (!cmd->isBackground() && i == tknr.commands.size() - 1)
waitpid(pid, &status, 0);
if (status > 1)
{ // exit if child didn't exec properly
exit(status);
}
}
}
// Clean remaining
if (tknr.commands.size() != 1)
{
close(old_fds[0]);
close(old_fds[1]);
}
}
free(wd);
free(last_directory);
}