1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
6 ;;;; Purpose: Operating System utilities
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Jul 2003
12 ;;;; *************************************************************************
16 (defun command-output (control-string &rest args)
17 "Interpolate ARGS into CONTROL-STRING as if by FORMAT, and
18 synchronously execute the result using a Bourne-compatible shell,
19 returns (VALUES string-output error-output exit-status)"
20 (let ((command (apply #'format nil control-string args)))
22 (let* ((process (sb-ext:run-program
25 :input nil :output :stream :error :stream))
26 (output (read-stream-to-string (sb-impl::process-output process)))
27 (error (read-stream-to-string (sb-impl::process-error process))))
28 (close (sb-impl::process-output process))
29 (close (sb-impl::process-error process))
33 (sb-impl::process-exit-code process)))
37 (let* ((process (ext:run-program
40 :input nil :output :stream :error :stream))
41 (output (read-stream-to-string (ext::process-output process)))
42 (error (read-stream-to-string (ext::process-error process))))
43 (close (ext::process-output process))
44 (close (ext::process-error process))
49 (ext::process-exit-code process)))
52 (multiple-value-bind (output error status)
53 (excl.osi:command-output command :whole t)
54 (values output error status))
57 ;; BUG: Lispworks combines output and error streams
58 (let ((output (make-string-output-stream)))
61 (system:call-system-showing-output
65 :output-stream output)))
66 (values (get-output-stream-string output) nil status))
70 ;; BUG: CLisp doesn't allow output to user-specified stream
74 (ext:run-shell-command command :output :terminal :wait t))
77 (let* ((process (ccl:run-program
80 :input nil :output :stream :error :stream
82 (output (read-stream-to-string (ccl::external-process-output-stream process)))
83 (error (read-stream-to-string (ccl::external-process-error-stream process))))
84 (close (ccl::external-process-output-stream process))
85 (close (ccl::external-process-error-stream process))
88 (nth-value 1 (ccl::external-process-status process))))
90 #-(or openmcl clisp lispworks allegro scl cmu sbcl)
91 (error "COMMAND-OUTPUT not implemented for this Lisp")
95 (defun run-shell-command (control-string &rest args)
96 "Interpolate ARGS into CONTROL-STRING as if by FORMAT, and
97 synchronously execute the result using a Bourne-compatible shell,
98 returns (VALUES output-string pid)"
99 (let ((command (apply #'format nil control-string args)))
101 (sb-impl::process-exit-code
105 :input nil :output nil))
108 (ext:process-exit-code
112 :input nil :output nil))
116 (excl:run-shell-command command :input nil :output nil
120 (system:call-system-showing-output
122 :shell-type "/bin/sh"
127 #+clisp ;XXX not exactly *verbose-out*, I know
128 (ext:run-shell-command command :output :terminal :wait t)
132 (ccl:external-process-status
133 (ccl:run-program "/bin/sh" (list "-c" command)
134 :input nil :output nil
137 #-(or openmcl clisp lispworks allegro scl cmu sbcl)
138 (error "RUN-SHELL-PROGRAM not implemented for this Lisp")
142 (defun delete-directory-and-files (dir &key (if-does-not-exist :error) (quiet t) force)
143 #+allegro (excl:delete-directory-and-files dir :if-does-not-exist if-does-not-exist
144 :quiet quiet :force force)
145 #-(or allegro) (declare (ignore force))
147 ((probe-directory dir)
148 (let ((cmd (format nil "rm -rf ~A" (namestring dir))))
150 (format *trace-output* ";; ~A" cmd))
151 (command-output cmd)))
152 ((eq if-does-not-exist :error)
153 (error "Directory ~A does not exist [delete-directory-and-files]." dir))))
155 (defun file-size (file)
156 (when (probe-file file)
157 #+allegro (let ((stat (excl.osi:stat (namestring file))))
158 (excl.osi:stat-size stat))
160 (with-open-file (in file :direction :input)
164 "Return the PID of the lisp process."
165 #+allegro (excl::getpid)
166 #+(and lispworks win32) (win32:get-current-process-id)
167 #+(and lispworks (not win32)) (system::getpid)
168 #+sbcl (sb-posix:getpid)
169 #+cmu (unix:unix-getpid)
170 #+openmcl (ccl::getpid)
171 #+(and clisp unix) (system::process-id)
172 #+(and clisp win32) (cond ((find-package :win32)
173 (funcall (find-symbol "GetCurrentProcessId"
176 (system::getenv "PID")))