C语言 使用 readline() 完成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6727171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using readline() for completion
提问by lilawood
I've got a question about Readline Library.
我有一个关于 Readline 库的问题。
I want to know if Readline can autocomplete filename from directories in a C program ?
我想知道 Readline 是否可以从 C 程序中的目录自动完成文件名?
I've searched and only seen command name completion.
我已经搜索过并且只看到命令名称完成。
thanks in advance.
提前致谢。
EDIT: I've copy filename in an array. These as functions that I use : in the file rline.c, char *command_generator,char **tab_completion (const char *text, int start, int end),void initialize_readline (). I think I have to use char * filename_completion_function (char *text, int state) ? When I type on "tab" key, it calls nothing, bind() didn't seem to be used. Do you know if I use right functions ? thanks !!
编辑:我已将文件名复制到数组中。这些作为我使用的函数:在文件 rline.c, char *command_generator,char **tab_completion (const char *text, int start, int end),void initialize_readline ()。我想我必须使用 char * filename_completion_function (char *text, int state) ?当我输入“tab”键时,它什么也不调用,似乎没有使用 bind() 。你知道我是否使用了正确的功能吗?谢谢 !!
回答by unkulunkulu
Filename completion is a built-in feature of readline, you don't need to populate filename lists etc. Here with readline 6.1 the following program allows filename completion by default.
文件名补全是 readline 的一个内置功能,您不需要填充文件名列表等。在 readline 6.1 中,以下程序默认允许文件名补全。
#include <stdio.h>
#include <stdlib.h>
#include <readline/readline.h>
#include <readline/history.h>
int main()
{
printf( "%s\n", readline( "test> " ) );
return 0;
}
There are ways to customize this mechanism, e.g. you can specify some functions like rl_filename_quoting_functionand rl_filename_dequoting_functionto help readline provide proper filename quotation for your application.
有办法自定义此机制,例如,你可以指定一个像某些功能rl_filename_quoting_function,并rl_filename_dequoting_function以帮助readline的为应用程序提供适当的文件名的报价。
I think you need to specify your version of readline if this doesn't work for you. /etc/inputrccontents should be examined as well. Do you have bash, which uses readline? Does the filename completion work there as expected?
Anyway, info readlineis a very good documentation provided you can use infoitself :) If not, look at Programming with GNU Readline.
如果这对您不起作用,我认为您需要指定您的 readline 版本。/etc/inputrc内容也应该检查。你有bash,哪个使用readline?文件名完成是否按预期工作?无论如何,这info readline是一个非常好的文档,只要您可以使用info它自己:) 如果没有,请查看Programming with GNU Readline。
回答by lilawood
To use the readline library specify -lreadline to your compiler. The following code snippet can be compiled with
要使用 readline 库,请为您的编译器指定 -lreadline。以下代码片段可以编译为
cc -lreadline some.c -o some
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
int main()
{
char *inpt;
int i = 0;
while ( i < 10 )
{
inpt = readline("Enter text: ");
add_history(inpt);
printf("%s", inpt);
printf("\n");
++i;
}
return 0;
}
回答by Jesus Ramos
I was confused about the readline you were referring to but it was pointed out to me that you meant the one from the GNU libraries.
我对您所指的 readline 感到困惑,但有人向我指出您指的是 GNU 库中的那个。
For an example of this please see Fredrik's link to the GNU Readline library that does just that.
有关此示例,请参阅 Fredrik 指向执行此操作的 GNU Readline 库的链接。
To apply this to your needs instead of the string cmd[]that you see you need to use an array of all the file names in the current directory and the rest of the code should be about the same.
要将此应用于您的需求而不是string cmd[]您看到的您需要使用当前目录中所有文件名的数组,其余代码应该大致相同。

