From: Eric Pouech epouech@codeweavers.com
With some improvements: - waiting on an input stream can be interrupted by ctrl-c,
Signed-off-by: Eric Pouech epouech@codeweavers.com --- programs/cmd/wcmdmain.c | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-)
diff --git a/programs/cmd/wcmdmain.c b/programs/cmd/wcmdmain.c index e3682e4256d..cf1c627cf81 100644 --- a/programs/cmd/wcmdmain.c +++ b/programs/cmd/wcmdmain.c @@ -601,26 +601,25 @@ static BOOL has_pending_char_events(HANDLE h) */ RETURN_CODE WCMD_wait_for_input(HANDLE hIn) { + HANDLE h[2] = {hIn, control_c_event}; RETURN_CODE return_code; DWORD oldmode; DWORD count; - WCHAR key; + char key;
- return_code = ERROR_INVALID_FUNCTION; + return_code = ERROR_SIGNAL_PENDING; /* some never returned value */ if (GetConsoleMode(hIn, &oldmode)) { - HANDLE h[2] = {hIn, control_c_event}; - SetConsoleMode(hIn, oldmode & ~ENABLE_LINE_INPUT); FlushConsoleInputBuffer(hIn); - while (return_code == ERROR_INVALID_FUNCTION) + while (return_code == ERROR_SIGNAL_PENDING) { switch (WaitForMultipleObjects(2, h, FALSE, INFINITE)) { case WAIT_OBJECT_0: if (has_pending_char_events(hIn)) return_code = NO_ERROR; - /* will make both hIn no long signaled, and also process the pending input record */ + /* will make both hIn no longer signaled, and also process the pending input record */ FlushConsoleInputBuffer(hIn); break; case WAIT_OBJECT_0 + 1: @@ -631,10 +630,26 @@ RETURN_CODE WCMD_wait_for_input(HANDLE hIn) } SetConsoleMode(hIn, oldmode); } - else if (WCMD_ReadFile(hIn, &key, 1, &count) && count) - return_code = NO_ERROR; else - return_code = ERROR_INVALID_FUNCTION; + { + while (return_code == ERROR_SIGNAL_PENDING) + { + switch (WaitForMultipleObjects(2, h, FALSE, INFINITE)) + { + case WAIT_OBJECT_0: + if (ReadFile(hIn, &key, 1, &count, NULL) && count) + return_code = NO_ERROR; + else + return_code = ERROR_INVALID_FUNCTION; + break; + case WAIT_OBJECT_0 + 1: + return_code = STATUS_CONTROL_C_EXIT; + break; + default: break; + } + } + } + return return_code; }