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

Last change on this file since 13899 was 13899, checked in by vvoutilainen, 9 years ago

Add :clear-env to run-program, add getenv-all

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