-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt.c
62 lines (52 loc) · 1.32 KB
/
prompt.c
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
#include "prompt.h"
#ifdef _USE_READLINE
const int num_commands = 7;
const char *commands[] = {
"exit", "hash", "put", "q", "quit", "remove", "show",
};
#endif
char *prompt(char* user_input) {
#ifdef _USE_READLINE
user_input = readline("> ");
#else // alternate prompt, ugly when input > MAX_INPUT_LENGTH but
// shouldn't cause actual issues
fputs("> ", stdout);
fgets(user_input, MAX_INPUT_LENGTH, stdin);
int len = strlen(user_input);
if (len > 0 && user_input[len - 1] == '\n') {
user_input[len - 1] = '\0';
}
#endif
return user_input;
}
#ifdef _USE_READLINE
void init_readline() {
rl_completion_entry_function = command_generator;
rl_attempted_completion_function = completion_function;
}
char **completion_function(const char *text, int start, int end) {
char **matches = NULL;
if (start == 0) {
matches = rl_completion_matches(text, command_generator);
}
return matches;
}
char *command_generator(const char *text, int state) {
static int i;
static int len; // cached for efficiency
if (state == 0) {
// initialize command generator
i = 0;
len = strlen(text);
}
for (; i < num_commands; ++i) {
if (!strncmp(commands[i], text, len)) {
char *res = strdup(commands[i]);
++i;
return res;
}
}
// if no command names matched
return NULL;
}
#endif