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