]> git.tdb.fi Git - libs/core.git/blobdiff - source/io/console.cpp
Implement controls for file descriptor inheritance
[libs/core.git] / source / io / console.cpp
index c846604806aa66e36ea8ce76d37199b7420b4372..a7417edcee8f186263c965363f4942d9b1f82768 100644 (file)
@@ -1,36 +1,7 @@
-#ifndef WIN32
-#include <errno.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <termios.h>
-#include <sys/ioctl.h>
-#endif
-#include <msp/core/systemerror.h>
 #include "console.h"
-#include "handle_private.h"
 
 using namespace std;
 
-namespace {
-
-#ifdef WIN32
-DWORD stream_to_sys(Msp::IO::Console::Stream stream)
-{
-       switch(stream)
-       {
-       case Msp::IO::Console::CIN: return STD_INPUT_HANDLE;
-       case Msp::IO::Console::COUT: return STD_OUTPUT_HANDLE;
-       case Msp::IO::Console::CERR: return STD_ERROR_HANDLE;
-       default: throw invalid_argument("stream_to_sys");
-       }
-}
-
-#else
-termios orig_attr;
-#endif
-
-} // namespace
-
 namespace Msp {
 namespace IO {
 
@@ -39,101 +10,22 @@ Console::Console(Stream s):
 {
        mode = (stream==CIN ? M_READ : M_WRITE);
 
-#ifdef WIN32
-       *handle = GetStdHandle(stream_to_sys(stream));
-#else
-       *handle = stream;
-
-       if(stream==CIN)
-               tcgetattr(*handle, &orig_attr);
-#endif
+       platform_init();
 
        if(stream==CIN)
                set_events(P_INPUT);
 }
 
-Console::~Console()
-{
-#ifndef WIN32
-       if(stream==CIN)
-               tcsetattr(*handle, TCSADRAIN, &orig_attr);
-#endif
-}
-
 void Console::set_block(bool b)
 {
-#ifdef WIN32
-       // XXX Dunno how to do this in win32
-       (void)b;
-#else
-       int flags = fcntl(*handle, F_GETFL);
-       flags = (flags&~O_NONBLOCK) | (b?0:O_NONBLOCK);
-       fcntl(*handle, F_SETFL, flags);
-#endif
-}
-
-void Console::set_local_echo(bool e)
-{
-       check_access(M_READ);
-
-#ifdef WIN32
-       DWORD m;
-       GetConsoleMode(*handle, &m);
-       SetConsoleMode(*handle, (m&~ENABLE_ECHO_INPUT) | (e?ENABLE_ECHO_INPUT:0));
-#else
-       termios t;
-       tcgetattr(*handle, &t);
-       t.c_lflag = (t.c_lflag&~ECHO) | (e?ECHO:0);
-       tcsetattr(*handle, TCSADRAIN, &t);
-#endif
-}
-
-void Console::set_line_buffer(bool l)
-{
-       check_access(M_READ);
-
-#ifdef WIN32
-       DWORD m;
-       if(!GetConsoleMode(*handle, &m))
-               throw system_error("GetConsoleMode");
-       if(!SetConsoleMode(*handle, (m&~ENABLE_LINE_INPUT) | (l?ENABLE_LINE_INPUT:0)))
-               throw system_error("SetConsoleMode");
-#else
-       // XXX ICANON does more than just set line buffering, may need a bit more thought
-       termios t;
-       if(tcgetattr(*handle, &t)==-1)
-               throw system_error("tcgetattr");
-       t.c_lflag = (t.c_lflag&~ICANON) | (l?ICANON:0);
-       if(tcsetattr(*handle, TCSADRAIN, &t)==-1)
-               throw system_error("tcsetattr");
-#endif
-}
-
-void Console::get_size(unsigned &rows, unsigned &cols)
-{
-       check_access(M_WRITE);
-
-#ifdef WIN32
-       // XXX Figure out how to do this
-       rows = 24;
-       cols = 80;
-#else
-       struct winsize wsz;
-       if(ioctl(*handle, TIOCGWINSZ, &wsz)==-1)
-               throw system_error("ioctl TIOCGWINSZ");
-       rows = wsz.ws_row;
-       cols = wsz.ws_col;
-#endif
+       adjust_mode(mode, M_NONBLOCK, !b);
+       sys_set_blocking(handle, b);
 }
 
-void Console::redirect(Base &other)
+void Console::set_inherit(bool i)
 {
-       Handle other_handle = other.get_handle(mode&M_RDWR);
-#ifdef WIN32
-       SetStdHandle(stream_to_sys(stream), *other_handle);
-#else
-       dup2(*other_handle, *handle);
-#endif
+       adjust_mode(mode, M_INHERIT, i);
+       sys_set_inherit(handle, i);
 }
 
 unsigned Console::do_write(const char *buf, unsigned len)
@@ -154,6 +46,12 @@ unsigned Console::do_read(char *buf, unsigned len)
        return ret;
 }
 
+const Handle &Console::get_handle(Mode m)
+{
+       check_access(m);
+       return handle;
+}
+
 Console &Console::instance(Stream s)
 {
        static Console in(CIN);