7 #include <msp/core/systemerror.h>
9 #include "handle_private.h"
16 DWORD stream_to_sys(Console::Stream stream)
20 case INPUT: return STD_INPUT_HANDLE;
21 case OUTPUT: return STD_OUTPUT_HANDLE;
22 case ERROR: return STD_ERROR_HANDLE;
23 default: throw invalid_argument("stream_to_sys");
36 Console::Console(Stream s):
39 mode = (stream==INPUT ? M_READ : M_WRITE);
42 *handle = GetStdHandle(stream_to_sys(stream));
47 tcgetattr(*handle, &orig_attr);
58 tcsetattr(*handle, TCSADRAIN, &orig_attr);
62 void Console::set_block(bool b)
65 // XXX Dunno how to do this in win32
68 int flags = fcntl(*handle, F_GETFL);
69 flags = (flags&~O_NONBLOCK) | (b?0:O_NONBLOCK);
70 fcntl(*handle, F_SETFL, flags);
74 void Console::set_local_echo(bool e)
80 GetConsoleMode(*handle, &m);
81 SetConsoleMode(*handle, (m&~ENABLE_ECHO_INPUT) | (e?ENABLE_ECHO_INPUT:0));
84 tcgetattr(*handle, &t);
85 t.c_lflag = (t.c_lflag&~ECHO) | (e?ECHO:0);
86 tcsetattr(*handle, TCSADRAIN, &t);
90 void Console::set_line_buffer(bool l)
96 if(!GetConsoleMode(*handle, &m))
97 throw system_error("GetConsoleMode");
98 if(!SetConsoleMode(*handle, (m&~ENABLE_LINE_INPUT) | (l?ENABLE_LINE_INPUT:0)))
99 throw system_error("SetConsoleMode");
101 // XXX ICANON does more than just set line buffering, may need a bit more thought
103 if(tcgetattr(*handle, &t)==-1)
104 throw system_error("tcgetattr");
105 t.c_lflag = (t.c_lflag&~ICANON) | (l?ICANON:0);
106 if(tcsetattr(*handle, TCSADRAIN, &t)==-1)
107 throw system_error("tcsetattr");
111 void Console::get_size(unsigned &rows, unsigned &cols)
113 check_access(M_WRITE);
116 // XXX Figure out how to do this
121 if(ioctl(*handle, TIOCGWINSZ, &wsz)==-1)
122 throw system_error("ioctl TIOCGWINSZ");
128 unsigned Console::do_write(const char *buf, unsigned len)
130 check_access(M_WRITE);
132 return sys_write(handle, buf, len);
135 unsigned Console::do_read(char *buf, unsigned len)
137 check_access(M_READ);
139 unsigned ret = sys_read(handle, buf, len);
146 Console &Console::instance(Stream s)
148 static Console in(INPUT);
149 static Console out(OUTPUT);
150 static Console err(ERROR);
154 case INPUT: return in;
155 case OUTPUT: return out;
156 case ERROR: return err;
159 throw invalid_argument("Console::instance");
162 Console &cin = Console::instance(Console::INPUT);
163 Console &cout = Console::instance(Console::OUTPUT);
164 Console &cerr = Console::instance(Console::ERROR);