@@ -68,22 +68,24 @@ bool Term::Private::get_term_size(int& rows, int& cols) {
6868 }
6969#endif
7070}
71+ char Term::Private::read_raw_stdin () {
72+ int c = getchar ();
73+ if (c >= 0 ) {
74+ return c;
75+ } else if (c == EOF) {
76+ // In non-raw (blocking) mode this happens when the input file
77+ // ends. In such a case, return the End of Transmission (EOT)
78+ // character (Ctrl-D)
79+ return 0x04 ;
80+ } else {
81+ throw std::runtime_error (" getchar() failed" );
82+ }
83+ }
7184
7285bool Term::Private::read_raw (char * s) {
73- // TODO: What if the keyboard is not initialized?
74- if (false ) {
75- int c = getchar ();
76- if (c >= 0 ) {
77- *s = c;
78- } else if (c == EOF) {
79- // In non-raw (blocking) mode this happens when the input file
80- // ends. In such a case, return the End of Transmission (EOT)
81- // character (Ctrl-D)
82- *s = 0x04 ;
83- } else {
84- throw std::runtime_error (" getchar() failed" );
85- }
86- return true ;
86+ // do nothing when TTY is not connected
87+ if (!is_stdin_a_tty ()) {
88+ return false ;
8789 }
8890#ifdef _WIN32
8991 HANDLE hin = GetStdHandle (STD_INPUT_HANDLE);
@@ -144,8 +146,9 @@ Term::Private::BaseTerminal::~BaseTerminal() noexcept(false) {
144146Term::Private::BaseTerminal::BaseTerminal (bool enable_keyboard,
145147 bool /* disable_ctrl_c*/ )
146148 : keyboard_enabled{enable_keyboard} {
147- // Uncomment this to silently disable raw mode for non-tty
148- // if (keyboard_enabled) keyboard_enabled = is_stdin_a_tty();
149+ // silently disable raw mode for non-tty
150+ if (keyboard_enabled)
151+ keyboard_enabled = is_stdin_a_tty ();
149152 out_console = is_stdout_a_tty ();
150153 if (out_console) {
151154 hout = GetStdHandle (STD_OUTPUT_HANDLE);
@@ -187,8 +190,9 @@ Term::Private::BaseTerminal::BaseTerminal(bool enable_keyboard,
187190 bool disable_ctrl_c)
188191 : orig_termios{std::make_unique<termios>()},
189192 keyboard_enabled{enable_keyboard} {
190- // Uncomment this to silently disable raw mode for non-tty
191- // if (keyboard_enabled) keyboard_enabled = is_stdin_a_tty();
193+ // silently disable raw mode for non-tty
194+ if (keyboard_enabled)
195+ keyboard_enabled = is_stdin_a_tty ();
192196 if (keyboard_enabled) {
193197 if (tcgetattr (STDIN_FILENO, orig_termios.get ()) == -1 ) {
194198 throw std::runtime_error (" tcgetattr() failed" );
0 commit comments