antirez/linenoise

Cannot paste multiple lines into the example program.

Opened this issue · 1 comments

Highlight and copy the 10 lines below:

This is line 1.
This is line 2.
This is line 3.
This is line 4.
This is line 5.
This is line 6.
This is line 7.
This is line 8.
This is line 9.
This is line 10.

Now run the example program and paste these10 lines into it. This is what you get:

$ ./linenoise_example
hello> This is line 1.
echo: 'This is line 1.'
hello>

Only the first line is received. Lines 2 - 9 are ignored. Each line should be printed with a prompt and a response. Pasting short code snippets into a REPL is a very common thing to do.

I think I found the explanation for this problem in the linux termios man page where it explains the TCSAFLUSH flag to tcsetattr:

      the change occurs after all output written to the object
          referred by fd has been transmitted, and all input that
          has been received but not read will be discarded before
          the change is made.

That flag is used in disableRawMode. So everything in the input buffer after the first newline gets discarded when disableRawMode is called after processing the first line.

The fix for this appears to be to use TCSADRAIN instead of TCSAFLUSH. That drains the output but does not discard the input.