[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Q] HOWTO redirect a i/o-port in Unix??



Quoting Eli Barzilay:
> I got stuck trying to use this with two processes where the first one
> gets input from Scheme:

I should have been more careful in my original post.

The bottom line is that you want to use `read-line' instead of
`read-string!':

(define (copy-stream/lines input-port output-port name)
  (thread (lambda ()
	    (let loop ()
	      (let ((l (read-line s input-port)))
		(unless (eof-object? l)
		  (display l output-port)
		  (newline output-port)
		  (loop))))
	    (close-input-port input-port)
	    (close-output-port output-port))))

The problem is that `read-string!' waits until it can fill the string
or until EOF, but your application relies on line-based
question--response I/O.

Previously in this thread (I think), I mentioned that MzScheme needs a
primitive like `read-string', except that it returns immediately if
there's something to read, and blocks only if nothing's available. With
that, we could have a truly general and reasonably efficient
`copy-stream'. The next verison of MzScheme will likely have such a
primitive.

Without it, we need at least `copy-stream', as before, and
`copy-stream/lines', as above.

Matthew