Skip to content

Commit

Permalink
FIX: better handling of CTRL-C in Windows console
Browse files Browse the repository at this point in the history
Now it's possible to escape from multiline edit and the signaling is not called when CTRL-C is handled from ReadConsole function.
  • Loading branch information
Oldes committed Jan 25, 2019
1 parent 94ed18f commit 900e1dc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/os/host-main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -278,7 +285,7 @@ int main(int argc, char **argv) {

#ifndef REB_CORE
Init_Windows();
Init_Graphics();
//Init_Graphics();
#endif

#ifdef TEST_EXTENSIONS
Expand Down
10 changes: 10 additions & 0 deletions src/os/win32/dev-stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 900e1dc

Please sign in to comment.