GNU Readline: Is there a function to cancel readline input requests?


Ikber

I'm new to GNU Readline, so I'm wondering if there is a function that can cancel the readline()request ?

Hans

To do this, you must use an alternate (or "callback") interface to read lines. In fact, you don't need to cancel anything, you just (temporarily) break out rl_callback_read_charof the loop to do whatever you need to do. This may even happen before the user sends ENTER , but only after the keypress .

#include <stdio.h>
#include <stdlib.h>
#include <readline/readline.h>

void line_handler(char *line) { /* This function (callback) gets called by readline                    
                                   whenever rl_callback_read_char sees an ENTER */
  printf("%s? Hah!!\n", line);
}

int main() {
  rl_callback_handler_install("Ask a question: ", &line_handler);

  while (1) {
    rl_callback_read_char();
    if (strstr(rl_line_buffer, "you")) { /* They're asking about *me* =:-0 */
      printf("\nNo personal questions please! Goodbye!\n");
      break;
      /* or make a snarky remark and continue */
    }
  }
}

If you want to "cancel" without pressing a key, you have to use a signal to interrupt the read()system call rl_callback_read_char()(for example, by setting alarm()). Note, however, that readline installs its own signal handlers .

A slightly more complicated approach would be to insert the select()upper 2 file descriptors into the ring, stdinand for example pipe (in the self-pipe trick ) to use that second descriptor (and/or timeout) to "wake up" select(), and then Break out of the loop like in the example below.

Related


Cancel readLine() of BufferedReader

User 9885271 I wrote an infinite loop and I want to send a user message every 5 seconds. So I wrote a thread that waits 5 seconds and then sends the message received by the readLine() method. If the user does not provide any input, the loop will not continue b

Cancel readLine() of BufferedReader

User 9885271 I wrote an infinite loop and I want to send a user message every 5 seconds. So I wrote a thread that waits 5 seconds and then sends the message received by the readLine() method. If the user does not provide any input, the loop will not continue b

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

Having trouble with readline() user input and survfit function

PyPer users I tried looking at past answers but couldn't find the exact answer. I'm new to R, so please bear with me. I'm doing a lot of functionality that requires user input. I've gathered that the best way to enter user text is via readlines() Right now, th

Having trouble with readline() user input and survfit function

PyPer users I tried looking at past answers but couldn't find the exact answer. I'm new to R, so please bear with me. I'm doing a lot of functionality that requires user input. I've gathered that the best way to enter user text is via readlines() Right now, th

GNU Readline: How to set the initial content of an input string

Tail Colleagues, tell me how to set the initial value of the input line in readline()? Suppose I can highly guess what value the user will enter. Although, there is still the possibility of different values. So I want the input to look like from the beginning

GNU Readline: How to set the initial content of an input string

Tail Colleagues, tell me how to set the initial value of the input line in readline()? Suppose I can highly guess what value the user will enter. Although, there is still the possibility of different values. So I want the input to look like from the beginning

GNU Readline: How to set the initial content of an input string

Tail Colleagues, tell me how to set the initial value of the input line in readline()? Suppose I can highly guess what value the user will enter. Although, there is still the possibility of different values. So I want the input to look like from the beginning

GNU Readline: How to set the initial content of an input string

Tail Colleagues, tell me how to set the initial value of the input line in readline()? Suppose I can highly guess what value the user will enter. Although, there is still the possibility of different values. So I want the input to look like from the beginning

GNU Readline: How to set the initial content of an input string

Tail Colleagues, tell me how to set the initial value of the input line in readline()? Suppose I can highly guess what value the user will enter. Although, there is still the possibility of different values. So I want the input to look like from the beginning

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(); st

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

Problems with readline() function in R

Vivek I want to take user input into a variable and display it on the screen. However, when I try to use the readline() function to accept input, sometimes it works fine, but not always. Sometimes, I get the following error: So, is there any other function tha

Problems with readline() function in R

Vivek I want to take user input into a variable and display it on the screen. However, when I try to use the readline() function to accept input, sometimes it works fine, but not always. Sometimes, I get the following error: So, is there any other function tha

Problems with readline() function in R

Vivek I want to take user input into a variable and display it on the screen. However, when I try to use the readline() function to accept input, sometimes it works fine, but not always. Sometimes, I get the following error: So, is there any other function tha

Problems with readline() function in R

Vivek I want to take user input into a variable and display it on the screen. However, when I try to use the readline() function to accept input, sometimes it works fine, but not always. Sometimes, I get the following error: So, is there any other function tha

Problems with readline() function in R

Vivek I want to take user input into a variable and display it on the screen. However, when I try to use the readline() function to accept input, sometimes it works fine, but not always. Sometimes, I get the following error: So, is there any other function tha

Problems with readline() function in R

Vivek I want to take user input into a variable and display it on the screen. However, when I try to use the readline() function to accept input, sometimes it works fine, but not always. Sometimes, I get the following error: So, is there any other function tha

Problems with readline() function in R

Vivek I want to take user input into a variable and display it on the screen. However, when I try to use the readline() function to accept input, sometimes it works fine, but not always. Sometimes, I get the following error: So, is there any other function tha