10

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

lilawood
  • 2,263
  • 5
  • 22
  • 27

3 Answers3

16

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.

Mathieu
  • 8,840
  • 7
  • 32
  • 45
unkulunkulu
  • 11,576
  • 2
  • 31
  • 49
7

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;

}
Ning Ben
  • 35
  • 6
  • 2
    Don't you need to free inpt after the printf? – netskink Mar 01 '17 at 19:57
  • readline() return value indeed should be freed after it becomes useless. `man 3 readline` says "The line returned is allocated with malloc(3); the caller must free it when finished." – uis May 27 '23 at 20:09
1

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.

Jesus Ramos
  • 22,940
  • 10
  • 58
  • 88
  • @cnicutar Thanks for pointing that out, I edited in a reference to Fredrik's comment which I just saw. – Jesus Ramos Jul 17 '11 at 22:32
  • thanks! To fullfill the array, if I use system("ls") it works ? or I use an other C function ? – lilawood Jul 19 '11 at 12:33
  • 1
    ls could work, you could also use a DIR *, and then add the name of the files to a dynamic array as well by getting all the files of the current directory. – Jesus Ramos Jul 19 '11 at 12:36
  • 1
    @lilawood No, system("ls") will not do (it would send the ls output to stdout). You need to read and understand the readline documentation. – Jens Jul 20 '11 at 20:42