[Larceny-users] reading from a process

Ray Racine ray.racine at comcast.net
Sun Dec 30 12:53:12 EST 2007


If you were seeging something like this, its because you using a textual
port proc with a binary port.  The error message could be a bit clearer.
And that may be the key for you as my solution below pulls in a lot my
library stuff.


Error: get-string-all: illegal argument(s) #<INPUT PORT input descriptor
port 40>
Entering debugger; type "?" for help.
debug> #<EOF>

(Illegal because its a binary port. I think.)


The following works (to make it better one could wrap the binary port
with a textual port transcoder for line reading.  Unfortunately, I've
been converting those things I need into R6RS libraries and the slippery
slope is I'm forking Larceny's standard libraries.  The other bad side
effect is that its all Linux specific. Though at the moment they're 90%
identical.

This works as a POC:

(library
 (test)
 
 (export doit)
 
 (import
  (rnrs)
  (rnrs lists)
  (rnrs io simple)
  (sys system unix)
  (sys system process))
 
(define reduce-r (lambda (pred l)
                   (let ((l1 (cdr l)))
                     (if (null? l1)
                         (car l)
                         (pred (car l) (reduce-r pred l1))))))

(define (call-with-input-pipe command pred)
  (let* ((results (process (if (string? command)
                               command
                               (reduce-r (lambda (arg result)
                                           (string-append arg " "
result))
                                         command))))
         (result (pred (car results))))
    (close-input-port (car results))
    (close-output-port (cadr results))
    (unix-waitpid (caddr results)) ; important in order to remove
process
    result))

(define doit
  (lambda ()
    (call-with-input-pipe '("date" "-I")
                          (lambda (ip)
                            (utf8->string (get-bytevector-n ip 512))))))
)

Larceny v0.96 "Fluoridation" (Dec 29 2007 17:37:47,
precise:Linux:unified)
larceny.heap, built on Sat Dec 29 17:39:47 EST 2007
ERR5RS mode (no libraries have been imported)

> Autoloading (rnrs)
Autoloading (rnrs lists)
Autoloading (sys system unix)
Autoloading (ffi ffi-std)
Autoloading (r5rs)
Autoloading (rnrs r5rs)
Autoloading (rnrs control)
Autoloading (rnrs eval)
Autoloading (rnrs mutable-strings)
Autoloading (rnrs mutable-pairs)
Autoloading (rnrs unicode)
Autoloading (rnrs load)
Autoloading (rnrs bytevectors)
Autoloading (ffi foreign-ctools)
Autoloading (rnrs syntax-case)
Autoloading (rnrs files)
Autoloading (sys system)
Autoloading (io print)
Autoloading (larceny deprecated)
Autoloading (rnrs io ports)
Autoloading (rnrs arithmetic fixnums)
Autoloading (rnrs enums)
Autoloading (rnrs hashtables)
Autoloading (rnrs programs)
Autoloading (rnrs conditions)
Autoloading (rnrs exceptions)
Autoloading (rnrs records syntactic)
Autoloading (err5rs records procedural)
Autoloading (rnrs records procedural)
Autoloading (rnrs sorting)

> (import (test))

> (doit)
"2007-12-30\n"


Since I have sockets working with threading, I'm sure that I can get
process spawning, with wait, working with threading as well.  They both
the same descriptors custom port code. 

I hope to have much of this stabilized in a few weeks.

Library for the above is here:

http://squire.svn.sourceforge.net/viewvc/squire/trunk/src/scheme/sys/system/unix.sls?revision=118&view=markup

I'm worried all dependencies may not be up to date in SVN.  Hopefully,
my first guess about textual/binary ports can help you modify your code.
i.e. Check if the process is call is returning a binary port and put a
textual transcoder on it or use binary reader procs.

Ray

On Sun, 2007-12-30 at 09:51 +0100, Sven.Hartrumpf at FernUni-Hagen.de
wrote:
> Hi all.
> 
> It seems that I cannot read (anymore?) using read-line (and friends)
> from the output generated by 'process'.
> Does anybody have a working alternative?
> 
> Here is a small example (named readprocess1.scm) showing the problem:
> 
> :
> 
> (require 'unix)
> 
> (define reduce-r (lambda (pred l)
>   (let ((l1 (cdr l)))
>     (if (null? l1)
>       (car l)
>       (pred (car l) (reduce-r pred l1))))))
> 
> (define (call-with-input-pipe command pred)
>   (let* ((results (process (if (string? command)
>                              command
>                              (reduce-r (lambda (arg result)
>                                          (string-append arg " " result))
>                                        command))))
>          (result (pred (car results))))
>     (close-input-port (car results))
>     (close-output-port (cadr results))
>     (unix/waitpid (caddr results)) ; important in order to remove process
>     result))
> 
> (call-with-input-pipe '("date" "-I") read-line)
> 
> 
> ----
> 
> > larceny
> Larceny v0.96 "Fluoridation" (Dec 23 2007 08:04:15, precise:Linux:unified)
> larceny.heap, built on Sun Dec 23 08:15:16 EST 2007
> 
> > (load "readprocess1.scm")
> Warning: loading source in favor of stale fasl file: .../larceny/lib/Base/string.sch
> 
> 
> Error: get-char: argument not a textual input port #<INPUT PORT input descriptor port 7>
> 
> 
> 
> Greetings
> Sven
> _______________________________________________
> Larceny-users mailing list
> Larceny-users at lists.ccs.neu.edu
> https://lists.ccs.neu.edu/bin/listinfo/larceny-users




More information about the Larceny-users mailing list