[gambit-list] creating scheme ports from filehandles
Marc Feeley
feeley at iro.umontreal.ca
Tue Jul 25 08:33:23 EDT 2006
On 24-Jul-06, at 6:34 PM, Christian wrote:
> Hello
>
> Is there a way to turn a POSIX filehandle into a Gambit scheme port,
> or could it be added?
>
> I'm imaging something like:
> (posix-fd->port 0) => returning an input-output port
> or
> (open-port (list posix-fd: 0 direction: 'input)) ;; in case one
> wants
> ;; the direction to be preserved.
> or similar.
There is an internal procedure for doing this. It is called ##open-
predefined and you can use it like this:
> (define in 1)
> (define out 2)
> (define inout 3)
> (define stderr (##open-predefined out '(STDERR) 2))
> stderr
#<output-port #3 (STDERR)>
> (display "hello\n" stderr)
> (force-output stderr)
hello
The third parameter of ##open-predefined is the file descriptor and
the fourth optional parameter is the port settings. Note that this
will only work on Unix (Windows does not use file descriptors). The
special file descriptors -1, -2, -3 and -4 represent STDIN, STDOUT,
STDERR and the console on Unix and Windows.
>
> This would allow things like implement functions creating unix
> sockets, or maybe even UDP ports; well for the latter maybe more
> changes would actually be needed.
My working version of Gambit has preliminary support for UDP and RAW
sockets.
> Maybe I should ask (instead of the
> above) how one can, from a C function, read/write from/to a file
> descriptor without blocking the system (using the scheduler, thus
> possibly running other scheme code in the mean time).
One way it to use ##open-predefined and have the C code call into
Scheme. Why do you need this? There is not a general solution that
will work in all situations.
Marc
More information about the Gambit-list
mailing list