[armedbear-cvs] r13271 - trunk/abcl/src/org/armedbear/lisp

Alessio Stalla astalla at common-lisp.net
Mon Apr 18 20:34:00 UTC 2011


Author: astalla
Date: Mon Apr 18 16:33:58 2011
New Revision: 13271

Log:
Included changes to compile-system.lisp missing from previous commit.
Refactored run-program to abstract jnew and jcall to low-level functions
(to be replaced by primitives).


Modified:
   trunk/abcl/src/org/armedbear/lisp/compile-system.lisp
   trunk/abcl/src/org/armedbear/lisp/run-program.lisp

Modified: trunk/abcl/src/org/armedbear/lisp/compile-system.lisp
==============================================================================
--- trunk/abcl/src/org/armedbear/lisp/compile-system.lisp	(original)
+++ trunk/abcl/src/org/armedbear/lisp/compile-system.lisp	Mon Apr 18 16:33:58 2011
@@ -238,6 +238,7 @@
                            "revappend.lisp"
                            "rotatef.lisp"
                            ;;"run-benchmarks.lisp"
+                           "run-program.lisp"
                            "run-shell-command.lisp"
                            ;;"runtime-class.lisp"
                            "search.lisp"

Modified: trunk/abcl/src/org/armedbear/lisp/run-program.lisp
==============================================================================
--- trunk/abcl/src/org/armedbear/lisp/run-program.lisp	(original)
+++ trunk/abcl/src/org/armedbear/lisp/run-program.lisp	Mon Apr 18 16:33:58 2011
@@ -36,18 +36,34 @@
 ;;Vaguely inspired by sb-ext:run-program in SBCL. See <http://www.sbcl.org/manual/Running-external-programs.html>. This implementation uses the JVM facilities for running external processes: <http://download.oracle.com/javase/6/docs/api/java/lang/ProcessBuilder.html>.
 (defun run-program (program args &key environment (wait t))
   ;;For documentation, see below.
-  (let ((pb (java:jnew "java.lang.ProcessBuilder"
-                       (java:jnew-array-from-list "java.lang.String" (cons program args)))))
+  (let ((pb (%make-process-builder program args)))
     (when environment
-      (let ((env-map (java:jcall "environment" pb)))
+      (let ((env-map (%process-builder-environment pb)))
         (dolist (entry environment)
-          (java:jcall "put" env-map
-                 (princ-to-string (car entry))
-                 (princ-to-string (cdr entry))))))
-    (let ((process (make-process (java:jcall "start" pb))))
+          (%process-builder-env-put env-map
+                                    (princ-to-string (car entry))
+                                    (princ-to-string (cdr entry))))))
+    (let ((process (make-process (%process-builder-start pb))))
       (when wait (process-wait process))
       process)))
 
