yhirose/cpp-linenoise

Cannot work with stdin after linenoise

egorpugin opened this issue · 4 comments

Hi,

I have following problem.
After I stop linenoise stdin is closed or something like this.
See https://github.com/cppan/cppan/blob/master/src/client/init.cpp#L130-L134
When while() loop is stopped, I need to read from stdin again, but I can't. (Tried this on linux/win32.)
Program can react only on ctrl+c.

Linenoise register at_exit callback to restore standard streams behavior. But I need to restore it immediately after while loop.
Probably it could be done by adding separate function for this.

Briefly:

while (!linenoise::Readline(...)) { ... }
cin >> myvar; // does not work!!

We need:

while (!linenoise::Readline(...)) { ... }
linenoise::RestoreStdinBehavior(...);
cin >> myvar; // works!!

@egorpugin, thanks for the report. I am trying to understand the problem fully.

This is my test problem.

#include <iostream>
#include "../linenoise.hpp"

using namespace std;

int main(int argc, const char** argv) {
  string line;
  while (!linenoise::Readline("> ", line)) {
    cout << line << endl;
  }
  string myvar;
  cin >> myvar;  // does not work!!
  cout << myvar << endl;
}

When I run it on my Mac OS and get out of the while loop with CTRL+C, I can successfully get another input at cin >> myvar.

Here is my result on my terminal.

> aaa // Input in the while loop
aaa
> // CTRL+C
bbb // Input at `cin >> myvar;`
bbb

What do you think I am missing to reproduce the problem?
Thank you for your help.

This example is broken on windows. That's the issue.
I cannot enter bbb.

> aaa // input
aaa
> // (on windows it's ESC button)
^C // cannot enter anything, only way to stop it - interrupt - CTRL+C

@egorpugin, sorry for the delay. I have just fixed the problem on Windows 10 on VirtualBox on my MacBook.
Please let me know if you still have the issue.

Works fine, thanks!