使用 readline() 完成
我有一个关于 Readline 图书馆的问题。
我想知道 Readline 是否可以从 C 程序中的目录中自动完成文件名?
我搜索过,只看到命令名称完成。
提前致谢。
编辑:我已将文件名复制到数组中。 这些是我使用的函数: 在文件 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() 似乎没有被使用。你知道我是否使用了正确的功能吗? 谢谢 !!
I've got a question about Readline Library.
I want to know if Readline can autocomplete filename from directories in a C program ?
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 !!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
文件名补全是 readline 的内置功能,您不需要填充文件名列表等。在 readline 6.1 中,以下程序默认允许文件名补全。
有多种方法可以自定义此机制,例如您可以指定一些函数,例如 rl_filename_quoting_function 和 rl_filename_dequoting_function 来帮助 readline 为您的应用程序提供正确的文件名引用。
我认为如果这对您不起作用,您需要指定您的 readline 版本。
/etc/inputrc
内容也应该被检查。你有使用 readline 的bash
吗?文件名补全是否按预期工作?无论如何,
info readline
是一个非常好的文档,只要您可以使用info
本身:) 如果没有,请查看 使用 GNU Readline 编程。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.
There are ways to customize this mechanism, e.g. you can specify some functions like
rl_filename_quoting_function
andrl_filename_dequoting_function
to help readline provide proper filename quotation for your application.I think you need to specify your version of readline if this doesn't work for you.
/etc/inputrc
contents should be examined as well. Do you havebash
, which uses readline? Does the filename completion work there as expected?Anyway,
info readline
is a very good documentation provided you can useinfo
itself :) If not, look at Programming with GNU Readline.要使用 readline 库,请为编译器指定 -lreadline 。可以使用以下代码片段进行编译
To use the readline library specify -lreadline to your compiler. The following code snippet can be compiled with
我对你所指的 readline 感到困惑,但有人向我指出你指的是 GNU 库中的 readline。
有关此示例,请参阅 Fredrik 的 GNU Readline 库链接,该库就是这样做的。
要将此应用到您的需求,而不是您看到的
string cmd[]
,您需要使用当前目录中所有文件名的数组,其余代码应该大致相同。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.
For an example of this please see Fredrik's link to the GNU Readline library that does just that.
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.