-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
588 lines (566 loc) · 12.9 KB
/
main.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
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <dirent.h>
#include <termios.h>
#include <unistd.h>
#include <locale.h>
#include <langinfo.h>
#include <time.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <stack>
#include <vector>
#include "commandList.h"
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
#define PLATFORM_NAME "windows"
#elif __APPLE__
#define PLATFORM_NAME "mac"
#elif __linux__
#define PLATFORM_NAME "unix"
#elif __unix__ // all unices not caught above
#define PLATFORM_NAME "unix"
#else
# #define PLATFORM_NAME "other"
#endif
using namespace std;
const int KEY_ESCAPE = 0x001b;
const int KEY_ENTER = 0x000a;
const int KEY_UP = 0x0105;
const int KEY_DOWN = 0x0106;
const int KEY_LEFT = 0x0107;
const int KEY_RIGHT = 0x0108;
const int COMMAND_MODE = 2;
const int NORMAL_MODE = 1;
const int SCROLL_CONSTANT = 5;
const string TYPE_FILE = "file";
const string TYPE_DIRECTORY = "dir";
char ROOT_PATH[4096];
int mode = NORMAL_MODE;
int CURRENT_ROW;
char currentPath[4096];
char inputBuffer[4096];
int ibc = 0;
struct termios old_tio, new_tio;
struct winsize WindowSize;
vector<string> prevBackDir;
vector<string> prevFwdDir;
vector<dirent *> listOfDirs;
vector<string> typeFileOrDir;
unsigned int xC = 1; //x co-ord for cursor
unsigned int yC = 1; //y co-ord for cursor
unsigned int term_row;
unsigned int term_col;
int currList = 0;
void listDirCurrentPath(char const *);
void navigateCommandMode(vector<string>);
void displayAtIndex(int);
int secondChar()
{
int c = 0;
tcgetattr(0, &old_tio);
memcpy(&new_tio, &old_tio, sizeof(new_tio));
new_tio.c_lflag &= ~(ICANON | ECHO);
new_tio.c_cc[VMIN] = 1;
new_tio.c_cc[VTIME] = 0;
tcsetattr(0, TCSANOW, &new_tio);
c = getchar();
if (c == EOF)
clearerr(stdin);
//printf("Returning %d\n",c);
tcsetattr(0, TCSANOW, &old_tio);
return c;
}
void clearScreen()
{
printf("\033[H\033[J");
}
int switchMode()
{
int c = 0;
tcgetattr(0, &old_tio);
memcpy(&new_tio, &old_tio, sizeof(new_tio));
new_tio.c_lflag &= ~(ICANON | ECHO);
new_tio.c_cc[VMIN] = 0;
new_tio.c_cc[VTIME] = 1;
tcsetattr(0, TCSANOW, &new_tio);
c = getchar();
tcsetattr(0, TCSANOW, &old_tio);
if (c != -1 && c != EOF)
ungetc(c, stdin);
return ((c != -1) ? 1 : 0);
}
int hitESC()
{
int c;
if (!switchMode())
return KEY_ESCAPE;
c = secondChar();
if (c == '[')
{
switch (secondChar())
{
case 'A':
c = KEY_UP;
break;
case 'B':
c = KEY_DOWN;
break;
case 'C':
c = KEY_RIGHT;
break;
case 'D':
c = KEY_LEFT;
break;
default:
c = 0;
break;
}
}
else
c = 0;
if (c == 0)
while (switchMode())
secondChar();
return c;
}
int firstChar()
{
int c;
c = secondChar();
//printf("Esc is %d got back is %d",KEY_ESCAPE,c);
return (c == KEY_ESCAPE) ? hitESC() : c;
}
void performOp(int x)
{
string fName, p;
dirent *dir;
switch (x)
{
case 1: //key right
if (prevFwdDir.size() == 0)
break;
prevBackDir.push_back(string(currentPath));
//strcpy(currentPath, prevFwdDir.back().c_str());
p = prevFwdDir.back();
prevFwdDir.pop_back();
listDirCurrentPath(p.c_str());
break;
case 2: //key left
if (prevBackDir.size() == 0)
break;
prevFwdDir.push_back(string(currentPath));
//strcpy(currentPath, prevBackDir.back().c_str());
p = prevBackDir.back();
prevBackDir.pop_back();
listDirCurrentPath(p.c_str());
break;
case 3: //key UP
//printf("\033M");
//break;
CURRENT_ROW = max(CURRENT_ROW - 1, 1);
if (yC > 1)
{
yC--;
if (CURRENT_ROW != 1)
printf("\033[%dA", 1); //move cursor up
else
{
printf("\033M"); //scroll up
printf("\33[2K"); //clear line
displayAtIndex(yC - 1);
cout << "\033[" << 1 << ";" << 1 << "H";
}
}
break;
case 4: //key down
CURRENT_ROW = min(CURRENT_ROW + 1, (int)WindowSize.ws_row);
if (yC < min(currList, (int)WindowSize.ws_row))
{
yC++;
printf("\033[%dB", 1);
}
else if (yC < currList)
{
printf("\n");
printf("\033[%dB", 1);
yC++;
displayAtIndex(yC - 1);
cout << "\033[" << yC << ";" << 1 << "H";
}
break;
case 5: //key home
if (strcmp(currentPath, ROOT_PATH) == 0)
break;
prevBackDir.push_back(string(currentPath));
//strcpy(currentPath, ROOT_PATH);
prevFwdDir.clear();
listDirCurrentPath(ROOT_PATH);
break;
case 6: //key up one level (parent DIR) (backspace)
if (strcmp(currentPath, ROOT_PATH) == 0)
break;
prevBackDir.push_back(string(currentPath));
prevFwdDir.clear();
//strcpy(currentPath, (string(currentPath).substr(0, string(currentPath).find_last_of('/'))).c_str());
listDirCurrentPath("../");
break;
case 7: //key ENTER
if (yC == currList + 1)
break;
dir = listOfDirs[yC - 1];
fName = string(dir->d_name);
if (fName == "." || fName == "..")
{
if (strcmp(currentPath, ROOT_PATH) == 0)
break;
if (fName == ".") //not sure
break; //performOp(2);
else
performOp(6);
}
else if (typeFileOrDir[yC - 1] == TYPE_DIRECTORY)
{
prevBackDir.push_back(string(currentPath));
//strcpy(currentPath, (string(currentPath) + "/" + fName).c_str());
listDirCurrentPath((string(currentPath) + "/" + fName).c_str());
}
else if (typeFileOrDir[yC - 1] == TYPE_FILE)
{
clearScreen();
string cc;
if (PLATFORM_NAME == "unix")
cc = "/usr/bin/vi"; //xdg-open for opening with associated program
else if (PLATFORM_NAME == "mac")
cc = "/usr/bin/vi"; //open for opening with associated program
if (PLATFORM_NAME == "windows")
cc = "start";
//execl(cc.c_str(), cc.c_str(), fName.c_str(), NULL);
pid_t pid;
if ((pid = fork()) == -1)
printf("fork error");
else if (pid == 0)
{
execl(cc.c_str(), cc.c_str(), fName.c_str(), NULL); //open each file in def. appl.
//exit(1);
printf("execl() error\n");
}
else
{
wait(NULL);
listDirCurrentPath(currentPath);
}
}
break;
case 8: //key command mode
clearScreen();
printf("\033[%d;%dH", WindowSize.ws_row, 1);//move to last line
printf("copy\tmove\trename\tcreate_file\tcreate_dir\tdelete_file\tdelete_dir\tgoto\tsearch\t\tESC");
printf("\033[%d;%dH", 1, 1);//reposition cursor
break;
}
}
vector<string> getTokens(string s)
{
vector<string> x;
string t;
stringstream iss(s);
while (getline(iss, t, ' '))
x.push_back(t);
return x;
}
void navigateNormalMode(int c)
{
if (c == KEY_ESCAPE) //go to normal mode;
{
if (mode == COMMAND_MODE)
{
mode = NORMAL_MODE;
ibc = 0;
listDirCurrentPath(currentPath);
}
}
else if (c == KEY_RIGHT)
{
if (mode == NORMAL_MODE)
performOp(1);
//else printf("\033[%dC", 1);
}
else if (c == KEY_LEFT)
{
if (mode == NORMAL_MODE)
performOp(2);
//else printf("\033[%dD", 1);
}
else if (c == KEY_UP)
{
if (mode == NORMAL_MODE)
performOp(3);
//else printf("\033[%dA", 1);
}
else if (c == KEY_DOWN)
{
if (mode == NORMAL_MODE)
performOp(4);
//else printf("\033[%dB", 1);
}
else if ((c == 'H' || c == 'h') && mode == NORMAL_MODE) //go to home
{
performOp(5);
}
else if (c == 127) //backspace = up one level
{
if (mode == NORMAL_MODE)
performOp(6);
else
{
printf("\b \b");
ibc = max(0, ibc - 1);
}
}
else if (c == KEY_ENTER) //enter - execute file or enter dir
{
if (mode == NORMAL_MODE)
performOp(7);
else if (ibc > 0)
{
string x = "";
//getline(cin, x);
for (int i = 0; i < ibc; i++)
x += inputBuffer[i];
//cout << "\ntaking " << x << " as input\n";
ibc = 0;
printf("\n");
navigateCommandMode(getTokens(x));
}
}
else if ((c == 'k' || c == 'K') && mode == NORMAL_MODE) //scroll up
{
for (int i = 1; i <= SCROLL_CONSTANT; i++)
performOp(3);
}
else if ((c == 'l' || c == 'L') && mode == NORMAL_MODE) //scroll down
{
for (int i = 1; i <= SCROLL_CONSTANT; i++)
performOp(4);
}
else if (c == ':' && mode == NORMAL_MODE) //command mode
{
mode = COMMAND_MODE;
ibc = 0;
clearScreen();
performOp(8);
}
else if ((c == 'q' || c == 'Q') && mode == NORMAL_MODE) //quit application
{
clearScreen();
exit(1);
}
else if (c == EOF) //unrecoverable error
{
//cout << "Unrecoverable error\n";
//exit(0);
}
else if (mode == COMMAND_MODE)
{
putchar(c);
inputBuffer[ibc++] = c;
}
}
const char *pathFormatter(string s) //if ~ is 1st char then assuming absolute path else relative path to currentPath
{
string pp;
if (s.at(s.size() - 1) == '/')
s.erase(s.size() - 1);
if (s.at(0) == '~')
pp = ROOT_PATH + s.substr(1);
else if (s.at(0) == '.')
pp = string(currentPath);
else
pp = string(currentPath) + (s.at(0) == '/' ? "" : "/") + s;
//cout<<"Using path "<<pp<<endl;
return pp.c_str();
}
void navigateCommandMode(vector<string> s)
{
if (s[0] == "copy")
{
string dest = s[s.size() - 1];
if (s.size() < 3)
printf("\nNot enough arguments\n");
else
copyFD(s, pathFormatter(dest), currentPath);
}
else if (s[0] == "move")
{
string dest = s[s.size() - 1];
if (s.size() < 3)
printf("Not enough arguments\n");
else
moveFD(s, pathFormatter(dest), currentPath);
}
else if (s[0] == "rename")
{
if (s.size() < 3)
printf("Not enough arguments\n");
else
rename(s[1].c_str(), s[2].c_str());
}
else if (s[0] == "create_file")
{
string dest = s[s.size() - 1];
if (s.size() < 3)
printf("Not enough arguments\n");
else
{
//printf("%s %s", s[1].c_str(), pathFormatter(dest));
createFile(s[1].c_str(), pathFormatter(dest));
}
}
else if (s[0] == "create_dir")
{
string dest = s[s.size() - 1];
if (s.size() < 3)
printf("Not enough arguments\n");
else
createDir(s[1].c_str(), pathFormatter(dest));
}
else if (s[0] == "delete_file")
{
if (s.size() < 2)
printf("Not enough arguments\n");
else
deleteFD(pathFormatter(s[1]));
}
else if (s[0] == "delete_dir")
{
if (s.size() < 2)
printf("Not enough arguments\n");
else
deleteFD(pathFormatter(s[1]));
}
else if (s[0] == "goto")
{
if (s.size() < 2)
printf("Not enough arguments\n");
else
{
string dest = s[s.size() - 1];
prevBackDir.push_back(string(currentPath));
prevFwdDir.clear();
chdir(pathFormatter(dest));
getcwd(currentPath, 4096);
}
}
else if (s[0] == "search")
{
if (s.size() < 2)
printf("Not enough arguments\n");
else
{
string dest = s[s.size() - 1];
bool x = searchFD(dest.c_str(), currentPath);
if (x)
printf("%s is present\n", dest.c_str());
else
printf("%s is not present\n", dest.c_str());
}
}
else
cout << "Unknown command\n";
}
void navigate()
{
int c;
string line;
while (true)
{
//if (mode == NORMAL_MODE){
c = firstChar();
navigateNormalMode(c);
/*}
else
{
getline(cin, line);
navigateCommandMode(getTokens(line));
}*/
}
}
void displayAtIndex(int i)
{
dirent *de = listOfDirs[i];
struct stat fileDetails;
int detailsStatus = stat(de->d_name, &fileDetails);
mode_t perms = fileDetails.st_mode;
string permissions = "";
permissions = permissions + ((S_ISDIR(perms)) ? "d" : "-");
if (permissions == "d")
typeFileOrDir.push_back(TYPE_DIRECTORY);
else
typeFileOrDir.push_back(TYPE_FILE);
permissions = permissions + ((perms & S_IRUSR) ? "r" : "-") + ((perms & S_IWUSR) ? "w" : "-") + ((perms & S_IXUSR) ? "x" : "-") + ((perms & S_IRGRP) ? "r" : "-") + ((perms & S_IWGRP) ? "w" : "-") + ((perms & S_IXGRP) ? "x" : "-") + ((perms & S_IROTH) ? "r" : "-") + ((perms & S_IWOTH) ? "w" : "-") + ((perms & S_IXOTH) ? "x" : "-");
struct passwd *pw = getpwuid(fileDetails.st_uid);
struct group *gr = getgrgid(fileDetails.st_gid);
struct tm *tm = localtime(&fileDetails.st_mtime);
string owner = (pw == NULL ? "Undefined" : pw->pw_name);
string grp = (gr == NULL ? "Undefined" : gr->gr_name);
char modTime[256];
strftime(modTime, sizeof(modTime), nl_langinfo(D_T_FMT), tm);
printf("%s \t %-8.8s\t%-8.8s\t%9jd\t%s\t%s", permissions.c_str(), pw->pw_name, gr->gr_name, (intmax_t)fileDetails.st_size, modTime, de->d_name);
}
void listDirCurrentPath(char const *pp)
{
struct dirent *dee;
DIR *dr = opendir(pp);
listOfDirs.clear();
typeFileOrDir.clear();
clearScreen();
chdir(pp);
getcwd(currentPath, 4096);
if (dr == NULL)
{
printf("Could not open current directory");
return;
}
//printf("Permissions\tOwner\tGroup\tSize(B)\tLast Modified\tName\n");
while ((dee = readdir(dr)) != NULL)
{
listOfDirs.push_back(dee);
}
int maxToDisplay = min((int)listOfDirs.size(), (int)WindowSize.ws_row);
for (int i = 0; i < maxToDisplay; i++)
{
displayAtIndex(i);
if (i != maxToDisplay - 1)
printf("\n");
else
printf("\033[6n");
}
currList = listOfDirs.size();
yC = 1;
closedir(dr);
cout << "\033[" << 1 << ";" << 1 << "H"; //move cursor to (1,1)
fflush(stdout);
}
int main()
{
//struct winsize size;
printf("\033[8;3000;1000t"); //maximise window
ioctl(STDOUT_FILENO, TIOCGWINSZ, &WindowSize);
//printf("\033[r");//enable scrolling
fflush(stdout);
getcwd(ROOT_PATH, 1024);
CURRENT_ROW = 1;
//strcpy(currentPath, ROOT_PATH);
listDirCurrentPath(ROOT_PATH);
navigate();
printf("\033[8;%d;%dt", WindowSize.ws_row, WindowSize.ws_col);
fflush(stdout);
return 0;
}