-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify_each_character.h
122 lines (110 loc) · 3.88 KB
/
verify_each_character.h
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
void handle_tab_press(char *partial_path, char* input) {
// Add your tab completion logic here
DIR *dir;
struct dirent *entry;
if ((dir = opendir(".")) != NULL) {
while ((entry = readdir(dir)) != NULL) {
if (strncmp(entry->d_name, partial_path, strlen(partial_path)) == 0) {
char temp[1000] = "";
strcpy(temp, entry->d_name + strlen(partial_path));
strcat(input, temp);
printf("%s",temp);
}
}
closedir(dir);
} else {
perror("opendir");
}
}
void verify_each_character(struct Terminal *terminal,char* input,int* inputIndex, int* historyIndex ){
char *current_cwd = get_cwd();
char *computer = get_computer_name();
char *user = current_user();
while (1){
int c = getchar();
// Detect Enter key or end of file
if (c == '\n' || c == EOF)
{
input[(*inputIndex)] = '\0';
printf("\n");
if ((*inputIndex) > 0)
{
terminal->addHistory(terminal, input);
(*historyIndex) = countHistory - 1;
}
break;
}
else if (c == 127)
{
// Backspace handling
if ((*inputIndex) > 0)
{
(*inputIndex)--;
input[(*inputIndex)] = '\0';
printf("\b \b"); // Move back, print space, move back
}
}
// Detect arrow keys
else if (c == '\033') // First byte of escape sequence
{
getchar(); // Skip '['
c = getchar(); // Get final byte
if (c == 'A') // Up arrow key
{
if ((*historyIndex) >= 0 && (*historyIndex) < countHistory)
{
strcpy(input, terminal->history[(*historyIndex)]);
printf("\r\033[K"); // Clear line
printf(BOLD_GREEN "%s@%s:" RESET_COLOUR, user, computer);
printf(BOLD_BLUE "%s" RESET_COLOUR, current_cwd);
printf(GREEN "$ " RESET_COLOUR "%s", input);
(*inputIndex) = strlen(input);
if ((*historyIndex) > 0)
{
(*historyIndex)--;
}
}
}
else if (c == 'B') // Down arrow key
{
if ((*historyIndex) < countHistory - 1)
{
(*historyIndex)++;
strcpy(input, terminal->history[(*historyIndex)]);
}
else if ((*historyIndex) == countHistory - 1)
{
(*historyIndex)++;
memset(input, 0, sizeof(input));
}
else
{
(*historyIndex) = countHistory - 1;
}
printf("\r\033[K"); // Clear the line
printf(BOLD_GREEN "%s@%s:" RESET_COLOUR, user, computer);
printf(BOLD_BLUE "%s" RESET_COLOUR, current_cwd);
printf(GREEN "$ " RESET_COLOUR "%s", input);
(*inputIndex) = strlen(input);
}
}
else if (c == '\t') {
// Call the function to handle Tab press
char temp[1000];
strcpy(temp, input);
if (strlen(temp) > 3) {
// Use strcpy to copy the substring starting from the third character
strcpy(temp, temp + 3);
handle_tab_press(temp , input);
(*inputIndex) = strlen(input);
}
}
else if (c >= 32 && c < 127)
{
// Handle regular characters
input[(*inputIndex)] = c;
(*inputIndex)++;
putchar(c);
}
}
}