[PATCH] Wild guess on windows console problems

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[PATCH] Wild guess on windows console problems

Paolo Bonzini-2
Here is what I meant in my last message.  Not even compiled, obviously,
but the idea should be clear.

Can anybody test it?

Paolo

--- orig/lib-src/socketx.c
+++ mod/lib-src/socketx.c
@@ -24,6 +24,9 @@
 #include <conio.h>
 
 #define MAX_WIN_HANDLES 300 /* max number of fd we can handle */
+#define CONSOLE_HUP -1
+
+static int get_number_of_console_key_events (HANDLE h);
 
 /* n = maxfd + 1 */
 int
@@ -90,56 +93,41 @@ win_select (int n, fd_set * rfds, fd_set
   nset = 0;
   for (i = 0; i < nhandles; i++)
     {
-      ret = WaitForSingleObject (handle_array[i], 0);
-      if (ret == WAIT_OBJECT_0)
- {
-  FD_SET (handle_fd[i], handle_set[i]);
-  nset++;
- }
+      HANDLE h = handle_array[i];
+      ret = WaitForSingleObject (h, 0);
+      if (ret != WAIT_OBJECT_0)
+ continue;
+
+      /* Discard non-key events.  */
+      if (GetFileType (h) == FILE_TYPE_CHAR
+  && get_number_of_console_key_events (h) == 0)
+ continue;
+
+      FD_SET (handle_fd[i], handle_set[i]);
+      nset++;
     }
 
   return nset;
 }
 
-/*
- * This only gets called with the MSG_PEEK flag by poll.c when a read is ready
- * It does not need to indicate the real
- * number of characters available ... only return values of 1 and 0
- * are currently tested for by poll.c
- * TODO: return real number of characters available
- */
 int
-win_recv (int fd, void *buffer, int n, int flags)
+get_number_of_console_key_events (HANDLE h)
 {
   BOOL bRet;
   int i, ret, data_available;
   size_t nread;
   INPUT_RECORD *irbuffer;
   DWORD nevents, nbuffer;
-  HANDLE h = (HANDLE) _get_osfhandle (fd);
-  ret = WaitForSingleObject (h, 0);
-  if (ret == WAIT_OBJECT_0)
-    data_available = 1;
-  else
-    data_available = 0;
-
-  if (flags != MSG_PEEK)
-    return 0;
-
-  /* MSG_PEEK */
-  if (GetFileType (h) != FILE_TYPE_CHAR)
-    return (data_available ? 1 : 0);
 
-  /* console (FILE_TYPE_CHAR) */
   nbuffer = nevents = 0;
   bRet = GetNumberOfConsoleInputEvents (h, &nbuffer);
   if (!bRet || nbuffer == 0)
-    return 0;
+    return CONSOLE_HUP;
 
   irbuffer = (INPUT_RECORD *) alloca (nbuffer * sizeof (INPUT_RECORD));
   bRet = PeekConsoleInput (h, irbuffer, nbuffer, &nevents);
   if (!bRet || nevents == 0)
-    return 0;
+    return CONSOLE_HUP;
 
   nread = 0;
   for (i = 0; i < nevents; i++)
@@ -148,4 +136,36 @@ win_recv (int fd, void *buffer, int n, i
 
   return nread;
 }
+
+/*
+ * This only gets called with the MSG_PEEK flag by poll.c when a read is ready
+ * It does not need to indicate the real
+ * number of characters available ... only return values of 1 and 0
+ * are currently tested for by poll.c
+ * TODO: return real number of characters available
+ */
+int
+win_recv (int fd, void *buffer, int n, int flags)
+{
+  HANDLE h;
+
+  if (flags != MSG_PEEK)
+    return 0;
+
+  h = (HANDLE) _get_osfhandle (fd);
+  ret = WaitForSingleObject (h, 0);
+  if (ret != WAIT_OBJECT_0)
+    return 0;
+
+  if (GetFileType (h) != FILE_TYPE_CHAR)
+    return 1;
+
+  else
+    {
+      int keys = get_number_of_console_key_events (h);
+      return (keys == CONSOLE_HANGUP ? 0 : keys);
+    }
+}
+
+
 #endif

_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk