Advertisement
Guest User

Using ABCL to create a Unix-like process pipe

a guest
May 19th, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.79 KB | None | 0 0
  1. (defun %run/process (specification &key input output error (wait nil))
  2.   "Runs the command-string with args as arguments and input and output streams."
  3.   (let* ((external-process
  4.           (system:run-program (first specification) (flatten (rest specification)) :wait wait))
  5.          (instance
  6.           (make-instance 'process-state
  7.                          :external-process external-process
  8.                          :input input
  9.                          :output output
  10.                          :error error)))
  11.     (with-slots (piper-threads) instance
  12.       (setf piper-threads nil)
  13.       (unless (or (null input) (eql input :stream))
  14.         (push (make-thread
  15.                #'(lambda ()
  16.                    (handler-case (copy-stream input (system:process-input external-process))
  17.                      (stream-error (c) t))
  18.                    (close (system:process-input external-process)))
  19.                :name "org.lispos.clash:piper-input")
  20.               piper-threads))
  21.       (unless (or (null output) (eql output :stream))
  22.         (push (make-thread
  23.                #'(lambda ()
  24.                    (handler-case (copy-stream (system:process-output external-process) output)
  25.                      (stream-error (c) t)))
  26.                :name "org.lispos.clash:piper-output")
  27.               piper-threads))
  28.       (unless (or (null error) (eql error :stream))
  29.         (push (make-thread
  30.                #'(lambda ()
  31.                    (handler-case (copy-stream (system:process-error external-process) error)
  32.                      (stream-error (c) t)))
  33.                :name "org.lispos.clash:piper-error")
  34.               piper-threads))
  35.       (when wait
  36.         (loop :for thread :in piper-threads :do (bordeaux-threads:join-thread thread))
  37.         (setf piper-threads nil)))
  38.     instance))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement