#ifndef WIN32
#include <errno.h>
+#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/ioctl.h>
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
namespace Msp {
namespace IO {
-Console::Console(unsigned n)
+Console::Console(Stream s):
+ stream(s)
{
- if(n>2)
- throw invalid_argument("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(n==0)
+ 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)
+ if(stream==CIN)
tcsetattr(*handle, TCSADRAIN, &orig_attr);
#endif
}
#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(*handle, &t);
+ if(tcgetattr(*handle, &t)==-1)
+ throw system_error("tcgetattr");
t.c_lflag = (t.c_lflag&~ICANON) | (l?ICANON:0);
- tcsetattr(*handle, TCSADRAIN, &t);
+ // man termios warns that VMIN and VTIME may share indices with VEOF and VEOL
+ if(l)
+ {
+ t.c_cc[VEOF] = orig_attr.c_cc[VEOF];
+ t.c_cc[VEOL] = orig_attr.c_cc[VEOL];
+ }
+ else
+ {
+ t.c_cc[VMIN] = 1;
+ t.c_cc[VTIME] = 0;
+ }
+ if(tcsetattr(*handle, TCSADRAIN, &t)==-1)
+ throw system_error("tcsetattr");
#endif
}
check_access(M_WRITE);
#ifdef WIN32
- // XXX Figure out how to do this
- rows = 24;
- cols = 80;
+ CONSOLE_SCREEN_BUFFER_INFO sbi;
+ if(!GetConsoleScreenBufferInfo(*handle, &sbi))
+ throw system_error("GetConsoleScreenBufferInfo");
+ // Right/bottom coords are inclusive
+ rows = sbi.srWindow.Bottom+1-sbi.srWindow.Top;
+ cols = sbi.srWindow.Right+1-sbi.srWindow.Left;
#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
}
+void Console::redirect(Base &other)
+{
+ Handle other_handle = other.get_handle(mode&M_RDWR);
+#ifdef WIN32
+ SetStdHandle(stream_to_sys(stream), *other_handle);
+#else
+ dup2(*other_handle, *handle);
+#endif
+}
+
unsigned Console::do_write(const char *buf, unsigned len)
{
check_access(M_WRITE);
return ret;
}
-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 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