Skip to content

Commit 900e1dc

Browse files
committed
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.
1 parent 94ed18f commit 900e1dc

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/os/host-main.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ void Host_Repl() {
123123

124124
if (!line) {
125125
// "end of stream" - for example on CTRL+C
126+
if(cont_level > 0) {
127+
// we were in multiline editing, so escape from it only
128+
cont_level = 0;
129+
input_len = 0;
130+
input[0] = 0;
131+
continue;
132+
}
126133
Put_Str(b_cast("\x1B[0m")); //reset console color before leaving
127134
goto cleanup_and_return;
128135
}
@@ -278,7 +285,7 @@ int main(int argc, char **argv) {
278285

279286
#ifndef REB_CORE
280287
Init_Windows();
281-
Init_Graphics();
288+
//Init_Graphics();
282289
#endif
283290

284291
#ifdef TEST_EXTENSIONS

src/os/win32/dev-stdio.c

+10
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ static REBCHR *Std_Buf = 0; // for input and output
7878
static BOOL Redir_Out = 0;
7979
static BOOL Redir_Inp = 0;
8080

81+
static BOOL Handled_Break = 0;
82+
8183
// Since some Windows10 version it's possible to use the new terminal processing,
8284
// of ANSI escape sequences. From my tests its not faster than my emulation, but
8385
// 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);
140142

141143
BOOL WINAPI Handle_Break(DWORD dwCtrlType)
142144
{
145+
//printf("\nHandle_Break %i\n", dwCtrlType);
146+
if(Handled_Break) {
147+
// CTRL-C was catched durring ReadConsoleW and was already processed
148+
Handled_Break = FALSE;
149+
return TRUE;
150+
}
143151
// Handle the MS CMD console CTRL-C, BREAK, and other events:
144152
if (dwCtrlType >= CTRL_CLOSE_EVENT) OS_Exit(100); // close button, shutdown, etc.
145153
RL_Escape(0);
@@ -531,6 +539,8 @@ static void close_stdio(void)
531539
if (ok) {
532540
if (total == 0) {
533541
// CTRL-C pressed
542+
Handled_Break = TRUE; // let the break handler (which is signaled later) to know,
543+
// that we handled it already
534544
SetConsoleTextAttribute(Std_Out, FOREGROUND_INTENSITY | FOREGROUND_MAGENTA);
535545
WriteConsoleW(Std_Out, L"[ESC]\r\n", 7, NULL, 0);
536546
SetConsoleTextAttribute(Std_Out, 0);

0 commit comments

Comments
 (0)