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