+(setf (documentation 'run-program 'function)
+      "run-program creates a new process specified by the program argument. args are the standard arguments that can be passed to a program. For no arguments, use nil (which means that just the name of the program is passed as arg 0).
+
+run-program will return a process structure.
+
+Notes about Unix environments (as in the :environment):
+
+    * The ABCL implementation of run-program, like SBCL, Perl and many other programs, copies the Unix environment by default.
+    * Running Unix programs from a setuid process, or in any other situation where the Unix environment is under the control of someone else, is a mother lode of security problems. If you are contemplating doing this, read about it first. (The Perl community has a lot of good documentation about this and other security issues in script-like programs.)
+
+The &key arguments have the following meanings:
+
+:environment
+    a alist of STRINGs (name . value) describing the new environment. The default is to copy the environment of the current process.
+:wait
+    If non-NIL (default), wait until the created process finishes. If nil, continue running Lisp until the program finishes.")
+
 ;;The process structure.
 
 (defstruct (process (:constructor %make-process (jprocess)))
@@ -55,49 +71,67 @@
 
 (defun make-process (proc)
   (let ((process (%make-process proc)))
-    (setf (process-input process)
-          (java:jnew "org.armedbear.lisp.Stream" 'system-stream
-                     (java:jcall "getOutputStream" proc)
-                     'character)) ;;not a typo!
-    (setf (process-output process)
-          (java:jnew "org.armedbear.lisp.Stream" 'system-stream
-                (java:jcall "getInputStream" proc) ;;not a typo|
-                'character))
-    (setf (process-error process)
-          (java:jnew "org.armedbear.lisp.Stream" 'system-stream
-                (java:jcall "getErrorStream" proc)
-                'character))
+    (setf (process-input process) (%make-process-input-stream proc))
+    (setf (process-output process) (%make-process-output-stream proc))
+    (setf (process-error process) (%make-process-error-stream proc))
     process))
 
 (defun process-alive-p (process)
   "Return t if process is still alive, nil otherwise."
-  (not (ignore-errors (java:jcall "exitValue" (process-jprocess process)))))
+  (%process-alive-p (process-jprocess process)))
 
 (defun process-wait (process)
   "Wait for process to quit running for some reason."
-  (java:jcall "waitFor" (process-jprocess process)))
+  (%process-wait (process-jprocess process)))
 
 (defun process-exit-code (instance)
   "The exit code of a process."
-  (ignore-errors (java:jcall "exitValue" (process-jprocess instance))))
+  (%process-exit-code (process-jprocess instance)))
 
 (defun process-kill (process)
   "Kills the process."
-  (java:jcall "destroy" (process-jprocess process)))
+  (%process-kill (process-jprocess process)))
 
-(setf (documentation 'run-program 'function)
-      "run-program creates a new process specified by the program argument. args are the standard arguments that can be passed to a program. For no arguments, use nil (which means that just the name of the program is passed as arg 0).
+;;Low-level functions. For now they're just a refactoring of the initial implementation with direct
+;;jnew & jcall forms in the code. As per Ville's suggestion, these should really be implemented as
+;;primitives.
 
-run-program will return a process structure.
+(defun %make-process-builder (program args)
+  (java:jnew "java.lang.ProcessBuilder"
+             (java:jnew-array-from-list "java.lang.String" (cons program args))))
 
-Notes about Unix environments (as in the :environment):
+(defun %process-builder-environment (pb)
+  (java:jcall "environment" pb))
 
-    * The ABCL implementation of run-program, like SBCL, Perl and many other programs, copies the Unix environment by default.
-    * Running Unix programs from a setuid process, or in any other situation where the Unix environment is under the control of someone else, is a mother lode of security problems. If you are contemplating doing this, read about it first. (The Perl community has a lot of good documentation about this and other security issues in script-like programs.)
+(defun %process-builder-env-put (env-map key value)
+  (java:jcall "put" env-map key value))
 
-The &key arguments have the following meanings:
+(defun %process-builder-start (pb)
+  (java:jcall "start" pb))
 
-:environment
-    a alist of STRINGs (name . value) describing the new environment. The default is to copy the environment of the current process.
-:wait
-    If non-NIL (default), wait until the created process finishes. If nil, continue running Lisp until the program finishes.")
+(defun %make-process-input-stream (proc)
+  (java:jnew "org.armedbear.lisp.Stream" 'system-stream
+             (java:jcall "getOutputStream" proc) ;;not a typo!
+             'character))
+
+(defun %make-process-output-stream (proc)
+  (java:jnew "org.armedbear.lisp.Stream" 'system-stream
+             (java:jcall "getInputStream" proc) ;;not a typo|
+             'character))
+
+(defun %make-process-error-stream (proc)
+  (java:jnew "org.armedbear.lisp.Stream" 'system-stream
+             (java:jcall "getErrorStream" proc)
+             'character))
+
+(defun %process-alive-p (jprocess)
+  (not (ignore-errors (java:jcall "exitValue" jprocess))))
+
+(defun %process-wait (jprocess)
+  (java:jcall "waitFor" jprocess))
+
+(defun %process-exit-code (jprocess)
+  (ignore-errors (java:jcall "exitValue" jprocess)))
+
+(defun %process-kill (jprocess)
+  (java:jcall "destroy" jprocess))
\ No newline at end of file




More information about the armedbear-cvs mailing list