Search code examples
cshellfilenamesgnureadline

using readline() for completion


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 !!


Solution

  • 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.