chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] quick port question


From: Tony Sidaway
Subject: Re: [Chicken-users] quick port question
Date: Fri, 23 Feb 2007 20:08:02 +0000

On 2/23/07, Peter Busser <address@hidden> wrote:
Hi!

> What is the recommended Scheme/Chicken way to run Unix/shell commands
> and read their output?

There is no standard Scheme way I think. But Chicken supports starting a
program and reading the output from a pipe. I don't remember the
function right now, but you can find it in the Chicken manual.


As it happens I've been coding something like that today.  You need to
the posix unit to do it.  A rather smarter and more general way of
making posix pipelines is given by the pipeline egg.  The latter is
very neat because it multithreads to avoid blocking the process when
waiting for input or output resources.

Here's a simple Posix example that starts up a process and creates
pipeline ports  so you can writes to the process and read its output.

You would typically invoke it via

(call-with-values gdb (lambda (pid, receive-port send-port) ... )




(use posix)
(define (gdb)
 (call-with-values create-pipe
   (lambda (bottom-pipe-from-scm-to-proc top-pipe-from-scm-to-proc)
     (call-with-values create-pipe
        (lambda (bottom-pipe-from-proc-to-scm top-pipe-from-proc-to-scm)
          (let ((pid (process-fork)))
            (if (= 0 pid)
                (begin ; STUFF TO DO IF WE'RE CHILD
                  (duplicate-fileno bottom-pipe-from-scm-to-proc 0)
                  (duplicate-fileno top-pipe-from-proc-to-scm 1)
                  (file-close bottom-pipe-from-proc-to-scm)
                  (file-close top-pipe-from-scm-to-proc)
                  ; Now we vanish off into another executable image.
                  (process-execute "/usr/bin/gdb" '("-interpreter=mi2"))))
        
            ;  THIS CODE IS EXECUTED ONLY BY THE PARENT
            (file-close bottom-pipe-from-scm-to-proc)
            (file-close top-pipe-from-proc-to-scm)
            (values pid
                    (open-input-file* bottom-pipe-from-proc-to-scm)
                    (open-output-file* top-pipe-from-scm-to-proc))))))))




reply via email to

[Prev in Thread] Current Thread [Next in Thread]