Skip to content

Commit

Permalink
Legacy apps that do not know about ENABLE_TERMINAL_INPUT will hang lo…
Browse files Browse the repository at this point in the history
…oking for VK_xx codes in the INPUT_RECORD during ReadConsoleInput. This change resets the console to non-VT mode before launching a child process.
  • Loading branch information
amoldeshpande committed Jul 21, 2023
1 parent 74903b2 commit e77bbcc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
20 changes: 20 additions & 0 deletions win32/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,26 @@ void nt_set_win10_vt_mode() {
nt_is_windows_10_or_greater = TRUE;
}
}
void nt_unset_win10_vt_mode() {
if (IsWindows10OrGreater()) {
DWORD dwmode;
if (!GetConsoleMode(ghstdout, &dwmode)) {
dprintf("getconsole mode failed %d\n",GetLastError());
return;
}
if (!SetConsoleMode(ghstdout, dwmode & ~ENABLE_VIRTUAL_TERMINAL_PROCESSING)) {
return;
}
if (!GetConsoleMode(ghstdin, &dwmode)) {
dprintf("getconsole mode for input failed %d\n",GetLastError());
return;
}
if (!SetConsoleMode(ghstdin, dwmode & ~(ENABLE_VIRTUAL_TERMINAL_INPUT | ENABLE_WINDOW_INPUT))) {
return;
}
nt_is_windows_10_or_greater = TRUE;
}
}

void nt_term_init() {

Expand Down
16 changes: 9 additions & 7 deletions win32/ntfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ void dostart(Char ** vc, struct command *c) {
*/

savepath = fix_path_for_child();
nt_unset_win10_vt_mode();

if (! CreateProcess(NULL,
cmdstr,
Expand Down Expand Up @@ -310,14 +311,14 @@ void dostart(Char ** vc, struct command *c) {
}
}
else {
// reset vt mode in case child process has messed with the console
nt_set_win10_vt_mode();
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);

heap_free(cmdstr);
restore_path(savepath);
}
// reset vt mode in case child process has messed with the console
nt_set_win10_vt_mode();
blkfree((Char **)v);
return;
}
Expand Down Expand Up @@ -489,6 +490,7 @@ int nt_feed_to_cmd(char *file,char **argv) {
}

ptr = fix_path_for_child();
nt_unset_win10_vt_mode();

if (!CreateProcess(NULL,
cmdbuf,
Expand All @@ -508,11 +510,10 @@ int nt_feed_to_cmd(char *file,char **argv) {
WaitForSingleObject(pi.hProcess,INFINITE);
CloseHandle(pi.hProcess);

// reset vt mode in case child process has messed with the console
nt_set_win10_vt_mode();

ExitProcess(0);
}
// reset vt mode in case child process has messed with the console
nt_set_win10_vt_mode();

return 1; /*NOTREACHED*/
}
Expand Down Expand Up @@ -1013,6 +1014,7 @@ int nt_texec(char *prog, char**args ) {
}while(retries < 3);
}
savepath = fix_path_for_child();
nt_unset_win10_vt_mode();
re_cp:
dprintf("nt_texec cmdstr %s\n",cmdstr);

Expand Down Expand Up @@ -1057,13 +1059,13 @@ int nt_texec(char *prog, char**args ) {
WaitForSingleObject(pi.hProcess,INFINITE);
(void)GetExitCodeProcess(pi.hProcess,&exitcode);
setv(STRstatus, putn(exitcode), VAR_READWRITE);/*FIXRESET*/
// reset vt mode in case child process has messed with the console
nt_set_win10_vt_mode();
}
retval = 0;
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
// reset vt mode in case child process has messed with the console
nt_set_win10_vt_mode();
free_mem:
CloseHandle(si.hStdInput);
CloseHandle(si.hStdOutput);
Expand Down
1 change: 1 addition & 0 deletions win32/ntport.h
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ extern int do_nt_check_cooked_mode(void);
extern void set_cons_attr (char *);
extern void NT_MoveToLineOrChar(int ,int ) ;
extern void nt_set_win10_vt_mode(void);
extern void nt_unset_win10_vt_mode(void);
extern void nt_term_init(void);
extern void nt_term_cleanup(void);
extern void nt_set_size(int,int);
Expand Down

0 comments on commit e77bbcc

Please sign in to comment.