From: Pillsy on
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
From: Pascal J. Bourguignon on
Pillsy <pillsbury(a)gmail.com> writes:

> 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.

(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__
From: Pillsy on
On Feb 28, 2:01 pm, p...(a)informatimago.com (Pascal J. Bourguignon)
wrote:
> Pillsy <pillsb...(a)gmail.com> writes:

> > 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.

> (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.

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
From: Raymond Toy on
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

From: Mirko on
On Mar 1, 9:16 am, Raymond Toy <toy.raym...(a)gmail.com> wrote:
> 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

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