11#include " cpp-terminal/terminal.hpp"
22
3+ #include " cpp-terminal/exception.hpp"
34#include " cpp-terminal/platforms/file.hpp"
45
56#ifdef _WIN32
1516 #define ENABLE_VIRTUAL_TERMINAL_INPUT 0x0200
1617 #endif
1718#else
18- #include < fcntl.h>
1919 #include < termios.h>
20- #include < unistd.h>
2120#endif
2221
23- #include " cpp-terminal/exception.hpp"
24- #include " cpp-terminal/platforms/file.hpp"
2522#include < fstream>
2623
2724void Term::Terminal::store_and_restore ()
@@ -65,22 +62,14 @@ void Term::Terminal::store_and_restore()
6562 static termios orig_termios;
6663 if (!enabled)
6764 {
68- int fd{open (" /dev/tty" , O_RDWR, O_NOCTTY)};
69- if (fd >= 0 )
70- {
71- if (tcgetattr (fd, &orig_termios) == -1 ) { throw Term::Exception (" tcgetattr() failed" ); }
72- }
73- close (fd);
65+ if (Private::std_cout.getHandler () >= 0 )
66+ if (tcgetattr (Private::std_cout.getHandler (), &orig_termios) == -1 ) { throw Term::Exception (" tcgetattr() failed" ); }
7467 enabled = true ;
7568 }
7669 else
7770 {
78- int fd{open (" /dev/tty" , O_RDWR, O_NOCTTY)};
79- if (fd >= 0 )
80- {
81- if (tcsetattr (fd, TCSAFLUSH, &orig_termios) == -1 ) { throw Term::Exception (" tcsetattr() failed in destructor" ); }
82- close (fd);
83- }
71+ if (Private::std_cout.getHandler () >= 0 )
72+ if (tcsetattr (Private::std_cout.getHandler (), TCSAFLUSH, &orig_termios) == -1 ) { throw Term::Exception (" tcsetattr() failed in destructor" ); }
8473 enabled = false ;
8574 }
8675#endif
@@ -89,17 +78,16 @@ void Term::Terminal::store_and_restore()
8978void Term::Terminal::setRawMode ()
9079{
9180#ifdef _WIN32
92- DWORD flags = {0 };
81+ DWORD flags = {0 };
9382 if (!GetConsoleMode (Private::std_cin.getHandler (), &flags)) { throw Term::Exception (" GetConsoleMode() failed" ); }
9483 if (m_options.has (Option::NoSignalKeys)) { flags &= ~ENABLE_PROCESSED_INPUT; }
9584 flags &= ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
9685 if (!SetConsoleMode (Private::std_cin.getHandler (), flags)) { throw Term::Exception (" SetConsoleMode() failed" ); }
9786#else
98- int fd{open (" /dev/tty" , O_RDWR, O_NOCTTY)};
99- if (fd >= 0 )
87+ if (Private::std_cout.getHandler () >= 0 )
10088 {
101- termios raw;
102- if (tcgetattr (fd , &raw) == -1 ) { throw Term::Exception (" tcgetattr() failed" ); }
89+ :: termios raw;
90+ if (tcgetattr (Private::std_cout. getHandler () , &raw) == -1 ) { throw Term::Exception (" tcgetattr() failed" ); }
10391 // Put terminal in raw mode
10492 raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
10593 // This disables output post-processing, requiring explicit \r\n. We
@@ -111,8 +99,7 @@ void Term::Terminal::setRawMode()
11199 if (m_options.has (Option::NoSignalKeys)) { raw.c_lflag &= ~ISIG; }
112100 raw.c_cc [VMIN] = 0 ;
113101 raw.c_cc [VTIME] = 0 ;
114- if (tcsetattr (fd, TCSAFLUSH, &raw) == -1 ) { throw Term::Exception (" tcsetattr() failed" ); }
115- close (fd);
102+ if (tcsetattr (Private::std_cout.getHandler (), TCSAFLUSH, &raw) == -1 ) { throw Term::Exception (" tcsetattr() failed" ); }
116103 }
117104#endif
118105}
@@ -167,10 +154,6 @@ void Term::Terminal::attachStreams()
167154 this ->cin .open (in.c_str (), std::ofstream::in);
168155 if (!this ->cin .is_open ()) this ->cin .open (blackHole.c_str (), std::ofstream::in);
169156 this ->cin .clear ();
170- // cout.clear();
171- // cerr.clear();
172- // clog.clear();
173- // cin.clear();
174157}
175158
176159void Term::Terminal::detachStreams ()
0 commit comments