35 #include "console_windows.h" 36 #include "lib/charval.h" 37 #include "lib/aengine.h" 38 #include "main/evaluator.h" 42 #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING 43 #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
45 #ifndef DISABLE_NEWLINE_AUTO_RETURN 46 #define DISABLE_NEWLINE_AUTO_RETURN 0x0008
48 #ifndef ENABLE_VIRTUAL_TERMINAL_INPUT 49 #define ENABLE_VIRTUAL_TERMINAL_INPUT 0x0200
53 ENABLE_PROCESSED_INPUT 56 ENABLE_PROCESSED_INPUT | 57 ENABLE_VIRTUAL_TERMINAL_INPUT 60 ENABLE_PROCESSED_OUTPUT | 61 ENABLE_WRAP_AT_EOL_OUTPUT 64 ENABLE_PROCESSED_OUTPUT | 65 ENABLE_WRAP_AT_EOL_OUTPUT | 66 ENABLE_VIRTUAL_TERMINAL_PROCESSING | 67 DISABLE_NEWLINE_AUTO_RETURN 70 ENABLE_PROCESSED_OUTPUT | 71 ENABLE_WRAP_AT_EOL_OUTPUT | 72 ENABLE_VIRTUAL_TERMINAL_PROCESSING 75 FOREGROUND_INTENSITY | 80 WindowsConsole::WindowsConsole(
const char* prompt, CharValidator* validator) :
81 ConsoleBase(prompt), line(
nullptr), exit(
false)
83 proc =
new AnsiConoleEngine(prompt, validator);
86 WindowsConsole::~WindowsConsole()
90 void WindowsConsole::Clear()
98 HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
99 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
100 if (in == INVALID_HANDLE_VALUE || out == INVALID_HANDLE_VALUE)
106 SetConsoleMode(in, IN_DEFAULT);
107 SetConsoleMode(out, OUT_DEFAULT);
111 void WindowsConsole::StartMessage()
115 ConsoleBase::StartMessage();
119 CONSOLE_SCREEN_BUFFER_INFO old;
122 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
123 if (out == INVALID_HANDLE_VALUE)
125 ConsoleBase::StartMessage();
129 if (!GetConsoleScreenBufferInfo(out, &old))
131 ConsoleBase::StartMessage();
135 oldColor = old.wAttributes;
137 if (!SetConsoleTextAttribute(out, OUT_WHITE))
139 ConsoleBase::StartMessage();
143 WriteString(INTROMSG);
146 SetConsoleTextAttribute(out, oldColor);
150 void WindowsConsole::ShowVersion()
154 ConsoleBase::ShowVersion();
158 CONSOLE_SCREEN_BUFFER_INFO old;
161 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
162 if (out == INVALID_HANDLE_VALUE)
164 ConsoleBase::ShowVersion();
168 if (!GetConsoleScreenBufferInfo(out, &old))
170 ConsoleBase::ShowVersion();
174 oldColor = old.wAttributes;
176 if (!SetConsoleTextAttribute(out, OUT_WHITE))
178 ConsoleBase::ShowVersion();
182 WriteString(GetVersionText());
185 SetConsoleTextAttribute(out, oldColor);
187 WriteString(NEWLINE);
188 WriteString(GetCompilerText());
189 WriteString(NEWLINE);
194 bool WindowsConsole::Open()
196 HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
197 if (in == INVALID_HANDLE_VALUE)
202 if (!GetConsoleMode(in, &oldInMode))
207 if (!SetConsoleMode(in, IN_DEFAULT))
212 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
213 if (out == INVALID_HANDLE_VALUE)
218 if (!GetConsoleMode(out, &oldOutMode))
223 if (!SetConsoleMode(out, OUT_DEFAULT))
231 void WindowsConsole::Close()
233 HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
234 if (in == INVALID_HANDLE_VALUE)
238 SetConsoleMode(in, oldInMode);
240 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
241 if (out == INVALID_HANDLE_VALUE)
245 SetConsoleMode(in, oldOutMode);
248 bool WindowsConsole::SetAnsiMode(
bool value)
250 HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
251 if (in == INVALID_HANDLE_VALUE)
253 ConsoleBase::SetAnsiMode(
false);
257 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
258 if (out == INVALID_HANDLE_VALUE)
260 ConsoleBase::SetAnsiMode(
false);
267 SetConsoleMode(in, IN_DEFAULT);
268 SetConsoleMode(out, OUT_DEFAULT);
269 ConsoleBase::SetAnsiMode(
false);
277 if (!SetConsoleMode(out, OUT_ANSI1))
279 if (!SetConsoleMode(out, OUT_ANSI2))
285 if (!SetConsoleMode(in, IN_ANSI))
292 SetConsoleMode(in, IN_DEFAULT);
293 SetConsoleMode(out, OUT_DEFAULT);
297 ConsoleBase::SetAnsiMode(success);
302 void WindowsConsole::Start()
311 Evaluator* evaluator =
new Evaluator(line);
312 evaluator->Evaluate();
313 const char* res = evaluator->GetResult();
314 Write(res, StrLen(res));
320 void WindowsConsole::Exit()
325 void WindowsConsole::SetPrompt(
const char* string)
327 ConsoleBase::SetPrompt(string);
328 proc->SetPrompt(string);
331 void WindowsConsole::ReadLine()
334 HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
335 if (in == INVALID_HANDLE_VALUE)
342 while (!proc->InputDone())
344 ReadConsole(in, &inchar, 1, &count,
nullptr);
345 const char* out = proc->ProcessChar(inchar);
349 line = proc->GetLine();
352 void WindowsConsole::WriteString(
const char* string)
354 Write(string, StrLen(string));
357 void WindowsConsole::Write(
const char* string,
unsigned int length)
360 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
361 if (out == INVALID_HANDLE_VALUE)
366 WriteConsoleA(out, string, length, &count,
nullptr);