使用 readline() 完成

发布于 2024-11-24 13:21:21 字数 404 浏览 0 评论 0原文

我有一个关于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

℡寂寞咖啡 2024-12-01 13:21:21

文件名补全是 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;
}

有多种方法可以自定义此机制,例如您可以指定一些函数,例如 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.

#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_function and rl_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 have bash, which uses readline? Does the filename completion work there as expected?
Anyway, info readline is a very good documentation provided you can use info itself :) If not, look at Programming with GNU Readline.

紧拥背影 2024-12-01 13:21:21

要使用 readline 库,请为编译器指定 -lreadline 。可以使用以下代码片段进行编译

cc some.c -o some -lreadline


#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;

}

To use the readline library specify -lreadline to your compiler. The following code snippet can be compiled with

cc some.c -o some -lreadline


#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;

}
你在我安 2024-12-01 13:21:21

我对你所指的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文