chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] Controlling output of processes


From: Jim Ursetto
Subject: Re: [Chicken-users] Controlling output of processes
Date: Fri, 12 Mar 2010 14:34:25 -0600

What you want to do in effect is redirect stderr to stdout so that output comes out in the right order. The simplest way using a UNIX shell as you know is:
(system "ls -l 2>&1")

We can avoid relying on the shell via something like:

(define command "ls")
(define args '("-l"))
(process-wait
 (process-fork
  (lambda ()
    (duplicate-fileno fileno/stdout fileno/stderr)
    (process-execute command args))))

Furthermore if we want to write the command's combined stdout/stderr to a file we can redirect our own stdout first (like `ls -l > test.txt 2>&1`)

(define fd (file-open "test.txt" (+ open/wronly open/trunc open/creat)))
(process-wait
 (process-fork
  (lambda ()
    (duplicate-fileno fd fileno/stdout)
    (duplicate-fileno fileno/stdout fileno/stderr)
    (process-execute command args))))
(file-close fd)
(print "done")

This assumes that your exec'ed command actually flushes its output after every write. If it does not then its output will likely be jumbled when writing to the file, due to stdio buffering.

This method doesn't use pipes or ports, instead acting as a pass- through. (process) could do it but it doesn't provide the capability to redirect stderr to stdout. I wonder if such an addition would be useful.

On Mar 12, 2010, at 10:14 AM, Lasse Kliemann wrote:

How can I start a process and control where it sends its stdout
or stderr? I know that I can do:

(receive
  (p-stdout p-stdin p-pid p-stderr)
  (process* command args)
  (...))

This gives input and output ports connected to the process. But
how can I express, e.g.: "everything available on port p-stdout
should go to (current-output-port)"? Or similar statements
involving p-stdout and/or p-stderr? I could use a loop that reads
from those ports and writes to (current-output-port) until it
sees EOF. But this feels plain wrong. In particular, when
forwarding both p-stdout and p-stderr, this would not represent
the order in which the process generated the output (but it would
first show all its stdout and then all its stderr, or vice
versa).

Can you give me some suggestions?
_______________________________________________
Chicken-users mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/chicken-users





reply via email to

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