C++ wrapper to GNU Readline


haywa

I'm trying to write a c++ wrapper for GNU Readline to be able to use custom completion easily, but I'm running into a small problem and can't think of a solution (I'm pretty new to c++).

class ReadLine {

public:

    ReadLine();
    ~ReadLine();

    std::string exec ();
    void enableHistory ();

private:
    std::vector<std::string> keywordList;
    bool history;

private:
    static char** my_completion (const char*, int, int);
    void* xmalloc (int);

    char* generator (const char*, int);
    char* dupstr (std::string);

};

cpp file:

std::string ReadLine::exec(){

    rl_attempted_completion_function = my_completion;

    std::string buf = "";

    buf = readline("Command>>");
    //enable auto-complete
    rl_bind_key('\t',rl_complete);

    if (buf[0]!=0)
        add_history(buf.c_str());

    return buf;
}




char** ReadLine::my_completion (const char* text, int start, int end) {


    char** matches;

    matches = NULL;

    if (start == 0) 
        matches = rl_completion_matches(text, my_generator);

    return matches;


}

My problem is the line

matchs = rl_completion_matches(text,my_generator)

Obviously throws an error: call to non-static member function without an object argumentbut I don't want to make the generator static, and I can't find the parameters it should take, because I won't have access to the class members in it (I need to use a keyword list to generate the keywords).

What do you suggest?

Mats Petersson

It's not easy to solve this problem in a good way, because the usual solution is to solve it by having a static wrapper function where you pass in a pointer to the class as a parameter.

Someone else might be able to suggest better, but I think the solution is to have a global variable that is a pointer to the current instance of the ReadLine class - this can be a stack, so you can push a new variable into it and then Eject to restore it to its original state.

In the simple case you would have something like:

ReadLine *currenReadLine = 0;

.... 

std::string ReadLine::exec(){
   ... 
   currentReadLine = this;
}


// declared as static in the class.
char ** ReadLine::my_completion(...)
{
    return currentReadLine->actual_completion(...);
}

and a similar solution my_generator.

Related


Lightweight GNU readline alternative

punekr12 : I am looking for a GNU readline alternative. It has many features but only a few of them worked for me as described below - I am developing an interactive command prompt application (displays the prompt and accepts the next user command to run). I w

Lightweight GNU readline alternative

punekr12 I am looking for a GNU readline alternative. It has many features but only a few of them worked for me as described below - I am developing an interactive command prompt application (displays the prompt and accepts the next user command to run). I wan

Lightweight GNU readline alternative

punekr12 : I am looking for a GNU readline alternative. It has many features but only a few of them worked for me as described below - I am developing an interactive command prompt application (displays the prompt and accepts the next user command to run). I w

How to install GNU Readline?

Josh Pinto Basically every question. I have the .tar.gzfile but can't seem to install it. I did the whole thing ./configureand makeit make installdidn't work. Anwar You can install it by using the command in the terminal (press install Ctrl- Altpackage-- T). s

Lightweight GNU readline alternative

punekr12 : I am looking for a GNU readline alternative. It has many features but only a few of them worked for me as described below - I am developing an interactive command prompt application (displays the prompt and accepts the next user command to run). I w

How to install GNU Readline?

Josh Pinto Basically every question. I have the .tar.gzfile but can't seem to install it. I did the whole thing ./configureand makeit make installdidn't work. Anwar You can install it by using the command in the terminal (press install Ctrl- Altpackage-- T). s

Using Completion in Term::ReadLine::Gnu

username I want to make a console and change the autocomplete when pressing Tab, but I want to distinguish two cases: If I press the Tab key and the start of the command matches the list I provide in the array, the autocomplete will proceed according to that a

Using Completion in Term::ReadLine::Gnu

username I want to make a console and change the autocomplete when pressing Tab, but I want to distinguish two cases: If I press the Tab key and the start of the command matches the list I provide in the array, the autocomplete will proceed according to that a

Using Completion in Term::ReadLine::Gnu

username I want to make a console and change the autocomplete when pressing Tab, but I want to distinguish two cases: If I press the Tab key and the start of the command matches the list I provide in the array, the autocomplete will proceed according to that a

How to use GNU readline with flex-lexer?

Nicholas R. I think it would be nice to use the GNU Readline library as a command line prompt, and I wish the shell I was using had that capability. Now, readline works for me (my environment is CLion, CMake, Ubuntu, BSD, C, flex-lexer and lemon-parser), but I

