Debian Codegraph @

src/cmp/cmpos-run.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: C -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;;; Copyright (c) 2010, Juan Jose Garcia Ripoll
6 ;;;;
7 ;;;; See file 'LICENSE' for the copyright details.
8
9 ;;;; CMPOS-RUN Executing auxiliary programs
10
11 (in-package "COMPILER")
12
13 #+(and cygwin (not ecl-min))
14 (ffi:clines "#include <stdlib.h>")
15
16 ;;; this is needed by compile.lsp.in
17 (defun safe-system (string)
18 (cmpnote "Invoking external command:~% ~A~%" string)
19 (let ((result (ext:system string)))
20 (unless (zerop result)
21 (cerror "Continues anyway."
22 "(SYSTEM ~S) returned non-zero value ~D"
23 string result))))
24
25 (defun save-directory (forms)
26 (let ((directory
27 (probe-file (make-pathname :name nil :type nil
28 :defaults *default-pathname-defaults*))))
29 (if directory
30 (let ((*default-pathname-defaults* directory)
31 (old-directory (ext:chdir directory)))
32 (unwind-protect
33 (funcall forms)
34 (ext:chdir old-directory)))
35 (funcall forms))))
36
37 (defmacro with-current-directory (&body forms)
38 `(save-directory #'(lambda () ,@forms)))
39
40 (defun safe-run-program (program args &aux result output)
41 (cmpnote "Invoking external command:~% ~A ~{~A ~}" program args)
42 (let* ((*standard-output* ext:+process-standard-output+)
43 (*error-output* ext:+process-error-output+)
44 (program (split-program-options program))
45 (args `(,@(cdr program) ,@args))
46 (program (car program)))
47 (with-current-directory
48 ;; when compiling ECL itself, we only have low-level functions
49 ;; available ...
50 #+(and ecl-min (not cygwin))
51 (multiple-value-bind (output-stream return-status pid)
52 (si:run-program-inner program args :default nil)
53 #+msvc
54 (si::stream-external-format-set output-stream
55 (list (si::windows-codepage-encoding) :crlf))
56 (setf output (collect-lines output-stream))
57 (multiple-value-setq (return-status result)
58 (si:waitpid pid t)))
59 ;; ... otherwise we can use run-program and get proper
60 ;; quoting of arguments ...
61 #+(and (not ecl-min) (not cygwin))
62 (multiple-value-bind (output-stream return-status process-obj)
63 (ext:run-program program args :wait nil
64 #+msvc :external-format
65 #+msvc (list (si::windows-codepage-encoding) :crlf))
66 (setf output (collect-lines output-stream))
67 (multiple-value-setq (return-status result)
68 (ext:external-process-wait process-obj t)))
69 ;; ... unless we're running on cygwin which has problems with
70 ;; forking so we have to use si:system
71 #+cygwin
72 (setf result (si:system (format nil "~A~{ ~A~}" program args)))))
73 (cond ((null result)
74 (cerror "Continues anyway."
75 "Unable to execute:~%(EXT:RUN-PROGRAM ~S ~S)"
76 program args result))
77 ((not (zerop result))
78 (cerror "Continues anyway."
79 "Error code ~D when executing~%(EXT:RUN-PROGRAM ~S ~S):~%~{~A~^~%~}"
80 result program args output)))
81 result)
82
83 (defun split-program-options (string)
84 (labels ((maybe-push (options current)
85 (if current
86 (cons current options)
87 options))
88 (new-string ()
89 (make-array 32 :element-type 'base-char :adjustable t
90 :fill-pointer 0))
91 (push-char (c s)
92 (unless s (setf s (new-string)))
93 (vector-push-extend c s)
94 s))
95 (loop with output = '()
96 with option = nil
97 with status = nil
98 for i from 0 below (length string)
99 for c = (char string i)
100 for now = (first status)
101 do (cond ((eq now #\')
102 (if (eq c #\')
103 (setf status (rest status))
104 (setf option (push-char c option))))
105 ((eq now #\\)
106 (setf option (push-char c option)
107 status (rest status)))
108 ((eq now #\")
109 (if (eq c #\")
110 (setf status (rest status))
111 (setf option (push-char c option))))
112 ((member c '(#\\ #\' #\"))
113 (push c status))
114 ((member c '(#\Space #\Tab #\Return #\Newline))
115 (setf output (maybe-push output option)
116 option nil))
117 (t
118 (setf option (push-char c option))))
119 finally (cond (status
120 (error "In split-program-options, unterminated option list:~%~S"
121 string))
122 (t
123 (return (nreverse (maybe-push output option))))))))