Make INFO's compiler-macro more forgiving.
[sbcl.git] / contrib / asdf / uiop.lisp
blob2ed53bb7b2674539cb589b1cbfa81d646133440f
1 ;;; This is UIOP 3.1.3
2 ;;;; ---------------------------------------------------------------------------
3 ;;;; Handle ASDF package upgrade, including implementation-dependent magic.
4 ;;
5 ;; See https://bugs.launchpad.net/asdf/+bug/485687
6 ;;
8 (defpackage :uiop/package
9 ;; CAUTION: we must handle the first few packages specially for hot-upgrade.
10 ;; This package definition MUST NOT change unless its name too changes;
11 ;; if/when it changes, don't forget to add new functions missing from below.
12 ;; Until then, uiop/package is frozen to forever
13 ;; import and export the same exact symbols as for ASDF 2.27.
14 ;; Any other symbol must be import-from'ed and re-export'ed in a different package.
15 (:use :common-lisp)
16 (:export
17 #:find-package* #:find-symbol* #:symbol-call
18 #:intern* #:export* #:import* #:shadowing-import* #:shadow* #:make-symbol* #:unintern*
19 #:symbol-shadowing-p #:home-package-p
20 #:symbol-package-name #:standard-common-lisp-symbol-p
21 #:reify-package #:unreify-package #:reify-symbol #:unreify-symbol
22 #:nuke-symbol-in-package #:nuke-symbol #:rehome-symbol
23 #:ensure-package-unused #:delete-package*
24 #:package-names #:packages-from-names #:fresh-package-name #:rename-package-away
25 #:package-definition-form #:parse-define-package-form
26 #:ensure-package #:define-package))
28 (in-package :uiop/package)
30 ;;;; General purpose package utilities
32 (eval-when (:load-toplevel :compile-toplevel :execute)
33 (defun find-package* (package-designator &optional (error t))
34 (let ((package (find-package package-designator)))
35 (cond
36 (package package)
37 (error (error "No package named ~S" (string package-designator)))
38 (t nil))))
39 (defun find-symbol* (name package-designator &optional (error t))
40 "Find a symbol in a package of given string'ified NAME;
41 unless CL:FIND-SYMBOL, work well with 'modern' case sensitive syntax
42 by letting you supply a symbol or keyword for the name;
43 also works well when the package is not present.
44 If optional ERROR argument is NIL, return NIL instead of an error
45 when the symbol is not found."
46 (block nil
47 (let ((package (find-package* package-designator error)))
48 (when package ;; package error handled by find-package* already
49 (multiple-value-bind (symbol status) (find-symbol (string name) package)
50 (cond
51 (status (return (values symbol status)))
52 (error (error "There is no symbol ~S in package ~S" name (package-name package))))))
53 (values nil nil))))
54 (defun symbol-call (package name &rest args)
55 "Call a function associated with symbol of given name in given package,
56 with given ARGS. Useful when the call is read before the package is loaded,
57 or when loading the package is optional."
58 (apply (find-symbol* name package) args))
59 (defun intern* (name package-designator &optional (error t))
60 (intern (string name) (find-package* package-designator error)))
61 (defun export* (name package-designator)
62 (let* ((package (find-package* package-designator))
63 (symbol (intern* name package)))
64 (export (or symbol (list symbol)) package)))
65 (defun import* (symbol package-designator)
66 (import (or symbol (list symbol)) (find-package* package-designator)))
67 (defun shadowing-import* (symbol package-designator)
68 (shadowing-import (or symbol (list symbol)) (find-package* package-designator)))
69 (defun shadow* (name package-designator)
70 (shadow (string name) (find-package* package-designator)))
71 (defun make-symbol* (name)
72 (etypecase name
73 (string (make-symbol name))
74 (symbol (copy-symbol name))))
75 (defun unintern* (name package-designator &optional (error t))
76 (block nil
77 (let ((package (find-package* package-designator error)))
78 (when package
79 (multiple-value-bind (symbol status) (find-symbol* name package error)
80 (cond
81 (status (unintern symbol package)
82 (return (values symbol status)))
83 (error (error "symbol ~A not present in package ~A"
84 (string symbol) (package-name package))))))
85 (values nil nil))))
86 (defun symbol-shadowing-p (symbol package)
87 (and (member symbol (package-shadowing-symbols package)) t))
88 (defun home-package-p (symbol package)
89 (and package (let ((sp (symbol-package symbol)))
90 (and sp (let ((pp (find-package* package)))
91 (and pp (eq sp pp))))))))
94 (eval-when (:load-toplevel :compile-toplevel :execute)
95 (defun symbol-package-name (symbol)
96 (let ((package (symbol-package symbol)))
97 (and package (package-name package))))
98 (defun standard-common-lisp-symbol-p (symbol)
99 (multiple-value-bind (sym status) (find-symbol* symbol :common-lisp nil)
100 (and (eq sym symbol) (eq status :external))))
101 (defun reify-package (package &optional package-context)
102 (if (eq package package-context) t
103 (etypecase package
104 (null nil)
105 ((eql (find-package :cl)) :cl)
106 (package (package-name package)))))
107 (defun unreify-package (package &optional package-context)
108 (etypecase package
109 (null nil)
110 ((eql t) package-context)
111 ((or symbol string) (find-package package))))
112 (defun reify-symbol (symbol &optional package-context)
113 (etypecase symbol
114 ((or keyword (satisfies standard-common-lisp-symbol-p)) symbol)
115 (symbol (vector (symbol-name symbol)
116 (reify-package (symbol-package symbol) package-context)))))
117 (defun unreify-symbol (symbol &optional package-context)
118 (etypecase symbol
119 (symbol symbol)
120 ((simple-vector 2)
121 (let* ((symbol-name (svref symbol 0))
122 (package-foo (svref symbol 1))
123 (package (unreify-package package-foo package-context)))
124 (if package (intern* symbol-name package)
125 (make-symbol* symbol-name)))))))
127 (eval-when (:load-toplevel :compile-toplevel :execute)
128 (defvar *all-package-happiness* '())
129 (defvar *all-package-fishiness* (list t))
130 (defun record-fishy (info)
131 ;;(format t "~&FISHY: ~S~%" info)
132 (push info *all-package-fishiness*))
133 (defmacro when-package-fishiness (&body body)
134 `(when *all-package-fishiness* ,@body))
135 (defmacro note-package-fishiness (&rest info)
136 `(when-package-fishiness (record-fishy (list ,@info)))))
138 (eval-when (:load-toplevel :compile-toplevel :execute)
139 #+(or clisp clozure)
140 (defun get-setf-function-symbol (symbol)
141 #+clisp (let ((sym (get symbol 'system::setf-function)))
142 (if sym (values sym :setf-function)
143 (let ((sym (get symbol 'system::setf-expander)))
144 (if sym (values sym :setf-expander)
145 (values nil nil)))))
146 #+clozure (gethash symbol ccl::%setf-function-names%))
147 #+(or clisp clozure)
148 (defun set-setf-function-symbol (new-setf-symbol symbol &optional kind)
149 #+clisp (assert (member kind '(:setf-function :setf-expander)))
150 #+clozure (assert (eq kind t))
151 #+clisp
152 (cond
153 ((null new-setf-symbol)
154 (remprop symbol 'system::setf-function)
155 (remprop symbol 'system::setf-expander))
156 ((eq kind :setf-function)
157 (setf (get symbol 'system::setf-function) new-setf-symbol))
158 ((eq kind :setf-expander)
159 (setf (get symbol 'system::setf-expander) new-setf-symbol))
160 (t (error "invalid kind of setf-function ~S for ~S to be set to ~S"
161 kind symbol new-setf-symbol)))
162 #+clozure
163 (progn
164 (gethash symbol ccl::%setf-function-names%) new-setf-symbol
165 (gethash new-setf-symbol ccl::%setf-function-name-inverses%) symbol))
166 #+(or clisp clozure)
167 (defun create-setf-function-symbol (symbol)
168 #+clisp (system::setf-symbol symbol)
169 #+clozure (ccl::construct-setf-function-name symbol))
170 (defun set-dummy-symbol (symbol reason other-symbol)
171 (setf (get symbol 'dummy-symbol) (cons reason other-symbol)))
172 (defun make-dummy-symbol (symbol)
173 (let ((dummy (copy-symbol symbol)))
174 (set-dummy-symbol dummy 'replacing symbol)
175 (set-dummy-symbol symbol 'replaced-by dummy)
176 dummy))
177 (defun dummy-symbol (symbol)
178 (get symbol 'dummy-symbol))
179 (defun get-dummy-symbol (symbol)
180 (let ((existing (dummy-symbol symbol)))
181 (if existing (values (cdr existing) (car existing))
182 (make-dummy-symbol symbol))))
183 (defun nuke-symbol-in-package (symbol package-designator)
184 (let ((package (find-package* package-designator))
185 (name (symbol-name symbol)))
186 (multiple-value-bind (sym stat) (find-symbol name package)
187 (when (and (member stat '(:internal :external)) (eq symbol sym))
188 (if (symbol-shadowing-p symbol package)
189 (shadowing-import* (get-dummy-symbol symbol) package)
190 (unintern* symbol package))))))
191 (defun nuke-symbol (symbol &optional (packages (list-all-packages)))
192 #+(or clisp clozure)
193 (multiple-value-bind (setf-symbol kind)
194 (get-setf-function-symbol symbol)
195 (when kind (nuke-symbol setf-symbol)))
196 (loop :for p :in packages :do (nuke-symbol-in-package symbol p)))
197 (defun rehome-symbol (symbol package-designator)
198 "Changes the home package of a symbol, also leaving it present in its old home if any"
199 (let* ((name (symbol-name symbol))
200 (package (find-package* package-designator))
201 (old-package (symbol-package symbol))
202 (old-status (and old-package (nth-value 1 (find-symbol name old-package))))
203 (shadowing (and old-package (symbol-shadowing-p symbol old-package) (make-symbol name))))
204 (multiple-value-bind (overwritten-symbol overwritten-symbol-status) (find-symbol name package)
205 (unless (eq package old-package)
206 (let ((overwritten-symbol-shadowing-p
207 (and overwritten-symbol-status
208 (symbol-shadowing-p overwritten-symbol package))))
209 (note-package-fishiness
210 :rehome-symbol name
211 (when old-package (package-name old-package)) old-status (and shadowing t)
212 (package-name package) overwritten-symbol-status overwritten-symbol-shadowing-p)
213 (when old-package
214 (if shadowing
215 (shadowing-import* shadowing old-package))
216 (unintern* symbol old-package))
217 (cond
218 (overwritten-symbol-shadowing-p
219 (shadowing-import* symbol package))
221 (when overwritten-symbol-status
222 (unintern* overwritten-symbol package))
223 (import* symbol package)))
224 (if shadowing
225 (shadowing-import* symbol old-package)
226 (import* symbol old-package))
227 #+(or clisp clozure)
228 (multiple-value-bind (setf-symbol kind)
229 (get-setf-function-symbol symbol)
230 (when kind
231 (let* ((setf-function (fdefinition setf-symbol))
232 (new-setf-symbol (create-setf-function-symbol symbol)))
233 (note-package-fishiness
234 :setf-function
235 name (package-name package)
236 (symbol-name setf-symbol) (symbol-package-name setf-symbol)
237 (symbol-name new-setf-symbol) (symbol-package-name new-setf-symbol))
238 (when (symbol-package setf-symbol)
239 (unintern* setf-symbol (symbol-package setf-symbol)))
240 (setf (fdefinition new-setf-symbol) setf-function)
241 (set-setf-function-symbol new-setf-symbol symbol kind))))
242 #+(or clisp clozure)
243 (multiple-value-bind (overwritten-setf foundp)
244 (get-setf-function-symbol overwritten-symbol)
245 (when foundp
246 (unintern overwritten-setf)))
247 (when (eq old-status :external)
248 (export* symbol old-package))
249 (when (eq overwritten-symbol-status :external)
250 (export* symbol package))))
251 (values overwritten-symbol overwritten-symbol-status))))
252 (defun ensure-package-unused (package)
253 (loop :for p :in (package-used-by-list package) :do
254 (unuse-package package p)))
255 (defun delete-package* (package &key nuke)
256 (let ((p (find-package package)))
257 (when p
258 (when nuke (do-symbols (s p) (when (home-package-p s p) (nuke-symbol s))))
259 (ensure-package-unused p)
260 (delete-package package))))
261 (defun package-names (package)
262 (cons (package-name package) (package-nicknames package)))
263 (defun packages-from-names (names)
264 (remove-duplicates (remove nil (mapcar #'find-package names)) :from-end t))
265 (defun fresh-package-name (&key (prefix :%TO-BE-DELETED)
266 separator
267 (index (random most-positive-fixnum)))
268 (loop :for i :from index
269 :for n = (format nil "~A~@[~A~D~]" prefix (and (plusp i) (or separator "")) i)
270 :thereis (and (not (find-package n)) n)))
271 (defun rename-package-away (p &rest keys &key prefix &allow-other-keys)
272 (let ((new-name
273 (apply 'fresh-package-name
274 :prefix (or prefix (format nil "__~A__" (package-name p))) keys)))
275 (record-fishy (list :rename-away (package-names p) new-name))
276 (rename-package p new-name))))
279 ;;; Communicable representation of symbol and package information
281 (eval-when (:load-toplevel :compile-toplevel :execute)
282 (defun package-definition-form (package-designator
283 &key (nicknamesp t) (usep t)
284 (shadowp t) (shadowing-import-p t)
285 (exportp t) (importp t) internp (error t))
286 (let* ((package (or (find-package* package-designator error)
287 (return-from package-definition-form nil)))
288 (name (package-name package))
289 (nicknames (package-nicknames package))
290 (use (mapcar #'package-name (package-use-list package)))
291 (shadow ())
292 (shadowing-import (make-hash-table :test 'equal))
293 (import (make-hash-table :test 'equal))
294 (export ())
295 (intern ()))
296 (when package
297 (loop :for sym :being :the :symbols :in package
298 :for status = (nth-value 1 (find-symbol* sym package)) :do
299 (ecase status
300 ((nil :inherited))
301 ((:internal :external)
302 (let* ((name (symbol-name sym))
303 (external (eq status :external))
304 (home (symbol-package sym))
305 (home-name (package-name home))
306 (imported (not (eq home package)))
307 (shadowing (symbol-shadowing-p sym package)))
308 (cond
309 ((and shadowing imported)
310 (push name (gethash home-name shadowing-import)))
311 (shadowing
312 (push name shadow))
313 (imported
314 (push name (gethash home-name import))))
315 (cond
316 (external
317 (push name export))
318 (imported)
319 (t (push name intern)))))))
320 (labels ((sort-names (names)
321 (sort (copy-list names) #'string<))
322 (table-keys (table)
323 (loop :for k :being :the :hash-keys :of table :collect k))
324 (when-relevant (key value)
325 (when value (list (cons key value))))
326 (import-options (key table)
327 (loop :for i :in (sort-names (table-keys table))
328 :collect `(,key ,i ,@(sort-names (gethash i table))))))
329 `(defpackage ,name
330 ,@(when-relevant :nicknames (and nicknamesp (sort-names nicknames)))
331 (:use ,@(and usep (sort-names use)))
332 ,@(when-relevant :shadow (and shadowp (sort-names shadow)))
333 ,@(import-options :shadowing-import-from (and shadowing-import-p shadowing-import))
334 ,@(import-options :import-from (and importp import))
335 ,@(when-relevant :export (and exportp (sort-names export)))
336 ,@(when-relevant :intern (and internp (sort-names intern)))))))))
339 ;;; ensure-package, define-package
340 (eval-when (:load-toplevel :compile-toplevel :execute)
341 (defun ensure-shadowing-import (name to-package from-package shadowed imported)
342 (check-type name string)
343 (check-type to-package package)
344 (check-type from-package package)
345 (check-type shadowed hash-table)
346 (check-type imported hash-table)
347 (let ((import-me (find-symbol* name from-package)))
348 (multiple-value-bind (existing status) (find-symbol name to-package)
349 (cond
350 ((gethash name shadowed)
351 (unless (eq import-me existing)
352 (error "Conflicting shadowings for ~A" name)))
354 (setf (gethash name shadowed) t)
355 (setf (gethash name imported) t)
356 (unless (or (null status)
357 (and (member status '(:internal :external))
358 (eq existing import-me)
359 (symbol-shadowing-p existing to-package)))
360 (note-package-fishiness
361 :shadowing-import name
362 (package-name from-package)
363 (or (home-package-p import-me from-package) (symbol-package-name import-me))
364 (package-name to-package) status
365 (and status (or (home-package-p existing to-package) (symbol-package-name existing)))))
366 (shadowing-import* import-me to-package))))))
367 (defun ensure-imported (import-me into-package &optional from-package)
368 (check-type import-me symbol)
369 (check-type into-package package)
370 (check-type from-package (or null package))
371 (let ((name (symbol-name import-me)))
372 (multiple-value-bind (existing status) (find-symbol name into-package)
373 (cond
374 ((not status)
375 (import* import-me into-package))
376 ((eq import-me existing))
378 (let ((shadowing-p (symbol-shadowing-p existing into-package)))
379 (note-package-fishiness
380 :ensure-imported name
381 (and from-package (package-name from-package))
382 (or (home-package-p import-me from-package) (symbol-package-name import-me))
383 (package-name into-package)
384 status
385 (and status (or (home-package-p existing into-package) (symbol-package-name existing)))
386 shadowing-p)
387 (cond
388 ((or shadowing-p (eq status :inherited))
389 (shadowing-import* import-me into-package))
391 (unintern* existing into-package)
392 (import* import-me into-package))))))))
393 (values))
394 (defun ensure-import (name to-package from-package shadowed imported)
395 (check-type name string)
396 (check-type to-package package)
397 (check-type from-package package)
398 (check-type shadowed hash-table)
399 (check-type imported hash-table)
400 (multiple-value-bind (import-me import-status) (find-symbol name from-package)
401 (when (null import-status)
402 (note-package-fishiness
403 :import-uninterned name (package-name from-package) (package-name to-package))
404 (setf import-me (intern* name from-package)))
405 (multiple-value-bind (existing status) (find-symbol name to-package)
406 (cond
407 ((and imported (gethash name imported))
408 (unless (and status (eq import-me existing))
409 (error "Can't import ~S from both ~S and ~S"
410 name (package-name (symbol-package existing)) (package-name from-package))))
411 ((gethash name shadowed)
412 (error "Can't both shadow ~S and import it from ~S" name (package-name from-package)))
414 (setf (gethash name imported) t))))
415 (ensure-imported import-me to-package from-package)))
416 (defun ensure-inherited (name symbol to-package from-package mixp shadowed imported inherited)
417 (check-type name string)
418 (check-type symbol symbol)
419 (check-type to-package package)
420 (check-type from-package package)
421 (check-type mixp (member nil t)) ; no cl:boolean on Genera
422 (check-type shadowed hash-table)
423 (check-type imported hash-table)
424 (check-type inherited hash-table)
425 (multiple-value-bind (existing status) (find-symbol name to-package)
426 (let* ((sp (symbol-package symbol))
427 (in (gethash name inherited))
428 (xp (and status (symbol-package existing))))
429 (when (null sp)
430 (note-package-fishiness
431 :import-uninterned name
432 (package-name from-package) (package-name to-package) mixp)
433 (import* symbol from-package)
434 (setf sp (package-name from-package)))
435 (cond
436 ((gethash name shadowed))
438 (unless (equal sp (first in))
439 (if mixp
440 (ensure-shadowing-import name to-package (second in) shadowed imported)
441 (error "Can't inherit ~S from ~S, it is inherited from ~S"
442 name (package-name sp) (package-name (first in))))))
443 ((gethash name imported)
444 (unless (eq symbol existing)
445 (error "Can't inherit ~S from ~S, it is imported from ~S"
446 name (package-name sp) (package-name xp))))
448 (setf (gethash name inherited) (list sp from-package))
449 (when (and status (not (eq sp xp)))
450 (let ((shadowing (symbol-shadowing-p existing to-package)))
451 (note-package-fishiness
452 :inherited name
453 (package-name from-package)
454 (or (home-package-p symbol from-package) (symbol-package-name symbol))
455 (package-name to-package)
456 (or (home-package-p existing to-package) (symbol-package-name existing)))
457 (if shadowing (ensure-shadowing-import name to-package from-package shadowed imported)
458 (unintern* existing to-package)))))))))
459 (defun ensure-mix (name symbol to-package from-package shadowed imported inherited)
460 (check-type name string)
461 (check-type symbol symbol)
462 (check-type to-package package)
463 (check-type from-package package)
464 (check-type shadowed hash-table)
465 (check-type imported hash-table)
466 (check-type inherited hash-table)
467 (unless (gethash name shadowed)
468 (multiple-value-bind (existing status) (find-symbol name to-package)
469 (let* ((sp (symbol-package symbol))
470 (im (gethash name imported))
471 (in (gethash name inherited)))
472 (cond
473 ((or (null status)
474 (and status (eq symbol existing))
475 (and in (eq sp (first in))))
476 (ensure-inherited name symbol to-package from-package t shadowed imported inherited))
478 (remhash name inherited)
479 (ensure-shadowing-import name to-package (second in) shadowed imported))
481 (error "Symbol ~S import from ~S~:[~; actually ~:[uninterned~;~:*from ~S~]~] conflicts with existing symbol in ~S~:[~; actually ~:[uninterned~;from ~:*~S~]~]"
482 name (package-name from-package)
483 (home-package-p symbol from-package) (symbol-package-name symbol)
484 (package-name to-package)
485 (home-package-p existing to-package) (symbol-package-name existing)))
487 (ensure-inherited name symbol to-package from-package t shadowed imported inherited)))))))
489 (defun recycle-symbol (name recycle exported)
490 ;; Takes a symbol NAME (a string), a list of package designators for RECYCLE
491 ;; packages, and a hash-table of names (strings) of symbols scheduled to be
492 ;; EXPORTED from the package being defined. It returns two values, the
493 ;; symbol found (if any, or else NIL), and a boolean flag indicating whether
494 ;; a symbol was found. The caller (DEFINE-PACKAGE) will then do the
495 ;; re-homing of the symbol, etc.
496 (check-type name string)
497 (check-type recycle list)
498 (check-type exported hash-table)
499 (when (gethash name exported) ;; don't bother recycling private symbols
500 (let (recycled foundp)
501 (dolist (r recycle (values recycled foundp))
502 (multiple-value-bind (symbol status) (find-symbol name r)
503 (when (and status (home-package-p symbol r))
504 (cond
505 (foundp
506 ;; (nuke-symbol symbol)) -- even simple variable names like O or C will do that.
507 (note-package-fishiness :recycled-duplicate name (package-name foundp) (package-name r)))
509 (setf recycled symbol foundp r)))))))))
510 (defun symbol-recycled-p (sym recycle)
511 (check-type sym symbol)
512 (check-type recycle list)
513 (and (member (symbol-package sym) recycle) t))
514 (defun ensure-symbol (name package intern recycle shadowed imported inherited exported)
515 (check-type name string)
516 (check-type package package)
517 (check-type intern (member nil t)) ; no cl:boolean on Genera
518 (check-type shadowed hash-table)
519 (check-type imported hash-table)
520 (check-type inherited hash-table)
521 (unless (or (gethash name shadowed)
522 (gethash name imported)
523 (gethash name inherited))
524 (multiple-value-bind (existing status)
525 (find-symbol name package)
526 (multiple-value-bind (recycled previous) (recycle-symbol name recycle exported)
527 (cond
528 ((and status (eq existing recycled) (eq previous package)))
529 (previous
530 (rehome-symbol recycled package))
531 ((and status (eq package (symbol-package existing))))
533 (when status
534 (note-package-fishiness
535 :ensure-symbol name
536 (reify-package (symbol-package existing) package)
537 status intern)
538 (unintern existing))
539 (when intern
540 (intern* name package))))))))
541 (declaim (ftype (function (t t t &optional t) t) ensure-exported))
542 (defun ensure-exported-to-user (name symbol to-package &optional recycle)
543 (check-type name string)
544 (check-type symbol symbol)
545 (check-type to-package package)
546 (check-type recycle list)
547 (assert (equal name (symbol-name symbol)))
548 (multiple-value-bind (existing status) (find-symbol name to-package)
549 (unless (and status (eq symbol existing))
550 (let ((accessible
551 (or (null status)
552 (let ((shadowing (symbol-shadowing-p existing to-package))
553 (recycled (symbol-recycled-p existing recycle)))
554 (unless (and shadowing (not recycled))
555 (note-package-fishiness
556 :ensure-export name (symbol-package-name symbol)
557 (package-name to-package)
558 (or (home-package-p existing to-package) (symbol-package-name existing))
559 status shadowing)
560 (if (or (eq status :inherited) shadowing)
561 (shadowing-import* symbol to-package)
562 (unintern existing to-package))
563 t)))))
564 (when (and accessible (eq status :external))
565 (ensure-exported name symbol to-package recycle))))))
566 (defun ensure-exported (name symbol from-package &optional recycle)
567 (dolist (to-package (package-used-by-list from-package))
568 (ensure-exported-to-user name symbol to-package recycle))
569 (unless (eq from-package (symbol-package symbol))
570 (ensure-imported symbol from-package))
571 (export* name from-package))
572 (defun ensure-export (name from-package &optional recycle)
573 (multiple-value-bind (symbol status) (find-symbol* name from-package)
574 (unless (eq status :external)
575 (ensure-exported name symbol from-package recycle))))
576 (defun ensure-package (name &key
577 nicknames documentation use
578 shadow shadowing-import-from
579 import-from export intern
580 recycle mix reexport
581 unintern)
582 #+genera (declare (ignore documentation))
583 (let* ((package-name (string name))
584 (nicknames (mapcar #'string nicknames))
585 (names (cons package-name nicknames))
586 (previous (packages-from-names names))
587 (discarded (cdr previous))
588 (to-delete ())
589 (package (or (first previous) (make-package package-name :nicknames nicknames)))
590 (recycle (packages-from-names recycle))
591 (use (mapcar 'find-package* use))
592 (mix (mapcar 'find-package* mix))
593 (reexport (mapcar 'find-package* reexport))
594 (shadow (mapcar 'string shadow))
595 (export (mapcar 'string export))
596 (intern (mapcar 'string intern))
597 (unintern (mapcar 'string unintern))
598 (shadowed (make-hash-table :test 'equal)) ; string to bool
599 (imported (make-hash-table :test 'equal)) ; string to bool
600 (exported (make-hash-table :test 'equal)) ; string to bool
601 ;; string to list home package and use package:
602 (inherited (make-hash-table :test 'equal)))
603 (when-package-fishiness (record-fishy package-name))
604 #-genera
605 (when documentation (setf (documentation package t) documentation))
606 (loop :for p :in (set-difference (package-use-list package) (append mix use))
607 :do (note-package-fishiness :over-use name (package-names p))
608 (unuse-package p package))
609 (loop :for p :in discarded
610 :for n = (remove-if #'(lambda (x) (member x names :test 'equal))
611 (package-names p))
612 :do (note-package-fishiness :nickname name (package-names p))
613 (cond (n (rename-package p (first n) (rest n)))
614 (t (rename-package-away p)
615 (push p to-delete))))
616 (rename-package package package-name nicknames)
617 (dolist (name unintern)
618 (multiple-value-bind (existing status) (find-symbol name package)
619 (when status
620 (unless (eq status :inherited)
621 (note-package-fishiness
622 :unintern (package-name package) name (symbol-package-name existing) status)
623 (unintern* name package nil)))))
624 (dolist (name export)
625 (setf (gethash name exported) t))
626 (dolist (p reexport)
627 (do-external-symbols (sym p)
628 (setf (gethash (string sym) exported) t)))
629 (do-external-symbols (sym package)
630 (let ((name (symbol-name sym)))
631 (unless (gethash name exported)
632 (note-package-fishiness
633 :over-export (package-name package) name
634 (or (home-package-p sym package) (symbol-package-name sym)))
635 (unexport sym package))))
636 (dolist (name shadow)
637 (setf (gethash name shadowed) t)
638 (multiple-value-bind (existing status) (find-symbol name package)
639 (multiple-value-bind (recycled previous) (recycle-symbol name recycle exported)
640 (let ((shadowing (and status (symbol-shadowing-p existing package))))
641 (cond
642 ((eq previous package))
643 (previous
644 (rehome-symbol recycled package))
645 ((or (member status '(nil :inherited))
646 (home-package-p existing package)))
648 (let ((dummy (make-symbol name)))
649 (note-package-fishiness
650 :shadow-imported (package-name package) name
651 (symbol-package-name existing) status shadowing)
652 (shadowing-import* dummy package)
653 (import* dummy package)))))))
654 (shadow* name package))
655 (loop :for (p . syms) :in shadowing-import-from
656 :for pp = (find-package* p) :do
657 (dolist (sym syms) (ensure-shadowing-import (string sym) package pp shadowed imported)))
658 (loop :for p :in mix
659 :for pp = (find-package* p) :do
660 (do-external-symbols (sym pp) (ensure-mix (symbol-name sym) sym package pp shadowed imported inherited)))
661 (loop :for (p . syms) :in import-from
662 :for pp = (find-package p) :do
663 (dolist (sym syms) (ensure-import (symbol-name sym) package pp shadowed imported)))
664 (dolist (p (append use mix))
665 (do-external-symbols (sym p) (ensure-inherited (string sym) sym package p nil shadowed imported inherited))
666 (use-package p package))
667 (loop :for name :being :the :hash-keys :of exported :do
668 (ensure-symbol name package t recycle shadowed imported inherited exported)
669 (ensure-export name package recycle))
670 (dolist (name intern)
671 (ensure-symbol name package t recycle shadowed imported inherited exported))
672 (do-symbols (sym package)
673 (ensure-symbol (symbol-name sym) package nil recycle shadowed imported inherited exported))
674 (map () 'delete-package* to-delete)
675 package)))
677 (eval-when (:load-toplevel :compile-toplevel :execute)
678 (defun parse-define-package-form (package clauses)
679 (loop
680 :with use-p = nil :with recycle-p = nil
681 :with documentation = nil
682 :for (kw . args) :in clauses
683 :when (eq kw :nicknames) :append args :into nicknames :else
684 :when (eq kw :documentation)
685 :do (cond
686 (documentation (error "define-package: can't define documentation twice"))
687 ((or (atom args) (cdr args)) (error "define-package: bad documentation"))
688 (t (setf documentation (car args)))) :else
689 :when (eq kw :use) :append args :into use :and :do (setf use-p t) :else
690 :when (eq kw :shadow) :append args :into shadow :else
691 :when (eq kw :shadowing-import-from) :collect args :into shadowing-import-from :else
692 :when (eq kw :import-from) :collect args :into import-from :else
693 :when (eq kw :export) :append args :into export :else
694 :when (eq kw :intern) :append args :into intern :else
695 :when (eq kw :recycle) :append args :into recycle :and :do (setf recycle-p t) :else
696 :when (eq kw :mix) :append args :into mix :else
697 :when (eq kw :reexport) :append args :into reexport :else
698 :when (eq kw :use-reexport) :append args :into use :and :append args :into reexport
699 :and :do (setf use-p t) :else
700 :when (eq kw :mix-reexport) :append args :into mix :and :append args :into reexport
701 :and :do (setf use-p t) :else
702 :when (eq kw :unintern) :append args :into unintern :else
703 :do (error "unrecognized define-package keyword ~S" kw)
704 :finally (return `(,package
705 :nicknames ,nicknames :documentation ,documentation
706 :use ,(if use-p use '(:common-lisp))
707 :shadow ,shadow :shadowing-import-from ,shadowing-import-from
708 :import-from ,import-from :export ,export :intern ,intern
709 :recycle ,(if recycle-p recycle (cons package nicknames))
710 :mix ,mix :reexport ,reexport :unintern ,unintern)))))
712 (defmacro define-package (package &rest clauses)
713 "DEFINE-PACKAGE takes a PACKAGE and a number of CLAUSES, of the form
714 \(KEYWORD . ARGS\).
715 DEFINE-PACKAGE supports the following keywords:
716 USE, SHADOW, SHADOWING-IMPORT-FROM, IMPORT-FROM, EXPORT, INTERN -- as per CL:DEFPACKAGE.
717 RECYCLE -- Recycle the package's exported symbols from the specified packages,
718 in order. For every symbol scheduled to be exported by the DEFINE-PACKAGE,
719 either through an :EXPORT option or a :REEXPORT option, if the symbol exists in
720 one of the :RECYCLE packages, the first such symbol is re-homed to the package
721 being defined.
722 For the sake of idempotence, it is important that the package being defined
723 should appear in first position if it already exists, and even if it doesn't,
724 ahead of any package that is not going to be deleted afterwards and never
725 created again. In short, except for special cases, always make it the first
726 package on the list if the list is not empty.
727 MIX -- Takes a list of package designators. MIX behaves like
728 \(:USE PKG1 PKG2 ... PKGn\) but additionally uses :SHADOWING-IMPORT-FROM to
729 resolve conflicts in favor of the first found symbol. It may still yield
730 an error if there is a conflict with an explicitly :IMPORT-FROM symbol.
731 REEXPORT -- Takes a list of package designators. For each package, p, in the list,
732 export symbols with the same name as those exported from p. Note that in the case
733 of shadowing, etc. the symbols with the same name may not be the same symbols.
734 UNINTERN -- Remove symbols here from PACKAGE."
735 (let ((ensure-form
736 `(apply 'ensure-package ',(parse-define-package-form package clauses))))
737 `(progn
738 #+(or ecl gcl mkcl) (defpackage ,package (:use))
739 (eval-when (:compile-toplevel :load-toplevel :execute)
740 ,ensure-form))))
742 ;;;; Final tricks to keep various implementations happy.
743 ;; We want most such tricks in common-lisp.lisp,
744 ;; but these need to be done before the define-package form there,
745 ;; that we nevertheless want to be the very first form.
746 (eval-when (:load-toplevel :compile-toplevel :execute)
747 #+allegro ;; We need to disable autoloading BEFORE any mention of package ASDF.
748 (setf excl::*autoload-package-name-alist*
749 (remove "asdf" excl::*autoload-package-name-alist*
750 :test 'equalp :key 'car)))
752 ;; Compatibility with whoever calls asdf/package
753 (define-package :asdf/package (:use :cl :uiop/package) (:reexport :uiop/package))
754 ;;;; -------------------------------------------------------------------------
755 ;;;; Handle compatibility with multiple implementations.
756 ;;; This file is for papering over the deficiencies and peculiarities
757 ;;; of various Common Lisp implementations.
758 ;;; For implementation-specific access to the system, see os.lisp instead.
759 ;;; A few functions are defined here, but actually exported from utility;
760 ;;; from this package only common-lisp symbols are exported.
762 (uiop/package:define-package :uiop/common-lisp
763 (:nicknames :uoip/cl :asdf/common-lisp :asdf/cl)
764 (:use :uiop/package)
765 (:use-reexport #-genera :common-lisp #+genera :future-common-lisp)
766 (:recycle :uiop/common-lisp :uoip/cl :asdf/common-lisp :asdf/cl :asdf)
767 #+allegro (:intern #:*acl-warn-save*)
768 #+cormanlisp (:shadow #:user-homedir-pathname)
769 #+cormanlisp
770 (:export
771 #:logical-pathname #:translate-logical-pathname
772 #:make-broadcast-stream #:file-namestring)
773 #+genera (:shadowing-import-from :scl #:boolean)
774 #+genera (:export #:boolean #:ensure-directories-exist #:read-sequence #:write-sequence)
775 #+mcl (:shadow #:user-homedir-pathname))
776 (in-package :uiop/common-lisp)
778 #-(or abcl allegro clisp clozure cmu cormanlisp ecl gcl genera lispworks mcl mkcl sbcl scl xcl)
779 (error "ASDF is not supported on your implementation. Please help us port it.")
781 ;; (declaim (optimize (speed 1) (debug 3) (safety 3))) ; DON'T: trust implementation defaults.
784 ;;;; Early meta-level tweaks
786 #+(or abcl allegro clisp cmu ecl mkcl clozure lispworks mkcl sbcl scl)
787 (eval-when (:load-toplevel :compile-toplevel :execute)
788 ;; Check for unicode at runtime, so that a hypothetical FASL compiled with unicode
789 ;; but loaded in a non-unicode setting (e.g. on Allegro) won't tell a lie.
790 (when (and #+allegro (member :ics *features*)
791 #+(or clisp cmu ecl mkcl) (member :unicode *features*)
792 #+sbcl (member :sb-unicode *features*))
793 (pushnew :asdf-unicode *features*)))
795 #+allegro
796 (eval-when (:load-toplevel :compile-toplevel :execute)
797 (defparameter *acl-warn-save*
798 (when (boundp 'excl:*warn-on-nested-reader-conditionals*)
799 excl:*warn-on-nested-reader-conditionals*))
800 (when (boundp 'excl:*warn-on-nested-reader-conditionals*)
801 (setf excl:*warn-on-nested-reader-conditionals* nil))
802 (setf *print-readably* nil))
804 #+clozure (in-package :ccl)
805 #+(and clozure windows-target) ;; See http://trac.clozure.com/ccl/ticket/1117
806 (eval-when (:load-toplevel :compile-toplevel :execute)
807 (unless (fboundp 'external-process-wait)
808 (in-development-mode
809 (defun external-process-wait (proc)
810 (when (and (external-process-pid proc) (eq (external-process-%status proc) :running))
811 (with-interrupts-enabled
812 (wait-on-semaphore (external-process-completed proc))))
813 (values (external-process-%exit-code proc)
814 (external-process-%status proc))))))
815 #+clozure (in-package :uiop/common-lisp)
818 #+cormanlisp
819 (eval-when (:load-toplevel :compile-toplevel :execute)
820 (deftype logical-pathname () nil)
821 (defun make-broadcast-stream () *error-output*)
822 (defun translate-logical-pathname (x) x)
823 (defun user-homedir-pathname (&optional host)
824 (declare (ignore host))
825 (parse-namestring (format nil "~A\\" (cl:user-homedir-pathname))))
826 (defun file-namestring (p)
827 (setf p (pathname p))
828 (format nil "~@[~A~]~@[.~A~]" (pathname-name p) (pathname-type p))))
830 #+ecl
831 (eval-when (:load-toplevel :compile-toplevel :execute)
832 (setf *load-verbose* nil)
833 (defun use-ecl-byte-compiler-p () (and (member :ecl-bytecmp *features*) t))
834 (unless (use-ecl-byte-compiler-p) (require :cmp)))
836 #+gcl
837 (eval-when (:load-toplevel :compile-toplevel :execute)
838 (unless (member :ansi-cl *features*)
839 (error "ASDF only supports GCL in ANSI mode. Aborting.~%"))
840 (setf compiler::*compiler-default-type* (pathname "")
841 compiler::*lsp-ext* "")
842 #.(let ((code ;; Only support very recent GCL 2.7.0 from November 2013 or later.
843 (cond
844 #+gcl
845 ((or (< system::*gcl-major-version* 2)
846 (and (= system::*gcl-major-version* 2)
847 (< system::*gcl-minor-version* 7)))
848 '(error "GCL 2.7 or later required to use ASDF")))))
849 (eval code)
850 code))
852 #+genera
853 (eval-when (:load-toplevel :compile-toplevel :execute)
854 (unless (fboundp 'lambda)
855 (defmacro lambda (&whole form &rest bvl-decls-and-body)
856 (declare (ignore bvl-decls-and-body)(zwei::indentation 1 1))
857 `#',(cons 'lisp::lambda (cdr form))))
858 (unless (fboundp 'ensure-directories-exist)
859 (defun ensure-directories-exist (path)
860 (fs:create-directories-recursively (pathname path))))
861 (unless (fboundp 'read-sequence)
862 (defun read-sequence (sequence stream &key (start 0) end)
863 (scl:send stream :string-in nil sequence start end)))
864 (unless (fboundp 'write-sequence)
865 (defun write-sequence (sequence stream &key (start 0) end)
866 (scl:send stream :string-out sequence start end)
867 sequence)))
869 #.(or #+mcl ;; the #$ doesn't work on other lisps, even protected by #+mcl, so we use this trick
870 (read-from-string
871 "(eval-when (:load-toplevel :compile-toplevel :execute)
872 (ccl:define-entry-point (_getenv \"getenv\") ((name :string)) :string)
873 (ccl:define-entry-point (_system \"system\") ((name :string)) :int)
874 ;; Note: ASDF may expect user-homedir-pathname to provide
875 ;; the pathname of the current user's home directory, whereas
876 ;; MCL by default provides the directory from which MCL was started.
877 ;; See http://code.google.com/p/mcl/wiki/Portability
878 (defun user-homedir-pathname ()
879 (ccl::findfolder #$kuserdomain #$kCurrentUserFolderType))
880 (defun probe-posix (posix-namestring)
881 \"If a file exists for the posix namestring, return the pathname\"
882 (ccl::with-cstrs ((cpath posix-namestring))
883 (ccl::rlet ((is-dir :boolean)
884 (fsref :fsref))
885 (when (eq #$noerr (#_fspathmakeref cpath fsref is-dir))
886 (ccl::%path-from-fsref fsref is-dir))))))"))
888 #+mkcl
889 (eval-when (:load-toplevel :compile-toplevel :execute)
890 (require :cmp)
891 (setq clos::*redefine-class-in-place* t)) ;; Make sure we have strict ANSI class redefinition semantics
894 ;;;; Looping
895 (eval-when (:load-toplevel :compile-toplevel :execute)
896 (defmacro loop* (&rest rest)
897 #-genera `(loop ,@rest)
898 #+genera `(lisp:loop ,@rest))) ;; In genera, CL:LOOP can't destructure, so we use LOOP*. Sigh.
901 ;;;; compatfmt: avoid fancy format directives when unsupported
902 (eval-when (:load-toplevel :compile-toplevel :execute)
903 (defun frob-substrings (string substrings &optional frob)
904 "for each substring in SUBSTRINGS, find occurrences of it within STRING
905 that don't use parts of matched occurrences of previous strings, and
906 FROB them, that is to say, remove them if FROB is NIL,
907 replace by FROB if FROB is a STRING, or if FROB is a FUNCTION,
908 call FROB with the match and a function that emits a string in the output.
909 Return a string made of the parts not omitted or emitted by FROB."
910 (declare (optimize (speed 0) (safety #-gcl 3 #+gcl 0) (debug 3)))
911 (let ((length (length string)) (stream nil))
912 (labels ((emit-string (x &optional (start 0) (end (length x)))
913 (when (< start end)
914 (unless stream (setf stream (make-string-output-stream)))
915 (write-string x stream :start start :end end)))
916 (emit-substring (start end)
917 (when (and (zerop start) (= end length))
918 (return-from frob-substrings string))
919 (emit-string string start end))
920 (recurse (substrings start end)
921 (cond
922 ((>= start end))
923 ((null substrings) (emit-substring start end))
924 (t (let* ((sub-spec (first substrings))
925 (sub (if (consp sub-spec) (car sub-spec) sub-spec))
926 (fun (if (consp sub-spec) (cdr sub-spec) frob))
927 (found (search sub string :start2 start :end2 end))
928 (more (rest substrings)))
929 (cond
930 (found
931 (recurse more start found)
932 (etypecase fun
933 (null)
934 (string (emit-string fun))
935 (function (funcall fun sub #'emit-string)))
936 (recurse substrings (+ found (length sub)) end))
938 (recurse more start end))))))))
939 (recurse substrings 0 length))
940 (if stream (get-output-stream-string stream) "")))
942 (defmacro compatfmt (format)
943 #+(or gcl genera)
944 (frob-substrings format `("~3i~_" #+genera ,@'("~@<" "~@;" "~@:>" "~:>")))
945 #-(or gcl genera) format))
946 ;;;; -------------------------------------------------------------------------
947 ;;;; General Purpose Utilities for ASDF
949 (uiop/package:define-package :uiop/utility
950 (:nicknames :asdf/utility)
951 (:recycle :uiop/utility :asdf/utility :asdf)
952 (:use :uiop/common-lisp :uiop/package)
953 ;; import and reexport a few things defined in :uiop/common-lisp
954 (:import-from :uiop/common-lisp #:compatfmt #:loop* #:frob-substrings
955 #+ecl #:use-ecl-byte-compiler-p #+mcl #:probe-posix)
956 (:export #:compatfmt #:loop* #:frob-substrings #:compatfmt
957 #+ecl #:use-ecl-byte-compiler-p #+mcl #:probe-posix)
958 (:export
959 ;; magic helper to define debugging functions:
960 #:uiop-debug #:load-uiop-debug-utility #:*uiop-debug-utility*
961 #:with-upgradability ;; (un)defining functions in an upgrade-friendly way
962 #:undefine-function #:undefine-functions #:defun* #:defgeneric*
963 #:nest #:if-let ;; basic flow control
964 #:while-collecting #:appendf #:length=n-p #:ensure-list ;; lists
965 #:remove-plist-keys #:remove-plist-key ;; plists
966 #:emptyp ;; sequences
967 #:+non-base-chars-exist-p+ ;; characters
968 #:+max-character-type-index+ #:character-type-index #:+character-types+
969 #:base-string-p #:strings-common-element-type #:reduce/strcat #:strcat ;; strings
970 #:first-char #:last-char #:split-string #:stripln #:+cr+ #:+lf+ #:+crlf+
971 #:string-prefix-p #:string-enclosed-p #:string-suffix-p
972 #:coerce-class ;; CLOS
973 #:stamp< #:stamps< #:stamp*< #:stamp<= ;; stamps
974 #:earlier-stamp #:stamps-earliest #:earliest-stamp
975 #:later-stamp #:stamps-latest #:latest-stamp #:latest-stamp-f
976 #:list-to-hash-set #:ensure-gethash ;; hash-table
977 #:ensure-function #:access-at #:access-at-count ;; functions
978 #:call-function #:call-functions #:register-hook-function
979 #:match-condition-p #:match-any-condition-p ;; conditions
980 #:call-with-muffled-conditions #:with-muffled-conditions
981 #:lexicographic< #:lexicographic<=
982 #:parse-version #:unparse-version #:version< #:version<= #:version-compatible-p)) ;; version
983 (in-package :uiop/utility)
985 ;;;; Defining functions in a way compatible with hot-upgrade:
986 ;; DEFUN* and DEFGENERIC* use FMAKUNBOUND to delete any previous fdefinition,
987 ;; thus replacing the function without warning or error
988 ;; even if the signature and/or generic-ness of the function has changed.
989 ;; For a generic function, this invalidates any previous DEFMETHOD.
990 (eval-when (:load-toplevel :compile-toplevel :execute)
991 (defun undefine-function (function-spec)
992 (cond
993 ((symbolp function-spec)
994 ;; undefining the previous function is the portable way
995 ;; of overriding any incompatible previous gf,
996 ;; but CLISP needs extra help with getting rid of previous methods.
997 #+clisp
998 (let ((f (and (fboundp function-spec) (fdefinition function-spec))))
999 (when (typep f 'clos:standard-generic-function)
1000 (loop :for m :in (clos:generic-function-methods f)
1001 :do (remove-method f m))))
1002 (fmakunbound function-spec))
1003 ((and (consp function-spec) (eq (car function-spec) 'setf)
1004 (consp (cdr function-spec)) (null (cddr function-spec)))
1005 (fmakunbound function-spec))
1006 (t (error "bad function spec ~S" function-spec))))
1007 (defun undefine-functions (function-spec-list)
1008 (map () 'undefine-function function-spec-list))
1009 (macrolet
1010 ((defdef (def* def)
1011 `(defmacro ,def* (name formals &rest rest)
1012 (destructuring-bind (name &key (supersede t))
1013 (if (or (atom name) (eq (car name) 'setf))
1014 (list name :supersede nil)
1015 name)
1016 (declare (ignorable supersede))
1017 `(progn
1018 ;; We usually try to do it only for the functions that need it,
1019 ;; which happens in asdf/upgrade - however, for ECL, we need this hammer.
1020 ,@(when (or supersede #+ecl t)
1021 `((undefine-function ',name)))
1022 ,@(when (and #+ecl (symbolp name)) ; fails for setf functions on ecl
1023 `((declaim (notinline ,name))))
1024 (,',def ,name ,formals ,@rest))))))
1025 (defdef defgeneric* defgeneric)
1026 (defdef defun* defun))
1027 (defmacro with-upgradability ((&optional) &body body)
1028 "Evaluate BODY at compile- load- and run- times, with DEFUN and DEFGENERIC modified
1029 to also declare the functions NOTINLINE and to accept a wrapping the function name
1030 specification into a list with keyword argument SUPERSEDE (which defaults to T if the name
1031 is not wrapped, and NIL if it is wrapped). If SUPERSEDE is true, call UNDEFINE-FUNCTION
1032 to supersede any previous definition."
1033 `(eval-when (:compile-toplevel :load-toplevel :execute)
1034 ,@(loop :for form :in body :collect
1035 (if (consp form)
1036 (destructuring-bind (car . cdr) form
1037 (case car
1038 ((defun) `(defun* ,@cdr))
1039 ((defgeneric) `(defgeneric* ,@cdr))
1040 (otherwise form)))
1041 form)))))
1043 ;;; Magic debugging help. See contrib/debug.lisp
1044 (with-upgradability ()
1045 (defvar *uiop-debug-utility*
1046 '(or (ignore-errors
1047 (symbol-call :asdf :system-relative-pathname :uiop "contrib/debug.lisp"))
1048 (symbol-call :uiop/pathname :subpathname (user-homedir-pathname) "cl/asdf/uiop/contrib/debug.lisp"))
1049 "form that evaluates to the pathname to your favorite debugging utilities")
1051 (defmacro uiop-debug (&rest keys)
1052 `(eval-when (:compile-toplevel :load-toplevel :execute)
1053 (load-uiop-debug-utility ,@keys)))
1055 (defun load-uiop-debug-utility (&key package utility-file)
1056 (let* ((*package* (if package (find-package package) *package*))
1057 (keyword (read-from-string
1058 (format nil ":DBG-~:@(~A~)" (package-name *package*)))))
1059 (unless (member keyword *features*)
1060 (let* ((utility-file (or utility-file *uiop-debug-utility*))
1061 (file (ignore-errors (probe-file (eval utility-file)))))
1062 (if file (load file)
1063 (error "Failed to locate debug utility file: ~S" utility-file)))))))
1065 ;;; Flow control
1066 (with-upgradability ()
1067 (defmacro nest (&rest things)
1068 "Macro to do keep code nesting and indentation under control." ;; Thanks to mbaringer
1069 (reduce #'(lambda (outer inner) `(,@outer ,inner))
1070 things :from-end t))
1072 (defmacro if-let (bindings &body (then-form &optional else-form)) ;; from alexandria
1073 ;; bindings can be (var form) or ((var1 form1) ...)
1074 (let* ((binding-list (if (and (consp bindings) (symbolp (car bindings)))
1075 (list bindings)
1076 bindings))
1077 (variables (mapcar #'car binding-list)))
1078 `(let ,binding-list
1079 (if (and ,@variables)
1080 ,then-form
1081 ,else-form)))))
1083 ;;; List manipulation
1084 (with-upgradability ()
1085 (defmacro while-collecting ((&rest collectors) &body body)
1086 "COLLECTORS should be a list of names for collections. A collector
1087 defines a function that, when applied to an argument inside BODY, will
1088 add its argument to the corresponding collection. Returns multiple values,
1089 a list for each collection, in order.
1090 E.g.,
1091 \(while-collecting \(foo bar\)
1092 \(dolist \(x '\(\(a 1\) \(b 2\) \(c 3\)\)\)
1093 \(foo \(first x\)\)
1094 \(bar \(second x\)\)\)\)
1095 Returns two values: \(A B C\) and \(1 2 3\)."
1096 (let ((vars (mapcar #'(lambda (x) (gensym (symbol-name x))) collectors))
1097 (initial-values (mapcar (constantly nil) collectors)))
1098 `(let ,(mapcar #'list vars initial-values)
1099 (flet ,(mapcar #'(lambda (c v) `(,c (x) (push x ,v) (values))) collectors vars)
1100 ,@body
1101 (values ,@(mapcar #'(lambda (v) `(reverse ,v)) vars))))))
1103 (define-modify-macro appendf (&rest args)
1104 append "Append onto list") ;; only to be used on short lists.
1106 (defun length=n-p (x n) ;is it that (= (length x) n) ?
1107 (check-type n (integer 0 *))
1108 (loop
1109 :for l = x :then (cdr l)
1110 :for i :downfrom n :do
1111 (cond
1112 ((zerop i) (return (null l)))
1113 ((not (consp l)) (return nil)))))
1115 (defun ensure-list (x)
1116 (if (listp x) x (list x))))
1119 ;;; remove a key from a plist, i.e. for keyword argument cleanup
1120 (with-upgradability ()
1121 (defun remove-plist-key (key plist)
1122 "Remove a single key from a plist"
1123 (loop* :for (k v) :on plist :by #'cddr
1124 :unless (eq k key)
1125 :append (list k v)))
1127 (defun remove-plist-keys (keys plist)
1128 "Remove a list of keys from a plist"
1129 (loop* :for (k v) :on plist :by #'cddr
1130 :unless (member k keys)
1131 :append (list k v))))
1134 ;;; Sequences
1135 (with-upgradability ()
1136 (defun emptyp (x)
1137 "Predicate that is true for an empty sequence"
1138 (or (null x) (and (vectorp x) (zerop (length x))))))
1141 ;;; Characters
1142 (with-upgradability () ;; base-char != character on ECL, LW, SBCL, Genera. LW also has SIMPLE-CHAR.
1143 (defconstant +non-base-chars-exist-p+ #.(not (subtypep 'character 'base-char)))
1144 #-scl ;; In SCL, all characters seem to be 16-bit base-char, but this flag gets set somehow???
1145 (when +non-base-chars-exist-p+ (pushnew :non-base-chars-exist-p *features*)))
1147 (with-upgradability ()
1148 (defparameter +character-types+ ;; assuming a simple hierarchy
1149 #(#+non-base-chars-exist-p base-char #+lispworks lw:simple-char character))
1150 (defparameter +max-character-type-index+ (1- (length +character-types+))))
1152 (with-upgradability ()
1153 (defun character-type-index (x)
1154 (declare (ignorable x))
1155 #.(case +max-character-type-index+
1156 (0 0)
1157 (1 '(etypecase x
1158 (character (if (typep x 'base-char) 0 1))
1159 (symbol (if (subtypep x 'base-char) 0 1))))
1160 (otherwise
1161 '(or (position-if (etypecase x
1162 (character #'(lambda (type) (typep x type)))
1163 (symbol #'(lambda (type) (subtypep x type))))
1164 +character-types+)
1165 (error "Not a character or character type: ~S" x))))))
1168 ;;; Strings
1169 (with-upgradability ()
1170 (defun base-string-p (string)
1171 "Does the STRING only contain BASE-CHARs?"
1172 (declare (ignorable string))
1173 (and #+non-base-chars-exist-p (eq 'base-char (array-element-type string))))
1175 (defun strings-common-element-type (strings)
1176 "What least subtype of CHARACTER can contain all the elements of all the STRINGS?"
1177 (declare (ignorable strings))
1178 #.(if +non-base-chars-exist-p+
1179 `(aref +character-types+
1180 (loop :with index = 0 :for s :in strings :do
1181 (cond
1182 ((= index ,+max-character-type-index+) (return index))
1183 ((emptyp s)) ;; NIL or empty string
1184 ((characterp s) (setf index (max index (character-type-index s))))
1185 ((stringp s) (unless (>= index (character-type-index (array-element-type s)))
1186 (setf index (reduce 'max s :key #'character-type-index
1187 :initial-value index))))
1188 (t (error "Invalid string designator ~S for ~S" s 'strings-common-element-type)))
1189 :finally (return index)))
1190 ''character))
1192 (defun reduce/strcat (strings &key key start end)
1193 "Reduce a list as if by STRCAT, accepting KEY START and END keywords like REDUCE.
1194 NIL is interpreted as an empty string. A character is interpreted as a string of length one."
1195 (when (or start end) (setf strings (subseq strings start end)))
1196 (when key (setf strings (mapcar key strings)))
1197 (loop :with output = (make-string (loop :for s :in strings
1198 :sum (if (characterp s) 1 (length s)))
1199 :element-type (strings-common-element-type strings))
1200 :with pos = 0
1201 :for input :in strings
1202 :do (etypecase input
1203 (null)
1204 (character (setf (char output pos) input) (incf pos))
1205 (string (replace output input :start1 pos) (incf pos (length input))))
1206 :finally (return output)))
1208 (defun strcat (&rest strings)
1209 "Concatenate strings.
1210 NIL is interpreted as an empty string, a character as a string of length one."
1211 (reduce/strcat strings))
1213 (defun first-char (s)
1214 "Return the first character of a non-empty string S, or NIL"
1215 (and (stringp s) (plusp (length s)) (char s 0)))
1217 (defun last-char (s)
1218 "Return the last character of a non-empty string S, or NIL"
1219 (and (stringp s) (plusp (length s)) (char s (1- (length s)))))
1221 (defun split-string (string &key max (separator '(#\Space #\Tab)))
1222 "Split STRING into a list of components separated by
1223 any of the characters in the sequence SEPARATOR.
1224 If MAX is specified, then no more than max(1,MAX) components will be returned,
1225 starting the separation from the end, e.g. when called with arguments
1226 \"a.b.c.d.e\" :max 3 :separator \".\" it will return (\"a.b.c\" \"d\" \"e\")."
1227 (block ()
1228 (let ((list nil) (words 0) (end (length string)))
1229 (when (zerop end) (return nil))
1230 (flet ((separatorp (char) (find char separator))
1231 (done () (return (cons (subseq string 0 end) list))))
1232 (loop
1233 :for start = (if (and max (>= words (1- max)))
1234 (done)
1235 (position-if #'separatorp string :end end :from-end t))
1236 :do (when (null start) (done))
1237 (push (subseq string (1+ start) end) list)
1238 (incf words)
1239 (setf end start))))))
1241 (defun string-prefix-p (prefix string)
1242 "Does STRING begin with PREFIX?"
1243 (let* ((x (string prefix))
1244 (y (string string))
1245 (lx (length x))
1246 (ly (length y)))
1247 (and (<= lx ly) (string= x y :end2 lx))))
1249 (defun string-suffix-p (string suffix)
1250 "Does STRING end with SUFFIX?"
1251 (let* ((x (string string))
1252 (y (string suffix))
1253 (lx (length x))
1254 (ly (length y)))
1255 (and (<= ly lx) (string= x y :start1 (- lx ly)))))
1257 (defun string-enclosed-p (prefix string suffix)
1258 "Does STRING begin with PREFIX and end with SUFFIX?"
1259 (and (string-prefix-p prefix string)
1260 (string-suffix-p string suffix))))
1262 (defvar +cr+ (coerce #(#\Return) 'string))
1263 (defvar +lf+ (coerce #(#\Linefeed) 'string))
1264 (defvar +crlf+ (coerce #(#\Return #\Linefeed) 'string))
1266 (defun stripln (x)
1267 "Strip a string X from any ending CR, LF or CRLF.
1268 Return two values, the stripped string and the ending that was stripped,
1269 or the original value and NIL if no stripping took place.
1270 Since our STRCAT accepts NIL as empty string designator,
1271 the two results passed to STRCAT always reconstitute the original string"
1272 (check-type x string)
1273 (block nil
1274 (flet ((c (end) (when (string-suffix-p x end)
1275 (return (values (subseq x 0 (- (length x) (length end))) end)))))
1276 (when x (c +crlf+) (c +lf+) (c +cr+) (values x nil)))))
1279 ;;; stamps: a REAL or a boolean where NIL=-infinity, T=+infinity
1280 (eval-when (#-lispworks :compile-toplevel :load-toplevel :execute)
1281 (deftype stamp () '(or real boolean)))
1282 (with-upgradability ()
1283 (defun stamp< (x y)
1284 (etypecase x
1285 (null (and y t))
1286 ((eql t) nil)
1287 (real (etypecase y
1288 (null nil)
1289 ((eql t) t)
1290 (real (< x y))))))
1291 (defun stamps< (list) (loop :for y :in list :for x = nil :then y :always (stamp< x y)))
1292 (defun stamp*< (&rest list) (stamps< list))
1293 (defun stamp<= (x y) (not (stamp< y x)))
1294 (defun earlier-stamp (x y) (if (stamp< x y) x y))
1295 (defun stamps-earliest (list) (reduce 'earlier-stamp list :initial-value t))
1296 (defun earliest-stamp (&rest list) (stamps-earliest list))
1297 (defun later-stamp (x y) (if (stamp< x y) y x))
1298 (defun stamps-latest (list) (reduce 'later-stamp list :initial-value nil))
1299 (defun latest-stamp (&rest list) (stamps-latest list))
1300 (define-modify-macro latest-stamp-f (&rest stamps) latest-stamp))
1303 ;;; Function designators
1304 (with-upgradability ()
1305 (defun ensure-function (fun &key (package :cl))
1306 "Coerce the object FUN into a function.
1308 If FUN is a FUNCTION, return it.
1309 If the FUN is a non-sequence literal constant, return constantly that,
1310 i.e. for a boolean keyword character number or pathname.
1311 Otherwise if FUN is a non-literally constant symbol, return its FDEFINITION.
1312 If FUN is a CONS, return the function that applies its CAR
1313 to the appended list of the rest of its CDR and the arguments,
1314 unless the CAR is LAMBDA, in which case the expression is evaluated.
1315 If FUN is a string, READ a form from it in the specified PACKAGE (default: CL)
1316 and EVAL that in a (FUNCTION ...) context."
1317 (etypecase fun
1318 (function fun)
1319 ((or boolean keyword character number pathname) (constantly fun))
1320 (hash-table #'(lambda (x) (gethash x fun)))
1321 (symbol (fdefinition fun))
1322 (cons (if (eq 'lambda (car fun))
1323 (eval fun)
1324 #'(lambda (&rest args) (apply (car fun) (append (cdr fun) args)))))
1325 (string (eval `(function ,(with-standard-io-syntax
1326 (let ((*package* (find-package package)))
1327 (read-from-string fun))))))))
1329 (defun access-at (object at)
1330 "Given an OBJECT and an AT specifier, list of successive accessors,
1331 call each accessor on the result of the previous calls.
1332 An accessor may be an integer, meaning a call to ELT,
1333 a keyword, meaning a call to GETF,
1334 NIL, meaning identity,
1335 a function or other symbol, meaning itself,
1336 or a list of a function designator and arguments, interpreted as per ENSURE-FUNCTION.
1337 As a degenerate case, the AT specifier may be an atom of a single such accessor
1338 instead of a list."
1339 (flet ((access (object accessor)
1340 (etypecase accessor
1341 (function (funcall accessor object))
1342 (integer (elt object accessor))
1343 (keyword (getf object accessor))
1344 (null object)
1345 (symbol (funcall accessor object))
1346 (cons (funcall (ensure-function accessor) object)))))
1347 (if (listp at)
1348 (dolist (accessor at object)
1349 (setf object (access object accessor)))
1350 (access object at))))
1352 (defun access-at-count (at)
1353 "From an AT specification, extract a COUNT of maximum number
1354 of sub-objects to read as per ACCESS-AT"
1355 (cond
1356 ((integerp at)
1357 (1+ at))
1358 ((and (consp at) (integerp (first at)))
1359 (1+ (first at)))))
1361 (defun call-function (function-spec &rest arguments)
1362 "Call the function designated by FUNCTION-SPEC as per ENSURE-FUNCTION,
1363 with the given ARGUMENTS"
1364 (apply (ensure-function function-spec) arguments))
1366 (defun call-functions (function-specs)
1367 "For each function in the list FUNCTION-SPECS, in order, call the function as per CALL-FUNCTION"
1368 (map () 'call-function function-specs))
1370 (defun register-hook-function (variable hook &optional call-now-p)
1371 "Push the HOOK function (a designator as per ENSURE-FUNCTION) onto the hook VARIABLE.
1372 When CALL-NOW-P is true, also call the function immediately."
1373 (pushnew hook (symbol-value variable) :test 'equal)
1374 (when call-now-p (call-function hook))))
1377 ;;; CLOS
1378 (with-upgradability ()
1379 (defun coerce-class (class &key (package :cl) (super t) (error 'error))
1380 "Coerce CLASS to a class that is subclass of SUPER if specified,
1381 or invoke ERROR handler as per CALL-FUNCTION.
1383 A keyword designates the name a symbol, which when found in either PACKAGE, designates a class.
1384 -- for backward compatibility, *PACKAGE* is also accepted for now, but this may go in the future.
1385 A string is read as a symbol while in PACKAGE, the symbol designates a class.
1387 A class object designates itself.
1388 NIL designates itself (no class).
1389 A symbol otherwise designates a class by name."
1390 (let* ((normalized
1391 (typecase class
1392 (keyword (or (find-symbol* class package nil)
1393 (find-symbol* class *package* nil)))
1394 (string (symbol-call :uiop :safe-read-from-string class :package package))
1395 (t class)))
1396 (found
1397 (etypecase normalized
1398 ((or standard-class built-in-class) normalized)
1399 ((or null keyword) nil)
1400 (symbol (find-class normalized nil nil)))))
1401 (or (and found
1402 (or (eq super t) (#-cormanlisp subtypep #+cormanlisp cl::subclassp found super))
1403 found)
1404 (call-function error "Can't coerce ~S to a ~@[class~;subclass of ~:*~S]" class super)))))
1407 ;;; Hash-tables
1408 (with-upgradability ()
1409 (defun ensure-gethash (key table default)
1410 "Lookup the TABLE for a KEY as by GETHASH, but if not present,
1411 call the (possibly constant) function designated by DEFAULT as per CALL-FUNCTION,
1412 set the corresponding entry to the result in the table.
1413 Return two values: the entry after its optional computation, and whether it was found"
1414 (multiple-value-bind (value foundp) (gethash key table)
1415 (values
1416 (if foundp
1417 value
1418 (setf (gethash key table) (call-function default)))
1419 foundp)))
1421 (defun list-to-hash-set (list &aux (h (make-hash-table :test 'equal)))
1422 "Convert a LIST into hash-table that has the same elements when viewed as a set,
1423 up to the given equality TEST"
1424 (dolist (x list h) (setf (gethash x h) t))))
1427 ;;; Version handling
1428 (with-upgradability ()
1429 (defun unparse-version (version-list)
1430 (format nil "~{~D~^.~}" version-list))
1432 (defun parse-version (version-string &optional on-error)
1433 "Parse a VERSION-STRING as a series of natural integers separated by dots.
1434 Return a (non-null) list of integers if the string is valid;
1435 otherwise return NIL.
1437 When invalid, ON-ERROR is called as per CALL-FUNCTION before to return NIL,
1438 with format arguments explaining why the version is invalid.
1439 ON-ERROR is also called if the version is not canonical
1440 in that it doesn't print back to itself, but the list is returned anyway."
1441 (block nil
1442 (unless (stringp version-string)
1443 (call-function on-error "~S: ~S is not a string" 'parse-version version-string)
1444 (return))
1445 (unless (loop :for prev = nil :then c :for c :across version-string
1446 :always (or (digit-char-p c)
1447 (and (eql c #\.) prev (not (eql prev #\.))))
1448 :finally (return (and c (digit-char-p c))))
1449 (call-function on-error "~S: ~S doesn't follow asdf version numbering convention"
1450 'parse-version version-string)
1451 (return))
1452 (let* ((version-list
1453 (mapcar #'parse-integer (split-string version-string :separator ".")))
1454 (normalized-version (unparse-version version-list)))
1455 (unless (equal version-string normalized-version)
1456 (call-function on-error "~S: ~S contains leading zeros" 'parse-version version-string))
1457 version-list)))
1459 (defun lexicographic< (< x y)
1460 (cond ((null y) nil)
1461 ((null x) t)
1462 ((funcall < (car x) (car y)) t)
1463 ((funcall < (car y) (car x)) nil)
1464 (t (lexicographic< < (cdr x) (cdr y)))))
1466 (defun lexicographic<= (< x y)
1467 (not (lexicographic< < y x)))
1469 (defun version< (version1 version2)
1470 (let ((v1 (parse-version version1 nil))
1471 (v2 (parse-version version2 nil)))
1472 (lexicographic< '< v1 v2)))
1474 (defun version<= (version1 version2)
1475 (not (version< version2 version1)))
1477 (defun version-compatible-p (provided-version required-version)
1478 "Is the provided version a compatible substitution for the required-version?
1479 If major versions differ, it's not compatible.
1480 If they are equal, then any later version is compatible,
1481 with later being determined by a lexicographical comparison of minor numbers."
1482 (let ((x (parse-version provided-version nil))
1483 (y (parse-version required-version nil)))
1484 (and x y (= (car x) (car y)) (lexicographic<= '< (cdr y) (cdr x))))))
1487 ;;; Condition control
1489 (with-upgradability ()
1490 (defparameter +simple-condition-format-control-slot+
1491 #+abcl 'system::format-control
1492 #+allegro 'excl::format-control
1493 #+clisp 'system::$format-control
1494 #+clozure 'ccl::format-control
1495 #+(or cmu scl) 'conditions::format-control
1496 #+(or ecl mkcl) 'si::format-control
1497 #+(or gcl lispworks) 'conditions::format-string
1498 #+sbcl 'sb-kernel:format-control
1499 #-(or abcl allegro clisp clozure cmu ecl gcl lispworks mkcl sbcl scl) nil
1500 "Name of the slot for FORMAT-CONTROL in simple-condition")
1502 (defun match-condition-p (x condition)
1503 "Compare received CONDITION to some pattern X:
1504 a symbol naming a condition class,
1505 a simple vector of length 2, arguments to find-symbol* with result as above,
1506 or a string describing the format-control of a simple-condition."
1507 (etypecase x
1508 (symbol (typep condition x))
1509 ((simple-vector 2)
1510 (ignore-errors (typep condition (find-symbol* (svref x 0) (svref x 1) nil))))
1511 (function (funcall x condition))
1512 (string (and (typep condition 'simple-condition)
1513 ;; On SBCL, it's always set and the check triggers a warning
1514 #+(or allegro clozure cmu lispworks scl)
1515 (slot-boundp condition +simple-condition-format-control-slot+)
1516 (ignore-errors (equal (simple-condition-format-control condition) x))))))
1518 (defun match-any-condition-p (condition conditions)
1519 "match CONDITION against any of the patterns of CONDITIONS supplied"
1520 (loop :for x :in conditions :thereis (match-condition-p x condition)))
1522 (defun call-with-muffled-conditions (thunk conditions)
1523 "calls the THUNK in a context where the CONDITIONS are muffled"
1524 (handler-bind ((t #'(lambda (c) (when (match-any-condition-p c conditions)
1525 (muffle-warning c)))))
1526 (funcall thunk)))
1528 (defmacro with-muffled-conditions ((conditions) &body body)
1529 "Shorthand syntax for CALL-WITH-MUFFLED-CONDITIONS"
1530 `(call-with-muffled-conditions #'(lambda () ,@body) ,conditions)))
1532 ;;;; ---------------------------------------------------------------------------
1533 ;;;; Access to the Operating System
1535 (uiop/package:define-package :uiop/os
1536 (:nicknames :asdf/os)
1537 (:recycle :uiop/os :asdf/os :asdf)
1538 (:use :uiop/common-lisp :uiop/package :uiop/utility)
1539 (:export
1540 #:featurep #:os-unix-p #:os-macosx-p #:os-windows-p #:os-genera-p #:detect-os ;; features
1541 #:getenv #:getenvp ;; environment variables
1542 #:implementation-identifier ;; implementation identifier
1543 #:implementation-type #:*implementation-type*
1544 #:operating-system #:architecture #:lisp-version-string
1545 #:hostname #:getcwd #:chdir
1546 ;; Windows shortcut support
1547 #:read-null-terminated-string #:read-little-endian
1548 #:parse-file-location-info #:parse-windows-shortcut))
1549 (in-package :uiop/os)
1551 ;;; Features
1552 (with-upgradability ()
1553 (defun featurep (x &optional (*features* *features*))
1554 "Checks whether a feature expression X is true with respect to the *FEATURES* set,
1555 as per the CLHS standard for #+ and #-. Beware that just like the CLHS,
1556 we assume symbols from the KEYWORD package are used, but that unless you're using #+/#-
1557 your reader will not have magically used the KEYWORD package, so you need specify
1558 keywords explicitly."
1559 (cond
1560 ((atom x) (and (member x *features*) t))
1561 ((eq :not (car x)) (assert (null (cddr x))) (not (featurep (cadr x))))
1562 ((eq :or (car x)) (some #'featurep (cdr x)))
1563 ((eq :and (car x)) (every #'featurep (cdr x)))
1564 (t (error "Malformed feature specification ~S" x))))
1566 (defun os-unix-p ()
1567 "Is the underlying operating system some Unix variant?"
1568 (or #+abcl (featurep :unix)
1569 #+(and (not abcl) (or unix cygwin darwin)) t))
1571 (defun os-macosx-p ()
1572 "Is the underlying operating system MacOS X?"
1573 ;; OS-MACOSX is not mutually exclusive with OS-UNIX,
1574 ;; in fact the former implies the latter.
1576 #+allegro (featurep :macosx)
1577 #+clisp (featurep :macos)
1578 (featurep :darwin)))
1580 (defun os-windows-p ()
1581 "Is the underlying operating system Microsoft Windows?"
1582 (or #+abcl (featurep :windows)
1583 #+(and (not (or abcl unix cygwin darwin)) (or win32 windows mswindows mingw32 mingw64)) t))
1585 (defun os-genera-p ()
1586 "Is the underlying operating system Genera (running on a Symbolics Lisp Machine)?"
1587 (or #+genera t))
1589 (defun os-oldmac-p ()
1590 "Is the underlying operating system an (emulated?) MacOS 9 or earlier?"
1591 (or #+mcl t))
1593 (defun detect-os ()
1594 "Detects the current operating system. Only needs be run at compile-time,
1595 except on ABCL where it might change between FASL compilation and runtime."
1596 (loop* :with o
1597 :for (feature . detect) :in '((:os-unix . os-unix-p) (:os-macosx . os-macosx-p)
1598 (:os-windows . os-windows-p)
1599 (:genera . os-genera-p) (:os-oldmac . os-oldmac-p))
1600 :when (and (or (not o) (eq feature :os-macosx)) (funcall detect))
1601 :do (setf o feature) (pushnew feature *features*)
1602 :else :do (setf *features* (remove feature *features*))
1603 :finally
1604 (return (or o (error "Congratulations for trying ASDF on an operating system~%~
1605 that is neither Unix, nor Windows, nor Genera, nor even old MacOS.~%Now you port it.")))))
1607 (detect-os))
1609 ;;;; Environment variables: getting them, and parsing them.
1611 (with-upgradability ()
1612 (defun getenv (x)
1613 "Query the environment, as in C getenv.
1614 Beware: may return empty string if a variable is present but empty;
1615 use getenvp to return NIL in such a case."
1616 (declare (ignorable x))
1617 #+(or abcl clisp ecl xcl) (ext:getenv x)
1618 #+allegro (sys:getenv x)
1619 #+clozure (ccl:getenv x)
1620 #+(or cmu scl) (cdr (assoc x ext:*environment-list* :test #'string=))
1621 #+cormanlisp
1622 (let* ((buffer (ct:malloc 1))
1623 (cname (ct:lisp-string-to-c-string x))
1624 (needed-size (win:getenvironmentvariable cname buffer 0))
1625 (buffer1 (ct:malloc (1+ needed-size))))
1626 (prog1 (if (zerop (win:getenvironmentvariable cname buffer1 needed-size))
1628 (ct:c-string-to-lisp-string buffer1))
1629 (ct:free buffer)
1630 (ct:free buffer1)))
1631 #+gcl (system:getenv x)
1632 #+genera nil
1633 #+lispworks (lispworks:environment-variable x)
1634 #+mcl (ccl:with-cstrs ((name x))
1635 (let ((value (_getenv name)))
1636 (unless (ccl:%null-ptr-p value)
1637 (ccl:%get-cstring value))))
1638 #+mkcl (#.(or (find-symbol* 'getenv :si nil) (find-symbol* 'getenv :mk-ext nil)) x)
1639 #+sbcl (sb-ext:posix-getenv x)
1640 #-(or abcl allegro clisp clozure cmu cormanlisp ecl gcl genera lispworks mcl mkcl sbcl scl xcl)
1641 (error "~S is not supported on your implementation" 'getenv))
1643 (defun getenvp (x)
1644 "Predicate that is true if the named variable is present in the libc environment,
1645 then returning the non-empty string value of the variable"
1646 (let ((g (getenv x))) (and (not (emptyp g)) g))))
1649 ;;;; implementation-identifier
1651 ;; produce a string to identify current implementation.
1652 ;; Initially stolen from SLIME's SWANK, completely rewritten since.
1653 ;; We're back to runtime checking, for the sake of e.g. ABCL.
1655 (with-upgradability ()
1656 (defun first-feature (feature-sets)
1657 "A helper for various feature detection functions"
1658 (dolist (x feature-sets)
1659 (multiple-value-bind (short long feature-expr)
1660 (if (consp x)
1661 (values (first x) (second x) (cons :or (rest x)))
1662 (values x x x))
1663 (when (featurep feature-expr)
1664 (return (values short long))))))
1666 (defun implementation-type ()
1667 "The type of Lisp implementation used, as a short UIOP-standardized keyword"
1668 (first-feature
1669 '(:abcl (:acl :allegro) (:ccl :clozure) :clisp (:corman :cormanlisp)
1670 (:cmu :cmucl :cmu) :ecl :gcl
1671 (:lwpe :lispworks-personal-edition) (:lw :lispworks)
1672 :mcl :mkcl :sbcl :scl (:smbx :symbolics) :xcl)))
1674 (defvar *implementation-type* (implementation-type)
1675 "The type of Lisp implementation used, as a short UIOP-standardized keyword")
1677 (defun operating-system ()
1678 "The operating system of the current host"
1679 (first-feature
1680 '(:cygwin
1681 (:win :windows :mswindows :win32 :mingw32) ;; try cygwin first!
1682 (:linux :linux :linux-target) ;; for GCL at least, must appear before :bsd
1683 (:macosx :macosx :darwin :darwin-target :apple) ; also before :bsd
1684 (:solaris :solaris :sunos)
1685 (:bsd :bsd :freebsd :netbsd :openbsd :dragonfly)
1686 :unix
1687 :genera)))
1689 (defun architecture ()
1690 "The CPU architecture of the current host"
1691 (first-feature
1692 '((:x64 :x86-64 :x86_64 :x8664-target :amd64 (:and :word-size=64 :pc386))
1693 (:x86 :x86 :i386 :i486 :i586 :i686 :pentium3 :pentium4 :pc386 :iapx386 :x8632-target)
1694 (:ppc64 :ppc64 :ppc64-target) (:ppc32 :ppc32 :ppc32-target :ppc :powerpc)
1695 :hppa64 :hppa :sparc64 (:sparc32 :sparc32 :sparc)
1696 :mipsel :mipseb :mips :alpha (:arm :arm :arm-target) :imach
1697 ;; Java comes last: if someone uses C via CFFI or otherwise JNA or JNI,
1698 ;; we may have to segregate the code still by architecture.
1699 (:java :java :java-1.4 :java-1.5 :java-1.6 :java-1.7))))
1701 #+clozure
1702 (defun ccl-fasl-version ()
1703 ;; the fasl version is target-dependent from CCL 1.8 on.
1704 (or (let ((s 'ccl::target-fasl-version))
1705 (and (fboundp s) (funcall s)))
1706 (and (boundp 'ccl::fasl-version)
1707 (symbol-value 'ccl::fasl-version))
1708 (error "Can't determine fasl version.")))
1710 (defun lisp-version-string ()
1711 "return a string that identifies the current Lisp implementation version"
1712 (let ((s (lisp-implementation-version)))
1713 (car ; as opposed to OR, this idiom prevents some unreachable code warning
1714 (list
1715 #+allegro
1716 (format nil "~A~@[~A~]~@[~A~]~@[~A~]"
1717 excl::*common-lisp-version-number*
1718 ;; M means "modern", as opposed to ANSI-compatible mode (which I consider default)
1719 (and (eq excl:*current-case-mode* :case-sensitive-lower) "M")
1720 ;; Note if not using International ACL
1721 ;; see http://www.franz.com/support/documentation/8.1/doc/operators/excl/ics-target-case.htm
1722 (excl:ics-target-case (:-ics "8"))
1723 (and (member :smp *features*) "S"))
1724 #+armedbear (format nil "~a-fasl~a" s system::*fasl-version*)
1725 #+clisp
1726 (subseq s 0 (position #\space s)) ; strip build information (date, etc.)
1727 #+clozure
1728 (format nil "~d.~d-f~d" ; shorten for windows
1729 ccl::*openmcl-major-version*
1730 ccl::*openmcl-minor-version*
1731 (logand (ccl-fasl-version) #xFF))
1732 #+cmu (substitute #\- #\/ s)
1733 #+scl (format nil "~A~A" s
1734 ;; ANSI upper case vs lower case.
1735 (ecase ext:*case-mode* (:upper "") (:lower "l")))
1736 #+ecl (format nil "~A~@[-~A~]" s
1737 (let ((vcs-id (ext:lisp-implementation-vcs-id)))
1738 (subseq vcs-id 0 (min (length vcs-id) 8))))
1739 #+gcl (subseq s (1+ (position #\space s)))
1740 #+genera
1741 (multiple-value-bind (major minor) (sct:get-system-version "System")
1742 (format nil "~D.~D" major minor))
1743 #+mcl (subseq s 8) ; strip the leading "Version "
1744 s))))
1746 (defun implementation-identifier ()
1747 "Return a string that identifies the ABI of the current implementation,
1748 suitable for use as a directory name to segregate Lisp FASLs, C dynamic libraries, etc."
1749 (substitute-if
1750 #\_ #'(lambda (x) (find x " /:;&^\\|?<>(){}[]$#`'\""))
1751 (format nil "~(~a~@{~@[-~a~]~}~)"
1752 (or (implementation-type) (lisp-implementation-type))
1753 (or (lisp-version-string) (lisp-implementation-version))
1754 (or (operating-system) (software-type))
1755 (or (architecture) (machine-type))))))
1758 ;;;; Other system information
1760 (with-upgradability ()
1761 (defun hostname ()
1762 "return the hostname of the current host"
1763 ;; Note: untested on RMCL
1764 #+(or abcl clozure cmu ecl genera lispworks mcl mkcl sbcl scl xcl) (machine-instance)
1765 #+cormanlisp "localhost" ;; is there a better way? Does it matter?
1766 #+allegro (symbol-call :excl.osi :gethostname)
1767 #+clisp (first (split-string (machine-instance) :separator " "))
1768 #+gcl (system:gethostname)))
1771 ;;; Current directory
1772 (with-upgradability ()
1774 #+cmu
1775 (defun parse-unix-namestring* (unix-namestring)
1776 "variant of LISP::PARSE-UNIX-NAMESTRING that returns a pathname object"
1777 (multiple-value-bind (host device directory name type version)
1778 (lisp::parse-unix-namestring unix-namestring 0 (length unix-namestring))
1779 (make-pathname :host (or host lisp::*unix-host*) :device device
1780 :directory directory :name name :type type :version version)))
1782 (defun getcwd ()
1783 "Get the current working directory as per POSIX getcwd(3), as a pathname object"
1784 (or #+abcl (truename (symbol-call :asdf/filesystem :parse-native-namestring
1785 (java:jstatic "getProperty" "java.lang.System" "user.dir")
1786 :ensure-directory t))
1787 #+allegro (excl::current-directory)
1788 #+clisp (ext:default-directory)
1789 #+clozure (ccl:current-directory)
1790 #+(or cmu scl) (#+cmu parse-unix-namestring* #+scl lisp::parse-unix-namestring
1791 (strcat (nth-value 1 (unix:unix-current-directory)) "/"))
1792 #+cormanlisp (pathname (pl::get-current-directory)) ;; Q: what type does it return?
1793 #+ecl (ext:getcwd)
1794 #+gcl (let ((*default-pathname-defaults* #p"")) (truename #p""))
1795 #+genera *default-pathname-defaults* ;; on a Lisp OS, it *is* canonical!
1796 #+lispworks (system:current-directory)
1797 #+mkcl (mk-ext:getcwd)
1798 #+sbcl (sb-ext:parse-native-namestring (sb-unix:posix-getcwd/))
1799 #+xcl (extensions:current-directory)
1800 (error "getcwd not supported on your implementation")))
1802 (defun chdir (x)
1803 "Change current directory, as per POSIX chdir(2), to a given pathname object"
1804 (if-let (x (pathname x))
1805 (or #+abcl (java:jstatic "setProperty" "java.lang.System" "user.dir" (namestring x))
1806 #+allegro (excl:chdir x)
1807 #+clisp (ext:cd x)
1808 #+clozure (setf (ccl:current-directory) x)
1809 #+(or cmu scl) (unix:unix-chdir (ext:unix-namestring x))
1810 #+cormanlisp (unless (zerop (win32::_chdir (namestring x)))
1811 (error "Could not set current directory to ~A" x))
1812 #+ecl (ext:chdir x)
1813 #+genera (setf *default-pathname-defaults* x)
1814 #+lispworks (hcl:change-directory x)
1815 #+mkcl (mk-ext:chdir x)
1816 #+sbcl (progn (require :sb-posix) (symbol-call :sb-posix :chdir (sb-ext:native-namestring x)))
1817 (error "chdir not supported on your implementation")))))
1820 ;;;; -----------------------------------------------------------------
1821 ;;;; Windows shortcut support. Based on:
1822 ;;;;
1823 ;;;; Jesse Hager: The Windows Shortcut File Format.
1824 ;;;; http://www.wotsit.org/list.asp?fc=13
1826 #-(or clisp genera) ; CLISP doesn't need it, and READ-SEQUENCE annoys old Genera that doesn't need it
1827 (with-upgradability ()
1828 (defparameter *link-initial-dword* 76)
1829 (defparameter *link-guid* #(1 20 2 0 0 0 0 0 192 0 0 0 0 0 0 70))
1831 (defun read-null-terminated-string (s)
1832 "Read a null-terminated string from an octet stream S"
1833 ;; note: doesn't play well with UNICODE
1834 (with-output-to-string (out)
1835 (loop :for code = (read-byte s)
1836 :until (zerop code)
1837 :do (write-char (code-char code) out))))
1839 (defun read-little-endian (s &optional (bytes 4))
1840 "Read a number in little-endian format from an byte (octet) stream S,
1841 the number having BYTES octets (defaulting to 4)."
1842 (loop :for i :from 0 :below bytes
1843 :sum (ash (read-byte s) (* 8 i))))
1845 (defun parse-file-location-info (s)
1846 "helper to parse-windows-shortcut"
1847 (let ((start (file-position s))
1848 (total-length (read-little-endian s))
1849 (end-of-header (read-little-endian s))
1850 (fli-flags (read-little-endian s))
1851 (local-volume-offset (read-little-endian s))
1852 (local-offset (read-little-endian s))
1853 (network-volume-offset (read-little-endian s))
1854 (remaining-offset (read-little-endian s)))
1855 (declare (ignore total-length end-of-header local-volume-offset))
1856 (unless (zerop fli-flags)
1857 (cond
1858 ((logbitp 0 fli-flags)
1859 (file-position s (+ start local-offset)))
1860 ((logbitp 1 fli-flags)
1861 (file-position s (+ start
1862 network-volume-offset
1863 #x14))))
1864 (strcat (read-null-terminated-string s)
1865 (progn
1866 (file-position s (+ start remaining-offset))
1867 (read-null-terminated-string s))))))
1869 (defun parse-windows-shortcut (pathname)
1870 "From a .lnk windows shortcut, extract the pathname linked to"
1871 ;; NB: doesn't do much checking & doesn't look like it will work well with UNICODE.
1872 (with-open-file (s pathname :element-type '(unsigned-byte 8))
1873 (handler-case
1874 (when (and (= (read-little-endian s) *link-initial-dword*)
1875 (let ((header (make-array (length *link-guid*))))
1876 (read-sequence header s)
1877 (equalp header *link-guid*)))
1878 (let ((flags (read-little-endian s)))
1879 (file-position s 76) ;skip rest of header
1880 (when (logbitp 0 flags)
1881 ;; skip shell item id list
1882 (let ((length (read-little-endian s 2)))
1883 (file-position s (+ length (file-position s)))))
1884 (cond
1885 ((logbitp 1 flags)
1886 (parse-file-location-info s))
1888 (when (logbitp 2 flags)
1889 ;; skip description string
1890 (let ((length (read-little-endian s 2)))
1891 (file-position s (+ length (file-position s)))))
1892 (when (logbitp 3 flags)
1893 ;; finally, our pathname
1894 (let* ((length (read-little-endian s 2))
1895 (buffer (make-array length)))
1896 (read-sequence buffer s)
1897 (map 'string #'code-char buffer)))))))
1898 (end-of-file (c)
1899 (declare (ignore c))
1900 nil)))))
1903 ;;;; -------------------------------------------------------------------------
1904 ;;;; Portability layer around Common Lisp pathnames
1905 ;; This layer allows for portable manipulation of pathname objects themselves,
1906 ;; which all is necessary prior to any access the filesystem or environment.
1908 (uiop/package:define-package :uiop/pathname
1909 (:nicknames :asdf/pathname)
1910 (:recycle :uiop/pathname :asdf/pathname :asdf)
1911 (:use :uiop/common-lisp :uiop/package :uiop/utility :uiop/os)
1912 (:export
1913 ;; Making and merging pathnames, portably
1914 #:normalize-pathname-directory-component #:denormalize-pathname-directory-component
1915 #:merge-pathname-directory-components #:*unspecific-pathname-type* #:make-pathname*
1916 #:make-pathname-component-logical #:make-pathname-logical
1917 #:merge-pathnames*
1918 #:nil-pathname #:*nil-pathname* #:with-pathname-defaults
1919 ;; Predicates
1920 #:pathname-equal #:logical-pathname-p #:physical-pathname-p #:physicalize-pathname
1921 #:absolute-pathname-p #:relative-pathname-p #:hidden-pathname-p #:file-pathname-p
1922 ;; Directories
1923 #:pathname-directory-pathname #:pathname-parent-directory-pathname
1924 #:directory-pathname-p #:ensure-directory-pathname
1925 ;; Parsing filenames
1926 #:component-name-to-pathname-components
1927 #:split-name-type #:parse-unix-namestring #:unix-namestring
1928 #:split-unix-namestring-directory-components
1929 ;; Absolute and relative pathnames
1930 #:subpathname #:subpathname*
1931 #:ensure-absolute-pathname
1932 #:pathname-root #:pathname-host-pathname
1933 #:subpathp #:enough-pathname #:with-enough-pathname #:call-with-enough-pathname
1934 ;; Checking constraints
1935 #:ensure-pathname ;; implemented in filesystem.lisp to accommodate for existence constraints
1936 ;; Wildcard pathnames
1937 #:*wild* #:*wild-file* #:*wild-directory* #:*wild-inferiors* #:*wild-path* #:wilden
1938 ;; Translate a pathname
1939 #:relativize-directory-component #:relativize-pathname-directory
1940 #:directory-separator-for-host #:directorize-pathname-host-device
1941 #:translate-pathname*
1942 #:*output-translation-function*))
1943 (in-package :uiop/pathname)
1945 ;;; Normalizing pathnames across implementations
1947 (with-upgradability ()
1948 (defun normalize-pathname-directory-component (directory)
1949 "Convert the DIRECTORY component from a format usable by the underlying
1950 implementation's MAKE-PATHNAME and other primitives to a CLHS-standard format
1951 that is a list and not a string."
1952 (cond
1953 #-(or cmu sbcl scl) ;; these implementations already normalize directory components.
1954 ((stringp directory) `(:absolute ,directory))
1955 ((or (null directory)
1956 (and (consp directory) (member (first directory) '(:absolute :relative))))
1957 directory)
1958 #+gcl
1959 ((consp directory)
1960 (cons :relative directory))
1962 (error (compatfmt "~@<Unrecognized pathname directory component ~S~@:>") directory))))
1964 (defun denormalize-pathname-directory-component (directory-component)
1965 "Convert the DIRECTORY-COMPONENT from a CLHS-standard format to a format usable
1966 by the underlying implementation's MAKE-PATHNAME and other primitives"
1967 directory-component)
1969 (defun merge-pathname-directory-components (specified defaults)
1970 "Helper for MERGE-PATHNAMES* that handles directory components"
1971 (let ((directory (normalize-pathname-directory-component specified)))
1972 (ecase (first directory)
1973 ((nil) defaults)
1974 (:absolute specified)
1975 (:relative
1976 (let ((defdir (normalize-pathname-directory-component defaults))
1977 (reldir (cdr directory)))
1978 (cond
1979 ((null defdir)
1980 directory)
1981 ((not (eq :back (first reldir)))
1982 (append defdir reldir))
1984 (loop :with defabs = (first defdir)
1985 :with defrev = (reverse (rest defdir))
1986 :while (and (eq :back (car reldir))
1987 (or (and (eq :absolute defabs) (null defrev))
1988 (stringp (car defrev))))
1989 :do (pop reldir) (pop defrev)
1990 :finally (return (cons defabs (append (reverse defrev) reldir)))))))))))
1992 ;; Giving :unspecific as :type argument to make-pathname is not portable.
1993 ;; See CLHS make-pathname and 19.2.2.2.3.
1994 ;; This will be :unspecific if supported, or NIL if not.
1995 (defparameter *unspecific-pathname-type*
1996 #+(or abcl allegro clozure cmu genera lispworks sbcl scl) :unspecific
1997 #+(or clisp ecl mkcl gcl xcl #|These haven't been tested:|# cormanlisp mcl) nil
1998 "Unspecific type component to use with the underlying implementation's MAKE-PATHNAME")
2000 (defun make-pathname* (&rest keys &key (directory nil)
2001 host (device () #+allegro devicep) name type version defaults
2002 #+scl &allow-other-keys)
2003 "Takes arguments like CL:MAKE-PATHNAME in the CLHS, and
2004 tries hard to make a pathname that will actually behave as documented,
2005 despite the peculiarities of each implementation"
2006 ;; TODO: reimplement defaulting for MCL, whereby an explicit NIL should override the defaults.
2007 (declare (ignorable host device directory name type version defaults))
2008 (apply 'make-pathname
2009 (append
2010 #+allegro (when (and devicep (null device)) `(:device :unspecific))
2011 keys)))
2013 (defun make-pathname-component-logical (x)
2014 "Make a pathname component suitable for use in a logical-pathname"
2015 (typecase x
2016 ((eql :unspecific) nil)
2017 #+clisp (string (string-upcase x))
2018 #+clisp (cons (mapcar 'make-pathname-component-logical x))
2019 (t x)))
2021 (defun make-pathname-logical (pathname host)
2022 "Take a PATHNAME's directory, name, type and version components,
2023 and make a new pathname with corresponding components and specified logical HOST"
2024 (make-pathname*
2025 :host host
2026 :directory (make-pathname-component-logical (pathname-directory pathname))
2027 :name (make-pathname-component-logical (pathname-name pathname))
2028 :type (make-pathname-component-logical (pathname-type pathname))
2029 :version (make-pathname-component-logical (pathname-version pathname))))
2031 (defun merge-pathnames* (specified &optional (defaults *default-pathname-defaults*))
2032 "MERGE-PATHNAMES* is like MERGE-PATHNAMES except that
2033 if the SPECIFIED pathname does not have an absolute directory,
2034 then the HOST and DEVICE both come from the DEFAULTS, whereas
2035 if the SPECIFIED pathname does have an absolute directory,
2036 then the HOST and DEVICE both come from the SPECIFIED pathname.
2037 This is what users want on a modern Unix or Windows operating system,
2038 unlike the MERGE-PATHNAMES behavior.
2039 Also, if either argument is NIL, then the other argument is returned unmodified;
2040 this is unlike MERGE-PATHNAMES which always merges with a pathname,
2041 by default *DEFAULT-PATHNAME-DEFAULTS*, which cannot be NIL."
2042 (when (null specified) (return-from merge-pathnames* defaults))
2043 (when (null defaults) (return-from merge-pathnames* specified))
2044 #+scl
2045 (ext:resolve-pathname specified defaults)
2046 #-scl
2047 (let* ((specified (pathname specified))
2048 (defaults (pathname defaults))
2049 (directory (normalize-pathname-directory-component (pathname-directory specified)))
2050 (name (or (pathname-name specified) (pathname-name defaults)))
2051 (type (or (pathname-type specified) (pathname-type defaults)))
2052 (version (or (pathname-version specified) (pathname-version defaults))))
2053 (labels ((unspecific-handler (p)
2054 (if (typep p 'logical-pathname) #'make-pathname-component-logical #'identity)))
2055 (multiple-value-bind (host device directory unspecific-handler)
2056 (ecase (first directory)
2057 ((:absolute)
2058 (values (pathname-host specified)
2059 (pathname-device specified)
2060 directory
2061 (unspecific-handler specified)))
2062 ((nil :relative)
2063 (values (pathname-host defaults)
2064 (pathname-device defaults)
2065 (merge-pathname-directory-components directory (pathname-directory defaults))
2066 (unspecific-handler defaults))))
2067 (make-pathname* :host host :device device :directory directory
2068 :name (funcall unspecific-handler name)
2069 :type (funcall unspecific-handler type)
2070 :version (funcall unspecific-handler version))))))
2072 (defun logical-pathname-p (x)
2073 "is X a logical-pathname?"
2074 (typep x 'logical-pathname))
2076 (defun physical-pathname-p (x)
2077 "is X a pathname that is not a logical-pathname?"
2078 (and (pathnamep x) (not (logical-pathname-p x))))
2080 (defun physicalize-pathname (x)
2081 "if X is a logical pathname, use translate-logical-pathname on it."
2082 ;; Ought to be the same as translate-logical-pathname, except the latter borks on CLISP
2083 (let ((p (when x (pathname x))))
2084 (if (logical-pathname-p p) (translate-logical-pathname p) p)))
2086 (defun nil-pathname (&optional (defaults *default-pathname-defaults*))
2087 "A pathname that is as neutral as possible for use as defaults
2088 when merging, making or parsing pathnames"
2089 ;; 19.2.2.2.1 says a NIL host can mean a default host;
2090 ;; see also "valid physical pathname host" in the CLHS glossary, that suggests
2091 ;; strings and lists of strings or :unspecific
2092 ;; But CMUCL decides to die on NIL.
2093 ;; MCL has issues with make-pathname, nil and defaulting
2094 (declare (ignorable defaults))
2095 #.`(make-pathname* :directory nil :name nil :type nil :version nil
2096 :device (or #+(and mkcl unix) :unspecific)
2097 :host (or #+cmu lisp::*unix-host* #+(and mkcl unix) "localhost")
2098 #+scl ,@'(:scheme nil :scheme-specific-part nil
2099 :username nil :password nil :parameters nil :query nil :fragment nil)
2100 ;; the default shouldn't matter, but we really want something physical
2101 #-mcl ,@'(:defaults defaults)))
2103 (defvar *nil-pathname* (nil-pathname (physicalize-pathname (user-homedir-pathname)))
2104 "A pathname that is as neutral as possible for use as defaults
2105 when merging, making or parsing pathnames")
2107 (defmacro with-pathname-defaults ((&optional defaults) &body body)
2108 "Execute BODY in a context where the *DEFAULT-PATHNAME-DEFAULTS* are as neutral as possible
2109 when merging, making or parsing pathnames"
2110 `(let ((*default-pathname-defaults* ,(or defaults '*nil-pathname*))) ,@body)))
2113 ;;; Some pathname predicates
2114 (with-upgradability ()
2115 (defun pathname-equal (p1 p2)
2116 "Are the two pathnames P1 and P2 reasonably equal in the paths they denote?"
2117 (when (stringp p1) (setf p1 (pathname p1)))
2118 (when (stringp p2) (setf p2 (pathname p2)))
2119 (flet ((normalize-component (x)
2120 (unless (member x '(nil :unspecific :newest (:relative)) :test 'equal)
2121 x)))
2122 (macrolet ((=? (&rest accessors)
2123 (flet ((frob (x)
2124 (reduce 'list (cons 'normalize-component accessors)
2125 :initial-value x :from-end t)))
2126 `(equal ,(frob 'p1) ,(frob 'p2)))))
2127 (or (and (null p1) (null p2))
2128 (and (pathnamep p1) (pathnamep p2)
2129 (and (=? pathname-host)
2130 #-(and mkcl unix) (=? pathname-device)
2131 (=? normalize-pathname-directory-component pathname-directory)
2132 (=? pathname-name)
2133 (=? pathname-type)
2134 #-mkcl (=? pathname-version)))))))
2136 (defun absolute-pathname-p (pathspec)
2137 "If PATHSPEC is a pathname or namestring object that parses as a pathname
2138 possessing an :ABSOLUTE directory component, return the (parsed) pathname.
2139 Otherwise return NIL"
2140 (and pathspec
2141 (typep pathspec '(or null pathname string))
2142 (let ((pathname (pathname pathspec)))
2143 (and (eq :absolute (car (normalize-pathname-directory-component
2144 (pathname-directory pathname))))
2145 pathname))))
2147 (defun relative-pathname-p (pathspec)
2148 "If PATHSPEC is a pathname or namestring object that parses as a pathname
2149 possessing a :RELATIVE or NIL directory component, return the (parsed) pathname.
2150 Otherwise return NIL"
2151 (and pathspec
2152 (typep pathspec '(or null pathname string))
2153 (let* ((pathname (pathname pathspec))
2154 (directory (normalize-pathname-directory-component
2155 (pathname-directory pathname))))
2156 (when (or (null directory) (eq :relative (car directory)))
2157 pathname))))
2159 (defun hidden-pathname-p (pathname)
2160 "Return a boolean that is true if the pathname is hidden as per Unix style,
2161 i.e. its name starts with a dot."
2162 (and pathname (equal (first-char (pathname-name pathname)) #\.)))
2164 (defun file-pathname-p (pathname)
2165 "Does PATHNAME represent a file, i.e. has a non-null NAME component?
2167 Accepts NIL, a string (converted through PARSE-NAMESTRING) or a PATHNAME.
2169 Note that this does _not_ check to see that PATHNAME points to an
2170 actually-existing file.
2172 Returns the (parsed) PATHNAME when true"
2173 (when pathname
2174 (let* ((pathname (pathname pathname))
2175 (name (pathname-name pathname)))
2176 (when (not (member name '(nil :unspecific "") :test 'equal))
2177 pathname)))))
2180 ;;; Directory pathnames
2181 (with-upgradability ()
2182 (defun pathname-directory-pathname (pathname)
2183 "Returns a new pathname with same HOST, DEVICE, DIRECTORY as PATHNAME,
2184 and NIL NAME, TYPE and VERSION components"
2185 (when pathname
2186 (make-pathname :name nil :type nil :version nil :defaults pathname)))
2188 (defun pathname-parent-directory-pathname (pathname)
2189 "Returns a new pathname that corresponds to the parent of the current pathname's directory,
2190 i.e. removing one level of depth in the DIRECTORY component. e.g. if pathname is
2191 Unix pathname /foo/bar/baz/file.type then return /foo/bar/"
2192 (when pathname
2193 (make-pathname* :name nil :type nil :version nil
2194 :directory (merge-pathname-directory-components
2195 '(:relative :back) (pathname-directory pathname))
2196 :defaults pathname)))
2198 (defun directory-pathname-p (pathname)
2199 "Does PATHNAME represent a directory?
2201 A directory-pathname is a pathname _without_ a filename. The three
2202 ways that the filename components can be missing are for it to be NIL,
2203 :UNSPECIFIC or the empty string.
2205 Note that this does _not_ check to see that PATHNAME points to an
2206 actually-existing directory."
2207 (when pathname
2208 ;; I tried using Allegro's excl:file-directory-p, but this cannot be done,
2209 ;; because it rejects apparently legal pathnames as
2210 ;; ill-formed. [2014/02/10:rpg]
2211 (let ((pathname (pathname pathname)))
2212 (flet ((check-one (x)
2213 (member x '(nil :unspecific) :test 'equal)))
2214 (and (not (wild-pathname-p pathname))
2215 (check-one (pathname-name pathname))
2216 (check-one (pathname-type pathname))
2217 t)))))
2219 (defun ensure-directory-pathname (pathspec &optional (on-error 'error))
2220 "Converts the non-wild pathname designator PATHSPEC to directory form."
2221 (cond
2222 ((stringp pathspec)
2223 (ensure-directory-pathname (pathname pathspec)))
2224 ((not (pathnamep pathspec))
2225 (call-function on-error (compatfmt "~@<Invalid pathname designator ~S~@:>") pathspec))
2226 ((wild-pathname-p pathspec)
2227 (call-function on-error (compatfmt "~@<Can't reliably convert wild pathname ~3i~_~S~@:>") pathspec))
2228 ((directory-pathname-p pathspec)
2229 pathspec)
2231 (make-pathname* :directory (append (or (normalize-pathname-directory-component
2232 (pathname-directory pathspec))
2233 (list :relative))
2234 (list (file-namestring pathspec)))
2235 :name nil :type nil :version nil :defaults pathspec)))))
2238 ;;; Parsing filenames
2239 (with-upgradability ()
2240 (defun split-unix-namestring-directory-components
2241 (unix-namestring &key ensure-directory dot-dot)
2242 "Splits the path string UNIX-NAMESTRING, returning four values:
2243 A flag that is either :absolute or :relative, indicating
2244 how the rest of the values are to be interpreted.
2245 A directory path --- a list of strings and keywords, suitable for
2246 use with MAKE-PATHNAME when prepended with the flag value.
2247 Directory components with an empty name or the name . are removed.
2248 Any directory named .. is read as DOT-DOT, or :BACK if it's NIL (not :UP).
2249 A last-component, either a file-namestring including type extension,
2250 or NIL in the case of a directory pathname.
2251 A flag that is true iff the unix-style-pathname was just
2252 a file-namestring without / path specification.
2253 ENSURE-DIRECTORY forces the namestring to be interpreted as a directory pathname:
2254 the third return value will be NIL, and final component of the namestring
2255 will be treated as part of the directory path.
2257 An empty string is thus read as meaning a pathname object with all fields nil.
2259 Note that : characters will NOT be interpreted as host specification.
2260 Absolute pathnames are only appropriate on Unix-style systems.
2262 The intention of this function is to support structured component names,
2263 e.g., \(:file \"foo/bar\"\), which will be unpacked to relative pathnames."
2264 (check-type unix-namestring string)
2265 (check-type dot-dot (member nil :back :up))
2266 (if (and (not (find #\/ unix-namestring)) (not ensure-directory)
2267 (plusp (length unix-namestring)))
2268 (values :relative () unix-namestring t)
2269 (let* ((components (split-string unix-namestring :separator "/"))
2270 (last-comp (car (last components))))
2271 (multiple-value-bind (relative components)
2272 (if (equal (first components) "")
2273 (if (equal (first-char unix-namestring) #\/)
2274 (values :absolute (cdr components))
2275 (values :relative nil))
2276 (values :relative components))
2277 (setf components (remove-if #'(lambda (x) (member x '("" ".") :test #'equal))
2278 components))
2279 (setf components (substitute (or dot-dot :back) ".." components :test #'equal))
2280 (cond
2281 ((equal last-comp "")
2282 (values relative components nil nil)) ; "" already removed from components
2283 (ensure-directory
2284 (values relative components nil nil))
2286 (values relative (butlast components) last-comp nil)))))))
2288 (defun split-name-type (filename)
2289 "Split a filename into two values NAME and TYPE that are returned.
2290 We assume filename has no directory component.
2291 The last . if any separates name and type from from type,
2292 except that if there is only one . and it is in first position,
2293 the whole filename is the NAME with an empty type.
2294 NAME is always a string.
2295 For an empty type, *UNSPECIFIC-PATHNAME-TYPE* is returned."
2296 (check-type filename string)
2297 (assert (plusp (length filename)))
2298 (destructuring-bind (name &optional (type *unspecific-pathname-type*))
2299 (split-string filename :max 2 :separator ".")
2300 (if (equal name "")
2301 (values filename *unspecific-pathname-type*)
2302 (values name type))))
2304 (defun parse-unix-namestring (name &rest keys &key type defaults dot-dot ensure-directory
2305 &allow-other-keys)
2306 "Coerce NAME into a PATHNAME using standard Unix syntax.
2308 Unix syntax is used whether or not the underlying system is Unix;
2309 on such non-Unix systems it is only usable but for relative pathnames;
2310 but especially to manipulate relative pathnames portably, it is of crucial
2311 to possess a portable pathname syntax independent of the underlying OS.
2312 This is what PARSE-UNIX-NAMESTRING provides, and why we use it in ASDF.
2314 When given a PATHNAME object, just return it untouched.
2315 When given NIL, just return NIL.
2316 When given a non-null SYMBOL, first downcase its name and treat it as a string.
2317 When given a STRING, portably decompose it into a pathname as below.
2319 #\\/ separates directory components.
2321 The last #\\/-separated substring is interpreted as follows:
2322 1- If TYPE is :DIRECTORY or ENSURE-DIRECTORY is true,
2323 the string is made the last directory component, and NAME and TYPE are NIL.
2324 if the string is empty, it's the empty pathname with all slots NIL.
2325 2- If TYPE is NIL, the substring is a file-namestring, and its NAME and TYPE
2326 are separated by SPLIT-NAME-TYPE.
2327 3- If TYPE is a string, it is the given TYPE, and the whole string is the NAME.
2329 Directory components with an empty name the name . are removed.
2330 Any directory named .. is read as DOT-DOT,
2331 which must be one of :BACK or :UP and defaults to :BACK.
2333 HOST, DEVICE and VERSION components are taken from DEFAULTS,
2334 which itself defaults to *NIL-PATHNAME*, also used if DEFAULTS is NIL.
2335 No host or device can be specified in the string itself,
2336 which makes it unsuitable for absolute pathnames outside Unix.
2338 For relative pathnames, these components (and hence the defaults) won't matter
2339 if you use MERGE-PATHNAMES* but will matter if you use MERGE-PATHNAMES,
2340 which is an important reason to always use MERGE-PATHNAMES*.
2342 Arbitrary keys are accepted, and the parse result is passed to ENSURE-PATHNAME
2343 with those keys, removing TYPE DEFAULTS and DOT-DOT.
2344 When you're manipulating pathnames that are supposed to make sense portably
2345 even though the OS may not be Unixish, we recommend you use :WANT-RELATIVE T
2346 to throw an error if the pathname is absolute"
2347 (block nil
2348 (check-type type (or null string (eql :directory)))
2349 (when ensure-directory
2350 (setf type :directory))
2351 (etypecase name
2352 ((or null pathname) (return name))
2353 (symbol
2354 (setf name (string-downcase name)))
2355 (string))
2356 (multiple-value-bind (relative path filename file-only)
2357 (split-unix-namestring-directory-components
2358 name :dot-dot dot-dot :ensure-directory (eq type :directory))
2359 (multiple-value-bind (name type)
2360 (cond
2361 ((or (eq type :directory) (null filename))
2362 (values nil nil))
2363 (type
2364 (values filename type))
2366 (split-name-type filename)))
2367 (apply 'ensure-pathname
2368 (make-pathname*
2369 :directory (unless file-only (cons relative path))
2370 :name name :type type
2371 :defaults (or #-mcl defaults *nil-pathname*))
2372 (remove-plist-keys '(:type :dot-dot :defaults) keys))))))
2374 (defun unix-namestring (pathname)
2375 "Given a non-wild PATHNAME, return a Unix-style namestring for it.
2376 If the PATHNAME is NIL or a STRING, return it unchanged.
2378 This only considers the DIRECTORY, NAME and TYPE components of the pathname.
2379 This is a portable solution for representing relative pathnames,
2380 But unless you are running on a Unix system, it is not a general solution
2381 to representing native pathnames.
2383 An error is signaled if the argument is not NULL, a STRING or a PATHNAME,
2384 or if it is a PATHNAME but some of its components are not recognized."
2385 (etypecase pathname
2386 ((or null string) pathname)
2387 (pathname
2388 (with-output-to-string (s)
2389 (flet ((err () #+lispworks (describe pathname) (error "Not a valid unix-namestring ~S" pathname)))
2390 (let* ((dir (normalize-pathname-directory-component (pathname-directory pathname)))
2391 (name (pathname-name pathname))
2392 (name (and (not (eq name :unspecific)) name))
2393 (type (pathname-type pathname))
2394 (type (and (not (eq type :unspecific)) type)))
2395 (cond
2396 ((member dir '(nil :unspecific)))
2397 ((eq dir '(:relative)) (princ "./" s))
2398 ((consp dir)
2399 (destructuring-bind (relabs &rest dirs) dir
2400 (or (member relabs '(:relative :absolute)) (err))
2401 (when (eq relabs :absolute) (princ #\/ s))
2402 (loop :for x :in dirs :do
2403 (cond
2404 ((member x '(:back :up)) (princ "../" s))
2405 ((equal x "") (err))
2406 ;;((member x '("." "..") :test 'equal) (err))
2407 ((stringp x) (format s "~A/" x))
2408 (t (err))))))
2409 (t (err)))
2410 (cond
2411 (name
2412 (unless (and (stringp name) (or (null type) (stringp type))) (err))
2413 (format s "~A~@[.~A~]" name type))
2415 (or (null type) (err)))))))))))
2417 ;;; Absolute and relative pathnames
2418 (with-upgradability ()
2419 (defun subpathname (pathname subpath &key type)
2420 "This function takes a PATHNAME and a SUBPATH and a TYPE.
2421 If SUBPATH is already a PATHNAME object (not namestring),
2422 and is an absolute pathname at that, it is returned unchanged;
2423 otherwise, SUBPATH is turned into a relative pathname with given TYPE
2424 as per PARSE-UNIX-NAMESTRING with :WANT-RELATIVE T :TYPE TYPE,
2425 then it is merged with the PATHNAME-DIRECTORY-PATHNAME of PATHNAME."
2426 (or (and (pathnamep subpath) (absolute-pathname-p subpath))
2427 (merge-pathnames* (parse-unix-namestring subpath :type type :want-relative t)
2428 (pathname-directory-pathname pathname))))
2430 (defun subpathname* (pathname subpath &key type)
2431 "returns NIL if the base pathname is NIL, otherwise like SUBPATHNAME."
2432 (and pathname
2433 (subpathname (ensure-directory-pathname pathname) subpath :type type)))
2435 (defun pathname-root (pathname)
2436 "return the root directory for the host and device of given PATHNAME"
2437 (make-pathname* :directory '(:absolute)
2438 :name nil :type nil :version nil
2439 :defaults pathname ;; host device, and on scl, *some*
2440 ;; scheme-specific parts: port username password, not others:
2441 . #.(or #+scl '(:parameters nil :query nil :fragment nil))))
2443 (defun pathname-host-pathname (pathname)
2444 "return a pathname with the same host as given PATHNAME, and all other fields NIL"
2445 (make-pathname* :directory nil
2446 :name nil :type nil :version nil :device nil
2447 :defaults pathname ;; host device, and on scl, *some*
2448 ;; scheme-specific parts: port username password, not others:
2449 . #.(or #+scl '(:parameters nil :query nil :fragment nil))))
2451 (defun ensure-absolute-pathname (path &optional defaults (on-error 'error))
2452 "Given a pathname designator PATH, return an absolute pathname as specified by PATH
2453 considering the DEFAULTS, or, if not possible, use CALL-FUNCTION on the specified ON-ERROR behavior,
2454 with a format control-string and other arguments as arguments"
2455 (cond
2456 ((absolute-pathname-p path))
2457 ((stringp path) (ensure-absolute-pathname (pathname path) defaults on-error))
2458 ((not (pathnamep path)) (call-function on-error "not a valid pathname designator ~S" path))
2459 ((let ((default-pathname (if (pathnamep defaults) defaults (call-function defaults))))
2460 (or (if (absolute-pathname-p default-pathname)
2461 (absolute-pathname-p (merge-pathnames* path default-pathname))
2462 (call-function on-error "Default pathname ~S is not an absolute pathname"
2463 default-pathname))
2464 (call-function on-error "Failed to merge ~S with ~S into an absolute pathname"
2465 path default-pathname))))
2466 (t (call-function on-error
2467 "Cannot ensure ~S is evaluated as an absolute pathname with defaults ~S"
2468 path defaults))))
2470 (defun subpathp (maybe-subpath base-pathname)
2471 "if MAYBE-SUBPATH is a pathname that is under BASE-PATHNAME, return a pathname object that
2472 when used with MERGE-PATHNAMES* with defaults BASE-PATHNAME, returns MAYBE-SUBPATH."
2473 (and (pathnamep maybe-subpath) (pathnamep base-pathname)
2474 (absolute-pathname-p maybe-subpath) (absolute-pathname-p base-pathname)
2475 (directory-pathname-p base-pathname) (not (wild-pathname-p base-pathname))
2476 (pathname-equal (pathname-root maybe-subpath) (pathname-root base-pathname))
2477 (with-pathname-defaults ()
2478 (let ((enough (enough-namestring maybe-subpath base-pathname)))
2479 (and (relative-pathname-p enough) (pathname enough))))))
2481 (defun enough-pathname (maybe-subpath base-pathname)
2482 "if MAYBE-SUBPATH is a pathname that is under BASE-PATHNAME, return a pathname object that
2483 when used with MERGE-PATHNAMES* with defaults BASE-PATHNAME, returns MAYBE-SUBPATH."
2484 (let ((sub (when maybe-subpath (pathname maybe-subpath)))
2485 (base (when base-pathname (ensure-absolute-pathname (pathname base-pathname)))))
2486 (or (and base (subpathp sub base)) sub)))
2488 (defun call-with-enough-pathname (maybe-subpath defaults-pathname thunk)
2489 "In a context where *DEFAULT-PATHNAME-DEFAULTS* is bound to DEFAULTS-PATHNAME (if not null,
2490 or else to its current value), call THUNK with ENOUGH-PATHNAME for MAYBE-SUBPATH
2491 given DEFAULTS-PATHNAME as a base pathname."
2492 (let ((enough (enough-pathname maybe-subpath defaults-pathname))
2493 (*default-pathname-defaults* (or defaults-pathname *default-pathname-defaults*)))
2494 (funcall thunk enough)))
2496 (defmacro with-enough-pathname ((pathname-var &key (pathname pathname-var)
2497 (defaults *default-pathname-defaults*))
2498 &body body)
2499 "Shorthand syntax for CALL-WITH-ENOUGH-PATHNAME"
2500 `(call-with-enough-pathname ,pathname ,defaults #'(lambda (,pathname-var) ,@body))))
2503 ;;; Wildcard pathnames
2504 (with-upgradability ()
2505 (defparameter *wild* (or #+cormanlisp "*" :wild)
2506 "Wild component for use with MAKE-PATHNAME")
2507 (defparameter *wild-directory-component* (or :wild)
2508 "Wild directory component for use with MAKE-PATHNAME")
2509 (defparameter *wild-inferiors-component* (or :wild-inferiors)
2510 "Wild-inferiors directory component for use with MAKE-PATHNAME")
2511 (defparameter *wild-file*
2512 (make-pathname :directory nil :name *wild* :type *wild*
2513 :version (or #-(or allegro abcl xcl) *wild*))
2514 "A pathname object with wildcards for matching any file in a given directory")
2515 (defparameter *wild-directory*
2516 (make-pathname* :directory `(:relative ,*wild-directory-component*)
2517 :name nil :type nil :version nil)
2518 "A pathname object with wildcards for matching any subdirectory")
2519 (defparameter *wild-inferiors*
2520 (make-pathname* :directory `(:relative ,*wild-inferiors-component*)
2521 :name nil :type nil :version nil)
2522 "A pathname object with wildcards for matching any recursive subdirectory")
2523 (defparameter *wild-path*
2524 (merge-pathnames* *wild-file* *wild-inferiors*)
2525 "A pathname object with wildcards for matching any file in any recursive subdirectory")
2527 (defun wilden (path)
2528 "From a pathname, return a wildcard pathname matching any file in any subdirectory of given pathname's directory"
2529 (merge-pathnames* *wild-path* path)))
2532 ;;; Translate a pathname
2533 (with-upgradability ()
2534 (defun relativize-directory-component (directory-component)
2535 "Given the DIRECTORY-COMPONENT of a pathname, return an otherwise similar relative directory component"
2536 (let ((directory (normalize-pathname-directory-component directory-component)))
2537 (cond
2538 ((stringp directory)
2539 (list :relative directory))
2540 ((eq (car directory) :absolute)
2541 (cons :relative (cdr directory)))
2543 directory))))
2545 (defun relativize-pathname-directory (pathspec)
2546 "Given a PATHNAME, return a relative pathname with otherwise the same components"
2547 (let ((p (pathname pathspec)))
2548 (make-pathname*
2549 :directory (relativize-directory-component (pathname-directory p))
2550 :defaults p)))
2552 (defun directory-separator-for-host (&optional (pathname *default-pathname-defaults*))
2553 "Given a PATHNAME, return the character used to delimit directory names on this host and device."
2554 (let ((foo (make-pathname* :directory '(:absolute "FOO") :defaults pathname)))
2555 (last-char (namestring foo))))
2557 #-scl
2558 (defun directorize-pathname-host-device (pathname)
2559 "Given a PATHNAME, return a pathname that has representations of its HOST and DEVICE components
2560 added to its DIRECTORY component. This is useful for output translations."
2561 #+(or unix abcl)
2562 (when (and #+abcl (os-unix-p) (physical-pathname-p pathname))
2563 (return-from directorize-pathname-host-device pathname))
2564 (let* ((root (pathname-root pathname))
2565 (wild-root (wilden root))
2566 (absolute-pathname (merge-pathnames* pathname root))
2567 (separator (directory-separator-for-host root))
2568 (root-namestring (namestring root))
2569 (root-string
2570 (substitute-if #\/
2571 #'(lambda (x) (or (eql x #\:)
2572 (eql x separator)))
2573 root-namestring)))
2574 (multiple-value-bind (relative path filename)
2575 (split-unix-namestring-directory-components root-string :ensure-directory t)
2576 (declare (ignore relative filename))
2577 (let ((new-base
2578 (make-pathname* :defaults root :directory `(:absolute ,@path))))
2579 (translate-pathname absolute-pathname wild-root (wilden new-base))))))
2581 #+scl
2582 (defun directorize-pathname-host-device (pathname)
2583 (let ((scheme (ext:pathname-scheme pathname))
2584 (host (pathname-host pathname))
2585 (port (ext:pathname-port pathname))
2586 (directory (pathname-directory pathname)))
2587 (flet ((specificp (x) (and x (not (eq x :unspecific)))))
2588 (if (or (specificp port)
2589 (and (specificp host) (plusp (length host)))
2590 (specificp scheme))
2591 (let ((prefix ""))
2592 (when (specificp port)
2593 (setf prefix (format nil ":~D" port)))
2594 (when (and (specificp host) (plusp (length host)))
2595 (setf prefix (strcat host prefix)))
2596 (setf prefix (strcat ":" prefix))
2597 (when (specificp scheme)
2598 (setf prefix (strcat scheme prefix)))
2599 (assert (and directory (eq (first directory) :absolute)))
2600 (make-pathname* :directory `(:absolute ,prefix ,@(rest directory))
2601 :defaults pathname)))
2602 pathname)))
2604 (defun* (translate-pathname*) (path absolute-source destination &optional root source)
2605 "A wrapper around TRANSLATE-PATHNAME to be used by the ASDF output-translations facility.
2606 PATH is the pathname to be translated.
2607 ABSOLUTE-SOURCE is an absolute pathname to use as source for translate-pathname,
2608 DESTINATION is either a function, to be called with PATH and ABSOLUTE-SOURCE,
2609 or a relative pathname, to be merged with ROOT and used as destination for translate-pathname
2610 or an absolute pathname, to be used as destination for translate-pathname.
2611 In that last case, if ROOT is non-NIL, PATH is first transformated by DIRECTORIZE-PATHNAME-HOST-DEVICE."
2612 (declare (ignore source))
2613 (cond
2614 ((functionp destination)
2615 (funcall destination path absolute-source))
2616 ((eq destination t)
2617 path)
2618 ((not (pathnamep destination))
2619 (error "Invalid destination"))
2620 ((not (absolute-pathname-p destination))
2621 (translate-pathname path absolute-source (merge-pathnames* destination root)))
2622 (root
2623 (translate-pathname (directorize-pathname-host-device path) absolute-source destination))
2625 (translate-pathname path absolute-source destination))))
2627 (defvar *output-translation-function* 'identity
2628 "Hook for output translations.
2630 This function needs to be idempotent, so that actions can work
2631 whether their inputs were translated or not,
2632 which they will be if we are composing operations. e.g. if some
2633 create-lisp-op creates a lisp file from some higher-level input,
2634 you need to still be able to use compile-op on that lisp file."))
2636 ;;;; -------------------------------------------------------------------------
2637 ;;;; Portability layer around Common Lisp filesystem access
2639 (uiop/package:define-package :uiop/filesystem
2640 (:nicknames :asdf/filesystem)
2641 (:recycle :uiop/filesystem :asdf/pathname :asdf)
2642 (:use :uiop/common-lisp :uiop/package :uiop/utility :uiop/os :uiop/pathname)
2643 (:export
2644 ;; Native namestrings
2645 #:native-namestring #:parse-native-namestring
2646 ;; Probing the filesystem
2647 #:truename* #:safe-file-write-date #:probe-file* #:directory-exists-p #:file-exists-p
2648 #:directory* #:filter-logical-directory-results #:directory-files #:subdirectories
2649 #:collect-sub*directories
2650 ;; Resolving symlinks somewhat
2651 #:truenamize #:resolve-symlinks #:*resolve-symlinks* #:resolve-symlinks*
2652 ;; merging with cwd
2653 #:get-pathname-defaults #:call-with-current-directory #:with-current-directory
2654 ;; Environment pathnames
2655 #:inter-directory-separator #:split-native-pathnames-string
2656 #:getenv-pathname #:getenv-pathnames
2657 #:getenv-absolute-directory #:getenv-absolute-directories
2658 #:lisp-implementation-directory #:lisp-implementation-pathname-p
2659 ;; Simple filesystem operations
2660 #:ensure-all-directories-exist
2661 #:rename-file-overwriting-target
2662 #:delete-file-if-exists #:delete-empty-directory #:delete-directory-tree))
2663 (in-package :uiop/filesystem)
2665 ;;; Native namestrings, as seen by the operating system calls rather than Lisp
2666 (with-upgradability ()
2667 (defun native-namestring (x)
2668 "From a non-wildcard CL pathname, a return namestring suitable for passing to the operating system"
2669 (when x
2670 (let ((p (pathname x)))
2671 #+clozure (with-pathname-defaults () (ccl:native-translated-namestring p)) ; see ccl bug 978
2672 #+(or cmu scl) (ext:unix-namestring p nil)
2673 #+sbcl (sb-ext:native-namestring p)
2674 #-(or clozure cmu sbcl scl)
2675 (if (os-unix-p) (unix-namestring p)
2676 (namestring p)))))
2678 (defun parse-native-namestring (string &rest constraints &key ensure-directory &allow-other-keys)
2679 "From a native namestring suitable for use by the operating system, return
2680 a CL pathname satisfying all the specified constraints as per ENSURE-PATHNAME"
2681 (check-type string (or string null))
2682 (let* ((pathname
2683 (when string
2684 (with-pathname-defaults ()
2685 #+clozure (ccl:native-to-pathname string)
2686 #+sbcl (sb-ext:parse-native-namestring string)
2687 #-(or clozure sbcl)
2688 (if (os-unix-p)
2689 (parse-unix-namestring string :ensure-directory ensure-directory)
2690 (parse-namestring string)))))
2691 (pathname
2692 (if ensure-directory
2693 (and pathname (ensure-directory-pathname pathname))
2694 pathname)))
2695 (apply 'ensure-pathname pathname constraints))))
2698 ;;; Probing the filesystem
2699 (with-upgradability ()
2700 (defun truename* (p)
2701 "Nicer variant of TRUENAME that plays well with NIL and avoids logical pathname contexts"
2702 ;; avoids both logical-pathname merging and physical resolution issues
2703 (and p (handler-case (with-pathname-defaults () (truename p)) (file-error () nil))))
2705 (defun safe-file-write-date (pathname)
2706 "Safe variant of FILE-WRITE-DATE that may return NIL rather than raise an error."
2707 ;; If FILE-WRITE-DATE returns NIL, it's possible that
2708 ;; the user or some other agent has deleted an input file.
2709 ;; Also, generated files will not exist at the time planning is done
2710 ;; and calls compute-action-stamp which calls safe-file-write-date.
2711 ;; So it is very possible that we can't get a valid file-write-date,
2712 ;; and we can survive and we will continue the planning
2713 ;; as if the file were very old.
2714 ;; (or should we treat the case in a different, special way?)
2715 (and pathname
2716 (handler-case (file-write-date (physicalize-pathname pathname))
2717 (file-error () nil))))
2719 (defun probe-file* (p &key truename)
2720 "when given a pathname P (designated by a string as per PARSE-NAMESTRING),
2721 probes the filesystem for a file or directory with given pathname.
2722 If it exists, return its truename is ENSURE-PATHNAME is true,
2723 or the original (parsed) pathname if it is false (the default)."
2724 (with-pathname-defaults () ;; avoids logical-pathname issues on some implementations
2725 (etypecase p
2726 (null nil)
2727 (string (probe-file* (parse-namestring p) :truename truename))
2728 (pathname
2729 (and (not (wild-pathname-p p))
2730 (handler-case
2732 #+allegro
2733 (probe-file p :follow-symlinks truename)
2734 #+gcl
2735 (if truename
2736 (truename* p)
2737 (let ((kind (car (si::stat p))))
2738 (when (eq kind :link)
2739 (setf kind (ignore-errors (car (si::stat (truename* p))))))
2740 (ecase kind
2741 ((nil) nil)
2742 ((:file :link)
2743 (cond
2744 ((file-pathname-p p) p)
2745 ((directory-pathname-p p)
2746 (subpathname p (car (last (pathname-directory p)))))))
2747 (:directory (ensure-directory-pathname p)))))
2748 #+clisp
2749 #.(flet ((probe (probe)
2750 `(let ((foundtrue ,probe))
2751 (cond
2752 (truename foundtrue)
2753 (foundtrue p)))))
2754 (let* ((fs (or #-os-windows (find-symbol* '#:file-stat :posix nil)))
2755 (pp (find-symbol* '#:probe-pathname :ext nil))
2756 (resolve (if pp
2757 `(ignore-errors (,pp p))
2758 '(or (truename* p)
2759 (truename* (ignore-errors (ensure-directory-pathname p)))))))
2760 (if fs
2761 `(if truename
2762 ,resolve
2763 (and (ignore-errors (,fs p)) p))
2764 (probe resolve))))
2765 #-(or allegro clisp gcl)
2766 (if truename
2767 (probe-file p)
2768 (ignore-errors
2769 (let ((pp (physicalize-pathname p)))
2770 (and
2771 #+(or cmu scl) (unix:unix-stat (ext:unix-namestring pp))
2772 #+(and lispworks unix) (system:get-file-stat pp)
2773 #+sbcl (sb-unix:unix-stat (sb-ext:native-namestring pp))
2774 #-(or cmu (and lispworks unix) sbcl scl) (file-write-date pp)
2775 p)))))
2776 (file-error () nil)))))))
2778 (defun directory-exists-p (x)
2779 "Is X the name of a directory that exists on the filesystem?"
2780 #+allegro
2781 (excl:probe-directory x)
2782 #+clisp
2783 (handler-case (ext:probe-directory x)
2784 (sys::simple-file-error ()
2785 nil))
2786 #-(or allegro clisp)
2787 (let ((p (probe-file* x :truename t)))
2788 (and (directory-pathname-p p) p)))
2790 (defun file-exists-p (x)
2791 "Is X the name of a file that exists on the filesystem?"
2792 (let ((p (probe-file* x :truename t)))
2793 (and (file-pathname-p p) p)))
2795 (defun directory* (pathname-spec &rest keys &key &allow-other-keys)
2796 "Return a list of the entries in a directory by calling DIRECTORY.
2797 Try to override the defaults to not resolving symlinks, if implementation allows."
2798 (apply 'directory pathname-spec
2799 (append keys '#.(or #+allegro '(:directories-are-files nil :follow-symbolic-links nil)
2800 #+(or clozure digitool) '(:follow-links nil)
2801 #+clisp '(:circle t :if-does-not-exist :ignore)
2802 #+(or cmu scl) '(:follow-links nil :truenamep nil)
2803 #+lispworks '(:link-transparency nil)
2804 #+sbcl (when (find-symbol* :resolve-symlinks '#:sb-impl nil)
2805 '(:resolve-symlinks nil))))))
2807 (defun filter-logical-directory-results (directory entries merger)
2808 "Given ENTRIES in a DIRECTORY, remove if the directory is logical
2809 the entries which are physical yet when transformed by MERGER have a different TRUENAME.
2810 This function is used as a helper to DIRECTORY-FILES to avoid invalid entries when using logical-pathnames."
2811 (remove-duplicates ;; on CLISP, querying ~/ will return duplicates
2812 (if (logical-pathname-p directory)
2813 ;; Try hard to not resolve logical-pathname into physical pathnames;
2814 ;; otherwise logical-pathname users/lovers will be disappointed.
2815 ;; If directory* could use some implementation-dependent magic,
2816 ;; we will have logical pathnames already; otherwise,
2817 ;; we only keep pathnames for which specifying the name and
2818 ;; translating the LPN commute.
2819 (loop :for f :in entries
2820 :for p = (or (and (logical-pathname-p f) f)
2821 (let* ((u (ignore-errors (call-function merger f))))
2822 ;; The first u avoids a cumbersome (truename u) error.
2823 ;; At this point f should already be a truename,
2824 ;; but isn't quite in CLISP, for it doesn't have :version :newest
2825 (and u (equal (truename* u) (truename* f)) u)))
2826 :when p :collect p)
2827 entries)
2828 :test 'pathname-equal))
2831 (defun directory-files (directory &optional (pattern *wild-file*))
2832 "Return a list of the files in a directory according to the PATTERN.
2833 Subdirectories should NOT be returned.
2834 PATTERN defaults to a pattern carefully chosen based on the implementation;
2835 override the default at your own risk.
2836 DIRECTORY-FILES tries NOT to resolve symlinks if the implementation
2837 permits this."
2838 (let ((dir (pathname directory)))
2839 (when (logical-pathname-p dir)
2840 ;; Because of the filtering we do below,
2841 ;; logical pathnames have restrictions on wild patterns.
2842 ;; Not that the results are very portable when you use these patterns on physical pathnames.
2843 (when (wild-pathname-p dir)
2844 (error "Invalid wild pattern in logical directory ~S" directory))
2845 (unless (member (pathname-directory pattern) '(() (:relative)) :test 'equal)
2846 (error "Invalid file pattern ~S for logical directory ~S" pattern directory))
2847 (setf pattern (make-pathname-logical pattern (pathname-host dir))))
2848 (let* ((pat (merge-pathnames* pattern dir))
2849 (entries (append (ignore-errors (directory* pat))
2850 #+(or clisp gcl)
2851 (when (equal :wild (pathname-type pattern))
2852 (ignore-errors (directory* (make-pathname :type nil :defaults pat)))))))
2853 (remove-if 'directory-pathname-p
2854 (filter-logical-directory-results
2855 directory entries
2856 #'(lambda (f)
2857 (make-pathname :defaults dir
2858 :name (make-pathname-component-logical (pathname-name f))
2859 :type (make-pathname-component-logical (pathname-type f))
2860 :version (make-pathname-component-logical (pathname-version f)))))))))
2862 (defun subdirectories (directory)
2863 "Given a DIRECTORY pathname designator, return a list of the subdirectories under it."
2864 (let* ((directory (ensure-directory-pathname directory))
2865 #-(or abcl cormanlisp genera xcl)
2866 (wild (merge-pathnames*
2867 #-(or abcl allegro cmu lispworks sbcl scl xcl)
2868 *wild-directory*
2869 #+(or abcl allegro cmu lispworks sbcl scl xcl) "*.*"
2870 directory))
2871 (dirs
2872 #-(or abcl cormanlisp genera xcl)
2873 (ignore-errors
2874 (directory* wild . #.(or #+clozure '(:directories t :files nil)
2875 #+mcl '(:directories t))))
2876 #+(or abcl xcl) (system:list-directory directory)
2877 #+cormanlisp (cl::directory-subdirs directory)
2878 #+genera (fs:directory-list directory))
2879 #+(or abcl allegro cmu genera lispworks sbcl scl xcl)
2880 (dirs (loop :for x :in dirs
2881 :for d = #+(or abcl xcl) (extensions:probe-directory x)
2882 #+allegro (excl:probe-directory x)
2883 #+(or cmu sbcl scl) (directory-pathname-p x)
2884 #+genera (getf (cdr x) :directory)
2885 #+lispworks (lw:file-directory-p x)
2886 :when d :collect #+(or abcl allegro xcl) d
2887 #+genera (ensure-directory-pathname (first x))
2888 #+(or cmu lispworks sbcl scl) x)))
2889 (filter-logical-directory-results
2890 directory dirs
2891 (let ((prefix (or (normalize-pathname-directory-component (pathname-directory directory))
2892 '(:absolute)))) ; because allegro returns NIL for #p"FOO:"
2893 #'(lambda (d)
2894 (let ((dir (normalize-pathname-directory-component (pathname-directory d))))
2895 (and (consp dir) (consp (cdr dir))
2896 (make-pathname
2897 :defaults directory :name nil :type nil :version nil
2898 :directory (append prefix (make-pathname-component-logical (last dir)))))))))))
2900 (defun collect-sub*directories (directory collectp recursep collector)
2901 "Given a DIRECTORY, call-function the COLLECTOR function designator
2902 on the directory if COLLECTP returns true when CALL-FUNCTION'ed with the directory,
2903 and recurse each of its subdirectories on which the RECURSEP returns true when CALL-FUNCTION'ed with them."
2904 (when (call-function collectp directory)
2905 (call-function collector directory))
2906 (dolist (subdir (subdirectories directory))
2907 (when (call-function recursep subdir)
2908 (collect-sub*directories subdir collectp recursep collector)))))
2910 ;;; Resolving symlinks somewhat
2911 (with-upgradability ()
2912 (defun truenamize (pathname)
2913 "Resolve as much of a pathname as possible"
2914 (block nil
2915 (when (typep pathname '(or null logical-pathname)) (return pathname))
2916 (let ((p pathname))
2917 (unless (absolute-pathname-p p)
2918 (setf p (or (absolute-pathname-p (ensure-absolute-pathname p 'get-pathname-defaults nil))
2919 (return p))))
2920 (when (logical-pathname-p p) (return p))
2921 (let ((found (probe-file* p :truename t)))
2922 (when found (return found)))
2923 (let* ((directory (normalize-pathname-directory-component (pathname-directory p)))
2924 (up-components (reverse (rest directory)))
2925 (down-components ()))
2926 (assert (eq :absolute (first directory)))
2927 (loop :while up-components :do
2928 (if-let (parent
2929 (ignore-errors
2930 (probe-file* (make-pathname* :directory `(:absolute ,@(reverse up-components))
2931 :name nil :type nil :version nil :defaults p))))
2932 (if-let (simplified
2933 (ignore-errors
2934 (merge-pathnames*
2935 (make-pathname* :directory `(:relative ,@down-components)
2936 :defaults p)
2937 (ensure-directory-pathname parent))))
2938 (return simplified)))
2939 (push (pop up-components) down-components)
2940 :finally (return p))))))
2942 (defun resolve-symlinks (path)
2943 "Do a best effort at resolving symlinks in PATH, returning a partially or totally resolved PATH."
2944 #-allegro (truenamize path)
2945 #+allegro
2946 (if (physical-pathname-p path)
2947 (or (ignore-errors (excl:pathname-resolve-symbolic-links path)) path)
2948 path))
2950 (defvar *resolve-symlinks* t
2951 "Determine whether or not ASDF resolves symlinks when defining systems.
2952 Defaults to T.")
2954 (defun resolve-symlinks* (path)
2955 "RESOLVE-SYMLINKS in PATH iff *RESOLVE-SYMLINKS* is T (the default)."
2956 (if *resolve-symlinks*
2957 (and path (resolve-symlinks path))
2958 path)))
2961 ;;; Check pathname constraints
2962 (with-upgradability ()
2963 (defun ensure-pathname
2964 (pathname &key
2965 on-error
2966 defaults type dot-dot namestring
2967 want-pathname
2968 want-logical want-physical ensure-physical
2969 want-relative want-absolute ensure-absolute ensure-subpath
2970 want-non-wild want-wild wilden
2971 want-file want-directory ensure-directory
2972 want-existing ensure-directories-exist
2973 truename resolve-symlinks truenamize
2974 &aux (p pathname)) ;; mutable working copy, preserve original
2975 "Coerces its argument into a PATHNAME,
2976 optionally doing some transformations and checking specified constraints.
2978 If the argument is NIL, then NIL is returned unless the WANT-PATHNAME constraint is specified.
2980 If the argument is a STRING, it is first converted to a pathname via
2981 PARSE-UNIX-NAMESTRING, PARSE-NAMESTRING or PARSE-NATIVE-NAMESTRING respectively
2982 depending on the NAMESTRING argument being :UNIX, :LISP or :NATIVE respectively,
2983 or else by using CALL-FUNCTION on the NAMESTRING argument;
2984 if :UNIX is specified (or NIL, the default, which specifies the same thing),
2985 then PARSE-UNIX-NAMESTRING it is called with the keywords
2986 DEFAULTS TYPE DOT-DOT ENSURE-DIRECTORY WANT-RELATIVE, and
2987 the result is optionally merged into the DEFAULTS if ENSURE-ABSOLUTE is true.
2989 The pathname passed or resulting from parsing the string
2990 is then subjected to all the checks and transformations below are run.
2992 Each non-nil constraint argument can be one of the symbols T, ERROR, CERROR or IGNORE.
2993 The boolean T is an alias for ERROR.
2994 ERROR means that an error will be raised if the constraint is not satisfied.
2995 CERROR means that an continuable error will be raised if the constraint is not satisfied.
2996 IGNORE means just return NIL instead of the pathname.
2998 The ON-ERROR argument, if not NIL, is a function designator (as per CALL-FUNCTION)
2999 that will be called with the the following arguments:
3000 a generic format string for ensure pathname, the pathname,
3001 the keyword argument corresponding to the failed check or transformation,
3002 a format string for the reason ENSURE-PATHNAME failed,
3003 and a list with arguments to that format string.
3004 If ON-ERROR is NIL, ERROR is used instead, which does the right thing.
3005 You could also pass (CERROR \"CONTINUE DESPITE FAILED CHECK\").
3007 The transformations and constraint checks are done in this order,
3008 which is also the order in the lambda-list:
3010 WANT-PATHNAME checks that pathname (after parsing if needed) is not null.
3011 Otherwise, if the pathname is NIL, ensure-pathname returns NIL.
3012 WANT-LOGICAL checks that pathname is a LOGICAL-PATHNAME
3013 WANT-PHYSICAL checks that pathname is not a LOGICAL-PATHNAME
3014 ENSURE-PHYSICAL ensures that pathname is physical via TRANSLATE-LOGICAL-PATHNAME
3015 WANT-RELATIVE checks that pathname has a relative directory component
3016 WANT-ABSOLUTE checks that pathname does have an absolute directory component
3017 ENSURE-ABSOLUTE merges with the DEFAULTS, then checks again
3018 that the result absolute is an absolute pathname indeed.
3019 ENSURE-SUBPATH checks that the pathname is a subpath of the DEFAULTS.
3020 WANT-FILE checks that pathname has a non-nil FILE component
3021 WANT-DIRECTORY checks that pathname has nil FILE and TYPE components
3022 ENSURE-DIRECTORY uses ENSURE-DIRECTORY-PATHNAME to interpret
3023 any file and type components as being actually a last directory component.
3024 WANT-NON-WILD checks that pathname is not a wild pathname
3025 WANT-WILD checks that pathname is a wild pathname
3026 WILDEN merges the pathname with **/*.*.* if it is not wild
3027 WANT-EXISTING checks that a file (or directory) exists with that pathname.
3028 ENSURE-DIRECTORIES-EXIST creates any parent directory with ENSURE-DIRECTORIES-EXIST.
3029 TRUENAME replaces the pathname by its truename, or errors if not possible.
3030 RESOLVE-SYMLINKS replaces the pathname by a variant with symlinks resolved by RESOLVE-SYMLINKS.
3031 TRUENAMIZE uses TRUENAMIZE to resolve as many symlinks as possible."
3032 (block nil
3033 (flet ((report-error (keyword description &rest arguments)
3034 (call-function (or on-error 'error)
3035 "Invalid pathname ~S: ~*~?"
3036 pathname keyword description arguments)))
3037 (macrolet ((err (constraint &rest arguments)
3038 `(report-error ',(intern* constraint :keyword) ,@arguments))
3039 (check (constraint condition &rest arguments)
3040 `(when ,constraint
3041 (unless ,condition (err ,constraint ,@arguments))))
3042 (transform (transform condition expr)
3043 `(when ,transform
3044 (,@(if condition `(when ,condition) '(progn))
3045 (setf p ,expr)))))
3046 (etypecase p
3047 ((or null pathname))
3048 (string
3049 (setf p (case namestring
3050 ((:unix nil)
3051 (parse-unix-namestring
3052 p :defaults defaults :type type :dot-dot dot-dot
3053 :ensure-directory ensure-directory :want-relative want-relative))
3054 ((:native)
3055 (parse-native-namestring p))
3056 ((:lisp)
3057 (parse-namestring p))
3059 (call-function namestring p))))))
3060 (etypecase p
3061 (pathname)
3062 (null
3063 (check want-pathname (pathnamep p) "Expected a pathname, not NIL")
3064 (return nil)))
3065 (check want-logical (logical-pathname-p p) "Expected a logical pathname")
3066 (check want-physical (physical-pathname-p p) "Expected a physical pathname")
3067 (transform ensure-physical () (physicalize-pathname p))
3068 (check ensure-physical (physical-pathname-p p) "Could not translate to a physical pathname")
3069 (check want-relative (relative-pathname-p p) "Expected a relative pathname")
3070 (check want-absolute (absolute-pathname-p p) "Expected an absolute pathname")
3071 (transform ensure-absolute (not (absolute-pathname-p p))
3072 (ensure-absolute-pathname p defaults (list #'report-error :ensure-absolute "~@?")))
3073 (check ensure-absolute (absolute-pathname-p p)
3074 "Could not make into an absolute pathname even after merging with ~S" defaults)
3075 (check ensure-subpath (absolute-pathname-p defaults)
3076 "cannot be checked to be a subpath of non-absolute pathname ~S" defaults)
3077 (check ensure-subpath (subpathp p defaults) "is not a sub pathname of ~S" defaults)
3078 (check want-file (file-pathname-p p) "Expected a file pathname")
3079 (check want-directory (directory-pathname-p p) "Expected a directory pathname")
3080 (transform ensure-directory (not (directory-pathname-p p)) (ensure-directory-pathname p))
3081 (check want-non-wild (not (wild-pathname-p p)) "Expected a non-wildcard pathname")
3082 (check want-wild (wild-pathname-p p) "Expected a wildcard pathname")
3083 (transform wilden (not (wild-pathname-p p)) (wilden p))
3084 (when want-existing
3085 (let ((existing (probe-file* p :truename truename)))
3086 (if existing
3087 (when truename
3088 (return existing))
3089 (err want-existing "Expected an existing pathname"))))
3090 (when ensure-directories-exist (ensure-directories-exist p))
3091 (when truename
3092 (let ((truename (truename* p)))
3093 (if truename
3094 (return truename)
3095 (err truename "Can't get a truename for pathname"))))
3096 (transform resolve-symlinks () (resolve-symlinks p))
3097 (transform truenamize () (truenamize p))
3098 p)))))
3101 ;;; Pathname defaults
3102 (with-upgradability ()
3103 (defun get-pathname-defaults (&optional (defaults *default-pathname-defaults*))
3104 "Find the actual DEFAULTS to use for pathnames, including
3105 resolving them with respect to GETCWD if the DEFAULTS were relative"
3106 (or (absolute-pathname-p defaults)
3107 (merge-pathnames* defaults (getcwd))))
3109 (defun call-with-current-directory (dir thunk)
3110 "call the THUNK in a context where the current directory was changed to DIR, if not NIL.
3111 Note that this operation is usually NOT thread-safe."
3112 (if dir
3113 (let* ((dir (resolve-symlinks* (get-pathname-defaults (pathname-directory-pathname dir))))
3114 (cwd (getcwd))
3115 (*default-pathname-defaults* dir))
3116 (chdir dir)
3117 (unwind-protect
3118 (funcall thunk)
3119 (chdir cwd)))
3120 (funcall thunk)))
3122 (defmacro with-current-directory ((&optional dir) &body body)
3123 "Call BODY while the POSIX current working directory is set to DIR"
3124 `(call-with-current-directory ,dir #'(lambda () ,@body))))
3127 ;;; Environment pathnames
3128 (with-upgradability ()
3129 (defun inter-directory-separator ()
3130 "What character does the current OS conventionally uses to separate directories?"
3131 (if (os-unix-p) #\: #\;))
3133 (defun split-native-pathnames-string (string &rest constraints &key &allow-other-keys)
3134 "Given a string of pathnames specified in native OS syntax, separate them in a list,
3135 check constraints and normalize each one as per ENSURE-PATHNAME."
3136 (loop :for namestring :in (split-string string :separator (string (inter-directory-separator)))
3137 :collect (apply 'parse-native-namestring namestring constraints)))
3139 (defun getenv-pathname (x &rest constraints &key ensure-directory want-directory on-error &allow-other-keys)
3140 "Extract a pathname from a user-configured environment variable, as per native OS,
3141 check constraints and normalize as per ENSURE-PATHNAME."
3142 ;; For backward compatibility with ASDF 2, want-directory implies ensure-directory
3143 (apply 'parse-native-namestring (getenvp x)
3144 :ensure-directory (or ensure-directory want-directory)
3145 :on-error (or on-error
3146 `(error "In (~S ~S), invalid pathname ~*~S: ~*~?" getenv-pathname ,x))
3147 constraints))
3148 (defun getenv-pathnames (x &rest constraints &key on-error &allow-other-keys)
3149 "Extract a list of pathname from a user-configured environment variable, as per native OS,
3150 check constraints and normalize each one as per ENSURE-PATHNAME."
3151 (apply 'split-native-pathnames-string (getenvp x)
3152 :on-error (or on-error
3153 `(error "In (~S ~S), invalid pathname ~*~S: ~*~?" getenv-pathnames ,x))
3154 constraints))
3155 (defun getenv-absolute-directory (x)
3156 "Extract an absolute directory pathname from a user-configured environment variable,
3157 as per native OS"
3158 (getenv-pathname x :want-absolute t :ensure-directory t))
3159 (defun getenv-absolute-directories (x)
3160 "Extract a list of absolute directories from a user-configured environment variable,
3161 as per native OS"
3162 (getenv-pathnames x :want-absolute t :ensure-directory t))
3164 (defun lisp-implementation-directory (&key truename)
3165 "Where are the system files of the current installation of the CL implementation?"
3166 (declare (ignorable truename))
3167 #+(or clozure ecl gcl mkcl sbcl)
3168 (let ((dir
3169 (ignore-errors
3170 #+clozure #p"ccl:"
3171 #+(or ecl mkcl) #p"SYS:"
3172 #+gcl system::*system-directory*
3173 #+sbcl (if-let (it (find-symbol* :sbcl-homedir-pathname :sb-int nil))
3174 (funcall it)
3175 (getenv-pathname "SBCL_HOME" :ensure-directory t)))))
3176 (if (and dir truename)
3177 (truename* dir)
3178 dir)))
3180 (defun lisp-implementation-pathname-p (pathname)
3181 "Is the PATHNAME under the current installation of the CL implementation?"
3182 ;; Other builtin systems are those under the implementation directory
3183 (and (when pathname
3184 (if-let (impdir (lisp-implementation-directory))
3185 (or (subpathp pathname impdir)
3186 (when *resolve-symlinks*
3187 (if-let (truename (truename* pathname))
3188 (if-let (trueimpdir (truename* impdir))
3189 (subpathp truename trueimpdir)))))))
3190 t)))
3193 ;;; Simple filesystem operations
3194 (with-upgradability ()
3195 (defun ensure-all-directories-exist (pathnames)
3196 "Ensure that for every pathname in PATHNAMES, we ensure its directories exist"
3197 (dolist (pathname pathnames)
3198 (when pathname
3199 (ensure-directories-exist (physicalize-pathname pathname)))))
3201 (defun rename-file-overwriting-target (source target)
3202 "Rename a file, overwriting any previous file with the TARGET name,
3203 in an atomic way if the implementation allows."
3204 #+clisp ;; in recent enough versions of CLISP, :if-exists :overwrite would make it atomic
3205 (progn (funcall 'require "syscalls")
3206 (symbol-call :posix :copy-file source target :method :rename))
3207 #-clisp
3208 (rename-file source target
3209 #+(or clozure ecl) :if-exists #+clozure :rename-and-delete #+ecl t))
3211 (defun delete-file-if-exists (x)
3212 "Delete a file X if it already exists"
3213 (when x (handler-case (delete-file x) (file-error () nil))))
3215 (defun delete-empty-directory (directory-pathname)
3216 "Delete an empty directory"
3217 #+(or abcl digitool gcl) (delete-file directory-pathname)
3218 #+allegro (excl:delete-directory directory-pathname)
3219 #+clisp (ext:delete-directory directory-pathname)
3220 #+clozure (ccl::delete-empty-directory directory-pathname)
3221 #+(or cmu scl) (multiple-value-bind (ok errno)
3222 (unix:unix-rmdir (native-namestring directory-pathname))
3223 (unless ok
3224 #+cmu (error "Error number ~A when trying to delete directory ~A"
3225 errno directory-pathname)
3226 #+scl (error "~@<Error deleting ~S: ~A~@:>"
3227 directory-pathname (unix:get-unix-error-msg errno))))
3228 #+cormanlisp (win32:delete-directory directory-pathname)
3229 #+ecl (si:rmdir directory-pathname)
3230 #+genera (fs:delete-directory directory-pathname)
3231 #+lispworks (lw:delete-directory directory-pathname)
3232 #+mkcl (mkcl:rmdir directory-pathname)
3233 #+sbcl #.(if-let (dd (find-symbol* :delete-directory :sb-ext nil))
3234 `(,dd directory-pathname) ;; requires SBCL 1.0.44 or later
3235 `(progn (require :sb-posix) (symbol-call :sb-posix :rmdir directory-pathname)))
3236 #+xcl (symbol-call :uiop :run-program `("rmdir" ,(native-namestring directory-pathname)))
3237 #-(or abcl allegro clisp clozure cmu cormanlisp digitool ecl gcl genera lispworks mkcl sbcl scl xcl)
3238 (error "~S not implemented on ~S" 'delete-empty-directory (implementation-type))) ; genera
3240 (defun delete-directory-tree (directory-pathname &key (validate nil validatep) (if-does-not-exist :error))
3241 "Delete a directory including all its recursive contents, aka rm -rf.
3243 To reduce the risk of infortunate mistakes, DIRECTORY-PATHNAME must be
3244 a physical non-wildcard directory pathname (not namestring).
3246 If the directory does not exist, the IF-DOES-NOT-EXIST argument specifies what happens:
3247 if it is :ERROR (the default), an error is signaled, whereas if it is :IGNORE, nothing is done.
3249 Furthermore, before any deletion is attempted, the DIRECTORY-PATHNAME must pass
3250 the validation function designated (as per ENSURE-FUNCTION) by the VALIDATE keyword argument
3251 which in practice is thus compulsory, and validates by returning a non-NIL result.
3252 If you're suicidal or extremely confident, just use :VALIDATE T."
3253 (check-type if-does-not-exist (member :error :ignore))
3254 (cond
3255 ((not (and (pathnamep directory-pathname) (directory-pathname-p directory-pathname)
3256 (physical-pathname-p directory-pathname) (not (wild-pathname-p directory-pathname))))
3257 (error "~S was asked to delete ~S but it is not a physical non-wildcard directory pathname"
3258 'delete-filesystem-tree directory-pathname))
3259 ((not validatep)
3260 (error "~S was asked to delete ~S but was not provided a validation predicate"
3261 'delete-filesystem-tree directory-pathname))
3262 ((not (call-function validate directory-pathname))
3263 (error "~S was asked to delete ~S but it is not valid ~@[according to ~S~]"
3264 'delete-filesystem-tree directory-pathname validate))
3265 ((not (directory-exists-p directory-pathname))
3266 (ecase if-does-not-exist
3267 (:error
3268 (error "~S was asked to delete ~S but the directory does not exist"
3269 'delete-filesystem-tree directory-pathname))
3270 (:ignore nil)))
3271 #-(or allegro cmu clozure genera sbcl scl)
3272 ((os-unix-p) ;; On Unix, don't recursively walk the directory and delete everything in Lisp,
3273 ;; except on implementations where we can prevent DIRECTORY from following symlinks;
3274 ;; instead spawn a standard external program to do the dirty work.
3275 (symbol-call :uiop :run-program `("rm" "-rf" ,(native-namestring directory-pathname))))
3277 ;; On supported implementation, call supported system functions
3278 #+allegro (symbol-call :excl.osi :delete-directory-and-files
3279 directory-pathname :if-does-not-exist if-does-not-exist)
3280 #+clozure (ccl:delete-directory directory-pathname)
3281 #+genera (fs:delete-directory directory-pathname :confirm nil)
3282 #+sbcl #.(if-let (dd (find-symbol* :delete-directory :sb-ext nil))
3283 `(,dd directory-pathname :recursive t) ;; requires SBCL 1.0.44 or later
3284 '(error "~S requires SBCL 1.0.44 or later" 'delete-directory-tree))
3285 ;; Outside Unix or on CMUCL and SCL that can avoid following symlinks,
3286 ;; do things the hard way.
3287 #-(or allegro clozure genera sbcl)
3288 (let ((sub*directories
3289 (while-collecting (c)
3290 (collect-sub*directories directory-pathname t t #'c))))
3291 (dolist (d (nreverse sub*directories))
3292 (map () 'delete-file (directory-files d))
3293 (delete-empty-directory d)))))))
3295 ;;;; ---------------------------------------------------------------------------
3296 ;;;; Utilities related to streams
3298 (uiop/package:define-package :uiop/stream
3299 (:nicknames :asdf/stream)
3300 (:recycle :uiop/stream :asdf/stream :asdf)
3301 (:use :uiop/common-lisp :uiop/package :uiop/utility :uiop/os :uiop/pathname :uiop/filesystem)
3302 (:export
3303 #:*default-stream-element-type*
3304 #:*stdin* #:setup-stdin #:*stdout* #:setup-stdout #:*stderr* #:setup-stderr
3305 #:detect-encoding #:*encoding-detection-hook* #:always-default-encoding
3306 #:encoding-external-format #:*encoding-external-format-hook* #:default-encoding-external-format
3307 #:*default-encoding* #:*utf-8-external-format*
3308 #:with-safe-io-syntax #:call-with-safe-io-syntax #:safe-read-from-string
3309 #:with-output #:output-string #:with-input
3310 #:with-input-file #:call-with-input-file #:with-output-file #:call-with-output-file
3311 #:null-device-pathname #:call-with-null-input #:with-null-input
3312 #:call-with-null-output #:with-null-output
3313 #:finish-outputs #:format! #:safe-format!
3314 #:copy-stream-to-stream #:concatenate-files #:copy-file
3315 #:slurp-stream-string #:slurp-stream-lines #:slurp-stream-line
3316 #:slurp-stream-forms #:slurp-stream-form
3317 #:read-file-string #:read-file-line #:read-file-lines #:safe-read-file-line
3318 #:read-file-forms #:read-file-form #:safe-read-file-form
3319 #:eval-input #:eval-thunk #:standard-eval-thunk
3320 #:println #:writeln
3321 ;; Temporary files
3322 #:*temporary-directory* #:temporary-directory #:default-temporary-directory
3323 #:setup-temporary-directory
3324 #:call-with-temporary-file #:with-temporary-file
3325 #:add-pathname-suffix #:tmpize-pathname
3326 #:call-with-staging-pathname #:with-staging-pathname))
3327 (in-package :uiop/stream)
3329 (with-upgradability ()
3330 (defvar *default-stream-element-type*
3331 (or #+(or abcl cmu cormanlisp scl xcl) 'character
3332 #+lispworks 'lw:simple-char
3333 :default)
3334 "default element-type for open (depends on the current CL implementation)")
3336 (defvar *stdin* *standard-input*
3337 "the original standard input stream at startup")
3339 (defun setup-stdin ()
3340 (setf *stdin*
3341 #.(or #+clozure 'ccl::*stdin*
3342 #+(or cmu scl) 'system:*stdin*
3343 #+ecl 'ext::+process-standard-input+
3344 #+sbcl 'sb-sys:*stdin*
3345 '*standard-input*)))
3347 (defvar *stdout* *standard-output*
3348 "the original standard output stream at startup")
3350 (defun setup-stdout ()
3351 (setf *stdout*
3352 #.(or #+clozure 'ccl::*stdout*
3353 #+(or cmu scl) 'system:*stdout*
3354 #+ecl 'ext::+process-standard-output+
3355 #+sbcl 'sb-sys:*stdout*
3356 '*standard-output*)))
3358 (defvar *stderr* *error-output*
3359 "the original error output stream at startup")
3361 (defun setup-stderr ()
3362 (setf *stderr*
3363 #.(or #+allegro 'excl::*stderr*
3364 #+clozure 'ccl::*stderr*
3365 #+(or cmu scl) 'system:*stderr*
3366 #+ecl 'ext::+process-error-output+
3367 #+sbcl 'sb-sys:*stderr*
3368 '*error-output*)))
3370 ;; Run them now. In image.lisp, we'll register them to be run at image restart.
3371 (setup-stdin) (setup-stdout) (setup-stderr))
3374 ;;; Encodings (mostly hooks only; full support requires asdf-encodings)
3375 (with-upgradability ()
3376 (defparameter *default-encoding*
3377 ;; preserve explicit user changes to something other than the legacy default :default
3378 (or (if-let (previous (and (boundp '*default-encoding*) (symbol-value '*default-encoding*)))
3379 (unless (eq previous :default) previous))
3380 :utf-8)
3381 "Default encoding for source files.
3382 The default value :utf-8 is the portable thing.
3383 The legacy behavior was :default.
3384 If you (asdf:load-system :asdf-encodings) then
3385 you will have autodetection via *encoding-detection-hook* below,
3386 reading emacs-style -*- coding: utf-8 -*- specifications,
3387 and falling back to utf-8 or latin1 if nothing is specified.")
3389 (defparameter *utf-8-external-format*
3390 (if (featurep :asdf-unicode)
3391 (or #+clisp charset:utf-8 :utf-8)
3392 :default)
3393 "Default :external-format argument to pass to CL:OPEN and also
3394 CL:LOAD or CL:COMPILE-FILE to best process a UTF-8 encoded file.
3395 On modern implementations, this will decode UTF-8 code points as CL characters.
3396 On legacy implementations, it may fall back on some 8-bit encoding,
3397 with non-ASCII code points being read as several CL characters;
3398 hopefully, if done consistently, that won't affect program behavior too much.")
3400 (defun always-default-encoding (pathname)
3401 "Trivial function to use as *encoding-detection-hook*,
3402 always 'detects' the *default-encoding*"
3403 (declare (ignore pathname))
3404 *default-encoding*)
3406 (defvar *encoding-detection-hook* #'always-default-encoding
3407 "Hook for an extension to define a function to automatically detect a file's encoding")
3409 (defun detect-encoding (pathname)
3410 "Detects the encoding of a specified file, going through user-configurable hooks"
3411 (if (and pathname (not (directory-pathname-p pathname)) (probe-file* pathname))
3412 (funcall *encoding-detection-hook* pathname)
3413 *default-encoding*))
3415 (defun default-encoding-external-format (encoding)
3416 "Default, ignorant, function to transform a character ENCODING as a
3417 portable keyword to an implementation-dependent EXTERNAL-FORMAT specification.
3418 Load system ASDF-ENCODINGS to hook in a better one."
3419 (case encoding
3420 (:default :default) ;; for backward-compatibility only. Explicit usage discouraged.
3421 (:utf-8 *utf-8-external-format*)
3422 (otherwise
3423 (cerror "Continue using :external-format :default" (compatfmt "~@<Your ASDF component is using encoding ~S but it isn't recognized. Your system should :defsystem-depends-on (:asdf-encodings).~:>") encoding)
3424 :default)))
3426 (defvar *encoding-external-format-hook*
3427 #'default-encoding-external-format
3428 "Hook for an extension (e.g. ASDF-ENCODINGS) to define a better mapping
3429 from non-default encodings to and implementation-defined external-format's")
3431 (defun encoding-external-format (encoding)
3432 "Transform a portable ENCODING keyword to an implementation-dependent EXTERNAL-FORMAT,
3433 going through all the proper hooks."
3434 (funcall *encoding-external-format-hook* (or encoding *default-encoding*))))
3437 ;;; Safe syntax
3438 (with-upgradability ()
3439 (defvar *standard-readtable* (with-standard-io-syntax *readtable*)
3440 "The standard readtable, implementing the syntax specified by the CLHS.
3441 It must never be modified, though only good implementations will even enforce that.")
3443 (defmacro with-safe-io-syntax ((&key (package :cl)) &body body)
3444 "Establish safe CL reader options around the evaluation of BODY"
3445 `(call-with-safe-io-syntax #'(lambda () (let ((*package* (find-package ,package))) ,@body))))
3447 (defun call-with-safe-io-syntax (thunk &key (package :cl))
3448 (with-standard-io-syntax
3449 (let ((*package* (find-package package))
3450 (*read-default-float-format* 'double-float)
3451 (*print-readably* nil)
3452 (*read-eval* nil))
3453 (funcall thunk))))
3455 (defun safe-read-from-string (string &key (package :cl) (eof-error-p t) eof-value (start 0) end preserve-whitespace)
3456 "Read from STRING using a safe syntax, as per WITH-SAFE-IO-SYNTAX"
3457 (with-safe-io-syntax (:package package)
3458 (read-from-string string eof-error-p eof-value :start start :end end :preserve-whitespace preserve-whitespace))))
3460 ;;; Output helpers
3461 (with-upgradability ()
3462 (defun call-with-output-file (pathname thunk
3463 &key
3464 (element-type *default-stream-element-type*)
3465 (external-format *utf-8-external-format*)
3466 (if-exists :error)
3467 (if-does-not-exist :create))
3468 "Open FILE for input with given recognizes options, call THUNK with the resulting stream.
3469 Other keys are accepted but discarded."
3470 (with-open-file (s pathname :direction :output
3471 :element-type element-type
3472 :external-format external-format
3473 :if-exists if-exists
3474 :if-does-not-exist if-does-not-exist)
3475 (funcall thunk s)))
3477 (defmacro with-output-file ((var pathname &rest keys
3478 &key element-type external-format if-exists if-does-not-exist)
3479 &body body)
3480 (declare (ignore element-type external-format if-exists if-does-not-exist))
3481 `(call-with-output-file ,pathname #'(lambda (,var) ,@body) ,@keys))
3483 (defun call-with-output (output function &key keys)
3484 "Calls FUNCTION with an actual stream argument,
3485 behaving like FORMAT with respect to how stream designators are interpreted:
3486 If OUTPUT is a STREAM, use it as the stream.
3487 If OUTPUT is NIL, use a STRING-OUTPUT-STREAM as the stream, and return the resulting string.
3488 If OUTPUT is T, use *STANDARD-OUTPUT* as the stream.
3489 If OUTPUT is a STRING with a fill-pointer, use it as a string-output-stream.
3490 If OUTPUT is a PATHNAME, open the file and write to it, passing KEYS to WITH-OUTPUT-FILE
3491 -- this latter as an extension since ASDF 3.1.
3492 Otherwise, signal an error."
3493 (etypecase output
3494 (null
3495 (with-output-to-string (stream) (funcall function stream)))
3496 ((eql t)
3497 (funcall function *standard-output*))
3498 (stream
3499 (funcall function output))
3500 (string
3501 (assert (fill-pointer output))
3502 (with-output-to-string (stream output) (funcall function stream)))
3503 (pathname
3504 (apply 'call-with-output-file output function keys))))
3506 (defmacro with-output ((output-var &optional (value output-var)) &body body)
3507 "Bind OUTPUT-VAR to an output stream, coercing VALUE (default: previous binding of OUTPUT-VAR)
3508 as per FORMAT, and evaluate BODY within the scope of this binding."
3509 `(call-with-output ,value #'(lambda (,output-var) ,@body)))
3511 (defun output-string (string &optional output)
3512 "If the desired OUTPUT is not NIL, print the string to the output; otherwise return the string"
3513 (if output
3514 (with-output (output) (princ string output))
3515 string)))
3518 ;;; Input helpers
3519 (with-upgradability ()
3520 (defun call-with-input-file (pathname thunk
3521 &key
3522 (element-type *default-stream-element-type*)
3523 (external-format *utf-8-external-format*)
3524 (if-does-not-exist :error))
3525 "Open FILE for input with given recognizes options, call THUNK with the resulting stream.
3526 Other keys are accepted but discarded."
3527 (with-open-file (s pathname :direction :input
3528 :element-type element-type
3529 :external-format external-format
3530 :if-does-not-exist if-does-not-exist)
3531 (funcall thunk s)))
3533 (defmacro with-input-file ((var pathname &rest keys
3534 &key element-type external-format if-does-not-exist)
3535 &body body)
3536 (declare (ignore element-type external-format if-does-not-exist))
3537 `(call-with-input-file ,pathname #'(lambda (,var) ,@body) ,@keys))
3539 (defun call-with-input (input function &key keys)
3540 "Calls FUNCTION with an actual stream argument, interpreting
3541 stream designators like READ, but also coercing strings to STRING-INPUT-STREAM,
3542 and PATHNAME to FILE-STREAM.
3543 If INPUT is a STREAM, use it as the stream.
3544 If INPUT is NIL, use a *STANDARD-INPUT* as the stream.
3545 If INPUT is T, use *TERMINAL-IO* as the stream.
3546 If INPUT is a STRING, use it as a string-input-stream.
3547 If INPUT is a PATHNAME, open it, passing KEYS to WITH-INPUT-FILE
3548 -- the latter is an extension since ASDF 3.1.
3549 Otherwise, signal an error."
3550 (etypecase input
3551 (null (funcall function *standard-input*))
3552 ((eql t) (funcall function *terminal-io*))
3553 (stream (funcall function input))
3554 (string (with-input-from-string (stream input) (funcall function stream)))
3555 (pathname (apply 'call-with-input-file input function keys))))
3557 (defmacro with-input ((input-var &optional (value input-var)) &body body)
3558 "Bind INPUT-VAR to an input stream, coercing VALUE (default: previous binding of INPUT-VAR)
3559 as per CALL-WITH-INPUT, and evaluate BODY within the scope of this binding."
3560 `(call-with-input ,value #'(lambda (,input-var) ,@body))))
3563 ;;; Null device
3564 (with-upgradability ()
3565 (defun null-device-pathname ()
3566 "Pathname to a bit bucket device that discards any information written to it
3567 and always returns EOF when read from"
3568 (cond
3569 ((os-unix-p) #p"/dev/null")
3570 ((os-windows-p) #p"NUL") ;; Q: how many Lisps accept the #p"NUL:" syntax?
3571 (t (error "No /dev/null on your OS"))))
3572 (defun call-with-null-input (fun &rest keys &key element-type external-format if-does-not-exist)
3573 "Call FUN with an input stream from the null device; pass keyword arguments to OPEN."
3574 (declare (ignore element-type external-format if-does-not-exist))
3575 (apply 'call-with-input-file (null-device-pathname) fun keys))
3576 (defmacro with-null-input ((var &rest keys
3577 &key element-type external-format if-does-not-exist)
3578 &body body)
3579 (declare (ignore element-type external-format if-does-not-exist))
3580 "Evaluate BODY in a context when VAR is bound to an input stream accessing the null device.
3581 Pass keyword arguments to OPEN."
3582 `(call-with-null-input #'(lambda (,var) ,@body) ,@keys))
3583 (defun call-with-null-output (fun
3584 &key (element-type *default-stream-element-type*)
3585 (external-format *utf-8-external-format*)
3586 (if-exists :overwrite)
3587 (if-does-not-exist :error))
3588 "Call FUN with an output stream to the null device; pass keyword arguments to OPEN."
3589 (call-with-output-file
3590 (null-device-pathname) fun
3591 :element-type element-type :external-format external-format
3592 :if-exists if-exists :if-does-not-exist if-does-not-exist))
3593 (defmacro with-null-output ((var &rest keys
3594 &key element-type external-format if-does-not-exist if-exists)
3595 &body body)
3596 "Evaluate BODY in a context when VAR is bound to an output stream accessing the null device.
3597 Pass keyword arguments to OPEN."
3598 (declare (ignore element-type external-format if-exists if-does-not-exist))
3599 `(call-with-null-output #'(lambda (,var) ,@body) ,@keys)))
3601 ;;; Ensure output buffers are flushed
3602 (with-upgradability ()
3603 (defun finish-outputs (&rest streams)
3604 "Finish output on the main output streams as well as any specified one.
3605 Useful for portably flushing I/O before user input or program exit."
3606 ;; CCL notably buffers its stream output by default.
3607 (dolist (s (append streams
3608 (list *stdout* *stderr* *error-output* *standard-output* *trace-output*
3609 *debug-io* *terminal-io* *query-io*)))
3610 (ignore-errors (finish-output s)))
3611 (values))
3613 (defun format! (stream format &rest args)
3614 "Just like format, but call finish-outputs before and after the output."
3615 (finish-outputs stream)
3616 (apply 'format stream format args)
3617 (finish-outputs stream))
3619 (defun safe-format! (stream format &rest args)
3620 "Variant of FORMAT that is safe against both
3621 dangerous syntax configuration and errors while printing."
3622 (with-safe-io-syntax ()
3623 (ignore-errors (apply 'format! stream format args))
3624 (finish-outputs stream)))) ; just in case format failed
3627 ;;; Simple Whole-Stream processing
3628 (with-upgradability ()
3629 (defun copy-stream-to-stream (input output &key element-type buffer-size linewise prefix)
3630 "Copy the contents of the INPUT stream into the OUTPUT stream.
3631 If LINEWISE is true, then read and copy the stream line by line, with an optional PREFIX.
3632 Otherwise, using WRITE-SEQUENCE using a buffer of size BUFFER-SIZE."
3633 (with-open-stream (input input)
3634 (if linewise
3635 (loop* :for (line eof) = (multiple-value-list (read-line input nil nil))
3636 :while line :do
3637 (when prefix (princ prefix output))
3638 (princ line output)
3639 (unless eof (terpri output))
3640 (finish-output output)
3641 (when eof (return)))
3642 (loop
3643 :with buffer-size = (or buffer-size 8192)
3644 :for buffer = (make-array (list buffer-size) :element-type (or element-type 'character))
3645 :for end = (read-sequence buffer input)
3646 :until (zerop end)
3647 :do (write-sequence buffer output :end end)
3648 (when (< end buffer-size) (return))))))
3650 (defun concatenate-files (inputs output)
3651 "create a new OUTPUT file the contents of which a the concatenate of the INPUTS files."
3652 (with-open-file (o output :element-type '(unsigned-byte 8)
3653 :direction :output :if-exists :rename-and-delete)
3654 (dolist (input inputs)
3655 (with-open-file (i input :element-type '(unsigned-byte 8)
3656 :direction :input :if-does-not-exist :error)
3657 (copy-stream-to-stream i o :element-type '(unsigned-byte 8))))))
3659 (defun copy-file (input output)
3660 "Copy contents of the INPUT file to the OUTPUT file"
3661 ;; Not available on LW personal edition or LW 6.0 on Mac: (lispworks:copy-file i f)
3662 (concatenate-files (list input) output))
3664 (defun slurp-stream-string (input &key (element-type 'character) stripped)
3665 "Read the contents of the INPUT stream as a string"
3666 (let ((string
3667 (with-open-stream (input input)
3668 (with-output-to-string (output)
3669 (copy-stream-to-stream input output :element-type element-type)))))
3670 (if stripped (stripln string) string)))
3672 (defun slurp-stream-lines (input &key count)
3673 "Read the contents of the INPUT stream as a list of lines, return those lines.
3675 Note: relies on the Lisp's READ-LINE, but additionally removes any remaining CR
3676 from the line-ending if the file or stream had CR+LF but Lisp only removed LF.
3678 Read no more than COUNT lines."
3679 (check-type count (or null integer))
3680 (with-open-stream (input input)
3681 (loop :for n :from 0
3682 :for l = (and (or (not count) (< n count))
3683 (read-line input nil nil))
3684 ;; stripln: to remove CR when the OS sends CRLF and Lisp only remove LF
3685 :while l :collect (stripln l))))
3687 (defun slurp-stream-line (input &key (at 0))
3688 "Read the contents of the INPUT stream as a list of lines,
3689 then return the ACCESS-AT of that list of lines using the AT specifier.
3690 PATH defaults to 0, i.e. return the first line.
3691 PATH is typically an integer, or a list of an integer and a function.
3692 If PATH is NIL, it will return all the lines in the file.
3694 The stream will not be read beyond the Nth lines,
3695 where N is the index specified by path
3696 if path is either an integer or a list that starts with an integer."
3697 (access-at (slurp-stream-lines input :count (access-at-count at)) at))
3699 (defun slurp-stream-forms (input &key count)
3700 "Read the contents of the INPUT stream as a list of forms,
3701 and return those forms.
3703 If COUNT is null, read to the end of the stream;
3704 if COUNT is an integer, stop after COUNT forms were read.
3706 BEWARE: be sure to use WITH-SAFE-IO-SYNTAX, or some variant thereof"
3707 (check-type count (or null integer))
3708 (loop :with eof = '#:eof
3709 :for n :from 0
3710 :for form = (if (and count (>= n count))
3712 (read-preserving-whitespace input nil eof))
3713 :until (eq form eof) :collect form))
3715 (defun slurp-stream-form (input &key (at 0))
3716 "Read the contents of the INPUT stream as a list of forms,
3717 then return the ACCESS-AT of these forms following the AT.
3718 AT defaults to 0, i.e. return the first form.
3719 AT is typically a list of integers.
3720 If AT is NIL, it will return all the forms in the file.
3722 The stream will not be read beyond the Nth form,
3723 where N is the index specified by path,
3724 if path is either an integer or a list that starts with an integer.
3726 BEWARE: be sure to use WITH-SAFE-IO-SYNTAX, or some variant thereof"
3727 (access-at (slurp-stream-forms input :count (access-at-count at)) at))
3729 (defun read-file-string (file &rest keys)
3730 "Open FILE with option KEYS, read its contents as a string"
3731 (apply 'call-with-input-file file 'slurp-stream-string keys))
3733 (defun read-file-lines (file &rest keys)
3734 "Open FILE with option KEYS, read its contents as a list of lines
3735 BEWARE: be sure to use WITH-SAFE-IO-SYNTAX, or some variant thereof"
3736 (apply 'call-with-input-file file 'slurp-stream-lines keys))
3738 (defun read-file-line (file &rest keys &key (at 0) &allow-other-keys)
3739 "Open input FILE with option KEYS (except AT),
3740 and read its contents as per SLURP-STREAM-LINE with given AT specifier.
3741 BEWARE: be sure to use WITH-SAFE-IO-SYNTAX, or some variant thereof"
3742 (apply 'call-with-input-file file
3743 #'(lambda (input) (slurp-stream-line input :at at))
3744 (remove-plist-key :at keys)))
3746 (defun read-file-forms (file &rest keys &key count &allow-other-keys)
3747 "Open input FILE with option KEYS (except COUNT),
3748 and read its contents as per SLURP-STREAM-FORMS with given COUNT.
3749 BEWARE: be sure to use WITH-SAFE-IO-SYNTAX, or some variant thereof"
3750 (apply 'call-with-input-file file
3751 #'(lambda (input) (slurp-stream-forms input :count count))
3752 (remove-plist-key :count keys)))
3754 (defun read-file-form (file &rest keys &key (at 0) &allow-other-keys)
3755 "Open input FILE with option KEYS (except AT),
3756 and read its contents as per SLURP-STREAM-FORM with given AT specifier.
3757 BEWARE: be sure to use WITH-SAFE-IO-SYNTAX, or some variant thereof"
3758 (apply 'call-with-input-file file
3759 #'(lambda (input) (slurp-stream-form input :at at))
3760 (remove-plist-key :at keys)))
3762 (defun safe-read-file-line (pathname &rest keys &key (package :cl) &allow-other-keys)
3763 "Reads the specified line from the top of a file using a safe standardized syntax.
3764 Extracts the line using READ-FILE-LINE,
3765 within an WITH-SAFE-IO-SYNTAX using the specified PACKAGE."
3766 (with-safe-io-syntax (:package package)
3767 (apply 'read-file-line pathname (remove-plist-key :package keys))))
3769 (defun safe-read-file-form (pathname &rest keys &key (package :cl) &allow-other-keys)
3770 "Reads the specified form from the top of a file using a safe standardized syntax.
3771 Extracts the form using READ-FILE-FORM,
3772 within an WITH-SAFE-IO-SYNTAX using the specified PACKAGE."
3773 (with-safe-io-syntax (:package package)
3774 (apply 'read-file-form pathname (remove-plist-key :package keys))))
3776 (defun eval-input (input)
3777 "Portably read and evaluate forms from INPUT, return the last values."
3778 (with-input (input)
3779 (loop :with results :with eof ='#:eof
3780 :for form = (read input nil eof)
3781 :until (eq form eof)
3782 :do (setf results (multiple-value-list (eval form)))
3783 :finally (return (apply 'values results)))))
3785 (defun eval-thunk (thunk)
3786 "Evaluate a THUNK of code:
3787 If a function, FUNCALL it without arguments.
3788 If a constant literal and not a sequence, return it.
3789 If a cons or a symbol, EVAL it.
3790 If a string, repeatedly read and evaluate from it, returning the last values."
3791 (etypecase thunk
3792 ((or boolean keyword number character pathname) thunk)
3793 ((or cons symbol) (eval thunk))
3794 (function (funcall thunk))
3795 (string (eval-input thunk))))
3797 (defun standard-eval-thunk (thunk &key (package :cl))
3798 "Like EVAL-THUNK, but in a more standardized evaluation context."
3799 ;; Note: it's "standard-" not "safe-", because evaluation is never safe.
3800 (when thunk
3801 (with-safe-io-syntax (:package package)
3802 (let ((*read-eval* t))
3803 (eval-thunk thunk))))))
3805 (with-upgradability ()
3806 (defun println (x &optional (stream *standard-output*))
3807 "Variant of PRINC that also calls TERPRI afterwards"
3808 (princ x stream) (terpri stream) (finish-output stream) (values))
3810 (defun writeln (x &rest keys &key (stream *standard-output*) &allow-other-keys)
3811 "Variant of WRITE that also calls TERPRI afterwards"
3812 (apply 'write x keys) (terpri stream) (finish-output stream) (values)))
3815 ;;; Using temporary files
3816 (with-upgradability ()
3817 (defun default-temporary-directory ()
3818 "Return a default directory to use for temporary files"
3820 (when (os-unix-p)
3821 (or (getenv-pathname "TMPDIR" :ensure-directory t)
3822 (parse-native-namestring "/tmp/")))
3823 (when (os-windows-p)
3824 (getenv-pathname "TEMP" :ensure-directory t))
3825 (subpathname (user-homedir-pathname) "tmp/")))
3827 (defvar *temporary-directory* nil "User-configurable location for temporary files")
3829 (defun temporary-directory ()
3830 "Return a directory to use for temporary files"
3831 (or *temporary-directory* (default-temporary-directory)))
3833 (defun setup-temporary-directory ()
3834 "Configure a default temporary directory to use."
3835 (setf *temporary-directory* (default-temporary-directory))
3836 #+gcl (setf system::*tmp-dir* *temporary-directory*))
3838 (defun call-with-temporary-file
3839 (thunk &key
3840 (want-stream-p t) (want-pathname-p t) (direction :io) keep after
3841 directory (type "tmp" typep) prefix (suffix (when typep "-tmp"))
3842 (element-type *default-stream-element-type*)
3843 (external-format *utf-8-external-format*))
3844 "Call a THUNK with stream and/or pathname arguments identifying a temporary file.
3846 The temporary file's pathname will be based on concatenating
3847 PREFIX (defaults to \"uiop\"), a random alphanumeric string,
3848 and optional SUFFIX (defaults to \"-tmp\" if a type was provided)
3849 and TYPE (defaults to \"tmp\", using a dot as separator if not NIL),
3850 within DIRECTORY (defaulting to the TEMPORARY-DIRECTORY) if the PREFIX isn't absolute.
3852 The file will be open with specified DIRECTION (defaults to :IO),
3853 ELEMENT-TYPE (defaults to *DEFAULT-STREAM-ELEMENT-TYPE*) and
3854 EXTERNAL-FORMAT (defaults to *UTF-8-EXTERNAL-FORMAT*).
3855 If WANT-STREAM-P is true (the defaults to T), then THUNK will then be CALL-FUNCTION'ed
3856 with the stream and the pathname (if WANT-PATHNAME-P is true, defaults to T),
3857 and stream with be closed after the THUNK exits (either normally or abnormally).
3858 If WANT-STREAM-P is false, then WANT-PATHAME-P must be true, and then
3859 THUNK is only CALL-FUNCTION'ed after the stream is closed, with the pathname as argument.
3860 Upon exit of THUNK, the AFTER thunk if defined is CALL-FUNCTION'ed with the pathname as argument.
3861 If AFTER is defined, its results are returned, otherwise, the results of THUNK are returned.
3862 Finally, the file will be deleted, unless the KEEP argument when CALL-FUNCTION'ed returns true."
3863 #+xcl (declare (ignorable typep))
3864 (check-type direction (member :output :io))
3865 (assert (or want-stream-p want-pathname-p))
3866 (loop
3867 :with prefix = (native-namestring
3868 (ensure-absolute-pathname
3869 (or prefix "tmp")
3870 (or (ensure-pathname directory :namestring :native :ensure-directory t)
3871 #'temporary-directory)))
3872 :with results = ()
3873 :for counter :from (random (expt 36 #-gcl 8 #+gcl 5))
3874 :for pathname = (parse-native-namestring
3875 (format nil "~A~36R~@[~A~]~@[.~A~]" prefix counter suffix type))
3876 :for okp = nil :do
3877 ;; TODO: on Unix, do something about umask
3878 ;; TODO: on Unix, audit the code so we make sure it uses O_CREAT|O_EXCL
3879 ;; TODO: on Unix, use CFFI and mkstemp --
3880 ;; except UIOP is precisely meant to not depend on CFFI or on anything! Grrrr.
3881 ;; Can we at least design some hook?
3882 (unwind-protect
3883 (progn
3884 (with-open-file (stream pathname
3885 :direction direction
3886 :element-type element-type
3887 :external-format external-format
3888 :if-exists nil :if-does-not-exist :create)
3889 (when stream
3890 (setf okp pathname)
3891 (when want-stream-p
3892 (setf results
3893 (multiple-value-list
3894 (if want-pathname-p
3895 (funcall thunk stream pathname)
3896 (funcall thunk stream)))))))
3897 (when okp
3898 (unless want-stream-p
3899 (setf results (multiple-value-list (call-function thunk pathname))))
3900 (when after
3901 (setf results (multiple-value-list (call-function after pathname))))
3902 (return (apply 'values results))))
3903 (when (and okp (not (call-function keep)))
3904 (ignore-errors (delete-file-if-exists okp))))))
3906 (defmacro with-temporary-file ((&key (stream (gensym "STREAM") streamp)
3907 (pathname (gensym "PATHNAME") pathnamep)
3908 directory prefix suffix type
3909 keep direction element-type external-format)
3910 &body body)
3911 "Evaluate BODY where the symbols specified by keyword arguments
3912 STREAM and PATHNAME (if respectively specified) are bound corresponding
3913 to a newly created temporary file ready for I/O, as per CALL-WITH-TEMPORARY-FILE.
3914 At least one of STREAM or PATHNAME must be specified.
3915 If the STREAM is not specified, it will be closed before the BODY is evaluated.
3916 If STREAM is specified, then the :CLOSE-STREAM label if it appears in the BODY,
3917 separates forms run before and after the stream is closed.
3918 The values of the last form of the BODY (not counting the separating :CLOSE-STREAM) are returned.
3919 Upon success, the KEEP form is evaluated and the file is is deleted unless it evaluates to TRUE."
3920 (check-type stream symbol)
3921 (check-type pathname symbol)
3922 (assert (or streamp pathnamep))
3923 (let* ((afterp (position :close-stream body))
3924 (before (if afterp (subseq body 0 afterp) body))
3925 (after (when afterp (subseq body (1+ afterp))))
3926 (beforef (gensym "BEFORE"))
3927 (afterf (gensym "AFTER")))
3928 `(flet (,@(when before
3929 `((,beforef (,@(when streamp `(,stream)) ,@(when pathnamep `(,pathname)))
3930 ,@(when after `((declare (ignorable ,pathname))))
3931 ,@before)))
3932 ,@(when after
3933 (assert pathnamep)
3934 `((,afterf (,pathname) ,@after))))
3935 #-gcl (declare (dynamic-extent ,@(when before `(#',beforef)) ,@(when after `(#',afterf))))
3936 (call-with-temporary-file
3937 ,(when before `#',beforef)
3938 :want-stream-p ,streamp
3939 :want-pathname-p ,pathnamep
3940 ,@(when direction `(:direction ,direction))
3941 ,@(when directory `(:directory ,directory))
3942 ,@(when prefix `(:prefix ,prefix))
3943 ,@(when suffix `(:suffix ,suffix))
3944 ,@(when type `(:type ,type))
3945 ,@(when keep `(:keep ,keep))
3946 ,@(when after `(:after #',afterf))
3947 ,@(when element-type `(:element-type ,element-type))
3948 ,@(when external-format `(:external-format ,external-format))))))
3950 (defun get-temporary-file (&key directory prefix suffix type)
3951 (with-temporary-file (:pathname pn :keep t
3952 :directory directory :prefix prefix :suffix suffix :type type)
3953 pn))
3955 ;; Temporary pathnames in simple cases where no contention is assumed
3956 (defun add-pathname-suffix (pathname suffix &rest keys)
3957 "Add a SUFFIX to the name of a PATHNAME, return a new pathname.
3958 Further KEYS can be passed to MAKE-PATHNAME."
3959 (apply 'make-pathname :name (strcat (pathname-name pathname) suffix)
3960 :defaults pathname keys))
3962 (defun tmpize-pathname (x)
3963 "Return a new pathname modified from X by adding a trivial deterministic suffix"
3964 (add-pathname-suffix x "-TMP"))
3966 (defun call-with-staging-pathname (pathname fun)
3967 "Calls FUN with a staging pathname, and atomically
3968 renames the staging pathname to the PATHNAME in the end.
3969 NB: this protects only against failure of the program, not against concurrent attempts.
3970 For the latter case, we ought pick a random suffix and atomically open it."
3971 (let* ((pathname (pathname pathname))
3972 (staging (tmpize-pathname pathname)))
3973 (unwind-protect
3974 (multiple-value-prog1
3975 (funcall fun staging)
3976 (rename-file-overwriting-target staging pathname))
3977 (delete-file-if-exists staging))))
3979 (defmacro with-staging-pathname ((pathname-var &optional (pathname-value pathname-var)) &body body)
3980 "Trivial syntax wrapper for CALL-WITH-STAGING-PATHNAME"
3981 `(call-with-staging-pathname ,pathname-value #'(lambda (,pathname-var) ,@body))))
3983 ;;;; -------------------------------------------------------------------------
3984 ;;;; Starting, Stopping, Dumping a Lisp image
3986 (uiop/package:define-package :uiop/image
3987 (:nicknames :asdf/image)
3988 (:recycle :uiop/image :asdf/image :xcvb-driver)
3989 (:use :uiop/common-lisp :uiop/package :uiop/utility :uiop/pathname :uiop/stream :uiop/os)
3990 (:export
3991 #:*image-dumped-p* #:raw-command-line-arguments #:*command-line-arguments*
3992 #:command-line-arguments #:raw-command-line-arguments #:setup-command-line-arguments #:argv0
3993 #:*lisp-interaction*
3994 #:*fatal-conditions* #:fatal-condition-p #:handle-fatal-condition
3995 #:call-with-fatal-condition-handler #:with-fatal-condition-handler
3996 #:*image-restore-hook* #:*image-prelude* #:*image-entry-point*
3997 #:*image-postlude* #:*image-dump-hook*
3998 #:quit #:die #:raw-print-backtrace #:print-backtrace #:print-condition-backtrace
3999 #:shell-boolean-exit
4000 #:register-image-restore-hook #:register-image-dump-hook
4001 #:call-image-restore-hook #:call-image-dump-hook
4002 #:restore-image #:dump-image #:create-image
4004 (in-package :uiop/image)
4006 (with-upgradability ()
4007 (defvar *lisp-interaction* t
4008 "Is this an interactive Lisp environment, or is it batch processing?")
4010 (defvar *command-line-arguments* nil
4011 "Command-line arguments")
4013 (defvar *image-dumped-p* nil ; may matter as to how to get to command-line-arguments
4014 "Is this a dumped image? As a standalone executable?")
4016 (defvar *image-restore-hook* nil
4017 "Functions to call (in reverse order) when the image is restored")
4019 (defvar *image-restored-p* nil
4020 "Has the image been restored? A boolean, or :in-progress while restoring, :in-regress while dumping")
4022 (defvar *image-prelude* nil
4023 "a form to evaluate, or string containing forms to read and evaluate
4024 when the image is restarted, but before the entry point is called.")
4026 (defvar *image-entry-point* nil
4027 "a function with which to restart the dumped image when execution is restored from it.")
4029 (defvar *image-postlude* nil
4030 "a form to evaluate, or string containing forms to read and evaluate
4031 before the image dump hooks are called and before the image is dumped.")
4033 (defvar *image-dump-hook* nil
4034 "Functions to call (in order) when before an image is dumped")
4036 (defvar *fatal-conditions* '(error)
4037 "conditions that cause the Lisp image to enter the debugger if interactive,
4038 or to die if not interactive"))
4041 ;;; Exiting properly or im-
4042 (with-upgradability ()
4043 (defun quit (&optional (code 0) (finish-output t))
4044 "Quits from the Lisp world, with the given exit status if provided.
4045 This is designed to abstract away the implementation specific quit forms."
4046 (when finish-output ;; essential, for ClozureCL, and for standard compliance.
4047 (finish-outputs))
4048 #+(or abcl xcl) (ext:quit :status code)
4049 #+allegro (excl:exit code :quiet t)
4050 #+clisp (ext:quit code)
4051 #+clozure (ccl:quit code)
4052 #+cormanlisp (win32:exitprocess code)
4053 #+(or cmu scl) (unix:unix-exit code)
4054 #+ecl (si:quit code)
4055 #+gcl (system:quit code)
4056 #+genera (error "~S: You probably don't want to Halt Genera. (code: ~S)" 'quit code)
4057 #+lispworks (lispworks:quit :status code :confirm nil :return nil :ignore-errors-p t)
4058 #+mcl (progn code (ccl:quit)) ;; or should we use FFI to call libc's exit(3) ?
4059 #+mkcl (mk-ext:quit :exit-code code)
4060 #+sbcl #.(let ((exit (find-symbol* :exit :sb-ext nil))
4061 (quit (find-symbol* :quit :sb-ext nil)))
4062 (cond
4063 (exit `(,exit :code code :abort (not finish-output)))
4064 (quit `(,quit :unix-status code :recklessly-p (not finish-output)))))
4065 #-(or abcl allegro clisp clozure cmu ecl gcl genera lispworks mcl mkcl sbcl scl xcl)
4066 (error "~S called with exit code ~S but there's no quitting on this implementation" 'quit code))
4068 (defun die (code format &rest arguments)
4069 "Die in error with some error message"
4070 (with-safe-io-syntax ()
4071 (ignore-errors
4072 (format! *stderr* "~&~?~&" format arguments)))
4073 (quit code))
4075 (defun raw-print-backtrace (&key (stream *debug-io*) count condition)
4076 "Print a backtrace, directly accessing the implementation"
4077 (declare (ignorable stream count condition))
4078 #+abcl
4079 (loop :for i :from 0
4080 :for frame :in (sys:backtrace (or count most-positive-fixnum)) :do
4081 (safe-format! stream "~&~D: ~A~%" i frame))
4082 #+allegro
4083 (let ((*terminal-io* stream)
4084 (*standard-output* stream)
4085 (tpl:*zoom-print-circle* *print-circle*)
4086 (tpl:*zoom-print-level* *print-level*)
4087 (tpl:*zoom-print-length* *print-length*))
4088 (tpl:do-command "zoom"
4089 :from-read-eval-print-loop nil
4090 :count (or count t)
4091 :all t))
4092 #+clisp
4093 (system::print-backtrace :out stream :limit count)
4094 #+(or clozure mcl)
4095 (let ((*debug-io* stream))
4096 #+clozure (ccl:print-call-history :count count :start-frame-number 1)
4097 #+mcl (ccl:print-call-history :detailed-p nil)
4098 (finish-output stream))
4099 #+(or cmu scl)
4100 (let ((debug:*debug-print-level* *print-level*)
4101 (debug:*debug-print-length* *print-length*))
4102 (debug:backtrace (or count most-positive-fixnum) stream))
4103 #+(or ecl mkcl)
4104 (let* ((top (si:ihs-top))
4105 (repeats (if count (min top count) top))
4106 (backtrace (loop :for ihs :from 0 :below top
4107 :collect (list (si::ihs-fun ihs)
4108 (si::ihs-env ihs)))))
4109 (loop :for i :from 0 :below repeats
4110 :for frame :in (nreverse backtrace) :do
4111 (safe-format! stream "~&~D: ~S~%" i frame)))
4112 #+gcl
4113 (let ((*debug-io* stream))
4114 (ignore-errors
4115 (with-safe-io-syntax ()
4116 (if condition
4117 (conditions::condition-backtrace condition)
4118 (system::simple-backtrace)))))
4119 #+lispworks
4120 (let ((dbg::*debugger-stack*
4121 (dbg::grab-stack nil :how-many (or count most-positive-fixnum)))
4122 (*debug-io* stream)
4123 (dbg:*debug-print-level* *print-level*)
4124 (dbg:*debug-print-length* *print-length*))
4125 (dbg:bug-backtrace nil))
4126 #+sbcl
4127 (sb-debug:backtrace
4128 #.(if (find-symbol* "*VERBOSITY*" "SB-DEBUG" nil) :stream '(or count most-positive-fixnum))
4129 stream)
4130 #+xcl
4131 (loop :for i :from 0 :below (or count most-positive-fixnum)
4132 :for frame :in (extensions:backtrace-as-list) :do
4133 (safe-format! stream "~&~D: ~S~%" i frame)))
4135 (defun print-backtrace (&rest keys &key stream count condition)
4136 "Print a backtrace"
4137 (declare (ignore stream count condition))
4138 (with-safe-io-syntax (:package :cl)
4139 (let ((*print-readably* nil)
4140 (*print-circle* t)
4141 (*print-miser-width* 75)
4142 (*print-length* nil)
4143 (*print-level* nil)
4144 (*print-pretty* t))
4145 (ignore-errors (apply 'raw-print-backtrace keys)))))
4147 (defun print-condition-backtrace (condition &key (stream *stderr*) count)
4148 "Print a condition after a backtrace triggered by that condition"
4149 ;; We print the condition *after* the backtrace,
4150 ;; for the sake of who sees the backtrace at a terminal.
4151 ;; It is up to the caller to print the condition *before*, with some context.
4152 (print-backtrace :stream stream :count count :condition condition)
4153 (when condition
4154 (safe-format! stream "~&Above backtrace due to this condition:~%~A~&"
4155 condition)))
4157 (defun fatal-condition-p (condition)
4158 "Is the CONDITION fatal? It is if it matches any in *FATAL-CONDITIONS*"
4159 (match-any-condition-p condition *fatal-conditions*))
4161 (defun handle-fatal-condition (condition)
4162 "Handle a fatal CONDITION:
4163 depending on whether *LISP-INTERACTION* is set, enter debugger or die"
4164 (cond
4165 (*lisp-interaction*
4166 (invoke-debugger condition))
4168 (safe-format! *stderr* "~&Fatal condition:~%~A~%" condition)
4169 (print-condition-backtrace condition :stream *stderr*)
4170 (die 99 "~A" condition))))
4172 (defun call-with-fatal-condition-handler (thunk)
4173 "Call THUNK in a context where fatal conditions are appropriately handled"
4174 (handler-bind (((satisfies fatal-condition-p) #'handle-fatal-condition))
4175 (funcall thunk)))
4177 (defmacro with-fatal-condition-handler ((&optional) &body body)
4178 "Execute BODY in a context where fatal conditions are appropriately handled"
4179 `(call-with-fatal-condition-handler #'(lambda () ,@body)))
4181 (defun shell-boolean-exit (x)
4182 "Quit with a return code that is 0 iff argument X is true"
4183 (quit (if x 0 1))))
4186 ;;; Using image hooks
4187 (with-upgradability ()
4188 (defun register-image-restore-hook (hook &optional (call-now-p t))
4189 "Regiter a hook function to be run when restoring a dumped image"
4190 (register-hook-function '*image-restore-hook* hook call-now-p))
4192 (defun register-image-dump-hook (hook &optional (call-now-p nil))
4193 "Register a the hook function to be run before to dump an image"
4194 (register-hook-function '*image-dump-hook* hook call-now-p))
4196 (defun call-image-restore-hook ()
4197 "Call the hook functions registered to be run when restoring a dumped image"
4198 (call-functions (reverse *image-restore-hook*)))
4200 (defun call-image-dump-hook ()
4201 "Call the hook functions registered to be run before to dump an image"
4202 (call-functions *image-dump-hook*)))
4205 ;;; Proper command-line arguments
4206 (with-upgradability ()
4207 (defun raw-command-line-arguments ()
4208 "Find what the actual command line for this process was."
4209 #+abcl ext:*command-line-argument-list* ; Use 1.0.0 or later!
4210 #+allegro (sys:command-line-arguments) ; default: :application t
4211 #+clisp (coerce (ext:argv) 'list)
4212 #+clozure (ccl::command-line-arguments)
4213 #+(or cmu scl) extensions:*command-line-strings*
4214 #+ecl (loop :for i :from 0 :below (si:argc) :collect (si:argv i))
4215 #+gcl si:*command-args*
4216 #+(or genera mcl) nil
4217 #+lispworks sys:*line-arguments-list*
4218 #+mkcl (loop :for i :from 0 :below (mkcl:argc) :collect (mkcl:argv i))
4219 #+sbcl sb-ext:*posix-argv*
4220 #+xcl system:*argv*
4221 #-(or abcl allegro clisp clozure cmu ecl gcl genera lispworks mcl mkcl sbcl scl xcl)
4222 (error "raw-command-line-arguments not implemented yet"))
4224 (defun command-line-arguments (&optional (arguments (raw-command-line-arguments)))
4225 "Extract user arguments from command-line invocation of current process.
4226 Assume the calling conventions of a generated script that uses --
4227 if we are not called from a directly executable image."
4228 (block nil
4229 #+abcl (return arguments)
4230 ;; SBCL and Allegro already separate user arguments from implementation arguments.
4231 #-(or sbcl allegro)
4232 (unless (eq *image-dumped-p* :executable)
4233 ;; LispWorks command-line processing isn't transparent to the user
4234 ;; unless you create a standalone executable; in that case,
4235 ;; we rely on cl-launch or some other script to set the arguments for us.
4236 #+lispworks (return *command-line-arguments*)
4237 ;; On other implementations, on non-standalone executables,
4238 ;; we trust cl-launch or whichever script starts the program
4239 ;; to use -- as a delimiter between implementation arguments and user arguments.
4240 #-lispworks (setf arguments (member "--" arguments :test 'string-equal)))
4241 (rest arguments)))
4243 (defun argv0 ()
4244 "On supported implementations (most that matter), or when invoked by a proper wrapper script,
4245 return a string that for the name with which the program was invoked, i.e. argv[0] in C.
4246 Otherwise, return NIL."
4247 (cond
4248 ((eq *image-dumped-p* :executable) ; yes, this ARGV0 is our argv0 !
4249 ;; NB: not currently available on ABCL, Corman, Genera, MCL
4250 (or #+(or allegro clisp clozure cmu gcl lispworks sbcl scl xcl)
4251 (first (raw-command-line-arguments))
4252 #+ecl (si:argv 0) #+mkcl (mkcl:argv 0)))
4253 (t ;; argv[0] is the name of the interpreter.
4254 ;; The wrapper script can export __CL_ARGV0. cl-launch does as of 4.0.1.8.
4255 (getenvp "__CL_ARGV0"))))
4257 (defun setup-command-line-arguments ()
4258 (setf *command-line-arguments* (command-line-arguments)))
4260 (defun restore-image (&key
4261 (lisp-interaction *lisp-interaction*)
4262 (restore-hook *image-restore-hook*)
4263 (prelude *image-prelude*)
4264 (entry-point *image-entry-point*)
4265 (if-already-restored '(cerror "RUN RESTORE-IMAGE ANYWAY")))
4266 "From a freshly restarted Lisp image, restore the saved Lisp environment
4267 by setting appropriate variables, running various hooks, and calling any specified entry point.
4269 If the image has already been restored or is already being restored, as per *IMAGE-RESTORED-P*,
4270 call the IF-ALREADY-RESTORED error handler (by default, a continuable error), and do return
4271 immediately to the surrounding restore process if allowed to continue.
4273 Then, comes the restore process itself:
4274 First, call each function in the RESTORE-HOOK,
4275 in the order they were registered with REGISTER-IMAGE-RESTORE-HOOK.
4276 Second, evaluate the prelude, which is often Lisp text that is read,
4277 as per EVAL-INPUT.
4278 Third, call the ENTRY-POINT function, if any is specified, with no argument.
4280 The restore process happens in a WITH-FATAL-CONDITION-HANDLER, so that if LISP-INTERACTION is NIL,
4281 any unhandled error leads to a backtrace and an exit with an error status.
4282 If LISP-INTERACTION is NIL, the process also exits when no error occurs:
4283 if neither restart nor entry function is provided, the program will exit with status 0 (success);
4284 if a function was provided, the program will exit after the function returns (if it returns),
4285 with status 0 if and only if the primary return value of result is generalized boolean true,
4286 and with status 1 if this value is NIL.
4288 If LISP-INTERACTION is true, unhandled errors will take you to the debugger, and the result
4289 of the function will be returned rather than interpreted as a boolean designating an exit code."
4290 (when *image-restored-p*
4291 (if if-already-restored
4292 (call-function if-already-restored "Image already ~:[being ~;~]restored"
4293 (eq *image-restored-p* t))
4294 (return-from restore-image)))
4295 (with-fatal-condition-handler ()
4296 (setf *lisp-interaction* lisp-interaction)
4297 (setf *image-restore-hook* restore-hook)
4298 (setf *image-prelude* prelude)
4299 (setf *image-restored-p* :in-progress)
4300 (call-image-restore-hook)
4301 (standard-eval-thunk prelude)
4302 (setf *image-restored-p* t)
4303 (let ((results (multiple-value-list
4304 (if entry-point
4305 (call-function entry-point)
4306 t))))
4307 (if lisp-interaction
4308 (apply 'values results)
4309 (shell-boolean-exit (first results)))))))
4312 ;;; Dumping an image
4314 (with-upgradability ()
4315 (defun dump-image (filename &key output-name executable
4316 (postlude *image-postlude*)
4317 (dump-hook *image-dump-hook*)
4318 #+clozure prepend-symbols #+clozure (purify t)
4319 #+sbcl compression
4320 #+(and sbcl os-windows) application-type)
4321 "Dump an image of the current Lisp environment at pathname FILENAME, with various options.
4323 First, finalize the image, by evaluating the POSTLUDE as per EVAL-INPUT, then calling each of
4324 the functions in DUMP-HOOK, in reverse order of registration by REGISTER-DUMP-HOOK.
4326 If EXECUTABLE is true, create an standalone executable program that calls RESTORE-IMAGE on startup.
4328 Pass various implementation-defined options, such as PREPEND-SYMBOLS and PURITY on CCL,
4329 or COMPRESSION on SBCL, and APPLICATION-TYPE on SBCL/Windows."
4330 ;; Note: at least SBCL saves only global values of variables in the heap image,
4331 ;; so make sure things you want to dump are NOT just local bindings shadowing the global values.
4332 (declare (ignorable filename output-name executable))
4333 (setf *image-dumped-p* (if executable :executable t))
4334 (setf *image-restored-p* :in-regress)
4335 (setf *image-postlude* postlude)
4336 (standard-eval-thunk *image-postlude*)
4337 (setf *image-dump-hook* dump-hook)
4338 (call-image-dump-hook)
4339 (setf *image-restored-p* nil)
4340 #-(or clisp clozure cmu lispworks sbcl scl)
4341 (when executable
4342 (error "Dumping an executable is not supported on this implementation! Aborting."))
4343 #+allegro
4344 (progn
4345 (sys:resize-areas :global-gc t :pack-heap t :sift-old-areas t :tenure t) ; :new 5000000
4346 (excl:dumplisp :name filename :suppress-allegro-cl-banner t))
4347 #+clisp
4348 (apply #'ext:saveinitmem filename
4349 :quiet t
4350 :start-package *package*
4351 :keep-global-handlers nil
4352 :executable (if executable 0 t) ;--- requires clisp 2.48 or later, still catches --clisp-x
4353 (when executable
4354 (list
4355 ;; :parse-options nil ;--- requires a non-standard patch to clisp.
4356 :norc t :script nil :init-function #'restore-image)))
4357 #+clozure
4358 (flet ((dump (prepend-kernel)
4359 (ccl:save-application filename :prepend-kernel prepend-kernel :purify purify
4360 :toplevel-function (when executable #'restore-image))))
4361 ;;(setf ccl::*application* (make-instance 'ccl::lisp-development-system))
4362 (if prepend-symbols
4363 (with-temporary-file (:prefix "ccl-symbols-" :direction :output :pathname path)
4364 (require 'elf)
4365 (funcall (fdefinition 'ccl::write-elf-symbols-to-file) path)
4366 (dump path))
4367 (dump t)))
4368 #+(or cmu scl)
4369 (progn
4370 (ext:gc :full t)
4371 (setf ext:*batch-mode* nil)
4372 (setf ext::*gc-run-time* 0)
4373 (apply 'ext:save-lisp filename
4374 #+cmu :executable #+cmu t
4375 (when executable '(:init-function restore-image :process-command-line nil))))
4376 #+gcl
4377 (progn
4378 (si::set-hole-size 500) (si::gbc nil) (si::sgc-on t)
4379 (si::save-system filename))
4380 #+lispworks
4381 (if executable
4382 (lispworks:deliver 'restore-image filename 0 :interface nil)
4383 (hcl:save-image filename :environment nil))
4384 #+sbcl
4385 (progn
4386 ;;(sb-pcl::precompile-random-code-segments) ;--- it is ugly slow at compile-time (!) when the initial core is a big CLOS program. If you want it, do it yourself
4387 (setf sb-ext::*gc-run-time* 0)
4388 (apply 'sb-ext:save-lisp-and-die filename
4389 :executable t ;--- always include the runtime that goes with the core
4390 (append
4391 (when compression (list :compression compression))
4392 ;;--- only save runtime-options for standalone executables
4393 (when executable (list :toplevel #'restore-image :save-runtime-options t))
4394 #+(and sbcl os-windows) ;; passing :application-type :gui will disable the console window.
4395 ;; the default is :console - only works with SBCL 1.1.15 or later.
4396 (when application-type (list :application-type application-type)))))
4397 #-(or allegro clisp clozure cmu gcl lispworks sbcl scl)
4398 (error "Can't ~S ~S: UIOP doesn't support image dumping with ~A.~%"
4399 'dump-image filename (nth-value 1 (implementation-type))))
4401 (defun create-image (destination lisp-object-files
4402 &key kind output-name prologue-code epilogue-code extra-object-files
4403 (prelude () preludep) (postlude () postludep)
4404 (entry-point () entry-point-p) build-args no-uiop)
4405 (declare (ignorable destination lisp-object-files extra-object-files kind output-name
4406 prologue-code epilogue-code prelude preludep postlude postludep
4407 entry-point entry-point-p build-args no-uiop))
4408 "On ECL, create an executable at pathname DESTINATION from the specified OBJECT-FILES and options"
4409 ;; Is it meaningful to run these in the current environment?
4410 ;; only if we also track the object files that constitute the "current" image,
4411 ;; and otherwise simulate dump-image, including quitting at the end.
4412 #-(or ecl mkcl) (error "~S not implemented for your implementation (yet)" 'create-image)
4413 #+(or ecl mkcl)
4414 (let ((epilogue-code
4415 (if no-uiop
4416 epilogue-code
4417 (let ((forms
4418 (append
4419 (when epilogue-code `(,epilogue-code))
4420 (when postludep `((setf *image-postlude* ',postlude)))
4421 (when preludep `((setf *image-prelude* ',prelude)))
4422 (when entry-point-p `((setf *image-entry-point* ',entry-point)))
4423 (case kind
4424 ((:image)
4425 (setf kind :program) ;; to ECL, it's just another program.
4426 `((setf *image-dumped-p* t)
4427 (si::top-level #+ecl t) (quit)))
4428 ((:program)
4429 `((setf *image-dumped-p* :executable)
4430 (shell-boolean-exit
4431 (restore-image))))))))
4432 (when forms `(progn ,@forms))))))
4433 #+ecl (check-type kind (member :dll :lib :static-library :program :object :fasl))
4434 (apply #+ecl 'c::builder #+ecl kind
4435 #+mkcl (ecase kind
4436 ((:dll) 'compiler::build-shared-library)
4437 ((:lib :static-library) 'compiler::build-static-library)
4438 ((:fasl) 'compiler::build-bundle)
4439 ((:program) 'compiler::build-program))
4440 (pathname destination)
4441 #+ecl :lisp-files #+mkcl :lisp-object-files (append lisp-object-files #+ecl extra-object-files)
4442 #+ecl :init-name #+ecl (c::compute-init-name (or output-name destination) :kind kind)
4443 (append
4444 (when prologue-code `(:prologue-code ,prologue-code))
4445 (when epilogue-code `(:epilogue-code ,epilogue-code))
4446 #+mkcl (when extra-object-files `(:object-files ,extra-object-files))
4447 build-args)))))
4450 ;;; Some universal image restore hooks
4451 (with-upgradability ()
4452 (map () 'register-image-restore-hook
4453 '(setup-stdin setup-stdout setup-stderr
4454 setup-command-line-arguments setup-temporary-directory
4455 #+abcl detect-os)))
4456 ;;;; -------------------------------------------------------------------------
4457 ;;;; run-program initially from xcvb-driver.
4459 (uiop/package:define-package :uiop/run-program
4460 (:nicknames :asdf/run-program)
4461 (:recycle :uiop/run-program :asdf/run-program :xcvb-driver)
4462 (:use :uiop/common-lisp :uiop/package :uiop/utility
4463 :uiop/pathname :uiop/os :uiop/filesystem :uiop/stream)
4464 (:export
4465 ;;; Escaping the command invocation madness
4466 #:easy-sh-character-p #:escape-sh-token #:escape-sh-command
4467 #:escape-windows-token #:escape-windows-command
4468 #:escape-token #:escape-command
4470 ;;; run-program
4471 #:slurp-input-stream #:vomit-output-stream
4472 #:run-program
4473 #:subprocess-error
4474 #:subprocess-error-code #:subprocess-error-command #:subprocess-error-process
4476 (in-package :uiop/run-program)
4478 ;;;; ----- Escaping strings for the shell -----
4480 (with-upgradability ()
4481 (defun requires-escaping-p (token &key good-chars bad-chars)
4482 "Does this token require escaping, given the specification of
4483 either good chars that don't need escaping or bad chars that do need escaping,
4484 as either a recognizing function or a sequence of characters."
4485 (some
4486 (cond
4487 ((and good-chars bad-chars)
4488 (error "only one of good-chars and bad-chars can be provided"))
4489 ((functionp good-chars)
4490 (complement good-chars))
4491 ((functionp bad-chars)
4492 bad-chars)
4493 ((and good-chars (typep good-chars 'sequence))
4494 #'(lambda (c) (not (find c good-chars))))
4495 ((and bad-chars (typep bad-chars 'sequence))
4496 #'(lambda (c) (find c bad-chars)))
4497 (t (error "requires-escaping-p: no good-char criterion")))
4498 token))
4500 (defun escape-token (token &key stream quote good-chars bad-chars escaper)
4501 "Call the ESCAPER function on TOKEN string if it needs escaping as per
4502 REQUIRES-ESCAPING-P using GOOD-CHARS and BAD-CHARS, otherwise output TOKEN,
4503 using STREAM as output (or returning result as a string if NIL)"
4504 (if (requires-escaping-p token :good-chars good-chars :bad-chars bad-chars)
4505 (with-output (stream)
4506 (apply escaper token stream (when quote `(:quote ,quote))))
4507 (output-string token stream)))
4509 (defun escape-windows-token-within-double-quotes (x &optional s)
4510 "Escape a string token X within double-quotes
4511 for use within a MS Windows command-line, outputing to S."
4512 (labels ((issue (c) (princ c s))
4513 (issue-backslash (n) (loop :repeat n :do (issue #\\))))
4514 (loop
4515 :initially (issue #\") :finally (issue #\")
4516 :with l = (length x) :with i = 0
4517 :for i+1 = (1+ i) :while (< i l) :do
4518 (case (char x i)
4519 ((#\") (issue-backslash 1) (issue #\") (setf i i+1))
4520 ((#\\)
4521 (let* ((j (and (< i+1 l) (position-if-not
4522 #'(lambda (c) (eql c #\\)) x :start i+1)))
4523 (n (- (or j l) i)))
4524 (cond
4525 ((null j)
4526 (issue-backslash (* 2 n)) (setf i l))
4527 ((and (< j l) (eql (char x j) #\"))
4528 (issue-backslash (1+ (* 2 n))) (issue #\") (setf i (1+ j)))
4530 (issue-backslash n) (setf i j)))))
4531 (otherwise
4532 (issue (char x i)) (setf i i+1))))))
4534 (defun escape-windows-token (token &optional s)
4535 "Escape a string TOKEN within double-quotes if needed
4536 for use within a MS Windows command-line, outputing to S."
4537 (escape-token token :stream s :bad-chars #(#\space #\tab #\") :quote nil
4538 :escaper 'escape-windows-token-within-double-quotes))
4540 (defun escape-sh-token-within-double-quotes (x s &key (quote t))
4541 "Escape a string TOKEN within double-quotes
4542 for use within a POSIX Bourne shell, outputing to S;
4543 omit the outer double-quotes if key argument :QUOTE is NIL"
4544 (when quote (princ #\" s))
4545 (loop :for c :across x :do
4546 (when (find c "$`\\\"") (princ #\\ s))
4547 (princ c s))
4548 (when quote (princ #\" s)))
4550 (defun easy-sh-character-p (x)
4551 "Is X an \"easy\" character that does not require quoting by the shell?"
4552 (or (alphanumericp x) (find x "+-_.,%@:/")))
4554 (defun escape-sh-token (token &optional s)
4555 "Escape a string TOKEN within double-quotes if needed
4556 for use within a POSIX Bourne shell, outputing to S."
4557 (escape-token token :stream s :quote #\" :good-chars #'easy-sh-character-p
4558 :escaper 'escape-sh-token-within-double-quotes))
4560 (defun escape-shell-token (token &optional s)
4561 "Escape a token for the current operating system shell"
4562 (cond
4563 ((os-unix-p) (escape-sh-token token s))
4564 ((os-windows-p) (escape-windows-token token s))))
4566 (defun escape-command (command &optional s
4567 (escaper 'escape-shell-token))
4568 "Given a COMMAND as a list of tokens, return a string of the
4569 spaced, escaped tokens, using ESCAPER to escape."
4570 (etypecase command
4571 (string (output-string command s))
4572 (list (with-output (s)
4573 (loop :for first = t :then nil :for token :in command :do
4574 (unless first (princ #\space s))
4575 (funcall escaper token s))))))
4577 (defun escape-windows-command (command &optional s)
4578 "Escape a list of command-line arguments into a string suitable for parsing
4579 by CommandLineToArgv in MS Windows"
4580 ;; http://msdn.microsoft.com/en-us/library/bb776391(v=vs.85).aspx
4581 ;; http://msdn.microsoft.com/en-us/library/17w5ykft(v=vs.85).aspx
4582 (escape-command command s 'escape-windows-token))
4584 (defun escape-sh-command (command &optional s)
4585 "Escape a list of command-line arguments into a string suitable for parsing
4586 by /bin/sh in POSIX"
4587 (escape-command command s 'escape-sh-token))
4589 (defun escape-shell-command (command &optional stream)
4590 "Escape a command for the current operating system's shell"
4591 (escape-command command stream 'escape-shell-token)))
4594 ;;;; Slurping a stream, typically the output of another program
4595 (with-upgradability ()
4596 (defun call-stream-processor (fun processor stream)
4597 "Given FUN (typically SLURP-INPUT-STREAM or VOMIT-OUTPUT-STREAM,
4598 a PROCESSOR specification which is either an atom or a list specifying
4599 a processor an keyword arguments, call the specified processor with
4600 the given STREAM as input"
4601 (if (consp processor)
4602 (apply fun (first processor) stream (rest processor))
4603 (funcall fun processor stream)))
4605 (defgeneric slurp-input-stream (processor input-stream &key)
4606 (:documentation
4607 "SLURP-INPUT-STREAM is a generic function with two positional arguments
4608 PROCESSOR and INPUT-STREAM and additional keyword arguments, that consumes (slurps)
4609 the contents of the INPUT-STREAM and processes them according to a method
4610 specified by PROCESSOR.
4612 Built-in methods include the following:
4613 * if PROCESSOR is a function, it is called with the INPUT-STREAM as its argument
4614 * if PROCESSOR is a list, its first element should be a function. It will be applied to a cons of the
4615 INPUT-STREAM and the rest of the list. That is (x . y) will be treated as
4616 \(APPLY x <stream> y\)
4617 * if PROCESSOR is an output-stream, the contents of INPUT-STREAM is copied to the output-stream,
4618 per copy-stream-to-stream, with appropriate keyword arguments.
4619 * if PROCESSOR is the symbol CL:STRING or the keyword :STRING, then the contents of INPUT-STREAM
4620 are returned as a string, as per SLURP-STREAM-STRING.
4621 * if PROCESSOR is the keyword :LINES then the INPUT-STREAM will be handled by SLURP-STREAM-LINES.
4622 * if PROCESSOR is the keyword :LINE then the INPUT-STREAM will be handled by SLURP-STREAM-LINE.
4623 * if PROCESSOR is the keyword :FORMS then the INPUT-STREAM will be handled by SLURP-STREAM-FORMS.
4624 * if PROCESSOR is the keyword :FORM then the INPUT-STREAM will be handled by SLURP-STREAM-FORM.
4625 * if PROCESSOR is T, it is treated the same as *standard-output*. If it is NIL, NIL is returned.
4627 Programmers are encouraged to define their own methods for this generic function."))
4629 #-genera
4630 (defmethod slurp-input-stream ((function function) input-stream &key)
4631 (funcall function input-stream))
4633 (defmethod slurp-input-stream ((list cons) input-stream &key)
4634 (apply (first list) input-stream (rest list)))
4636 #-genera
4637 (defmethod slurp-input-stream ((output-stream stream) input-stream
4638 &key linewise prefix (element-type 'character) buffer-size)
4639 (copy-stream-to-stream
4640 input-stream output-stream
4641 :linewise linewise :prefix prefix :element-type element-type :buffer-size buffer-size))
4643 (defmethod slurp-input-stream ((x (eql 'string)) stream &key stripped)
4644 (slurp-stream-string stream :stripped stripped))
4646 (defmethod slurp-input-stream ((x (eql :string)) stream &key stripped)
4647 (slurp-stream-string stream :stripped stripped))
4649 (defmethod slurp-input-stream ((x (eql :lines)) stream &key count)
4650 (slurp-stream-lines stream :count count))
4652 (defmethod slurp-input-stream ((x (eql :line)) stream &key (at 0))
4653 (slurp-stream-line stream :at at))
4655 (defmethod slurp-input-stream ((x (eql :forms)) stream &key count)
4656 (slurp-stream-forms stream :count count))
4658 (defmethod slurp-input-stream ((x (eql :form)) stream &key (at 0))
4659 (slurp-stream-form stream :at at))
4661 (defmethod slurp-input-stream ((x (eql t)) stream &rest keys &key &allow-other-keys)
4662 (apply 'slurp-input-stream *standard-output* stream keys))
4664 (defmethod slurp-input-stream ((x null) (stream t) &key)
4665 nil)
4667 (defmethod slurp-input-stream ((pathname pathname) input
4668 &key
4669 (element-type *default-stream-element-type*)
4670 (external-format *utf-8-external-format*)
4671 (if-exists :rename-and-delete)
4672 (if-does-not-exist :create)
4673 buffer-size
4674 linewise)
4675 (with-output-file (output pathname
4676 :element-type element-type
4677 :external-format external-format
4678 :if-exists if-exists
4679 :if-does-not-exist if-does-not-exist)
4680 (copy-stream-to-stream
4681 input output
4682 :element-type element-type :buffer-size buffer-size :linewise linewise)))
4684 (defmethod slurp-input-stream (x stream
4685 &key linewise prefix (element-type 'character) buffer-size)
4686 (declare (ignorable stream linewise prefix element-type buffer-size))
4687 (cond
4688 #+genera
4689 ((functionp x) (funcall x stream))
4690 #+genera
4691 ((output-stream-p x)
4692 (copy-stream-to-stream
4693 stream x
4694 :linewise linewise :prefix prefix :element-type element-type :buffer-size buffer-size))
4696 (error "Invalid ~S destination ~S" 'slurp-input-stream x)))))
4699 (with-upgradability ()
4700 (defgeneric vomit-output-stream (processor output-stream &key)
4701 (:documentation
4702 "VOMIT-OUTPUT-STREAM is a generic function with two positional arguments
4703 PROCESSOR and OUTPUT-STREAM and additional keyword arguments, that produces (vomits)
4704 some content onto the OUTPUT-STREAM, according to a method specified by PROCESSOR.
4706 Built-in methods include the following:
4707 * if PROCESSOR is a function, it is called with the OUTPUT-STREAM as its argument
4708 * if PROCESSOR is a list, its first element should be a function.
4709 It will be applied to a cons of the OUTPUT-STREAM and the rest of the list.
4710 That is (x . y) will be treated as \(APPLY x <stream> y\)
4711 * if PROCESSOR is an input-stream, its contents will be copied the OUTPUT-STREAM,
4712 per copy-stream-to-stream, with appropriate keyword arguments.
4713 * if PROCESSOR is a string, its contents will be printed to the OUTPUT-STREAM.
4714 * if PROCESSOR is T, it is treated the same as *standard-input*. If it is NIL, nothing is done.
4716 Programmers are encouraged to define their own methods for this generic function."))
4718 #-genera
4719 (defmethod vomit-output-stream ((function function) output-stream &key)
4720 (funcall function output-stream))
4722 (defmethod vomit-output-stream ((list cons) output-stream &key)
4723 (apply (first list) output-stream (rest list)))
4725 #-genera
4726 (defmethod vomit-output-stream ((input-stream stream) output-stream
4727 &key linewise prefix (element-type 'character) buffer-size)
4728 (copy-stream-to-stream
4729 input-stream output-stream
4730 :linewise linewise :prefix prefix :element-type element-type :buffer-size buffer-size))
4732 (defmethod vomit-output-stream ((x string) stream &key fresh-line terpri)
4733 (princ x stream)
4734 (when fresh-line (fresh-line stream))
4735 (when terpri (terpri stream))
4736 (values))
4738 (defmethod vomit-output-stream ((x (eql t)) stream &rest keys &key &allow-other-keys)
4739 (apply 'vomit-output-stream *standard-input* stream keys))
4741 (defmethod vomit-output-stream ((x null) (stream t) &key)
4742 (values))
4744 (defmethod vomit-output-stream ((pathname pathname) input
4745 &key
4746 (element-type *default-stream-element-type*)
4747 (external-format *utf-8-external-format*)
4748 (if-exists :rename-and-delete)
4749 (if-does-not-exist :create)
4750 buffer-size
4751 linewise)
4752 (with-output-file (output pathname
4753 :element-type element-type
4754 :external-format external-format
4755 :if-exists if-exists
4756 :if-does-not-exist if-does-not-exist)
4757 (copy-stream-to-stream
4758 input output
4759 :element-type element-type :buffer-size buffer-size :linewise linewise)))
4761 (defmethod vomit-output-stream (x stream
4762 &key linewise prefix (element-type 'character) buffer-size)
4763 (declare (ignorable stream linewise prefix element-type buffer-size))
4764 (cond
4765 #+genera
4766 ((functionp x) (funcall x stream))
4767 #+genera
4768 ((input-stream-p x)
4769 (copy-stream-to-stream
4770 x stream
4771 :linewise linewise :prefix prefix :element-type element-type :buffer-size buffer-size))
4773 (error "Invalid ~S source ~S" 'vomit-output-stream x)))))
4776 ;;;; ----- Running an external program -----
4777 ;;; Simple variant of run-program with no input, and capturing output
4778 ;;; On some implementations, may output to a temporary file...
4779 (with-upgradability ()
4780 (define-condition subprocess-error (error)
4781 ((code :initform nil :initarg :code :reader subprocess-error-code)
4782 (command :initform nil :initarg :command :reader subprocess-error-command)
4783 (process :initform nil :initarg :process :reader subprocess-error-process))
4784 (:report (lambda (condition stream)
4785 (format stream "Subprocess~@[ ~S~]~@[ run with command ~S~] exited with error~@[ code ~D~]"
4786 (subprocess-error-process condition)
4787 (subprocess-error-command condition)
4788 (subprocess-error-code condition)))))
4790 ;;; Internal helpers for run-program
4791 (defun %normalize-command (command)
4792 "Given a COMMAND as a list or string, transform it in a format suitable
4793 for the implementation's underlying run-program function"
4794 (etypecase command
4795 #+os-unix (string `("/bin/sh" "-c" ,command))
4796 #+os-unix (list command)
4797 #+os-windows
4798 (string
4799 #+mkcl (list "cmd" '#:/c command)
4800 ;; NB: We do NOT add cmd /c here. You might want to.
4801 #+(or allegro clisp) command
4802 ;; On ClozureCL for Windows, we assume you are using
4803 ;; r15398 or later in 1.9 or later,
4804 ;; so that bug 858 is fixed http://trac.clozure.com/ccl/ticket/858
4805 #+clozure (cons "cmd" (strcat "/c " command))
4806 ;; NB: On other Windows implementations, this is utterly bogus
4807 ;; except in the most trivial cases where no quoting is needed.
4808 ;; Use at your own risk.
4809 #-(or allegro clisp clozure mkcl) (list "cmd" "/c" command))
4810 #+os-windows
4811 (list
4812 #+allegro (escape-windows-command command)
4813 #-allegro command)))
4815 (defun %active-io-specifier-p (specifier)
4816 "Determines whether a run-program I/O specifier requires Lisp-side processing
4817 via SLURP-INPUT-STREAM or VOMIT-OUTPUT-STREAM (return T),
4818 or whether it's already taken care of by the implementation's underlying run-program."
4819 (not (typep specifier '(or null string pathname (member :interactive :output)
4820 #+(or cmu (and sbcl os-unix) scl) (or stream (eql t))
4821 #+lispworks file-stream)))) ;; not a type!? comm:socket-stream
4823 (defun %normalize-io-specifier (specifier &optional role)
4824 "Normalizes a portable I/O specifier for %RUN-PROGRAM into an implementation-dependent
4825 argument to pass to the internal RUN-PROGRAM"
4826 (declare (ignorable role))
4827 (etypecase specifier
4828 (null (or #+(or allegro lispworks) (null-device-pathname)))
4829 (string (parse-native-namestring specifier))
4830 (pathname specifier)
4831 (stream specifier)
4832 ((eql :stream) :stream)
4833 ((eql :interactive)
4834 #+allegro nil
4835 #+clisp :terminal
4836 #+(or clozure cmu ecl mkcl sbcl scl) t)
4837 #+(or allegro clozure cmu ecl lispworks mkcl sbcl scl)
4838 ((eql :output)
4839 (if (eq role :error-output)
4840 :output
4841 (error "Wrong specifier ~S for role ~S" specifier role)))))
4843 (defun %interactivep (input output error-output)
4844 (member :interactive (list input output error-output)))
4846 #+clisp
4847 (defun clisp-exit-code (raw-exit-code)
4848 (typecase raw-exit-code
4849 (null 0) ; no error
4850 (integer raw-exit-code) ; negative: signal
4851 (t -1)))
4853 (defun %run-program (command
4854 &rest keys
4855 &key input (if-input-does-not-exist :error)
4856 output (if-output-exists :overwrite)
4857 error-output (if-error-output-exists :overwrite)
4858 directory wait
4859 #+allegro separate-streams
4860 &allow-other-keys)
4861 "A portable abstraction of a low-level call to the implementation's run-program or equivalent.
4862 It spawns a subprocess that runs the specified COMMAND (a list of program and arguments).
4863 INPUT, OUTPUT and ERROR-OUTPUT specify a portable IO specifer,
4864 to be normalized by %NORMALIZE-IO-SPECIFIER.
4865 It returns a process-info plist with possible keys:
4866 PROCESS, EXIT-CODE, INPUT-STREAM, OUTPUT-STREAM, BIDIR-STREAM, ERROR-STREAM."
4867 ;; NB: these implementations have unix vs windows set at compile-time.
4868 (declare (ignorable directory if-input-does-not-exist if-output-exists if-error-output-exists))
4869 (assert (not (and wait (member :stream (list input output error-output)))))
4870 #-(or allegro clisp clozure cmu (and lispworks os-unix) mkcl sbcl scl)
4871 (progn command keys directory
4872 (error "run-program not available"))
4873 #+(or allegro clisp clozure cmu (and lispworks os-unix) mkcl sbcl scl)
4874 (let* ((%command (%normalize-command command))
4875 (%input (%normalize-io-specifier input :input))
4876 (%output (%normalize-io-specifier output :output))
4877 (%error-output (%normalize-io-specifier error-output :error-output))
4878 #+(and allegro os-windows) (interactive (%interactivep input output error-output))
4879 (process*
4880 #+allegro
4881 (multiple-value-list
4882 (apply
4883 'excl:run-shell-command
4884 #+os-unix (coerce (cons (first %command) %command) 'vector)
4885 #+os-windows %command
4886 :input %input
4887 :output %output
4888 :error-output %error-output
4889 :directory directory :wait wait
4890 #+os-windows :show-window #+os-windows (if interactive nil :hide)
4891 :allow-other-keys t keys))
4892 #-allegro
4893 (with-current-directory (#-(or sbcl mkcl) directory)
4894 #+clisp
4895 (flet ((run (f x &rest args)
4896 (multiple-value-list
4897 (apply f x :input %input :output %output
4898 :allow-other-keys t `(,@args ,@keys)))))
4899 (assert (eq %error-output :terminal))
4900 ;;; since we now always return a code, we can't use this code path, anyway!
4901 (etypecase %command
4902 #+os-windows (string (run 'ext:run-shell-command %command))
4903 (list (run 'ext:run-program (car %command)
4904 :arguments (cdr %command)))))
4905 #+(or clozure cmu ecl mkcl sbcl scl)
4906 (#-(or ecl mkcl) progn #+(or ecl mkcl) multiple-value-list
4907 (apply
4908 '#+(or cmu ecl scl) ext:run-program
4909 #+clozure ccl:run-program #+sbcl sb-ext:run-program #+mkcl mk-ext:run-program
4910 (car %command) (cdr %command)
4911 :input %input
4912 :output %output
4913 :error %error-output
4914 :wait wait
4915 :allow-other-keys t
4916 (append
4917 #+(or clozure cmu mkcl sbcl scl)
4918 `(:if-input-does-not-exist ,if-input-does-not-exist
4919 :if-output-exists ,if-output-exists
4920 :if-error-exists ,if-error-output-exists)
4921 #+sbcl `(:search t
4922 :if-output-does-not-exist :create
4923 :if-error-does-not-exist :create)
4924 #-sbcl keys #+sbcl (if directory keys (remove-plist-key :directory keys)))))
4925 #+(and lispworks os-unix) ;; note: only used on Unix in non-interactive case
4926 (multiple-value-list
4927 (apply
4928 'system:run-shell-command
4929 (cons "/usr/bin/env" %command) ; lispworks wants a full path.
4930 :input %input :if-input-does-not-exist if-input-does-not-exist
4931 :output %output :if-output-exists if-output-exists
4932 :error-output %error-output :if-error-output-exists if-error-output-exists
4933 :wait wait :save-exit-status t :allow-other-keys t keys))))
4934 (process-info-r ()))
4935 (flet ((prop (key value) (push key process-info-r) (push value process-info-r)))
4936 #+allegro
4937 (cond
4938 (wait (prop :exit-code (first process*)))
4939 (separate-streams
4940 (destructuring-bind (in out err pid) process*
4941 (prop :process pid)
4942 (when (eq input :stream) (prop :input-stream in))
4943 (when (eq output :stream) (prop :output-stream out))
4944 (when (eq error-output :stream) (prop :error-stream err))))
4946 (prop :process (third process*))
4947 (let ((x (first process*)))
4948 (ecase (+ (if (eq input :stream) 1 0) (if (eq output :stream) 2 0))
4950 (1 (prop :input-stream x))
4951 (2 (prop :output-stream x))
4952 (3 (prop :bidir-stream x))))
4953 (when (eq error-output :stream)
4954 (prop :error-stream (second process*)))))
4955 #+clisp
4956 (cond
4957 (wait (prop :exit-code (clisp-exit-code (first process*))))
4959 (ecase (+ (if (eq input :stream) 1 0) (if (eq output :stream) 2 0))
4961 (1 (prop :input-stream (first process*)))
4962 (2 (prop :output-stream (first process*)))
4963 (3 (prop :bidir-stream (pop process*))
4964 (prop :input-stream (pop process*))
4965 (prop :output-stream (pop process*))))))
4966 #+(or clozure cmu sbcl scl)
4967 (progn
4968 (prop :process process*)
4969 (when (eq input :stream)
4970 (prop :input-stream
4971 #+clozure (ccl:external-process-input-stream process*)
4972 #+(or cmu scl) (ext:process-input process*)
4973 #+sbcl (sb-ext:process-input process*)))
4974 (when (eq output :stream)
4975 (prop :output-stream
4976 #+clozure (ccl:external-process-output-stream process*)
4977 #+(or cmu scl) (ext:process-output process*)
4978 #+sbcl (sb-ext:process-output process*)))
4979 (when (eq error-output :stream)
4980 (prop :error-output-stream
4981 #+clozure (ccl:external-process-error-stream process*)
4982 #+(or cmu scl) (ext:process-error process*)
4983 #+sbcl (sb-ext:process-error process*))))
4984 #+(or ecl mkcl)
4985 (destructuring-bind #+ecl (stream code process) #+mkcl (stream process code) process*
4986 (let ((mode (+ (if (eq input :stream) 1 0) (if (eq output :stream) 2 0))))
4987 (cond
4988 ((zerop mode))
4989 ((null process*) (prop :exit-code -1))
4990 (t (prop (case mode (1 :input-stream) (2 :output-stream) (3 :bidir-stream)) stream))))
4991 (when code (prop :exit-code code))
4992 (when process (prop :process process)))
4993 #+lispworks
4994 (if wait
4995 (prop :exit-code (first process*))
4996 (let ((mode (+ (if (eq input :stream) 1 0) (if (eq output :stream) 2 0))))
4997 (if (zerop mode)
4998 (prop :process (first process*))
4999 (destructuring-bind (x err pid) process*
5000 (prop :process pid)
5001 (prop (ecase mode (1 :input-stream) (2 :output-stream) (3 :bidir-stream)) x)
5002 (when (eq error-output :stream) (prop :error-stream err))))))
5003 (nreverse process-info-r))))
5005 (defun %process-info-pid (process-info)
5006 (let ((process (getf process-info :process)))
5007 (declare (ignorable process))
5008 #+(or allegro lispworks) process
5009 #+clozure (ccl::external-process-pid process)
5010 #+ecl (si:external-process-pid process)
5011 #+(or cmu scl) (ext:process-pid process)
5012 #+mkcl (mkcl:process-id process)
5013 #+sbcl (sb-ext:process-pid process)
5014 #-(or allegro cmu mkcl sbcl scl) (error "~S not implemented" '%process-info-pid)))
5016 (defun %wait-process-result (process-info)
5017 (or (getf process-info :exit-code)
5018 (let ((process (getf process-info :process)))
5019 (when process
5020 ;; 1- wait
5021 #+clozure (ccl::external-process-wait process)
5022 #+(or cmu scl) (ext:process-wait process)
5023 #+(and ecl os-unix) (ext:external-process-wait process)
5024 #+sbcl (sb-ext:process-wait process)
5025 ;; 2- extract result
5026 #+allegro (sys:reap-os-subprocess :pid process :wait t)
5027 #+clozure (nth-value 1 (ccl:external-process-status process))
5028 #+(or cmu scl) (ext:process-exit-code process)
5029 #+ecl (nth-value 1 (ext:external-process-status process))
5030 #+lispworks
5031 (if-let ((stream (or (getf process-info :input-stream)
5032 (getf process-info :output-stream)
5033 (getf process-info :bidir-stream)
5034 (getf process-info :error-stream))))
5035 (system:pipe-exit-status stream :wait t)
5036 (if-let ((f (find-symbol* :pid-exit-status :system nil)))
5037 (funcall f process :wait t)))
5038 #+sbcl (sb-ext:process-exit-code process)
5039 #+mkcl (mkcl:join-process process)))))
5041 (defun %check-result (exit-code &key command process ignore-error-status)
5042 (unless ignore-error-status
5043 (unless (eql exit-code 0)
5044 (cerror "IGNORE-ERROR-STATUS"
5045 'subprocess-error :command command :code exit-code :process process)))
5046 exit-code)
5048 (defun %call-with-program-io (gf tval stream-easy-p fun direction spec activep returner
5049 &key element-type external-format &allow-other-keys)
5050 ;; handle redirection for run-program and system
5051 ;; SPEC is the specification for the subprocess's input or output or error-output
5052 ;; TVAL is the value used if the spec is T
5053 ;; GF is the generic function to call to handle arbitrary values of SPEC
5054 ;; STREAM-EASY-P is T if we're going to use a RUN-PROGRAM that copies streams in the background
5055 ;; (it's only meaningful on CMUCL, SBCL, SCL that actually do it)
5056 ;; DIRECTION is :INPUT, :OUTPUT or :ERROR-OUTPUT for the direction of this io argument
5057 ;; FUN is a function of the new reduced spec and an activity function to call with a stream
5058 ;; when the subprocess is active and communicating through that stream.
5059 ;; ACTIVEP is a boolean true if we will get to run code while the process is running
5060 ;; ELEMENT-TYPE and EXTERNAL-FORMAT control what kind of temporary file we may open.
5061 ;; RETURNER is a function called with the value of the activity.
5062 ;; --- TODO (fare@tunes.org): handle if-output-exists and such when doing it the hard way.
5063 (declare (ignorable stream-easy-p))
5064 (let* ((actual-spec (if (eq spec t) tval spec))
5065 (activity-spec (if (eq actual-spec :output)
5066 (ecase direction
5067 ((:input :output)
5068 (error "~S not allowed as a ~S ~S spec"
5069 :output 'run-program direction))
5070 ((:error-output)
5071 nil))
5072 actual-spec)))
5073 (labels ((activity (stream)
5074 (call-function returner (call-stream-processor gf activity-spec stream)))
5075 (easy-case ()
5076 (funcall fun actual-spec nil))
5077 (hard-case ()
5078 (if activep
5079 (funcall fun :stream #'activity)
5080 (with-temporary-file (:pathname tmp)
5081 (ecase direction
5082 (:input
5083 (with-output-file (s tmp :if-exists :overwrite
5084 :external-format external-format
5085 :element-type element-type)
5086 (activity s))
5087 (funcall fun tmp nil))
5088 ((:output :error-output)
5089 (multiple-value-prog1 (funcall fun tmp nil)
5090 (with-input-file (s tmp
5091 :external-format external-format
5092 :element-type element-type)
5093 (activity s)))))))))
5094 (typecase activity-spec
5095 ((or null string pathname (eql :interactive))
5096 (easy-case))
5097 #+(or cmu (and sbcl os-unix) scl) ;; streams are only easy on implementations that try very hard
5098 (stream
5099 (if stream-easy-p (easy-case) (hard-case)))
5101 (hard-case))))))
5103 (defmacro place-setter (place)
5104 (when place
5105 (let ((value (gensym)))
5106 `#'(lambda (,value) (setf ,place ,value)))))
5108 (defmacro with-program-input (((reduced-input-var
5109 &optional (input-activity-var (gensym) iavp))
5110 input-form &key setf stream-easy-p active keys) &body body)
5111 `(apply '%call-with-program-io 'vomit-output-stream *standard-input* ,stream-easy-p
5112 #'(lambda (,reduced-input-var ,input-activity-var)
5113 ,@(unless iavp `((declare (ignore ,input-activity-var))))
5114 ,@body)
5115 :input ,input-form ,active (place-setter ,setf) ,keys))
5117 (defmacro with-program-output (((reduced-output-var
5118 &optional (output-activity-var (gensym) oavp))
5119 output-form &key setf stream-easy-p active keys) &body body)
5120 `(apply '%call-with-program-io 'slurp-input-stream *standard-output* ,stream-easy-p
5121 #'(lambda (,reduced-output-var ,output-activity-var)
5122 ,@(unless oavp `((declare (ignore ,output-activity-var))))
5123 ,@body)
5124 :output ,output-form ,active (place-setter ,setf) ,keys))
5126 (defmacro with-program-error-output (((reduced-error-output-var
5127 &optional (error-output-activity-var (gensym) eoavp))
5128 error-output-form &key setf stream-easy-p active keys)
5129 &body body)
5130 `(apply '%call-with-program-io 'slurp-input-stream *error-output* ,stream-easy-p
5131 #'(lambda (,reduced-error-output-var ,error-output-activity-var)
5132 ,@(unless eoavp `((declare (ignore ,error-output-activity-var))))
5133 ,@body)
5134 :error-output ,error-output-form ,active (place-setter ,setf) ,keys))
5136 (defun %use-run-program (command &rest keys
5137 &key input output error-output ignore-error-status &allow-other-keys)
5138 ;; helper for RUN-PROGRAM when using %run-program
5139 #+(or abcl cormanlisp gcl (and lispworks os-windows) mcl xcl)
5140 (progn
5141 command keys input output error-output ignore-error-status ;; ignore
5142 (error "Not implemented on this platform"))
5143 (assert (not (member :stream (list input output error-output))))
5144 (let* ((active-input-p (%active-io-specifier-p input))
5145 (active-output-p (%active-io-specifier-p output))
5146 (active-error-output-p (%active-io-specifier-p error-output))
5147 (activity
5148 (cond
5149 (active-output-p :output)
5150 (active-input-p :input)
5151 (active-error-output-p :error-output)
5152 (t nil)))
5153 (wait (not activity))
5154 output-result error-output-result exit-code)
5155 (with-program-output ((reduced-output output-activity)
5156 output :keys keys :setf output-result
5157 :stream-easy-p t :active (eq activity :output))
5158 (with-program-error-output ((reduced-error-output error-output-activity)
5159 error-output :keys keys :setf error-output-result
5160 :stream-easy-p t :active (eq activity :error-output))
5161 (with-program-input ((reduced-input input-activity)
5162 input :keys keys
5163 :stream-easy-p t :active (eq activity :input))
5164 (let ((process-info
5165 (apply '%run-program command
5166 :wait wait :input reduced-input :output reduced-output
5167 :error-output (if (eq error-output :output) :output reduced-error-output)
5168 keys)))
5169 (labels ((get-stream (stream-name &optional fallbackp)
5170 (or (getf process-info stream-name)
5171 (when fallbackp
5172 (getf process-info :bidir-stream))))
5173 (run-activity (activity stream-name &optional fallbackp)
5174 (if-let (stream (get-stream stream-name fallbackp))
5175 (funcall activity stream)
5176 (error 'subprocess-error
5177 :code `(:missing ,stream-name)
5178 :command command :process process-info))))
5179 (unwind-protect
5180 (ecase activity
5181 ((nil))
5182 (:input (run-activity input-activity :input-stream t))
5183 (:output (run-activity output-activity :output-stream t))
5184 (:error-output (run-activity error-output-activity :error-output-stream)))
5185 (loop :for (() val) :on process-info :by #'cddr
5186 :when (streamp val) :do (ignore-errors (close val)))
5187 (setf exit-code
5188 (%check-result (%wait-process-result process-info)
5189 :command command :process process-info
5190 :ignore-error-status ignore-error-status))))))))
5191 (values output-result error-output-result exit-code)))
5193 (defun %normalize-system-command (command) ;; helper for %USE-SYSTEM
5194 (etypecase command
5195 (string command)
5196 (list (escape-shell-command
5197 (if (os-unix-p) (cons "exec" command) command)))))
5199 (defun %redirected-system-command (command in out err directory) ;; helper for %USE-SYSTEM
5200 (flet ((redirect (spec operator)
5201 (let ((pathname
5202 (typecase spec
5203 (null (null-device-pathname))
5204 (string (parse-native-namestring spec))
5205 (pathname spec)
5206 ((eql :output)
5207 (assert (equal operator " 2>"))
5208 (return-from redirect '(" 2>&1"))))))
5209 (when pathname
5210 (list operator " "
5211 (escape-shell-token (native-namestring pathname)))))))
5212 (multiple-value-bind (before after)
5213 (let ((normalized (%normalize-system-command command)))
5214 (if (os-unix-p)
5215 (values '("exec") (list " ; " normalized))
5216 (values (list normalized) ())))
5217 (reduce/strcat
5218 (append
5219 before (redirect in " <") (redirect out " >") (redirect err " 2>")
5220 (when (and directory (os-unix-p)) ;; NB: unless on Unix, %system uses with-current-directory
5221 `(" ; cd " ,(escape-shell-token (native-namestring directory))))
5222 after)))))
5224 (defun %system (command &rest keys
5225 &key input output error-output directory &allow-other-keys)
5226 "A portable abstraction of a low-level call to libc's system()."
5227 (declare (ignorable input output error-output directory keys))
5228 #+(or allegro clozure cmu (and lispworks os-unix) sbcl scl)
5229 (%wait-process-result
5230 (apply '%run-program (%normalize-system-command command) :wait t keys))
5231 #+(or abcl cormanlisp clisp ecl gcl genera (and lispworks os-windows) mkcl xcl)
5232 (let ((%command (%redirected-system-command command input output error-output directory)))
5233 #+(and lispworks os-windows)
5234 (system:call-system %command :current-directory directory :wait t)
5235 #+clisp
5236 (%wait-process-result
5237 (apply '%run-program %command :wait t
5238 :input :interactive :output :interactive :error-output :interactive keys))
5239 #-(or clisp (and lispworks os-windows))
5240 (with-current-directory ((unless (os-unix-p) directory))
5241 #+abcl (ext:run-shell-command %command)
5242 #+cormanlisp (win32:system %command)
5243 #+ecl (let ((*standard-input* *stdin*)
5244 (*standard-output* *stdout*)
5245 (*error-output* *stderr*))
5246 (ext:system %command))
5247 #+gcl (system:system %command)
5248 #+genera (error "~S not supported on Genera, cannot run ~S"
5249 '%system %command)
5250 #+mcl (ccl::with-cstrs ((%%command %command)) (_system %%command))
5251 #+mkcl (mkcl:system %command)
5252 #+xcl (system:%run-shell-command %command))))
5254 (defun %use-system (command &rest keys
5255 &key input output error-output ignore-error-status &allow-other-keys)
5256 ;; helper for RUN-PROGRAM when using %system
5257 (let (output-result error-output-result exit-code)
5258 (with-program-output ((reduced-output)
5259 output :keys keys :setf output-result)
5260 (with-program-error-output ((reduced-error-output)
5261 error-output :keys keys :setf error-output-result)
5262 (with-program-input ((reduced-input) input :keys keys)
5263 (setf exit-code
5264 (%check-result (apply '%system command
5265 :input reduced-input :output reduced-output
5266 :error-output reduced-error-output keys)
5267 :command command
5268 :ignore-error-status ignore-error-status)))))
5269 (values output-result error-output-result exit-code)))
5271 (defun run-program (command &rest keys
5272 &key ignore-error-status force-shell
5273 (input nil inputp) (if-input-does-not-exist :error)
5274 output (if-output-exists :overwrite)
5275 (error-output nil error-output-p) (if-error-output-exists :overwrite)
5276 (element-type #-clozure *default-stream-element-type* #+clozure 'character)
5277 (external-format *utf-8-external-format*)
5278 &allow-other-keys)
5279 "Run program specified by COMMAND,
5280 either a list of strings specifying a program and list of arguments,
5281 or a string specifying a shell command (/bin/sh on Unix, CMD.EXE on Windows).
5283 Always call a shell (rather than directly execute the command when possible)
5284 if FORCE-SHELL is specified.
5286 Signal a continuable SUBPROCESS-ERROR if the process wasn't successful (exit-code 0),
5287 unless IGNORE-ERROR-STATUS is specified.
5289 If OUTPUT is a pathname, a string designating a pathname, or NIL designating the null device,
5290 the file at that path is used as output.
5291 If it's :INTERACTIVE, output is inherited from the current process;
5292 beware that this may be different from your *STANDARD-OUTPUT*,
5293 and under SLIME will be on your *inferior-lisp* buffer.
5294 If it's T, output goes to your current *STANDARD-OUTPUT* stream.
5295 Otherwise, OUTPUT should be a value that is a suitable first argument to
5296 SLURP-INPUT-STREAM (qv.), or a list of such a value and keyword arguments.
5297 In this case, RUN-PROGRAM will create a temporary stream for the program output;
5298 the program output, in that stream, will be processed by a call to SLURP-INPUT-STREAM,
5299 using OUTPUT as the first argument (or the first element of OUTPUT, and the rest as keywords).
5300 The primary value resulting from that call (or NIL if no call was needed)
5301 will be the first value returned by RUN-PROGRAM.
5302 E.g., using :OUTPUT :STRING will have it return the entire output stream as a string.
5303 And using :OUTPUT '(:STRING :STRIPPED T) will have it return the same string
5304 stripped of any ending newline.
5306 ERROR-OUTPUT is similar to OUTPUT, except that the resulting value is returned
5307 as the second value of RUN-PROGRAM. T designates the *ERROR-OUTPUT*.
5308 Also :OUTPUT means redirecting the error output to the output stream,
5309 in which case NIL is returned.
5311 INPUT is similar to OUTPUT, except that VOMIT-OUTPUT-STREAM is used,
5312 no value is returned, and T designates the *STANDARD-INPUT*.
5314 Use ELEMENT-TYPE and EXTERNAL-FORMAT are passed on
5315 to your Lisp implementation, when applicable, for creation of the output stream.
5317 One and only one of the stream slurping or vomiting may or may not happen
5318 in parallel in parallel with the subprocess,
5319 depending on options and implementation,
5320 and with priority being given to output processing.
5321 Other streams are completely produced or consumed
5322 before or after the subprocess is spawned, using temporary files.
5324 RUN-PROGRAM returns 3 values:
5325 0- the result of the OUTPUT slurping if any, or NIL
5326 1- the result of the ERROR-OUTPUT slurping if any, or NIL
5327 2- either 0 if the subprocess exited with success status,
5328 or an indication of failure via the EXIT-CODE of the process"
5329 (declare (ignorable ignore-error-status))
5330 #-(or abcl allegro clisp clozure cmu cormanlisp ecl gcl lispworks mcl mkcl sbcl scl xcl)
5331 (error "RUN-PROGRAM not implemented for this Lisp")
5332 (flet ((default (x xp output) (cond (xp x) ((eq output :interactive) :interactive))))
5333 (apply (if (or force-shell
5334 #+(or clisp ecl) (or (not ignore-error-status) t)
5335 #+clisp (eq error-output :interactive)
5336 #+(or abcl clisp) (eq :error-output :output)
5337 #+(and lispworks os-unix) (%interactivep input output error-output)
5338 #+(or abcl cormanlisp gcl (and lispworks os-windows) mcl xcl) t)
5339 '%use-system '%use-run-program)
5340 command
5341 :input (default input inputp output)
5342 :error-output (default error-output error-output-p output)
5343 :if-input-does-not-exist if-input-does-not-exist
5344 :if-output-exists if-output-exists
5345 :if-error-output-exists if-error-output-exists
5346 :element-type element-type :external-format external-format
5347 keys))))
5348 ;;;; -------------------------------------------------------------------------
5349 ;;;; Support to build (compile and load) Lisp files
5351 (uiop/package:define-package :uiop/lisp-build
5352 (:nicknames :asdf/lisp-build)
5353 (:recycle :uiop/lisp-build :asdf/lisp-build :asdf)
5354 (:use :uiop/common-lisp :uiop/package :uiop/utility
5355 :uiop/os :uiop/pathname :uiop/filesystem :uiop/stream :uiop/image)
5356 (:export
5357 ;; Variables
5358 #:*compile-file-warnings-behaviour* #:*compile-file-failure-behaviour*
5359 #:*output-translation-function*
5360 #:*optimization-settings* #:*previous-optimization-settings*
5361 #:*base-build-directory*
5362 #:compile-condition #:compile-file-error #:compile-warned-error #:compile-failed-error
5363 #:compile-warned-warning #:compile-failed-warning
5364 #:check-lisp-compile-results #:check-lisp-compile-warnings
5365 #:*uninteresting-conditions* #:*usual-uninteresting-conditions*
5366 #:*uninteresting-compiler-conditions* #:*uninteresting-loader-conditions*
5367 ;; Types
5368 #+sbcl #:sb-grovel-unknown-constant-condition
5369 ;; Functions & Macros
5370 #:get-optimization-settings #:proclaim-optimization-settings #:with-optimization-settings
5371 #:call-with-muffled-compiler-conditions #:with-muffled-compiler-conditions
5372 #:call-with-muffled-loader-conditions #:with-muffled-loader-conditions
5373 #:reify-simple-sexp #:unreify-simple-sexp
5374 #:reify-deferred-warnings #:unreify-deferred-warnings
5375 #:reset-deferred-warnings #:save-deferred-warnings #:check-deferred-warnings
5376 #:with-saved-deferred-warnings #:warnings-file-p #:warnings-file-type #:*warnings-file-type*
5377 #:enable-deferred-warnings-check #:disable-deferred-warnings-check
5378 #:current-lisp-file-pathname #:load-pathname
5379 #:lispize-pathname #:compile-file-type #:call-around-hook
5380 #:compile-file* #:compile-file-pathname* #:*compile-check*
5381 #:load* #:load-from-string #:combine-fasls)
5382 (:intern #:defaults #:failure-p #:warnings-p #:s #:y #:body))
5383 (in-package :uiop/lisp-build)
5385 (with-upgradability ()
5386 (defvar *compile-file-warnings-behaviour*
5387 (or #+clisp :ignore :warn)
5388 "How should ASDF react if it encounters a warning when compiling a file?
5389 Valid values are :error, :warn, and :ignore.")
5391 (defvar *compile-file-failure-behaviour*
5392 (or #+(or mkcl sbcl) :error #+clisp :ignore :warn)
5393 "How should ASDF react if it encounters a failure (per the ANSI spec of COMPILE-FILE)
5394 when compiling a file, which includes any non-style-warning warning.
5395 Valid values are :error, :warn, and :ignore.
5396 Note that ASDF ALWAYS raises an error if it fails to create an output file when compiling.")
5398 (defvar *base-build-directory* nil
5399 "When set to a non-null value, it should be an absolute directory pathname,
5400 which will serve as the *DEFAULT-PATHNAME-DEFAULTS* around a COMPILE-FILE,
5401 what more while the input-file is shortened if possible to ENOUGH-PATHNAME relative to it.
5402 This can help you produce more deterministic output for FASLs."))
5404 ;;; Optimization settings
5405 (with-upgradability ()
5406 (defvar *optimization-settings* nil
5407 "Optimization settings to be used by PROCLAIM-OPTIMIZATION-SETTINGS")
5408 (defvar *previous-optimization-settings* nil
5409 "Optimization settings saved by PROCLAIM-OPTIMIZATION-SETTINGS")
5410 (defparameter +optimization-variables+
5411 ;; TODO: allegro genera corman mcl
5412 (or #+(or abcl xcl) '(system::*speed* system::*space* system::*safety* system::*debug*)
5413 #+clisp '() ;; system::*optimize* is a constant hash-table! (with non-constant contents)
5414 #+clozure '(ccl::*nx-speed* ccl::*nx-space* ccl::*nx-safety*
5415 ccl::*nx-debug* ccl::*nx-cspeed*)
5416 #+(or cmu scl) '(c::*default-cookie*)
5417 #+ecl (unless (use-ecl-byte-compiler-p) '(c::*speed* c::*space* c::*safety* c::*debug*))
5418 #+gcl '(compiler::*speed* compiler::*space* compiler::*compiler-new-safety* compiler::*debug*)
5419 #+lispworks '(compiler::*optimization-level*)
5420 #+mkcl '(si::*speed* si::*space* si::*safety* si::*debug*)
5421 #+sbcl '(sb-c::*policy*)))
5422 (defun get-optimization-settings ()
5423 "Get current compiler optimization settings, ready to PROCLAIM again"
5424 #-(or abcl allegro clisp clozure cmu ecl lispworks mkcl sbcl scl xcl)
5425 (warn "~S does not support ~S. Please help me fix that."
5426 'get-optimization-settings (implementation-type))
5427 #+(or abcl allegro clisp clozure cmu ecl lispworks mkcl sbcl scl xcl)
5428 (let ((settings '(speed space safety debug compilation-speed #+(or cmu scl) c::brevity)))
5429 #.`(loop #+(or allegro clozure)
5430 ,@'(:with info = #+allegro (sys:declaration-information 'optimize)
5431 #+clozure (ccl:declaration-information 'optimize nil))
5432 :for x :in settings
5433 ,@(or #+(or abcl ecl gcl mkcl xcl) '(:for v :in +optimization-variables+))
5434 :for y = (or #+(or allegro clozure) (second (assoc x info)) ; normalize order
5435 #+clisp (gethash x system::*optimize* 1)
5436 #+(or abcl ecl mkcl xcl) (symbol-value v)
5437 #+(or cmu scl) (slot-value c::*default-cookie*
5438 (case x (compilation-speed 'c::cspeed)
5439 (otherwise x)))
5440 #+lispworks (slot-value compiler::*optimization-level* x)
5441 #+sbcl (cdr (assoc x sb-c::*policy*)))
5442 :when y :collect (list x y))))
5443 (defun proclaim-optimization-settings ()
5444 "Proclaim the optimization settings in *OPTIMIZATION-SETTINGS*"
5445 (proclaim `(optimize ,@*optimization-settings*))
5446 (let ((settings (get-optimization-settings)))
5447 (unless (equal *previous-optimization-settings* settings)
5448 (setf *previous-optimization-settings* settings))))
5449 (defmacro with-optimization-settings ((&optional (settings *optimization-settings*)) &body body)
5450 #+(or allegro clisp)
5451 (let ((previous-settings (gensym "PREVIOUS-SETTINGS")))
5452 `(let ((,previous-settings (get-optimization-settings)))
5453 ,@(when settings `((proclaim `(optimize ,@,settings))))
5454 (unwind-protect (progn ,@body)
5455 (proclaim `(optimize ,@,previous-settings)))))
5456 #-(or allegro clisp)
5457 `(let ,(loop :for v :in +optimization-variables+ :collect `(,v ,v))
5458 ,@(when settings `((proclaim `(optimize ,@,settings))))
5459 ,@body)))
5462 ;;; Condition control
5463 (with-upgradability ()
5464 #+sbcl
5465 (progn
5466 (defun sb-grovel-unknown-constant-condition-p (c)
5467 "Detect SB-GROVEL unknown-constant conditions on older versions of SBCL"
5468 (and (typep c 'sb-int:simple-style-warning)
5469 (string-enclosed-p
5470 "Couldn't grovel for "
5471 (simple-condition-format-control c)
5472 " (unknown to the C compiler).")))
5473 (deftype sb-grovel-unknown-constant-condition ()
5474 '(and style-warning (satisfies sb-grovel-unknown-constant-condition-p))))
5476 (defvar *usual-uninteresting-conditions*
5477 (append
5478 ;;#+clozure '(ccl:compiler-warning)
5479 #+cmu '("Deleting unreachable code.")
5480 #+lispworks '("~S being redefined in ~A (previously in ~A)."
5481 "~S defined more than once in ~A.") ;; lispworks gets confused by eval-when.
5482 #+sbcl
5483 '(sb-c::simple-compiler-note
5484 "&OPTIONAL and &KEY found in the same lambda list: ~S"
5485 #+sb-eval sb-kernel:lexical-environment-too-complex
5486 sb-kernel:undefined-alien-style-warning
5487 sb-grovel-unknown-constant-condition ; defined above.
5488 sb-ext:implicit-generic-function-warning ;; Controversial.
5489 sb-int:package-at-variance
5490 sb-kernel:uninteresting-redefinition
5491 ;; BEWARE: the below four are controversial to include here.
5492 sb-kernel:redefinition-with-defun
5493 sb-kernel:redefinition-with-defgeneric
5494 sb-kernel:redefinition-with-defmethod
5495 sb-kernel::redefinition-with-defmacro) ; not exported by old SBCLs
5496 '("No generic function ~S present when encountering macroexpansion of defmethod. Assuming it will be an instance of standard-generic-function.")) ;; from closer2mop
5497 "A suggested value to which to set or bind *uninteresting-conditions*.")
5499 (defvar *uninteresting-conditions* '()
5500 "Conditions that may be skipped while compiling or loading Lisp code.")
5501 (defvar *uninteresting-compiler-conditions* '()
5502 "Additional conditions that may be skipped while compiling Lisp code.")
5503 (defvar *uninteresting-loader-conditions*
5504 (append
5505 '("Overwriting already existing readtable ~S." ;; from named-readtables
5506 #(#:finalizers-off-warning :asdf-finalizers)) ;; from asdf-finalizers
5507 #+clisp '(clos::simple-gf-replacing-method-warning))
5508 "Additional conditions that may be skipped while loading Lisp code."))
5510 ;;;; ----- Filtering conditions while building -----
5511 (with-upgradability ()
5512 (defun call-with-muffled-compiler-conditions (thunk)
5513 "Call given THUNK in a context where uninteresting conditions and compiler conditions are muffled"
5514 (call-with-muffled-conditions
5515 thunk (append *uninteresting-conditions* *uninteresting-compiler-conditions*)))
5516 (defmacro with-muffled-compiler-conditions ((&optional) &body body)
5517 "Trivial syntax for CALL-WITH-MUFFLED-COMPILER-CONDITIONS"
5518 `(call-with-muffled-compiler-conditions #'(lambda () ,@body)))
5519 (defun call-with-muffled-loader-conditions (thunk)
5520 "Call given THUNK in a context where uninteresting conditions and loader conditions are muffled"
5521 (call-with-muffled-conditions
5522 thunk (append *uninteresting-conditions* *uninteresting-loader-conditions*)))
5523 (defmacro with-muffled-loader-conditions ((&optional) &body body)
5524 "Trivial syntax for CALL-WITH-MUFFLED-LOADER-CONDITIONS"
5525 `(call-with-muffled-loader-conditions #'(lambda () ,@body))))
5528 ;;;; Handle warnings and failures
5529 (with-upgradability ()
5530 (define-condition compile-condition (condition)
5531 ((context-format
5532 :initform nil :reader compile-condition-context-format :initarg :context-format)
5533 (context-arguments
5534 :initform nil :reader compile-condition-context-arguments :initarg :context-arguments)
5535 (description
5536 :initform nil :reader compile-condition-description :initarg :description))
5537 (:report (lambda (c s)
5538 (format s (compatfmt "~@<~A~@[ while ~?~]~@:>")
5539 (or (compile-condition-description c) (type-of c))
5540 (compile-condition-context-format c)
5541 (compile-condition-context-arguments c)))))
5542 (define-condition compile-file-error (compile-condition error) ())
5543 (define-condition compile-warned-warning (compile-condition warning) ())
5544 (define-condition compile-warned-error (compile-condition error) ())
5545 (define-condition compile-failed-warning (compile-condition warning) ())
5546 (define-condition compile-failed-error (compile-condition error) ())
5548 (defun check-lisp-compile-warnings (warnings-p failure-p
5549 &optional context-format context-arguments)
5550 "Given the warnings or failures as resulted from COMPILE-FILE or checking deferred warnings,
5551 raise an error or warning as appropriate"
5552 (when failure-p
5553 (case *compile-file-failure-behaviour*
5554 (:warn (warn 'compile-failed-warning
5555 :description "Lisp compilation failed"
5556 :context-format context-format
5557 :context-arguments context-arguments))
5558 (:error (error 'compile-failed-error
5559 :description "Lisp compilation failed"
5560 :context-format context-format
5561 :context-arguments context-arguments))
5562 (:ignore nil)))
5563 (when warnings-p
5564 (case *compile-file-warnings-behaviour*
5565 (:warn (warn 'compile-warned-warning
5566 :description "Lisp compilation had style-warnings"
5567 :context-format context-format
5568 :context-arguments context-arguments))
5569 (:error (error 'compile-warned-error
5570 :description "Lisp compilation had style-warnings"
5571 :context-format context-format
5572 :context-arguments context-arguments))
5573 (:ignore nil))))
5575 (defun check-lisp-compile-results (output warnings-p failure-p
5576 &optional context-format context-arguments)
5577 "Given the results of COMPILE-FILE, raise an error or warning as appropriate"
5578 (unless output
5579 (error 'compile-file-error :context-format context-format :context-arguments context-arguments))
5580 (check-lisp-compile-warnings warnings-p failure-p context-format context-arguments)))
5583 ;;;; Deferred-warnings treatment, originally implemented by Douglas Katzman.
5585 ;;; To support an implementation, three functions must be implemented:
5586 ;;; reify-deferred-warnings unreify-deferred-warnings reset-deferred-warnings
5587 ;;; See their respective docstrings.
5588 (with-upgradability ()
5589 (defun reify-simple-sexp (sexp)
5590 "Given a simple SEXP, return a representation of it as a portable SEXP.
5591 Simple means made of symbols, numbers, characters, simple-strings, pathnames, cons cells."
5592 (etypecase sexp
5593 (symbol (reify-symbol sexp))
5594 ((or number character simple-string pathname) sexp)
5595 (cons (cons (reify-simple-sexp (car sexp)) (reify-simple-sexp (cdr sexp))))
5596 (simple-vector (vector (mapcar 'reify-simple-sexp (coerce sexp 'list))))))
5598 (defun unreify-simple-sexp (sexp)
5599 "Given the portable output of REIFY-SIMPLE-SEXP, return the simple SEXP it represents"
5600 (etypecase sexp
5601 ((or symbol number character simple-string pathname) sexp)
5602 (cons (cons (unreify-simple-sexp (car sexp)) (unreify-simple-sexp (cdr sexp))))
5603 ((simple-vector 2) (unreify-symbol sexp))
5604 ((simple-vector 1) (coerce (mapcar 'unreify-simple-sexp (aref sexp 0)) 'vector))))
5606 #+clozure
5607 (progn
5608 (defun reify-source-note (source-note)
5609 (when source-note
5610 (with-accessors ((source ccl::source-note-source) (filename ccl:source-note-filename)
5611 (start-pos ccl:source-note-start-pos) (end-pos ccl:source-note-end-pos)) source-note
5612 (declare (ignorable source))
5613 (list :filename filename :start-pos start-pos :end-pos end-pos
5614 #|:source (reify-source-note source)|#))))
5615 (defun unreify-source-note (source-note)
5616 (when source-note
5617 (destructuring-bind (&key filename start-pos end-pos source) source-note
5618 (ccl::make-source-note :filename filename :start-pos start-pos :end-pos end-pos
5619 :source (unreify-source-note source)))))
5620 (defun unsymbolify-function-name (name)
5621 (if-let (setfed (gethash name ccl::%setf-function-name-inverses%))
5622 `(setf ,setfed)
5623 name))
5624 (defun symbolify-function-name (name)
5625 (if (and (consp name) (eq (first name) 'setf))
5626 (let ((setfed (second name)))
5627 (gethash setfed ccl::%setf-function-names%))
5628 name))
5629 (defun reify-function-name (function-name)
5630 (let ((name (or (first function-name) ;; defun: extract the name
5631 (let ((sec (second function-name)))
5632 (or (and (atom sec) sec) ; scoped method: drop scope
5633 (first sec)))))) ; method: keep gf name, drop method specializers
5634 (list name)))
5635 (defun unreify-function-name (function-name)
5636 function-name)
5637 (defun nullify-non-literals (sexp)
5638 (typecase sexp
5639 ((or number character simple-string symbol pathname) sexp)
5640 (cons (cons (nullify-non-literals (car sexp))
5641 (nullify-non-literals (cdr sexp))))
5642 (t nil)))
5643 (defun reify-deferred-warning (deferred-warning)
5644 (with-accessors ((warning-type ccl::compiler-warning-warning-type)
5645 (args ccl::compiler-warning-args)
5646 (source-note ccl:compiler-warning-source-note)
5647 (function-name ccl:compiler-warning-function-name)) deferred-warning
5648 (list :warning-type warning-type :function-name (reify-function-name function-name)
5649 :source-note (reify-source-note source-note)
5650 :args (destructuring-bind (fun &rest more)
5651 args
5652 (cons (unsymbolify-function-name fun)
5653 (nullify-non-literals more))))))
5654 (defun unreify-deferred-warning (reified-deferred-warning)
5655 (destructuring-bind (&key warning-type function-name source-note args)
5656 reified-deferred-warning
5657 (make-condition (or (cdr (ccl::assq warning-type ccl::*compiler-whining-conditions*))
5658 'ccl::compiler-warning)
5659 :function-name (unreify-function-name function-name)
5660 :source-note (unreify-source-note source-note)
5661 :warning-type warning-type
5662 :args (destructuring-bind (fun . more) args
5663 (cons (symbolify-function-name fun) more))))))
5664 #+(or cmu scl)
5665 (defun reify-undefined-warning (warning)
5666 ;; Extracting undefined-warnings from the compilation-unit
5667 ;; To be passed through the above reify/unreify link, it must be a "simple-sexp"
5668 (list*
5669 (c::undefined-warning-kind warning)
5670 (c::undefined-warning-name warning)
5671 (c::undefined-warning-count warning)
5672 (mapcar
5673 #'(lambda (frob)
5674 ;; the lexenv slot can be ignored for reporting purposes
5675 `(:enclosing-source ,(c::compiler-error-context-enclosing-source frob)
5676 :source ,(c::compiler-error-context-source frob)
5677 :original-source ,(c::compiler-error-context-original-source frob)
5678 :context ,(c::compiler-error-context-context frob)
5679 :file-name ,(c::compiler-error-context-file-name frob) ; a pathname
5680 :file-position ,(c::compiler-error-context-file-position frob) ; an integer
5681 :original-source-path ,(c::compiler-error-context-original-source-path frob)))
5682 (c::undefined-warning-warnings warning))))
5684 #+sbcl
5685 (defun reify-undefined-warning (warning)
5686 ;; Extracting undefined-warnings from the compilation-unit
5687 ;; To be passed through the above reify/unreify link, it must be a "simple-sexp"
5688 (list*
5689 (sb-c::undefined-warning-kind warning)
5690 (sb-c::undefined-warning-name warning)
5691 (sb-c::undefined-warning-count warning)
5692 (mapcar
5693 #'(lambda (frob)
5694 ;; the lexenv slot can be ignored for reporting purposes
5695 `(:enclosing-source ,(sb-c::compiler-error-context-enclosing-source frob)
5696 :source ,(sb-c::compiler-error-context-source frob)
5697 :original-source ,(sb-c::compiler-error-context-original-source frob)
5698 :context ,(sb-c::compiler-error-context-context frob)
5699 :file-name ,(sb-c::compiler-error-context-file-name frob) ; a pathname
5700 :file-position ,(sb-c::compiler-error-context-file-position frob) ; an integer
5701 :original-source-path ,(sb-c::compiler-error-context-original-source-path frob)))
5702 (sb-c::undefined-warning-warnings warning))))
5704 (defun reify-deferred-warnings ()
5705 "return a portable S-expression, portably readable and writeable in any Common Lisp implementation
5706 using READ within a WITH-SAFE-IO-SYNTAX, that represents the warnings currently deferred by
5707 WITH-COMPILATION-UNIT. One of three functions required for deferred-warnings support in ASDF."
5708 #+allegro
5709 (list :functions-defined excl::.functions-defined.
5710 :functions-called excl::.functions-called.)
5711 #+clozure
5712 (mapcar 'reify-deferred-warning
5713 (if-let (dw ccl::*outstanding-deferred-warnings*)
5714 (let ((mdw (ccl::ensure-merged-deferred-warnings dw)))
5715 (ccl::deferred-warnings.warnings mdw))))
5716 #+(or cmu scl)
5717 (when lisp::*in-compilation-unit*
5718 ;; Try to send nothing through the pipe if nothing needs to be accumulated
5719 `(,@(when c::*undefined-warnings*
5720 `((c::*undefined-warnings*
5721 ,@(mapcar #'reify-undefined-warning c::*undefined-warnings*))))
5722 ,@(loop :for what :in '(c::*compiler-error-count*
5723 c::*compiler-warning-count*
5724 c::*compiler-note-count*)
5725 :for value = (symbol-value what)
5726 :when (plusp value)
5727 :collect `(,what . ,value))))
5728 #+sbcl
5729 (when sb-c::*in-compilation-unit*
5730 ;; Try to send nothing through the pipe if nothing needs to be accumulated
5731 `(,@(when sb-c::*undefined-warnings*
5732 `((sb-c::*undefined-warnings*
5733 ,@(mapcar #'reify-undefined-warning sb-c::*undefined-warnings*))))
5734 ,@(loop :for what :in '(sb-c::*aborted-compilation-unit-count*
5735 sb-c::*compiler-error-count*
5736 sb-c::*compiler-warning-count*
5737 sb-c::*compiler-style-warning-count*
5738 sb-c::*compiler-note-count*)
5739 :for value = (symbol-value what)
5740 :when (plusp value)
5741 :collect `(,what . ,value)))))
5743 (defun unreify-deferred-warnings (reified-deferred-warnings)
5744 "given a S-expression created by REIFY-DEFERRED-WARNINGS, reinstantiate the corresponding
5745 deferred warnings as to be handled at the end of the current WITH-COMPILATION-UNIT.
5746 Handle any warning that has been resolved already,
5747 such as an undefined function that has been defined since.
5748 One of three functions required for deferred-warnings support in ASDF."
5749 (declare (ignorable reified-deferred-warnings))
5750 #+allegro
5751 (destructuring-bind (&key functions-defined functions-called)
5752 reified-deferred-warnings
5753 (setf excl::.functions-defined.
5754 (append functions-defined excl::.functions-defined.)
5755 excl::.functions-called.
5756 (append functions-called excl::.functions-called.)))
5757 #+clozure
5758 (let ((dw (or ccl::*outstanding-deferred-warnings*
5759 (setf ccl::*outstanding-deferred-warnings* (ccl::%defer-warnings t)))))
5760 (appendf (ccl::deferred-warnings.warnings dw)
5761 (mapcar 'unreify-deferred-warning reified-deferred-warnings)))
5762 #+(or cmu scl)
5763 (dolist (item reified-deferred-warnings)
5764 ;; Each item is (symbol . adjustment) where the adjustment depends on the symbol.
5765 ;; For *undefined-warnings*, the adjustment is a list of initargs.
5766 ;; For everything else, it's an integer.
5767 (destructuring-bind (symbol . adjustment) item
5768 (case symbol
5769 ((c::*undefined-warnings*)
5770 (setf c::*undefined-warnings*
5771 (nconc (mapcan
5772 #'(lambda (stuff)
5773 (destructuring-bind (kind name count . rest) stuff
5774 (unless (case kind (:function (fboundp name)))
5775 (list
5776 (c::make-undefined-warning
5777 :name name
5778 :kind kind
5779 :count count
5780 :warnings
5781 (mapcar #'(lambda (x)
5782 (apply #'c::make-compiler-error-context x))
5783 rest))))))
5784 adjustment)
5785 c::*undefined-warnings*)))
5786 (otherwise
5787 (set symbol (+ (symbol-value symbol) adjustment))))))
5788 #+sbcl
5789 (dolist (item reified-deferred-warnings)
5790 ;; Each item is (symbol . adjustment) where the adjustment depends on the symbol.
5791 ;; For *undefined-warnings*, the adjustment is a list of initargs.
5792 ;; For everything else, it's an integer.
5793 (destructuring-bind (symbol . adjustment) item
5794 (case symbol
5795 ((sb-c::*undefined-warnings*)
5796 (setf sb-c::*undefined-warnings*
5797 (nconc (mapcan
5798 #'(lambda (stuff)
5799 (destructuring-bind (kind name count . rest) stuff
5800 (unless (case kind (:function (fboundp name)))
5801 (list
5802 (sb-c::make-undefined-warning
5803 :name name
5804 :kind kind
5805 :count count
5806 :warnings
5807 (mapcar #'(lambda (x)
5808 (apply #'sb-c::make-compiler-error-context x))
5809 rest))))))
5810 adjustment)
5811 sb-c::*undefined-warnings*)))
5812 (otherwise
5813 (set symbol (+ (symbol-value symbol) adjustment)))))))
5815 (defun reset-deferred-warnings ()
5816 "Reset the set of deferred warnings to be handled at the end of the current WITH-COMPILATION-UNIT.
5817 One of three functions required for deferred-warnings support in ASDF."
5818 #+allegro
5819 (setf excl::.functions-defined. nil
5820 excl::.functions-called. nil)
5821 #+clozure
5822 (if-let (dw ccl::*outstanding-deferred-warnings*)
5823 (let ((mdw (ccl::ensure-merged-deferred-warnings dw)))
5824 (setf (ccl::deferred-warnings.warnings mdw) nil)))
5825 #+(or cmu scl)
5826 (when lisp::*in-compilation-unit*
5827 (setf c::*undefined-warnings* nil
5828 c::*compiler-error-count* 0
5829 c::*compiler-warning-count* 0
5830 c::*compiler-note-count* 0))
5831 #+sbcl
5832 (when sb-c::*in-compilation-unit*
5833 (setf sb-c::*undefined-warnings* nil
5834 sb-c::*aborted-compilation-unit-count* 0
5835 sb-c::*compiler-error-count* 0
5836 sb-c::*compiler-warning-count* 0
5837 sb-c::*compiler-style-warning-count* 0
5838 sb-c::*compiler-note-count* 0)))
5840 (defun save-deferred-warnings (warnings-file)
5841 "Save forward reference conditions so they may be issued at a latter time,
5842 possibly in a different process."
5843 (with-open-file (s warnings-file :direction :output :if-exists :supersede
5844 :element-type *default-stream-element-type*
5845 :external-format *utf-8-external-format*)
5846 (with-safe-io-syntax ()
5847 (write (reify-deferred-warnings) :stream s :pretty t :readably t)
5848 (terpri s))))
5850 (defun warnings-file-type (&optional implementation-type)
5851 "The pathname type for warnings files on given IMPLEMENTATION-TYPE,
5852 where NIL designates the current one"
5853 (case (or implementation-type *implementation-type*)
5854 ((:acl :allegro) "allegro-warnings")
5855 ;;((:clisp) "clisp-warnings")
5856 ((:cmu :cmucl) "cmucl-warnings")
5857 ((:sbcl) "sbcl-warnings")
5858 ((:clozure :ccl) "ccl-warnings")
5859 ((:scl) "scl-warnings")))
5861 (defvar *warnings-file-type* nil
5862 "Pathname type for warnings files, or NIL if disabled")
5864 (defun enable-deferred-warnings-check ()
5865 "Enable the saving of deferred warnings"
5866 (setf *warnings-file-type* (warnings-file-type)))
5868 (defun disable-deferred-warnings-check ()
5869 "Disable the saving of deferred warnings"
5870 (setf *warnings-file-type* nil))
5872 (defun warnings-file-p (file &optional implementation-type)
5873 "Is FILE a saved warnings file for the given IMPLEMENTATION-TYPE?
5874 If that given type is NIL, use the currently configured *WARNINGS-FILE-TYPE* instead."
5875 (if-let (type (if implementation-type
5876 (warnings-file-type implementation-type)
5877 *warnings-file-type*))
5878 (equal (pathname-type file) type)))
5880 (defun check-deferred-warnings (files &optional context-format context-arguments)
5881 "Given a list of FILES containing deferred warnings saved by CALL-WITH-SAVED-DEFERRED-WARNINGS,
5882 re-intern and raise any warnings that are still meaningful."
5883 (let ((file-errors nil)
5884 (failure-p nil)
5885 (warnings-p nil))
5886 (handler-bind
5887 ((warning #'(lambda (c)
5888 (setf warnings-p t)
5889 (unless (typep c 'style-warning)
5890 (setf failure-p t)))))
5891 (with-compilation-unit (:override t)
5892 (reset-deferred-warnings)
5893 (dolist (file files)
5894 (unreify-deferred-warnings
5895 (handler-case (safe-read-file-form file)
5896 (error (c)
5897 ;;(delete-file-if-exists file) ;; deleting forces rebuild but prevents debugging
5898 (push c file-errors)
5899 nil))))))
5900 (dolist (error file-errors) (error error))
5901 (check-lisp-compile-warnings
5902 (or failure-p warnings-p) failure-p context-format context-arguments)))
5905 Mini-guide to adding support for deferred warnings on an implementation.
5907 First, look at what such a warning looks like:
5909 (describe
5910 (handler-case
5911 (and (eval '(lambda () (some-undefined-function))) nil)
5912 (t (c) c)))
5914 Then you can grep for the condition type in your compiler sources
5915 and see how to catch those that have been deferred,
5916 and/or read, clear and restore the deferred list.
5918 Also look at
5919 (macroexpand-1 '(with-compilation-unit () foo))
5922 (defun call-with-saved-deferred-warnings (thunk warnings-file &key source-namestring)
5923 "If WARNINGS-FILE is not nil, record the deferred-warnings around a call to THUNK
5924 and save those warnings to the given file for latter use,
5925 possibly in a different process. Otherwise just call THUNK."
5926 (declare (ignorable source-namestring))
5927 (if warnings-file
5928 (with-compilation-unit (:override t #+sbcl :source-namestring #+sbcl source-namestring)
5929 (unwind-protect
5930 (let (#+sbcl (sb-c::*undefined-warnings* nil))
5931 (multiple-value-prog1
5932 (funcall thunk)
5933 (save-deferred-warnings warnings-file)))
5934 (reset-deferred-warnings)))
5935 (funcall thunk)))
5937 (defmacro with-saved-deferred-warnings ((warnings-file &key source-namestring) &body body)
5938 "Trivial syntax for CALL-WITH-SAVED-DEFERRED-WARNINGS"
5939 `(call-with-saved-deferred-warnings
5940 #'(lambda () ,@body) ,warnings-file :source-namestring ,source-namestring)))
5943 ;;; from ASDF
5944 (with-upgradability ()
5945 (defun current-lisp-file-pathname ()
5946 "Portably return the PATHNAME of the current Lisp source file being compiled or loaded"
5947 (or *compile-file-pathname* *load-pathname*))
5949 (defun load-pathname ()
5950 "Portably return the LOAD-PATHNAME of the current source file or fasl"
5951 *load-pathname*) ;; magic no longer needed for GCL.
5953 (defun lispize-pathname (input-file)
5954 "From a INPUT-FILE pathname, return a corresponding .lisp source pathname"
5955 (make-pathname :type "lisp" :defaults input-file))
5957 (defun compile-file-type (&rest keys)
5958 "pathname TYPE for lisp FASt Loading files"
5959 (declare (ignorable keys))
5960 #-(or ecl mkcl) (load-time-value (pathname-type (compile-file-pathname "foo.lisp")))
5961 #+(or ecl mkcl) (pathname-type (apply 'compile-file-pathname "foo" keys)))
5963 (defun call-around-hook (hook function)
5964 "Call a HOOK around the execution of FUNCTION"
5965 (call-function (or hook 'funcall) function))
5967 (defun compile-file-pathname* (input-file &rest keys &key output-file &allow-other-keys)
5968 "Variant of COMPILE-FILE-PATHNAME that works well with COMPILE-FILE*"
5969 (let* ((keys
5970 (remove-plist-keys `(#+(or (and allegro (not (version>= 8 2)))) :external-format
5971 ,@(unless output-file '(:output-file))) keys)))
5972 (if (absolute-pathname-p output-file)
5973 ;; what cfp should be doing, w/ mp* instead of mp
5974 (let* ((type (pathname-type (apply 'compile-file-type keys)))
5975 (defaults (make-pathname
5976 :type type :defaults (merge-pathnames* input-file))))
5977 (merge-pathnames* output-file defaults))
5978 (funcall *output-translation-function*
5979 (apply 'compile-file-pathname input-file keys)))))
5981 (defvar *compile-check* nil
5982 "A hook for user-defined compile-time invariants")
5984 (defun* (compile-file*) (input-file &rest keys
5985 &key (compile-check *compile-check*) output-file warnings-file
5986 #+clisp lib-file #+(or ecl mkcl) object-file #+sbcl emit-cfasl
5987 &allow-other-keys)
5988 "This function provides a portable wrapper around COMPILE-FILE.
5989 It ensures that the OUTPUT-FILE value is only returned and
5990 the file only actually created if the compilation was successful,
5991 even though your implementation may not do that, and including
5992 an optional call to an user-provided consistency check function COMPILE-CHECK;
5993 it will call this function if not NIL at the end of the compilation
5994 with the arguments sent to COMPILE-FILE*, except with :OUTPUT-FILE TMP-FILE
5995 where TMP-FILE is the name of a temporary output-file.
5996 It also checks two flags (with legacy british spelling from ASDF1),
5997 *COMPILE-FILE-FAILURE-BEHAVIOUR* and *COMPILE-FILE-WARNINGS-BEHAVIOUR*
5998 with appropriate implementation-dependent defaults,
5999 and if a failure (respectively warnings) are reported by COMPILE-FILE
6000 with consider it an error unless the respective behaviour flag
6001 is one of :SUCCESS :WARN :IGNORE.
6002 If WARNINGS-FILE is defined, deferred warnings are saved to that file.
6003 On ECL or MKCL, it creates both the linkable object and loadable fasl files.
6004 On implementations that erroneously do not recognize standard keyword arguments,
6005 it will filter them appropriately."
6006 #+ecl (when (and object-file (equal (compile-file-type) (pathname object-file)))
6007 (format t "Whoa, some funky ASDF upgrade switched ~S calling convention for ~S and ~S~%"
6008 'compile-file* output-file object-file)
6009 (rotatef output-file object-file))
6010 (let* ((keywords (remove-plist-keys
6011 `(:output-file :compile-check :warnings-file
6012 #+clisp :lib-file #+(or ecl mkcl) :object-file) keys))
6013 (output-file
6014 (or output-file
6015 (apply 'compile-file-pathname* input-file :output-file output-file keywords)))
6016 #+ecl
6017 (object-file
6018 (unless (use-ecl-byte-compiler-p)
6019 (or object-file
6020 (compile-file-pathname output-file :type :object))))
6021 #+mkcl
6022 (object-file
6023 (or object-file
6024 (compile-file-pathname output-file :fasl-p nil)))
6025 (tmp-file (tmpize-pathname output-file))
6026 #+sbcl
6027 (cfasl-file (etypecase emit-cfasl
6028 (null nil)
6029 ((eql t) (make-pathname :type "cfasl" :defaults output-file))
6030 (string (parse-namestring emit-cfasl))
6031 (pathname emit-cfasl)))
6032 #+sbcl
6033 (tmp-cfasl (when cfasl-file (make-pathname :type "cfasl" :defaults tmp-file)))
6034 #+clisp
6035 (tmp-lib (make-pathname :type "lib" :defaults tmp-file)))
6036 (multiple-value-bind (output-truename warnings-p failure-p)
6037 (with-enough-pathname (input-file :defaults *base-build-directory*)
6038 (with-saved-deferred-warnings (warnings-file :source-namestring (namestring input-file))
6039 (with-muffled-compiler-conditions ()
6040 (or #-(or ecl mkcl)
6041 (apply 'compile-file input-file :output-file tmp-file
6042 #+sbcl (if emit-cfasl (list* :emit-cfasl tmp-cfasl keywords) keywords)
6043 #-sbcl keywords)
6044 #+ecl (apply 'compile-file input-file :output-file
6045 (if object-file
6046 (list* object-file :system-p t keywords)
6047 (list* tmp-file keywords)))
6048 #+mkcl (apply 'compile-file input-file
6049 :output-file object-file :fasl-p nil keywords)))))
6050 (cond
6051 ((and output-truename
6052 (flet ((check-flag (flag behaviour)
6053 (or (not flag) (member behaviour '(:success :warn :ignore)))))
6054 (and (check-flag failure-p *compile-file-failure-behaviour*)
6055 (check-flag warnings-p *compile-file-warnings-behaviour*)))
6056 (progn
6057 #+(or ecl mkcl)
6058 (when (and #+ecl object-file)
6059 (setf output-truename
6060 (compiler::build-fasl
6061 tmp-file #+ecl :lisp-files #+mkcl :lisp-object-files
6062 (list object-file))))
6063 (or (not compile-check)
6064 (apply compile-check input-file :output-file tmp-file keywords))))
6065 (delete-file-if-exists output-file)
6066 (when output-truename
6067 #+clisp (when lib-file (rename-file-overwriting-target tmp-lib lib-file))
6068 #+sbcl (when cfasl-file (rename-file-overwriting-target tmp-cfasl cfasl-file))
6069 (rename-file-overwriting-target output-truename output-file)
6070 (setf output-truename (truename output-file)))
6071 #+clisp (delete-file-if-exists tmp-lib))
6072 (t ;; error or failed check
6073 (delete-file-if-exists output-truename)
6074 #+clisp (delete-file-if-exists tmp-lib)
6075 #+sbcl (delete-file-if-exists tmp-cfasl)
6076 (setf output-truename nil)))
6077 (values output-truename warnings-p failure-p))))
6079 (defun load* (x &rest keys &key &allow-other-keys)
6080 "Portable wrapper around LOAD that properly handles loading from a stream."
6081 (with-muffled-loader-conditions ()
6082 (etypecase x
6083 ((or pathname string #-(or allegro clozure genera) stream #+clozure file-stream)
6084 (apply 'load x keys))
6085 ;; Genera can't load from a string-input-stream
6086 ;; ClozureCL 1.6 can only load from file input stream
6087 ;; Allegro 5, I don't remember but it must have been broken when I tested.
6088 #+(or allegro clozure genera)
6089 (stream ;; make do this way
6090 (let ((*package* *package*)
6091 (*readtable* *readtable*)
6092 (*load-pathname* nil)
6093 (*load-truename* nil))
6094 (eval-input x))))))
6096 (defun load-from-string (string)
6097 "Portably read and evaluate forms from a STRING."
6098 (with-input-from-string (s string) (load* s))))
6100 ;;; Links FASLs together
6101 (with-upgradability ()
6102 (defun combine-fasls (inputs output)
6103 "Combine a list of FASLs INPUTS into a single FASL OUTPUT"
6104 #-(or abcl allegro clisp clozure cmu lispworks sbcl scl xcl)
6105 (error "~A does not support ~S~%inputs ~S~%output ~S"
6106 (implementation-type) 'combine-fasls inputs output)
6107 #+abcl (funcall 'sys::concatenate-fasls inputs output) ; requires ABCL 1.2.0
6108 #+(or allegro clisp cmu sbcl scl xcl) (concatenate-files inputs output)
6109 #+clozure (ccl:fasl-concatenate output inputs :if-exists :supersede)
6110 #+lispworks
6111 (let (fasls)
6112 (unwind-protect
6113 (progn
6114 (loop :for i :in inputs
6115 :for n :from 1
6116 :for f = (add-pathname-suffix
6117 output (format nil "-FASL~D" n))
6118 :do (copy-file i f)
6119 (push f fasls))
6120 (ignore-errors (lispworks:delete-system :fasls-to-concatenate))
6121 (eval `(scm:defsystem :fasls-to-concatenate
6122 (:default-pathname ,(pathname-directory-pathname output))
6123 :members
6124 ,(loop :for f :in (reverse fasls)
6125 :collect `(,(namestring f) :load-only t))))
6126 (scm:concatenate-system output :fasls-to-concatenate))
6127 (loop :for f :in fasls :do (ignore-errors (delete-file f)))
6128 (ignore-errors (lispworks:delete-system :fasls-to-concatenate))))))
6130 ;;;; ---------------------------------------------------------------------------
6131 ;;;; Generic support for configuration files
6133 (uiop/package:define-package :uiop/configuration
6134 (:nicknames :asdf/configuration)
6135 (:recycle :uiop/configuration :asdf/configuration :asdf)
6136 (:use :uiop/common-lisp :uiop/utility
6137 :uiop/os :uiop/pathname :uiop/filesystem :uiop/stream :uiop/image :uiop/lisp-build)
6138 (:export
6139 #:get-folder-path
6140 #:user-configuration-directories #:system-configuration-directories
6141 #:in-first-directory
6142 #:in-user-configuration-directory #:in-system-configuration-directory
6143 #:validate-configuration-form #:validate-configuration-file #:validate-configuration-directory
6144 #:configuration-inheritance-directive-p
6145 #:report-invalid-form #:invalid-configuration #:*ignored-configuration-form* #:*user-cache*
6146 #:*clear-configuration-hook* #:clear-configuration #:register-clear-configuration-hook
6147 #:resolve-location #:location-designator-p #:location-function-p #:*here-directory*
6148 #:resolve-relative-location #:resolve-absolute-location #:upgrade-configuration))
6149 (in-package :uiop/configuration)
6151 (with-upgradability ()
6152 (define-condition invalid-configuration ()
6153 ((form :reader condition-form :initarg :form)
6154 (location :reader condition-location :initarg :location)
6155 (format :reader condition-format :initarg :format)
6156 (arguments :reader condition-arguments :initarg :arguments :initform nil))
6157 (:report (lambda (c s)
6158 (format s (compatfmt "~@<~? (will be skipped)~@:>")
6159 (condition-format c)
6160 (list* (condition-form c) (condition-location c)
6161 (condition-arguments c))))))
6163 (defun get-folder-path (folder)
6164 "Semi-portable implementation of a subset of LispWorks' sys:get-folder-path,
6165 this function tries to locate the Windows FOLDER for one of
6166 :LOCAL-APPDATA, :APPDATA or :COMMON-APPDATA."
6167 (or #+(and lispworks mswindows) (sys:get-folder-path folder)
6168 ;; read-windows-registry HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\AppData
6169 (ecase folder
6170 (:local-appdata (getenv-absolute-directory "LOCALAPPDATA"))
6171 (:appdata (getenv-absolute-directory "APPDATA"))
6172 (:common-appdata (or (getenv-absolute-directory "ALLUSERSAPPDATA")
6173 (subpathname* (getenv-absolute-directory "ALLUSERSPROFILE") "Application Data/"))))))
6175 (defun user-configuration-directories ()
6176 "Determine user configuration directories"
6177 (let ((dirs
6178 `(,@(when (os-unix-p)
6179 (cons
6180 (subpathname* (getenv-absolute-directory "XDG_CONFIG_HOME") "common-lisp/")
6181 (loop :for dir :in (getenv-absolute-directories "XDG_CONFIG_DIRS")
6182 :collect (subpathname* dir "common-lisp/"))))
6183 ,@(when (os-windows-p)
6184 `(,(subpathname* (get-folder-path :local-appdata) "common-lisp/config/")
6185 ,(subpathname* (get-folder-path :appdata) "common-lisp/config/")))
6186 ,(subpathname (user-homedir-pathname) ".config/common-lisp/"))))
6187 (remove-duplicates (remove-if-not #'absolute-pathname-p dirs)
6188 :from-end t :test 'equal)))
6190 (defun system-configuration-directories ()
6191 "Determine system user configuration directories"
6192 (cond
6193 ((os-unix-p) '(#p"/etc/common-lisp/"))
6194 ((os-windows-p)
6195 (if-let (it (subpathname* (get-folder-path :common-appdata) "common-lisp/config/"))
6196 (list it)))))
6198 (defun in-first-directory (dirs x &key (direction :input))
6199 "Determine system user configuration directories"
6200 (loop :with fun = (ecase direction
6201 ((nil :input :probe) 'probe-file*)
6202 ((:output :io) 'identity))
6203 :for dir :in dirs
6204 :thereis (and dir (funcall fun (subpathname (ensure-directory-pathname dir) x)))))
6206 (defun in-user-configuration-directory (x &key (direction :input))
6207 "return pathname under user configuration directory, subpathname X"
6208 (in-first-directory (user-configuration-directories) x :direction direction))
6209 (defun in-system-configuration-directory (x &key (direction :input))
6210 "return pathname under system configuration directory, subpathname X"
6211 (in-first-directory (system-configuration-directories) x :direction direction))
6213 (defun configuration-inheritance-directive-p (x)
6214 "Is X a configuration inheritance directive?"
6215 (let ((kw '(:inherit-configuration :ignore-inherited-configuration)))
6216 (or (member x kw)
6217 (and (length=n-p x 1) (member (car x) kw)))))
6219 (defun report-invalid-form (reporter &rest args)
6220 "Report an invalid form according to REPORTER and various ARGS"
6221 (etypecase reporter
6222 (null
6223 (apply 'error 'invalid-configuration args))
6224 (function
6225 (apply reporter args))
6226 ((or symbol string)
6227 (apply 'error reporter args))
6228 (cons
6229 (apply 'apply (append reporter args)))))
6231 (defvar *ignored-configuration-form* nil
6232 "Have configuration forms been ignored while parsing the configuration?")
6234 (defun validate-configuration-form (form tag directive-validator
6235 &key location invalid-form-reporter)
6236 "Validate a configuration FORM"
6237 (unless (and (consp form) (eq (car form) tag))
6238 (setf *ignored-configuration-form* t)
6239 (report-invalid-form invalid-form-reporter :form form :location location)
6240 (return-from validate-configuration-form nil))
6241 (loop :with inherit = 0 :with ignore-invalid-p = nil :with x = (list tag)
6242 :for directive :in (cdr form)
6243 :when (cond
6244 ((configuration-inheritance-directive-p directive)
6245 (incf inherit) t)
6246 ((eq directive :ignore-invalid-entries)
6247 (setf ignore-invalid-p t) t)
6248 ((funcall directive-validator directive)
6250 (ignore-invalid-p
6251 nil)
6253 (setf *ignored-configuration-form* t)
6254 (report-invalid-form invalid-form-reporter :form directive :location location)
6255 nil))
6256 :do (push directive x)
6257 :finally
6258 (unless (= inherit 1)
6259 (report-invalid-form invalid-form-reporter
6260 :form form :location location
6261 ;; we throw away the form and location arguments, hence the ~2*
6262 ;; this is necessary because of the report in INVALID-CONFIGURATION
6263 :format (compatfmt "~@<Invalid source registry ~S~@[ in ~S~]. ~
6264 One and only one of ~S or ~S is required.~@:>")
6265 :arguments '(:inherit-configuration :ignore-inherited-configuration)))
6266 (return (nreverse x))))
6268 (defun validate-configuration-file (file validator &key description)
6269 "Validate a configuration file for conformance of its form with the validator function"
6270 (let ((forms (read-file-forms file)))
6271 (unless (length=n-p forms 1)
6272 (error (compatfmt "~@<One and only one form allowed for ~A. Got: ~3i~_~S~@:>~%")
6273 description forms))
6274 (funcall validator (car forms) :location file)))
6276 (defun validate-configuration-directory (directory tag validator &key invalid-form-reporter)
6277 "Map the VALIDATOR across the .conf files in DIRECTORY, the TAG will
6278 be applied to the results to yield a configuration form. Current
6279 values of TAG include :source-registry and :output-translations."
6280 (let ((files (sort (ignore-errors ;; SORT w/o COPY-LIST is OK: DIRECTORY returns a fresh list
6281 (remove-if
6282 'hidden-pathname-p
6283 (directory* (make-pathname :name *wild* :type "conf" :defaults directory))))
6284 #'string< :key #'namestring)))
6285 `(,tag
6286 ,@(loop :for file :in files :append
6287 (loop :with ignore-invalid-p = nil
6288 :for form :in (read-file-forms file)
6289 :when (eq form :ignore-invalid-entries)
6290 :do (setf ignore-invalid-p t)
6291 :else
6292 :when (funcall validator form)
6293 :collect form
6294 :else
6295 :when ignore-invalid-p
6296 :do (setf *ignored-configuration-form* t)
6297 :else
6298 :do (report-invalid-form invalid-form-reporter :form form :location file)))
6299 :inherit-configuration)))
6301 (defun resolve-relative-location (x &key ensure-directory wilden)
6302 "Given a designator X for an relative location, resolve it to a pathname"
6303 (ensure-pathname
6304 (etypecase x
6305 (pathname x)
6306 (string (parse-unix-namestring
6307 x :ensure-directory ensure-directory))
6308 (cons
6309 (if (null (cdr x))
6310 (resolve-relative-location
6311 (car x) :ensure-directory ensure-directory :wilden wilden)
6312 (let* ((car (resolve-relative-location
6313 (car x) :ensure-directory t :wilden nil)))
6314 (merge-pathnames*
6315 (resolve-relative-location
6316 (cdr x) :ensure-directory ensure-directory :wilden wilden)
6317 car))))
6318 ((eql :*/) *wild-directory*)
6319 ((eql :**/) *wild-inferiors*)
6320 ((eql :*.*.*) *wild-file*)
6321 ((eql :implementation)
6322 (parse-unix-namestring
6323 (implementation-identifier) :ensure-directory t))
6324 ((eql :implementation-type)
6325 (parse-unix-namestring
6326 (string-downcase (implementation-type)) :ensure-directory t))
6327 ((eql :hostname)
6328 (parse-unix-namestring (hostname) :ensure-directory t)))
6329 :wilden (and wilden (not (pathnamep x)) (not (member x '(:*/ :**/ :*.*.*))))
6330 :want-relative t))
6332 (defvar *here-directory* nil
6333 "This special variable is bound to the currect directory during calls to
6334 PROCESS-SOURCE-REGISTRY in order that we be able to interpret the :here
6335 directive.")
6337 (defvar *user-cache* nil
6338 "A specification as per RESOLVE-LOCATION of where the user keeps his FASL cache")
6340 (defun compute-user-cache ()
6341 "Compute the location of the default user-cache for translate-output objects"
6342 (setf *user-cache*
6343 (flet ((try (x &rest sub) (and x `(,x ,@sub))))
6345 (try (getenv-absolute-directory "XDG_CACHE_HOME") "common-lisp" :implementation)
6346 (when (os-windows-p)
6347 (try (or (get-folder-path :local-appdata)
6348 (get-folder-path :appdata))
6349 "common-lisp" "cache" :implementation))
6350 '(:home ".cache" "common-lisp" :implementation)))))
6351 (register-image-restore-hook 'compute-user-cache)
6353 (defun resolve-absolute-location (x &key ensure-directory wilden)
6354 "Given a designator X for an absolute location, resolve it to a pathname"
6355 (ensure-pathname
6356 (etypecase x
6357 (pathname x)
6358 (string
6359 (let ((p #-mcl (parse-namestring x)
6360 #+mcl (probe-posix x)))
6361 #+mcl (unless p (error "POSIX pathname ~S does not exist" x))
6362 (if ensure-directory (ensure-directory-pathname p) p)))
6363 (cons
6364 (return-from resolve-absolute-location
6365 (if (null (cdr x))
6366 (resolve-absolute-location
6367 (car x) :ensure-directory ensure-directory :wilden wilden)
6368 (merge-pathnames*
6369 (resolve-relative-location
6370 (cdr x) :ensure-directory ensure-directory :wilden wilden)
6371 (resolve-absolute-location
6372 (car x) :ensure-directory t :wilden nil)))))
6373 ((eql :root)
6374 ;; special magic! we return a relative pathname,
6375 ;; but what it means to the output-translations is
6376 ;; "relative to the root of the source pathname's host and device".
6377 (return-from resolve-absolute-location
6378 (let ((p (make-pathname* :directory '(:relative))))
6379 (if wilden (wilden p) p))))
6380 ((eql :home) (user-homedir-pathname))
6381 ((eql :here) (resolve-absolute-location
6382 (or *here-directory* (pathname-directory-pathname (load-pathname)))
6383 :ensure-directory t :wilden nil))
6384 ((eql :user-cache) (resolve-absolute-location
6385 *user-cache* :ensure-directory t :wilden nil)))
6386 :wilden (and wilden (not (pathnamep x)))
6387 :resolve-symlinks *resolve-symlinks*
6388 :want-absolute t))
6390 ;; Try to override declaration in previous versions of ASDF.
6391 (declaim (ftype (function (t &key (:directory boolean) (:wilden boolean)
6392 (:ensure-directory boolean)) t) resolve-location))
6394 (defun* (resolve-location) (x &key ensure-directory wilden directory)
6395 "Resolve location designator X into a PATHNAME"
6396 ;; :directory backward compatibility, until 2014-01-16: accept directory as well as ensure-directory
6397 (loop* :with dirp = (or directory ensure-directory)
6398 :with (first . rest) = (if (atom x) (list x) x)
6399 :with path = (resolve-absolute-location
6400 first :ensure-directory (and (or dirp rest) t)
6401 :wilden (and wilden (null rest)))
6402 :for (element . morep) :on rest
6403 :for dir = (and (or morep dirp) t)
6404 :for wild = (and wilden (not morep))
6405 :for sub = (merge-pathnames*
6406 (resolve-relative-location
6407 element :ensure-directory dir :wilden wild)
6408 path)
6409 :do (setf path (if (absolute-pathname-p sub) (resolve-symlinks* sub) sub))
6410 :finally (return path)))
6412 (defun location-designator-p (x)
6413 "Is X a designator for a location?"
6414 (flet ((absolute-component-p (c)
6415 (typep c '(or string pathname
6416 (member :root :home :here :user-cache))))
6417 (relative-component-p (c)
6418 (typep c '(or string pathname
6419 (member :*/ :**/ :*.*.* :implementation :implementation-type)))))
6420 (or (typep x 'boolean)
6421 (absolute-component-p x)
6422 (and (consp x) (absolute-component-p (first x)) (every #'relative-component-p (rest x))))))
6424 (defun location-function-p (x)
6425 "Is X the specification of a location function?"
6426 (and
6427 (length=n-p x 2)
6428 (eq (car x) :function)))
6430 (defvar *clear-configuration-hook* '())
6432 (defun register-clear-configuration-hook (hook-function &optional call-now-p)
6433 "Register a function to be called when clearing configuration"
6434 (register-hook-function '*clear-configuration-hook* hook-function call-now-p))
6436 (defun clear-configuration ()
6437 "Call the functions in *CLEAR-CONFIGURATION-HOOK*"
6438 (call-functions *clear-configuration-hook*))
6440 (register-image-dump-hook 'clear-configuration)
6442 (defun upgrade-configuration ()
6443 "If a previous version of ASDF failed to read some configuration, try again now."
6444 (when *ignored-configuration-form*
6445 (clear-configuration)
6446 (setf *ignored-configuration-form* nil))))
6449 ;;;; -------------------------------------------------------------------------
6450 ;;; Hacks for backward-compatibility of the driver
6452 (uiop/package:define-package :uiop/backward-driver
6453 (:nicknames :asdf/backward-driver)
6454 (:recycle :uiop/backward-driver :asdf/backward-driver :asdf)
6455 (:use :uiop/common-lisp :uiop/package :uiop/utility
6456 :uiop/pathname :uiop/stream :uiop/os :uiop/image
6457 :uiop/run-program :uiop/lisp-build
6458 :uiop/configuration)
6459 (:export
6460 #:coerce-pathname #:component-name-to-pathname-components
6461 #+(or ecl mkcl) #:compile-file-keeping-object
6463 (in-package :uiop/backward-driver)
6465 ;;;; Backward compatibility with various pathname functions.
6467 (with-upgradability ()
6468 (defun coerce-pathname (name &key type defaults)
6469 ;; For backward-compatibility only, for people using internals
6470 ;; Reported users in quicklisp: hu.dwim.asdf, asdf-utils, xcvb
6471 ;; Will be removed after 2014-01-16.
6472 ;;(warn "Please don't use ASDF::COERCE-PATHNAME. Use ASDF/PATHNAME:PARSE-UNIX-NAMESTRING.")
6473 (parse-unix-namestring name :type type :defaults defaults))
6475 (defun component-name-to-pathname-components (unix-style-namestring
6476 &key force-directory force-relative)
6477 ;; Will be removed after 2014-01-16.
6478 ;; (warn "Please don't use ASDF::COMPONENT-NAME-TO-PATHNAME-COMPONENTS, use SPLIT-UNIX-NAMESTRING-DIRECTORY-COMPONENTS")
6479 (multiple-value-bind (relabs path filename file-only)
6480 (split-unix-namestring-directory-components
6481 unix-style-namestring :ensure-directory force-directory)
6482 (declare (ignore file-only))
6483 (when (and force-relative (not (eq relabs :relative)))
6484 (error (compatfmt "~@<Absolute pathname designator not allowed: ~3i~_~S~@:>")
6485 unix-style-namestring))
6486 (values relabs path filename)))
6488 #+(or ecl mkcl)
6489 (defun compile-file-keeping-object (&rest args) (apply #'compile-file* args)))
6490 ;;;; ---------------------------------------------------------------------------
6491 ;;;; Re-export all the functionality in UIOP
6493 (uiop/package:define-package :uiop/driver
6494 (:nicknames :uiop :asdf/driver :asdf-driver :asdf-utils)
6495 (:use :uiop/common-lisp)
6496 ;; NB: not reexporting uiop/common-lisp
6497 ;; which include all of CL with compatibility modifications on select platforms,
6498 ;; that could cause potential conflicts for packages that would :use (cl uiop)
6499 ;; or :use (closer-common-lisp uiop), etc.
6500 (:use-reexport
6501 :uiop/package :uiop/utility
6502 :uiop/os :uiop/pathname :uiop/stream :uiop/filesystem :uiop/image
6503 :uiop/run-program :uiop/lisp-build
6504 :uiop/configuration :uiop/backward-driver))
6506 ;; Provide both lowercase and uppercase, to satisfy more people.
6507 (provide "uiop") (provide "UIOP")
6508 (provide "UIOP")
6509 (provide "uiop")