From 87bc3fa0843f308a1b9175b8ae57c03f67a9116e Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Thu, 6 Feb 2020 16:35:50 +0000 Subject: winansi: fix alternate screen buffer in Windows 7 Closing 'vi' in Windows 7 resulted in the console window failing to echo any output correctly until the 'reset' builtin was run. This didn't happen in Windows XP, 8 or 10. The problem is fixed by only duplicating the console handle when switching *to* the alternate screen buffer. Add sanity checks so that switching to the alternate buffer requires the cached handled to be invalid, while it must be valid when switching from the alternate buffer. --- win32/winansi.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'win32') diff --git a/win32/winansi.c b/win32/winansi.c index 41ca3ccca..dcfdd7e1f 100644 --- a/win32/winansi.c +++ b/win32/winansi.c @@ -87,15 +87,16 @@ static void use_alt_buffer(int flag) static HANDLE console_orig = INVALID_HANDLE_VALUE; HANDLE console, h; - console = get_console(); - console_orig = dup_handle(console); - if (console_orig == INVALID_HANDLE_VALUE) - return; - if (flag) { SECURITY_ATTRIBUTES sa; CONSOLE_SCREEN_BUFFER_INFO sbi; + if (console_orig != INVALID_HANDLE_VALUE) + return; + + console = get_console(); + console_orig = dup_handle(console); + // handle should be inheritable memset(&sa, 0, sizeof(sa)); sa.nLength = sizeof(sa); @@ -109,13 +110,16 @@ static void use_alt_buffer(int flag) if (h == INVALID_HANDLE_VALUE) return; - console = get_console(); if (GetConsoleScreenBufferInfo(console, &sbi)) SetConsoleScreenBufferSize(h, sbi.dwSize); } else { + if (console_orig == INVALID_HANDLE_VALUE) + return; + // revert to original buffer h = dup_handle(console_orig); + console_orig = INVALID_HANDLE_VALUE; if (h == INVALID_HANDLE_VALUE) return; } -- cgit v1.2.3-55-g6feb