Statically linking Go programs with GNU readline

optimal: I'm writing a Go program that uses the GNU readline library for a fancy command line interface. To simplify the installation process without worrying about library versions and other stuff, I would like to link it statically. The problem is that I rea

How to use GNU readline with flex-lexer?

Nicholas R. I think it would be nice to use the GNU Readline library as a command line prompt, and I wish the shell I was using had that capability. Now, readline works for me (my environment is CLion, CMake, Ubuntu, BSD, C, flex-lexer and lemon-parser), but I

How to keep GNU Readline away from Stdout

Jason So I want to make a program that accepts input stdinvia redirection or interactive input. getlineRedirected reading is possible, but I'd like to have all the nice features readlinefor interactive input . The purpose of this program is to manipulate text

How to iterate over possible completions in gnu readline?

Urine In a shell that implements GNU Readline , I can click that key to get a list of things I might want to complete, which is what I start typing. E.g: C:\>cd P<TAB> PerfLogs\ Program Files (x86)\ Python27\ Program Files\ ProgramData\ No

Statically linking Go programs with GNU readline

optimal I'm writing a Go program that uses the GNU readline library for a fancy command line interface. To simplify the installation process without worrying about the version of the library and other stuff, I would like to link it statically. The problem is t

Which popular programs use GNU Readline?

Philip Kirkbride I'm reading about keybindings in Bash. They appear to be Emacs-based, but offer the ability to switch to Emacs- vibased . You can bash set -o viin your .bashrcimplementation, but I see there is also an option to edit a file called .inputrcby a

Statically linking Go programs with GNU readline

optimal: I'm writing a Go program that uses the GNU readline library for a fancy command line interface. To simplify the installation process without worrying about library versions and other stuff, I would like to link it statically. The problem is that I rea

Statically linking Go programs with GNU readline

optimal: I'm writing a Go program that uses the GNU readline library for a fancy command line interface. To simplify the installation process without worrying about library versions and other stuff, I would like to link it statically. The problem is that I rea

How to use GNU readline with flex-lexer?

Nicholas R. I think it would be nice to use the GNU Readline library as a command line prompt, and I wish the shell I was using had that capability. Now, readline works for me (my environment is CLion, CMake, Ubuntu, BSD, C, flex-lexer and lemon-parser), but I

How to keep GNU Readline away from Stdout

Jason So I want to make a program that accepts input stdinvia redirection or interactive input. getlineRedirected reading is possible, but I'd like to have all the nice features readlinefor interactive input . The purpose of this program is to manipulate text

How to keep GNU Readline away from Stdout

Jason So I want to make a program that accepts input stdinvia redirection or interactive input. getlineRedirected reading is possible, but I'd like to have all the nice features readlinefor interactive input . The purpose of this program is to manipulate text

How to use GNU readline with flex-lexer?

Nicholas R. I think it would be nice to use the GNU Readline library as a command line prompt, and I wish the shell I was using had that capability. Now, readline works for me (my environment is CLion, CMake, Ubuntu, BSD, C, flex-lexer and lemon-parser), but I

How to iterate over possible completions in gnu readline?

Pee In a shell that implements GNU Readline , I can click that key to get a list of things I might want to complete, which is what I start typing. E.g: C:\>cd P<TAB> PerfLogs\ Program Files (x86)\ Python27\ Program Files\ ProgramData\ Now

Readline C: Force some text to be returned in readline()

Zach I am trying to allow interrupts to make readline return a certain value. Here is a minimal example: #include <stdio.h> #include <signal.h> #include <readline/readline.h> void handler (int status) { rl_replace_line("word",0); rl_redisplay(); rl_d

Readline C: Force some text to be returned in readline()

Zach I am trying to allow interrupts to make readline return a certain value. Here is a minimal example: #include <stdio.h> #include <signal.h> #include <readline/readline.h> void handler (int status) { rl_replace_line("word",0); rl_redisplay(); rl_d

Readline C: Force some text to be returned in readline()

Zach I am trying to allow interrupts to make readline return a certain value. Here is a minimal example: #include <stdio.h> #include <signal.h> #include <readline/readline.h> void handler (int status) { rl_replace_line("word",0); rl_redisplay(); rl_d

C - Readline() undefined reference

Angelo I'm trying to run this code, but the compiler fails: undefined reference to 'readline' and no reference to 'add_history'. I am using CodeBlocks. Here is my code: #include <stdio.h> #include <stdlib.h> #include <readline/readline.h> #include <readline/h