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
I got a working answer from Raymond Toy on comp.lang.lisp. His solution was for CMUCL, but it worked with the essentially identical RUN-PROGRAM
function on the closely related SBCL, and with minor changes it will work on CCL as well, because CCL's RUN-PROGRAM
is basically a clone of the one from CMUCL/SBCL.
The secret, as it were, is to set up the ls
process first, and then provide its output stream to the grep
process as input, like so:
(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)))))
I also experimented with omitting the :WAIT NIL
argument from the RUN-PROGRAM
call for ls
, and it worked just as well.