X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;ds=sidebyside;f=cbits%2FinputReady.c;h=f827fe5def79ec1f5deb0a0521f8847281d4bdd4;hb=74bc2d04fdbae494bcf4839c4ec5e6ec1d0bf600;hp=63233ecae174cb6f020f5770d009d9bef38ddcf1;hpb=91ad6d44f7b29d2ede170e3106042b36c9904da1;p=haskell-directory.git diff --git a/cbits/inputReady.c b/cbits/inputReady.c index 63233ec..f827fe5 100644 --- a/cbits/inputReady.c +++ b/cbits/inputReady.c @@ -1,9 +1,7 @@ /* - * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998 + * (c) The GRASP/AQUA Project, Glasgow University, 1994-2002 * - * $Id: inputReady.c,v 1.5 2002/02/07 11:13:30 simonmar Exp $ - * - * hReady Runtime Support + * hWaitForInput Runtime Support */ /* select and supporting types is not Posix */ @@ -18,51 +16,76 @@ int inputReady(int fd, int msecs, int isSock) { - if -#ifndef mingw32_TARGET_OS - ( 1 ) { -#else + if +#if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32) ( isSock ) { +#else + ( 1 ) { #endif - int maxfd, ready; - fd_set rfd; - struct timeval tv; - - FD_ZERO(&rfd); - 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; + int maxfd, ready; + fd_set rfd; + struct timeval tv; + + FD_ZERO(&rfd); + 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, NULL, NULL, &tv)) < 0 ) { + if (errno != EINTR ) { + return -1; + } + } + + /* 1 => Input ready, 0 => not ready, -1 => error */ + return (ready); + } +#if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32) + else { + DWORD rc; + HANDLE hFile = (HANDLE)_get_osfhandle(fd); + DWORD avail; - while ((ready = select(maxfd, &rfd, NULL, NULL, &tv)) < 0 ) { - if (errno != EINTR ) { - return -1; - } - } + // WaitForMultipleObjects() works for Console input, but it + // doesn't work for pipes (it always returns WAIT_OBJECT_0 + // even when no data is available). There doesn't seem to be + // an easy way to distinguish the two kinds of HANDLE, so we + // try to detect pipe input first, and if that fails we try + // WaitForMultipleObjects(). + // + rc = PeekNamedPipe( hFile, NULL, 0, NULL, &avail, NULL ); + if (rc != 0) { + if (avail != 0) { + return 1; + } else { + return 0; + } + } else { + rc = GetLastError(); + if (rc == ERROR_BROKEN_PIPE) { + return 1; // this is probably what we want + } + if (rc != ERROR_INVALID_HANDLE) { + return -1; + } + } - /* 1 => Input ready, 0 => not ready, -1 => error */ - return (ready); -#ifdef mingw32_TARGET_OS - } else { - DWORD rc; - HANDLE hFile = (HANDLE)_get_osfhandle(fd); - - rc = MsgWaitForMultipleObjects( 1, - &hFile, - FALSE, /* wait all */ - msecs, /*millisecs*/ - QS_ALLEVENTS); - - /* 1 => Input ready, 0 => not ready, -1 => error */ - switch (rc) { - case WAIT_TIMEOUT: return 0; - case WAIT_OBJECT_0: return 1; - default: return -1; - } + rc = WaitForMultipleObjects( 1, + &hFile, + TRUE, /* wait all */ + msecs); /*millisecs*/ + + /* 1 => Input ready, 0 => not ready, -1 => error */ + switch (rc) { + case WAIT_TIMEOUT: return 0; + case WAIT_OBJECT_0: return 1; + default: return -1; + } } #endif - }} +}