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);
126 return sys_write(handle, buf, len);
129 unsigned Console::do_read(char *buf, unsigned len)
132 throw invalid_access(M_READ);
134 unsigned ret = sys_read(handle, buf, len);
141 Console &Console::instance(unsigned n)
143 static Console in(0);
144 static Console out(1);
145 static Console err(2);
154 throw invalid_argument("Console::instance");
157 Console &cin = Console::instance(0);
158 Console &cout = Console::instance(1);
159 Console &cerr = Console::instance(2);