source: branches/streams/abcl/src/org/armedbear/lisp/run-program.lisp

Last change on this file was 14732, checked in by Mark Evenson, 10 years ago

abcl-asdf: fix RESOLVE to work with "group-id:artifact-id" parameters.

MVN-VERSION now tries harder to include diagnostic information when it
signals an error.

Fixes <http://abcl.org/trac/ticket/372>.

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