From 80308079ec9f794976411fa09a61cfab43f5faad Mon Sep 17 00:00:00 2001
From: Justine Tunney <jtunney@gmail.com>
Date: Sat, 16 Apr 2022 23:46:16 -0700
Subject: [PATCH] Disable redbean repl on windows

---
 tool/build/blinkenlights.c | 48 ++++++++++++++++++++++----------------
 tool/net/redbean.c         |  5 ++--
 2 files changed, 30 insertions(+), 23 deletions(-)

diff --git a/tool/build/blinkenlights.c b/tool/build/blinkenlights.c
index 95b14319b..a6640d41d 100644
--- a/tool/build/blinkenlights.c
+++ b/tool/build/blinkenlights.c
@@ -700,12 +700,14 @@ static void LoadSyms(void) {
 static int DrainInput(int fd) {
   char buf[32];
   struct pollfd fds[1];
-  for (;;) {
-    fds[0].fd = fd;
-    fds[0].events = POLLIN;
-    if (poll(fds, ARRAYLEN(fds), 0) == -1) return -1;
-    if (!(fds[0].revents & POLLIN)) break;
-    if (read(fd, buf, sizeof(buf)) == -1) return -1;
+  if (!IsWindows()) {
+    for (;;) {
+      fds[0].fd = fd;
+      fds[0].events = POLLIN;
+      if (poll(fds, ARRAYLEN(fds), 0) == -1) return -1;
+      if (!(fds[0].revents & POLLIN)) break;
+      if (read(fd, buf, sizeof(buf)) == -1) return -1;
+    }
   }
   return 0;
 }
@@ -1928,19 +1930,21 @@ static int OnPtyFdPoll(struct pollfd *fds, size_t nfds, int ms) {
         }
         p2.fd = fds[i].fd;
         p2.events = fds[i].events;
-        switch (poll(&p2, 1, ms)) {
-          case -1:
-            re = POLLERR;
-            ++t;
-            break;
-          case 0:
-            break;
-          case 1:
-            re = p2.revents;
-            ++t;
-            break;
-          default:
-            unreachable;
+        if (!IsWindows()) {
+          switch (poll(&p2, 1, ms)) {
+            case -1:
+              re = POLLERR;
+              ++t;
+              break;
+            case 0:
+              break;
+            case 1:
+              re = p2.revents;
+              ++t;
+              break;
+            default:
+              unreachable;
+          }
         }
       }
     }
@@ -2602,7 +2606,11 @@ static bool HasPendingKeyboard(void) {
 }
 
 static void Sleep(int ms) {
-  poll((struct pollfd[]){{ttyin, POLLIN}}, 1, ms);
+  if (IsWindows()) {
+    usleep(ms * 1000L);
+  } else {
+    poll((struct pollfd[]){{ttyin, POLLIN}}, 1, ms);
+  }
 }
 
 static void OnMouseWheelUp(struct Panel *p, int y, int x) {
diff --git a/tool/net/redbean.c b/tool/net/redbean.c
index 6feeec8f5..8ee14763f 100644
--- a/tool/net/redbean.c
+++ b/tool/net/redbean.c
@@ -6830,7 +6830,6 @@ static int EventLoop(int fd, int ms) {
   int rc;
   long double t;
   rc = -1;
-  polls[0].fd = 0;
   while (!terminated) {
     errno = 0;
     if (zombied) {
@@ -6848,13 +6847,13 @@ static int EventLoop(int fd, int ms) {
       break;  // return control to linenoise
     }
   }
-  polls[0].fd = -1;
   return rc;
 }
 
 static void ReplEventLoop(void) {
   int status;
   lua_State *L = GL;
+  polls[0].fd = 0;
   __nomultics = 2;
   __replmode = true;
   lua_initrepl("redbean");
@@ -7096,7 +7095,7 @@ void RedBean(int argc, char *argv[]) {
 #ifdef STATIC
   EventLoop();
 #else
-  if (isatty(0)) {
+  if (!IsWindows() && isatty(0)) {
     ReplEventLoop();
   } else {
     EventLoop(-1, HEARTBEAT);