[plt-scheme] Read and write ports from pre-opened file descriptors?

From: Eli Barzilay (eli at barzilay.org)
Date: Wed Feb 27 14:19:36 EST 2008

On Feb 27, David Fayram wrote:
> Hello, everyone. I'm relatively new to scheme and I've been trying
> to writing a binding between Erlang and Scheme for a clustering
> application I've built in Erlang. Part of this entails reading and
> writing to file descriptors 3 and 4 in a binary protocol for
> communication with Erlang via its Ports abstraction.
> It seems like this is possible to do at the C level, but I'm not
> sure how to do it with MzScheme's ffi library. It would be best if
> there were no external binary objects. Could someone suggest a way
> that this might be accomplished?

A rough start at doing this:

  (module fd-read mzscheme
    (require (lib "foreign.ss")) (unsafe!)
    (provide fd-read)
    (define buf-len 512)
    (define fd-read
      (get-ffi-obj "read" #f
        (_fun ;; the fd argument is the only actual input for this
              [fd : _int]
              ;; the `buf' input is automatically set to a newly
              ;; created byte-string
              [buf : _bytes = (make-bytes buf-len)]
              ;; len is also automatic -- the size of the buffer
              [len : _int = buf-len]
              ;; we get back a value that we bind to `n'
              -> [n : _int]
              ;; but this is what we actually return
              -> (if (= n buf-len) buf (subbytes buf 0 n))))))

I'm saying `rough' because there might be some off-by-1 errors with
the limits, and you might want to avoid allocating a new byte-string
on every call and/or avoid copying the actual result from the buffer.
(But you'll need to implement some locking if you want to do it

I tried it with bash, and the only way I could get it to get input on
4 (from "x") and keep 0 as stdin is

  mzscheme 5<&0 0<x 4<&0- 0<&5

It seems to work with that.

(This obviously is not intended for Windows...)

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!

Posted on the users mailing list.