pathname.lisp 35.7 KB
Newer Older
1 2
;;;; -------------------------------------------------------------------------
;;;; Portability layer around Common Lisp pathnames
3 4
;; This layer allows for portable manipulation of pathname objects themselves,
;; which all is necessary prior to any access the filesystem or environment.
5

6
(uiop/package:define-package :uiop/pathname
7
  (:nicknames :asdf/pathname) ;; deprecated. Used by ceramic
8
  (:use :uiop/common-lisp :uiop/package :uiop/utility :uiop/os)
9
  (:export
10
   ;; Making and merging pathnames, portably
11
   #:normalize-pathname-directory-component #:denormalize-pathname-directory-component
12
   #:merge-pathname-directory-components #:*unspecific-pathname-type* #:make-pathname*
13 14
   #:make-pathname-component-logical #:make-pathname-logical
   #:merge-pathnames*
15
   #:nil-pathname #:*nil-pathname* #:with-pathname-defaults
16
   ;; Predicates
17
   #:pathname-equal #:logical-pathname-p #:physical-pathname-p #:physicalize-pathname
18
   #:absolute-pathname-p #:relative-pathname-p #:hidden-pathname-p #:file-pathname-p
19 20
   ;; Directories
   #:pathname-directory-pathname #:pathname-parent-directory-pathname
21 22
   #:directory-pathname-p #:ensure-directory-pathname
   ;; Parsing filenames
23
   #:component-name-to-pathname-components
24
   #:split-name-type #:parse-unix-namestring #:unix-namestring
25
   #:split-unix-namestring-directory-components
26
   ;; Absolute and relative pathnames
27
   #:subpathname #:subpathname*
28
   #:ensure-absolute-pathname
29
   #:pathname-root #:pathname-host-pathname
30
   #:subpathp #:enough-pathname #:with-enough-pathname #:call-with-enough-pathname
31
   ;; Checking constraints
32 33 34
   #:ensure-pathname ;; implemented in filesystem.lisp to accommodate for existence constraints
   ;; Wildcard pathnames
   #:*wild* #:*wild-file* #:*wild-directory* #:*wild-inferiors* #:*wild-path* #:wilden
35 36 37 38
   ;; Translate a pathname
   #:relativize-directory-component #:relativize-pathname-directory
   #:directory-separator-for-host #:directorize-pathname-host-device
   #:translate-pathname*
39
   #:*output-translation-function*))
40
(in-package :uiop/pathname)
41

42
;;; Normalizing pathnames across implementations
43

44 45
(with-upgradability ()
  (defun normalize-pathname-directory-component (directory)
46 47 48
    "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."
49
    (cond
50
      #-(or cmucl sbcl scl) ;; these implementations already normalize directory components.
51 52 53 54
      ((stringp directory) `(:absolute ,directory))
      ((or (null directory)
           (and (consp directory) (member (first directory) '(:absolute :relative))))
       directory)
55 56 57
      #+gcl
      ((consp directory)
       (cons :relative directory))
58 59 60 61
      (t
       (error (compatfmt "~@<Unrecognized pathname directory component ~S~@:>") directory))))

  (defun denormalize-pathname-directory-component (directory-component)
62 63
    "Convert the DIRECTORY-COMPONENT from a CLHS-standard format to a format usable
by the underlying implementation's MAKE-PATHNAME and other primitives"
64
    directory-component)
65 66

  (defun merge-pathname-directory-components (specified defaults)
67
    "Helper for MERGE-PATHNAMES* that handles directory components"
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    (let ((directory (normalize-pathname-directory-component specified)))
      (ecase (first directory)
        ((nil) defaults)
        (:absolute specified)
        (:relative
         (let ((defdir (normalize-pathname-directory-component defaults))
               (reldir (cdr directory)))
           (cond
             ((null defdir)
              directory)
             ((not (eq :back (first reldir)))
              (append defdir reldir))
             (t
              (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 19.2.2.2.3.
  ;; This will be :unspecific if supported, or NIL if not.
  (defparameter *unspecific-pathname-type*
93
    #+(or abcl allegro clozure cmucl genera lispworks sbcl scl) :unspecific
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
94
    #+(or clasp clisp ecl mkcl gcl xcl #|These haven't been tested:|# cormanlisp mcl) nil
95
    "Unspecific type component to use with the underlying implementation's MAKE-PATHNAME")
96

97
  (defun make-pathname* (&rest keys &key directory host device name type version defaults
98 99
                                      #+scl &allow-other-keys)
    "Takes arguments like CL:MAKE-PATHNAME in the CLHS, and
100
   tries hard to make a pathname that will actually behave as documented,
101 102 103
   despite the peculiarities of each implementation. DEPRECATED: just use MAKE-PATHNAME."
    (declare (ignore host device directory name type version defaults))
    (apply 'make-pathname keys))
104 105 106 107 108 109 110 111 112 113 114

  (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,
115
and make a new pathname with corresponding components and specified logical HOST"
116
    (make-pathname
117 118 119 120 121 122 123 124
     :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*))
    "MERGE-PATHNAMES* is like MERGE-PATHNAMES except that
125 126 127
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,
128
then the HOST and DEVICE both come from the SPECIFIED pathname.
129
This is what users want on a modern Unix or Windows operating system,
130
unlike the MERGE-PATHNAMES behavior.
131
Also, if either argument is NIL, then the other argument is returned unmodified;
132
this is unlike MERGE-PATHNAMES which always merges with a pathname,
133
by default *DEFAULT-PATHNAME-DEFAULTS*, which cannot be NIL."
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    (when (null specified) (return-from merge-pathnames* defaults))
    (when (null defaults) (return-from merge-pathnames* specified))
    #+scl
    (ext:resolve-pathname specified defaults)
    #-scl
    (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)
              ((:absolute)
               (values (pathname-host specified)
                       (pathname-device specified)
                       directory
                       (unspecific-handler specified)))
              ((nil :relative)
               (values (pathname-host defaults)
                       (pathname-device defaults)
                       (merge-pathname-directory-components directory (pathname-directory defaults))
                       (unspecific-handler defaults))))
159 160 161 162
          (make-pathname :host host :device device :directory directory
                         :name (funcall unspecific-handler name)
                         :type (funcall unspecific-handler type)
                         :version (funcall unspecific-handler version))))))
163

164 165 166 167 168 169 170 171 172 173 174 175 176 177
  (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)))

178 179
  (defun nil-pathname (&optional (defaults *default-pathname-defaults*))
    "A pathname that is as neutral as possible for use as defaults
180
when merging, making or parsing pathnames"
181 182 183 184
    ;; 19.2.2.2.1 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.
185 186
    ;; MCL has issues with make-pathname, nil and defaulting
    (declare (ignorable defaults))
187 188
    #.`(make-pathname :directory nil :name nil :type nil :version nil
                      :device (or #+(and mkcl unix) :unspecific)
189
                      :host (or #+cmucl lisp::*unix-host* #+(and mkcl unix) "localhost")
190 191 192 193
                      #+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)))
194

195
  (defvar *nil-pathname* (nil-pathname (physicalize-pathname (user-homedir-pathname)))
196 197
    "A pathname that is as neutral as possible for use as defaults
when merging, making or parsing pathnames")
198

199
  (defmacro with-pathname-defaults ((&optional defaults) &body body)
200
    "Execute BODY in a context where the *DEFAULT-PATHNAME-DEFAULTS* is as specified,
201 202
where leaving the defaults NIL or unspecified means a (NIL-PATHNAME), except
on ABCL, Genera and XCL, where it remains unchanged for it doubles as current-directory."
203
    `(let ((*default-pathname-defaults*
204 205 206
             ,(or defaults
                  #-(or abcl genera xcl) '*nil-pathname*
                  #+(or abcl genera xcl) '*default-pathname-defaults*)))
207
       ,@body)))
208

209

210
;;; Some pathname predicates
211 212
(with-upgradability ()
  (defun pathname-equal (p1 p2)
213
    "Are the two pathnames P1 and P2 reasonably equal in the paths they denote?"
214 215 216 217 218 219 220 221 222 223 224 225 226
    (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)
               x)))
      (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)
227
                      #-(and mkcl unix) (=? pathname-device)
228 229 230
                      (=? normalize-pathname-directory-component pathname-directory)
                      (=? pathname-name)
                      (=? pathname-type)
231
                      #-mkcl (=? pathname-version)))))))
232 233 234

  (defun absolute-pathname-p (pathspec)
    "If PATHSPEC is a pathname or namestring object that parses as a pathname
235 236
possessing an :ABSOLUTE directory component, return the (parsed) pathname.
Otherwise return NIL"
237 238 239 240 241 242 243 244 245
    (and pathspec
         (typep pathspec '(or null pathname string))
         (let ((pathname (pathname pathspec)))
           (and (eq :absolute (car (normalize-pathname-directory-component
                                    (pathname-directory pathname))))
                pathname))))

  (defun relative-pathname-p (pathspec)
    "If PATHSPEC is a pathname or namestring object that parses as a pathname
246 247
possessing a :RELATIVE or NIL directory component, return the (parsed) pathname.
Otherwise return NIL"
248 249 250 251 252 253 254 255 256 257
    (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)))
             pathname))))

  (defun hidden-pathname-p (pathname)
    "Return a boolean that is true if the pathname is hidden as per Unix style,
258
i.e. its name starts with a dot."
259
    (and pathname (equal (first-char (pathname-name pathname)) #\.)))
260

261 262
  (defun file-pathname-p (pathname)
    "Does PATHNAME represent a file, i.e. has a non-null NAME component?
263

264 265 266 267 268 269
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"
270
    (when pathname
271 272 273
      (let ((pathname (pathname pathname)))
        (unless (and (member (pathname-name pathname) '(nil :unspecific "") :test 'equal)
                     (member (pathname-type pathname) '(nil :unspecific "") :test 'equal))
274
          pathname)))))
275

276 277

;;; Directory pathnames
278 279 280
(with-upgradability ()
  (defun pathname-directory-pathname (pathname)
    "Returns a new pathname with same HOST, DEVICE, DIRECTORY as PATHNAME,
281
and NIL NAME, TYPE and VERSION components"
282 283
    (when pathname
      (make-pathname :name nil :type nil :version nil :defaults pathname)))
284

285 286
  (defun pathname-parent-directory-pathname (pathname)
    "Returns a new pathname that corresponds to the parent of the current pathname's directory,
287 288
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/"
289
    (when pathname
290 291 292 293
      (make-pathname :name nil :type nil :version nil
                     :directory (merge-pathname-directory-components
                                 '(:relative :back) (pathname-directory pathname))
                     :defaults pathname)))
294

295 296
  (defun directory-pathname-p (pathname)
    "Does PATHNAME represent a directory?
297 298 299 300 301 302 303

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."
304
    (when pathname
305 306 307
      ;; 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]
308 309
      (let ((pathname (pathname pathname)))
        (flet ((check-one (x)
310
                 (member x '(nil :unspecific) :test 'equal)))
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
          (and (not (wild-pathname-p pathname))
               (check-one (pathname-name pathname))
               (check-one (pathname-type pathname))
               t)))))

  (defun ensure-directory-pathname (pathspec &optional (on-error 'error))
    "Converts the non-wild pathname designator PATHSPEC to directory form."
    (cond
      ((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)
       pathspec)
      (t
328 329 330 331 332
       (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)))))
333 334


335
;;; Parsing filenames
336 337 338 339
(with-upgradability ()
  (defun split-unix-namestring-directory-components
      (unix-namestring &key ensure-directory dot-dot)
    "Splits the path string UNIX-NAMESTRING, returning four values:
340 341 342 343 344 345 346 347 348 349 350 351 352
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.
353

354
An empty string is thus read as meaning a pathname object with all fields nil.
355

356
Note that colon characters #\: will NOT be interpreted as host specification.
357
Absolute pathnames are only appropriate on Unix-style systems.
358

359 360
The intention of this function is to support structured component names,
e.g., \(:file \"foo/bar\"\), which will be unpacked to relative pathnames."
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
    (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))
                                        components))
            (setf components (substitute (or dot-dot :back) ".." components :test #'equal))
            (cond
              ((equal last-comp "")
               (values relative components nil nil)) ; "" already removed from components
              (ensure-directory
               (values relative components nil nil))
              (t
               (values relative (butlast components) last-comp nil)))))))

  (defun split-name-type (filename)
    "Split a filename into two values NAME and TYPE that are returned.
387 388 389 390 391 392
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."
393 394 395 396 397 398 399 400 401 402 403
    (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
                                &allow-other-keys)
    "Coerce NAME into a PATHNAME using standard Unix syntax.
404 405

Unix syntax is used whether or not the underlying system is Unix;
406 407 408
on such non-Unix systems it is reliably usable only for relative pathnames.
This function is especially useful to manipulate relative pathnames portably,
where it is of crucial to possess a portable pathname syntax independent of the underlying OS.
409 410 411 412 413 414 415 416 417 418
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:
419 420 421
1- If TYPE is :DIRECTORY or ENSURE-DIRECTORY is true,
 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.
422
2- If TYPE is NIL, the substring is a file-namestring, and its NAME and TYPE
423 424
 are separated by SPLIT-NAME-TYPE.
3- If TYPE is a string, it is the given TYPE, and the whole string is the NAME.
425

426 427
Directory components with an empty name or the name \".\" are removed.
Any directory named \"..\" is read as DOT-DOT,
428
which must be one of :BACK or :UP and defaults to :BACK.
429 430

HOST, DEVICE and VERSION components are taken from DEFAULTS,
431
which itself defaults to *NIL-PATHNAME*, also used if DEFAULTS is NIL.
432
No host or device can be specified in the string itself,
433
which makes it unsuitable for absolute pathnames outside Unix.
434

435 436 437
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*.
438

439 440 441 442 443
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"
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
    (block nil
      (check-type type (or null string (eql :directory)))
      (when ensure-directory
        (setf type :directory))
      (etypecase name
        ((or null pathname) (return name))
        (symbol
         (setf name (string-downcase name)))
        (string))
      (multiple-value-bind (relative path filename file-only)
          (split-unix-namestring-directory-components
           name :dot-dot dot-dot :ensure-directory (eq type :directory))
        (multiple-value-bind (name type)
            (cond
              ((or (eq type :directory) (null filename))
               (values nil nil))
              (type
               (values filename type))
              (t
               (split-name-type filename)))
          (apply 'ensure-pathname
465
                 (make-pathname
466 467
                  :directory (unless file-only (cons relative path))
                  :name name :type type
468
                  :defaults (or #-mcl defaults *nil-pathname*))
469 470 471 472
                 (remove-plist-keys '(:type :dot-dot :defaults) keys))))))

  (defun unix-namestring (pathname)
    "Given a non-wild PATHNAME, return a Unix-style namestring for it.
473 474 475 476 477 478 479 480 481
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."
482 483 484 485
    (etypecase pathname
      ((or null string) pathname)
      (pathname
       (with-output-to-string (s)
486
         (flet ((err () #+lispworks (describe pathname) (error "Not a valid unix-namestring ~S" pathname)))
487 488
           (let* ((dir (normalize-pathname-directory-component (pathname-directory pathname)))
                  (name (pathname-name pathname))
489
                  (name (and (not (eq name :unspecific)) name))
490 491 492
                  (type (pathname-type pathname))
                  (type (and (not (eq type :unspecific)) type)))
             (cond
493
               ((member dir '(nil :unspecific)))
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
               ((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
                    (cond
                      ((member x '(:back :up)) (princ "../" s))
                      ((equal x "") (err))
                      ;;((member x '("." "..") :test 'equal) (err))
                      ((stringp x) (format s "~A/" x))
                      (t (err))))))
               (t (err)))
             (cond
               (name
509
                (unless (and (stringp name) (or (null type) (stringp type))) (err))
510 511 512
                (format s "~A~@[.~A~]" name type))
               (t
                (or (null type) (err)))))))))))
513

514
;;; Absolute and relative pathnames
515 516 517
(with-upgradability ()
  (defun subpathname (pathname subpath &key type)
    "This function takes a PATHNAME and a SUBPATH and a TYPE.
518 519 520 521 522
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
as per PARSE-UNIX-NAMESTRING with :WANT-RELATIVE T :TYPE TYPE,
then it is merged with the PATHNAME-DIRECTORY-PATHNAME of PATHNAME."
523 524 525 526 527 528 529 530 531 532
    (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)
533
    "return the root directory for the host and device of given PATHNAME"
534 535 536 537 538
    (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))))
539 540

  (defun pathname-host-pathname (pathname)
541
    "return a pathname with the same host as given PATHNAME, and all other fields NIL"
542 543 544 545 546
    (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))))
547

548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
  (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"
    (cond
      ((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"
                                default-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))))

567
  (defun subpathp (maybe-subpath base-pathname)
568 569
    "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."
570 571 572 573
    (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))
574
         (with-pathname-defaults (*nil-pathname*)
575 576 577
           (let ((enough (enough-namestring maybe-subpath base-pathname)))
             (and (relative-pathname-p enough) (pathname enough))))))

578 579 580
  (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."
581
    (let ((sub (when maybe-subpath (pathname maybe-subpath)))
582
          (base (when base-pathname (ensure-absolute-pathname (pathname base-pathname)))))
583
      (or (and base (subpathp sub base)) sub)))
584 585 586 587 588 589 590 591 592 593 594 595 596

  (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"
597
    `(call-with-enough-pathname ,pathname ,defaults #'(lambda (,pathname-var) ,@body))))
598

599

600
;;; Wildcard pathnames
601
(with-upgradability ()
602 603
  (defparameter *wild* (or #+cormanlisp "*" :wild)
    "Wild component for use with MAKE-PATHNAME")
604
  (defparameter *wild-directory-component* (or :wild)
605
    "Wild directory component for use with MAKE-PATHNAME")
606
  (defparameter *wild-inferiors-component* (or :wild-inferiors)
607
    "Wild-inferiors directory component for use with MAKE-PATHNAME")
608 609
  (defparameter *wild-file*
    (make-pathname :directory nil :name *wild* :type *wild*
610 611
                   :version (or #-(or allegro abcl xcl) *wild*))
    "A pathname object with wildcards for matching any file in a given directory")
612
  (defparameter *wild-directory*
613 614
    (make-pathname :directory `(:relative ,*wild-directory-component*)
                   :name nil :type nil :version nil)
615
    "A pathname object with wildcards for matching any subdirectory")
616
  (defparameter *wild-inferiors*
617 618
    (make-pathname :directory `(:relative ,*wild-inferiors-component*)
                   :name nil :type nil :version nil)
619
    "A pathname object with wildcards for matching any recursive subdirectory")
620
  (defparameter *wild-path*
621 622
    (merge-pathnames* *wild-file* *wild-inferiors*)
    "A pathname object with wildcards for matching any file in any recursive subdirectory")
623 624

  (defun wilden (path)
625
    "From a pathname, return a wildcard pathname matching any file in any subdirectory of given pathname's directory"
626
    (merge-pathnames* *wild-path* path)))
627 628 629


;;; Translate a pathname
630 631
(with-upgradability ()
  (defun relativize-directory-component (directory-component)
632
    "Given the DIRECTORY-COMPONENT of a pathname, return an otherwise similar relative directory component"
633 634 635 636 637 638 639 640 641 642
    (let ((directory (normalize-pathname-directory-component directory-component)))
      (cond
        ((stringp directory)
         (list :relative directory))
        ((eq (car directory) :absolute)
         (cons :relative (cdr directory)))
        (t
         directory))))

  (defun relativize-pathname-directory (pathspec)
643
    "Given a PATHNAME, return a relative pathname with otherwise the same components"
644
    (let ((p (pathname pathspec)))
645
      (make-pathname
646 647 648 649
       :directory (relativize-directory-component (pathname-directory p))
       :defaults p)))

  (defun directory-separator-for-host (&optional (pathname *default-pathname-defaults*))
650
    "Given a PATHNAME, return the character used to delimit directory names on this host and device."
651
    (let ((foo (make-pathname :directory '(:absolute "FOO") :defaults pathname)))
652 653 654 655
      (last-char (namestring foo))))

  #-scl
  (defun directorize-pathname-host-device (pathname)
656 657
    "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."
658 659 660 661
    (os-cond
     ((os-unix-p)
      (when (physical-pathname-p pathname)
        (return-from directorize-pathname-host-device pathname))))
662 663 664 665 666 667 668 669 670 671 672 673 674
    (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))
           (root-string
             (substitute-if #\/
                            #'(lambda (x) (or (eql x #\:)
                                              (eql x separator)))
                            root-namestring)))
      (multiple-value-bind (relative path filename)
          (split-unix-namestring-directory-components root-string :ensure-directory t)
        (declare (ignore relative filename))
675
        (let ((new-base (make-pathname :defaults root :directory `(:absolute ,@path))))
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
          (translate-pathname absolute-pathname wild-root (wilden new-base))))))

  #+scl
  (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)))
697 698
              (make-pathname :directory `(:absolute ,prefix ,@(rest directory))
                             :defaults pathname)))
699 700 701
        pathname)))

  (defun* (translate-pathname*) (path absolute-source destination &optional root source)
702 703 704 705 706 707 708
    "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."
709
    (declare (ignore source))
710
    (cond
711 712 713 714 715 716 717 718 719 720
      ((functionp destination)
       (funcall destination path absolute-source))
      ((eq destination t)
       path)
      ((not (pathnamep destination))
       (error "Invalid destination"))
      ((not (absolute-pathname-p destination))
       (translate-pathname path absolute-source (merge-pathnames* destination root)))
      (root
       (translate-pathname (directorize-pathname-host-device path) absolute-source destination))
721
      (t
722
       (translate-pathname path absolute-source destination))))
723

724 725 726 727 728 729 730 731
  (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."))
732