1 ;;;; machine/filesystem-independent pathname functions
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!IMPL")
14 #!-sb-fluid
(declaim (freeze-type logical-pathname logical-host
))
16 ;;; To be initialized in unix/win32-pathname.lisp
17 (defglobal *physical-host
* nil
)
19 ;;; Return a value suitable, e.g., for preinitializing
20 ;;; *DEFAULT-PATHNAME-DEFAULTS* before *DEFAULT-PATHNAME-DEFAULTS* is
21 ;;; initialized (at which time we can't safely call e.g. #'PATHNAME).
22 (defun make-trivial-default-pathname ()
23 (%%make-pathname
*physical-host
* nil nil nil nil
:newest
))
27 ;;; SXHASH does a really poor job on pathname directory, especially if in your
28 ;;; environment, the directories of interest are all many levels down from the
29 ;;; filesystem root- every directory in your work space might hash to the same
30 ;;; value under SXHASH. Mixing in all pieces of the directory path solves that.
31 (defun pathname-dir-hash (directory)
32 (let ((hash (sxhash (car directory
))))
33 (dolist (piece (cdr directory
) hash
)
34 (mixf hash
(sxhash piece
)))))
36 (defmethod print-object ((pathname pathname
) stream
)
37 (let ((namestring (handler-case (namestring pathname
)
41 (if (or *print-readably
* *print-escape
*)
44 (coerce namestring
'(simple-array character
(*))))
45 (print-unreadable-object (pathname stream
:type t
)
47 "~@<(with no namestring) ~_:HOST ~S ~_:DEVICE ~S ~_:DIRECTORY ~S ~
48 ~_:NAME ~S ~_:TYPE ~S ~_:VERSION ~S~:>"
49 (%pathname-host pathname
)
50 (%pathname-device pathname
)
51 (%pathname-directory pathname
)
52 (%pathname-name pathname
)
53 (%pathname-type pathname
)
54 (%pathname-version pathname
))))))
56 (defmethod make-load-form ((pathname pathname
) &optional environment
)
57 (make-load-form-saving-slots pathname
:environment environment
))
59 ;;; A pathname is logical if the host component is a logical host.
60 ;;; This constructor is used to make an instance of the correct type
61 ;;; from parsed arguments.
62 (defun %make-maybe-logical-pathname
(host device directory name type version
)
63 ;; We canonicalize logical pathname components to uppercase. ANSI
64 ;; doesn't strictly require this, leaving it up to the implementor;
65 ;; but the arguments given in the X3J13 cleanup issue
66 ;; PATHNAME-LOGICAL:ADD seem compelling: we should canonicalize the
67 ;; case, and uppercase is the ordinary way to do that.
68 (flet ((upcase-maybe (x) (typecase x
(string (logical-word-or-lose x
)) (t x
))))
69 (if (typep host
'logical-host
)
70 (%make-logical-pathname host
72 (mapcar #'upcase-maybe directory
)
77 (aver (eq host
*physical-host
*))
78 (%make-pathname host device directory name type version
)))))
80 ;;; Hash table searching maps a logical pathname's host to its
81 ;;; physical pathname translation.
82 (define-load-time-global *logical-hosts
*
83 (make-hash-table :test
'equal
:synchronized t
))
87 (defmethod make-load-form ((pattern pattern
) &optional environment
)
88 (make-load-form-saving-slots pattern
:environment environment
))
90 (defmethod print-object ((pattern pattern
) stream
)
91 (print-unreadable-object (pattern stream
:type t
)
93 (let ((*print-escape
* t
))
94 (pprint-fill stream
(pattern-pieces pattern
) nil
))
95 (prin1 (pattern-pieces pattern
) stream
))))
97 (defun pattern= (pattern1 pattern2
)
98 (declare (type pattern pattern1 pattern2
))
99 (let ((pieces1 (pattern-pieces pattern1
))
100 (pieces2 (pattern-pieces pattern2
)))
101 (and (= (length pieces1
) (length pieces2
))
102 (every (lambda (piece1 piece2
)
105 (and (simple-string-p piece2
)
106 (string= piece1 piece2
)))
109 (eq (car piece1
) (car piece2
))
110 (string= (cdr piece1
) (cdr piece2
))))
112 (eq piece1 piece2
))))
116 ;;; If the string matches the pattern returns the multiple values T
117 ;;; and a list of the matched strings.
118 (defun pattern-matches (pattern string
)
119 (declare (type pattern pattern
)
120 (type simple-string string
))
121 (let ((len (length string
)))
122 (labels ((maybe-prepend (subs cur-sub chars
)
124 (let* ((len (length chars
))
125 (new (make-string len
))
128 (setf (schar new
(decf index
)) char
))
131 (matches (pieces start subs cur-sub chars
)
134 (values t
(maybe-prepend subs cur-sub chars
))
136 (let ((piece (car pieces
)))
139 (let ((end (+ start
(length piece
))))
141 (string= piece string
142 :start2 start
:end2 end
)
143 (matches (cdr pieces
) end
144 (maybe-prepend subs cur-sub chars
)
150 (let ((char (schar string start
)))
151 (if (find char
(cdr piece
) :test
#'char
=)
152 (matches (cdr pieces
) (1+ start
) subs t
153 (cons char chars
))))))))
154 ((member :single-char-wild
)
156 (matches (cdr pieces
) (1+ start
) subs t
157 (cons (schar string start
) chars
))))
158 ((member :multi-char-wild
)
159 (multiple-value-bind (won new-subs
)
160 (matches (cdr pieces
) start subs t chars
)
164 (matches pieces
(1+ start
) subs t
165 (cons (schar string start
)
167 (multiple-value-bind (won subs
)
168 (matches (pattern-pieces pattern
) 0 nil nil nil
)
169 (values won
(reverse subs
))))))
171 ;;; PATHNAME-MATCH-P for directory components
172 (defun directory-components-match (thing wild
)
175 ;; If THING has a null directory, assume that it matches
176 ;; (:ABSOLUTE :WILD-INFERIORS) or (:RELATIVE :WILD-INFERIORS).
179 (member (first wild
) '(:absolute
:relative
))
180 (eq (second wild
) :wild-inferiors
))
182 (let ((wild1 (first wild
)))
183 (if (eq wild1
:wild-inferiors
)
184 (let ((wild-subdirs (rest wild
)))
185 (or (null wild-subdirs
)
187 (when (directory-components-match thing wild-subdirs
)
190 (unless thing
(return nil
)))))
192 (components-match (first thing
) wild1
)
193 (directory-components-match (rest thing
)
196 ;;; Return true if pathname component THING is matched by WILD. (not
198 (defun components-match (thing wild
)
199 (declare (type (or pattern symbol simple-string integer
) thing wild
))
204 ;; String is matched by itself, a matching pattern or :WILD.
207 (values (pattern-matches wild thing
)))
209 (string= thing wild
))))
211 ;; A pattern is only matched by an identical pattern.
212 (and (pattern-p wild
) (pattern= thing wild
)))
214 ;; An integer (version number) is matched by :WILD or the
215 ;; same integer. This branch will actually always be NIL as
216 ;; long as the version is a fixnum.
219 ;;; a predicate for comparing two pathname slot component sub-entries
220 (defun compare-component (this that
)
224 (and (simple-string-p that
)
225 (string= this that
)))
227 (and (pattern-p that
)
228 (pattern= this that
)))
231 (compare-component (car this
) (car that
))
232 (compare-component (cdr this
) (cdr that
)))))))
234 ;;;; pathname functions
236 (defun pathname= (pathname1 pathname2
)
237 (declare (type pathname pathname1
)
238 (type pathname pathname2
))
239 (or (eq pathname1 pathname2
)
240 (and (eq (%pathname-host pathname1
)
241 (%pathname-host pathname2
))
242 (= (%pathname-dir-hash pathname1
) (%pathname-dir-hash pathname2
))
243 (= (%pathname-stem-hash pathname1
) (%pathname-stem-hash pathname2
))
244 (compare-component (%pathname-device pathname1
)
245 (%pathname-device pathname2
))
246 (compare-component (%pathname-directory pathname1
)
247 (%pathname-directory pathname2
))
248 (compare-component (%pathname-name pathname1
)
249 (%pathname-name pathname2
))
250 (compare-component (%pathname-type pathname1
)
251 (%pathname-type pathname2
))
252 (or (eq (%pathname-host pathname1
) *physical-host
*)
253 (compare-component (%pathname-version pathname1
)
254 (%pathname-version pathname2
))))))
256 ;;; This is conceptually like (DEFUN-CACHED (%MAKE-PATHNAME ...))
257 ;;; except that we try hard never to evict entries until SAVE-LISP-AND-DIE.
258 ;;; Entries can still be kicked out randomly though.
259 ;;; A two-level lookup is used- it works better than mixing all
260 ;;; pathname components into a hash key.
261 (define-load-time-global *pathnames
* (make-array 211 :initial-element nil
))
262 (defglobal *pathnames-lock
* (sb!thread
:make-mutex
:name
"Pathnames"))
264 (defun %make-pathname
(host device directory name type version
)
265 (if (or device
(neq host
*physical-host
*))
266 (%%make-pathname host device directory name type version
)
267 (let* ((table *pathnames
*)
268 (index (rem (pathname-dir-hash directory
) (length table
)))
270 ;; Candidates is a list of ((dir . contents) ...)
272 with candidates
= (svref table index
) and new
= nil
274 (let ((n-candidates 0))
275 (dolist (candidate candidates
)
277 (when (compare-component (car candidate
) directory
)
278 (return-from outer candidate
)))
280 (setq new
(cons directory
(make-array 3 :initial-element nil
))))
281 (cond ((< n-candidates
10)
282 (let* ((cell (cons new candidates
))
283 (actual-old (cas (svref table index
) candidates cell
)))
284 (when (eq actual-old candidates
)
285 (return-from outer new
))
286 (setq candidates actual-old
)))
288 ;; Clobber this cache entry, losing all directories in it.
289 ;; Hopefully this doesn't happen often.
290 #+nil
(format t
"~&*** Pathname cache overflow: ~D ~S~%"
291 index
(mapcar 'car candidates
))
292 (setf (svref table index
) (list new
))
293 (return-from outer new
)))))))
294 (flet ((matchp (stem-hash candidates
)
295 (let ((n-candidates 0))
296 (dolist (pathname candidates
(values nil n-candidates
))
297 (when (and (= (%pathname-stem-hash pathname
) stem-hash
)
298 (compare-component (%pathname-version pathname
) version
)
299 (compare-component (%pathname-name pathname
) name
)
300 (compare-component (%pathname-type pathname
) type
))
301 (return (values pathname
0)))
302 (incf n-candidates
)))))
303 ;; We have tests asserting that the distinction between :NEWEST
304 ;; and NIL is preserved, though there is no effective difference.
305 (binding* ((stem-hash (mix (sxhash name
) (sxhash type
)))
306 (vector (the simple-vector
(cdr dir-holder
)))
307 (index (rem stem-hash
(length vector
)))
308 (candidates (svref vector index
))
309 ((found n-candidates
) (matchp stem-hash candidates
)))
311 (return-from %make-pathname found
))
312 ;; Optimistically assuming that the pathname won't be found
313 ;; on the double-check, allocate it now
314 (let ((pathname (%%make-pathname
*physical-host
* nil
(car dir-holder
)
316 (sb!thread
::with-system-mutex
(*pathnames-lock
*)
317 (when (>= n-candidates
10)
318 ;; Rehash into a larger vector
319 (let* ((old-len (length vector
))
320 (new-len (+ old-len
4))
321 (new-vector (make-array new-len
:initial-element nil
)))
322 (dovector (list vector
)
324 (push p
(svref new-vector
(rem (%pathname-stem-hash p
)
326 (rplacd dir-holder new-vector
)
327 (setq vector new-vector
328 index
(rem stem-hash new-len
)
329 candidates
(svref vector index
))))
330 (let ((found (matchp stem-hash candidates
)))
332 (setq pathname found
)
333 (push pathname
(svref vector index
)))))
336 ;;; Convert PATHNAME-DESIGNATOR (a pathname, or string, or
337 ;;; stream), into a pathname in PATHNAME.
338 (eval-when (:compile-toplevel
:execute
)
339 (sb!xc
:defmacro with-pathname
((pathname pathname-designator
) &body body
)
340 (once-only ((pathname-designator pathname-designator
))
341 `(let ((,pathname
(etypecase ,pathname-designator
342 (pathname ,pathname-designator
)
343 (string (parse-namestring ,pathname-designator
))
344 (file-stream (file-name ,pathname-designator
)))))
347 (sb!xc
:defmacro with-native-pathname
((pathname pathname-designator
) &body body
)
348 (once-only ((pathname-designator pathname-designator
))
349 `(let ((,pathname
(etypecase ,pathname-designator
350 (pathname ,pathname-designator
)
351 (string (parse-native-namestring ,pathname-designator
))
354 (file-stream (file-name ,pathname-designator
)))))
357 (sb!xc
:defmacro with-host
((host host-designator
) &body body
)
358 ;; Generally, redundant specification of information in software,
359 ;; whether in code or in comments, is bad. However, the ANSI spec
360 ;; for this is messy enough that it's hard to hold in short-term
361 ;; memory, so I've recorded these redundant notes on the
362 ;; implications of the ANSI spec.
364 ;; According to the ANSI spec, HOST can be a valid pathname host, or
365 ;; a logical host, or NIL.
367 ;; A valid pathname host can be a valid physical pathname host or a
368 ;; valid logical pathname host.
370 ;; A valid physical pathname host is "any of a string, a list of
371 ;; strings, or the symbol :UNSPECIFIC, that is recognized by the
372 ;; implementation as the name of a host". In SBCL as of 0.6.9.8,
373 ;; that means :UNSPECIFIC: though someday we might want to
374 ;; generalize it to allow strings like "RTFM.MIT.EDU" or lists like
375 ;; '("RTFM" "MIT" "EDU"), that's not supported now.
377 ;; A valid logical pathname host is a string which has been defined as
378 ;; the name of a logical host, as with LOAD-LOGICAL-PATHNAME-TRANSLATIONS.
380 ;; A logical host is an object of implementation-dependent nature. In
381 ;; SBCL, it's a member of the HOST class (a subclass of STRUCTURE-OBJECT).
382 (once-only ((host-designator host-designator
))
383 `(let ((,host
(etypecase ,host-designator
385 ;; This is a special host. It's not valid as a
386 ;; logical host, so it is a sensible thing to
387 ;; designate the physical host object. So we do
391 ;; In general ANSI-compliant Common Lisps, a
392 ;; string might also be a physical pathname
393 ;; host, but ANSI leaves this up to the
394 ;; implementor, and in SBCL we don't do it, so
395 ;; it must be a logical host.
396 (find-logical-host ,host-designator
))
397 ((or null
(member :unspecific
))
398 ;; CLHS says that HOST=:UNSPECIFIC has
399 ;; implementation-defined behavior. We
400 ;; just turn it into NIL.
403 ;; ANSI also allows LISTs to designate hosts,
404 ;; but leaves its interpretation
405 ;; implementation-defined. Our interpretation
406 ;; is that it's unsupported.:-|
407 (error "A LIST representing a pathname host is not ~
408 supported in this implementation:~% ~S"
410 (host ,host-designator
))))
414 (defun find-host (host-designator &optional
(errorp t
))
415 (with-host (host host-designator
)
416 (when (and errorp
(not host
))
417 (error "Couldn't find host: ~S" host-designator
))
420 (defun pathname (pathspec)
421 "Convert PATHSPEC (a pathname designator) into a pathname."
422 (declare (type pathname-designator pathspec
))
423 (with-pathname (pathname pathspec
)
426 (defun native-pathname (pathspec)
427 "Convert PATHSPEC (a pathname designator) into a pathname, assuming
428 the operating system native pathname conventions."
429 (with-native-pathname (pathname pathspec
)
432 ;;; Change the case of thing if DIDDLE-P.
433 (defun maybe-diddle-case (thing diddle-p
)
434 (if (and diddle-p
(not (or (symbolp thing
) (integerp thing
))))
435 (labels ((check-for (pred in
)
438 (dolist (piece (pattern-pieces in
))
439 (when (typecase piece
441 (check-for pred piece
))
445 (check-for pred
(cdr piece
))))))
449 (when (check-for pred x
)
452 (dotimes (i (length in
))
453 (when (funcall pred
(schar in i
))
456 (diddle-with (fun thing
)
460 (mapcar (lambda (piece)
468 (funcall fun
(cdr piece
))))
473 (pattern-pieces thing
))))
480 (let ((any-uppers (check-for #'upper-case-p thing
))
481 (any-lowers (check-for #'lower-case-p thing
)))
482 (cond ((and any-uppers any-lowers
)
483 ;; mixed case, stays the same
486 ;; all uppercase, becomes all lower case
487 (diddle-with (lambda (x) (if (stringp x
)
491 ;; all lowercase, becomes all upper case
492 (diddle-with (lambda (x) (if (stringp x
)
496 ;; no letters? I guess just leave it.
500 (defun merge-directories (dir1 dir2 diddle-case
)
501 (if (or (eq (car dir1
) :absolute
)
506 (if (and (eq dir
:back
)
508 (typep (car results
) '(or string pattern
509 (member :wild
:wild-inferiors
))))
511 (push dir results
))))
512 (dolist (dir (maybe-diddle-case dir2 diddle-case
))
514 (dolist (dir (cdr dir1
))
518 (defun merge-pathnames (pathname
520 (defaults *default-pathname-defaults
*)
521 (default-version :newest
))
522 "Construct a filled in pathname by completing the unspecified components
524 (declare (type pathname-designator pathname
)
525 (type pathname-designator defaults
)
527 (with-pathname (defaults defaults
)
528 (let ((pathname (let ((*default-pathname-defaults
* defaults
))
529 (pathname pathname
))))
530 (let* ((default-host (%pathname-host defaults
))
531 (pathname-host (%pathname-host pathname
))
533 (and default-host pathname-host
534 (not (eq (host-customary-case default-host
)
535 (host-customary-case pathname-host
)))))
536 (directory (merge-directories (%pathname-directory pathname
)
537 (%pathname-directory defaults
)
539 (%make-maybe-logical-pathname
540 (or pathname-host default-host
)
541 (and ;; The device of ~/ shouldn't be merged,
542 ;; because the expansion may have a different device
543 (not (and (>= (length directory
) 2)
544 (eql (car directory
) :absolute
)
545 (eql (cadr directory
) :home
)))
546 (or (%pathname-device pathname
)
547 (maybe-diddle-case (%pathname-device defaults
)
550 (or (%pathname-name pathname
)
551 (maybe-diddle-case (%pathname-name defaults
)
553 (or (%pathname-type pathname
)
554 (maybe-diddle-case (%pathname-type defaults
)
556 (or (%pathname-version pathname
)
557 (and (not (%pathname-name pathname
)) (%pathname-version defaults
))
558 default-version
))))))
560 (defun import-directory (directory diddle-case
)
563 ((member :wild
) '(:absolute
:wild-inferiors
))
564 ((member :unspecific
) '(:relative
))
566 (let ((root (pop directory
))
568 (if (member root
'(:relative
:absolute
))
570 (error "List of directory components must start with ~S or ~S."
571 :absolute
:relative
))
573 (let ((next (car directory
)))
574 (when (or (eq :home next
)
575 (typep next
'(cons (eql :home
) (cons string null
))))
576 (push (pop directory
) results
)))
577 (dolist (piece directory
)
579 ((member :wild
:wild-inferiors
:up
)
580 (push piece results
))
582 (if (typep (car results
) '(or string pattern
583 (member :wild
:wild-inferiors
)))
585 (push piece results
)))
587 (when (typep piece
'(and string
(not simple-array
)))
588 (setq piece
(coerce piece
'simple-string
)))
589 ;; Unix namestrings allow embedded "//" within them. Consecutive
590 ;; slashes are treated as one, which is weird but often convenient.
591 ;; However, preserving empty directory components:
593 ;; - makes (NAMESTRING (MAKE-PATHNAME :DIRECTORY '(:RELATIVE "" "d")))
594 ;; visually indistinguishable from the absolute pathname "/d/"
595 ;; - can causes a pathname equality test to return NIL
596 ;; on semantically equivalent pathnames. This can happen for
597 ;; other reasons, but fewer false negatives is better.
598 (unless (and (stringp piece
) (zerop (length piece
)))
599 (push (maybe-diddle-case piece diddle-case
) results
)))
601 (error "~S is not allowed as a directory component." piece
)))))
604 (cond ((zerop (length directory
)) `(:absolute
))
606 (when (typep directory
'(not simple-array
))
607 (setq directory
(coerce directory
'simple-string
)))
608 `(:absolute
,(maybe-diddle-case directory diddle-case
)))))))
610 (defun make-pathname (&key host
615 (version nil versionp
)
618 "Makes a new pathname from the component arguments. Note that host is
619 a host-structure or string."
620 (declare (type (or string host pathname-component-tokens
) host
)
621 (type (or string pathname-component-tokens
) device
)
622 (type (or list string pattern pathname-component-tokens
) directory
)
623 (type (or string pattern pathname-component-tokens
) name type
)
624 (type (or integer pathname-component-tokens
(member :newest
))
626 (type (or pathname-designator null
) defaults
)
627 (type (member :common
:local
) case
))
628 (let* ((defaults (when defaults
629 (with-pathname (defaults defaults
) defaults
)))
630 (default-host (if defaults
631 (%pathname-host defaults
)
632 (pathname-host *default-pathname-defaults
*)))
633 ;; Raymond Toy writes: CLHS says make-pathname can take a
634 ;; string (as a logical-host) for the host part. We map that
635 ;; string into the corresponding logical host structure.
637 ;; Paul Werkowski writes:
638 ;; HyperSpec says for the arg to MAKE-PATHNAME;
639 ;; "host---a valid physical pathname host. ..."
640 ;; where it probably means -- a valid pathname host.
641 ;; "valid pathname host n. a valid physical pathname host or
642 ;; a valid logical pathname host."
644 ;; "valid physical pathname host n. any of a string,
645 ;; a list of strings, or the symbol :unspecific,
646 ;; that is recognized by the implementation as the name of a host."
647 ;; "valid logical pathname host n. a string that has been defined
648 ;; as the name of a logical host. ..."
649 ;; HS is silent on what happens if the :HOST arg is NOT one of these.
650 ;; It seems an error message is appropriate.
651 (host (or (find-host host nil
) default-host
))
652 (diddle-args (and (eq (host-customary-case host
) :lower
)
655 (not (eq (host-customary-case host
)
656 (host-customary-case default-host
))))
657 (dev (if devp device
(if defaults
(%pathname-device defaults
))))
658 (dir (import-directory directory diddle-args
))
661 (defaults (%pathname-version defaults
))
663 (when (and defaults
(not dirp
))
665 (merge-directories dir
666 (%pathname-directory defaults
)
669 (macrolet ((pick (var varp field
)
670 `(cond ((or (simple-string-p ,var
)
672 (maybe-diddle-case ,var diddle-args
))
674 (maybe-diddle-case (coerce ,var
'simple-string
)
677 (maybe-diddle-case ,var diddle-args
))
679 (maybe-diddle-case (,field defaults
)
683 (%make-maybe-logical-pathname host
684 dev
; forced to :UNSPECIFIC when logical
686 (pick name namep %pathname-name
)
687 (pick type typep %pathname-type
)
690 (defun pathname-host (pathname &key
(case :local
))
691 "Return PATHNAME's host."
692 (declare (type pathname-designator pathname
)
693 (type (member :local
:common
) case
)
696 (with-pathname (pathname pathname
)
697 (%pathname-host pathname
)))
699 (defun pathname-device (pathname &key
(case :local
))
700 "Return PATHNAME's device."
701 (declare (type pathname-designator pathname
)
702 (type (member :local
:common
) case
))
703 (with-pathname (pathname pathname
)
704 (maybe-diddle-case (%pathname-device pathname
)
705 (and (eq case
:common
)
706 (eq (host-customary-case
707 (%pathname-host pathname
))
710 (defun pathname-directory (pathname &key
(case :local
))
711 "Return PATHNAME's directory."
712 (declare (type pathname-designator pathname
)
713 (type (member :local
:common
) case
))
714 (with-pathname (pathname pathname
)
715 (maybe-diddle-case (%pathname-directory pathname
)
716 (and (eq case
:common
)
717 (eq (host-customary-case
718 (%pathname-host pathname
))
720 (defun pathname-name (pathname &key
(case :local
))
721 "Return PATHNAME's name."
722 (declare (type pathname-designator pathname
)
723 (type (member :local
:common
) case
))
724 (with-pathname (pathname pathname
)
725 (maybe-diddle-case (%pathname-name pathname
)
726 (and (eq case
:common
)
727 (eq (host-customary-case
728 (%pathname-host pathname
))
731 (defun pathname-type (pathname &key
(case :local
))
732 "Return PATHNAME's type."
733 (declare (type pathname-designator pathname
)
734 (type (member :local
:common
) case
))
735 (with-pathname (pathname pathname
)
736 (maybe-diddle-case (%pathname-type pathname
)
737 (and (eq case
:common
)
738 (eq (host-customary-case
739 (%pathname-host pathname
))
742 (defun pathname-version (pathname)
743 "Return PATHNAME's version."
744 (declare (type pathname-designator pathname
))
745 (with-pathname (pathname pathname
)
746 (%pathname-version pathname
)))
750 ;;; Handle the case for PARSE-NAMESTRING parsing a potentially
751 ;;; syntactically valid logical namestring with an explicit host.
753 ;;; This then isn't fully general -- we are relying on the fact that
754 ;;; we will only pass to parse-namestring namestring with an explicit
755 ;;; logical host, so that we can pass the host return from
756 ;;; parse-logical-namestring through to %PARSE-NAMESTRING as a truth
757 ;;; value. Yeah, this is probably a KLUDGE - CSR, 2002-04-18
758 (defun parseable-logical-namestring-p (namestr start end
)
761 ((namestring-parse-error (lambda (c)
764 (let ((colon (position #\
: namestr
:start start
:end end
)))
766 (let ((potential-host
767 (logical-word-or-lose (subseq namestr start colon
))))
768 ;; depending on the outcome of CSR comp.lang.lisp post
769 ;; "can PARSE-NAMESTRING create logical hosts", we may need
770 ;; to do things with potential-host (create it
771 ;; temporarily, parse the namestring and unintern the
772 ;; logical host potential-host on failure.
773 (declare (ignore potential-host
))
776 ((simple-type-error (lambda (c)
779 (parse-logical-namestring namestr start end
))))
780 ;; if we got this far, we should have an explicit host
781 ;; (first return value of parse-logical-namestring)
785 ;;; Handle the case where PARSE-NAMESTRING is actually parsing a
786 ;;; namestring. We pick off the :JUNK-ALLOWED case then find a host to
787 ;;; use for parsing, call the parser, then check whether the host matches.
788 (defun %parse-namestring
(namestr host defaults start end junk-allowed
)
789 (declare (type (or host null
) host
)
790 (type string namestr
)
792 (type (or index null
) end
))
796 (%parse-namestring namestr host defaults start end nil
)
797 (namestring-parse-error (condition)
798 (values nil
(namestring-parse-error-offset condition
)))))
800 (let* ((end (%check-vector-sequence-bounds namestr start end
)))
801 (multiple-value-bind (new-host device directory file type version
)
802 ;; Comments below are quotes from the HyperSpec
803 ;; PARSE-NAMESTRING entry, reproduced here to demonstrate
804 ;; that we actually have to do things this way rather than
805 ;; some possibly more logical way. - CSR, 2002-04-18
807 ;; "If host is a logical host then thing is parsed as a
808 ;; logical pathname namestring on the host."
809 (host (funcall (host-parse host
) namestr start end
))
810 ;; "If host is nil and thing is a syntactically valid
811 ;; logical pathname namestring containing an explicit
812 ;; host, then it is parsed as a logical pathname
814 ((parseable-logical-namestring-p namestr start end
)
815 (parse-logical-namestring namestr start end
))
816 ;; "If host is nil, default-pathname is a logical
817 ;; pathname, and thing is a syntactically valid logical
818 ;; pathname namestring without an explicit host, then it
819 ;; is parsed as a logical pathname namestring on the
820 ;; host that is the host component of default-pathname."
822 ;; "Otherwise, the parsing of thing is
823 ;; implementation-defined."
825 ;; Both clauses are handled here, as the default
826 ;; *DEFAULT-PATHNAME-DEFAULTS* has a SB-IMPL::UNIX-HOST
828 ((pathname-host defaults
)
829 (funcall (host-parse (pathname-host defaults
))
833 ;; I don't think we should ever get here, as the default
834 ;; host will always have a non-null HOST, given that we
835 ;; can't create a new pathname without going through
836 ;; *DEFAULT-PATHNAME-DEFAULTS*, which has a non-null
838 (t (bug "Fallen through COND in %PARSE-NAMESTRING")))
839 (when (and host new-host
(not (eq new-host host
)))
840 (error 'simple-type-error
842 ;; Note: ANSI requires that this be a TYPE-ERROR,
843 ;; but there seems to be no completely correct
844 ;; value to use for TYPE-ERROR-EXPECTED-TYPE.
845 ;; Instead, we return a sort of "type error allowed
846 ;; type", trying to say "it would be OK if you
847 ;; passed NIL as the host value" but not mentioning
848 ;; that a matching string would be OK too.
851 "The host in the namestring, ~S,~@
852 does not match the explicit HOST argument, ~S."
853 :format-arguments
(list new-host host
)))
854 (let ((pn-host (or new-host host
(pathname-host defaults
))))
855 (values (%make-maybe-logical-pathname
856 pn-host device directory file type version
)
859 ;;; If NAMESTR begins with a colon-terminated, defined, logical host,
860 ;;; then return that host, otherwise return NIL.
861 (defun extract-logical-host-prefix (namestr start end
)
862 (declare (type simple-string namestr
)
863 (type index start end
)
864 (values (or logical-host null
)))
865 (let ((colon-pos (position #\
: namestr
:start start
:end end
)))
867 (values (gethash (nstring-upcase (subseq namestr start colon-pos
))
871 (defun parse-namestring (thing
874 (defaults *default-pathname-defaults
*)
875 &key
(start 0) end junk-allowed
)
876 (declare (ftype (function * (values (or null pathname
) (or null index
)))
878 (with-host (found-host host
)
879 (let (;; According to ANSI defaults may be any valid pathname designator
880 (defaults (etypecase defaults
884 (aver (pathnamep *default-pathname-defaults
*))
885 (parse-namestring defaults
))
887 (truename defaults
)))))
888 (declare (type pathname defaults
))
891 (with-array-data ((thing thing
) (start start
) (end end
)
892 :check-fill-pointer t
)
893 (multiple-value-bind (pathname position
)
894 (%parse-namestring thing found-host defaults start end junk-allowed
)
895 (values pathname
(- position start
)))))
897 (let ((defaulted-host (or found-host
(%pathname-host defaults
))))
898 (declare (type host defaulted-host
))
899 (unless (eq defaulted-host
(%pathname-host thing
))
900 (error "The HOST argument doesn't match the pathname host:~% ~
902 defaulted-host
(%pathname-host thing
))))
903 (values thing start
))
905 (let ((name (file-name thing
)))
907 (error "can't figure out the file associated with stream:~% ~S"
909 (values name nil
)))))))
911 (defun %parse-native-namestring
(namestr host defaults start end junk-allowed
913 (declare (type (or host null
) host
)
914 (type string namestr
)
916 (type (or index null
) end
))
920 (%parse-native-namestring namestr host defaults start end nil as-directory
)
921 (namestring-parse-error (condition)
922 (values nil
(namestring-parse-error-offset condition
)))))
924 (let* ((end (%check-vector-sequence-bounds namestr start end
)))
925 (multiple-value-bind (new-host device directory file type version
)
928 (funcall (host-parse-native host
) namestr start end as-directory
))
929 ((pathname-host defaults
)
930 (funcall (host-parse-native (pathname-host defaults
))
935 ;; I don't think we should ever get here, as the default
936 ;; host will always have a non-null HOST, given that we
937 ;; can't create a new pathname without going through
938 ;; *DEFAULT-PATHNAME-DEFAULTS*, which has a non-null
940 (t (bug "Fallen through COND in %PARSE-NAMESTRING")))
941 (when (and host new-host
(not (eq new-host host
)))
942 (error 'simple-type-error
944 :expected-type
`(or null
(eql ,host
))
946 "The host in the namestring, ~S,~@
947 does not match the explicit HOST argument, ~S."
948 :format-arguments
(list new-host host
)))
949 (let ((pn-host (or new-host host
(pathname-host defaults
))))
950 (values (%make-pathname
951 pn-host device directory file type version
)
954 (defun parse-native-namestring (thing
957 (defaults *default-pathname-defaults
*)
958 &key
(start 0) end junk-allowed
960 "Convert THING into a pathname, using the native conventions
961 appropriate for the pathname host HOST, or if not specified the
962 host of DEFAULTS. If THING is a string, the parse is bounded by
963 START and END, and error behaviour is controlled by JUNK-ALLOWED,
964 as with PARSE-NAMESTRING. For file systems whose native
965 conventions allow directories to be indicated as files, if
966 AS-DIRECTORY is true, return a pathname denoting THING as a
968 (declare (type pathname-designator thing defaults
)
969 (type (or list host string
(member :unspecific
)) host
)
971 (type (or index null
) end
)
972 (type (or t null
) junk-allowed
)
973 (values (or null pathname
) (or null index
)))
974 (declare (ftype (function * (values (or null pathname
) (or null index
)))
975 %parse-native-namestring
))
976 (with-host (found-host host
)
977 (let ((defaults (etypecase defaults
981 (aver (pathnamep *default-pathname-defaults
*))
982 (parse-native-namestring defaults
))
984 (truename defaults
)))))
985 (declare (type pathname defaults
))
988 (with-array-data ((thing thing
) (start start
) (end end
)
989 :check-fill-pointer t
)
990 (multiple-value-bind (pathname position
)
991 (%parse-native-namestring thing
992 found-host defaults start end junk-allowed
994 (values pathname
(- position start
)))))
996 (let ((defaulted-host (or found-host
(%pathname-host defaults
))))
997 (declare (type host defaulted-host
))
998 (unless (eq defaulted-host
(%pathname-host thing
))
999 (error "The HOST argument doesn't match the pathname host:~% ~
1001 defaulted-host
(%pathname-host thing
))))
1002 (values thing start
))
1005 (let ((name (file-name thing
)))
1007 (error "can't figure out the file associated with stream:~% ~S"
1009 (values name nil
)))))))
1011 (defun namestring (pathname)
1012 "Construct the full (name)string form of the pathname."
1013 (declare (type pathname-designator pathname
))
1014 (with-pathname (pathname pathname
)
1016 (or (%pathname-namestring pathname
)
1017 (let ((host (%pathname-host pathname
)))
1019 (no-namestring-error
1020 pathname
"there is no ~S component." :host
)
1021 (setf (%pathname-namestring pathname
)
1022 (logically-readonlyize
1023 (possibly-base-stringize
1024 (funcall (host-unparse host
) pathname
))))))))))
1026 (defun native-namestring (pathname &key as-file
)
1027 "Construct the full native (name)string form of PATHNAME. For
1028 file systems whose native conventions allow directories to be
1029 indicated as files, if AS-FILE is true and the name, type, and
1030 version components of PATHNAME are all NIL or :UNSPECIFIC,
1031 construct a string that names the directory according to the file
1032 system's syntax for files."
1033 (declare (type pathname-designator pathname
))
1034 (with-native-pathname (pathname pathname
)
1036 (let ((host (%pathname-host pathname
)))
1038 (error "can't determine the native namestring for pathnames with no ~
1039 host:~% ~S" pathname
))
1040 (funcall (host-unparse-native host
) pathname as-file
)))))
1042 (defun host-namestring (pathname)
1043 "Return a string representation of the name of the host in the pathname."
1044 (declare (type pathname-designator pathname
))
1045 (with-pathname (pathname pathname
)
1046 (let ((host (%pathname-host pathname
)))
1048 (funcall (host-unparse-host host
) pathname
)
1050 "can't determine the namestring for pathnames with no host:~% ~S"
1053 (defun directory-namestring (pathname)
1054 "Return a string representation of the directories used in the pathname."
1055 (declare (type pathname-designator pathname
))
1056 (with-pathname (pathname pathname
)
1057 (let ((host (%pathname-host pathname
)))
1059 (funcall (host-unparse-directory host
) pathname
)
1061 "can't determine the namestring for pathnames with no host:~% ~S"
1064 (defun file-namestring (pathname)
1065 "Return a string representation of the name used in the pathname."
1066 (declare (type pathname-designator pathname
))
1067 (with-pathname (pathname pathname
)
1068 (let ((host (%pathname-host pathname
)))
1070 (funcall (host-unparse-file host
) pathname
)
1072 "can't determine the namestring for pathnames with no host:~% ~S"
1075 (defun enough-namestring (pathname
1077 (defaults *default-pathname-defaults
*))
1078 "Return an abbreviated pathname sufficient to identify the pathname relative
1080 (declare (type pathname-designator pathname
))
1081 (with-pathname (pathname pathname
)
1082 (let ((host (%pathname-host pathname
)))
1084 (with-pathname (defaults defaults
)
1085 (funcall (host-unparse-enough host
) pathname defaults
))
1087 "can't determine the namestring for pathnames with no host:~% ~S"
1092 (defun wild-pathname-p (pathname &optional field-key
)
1093 "Predicate for determining whether pathname contains any wildcards."
1094 (declare (type pathname-designator pathname
)
1095 (type (member nil
:host
:device
:directory
:name
:type
:version
)
1097 (with-pathname (pathname pathname
)
1099 (or (pattern-p x
) (member x
'(:wild
:wild-inferiors
)))))
1102 (or (wild-pathname-p pathname
:host
)
1103 (wild-pathname-p pathname
:device
)
1104 (wild-pathname-p pathname
:directory
)
1105 (wild-pathname-p pathname
:name
)
1106 (wild-pathname-p pathname
:type
)
1107 (wild-pathname-p pathname
:version
)))
1108 (:host
(frob (%pathname-host pathname
)))
1109 (:device
(frob (%pathname-host pathname
)))
1110 (:directory
(some #'frob
(%pathname-directory pathname
)))
1111 (:name
(frob (%pathname-name pathname
)))
1112 (:type
(frob (%pathname-type pathname
)))
1113 (:version
(frob (%pathname-version pathname
)))))))
1115 (defun pathname-match-p (in-pathname in-wildname
)
1116 "Pathname matches the wildname template?"
1117 (declare (type pathname-designator in-pathname
))
1118 (with-pathname (pathname in-pathname
)
1119 (with-pathname (wildname in-wildname
)
1120 (macrolet ((frob (field &optional
(op 'components-match
))
1121 `(or (null (,field wildname
))
1122 (,op
(,field pathname
) (,field wildname
)))))
1123 (and (or (null (%pathname-host wildname
))
1124 (eq (%pathname-host wildname
) (%pathname-host pathname
)))
1125 (frob %pathname-device
)
1126 (frob %pathname-directory directory-components-match
)
1127 (frob %pathname-name
)
1128 (frob %pathname-type
)
1129 (or (eq (%pathname-host wildname
) *physical-host
*)
1130 (frob %pathname-version
)))))))
1132 ;;; Place the substitutions into the pattern and return the string or pattern
1133 ;;; that results. If DIDDLE-CASE is true, we diddle the result case as well,
1134 ;;; in case we are translating between hosts with difference conventional case.
1135 ;;; The second value is the tail of subs with all of the values that we used up
1136 ;;; stripped off. Note that PATTERN-MATCHES matches all consecutive wildcards
1137 ;;; as a single string, so we ignore subsequent contiguous wildcards.
1138 (defun substitute-into (pattern subs diddle-case
)
1139 (declare (type pattern pattern
)
1141 (values (or simple-string pattern
) list
))
1142 (let ((in-wildcard nil
)
1145 (dolist (piece (pattern-pieces pattern
))
1146 (cond ((simple-string-p piece
)
1147 (push piece strings
)
1148 (setf in-wildcard nil
))
1151 (setf in-wildcard t
)
1153 (error "not enough wildcards in FROM pattern to match ~
1156 (let ((sub (pop subs
)))
1160 (push (apply #'concatenate
'simple-string
1163 (dolist (piece (pattern-pieces sub
))
1164 (push piece pieces
)))
1168 (error "can't substitute this into the middle of a word:~
1173 (push (apply #'concatenate
'simple-string
(nreverse strings
))
1177 (if (and pieces
(simple-string-p (car pieces
)) (null (cdr pieces
)))
1179 (make-pattern (nreverse pieces
)))
1183 ;;; Called when we can't see how source and from matched.
1184 (defun didnt-match-error (source from
)
1185 (error "Pathname components from SOURCE and FROM args to TRANSLATE-PATHNAME~@
1186 did not match:~% ~S ~S"
1189 ;;; Do TRANSLATE-COMPONENT for all components except host, directory
1191 (defun translate-component (source from to diddle-case
)
1198 (if (pattern= from source
)
1200 (didnt-match-error source from
)))
1202 (multiple-value-bind (won subs
) (pattern-matches from source
)
1204 (values (substitute-into to subs diddle-case
))
1205 (didnt-match-error source from
))))
1207 (maybe-diddle-case source diddle-case
))))
1209 (values (substitute-into to
(list source
) diddle-case
)))
1211 (if (components-match source from
)
1212 (maybe-diddle-case source diddle-case
)
1213 (didnt-match-error source from
)))))
1215 (maybe-diddle-case source diddle-case
))
1217 (if (components-match source from
)
1219 (didnt-match-error source from
)))))
1221 ;;; Return a list of all the things that we want to substitute into the TO
1222 ;;; pattern (the things matched by from on source.) When From contains
1223 ;;; :WILD-INFERIORS, the result contains a sublist of the matched source
1225 (defun compute-directory-substitutions (orig-source orig-from
)
1226 (let ((source orig-source
)
1231 (unless (every (lambda (x) (eq x
:wild-inferiors
)) from
)
1232 (didnt-match-error orig-source orig-from
))
1235 (unless from
(didnt-match-error orig-source orig-from
))
1236 (let ((from-part (pop from
))
1237 (source-part (pop source
)))
1240 (typecase source-part
1242 (if (pattern= from-part source-part
)
1244 (didnt-match-error orig-source orig-from
)))
1246 (multiple-value-bind (won new-subs
)
1247 (pattern-matches from-part source-part
)
1249 (dolist (sub new-subs
)
1251 (didnt-match-error orig-source orig-from
))))
1253 (didnt-match-error orig-source orig-from
))))
1256 ((member :wild-inferiors
)
1257 (let ((remaining-source (cons source-part source
)))
1260 (when (directory-components-match remaining-source from
)
1262 (unless remaining-source
1263 (didnt-match-error orig-source orig-from
))
1264 (res (pop remaining-source
)))
1266 (setq source remaining-source
))))
1268 (unless (and (simple-string-p source-part
)
1269 (string= from-part source-part
))
1270 (didnt-match-error orig-source orig-from
)))
1272 (didnt-match-error orig-source orig-from
)))))
1275 ;;; This is called by TRANSLATE-PATHNAME on the directory components
1276 ;;; of its argument pathnames to produce the result directory
1277 ;;; component. If this leaves the directory NIL, we return the source
1278 ;;; directory. The :RELATIVE or :ABSOLUTE is taken from the source
1279 ;;; directory, except if TO is :ABSOLUTE, in which case the result
1280 ;;; will be :ABSOLUTE.
1281 (defun translate-directories (source from to diddle-case
)
1282 (if (not (and source to from
))
1283 (or (and to
(null source
) (remove :wild-inferiors to
))
1284 (mapcar (lambda (x) (maybe-diddle-case x diddle-case
)) source
))
1286 ;; If TO is :ABSOLUTE, the result should still be :ABSOLUTE.
1287 (res (if (eq (first to
) :absolute
)
1290 (let ((subs-left (compute-directory-substitutions (rest source
)
1292 (dolist (to-part (rest to
))
1296 (let ((match (pop subs-left
)))
1298 (error ":WILD-INFERIORS is not paired in from and to ~
1299 patterns:~% ~S ~S" from to
))
1300 (res (maybe-diddle-case match diddle-case
))))
1301 ((member :wild-inferiors
)
1303 (let ((match (pop subs-left
)))
1304 (unless (listp match
)
1305 (error ":WILD-INFERIORS not paired in from and to ~
1306 patterns:~% ~S ~S" from to
))
1308 (res (maybe-diddle-case x diddle-case
)))))
1310 (multiple-value-bind
1312 (substitute-into to-part subs-left diddle-case
)
1313 (setf subs-left new-subs-left
)
1315 (t (res to-part
)))))
1318 (defun translate-pathname (source from-wildname to-wildname
&key
)
1319 "Use the source pathname to translate the from-wildname's wild and
1320 unspecified elements into a completed to-pathname based on the to-wildname."
1321 (declare (type pathname-designator source from-wildname to-wildname
))
1322 (with-pathname (source source
)
1323 (with-pathname (from from-wildname
)
1324 (with-pathname (to to-wildname
)
1325 (let* ((source-host (%pathname-host source
))
1326 (from-host (%pathname-host from
))
1327 (to-host (%pathname-host to
))
1329 (and source-host to-host
1330 (not (eq (host-customary-case source-host
)
1331 (host-customary-case to-host
))))))
1332 (macrolet ((frob (field &optional
(op 'translate-component
))
1333 `(let ((result (,op
(,field source
)
1337 (if (eq result
:error
)
1338 (error "~S doesn't match ~S." source from
)
1340 (%make-maybe-logical-pathname
1341 (or to-host source-host
)
1342 (frob %pathname-device
)
1343 (frob %pathname-directory translate-directories
)
1344 (frob %pathname-name
)
1345 (frob %pathname-type
)
1346 (if (eq from-host
*physical-host
*)
1347 (if (or (eq (%pathname-version to
) :wild
)
1348 (eq (%pathname-version to
) nil
))
1349 (%pathname-version source
)
1350 (%pathname-version to
))
1351 (frob %pathname-version
)))))))))
1353 ;;;; logical pathname support. ANSI 92-102 specification.
1355 ;;;; As logical-pathname translations are loaded they are
1356 ;;;; canonicalized as patterns to enable rapid efficient translation
1357 ;;;; into physical pathnames.
1361 ;;; Access *DEFAULT-PATHNAME-DEFAULTS*, issuing a warning if its value
1362 ;;; is silly. (Unlike the vaguely-analogous SANE-PACKAGE, we don't
1363 ;;; actually need to reset the variable when it's silly, since even
1364 ;;; crazy values of *DEFAULT-PATHNAME-DEFAULTS* don't leave the system
1365 ;;; in a state where it's hard to recover interactively.)
1366 (defun sane-default-pathname-defaults ()
1367 (let* ((dfd *default-pathname-defaults
*)
1368 (dfd-dir (pathname-directory dfd
)))
1369 ;; It's generally not good to use a relative pathname for
1370 ;; *DEFAULT-PATHNAME-DEFAULTS*, since relative pathnames
1371 ;; are defined by merging into a default pathname (which is,
1372 ;; by default, *DEFAULT-PATHNAME-DEFAULTS*).
1373 (when (and (consp dfd-dir
)
1374 (eql (first dfd-dir
) :relative
))
1376 "~@<~S is a relative pathname. (But we'll try using it anyway.)~@:>"
1377 '*default-pathname-defaults
*))
1380 (defun simplify-namestring (namestring &optional host
)
1381 (funcall (host-simplify-namestring
1383 (pathname-host (sane-default-pathname-defaults))))
1386 ;;; Canonicalize a logical pathname word by uppercasing it checking that it
1387 ;;; contains only legal characters.
1388 (defun logical-word-or-lose (word)
1389 (declare (string word
))
1390 (when (string= word
"")
1391 (error 'namestring-parse-error
1392 :complaint
"Attempted to treat invalid logical hostname ~
1393 as a logical host:~% ~S"
1395 :namestring word
:offset
0))
1396 (let ((word (string-upcase word
)))
1397 (dotimes (i (length word
))
1398 (let ((ch (schar word i
)))
1399 (unless (and (typep ch
'standard-char
)
1400 (or (alpha-char-p ch
) (digit-char-p ch
) (char= ch
#\-
)))
1401 (error 'namestring-parse-error
1402 :complaint
"logical namestring character which ~
1403 is not alphanumeric or hyphen:~% ~S"
1405 :namestring word
:offset i
))))
1406 (coerce word
'string
))) ; why not simple-string?
1408 ;;; Given a logical host or string, return a logical host. If ERROR-P
1409 ;;; is NIL, then return NIL when no such host exists.
1410 (defun find-logical-host (thing &optional
(errorp t
))
1413 (let ((found (gethash (logical-word-or-lose thing
)
1415 (if (or found
(not errorp
))
1417 ;; This is the error signalled from e.g.
1418 ;; LOGICAL-PATHNAME-TRANSLATIONS when host is not a defined
1419 ;; host, and ANSI specifies that that's a TYPE-ERROR.
1420 (error 'simple-type-error
1422 ;; God only knows what ANSI expects us to use for
1423 ;; the EXPECTED-TYPE here. Maybe this will be OK..
1425 '(and string
(satisfies logical-pathname-translations
))
1426 :format-control
"logical host not yet defined: ~S"
1427 :format-arguments
(list thing
)))))
1428 (logical-host thing
)))
1430 ;;; Given a logical host name or host, return a logical host, creating
1431 ;;; a new one if necessary.
1432 (defun intern-logical-host (thing)
1433 (with-locked-system-table (*logical-hosts
*)
1434 (or (find-logical-host thing nil
)
1435 (let* ((name (logical-word-or-lose thing
))
1436 (new (make-logical-host :name name
)))
1437 (setf (gethash name
*logical-hosts
*) new
)
1440 ;;;; logical pathname parsing
1442 ;;; Deal with multi-char wildcards in a logical pathname token.
1443 (defun maybe-make-logical-pattern (namestring chunks
)
1444 (let ((chunk (caar chunks
)))
1445 (collect ((pattern))
1447 (len (length chunk
)))
1448 (declare (fixnum last-pos
))
1450 (when (= last-pos len
) (return))
1451 (let ((pos (or (position #\
* chunk
:start last-pos
) len
)))
1452 (if (= pos last-pos
)
1454 (error 'namestring-parse-error
1455 :complaint
"double asterisk inside of logical ~
1458 :namestring namestring
1459 :offset
(+ (cdar chunks
) pos
)))
1460 (pattern (subseq chunk last-pos pos
)))
1463 (pattern :multi-char-wild
))
1464 (setq last-pos
(1+ pos
)))))
1467 (make-pattern (pattern))
1468 (let ((x (car (pattern))))
1469 (if (eq x
:multi-char-wild
)
1473 ;;; Return a list of conses where the CDR is the start position and
1474 ;;; the CAR is a string (token) or character (punctuation.)
1475 (defun logical-chunkify (namestr start end
)
1477 (do ((i start
(1+ i
))
1481 (chunks (cons (nstring-upcase (subseq namestr prev end
)) prev
))))
1482 (let ((ch (schar namestr i
)))
1483 (unless (or (alpha-char-p ch
) (digit-char-p ch
)
1484 (member ch
'(#\-
#\
*)))
1486 (chunks (cons (nstring-upcase (subseq namestr prev i
)) prev
)))
1488 (unless (member ch
'(#\
; #\: #\.))
1489 (error 'namestring-parse-error
1490 :complaint
"illegal character for logical pathname:~% ~S"
1494 (chunks (cons ch i
)))))
1497 ;;; Break up a logical-namestring, always a string, into its
1498 ;;; constituent parts.
1499 (defun parse-logical-namestring (namestr start end
)
1500 (declare (type simple-string namestr
)
1501 (type index start end
))
1502 (collect ((directory))
1507 (labels ((expecting (what chunks
)
1508 (unless (and chunks
(simple-string-p (caar chunks
)))
1509 (error 'namestring-parse-error
1510 :complaint
"expecting ~A, got ~:[nothing~;~S~]."
1511 :args
(list what
(caar chunks
) (caar chunks
))
1513 :offset
(if chunks
(cdar chunks
) end
)))
1515 (parse-host (chunks)
1516 (case (caadr chunks
)
1519 (find-logical-host (expecting "a host name" chunks
)))
1520 (parse-relative (cddr chunks
)))
1522 (parse-relative chunks
))))
1523 (parse-relative (chunks)
1526 (directory :relative
)
1527 (parse-directory (cdr chunks
)))
1529 (directory :absolute
) ; Assumption! Maybe revoked later.
1530 (parse-directory chunks
))))
1531 (parse-directory (chunks)
1532 (case (caadr chunks
)
1535 (let ((res (expecting "a directory name" chunks
)))
1536 (cond ((string= res
"..") :up
)
1537 ((string= res
"**") :wild-inferiors
)
1539 (maybe-make-logical-pattern namestr chunks
)))))
1540 (parse-directory (cddr chunks
)))
1542 (parse-name chunks
))))
1543 (parse-name (chunks)
1545 (expecting "a file name" chunks
)
1546 (setq name
(maybe-make-logical-pattern namestr chunks
))
1547 (expecting-dot (cdr chunks
))))
1548 (expecting-dot (chunks)
1550 (unless (eql (caar chunks
) #\.
)
1551 (error 'namestring-parse-error
1552 :complaint
"expecting a dot, got ~S."
1553 :args
(list (caar chunks
))
1555 :offset
(cdar chunks
)))
1557 (parse-version (cdr chunks
))
1558 (parse-type (cdr chunks
)))))
1559 (parse-type (chunks)
1560 (expecting "a file type" chunks
)
1561 (setq type
(maybe-make-logical-pattern namestr chunks
))
1562 (expecting-dot (cdr chunks
)))
1563 (parse-version (chunks)
1564 (let ((str (expecting "a positive integer, * or NEWEST"
1567 ((string= str
"*") (setq version
:wild
))
1568 ((string= str
"NEWEST") (setq version
:newest
))
1570 (multiple-value-bind (res pos
)
1571 (parse-integer str
:junk-allowed t
)
1572 (unless (and res
(plusp res
))
1573 (error 'namestring-parse-error
1574 :complaint
"expected a positive integer, ~
1578 :offset
(+ pos
(cdar chunks
))))
1579 (setq version res
)))))
1581 (error 'namestring-parse-error
1582 :complaint
"extra stuff after end of file name"
1584 :offset
(cdadr chunks
)))))
1585 (parse-host (logical-chunkify namestr start end
)))
1586 (values host
:unspecific
(directory) name type version
))))
1588 ;;; We can't initialize this yet because not all host methods are
1590 (defvar *logical-pathname-defaults
*)
1592 (defun logical-namestring-p (x)
1595 (typep (pathname x
) 'logical-pathname
))))
1597 (deftype logical-namestring
()
1598 `(satisfies logical-namestring-p
))
1600 (defun logical-pathname (pathspec)
1601 "Converts the pathspec argument to a logical-pathname and returns it."
1602 (declare (type (or logical-pathname string stream
) pathspec
)
1603 (values logical-pathname
))
1604 (if (typep pathspec
'logical-pathname
)
1606 (flet ((oops (problem)
1607 (error 'simple-type-error
1609 :expected-type
'logical-namestring
1610 :format-control
"~S is not a valid logical namestring:~% ~A"
1611 :format-arguments
(list pathspec problem
))))
1612 (let ((res (handler-case
1613 (parse-namestring pathspec nil
*logical-pathname-defaults
*)
1614 (error (e) (oops e
)))))
1615 (when (eq (%pathname-host res
)
1616 (%pathname-host
*logical-pathname-defaults
*))
1617 (oops "no host specified"))
1620 ;;;; logical pathname unparsing
1622 (defun unparse-logical-directory (pathname)
1623 (declare (type pathname pathname
))
1625 (let ((directory (%pathname-directory pathname
)))
1627 (ecase (pop directory
)
1628 (:absolute
) ; nothing special
1629 (:relative
(pieces ";")))
1630 (dolist (dir directory
)
1631 (cond ((or (stringp dir
) (pattern-p dir
))
1632 (pieces (unparse-logical-piece dir
))
1636 ((eq dir
:wild-inferiors
)
1639 (error "invalid directory component: ~S" dir
))))))
1640 (apply #'concatenate
'simple-string
(pieces))))
1642 (defun unparse-logical-piece (thing)
1644 ((member :wild
) "*")
1645 (simple-string thing
)
1647 (collect ((strings))
1648 (dolist (piece (pattern-pieces thing
))
1650 (simple-string (strings piece
))
1652 (cond ((eq piece
:wild-inferiors
)
1654 ((eq piece
:multi-char-wild
)
1656 (t (error "invalid keyword: ~S" piece
))))))
1657 (apply #'concatenate
'simple-string
(strings))))))
1659 (defun unparse-logical-file (pathname)
1660 (declare (type pathname pathname
))
1661 (collect ((strings))
1662 (let* ((name (%pathname-name pathname
))
1663 (type (%pathname-type pathname
))
1664 (version (%pathname-version pathname
))
1665 (type-supplied (not (or (null type
) (eq type
:unspecific
))))
1666 (version-supplied (not (or (null version
)
1667 (eq version
:unspecific
)))))
1669 (when (and (null type
)
1670 (typep name
'string
)
1671 (position #\. name
:start
1))
1672 (error "too many dots in the name: ~S" pathname
))
1673 (strings (unparse-logical-piece name
)))
1676 (error "cannot specify the type without a file: ~S" pathname
))
1677 (when (typep type
'string
)
1678 (when (position #\. type
)
1679 (error "type component can't have a #\. inside: ~S" pathname
)))
1681 (strings (unparse-logical-piece type
)))
1682 (when version-supplied
1683 (unless type-supplied
1684 (error "cannot specify the version without a type: ~S" pathname
))
1686 ((member :newest
) (strings ".NEWEST")) ; really? not in LPNIFY-NAMESTRING
1687 ((member :wild
) (strings ".*"))
1688 (fixnum (strings ".") (strings (format nil
"~D" version
))))))
1689 (apply #'concatenate
'simple-string
(strings))))
1691 ;;; Unparse a logical pathname string.
1692 (defun unparse-enough-namestring (pathname defaults
)
1693 (let* ((path-directory (pathname-directory pathname
))
1694 (def-directory (pathname-directory defaults
))
1696 ;; Go down the directory lists to see what matches. What's
1697 ;; left is what we want, more or less.
1698 (cond ((and (eq (first path-directory
) (first def-directory
))
1699 (eq (first path-directory
) :absolute
))
1700 ;; Both paths are :ABSOLUTE, so find where the
1701 ;; common parts end and return what's left
1702 (do* ((p (rest path-directory
) (rest p
))
1703 (d (rest def-directory
) (rest d
)))
1704 ((or (endp p
) (endp d
)
1705 (not (equal (first p
) (first d
))))
1708 ;; At least one path is :RELATIVE, so just return the
1709 ;; original path. If the original path is :RELATIVE,
1710 ;; then that's the right one. If PATH-DIRECTORY is
1711 ;; :ABSOLUTE, we want to return that except when
1712 ;; DEF-DIRECTORY is :ABSOLUTE, as handled above. so return
1713 ;; the original directory.
1715 (unparse-logical-namestring
1716 (make-pathname :host
(pathname-host pathname
)
1717 :directory enough-directory
1718 :name
(pathname-name pathname
)
1719 :type
(pathname-type pathname
)
1720 :version
(pathname-version pathname
)))))
1722 (defun unparse-logical-namestring (pathname)
1723 (declare (type logical-pathname pathname
))
1724 (concatenate 'simple-string
1725 (logical-host-name (%pathname-host pathname
)) ":"
1726 (unparse-logical-directory pathname
)
1727 (unparse-logical-file pathname
)))
1729 ;;;; logical pathname translations
1731 ;;; Verify that the list of translations consists of lists and prepare
1732 ;;; canonical translations. (Parse pathnames and expand out wildcards
1734 (defun canonicalize-logical-pathname-translations (translation-list host
)
1735 (declare (type list translation-list
) (type host host
)
1737 (mapcar (lambda (translation)
1738 (destructuring-bind (from to
) translation
1739 (list (if (typep from
'logical-pathname
)
1741 (parse-namestring from host
))
1745 (defun logical-pathname-translations (host)
1746 "Return the (logical) host object argument's list of translations."
1747 (declare (type (or string logical-host
) host
)
1749 (logical-host-translations (find-logical-host host
)))
1751 (defun (setf logical-pathname-translations
) (translations host
)
1752 "Set the translations list for the logical host argument."
1753 (declare (type (or string logical-host
) host
)
1754 (type list translations
)
1756 (let ((host (intern-logical-host host
)))
1757 (setf (logical-host-canon-transls host
)
1758 (canonicalize-logical-pathname-translations translations host
))
1759 (setf (logical-host-translations host
) translations
)))
1761 (defun translate-logical-pathname (pathname &key
)
1762 "Translate PATHNAME to a physical pathname, which is returned."
1763 (declare (type pathname-designator pathname
)
1764 (values (or null pathname
)))
1767 (dolist (x (logical-host-canon-transls (%pathname-host pathname
))
1768 (error 'simple-file-error
1770 :format-control
"no translation for ~S"
1771 :format-arguments
(list pathname
)))
1772 (destructuring-bind (from to
) x
1773 (when (pathname-match-p pathname from
)
1774 (return (translate-logical-pathname
1775 (translate-pathname pathname from to
)))))))
1777 (t (translate-logical-pathname (pathname pathname
)))))
1779 (defvar *logical-pathname-defaults
*
1780 (%make-logical-pathname
1781 (make-logical-host :name
(logical-word-or-lose "BOGUS"))
1782 :unspecific nil nil nil nil
))
1784 (defun load-logical-pathname-translations (host)
1785 "Reads logical pathname translations from SYS:SITE;HOST.TRANSLATIONS.NEWEST,
1786 with HOST replaced by the supplied parameter. Returns T on success.
1788 If HOST is already defined as logical pathname host, no file is loaded and NIL
1791 The file should contain a single form, suitable for use with
1792 \(SETF LOGICAL-PATHNAME-TRANSLATIONS).
1794 Note: behaviour of this function is highly implementation dependent, and
1795 historically it used to be a no-op in SBCL -- the current approach is somewhat
1796 experimental and subject to change."
1797 (declare (type string host
)
1798 (values (member t nil
)))
1799 (if (find-logical-host host nil
)
1800 ;; This host is already defined, all is well and good.
1802 ;; ANSI: "The specific nature of the search is
1803 ;; implementation-defined."
1805 (setf (logical-pathname-translations host
)
1806 (with-open-file (lpt (make-pathname :host
"SYS"
1807 :directory
'(:absolute
"SITE")
1809 :type
"TRANSLATIONS"
1813 (defun !pathname-cold-init
()
1814 (let* ((sys *default-pathname-defaults
*)
1817 (make-pathname :directory
'(:relative
"src" :wild-inferiors
)
1818 :name
:wild
:type
:wild
)
1822 (make-pathname :directory
'(:relative
"contrib" :wild-inferiors
)
1823 :name
:wild
:type
:wild
)
1827 (make-pathname :directory
'(:relative
"output" :wild-inferiors
)
1828 :name
:wild
:type
:wild
)
1830 (setf (logical-pathname-translations "SYS")
1831 `(("SYS:SRC;**;*.*.*" ,src
)
1832 ("SYS:CONTRIB;**;*.*.*" ,contrib
)
1833 ("SYS:OUTPUT;**;*.*.*" ,output
)))))
1835 (defun set-sbcl-source-location (pathname)
1836 "Initialize the SYS logical host based on PATHNAME, which should be
1837 the top-level directory of the SBCL sources. This will replace any
1838 existing translations for \"SYS:SRC;\", \"SYS:CONTRIB;\", and
1839 \"SYS:OUTPUT;\". Other \"SYS:\" translations are preserved."
1840 (let ((truename (truename pathname
))
1841 (current-translations
1842 (remove-if (lambda (translation)
1843 (or (pathname-match-p "SYS:SRC;" translation
)
1844 (pathname-match-p "SYS:CONTRIB;" translation
)
1845 (pathname-match-p "SYS:OUTPUT;" translation
)))
1846 (logical-pathname-translations "SYS")
1848 (flet ((physical-target (component)
1850 (make-pathname :directory
(list :relative component
1855 (setf (logical-pathname-translations "SYS")
1856 `(("SYS:SRC;**;*.*.*" ,(physical-target "src"))
1857 ("SYS:CONTRIB;**;*.*.*" ,(physical-target "contrib"))
1858 ("SYS:OUTPUT;**;*.*.*" ,(physical-target "output"))
1859 ,@current-translations
)))))