r8931: command-output fixes
[kmrcl.git] / os.lisp
diff --git a/os.lisp b/os.lisp
index 9b193ab0119dbd81ca3f2c6696076464a63548d5..954a27f84796e1d277d29cd14319e60a92f01883 100644 (file)
--- a/os.lisp
+++ b/os.lisp
 
 (in-package #:kmrcl)
 
+(defun command-output (control-string &rest args)
+  "Interpolate ARGS into CONTROL-STRING as if by FORMAT, and
+synchronously execute the result using a Bourne-compatible shell, 
+returns (VALUES string-output error-output exit-status)"
+  (let ((command (apply #'format nil control-string args)))
+    #+sbcl
+    (let ((process (sb-ext:run-program  
+                   "/bin/sh"
+                   (list "-c" command)
+                   :input nil :output :stream :error :stream)))
+      (values
+       (sb-impl::process-output process)
+       (sb-impl::process-error process)
+       (sb-impl::process-exit-code process)))
+    
+    #+(or cmu scl)
+    (let ((process (ext:run-program  
+                   "/bin/sh"
+                   (list "-c" command)
+                   :input nil :output :stream :error :stream)))
+      (values
+       (ext::process-output process)
+       (ext::process-error process)
+       (ext::process-exit-code process)))    
+
+    #+allegro
+    (multiple-value-bind (output error status)
+       (excl.osi:command-output command :whole t)
+      (values output error status))
+    
+    #+lispworks
+    ;; BUG: Lispworks combines output and error streams
+    (let ((output (make-output-string-stream)))
+      (unwind-protect
+         (let ((status 
+                (system:call-system-showing-output
+                 command
+                 :shell-type "/bin/sh"
+                 :output-stream output)))
+           (values (get-output-string output) nil status))
+       (close output)))
+    
+    #+clisp            
+    ;; BUG: CLisp doesn't allow output to user-specified stream
+    (values
+     nil
+     nil
+     (ext:run-shell-command  command :output :terminal :wait t))
+    
+    #+openmcl
+    (let* ((process (ccl:run-program  
+                    "/bin/sh"
+                    (list "-c" command)
+                    :input nil :output :stream :error :stream
+                    :wait t))
+          (output (read-stream-to-string (ccl::external-process-output-stream process)))
+          (error (read-stream-to-string (ccl::external-process-error-stream process))))
+      (close (ccl::external-process-output-stream process))
+      (close (ccl::external-process-error-stream process))
+      (values output
+             error
+             (nth-value 1 (ccl::external-process-status process))))
+  
+    #-(or openmcl clisp lispworks allegro scl cmu sbcl)
+    (error "COMMAND-OUTPUT not implemented for this Lisp")
+
+    ))
+
 (defun run-shell-command (control-string &rest args)
   "Interpolate ARGS into CONTROL-STRING as if by FORMAT, and
 synchronously execute the result using a Bourne-compatible shell, 
-returns (VALUES string-output exit-code)"
+returns (VALUES output-string pid)"
   (let ((command (apply #'format nil control-string args)))
     #+sbcl
     (sb-impl::process-exit-code
@@ -32,13 +100,11 @@ returns (VALUES string-output exit-code)"
       (list  "-c" command)
       :input nil :output nil))
     
-    #+allegro
-    (multiple-value-bind (output dummy exit)
-       (excl:run-shell-command command :input nil :output :stream
-                               :wait nil)
-      (declare (ignore dummy))
-      (values output exit))
     
+    #+allegro
+    (excl:run-shell-command command :input nil :output nil
+                           :wait t)
+
     #+lispworks
     (system:call-system-showing-output
      command