how to output messages to stdout on another thread while reading from stdin with GNU readline without messing up the inputs?

StackOverflow https://stackoverflow.com/questions/5068846

Question

Sorry for the long title. I am developing a network program in C which may display messages received from network on stdout and accept user input on stdin via the GNU readline library. The problem is, when the user is typing commands on the main thread via readline, a network message arrives and output to stdout, which will produce something like this:

Scenario:

Input: 1234567890
Network message: Hello
The network message arrives when the user just typed "7"

Actual output on terminal:

Input> 1234567Hello
890_

Is there a way to have the output like this?

Hello
Input> 1234567890_

p.s. _ is the cursor.

Thanks in advance!

Was it helpful?

Solution

OK I have found a solution to this after searching around and made the following replacement to printf(). By using rl_replace_line("", 0) it will clear the current line and place the cursor at the start of line, then I can print a line of message, and then restore the readline prompt, replace the original line back in place, and restore the cursor position.
However, this hack requires any call to this printf function to include a \n at the end, otherwise the line will be overwritten by readline.

#define printf(...) my_rl_printf(__VA_ARGS__)
void my_rl_printf(char *fmt, ...)
{
    int need_hack = (rl_readline_state & RL_STATE_READCMD) > 0;
    char *saved_line;
    int saved_point;
    if (need_hack)
    {
        saved_point = rl_point;
        saved_line = rl_copy_text(0, rl_end);
        rl_save_prompt();
        rl_replace_line("", 0);
        rl_redisplay();
    }

    va_list args;
    va_start(args, fmt);
    vprintf(fmt, args);
    va_end(args);

    if (need_hack)
    {
        rl_restore_prompt();
        rl_replace_line(saved_line, 0);
        rl_point = saved_point;
        rl_redisplay();
        free(saved_line);
    }
}

OTHER TIPS

Dump readline and read every char the user is inputting. If you get any network messages while the user is inputting clear the current line, output the network message, then reprint the line the user is currently inputting.

That might seem like a lot of work, but it's the only way that comes to my mind right now.

If you do dump readline then you can use curses which makes it easier...

You should put some kind of synchronization between those two threads. A mutex lock or just a flag from one thread to another indicating that it is working on the stdin or stdout.

This can be done using the curses(3) library.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top