From bb085287d84c1ad37717435e64344be2ac5d01ac Mon Sep 17 00:00:00 2001 From: Peter Date: Wed, 4 Dec 2013 01:55:22 +0000 Subject: [PATCH] Fix console input on Windows to permit the debugger to function. Previously, on Win32 with MSVC, the non-blocking input function didn't work correctly and prevented use of the debugger. Instead, use native console I/O to implement non-blocking input, which both works and avoids CRT (i.e. MSVC vs. Cygwin) dependency. --- src/dis.c | 3 ++- src/win_console.c | 32 +++++++++++++++++++++----------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/dis.c b/src/dis.c index c01aea2..77b18b3 100644 --- a/src/dis.c +++ b/src/dis.c @@ -392,6 +392,7 @@ do_debug_intfc() show_regs(); break; case '\n': + case '\r': *line_ptr = 0; if(old_mode == 's') { do_blank(); @@ -720,7 +721,7 @@ read_line(char *buf, int len) break; } space_left -= ret; - if(buf[ret-1] == 0x0a) { + if(buf[ret-1] == '\r' || buf[ret-1] == '\n') { break; } buf = &buf[ret]; diff --git a/src/win_console.c b/src/win_console.c index 86736aa..e59a7bc 100644 --- a/src/win_console.c +++ b/src/win_console.c @@ -41,18 +41,20 @@ extern int g_win_fullscreen_state; int win_nonblock_read_stdin(int fd, char *bufptr, int len) { - HANDLE oshandle; - DWORD dwret; - int ret; - - errno = EAGAIN; - oshandle = (HANDLE)_get_osfhandle(fd); // get stdin handle - dwret = WaitForSingleObject(oshandle, 1); // wait 1msec for data - ret = -1; - if(dwret == WAIT_OBJECT_0) { - ret = read(fd, bufptr, len); + DWORD charsRead = 0; + ReadConsole(GetStdHandle(STD_INPUT_HANDLE), bufptr, len, &charsRead, NULL); + + if (charsRead == 0) + { + errno = EAGAIN; + return -1; + } + else + { + DWORD charsWritten = 0; + WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), bufptr, charsRead, &charsWritten, NULL); + return charsRead; } - return ret; } void get_cwd(LPTSTR buffer, int size) @@ -166,6 +168,14 @@ main(int argc, char **argv) printf("...rect is: %ld, %ld, %ld, %ld\n", rect.left, rect.top, rect.right, rect.bottom); + // Enable non-blocking, character-at-a-time console I/O. + // win_nonblock_read_stdin() expects this behavior. + DWORD mode; + GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &mode); + mode &= ~ENABLE_LINE_INPUT; + SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), mode); + + gsportinit(hwnd); int ret = gsportmain(argc, argv);