source: trunk/abcl/src/org/armedbear/lisp/run-program.lisp @ 13271

Last change on this file since 13271 was 13271, checked in by astalla, 10 years ago

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).

File size: 5.9 KB
Line 
1;;; run-program.lisp
2;;;
3;;; Copyright (C) 2011 Alessio Stalla
4;;; $Id$
5;;;
6;;; This program is free software; you can redistribute it and/or
7;;; modify it under the terms of the GNU General Public License
8;;; as published by the Free Software Foundation; either version 2
9;;; of the License, or (at your option) any later version.
10;;;
11;;; This program is distributed in the hope that it will be useful,
12;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with this program; if not, write to the Free Software
18;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19;;;
20;;; As a special exception, the copyright holders of this library give you
21;;; permission to link this library with independent modules to produce an
22;;; executable, regardless of the license terms of these independent
23;;; modules, and to copy and distribute the resulting executable under
24;;; terms of your choice, provided that you also meet, for each linked
25;;; independent module, the terms and conditions of the license of that
26;;; module.  An independent module is a module which is not derived from
27;;; or based on this library.  If you modify this library, you may extend
28;;; this exception to your version of the library, but you are not
29;;; obligated to do so.  If you do not wish to do so, delete this
30;;; exception statement from your version.
31
32(in-package "SYSTEM")
33
34(require "JAVA")
35
36;;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>.
37(defun run-program (program args &key environment (wait t))
38  ;;For documentation, see below.
39  (let ((pb (%make-process-builder program args)))
40    (when environment
41      (let ((env-map (%process-builder-environment pb)))
42        (dolist (entry environment)
43          (%process-builder-env-put env-map
44                                    (princ-to-string (car entry))
45                                    (princ-to-string (cdr entry))))))
46    (let ((process (make-process (%process-builder-start pb))))
47      (when wait (process-wait process))
48      process)))
49
50(setf (documentation 'run-program 'function)
51      "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).
52
53run-program will return a process structure.
54
55Notes about Unix environments (as in the :environment):
56
57    * The ABCL implementation of run-program, like SBCL, Perl and many other programs, copies the Unix environment by default.
58    * 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.)
59
60The &key arguments have the following meanings:
61
62:environment
63    a alist of STRINGs (name . value) describing the new environment. The default is to copy the environment of the current process.
64:wait
65    If non-NIL (default), wait until the created process finishes. If nil, continue running Lisp until the program finishes.")
66
67;;The process structure.
68
69(defstruct (process (:constructor %make-process (jprocess)))
70  jprocess input output error)
71
72(defun make-process (proc)
73  (let ((process (%make-process proc)))
74    (setf (process-input process) (%make-process-input-stream proc))
75    (setf (process-output process) (%make-process-output-stream proc))
76    (setf (process-error process) (%make-process-error-stream proc))
77    process))
78
79(defun process-alive-p (process)
80  "Return t if process is still alive, nil otherwise."
81  (%process-alive-p (process-jprocess process)))
82
83(defun process-wait (process)
84  "Wait for process to quit running for some reason."
85  (%process-wait (process-jprocess process)))
86
87(defun process-exit-code (instance)
88  "The exit code of a process."
89  (%process-exit-code (process-jprocess instance)))
90
91(defun process-kill (process)
92  "Kills the process."
93  (%process-kill (process-jprocess process)))
94
95;;Low-level functions. For now they're just a refactoring of the initial implementation with direct
96;;jnew & jcall forms in the code. As per Ville's suggestion, these should really be implemented as
97;;primitives.
98
99(defun %make-process-builder (program args)
100  (java:jnew "java.lang.ProcessBuilder"
101             (java:jnew-array-from-list "java.lang.String" (cons program args))))
102
103(defun %process-builder-environment (pb)
104  (java:jcall "environment" pb))
105
106(defun %process-builder-env-put (env-map key value)
107  (java:jcall "put" env-map key value))
108
109(defun %process-builder-start (pb)
110  (java:jcall "start" pb))
111
112(defun %make-process-input-stream (proc)
113  (java:jnew "org.armedbear.lisp.Stream" 'system-stream
114             (java:jcall "getOutputStream" proc) ;;not a typo!
115             'character))
116
117(defun %make-process-output-stream (proc)
118  (java:jnew "org.armedbear.lisp.Stream" 'system-stream
119             (java:jcall "getInputStream" proc) ;;not a typo|
120             'character))
121
122(defun %make-process-error-stream (proc)
123  (java:jnew "org.armedbear.lisp.Stream" 'system-stream
124             (java:jcall "getErrorStream" proc)
125             'character))
126
127(defun %process-alive-p (jprocess)
128  (not (ignore-errors (java:jcall "exitValue" jprocess))))
129
130(defun %process-wait (jprocess)
131  (java:jcall "waitFor" jprocess))
132
133(defun %process-exit-code (jprocess)
134  (ignore-errors (java:jcall "exitValue" jprocess)))
135
136(defun %process-kill (jprocess)
137  (java:jcall "destroy" jprocess))
Note: See TracBrowser for help on using the repository browser.