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

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

simple run-program implementation using the JRE ProcessBuilder? class.

File size: 4.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 (java:jnew "java.lang.ProcessBuilder"
40                       (java:jnew-array-from-list "java.lang.String" (cons program args)))))
41    (when environment
42      (let ((env-map (java:jcall "environment" pb)))
43        (dolist (entry environment)
44          (java:jcall "put" env-map
45                 (princ-to-string (car entry))
46                 (princ-to-string (cdr entry))))))
47    (let ((process (make-process (java:jcall "start" pb))))
48      (when wait (process-wait process))
49      process)))
50
51;;The process structure.
52
53(defstruct (process (:constructor %make-process (jprocess)))
54  jprocess input output error)
55
56(defun make-process (proc)
57  (let ((process (%make-process proc)))
58    (setf (process-input process)
59          (java:jnew "org.armedbear.lisp.Stream" 'system-stream
60                     (java:jcall "getOutputStream" proc)
61                     'character)) ;;not a typo!
62    (setf (process-output process)
63          (java:jnew "org.armedbear.lisp.Stream" 'system-stream
64                (java:jcall "getInputStream" proc) ;;not a typo|
65                'character))
66    (setf (process-error process)
67          (java:jnew "org.armedbear.lisp.Stream" 'system-stream
68                (java:jcall "getErrorStream" proc)
69                'character))
70    process))
71
72(defun process-alive-p (process)
73  "Return t if process is still alive, nil otherwise."
74  (not (ignore-errors (java:jcall "exitValue" (process-jprocess process)))))
75
76(defun process-wait (process)
77  "Wait for process to quit running for some reason."
78  (java:jcall "waitFor" (process-jprocess process)))
79
80(defun process-exit-code (instance)
81  "The exit code of a process."
82  (ignore-errors (java:jcall "exitValue" (process-jprocess instance))))
83
84(defun process-kill (process)
85  "Kills the process."
86  (java:jcall "destroy" (process-jprocess process)))
87
88(setf (documentation 'run-program 'function)
89      "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).
90
91run-program will return a process structure.
92
93Notes about Unix environments (as in the :environment):
94
95    * The ABCL implementation of run-program, like SBCL, Perl and many other programs, copies the Unix environment by default.
96    * 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.)
97
98The &key arguments have the following meanings:
99
100:environment
101    a alist of STRINGs (name . value) describing the new environment. The default is to copy the environment of the current process.
102:wait
103    If non-NIL (default), wait until the created process finishes. If nil, continue running Lisp until the program finishes.")
Note: See TracBrowser for help on using the repository browser.