From 900e1dc894b85cc7020f828108ce11c4ce50e07f Mon Sep 17 00:00:00 2001 From: Oldes Date: Fri, 25 Jan 2019 17:18:49 +0100 Subject: [PATCH] FIX: better handling of CTRL-C in Windows console Now it's possible to escape from multiline edit and the signaling is not called when CTRL-C is handled from ReadConsole function. --- src/os/host-main.c | 9 ++++++++- src/os/win32/dev-stdio.c | 10 ++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/os/host-main.c b/src/os/host-main.c index 7d676a6288..c64a7ca43e 100644 --- a/src/os/host-main.c +++ b/src/os/host-main.c @@ -123,6 +123,13 @@ void Host_Repl() { if (!line) { // "end of stream" - for example on CTRL+C + if(cont_level > 0) { + // we were in multiline editing, so escape from it only + cont_level = 0; + input_len = 0; + input[0] = 0; + continue; + } Put_Str(b_cast("\x1B[0m")); //reset console color before leaving goto cleanup_and_return; } @@ -278,7 +285,7 @@ int main(int argc, char **argv) { #ifndef REB_CORE Init_Windows(); - Init_Graphics(); + //Init_Graphics(); #endif #ifdef TEST_EXTENSIONS diff --git a/src/os/win32/dev-stdio.c b/src/os/win32/dev-stdio.c index 8105839193..85de3bc549 100644 --- a/src/os/win32/dev-stdio.c +++ b/src/os/win32/dev-stdio.c @@ -78,6 +78,8 @@ static REBCHR *Std_Buf = 0; // for input and output static BOOL Redir_Out = 0; static BOOL Redir_Inp = 0; +static BOOL Handled_Break = 0; + // Since some Windows10 version it's possible to use the new terminal processing, // of ANSI escape sequences. From my tests its not faster than my emulation, but // may be used for functionalities which are not handled in the emulation. @@ -140,6 +142,12 @@ const REBYTE* Parse_ANSI_sequence(const REBYTE *cp, const REBYTE *ep); BOOL WINAPI Handle_Break(DWORD dwCtrlType) { + //printf("\nHandle_Break %i\n", dwCtrlType); + if(Handled_Break) { + // CTRL-C was catched durring ReadConsoleW and was already processed + Handled_Break = FALSE; + return TRUE; + } // Handle the MS CMD console CTRL-C, BREAK, and other events: if (dwCtrlType >= CTRL_CLOSE_EVENT) OS_Exit(100); // close button, shutdown, etc. RL_Escape(0); @@ -531,6 +539,8 @@ static void close_stdio(void) if (ok) { if (total == 0) { // CTRL-C pressed + Handled_Break = TRUE; // let the break handler (which is signaled later) to know, + // that we handled it already SetConsoleTextAttribute(Std_Out, FOREGROUND_INTENSITY | FOREGROUND_MAGENTA); WriteConsoleW(Std_Out, L"[ESC]\r\n", 7, NULL, 0); SetConsoleTextAttribute(Std_Out, 0);