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

Last change on this file since 14196 was 14121, checked in by ehuelsmann, 8 years ago

Move exports from autoloads.lisp to the respective defining files.
Also, delete JVM-COMPILE-PACKAGE, which hasn't been used in our
sources for ages and doesn't seem to serve an external purpose.

File size: 6.5 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(export '(run-program process process-p process-input process-output
37          process-error process-alive-p process-wait process-exit-code
38          process-kill))
39
40;;; Vaguely inspired by sb-ext:run-program in SBCL.
41;;;
42;;; See <http://www.sbcl.org/manual/Running-external-programs.html>.
43;;;
44;;; This implementation uses the JVM facilities for running external
45;;; processes.
46;;; <http://download.oracle.com/javase/6/docs/api/java/lang/ProcessBuilder.html>.
47(defun run-program (program args &key environment (wait t) clear-env)
48  ;;For documentation, see below.
49  (let ((pb (%make-process-builder program args)))
50    (let ((env-map (%process-builder-environment pb)))
51      (when clear-env
52        (%process-builder-env-clear env-map))           
53      (when environment
54        (dolist (entry environment)
55          (%process-builder-env-put env-map
56                                    (princ-to-string (car entry))
57                                    (princ-to-string (cdr entry))))))
58    (let ((process (make-process (%process-builder-start pb))))
59      (when wait (process-wait process))
60      process)))
61
62(setf (documentation 'run-program 'function)
63      "Creates a new process running the the PROGRAM.
64ARGS are a list of strings to be passed to the program as arguments.
65
66For no arguments, use nil which means that just the name of the
67program is passed as arg 0.
68
69Returns a process structure containing the JAVA-OBJECT wrapped Process
70object, and the PROCESS-INPUT, PROCESS-OUTPUT, and PROCESS-ERROR streams.
71
72c.f. http://download.oracle.com/javase/6/docs/api/java/lang/Process.html
73
74Notes about Unix environments (as in the :environment):
75
76    * The ABCL implementation of run-program, like SBCL, Perl and many
77      other programs, copies the Unix environment by default.
78
79    * Running Unix programs from a setuid process, or in any other
80      situation where the Unix environment is under the control of
81      someone else, is a mother lode of security problems. If you are
82      contemplating doing this, read about it first. (The Perl
83      community has a lot of good documentation about this and other
84      security issues in script-like programs.)
85
86The &key arguments have the following meanings:
87
88:environment
89    An alist of STRINGs (name . value) describing new
90    environment values that replace existing ones.
91
92:clear-env
93    If non-NIL, the current environment is cleared before the
94    values supplied by :environment are inserted.
95
96:wait
97    If non-NIL, which is the default, wait until the created process
98    finishes. If NIL, continue running Lisp until the program
99    finishes.")
100
101;;The process structure.
102
103(defstruct (process (:constructor %make-process (jprocess)))
104  jprocess input output error)
105
106(defun make-process (proc)
107  (let ((process (%make-process proc)))
108    (setf (process-input process) (%make-process-input-stream proc))
109    (setf (process-output process) (%make-process-output-stream proc))
110    (setf (process-error process) (%make-process-error-stream proc))
111    process))
112
113(defun process-alive-p (process)
114  "Return t if process is still alive, nil otherwise."
115  (%process-alive-p (process-jprocess process)))
116
117(defun process-wait (process)
118  "Wait for process to quit running for some reason."
119  (%process-wait (process-jprocess process)))
120
121(defun process-exit-code (instance)
122  "The exit code of a process."
123  (%process-exit-code (process-jprocess instance)))
124
125(defun process-kill (process)
126  "Kills the process."
127  (%process-kill (process-jprocess process)))
128
129;;; Low-level functions. For now they're just a refactoring of the
130;;; initial implementation with direct jnew & jcall forms in the
131;;; code. As per Ville's suggestion, these should really be implemented
132;;; as primitives.
133(defun %make-process-builder (program args)
134  (java:jnew "java.lang.ProcessBuilder"
135             (java:jnew-array-from-list "java.lang.String" (cons program args))))
136
137(defun %process-builder-environment (pb)
138  (java:jcall "environment" pb))
139
140(defun %process-builder-env-put (env-map key value)
141  (java:jcall "put" env-map key value))
142
143(defun %process-builder-env-clear (env-map)
144  (java:jcall "clear" env-map))
145
146(defun %process-builder-start (pb)
147  (java:jcall "start" pb))
148
149(defun %make-process-input-stream (proc)
150  (java:jnew "org.armedbear.lisp.Stream" 'system-stream
151             (java:jcall "getOutputStream" proc) ;;not a typo!
152             'character))
153
154(defun %make-process-output-stream (proc)
155  (java:jnew "org.armedbear.lisp.Stream" 'system-stream
156             (java:jcall "getInputStream" proc) ;;not a typo|
157             'character))
158
159(defun %make-process-error-stream (proc)
160  (java:jnew "org.armedbear.lisp.Stream" 'system-stream
161             (java:jcall "getErrorStream" proc)
162             'character))
163
164(defun %process-alive-p (jprocess)
165  (not (ignore-errors (java:jcall "exitValue" jprocess))))
166
167(defun %process-wait (jprocess)
168  (java:jcall "waitFor" jprocess))
169
170(defun %process-exit-code (jprocess)
171  (ignore-errors (java:jcall "exitValue" jprocess)))
172
173(defun %process-kill (jprocess)
174  (java:jcall "destroy" jprocess))
Note: See TracBrowser for help on using the repository browser.