Cygwin: pty: Fix screen alternation while pseudo console switching.

- If screen alternated while pseudo console switching, it sometimes
  failed. This might happen when the output of the non-cygwin program
  is piped to less. This patch fixes this issue.
This commit is contained in:
Takashi Yano 2019-09-08 22:23:23 +09:00 committed by Ken Brown
parent b088f50426
commit bd31b7c5d5

View File

@ -73,6 +73,7 @@ struct pipe_reply {
static int pcon_attached_to = -1; static int pcon_attached_to = -1;
static bool isHybrid; static bool isHybrid;
static bool do_not_reset_switch_to_pcon;
#if USE_API_HOOK #if USE_API_HOOK
static void static void
@ -1046,6 +1047,8 @@ fhandler_pty_slave::reset_switch_to_pcon (void)
init_console_handler (true); init_console_handler (true);
return; return;
} }
if (do_not_reset_switch_to_pcon)
return;
if (get_ttyp ()->switch_to_pcon) if (get_ttyp ()->switch_to_pcon)
{ {
DWORD mode; DWORD mode;
@ -1108,6 +1111,8 @@ fhandler_pty_slave::push_to_pcon_screenbuffer (const char *ptr, size_t len)
{ {
//p0 += 8; //p0 += 8;
get_ttyp ()->screen_alternated = true; get_ttyp ()->screen_alternated = true;
if (get_ttyp ()->switch_to_pcon)
do_not_reset_switch_to_pcon = true;
} }
} }
if (get_ttyp ()->screen_alternated) if (get_ttyp ()->screen_alternated)
@ -1118,6 +1123,7 @@ fhandler_pty_slave::push_to_pcon_screenbuffer (const char *ptr, size_t len)
{ {
p1 += 8; p1 += 8;
get_ttyp ()->screen_alternated = false; get_ttyp ()->screen_alternated = false;
do_not_reset_switch_to_pcon = false;
memmove (p0, p1, buf+nlen - p1); memmove (p0, p1, buf+nlen - p1);
nlen -= p1 - p0; nlen -= p1 - p0;
} }
@ -1177,7 +1183,8 @@ fhandler_pty_slave::push_to_pcon_screenbuffer (const char *ptr, size_t len)
p += wLen; p += wLen;
} }
/* Detach from pseudo console and resume. */ /* Detach from pseudo console and resume. */
SetConsoleMode (get_output_handle (), dwMode); flags = ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode (get_output_handle (), dwMode | flags);
cleanup: cleanup:
SetConsoleOutputCP (origCP); SetConsoleOutputCP (origCP);
HeapFree (GetProcessHeap (), 0, buf); HeapFree (GetProcessHeap (), 0, buf);
@ -1267,7 +1274,7 @@ fhandler_pty_slave::write (const void *ptr, size_t len)
HeapFree (GetProcessHeap (), 0, buf); HeapFree (GetProcessHeap (), 0, buf);
flags = ENABLE_VIRTUAL_TERMINAL_PROCESSING; flags = ENABLE_VIRTUAL_TERMINAL_PROCESSING;
if (get_ttyp ()->switch_to_pcon && !fallback) if (get_ttyp ()->switch_to_pcon && !fallback)
SetConsoleMode (get_output_handle (), dwMode); SetConsoleMode (get_output_handle (), dwMode | flags);
restore_reattach_pcon (); restore_reattach_pcon ();
@ -2899,12 +2906,11 @@ fhandler_pty_slave::fixup_after_attach (bool native_maybe)
{ {
FlushConsoleInputBuffer (get_handle ()); FlushConsoleInputBuffer (get_handle ());
DWORD mode; DWORD mode;
GetConsoleMode (get_handle (), &mode); mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
SetConsoleMode (get_handle (), SetConsoleMode (get_output_handle (), mode);
(mode & ~ENABLE_VIRTUAL_TERMINAL_INPUT) | FlushConsoleInputBuffer (get_handle ());
ENABLE_ECHO_INPUT | mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;
ENABLE_LINE_INPUT | SetConsoleMode (get_handle (), mode);
ENABLE_PROCESSED_INPUT);
Sleep (20); Sleep (20);
if (get_ttyp ()->pcon_pid == 0 || if (get_ttyp ()->pcon_pid == 0 ||
kill (get_ttyp ()->pcon_pid, 0) != 0) kill (get_ttyp ()->pcon_pid, 0) != 0)