]> git.tdb.fi Git - libs/core.git/blobdiff - source/io/console.cpp
Windows compilation fixes
[libs/core.git] / source / io / console.cpp
index 9db77377f0c2267ea2997808764b607d36ffcbea..c846604806aa66e36ea8ce76d37199b7420b4372 100644 (file)
@@ -1,15 +1,31 @@
 #ifndef WIN32
 #include <errno.h>
+#include <unistd.h>
 #include <fcntl.h>
 #include <termios.h>
 #include <sys/ioctl.h>
 #endif
-#include <msp/core/except.h>
+#include <msp/core/systemerror.h>
 #include "console.h"
+#include "handle_private.h"
+
+using namespace std;
 
 namespace {
 
-#ifndef WIN32
+#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
 
@@ -18,36 +34,29 @@ termios orig_attr;
 namespace Msp {
 namespace IO {
 
-Console::Console(unsigned n)
+Console::Console(Stream s):
+       stream(s)
 {
-       if(n>2)
-               throw InvalidParameterValue("Invalid parameter for Console::Console");
-
-       mode = (n==0 ? M_READ : M_WRITE);
+       mode = (stream==CIN ? M_READ : M_WRITE);
 
 #ifdef WIN32
-       switch(n)
-       {
-       case 0: handle = GetStdHandle(STD_INPUT_HANDLE); break;
-       case 1: handle = GetStdHandle(STD_OUTPUT_HANDLE); break;
-       case 2: handle = GetStdHandle(STD_ERROR_HANDLE); break;
-       }
+       *handle = GetStdHandle(stream_to_sys(stream));
 #else
-       handle = n;
+       *handle = stream;
 
-       if(handle==0)
-               tcgetattr(handle, &orig_attr);
+       if(stream==CIN)
+               tcgetattr(*handle, &orig_attr);
 #endif
 
-       if(n==0)
+       if(stream==CIN)
                set_events(P_INPUT);
 }
 
 Console::~Console()
 {
 #ifndef WIN32
-       if(handle==0)
-               tcsetattr(handle, TCSADRAIN, &orig_attr);
+       if(stream==CIN)
+               tcsetattr(*handle, TCSADRAIN, &orig_attr);
 #endif
 }
 
@@ -57,51 +66,52 @@ void Console::set_block(bool b)
        // XXX Dunno how to do this in win32
        (void)b;
 #else
-       int flags = fcntl(0, F_GETFL);
+       int flags = fcntl(*handle, F_GETFL);
        flags = (flags&~O_NONBLOCK) | (b?0:O_NONBLOCK);
-       fcntl(0, F_SETFL, flags);
+       fcntl(*handle, F_SETFL, flags);
 #endif
 }
 
 void Console::set_local_echo(bool e)
 {
-       if(!(mode&M_READ))
-               throw InvalidState("Local echo can only be set on input console");
+       check_access(M_READ);
 
 #ifdef WIN32
        DWORD m;
-       GetConsoleMode(handle, &m);
-       SetConsoleMode(handle, (m&~ENABLE_ECHO_INPUT) | (e?ENABLE_ECHO_INPUT:0));
+       GetConsoleMode(*handle, &m);
+       SetConsoleMode(*handle, (m&~ENABLE_ECHO_INPUT) | (e?ENABLE_ECHO_INPUT:0));
 #else
        termios t;
-       tcgetattr(0, &t);
+       tcgetattr(*handle, &t);
        t.c_lflag = (t.c_lflag&~ECHO) | (e?ECHO:0);
-       tcsetattr(0, TCSADRAIN, &t);
+       tcsetattr(*handle, TCSADRAIN, &t);
 #endif
 }
 
 void Console::set_line_buffer(bool l)
 {
-       if(!(mode&M_READ))
-               throw InvalidState("Line buffering can only be set on input console");
+       check_access(M_READ);
 
 #ifdef WIN32
        DWORD m;
-       GetConsoleMode(handle, &m);
-       SetConsoleMode(handle, (m&~ENABLE_LINE_INPUT) | (l?ENABLE_LINE_INPUT:0));
+       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;
-       tcgetattr(0, &t);
+       if(tcgetattr(*handle, &t)==-1)
+               throw system_error("tcgetattr");
        t.c_lflag = (t.c_lflag&~ICANON) | (l?ICANON:0);
-       tcsetattr(0, TCSADRAIN, &t);
+       if(tcsetattr(*handle, TCSADRAIN, &t)==-1)
+               throw system_error("tcsetattr");
 #endif
 }
 
 void Console::get_size(unsigned &rows, unsigned &cols)
 {
-       if(!(mode&M_WRITE))
-               throw InvalidState("Size can only be queried from an output console");
+       check_access(M_WRITE);
 
 #ifdef WIN32
        // XXX Figure out how to do this
@@ -109,79 +119,60 @@ void Console::get_size(unsigned &rows, unsigned &cols)
        cols = 80;
 #else
        struct winsize wsz;
-       ioctl(handle, TIOCGWINSZ, &wsz);
+       if(ioctl(*handle, TIOCGWINSZ, &wsz)==-1)
+               throw system_error("ioctl TIOCGWINSZ");
        rows = wsz.ws_row;
        cols = wsz.ws_col;
 #endif
 }
 
-unsigned Console::do_write(const char *buf, unsigned len)
+void Console::redirect(Base &other)
 {
-       if(!(mode&M_WRITE))
-               throw InvalidState("Console is not writable");
-
+       Handle other_handle = other.get_handle(mode&M_RDWR);
 #ifdef WIN32
-       DWORD ret;
-       if(!WriteFile(handle, buf, len, &ret, 0))
-               throw SystemError("Writing to console failed", GetLastError());
+       SetStdHandle(stream_to_sys(stream), *other_handle);
 #else
-       int ret = ::write(handle, buf, len);
-       if(ret==-1)
-               throw SystemError("Writing to console failed", errno);
+       dup2(*other_handle, *handle);
 #endif
+}
 
-       return ret;
+unsigned Console::do_write(const char *buf, unsigned len)
+{
+       check_access(M_WRITE);
+
+       return sys_write(handle, buf, len);
 }
 
 unsigned Console::do_read(char *buf, unsigned len)
 {
-       if(!(mode&M_READ))
-               throw InvalidState("Console is not readable");
+       check_access(M_READ);
 
-#ifdef WIN32
-       DWORD ret;
-       if(!ReadFile(handle, buf, len, &ret, 0))
-               throw SystemError("Reading from console failed", GetLastError());
-#else
-       int ret = ::read(handle, buf, len);
-       if(ret==-1)
-       {
-               if(errno==EAGAIN)
-                       return 0;
-               else
-                       throw SystemError("Reading from console failed", errno);
-       }
-       else if(ret==0)
-               eof_flag = true;
-#endif
+       unsigned ret = sys_read(handle, buf, len);
+       if(ret==0)
+               set_eof();
 
        return ret;
 }
 
-Handle Console::get_event_handle()
-{
-       return 0;
-}
-
-Console &Console::instance(unsigned n)
+Console &Console::instance(Stream s)
 {
-       static Console in(0);
-       static Console out(1);
-       static Console err(2);
+       static Console in(CIN);
+       static Console out(COUT);
+       static Console err(CERR);
 
-       switch(n)
+       switch(s)
        {
-       case 0: return in;
-       case 1: return out;
-       case 2: return err;
+       case CIN: return in;
+       case COUT: return out;
+       case CERR: return err;
        }
 
-       throw InvalidParameterValue("Unknown Console instance requested");
+       throw invalid_argument("Console::instance");
 }
 
-Console &cin = Console::instance(0);
-Console &cout = Console::instance(1);
-Console &cerr = Console::instance(2);
+Console &cin = Console::instance(Console::CIN);
+Console &cout = Console::instance(Console::COUT);
+Console &cerr = Console::instance(Console::CERR);
 
 } // namespace IO
 } // namespace Msp