I use the GNU Readline in select mode by registering a callback function as follows:
rl_callback_handler_install("", on_readline_input);
And then hook rl_callback_read_charas a callback for the loop select()for STDIN_FILENO. This is all pretty standard stuff and works great.
Now my program prints messages on the screen asynchronously, sometimes alternating with user input. A “clean” session will look like this:
user input
SERVER OUTPUT
SERVER OUTPUT
user input
SERVER OUTPUT
But what if the user is halfway through the line when a server response arrives? Then it gets ugly:
user input
SERVER OUTPUT
user inSERVER OUTPUT
put
SERVER OUTPUT
, , - ( , rl_line_buffer), rl_forced_update_display() . :
user input
SERVER OUTPUT
user in
SERVER OUTPUT
user input
SERVER OUTPUT
, . , , Enter - :
user input
SERVER OUTPUT
user input
SERVER OUTPUT
user input
SERVER OUTPUT
, , ( , , ).
( ) :
user input
SERVER OUTPUT
user input - INCOMPLETE
SERVER OUTPUT
user input
SERVER OUTPUT
, , backspace ('\ b') " - INCOMPLETE", , , (gnome-terminal Ubuntu Hardy). printf("ABC\b"); ABC .
, ? backspaces - ( , - strlen(rl_line_buffer)), Readline, ?