[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] Controlling output of processes
From: |
Lasse Kliemann |
Subject: |
Re: [Chicken-users] Controlling output of processes |
Date: |
Fri, 12 Mar 2010 21:47:01 +0100 |
* Message by -Peter Bex- from Fri 2010-03-12:
> On Fri, Mar 12, 2010 at 07:30:34PM +0100, Lasse Kliemann wrote:
> > Hello Peter, thank you for the answer. I tried it out, but it
> > does not work like the thing I wanted. Here is an example:
> >
> > (receive
> > (p-stdout p-stdin p-pid p-stderr)
> > (process* "ls" '("-l"))
> > (with-input-from-port p-stdout (lambda () (process-wait p-pid))))
> >
> > When I run this, I receive no output. The goal was that my
> > program hands the output of "ls -l" through. So this program was
> > supposed to give exactly the output of "ls -l". You know what I
> > mean?
>
> Hi Lasse,
>
> I think I do. The idea of calling with-input-from-port is that you
> put the code that reads from that port inside the thunk. Just waiting
> for the process isn't enough; there has to be some code that actually
> reads out the data from stdout.
>
> Here's an example:
> (receive (p-stdout p-stdin p-pid p-stderr)
> (process* "ls" '("-l"))
> (let ((output (with-input-from-port p-stdout read-lines)))
> (process-wait p-pid)
> output))
>
> This returns a list of strings, with one entry for each line that is
> output by "ls". If you would like something else, you can change
> read-lines by (lambda () <whatever you want to do>)
I'd rather hand it through directly, but this can be done with a
loop involving read-line and write-line. What I can't have this
way is standard output and standard error intermixed in the order
as output comes - but for this I can use process-run.
So if I want both, I use process-run. If I want one of them
(either standard output or standard error) I use the above with a
read-line and write-line loop. If I want none of them, I use the
above and simply ignore the ports.
Instead of handing output through with a loop, it may be a
cleaner approach to implement a more flexible version of
process*. I've seen that the implementation is in Scheme, in
posixunix.scm starting from line 2336. So it should be possible
without too much hassle. I'll consider this.
Kind regards,
Lasse
pgpXfwYEVjq8Z.pgp
Description: PGP signature