-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11_tab_completion.cpp
More file actions
66 lines (53 loc) · 2.09 KB
/
11_tab_completion.cpp
File metadata and controls
66 lines (53 loc) · 2.09 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
#include <iostream>
#include <cstring>
#include <vector>
#include <readline/readline.h>
#include <readline/history.h>
// Function to set up tab completion
void setup_tab_completion() {
rl_bind_key('\t', rl_complete); // Bind the TAB key to rl_complete
}
int main() {
using_history(); // Enable command history
while (1) {
char* input = readline("YourShell> ");
if (!input) {
break; // Exit on Ctrl+D or EOF
}
// Add the entered command to history
add_history(input);
// Process and execute the command
// Handle built-in commands (ls, echo, cd, pwd, pinfo) and other user commands
// Implement tab completion
rl_attempted_completion_function = [](const char* text, int start, int end) -> char** {
// Get a list of files and directories in the current directory
char* dir = strdup("."); // Current directory
char** matches = (char**)NULL;
// Use scandir to list files and directories
int num_entries = scandir(dir, &matches, NULL, alphasort);
// Create a vector to store matching completions
std::vector<std::string> completions;
for (int i = 0; i < num_entries; i++) {
if (strncmp(matches[i], text, end - start) == 0) {
completions.push_back(matches[i]);
}
free(matches[i]);
}
free(matches);
// Allocate and return completions as a char** array
char** rl_completions = (char**)NULL;
if (!completions.empty()) {
rl_completions = (char**)malloc((completions.size() + 1) * sizeof(char*));
for (size_t i = 0; i < completions.size(); i++) {
rl_completions[i] = strdup(completions[i].c_str());
}
rl_completions[completions.size()] = (char*)NULL;
}
return rl_completions;
};
// Process and execute the command
// Free the input string
free(input);
}
return 0;
}