Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
11365 lines (10234 sloc) 542 KB
;;; -*- mode: Common-Lisp; Base: 10 ; Syntax: ANSI-Common-Lisp ; buffer-read-only: t; -*-
;;; This is ASDF 3.1.3: Another System Definition Facility.
;;; Feedback, bug reports, and patches are all welcome:
;;; please mail to <>.
;;; Note first that the canonical source for ASDF is presently
;;; <URL:>.
;;; If you obtained this copy from anywhere else, and you experience
;;; trouble using it, or find bugs, you may want to check at the
;;; location above for a more recent version (and for documentation
;;; and test files, if your copy came without them) before reporting
;;; bugs. There are usually two "supported" revisions - the git master
;;; branch is the latest development version, whereas the git release
;;; branch may be slightly older but is considered `stable'
;;; (This is the MIT / X Consortium license as taken from
;;; on or about
;;; Monday; July 13, 2009)
;;; Copyright (c) 2001-2014 Daniel Barlow and contributors
;;; Permission is hereby granted, free of charge, to any person obtaining
;;; a copy of this software and associated documentation files (the
;;; "Software"), to deal in the Software without restriction, including
;;; without limitation the rights to use, copy, modify, merge, publish,
;;; distribute, sublicense, and/or sell copies of the Software, and to
;;; permit persons to whom the Software is furnished to do so, subject to
;;; the following conditions:
;;; The above copyright notice and this permission notice shall be
;;; included in all copies or substantial portions of the Software.
;;; The problem with writing a defsystem replacement is bootstrapping:
;;; we can't use defsystem to compile it. Hence, all in one file.
#+xcvb (module ())
(in-package :cl-user)
(eval-when (:load-toplevel :compile-toplevel :execute)
(setf ext:*gc-verbose* nil))
;;; pre 1.3.0 ABCL versions do not support the bundle-op on Mac OS X
(eval-when (:load-toplevel :compile-toplevel :execute)
(unless (and (member :darwin *features*)
(second (third (sys::arglist 'directory))))
(push :abcl-bundle-op-supported *features*)))
;; Punt on hard package upgrade: from ASDF1 always, and even from ASDF2 on most implementations.
(eval-when (:load-toplevel :compile-toplevel :execute)
(unless (member :asdf3 *features*)
(let* ((existing-version
(when (find-package :asdf)
(or (symbol-value (find-symbol (string :*asdf-version*) :asdf))
(let ((ver (symbol-value (find-symbol (string :*asdf-revision*) :asdf))))
(etypecase ver
(string ver)
(cons (format nil "~{~D~^.~}" ver))
(null "1.0"))))))
(first-dot (when existing-version (position #\. existing-version)))
(second-dot (when first-dot (position #\. existing-version :start (1+ first-dot))))
(existing-major-minor (subseq existing-version 0 second-dot))
(existing-version-number (and existing-version (read-from-string existing-major-minor)))
(away (format nil "~A-~A" :asdf existing-version)))
(when (and existing-version
(< existing-version-number
#+(or allegro clisp lispworks sbcl) 2.0
#-(or allegro clisp lispworks sbcl) 2.27))
(rename-package :asdf away)
(when *load-verbose*
(format t "~&; Renamed old ~A package away to ~A~%" :asdf away))))))
;;;; ---------------------------------------------------------------------------
;;;; Handle ASDF package upgrade, including implementation-dependent magic.
;; See
(defpackage :uiop/package
;; CAUTION: we must handle the first few packages specially for hot-upgrade.
;; This package definition MUST NOT change unless its name too changes;
;; if/when it changes, don't forget to add new functions missing from below.
;; Until then, uiop/package is frozen to forever
;; import and export the same exact symbols as for ASDF 2.27.
;; Any other symbol must be import-from'ed and re-export'ed in a different package.
(:use :common-lisp)
#:find-package* #:find-symbol* #:symbol-call
#:intern* #:export* #:import* #:shadowing-import* #:shadow* #:make-symbol* #:unintern*
#:symbol-shadowing-p #:home-package-p
#:symbol-package-name #:standard-common-lisp-symbol-p
#:reify-package #:unreify-package #:reify-symbol #:unreify-symbol
#:nuke-symbol-in-package #:nuke-symbol #:rehome-symbol
#:ensure-package-unused #:delete-package*
#:package-names #:packages-from-names #:fresh-package-name #:rename-package-away
#:package-definition-form #:parse-define-package-form
#:ensure-package #:define-package))
(in-package :uiop/package)
;;;; General purpose package utilities
(eval-when (:load-toplevel :compile-toplevel :execute)
(defun find-package* (package-designator &optional (error t))
(let ((package (find-package package-designator)))
(package package)
(error (error "No package named ~S" (string package-designator)))
(t nil))))
(defun find-symbol* (name package-designator &optional (error t))
"Find a symbol in a package of given string'ified NAME;
unless CL:FIND-SYMBOL, work well with 'modern' case sensitive syntax
by letting you supply a symbol or keyword for the name;
also works well when the package is not present.
If optional ERROR argument is NIL, return NIL instead of an error
when the symbol is not found."
(block nil
(let ((package (find-package* package-designator error)))
(when package ;; package error handled by find-package* already
(multiple-value-bind (symbol status) (find-symbol (string name) package)
(status (return (values symbol status)))
(error (error "There is no symbol ~S in package ~S" name (package-name package))))))
(values nil nil))))
(defun symbol-call (package name &rest args)
"Call a function associated with symbol of given name in given package,
with given ARGS. Useful when the call is read before the package is loaded,
or when loading the package is optional."
(apply (find-symbol* name package) args))
(defun intern* (name package-designator &optional (error t))
(intern (string name) (find-package* package-designator error)))
(defun export* (name package-designator)
(let* ((package (find-package* package-designator))
(symbol (intern* name package)))
(export (or symbol (list symbol)) package)))
(defun import* (symbol package-designator)
(import (or symbol (list symbol)) (find-package* package-designator)))
(defun shadowing-import* (symbol package-designator)
(shadowing-import (or symbol (list symbol)) (find-package* package-designator)))
(defun shadow* (name package-designator)
(shadow (string name) (find-package* package-designator)))
(defun make-symbol* (name)
(etypecase name
(string (make-symbol name))
(symbol (copy-symbol name))))
(defun unintern* (name package-designator &optional (error t))
(block nil
(let ((package (find-package* package-designator error)))
(when package
(multiple-value-bind (symbol status) (find-symbol* name package error)
(status (unintern symbol package)
(return (values symbol status)))
(error (error "symbol ~A not present in package ~A"
(string symbol) (package-name package))))))
(values nil nil))))
(defun symbol-shadowing-p (symbol package)
(and (member symbol (package-shadowing-symbols package)) t))
(defun home-package-p (symbol package)
(and package (let ((sp (symbol-package symbol)))
(and sp (let ((pp (find-package* package)))
(and pp (eq sp pp))))))))
(eval-when (:load-toplevel :compile-toplevel :execute)
(defun symbol-package-name (symbol)
(let ((package (symbol-package symbol)))
(and package (package-name package))))
(defun standard-common-lisp-symbol-p (symbol)
(multiple-value-bind (sym status) (find-symbol* symbol :common-lisp nil)
(and (eq sym symbol) (eq status :external))))
(defun reify-package (package &optional package-context)
(if (eq package package-context) t
(etypecase package
(null nil)
((eql (find-package :cl)) :cl)
(package (package-name package)))))
(defun unreify-package (package &optional package-context)
(etypecase package
(null nil)
((eql t) package-context)
((or symbol string) (find-package package))))
(defun reify-symbol (symbol &optional package-context)
(etypecase symbol
((or keyword (satisfies standard-common-lisp-symbol-p)) symbol)
(symbol (vector (symbol-name symbol)
(reify-package (symbol-package symbol) package-context)))))
(defun unreify-symbol (symbol &optional package-context)
(etypecase symbol
(symbol symbol)
((simple-vector 2)
(let* ((symbol-name (svref symbol 0))
(package-foo (svref symbol 1))
(package (unreify-package package-foo package-context)))
(if package (intern* symbol-name package)
(make-symbol* symbol-name)))))))
(eval-when (:load-toplevel :compile-toplevel :execute)
(defvar *all-package-happiness* '())
(defvar *all-package-fishiness* (list t))
(defun record-fishy (info)
;;(format t "~&FISHY: ~S~%" info)
(push info *all-package-fishiness*))
(defmacro when-package-fishiness (&body body)
`(when *all-package-fishiness* ,@body))
(defmacro note-package-fishiness (&rest info)
`(when-package-fishiness (record-fishy (list ,@info)))))
(eval-when (:load-toplevel :compile-toplevel :execute)
#+(or clisp clozure)
(defun get-setf-function-symbol (symbol)
#+clisp (let ((sym (get symbol 'system::setf-function)))
(if sym (values sym :setf-function)
(let ((sym (get symbol 'system::setf-expander)))
(if sym (values sym :setf-expander)
(values nil nil)))))
#+clozure (gethash symbol ccl::%setf-function-names%))
#+(or clisp clozure)
(defun set-setf-function-symbol (new-setf-symbol symbol &optional kind)
#+clisp (assert (member kind '(:setf-function :setf-expander)))
#+clozure (assert (eq kind t))
((null new-setf-symbol)
(remprop symbol 'system::setf-function)
(remprop symbol 'system::setf-expander))
((eq kind :setf-function)
(setf (get symbol 'system::setf-function) new-setf-symbol))
((eq kind :setf-expander)
(setf (get symbol 'system::setf-expander) new-setf-symbol))
(t (error "invalid kind of setf-function ~S for ~S to be set to ~S"
kind symbol new-setf-symbol)))
(gethash symbol ccl::%setf-function-names%) new-setf-symbol
(gethash new-setf-symbol ccl::%setf-function-name-inverses%) symbol))
#+(or clisp clozure)
(defun create-setf-function-symbol (symbol)
#+clisp (system::setf-symbol symbol)
#+clozure (ccl::construct-setf-function-name symbol))
(defun set-dummy-symbol (symbol reason other-symbol)
(setf (get symbol 'dummy-symbol) (cons reason other-symbol)))
(defun make-dummy-symbol (symbol)
(let ((dummy (copy-symbol symbol)))
(set-dummy-symbol dummy 'replacing symbol)
(set-dummy-symbol symbol 'replaced-by dummy)
(defun dummy-symbol (symbol)
(get symbol 'dummy-symbol))
(defun get-dummy-symbol (symbol)
(let ((existing (dummy-symbol symbol)))
(if existing (values (cdr existing) (car existing))
(make-dummy-symbol symbol))))
(defun nuke-symbol-in-package (symbol package-designator)
(let ((package (find-package* package-designator))
(name (symbol-name symbol)))
(multiple-value-bind (sym stat) (find-symbol name package)
(when (and (member stat '(:internal :external)) (eq symbol sym))
(if (symbol-shadowing-p symbol package)
(shadowing-import* (get-dummy-symbol symbol) package)
(unintern* symbol package))))))
(defun nuke-symbol (symbol &optional (packages (list-all-packages)))
#+(or clisp clozure)
(multiple-value-bind (setf-symbol kind)
(get-setf-function-symbol symbol)
(when kind (nuke-symbol setf-symbol)))
(loop :for p :in packages :do (nuke-symbol-in-package symbol p)))
(defun rehome-symbol (symbol package-designator)
"Changes the home package of a symbol, also leaving it present in its old home if any"
(let* ((name (symbol-name symbol))
(package (find-package* package-designator))
(old-package (symbol-package symbol))
(old-status (and old-package (nth-value 1 (find-symbol name old-package))))
(shadowing (and old-package (symbol-shadowing-p symbol old-package) (make-symbol name))))
(multiple-value-bind (overwritten-symbol overwritten-symbol-status) (find-symbol name package)
(unless (eq package old-package)
(let ((overwritten-symbol-shadowing-p
(and overwritten-symbol-status
(symbol-shadowing-p overwritten-symbol package))))
:rehome-symbol name
(when old-package (package-name old-package)) old-status (and shadowing t)
(package-name package) overwritten-symbol-status overwritten-symbol-shadowing-p)
(when old-package
(if shadowing
(shadowing-import* shadowing old-package))
(unintern* symbol old-package))
(shadowing-import* symbol package))
(when overwritten-symbol-status
(unintern* overwritten-symbol package))
(import* symbol package)))
(if shadowing
(shadowing-import* symbol old-package)
(import* symbol old-package))
#+(or clisp clozure)
(multiple-value-bind (setf-symbol kind)
(get-setf-function-symbol symbol)
(when kind
(let* ((setf-function (fdefinition setf-symbol))
(new-setf-symbol (create-setf-function-symbol symbol)))
name (package-name package)
(symbol-name setf-symbol) (symbol-package-name setf-symbol)
(symbol-name new-setf-symbol) (symbol-package-name new-setf-symbol))
(when (symbol-package setf-symbol)
(unintern* setf-symbol (symbol-package setf-symbol)))
(setf (fdefinition new-setf-symbol) setf-function)
(set-setf-function-symbol new-setf-symbol symbol kind))))
#+(or clisp clozure)
(multiple-value-bind (overwritten-setf foundp)
(get-setf-function-symbol overwritten-symbol)
(when foundp
(unintern overwritten-setf)))
(when (eq old-status :external)
(export* symbol old-package))
(when (eq overwritten-symbol-status :external)
(export* symbol package))))
(values overwritten-symbol overwritten-symbol-status))))
(defun ensure-package-unused (package)
(loop :for p :in (package-used-by-list package) :do
(unuse-package package p)))
(defun delete-package* (package &key nuke)
(let ((p (find-package package)))
(when p
(when nuke (do-symbols (s p) (when (home-package-p s p) (nuke-symbol s))))
(ensure-package-unused p)
(delete-package package))))
(defun package-names (package)
(cons (package-name package) (package-nicknames package)))
(defun packages-from-names (names)
(remove-duplicates (remove nil (mapcar #'find-package names)) :from-end t))
(defun fresh-package-name (&key (prefix :%TO-BE-DELETED)
(index (random most-positive-fixnum)))
(loop :for i :from index
:for n = (format nil "~A~@[~A~D~]" prefix (and (plusp i) (or separator "")) i)
:thereis (and (not (find-package n)) n)))
(defun rename-package-away (p &rest keys &key prefix &allow-other-keys)
(let ((new-name
(apply 'fresh-package-name
:prefix (or prefix (format nil "__~A__" (package-name p))) keys)))
(record-fishy (list :rename-away (package-names p) new-name))
(rename-package p new-name))))
;;; Communicable representation of symbol and package information
(eval-when (:load-toplevel :compile-toplevel :execute)
(defun package-definition-form (package-designator
&key (nicknamesp t) (usep t)
(shadowp t) (shadowing-import-p t)
(exportp t) (importp t) internp (error t))
(let* ((package (or (find-package* package-designator error)
(return-from package-definition-form nil)))
(name (package-name package))
(nicknames (package-nicknames package))
(use (mapcar #'package-name (package-use-list package)))
(shadow ())
(shadowing-import (make-hash-table :test 'equal))
(import (make-hash-table :test 'equal))
(export ())
(intern ()))
(when package
(loop :for sym :being :the :symbols :in package
:for status = (nth-value 1 (find-symbol* sym package)) :do
(ecase status
((nil :inherited))
((:internal :external)
(let* ((name (symbol-name sym))
(external (eq status :external))
(home (symbol-package sym))
(home-name (package-name home))
(imported (not (eq home package)))
(shadowing (symbol-shadowing-p sym package)))
((and shadowing imported)
(push name (gethash home-name shadowing-import)))
(push name shadow))
(push name (gethash home-name import))))
(push name export))
(t (push name intern)))))))
(labels ((sort-names (names)
(sort (copy-list names) #'string<))
(table-keys (table)
(loop :for k :being :the :hash-keys :of table :collect k))
(when-relevant (key value)
(when value (list (cons key value))))
(import-options (key table)
(loop :for i :in (sort-names (table-keys table))
:collect `(,key ,i ,@(sort-names (gethash i table))))))
`(defpackage ,name
,@(when-relevant :nicknames (and nicknamesp (sort-names nicknames)))
(:use ,@(and usep (sort-names use)))
,@(when-relevant :shadow (and shadowp (sort-names shadow)))
,@(import-options :shadowing-import-from (and shadowing-import-p shadowing-import))
,@(import-options :import-from (and importp import))
,@(when-relevant :export (and exportp (sort-names export)))
,@(when-relevant :intern (and internp (sort-names intern)))))))))
;;; ensure-package, define-package
(eval-when (:load-toplevel :compile-toplevel :execute)
(defun ensure-shadowing-import (name to-package from-package shadowed imported)
(check-type name string)
(check-type to-package package)
(check-type from-package package)
(check-type shadowed hash-table)
(check-type imported hash-table)
(let ((import-me (find-symbol* name from-package)))
(multiple-value-bind (existing status) (find-symbol name to-package)
((gethash name shadowed)
(unless (eq import-me existing)
(error "Conflicting shadowings for ~A" name)))
(setf (gethash name shadowed) t)
(setf (gethash name imported) t)
(unless (or (null status)
(and (member status '(:internal :external))
(eq existing import-me)
(symbol-shadowing-p existing to-package)))
:shadowing-import name
(package-name from-package)
(or (home-package-p import-me from-package) (symbol-package-name import-me))
(package-name to-package) status
(and status (or (home-package-p existing to-package) (symbol-package-name existing)))))
(shadowing-import* import-me to-package))))))
(defun ensure-imported (import-me into-package &optional from-package)
(check-type import-me symbol)
(check-type into-package package)
(check-type from-package (or null package))
(let ((name (symbol-name import-me)))
(multiple-value-bind (existing status) (find-symbol name into-package)
((not status)
(import* import-me into-package))
((eq import-me existing))
(let ((shadowing-p (symbol-shadowing-p existing into-package)))
:ensure-imported name
(and from-package (package-name from-package))
(or (home-package-p import-me from-package) (symbol-package-name import-me))
(package-name into-package)
(and status (or (home-package-p existing into-package) (symbol-package-name existing)))
((or shadowing-p (eq status :inherited))
(shadowing-import* import-me into-package))
(unintern* existing into-package)
(import* import-me into-package))))))))
(defun ensure-import (name to-package from-package shadowed imported)
(check-type name string)
(check-type to-package package)
(check-type from-package package)
(check-type shadowed hash-table)
(check-type imported hash-table)
(multiple-value-bind (import-me import-status) (find-symbol name from-package)
(when (null import-status)
:import-uninterned name (package-name from-package) (package-name to-package))
(setf import-me (intern* name from-package)))
(multiple-value-bind (existing status) (find-symbol name to-package)
((and imported (gethash name imported))
(unless (and status (eq import-me existing))
(error "Can't import ~S from both ~S and ~S"
name (package-name (symbol-package existing)) (package-name from-package))))
((gethash name shadowed)
(error "Can't both shadow ~S and import it from ~S" name (package-name from-package)))
(setf (gethash name imported) t))))
(ensure-imported import-me to-package from-package)))
(defun ensure-inherited (name symbol to-package from-package mixp shadowed imported inherited)
(check-type name string)
(check-type symbol symbol)
(check-type to-package package)
(check-type from-package package)
(check-type mixp (member nil t)) ; no cl:boolean on Genera
(check-type shadowed hash-table)
(check-type imported hash-table)
(check-type inherited hash-table)
(multiple-value-bind (existing status) (find-symbol name to-package)
(let* ((sp (symbol-package symbol))
(in (gethash name inherited))
(xp (and status (symbol-package existing))))
(when (null sp)
:import-uninterned name
(package-name from-package) (package-name to-package) mixp)
(import* symbol from-package)
(setf sp (package-name from-package)))
((gethash name shadowed))
(unless (equal sp (first in))
(if mixp
(ensure-shadowing-import name to-package (second in) shadowed imported)
(error "Can't inherit ~S from ~S, it is inherited from ~S"
name (package-name sp) (package-name (first in))))))
((gethash name imported)
(unless (eq symbol existing)
(error "Can't inherit ~S from ~S, it is imported from ~S"
name (package-name sp) (package-name xp))))
(setf (gethash name inherited) (list sp from-package))
(when (and status (not (eq sp xp)))
(let ((shadowing (symbol-shadowing-p existing to-package)))
:inherited name
(package-name from-package)
(or (home-package-p symbol from-package) (symbol-package-name symbol))
(package-name to-package)
(or (home-package-p existing to-package) (symbol-package-name existing)))
(if shadowing (ensure-shadowing-import name to-package from-package shadowed imported)
(unintern* existing to-package)))))))))
(defun ensure-mix (name symbol to-package from-package shadowed imported inherited)
(check-type name string)
(check-type symbol symbol)
(check-type to-package package)
(check-type from-package package)
(check-type shadowed hash-table)
(check-type imported hash-table)
(check-type inherited hash-table)
(unless (gethash name shadowed)
(multiple-value-bind (existing status) (find-symbol name to-package)
(let* ((sp (symbol-package symbol))
(im (gethash name imported))
(in (gethash name inherited)))
((or (null status)
(and status (eq symbol existing))
(and in (eq sp (first in))))
(ensure-inherited name symbol to-package from-package t shadowed imported inherited))
(remhash name inherited)
(ensure-shadowing-import name to-package (second in) shadowed imported))
(error "Symbol ~S import from ~S~:[~; actually ~:[uninterned~;~:*from ~S~]~] conflicts with existing symbol in ~S~:[~; actually ~:[uninterned~;from ~:*~S~]~]"
name (package-name from-package)
(home-package-p symbol from-package) (symbol-package-name symbol)
(package-name to-package)
(home-package-p existing to-package) (symbol-package-name existing)))
(ensure-inherited name symbol to-package from-package t shadowed imported inherited)))))))
(defun recycle-symbol (name recycle exported)
;; Takes a symbol NAME (a string), a list of package designators for RECYCLE
;; packages, and a hash-table of names (strings) of symbols scheduled to be
;; EXPORTED from the package being defined. It returns two values, the
;; symbol found (if any, or else NIL), and a boolean flag indicating whether
;; a symbol was found. The caller (DEFINE-PACKAGE) will then do the
;; re-homing of the symbol, etc.
(check-type name string)
(check-type recycle list)
(check-type exported hash-table)
(when (gethash name exported) ;; don't bother recycling private symbols
(let (recycled foundp)
(dolist (r recycle (values recycled foundp))
(multiple-value-bind (symbol status) (find-symbol name r)
(when (and status (home-package-p symbol r))
;; (nuke-symbol symbol)) -- even simple variable names like O or C will do that.
(note-package-fishiness :recycled-duplicate name (package-name foundp) (package-name r)))
(setf recycled symbol foundp r)))))))))
(defun symbol-recycled-p (sym recycle)
(check-type sym symbol)
(check-type recycle list)
(and (member (symbol-package sym) recycle) t))
(defun ensure-symbol (name package intern recycle shadowed imported inherited exported)
(check-type name string)
(check-type package package)
(check-type intern (member nil t)) ; no cl:boolean on Genera
(check-type shadowed hash-table)
(check-type imported hash-table)
(check-type inherited hash-table)
(unless (or (gethash name shadowed)
(gethash name imported)
(gethash name inherited))
(multiple-value-bind (existing status)
(find-symbol name package)
(multiple-value-bind (recycled previous) (recycle-symbol name recycle exported)
((and status (eq existing recycled) (eq previous package)))
(rehome-symbol recycled package))
((and status (eq package (symbol-package existing))))
(when status
:ensure-symbol name
(reify-package (symbol-package existing) package)
status intern)
(unintern existing))
(when intern
(intern* name package))))))))
(declaim (ftype (function (t t t &optional t) t) ensure-exported))
(defun ensure-exported-to-user (name symbol to-package &optional recycle)
(check-type name string)
(check-type symbol symbol)
(check-type to-package package)
(check-type recycle list)
(assert (equal name (symbol-name symbol)))
(multiple-value-bind (existing status) (find-symbol name to-package)
(unless (and status (eq symbol existing))
(let ((accessible
(or (null status)
(let ((shadowing (symbol-shadowing-p existing to-package))
(recycled (symbol-recycled-p existing recycle)))
(unless (and shadowing (not recycled))
:ensure-export name (symbol-package-name symbol)
(package-name to-package)
(or (home-package-p existing to-package) (symbol-package-name existing))
status shadowing)
(if (or (eq status :inherited) shadowing)
(shadowing-import* symbol to-package)
(unintern existing to-package))
(when (and accessible (eq status :external))
(ensure-exported name symbol to-package recycle))))))
(defun ensure-exported (name symbol from-package &optional recycle)
(dolist (to-package (package-used-by-list from-package))
(ensure-exported-to-user name symbol to-package recycle))
(unless (eq from-package (symbol-package symbol))
(ensure-imported symbol from-package))
(export* name from-package))
(defun ensure-export (name from-package &optional recycle)
(multiple-value-bind (symbol status) (find-symbol* name from-package)
(unless (eq status :external)
(ensure-exported name symbol from-package recycle))))
(defun ensure-package (name &key
nicknames documentation use
shadow shadowing-import-from
import-from export intern
recycle mix reexport
#+genera (declare (ignore documentation))
(let* ((package-name (string name))
(nicknames (mapcar #'string nicknames))
(names (cons package-name nicknames))
(previous (packages-from-names names))
(discarded (cdr previous))
(to-delete ())
(package (or (first previous) (make-package package-name :nicknames nicknames)))
(recycle (packages-from-names recycle))
(use (mapcar 'find-package* use))
(mix (mapcar 'find-package* mix))
(reexport (mapcar 'find-package* reexport))
(shadow (mapcar 'string shadow))
(export (mapcar 'string export))
(intern (mapcar 'string intern))
(unintern (mapcar 'string unintern))
(shadowed (make-hash-table :test 'equal)) ; string to bool
(imported (make-hash-table :test 'equal)) ; string to bool
(exported (make-hash-table :test 'equal)) ; string to bool
;; string to list home package and use package:
(inherited (make-hash-table :test 'equal)))
(when-package-fishiness (record-fishy package-name))
(when documentation (setf (documentation package t) documentation))
(loop :for p :in (set-difference (package-use-list package) (append mix use))
:do (note-package-fishiness :over-use name (package-names p))
(unuse-package p package))
(loop :for p :in discarded
:for n = (remove-if #'(lambda (x) (member x names :test 'equal))
(package-names p))
:do (note-package-fishiness :nickname name (package-names p))
(cond (n (rename-package p (first n) (rest n)))
(t (rename-package-away p)
(push p to-delete))))
(rename-package package package-name nicknames)
(dolist (name unintern)
(multiple-value-bind (existing status) (find-symbol name package)
(when status
(unless (eq status :inherited)
:unintern (package-name package) name (symbol-package-name existing) status)
(unintern* name package nil)))))
(dolist (name export)
(setf (gethash name exported) t))
(dolist (p reexport)
(do-external-symbols (sym p)
(setf (gethash (string sym) exported) t)))
(do-external-symbols (sym package)
(let ((name (symbol-name sym)))
(unless (gethash name exported)
:over-export (package-name package) name
(or (home-package-p sym package) (symbol-package-name sym)))
(unexport sym package))))
(dolist (name shadow)
(setf (gethash name shadowed) t)
(multiple-value-bind (existing status) (find-symbol name package)
(multiple-value-bind (recycled previous) (recycle-symbol name recycle exported)
(let ((shadowing (and status (symbol-shadowing-p existing package))))
((eq previous package))
(rehome-symbol recycled package))
((or (member status '(nil :inherited))
(home-package-p existing package)))
(let ((dummy (make-symbol name)))
:shadow-imported (package-name package) name
(symbol-package-name existing) status shadowing)
(shadowing-import* dummy package)
(import* dummy package)))))))
(shadow* name package))
(loop :for (p . syms) :in shadowing-import-from
:for pp = (find-package* p) :do
(dolist (sym syms) (ensure-shadowing-import (string sym) package pp shadowed imported)))
(loop :for p :in mix
:for pp = (find-package* p) :do
(do-external-symbols (sym pp) (ensure-mix (symbol-name sym) sym package pp shadowed imported inherited)))
(loop :for (p . syms) :in import-from
:for pp = (find-package p) :do
(dolist (sym syms) (ensure-import (symbol-name sym) package pp shadowed imported)))
(dolist (p (append use mix))
(do-external-symbols (sym p) (ensure-inherited (string sym) sym package p nil shadowed imported inherited))
(use-package p package))
(loop :for name :being :the :hash-keys :of exported :do
(ensure-symbol name package t recycle shadowed imported inherited exported)
(ensure-export name package recycle))
(dolist (name intern)
(ensure-symbol name package t recycle shadowed imported inherited exported))
(do-symbols (sym package)
(ensure-symbol (symbol-name sym) package nil recycle shadowed imported inherited exported))
(map () 'delete-package* to-delete)
(eval-when (:load-toplevel :compile-toplevel :execute)
(defun parse-define-package-form (package clauses)
:with use-p = nil :with recycle-p = nil
:with documentation = nil
:for (kw . args) :in clauses
:when (eq kw :nicknames) :append args :into nicknames :else
:when (eq kw :documentation)
:do (cond
(documentation (error "define-package: can't define documentation twice"))
((or (atom args) (cdr args)) (error "define-package: bad documentation"))
(t (setf documentation (car args)))) :else
:when (eq kw :use) :append args :into use :and :do (setf use-p t) :else
:when (eq kw :shadow) :append args :into shadow :else
:when (eq kw :shadowing-import-from) :collect args :into shadowing-import-from :else
:when (eq kw :import-from) :collect args :into import-from :else
:when (eq kw :export) :append args :into export :else
:when (eq kw :intern) :append args :into intern :else
:when (eq kw :recycle) :append args :into recycle :and :do (setf recycle-p t) :else
:when (eq kw :mix) :append args :into mix :else
:when (eq kw :reexport) :append args :into reexport :else
:when (eq kw :use-reexport) :append args :into use :and :append args :into reexport
:and :do (setf use-p t) :else
:when (eq kw :mix-reexport) :append args :into mix :and :append args :into reexport
:and :do (setf use-p t) :else
:when (eq kw :unintern) :append args :into unintern :else
:do (error "unrecognized define-package keyword ~S" kw)
:finally (return `(,package
:nicknames ,nicknames :documentation ,documentation
:use ,(if use-p use '(:common-lisp))
:shadow ,shadow :shadowing-import-from ,shadowing-import-from
:import-from ,import-from :export ,export :intern ,intern
:recycle ,(if recycle-p recycle (cons package nicknames))
:mix ,mix :reexport ,reexport :unintern ,unintern)))))
(defmacro define-package (package &rest clauses)
"DEFINE-PACKAGE takes a PACKAGE and a number of CLAUSES, of the form
DEFINE-PACKAGE supports the following keywords:
RECYCLE -- Recycle the package's exported symbols from the specified packages,
in order. For every symbol scheduled to be exported by the DEFINE-PACKAGE,
either through an :EXPORT option or a :REEXPORT option, if the symbol exists in
one of the :RECYCLE packages, the first such symbol is re-homed to the package
being defined.
For the sake of idempotence, it is important that the package being defined
should appear in first position if it already exists, and even if it doesn't,
ahead of any package that is not going to be deleted afterwards and never
created again. In short, except for special cases, always make it the first
package on the list if the list is not empty.
MIX -- Takes a list of package designators. MIX behaves like
\(:USE PKG1 PKG2 ... PKGn\) but additionally uses :SHADOWING-IMPORT-FROM to
resolve conflicts in favor of the first found symbol. It may still yield
an error if there is a conflict with an explicitly :IMPORT-FROM symbol.
REEXPORT -- Takes a list of package designators. For each package, p, in the list,
export symbols with the same name as those exported from p. Note that in the case
of shadowing, etc. the symbols with the same name may not be the same symbols.
UNINTERN -- Remove symbols here from PACKAGE."
(let ((ensure-form
`(apply 'ensure-package ',(parse-define-package-form package clauses))))
#+(or ecl gcl mkcl) (defpackage ,package (:use))
(eval-when (:compile-toplevel :load-toplevel :execute)
;;;; Final tricks to keep various implementations happy.
;; We want most such tricks in common-lisp.lisp,
;; but these need to be done before the define-package form there,
;; that we nevertheless want to be the very first form.
(eval-when (:load-toplevel :compile-toplevel :execute)
#+allegro ;; We need to disable autoloading BEFORE any mention of package ASDF.
(setf excl::*autoload-package-name-alist*
(remove "asdf" excl::*autoload-package-name-alist*
:test 'equalp :key 'car)))
;; Compatibility with whoever calls asdf/package
(define-package :asdf/package (:use :cl :uiop/package) (:reexport :uiop/package))
;;;; -------------------------------------------------------------------------
;;;; Handle compatibility with multiple implementations.
;;; This file is for papering over the deficiencies and peculiarities
;;; of various Common Lisp implementations.
;;; For implementation-specific access to the system, see os.lisp instead.
;;; A few functions are defined here, but actually exported from utility;
;;; from this package only common-lisp symbols are exported.
(uiop/package:define-package :uiop/common-lisp
(:nicknames :uoip/cl :asdf/common-lisp :asdf/cl)
(:use :uiop/package)
(:use-reexport #-genera :common-lisp #+genera :future-common-lisp)
(:recycle :uiop/common-lisp :uoip/cl :asdf/common-lisp :asdf/cl :asdf)
#+allegro (:intern #:*acl-warn-save*)
#+cormanlisp (:shadow #:user-homedir-pathname)
#:logical-pathname #:translate-logical-pathname
#:make-broadcast-stream #:file-namestring)
#+genera (:shadowing-import-from :scl #:boolean)
#+genera (:export #:boolean #:ensure-directories-exist #:read-sequence #:write-sequence)
#+mcl (:shadow #:user-homedir-pathname))
(in-package :uiop/common-lisp)
#-(or abcl allegro clisp clozure cmu cormanlisp ecl gcl genera lispworks mcl mkcl sbcl scl xcl)
(error "ASDF is not supported on your implementation. Please help us port it.")
;; (declaim (optimize (speed 1) (debug 3) (safety 3))) ; DON'T: trust implementation defaults.
;;;; Early meta-level tweaks
#+(or abcl allegro clisp cmu ecl mkcl clozure lispworks mkcl sbcl scl)
(eval-when (:load-toplevel :compile-toplevel :execute)
;; Check for unicode at runtime, so that a hypothetical FASL compiled with unicode
;; but loaded in a non-unicode setting (e.g. on Allegro) won't tell a lie.
(when (and #+allegro (member :ics *features*)
#+(or clisp cmu ecl mkcl) (member :unicode *features*)
#+sbcl (member :sb-unicode *features*))
(pushnew :asdf-unicode *features*)))
(eval-when (:load-toplevel :compile-toplevel :execute)
(defparameter *acl-warn-save*
(when (boundp 'excl:*warn-on-nested-reader-conditionals*)
(when (boundp 'excl:*warn-on-nested-reader-conditionals*)
(setf excl:*warn-on-nested-reader-conditionals* nil))
(setf *print-readably* nil))
#+clozure (in-package :ccl)
#+(and clozure windows-target) ;; See
(eval-when (:load-toplevel :compile-toplevel :execute)
(unless (fboundp 'external-process-wait)
(defun external-process-wait (proc)
(when (and (external-process-pid proc) (eq (external-process-%status proc) :running))
(wait-on-semaphore (external-process-completed proc))))
(values (external-process-%exit-code proc)
(external-process-%status proc))))))
#+clozure (in-package :uiop/common-lisp)
(eval-when (:load-toplevel :compile-toplevel :execute)
(deftype logical-pathname () nil)
(defun make-broadcast-stream () *error-output*)
(defun translate-logical-pathname (x) x)
(defun user-homedir-pathname (&optional host)
(declare (ignore host))
(parse-namestring (format nil "~A\\" (cl:user-homedir-pathname))))
(defun file-namestring (p)
(setf p (pathname p))
(format nil "~@[~A~]~@[.~A~]" (pathname-name p) (pathname-type p))))
(eval-when (:load-toplevel :compile-toplevel :execute)
(setf *load-verbose* nil)
(defun use-ecl-byte-compiler-p () (and (member :ecl-bytecmp *features*) t))
(unless (use-ecl-byte-compiler-p) (require :cmp)))
(eval-when (:load-toplevel :compile-toplevel :execute)
(unless (member :ansi-cl *features*)
(error "ASDF only supports GCL in ANSI mode. Aborting.~%"))
(setf compiler::*compiler-default-type* (pathname "")
compiler::*lsp-ext* "")
#.(let ((code ;; Only support very recent GCL 2.7.0 from November 2013 or later.
((or (< system::*gcl-major-version* 2)
(and (= system::*gcl-major-version* 2)
(< system::*gcl-minor-version* 7)))
'(error "GCL 2.7 or later required to use ASDF")))))
(eval code)
(eval-when (:load-toplevel :compile-toplevel :execute)
(unless (fboundp 'lambda)
(defmacro lambda (&whole form &rest bvl-decls-and-body)
(declare (ignore bvl-decls-and-body)(zwei::indentation 1 1))
`#',(cons 'lisp::lambda (cdr form))))
(unless (fboundp 'ensure-directories-exist)
(defun ensure-directories-exist (path)
(fs:create-directories-recursively (pathname path))))
(unless (fboundp 'read-sequence)
(defun read-sequence (sequence stream &key (start 0) end)
(scl:send stream :string-in nil sequence start end)))
(unless (fboundp 'write-sequence)
(defun write-sequence (sequence stream &key (start 0) end)
(scl:send stream :string-out sequence start end)
#.(or #+mcl ;; the #$ doesn't work on other lisps, even protected by #+mcl, so we use this trick
"(eval-when (:load-toplevel :compile-toplevel :execute)
(ccl:define-entry-point (_getenv \"getenv\") ((name :string)) :string)
(ccl:define-entry-point (_system \"system\") ((name :string)) :int)
;; Note: ASDF may expect user-homedir-pathname to provide
;; the pathname of the current user's home directory, whereas
;; MCL by default provides the directory from which MCL was started.
;; See
(defun user-homedir-pathname ()
(ccl::findfolder #$kuserdomain #$kCurrentUserFolderType))
(defun probe-posix (posix-namestring)
\"If a file exists for the posix namestring, return the pathname\"
(ccl::with-cstrs ((cpath posix-namestring))
(ccl::rlet ((is-dir :boolean)
(fsref :fsref))
(when (eq #$noerr (#_fspathmakeref cpath fsref is-dir))
(ccl::%path-from-fsref fsref is-dir))))))"))
(eval-when (:load-toplevel :compile-toplevel :execute)
(require :cmp)
(setq clos::*redefine-class-in-place* t)) ;; Make sure we have strict ANSI class redefinition semantics
;;;; Looping
(eval-when (:load-toplevel :compile-toplevel :execute)
(defmacro loop* (&rest rest)
#-genera `(loop ,@rest)
#+genera `(lisp:loop ,@rest))) ;; In genera, CL:LOOP can't destructure, so we use LOOP*. Sigh.
;;;; compatfmt: avoid fancy format directives when unsupported
(eval-when (:load-toplevel :compile-toplevel :execute)
(defun frob-substrings (string substrings &optional frob)
"for each substring in SUBSTRINGS, find occurrences of it within STRING
that don't use parts of matched occurrences of previous strings, and
FROB them, that is to say, remove them if FROB is NIL,
replace by FROB if FROB is a STRING, or if FROB is a FUNCTION,
call FROB with the match and a function that emits a string in the output.
Return a string made of the parts not omitted or emitted by FROB."
(declare (optimize (speed 0) (safety #-gcl 3 #+gcl 0) (debug 3)))
(let ((length (length string)) (stream nil))
(labels ((emit-string (x &optional (start 0) (end (length x)))
(when (< start end)
(unless stream (setf stream (make-string-output-stream)))
(write-string x stream :start start :end end)))
(emit-substring (start end)
(when (and (zerop start) (= end length))
(return-from frob-substrings string))
(emit-string string start end))
(recurse (substrings start end)
((>= start end))
((null substrings) (emit-substring start end))
(t (let* ((sub-spec (first substrings))
(sub (if (consp sub-spec) (car sub-spec) sub-spec))
(fun (if (consp sub-spec) (cdr sub-spec) frob))
(found (search sub string :start2 start :end2 end))
(more (rest substrings)))
(recurse more start found)
(etypecase fun
(string (emit-string fun))
(function (funcall fun sub #'emit-string)))
(recurse substrings (+ found (length sub)) end))
(recurse more start end))))))))
(recurse substrings 0 length))
(if stream (get-output-stream-string stream) "")))
(defmacro compatfmt (format)
#+(or gcl genera)
(frob-substrings format `("~3i~_" #+genera ,@'("~@<" "~@;" "~@:>" "~:>")))
#-(or gcl genera) format))
;;;; -------------------------------------------------------------------------
;;;; General Purpose Utilities for ASDF
(uiop/package:define-package :uiop/utility
(:nicknames :asdf/utility)
(:recycle :uiop/utility :asdf/utility :asdf)
(:use :uiop/common-lisp :uiop/package)
;; import and reexport a few things defined in :uiop/common-lisp
(:import-from :uiop/common-lisp #:compatfmt #:loop* #:frob-substrings
#+ecl #:use-ecl-byte-compiler-p #+mcl #:probe-posix)
(:export #:compatfmt #:loop* #:frob-substrings #:compatfmt
#+ecl #:use-ecl-byte-compiler-p #+mcl #:probe-posix)
;; magic helper to define debugging functions:
#:uiop-debug #:load-uiop-debug-utility #:*uiop-debug-utility*
#:with-upgradability ;; (un)defining functions in an upgrade-friendly way
#:undefine-function #:undefine-functions #:defun* #:defgeneric*
#:nest #:if-let ;; basic flow control
#:while-collecting #:appendf #:length=n-p #:ensure-list ;; lists
#:remove-plist-keys #:remove-plist-key ;; plists
#:emptyp ;; sequences
#:+non-base-chars-exist-p+ ;; characters
#:+max-character-type-index+ #:character-type-index #:+character-types+
#:base-string-p #:strings-common-element-type #:reduce/strcat #:strcat ;; strings
#:first-char #:last-char #:split-string #:stripln #:+cr+ #:+lf+ #:+crlf+
#:string-prefix-p #:string-enclosed-p #:string-suffix-p
#:coerce-class ;; CLOS
#:stamp< #:stamps< #:stamp*< #:stamp<= ;; stamps
#:earlier-stamp #:stamps-earliest #:earliest-stamp
#:later-stamp #:stamps-latest #:latest-stamp #:latest-stamp-f
#:list-to-hash-set #:ensure-gethash ;; hash-table
#:ensure-function #:access-at #:access-at-count ;; functions
#:call-function #:call-functions #:register-hook-function
#:match-condition-p #:match-any-condition-p ;; conditions
#:call-with-muffled-conditions #:with-muffled-conditions
#:lexicographic< #:lexicographic<=
#:parse-version #:unparse-version #:version< #:version<= #:version-compatible-p)) ;; version
(in-package :uiop/utility)
;;;; Defining functions in a way compatible with hot-upgrade:
;; DEFUN* and DEFGENERIC* use FMAKUNBOUND to delete any previous fdefinition,
;; thus replacing the function without warning or error
;; even if the signature and/or generic-ness of the function has changed.
;; For a generic function, this invalidates any previous DEFMETHOD.
(eval-when (:load-toplevel :compile-toplevel :execute)
(defun undefine-function (function-spec)
((symbolp function-spec)
;; undefining the previous function is the portable way
;; of overriding any incompatible previous gf,
;; but CLISP needs extra help with getting rid of previous methods.
(let ((f (and (fboundp function-spec) (fdefinition function-spec))))
(when (typep f 'clos:standard-generic-function)
(loop :for m :in (clos:generic-function-methods f)
:do (remove-method f m))))
(fmakunbound function-spec))
((and (consp function-spec) (eq (car function-spec) 'setf)
(consp (cdr function-spec)) (null (cddr function-spec)))
(fmakunbound function-spec))
(t (error "bad function spec ~S" function-spec))))
(defun undefine-functions (function-spec-list)
(map () 'undefine-function function-spec-list))
((defdef (def* def)
`(defmacro ,def* (name formals &rest rest)
(destructuring-bind (name &key (supersede t))
(if (or (atom name) (eq (car name) 'setf))
(list name :supersede nil)
(declare (ignorable supersede))
;; We usually try to do it only for the functions that need it,
;; which happens in asdf/upgrade - however, for ECL, we need this hammer.
,@(when (or supersede #+ecl t)
`((undefine-function ',name)))
,@(when (and #+ecl (symbolp name)) ; fails for setf functions on ecl
`((declaim (notinline ,name))))
(,',def ,name ,formals ,@rest))))))
(defdef defgeneric* defgeneric)
(defdef defun* defun))
(defmacro with-upgradability ((&optional) &body body)
"Evaluate BODY at compile- load- and run- times, with DEFUN and DEFGENERIC modified
to also declare the functions NOTINLINE and to accept a wrapping the function name
specification into a list with keyword argument SUPERSEDE (which defaults to T if the name
is not wrapped, and NIL if it is wrapped). If SUPERSEDE is true, call UNDEFINE-FUNCTION
to supersede any previous definition."
`(eval-when (:compile-toplevel :load-toplevel :execute)
,@(loop :for form :in body :collect
(if (consp form)
(destructuring-bind (car . cdr) form
(case car
((defun) `(defun* ,@cdr))
((defgeneric) `(defgeneric* ,@cdr))
(otherwise form)))
;;; Magic debugging help. See contrib/debug.lisp
(with-upgradability ()
(defvar *uiop-debug-utility*
'(or (ignore-errors
(symbol-call :asdf :system-relative-pathname :uiop "contrib/debug.lisp"))
(symbol-call :uiop/pathname :subpathname (user-homedir-pathname) "cl/asdf/uiop/contrib/debug.lisp"))
"form that evaluates to the pathname to your favorite debugging utilities")
(defmacro uiop-debug (&rest keys)
`(eval-when (:compile-toplevel :load-toplevel :execute)
(load-uiop-debug-utility ,@keys)))
(defun load-uiop-debug-utility (&key package utility-file)
(let* ((*package* (if package (find-package package) *package*))
(keyword (read-from-string
(format nil ":DBG-~:@(~A~)" (package-name *package*)))))
(unless (member keyword *features*)
(let* ((utility-file (or utility-file *uiop-debug-utility*))
(file (ignore-errors (probe-file (eval utility-file)))))
(if file (load file)
(error "Failed to locate debug utility file: ~S" utility-file)))))))
;;; Flow control
(with-upgradability ()
(defmacro nest (&rest things)
"Macro to do keep code nesting and indentation under control." ;; Thanks to mbaringer
(reduce #'(lambda (outer inner) `(,@outer ,inner))
things :from-end t))
(defmacro if-let (bindings &body (then-form &optional else-form)) ;; from alexandria
;; bindings can be (var form) or ((var1 form1) ...)
(let* ((binding-list (if (and (consp bindings) (symbolp (car bindings)))
(list bindings)
(variables (mapcar #'car binding-list)))
`(let ,binding-list
(if (and ,@variables)
;;; List manipulation
(with-upgradability ()
(defmacro while-collecting ((&rest collectors) &body body)
"COLLECTORS should be a list of names for collections. A collector
defines a function that, when applied to an argument inside BODY, will
add its argument to the corresponding collection. Returns multiple values,
a list for each collection, in order.
\(while-collecting \(foo bar\)
\(dolist \(x '\(\(a 1\) \(b 2\) \(c 3\)\)\)
\(foo \(first x\)\)
\(bar \(second x\)\)\)\)
Returns two values: \(A B C\) and \(1 2 3\)."
(let ((vars (mapcar #'(lambda (x) (gensym (symbol-name x))) collectors))
(initial-values (mapcar (constantly nil) collectors)))
`(let ,(mapcar #'list vars initial-values)
(flet ,(mapcar #'(lambda (c v) `(,c (x) (push x ,v) (values))) collectors vars)
(values ,@(mapcar #'(lambda (v) `(reverse ,v)) vars))))))
(define-modify-macro appendf (&rest args)
append "Append onto list") ;; only to be used on short lists.
(defun length=n-p (x n) ;is it that (= (length x) n) ?
(check-type n (integer 0 *))
:for l = x :then (cdr l)
:for i :downfrom n :do
((zerop i) (return (null l)))
((not (consp l)) (return nil)))))
(defun ensure-list (x)
(if (listp x) x (list x))))
;;; remove a key from a plist, i.e. for keyword argument cleanup
(with-upgradability ()
(defun remove-plist-key (key plist)
"Remove a single key from a plist"
(loop* :for (k v) :on plist :by #'cddr
:unless (eq k key)
:append (list k v)))
(defun remove-plist-keys (keys plist)
"Remove a list of keys from a plist"
(loop* :for (k v) :on plist :by #'cddr
:unless (member k keys)
:append (list k v))))
;;; Sequences
(with-upgradability ()
(defun emptyp (x)
"Predicate that is true for an empty sequence"
(or (null x) (and (vectorp x) (zerop (length x))))))
;;; Characters
(with-upgradability () ;; base-char != character on ECL, LW, SBCL, Genera. LW also has SIMPLE-CHAR.
(defconstant +non-base-chars-exist-p+ #.(not (subtypep 'character 'base-char)))
#-scl ;; In SCL, all characters seem to be 16-bit base-char, but this flag gets set somehow???
(when +non-base-chars-exist-p+ (pushnew :non-base-chars-exist-p *features*)))
(with-upgradability ()
(defparameter +character-types+ ;; assuming a simple hierarchy
#(#+non-base-chars-exist-p base-char #+lispworks lw:simple-char character))
(defparameter +max-character-type-index+ (1- (length +character-types+))))
(with-upgradability ()
(defun character-type-index (x)
(declare (ignorable x))
#.(case +max-character-type-index+
(0 0)
(1 '(etypecase x
(character (if (typep x 'base-char) 0 1))
(symbol (if (subtypep x 'base-char) 0 1))))
'(or (position-if (etypecase x
(character #'(lambda (type) (typep x type)))
(symbol #'(lambda (type) (subtypep x type))))
(error "Not a character or character type: ~S" x))))))
;;; Strings
(with-upgradability ()
(defun base-string-p (string)
"Does the STRING only contain BASE-CHARs?"
(declare (ignorable string))
(and #+non-base-chars-exist-p (eq 'base-char (array-element-type string))))
(defun strings-common-element-type (strings)
"What least subtype of CHARACTER can contain all the elements of all the STRINGS?"
(declare (ignorable strings))
#.(if +non-base-chars-exist-p+
`(aref +character-types+
(loop :with index = 0 :for s :in strings :do
((= index ,+max-character-type-index+) (return index))
((emptyp s)) ;; NIL or empty string
((characterp s) (setf index (max index (character-type-index s))))
((stringp s) (unless (>= index (character-type-index (array-element-type s)))
(setf index (reduce 'max s :key #'character-type-index
:initial-value index))))
(t (error "Invalid string designator ~S for ~S" s 'strings-common-element-type)))
:finally (return index)))
(defun reduce/strcat (strings &key key start end)
"Reduce a list as if by STRCAT, accepting KEY START and END keywords like REDUCE.
NIL is interpreted as an empty string. A character is interpreted as a string of length one."
(when (or start end) (setf strings (subseq strings start end)))
(when key (setf strings (mapcar key strings)))
(loop :with output = (make-string (loop :for s :in strings
:sum (if (characterp s) 1 (length s)))
:element-type (strings-common-element-type strings))
:with pos = 0
:for input :in strings
:do (etypecase input
(character (setf (char output pos) input) (incf pos))
(string (replace output input :start1 pos) (incf pos (length input))))
:finally (return output)))
(defun strcat (&rest strings)
"Concatenate strings.
NIL is interpreted as an empty string, a character as a string of length one."
(reduce/strcat strings))
(defun first-char (s)
"Return the first character of a non-empty string S, or NIL"
(and (stringp s) (plusp (length s)) (char s 0)))
(defun last-char (s)
"Return the last character of a non-empty string S, or NIL"
(and (stringp s) (plusp (length s)) (char s (1- (length s)))))
(defun split-string (string &key max (separator '(#\Space #\Tab)))
"Split STRING into a list of components separated by
any of the characters in the sequence SEPARATOR.
If MAX is specified, then no more than max(1,MAX) components will be returned,
starting the separation from the end, e.g. when called with arguments
\"a.b.c.d.e\" :max 3 :separator \".\" it will return (\"a.b.c\" \"d\" \"e\")."
(block ()
(let ((list nil) (words 0) (end (length string)))
(when (zerop end) (return nil))
(flet ((separatorp (char) (find char separator))
(done () (return (cons (subseq string 0 end) list))))
:for start = (if (and max (>= words (1- max)))
(position-if #'separatorp string :end end :from-end t))
:do (when (null start) (done))
(push (subseq string (1+ start) end) list)
(incf words)
(setf end start))))))
(defun string-prefix-p (prefix string)
"Does STRING begin with PREFIX?"
(let* ((x (string prefix))
(y (string string))
(lx (length x))
(ly (length y)))
(and (<= lx ly) (string= x y :end2 lx))))
(defun string-suffix-p (string suffix)
"Does STRING end with SUFFIX?"
(let* ((x (string string))
(y (string suffix))
(lx (length x))
(ly (length y)))
(and (<= ly lx) (string= x y :start1 (- lx ly)))))
(defun string-enclosed-p (prefix string suffix)
"Does STRING begin with PREFIX and end with SUFFIX?"
(and (string-prefix-p prefix string)
(string-suffix-p string suffix))))
(defvar +cr+ (coerce #(#\Return) 'string))
(defvar +lf+ (coerce #(#\Linefeed) 'string))
(defvar +crlf+ (coerce #(#\Return #\Linefeed) 'string))
(defun stripln (x)
"Strip a string X from any ending CR, LF or CRLF.
Return two values, the stripped string and the ending that was stripped,
or the original value and NIL if no stripping took place.
Since our STRCAT accepts NIL as empty string designator,
the two results passed to STRCAT always reconstitute the original string"
(check-type x string)
(block nil
(flet ((c (end) (when (string-suffix-p x end)
(return (values (subseq x 0 (- (length x) (length end))) end)))))
(when x (c +crlf+) (c +lf+) (c +cr+) (values x nil)))))
;;; stamps: a REAL or a boolean where NIL=-infinity, T=+infinity
(eval-when (#-lispworks :compile-toplevel :load-toplevel :execute)
(deftype stamp () '(or real boolean)))
(with-upgradability ()
(defun stamp< (x y)
(etypecase x
(null (and y t))
((eql t) nil)
(real (etypecase y
(null nil)
((eql t) t)
(real (< x y))))))
(defun stamps< (list) (loop :for y :in list :for x = nil :then y :always (stamp< x y)))
(defun stamp*< (&rest list) (stamps< list))
(defun stamp<= (x y) (not (stamp< y x)))
(defun earlier-stamp (x y) (if (stamp< x y) x y))
(defun stamps-earliest (list) (reduce 'earlier-stamp list :initial-value t))
(defun earliest-stamp (&rest list) (stamps-earliest list))
(defun later-stamp (x y) (if (stamp< x y) y x))
(defun stamps-latest (list) (reduce 'later-stamp list :initial-value nil))
(defun latest-stamp (&rest list) (stamps-latest list))
(define-modify-macro latest-stamp-f (&rest stamps) latest-stamp))
;;; Function designators
(with-upgradability ()
(defun ensure-function (fun &key (package :cl))
"Coerce the object FUN into a function.
If FUN is a FUNCTION, return it.
If the FUN is a non-sequence literal constant, return constantly that,
i.e. for a boolean keyword character number or pathname.
Otherwise if FUN is a non-literally constant symbol, return its FDEFINITION.
If FUN is a CONS, return the function that applies its CAR
to the appended list of the rest of its CDR and the arguments,
unless the CAR is LAMBDA, in which case the expression is evaluated.
If FUN is a string, READ a form from it in the specified PACKAGE (default: CL)
and EVAL that in a (FUNCTION ...) context."
(etypecase fun
(function fun)
((or boolean keyword character number pathname) (constantly fun))
(hash-table #'(lambda (x) (gethash x fun)))
(symbol (fdefinition fun))
(cons (if (eq 'lambda (car fun))
(eval fun)
#'(lambda (&rest args) (apply (car fun) (append (cdr fun) args)))))
(string (eval `(function ,(with-standard-io-syntax
(let ((*package* (find-package package)))
(read-from-string fun))))))))
(defun access-at (object at)
"Given an OBJECT and an AT specifier, list of successive accessors,
call each accessor on the result of the previous calls.
An accessor may be an integer, meaning a call to ELT,
a keyword, meaning a call to GETF,
NIL, meaning identity,
a function or other symbol, meaning itself,
or a list of a function designator and arguments, interpreted as per ENSURE-FUNCTION.
As a degenerate case, the AT specifier may be an atom of a single such accessor
instead of a list."
(flet ((access (object accessor)
(etypecase accessor
(function (funcall accessor object))
(integer (elt object accessor))
(keyword (getf object accessor))
(null object)
(symbol (funcall accessor object))
(cons (funcall (ensure-function accessor) object)))))
(if (listp at)
(dolist (accessor at object)
(setf object (access object accessor)))
(access object at))))
(defun access-at-count (at)
"From an AT specification, extract a COUNT of maximum number
of sub-objects to read as per ACCESS-AT"
((integerp at)
(1+ at))
((and (consp at) (integerp (first at)))
(1+ (first at)))))
(defun call-function (function-spec &rest arguments)
"Call the function designated by FUNCTION-SPEC as per ENSURE-FUNCTION,
with the given ARGUMENTS"
(apply (ensure-function function-spec) arguments))
(defun call-functions (function-specs)
"For each function in the list FUNCTION-SPECS, in order, call the function as per CALL-FUNCTION"
(map () 'call-function function-specs))
(defun register-hook-function (variable hook &optional call-now-p)
"Push the HOOK function (a designator as per ENSURE-FUNCTION) onto the hook VARIABLE.
When CALL-NOW-P is true, also call the function immediately."
(pushnew hook (symbol-value variable) :test 'equal)
(when call-now-p (call-function hook))))
;;; CLOS
(with-upgradability ()
(defun coerce-class (class &key (package :cl) (super t) (error 'error))
"Coerce CLASS to a class that is subclass of SUPER if specified,
or invoke ERROR handler as per CALL-FUNCTION.
A keyword designates the name a symbol, which when found in either PACKAGE, designates a class.
-- for backward compatibility, *PACKAGE* is also accepted for now, but this may go in the future.
A string is read as a symbol while in PACKAGE, the symbol designates a class.
A class object designates itself.
NIL designates itself (no class).
A symbol otherwise designates a class by name."
(let* ((normalized
(typecase class
(keyword (or (find-symbol* class package nil)
(find-symbol* class *package* nil)))
(string (symbol-call :uiop :safe-read-from-string class :package package))
(t class)))
(etypecase normalized
((or standard-class built-in-class) normalized)
((or null keyword) nil)
(symbol (find-class normalized nil nil)))))
(or (and found
(or (eq super t) (#-cormanlisp subtypep #+cormanlisp cl::subclassp found super))
(call-function error "Can't coerce ~S to a ~@[class~;subclass of ~:*~S]" class super)))))
;;; Hash-tables
(with-upgradability ()
(defun ensure-gethash (key table default)
"Lookup the TABLE for a KEY as by GETHASH, but if not present,
call the (possibly constant) function designated by DEFAULT as per CALL-FUNCTION,
set the corresponding entry to the result in the table.
Return two values: the entry after its optional computation, and whether it was found"
(multiple-value-bind (value foundp) (gethash key table)
(if foundp
(setf (gethash key table) (call-function default)))
(defun list-to-hash-set (list &aux (h (make-hash-table :test 'equal)))
"Convert a LIST into hash-table that has the same elements when viewed as a set,
up to the given equality TEST"
(dolist (x list h) (setf (gethash x h) t))))
;;; Version handling
(with-upgradability ()
(defun unparse-version (version-list)
(format nil "~{~D~^.~}" version-list))
(defun parse-version (version-string &optional on-error)
"Parse a VERSION-STRING as a series of natural integers separated by dots.
Return a (non-null) list of integers if the string is valid;
otherwise return NIL.
When invalid, ON-ERROR is called as per CALL-FUNCTION before to return NIL,
with format arguments explaining why the version is invalid.
ON-ERROR is also called if the version is not canonical
in that it doesn't print back to itself, but the list is returned anyway."
(block nil
(unless (stringp version-string)
(call-function on-error "~S: ~S is not a string" 'parse-version version-string)
(unless (loop :for prev = nil :then c :for c :across version-string
:always (or (digit-char-p c)
(and (eql c #\.) prev (not (eql prev #\.))))
:finally (return (and c (digit-char-p c))))
(call-function on-error "~S: ~S doesn't follow asdf version numbering convention"
'parse-version version-string)
(let* ((version-list
(mapcar #'parse-integer (split-string version-string :separator ".")))
(normalized-version (unparse-version version-list)))
(unless (equal version-string normalized-version)
(call-function on-error "~S: ~S contains leading zeros" 'parse-version version-string))
(defun lexicographic< (< x y)
(cond ((null y) nil)
((null x) t)
((funcall < (car x) (car y)) t)
((funcall < (car y) (car x)) nil)
(t (lexicographic< < (cdr x) (cdr y)))))
(defun lexicographic<= (< x y)
(not (lexicographic< < y x)))
(defun version< (version1 version2)
(let ((v1 (parse-version version1 nil))
(v2 (parse-version version2 nil)))
(lexicographic< '< v1 v2)))
(defun version<= (version1 version2)
(not (version< version2 version1)))
(defun version-compatible-p (provided-version required-version)
"Is the provided version a compatible substitution for the required-version?
If major versions differ, it's not compatible.
If they are equal, then any later version is compatible,
with later being determined by a lexicographical comparison of minor numbers."
(let ((x (parse-version provided-version nil))
(y (parse-version required-version nil)))
(and x y (= (car x) (car y)) (lexicographic<= '< (cdr y) (cdr x))))))
;;; Condition control
(with-upgradability ()
(defparameter +simple-condition-format-control-slot+
#+abcl 'system::format-control
#+allegro 'excl::format-control
#+clisp 'system::$format-control
#+clozure 'ccl::format-control
#+(or cmu scl) 'conditions::format-control
#+(or ecl mkcl) 'si::format-control
#+(or gcl lispworks) 'conditions::format-string
#+sbcl 'sb-kernel:format-control
#-(or abcl allegro clisp clozure cmu ecl gcl lispworks mkcl sbcl scl) nil
"Name of the slot for FORMAT-CONTROL in simple-condition")
(defun match-condition-p (x condition)
"Compare received CONDITION to some pattern X:
a symbol naming a condition class,
a simple vector of length 2, arguments to find-symbol* with result as above,
or a string describing the format-control of a simple-condition."
(etypecase x
(symbol (typep condition x))
((simple-vector 2)
(ignore-errors (typep condition (find-symbol* (svref x 0) (svref x 1) nil))))
(function (funcall x condition))
(string (and (typep condition 'simple-condition)
;; On SBCL, it's always set and the check triggers a warning
#+(or allegro clozure cmu lispworks scl)
(slot-boundp condition +simple-condition-format-control-slot+)
(ignore-errors (equal (simple-condition-format-control condition) x))))))
(defun match-any-condition-p (condition conditions)
"match CONDITION against any of the patterns of CONDITIONS supplied"
(loop :for x :in conditions :thereis (match-condition-p x condition)))
(defun call-with-muffled-conditions (thunk conditions)
"calls the THUNK in a context where the CONDITIONS are muffled"
(handler-bind ((t #'(lambda (c) (when (match-any-condition-p c conditions)
(muffle-warning c)))))
(funcall thunk)))
(defmacro with-muffled-conditions ((conditions) &body body)
`(call-with-muffled-conditions #'(lambda () ,@body) ,conditions)))
;;;; ---------------------------------------------------------------------------
;;;; Access to the Operating System
(uiop/package:define-package :uiop/os
(:nicknames :asdf/os)
(:recycle :uiop/os :asdf/os :asdf)
(:use :uiop/common-lisp :uiop/package :uiop/utility)
#:featurep #:os-unix-p #:os-macosx-p #:os-windows-p #:os-genera-p #:detect-os ;; features
#:getenv #:getenvp ;; environment variables
#:implementation-identifier ;; implementation identifier
#:implementation-type #:*implementation-type*
#:operating-system #:architecture #:lisp-version-string
#:hostname #:getcwd #:chdir
;; Windows shortcut support
#:read-null-terminated-string #:read-little-endian
#:parse-file-location-info #:parse-windows-shortcut))
(in-package :uiop/os)
;;; Features
(with-upgradability ()
(defun featurep (x &optional (*features* *features*))
"Checks whether a feature expression X is true with respect to the *FEATURES* set,
as per the CLHS standard for #+ and #-. Beware that just like the CLHS,
we assume symbols from the KEYWORD package are used, but that unless you're using #+/#-
your reader will not have magically used the KEYWORD package, so you need specify
keywords explicitly."
((atom x) (and (member x *features*) t))
((eq :not (car x)) (assert (null (cddr x))) (not (featurep (cadr x))))
((eq :or (car x)) (some #'featurep (cdr x)))
((eq :and (car x)) (every #'featurep (cdr x)))
(t (error "Malformed feature specification ~S" x))))
(defun os-unix-p ()
"Is the underlying operating system some Unix variant?"
(or #+abcl (featurep :unix)
#+(and (not abcl) (or unix cygwin darwin)) t))
(defun os-macosx-p ()
"Is the underlying operating system MacOS X?"
;; OS-MACOSX is not mutually exclusive with OS-UNIX,
;; in fact the former implies the latter.
#+allegro (featurep :macosx)
#+clisp (featurep :macos)
(featurep :darwin)))
(defun os-windows-p ()
"Is the underlying operating system Microsoft Windows?"
(or #+abcl (featurep :windows)
#+(and (not (or abcl unix cygwin darwin)) (or win32 windows mswindows mingw32 mingw64)) t))
(defun os-genera-p ()
"Is the underlying operating system Genera (running on a Symbolics Lisp Machine)?"
(or #+genera t))
(defun os-oldmac-p ()
"Is the underlying operating system an (emulated?) MacOS 9 or earlier?"
(or #+mcl t))
(defun detect-os ()
"Detects the current operating system. Only needs be run at compile-time,
except on ABCL where it might change between FASL compilation and runtime."
(loop* :with o
:for (feature . detect) :in '((:os-unix . os-unix-p) (:os-macosx . os-macosx-p)
(:os-windows . os-windows-p)
(:genera . os-genera-p) (:os-oldmac . os-oldmac-p))
:when (and (or (not o) (eq feature :os-macosx)) (funcall detect))
:do (setf o feature) (pushnew feature *features*)
:else :do (setf *features* (remove feature *features*))
(return (or o (error "Congratulations for trying ASDF on an operating system~%~
that is neither Unix, nor Windows, nor Genera, nor even old MacOS.~%Now you port it.")))))
;;;; Environment variables: getting them, and parsing them.
(with-upgradability ()
(defun getenv (x)
"Query the environment, as in C getenv.
Beware: may return empty string if a variable is present but empty;
use getenvp to return NIL in such a case."
(declare (ignorable x))
#+(or abcl clisp ecl xcl) (ext:getenv x)
#+allegro (sys:getenv x)
#+clozure (ccl:getenv x)
#+(or cmu scl) (cdr (assoc x ext:*environment-list* :test #'string=))
(let* ((buffer (ct:malloc 1))
(cname (ct:lisp-string-to-c-string x))
(needed-size (win:getenvironmentvariable cname buffer 0))
(buffer1 (ct:malloc (1+ needed-size))))
(prog1 (if (zerop (win:getenvironmentvariable cname buffer1 needed-size))
(ct:c-string-to-lisp-string buffer1))
(ct:free buffer)
(ct:free buffer1)))
#+gcl (system:getenv x)
#+genera nil
#+lispworks (lispworks:environment-variable x)
#+mcl (ccl:with-cstrs ((name x))
(let ((value (_getenv name)))
(unless (ccl:%null-ptr-p value)
(ccl:%get-cstring value))))
#+mkcl (#.(or (find-symbol* 'getenv :si nil) (find-symbol* 'getenv :mk-ext nil)) x)
#+sbcl (sb-ext:posix-getenv x)
#-(or abcl allegro clisp clozure cmu cormanlisp ecl gcl genera lispworks mcl mkcl sbcl scl xcl)
(error "~S is not supported on your implementation" 'getenv))
(defun getenvp (x)
"Predicate that is true if the named variable is present in the libc environment,
then returning the non-empty string value of the variable"
(let ((g (getenv x))) (and (not (emptyp g)) g))))
;;;; implementation-identifier
;; produce a string to identify current implementation.
;; Initially stolen from SLIME's SWANK, completely rewritten since.
;; We're back to runtime checking, for the sake of e.g. ABCL.
(with-upgradability ()
(defun first-feature (feature-sets)
"A helper for various feature detection functions"
(dolist (x feature-sets)
(multiple-value-bind (short long feature-expr)
(if (consp x)
(values (first x) (second x) (cons :or (rest x)))
(values x x x))
(when (featurep feature-expr)
(return (values short long))))))
(defun implementation-type ()
"The type of Lisp implementation used, as a short UIOP-standardized keyword"
'(:abcl (:acl :allegro) (:ccl :clozure) :clisp (:corman :cormanlisp)
(:cmu :cmucl :cmu) :ecl :gcl
(:lwpe :lispworks-personal-edition) (:lw :lispworks)
:mcl :mkcl :sbcl :scl (:smbx :symbolics) :xcl)))
(defvar *implementation-type* (implementation-type)
"The type of Lisp implementation used, as a short UIOP-standardized keyword")
(defun operating-system ()
"The operating system of the current host"
(:win :windows :mswindows :win32 :mingw32) ;; try cygwin first!
(:linux :linux :linux-target) ;; for GCL at least, must appear before :bsd
(:macosx :macosx :darwin :darwin-target :apple) ; also before :bsd
(:solaris :solaris :sunos)
(:bsd :bsd :freebsd :netbsd :openbsd :dragonfly)
(defun architecture ()
"The CPU architecture of the current host"
'((:x64 :x86-64 :x86_64 :x8664-target :amd64 (:and :word-size=64 :pc386))
(:x86 :x86 :i386 :i486 :i586 :i686 :pentium3 :pentium4 :pc386 :iapx386 :x8632-target)
(:ppc64 :ppc64 :ppc64-target) (:ppc32 :ppc32 :ppc32-target :ppc :powerpc)
:hppa64 :hppa :sparc64 (:sparc32 :sparc32 :sparc)
:mipsel :mipseb :mips :alpha (:arm :arm :arm-target) :imach
;; Java comes last: if someone uses C via CFFI or otherwise JNA or JNI,
;; we may have to segregate the code still by architecture.
(:java :java :java-1.4 :java-1.5 :java-1.6 :java-1.7))))
(defun ccl-fasl-version ()
;; the fasl version is target-dependent from CCL 1.8 on.
(or (let ((s 'ccl::target-fasl-version))
(and (fboundp s) (funcall s)))
(and (boundp 'ccl::fasl-version)
(symbol-value 'ccl::fasl-version))
(error "Can't determine fasl version.")))
(defun lisp-version-string ()
"return a string that identifies the current Lisp implementation version"
(let ((s (lisp-implementation-version)))
(car ; as opposed to OR, this idiom prevents some unreachable code warning
(format nil "~A~@[~A~]~@[~A~]~@[~A~]"
;; M means "modern", as opposed to ANSI-compatible mode (which I consider default)
(and (eq excl:*current-case-mode* :case-sensitive-lower) "M")
;; Note if not using International ACL
;; see
(excl:ics-target-case (:-ics "8"))
(and (member :smp *features*) "S"))
#+armedbear (format nil "~a-fasl~a" s system::*fasl-version*)
(subseq s 0 (position #\space s)) ; strip build information (date, etc.)
(format nil "~d.~d-f~d" ; shorten for windows
(logand (ccl-fasl-version) #xFF))
#+cmu (substitute #\- #\/ s)
#+scl (format nil "~A~A" s
;; ANSI upper case vs lower case.
(ecase ext:*case-mode* (:upper "") (:lower "l")))
#+ecl (format nil "~A~@[-~A~]" s
(let ((vcs-id (ext:lisp-implementation-vcs-id)))
(subseq vcs-id 0 (min (length vcs-id) 8))))
#+gcl (subseq s (1+ (position #\space s)))
(multiple-value-bind (major minor) (sct:get-system-version "System")
(format nil "~D.~D" major minor))
#+mcl (subseq s 8) ; strip the leading "Version "
(defun implementation-identifier ()
"Return a string that identifies the ABI of the current implementation,
suitable for use as a directory name to segregate Lisp FASLs, C dynamic libraries, etc."
#\_ #'(lambda (x) (find x " /:;&^\\|?<>(){}[]$#`'\""))
(format nil "~(~a~@{~@[-~a~]~}~)"
(or (implementation-type) (lisp-implementation-type))
(or (lisp-version-string) (lisp-implementation-version))
(or (operating-system) (software-type))
(or (architecture) (machine-type))))))
;;;; Other system information
(with-upgradability ()
(defun hostname ()
"return the hostname of the current host"
;; Note: untested on RMCL
#+(or abcl clozure cmu ecl genera lispworks mcl mkcl sbcl scl xcl) (machine-instance)
#+cormanlisp "localhost" ;; is there a better way? Does it matter?
#+allegro (symbol-call :excl.osi :gethostname)
#+clisp (first (split-string (machine-instance) :separator " "))
#+gcl (system:gethostname)))
;;; Current directory
(with-upgradability ()
(defun parse-unix-namestring* (unix-namestring)
"variant of LISP::PARSE-UNIX-NAMESTRING that returns a pathname object"
(multiple-value-bind (host device directory name type version)
(lisp::parse-unix-namestring unix-namestring 0 (length unix-namestring))
(make-pathname :host (or host lisp::*unix-host*) :device device
:directory directory :name name :type type :version version)))
(defun getcwd ()
"Get the current working directory as per POSIX getcwd(3), as a pathname object"
(or #+abcl (truename (symbol-call :asdf/filesystem :parse-native-namestring
(java:jstatic "getProperty" "java.lang.System" "user.dir")
:ensure-directory t))
#+allegro (excl::current-directory)
#+clisp (ext:default-directory)
#+clozure (ccl:current-directory)
#+(or cmu scl) (#+cmu parse-unix-namestring* #+scl lisp::parse-unix-namestring
(strcat (nth-value 1 (unix:unix-current-directory)) "/"))
#+cormanlisp (pathname (pl::get-current-directory)) ;; Q: what type does it return?
#+ecl (ext:getcwd)
#+gcl (let ((*default-pathname-defaults* #p"")) (truename #p""))
#+genera *default-pathname-defaults* ;; on a Lisp OS, it *is* canonical!
#+lispworks (system:current-directory)
#+mkcl (mk-ext:getcwd)
#+sbcl (sb-ext:parse-native-namestring (sb-unix:posix-getcwd/))
#+xcl (extensions:current-directory)
(error "getcwd not supported on your implementation")))
(defun chdir (x)
"Change current directory, as per POSIX chdir(2), to a given pathname object"
(if-let (x (pathname x))
(or #+abcl (java:jstatic "setProperty" "java.lang.System" "user.dir" (namestring x))
#+allegro (excl:chdir x)
#+clisp (ext:cd x)
#+clozure (setf (ccl:current-directory) x)
#+(or cmu scl) (unix:unix-chdir (ext:unix-namestring x))
#+cormanlisp (unless (zerop (win32::_chdir (namestring x)))
(error "Could not set current directory to ~A" x))
#+ecl (ext:chdir x)
#+genera (setf *default-pathname-defaults* x)
#+lispworks (hcl:change-directory x)
#+mkcl (mk-ext:chdir x)
#+sbcl (progn (require :sb-posix) (symbol-call :sb-posix :chdir (sb-ext:native-namestring x)))
(error "chdir not supported on your implementation")))))
;;;; -----------------------------------------------------------------
;;;; Windows shortcut support. Based on:
;;;; Jesse Hager: The Windows Shortcut File Format.
#-(or clisp genera) ; CLISP doesn't need it, and READ-SEQUENCE annoys old Genera that doesn't need it
(with-upgradability ()
(defparameter *link-initial-dword* 76)
(defparameter *link-guid* #(1 20 2 0 0 0 0 0 192 0 0 0 0 0 0 70))
(defun read-null-terminated-string (s)
"Read a null-terminated string from an octet stream S"
;; note: doesn't play well with UNICODE
(with-output-to-string (out)
(loop :for code = (read-byte s)
:until (zerop code)
:do (write-char (code-char code) out))))
(defun read-little-endian (s &optional (bytes 4))
"Read a number in little-endian format from an byte (octet) stream S,
the number having BYTES octets (defaulting to 4)."
(loop :for i :from 0 :below bytes
:sum (ash (read-byte s) (* 8 i))))
(defun parse-file-location-info (s)
"helper to parse-windows-shortcut"
(let ((start (file-position s))
(total-length (read-little-endian s))
(end-of-header (read-little-endian s))
(fli-flags (read-little-endian s))
(local-volume-offset (read-little-endian s))
(local-offset (read-little-endian s))
(network-volume-offset (read-little-endian s))
(remaining-offset (read-little-endian s)))
(declare (ignore total-length end-of-header local-volume-offset))
(unless (zerop fli-flags)
((logbitp 0 fli-flags)
(file-position s (+ start local-offset)))
((logbitp 1 fli-flags)
(file-position s (+ start
(strcat (read-null-terminated-string s)
(file-position s (+ start remaining-offset))
(read-null-terminated-string s))))))
(defun parse-windows-shortcut (pathname)
"From a .lnk windows shortcut, extract the pathname linked to"
;; NB: doesn't do much checking & doesn't look like it will work well with UNICODE.
(with-open-file (s pathname :element-type '(unsigned-byte 8))
(when (and (= (read-little-endian s) *link-initial-dword*)
(let ((header (make-array (length *link-guid*))))
(read-sequence header s)
(equalp header *link-guid*)))
(let ((flags (read-little-endian s)))
(file-position s 76) ;skip rest of header
(when (logbitp 0 flags)
;; skip shell item id list
(let ((length (read-little-endian s 2)))
(file-position s (+ length (file-position s)))))
((logbitp 1 flags)
(parse-file-location-info s))
(when (logbitp 2 flags)
;; skip description string
(let ((length (read-little-endian s 2)))
(file-position s (+ length (file-position s)))))
(when (logbitp 3 flags)
;; finally, our pathname
(let* ((length (read-little-endian s 2))
(buffer (make-array length)))
(read-sequence buffer s)
(map 'string #'code-char buffer)))))))
(end-of-file (c)
(declare (ignore c))
;;;; -------------------------------------------------------------------------
;;;; Portability layer around Common Lisp pathnames
;; This layer allows for portable manipulation of pathname objects themselves,
;; which all is necessary prior to any access the filesystem or environment.
(uiop/package:define-package :uiop/pathname
(:nicknames :asdf/pathname)
(:recycle :uiop/pathname :asdf/pathname :asdf)
(:use :uiop/common-lisp :uiop/package :uiop/utility :uiop/os)
;; Making and merging pathnames, portably
#:normalize-pathname-directory-component #:denormalize-pathname-directory-component
#:merge-pathname-directory-components #:*unspecific-pathname-type* #:make-pathname*
#:make-pathname-component-logical #:make-pathname-logical
#:nil-pathname #:*nil-pathname* #:with-pathname-defaults
;; Predicates
#:pathname-equal #:logical-pathname-p #:physical-pathname-p #:physicalize-pathname
#:absolute-pathname-p #:relative-pathname-p #:hidden-pathname-p #:file-pathname-p
;; Directories
#:pathname-directory-pathname #:pathname-parent-directory-pathname
#:directory-pathname-p #:ensure-directory-pathname
;; Parsing filenames
#:split-name-type #:parse-unix-namestring #:unix-namestring
;; Absolute and relative pathnames
#:subpathname #:subpathname*
#:pathname-root #:pathname-host-pathname
#:subpathp #:enough-pathname #:with-enough-pathname #:call-with-enough-pathname
;; Checking constraints
#:ensure-pathname ;; implemented in filesystem.lisp to accommodate for existence constraints
;; Wildcard pathnames
#:*wild* #:*wild-file* #:*wild-directory* #:*wild-inferiors* #:*wild-path* #:wilden
;; Translate a pathname
#:relativize-directory-component #:relativize-pathname-directory
#:directory-separator-for-host #:directorize-pathname-host-device
(in-package :uiop/pathname)
;;; Normalizing pathnames across implementations
(with-upgradability ()
(defun normalize-pathname-directory-component (directory)
"Convert the DIRECTORY component from a format usable by the underlying
implementation's MAKE-PATHNAME and other primitives to a CLHS-standard format
that is a list and not a string."
#-(or cmu sbcl scl) ;; these implementations already normalize directory components.
((stringp directory) `(:absolute ,directory))
((or (null directory)
(and (consp directory) (member (first directory) '(:absolute :relative))))
((consp directory)
(cons :relative directory))
(error (compatfmt "~@<Unrecognized pathname directory component ~S~@:>") directory))))
(defun denormalize-pathname-directory-component (directory-component)
"Convert the DIRECTORY-COMPONENT from a CLHS-standard format to a format usable
by the underlying implementation's MAKE-PATHNAME and other primitives"
(defun merge-pathname-directory-components (specified defaults)
"Helper for MERGE-PATHNAMES* that handles directory components"
(let ((directory (normalize-pathname-directory-component specified)))
(ecase (first directory)
((nil) defaults)
(:absolute specified)
(let ((defdir (normalize-pathname-directory-component defaults))
(reldir (cdr directory)))
((null defdir)
((not (eq :back (first reldir)))
(append defdir reldir))
(loop :with defabs = (first defdir)
:with defrev = (reverse (rest defdir))
:while (and (eq :back (car reldir))
(or (and (eq :absolute defabs) (null defrev))
(stringp (car defrev))))
:do (pop reldir) (pop defrev)
:finally (return (cons defabs (append (reverse defrev) reldir)))))))))))
;; Giving :unspecific as :type argument to make-pathname is not portable.
;; See CLHS make-pathname and
;; This will be :unspecific if supported, or NIL if not.
(defparameter *unspecific-pathname-type*
#+(or abcl allegro clozure cmu genera lispworks sbcl scl) :unspecific
#+(or clisp ecl mkcl gcl xcl #|These haven't been tested:|# cormanlisp mcl) nil
"Unspecific type component to use with the underlying implementation's MAKE-PATHNAME")
(defun make-pathname* (&rest keys &key (directory nil)
host (device () #+allegro devicep) name type version defaults
#+scl &allow-other-keys)
"Takes arguments like CL:MAKE-PATHNAME in the CLHS, and
tries hard to make a pathname that will actually behave as documented,
despite the peculiarities of each implementation"
;; TODO: reimplement defaulting for MCL, whereby an explicit NIL should override the defaults.
(declare (ignorable host device directory name type version defaults))
(apply 'make-pathname
#+allegro (when (and devicep (null device)) `(:device :unspecific))
(defun make-pathname-component-logical (x)
"Make a pathname component suitable for use in a logical-pathname"
(typecase x
((eql :unspecific) nil)
#+clisp (string (string-upcase x))
#+clisp (cons (mapcar 'make-pathname-component-logical x))
(t x)))
(defun make-pathname-logical (pathname host)
"Take a PATHNAME's directory, name, type and version components,
and make a new pathname with corresponding components and specified logical HOST"
:host host
:directory (make-pathname-component-logical (pathname-directory pathname))
:name (make-pathname-component-logical (pathname-name pathname))
:type (make-pathname-component-logical (pathname-type pathname))
:version (make-pathname-component-logical (pathname-version pathname))))
(defun merge-pathnames* (specified &optional (defaults *default-pathname-defaults*))
if the SPECIFIED pathname does not have an absolute directory,
then the HOST and DEVICE both come from the DEFAULTS, whereas
if the SPECIFIED pathname does have an absolute directory,
then the HOST and DEVICE both come from the SPECIFIED pathname.
This is what users want on a modern Unix or Windows operating system,
unlike the MERGE-PATHNAMES behavior.
Also, if either argument is NIL, then the other argument is returned unmodified;
this is unlike MERGE-PATHNAMES which always merges with a pathname,
by default *DEFAULT-PATHNAME-DEFAULTS*, which cannot be NIL."
(when (null specified) (return-from merge-pathnames* defaults))
(when (null defaults) (return-from merge-pathnames* specified))
(ext:resolve-pathname specified defaults)
(let* ((specified (pathname specified))
(defaults (pathname defaults))
(directory (normalize-pathname-directory-component (pathname-directory specified)))
(name (or (pathname-name specified) (pathname-name defaults)))
(type (or (pathname-type specified) (pathname-type defaults)))
(version (or (pathname-version specified) (pathname-version defaults))))
(labels ((unspecific-handler (p)
(if (typep p 'logical-pathname) #'make-pathname-component-logical #'identity)))
(multiple-value-bind (host device directory unspecific-handler)
(ecase (first directory)
(values (pathname-host specified)
(pathname-device specified)
(unspecific-handler specified)))
((nil :relative)
(values (pathname-host defaults)
(pathname-device defaults)
(merge-pathname-directory-components directory (pathname-directory defaults))
(unspecific-handler defaults))))
(make-pathname* :host host :device device :directory directory
:name (funcall unspecific-handler name)
:type (funcall unspecific-handler type)
:version (funcall unspecific-handler version))))))
(defun logical-pathname-p (x)
"is X a logical-pathname?"
(typep x 'logical-pathname))
(defun physical-pathname-p (x)
"is X a pathname that is not a logical-pathname?"
(and (pathnamep x) (not (logical-pathname-p x))))
(defun physicalize-pathname (x)
"if X is a logical pathname, use translate-logical-pathname on it."
;; Ought to be the same as translate-logical-pathname, except the latter borks on CLISP
(let ((p (when x (pathname x))))
(if (logical-pathname-p p) (translate-logical-pathname p) p)))
(defun nil-pathname (&optional (defaults *default-pathname-defaults*))
"A pathname that is as neutral as possible for use as defaults
when merging, making or parsing pathnames"
;; says a NIL host can mean a default host;
;; see also "valid physical pathname host" in the CLHS glossary, that suggests
;; strings and lists of strings or :unspecific
;; But CMUCL decides to die on NIL.
;; MCL has issues with make-pathname, nil and defaulting
(declare (ignorable defaults))
#.`(make-pathname* :directory nil :name nil :type nil :version nil
:device (or #+(and mkcl unix) :unspecific)
:host (or #+cmu lisp::*unix-host* #+(and mkcl unix) "localhost")
#+scl ,@'(:scheme nil :scheme-specific-part nil
:username nil :password nil :parameters nil :query nil :fragment nil)
;; the default shouldn't matter, but we really want something physical
#-mcl ,@'(:defaults defaults)))
(defvar *nil-pathname* (nil-pathname (physicalize-pathname (user-homedir-pathname)))
"A pathname that is as neutral as possible for use as defaults
when merging, making or parsing pathnames")
(defmacro with-pathname-defaults ((&optional defaults) &body body)
"Execute BODY in a context where the *DEFAULT-PATHNAME-DEFAULTS* are as neutral as possible
when merging, making or parsing pathnames"
`(let ((*default-pathname-defaults* ,(or defaults '*nil-pathname*))) ,@body)))
;;; Some pathname predicates
(with-upgradability ()
(defun pathname-equal (p1 p2)
"Are the two pathnames P1 and P2 reasonably equal in the paths they denote?"
(when (stringp p1) (setf p1 (pathname p1)))
(when (stringp p2) (setf p2 (pathname p2)))
(flet ((normalize-component (x)
(unless (member x '(nil :unspecific :newest (:relative)) :test 'equal)
(macrolet ((=? (&rest accessors)
(flet ((frob (x)
(reduce 'list (cons 'normalize-component accessors)
:initial-value x :from-end t)))
`(equal ,(frob 'p1) ,(frob 'p2)))))
(or (and (null p1) (null p2))
(and (pathnamep p1) (pathnamep p2)
(and (=? pathname-host)
#-(and mkcl unix) (=? pathname-device)
(=? normalize-pathname-directory-component pathname-directory)
(=? pathname-name)
(=? pathname-type)
#-mkcl (=? pathname-version)))))))
(defun absolute-pathname-p (pathspec)
"If PATHSPEC is a pathname or namestring object that parses as a pathname
possessing an :ABSOLUTE directory component, return the (parsed) pathname.
Otherwise return NIL"
(and pathspec
(typep pathspec '(or null pathname string))
(let ((pathname (pathname pathspec)))
(and (eq :absolute (car (normalize-pathname-directory-component
(pathname-directory pathname))))
(defun relative-pathname-p (pathspec)
"If PATHSPEC is a pathname or namestring object that parses as a pathname
possessing a :RELATIVE or NIL directory component, return the (parsed) pathname.
Otherwise return NIL"
(and pathspec
(typep pathspec '(or null pathname string))
(let* ((pathname (pathname pathspec))
(directory (normalize-pathname-directory-component
(pathname-directory pathname))))
(when (or (null directory) (eq :relative (car directory)))
(defun hidden-pathname-p (pathname)
"Return a boolean that is true if the pathname is hidden as per Unix style,
i.e. its name starts with a dot."
(and pathname (equal (first-char (pathname-name pathname)) #\.)))
(defun file-pathname-p (pathname)
"Does PATHNAME represent a file, i.e. has a non-null NAME component?
Accepts NIL, a string (converted through PARSE-NAMESTRING) or a PATHNAME.
Note that this does _not_ check to see that PATHNAME points to an
actually-existing file.
Returns the (parsed) PATHNAME when true"
(when pathname
(let* ((pathname (pathname pathname))
(name (pathname-name pathname)))
(when (not (member name '(nil :unspecific "") :test 'equal))
;;; Directory pathnames
(with-upgradability ()
(defun pathname-directory-pathname (pathname)
"Returns a new pathname with same HOST, DEVICE, DIRECTORY as PATHNAME,
and NIL NAME, TYPE and VERSION components"
(when pathname
(make-pathname :name nil :type nil :version nil :defaults pathname)))
(defun pathname-parent-directory-pathname (pathname)
"Returns a new pathname that corresponds to the parent of the current pathname's directory,
i.e. removing one level of depth in the DIRECTORY component. e.g. if pathname is
Unix pathname /foo/bar/baz/file.type then return /foo/bar/"
(when pathname
(make-pathname* :name nil :type nil :version nil
:directory (merge-pathname-directory-components
'(:relative :back) (pathname-directory pathname))
:defaults pathname)))
(defun directory-pathname-p (pathname)
"Does PATHNAME represent a directory?
A directory-pathname is a pathname _without_ a filename. The three
ways that the filename components can be missing are for it to be NIL,
:UNSPECIFIC or the empty string.
Note that this does _not_ check to see that PATHNAME points to an
actually-existing directory."
(when pathname
;; I tried using Allegro's excl:file-directory-p, but this cannot be done,
;; because it rejects apparently legal pathnames as
;; ill-formed. [2014/02/10:rpg]
(let ((pathname (pathname pathname)))
(flet ((check-one (x)
(member x '(nil :unspecific) :test 'equal)))
(and (not (wild-pathname-p pathname))
(check-one (pathname-name pathname))
(check-one (pathname-type pathname))
(defun ensure-directory-pathname (pathspec &optional (on-error 'error))
"Converts the non-wild pathname designator PATHSPEC to directory form."
((stringp pathspec)
(ensure-directory-pathname (pathname pathspec)))
((not (pathnamep pathspec))
(call-function on-error (compatfmt "~@<Invalid pathname designator ~S~@:>") pathspec))
((wild-pathname-p pathspec)
(call-function on-error (compatfmt "~@<Can't reliably convert wild pathname ~3i~_~S~@:>") pathspec))
((directory-pathname-p pathspec)
(make-pathname* :directory (append (or (normalize-pathname-directory-component
(pathname-directory pathspec))
(list :relative))
(list (file-namestring pathspec)))
:name nil :type nil :version nil :defaults pathspec)))))
;;; Parsing filenames
(with-upgradability ()
(defun split-unix-namestring-directory-components
(unix-namestring &key ensure-directory dot-dot)
"Splits the path string UNIX-NAMESTRING, returning four values:
A flag that is either :absolute or :relative, indicating
how the rest of the values are to be interpreted.
A directory path --- a list of strings and keywords, suitable for
use with MAKE-PATHNAME when prepended with the flag value.
Directory components with an empty name or the name . are removed.
Any directory named .. is read as DOT-DOT, or :BACK if it's NIL (not :UP).
A last-component, either a file-namestring including type extension,
or NIL in the case of a directory pathname.
A flag that is true iff the unix-style-pathname was just
a file-namestring without / path specification.
ENSURE-DIRECTORY forces the namestring to be interpreted as a directory pathname:
the third return value will be NIL, and final component of the namestring
will be treated as part of the directory path.
An empty string is thus read as meaning a pathname object with all fields nil.
Note that : characters will NOT be interpreted as host specification.
Absolute pathnames are only appropriate on Unix-style systems.
The intention of this function is to support structured component names,
e.g., \(:file \"foo/bar\"\), which will be unpacked to relative pathnames."
(check-type unix-namestring string)
(check-type dot-dot (member nil :back :up))
(if (and (not (find #\/ unix-namestring)) (not ensure-directory)
(plusp (length unix-namestring)))
(values :relative () unix-namestring t)
(let* ((components (split-string unix-namestring :separator "/"))
(last-comp (car (last components))))
(multiple-value-bind (relative components)
(if (equal (first components) "")
(if (equal (first-char unix-namestring) #\/)
(values :absolute (cdr components))
(values :relative nil))
(values :relative components))
(setf components (remove-if #'(lambda (x) (member x '("" ".") :test #'equal))
(setf components (substitute (or dot-dot :back) ".." components :test #'equal))
((equal last-comp "")
(values relative components nil nil)) ; "" already removed from components
(values relative components nil nil))
(values relative (butlast components) last-comp nil)))))))
(defun split-name-type (filename)
"Split a filename into two values NAME and TYPE that are returned.
We assume filename has no directory component.
The last . if any separates name and type from from type,
except that if there is only one . and it is in first position,
the whole filename is the NAME with an empty type.
NAME is always a string.
For an empty type, *UNSPECIFIC-PATHNAME-TYPE* is returned."
(check-type filename string)
(assert (plusp (length filename)))
(destructuring-bind (name &optional (type *unspecific-pathname-type*))
(split-string filename :max 2 :separator ".")
(if (equal name "")
(values filename *unspecific-pathname-type*)
(values name type))))
(defun parse-unix-namestring (name &rest keys &key type defaults dot-dot ensure-directory
"Coerce NAME into a PATHNAME using standard Unix syntax.
Unix syntax is used whether or not the underlying system is Unix;
on such non-Unix systems it is only usable but for relative pathnames;
but especially to manipulate relative pathnames portably, it is of crucial
to possess a portable pathname syntax independent of the underlying OS.
This is what PARSE-UNIX-NAMESTRING provides, and why we use it in ASDF.
When given a PATHNAME object, just return it untouched.
When given NIL, just return NIL.
When given a non-null SYMBOL, first downcase its name and treat it as a string.
When given a STRING, portably decompose it into a pathname as below.
#\\/ separates directory components.
The last #\\/-separated substring is interpreted as follows:
the string is made the last directory component, and NAME and TYPE are NIL.
if the string is empty, it's the empty pathname with all slots NIL.
2- If TYPE is NIL, the substring is a file-namestring, and its NAME and TYPE
are separated by SPLIT-NAME-TYPE.
3- If TYPE is a string, it is the given TYPE, and the whole string is the NAME.
Directory components with an empty name the name . are removed.
Any directory named .. is read as DOT-DOT,
which must be one of :BACK or :UP and defaults to :BACK.
HOST, DEVICE and VERSION components are taken from DEFAULTS,
which itself defaults to *NIL-PATHNAME*, also used if DEFAULTS is NIL.
No host or device can be specified in the string itself,
which makes it unsuitable for absolute pathnames outside Unix.
For relative pathnames, these components (and hence the defaults) won't matter
if you use MERGE-PATHNAMES* but will matter if you use MERGE-PATHNAMES,
which is an important reason to always use MERGE-PATHNAMES*.
Arbitrary keys are accepted, and the parse result is passed to ENSURE-PATHNAME
with those keys, removing TYPE DEFAULTS and DOT-DOT.
When you're manipulating pathnames that are supposed to make sense portably
even though the OS may not be Unixish, we recommend you use :WANT-RELATIVE T
to throw an error if the pathname is absolute"
(block nil
(check-type type (or null string (eql :directory)))
(when ensure-directory
(setf type :directory))
(etypecase name
((or null pathname) (return name))
(setf name (string-downcase name)))
(multiple-value-bind (relative path filename file-only)
name :dot-dot dot-dot :ensure-directory (eq type :directory))
(multiple-value-bind (name type)
((or (eq type :directory) (null filename))
(values nil nil))
(values filename type))
(split-name-type filename)))
(apply 'ensure-pathname
:directory (unless file-only (cons relative path))
:name name :type type
:defaults (or #-mcl defaults *nil-pathname*))
(remove-plist-keys '(:type :dot-dot :defaults) keys))))))
(defun unix-namestring (pathname)
"Given a non-wild PATHNAME, return a Unix-style namestring for it.
If the PATHNAME is NIL or a STRING, return it unchanged.
This only considers the DIRECTORY, NAME and TYPE components of the pathname.
This is a portable solution for representing relative pathnames,
But unless you are running on a Unix system, it is not a general solution
to representing native pathnames.
An error is signaled if the argument is not NULL, a STRING or a PATHNAME,
or if it is a PATHNAME but some of its components are not recognized."
(etypecase pathname
((or null string) pathname)
(with-output-to-string (s)
(flet ((err () #+lispworks (describe pathname) (error "Not a valid unix-namestring ~S" pathname)))
(let* ((dir (normalize-pathname-directory-component (pathname-directory pathname)))
(name (pathname-name pathname))
(name (and (not (eq name :unspecific)) name))
(type (pathname-type pathname))
(type (and (not (eq type :unspecific)) type)))
((member dir '(nil :unspecific)))
((eq dir '(:relative)) (princ "./" s))
((consp dir)
(destructuring-bind (relabs &rest dirs) dir
(or (member relabs '(:relative :absolute)) (err))
(when (eq relabs :absolute) (princ #\/ s))
(loop :for x :in dirs :do
((member x '(:back :up)) (princ "../" s))
((equal x "") (err))
;;((member x '("." "..") :test 'equal) (err))
((stringp x) (format s "~A/" x))
(t (err))))))
(t (err)))
(unless (and (stringp name) (or (null type) (stringp type))) (err))
(format s "~A~@[.~A~]" name type))
(or (null type) (err)))))))))))
;;; Absolute and relative pathnames
(with-upgradability ()
(defun subpathname (pathname subpath &key type)
"This function takes a PATHNAME and a SUBPATH and a TYPE.
If SUBPATH is already a PATHNAME object (not namestring),
and is an absolute pathname at that, it is returned unchanged;
otherwise, SUBPATH is turned into a relative pathname with given TYPE
then it is merged with the PATHNAME-DIRECTORY-PATHNAME of PATHNAME."
(or (and (pathnamep subpath) (absolute-pathname-p subpath))
(merge-pathnames* (parse-unix-namestring subpath :type type :want-relative t)
(pathname-directory-pathname pathname))))
(defun subpathname* (pathname subpath &key type)
"returns NIL if the base pathname is NIL, otherwise like SUBPATHNAME."
(and pathname
(subpathname (ensure-directory-pathname pathname) subpath :type type)))
(defun pathname-root (pathname)
"return the root directory for the host and device of given PATHNAME"
(make-pathname* :directory '(:absolute)
:name nil :type nil :version nil
:defaults pathname ;; host device, and on scl, *some*
;; scheme-specific parts: port username password, not others:
. #.(or #+scl '(:parameters nil :query nil :fragment nil))))
(defun pathname-host-pathname (pathname)
"return a pathname with the same host as given PATHNAME, and all other fields NIL"
(make-pathname* :directory nil
:name nil :type nil :version nil :device nil
:defaults pathname ;; host device, and on scl, *some*
;; scheme-specific parts: port username password, not others:
. #.(or #+scl '(:parameters nil :query nil :fragment nil))))
(defun ensure-absolute-pathname (path &optional defaults (on-error 'error))
"Given a pathname designator PATH, return an absolute pathname as specified by PATH
considering the DEFAULTS, or, if not possible, use CALL-FUNCTION on the specified ON-ERROR behavior,
with a format control-string and other arguments as arguments"
((absolute-pathname-p path))
((stringp path) (ensure-absolute-pathname (pathname path) defaults on-error))
((not (pathnamep path)) (call-function on-error "not a valid pathname designator ~S" path))
((let ((default-pathname (if (pathnamep defaults) defaults (call-function defaults))))
(or (if (absolute-pathname-p default-pathname)
(absolute-pathname-p (merge-pathnames* path default-pathname))
(call-function on-error "Default pathname ~S is not an absolute pathname"
(call-function on-error "Failed to merge ~S with ~S into an absolute pathname"
path default-pathname))))
(t (call-function on-error
"Cannot ensure ~S is evaluated as an absolute pathname with defaults ~S"
path defaults))))
(defun subpathp (maybe-subpath base-pathname)
"if MAYBE-SUBPATH is a pathname that is under BASE-PATHNAME, return a pathname object that
when used with MERGE-PATHNAMES* with defaults BASE-PATHNAME, returns MAYBE-SUBPATH."
(and (pathnamep maybe-subpath) (pathnamep base-pathname)
(absolute-pathname-p maybe-subpath) (absolute-pathname-p base-pathname)
(directory-pathname-p base-pathname) (not (wild-pathname-p base-pathname))
(pathname-equal (pathname-root maybe-subpath) (pathname-root base-pathname))
(with-pathname-defaults ()
(let ((enough (enough-namestring maybe-subpath base-pathname)))
(and (relative-pathname-p enough) (pathname enough))))))
(defun enough-pathname (maybe-subpath base-pathname)
"if MAYBE-SUBPATH is a pathname that is under BASE-PATHNAME, return a pathname object that
when used with MERGE-PATHNAMES* with defaults BASE-PATHNAME, returns MAYBE-SUBPATH."
(let ((sub (when maybe-subpath (pathname maybe-subpath)))
(base (when base-pathname (ensure-absolute-pathname (pathname base-pathname)))))
(or (and base (subpathp sub base)) sub)))
(defun call-with-enough-pathname (maybe-subpath defaults-pathname thunk)
"In a context where *DEFAULT-PATHNAME-DEFAULTS* is bound to DEFAULTS-PATHNAME (if not null,
or else to its current value), call THUNK with ENOUGH-PATHNAME for MAYBE-SUBPATH
given DEFAULTS-PATHNAME as a base pathname."
(let ((enough (enough-pathname maybe-subpath defaults-pathname))
(*default-pathname-defaults* (or defaults-pathname *default-pathname-defaults*)))
(funcall thunk enough)))
(defmacro with-enough-pathname ((pathname-var &key (pathname pathname-var)
(defaults *default-pathname-defaults*))
&body body)
"Shorthand syntax for CALL-WITH-ENOUGH-PATHNAME"
`(call-with-enough-pathname ,pathname ,defaults #'(lambda (,pathname-var) ,@body))))
;;; Wildcard pathnames
(with-upgradability ()
(defparameter *wild* (or #+cormanlisp "*" :wild)
"Wild component for use with MAKE-PATHNAME")
(defparameter *wild-directory-component* (or :wild)
"Wild directory component for use with MAKE-PATHNAME")
(defparameter *wild-inferiors-component* (or :wild-inferiors)
"Wild-inferiors directory component for use with MAKE-PATHNAME")
(defparameter *wild-file*
(make-pathname :directory nil :name *wild* :type *wild*
:version (or #-(or allegro abcl xcl) *wild*))
"A pathname object with wildcards for matching any file in a given directory")
(defparameter *wild-directory*
(make-pathname* :directory `(:relative ,*wild-directory-component*)
:name nil :type nil :version nil)
"A pathname object with wildcards for matching any subdirectory")
(defparameter *wild-inferiors*
(make-pathname* :directory `(:relative ,*wild-inferiors-component*)
:name nil :type nil :version nil)
"A pathname object with wildcards for matching any recursive subdirectory")
(defparameter *wild-path*
(merge-pathnames* *wild-file* *wild-inferiors*)
"A pathname object with wildcards for matching any file in any recursive subdirectory")
(defun wilden (path)
"From a pathname, return a wildcard pathname matching any file in any subdirectory of given pathname's directory"
(merge-pathnames* *wild-path* path)))
;;; Translate a pathname
(with-upgradability ()
(defun relativize-directory-component (directory-component)
"Given the DIRECTORY-COMPONENT of a pathname, return an otherwise similar relative directory component"
(let ((directory (normalize-pathname-directory-component directory-component)))
((stringp directory)
(list :relative directory))
((eq (car directory) :absolute)
(cons :relative (cdr directory)))
(defun relativize-pathname-directory (pathspec)
"Given a PATHNAME, return a relative pathname with otherwise the same components"
(let ((p (pathname pathspec)))
:directory (relativize-directory-component (pathname-directory p))
:defaults p)))
(defun directory-separator-for-host (&optional (pathname *default-pathname-defaults*))
"Given a PATHNAME, return the character used to delimit directory names on this host and device."
(let ((foo (make-pathname* :directory '(:absolute "FOO") :defaults pathname)))
(last-char (namestring foo))))
(defun directorize-pathname-host-device (pathname)
"Given a PATHNAME, return a pathname that has representations of its HOST and DEVICE components
added to its DIRECTORY component. This is useful for output translations."
#+(or unix abcl)
(when (and #+abcl (os-unix-p) (physical-pathname-p pathname))
(return-from directorize-pathname-host-device pathname))
(let* ((root (pathname-root pathname))
(wild-root (wilden root))
(absolute-pathname (merge-pathnames* pathname root))
(separator (directory-separator-for-host root))
(root-namestring (namestring root))
(substitute-if #\/
#'(lambda (x) (or (eql x #\:)
(eql x separator)))
(multiple-value-bind (relative path filename)
(split-unix-namestring-directory-components root-string :ensure-directory t)
(declare (ignore relative filename))
(let ((new-base
(make-pathname* :defaults root :directory `(:absolute ,@path))))
(translate-pathname absolute-pathname wild-root (wilden new-base))))))
(defun directorize-pathname-host-device (pathname)
(let ((scheme (ext:pathname-scheme pathname))
(host (pathname-host pathname))
(port (ext:pathname-port pathname))
(directory (pathname-directory pathname)))
(flet ((specificp (x) (and x (not (eq x :unspecific)))))
(if (or (specificp port)
(and (specificp host) (plusp (length host)))
(specificp scheme))
(let ((prefix ""))
(when (specificp port)
(setf prefix (format nil ":~D" port)))
(when (and (specificp host) (plusp (length host)))
(setf prefix (strcat host prefix)))
(setf prefix (strcat ":" prefix))
(when (specificp scheme)
(setf prefix (strcat scheme prefix)))
(assert (and directory (eq (first directory) :absolute)))
(make-pathname* :directory `(:absolute ,prefix ,@(rest directory))
:defaults pathname)))
(defun* (translate-pathname*) (path absolute-source destination &optional root source)
"A wrapper around TRANSLATE-PATHNAME to be used by the ASDF output-translations facility.
PATH is the pathname to be translated.
ABSOLUTE-SOURCE is an absolute pathname to use as source for translate-pathname,
DESTINATION is either a function, to be called with PATH and ABSOLUTE-SOURCE,
or a relative pathname, to be merged with ROOT and used as destination for translate-pathname
or an absolute pathname, to be used as destination for translate-pathname.
In that last case, if ROOT is non-NIL, PATH is first transformated by DIRECTORIZE-PATHNAME-HOST-DEVICE."
(declare (ignore source))
((functionp destination)
(funcall destination path absolute-source))
((eq destination t)
((not (pathnamep destination))
(error "Invalid destination"))
((not (absolute-pathname-p destination))
(translate-pathname path absolute-source (merge-pathnames* destination root)))
(translate-pathname (directorize-pathname-host-device path) absolute-source destination))
(translate-pathname path absolute-source destination))))
(defvar *output-translation-function* 'identity
"Hook for output translations.
This function needs to be idempotent, so that actions can work
whether their inputs were translated or not,
which they will be if we are composing operations. e.g. if some
create-lisp-op creates a lisp file from some higher-level input,
you need to still be able to use compile-op on that lisp file."))
;;;; -------------------------------------------------------------------------
;;;; Portability layer around Common Lisp filesystem access
(uiop/package:define-package :uiop/filesystem
(:nicknames :asdf/filesystem)
(:recycle :uiop/filesystem :asdf/pathname :asdf)
(:use :uiop/common-lisp :uiop/package :uiop/utility :uiop/os :uiop/pathname)
;; Native namestrings
#:native-namestring #:parse-native-namestring
;; Probing the filesystem
#:truename* #:safe-file-write-date #:probe-file* #:directory-exists-p #:file-exists-p
#:directory* #:filter-logical-directory-results #:directory-files #:subdirectories
;; Resolving symlinks somewhat
#:truenamize #:resolve-symlinks #:*resolve-symlinks* #:resolve-symlinks*
;; merging with cwd
#:get-pathname-defaults #:call-with-current-directory #:with-current-directory
;; Environment pathnames
#:inter-directory-separator #:split-native-pathnames-string
#:getenv-pathname #:getenv-pathnames
#:getenv-absolute-directory #:getenv-absolute-directories
#:lisp-implementation-directory #:lisp-implementation-pathname-p
;; Simple filesystem operations
#:delete-file-if-exists #:delete-empty-directory #:delete-directory-tree))
(in-package :uiop/filesystem)
;;; Native namestrings, as seen by the operating system calls rather than Lisp
(with-upgradability ()
(defun native-namestring (x)
"From a non-wildcard CL pathname, a return namestring suitable for passing to the operating system"
(when x
(let ((p (pathname x)))
#+clozure (with-pathname-defaults () (ccl:native-translated-namestring p)) ; see ccl bug 978
#+(or cmu scl) (ext:unix-namestring p nil)
#+sbcl (sb-ext:native-namestring p)
#-(or clozure cmu sbcl scl)
(if (os-unix-p) (unix-namestring p)
(namestring p)))))
(defun parse-native-namestring (string &rest constraints &key ensure-directory &allow-other-keys)
"From a native namestring suitable for use by the operating system, return
a CL pathname satisfying all the specified constraints as per ENSURE-PATHNAME"
(check-type string (or string null))
(let* ((pathname
(when string
(with-pathname-defaults ()
#+clozure (ccl:native-to-pathname string)
#+sbcl (sb-ext:parse-native-namestring string)
#-(or clozure sbcl)
(if (os-unix-p)
(parse-unix-namestring string :ensure-directory ensure-directory)
(parse-namestring string)))))
(if ensure-directory
(and pathname (ensure-directory-pathname pathname))
(apply 'ensure-pathname pathname constraints))))
;;; Probing the filesystem
(with-upgradability ()
(defun truename* (p)
"Nicer variant of TRUENAME that plays well with NIL and avoids logical pathname contexts"
;; avoids both logical-pathname merging and physical resolution issues
(and p (handler-case (with-pathname-defaults () (truename p)) (file-error () nil))))
(defun safe-file-write-date (pathname)
"Safe variant of FILE-WRITE-DATE that may return NIL rather than raise an error."
;; If FILE-WRITE-DATE returns NIL, it's possible that
;; the user or some other agent has deleted an input file.
;; Also, generated files will not exist at the time planning is done
;; and calls compute-action-stamp which calls safe-file-write-date.
;; So it is very possible that we can't get a valid file-write-date,
;; and we can survive and we will continue the planning
;; as if the file were very old.
;; (or should we treat the case in a different, special way?)
(and pathname
(handler-case (file-write-date (physicalize-pathname pathname))
(file-error () nil))))
(defun probe-file* (p &key truename)
"when given a pathname P (designated by a string as per PARSE-NAMESTRING),
probes the filesystem for a file or directory with given pathname.
If it exists, return its truename is ENSURE-PATHNAME is true,
or the original (parsed) pathname if it is false (the default)."
(with-pathname-defaults () ;; avoids logical-pathname issues on some implementations
(etypecase p
(null nil)
(string (probe-file* (parse-namestring p) :truename truename))
(and (not (wild-pathname-p p))
(probe-file p :follow-symlinks truename)
(if truename
(truename* p)
(let ((kind (car (si::stat p))))
(when (eq kind :link)
(setf kind (ignore-errors (car (si::stat (truename* p))))))
(ecase kind
((nil) nil)
((:file :link)
((file-pathname-p p) p)
((directory-pathname-p p)
(subpathname p (car (last (pathname-directory p)))))))
(:directory (ensure-directory-pathname p)))))
#.(flet ((probe (probe)
`(let ((foundtrue ,probe))
(truename foundtrue)
(foundtrue p)))))
(let* ((fs (or #-os-windows (find-symbol* '#:file-stat :posix nil)))
(pp (find-symbol* '#:probe-pathname :ext nil))
(resolve (if pp
`(ignore-errors (,pp p))
'(or (truename* p)
(truename* (ignore-errors (ensure-directory-pathname p)))))))
(if fs
`(if truename
(and (ignore-errors (,fs p)) p))
(probe resolve))))
#-(or allegro clisp gcl)
(if truename
(probe-file p)
(let ((pp (physicalize-pathname p)))
#+(or cmu scl) (unix:unix-stat (ext:unix-namestring pp))
#+(and lispworks unix) (system:get-file-stat pp)
#+sbcl (sb-unix:unix-stat (sb-ext:native-namestring pp))
#-(or cmu (and lispworks unix) sbcl scl) (file-write-date pp)
(file-error () nil)))))))
(defun directory-exists-p (x)
"Is X the name of a directory that exists on the filesystem?"
(excl:probe-directory x)
(handler-case (ext:probe-directory x)
(sys::simple-file-error ()
#-(or allegro clisp)
(let ((p (probe-file* x :truename t)))
(and (directory-pathname-p p) p)))
(defun file-exists-p (x)
"Is X the name of a file that exists on the filesystem?"
(let ((p (probe-file* x :truename t)))
(and (file-pathname-p p) p)))
(defun directory* (pathname-spec &rest keys &key &allow-other-keys)
"Return a list of the entries in a directory by calling DIRECTORY.
Try to override the defaults to not resolving symlinks, if implementation allows."
(apply 'directory pathname-spec
(append keys '#.(or #+allegro '(:directories-are-files nil :follow-symbolic-links nil)
#+(or clozure digitool) '(:follow-links nil)
#+clisp '(:circle t :if-does-not-exist :ignore)
#+(or cmu scl) '(:follow-links nil :truenamep nil)
#+lispworks '(:link-transparency nil)
#+sbcl (when (find-symbol* :resolve-symlinks '#:sb-impl nil)
'(:resolve-symlinks nil))))))
(defun filter-logical-directory-results (directory entries merger)
"Given ENTRIES in a DIRECTORY, remove if the directory is logical
the entries which are physical yet when transformed by MERGER have a different TRUENAME.
This function is used as a helper to DIRECTORY-FILES to avoid invalid entries when using logical-pathnames."
(remove-duplicates ;; on CLISP, querying ~/ will return duplicates
(if (logical-pathname-p directory)
;; Try hard to not resolve logical-pathname into physical pathnames;
;; otherwise logical-pathname users/lovers will be disappointed.
;; If directory* could use some implementation-dependent magic,
;; we will have logical pathnames already; otherwise,
;; we only keep pathnames for which specifying the name and
;; translating the LPN commute.
(loop :for f :in entries
:for p = (or (and (logical-pathname-p f) f)
(let* ((u (ignore-errors (call-function merger f))))
;; The first u avoids a cumbersome (truename u) error.
;; At this point f should already be a truename,
;; but isn't quite in CLISP, for it doesn't have :version :newest
(and u (equal (truename* u) (truename* f)) u)))
:when p :collect p)
:test 'pathname-equal))
(defun directory-files (directory &optional (pattern *wild-file*))
"Return a list of the files in a directory according to the PATTERN.
Subdirectories should NOT be returned.
PATTERN defaults to a pattern carefully chosen based on the implementation;
override the default at your own risk.
DIRECTORY-FILES tries NOT to resolve symlinks if the implementation
permits this."
(let ((dir (pathname directory)))
(when (logical-pathname-p dir)
;; Because of the filtering we do below,
;; logical pathnames have restrictions on wild patterns.
;; Not that the results are very portable when you use these patterns on physical pathnames.
(when (wild-pathname-p dir)
(error "Invalid wild pattern in logical directory ~S" directory))
(unless (member (pathname-directory pattern) '(() (:relative)) :test 'equal)
(error "Invalid file pattern ~S for logical directory ~S" pattern directory))
(setf pattern (make-pathname-logical pattern (pathname-host dir))))
(let* ((pat (merge-pathnames* pattern dir))
(entries (append (ignore-errors (directory* pat))
#+(or clisp gcl)
(when (equal :wild (pathname-type pattern))
(ignore-errors (directory* (make-pathname :type nil :defaults pat)))))))
(remove-if 'directory-pathname-p
directory entries
#'(lambda (f)
(make-pathname :defaults dir
:name (make-pathname-component-logical (pathname-name f))
:type (make-pathname-component-logical (pathname-type f))
:version (make-pathname-component-logical (pathname-version f)))))))))
(defun subdirectories (directory)
"Given a DIRECTORY pathname designator, return a list of the subdirectories under it."
(let* ((directory (ensure-directory-pathname directory))
#-(or abcl cormanlisp genera xcl)
(wild (merge-pathnames*
#-(or abcl allegro cmu lispworks sbcl scl xcl)
#+(or abcl allegro cmu lispworks sbcl scl xcl) "*.*"
#-(or abcl cormanlisp genera xcl)
(directory* wild . #.(or #+clozure '(:directories t :files nil)
#+mcl '(:directories t))))
#+(or abcl xcl) (system:list-directory directory)
#+cormanlisp (cl::directory-subdirs directory)
#+genera (fs:directory-list directory))
#+(or abcl allegro cmu genera lispworks sbcl scl xcl)
(dirs (loop :for x :in dirs
:for d = #+(or abcl xcl) (extensions:probe-directory x)
#+allegro (excl:probe-directory x)
#+(or cmu sbcl scl) (directory-pathname-p x)
#+genera (getf (cdr x) :directory)
#+lispworks (lw:file-directory-p x)
:when d :collect #+(or abcl allegro xcl) d
#+genera (ensure-directory-pathname (first x))
#+(or cmu lispworks sbcl scl) x)))
directory dirs
(let ((prefix (or (normalize-pathname-directory-component (pathname-directory directory))
'(:absolute)))) ; because allegro returns NIL for #p"FOO:"
#'(lambda (d)
(let ((dir (normalize-pathname-directory-component (pathname-directory d))))
(and (consp dir) (consp (cdr dir))
:defaults directory :name nil :type nil :version nil
:directory (append prefix (make-pathname-component-logical (last dir)))))))))))
(defun collect-sub*directories (directory collectp recursep collector)
"Given a DIRECTORY, call-function the COLLECTOR function designator
on the directory if COLLECTP returns true when CALL-FUNCTION'ed with the directory,
and recurse each of its subdirectories on which the RECURSEP returns true when CALL-FUNCTION'ed with them."
(when (call-function collectp directory)
(call-function collector directory))
(dolist (subdir (subdirectories directory))
(when (call-function recursep subdir)
(collect-sub*directories subdir collectp recursep collector)))))
;;; Resolving symlinks somewhat
(with-upgradability ()
(defun truenamize (pathname)
"Resolve as much of a pathname as possible"
(block nil
(when (typep pathname '(or null logical-pathname)) (return pathname))
(let ((p pathname))
(unless (absolute-pathname-p p)
(setf p (or (absolute-pathname-p (ensure-absolute-pathname p 'get-pathname-defaults nil))
(return p))))
(when (logical-pathname-p p) (return p))
(let ((found (probe-file* p :truename t)))
(when found (return found)))
(let* ((directory (normalize-pathname-directory-component (pathname-directory p)))
(up-components (reverse (rest directory)))
(down-components ()))
(assert (eq :absolute (first directory)))
(loop :while up-components :do
(if-let (parent
(probe-file* (make-pathname* :directory `(:absolute ,@(reverse up-components))
:name nil :type nil :version nil :defaults p))))
(if-let (simplified
(make-pathname* :directory `(:relative ,@down-components)
:defaults p)
(ensure-directory-pathname parent))))
(return simplified)))
(push (pop up-components) down-components)
:finally (return p))))))
(defun resolve-symlinks (path)
"Do a best effort at resolving symlinks in PATH, returning a partially or totally resolved PATH."
#-allegro (truenamize path)
(if (physical-pathname-p path)
(or (ignore-errors (excl:pathname-resolve-symbolic-links path)) path)
(defvar *resolve-symlinks* t
"Determine whether or not ASDF resolves symlinks when defining systems.
Defaults to T.")
(defun resolve-symlinks* (path)
(if *resolve-symlinks*
(and path (resolve-symlinks path))
;;; Check pathname constraints
(with-upgradability ()
(defun ensure-pathname
(pathname &key
defaults type dot-dot namestring
want-logical want-physical ensure-physical
want-relative want-absolute ensure-absolute ensure-subpath
want-non-wild want-wild wilden
want-file want-directory ensure-directory
want-existing ensure-directories-exist
truename resolve-symlinks truenamize
&aux (p pathname)) ;; mutable working copy, preserve original
"Coerces its argument into a PATHNAME,
optionally doing some transformations and checking specified constraints.
If the argument is NIL, then NIL is returned unless the WANT-PATHNAME constraint is specified.
If the argument is a STRING, it is first converted to a pathname via
depending on the NAMESTRING argument being :UNIX, :LISP or :NATIVE respectively,
or else by using CALL-FUNCTION on the NAMESTRING argument;
if :UNIX is specified (or NIL, the default, which specifies the same thing),
then PARSE-UNIX-NAMESTRING it is called with the keywords
the result is optionally merged into the DEFAULTS if ENSURE-ABSOLUTE is true.
The pathname passed or resulting from parsing the string
is then subjected to all the checks and transformations below are run.
Each non-nil constraint argument can be one of the symbols T, ERROR, CERROR or IGNORE.
The boolean T is an alias for ERROR.
ERROR means that an error will be raised if the constraint is not satisfied.
CERROR means that an continuable error will be raised if the constraint is not satisfied.
IGNORE means just return NIL instead of the pathname.
The ON-ERROR argument, if not NIL, is a function designator (as per CALL-FUNCTION)
that will be called with the the following arguments:
a generic format string for ensure pathname, the pathname,
the keyword argument corresponding to the failed check or transformation,
a format string for the reason ENSURE-PATHNAME failed,
and a list with arguments to that format string.
If ON-ERROR is NIL, ERROR is used instead, which does the right thing.
The transformations and constraint checks are done in this order,
which is also the order in the lambda-list:
WANT-PATHNAME checks that pathname (after parsing if needed) is not null.
Otherwise, if the pathname is NIL, ensure-pathname returns NIL.
WANT-LOGICAL checks that pathname is a LOGICAL-PATHNAME
WANT-PHYSICAL checks that pathname is not a LOGICAL-PATHNAME
ENSURE-PHYSICAL ensures that pathname is physical via TRANSLATE-LOGICAL-PATHNAME
WANT-RELATIVE checks that pathname has a relative directory component
WANT-ABSOLUTE checks that pathname does have an absolute directory component
ENSURE-ABSOLUTE merges with the DEFAULTS, then checks again
that the result absolute is an absolute pathname indeed.
ENSURE-SUBPATH checks that the pathname is a subpath of the DEFAULTS.
WANT-FILE checks that pathname has a non-nil FILE component
WANT-DIRECTORY checks that pathname has nil FILE and TYPE components
any file and type components as being actually a last directory component.
WANT-NON-WILD checks that pathname is not a wild pathname
WANT-WILD checks that pathname is a wild pathname
WILDEN merges the pathname with **/*.*.* if it is not wild
WANT-EXISTING checks that a file (or directory) exists with that pathname.
TRUENAME replaces the pathname by its truename, or errors if not possible.
RESOLVE-SYMLINKS replaces the pathname by a variant with symlinks resolved by RESOLVE-SYMLINKS.
TRUENAMIZE uses TRUENAMIZE to resolve as many symlinks as possible."
(block nil
(flet ((report-error (keyword description &rest arguments)
(call-function (or on-error 'error)
"Invalid pathname ~S: ~*~?"
pathname keyword description arguments)))
(macrolet ((err (constraint &rest arguments)
`(report-error ',(intern* constraint :keyword) ,@arguments))
(check (constraint condition &rest arguments)
`(when ,constraint
(unless ,condition (err ,constraint ,@arguments))))
(transform (transform condition expr)
`(when ,transform
(,@(if condition `(when ,condition) '(progn))
(setf p ,expr)))))
(etypecase p
((or null pathname))
(setf p (case namestring
((:unix nil)
p :defaults defaults :type type :dot-dot dot-dot
:ensure-directory ensure-directory :want-relative want-relative))
(parse-native-namestring p))
(parse-namestring p))
(call-function namestring p))))))
(etypecase p
(check want-pathname (pathnamep p) "Expected a pathname, not NIL")
(return nil)))
(check want-logical (logical-pathname-p p) "Expected a logical pathname")
(check want-physical (physical-pathname-p p) "Expected a physical pathname")
(transform ensure-physical () (physicalize-pathname p))
(check ensure-physical (physical-pathname-p p) "Could not translate to a physical pathname")
(check want-relative (relative-pathname-p p) "Expected a relative pathname")
(check want-absolute (absolute-pathname-p p) "Expected an absolute pathname")
(transform ensure-absolute (not (absolute-pathname-p p))
(ensure-absolute-pathname p defaults (list #'report-error :ensure-absolute "~@?")))
(check ensure-absolute (absolute-pathname-p p)
"Could not make into an absolute pathname even after merging with ~S" defaults)
(check ensure-subpath (absolute-pathname-p defaults)
"cannot be checked to be a subpath of non-absolute pathname ~S" defaults)
(check ensure-subpath (subpathp p defaults) "is not a sub pathname of ~S" defaults)
(check want-file (file-pathname-p p) "Expected a file pathname")
(check want-directory (directory-pathname-p p) "Expected a directory pathname")
(transform ensure-directory (not (directory-pathname-p p)) (ensure-directory-pathname p))
(check want-non-wild (not (wild-pathname-p p)) "Expected a non-wildcard pathname")
(check want-wild (wild-pathname-p p) "Expected a wildcard pathname")
(transform wilden (not (wild-pathname-p p)) (wilden p))
(when want-existing
(let ((existing (probe-file* p :truename truename)))
(if existing
(when truename
(return existing))
(err want-existing "Expected an existing pathname"))))
(when ensure-directories-exist (ensure-directories-exist p))
(when truename
(let ((truename (truename* p)))
(if truename
(return truename)
(err truename "Can't get a truename for pathname"))))
(transform resolve-symlinks () (resolve-symlinks p))
(transform truenamize () (truenamize p))
;;; Pathname defaults
(with-upgradability ()
(defun get-pathname-defaults (&optional (defaults *default-pathname-defaults*))
"Find the actual DEFAULTS to use for pathnames, including
resolving them with respect to GETCWD if the DEFAULTS were relative"
(or (absolute-pathname-p defaults)
(merge-pathnames* defaults (getcwd))))
(defun call-with-current-directory (dir thunk)
"call the THUNK in a context where the current directory was changed to DIR, if not NIL.
Note that this operation is usually NOT thread-safe."
(if dir
(let* ((dir (resolve-symlinks* (get-pathname-defaults (pathname-directory-pathname dir))))
(cwd (getcwd))
(*default-pathname-defaults* dir))
(chdir dir)
(funcall thunk)
(chdir cwd)))
(funcall thunk)))
(defmacro with-current-directory ((&optional dir) &body body)
"Call BODY while the POSIX current working directory is set to DIR"
`(call-with-current-directory ,dir #'(lambda () ,@body))))
;;; Environment pathnames
(with-upgradability ()
(defun inter-directory-separator ()
"What character does the current OS conventionally uses to separate directories?"
(if (os-unix-p) #\: #\;))
(defun split-native-pathnames-string (string &rest constraints &key &allow-other-keys)
"Given a string of pathnames specified in native OS syntax, separate them in a list,
check constraints and normalize each one as per ENSURE-PATHNAME."
(loop :for namestring :in (split-string string :separator (string (inter-directory-separator)))
:collect (apply 'parse-native-namestring namestring constraints)))
(defun getenv-pathname (x &rest constraints &key ensure-directory want-directory on-error &allow-other-keys)
"Extract a pathname from a user-configured environment variable, as per native OS,
check constraints and normalize as per ENSURE-PATHNAME."
;; For backward compatibility with ASDF 2, want-directory implies ensure-directory
(apply 'parse-native-namestring (getenvp x)
:ensure-directory (or ensure-directory want-directory)
:on-error (or on-error
`(error "In (~S ~S), invalid pathname ~*~S: ~*~?" getenv-pathname ,x))
(defun getenv-pathnames (x &rest constraints &key on-error &allow-other-keys)
"Extract a list of pathname from a user-configured environment variable, as per native OS,
check constraints and normalize each one as per ENSURE-PATHNAME."
(apply 'split-native-pathnames-string (getenvp x)
:on-error (or on-error
`(error "In (~S ~S), invalid pathname ~*~S: ~*~?" getenv-pathnames ,x))
(defun getenv-absolute-directory (x)
"Extract an absolute directory pathname from a user-configured environment variable,
as per native OS"
(getenv-pathname x :want-absolute t :ensure-directory t))
(defun getenv-absolute-directories (x)
"Extract a list of absolute directories from a user-configured environment variable,
as per native OS"
(getenv-pathnames x :want-absolute t :ensure-directory t))
(defun lisp-implementation-directory (&key truename)
"Where are the system files of the current installation of the CL implementation?"
(declare (ignorable truename))
#+(or clozure ecl gcl mkcl sbcl)
(let ((dir
#+clozure #p"ccl:"
#+(or ecl mkcl) #p"SYS:"
#+gcl system::*system-directory*
#+sbcl (if-let (it (find-symbol* :sbcl-homedir-pathname :sb-int nil))
(funcall it)
(getenv-pathname "SBCL_HOME" :ensure-directory t)))))
(if (and dir truename)
(truename* dir)
(defun lisp-implementation-pathname-p (pathname)
"Is the PATHNAME under the current installation of the CL implementation?"
;; Other builtin systems are those under the implementation directory
(and (when pathname
(if-let (impdir (lisp-implementation-directory))
(or (subpathp pathname impdir)
(when *resolve-symlinks*
(if-let (truename (truename* pathname))
(if-let (trueimpdir (truename* impdir))
(subpathp truename trueimpdir)))))))
;;; Simple filesystem operations
(with-upgradability ()
(defun ensure-all-directories-exist (pathnames)
"Ensure that for every pathname in PATHNAMES, we ensure its directories exist"
(dolist (pathname pathnames)
(when pathname
(ensure-directories-exist (physicalize-pathname pathname)))))
(defun rename-file-overwriting-target (source target)
"Rename a file, overwriting any previous file with the TARGET name,
in an atomic way if the implementation allows."
#+clisp ;; in recent enough versions of CLISP, :if-exists :overwrite would make it atomic
(progn (funcall 'require "syscalls")
(symbol-call :posix :copy-file source target :method :rename))
(rename-file source target
#+(or clozure ecl) :if-exists #+clozure :rename-and-delete #+ecl t))
(defun delete-file-if-exists (x)
"Delete a file X if it already exists"
(when x (handler-case (delete-file x) (file-error () nil))))
(defun delete-empty-directory (directory-pathname)
"Delete an empty directory"
#+(or abcl digitool gcl) (delete-file directory-pathname)
#+allegro (excl:delete-directory directory-pathname)
#+clisp (ext:delete-directory directory-pathname)
#+clozure (ccl::delete-empty-directory directory-pathname)
#+(or cmu scl) (multiple-value-bind (ok errno)
(unix:unix-rmdir (native-namestring directory-pathname))
(unless ok
#+cmu (error "Error number ~A when trying to delete directory ~A"
errno directory-pathname)
#+scl (error "~@<Error deleting ~S: ~A~@:>"
directory-pathname (unix:get-unix-error-msg errno))))
#+cormanlisp (win32:delete-directory directory-pathname)
#+ecl (si:rmdir directory-pathname)
#+genera (fs:delete-directory directory-pathname)
#+lispworks (lw:delete-directory directory-pathname)
#+mkcl (mkcl:rmdir directory-pathname)
#+sbcl #.(if-let (dd (find-symbol* :delete-directory :sb-ext nil))
`(,dd directory-pathname) ;; requires SBCL 1.0.44 or later
`(progn (require :sb-posix) (symbol-call :sb-posix :rmdir directory-pathname)))
#+xcl (symbol-call :uiop :run-program `("rmdir" ,(native-namestring directory-pathname)))
#-(or abcl allegro clisp clozure cmu cormanlisp digitool ecl gcl genera lispworks mkcl sbcl scl xcl)
(error "~S not implemented on ~S" 'delete-empty-directory (implementation-type))) ; genera
(defun delete-directory-tree (directory-pathname &key (validate nil validatep) (if-does-not-exist :error))
"Delete a directory including all its recursive contents, aka rm -rf.
To reduce the risk of infortunate mistakes, DIRECTORY-PATHNAME must be
a physical non-wildcard directory pathname (not namestring).
If the directory does not exist, the IF-DOES-NOT-EXIST argument specifies what happens:
if it is :ERROR (the default), an error is signaled, whereas if it is :IGNORE, nothing is done.
Furthermore, before any deletion is attempted, the DIRECTORY-PATHNAME must pass
the validation function designated (as per ENSURE-FUNCTION) by the VALIDATE keyword argument
which in practice is thus compulsory, and validates by returning a non-NIL result.
If you're suicidal or extremely confident, just use :VALIDATE T."
(check-type if-does-not-exist (member :error :ignore))
((not (and (pathnamep directory-pathname) (directory-pathname-p directory-pathname)
(physical-pathname-p directory-pathname) (not (wild-pathname-p directory-pathname))))
(error "~S was asked to delete ~S but it is not a physical non-wildcard directory pathname"
'delete-filesystem-tree directory-pathname))
((not validatep)
(error "~S was asked to delete ~S but was not provided a validation predicate"
'delete-filesystem-tree directory-pathname))
((not (call-function validate directory-pathname))
(error "~S was asked to delete ~S but it is not valid ~@[according to ~S~]"
'delete-filesystem-tree directory-pathname validate))
((not (directory-exists-p directory-pathname))
(ecase if-does-not-exist
(error "~S was asked to delete ~S but the directory does not exist"
'delete-filesystem-tree directory-pathname))
(:ignore nil)))
#-(or allegro cmu clozure genera sbcl scl)
((os-unix-p) ;; On Unix, don't recursively walk the directory and delete everything in Lisp,
;; except on implementations where we can prevent DIRECTORY from following symlinks;
;; instead spawn a standard external program to do the dirty work.
(symbol-call :uiop :run-program `("rm" "-rf" ,(native-namestring directory-pathname))))
;; On supported implementation, call supported system functions
#+allegro (symbol-call :excl.osi :delete-directory-and-files
directory-pathname :if-does-not-exist if-does-not-exist)
#+clozure (ccl:delete-directory directory-pathname)
#+genera (fs:delete-directory directory-pathname :confirm nil)
#+sbcl #.(if-let (dd (find-symbol* :delete-directory :sb-ext nil))
`(,dd directory-pathname :recursive t) ;; requires SBCL 1.0.44 or later
'(error "~S requires SBCL 1.0.44 or later" 'delete-directory-tree))
;; Outside Unix or on CMUCL and SCL that can avoid following symlinks,
;; do things the hard way.
#-(or allegro clozure genera sbcl)
(let ((sub*directories
(while-collecting (c)
(collect-sub*directories directory-pathname t t #'c))))
(dolist (d (nreverse sub*directories))
(map () 'delete-file (directory-files d))
(delete-empty-directory d)))))))
;;;; ---------------------------------------------------------------------------
;;;; Utilities related to streams
(uiop/package:define-package :uiop/stream
(:nicknames :asdf/stream)
(:recycle :uiop/stream :asdf/stream :asdf)
(:use :uiop/common-lisp :uiop/package :uiop/utility :uiop/os :uiop/pathname :uiop/filesystem)
#:*stdin* #:setup-stdin #:*stdout* #:setup-stdout #:*stderr* #:setup-stderr
#:detect-encoding #:*encoding-detection-hook* #:always-default-encoding
#:encoding-external-format #:*encoding-external-format-hook* #:default-encoding-external-format
#:*default-encoding* #:*utf-8-external-format*
#:with-safe-io-syntax #:call-with-safe-io-syntax #:safe-read-from-string
#:with-output #:output-string #:with-input
#:with-input-file #:call-with-input-file #:with-output-file #:call-with-output-file
#:null-device-pathname #:call-with-null-input #:with-null-input
#:call-with-null-output #:with-null-output
#:finish-outputs #:format! #:safe-format!
#:copy-stream-to-stream #:concatenate-files #:copy-file
#:slurp-stream-string #:slurp-stream-lines #:slurp-stream-line
#:slurp-stream-forms #:slurp-stream-form
#:read-file-string #:read-file-line #:read-file-lines #:safe-read-file-line
#:read-file-forms #:read-file-form #:safe-read-file-form
#:eval-input #:eval-thunk #:standard-eval-thunk
#:println #:writeln
;; Temporary files
#:*temporary-directory* #:temporary-directory #:default-temporary-directory
#:call-with-temporary-file #:with-temporary-file
#:add-pathname-suffix #:tmpize-pathname
#:call-with-staging-pathname #:with-staging-pathname))
(in-package :uiop/stream)
(with-upgradability ()
(defvar *default-stream-element-type*
(or #+(or abcl cmu cormanlisp scl xcl) 'character
#+lispworks 'lw:simple-char
"default element-type for open (depends on the current CL implementation)")
(defvar *stdin* *standard-input*
"the original standard input stream at startup")
(defun setup-stdin ()
(setf *stdin*
#.(or #+clozure 'ccl::*stdin*
#+(or cmu scl) 'system:*stdin*
#+ecl 'ext::+process-standard-input+
#+sbcl 'sb-sys:*stdin*
(defvar *stdout* *standard-output*
"the original standard output stream at startup")
(defun setup-stdout ()
(setf *stdout*
#.(or #+clozure 'ccl::*stdout*
#+(or cmu scl) 'system:*stdout*
#+ecl 'ext::+process-standard-output+
#+sbcl 'sb-sys:*stdout*
(defvar *stderr* *error-output*
"the original error output stream at startup")
(defun setup-stderr ()
(setf *stderr*
#.(or #+allegro 'excl::*stderr*
#+clozure 'ccl::*stderr*
#+(or cmu scl) 'system:*stderr*
#+ecl 'ext::+process-error-output+
#+sbcl 'sb-sys:*stderr*
;; Run them now. In image.lisp, we'll register them to be run at image restart.
(setup-stdin) (setup-stdout) (setup-stderr))
;;; Encodings (mostly hooks only; full support requires asdf-encodings)
(with-upgradability ()
(defparameter *default-encoding*
;; preserve explicit user changes to something other than the legacy default :default
(or (if-let (previous (and (boundp '*default-encoding*) (symbol-value '*default-encoding*)))
(unless (eq previous :default) previous))
"Default encoding for source files.
The default value :utf-8 is the portable thing.
The legacy behavior was :default.
If you (asdf:load-system :asdf-encodings) then
you will have autodetection via *encoding-detection-hook* below,
reading emacs-style -*- coding: utf-8 -*- specifications,
and falling back to utf-8 or latin1 if nothing is specified.")
(defparameter *utf-8-external-format*
(if (featurep :asdf-unicode)
(or #+clisp charset:utf-8 :utf-8)
"Default :external-format argument to pass to CL:OPEN and also
CL:LOAD or CL:COMPILE-FILE to best process a UTF-8 encoded file.
On modern implementations, this will decode UTF-8 code points as CL characters.
On legacy implementations, it may fall back on some 8-bit encoding,
with non-ASCII code points being read as several CL characters;
hopefully, if done consistently, that won't affect program behavior too much.")
(defun always-default-encoding (pathname)
"Trivial function to use as *encoding-detection-hook*,
always 'detects' the *default-encoding*"
(declare (ignore pathname))
(defvar *encoding-detection-hook* #'always-default-encoding
"Hook for an extension to define a function to automatically detect a file's encoding")
(defun detect-encoding (pathname)
"Detects the encoding of a specified file, going through user-configurable hooks"
(if (and pathname (not (directory-pathname-p pathname)) (probe-file* pathname))
(funcall *encoding-detection-hook* pathname)
(defun default-encoding-external-format (encoding)
"Default, ignorant, function to transform a character ENCODING as a
portable keyword to an implementation-dependent EXTERNAL-FORMAT specification.
Load system ASDF-ENCODINGS to hook in a better one."
(case encoding
(:default :default) ;; for backward-compatibility only. Explicit usage discouraged.
(:utf-8 *utf-8-external-format*)
(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)
(defvar *encoding-external-format-hook*
"Hook for an extension (e.g. ASDF-ENCODINGS) to define a better mapping
from non-default encodings to and implementation-defined external-format's")
(defun encoding-external-format (encoding)
"Transform a portable ENCODING keyword to an implementation-dependent EXTERNAL-FORMAT,
going through all the proper hooks."
(funcall *encoding-external-format-hook* (or encoding *default-encoding*))))
;;; Safe syntax
(with-upgradability ()
(defvar *standard-readtable* (with-standard-io-syntax *readtable*)
"The standard readtable, implementing the syntax specified by the CLHS.
It must never be modified, though only good implementations will even enforce that.")
(defmacro with-safe-io-syntax ((&key (package :cl)) &body body)
"Establish safe CL reader options around the evaluation of BODY"
`(call-with-safe-io-syntax #'(lambda () (let ((*package* (find-package ,package))) ,@body))))
(defun call-with-safe-io-syntax (thunk &key (package :cl))
(let ((*package* (find-package package))
(*read-default-float-format* 'double-float)
(*print-readably* nil)
(*read-eval* nil))
(funcall thunk))))
(defun safe-read-from-string (string &key (package :cl) (eof-error-p t) eof-value (start 0) end preserve-whitespace)
"Read from STRING using a safe syntax, as per WITH-SAFE-IO-SYNTAX"
(with-safe-io-syntax (:package package)
(read-from-string string eof-error-p eof-value :start start :end end :preserve-whitespace preserve-whitespace))))
;;; Output helpers
(with-upgradability ()
(defun call-with-output-file (pathname thunk
(element-type *default-stream-element-type*)
(external-format *utf-8-external-format*)
(if-exists :error)
(if-does-not-exist :create))
"Open FILE for input with given recognizes options, call THUNK with the resulting stream.
Other keys are accepted but discarded."
(with-open-file (s pathname :direction :output
:element-type element-type
:external-format external-format
:if-exists if-exists
:if-does-not-exist if-does-not-exist)
(funcall thunk s)))
(defmacro with-output-file ((var pathname &rest keys
&key element-type external-format if-exists if-does-not-exist)
&body body)
(declare (ignore element-type external-format if-exists if-does-not-exist))
`(call-with-output-file ,pathname #'(lambda (,var) ,@body) ,@keys))
(defun call-with-output (output function &key keys)
"Calls FUNCTION with an actual stream argument,
behaving like FORMAT with respect to how stream designators are interpreted:
If OUTPUT is a STREAM, use it as the stream.
If OUTPUT is NIL, use a STRING-OUTPUT-STREAM as the stream, and return the resulting string.
If OUTPUT is T, use *STANDARD-OUTPUT* as the stream.
If OUTPUT is a STRING with a fill-pointer, use it as a string-output-stream.
If OUTPUT is a PATHNAME, open the file and write to it, passing KEYS to WITH-OUTPUT-FILE
-- this latter as an extension since ASDF 3.1.
Otherwise, signal an error."
(etypecase output
(with-output-to-string (stream) (funcall function stream)))
((eql t)
(funcall function *standard-output*))
(funcall function output))
(assert (fill-pointer output))
(with-output-to-string (stream output) (funcall function stream)))
(apply 'call-with-output-file output function keys))))
(defmacro with-output ((output-var &optional (value output-var)) &body body)
"Bind OUTPUT-VAR to an output stream, coercing VALUE (default: previous binding of OUTPUT-VAR)
as per FORMAT, and evaluate BODY within the scope of this binding."
`(call-with-output ,value #'(lambda (,output-var) ,@body)))
(defun output-string (string &optional output)
"If the desired OUTPUT is not NIL, print the string to the output; otherwise return the string"
(if output
(with-output (output) (princ string output))
;;; Input helpers
(with-upgradability ()
(defun call-with-input-file (pathname thunk
(element-type *default-stream-element-type*)
(external-format *utf-8-external-format*)
(if-does-not-exist :error))
"Open FILE for input with given recognizes options, call THUNK with the resulting stream.
Other keys are accepted but discarded."
(with-open-file (s pathname :direction :input
:element-type element-type
:external-format external-format
:if-does-not-exist if-does-not-exist)
(funcall thunk s)))
(defmacro with-input-file ((var pathname &rest keys
&key element-type external-format if-does-not-exist)
&body body)
(declare (ignore element-type external-format if-does-not-exist))
`(call-with-input-file ,pathname #'(lambda (,var) ,@body) ,@keys))
(defun call-with-input (input function &key keys)
"Calls FUNCTION with an actual stream argument, interpreting
stream designators like READ, but also coercing strings to STRING-INPUT-STREAM,
If INPUT is a STREAM, use it as the stream.
If INPUT is NIL, use a *STANDARD-INPUT* as the stream.
If INPUT is T, use *TERMINAL-IO* as the stream.
If INPUT is a STRING, use it as a string-input-stream.
If INPUT is a PATHNAME, open it, passing KEYS to WITH-INPUT-FILE
-- the latter is an extension since ASDF 3.1.
Otherwise, signal an error."
(etypecase input
(null (funcall function *standard-input*))
((eql t) (funcall function *terminal-io*))
(stream (funcall function input))
(string (with-input-from-string (stream input) (funcall function stream)))
(pathname (apply 'call-with-input-file input function keys))))
(defmacro with-input ((input-var &optional (value input-var)) &body body)
"Bind INPUT-VAR to an input stream, coercing VALUE (default: previous binding of INPUT-VAR)
as per CALL-WITH-INPUT, and evaluate BODY within the scope of this binding."
`(call-with-input ,value #'(lambda (,input-var) ,@body))))
;;; Null device
(with-upgradability ()
(defun null-device-pathname ()
"Pathname to a bit bucket device that discards any information written to it
and always returns EOF when read from"
((os-unix-p) #p"/dev/null")
((os-windows-p) #p"NUL") ;; Q: how many Lisps accept the #p"NUL:" syntax?
(t (error "No /dev/null on your OS"))))
(defun call-with-null-input (fun &rest keys &key element-type external-format if-does-not-exist)
"Call FUN with an input stream from the null device; pass keyword arguments to OPEN."
(declare (ignore element-type external-format if-does-not-exist))
(apply 'call-with-input-file (null-device-pathname) fun keys))
(defmacro with-null-input ((var &rest keys
&key element-type external-format if-does-not-exist)
&body body)
(declare (ignore element-type external-format if-does-not-exist))
"Evaluate BODY in a context when VAR is bound to an input stream accessing the null device.
Pass keyword arguments to OPEN."
`(call-with-null-input #'(lambda (,var) ,@body) ,@keys))
(defun call-with-null-output (fun
&key (element-type *default-stream-element-type*)
(external-format *utf-8-external-format*)
(if-exists :overwrite)
(if-does-not-exist :error))
"Call FUN with an output stream to the null device; pass keyword arguments to OPEN."
(null-device-pathname) fun
:element-type element-type :external-format external-format
:if-exists if-exists :if-does-not-exist if-does-not-exist))
(defmacro with-null-output ((var &rest keys
&key element-type external-format if-does-not-exist if-exists)
&body body)
"Evaluate BODY in a context when VAR is bound to an output stream accessing the null device.
Pass keyword arguments to OPEN."
(declare (ignore element-type external-format if-exists if-does-not-exist))
`(call-with-null-output #'(lambda (,var) ,@body) ,@keys)))
;;; Ensure output buffers are flushed
(with-upgradability ()
(defun finish-outputs (&rest streams)
"Finish output on the main output streams as well as any specified one.
Useful for portably flushing I/O before user input or program exit."
;; CCL notably buffers its stream output by default.
(dolist (s (append streams
(list *stdout* *stderr* *error-output* *standard-output* *trace-output*
*debug-io* *terminal-io* *query-io*)))
(ignore-errors (finish-output s)))
(defun format! (stream format &rest args)
"Just like format, but call finish-outputs before and after the output."
(finish-outputs stream)
(apply 'format stream format args)
(finish-outputs stream))
(defun safe-format! (stream format &rest args)
"Variant of FORMAT that is safe against both
dangerous syntax configuration and errors while printing."
(with-safe-io-syntax ()
(ignore-errors (apply 'format! stream format args))
(finish-outputs stream)))) ; just in case format failed
;;; Simple Whole-Stream processing
(with-upgradability ()
(defun copy-stream-to-stream (input output &key element-type buffer-size linewise prefix)
"Copy the contents of the INPUT stream into the OUTPUT stream.
If LINEWISE is true, then read and copy the stream line by line, with an optional PREFIX.
Otherwise, using WRITE-SEQUENCE using a buffer of size BUFFER-SIZE."
(with-open-stream (input input)
(if linewise
(loop* :for (line eof) = (multiple-value-list (read-line input nil nil))
:while line :do
(when prefix (princ prefix output))
(princ line output)
(unless eof (terpri output))
(finish-output output)
(when eof (return)))
:with buffer-size = (or buffer-size 8192)
:for buffer = (make-array (list buffer-size) :element-type (or element-type 'character))
:for end = (read-sequence buffer input)
:until (zerop end)
:do (write-sequence buffer output :end end)
(when (< end buffer-size) (return))))))
(defun concatenate-files (inputs output)
"create a new OUTPUT file the contents of which a the concatenate of the INPUTS files."
(with-open-file (o output :element-type '(unsigned-byte 8)
:direction :output :if-exists :rename-and-delete)
(dolist (input inputs)
(with-open-file (i input :element-type '(unsigned-byte 8)
:direction :input :if-does-not-exist :error)
(copy-stream-to-stream i o :element-type '(unsigned-byte 8))))))
(defun copy-file (input output)
"Copy contents of the INPUT file to the OUTPUT file"
;; Not available on LW personal edition or LW 6.0 on Mac: (lispworks:copy-file i f)
(concatenate-files (list input) output))
(defun slurp-stream-string (input &key (element-type 'character) stripped)
"Read the contents of the INPUT stream as a string"
(let ((string
(with-open-stream (input input)
(with-output-to-string (output)
(copy-stream-to-stream input output :element-type element-type)))))
(if stripped (stripln string) string)))
(defun slurp-stream-lines (input &key count)
"Read the contents of the INPUT stream as a list of lines, return those lines.
Note: relies on the Lisp's READ-LINE, but additionally removes any remaining CR
from the line-ending if the file or stream had CR+LF but Lisp only removed LF.
Read no more than COUNT lines."
(check-type count (or null integer))
(with-open-stream (input input)
(loop :for n :from 0
:for l = (and (or (not count) (< n count))
(read-line input nil nil))
;; stripln: to remove CR when the OS sends CRLF and Lisp only remove LF
:while l :collect (stripln l))))
(defun slurp-stream-line (input &key (at 0))
"Read the contents of the INPUT stream as a list of lines,
then return the ACCESS-AT of that list of lines using the AT specifier.
PATH defaults to 0, i.e. return the first line.
PATH is typically an integer, or a list of an integer and a function.
If PATH is NIL, it will return all the lines in the file.
The stream will not be read beyond the Nth lines,
where N is the index specified by path
if path is either an integer or a list that starts with an integer."
(access-at (slurp-stream-lines input :count (access-at-count at)) at))
(defun slurp-stream-forms (input &key count)
"Read the contents of the INPUT stream as a list of forms,
and return those forms.
If COUNT is null, read to the end of the stream;
if COUNT is an integer, stop after COUNT forms were read.
BEWARE: be sure to use WITH-SAFE-IO-SYNTAX, or some variant thereof"
(check-type count (or null integer))
(loop :with eof = '#:eof
:for n :from 0
:for form = (if (and count (>= n count))