cl-rw

Layered streams for Common Lisp
git clone https://logand.com/git/cl-rw.git/
Log | Files | Refs

commit 2e5c192d6d90ad6cec970421bda48ceeeec1d31a
parent 48b1a06bdba2debb4f1c7a0b5c6d32b77f3f9905
Author: Tomas Hlavaty <tom@logand.com>
Date:   Wed, 28 Aug 2013 23:26:50 +0200

rw.os:run-program refactored

Diffstat:
Mos.lisp | 74+++++++++++++++++++++++---------------------------------------------------
1 file changed, 23 insertions(+), 51 deletions(-)

diff --git a/os.lisp b/os.lisp @@ -33,52 +33,18 @@ (let ((reason (or (cdr (assoc code error-plist)) ""))) (error (format nil "~a error ~d: ~a ~s" cmd code reason args))))) -(defun run-command (cmd args &optional error-plist) - #-(or cmu sbcl clisp openmcl ecl) - (error "TODO port RW.OS:RUN-COMMAND") - (let ((p - #+cmu(ext:run-program cmd args) - #+sbcl(sb-ext:run-program cmd args :search t) - #+clisp(ext:run-program cmd :arguments args) - #+openmcl(ccl:run-program cmd args) - #+ecl (nth-value 2 (ext:run-program cmd args - :input nil - :output nil - :error nil)))) - (when p - (unwind-protect - (let ((code #+cmu(ext:process-exit-code p) - #+sbcl(sb-ext:process-exit-code p) - #+clisp 0 - #+openmcl(multiple-value-bind (a b) - (ccl:external-process-status p) - (declare (ignore a)) - b) - #+ecl(ext::external-process-%code p))) - (if (eq 0 code) - t - (throw-error cmd args code error-plist))) - #+cmu(ext:process-close p) - #+sbcl(sb-ext:process-close p) - #+openmcl(flet ((finish (x) (when x (close x)))) - (finish (ccl:external-process-output-stream p)) - (finish (ccl:external-process-input-stream p)) - (finish (ccl:external-process-error-stream p))) - ;; TODO ecl? - )))) - -(defun call-with-program-output (cmd args error-plist fn) +(defun call-with-program-output (output cmd args error-plist fn) #-(or cmu sbcl clisp openmcl ecl) (error "TODO port RW.OS::CALL-WITH-PROGRAM-OUTPUT") (let ((p - #+cmu(ext:run-program cmd args :output :stream) - #+sbcl(sb-ext:run-program cmd args :output :stream :search t) - #+clisp(ext:run-program cmd :arguments args :output :stream) - #+openmcl(ccl:run-program cmd args :output :stream) - #+ecl (nth-value 2 (ext:run-program cmd args - :input nil - :output :stream - :error nil)))) + #+cmu(ext:run-program cmd args :output output) + #+sbcl(sb-ext:run-program cmd args :output output :search t) + #+clisp(ext:run-program cmd :arguments args :output output) + #+openmcl(ccl:run-program cmd args :output output) + #+ecl(nth-value 2 (ext:run-program cmd args + :input nil + :output output + :error nil)))) (when p (unwind-protect (let ((code #+cmu(ext:process-exit-code p) @@ -90,12 +56,14 @@ b) #+ecl(ext::external-process-%code p))) (if (eq 0 code) - (funcall fn - #+cmu(ext:process-output p) - #+sbcl(sb-ext:process-output p) - #+clisp p - #+openmcl(ccl:external-process-output-stream p) - #+ecl(ext:external-process-output p)) + (if (eq :stream output) + (funcall fn + #+cmu(ext:process-output p) + #+sbcl(sb-ext:process-output p) + #+clisp p + #+openmcl(ccl:external-process-output-stream p) + #+ecl(ext:external-process-output p)) + t) (throw-error cmd args code error-plist))) #+cmu(ext:process-close p) #+sbcl(sb-ext:process-close p) @@ -103,11 +71,15 @@ (finish (ccl:external-process-output-stream p)) (finish (ccl:external-process-input-stream p)) (finish (ccl:external-process-error-stream p))) - ;; TODO ecl? + ;; TODO ecl process clean up? )))) +(defun run-command (cmd args &optional error-plist) + (call-with-program-output nil cmd args error-plist nil)) + (defmacro with-program-output ((var cmd args &optional error-plist) &body body) - `(call-with-program-output ,cmd ,args ,error-plist (lambda (,var) ,@body))) + `(call-with-program-output :stream ,cmd ,args ,error-plist + (lambda (,var) ,@body))) (defun sha1sum (file) (let ((name (format nil "~a" file)))