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)
75 GetConsoleMode(*handle, &m);
76 SetConsoleMode(*handle, (m&~ENABLE_ECHO_INPUT) | (e?ENABLE_ECHO_INPUT:0));
79 tcgetattr(*handle, &t);
80 t.c_lflag = (t.c_lflag&~ECHO) | (e?ECHO:0);
81 tcsetattr(*handle, TCSADRAIN, &t);
85 void Console::set_line_buffer(bool l)
91 GetConsoleMode(*handle, &m);
92 SetConsoleMode(*handle, (m&~ENABLE_LINE_INPUT) | (l?ENABLE_LINE_INPUT:0));
94 // XXX ICANON does more than just set line buffering, may need a bit more thought
96 tcgetattr(*handle, &t);
97 t.c_lflag = (t.c_lflag&~ICANON) | (l?ICANON:0);
98 tcsetattr(*handle, TCSADRAIN, &t);
102 void Console::get_size(unsigned &rows, unsigned &cols)
104 check_access(M_WRITE);
107 // XXX Figure out how to do this
112 ioctl(*handle, TIOCGWINSZ, &wsz);
118 unsigned Console::do_write(const char *buf, unsigned len)
120 check_access(M_WRITE);
122 return sys_write(handle, buf, len);
125 unsigned Console::do_read(char *buf, unsigned len)
127 check_access(M_READ);
129 unsigned ret = sys_read(handle, buf, len);
136 Console &Console::instance(unsigned n)
138 static Console in(0);
139 static Console out(1);
140 static Console err(2);
149 throw invalid_argument("Console::instance");
152 Console &cin = Console::instance(0);
153 Console &cout = Console::instance(1);
154 Console &cerr = Console::instance(2);