diff options
author | Ben Gamari <ben@smart-cactus.org> | 2017-06-27 10:22:49 -0400 |
---|---|---|
committer | Ben Gamari <ben@smart-cactus.org> | 2017-06-27 10:22:49 -0400 |
commit | b8f87363cfc6ddeacc67d99a79e316980ffd1f5c (patch) | |
tree | 79a494c31bcfea905d845e9c131dfa9d1bef2493 /libraries/base/cbits/inputReady.c | |
parent | 86abe0e03f6cc2392758d6b45390177d44896113 (diff) | |
download | haskell-b8f87363cfc6ddeacc67d99a79e316980ffd1f5c.tar.gz |
base/inputReady: Whitespace cleanup
Diffstat (limited to 'libraries/base/cbits/inputReady.c')
-rw-r--r-- | libraries/base/cbits/inputReady.c | 168 |
1 files changed, 84 insertions, 84 deletions
diff --git a/libraries/base/cbits/inputReady.c b/libraries/base/cbits/inputReady.c index 1530d5be90..0a84668689 100644 --- a/libraries/base/cbits/inputReady.c +++ b/libraries/base/cbits/inputReady.c @@ -1,4 +1,4 @@ -/* +/* * (c) The GRASP/AQUA Project, Glasgow University, 1994-2002 * * hWaitForInput Runtime Support @@ -68,109 +68,109 @@ fdReady(int fd, int write, int msecs, int isSock) #else if (isSock) { - int maxfd, ready; - fd_set rfd, wfd; - struct timeval tv; + int maxfd, ready; + fd_set rfd, wfd; + struct timeval tv; if ((fd >= (int)FD_SETSIZE) || (fd < 0)) { fprintf(stderr, "fdReady: fd is too big"); abort(); } - FD_ZERO(&rfd); - FD_ZERO(&wfd); + FD_ZERO(&rfd); + FD_ZERO(&wfd); if (write) { FD_SET(fd, &wfd); } else { FD_SET(fd, &rfd); } - - /* select() will consider the descriptor set in the range of 0 to - * (maxfd-1) - */ - maxfd = fd + 1; - tv.tv_sec = msecs / 1000; - tv.tv_usec = (msecs % 1000) * 1000; - - while ((ready = select(maxfd, &rfd, &wfd, NULL, &tv)) < 0 ) { - if (errno != EINTR ) { - return -1; - } - } - - /* 1 => Input ready, 0 => not ready, -1 => error */ - return (ready); + + /* select() will consider the descriptor set in the range of 0 to + * (maxfd-1) + */ + maxfd = fd + 1; + tv.tv_sec = msecs / 1000; + tv.tv_usec = (msecs % 1000) * 1000; + + while ((ready = select(maxfd, &rfd, &wfd, NULL, &tv)) < 0 ) { + if (errno != EINTR ) { + return -1; + } + } + + /* 1 => Input ready, 0 => not ready, -1 => error */ + return (ready); } else { - DWORD rc; - HANDLE hFile = (HANDLE)_get_osfhandle(fd); - DWORD avail; + DWORD rc; + HANDLE hFile = (HANDLE)_get_osfhandle(fd); + DWORD avail; switch (GetFileType(hFile)) { case FILE_TYPE_CHAR: - { - INPUT_RECORD buf[1]; - DWORD count; - - // nightmare. A Console Handle will appear to be ready - // (WaitForSingleObject() returned WAIT_OBJECT_0) when - // it has events in its input buffer, but these events might - // not be keyboard events, so when we read from the Handle the - // read() will block. So here we try to discard non-keyboard - // events from a console handle's input buffer and then try - // the WaitForSingleObject() again. - - while (1) // keep trying until we find a real key event { - rc = WaitForSingleObject( hFile, msecs ); - switch (rc) { - case WAIT_TIMEOUT: return 0; - case WAIT_OBJECT_0: break; - default: /* WAIT_FAILED */ maperrno(); return -1; - } - - while (1) // discard non-key events - { - rc = PeekConsoleInput(hFile, buf, 1, &count); - // printf("peek, rc=%d, count=%d, type=%d\n", rc, count, buf[0].EventType); - if (rc == 0) { - rc = GetLastError(); - if (rc == ERROR_INVALID_HANDLE || rc == ERROR_INVALID_FUNCTION) { - return 1; - } else { - maperrno(); - return -1; + INPUT_RECORD buf[1]; + DWORD count; + + // nightmare. A Console Handle will appear to be ready + // (WaitForSingleObject() returned WAIT_OBJECT_0) when + // it has events in its input buffer, but these events might + // not be keyboard events, so when we read from the Handle the + // read() will block. So here we try to discard non-keyboard + // events from a console handle's input buffer and then try + // the WaitForSingleObject() again. + + while (1) // keep trying until we find a real key event + { + rc = WaitForSingleObject( hFile, msecs ); + switch (rc) { + case WAIT_TIMEOUT: return 0; + case WAIT_OBJECT_0: break; + default: /* WAIT_FAILED */ maperrno(); return -1; } - } - - if (count == 0) break; // no more events => wait again - // discard console events that are not "key down", because - // these will also be discarded by ReadFile(). - if (buf[0].EventType == KEY_EVENT && - buf[0].Event.KeyEvent.bKeyDown && - buf[0].Event.KeyEvent.uChar.AsciiChar != '\0') - { - // it's a proper keypress: - return 1; - } - else - { - // it's a non-key event, a key up event, or a - // non-character key (e.g. shift). discard it. - rc = ReadConsoleInput(hFile, buf, 1, &count); - if (rc == 0) { - rc = GetLastError(); - if (rc == ERROR_INVALID_HANDLE || rc == ERROR_INVALID_FUNCTION) { - return 1; - } else { - maperrno(); - return -1; + while (1) // discard non-key events + { + rc = PeekConsoleInput(hFile, buf, 1, &count); + // printf("peek, rc=%d, count=%d, type=%d\n", rc, count, buf[0].EventType); + if (rc == 0) { + rc = GetLastError(); + if (rc == ERROR_INVALID_HANDLE || rc == ERROR_INVALID_FUNCTION) { + return 1; + } else { + maperrno(); + return -1; + } + } + + if (count == 0) break; // no more events => wait again + + // discard console events that are not "key down", because + // these will also be discarded by ReadFile(). + if (buf[0].EventType == KEY_EVENT && + buf[0].Event.KeyEvent.bKeyDown && + buf[0].Event.KeyEvent.uChar.AsciiChar != '\0') + { + // it's a proper keypress: + return 1; + } + else + { + // it's a non-key event, a key up event, or a + // non-character key (e.g. shift). discard it. + rc = ReadConsoleInput(hFile, buf, 1, &count); + if (rc == 0) { + rc = GetLastError(); + if (rc == ERROR_INVALID_HANDLE || rc == ERROR_INVALID_FUNCTION) { + return 1; + } else { + maperrno(); + return -1; + } + } + } } - } } - } } - } case FILE_TYPE_DISK: // assume that disk files are always ready: @@ -213,4 +213,4 @@ fdReady(int fd, int write, int msecs, int isSock) } } #endif -} +} |