Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added shell-command-to-string

  • Loading branch information...
commit 3510ec9c7c42dd6635561fe2b891e8e519f316d0 1 parent 790a455
@wesen wesen authored
Showing with 76 additions and 0 deletions.
  1. +4 −0 src/utils/package.lisp
  2. +72 −0 src/utils/utils.lisp
View
4 src/utils/package.lisp
@@ -31,6 +31,10 @@
#:next-day
#:month-num-days
+ ;; shell functions
+ #:run-shell-command-to-string
+ #:run-shell-command
+
#:hostname
#:parse-time
View
72 src/utils/utils.lisp
@@ -134,6 +134,78 @@
"~d~2,'0d~2,'0d"
year month date)))
+;;; run shell command and return output as string
+;;; (lifted out of ASDF)
+
+(defun run-shell-command-to-string (control-string &rest args)
+ "Interpolate ARGS into CONTROL-STRING as if by FORMAT, and
+synchronously execute the result using a Bourne-compatible shell.
+ Returns the output and the shell's exit code."
+ (let* ((result)
+ (command (apply #'format nil control-string args))
+ (str (with-output-to-string (s)
+ (setf result (run-shell-command command s)))))
+ (values str result)))
+
+(defun run-shell-command (command s)
+ #+abcl
+ (ext:run-shell-command command :output s)
+
+ #+allegro
+ ;; will this fail if command has embedded quotes - it seems to work
+ (multiple-value-bind (stdout stderr exit-code)
+ (excl.osi:command-output
+ (format nil "~a -c \"~a\""
+ #+mswindows "sh" #-mswindows "/bin/sh" command)
+ :input nil :whole nil
+ #+mswindows :show-window #+mswindows :hide)
+ (format s "~{~&; ~a~%~}~%" stderr)
+ (format s "~{~&; ~a~%~}~%" stdout)
+ exit-code)
+
+ #+clisp ;XXX not exactly s, I know
+ (ext:run-shell-command command :output :terminal :wait t)
+
+ #+clozure
+ (nth-value 1
+ (ccl:external-process-status
+ (ccl:run-program "/bin/sh" (list "-c" command)
+ :input nil :output s
+ :wait t)))
+
+ #+ecl ;; courtesy of Juan Jose Garcia Ripoll
+ (si:system command)
+
+ #+gcl
+ (lisp:system command)
+
+ #+lispworks
+ (system:call-system-showing-output
+ command
+ :shell-type "/bin/sh"
+ :show-cmd nil
+ :prefix ""
+ :output-stream s)
+
+ #+sbcl
+ (sb-ext:process-exit-code
+ (apply #'sb-ext:run-program
+ #+win32 "sh" #-win32 "/bin/sh"
+ (list "-c" command)
+ :input nil :output s
+ #+win32 '(:search t) #-win32 nil))
+
+ #+(or cmu scl)
+ (ext:process-exit-code
+ (ext:run-program
+ "/bin/sh"
+ (list "-c" command)
+ :input nil :output s))
+
+ #-(or abcl allegro clisp clozure cmu ecl gcl lispworks sbcl scl)
+ (error "RUN-SHELL-COMMAND not implemented for this Lisp")
+ )
+
;;; local hostname
Please sign in to comment.
Something went wrong with that request. Please try again.