Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Redirecting program output with SBCL's RUN-PROGRAM

448 views
Skip to first unread message

Pillsy

unread,
Feb 28, 2010, 1:26:51 PM2/28/10
to
I'm trying to figure out how to use the output stream of one program I
start with RUN-PROGRAM so it can be used as the input of another
program started with RUN-PROGRAM (i.e., the moral and perhaps literal
equivalent of piping). I've tried using a number of combinations of
the :INPUT, :OUTPUT and :WAIT keyword arguments, but nothing I've hit
upon has been productive so far. Any tips would be helpful; for
example, how would I go about doing something like "ls | grep lisp"
from the shell?

One of my attempts is

(defun piping-test ()
(let ((grep-process (run-program "/usr/bin/grep" '("lisp")
:input :stream
:output :stream)))
(unwind-protect
(with-open-stream (s (process-input grep-process))
(let ((ls-process (run-program "/bin/ls" '()
:output s)))
(when ls-process
(unwind-protect
(with-open-stream (o (process-output grep-process))
(loop
:for line := (read-line o nil nil)
:while line
:collect line))
(process-close ls-process)))))
(when grep-process (process-close grep-process)))))

Running this in a SLIME REPL causes everything to hang until I break
with C-c C-c, so it's pretty obviously not the right thing, but I'm
not sure how to change it so it is the right thing.

Thanks,
Pillsy

Pascal J. Bourguignon

unread,
Feb 28, 2010, 2:01:22 PM2/28/10
to
Pillsy <pill...@gmail.com> writes:

(defun piping-test () ; should be written as:
(pipe
(command "ls")
(command "grep" :arguments '("lisp"))))

; implementation of pipe and command (and other verbs such as input,
; output, and error redirections), left as an exercise to the reader.
; scsh may be taken for inspiration.

--
__Pascal Bourguignon__

Pillsy

unread,
Mar 1, 2010, 7:36:29 AM3/1/10
to
On Feb 28, 2:01 pm, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
> Pillsy <pillsb...@gmail.com> writes:

The problem I'm having is exactly in your "exercise to the reader".
It's not entirely clear how the sources for another implementation of
an entirely different implementation are going to be useful in
figuring out the implementation-specific functionality of SBCL.

Cheers,
Pillsy

Raymond Toy

unread,
Mar 1, 2010, 9:16:43 AM3/1/10
to
On 2/28/10 1:26 PM, Pillsy wrote:
> (defun piping-test ()
> (let ((grep-process (run-program "/usr/bin/grep" '("lisp")
> :input :stream
> :output :stream)))
> (unwind-protect
> (with-open-stream (s (process-input grep-process))
> (let ((ls-process (run-program "/bin/ls" '()
> :output s)))
> (when ls-process
> (unwind-protect
> (with-open-stream (o (process-output grep-process))
> (loop
> :for line := (read-line o nil nil)
> :while line
> :collect line))
> (process-close ls-process)))))
> (when grep-process (process-close grep-process)))))

Try this one. It works for me with CMUCL; perhaps it works with SBCL.
It basically starts ls first before starting grep.

(defun piping-test2 ()


(let ((ls-process (run-program "/bin/ls" '()

:wait nil
:output :stream)))
(unwind-protect
(with-open-stream (s (process-output ls-process))


(let ((grep-process (run-program "/usr/bin/grep" '("lisp")

:input s
:output :stream)))
(when grep-process


(unwind-protect
(with-open-stream (o (process-output grep-process))
(loop
:for line := (read-line o nil nil)
:while line
:collect line))

(process-close grep-process)))))
(when ls-process (process-close ls-process)))))

Ray

Mirko

unread,
Mar 1, 2010, 1:49:06 PM3/1/10
to

Confirmed on on RHEL5 (slightly different paths) and SBCL (only added
sb-ext package qualifiers)

(let ((ls-process (sb-ext:run-program "/bin/ls" '()


:wait nil
:output :stream)))
(unwind-protect

(with-open-stream (s (sb-ext:process-output ls-process))
(let ((grep-process (sb-ext:run-program "/bin/grep" '("lisp")


:input s
:output :stream)))
(when grep-process
(unwind-protect

(with-open-stream (o (sb-ext:process-output grep-process))


(loop
:for line := (read-line o nil nil)
:while line
:collect line))

(sb-ext:process-close grep-process)))))
(when ls-process (sb-ext:process-close ls-process))))

Thanks,

Mirko

Pillsy

unread,
Mar 2, 2010, 9:20:55 AM3/2/10
to
On Mar 1, 9:16 am, Raymond Toy <toy.raym...@gmail.com> wrote:
[...]

> Try this one.  It works for me with CMUCL; perhaps it works with SBCL.
> It basically starts ls first before starting grep.

Thanks, this works! Not only does it work with SBCL, with minor
modifications it also works with CCL[1].

One interesting wrinkle is that this takes a really long time (one
whole second) on SBCL when run through SLIME. It's orders of magnitude
faster when run from an SBCL on the command line (or when used through
the CCL Cocoa listener). This is disappointing, since I was hoping to
put something together that would let me "live" in SLIME and SBCL
instead of having to divide time between it and a terminal window.

Cheers,
Pillsy

> (defun piping-test2 ()
>   (let ((ls-process (run-program "/bin/ls" '()
>                                    :wait nil
>                                    :output :stream)))
>     (unwind-protect
>          (with-open-stream (s (process-output ls-process))
>            (let ((grep-process (run-program "/usr/bin/grep" '("lisp")
>                                           :input s
>                                           :output :stream)))
>              (when grep-process
>                (unwind-protect
>                     (with-open-stream (o (process-output grep-process))
>                       (loop
>                          :for line := (read-line o nil nil)
>                          :while line
>                          :collect line))
>                  (process-close grep-process)))))
>       (when ls-process (process-close ls-process)))))

[1] CCL's RUN-PROGRAM is evidently a pretty close copy of the one
provided by SBCL and CMUCL.

Raymond Toy

unread,
Mar 2, 2010, 10:21:25 AM3/2/10
to
On 3/2/10 9:20 AM, Pillsy wrote:
> On Mar 1, 9:16 am, Raymond Toy <toy.raym...@gmail.com> wrote:
> [...]
>> Try this one. It works for me with CMUCL; perhaps it works with SBCL.
>> It basically starts ls first before starting grep.
>
> Thanks, this works! Not only does it work with SBCL, with minor
> modifications it also works with CCL[1].

Cool.

But if you want to do pipes like this, why not just do something like
(untested):

(run-program "/bin/sh" '("-c" "ls | grep lisp") :output <output stream>)

Ray

Pillsy

unread,
Mar 2, 2010, 10:27:07 AM3/2/10
to
On Mar 2, 10:21 am, Raymond Toy <toy.raym...@gmail.com> wrote:
> On 3/2/10 9:20 AM, Pillsy wrote:
[...]

> > Thanks, this works! Not only does it work with SBCL, with minor
> > modifications it also works with CCL[1].

> Cool.

> But if you want to do pipes like this, why not just do something like
> (untested):

> (run-program "/bin/sh" '("-c" "ls | grep lisp") :output <output stream>)

That means giviing up on the possibility of sticking Lisp code into
the pipeline, which is (part of) my ultimate goal here. Perhaps that's
just completely unworkable though, or this is the wrong level of
abstraction to attack the goal at.

Cheers,
Pillsy

0 new messages