Я хотел бы выбрать автозавершение предложений библиотеки Readline в C. Первый шаг, который я хотел бы сделать, это просто выделить первый блок текста, предложенный Readline, и сохранить его в файле char *
. Теперь я могу распечатать предложения автозаполнения после того, как пользователь нажмет вкладку, например:
> view FILE_NAME(user presses tab key)
FILE_NAME_1 FILE_NAME_2 FILE_NAME_3 (suggestions by the readline library)
Как выделить предложение FILE_NAME_1
и сохранить его в переменной char *
после того, как пользователь нажмет tab
во второй раз? Код здесь является продолжением этого вопроса. а>. Исходный код можно найти здесь.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <term.h>
#include <readline/readline.h>
int execute_line(char *line);
void initialize_readline();
static char **fileman_completion(char *text, int start, int end);
static char *command_generator(char *text, int state);
char *command[] = { "view", "quit", (char *)NULL };
int done; /* When non-zero, this global means the user is done using this program. */
int main(int argc, char **argv)
{
char *line;
initialize_readline(); /* Bind our completer. */
for ( ; done == 0; ) {
line = readline("> ");
if (!line) break;
if (*line)
execute_line(line);
free(line);
}
return 0;
}
/* String to pass to system(). This is for the VIEW command. */
static char syscom[1024];
int execute_line(char *line)
{
int i = 0;
char *word;
/* Isolate the command word. */
while (line[i] && whitespace(line[i]))
i++;
word = line + i;
while (line[i] && !whitespace(line[i])) i++;
if (line[i]) line[i++] = '\0';
if (strcmp(word, "quit") == 0) {
done = 1;
return 0;
} else if (strcmp(word, "view")) {
fprintf(stderr, "%s: Choose only \"view FILE\" or \"quit\" as your command.\n", word);
return -1;
}
/* Get argument to command, if any. */
while (whitespace(line[i])) i++;
word = line + i;
if(!word || !*word) {
fprintf(stderr, "view: Argument required.\n");
return -1;
}
sprintf(syscom, "more %s", word);
return system(syscom);
}
static void display_matches(char** matches, int len, int max)
{
int saved_point = rl_point;
char *saved_line = rl_copy_text(0, rl_end);
rl_save_prompt();
rl_replace_line("", 0); // Clear the previous text
rl_display_match_list(matches, len, max);
putp(cursor_up);
rl_restore_prompt();
rl_replace_line(saved_line, 0);
rl_point = saved_point;
rl_redisplay();
free(saved_line);
}
void initialize_readline()
{
rl_readline_name = "rline";
rl_attempted_completion_function = (rl_completion_func_t *)fileman_completion;
rl_completion_display_matches_hook = display_matches;
setupterm(NULL, 1, (int*)0);
}
static char **fileman_completion(char *text, int start, int end)
{
if (start == 0)
return rl_completion_matches(text, (rl_compentry_func_t *)*command_generator);
return NULL;
}
static char *command_generator(char *text, int state)
{
static int list_index, len = 0;
char *name;
if (!state) {
list_index = 0;
len = strlen(text);
}
if (len > 0)
while ((name = command[list_index++]))
if (strncmp(name, text, len) == 0)
return strdup(name);
return NULL;
}
"more %s"
вызоветmore
, который на самом деле не обеспечивает возврат конкретной функции строки - если я правильно прочитал ваш код. - person David C. Rankin   schedule 11.10.2020