7 #include <msp/core/systemerror.h>
9 #include "handle_private.h"
24 Console::Console(unsigned n)
27 throw invalid_argument("Console::Console");
29 mode = (n==0 ? M_READ : M_WRITE);
34 case 0: *handle = GetStdHandle(STD_INPUT_HANDLE); break;
35 case 1: *handle = GetStdHandle(STD_OUTPUT_HANDLE); break;
36 case 2: *handle = GetStdHandle(STD_ERROR_HANDLE); break;
42 tcgetattr(*handle, &orig_attr);
53 tcsetattr(*handle, TCSADRAIN, &orig_attr);
57 void Console::set_block(bool b)
60 // XXX Dunno how to do this in win32
63 int flags = fcntl(*handle, F_GETFL);
64 flags = (flags&~O_NONBLOCK) | (b?0:O_NONBLOCK);
65 fcntl(*handle, F_SETFL, flags);
69 void Console::set_local_echo(bool e)
72 throw invalid_access(M_READ);
76 GetConsoleMode(*handle, &m);
77 SetConsoleMode(*handle, (m&~ENABLE_ECHO_INPUT) | (e?ENABLE_ECHO_INPUT:0));
80 tcgetattr(*handle, &t);
81 t.c_lflag = (t.c_lflag&~ECHO) | (e?ECHO:0);
82 tcsetattr(*handle, TCSADRAIN, &t);
86 void Console::set_line_buffer(bool l)
89 throw invalid_access(M_READ);
93 GetConsoleMode(*handle, &m);
94 SetConsoleMode(*handle, (m&~ENABLE_LINE_INPUT) | (l?ENABLE_LINE_INPUT:0));
96 // XXX ICANON does more than just set line buffering, may need a bit more thought
98 tcgetattr(*handle, &t);
99 t.c_lflag = (t.c_lflag&~ICANON) | (l?ICANON:0);
100 tcsetattr(*handle, TCSADRAIN, &t);
104 void Console::get_size(unsigned &rows, unsigned &cols)
107 throw invalid_access(M_WRITE);
110 // XXX Figure out how to do this
115 ioctl(*handle, TIOCGWINSZ, &wsz);
121 unsigned Console::do_write(const char *buf, unsigned len)
124 throw invalid_access(M_WRITE);
128 if(!WriteFile(*handle, buf, len, &ret, 0))
129 throw system_error("WriteFile");
131 int ret = ::write(*handle, buf, len);
133 throw system_error("write");
139 unsigned Console::do_read(char *buf, unsigned len)
142 throw invalid_access(M_READ);
146 if(!ReadFile(*handle, buf, len, &ret, 0))
147 throw system_error("ReadFile");
149 int ret = ::read(*handle, buf, len);
155 throw system_error("read");
164 Console &Console::instance(unsigned n)
166 static Console in(0);
167 static Console out(1);
168 static Console err(2);
177 throw invalid_argument("Console::instance");
180 Console &cin = Console::instance(0);
181 Console &cout = Console::instance(1);
182 Console &cerr = Console::instance(2);