I've been writing an IRC library, but I'm a bit stuck on the best way to arrange things so that you can type input at a terminal while still running an event loop that polls the server periodically. As we know, there's no way you can have a non-blocking read-line, so how would it be possible to combine both 'read' and 'write' tasks into a single terminal window? (My previous attempt - using fork - doesn't seem to be what I'm looking for.)
My 'read' code looks like this:
(define (read-irc)
(let ((buffer {}))
(while (not (net-select Iserver "read" 500))
(sleep 500))
(net-receive Iserver buffer 8192 "n")
(unless (empty? buffer)
(parse-buffer buffer))))
(define (read-irc-loop)
(let ((buffer {}))
(while Iconnected
(read-irc))))
The following program will print a dot after every second until you enter something from the keyboard:
#!/usr/bin/newlisp
(while (zero? (peek 0)) (sleep 1000) (println ".") ) ; 0 for stdin
(println "--->" (read-line 0))
(exit)
Instead of waiting and printing dots, something else could be done, like waiting for and displaying input from IRC.
See also here: http://www.newlisp.org/downloads/newlisp_manual.html#peek
Ah, that looks promising. Thanks!