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 (defvar *physical-host
*)
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 (defvar *logical-hosts
* (make-hash-table :test
'equal
:synchronized t
))
86 (defmethod make-load-form ((pattern pattern
) &optional environment
)
87 (make-load-form-saving-slots pattern
:environment environment
))
89 (defmethod print-object ((pattern pattern
) stream
)
90 (print-unreadable-object (pattern stream
:type t
)
92 (let ((*print-escape
* t
))
93 (pprint-fill stream
(pattern-pieces pattern
) nil
))
94 (prin1 (pattern-pieces pattern
) stream
))))
96 (defun pattern= (pattern1 pattern2
)
97 (declare (type pattern pattern1 pattern2
))
98 (let ((pieces1 (pattern-pieces pattern1
))
99 (pieces2 (pattern-pieces pattern2
)))
100 (and (= (length pieces1
) (length pieces2
))
101 (every (lambda (piece1 piece2
)
104 (and (simple-string-p piece2
)
105 (string= piece1 piece2
)))
108 (eq (car piece1
) (car piece2
))
109 (string= (cdr piece1
) (cdr piece2
))))
111 (eq piece1 piece2
))))
115 ;;; If the string matches the pattern returns the multiple values T
116 ;;; and a list of the matched strings.
117 (defun pattern-matches (pattern string
)
118 (declare (type pattern pattern
)
119 (type simple-string string
))
120 (let ((len (length string
)))
121 (labels ((maybe-prepend (subs cur-sub chars
)
123 (let* ((len (length chars
))
124 (new (make-string len
))
127 (setf (schar new
(decf index
)) char
))
130 (matches (pieces start subs cur-sub chars
)
133 (values t
(maybe-prepend subs cur-sub chars
))
135 (let ((piece (car pieces
)))
138 (let ((end (+ start
(length piece
))))
140 (string= piece string
141 :start2 start
:end2 end
)
142 (matches (cdr pieces
) end
143 (maybe-prepend subs cur-sub chars
)
149 (let ((char (schar string start
)))
150 (if (find char
(cdr piece
) :test
#'char
=)
151 (matches (cdr pieces
) (1+ start
) subs t
152 (cons char chars
))))))))
153 ((member :single-char-wild
)
155 (matches (cdr pieces
) (1+ start
) subs t
156 (cons (schar string start
) chars
))))
157 ((member :multi-char-wild
)
158 (multiple-value-bind (won new-subs
)
159 (matches (cdr pieces
) start subs t chars
)
163 (matches pieces
(1+ start
) subs t
164 (cons (schar string start
)
166 (multiple-value-bind (won subs
)
167 (matches (pattern-pieces pattern
) 0 nil nil nil
)
168 (values won
(reverse subs
))))))
170 ;;; PATHNAME-MATCH-P for directory components
171 (defun directory-components-match (thing wild
)
174 ;; If THING has a null directory, assume that it matches
175 ;; (:ABSOLUTE :WILD-INFERIORS) or (:RELATIVE :WILD-INFERIORS).
178 (member (first wild
) '(:absolute
:relative
))
179 (eq (second wild
) :wild-inferiors
))
181 (let ((wild1 (first wild
)))
182 (if (eq wild1
:wild-inferiors
)
183 (let ((wild-subdirs (rest wild
)))
184 (or (null wild-subdirs
)
186 (when (directory-components-match thing wild-subdirs
)
189 (unless thing
(return nil
)))))
191 (components-match (first thing
) wild1
)
192 (directory-components-match (rest thing
)
195 ;;; Return true if pathname component THING is matched by WILD. (not
197 (defun components-match (thing wild
)
198 (declare (type (or pattern symbol simple-string integer
) thing wild
))
203 ;; String is matched by itself, a matching pattern or :WILD.
206 (values (pattern-matches wild thing
)))
208 (string= thing wild
))))
210 ;; A pattern is only matched by an identical pattern.
211 (and (pattern-p wild
) (pattern= thing wild
)))
213 ;; An integer (version number) is matched by :WILD or the
214 ;; same integer. This branch will actually always be NIL as
215 ;; long as the version is a fixnum.
218 ;;; a predicate for comparing two pathname slot component sub-entries
219 (defun compare-component (this that
)
223 (and (simple-string-p that
)
224 (string= this that
)))
226 (and (pattern-p that
)
227 (pattern= this that
)))
230 (compare-component (car this
) (car that
))
231 (compare-component (cdr this
) (cdr that
)))))))
233 ;;;; pathname functions
235 (defun pathname= (pathname1 pathname2
)
236 (declare (type pathname pathname1
)
237 (type pathname pathname2
))
238 (or (eq pathname1 pathname2
)
239 (and (eq (%pathname-host pathname1
)
240 (%pathname-host pathname2
))
241 (= (%pathname-dir-hash pathname1
) (%pathname-dir-hash pathname2
))
242 (= (%pathname-stem-hash pathname1
) (%pathname-stem-hash pathname2
))
243 (compare-component (%pathname-device pathname1
)
244 (%pathname-device pathname2
))
245 (compare-component (%pathname-directory pathname1
)
246 (%pathname-directory pathname2
))
247 (compare-component (%pathname-name pathname1
)
248 (%pathname-name pathname2
))
249 (compare-component (%pathname-type pathname1
)
250 (%pathname-type pathname2
))
251 (or (eq (%pathname-host pathname1
) *physical-host
*)
252 (compare-component (%pathname-version pathname1
)
253 (%pathname-version pathname2
))))))
255 ;;; This is conceptually like (DEFUN-CACHED (%MAKE-PATHNAME ...))
256 ;;; except that we try hard never to evict entries until SAVE-LISP-AND-DIE.
257 ;;; Entries can still be kicked out randomly though.
258 ;;; A two-level lookup is used- it works better than mixing all
259 ;;; pathname components into a hash key.
260 (define-load-time-global *pathnames
* (make-array 211 :initial-element nil
))
261 (defglobal *pathnames-lock
* (sb!thread
:make-mutex
:name
"Pathnames"))
263 (defun %make-pathname
(host device directory name type version
)
264 (if (or device
(neq host
*physical-host
*))
265 (%%make-pathname host device directory name type version
)
266 (let* ((table *pathnames
*)
267 (index (rem (pathname-dir-hash directory
) (length table
)))
269 ;; Candidates is a list of ((dir . contents) ...)
271 with candidates
= (svref table index
) and new
= nil
273 (let ((n-candidates 0))
274 (dolist (candidate candidates
)
276 (when (compare-component (car candidate
) directory
)
277 (return-from outer candidate
)))
279 (setq new
(cons directory
(make-array 3 :initial-element nil
))))
280 (cond ((< n-candidates
10)
281 (let* ((cell (cons new candidates
))
282 (actual-old (cas (svref table index
) candidates cell
)))
283 (when (eq actual-old candidates
)
284 (return-from outer new
))
285 (setq candidates actual-old
)))
287 ;; Clobber this cache entry, losing all directories in it.
288 ;; Hopefully this doesn't happen often.
289 #+nil
(format t
"~&*** Pathname cache overflow: ~D ~S~%"
290 index
(mapcar 'car candidates
))
291 (setf (svref table index
) (list new
))
292 (return-from outer new
)))))))
293 (flet ((matchp (stem-hash candidates
)
294 (let ((n-candidates 0))
295 (dolist (pathname candidates
(values nil n-candidates
))
296 (when (and (= (%pathname-stem-hash pathname
) stem-hash
)
297 (compare-component (%pathname-version pathname
) version
)
298 (compare-component (%pathname-name pathname
) name
)
299 (compare-component (%pathname-type pathname
) type
))
300 (return (values pathname
0)))
301 (incf n-candidates
)))))
302 ;; We have tests asserting that the distinction between :NEWEST
303 ;; and NIL is preserved, though there is no effective difference.
304 (binding* ((stem-hash (mix (sxhash name
) (sxhash type
)))
305 (vector (the simple-vector
(cdr dir-holder
)))
306 (index (rem stem-hash
(length vector
)))
307 (candidates (svref vector index
))
308 ((found n-candidates
) (matchp stem-hash candidates
)))
310 (return-from %make-pathname found
))
311 ;; Optimistically assuming that the pathname won't be found
312 ;; on the double-check, allocate it now
313 (let ((pathname (%%make-pathname
*physical-host
* nil
(car dir-holder
)
315 (sb!thread
::with-system-mutex
(*pathnames-lock
*)
316 (when (>= n-candidates
10)
317 ;; Rehash into a larger vector
318 (let* ((old-len (length vector
))
319 (new-len (+ old-len
4))
320 (new-vector (make-array new-len
:initial-element nil
)))
321 (dovector (list vector
)
323 (push p
(svref new-vector
(rem (%pathname-stem-hash p
)
325 (rplacd dir-holder new-vector
)
326 (setq vector new-vector
327 index
(rem stem-hash new-len
)
328 candidates
(svref vector index
))))
329 (let ((found (matchp stem-hash candidates
)))
331 (setq pathname found
)
332 (push pathname
(svref vector index
)))))
335 ;;; Convert PATHNAME-DESIGNATOR (a pathname, or string, or
336 ;;; stream), into a pathname in pathname.
338 ;;; FIXME: was rewritten, should be tested (or rewritten again, this
339 ;;; time using ONCE-ONLY, *then* tested)
340 (eval-when (:compile-toplevel
:execute
)
341 (sb!xc
:defmacro with-pathname
((pathname pathname-designator
) &body body
)
342 (let ((pd0 (gensym)))
343 `(let* ((,pd0
,pathname-designator
)
344 (,pathname
(etypecase ,pd0
346 (string (parse-namestring ,pd0
))
347 (file-stream (file-name ,pd0
)))))
350 (sb!xc
:defmacro with-native-pathname
((pathname pathname-designator
) &body body
)
351 (let ((pd0 (gensym)))
352 `(let* ((,pd0
,pathname-designator
)
353 (,pathname
(etypecase ,pd0
355 (string (parse-native-namestring ,pd0
))
358 (file-stream (file-name ,pd0
)))))
361 (sb!xc
:defmacro with-host
((host host-designator
) &body body
)
362 ;; Generally, redundant specification of information in software,
363 ;; whether in code or in comments, is bad. However, the ANSI spec
364 ;; for this is messy enough that it's hard to hold in short-term
365 ;; memory, so I've recorded these redundant notes on the
366 ;; implications of the ANSI spec.
368 ;; According to the ANSI spec, HOST can be a valid pathname host, or
369 ;; a logical host, or NIL.
371 ;; A valid pathname host can be a valid physical pathname host or a
372 ;; valid logical pathname host.
374 ;; A valid physical pathname host is "any of a string, a list of
375 ;; strings, or the symbol :UNSPECIFIC, that is recognized by the
376 ;; implementation as the name of a host". In SBCL as of 0.6.9.8,
377 ;; that means :UNSPECIFIC: though someday we might want to
378 ;; generalize it to allow strings like "RTFM.MIT.EDU" or lists like
379 ;; '("RTFM" "MIT" "EDU"), that's not supported now.
381 ;; A valid logical pathname host is a string which has been defined as
382 ;; the name of a logical host, as with LOAD-LOGICAL-PATHNAME-TRANSLATIONS.
384 ;; A logical host is an object of implementation-dependent nature. In
385 ;; SBCL, it's a member of the HOST class (a subclass of STRUCTURE-OBJECT).
386 (let ((hd0 (gensym)))
387 `(let* ((,hd0
,host-designator
)
388 (,host
(etypecase ,hd0
390 ;; This is a special host. It's not valid as a
391 ;; logical host, so it is a sensible thing to
392 ;; designate the physical host object. So we do
396 ;; In general ANSI-compliant Common Lisps, a
397 ;; string might also be a physical pathname
398 ;; host, but ANSI leaves this up to the
399 ;; implementor, and in SBCL we don't do it, so
400 ;; it must be a logical host.
401 (find-logical-host ,hd0
))
402 ((or null
(member :unspecific
))
403 ;; CLHS says that HOST=:UNSPECIFIC has
404 ;; implementation-defined behavior. We
405 ;; just turn it into NIL.
408 ;; ANSI also allows LISTs to designate hosts,
409 ;; but leaves its interpretation
410 ;; implementation-defined. Our interpretation
411 ;; is that it's unsupported.:-|
412 (error "A LIST representing a pathname host is not ~
413 supported in this implementation:~% ~S"
419 (defun find-host (host-designator &optional
(errorp t
))
420 (with-host (host host-designator
)
421 (when (and errorp
(not host
))
422 (error "Couldn't find host: ~S" host-designator
))
425 (defun pathname (pathspec)
426 "Convert PATHSPEC (a pathname designator) into a pathname."
427 (declare (type pathname-designator pathspec
))
428 (with-pathname (pathname pathspec
)
431 (defun native-pathname (pathspec)
432 "Convert PATHSPEC (a pathname designator) into a pathname, assuming
433 the operating system native pathname conventions."
434 (with-native-pathname (pathname pathspec
)
437 ;;; Change the case of thing if DIDDLE-P.
438 (defun maybe-diddle-case (thing diddle-p
)
439 (if (and diddle-p
(not (or (symbolp thing
) (integerp thing
))))
440 (labels ((check-for (pred in
)
443 (dolist (piece (pattern-pieces in
))
444 (when (typecase piece
446 (check-for pred piece
))
450 (check-for pred
(cdr piece
))))))
454 (when (check-for pred x
)
457 (dotimes (i (length in
))
458 (when (funcall pred
(schar in i
))
461 (diddle-with (fun thing
)
465 (mapcar (lambda (piece)
473 (funcall fun
(cdr piece
))))
478 (pattern-pieces thing
))))
485 (let ((any-uppers (check-for #'upper-case-p thing
))
486 (any-lowers (check-for #'lower-case-p thing
)))
487 (cond ((and any-uppers any-lowers
)
488 ;; mixed case, stays the same
491 ;; all uppercase, becomes all lower case
492 (diddle-with (lambda (x) (if (stringp x
)
496 ;; all lowercase, becomes all upper case
497 (diddle-with (lambda (x) (if (stringp x
)
501 ;; no letters? I guess just leave it.
505 (defun merge-directories (dir1 dir2 diddle-case
)
506 (if (or (eq (car dir1
) :absolute
)
511 (if (and (eq dir
:back
)
513 (typep (car results
) '(or string pattern
514 (member :wild
:wild-inferiors
))))
516 (push dir results
))))
517 (dolist (dir (maybe-diddle-case dir2 diddle-case
))
519 (dolist (dir (cdr dir1
))
523 (defun merge-pathnames (pathname
525 (defaults *default-pathname-defaults
*)
526 (default-version :newest
))
527 "Construct a filled in pathname by completing the unspecified components
529 (declare (type pathname-designator pathname
)
530 (type pathname-designator defaults
)
532 (with-pathname (defaults defaults
)
533 (let ((pathname (let ((*default-pathname-defaults
* defaults
))
534 (pathname pathname
))))
535 (let* ((default-host (%pathname-host defaults
))
536 (pathname-host (%pathname-host pathname
))
538 (and default-host pathname-host
539 (not (eq (host-customary-case default-host
)
540 (host-customary-case pathname-host
)))))
541 (directory (merge-directories (%pathname-directory pathname
)
542 (%pathname-directory defaults
)
544 (%make-maybe-logical-pathname
545 (or pathname-host default-host
)
546 (and ;; The device of ~/ shouldn't be merged,
547 ;; because the expansion may have a different device
548 (not (and (>= (length directory
) 2)
549 (eql (car directory
) :absolute
)
550 (eql (cadr directory
) :home
)))
551 (or (%pathname-device pathname
)
552 (maybe-diddle-case (%pathname-device defaults
)
555 (or (%pathname-name pathname
)
556 (maybe-diddle-case (%pathname-name defaults
)
558 (or (%pathname-type pathname
)
559 (maybe-diddle-case (%pathname-type defaults
)
561 (or (%pathname-version pathname
)
562 (and (not (%pathname-name pathname
)) (%pathname-version defaults
))
563 default-version
))))))
565 (defun import-directory (directory diddle-case
)
568 ((member :wild
) '(:absolute
:wild-inferiors
))
569 ((member :unspecific
) '(:relative
))
571 (let ((root (pop directory
))
573 (if (member root
'(:relative
:absolute
))
575 (error "List of directory components must start with ~S or ~S."
576 :absolute
:relative
))
578 (let ((next (car directory
)))
579 (when (or (eq :home next
)
580 (typep next
'(cons (eql :home
) (cons string null
))))
581 (push (pop directory
) results
)))
582 (dolist (piece directory
)
584 ((member :wild
:wild-inferiors
:up
)
585 (push piece results
))
587 (if (typep (car results
) '(or string pattern
588 (member :wild
:wild-inferiors
)))
590 (push piece results
)))
592 (when (typep piece
'(and string
(not simple-array
)))
593 (setq piece
(coerce piece
'simple-string
)))
594 ;; Unix namestrings allow embedded "//" within them. Consecutive
595 ;; slashes are treated as one, which is weird but often convenient.
596 ;; However, preserving empty directory components:
598 ;; - makes (NAMESTRING (MAKE-PATHNAME :DIRECTORY '(:RELATIVE "" "d")))
599 ;; visually indistinguishable from the absolute pathname "/d/"
600 ;; - can causes a pathname equality test to return NIL
601 ;; on semantically equivalent pathnames. This can happen for
602 ;; other reasons, but fewer false negatives is better.
603 (unless (and (stringp piece
) (zerop (length piece
)))
604 (push (maybe-diddle-case piece diddle-case
) results
)))
606 (error "~S is not allowed as a directory component." piece
)))))
609 (cond ((zerop (length directory
)) `(:absolute
))
611 (when (typep directory
'(not simple-array
))
612 (setq directory
(coerce directory
'simple-string
)))
613 `(:absolute
,(maybe-diddle-case directory diddle-case
)))))))
615 (defun make-pathname (&key host
620 (version nil versionp
)
623 "Makes a new pathname from the component arguments. Note that host is
624 a host-structure or string."
625 (declare (type (or string host pathname-component-tokens
) host
)
626 (type (or string pathname-component-tokens
) device
)
627 (type (or list string pattern pathname-component-tokens
) directory
)
628 (type (or string pattern pathname-component-tokens
) name type
)
629 (type (or integer pathname-component-tokens
(member :newest
))
631 (type (or pathname-designator null
) defaults
)
632 (type (member :common
:local
) case
))
633 (let* ((defaults (when defaults
634 (with-pathname (defaults defaults
) defaults
)))
635 (default-host (if defaults
636 (%pathname-host defaults
)
637 (pathname-host *default-pathname-defaults
*)))
638 ;; Raymond Toy writes: CLHS says make-pathname can take a
639 ;; string (as a logical-host) for the host part. We map that
640 ;; string into the corresponding logical host structure.
642 ;; Paul Werkowski writes:
643 ;; HyperSpec says for the arg to MAKE-PATHNAME;
644 ;; "host---a valid physical pathname host. ..."
645 ;; where it probably means -- a valid pathname host.
646 ;; "valid pathname host n. a valid physical pathname host or
647 ;; a valid logical pathname host."
649 ;; "valid physical pathname host n. any of a string,
650 ;; a list of strings, or the symbol :unspecific,
651 ;; that is recognized by the implementation as the name of a host."
652 ;; "valid logical pathname host n. a string that has been defined
653 ;; as the name of a logical host. ..."
654 ;; HS is silent on what happens if the :HOST arg is NOT one of these.
655 ;; It seems an error message is appropriate.
656 (host (or (find-host host nil
) default-host
))
657 (diddle-args (and (eq (host-customary-case host
) :lower
)
660 (not (eq (host-customary-case host
)
661 (host-customary-case default-host
))))
662 (dev (if devp device
(if defaults
(%pathname-device defaults
))))
663 (dir (import-directory directory diddle-args
))
666 (defaults (%pathname-version defaults
))
668 (when (and defaults
(not dirp
))
670 (merge-directories dir
671 (%pathname-directory defaults
)
674 (macrolet ((pick (var varp field
)
675 `(cond ((or (simple-string-p ,var
)
677 (maybe-diddle-case ,var diddle-args
))
679 (maybe-diddle-case (coerce ,var
'simple-string
)
682 (maybe-diddle-case ,var diddle-args
))
684 (maybe-diddle-case (,field defaults
)
688 (%make-maybe-logical-pathname host
689 dev
; forced to :UNSPECIFIC when logical
691 (pick name namep %pathname-name
)
692 (pick type typep %pathname-type
)
695 (defun pathname-host (pathname &key
(case :local
))
696 "Return PATHNAME's host."
697 (declare (type pathname-designator pathname
)
698 (type (member :local
:common
) case
)
701 (with-pathname (pathname pathname
)
702 (%pathname-host pathname
)))
704 (defun pathname-device (pathname &key
(case :local
))
705 "Return PATHNAME's device."
706 (declare (type pathname-designator pathname
)
707 (type (member :local
:common
) case
))
708 (with-pathname (pathname pathname
)
709 (maybe-diddle-case (%pathname-device pathname
)
710 (and (eq case
:common
)
711 (eq (host-customary-case
712 (%pathname-host pathname
))
715 (defun pathname-directory (pathname &key
(case :local
))
716 "Return PATHNAME's directory."
717 (declare (type pathname-designator pathname
)
718 (type (member :local
:common
) case
))
719 (with-pathname (pathname pathname
)
720 (maybe-diddle-case (%pathname-directory pathname
)
721 (and (eq case
:common
)
722 (eq (host-customary-case
723 (%pathname-host pathname
))
725 (defun pathname-name (pathname &key
(case :local
))
726 "Return PATHNAME's name."
727 (declare (type pathname-designator pathname
)
728 (type (member :local
:common
) case
))
729 (with-pathname (pathname pathname
)
730 (maybe-diddle-case (%pathname-name pathname
)
731 (and (eq case
:common
)
732 (eq (host-customary-case
733 (%pathname-host pathname
))
736 (defun pathname-type (pathname &key
(case :local
))
737 "Return PATHNAME's type."
738 (declare (type pathname-designator pathname
)
739 (type (member :local
:common
) case
))
740 (with-pathname (pathname pathname
)
741 (maybe-diddle-case (%pathname-type pathname
)
742 (and (eq case
:common
)
743 (eq (host-customary-case
744 (%pathname-host pathname
))
747 (defun pathname-version (pathname)
748 "Return PATHNAME's version."
749 (declare (type pathname-designator pathname
))
750 (with-pathname (pathname pathname
)
751 (%pathname-version pathname
)))
755 ;;; Handle the case for PARSE-NAMESTRING parsing a potentially
756 ;;; syntactically valid logical namestring with an explicit host.
758 ;;; This then isn't fully general -- we are relying on the fact that
759 ;;; we will only pass to parse-namestring namestring with an explicit
760 ;;; logical host, so that we can pass the host return from
761 ;;; parse-logical-namestring through to %PARSE-NAMESTRING as a truth
762 ;;; value. Yeah, this is probably a KLUDGE - CSR, 2002-04-18
763 (defun parseable-logical-namestring-p (namestr start end
)
766 ((namestring-parse-error (lambda (c)
769 (let ((colon (position #\
: namestr
:start start
:end end
)))
771 (let ((potential-host
772 (logical-word-or-lose (subseq namestr start colon
))))
773 ;; depending on the outcome of CSR comp.lang.lisp post
774 ;; "can PARSE-NAMESTRING create logical hosts", we may need
775 ;; to do things with potential-host (create it
776 ;; temporarily, parse the namestring and unintern the
777 ;; logical host potential-host on failure.
778 (declare (ignore potential-host
))
781 ((simple-type-error (lambda (c)
784 (parse-logical-namestring namestr start end
))))
785 ;; if we got this far, we should have an explicit host
786 ;; (first return value of parse-logical-namestring)
790 ;;; Handle the case where PARSE-NAMESTRING is actually parsing a
791 ;;; namestring. We pick off the :JUNK-ALLOWED case then find a host to
792 ;;; use for parsing, call the parser, then check whether the host matches.
793 (defun %parse-namestring
(namestr host defaults start end junk-allowed
)
794 (declare (type (or host null
) host
)
795 (type string namestr
)
797 (type (or index null
) end
))
801 (%parse-namestring namestr host defaults start end nil
)
802 (namestring-parse-error (condition)
803 (values nil
(namestring-parse-error-offset condition
)))))
805 (let* ((end (%check-vector-sequence-bounds namestr start end
)))
806 (multiple-value-bind (new-host device directory file type version
)
807 ;; Comments below are quotes from the HyperSpec
808 ;; PARSE-NAMESTRING entry, reproduced here to demonstrate
809 ;; that we actually have to do things this way rather than
810 ;; some possibly more logical way. - CSR, 2002-04-18
812 ;; "If host is a logical host then thing is parsed as a
813 ;; logical pathname namestring on the host."
814 (host (funcall (host-parse host
) namestr start end
))
815 ;; "If host is nil and thing is a syntactically valid
816 ;; logical pathname namestring containing an explicit
817 ;; host, then it is parsed as a logical pathname
819 ((parseable-logical-namestring-p namestr start end
)
820 (parse-logical-namestring namestr start end
))
821 ;; "If host is nil, default-pathname is a logical
822 ;; pathname, and thing is a syntactically valid logical
823 ;; pathname namestring without an explicit host, then it
824 ;; is parsed as a logical pathname namestring on the
825 ;; host that is the host component of default-pathname."
827 ;; "Otherwise, the parsing of thing is
828 ;; implementation-defined."
830 ;; Both clauses are handled here, as the default
831 ;; *DEFAULT-PATHNAME-DEFAULTS* has a SB-IMPL::UNIX-HOST
833 ((pathname-host defaults
)
834 (funcall (host-parse (pathname-host defaults
))
838 ;; I don't think we should ever get here, as the default
839 ;; host will always have a non-null HOST, given that we
840 ;; can't create a new pathname without going through
841 ;; *DEFAULT-PATHNAME-DEFAULTS*, which has a non-null
843 (t (bug "Fallen through COND in %PARSE-NAMESTRING")))
844 (when (and host new-host
(not (eq new-host host
)))
845 (error 'simple-type-error
847 ;; Note: ANSI requires that this be a TYPE-ERROR,
848 ;; but there seems to be no completely correct
849 ;; value to use for TYPE-ERROR-EXPECTED-TYPE.
850 ;; Instead, we return a sort of "type error allowed
851 ;; type", trying to say "it would be OK if you
852 ;; passed NIL as the host value" but not mentioning
853 ;; that a matching string would be OK too.
856 "The host in the namestring, ~S,~@
857 does not match the explicit HOST argument, ~S."
858 :format-arguments
(list new-host host
)))
859 (let ((pn-host (or new-host host
(pathname-host defaults
))))
860 (values (%make-maybe-logical-pathname
861 pn-host device directory file type version
)
864 ;;; If NAMESTR begins with a colon-terminated, defined, logical host,
865 ;;; then return that host, otherwise return NIL.
866 (defun extract-logical-host-prefix (namestr start end
)
867 (declare (type simple-string namestr
)
868 (type index start end
)
869 (values (or logical-host null
)))
870 (let ((colon-pos (position #\
: namestr
:start start
:end end
)))
872 (values (gethash (nstring-upcase (subseq namestr start colon-pos
))
876 (defun parse-namestring (thing
879 (defaults *default-pathname-defaults
*)
880 &key
(start 0) end junk-allowed
)
881 (declare (ftype (function * (values (or null pathname
) (or null index
)))
883 (with-host (found-host host
)
884 (let (;; According to ANSI defaults may be any valid pathname designator
885 (defaults (etypecase defaults
889 (aver (pathnamep *default-pathname-defaults
*))
890 (parse-namestring defaults
))
892 (truename defaults
)))))
893 (declare (type pathname defaults
))
896 (with-array-data ((thing thing
) (start start
) (end end
)
897 :check-fill-pointer t
)
898 (multiple-value-bind (pathname position
)
899 (%parse-namestring thing found-host defaults start end junk-allowed
)
900 (values pathname
(- position start
)))))
902 (let ((defaulted-host (or found-host
(%pathname-host defaults
))))
903 (declare (type host defaulted-host
))
904 (unless (eq defaulted-host
(%pathname-host thing
))
905 (error "The HOST argument doesn't match the pathname host:~% ~
907 defaulted-host
(%pathname-host thing
))))
908 (values thing start
))
910 (let ((name (file-name thing
)))
912 (error "can't figure out the file associated with stream:~% ~S"
914 (values name nil
)))))))
916 (defun %parse-native-namestring
(namestr host defaults start end junk-allowed
918 (declare (type (or host null
) host
)
919 (type string namestr
)
921 (type (or index null
) end
))
925 (%parse-native-namestring namestr host defaults start end nil as-directory
)
926 (namestring-parse-error (condition)
927 (values nil
(namestring-parse-error-offset condition
)))))
929 (let* ((end (%check-vector-sequence-bounds namestr start end
)))
930 (multiple-value-bind (new-host device directory file type version
)
933 (funcall (host-parse-native host
) namestr start end as-directory
))
934 ((pathname-host defaults
)
935 (funcall (host-parse-native (pathname-host defaults
))
940 ;; I don't think we should ever get here, as the default
941 ;; host will always have a non-null HOST, given that we
942 ;; can't create a new pathname without going through
943 ;; *DEFAULT-PATHNAME-DEFAULTS*, which has a non-null
945 (t (bug "Fallen through COND in %PARSE-NAMESTRING")))
946 (when (and host new-host
(not (eq new-host host
)))
947 (error 'simple-type-error
949 :expected-type
`(or null
(eql ,host
))
951 "The host in the namestring, ~S,~@
952 does not match the explicit HOST argument, ~S."
953 :format-arguments
(list new-host host
)))
954 (let ((pn-host (or new-host host
(pathname-host defaults
))))
955 (values (%make-pathname
956 pn-host device directory file type version
)
959 (defun parse-native-namestring (thing
962 (defaults *default-pathname-defaults
*)
963 &key
(start 0) end junk-allowed
965 "Convert THING into a pathname, using the native conventions
966 appropriate for the pathname host HOST, or if not specified the
967 host of DEFAULTS. If THING is a string, the parse is bounded by
968 START and END, and error behaviour is controlled by JUNK-ALLOWED,
969 as with PARSE-NAMESTRING. For file systems whose native
970 conventions allow directories to be indicated as files, if
971 AS-DIRECTORY is true, return a pathname denoting THING as a
973 (declare (type pathname-designator thing defaults
)
974 (type (or list host string
(member :unspecific
)) host
)
976 (type (or index null
) end
)
977 (type (or t null
) junk-allowed
)
978 (values (or null pathname
) (or null index
)))
979 (declare (ftype (function * (values (or null pathname
) (or null index
)))
980 %parse-native-namestring
))
981 (with-host (found-host host
)
982 (let ((defaults (etypecase defaults
986 (aver (pathnamep *default-pathname-defaults
*))
987 (parse-native-namestring defaults
))
989 (truename defaults
)))))
990 (declare (type pathname defaults
))
993 (with-array-data ((thing thing
) (start start
) (end end
)
994 :check-fill-pointer t
)
995 (multiple-value-bind (pathname position
)
996 (%parse-native-namestring thing
997 found-host defaults start end junk-allowed
999 (values pathname
(- position start
)))))
1001 (let ((defaulted-host (or found-host
(%pathname-host defaults
))))
1002 (declare (type host defaulted-host
))
1003 (unless (eq defaulted-host
(%pathname-host thing
))
1004 (error "The HOST argument doesn't match the pathname host:~% ~
1006 defaulted-host
(%pathname-host thing
))))
1007 (values thing start
))
1010 (let ((name (file-name thing
)))
1012 (error "can't figure out the file associated with stream:~% ~S"
1014 (values name nil
)))))))
1016 (defun namestring (pathname)
1017 "Construct the full (name)string form of the pathname."
1018 (declare (type pathname-designator pathname
))
1019 (with-pathname (pathname pathname
)
1021 (or (%pathname-namestring pathname
)
1022 (let ((host (%pathname-host pathname
)))
1025 "can't determine the namestring for pathnames with no host:~% ~S"
1027 (setf (%pathname-namestring pathname
)
1028 (logically-readonlyize
1029 (possibly-base-stringize
1030 (funcall (host-unparse host
) pathname
))))))))))
1032 (defun native-namestring (pathname &key as-file
)
1033 "Construct the full native (name)string form of PATHNAME. For
1034 file systems whose native conventions allow directories to be
1035 indicated as files, if AS-FILE is true and the name, type, and
1036 version components of PATHNAME are all NIL or :UNSPECIFIC,
1037 construct a string that names the directory according to the file
1038 system's syntax for files."
1039 (declare (type pathname-designator pathname
))
1040 (with-native-pathname (pathname pathname
)
1042 (let ((host (%pathname-host pathname
)))
1044 (error "can't determine the native namestring for pathnames with no ~
1045 host:~% ~S" pathname
))
1046 (funcall (host-unparse-native host
) pathname as-file
)))))
1048 (defun host-namestring (pathname)
1049 "Return a string representation of the name of the host in the pathname."
1050 (declare (type pathname-designator pathname
))
1051 (with-pathname (pathname pathname
)
1052 (let ((host (%pathname-host pathname
)))
1054 (funcall (host-unparse-host host
) pathname
)
1056 "can't determine the namestring for pathnames with no host:~% ~S"
1059 (defun directory-namestring (pathname)
1060 "Return a string representation of the directories used in the pathname."
1061 (declare (type pathname-designator pathname
))
1062 (with-pathname (pathname pathname
)
1063 (let ((host (%pathname-host pathname
)))
1065 (funcall (host-unparse-directory host
) pathname
)
1067 "can't determine the namestring for pathnames with no host:~% ~S"
1070 (defun file-namestring (pathname)
1071 "Return a string representation of the name used in the pathname."
1072 (declare (type pathname-designator pathname
))
1073 (with-pathname (pathname pathname
)
1074 (let ((host (%pathname-host pathname
)))
1076 (funcall (host-unparse-file host
) pathname
)
1078 "can't determine the namestring for pathnames with no host:~% ~S"
1081 (defun enough-namestring (pathname
1083 (defaults *default-pathname-defaults
*))
1084 "Return an abbreviated pathname sufficient to identify the pathname relative
1086 (declare (type pathname-designator pathname
))
1087 (with-pathname (pathname pathname
)
1088 (let ((host (%pathname-host pathname
)))
1090 (with-pathname (defaults defaults
)
1091 (funcall (host-unparse-enough host
) pathname defaults
))
1093 "can't determine the namestring for pathnames with no host:~% ~S"
1098 (defun wild-pathname-p (pathname &optional field-key
)
1099 "Predicate for determining whether pathname contains any wildcards."
1100 (declare (type pathname-designator pathname
)
1101 (type (member nil
:host
:device
:directory
:name
:type
:version
)
1103 (with-pathname (pathname pathname
)
1105 (or (pattern-p x
) (member x
'(:wild
:wild-inferiors
)))))
1108 (or (wild-pathname-p pathname
:host
)
1109 (wild-pathname-p pathname
:device
)
1110 (wild-pathname-p pathname
:directory
)
1111 (wild-pathname-p pathname
:name
)
1112 (wild-pathname-p pathname
:type
)
1113 (wild-pathname-p pathname
:version
)))
1114 (:host
(frob (%pathname-host pathname
)))
1115 (:device
(frob (%pathname-host pathname
)))
1116 (:directory
(some #'frob
(%pathname-directory pathname
)))
1117 (:name
(frob (%pathname-name pathname
)))
1118 (:type
(frob (%pathname-type pathname
)))
1119 (:version
(frob (%pathname-version pathname
)))))))
1121 (defun pathname-match-p (in-pathname in-wildname
)
1122 "Pathname matches the wildname template?"
1123 (declare (type pathname-designator in-pathname
))
1124 (with-pathname (pathname in-pathname
)
1125 (with-pathname (wildname in-wildname
)
1126 (macrolet ((frob (field &optional
(op 'components-match
))
1127 `(or (null (,field wildname
))
1128 (,op
(,field pathname
) (,field wildname
)))))
1129 (and (or (null (%pathname-host wildname
))
1130 (eq (%pathname-host wildname
) (%pathname-host pathname
)))
1131 (frob %pathname-device
)
1132 (frob %pathname-directory directory-components-match
)
1133 (frob %pathname-name
)
1134 (frob %pathname-type
)
1135 (or (eq (%pathname-host wildname
) *physical-host
*)
1136 (frob %pathname-version
)))))))
1138 ;;; Place the substitutions into the pattern and return the string or pattern
1139 ;;; that results. If DIDDLE-CASE is true, we diddle the result case as well,
1140 ;;; in case we are translating between hosts with difference conventional case.
1141 ;;; The second value is the tail of subs with all of the values that we used up
1142 ;;; stripped off. Note that PATTERN-MATCHES matches all consecutive wildcards
1143 ;;; as a single string, so we ignore subsequent contiguous wildcards.
1144 (defun substitute-into (pattern subs diddle-case
)
1145 (declare (type pattern pattern
)
1147 (values (or simple-string pattern
) list
))
1148 (let ((in-wildcard nil
)
1151 (dolist (piece (pattern-pieces pattern
))
1152 (cond ((simple-string-p piece
)
1153 (push piece strings
)
1154 (setf in-wildcard nil
))
1157 (setf in-wildcard t
)
1159 (error "not enough wildcards in FROM pattern to match ~
1162 (let ((sub (pop subs
)))
1166 (push (apply #'concatenate
'simple-string
1169 (dolist (piece (pattern-pieces sub
))
1170 (push piece pieces
)))
1174 (error "can't substitute this into the middle of a word:~
1179 (push (apply #'concatenate
'simple-string
(nreverse strings
))
1183 (if (and pieces
(simple-string-p (car pieces
)) (null (cdr pieces
)))
1185 (make-pattern (nreverse pieces
)))
1189 ;;; Called when we can't see how source and from matched.
1190 (defun didnt-match-error (source from
)
1191 (error "Pathname components from SOURCE and FROM args to TRANSLATE-PATHNAME~@
1192 did not match:~% ~S ~S"
1195 ;;; Do TRANSLATE-COMPONENT for all components except host, directory
1197 (defun translate-component (source from to diddle-case
)
1204 (if (pattern= from source
)
1206 (didnt-match-error source from
)))
1208 (multiple-value-bind (won subs
) (pattern-matches from source
)
1210 (values (substitute-into to subs diddle-case
))
1211 (didnt-match-error source from
))))
1213 (maybe-diddle-case source diddle-case
))))
1215 (values (substitute-into to
(list source
) diddle-case
)))
1217 (if (components-match source from
)
1218 (maybe-diddle-case source diddle-case
)
1219 (didnt-match-error source from
)))))
1221 (maybe-diddle-case source diddle-case
))
1223 (if (components-match source from
)
1225 (didnt-match-error source from
)))))
1227 ;;; Return a list of all the things that we want to substitute into the TO
1228 ;;; pattern (the things matched by from on source.) When From contains
1229 ;;; :WILD-INFERIORS, the result contains a sublist of the matched source
1231 (defun compute-directory-substitutions (orig-source orig-from
)
1232 (let ((source orig-source
)
1237 (unless (every (lambda (x) (eq x
:wild-inferiors
)) from
)
1238 (didnt-match-error orig-source orig-from
))
1241 (unless from
(didnt-match-error orig-source orig-from
))
1242 (let ((from-part (pop from
))
1243 (source-part (pop source
)))
1246 (typecase source-part
1248 (if (pattern= from-part source-part
)
1250 (didnt-match-error orig-source orig-from
)))
1252 (multiple-value-bind (won new-subs
)
1253 (pattern-matches from-part source-part
)
1255 (dolist (sub new-subs
)
1257 (didnt-match-error orig-source orig-from
))))
1259 (didnt-match-error orig-source orig-from
))))
1262 ((member :wild-inferiors
)
1263 (let ((remaining-source (cons source-part source
)))
1266 (when (directory-components-match remaining-source from
)
1268 (unless remaining-source
1269 (didnt-match-error orig-source orig-from
))
1270 (res (pop remaining-source
)))
1272 (setq source remaining-source
))))
1274 (unless (and (simple-string-p source-part
)
1275 (string= from-part source-part
))
1276 (didnt-match-error orig-source orig-from
)))
1278 (didnt-match-error orig-source orig-from
)))))
1281 ;;; This is called by TRANSLATE-PATHNAME on the directory components
1282 ;;; of its argument pathnames to produce the result directory
1283 ;;; component. If this leaves the directory NIL, we return the source
1284 ;;; directory. The :RELATIVE or :ABSOLUTE is taken from the source
1285 ;;; directory, except if TO is :ABSOLUTE, in which case the result
1286 ;;; will be :ABSOLUTE.
1287 (defun translate-directories (source from to diddle-case
)
1288 (if (not (and source to from
))
1289 (or (and to
(null source
) (remove :wild-inferiors to
))
1290 (mapcar (lambda (x) (maybe-diddle-case x diddle-case
)) source
))
1292 ;; If TO is :ABSOLUTE, the result should still be :ABSOLUTE.
1293 (res (if (eq (first to
) :absolute
)
1296 (let ((subs-left (compute-directory-substitutions (rest source
)
1298 (dolist (to-part (rest to
))
1302 (let ((match (pop subs-left
)))
1304 (error ":WILD-INFERIORS is not paired in from and to ~
1305 patterns:~% ~S ~S" from to
))
1306 (res (maybe-diddle-case match diddle-case
))))
1307 ((member :wild-inferiors
)
1309 (let ((match (pop subs-left
)))
1310 (unless (listp match
)
1311 (error ":WILD-INFERIORS not paired in from and to ~
1312 patterns:~% ~S ~S" from to
))
1314 (res (maybe-diddle-case x diddle-case
)))))
1316 (multiple-value-bind
1318 (substitute-into to-part subs-left diddle-case
)
1319 (setf subs-left new-subs-left
)
1321 (t (res to-part
)))))
1324 (defun translate-pathname (source from-wildname to-wildname
&key
)
1325 "Use the source pathname to translate the from-wildname's wild and
1326 unspecified elements into a completed to-pathname based on the to-wildname."
1327 (declare (type pathname-designator source from-wildname to-wildname
))
1328 (with-pathname (source source
)
1329 (with-pathname (from from-wildname
)
1330 (with-pathname (to to-wildname
)
1331 (let* ((source-host (%pathname-host source
))
1332 (from-host (%pathname-host from
))
1333 (to-host (%pathname-host to
))
1335 (and source-host to-host
1336 (not (eq (host-customary-case source-host
)
1337 (host-customary-case to-host
))))))
1338 (macrolet ((frob (field &optional
(op 'translate-component
))
1339 `(let ((result (,op
(,field source
)
1343 (if (eq result
:error
)
1344 (error "~S doesn't match ~S." source from
)
1346 (%make-maybe-logical-pathname
1347 (or to-host source-host
)
1348 (frob %pathname-device
)
1349 (frob %pathname-directory translate-directories
)
1350 (frob %pathname-name
)
1351 (frob %pathname-type
)
1352 (if (eq from-host
*physical-host
*)
1353 (if (or (eq (%pathname-version to
) :wild
)
1354 (eq (%pathname-version to
) nil
))
1355 (%pathname-version source
)
1356 (%pathname-version to
))
1357 (frob %pathname-version
)))))))))
1359 ;;;; logical pathname support. ANSI 92-102 specification.
1361 ;;;; As logical-pathname translations are loaded they are
1362 ;;;; canonicalized as patterns to enable rapid efficient translation
1363 ;;;; into physical pathnames.
1367 (defun simplify-namestring (namestring &optional host
)
1368 (funcall (host-simplify-namestring
1370 (pathname-host (sane-default-pathname-defaults))))
1373 ;;; Canonicalize a logical pathname word by uppercasing it checking that it
1374 ;;; contains only legal characters.
1375 (defun logical-word-or-lose (word)
1376 (declare (string word
))
1377 (when (string= word
"")
1378 (error 'namestring-parse-error
1379 :complaint
"Attempted to treat invalid logical hostname ~
1380 as a logical host:~% ~S"
1382 :namestring word
:offset
0))
1383 (let ((word (string-upcase word
)))
1384 (dotimes (i (length word
))
1385 (let ((ch (schar word i
)))
1386 (unless (and (typep ch
'standard-char
)
1387 (or (alpha-char-p ch
) (digit-char-p ch
) (char= ch
#\-
)))
1388 (error 'namestring-parse-error
1389 :complaint
"logical namestring character which ~
1390 is not alphanumeric or hyphen:~% ~S"
1392 :namestring word
:offset i
))))
1393 (coerce word
'string
))) ; why not simple-string?
1395 ;;; Given a logical host or string, return a logical host. If ERROR-P
1396 ;;; is NIL, then return NIL when no such host exists.
1397 (defun find-logical-host (thing &optional
(errorp t
))
1400 (let ((found (gethash (logical-word-or-lose thing
)
1402 (if (or found
(not errorp
))
1404 ;; This is the error signalled from e.g.
1405 ;; LOGICAL-PATHNAME-TRANSLATIONS when host is not a defined
1406 ;; host, and ANSI specifies that that's a TYPE-ERROR.
1407 (error 'simple-type-error
1409 ;; God only knows what ANSI expects us to use for
1410 ;; the EXPECTED-TYPE here. Maybe this will be OK..
1412 '(and string
(satisfies logical-pathname-translations
))
1413 :format-control
"logical host not yet defined: ~S"
1414 :format-arguments
(list thing
)))))
1415 (logical-host thing
)))
1417 ;;; Given a logical host name or host, return a logical host, creating
1418 ;;; a new one if necessary.
1419 (defun intern-logical-host (thing)
1420 (with-locked-system-table (*logical-hosts
*)
1421 (or (find-logical-host thing nil
)
1422 (let* ((name (logical-word-or-lose thing
))
1423 (new (make-logical-host :name name
)))
1424 (setf (gethash name
*logical-hosts
*) new
)
1427 ;;;; logical pathname parsing
1429 ;;; Deal with multi-char wildcards in a logical pathname token.
1430 (defun maybe-make-logical-pattern (namestring chunks
)
1431 (let ((chunk (caar chunks
)))
1432 (collect ((pattern))
1434 (len (length chunk
)))
1435 (declare (fixnum last-pos
))
1437 (when (= last-pos len
) (return))
1438 (let ((pos (or (position #\
* chunk
:start last-pos
) len
)))
1439 (if (= pos last-pos
)
1441 (error 'namestring-parse-error
1442 :complaint
"double asterisk inside of logical ~
1445 :namestring namestring
1446 :offset
(+ (cdar chunks
) pos
)))
1447 (pattern (subseq chunk last-pos pos
)))
1450 (pattern :multi-char-wild
))
1451 (setq last-pos
(1+ pos
)))))
1454 (make-pattern (pattern))
1455 (let ((x (car (pattern))))
1456 (if (eq x
:multi-char-wild
)
1460 ;;; Return a list of conses where the CDR is the start position and
1461 ;;; the CAR is a string (token) or character (punctuation.)
1462 (defun logical-chunkify (namestr start end
)
1464 (do ((i start
(1+ i
))
1468 (chunks (cons (nstring-upcase (subseq namestr prev end
)) prev
))))
1469 (let ((ch (schar namestr i
)))
1470 (unless (or (alpha-char-p ch
) (digit-char-p ch
)
1471 (member ch
'(#\-
#\
*)))
1473 (chunks (cons (nstring-upcase (subseq namestr prev i
)) prev
)))
1475 (unless (member ch
'(#\
; #\: #\.))
1476 (error 'namestring-parse-error
1477 :complaint
"illegal character for logical pathname:~% ~S"
1481 (chunks (cons ch i
)))))
1484 ;;; Break up a logical-namestring, always a string, into its
1485 ;;; constituent parts.
1486 (defun parse-logical-namestring (namestr start end
)
1487 (declare (type simple-string namestr
)
1488 (type index start end
))
1489 (collect ((directory))
1494 (labels ((expecting (what chunks
)
1495 (unless (and chunks
(simple-string-p (caar chunks
)))
1496 (error 'namestring-parse-error
1497 :complaint
"expecting ~A, got ~:[nothing~;~S~]."
1498 :args
(list what
(caar chunks
) (caar chunks
))
1500 :offset
(if chunks
(cdar chunks
) end
)))
1502 (parse-host (chunks)
1503 (case (caadr chunks
)
1506 (find-logical-host (expecting "a host name" chunks
)))
1507 (parse-relative (cddr chunks
)))
1509 (parse-relative chunks
))))
1510 (parse-relative (chunks)
1513 (directory :relative
)
1514 (parse-directory (cdr chunks
)))
1516 (directory :absolute
) ; Assumption! Maybe revoked later.
1517 (parse-directory chunks
))))
1518 (parse-directory (chunks)
1519 (case (caadr chunks
)
1522 (let ((res (expecting "a directory name" chunks
)))
1523 (cond ((string= res
"..") :up
)
1524 ((string= res
"**") :wild-inferiors
)
1526 (maybe-make-logical-pattern namestr chunks
)))))
1527 (parse-directory (cddr chunks
)))
1529 (parse-name chunks
))))
1530 (parse-name (chunks)
1532 (expecting "a file name" chunks
)
1533 (setq name
(maybe-make-logical-pattern namestr chunks
))
1534 (expecting-dot (cdr chunks
))))
1535 (expecting-dot (chunks)
1537 (unless (eql (caar chunks
) #\.
)
1538 (error 'namestring-parse-error
1539 :complaint
"expecting a dot, got ~S."
1540 :args
(list (caar chunks
))
1542 :offset
(cdar chunks
)))
1544 (parse-version (cdr chunks
))
1545 (parse-type (cdr chunks
)))))
1546 (parse-type (chunks)
1547 (expecting "a file type" chunks
)
1548 (setq type
(maybe-make-logical-pattern namestr chunks
))
1549 (expecting-dot (cdr chunks
)))
1550 (parse-version (chunks)
1551 (let ((str (expecting "a positive integer, * or NEWEST"
1554 ((string= str
"*") (setq version
:wild
))
1555 ((string= str
"NEWEST") (setq version
:newest
))
1557 (multiple-value-bind (res pos
)
1558 (parse-integer str
:junk-allowed t
)
1559 (unless (and res
(plusp res
))
1560 (error 'namestring-parse-error
1561 :complaint
"expected a positive integer, ~
1565 :offset
(+ pos
(cdar chunks
))))
1566 (setq version res
)))))
1568 (error 'namestring-parse-error
1569 :complaint
"extra stuff after end of file name"
1571 :offset
(cdadr chunks
)))))
1572 (parse-host (logical-chunkify namestr start end
)))
1573 (values host
:unspecific
(directory) name type version
))))
1575 ;;; We can't initialize this yet because not all host methods are
1577 (defvar *logical-pathname-defaults
*)
1579 (defun logical-namestring-p (x)
1582 (typep (pathname x
) 'logical-pathname
))))
1584 (deftype logical-namestring
()
1585 `(satisfies logical-namestring-p
))
1587 (defun logical-pathname (pathspec)
1588 "Converts the pathspec argument to a logical-pathname and returns it."
1589 (declare (type (or logical-pathname string stream
) pathspec
)
1590 (values logical-pathname
))
1591 (if (typep pathspec
'logical-pathname
)
1593 (flet ((oops (problem)
1594 (error 'simple-type-error
1596 :expected-type
'logical-namestring
1597 :format-control
"~S is not a valid logical namestring:~% ~A"
1598 :format-arguments
(list pathspec problem
))))
1599 (let ((res (handler-case
1600 (parse-namestring pathspec nil
*logical-pathname-defaults
*)
1601 (error (e) (oops e
)))))
1602 (when (eq (%pathname-host res
)
1603 (%pathname-host
*logical-pathname-defaults
*))
1604 (oops "no host specified"))
1607 ;;;; logical pathname unparsing
1609 (defun unparse-logical-directory (pathname)
1610 (declare (type pathname pathname
))
1612 (let ((directory (%pathname-directory pathname
)))
1614 (ecase (pop directory
)
1615 (:absolute
) ; nothing special
1616 (:relative
(pieces ";")))
1617 (dolist (dir directory
)
1618 (cond ((or (stringp dir
) (pattern-p dir
))
1619 (pieces (unparse-logical-piece dir
))
1623 ((eq dir
:wild-inferiors
)
1626 (error "invalid directory component: ~S" dir
))))))
1627 (apply #'concatenate
'simple-string
(pieces))))
1629 (defun unparse-logical-piece (thing)
1631 ((member :wild
) "*")
1632 (simple-string thing
)
1634 (collect ((strings))
1635 (dolist (piece (pattern-pieces thing
))
1637 (simple-string (strings piece
))
1639 (cond ((eq piece
:wild-inferiors
)
1641 ((eq piece
:multi-char-wild
)
1643 (t (error "invalid keyword: ~S" piece
))))))
1644 (apply #'concatenate
'simple-string
(strings))))))
1646 (defun unparse-logical-file (pathname)
1647 (declare (type pathname pathname
))
1648 (collect ((strings))
1649 (let* ((name (%pathname-name pathname
))
1650 (type (%pathname-type pathname
))
1651 (version (%pathname-version pathname
))
1652 (type-supplied (not (or (null type
) (eq type
:unspecific
))))
1653 (version-supplied (not (or (null version
)
1654 (eq version
:unspecific
)))))
1656 (when (and (null type
)
1657 (typep name
'string
)
1658 (position #\. name
:start
1))
1659 (error "too many dots in the name: ~S" pathname
))
1660 (strings (unparse-logical-piece name
)))
1663 (error "cannot specify the type without a file: ~S" pathname
))
1664 (when (typep type
'string
)
1665 (when (position #\. type
)
1666 (error "type component can't have a #\. inside: ~S" pathname
)))
1668 (strings (unparse-logical-piece type
)))
1669 (when version-supplied
1670 (unless type-supplied
1671 (error "cannot specify the version without a type: ~S" pathname
))
1673 ((member :newest
) (strings ".NEWEST"))
1674 ((member :wild
) (strings ".*"))
1675 (fixnum (strings ".") (strings (format nil
"~D" version
))))))
1676 (apply #'concatenate
'simple-string
(strings))))
1678 ;;; Unparse a logical pathname string.
1679 (defun unparse-enough-namestring (pathname defaults
)
1680 (let* ((path-directory (pathname-directory pathname
))
1681 (def-directory (pathname-directory defaults
))
1683 ;; Go down the directory lists to see what matches. What's
1684 ;; left is what we want, more or less.
1685 (cond ((and (eq (first path-directory
) (first def-directory
))
1686 (eq (first path-directory
) :absolute
))
1687 ;; Both paths are :ABSOLUTE, so find where the
1688 ;; common parts end and return what's left
1689 (do* ((p (rest path-directory
) (rest p
))
1690 (d (rest def-directory
) (rest d
)))
1691 ((or (endp p
) (endp d
)
1692 (not (equal (first p
) (first d
))))
1695 ;; At least one path is :RELATIVE, so just return the
1696 ;; original path. If the original path is :RELATIVE,
1697 ;; then that's the right one. If PATH-DIRECTORY is
1698 ;; :ABSOLUTE, we want to return that except when
1699 ;; DEF-DIRECTORY is :ABSOLUTE, as handled above. so return
1700 ;; the original directory.
1702 (unparse-logical-namestring
1703 (make-pathname :host
(pathname-host pathname
)
1704 :directory enough-directory
1705 :name
(pathname-name pathname
)
1706 :type
(pathname-type pathname
)
1707 :version
(pathname-version pathname
)))))
1709 (defun unparse-logical-namestring (pathname)
1710 (declare (type logical-pathname pathname
))
1711 (concatenate 'simple-string
1712 (logical-host-name (%pathname-host pathname
)) ":"
1713 (unparse-logical-directory pathname
)
1714 (unparse-logical-file pathname
)))
1716 ;;;; logical pathname translations
1718 ;;; Verify that the list of translations consists of lists and prepare
1719 ;;; canonical translations. (Parse pathnames and expand out wildcards
1721 (defun canonicalize-logical-pathname-translations (translation-list host
)
1722 (declare (type list translation-list
) (type host host
)
1724 (mapcar (lambda (translation)
1725 (destructuring-bind (from to
) translation
1726 (list (if (typep from
'logical-pathname
)
1728 (parse-namestring from host
))
1732 (defun logical-pathname-translations (host)
1733 "Return the (logical) host object argument's list of translations."
1734 (declare (type (or string logical-host
) host
)
1736 (logical-host-translations (find-logical-host host
)))
1738 (defun (setf logical-pathname-translations
) (translations host
)
1739 "Set the translations list for the logical host argument."
1740 (declare (type (or string logical-host
) host
)
1741 (type list translations
)
1743 (let ((host (intern-logical-host host
)))
1744 (setf (logical-host-canon-transls host
)
1745 (canonicalize-logical-pathname-translations translations host
))
1746 (setf (logical-host-translations host
) translations
)))
1748 (defun translate-logical-pathname (pathname &key
)
1749 "Translate PATHNAME to a physical pathname, which is returned."
1750 (declare (type pathname-designator pathname
)
1751 (values (or null pathname
)))
1754 (dolist (x (logical-host-canon-transls (%pathname-host pathname
))
1755 (error 'simple-file-error
1757 :format-control
"no translation for ~S"
1758 :format-arguments
(list pathname
)))
1759 (destructuring-bind (from to
) x
1760 (when (pathname-match-p pathname from
)
1761 (return (translate-logical-pathname
1762 (translate-pathname pathname from to
)))))))
1764 (t (translate-logical-pathname (pathname pathname
)))))
1766 (defvar *logical-pathname-defaults
*
1767 (%make-logical-pathname
1768 (make-logical-host :name
(logical-word-or-lose "BOGUS"))
1769 :unspecific nil nil nil nil
))
1771 (defun load-logical-pathname-translations (host)
1772 "Reads logical pathname translations from SYS:SITE;HOST.TRANSLATIONS.NEWEST,
1773 with HOST replaced by the supplied parameter. Returns T on success.
1775 If HOST is already defined as logical pathname host, no file is loaded and NIL
1778 The file should contain a single form, suitable for use with
1779 \(SETF LOGICAL-PATHNAME-TRANSLATIONS).
1781 Note: behaviour of this function is highly implementation dependent, and
1782 historically it used to be a no-op in SBCL -- the current approach is somewhat
1783 experimental and subject to change."
1784 (declare (type string host
)
1785 (values (member t nil
)))
1786 (if (find-logical-host host nil
)
1787 ;; This host is already defined, all is well and good.
1789 ;; ANSI: "The specific nature of the search is
1790 ;; implementation-defined."
1792 (setf (logical-pathname-translations host
)
1793 (with-open-file (lpt (make-pathname :host
"SYS"
1794 :directory
'(:absolute
"SITE")
1796 :type
"TRANSLATIONS"
1800 (defun !pathname-cold-init
()
1801 (let* ((sys *default-pathname-defaults
*)
1804 (make-pathname :directory
'(:relative
"src" :wild-inferiors
)
1805 :name
:wild
:type
:wild
)
1809 (make-pathname :directory
'(:relative
"contrib" :wild-inferiors
)
1810 :name
:wild
:type
:wild
)
1814 (make-pathname :directory
'(:relative
"output" :wild-inferiors
)
1815 :name
:wild
:type
:wild
)
1817 (setf (logical-pathname-translations "SYS")
1818 `(("SYS:SRC;**;*.*.*" ,src
)
1819 ("SYS:CONTRIB;**;*.*.*" ,contrib
)
1820 ("SYS:OUTPUT;**;*.*.*" ,output
)))))
1822 (defun set-sbcl-source-location (pathname)
1823 "Initialize the SYS logical host based on PATHNAME, which should be
1824 the top-level directory of the SBCL sources. This will replace any
1825 existing translations for \"SYS:SRC;\", \"SYS:CONTRIB;\", and
1826 \"SYS:OUTPUT;\". Other \"SYS:\" translations are preserved."
1827 (let ((truename (truename pathname
))
1828 (current-translations
1829 (remove-if (lambda (translation)
1830 (or (pathname-match-p "SYS:SRC;" translation
)
1831 (pathname-match-p "SYS:CONTRIB;" translation
)
1832 (pathname-match-p "SYS:OUTPUT;" translation
)))
1833 (logical-pathname-translations "SYS")
1835 (flet ((physical-target (component)
1837 (make-pathname :directory
(list :relative component
1842 (setf (logical-pathname-translations "SYS")
1843 `(("SYS:SRC;**;*.*.*" ,(physical-target "src"))
1844 ("SYS:CONTRIB;**;*.*.*" ,(physical-target "contrib"))
1845 ("SYS:OUTPUT;**;*.*.*" ,(physical-target "output"))
1846 ,@current-translations
)))))