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

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



On Feb 26, Matthew Flatt wrote:
> Here's a fairly general and efficient function:
> 
>  (define (copy-stream input-port output-port)
>    ...)

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

======================================================================
(define (eprintf . args)
  (apply fprintf (current-error-port) args) (newline
  (current-error-port)))
(define (copy-stream input-port output-port name)
  (thread (lambda ()
            (let ((s (make-string 4096)))
              (let loop ()
                (eprintf "!!! 1 [~a]" name)
                (let ((l (read-string! s input-port)))
                  (eprintf "!!! 2 [~a]" name)
                  (unless (eof-object? l)
                    (display (if (< l 4096) (substring s 0 l) s)
                             output-port)
                    (loop))))
              (close-input-port input-port)
              (close-output-port output-port)))))
(define-values (from-1st to-1st 1st-pid 1st-err 1st-stat)
  (apply values (process* "/bin/cat")))
(define-values (from-2nd to-2nd 2nd-pid 2nd-err 2nd-stat)
  (apply values (process* "/bin/cat")))
(when (file-exists? "/tmp/foo") (delete-file "/tmp/foo"))
(define 1st-thread
  (begin (copy-stream from-1st to-2nd "1st->2nd")
         (copy-stream from-2nd (open-output-file "/tmp/foo") "2nd->foo")))
(eprintf "!!! A")
(fprintf to-1st "12345678901234567890123456789012345678901234567890~%")
(eprintf "!!! B")
(close-output-port to-1st)
(eprintf "!!! C")
(thread-wait 1st-thread)
(exit)
======================================================================

It looks like both threads are getting stuck on the read-string!, but
I couldn't think of any reason for them to do so...

Am I doing anything wrong here?

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
         http://www.cs.cornell.edu/eli/meaning.html        Maze is Life!