How to process input and output streams in Steel Bank Common Lisp?

前端 未结 3 525
萌比男神i
萌比男神i 2020-12-28 21:36

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

3条回答
  •  温柔的废话
    2020-12-28 22:03

    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.

提交回复
热议问题