about summary refs log tree commit diff stats
path: root/adapter/format
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-09-23 19:51:20 +0200
committerbptato <nincsnevem662@gmail.com>2024-09-23 19:58:54 +0200
commitfcd9aa9f9c604ed5d104343542962a26b2acda62 (patch)
tree7b0eea9b63bacc27cdc6471e2b2409b7b5d15c9e /adapter/format
parent8e8c7f0911f4a20446a83090d722fecaf203f6f3 (diff)
downloadchawan-fcd9aa9f9c604ed5d104343542962a26b2acda62.tar.gz
Replace std/selectors with poll
std/selectors uses OS-specific selector APIs, which sounds good in
theory (faster than poll!), but sucks for portability in practice.
Sure, you can fix portability bugs, but who knows how many there are
on untested platforms... poll is standard, so if it works on one
computer it should work on all other ones. (I hope.)

As a bonus, I rewrote the timeout API for poll, which incidentally
fixes setTimeout across forks. Also, SIGWINCH should now work on all
platforms (as we self-pipe instead of signalfd/kqueue magic).
Diffstat (limited to 'adapter/format')
-rw-r--r--adapter/format/ansi2html.nim35
1 files changed, 16 insertions, 19 deletions
diff --git a/adapter/format/ansi2html.nim b/adapter/format/ansi2html.nim
index dcbc4210..9f24dd0b 100644
--- a/adapter/format/ansi2html.nim
+++ b/adapter/format/ansi2html.nim
@@ -1,8 +1,9 @@
 import std/options
 import std/os
 import std/posix
-import std/selectors
 
+import io/dynstream
+import io/poll
 import types/color
 import utils/twtstr
 
@@ -381,27 +382,23 @@ proc main() =
   if standalone:
     state.puts("<body>\n")
   state.puts("<pre style='margin: 0'>\n")
-  let ofl = fcntl(STDIN_FILENO, F_GETFL, 0)
-  doAssert ofl != -1
-  discard fcntl(STDIN_FILENO, F_SETFL, ofl and not O_NONBLOCK)
+  let ps = newPosixStream(STDIN_FILENO)
+  ps.setBlocking(false)
   var buffer {.noinit.}: array[4096, char]
-  var selector = newSelector[int]()
-  block mainloop:
-    while true:
-      let n = read(STDIN_FILENO, addr buffer[0], buffer.high)
-      if n != -1:
-        if n == 0:
-          break
-        state.processData(buffer.toOpenArray(0, n - 1))
-      else:
-        doAssert errno == EAGAIN or errno == EWOULDBLOCK
-        state.flushOutbuf()
-        selector.registerHandle(STDIN_FILENO, {Read}, 0)
-        discard selector.select(-1)
-        selector.unregister(STDIN_FILENO)
+  var pollData = PollData()
+  while true:
+    try:
+      let n = ps.recvData(buffer)
+      if n == 0:
+        break
+      state.processData(buffer.toOpenArray(0, n - 1))
+    except ErrorAgain:
+      state.flushOutbuf()
+      pollData.register(ps.fd, POLLIN)
+      pollData.poll(-1)
+      pollData.unregister(ps.fd)
   if standalone:
     state.puts("</body>")
   state.flushOutbuf()
-  discard fcntl(STDIN_FILENO, F_SETFL, ofl)
 
 main()