-
Notifications
You must be signed in to change notification settings - Fork 0
/
gbsh.cpp
814 lines (654 loc) Β· 21.4 KB
/
gbsh.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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
#include <sys/utsname.h> //getting system info
#include <signal.h> //overriding interrupt signal
#include <list> //for command history
#include <pwd.h> //getting user info
#include <pthread.h> //making background processes
#include "./include/color_modder.h" //to alter bash output colors
#include <stdlib.h> //for atoi function
#define ever (;;)
const int KEY_UP []= {27, 91, 65};
const int KEY_DOWN []= {27, 91, 66};
const int KEY_LEFT []= {27, 91, 68};
const int KEY_RIGHT []= {27, 91, 67};
//void* helloWorld(){
// printf("Hello world\n");
// return (void*)0;
//}
/* Must toDo: Improve the structure of special key recognition, because many operations repeat */
//struct {
// int values [3];
// int number_of_values;
// const char* key;
// void* (*fn)();
//} specialkeys_lookup_table [] =
//{
// {{27, 91, 65}, 3, "KEY_UP", &helloWorld},
// {{27, 91, 66}, 3, "KEY_DOWN", &helloWorld}
//};
//bool special_key_lookup_and_call( char** arguments, int argument_count ){
// for (int i = 0; specialkeys_lookup_table[ i ].fn; i++)
// if (strcmp(specialkeys_lookup_table[ i ].key, arguments[0]) == 0){
// (*(specialkeys_lookup_table[ i ].fn))(arguments, argument_count-1); //-1 because we are excluding the NULL at the end
// return true;
// }
// return false;
//}
std::list <std::string> CommandHistory;
std::list <std::string>::iterator cmdhist_it = CommandHistory.begin();
/* feeling cute, might use this later for multithreaded producer-consumer problems */
//pthread_mutex_t lock;
/* "I'll be back" - The Terminator */
void sigintHandler(int sig_num){
signal(SIGINT, sigintHandler);
fflush(stdout);
}
/* declaring global Env variables for this terminal */
size_t defaultpathsize = 50;
char* DEFAULT_PATH = (char *)malloc(defaultpathsize * sizeof(char)); //default path for terminal commands (Virtual System)
size_t pathsize = 100;
char* CURRENT_PATH_RELATIVE = (char *)malloc(pathsize * sizeof(char));
size_t hostnamesize = 50;
char* HOST_NAME = (char *)malloc(hostnamesize * sizeof(char));
struct utsname systemInfo;
struct passwd * userInfo;
enum EXPRS {
NO_INPUT_FLAG,
DAEMON,
STD_REDIR,
PIPE,
PIPE_END,
INTERNAL_COMMAND
};
// Because I always forget
#define DEFAULT_STD_OUT 1
#define DEFAULT_STD_IN 0
/* end declaration */
/* Include all builtin functions of mard */
#include "./include/mrd_fn.h"
/* ================ Assistive Terminal Functions START ================ */
void* daemonListener(void * args){
// printf("In the listener\n");
int * argv = (int *)args;
int childPid = argv[0];
int flag = argv[1];
if (flag != PIPE)
printf("[%lu] %d\n", pthread_self(), childPid);
waitpid(childPid, NULL, 0);
if (flag != PIPE)
printf("[%lu]\tDone\t\t\t%d\n", pthread_self(), childPid);
if (argv){
free (argv);
argv = NULL;
}
/* toDo: return status of child */
return (void*)0;
}
void setCWD(){
size_t currentpathsize = 100;
char* CURRENT_PATH = (char *)malloc(currentpathsize * sizeof(char));
getcwd(CURRENT_PATH, currentpathsize);
int j;
for (j=0;j<(signed int)strlen(CURRENT_PATH);j++){
if (userInfo->pw_dir[j] == '\0'){
break;
}
else if (CURRENT_PATH[j] != userInfo->pw_dir[j])
break;
}
// printf ("j is %d, strlen is %d", j, strlen(userInfo->pw_dir));
if (j!=(signed int)strlen(userInfo->pw_dir)){
int i;
for (i=0; CURRENT_PATH[i]!='\0'; i++){
CURRENT_PATH_RELATIVE[i] = CURRENT_PATH[i];
}
CURRENT_PATH_RELATIVE[i] = '\0';
if (CURRENT_PATH){
free (CURRENT_PATH);
CURRENT_PATH = nullptr;
}
}
else{
CURRENT_PATH_RELATIVE[0]='~';
int i;
for (i=1; CURRENT_PATH[j]!='\0'; i++, j++){
CURRENT_PATH_RELATIVE[i] = CURRENT_PATH[j];
}
CURRENT_PATH_RELATIVE[i] = '\0';
}
pathsize = strlen(CURRENT_PATH_RELATIVE);
if (CURRENT_PATH){
free(CURRENT_PATH);
CURRENT_PATH = nullptr;
}
}
/* Smart, relaxing and useful input taker for complaint systems */
void fancyInput (int &characters, char* &input, int &CURSOR_POS){
int c; // used to capture the ASCII value of the input
/* use system call to make terminal send all keystrokes directly to stdin */
system ("/bin/stty raw");
int specialKeyCount = 0;
std::string nbInput="";
int specialInput [] = {0, 0, 0};
CURSOR_POS = 0;
cmdhist_it = CommandHistory.end();
while((c=getchar())!= 13) {
/* Use the ASCII value of ENTER to break out of the loop, since CTRL-D or CTRL-C won't work in raw */
// std::cout<<"{int is "<<c<<"}";
if (c == 127){
if (!nbInput.empty()){
printf("\b\b\b \b\b\b");
nbInput.pop_back();
characters--;
CURSOR_POS--;
}
else {
printf("\b\b \b\b");
}
}
else if (KEY_UP[specialKeyCount] == c){
// std::cout << std::endl << "special input" << std::endl;//key up
specialInput[specialKeyCount] = c;
specialKeyCount++;
if (specialKeyCount == 3){
bool isSame=true;
for (int i=0; i<3; i++){
if (specialInput[i] != KEY_UP[i]){
isSame = false;
break;
}
}
if (isSame){
if (cmdhist_it!=CommandHistory.begin())
cmdhist_it--;
if (!CommandHistory.empty()){
// remove the traces of previous input
std::string backTrack = "";
for (int i=0; i<(signed int)nbInput.size(); i++)
backTrack += '\b';
for (int i=0; i<(signed int)nbInput.size(); i++)
backTrack += ' ';
for (int i=0; i<(signed int)nbInput.size(); i++)
backTrack += '\b';
// reassign input
nbInput = (*cmdhist_it);
printf("\b\b\b\b \b\b\b\b%s%s", backTrack.c_str(), nbInput.c_str());
CURSOR_POS = nbInput.size();
// printf("[%d]", CURSOR_POS);
// printf("[%d]", nbInput.size());
// printf("[%d]", CURSOR_POS);
// printf("[nbInput is %s]", nbInput.c_str());
}
else{
printf("\b\b\b\b \b\b\b\b");
}
// printf("\b\b\b\b[User pressed UP!]");
specialKeyCount = 0;
}
}
}
else if (KEY_DOWN[specialKeyCount] == c){
// std::cout << std::endl << "special input" << std::endl;//key up
specialInput[specialKeyCount] = c;
specialKeyCount++;
if (specialKeyCount == 3){
bool isSame=true;
for (int i=0; i<3; i++){
if (specialInput[i] != KEY_DOWN[i]){
isSame = false;
break;
}
}
if (isSame){
if (cmdhist_it!=CommandHistory.end())
cmdhist_it++;
if (!CommandHistory.empty()){
// remove the traces of previous input
std::string backTrack = "";
for (int i=0; i<(signed int)nbInput.size(); i++)
backTrack += '\b';
for (int i=0; i<(signed int)nbInput.size(); i++)
backTrack += ' ';
for (int i=0; i<(signed int)nbInput.size(); i++)
backTrack += '\b';
// reassign input
nbInput = (*cmdhist_it);
printf("\b\b\b\b \b\b\b\b%s%s", backTrack.c_str(), nbInput.c_str());
CURSOR_POS = nbInput.size();
// printf("[nbInput is %s]", nbInput.c_str());
}
else{
printf("\b\b\b\b \b\b\b\b");
}
// printf("\b\b\b\b[User pressed DOWN!]");
specialKeyCount = 0;
}
}
}
else if (KEY_LEFT[specialKeyCount] == c){
// std::cout << std::endl << "special input" << std::endl;//key up
specialInput[specialKeyCount] = c;
specialKeyCount++;
if (specialKeyCount == 3){
bool isSame=true;
for (int i=0; i<3; i++){
if (specialInput[i] != KEY_LEFT[i]){
isSame = false;
break;
}
}
if (isSame){
// printf("\b\b\b\b[User pressed LEFT!]");
if (CURSOR_POS>0){
CURSOR_POS--;
std::string backTracks = "";
int num = nbInput.size() - CURSOR_POS;
for (int i=0; i<num; i++)
backTracks += '\b';
printf("\b\b\b\b \b\b\b\b\b%s%s", &nbInput[CURSOR_POS], backTracks.c_str());
}
else{
printf("\b\b\b\b \b\b\b\b");
}
specialKeyCount = 0;
// std::cout<<num;
}
}
}
else{
specialKeyCount = 0;
if (CURSOR_POS<characters)
nbInput[CURSOR_POS] = (char)c;
else
nbInput += (char)c;
characters++;
CURSOR_POS++;
// printf("[%d]", CURSOR_POS);
}
// std::cout<<"[Cursor position is "<<CURSOR_POS<<"]";
// moveCursor();
}
/* use system call to set terminal behavior to more normal behavior */
system ("/bin/stty cooked");
/* remove Enter ASCII ("^M") */
printf("\b\b \b\b%s\n", &nbInput[CURSOR_POS]);
/* copy the nbInput string to the official input char* */
strcpy( input, nbInput.c_str());
/* add command to Command History */
if (nbInput != ""){
if (nbInput != CommandHistory.back()) //avoid repeating commands
CommandHistory.push_back(nbInput);
}
}
/* Standard and easy input taker for non-complaint systems */
void normalInput (int &characters, char* &input, unsigned long int &inputsize){
characters = getline(&input, &inputsize, stdin);
input[characters-1] = '\0';
}
struct Token{
const char token;
bool isIncluded;
} tokenTypes_lookup_table[] =
{
{ ' ', false },
{ '<', true },
{ '>', true },
{ '|', true },
{ '&', true },
{ 0 , false },
{ '?', false } //ending delimiter
};
Token token_lookup (const char token){
int i;
for (i=0; tokenTypes_lookup_table[i].token != '?'; i++){
if (token == tokenTypes_lookup_table[i].token){
return tokenTypes_lookup_table[i];
}
}
return tokenTypes_lookup_table[i];
}
void tokenizeInput (char* &input, std::vector<char*>& arguments, const char* token = " "){
int START_POS = 0;
int END_POS = 0;
for (int i=0; i<strlen(input)+1; i++){
auto found = token_lookup(input[i]);
if (found.token != '?'){
END_POS = i;
if (END_POS - START_POS>0){
char* arg = (char*)malloc( (END_POS-START_POS+1) * sizeof(char));
strncpy(arg, &input[START_POS], END_POS - START_POS);
arg[END_POS-START_POS] = '\0';
if (strcmp(arg, " ") != 0) //prevents pre-spaces in command
arguments.push_back(arg);
if (found.isIncluded){
char* argt = (char*)malloc(2 * sizeof(char));
argt[0] = found.token;
argt[1] = '\0';
arguments.push_back(argt);
// free (argt); //cannot free because it would create a dangling pointer!
}
i++;
if (input[i]=='\0')
break;
char foundc;
do {
auto foundnext = token_lookup(input[i]);
foundc = foundnext.token;
if (foundnext.isIncluded){
char* argt = (char*)malloc(2 * sizeof(char));
argt[0] = foundnext.token;
argt[1] = '\0';
arguments.push_back(argt);
// free (argt); //cannot free because it would create a dangling pointer!
}
i++;
}while (foundc!='?');
START_POS = i-1;
}
}
}
// ======== FOR VERBOSE DEBUGGING ======== //
// printf("Printing arguments\n");
// printf("%d\n", arguments.size());
// for (int i=0; i<arguments.size(); i++){
// printf("%d. %s\n", i, arguments[i]);
// }
// ======== FOR VERBOSE DEBUGGING ======== //
arguments.push_back(NULL);
}
struct Command {
std::vector<char*> arguments;
int output_fd;
int input_fd;
int flag;
Command ():output_fd(DEFAULT_STD_OUT), input_fd(DEFAULT_STD_IN), flag(NO_INPUT_FLAG){}
// Command (int ofd, int ifd, int flg=NO_INPUT_FLAG):output_fd(ofd), input_fd(ifd), flag(flg){} // Ignore Parameterized constructor as it's not used
};
int evalExpression(std::vector<char*>& arguments, std::vector<Command>& commands){
int CURRENT_FLAG = -1;
for (int i=0; i<arguments.size()-1; i++){
if (strcmp(arguments[i], ">") == 0){
int fd = open(arguments[++i], O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
commands.back().output_fd = fd;
commands.back().flag = STD_REDIR;
}
else if (strcmp(arguments[i], "<") == 0){
int fd = open(arguments[++i], O_RDWR, S_IRUSR | S_IWUSR);
commands.back().input_fd = fd;
commands.back().flag = STD_REDIR;
}
else if (strcmp (arguments[i], "|") == 0){
int fd[2];
pipe (fd);
commands.back().output_fd = fd[1];
commands.back().arguments.push_back(NULL);
commands.back().flag = PIPE;
Command pushTo;
commands.push_back(pushTo);
commands.back().input_fd = fd[0];
commands.back().flag = PIPE_END;
}
else if (strcmp (arguments[i], "&") == 0 and i!=arguments.size()-2 and strcmp(arguments[i+1], "&") == 0){
commands.back().arguments.push_back(NULL);
Command pushTo;
commands.push_back(pushTo);
i++;
}
else{
if (commands.empty()){
Command pushTo;
commands.push_back(pushTo);
}
commands.back().arguments.push_back(arguments[i]);
}
CURRENT_FLAG = 0;
}
commands.back().arguments.push_back(NULL);
// ======== FOR VERBOSE DEBUGGING ======== //
// // printing commands and their arguments
// for (int i=0; i<commands.size(); i++){
// printf("Command #%d: ", i);
// for (int j=0; j<commands[i].arguments.size()-1; j++){
// printf ("%s, ", commands[i].arguments[j]);
// }
// printf("\n");
// }
// ======== FOR VERBOSE DEBUGGING ======== //
return CURRENT_FLAG;
}
int understandInput(char* &input, std::vector<Command>& commands){
std::vector<char*> arguments;
/* tokenize the input and place it into a vector of char* */
tokenizeInput(input, arguments, " ");
if (arguments[0] == NULL or arguments.empty()){
return -1;
}
/* check if it is an expression or an environment variable definition */
int _EXPR = evalExpression(arguments, commands);
return _EXPR;
}
/* standard function macro which just redirects the program's I/O to the command's I/O */
#define REDIR_IO() \
if (commands[i].input_fd!=DEFAULT_STD_IN) dup2(commands[i].input_fd, DEFAULT_STD_IN); \
if (commands[i].output_fd!=DEFAULT_STD_OUT) dup2(commands[i].output_fd, DEFAULT_STD_OUT);
int implementInput(std::vector<Command>& commands, int FLAG = NO_INPUT_FLAG){
int childPid = 0;
std::vector <pthread_t> daemonListeners;
for (int i=0; i<commands.size(); i++){
// checks if Daemon process or not (ampersand '&' in the end)
if (strcmp(commands[i].arguments[commands[i].arguments.size()-2], "&") == 0){
commands[i].arguments.pop_back();
commands[i].arguments.pop_back();
commands[i].arguments.push_back(NULL);
FLAG = DAEMON;
}
char ** argv = &commands[i].arguments[0];
// saves the original terminal I/O FDs in case they're lost
int saved_stdout = dup(DEFAULT_STD_OUT);
int saved_stdin = dup(DEFAULT_STD_IN);
int index;
/* checks if a builtin bash command (normal and non-Daemon - results in faster execution) */
if (FLAG != DAEMON and commands[i].flag != PIPE and commands[i].flag != PIPE_END and ((index = lookup_function(argv[0])) >= 0)) {
/* redirects I/O if the default input and output have been changed */
REDIR_IO();
/* call supercedes I/O redirection */
call(index, argv, commands[i].arguments.size());
}
else if ((childPid = fork()) == 0){
setenv("PARENT", getenv("SHELL"), true);
/* redirects I/O if the default input and output have been changed */
REDIR_IO();
/* If it's a daemon, checks if a builtin bash command and immediately call if true*/
if (call(lookup_function(argv[0]), argv, commands[i].arguments.size())) {
exit(0);
}
//is a system command
if (strchr(commands[i].arguments[0],'/') == nullptr){
//is a Virtual System command
std::string defpath = getenv("SHELL");
defpath += "/bin/";
std::string path = defpath + commands[i].arguments[0];
execv(path.c_str(), argv);
//is a Native System command
char* allpaths = getenv("PATH");
char* nat_path = strtok (allpaths,":");
std::string theCommand = commands[i].arguments[0];
while (nat_path != NULL) {
std::string argpath = nat_path;
argpath += "/";
argpath += theCommand;
commands[i].arguments[0] = &argpath[0];
execv(argpath.c_str(), argv);
nat_path = strtok(NULL, ":");
}
//if both commands fail
// if (COMMAND_NOT_FOUND_PY)
// fprintf(stderr, "Command '%s' not found\n", theCommand.c_str());
std::string errPath = getenv("SHELL");
errPath += "/usr/lib/command-not-found";
if (execl(errPath.c_str(), errPath.c_str(), theCommand.c_str(), (char*) NULL)<0){
if (errno == 13){ //Permissions error
if (FLAG == INTERNAL_COMMAND){
fprintf(stderr, "Cannot release permissions for command-not-found. Please manually enter the command\n\nsudo chmod a+x usr/lib/command-not-found\n\nin this terminal or your official Bash terminal, or set the permissions to allow Execute in the system GUI.\nYou may choose to turn this feature off with the CONFIG file.\n");
exit(1);
}
else{
fprintf(stderr, "mard: Cannot give command suggestions.\nReason: %s\n\n", strerror(errno));
std::vector <Command> unlockPermissions;
Command unlocker;
unlocker.arguments.push_back((char*)"sudo");
unlocker.arguments.push_back((char*)"chmod");
unlocker.arguments.push_back((char*)"a+x");
unlocker.arguments.push_back((char*)"usr/lib/command-not-found");
unlocker.arguments.push_back(NULL);
unlockPermissions.push_back(unlocker);
implementInput (unlockPermissions, INTERNAL_COMMAND);
}
}
}
}
//is a file or directory
else{
const char* path = commands[i].arguments[0];
// for (int k=0; argv[k]; k++)
// printf (" { %s },\n ", argv[k]);
// printf (" { path = %s\n ", path.c_str());
if (execv(path, argv)<0){
struct stat path_stat;
if (stat(path, &path_stat)>=0){
if (S_ISDIR(path_stat.st_mode)){
printf("mard: %s: Is a directory\n", commands[i].arguments[0]);
}
else {
fprintf(stderr, "mard: %s: %s\n", commands[i].arguments[0], strerror(errno));
}
}
else
fprintf(stderr, "mard: %s: %s\n", commands[i].arguments[0], strerror( errno ));
}
}
//everything failed
exit(1);
}
else{
if (FLAG == DAEMON or commands[i].flag == PIPE){
if (commands[i].output_fd != DEFAULT_STD_OUT)
close (commands[i].output_fd);
daemonListeners.push_back(0);
int *argsToSend = (int*)malloc(2*sizeof(int));
argsToSend[0] = childPid;
argsToSend[1] = commands[i].flag;
pthread_create(&daemonListeners.back(), NULL, daemonListener, (void*)argsToSend);
}
else{
if (commands[i].input_fd != DEFAULT_STD_IN and commands[i].flag == PIPE_END)
close (commands[i].input_fd);
waitpid(childPid, NULL, 0);
}
}
/* restore terminal I/O if changed */
if (dup(DEFAULT_STD_OUT) != saved_stdout){
/* Restore stdout */
dup2(saved_stdout, DEFAULT_STD_OUT);
close(saved_stdout);
}
if (dup(DEFAULT_STD_IN) != saved_stdin){
/* Restore stdin */
dup2(saved_stdin, DEFAULT_STD_IN);
close(saved_stdin);
}
/* Reset FLAG here (Might cause problems imo, should be replaced by command-specific flags) */
FLAG = NO_INPUT_FLAG;
}
}
void inputLoop(){
char * input;
size_t inputsize = 100;
int CURSOR_POS = 0;
Color::Modifier green(Color::FG_GREEN);
Color::Modifier def(Color::FG_DEFAULT);
Color::Modifier blue(Color::FG_BLUE);
/* Runs forever ;) */
for ever {
setCWD();
printf("\033[1m%s%s@%s%s:%s%s%s\033[0m > ", green.getColor().c_str(), userInfo->pw_name, HOST_NAME, def.getColor().c_str(), blue.getColor().c_str(), CURRENT_PATH_RELATIVE, def.getColor().c_str());
/* contains the number of characters read by stdin
* in both normalInput and fancyInput
*/
int characters = 0;
/* flush buffer to prevent leftover input from programs disturbing the terminal input */
fflush(stdin);
input = (char *)malloc(inputsize * sizeof(char));
#ifdef linux
/* For Linux systems only, right now */
fancyInput(characters, input, CURSOR_POS);
#else
/* For Mac, LSW, UNIX and other systems */
normalInput(characters, input, inputsize);
#endif
std::vector <Command> commands;
auto didItUnderstand = understandInput (input, commands);
if (didItUnderstand < 0)
continue;
/* causes segfaults and double free or corruption (fasttop) */
// clear input...
if (input){
free (input);
input = nullptr;
}
// // printing commands and their arguments
// for (int i=0; i<commands.size(); i++){
// printf("Command #%d: ", i);
// for (int j=0; j<commands[i].arguments.size()-1; j++){
// printf ("%s, ", commands[i].arguments[j]);
// }
// printf("\n");
// }
implementInput (commands, didItUnderstand);
}
}
void initializeTerminal(){
signal(SIGINT, sigintHandler);
clearScreen(NULL, 0);
getcwd(DEFAULT_PATH, defaultpathsize);
// Setting SHELL's default path
setenv("SHELL", DEFAULT_PATH, 1);
for (int i=0;; i++){
if (DEFAULT_PATH[i]=='\0'){
defaultpathsize=i;
break;
};
}
if (DEFAULT_PATH){
// printf("boutta do a fasttop error");
free(DEFAULT_PATH);
DEFAULT_PATH = nullptr;
}
gethostname(HOST_NAME, hostnamesize);
uname(&systemInfo);
userInfo = getpwuid(getuid());
std::string userName = userInfo->pw_gecos;
for (int i=0, foundComma = false; i<(signed int)userName.size(); i++){
if (userName[i] == ','){
foundComma = true;
}
if (foundComma){
userName.pop_back();
}
}
userName.pop_back();
printf("Welcome back to your %s (%s) system", systemInfo.sysname, systemInfo.machine);
if (userName.c_str())
printf(", %s", userName.c_str());
printf("!\n\n");
printf("mard is a chad 3rd-party bash. It comes with command history, a virtual machine, and even built-in games.\n");
printf("Use 'system' before a Linux/UNIX system call to use the machine's default system calls.\n\n");
printf("The SHELL default path is: %s\n", getenv("SHELL"));
printf("Env PATH variable is: %s\n\n", getenv("PATH"));
}
/* ================ Assistive Terminal Functions END ================ */
/* driver code */
int main(int argc, char **argv) {
initializeTerminal();
inputLoop();
return 0;
}