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 ;;;; PHYSICAL-HOST stuff
18 (def!struct
(unix-host
19 (:make-load-form-fun make-unix-host-load-form
)
21 (parse #'parse-unix-namestring
)
22 (parse-native #'parse-native-unix-namestring
)
23 (unparse #'unparse-unix-namestring
)
24 (unparse-native #'unparse-native-unix-namestring
)
25 (unparse-host #'unparse-unix-host
)
26 (unparse-directory #'unparse-unix-directory
)
27 (unparse-file #'unparse-unix-file
)
28 (unparse-enough #'unparse-unix-enough
)
29 (unparse-directory-separator "/")
30 (simplify-namestring #'simplify-unix-namestring
)
31 (customary-case :lower
))))
32 (defvar *unix-host
* (make-unix-host))
33 (defun make-unix-host-load-form (host)
34 (declare (ignore host
))
37 (def!struct
(win32-host
38 (:make-load-form-fun make-win32-host-load-form
)
40 (parse #'parse-win32-namestring
)
41 (parse-native #'parse-native-win32-namestring
)
42 (unparse #'unparse-win32-namestring
)
43 (unparse-native #'unparse-native-win32-namestring
)
44 (unparse-host #'unparse-win32-host
)
45 (unparse-directory #'unparse-win32-directory
)
46 (unparse-file #'unparse-win32-file
)
47 (unparse-enough #'unparse-win32-enough
)
48 (unparse-directory-separator "\\")
49 (simplify-namestring #'simplify-win32-namestring
)
50 (customary-case :upper
))))
51 (defparameter *win32-host
* (make-win32-host))
52 (defun make-win32-host-load-form (host)
53 (declare (ignore host
))
56 (defvar *physical-host
*
58 #!+win32
*win32-host
*)
60 ;;; Return a value suitable, e.g., for preinitializing
61 ;;; *DEFAULT-PATHNAME-DEFAULTS* before *DEFAULT-PATHNAME-DEFAULTS* is
62 ;;; initialized (at which time we can't safely call e.g. #'PATHNAME).
63 (defun make-trivial-default-pathname ()
64 (%make-pathname
*physical-host
* nil nil nil nil
:newest
))
68 (def!method print-object
((pathname pathname
) stream
)
69 (let ((namestring (handler-case (namestring pathname
)
73 (if (or *print-readably
* *print-escape
*)
76 (coerce namestring
'(simple-array character
(*))))
77 (print-unreadable-object (pathname stream
:type t
)
79 "~@<(with no namestring) ~_:HOST ~S ~_:DEVICE ~S ~_:DIRECTORY ~S ~
80 ~_:NAME ~S ~_:TYPE ~S ~_:VERSION ~S~:>"
81 (%pathname-host pathname
)
82 (%pathname-device pathname
)
83 (%pathname-directory pathname
)
84 (%pathname-name pathname
)
85 (%pathname-type pathname
)
86 (%pathname-version pathname
))))))
88 (def!method make-load-form
((pathname pathname
) &optional environment
)
89 (make-load-form-saving-slots pathname
:environment environment
))
91 ;;; A pathname is logical if the host component is a logical host.
92 ;;; This constructor is used to make an instance of the correct type
93 ;;; from parsed arguments.
94 (defun %make-maybe-logical-pathname
(host device directory name type version
)
95 ;; We canonicalize logical pathname components to uppercase. ANSI
96 ;; doesn't strictly require this, leaving it up to the implementor;
97 ;; but the arguments given in the X3J13 cleanup issue
98 ;; PATHNAME-LOGICAL:ADD seem compelling: we should canonicalize the
99 ;; case, and uppercase is the ordinary way to do that.
100 (flet ((upcase-maybe (x) (typecase x
(string (logical-word-or-lose x
)) (t x
))))
101 (if (typep host
'logical-host
)
102 (%make-logical-pathname host
104 (mapcar #'upcase-maybe directory
)
109 (aver (eq host
*physical-host
*))
110 (%make-pathname host device directory name type version
)))))
112 ;;; Hash table searching maps a logical pathname's host to its
113 ;;; physical pathname translation.
114 (defvar *logical-hosts
* (make-hash-table :test
'equal
))
118 (def!method make-load-form
((pattern pattern
) &optional environment
)
119 (make-load-form-saving-slots pattern
:environment environment
))
121 (def!method print-object
((pattern pattern
) stream
)
122 (print-unreadable-object (pattern stream
:type t
)
124 (let ((*print-escape
* t
))
125 (pprint-fill stream
(pattern-pieces pattern
) nil
))
126 (prin1 (pattern-pieces pattern
) stream
))))
128 (defun pattern= (pattern1 pattern2
)
129 (declare (type pattern pattern1 pattern2
))
130 (let ((pieces1 (pattern-pieces pattern1
))
131 (pieces2 (pattern-pieces pattern2
)))
132 (and (= (length pieces1
) (length pieces2
))
133 (every (lambda (piece1 piece2
)
136 (and (simple-string-p piece2
)
137 (string= piece1 piece2
)))
140 (eq (car piece1
) (car piece2
))
141 (string= (cdr piece1
) (cdr piece2
))))
143 (eq piece1 piece2
))))
147 ;;; If the string matches the pattern returns the multiple values T
148 ;;; and a list of the matched strings.
149 (defun pattern-matches (pattern string
)
150 (declare (type pattern pattern
)
151 (type simple-string string
))
152 (let ((len (length string
)))
153 (labels ((maybe-prepend (subs cur-sub chars
)
155 (let* ((len (length chars
))
156 (new (make-string len
))
159 (setf (schar new
(decf index
)) char
))
162 (matches (pieces start subs cur-sub chars
)
165 (values t
(maybe-prepend subs cur-sub chars
))
167 (let ((piece (car pieces
)))
170 (let ((end (+ start
(length piece
))))
172 (string= piece string
173 :start2 start
:end2 end
)
174 (matches (cdr pieces
) end
175 (maybe-prepend subs cur-sub chars
)
181 (let ((char (schar string start
)))
182 (if (find char
(cdr piece
) :test
#'char
=)
183 (matches (cdr pieces
) (1+ start
) subs t
184 (cons char chars
))))))))
185 ((member :single-char-wild
)
187 (matches (cdr pieces
) (1+ start
) subs t
188 (cons (schar string start
) chars
))))
189 ((member :multi-char-wild
)
190 (multiple-value-bind (won new-subs
)
191 (matches (cdr pieces
) start subs t chars
)
195 (matches pieces
(1+ start
) subs t
196 (cons (schar string start
)
198 (multiple-value-bind (won subs
)
199 (matches (pattern-pieces pattern
) 0 nil nil nil
)
200 (values won
(reverse subs
))))))
202 ;;; PATHNAME-MATCH-P for directory components
203 (defun directory-components-match (thing wild
)
206 ;; If THING has a null directory, assume that it matches
207 ;; (:ABSOLUTE :WILD-INFERIORS) or (:RELATIVE :WILD-INFERIORS).
210 (member (first wild
) '(:absolute
:relative
))
211 (eq (second wild
) :wild-inferiors
))
213 (let ((wild1 (first wild
)))
214 (if (eq wild1
:wild-inferiors
)
215 (let ((wild-subdirs (rest wild
)))
216 (or (null wild-subdirs
)
218 (when (directory-components-match thing wild-subdirs
)
221 (unless thing
(return nil
)))))
223 (components-match (first thing
) wild1
)
224 (directory-components-match (rest thing
)
227 ;;; Return true if pathname component THING is matched by WILD. (not
229 (defun components-match (thing wild
)
230 (declare (type (or pattern symbol simple-string integer
) thing wild
))
235 ;; String is matched by itself, a matching pattern or :WILD.
238 (values (pattern-matches wild thing
)))
240 (string= thing wild
))))
242 ;; A pattern is only matched by an identical pattern.
243 (and (pattern-p wild
) (pattern= thing wild
)))
245 ;; An integer (version number) is matched by :WILD or the
246 ;; same integer. This branch will actually always be NIL as
247 ;; long as the version is a fixnum.
250 ;;; a predicate for comparing two pathname slot component sub-entries
251 (defun compare-component (this that
)
255 (and (simple-string-p that
)
256 (string= this that
)))
258 (and (pattern-p that
)
259 (pattern= this that
)))
262 (compare-component (car this
) (car that
))
263 (compare-component (cdr this
) (cdr that
)))))))
265 ;;;; pathname functions
267 (defun pathname= (pathname1 pathname2
)
268 (declare (type pathname pathname1
)
269 (type pathname pathname2
))
270 (and (eq (%pathname-host pathname1
)
271 (%pathname-host pathname2
))
272 (compare-component (%pathname-device pathname1
)
273 (%pathname-device pathname2
))
274 (compare-component (%pathname-directory pathname1
)
275 (%pathname-directory pathname2
))
276 (compare-component (%pathname-name pathname1
)
277 (%pathname-name pathname2
))
278 (compare-component (%pathname-type pathname1
)
279 (%pathname-type pathname2
))
280 (or (eq (%pathname-host pathname1
) *unix-host
*)
281 (compare-component (%pathname-version pathname1
)
282 (%pathname-version pathname2
)))))
284 ;;; Convert PATHNAME-DESIGNATOR (a pathname, or string, or
285 ;;; stream), into a pathname in pathname.
287 ;;; FIXME: was rewritten, should be tested (or rewritten again, this
288 ;;; time using ONCE-ONLY, *then* tested)
289 ;;; FIXME: become SB!XC:DEFMACRO inside EVAL-WHEN (COMPILE EVAL)?
290 (defmacro with-pathname
((pathname pathname-designator
) &body body
)
291 (let ((pd0 (gensym)))
292 `(let* ((,pd0
,pathname-designator
)
293 (,pathname
(etypecase ,pd0
295 (string (parse-namestring ,pd0
))
296 (file-stream (file-name ,pd0
)))))
299 (defmacro with-native-pathname
((pathname pathname-designator
) &body body
)
300 (let ((pd0 (gensym)))
301 `(let* ((,pd0
,pathname-designator
)
302 (,pathname
(etypecase ,pd0
304 (string (parse-native-namestring ,pd0
))
307 (file-stream (file-name ,pd0
)))))
310 (defmacro with-host
((host host-designator
) &body body
)
311 ;; Generally, redundant specification of information in software,
312 ;; whether in code or in comments, is bad. However, the ANSI spec
313 ;; for this is messy enough that it's hard to hold in short-term
314 ;; memory, so I've recorded these redundant notes on the
315 ;; implications of the ANSI spec.
317 ;; According to the ANSI spec, HOST can be a valid pathname host, or
318 ;; a logical host, or NIL.
320 ;; A valid pathname host can be a valid physical pathname host or a
321 ;; valid logical pathname host.
323 ;; A valid physical pathname host is "any of a string, a list of
324 ;; strings, or the symbol :UNSPECIFIC, that is recognized by the
325 ;; implementation as the name of a host". In SBCL as of 0.6.9.8,
326 ;; that means :UNSPECIFIC: though someday we might want to
327 ;; generalize it to allow strings like "RTFM.MIT.EDU" or lists like
328 ;; '("RTFM" "MIT" "EDU"), that's not supported now.
330 ;; A valid logical pathname host is a string which has been defined as
331 ;; the name of a logical host, as with LOAD-LOGICAL-PATHNAME-TRANSLATIONS.
333 ;; A logical host is an object of implementation-dependent nature. In
334 ;; SBCL, it's a member of the HOST class (a subclass of STRUCTURE-OBJECT).
335 (let ((hd0 (gensym)))
336 `(let* ((,hd0
,host-designator
)
337 (,host
(etypecase ,hd0
339 ;; This is a special host. It's not valid as a
340 ;; logical host, so it is a sensible thing to
341 ;; designate the physical host object. So we do
345 ;; In general ANSI-compliant Common Lisps, a
346 ;; string might also be a physical pathname
347 ;; host, but ANSI leaves this up to the
348 ;; implementor, and in SBCL we don't do it, so
349 ;; it must be a logical host.
350 (find-logical-host ,hd0
))
351 ((or null
(member :unspecific
))
352 ;; CLHS says that HOST=:UNSPECIFIC has
353 ;; implementation-defined behavior. We
354 ;; just turn it into NIL.
357 ;; ANSI also allows LISTs to designate hosts,
358 ;; but leaves its interpretation
359 ;; implementation-defined. Our interpretation
360 ;; is that it's unsupported.:-|
361 (error "A LIST representing a pathname host is not ~
362 supported in this implementation:~% ~S"
367 (defun find-host (host-designator &optional
(errorp t
))
368 (with-host (host host-designator
)
369 (when (and errorp
(not host
))
370 (error "Couldn't find host: ~S" host-designator
))
373 (defun pathname (pathspec)
375 "Convert PATHSPEC (a pathname designator) into a pathname."
376 (declare (type pathname-designator pathspec
))
377 (with-pathname (pathname pathspec
)
380 (defun native-pathname (pathspec)
382 "Convert PATHSPEC (a pathname designator) into a pathname, assuming
383 the operating system native pathname conventions."
384 (with-native-pathname (pathname pathspec
)
387 ;;; Change the case of thing if DIDDLE-P.
388 (defun maybe-diddle-case (thing diddle-p
)
389 (if (and diddle-p
(not (or (symbolp thing
) (integerp thing
))))
390 (labels ((check-for (pred in
)
393 (dolist (piece (pattern-pieces in
))
394 (when (typecase piece
396 (check-for pred piece
))
400 (check-for pred
(cdr piece
))))))
404 (when (check-for pred x
)
407 (dotimes (i (length in
))
408 (when (funcall pred
(schar in i
))
411 (diddle-with (fun thing
)
415 (mapcar (lambda (piece)
423 (funcall fun
(cdr piece
))))
428 (pattern-pieces thing
))))
435 (let ((any-uppers (check-for #'upper-case-p thing
))
436 (any-lowers (check-for #'lower-case-p thing
)))
437 (cond ((and any-uppers any-lowers
)
438 ;; mixed case, stays the same
441 ;; all uppercase, becomes all lower case
442 (diddle-with (lambda (x) (if (stringp x
)
446 ;; all lowercase, becomes all upper case
447 (diddle-with (lambda (x) (if (stringp x
)
451 ;; no letters? I guess just leave it.
455 (defun merge-directories (dir1 dir2 diddle-case
)
456 (if (or (eq (car dir1
) :absolute
)
461 (if (and (eq dir
:back
)
463 (not (member (car results
)
464 '(:back
:wild-inferiors
:relative
:absolute
))))
466 (push dir results
))))
467 (dolist (dir (maybe-diddle-case dir2 diddle-case
))
469 (dolist (dir (cdr dir1
))
473 (defun merge-pathnames (pathname
475 (defaults *default-pathname-defaults
*)
476 (default-version :newest
))
478 "Construct a filled in pathname by completing the unspecified components
480 (declare (type pathname-designator pathname
)
481 (type pathname-designator defaults
)
483 (with-pathname (defaults defaults
)
484 (let ((pathname (let ((*default-pathname-defaults
* defaults
))
485 (pathname pathname
))))
486 (let* ((default-host (%pathname-host defaults
))
487 (pathname-host (%pathname-host pathname
))
489 (and default-host pathname-host
490 (not (eq (host-customary-case default-host
)
491 (host-customary-case pathname-host
))))))
492 (%make-maybe-logical-pathname
493 (or pathname-host default-host
)
494 (or (%pathname-device pathname
)
495 (maybe-diddle-case (%pathname-device defaults
)
497 (merge-directories (%pathname-directory pathname
)
498 (%pathname-directory defaults
)
500 (or (%pathname-name pathname
)
501 (maybe-diddle-case (%pathname-name defaults
)
503 (or (%pathname-type pathname
)
504 (maybe-diddle-case (%pathname-type defaults
)
506 (or (%pathname-version pathname
)
507 (and (not (%pathname-name pathname
)) (%pathname-version defaults
))
508 default-version
))))))
510 (defun import-directory (directory diddle-case
)
513 ((member :wild
) '(:absolute
:wild-inferiors
))
514 ((member :unspecific
) '(:relative
))
517 (results (pop directory
))
518 (dolist (piece directory
)
519 (cond ((member piece
'(:wild
:wild-inferiors
:up
:back
))
521 ((or (simple-string-p piece
) (pattern-p piece
))
522 (results (maybe-diddle-case piece diddle-case
)))
524 (results (maybe-diddle-case (coerce piece
'simple-string
)
527 (error "~S is not allowed as a directory component." piece
))))
530 `(:absolute
,(maybe-diddle-case directory diddle-case
)))
533 ,(maybe-diddle-case (coerce directory
'simple-string
) diddle-case
)))))
535 (defun make-pathname (&key host
540 (version nil versionp
)
544 "Makes a new pathname from the component arguments. Note that host is
545 a host-structure or string."
546 (declare (type (or string host pathname-component-tokens
) host
)
547 (type (or string pathname-component-tokens
) device
)
548 (type (or list string pattern pathname-component-tokens
) directory
)
549 (type (or string pattern pathname-component-tokens
) name type
)
550 (type (or integer pathname-component-tokens
(member :newest
))
552 (type (or pathname-designator null
) defaults
)
553 (type (member :common
:local
) case
))
554 (let* ((defaults (when defaults
555 (with-pathname (defaults defaults
) defaults
)))
556 (default-host (if defaults
557 (%pathname-host defaults
)
558 (pathname-host *default-pathname-defaults
*)))
559 ;; Raymond Toy writes: CLHS says make-pathname can take a
560 ;; string (as a logical-host) for the host part. We map that
561 ;; string into the corresponding logical host structure.
563 ;; Paul Werkowski writes:
564 ;; HyperSpec says for the arg to MAKE-PATHNAME;
565 ;; "host---a valid physical pathname host. ..."
566 ;; where it probably means -- a valid pathname host.
567 ;; "valid pathname host n. a valid physical pathname host or
568 ;; a valid logical pathname host."
570 ;; "valid physical pathname host n. any of a string,
571 ;; a list of strings, or the symbol :unspecific,
572 ;; that is recognized by the implementation as the name of a host."
573 ;; "valid logical pathname host n. a string that has been defined
574 ;; as the name of a logical host. ..."
575 ;; HS is silent on what happens if the :HOST arg is NOT one of these.
576 ;; It seems an error message is appropriate.
577 (host (or (find-host host nil
) default-host
))
578 (diddle-args (and (eq (host-customary-case host
) :lower
)
581 (not (eq (host-customary-case host
)
582 (host-customary-case default-host
))))
583 (dev (if devp device
(if defaults
(%pathname-device defaults
))))
584 (dir (import-directory directory diddle-args
))
587 (defaults (%pathname-version defaults
))
589 (when (and defaults
(not dirp
))
591 (merge-directories dir
592 (%pathname-directory defaults
)
595 (macrolet ((pick (var varp field
)
596 `(cond ((or (simple-string-p ,var
)
598 (maybe-diddle-case ,var diddle-args
))
600 (maybe-diddle-case (coerce ,var
'simple-string
)
603 (maybe-diddle-case ,var diddle-args
))
605 (maybe-diddle-case (,field defaults
)
609 (%make-maybe-logical-pathname host
610 dev
; forced to :UNSPECIFIC when logical
612 (pick name namep %pathname-name
)
613 (pick type typep %pathname-type
)
616 (defun pathname-host (pathname &key
(case :local
))
618 "Return PATHNAME's host."
619 (declare (type pathname-designator pathname
)
620 (type (member :local
:common
) case
)
623 (with-pathname (pathname pathname
)
624 (%pathname-host pathname
)))
626 (defun pathname-device (pathname &key
(case :local
))
628 "Return PATHNAME's device."
629 (declare (type pathname-designator pathname
)
630 (type (member :local
:common
) case
))
631 (with-pathname (pathname pathname
)
632 (maybe-diddle-case (%pathname-device pathname
)
633 (and (eq case
:common
)
634 (eq (host-customary-case
635 (%pathname-host pathname
))
638 (defun pathname-directory (pathname &key
(case :local
))
640 "Return PATHNAME's directory."
641 (declare (type pathname-designator pathname
)
642 (type (member :local
:common
) case
))
643 (with-pathname (pathname pathname
)
644 (maybe-diddle-case (%pathname-directory pathname
)
645 (and (eq case
:common
)
646 (eq (host-customary-case
647 (%pathname-host pathname
))
649 (defun pathname-name (pathname &key
(case :local
))
651 "Return PATHNAME's name."
652 (declare (type pathname-designator pathname
)
653 (type (member :local
:common
) case
))
654 (with-pathname (pathname pathname
)
655 (maybe-diddle-case (%pathname-name pathname
)
656 (and (eq case
:common
)
657 (eq (host-customary-case
658 (%pathname-host pathname
))
661 (defun pathname-type (pathname &key
(case :local
))
663 "Return PATHNAME's type."
664 (declare (type pathname-designator pathname
)
665 (type (member :local
:common
) case
))
666 (with-pathname (pathname pathname
)
667 (maybe-diddle-case (%pathname-type pathname
)
668 (and (eq case
:common
)
669 (eq (host-customary-case
670 (%pathname-host pathname
))
673 (defun pathname-version (pathname)
675 "Return PATHNAME's version."
676 (declare (type pathname-designator pathname
))
677 (with-pathname (pathname pathname
)
678 (%pathname-version pathname
)))
682 ;;; Handle the case for PARSE-NAMESTRING parsing a potentially
683 ;;; syntactically valid logical namestring with an explicit host.
685 ;;; This then isn't fully general -- we are relying on the fact that
686 ;;; we will only pass to parse-namestring namestring with an explicit
687 ;;; logical host, so that we can pass the host return from
688 ;;; parse-logical-namestring through to %PARSE-NAMESTRING as a truth
689 ;;; value. Yeah, this is probably a KLUDGE - CSR, 2002-04-18
690 (defun parseable-logical-namestring-p (namestr start end
)
693 ((namestring-parse-error (lambda (c)
696 (let ((colon (position #\
: namestr
:start start
:end end
)))
698 (let ((potential-host
699 (logical-word-or-lose (subseq namestr start colon
))))
700 ;; depending on the outcome of CSR comp.lang.lisp post
701 ;; "can PARSE-NAMESTRING create logical hosts", we may need
702 ;; to do things with potential-host (create it
703 ;; temporarily, parse the namestring and unintern the
704 ;; logical host potential-host on failure.
705 (declare (ignore potential-host
))
708 ((simple-type-error (lambda (c)
711 (parse-logical-namestring namestr start end
))))
712 ;; if we got this far, we should have an explicit host
713 ;; (first return value of parse-logical-namestring)
717 ;;; Handle the case where PARSE-NAMESTRING is actually parsing a
718 ;;; namestring. We pick off the :JUNK-ALLOWED case then find a host to
719 ;;; use for parsing, call the parser, then check whether the host matches.
720 (defun %parse-namestring
(namestr host defaults start end junk-allowed
)
721 (declare (type (or host null
) host
)
722 (type string namestr
)
724 (type (or index null
) end
))
728 (%parse-namestring namestr host defaults start end nil
)
729 (namestring-parse-error (condition)
730 (values nil
(namestring-parse-error-offset condition
)))))
732 (let* ((end (%check-vector-sequence-bounds namestr start end
)))
733 (multiple-value-bind (new-host device directory file type version
)
734 ;; Comments below are quotes from the HyperSpec
735 ;; PARSE-NAMESTRING entry, reproduced here to demonstrate
736 ;; that we actually have to do things this way rather than
737 ;; some possibly more logical way. - CSR, 2002-04-18
739 ;; "If host is a logical host then thing is parsed as a
740 ;; logical pathname namestring on the host."
741 (host (funcall (host-parse host
) namestr start end
))
742 ;; "If host is nil and thing is a syntactically valid
743 ;; logical pathname namestring containing an explicit
744 ;; host, then it is parsed as a logical pathname
746 ((parseable-logical-namestring-p namestr start end
)
747 (parse-logical-namestring namestr start end
))
748 ;; "If host is nil, default-pathname is a logical
749 ;; pathname, and thing is a syntactically valid logical
750 ;; pathname namestring without an explicit host, then it
751 ;; is parsed as a logical pathname namestring on the
752 ;; host that is the host component of default-pathname."
754 ;; "Otherwise, the parsing of thing is
755 ;; implementation-defined."
757 ;; Both clauses are handled here, as the default
758 ;; *DEFAULT-PATHNAME-DEFAULTS* has a SB-IMPL::UNIX-HOST
760 ((pathname-host defaults
)
761 (funcall (host-parse (pathname-host defaults
))
765 ;; I don't think we should ever get here, as the default
766 ;; host will always have a non-null HOST, given that we
767 ;; can't create a new pathname without going through
768 ;; *DEFAULT-PATHNAME-DEFAULTS*, which has a non-null
770 (t (bug "Fallen through COND in %PARSE-NAMESTRING")))
771 (when (and host new-host
(not (eq new-host host
)))
772 (error 'simple-type-error
774 ;; Note: ANSI requires that this be a TYPE-ERROR,
775 ;; but there seems to be no completely correct
776 ;; value to use for TYPE-ERROR-EXPECTED-TYPE.
777 ;; Instead, we return a sort of "type error allowed
778 ;; type", trying to say "it would be OK if you
779 ;; passed NIL as the host value" but not mentioning
780 ;; that a matching string would be OK too.
783 "The host in the namestring, ~S,~@
784 does not match the explicit HOST argument, ~S."
785 :format-arguments
(list new-host host
)))
786 (let ((pn-host (or new-host host
(pathname-host defaults
))))
787 (values (%make-maybe-logical-pathname
788 pn-host device directory file type version
)
791 ;;; If NAMESTR begins with a colon-terminated, defined, logical host,
792 ;;; then return that host, otherwise return NIL.
793 (defun extract-logical-host-prefix (namestr start end
)
794 (declare (type simple-string namestr
)
795 (type index start end
)
796 (values (or logical-host null
)))
797 (let ((colon-pos (position #\
: namestr
:start start
:end end
)))
799 (values (gethash (nstring-upcase (subseq namestr start colon-pos
))
803 (defun parse-namestring (thing
806 (defaults *default-pathname-defaults
*)
807 &key
(start 0) end junk-allowed
)
808 (declare (type pathname-designator thing defaults
)
809 (type (or list host string
(member :unspecific
)) host
)
811 (type (or index null
) end
)
812 (type (or t null
) junk-allowed
)
813 (values (or null pathname
) (or null index
)))
814 (with-host (found-host host
)
815 (let (;; According to ANSI defaults may be any valid pathname designator
816 (defaults (etypecase defaults
820 (aver (pathnamep *default-pathname-defaults
*))
821 (parse-namestring defaults
))
823 (truename defaults
)))))
824 (declare (type pathname defaults
))
827 (%parse-namestring thing found-host defaults start end junk-allowed
))
829 (%parse-namestring
(coerce thing
'simple-string
)
830 found-host defaults start end junk-allowed
))
832 (let ((defaulted-host (or found-host
(%pathname-host defaults
))))
833 (declare (type host defaulted-host
))
834 (unless (eq defaulted-host
(%pathname-host thing
))
835 (error "The HOST argument doesn't match the pathname host:~% ~
837 defaulted-host
(%pathname-host thing
))))
838 (values thing start
))
840 (let ((name (file-name thing
)))
842 (error "can't figure out the file associated with stream:~% ~S"
844 (values name nil
)))))))
846 (defun %parse-native-namestring
(namestr host defaults start end junk-allowed
)
847 (declare (type (or host null
) host
)
848 (type string namestr
)
850 (type (or index null
) end
))
854 (%parse-namestring namestr host defaults start end nil
)
855 (namestring-parse-error (condition)
856 (values nil
(namestring-parse-error-offset condition
)))))
858 (let* ((end (%check-vector-sequence-bounds namestr start end
)))
859 (multiple-value-bind (new-host device directory file type version
)
862 (funcall (host-parse-native host
) namestr start end
))
863 ((pathname-host defaults
)
864 (funcall (host-parse-native (pathname-host defaults
))
868 ;; I don't think we should ever get here, as the default
869 ;; host will always have a non-null HOST, given that we
870 ;; can't create a new pathname without going through
871 ;; *DEFAULT-PATHNAME-DEFAULTS*, which has a non-null
873 (t (bug "Fallen through COND in %PARSE-NAMESTRING")))
874 (when (and host new-host
(not (eq new-host host
)))
875 (error 'simple-type-error
877 :expected-type
`(or null
(eql ,host
))
879 "The host in the namestring, ~S,~@
880 does not match the explicit HOST argument, ~S."
881 :format-arguments
(list new-host host
)))
882 (let ((pn-host (or new-host host
(pathname-host defaults
))))
883 (values (%make-pathname
884 pn-host device directory file type version
)
887 (defun parse-native-namestring (thing
890 (defaults *default-pathname-defaults
*)
891 &key
(start 0) end junk-allowed
)
893 "Convert THING into a pathname, using the native conventions
894 appropriate for the pathname host HOST, or if not specified the host
895 of DEFAULTS. If THING is a string, the parse is bounded by START and
896 END, and error behaviour is controlled by JUNK-ALLOWED, as with
898 (declare (type pathname-designator thing defaults
)
899 (type (or list host string
(member :unspecific
)) host
)
901 (type (or index null
) end
)
902 (type (or t null
) junk-allowed
)
903 (values (or null pathname
) (or null index
)))
904 (with-host (found-host host
)
905 (let ((defaults (etypecase defaults
909 (aver (pathnamep *default-pathname-defaults
*))
910 (parse-native-namestring defaults
))
912 (truename defaults
)))))
913 (declare (type pathname defaults
))
916 (%parse-native-namestring
917 thing found-host defaults start end junk-allowed
))
919 (%parse-native-namestring
(coerce thing
'simple-string
)
920 found-host defaults start end junk-allowed
))
922 (let ((defaulted-host (or found-host
(%pathname-host defaults
))))
923 (declare (type host defaulted-host
))
924 (unless (eq defaulted-host
(%pathname-host thing
))
925 (error "The HOST argument doesn't match the pathname host:~% ~
927 defaulted-host
(%pathname-host thing
))))
928 (values thing start
))
931 (let ((name (file-name thing
)))
933 (error "can't figure out the file associated with stream:~% ~S"
935 (values name nil
)))))))
937 (defun namestring (pathname)
939 "Construct the full (name)string form of the pathname."
940 (declare (type pathname-designator pathname
))
941 (with-pathname (pathname pathname
)
943 (let ((host (%pathname-host pathname
)))
945 (error "can't determine the namestring for pathnames with no ~
946 host:~% ~S" pathname
))
947 (funcall (host-unparse host
) pathname
)))))
949 (defun native-namestring (pathname)
951 "Construct the full native (name)string form of PATHNAME."
952 (declare (type pathname-designator pathname
))
953 (with-native-pathname (pathname pathname
)
955 (let ((host (%pathname-host pathname
)))
957 (error "can't determine the native namestring for pathnames with no ~
958 host:~% ~S" pathname
))
959 (funcall (host-unparse-native host
) pathname
)))))
961 (defun host-namestring (pathname)
963 "Return a string representation of the name of the host in the pathname."
964 (declare (type pathname-designator pathname
))
965 (with-pathname (pathname pathname
)
966 (let ((host (%pathname-host pathname
)))
968 (funcall (host-unparse-host host
) pathname
)
970 "can't determine the namestring for pathnames with no host:~% ~S"
973 (defun directory-namestring (pathname)
975 "Return a string representation of the directories used in the pathname."
976 (declare (type pathname-designator pathname
))
977 (with-pathname (pathname pathname
)
978 (let ((host (%pathname-host pathname
)))
980 (funcall (host-unparse-directory host
) pathname
)
982 "can't determine the namestring for pathnames with no host:~% ~S"
985 (defun file-namestring (pathname)
987 "Return a string representation of the name used in the pathname."
988 (declare (type pathname-designator pathname
))
989 (with-pathname (pathname pathname
)
990 (let ((host (%pathname-host pathname
)))
992 (funcall (host-unparse-file host
) pathname
)
994 "can't determine the namestring for pathnames with no host:~% ~S"
997 (defun enough-namestring (pathname
999 (defaults *default-pathname-defaults
*))
1001 "Return an abbreviated pathname sufficent to identify the pathname relative
1003 (declare (type pathname-designator pathname
))
1004 (with-pathname (pathname pathname
)
1005 (let ((host (%pathname-host pathname
)))
1007 (with-pathname (defaults defaults
)
1008 (funcall (host-unparse-enough host
) pathname defaults
))
1010 "can't determine the namestring for pathnames with no host:~% ~S"
1015 (defun wild-pathname-p (pathname &optional field-key
)
1017 "Predicate for determining whether pathname contains any wildcards."
1018 (declare (type pathname-designator pathname
)
1019 (type (member nil
:host
:device
:directory
:name
:type
:version
)
1021 (with-pathname (pathname pathname
)
1023 (or (pattern-p x
) (member x
'(:wild
:wild-inferiors
)))))
1026 (or (wild-pathname-p pathname
:host
)
1027 (wild-pathname-p pathname
:device
)
1028 (wild-pathname-p pathname
:directory
)
1029 (wild-pathname-p pathname
:name
)
1030 (wild-pathname-p pathname
:type
)
1031 (wild-pathname-p pathname
:version
)))
1032 (:host
(frob (%pathname-host pathname
)))
1033 (:device
(frob (%pathname-host pathname
)))
1034 (:directory
(some #'frob
(%pathname-directory pathname
)))
1035 (:name
(frob (%pathname-name pathname
)))
1036 (:type
(frob (%pathname-type pathname
)))
1037 (:version
(frob (%pathname-version pathname
)))))))
1039 (defun pathname-match-p (in-pathname in-wildname
)
1041 "Pathname matches the wildname template?"
1042 (declare (type pathname-designator in-pathname
))
1043 (with-pathname (pathname in-pathname
)
1044 (with-pathname (wildname in-wildname
)
1045 (macrolet ((frob (field &optional
(op 'components-match
))
1046 `(or (null (,field wildname
))
1047 (,op
(,field pathname
) (,field wildname
)))))
1048 (and (or (null (%pathname-host wildname
))
1049 (eq (%pathname-host wildname
) (%pathname-host pathname
)))
1050 (frob %pathname-device
)
1051 (frob %pathname-directory directory-components-match
)
1052 (frob %pathname-name
)
1053 (frob %pathname-type
)
1054 (or (eq (%pathname-host wildname
) *unix-host
*)
1055 (frob %pathname-version
)))))))
1057 ;;; Place the substitutions into the pattern and return the string or pattern
1058 ;;; that results. If DIDDLE-CASE is true, we diddle the result case as well,
1059 ;;; in case we are translating between hosts with difference conventional case.
1060 ;;; The second value is the tail of subs with all of the values that we used up
1061 ;;; stripped off. Note that PATTERN-MATCHES matches all consecutive wildcards
1062 ;;; as a single string, so we ignore subsequent contiguous wildcards.
1063 (defun substitute-into (pattern subs diddle-case
)
1064 (declare (type pattern pattern
)
1066 (values (or simple-string pattern
) list
))
1067 (let ((in-wildcard nil
)
1070 (dolist (piece (pattern-pieces pattern
))
1071 (cond ((simple-string-p piece
)
1072 (push piece strings
)
1073 (setf in-wildcard nil
))
1076 (setf in-wildcard t
)
1078 (error "not enough wildcards in FROM pattern to match ~
1081 (let ((sub (pop subs
)))
1085 (push (apply #'concatenate
'simple-string
1088 (dolist (piece (pattern-pieces sub
))
1089 (push piece pieces
)))
1093 (error "can't substitute this into the middle of a word:~
1098 (push (apply #'concatenate
'simple-string
(nreverse strings
))
1102 (if (and pieces
(simple-string-p (car pieces
)) (null (cdr pieces
)))
1104 (make-pattern (nreverse pieces
)))
1108 ;;; Called when we can't see how source and from matched.
1109 (defun didnt-match-error (source from
)
1110 (error "Pathname components from SOURCE and FROM args to TRANSLATE-PATHNAME~@
1111 did not match:~% ~S ~S"
1114 ;;; Do TRANSLATE-COMPONENT for all components except host, directory
1116 (defun translate-component (source from to diddle-case
)
1123 (if (pattern= from source
)
1125 (didnt-match-error source from
)))
1127 (multiple-value-bind (won subs
) (pattern-matches from source
)
1129 (values (substitute-into to subs diddle-case
))
1130 (didnt-match-error source from
))))
1132 (maybe-diddle-case source diddle-case
))))
1134 (values (substitute-into to
(list source
) diddle-case
)))
1136 (if (components-match source from
)
1137 (maybe-diddle-case source diddle-case
)
1138 (didnt-match-error source from
)))))
1140 (maybe-diddle-case source diddle-case
))
1142 (if (components-match source from
)
1144 (didnt-match-error source from
)))))
1146 ;;; Return a list of all the things that we want to substitute into the TO
1147 ;;; pattern (the things matched by from on source.) When From contains
1148 ;;; :WILD-INFERIORS, the result contains a sublist of the matched source
1150 (defun compute-directory-substitutions (orig-source orig-from
)
1151 (let ((source orig-source
)
1156 (unless (every (lambda (x) (eq x
:wild-inferiors
)) from
)
1157 (didnt-match-error orig-source orig-from
))
1160 (unless from
(didnt-match-error orig-source orig-from
))
1161 (let ((from-part (pop from
))
1162 (source-part (pop source
)))
1165 (typecase source-part
1167 (if (pattern= from-part source-part
)
1169 (didnt-match-error orig-source orig-from
)))
1171 (multiple-value-bind (won new-subs
)
1172 (pattern-matches from-part source-part
)
1174 (dolist (sub new-subs
)
1176 (didnt-match-error orig-source orig-from
))))
1178 (didnt-match-error orig-source orig-from
))))
1181 ((member :wild-inferiors
)
1182 (let ((remaining-source (cons source-part source
)))
1185 (when (directory-components-match remaining-source from
)
1187 (unless remaining-source
1188 (didnt-match-error orig-source orig-from
))
1189 (res (pop remaining-source
)))
1191 (setq source remaining-source
))))
1193 (unless (and (simple-string-p source-part
)
1194 (string= from-part source-part
))
1195 (didnt-match-error orig-source orig-from
)))
1197 (didnt-match-error orig-source orig-from
)))))
1200 ;;; This is called by TRANSLATE-PATHNAME on the directory components
1201 ;;; of its argument pathnames to produce the result directory
1202 ;;; component. If this leaves the directory NIL, we return the source
1203 ;;; directory. The :RELATIVE or :ABSOLUTE is taken from the source
1204 ;;; directory, except if TO is :ABSOLUTE, in which case the result
1205 ;;; will be :ABSOLUTE.
1206 (defun translate-directories (source from to diddle-case
)
1207 (if (not (and source to from
))
1208 (or (and to
(null source
) (remove :wild-inferiors to
))
1209 (mapcar (lambda (x) (maybe-diddle-case x diddle-case
)) source
))
1211 ;; If TO is :ABSOLUTE, the result should still be :ABSOLUTE.
1212 (res (if (eq (first to
) :absolute
)
1215 (let ((subs-left (compute-directory-substitutions (rest source
)
1217 (dolist (to-part (rest to
))
1221 (let ((match (pop subs-left
)))
1223 (error ":WILD-INFERIORS is not paired in from and to ~
1224 patterns:~% ~S ~S" from to
))
1225 (res (maybe-diddle-case match diddle-case
))))
1226 ((member :wild-inferiors
)
1228 (let ((match (pop subs-left
)))
1229 (unless (listp match
)
1230 (error ":WILD-INFERIORS not paired in from and to ~
1231 patterns:~% ~S ~S" from to
))
1233 (res (maybe-diddle-case x diddle-case
)))))
1235 (multiple-value-bind
1237 (substitute-into to-part subs-left diddle-case
)
1238 (setf subs-left new-subs-left
)
1240 (t (res to-part
)))))
1243 (defun translate-pathname (source from-wildname to-wildname
&key
)
1245 "Use the source pathname to translate the from-wildname's wild and
1246 unspecified elements into a completed to-pathname based on the to-wildname."
1247 (declare (type pathname-designator source from-wildname to-wildname
))
1248 (with-pathname (source source
)
1249 (with-pathname (from from-wildname
)
1250 (with-pathname (to to-wildname
)
1251 (let* ((source-host (%pathname-host source
))
1252 (from-host (%pathname-host from
))
1253 (to-host (%pathname-host to
))
1255 (and source-host to-host
1256 (not (eq (host-customary-case source-host
)
1257 (host-customary-case to-host
))))))
1258 (macrolet ((frob (field &optional
(op 'translate-component
))
1259 `(let ((result (,op
(,field source
)
1263 (if (eq result
:error
)
1264 (error "~S doesn't match ~S." source from
)
1266 (%make-maybe-logical-pathname
1267 (or to-host source-host
)
1268 (frob %pathname-device
)
1269 (frob %pathname-directory translate-directories
)
1270 (frob %pathname-name
)
1271 (frob %pathname-type
)
1272 (if (eq from-host
*unix-host
*)
1273 (if (or (eq (%pathname-version to
) :wild
)
1274 (eq (%pathname-version to
) nil
))
1275 (%pathname-version source
)
1276 (%pathname-version to
))
1277 (frob %pathname-version
)))))))))
1279 ;;;; logical pathname support. ANSI 92-102 specification.
1281 ;;;; As logical-pathname translations are loaded they are
1282 ;;;; canonicalized as patterns to enable rapid efficient translation
1283 ;;;; into physical pathnames.
1287 (defun simplify-namestring (namestring &optional host
)
1288 (funcall (host-simplify-namestring
1290 (pathname-host (sane-default-pathname-defaults))))
1293 ;;; Canonicalize a logical pathname word by uppercasing it checking that it
1294 ;;; contains only legal characters.
1295 (defun logical-word-or-lose (word)
1296 (declare (string word
))
1297 (when (string= word
"")
1298 (error 'namestring-parse-error
1299 :complaint
"Attempted to treat invalid logical hostname ~
1300 as a logical host:~% ~S"
1302 :namestring word
:offset
0))
1303 (let ((word (string-upcase word
)))
1304 (dotimes (i (length word
))
1305 (let ((ch (schar word i
)))
1306 (unless (and (typep ch
'standard-char
)
1307 (or (alpha-char-p ch
) (digit-char-p ch
) (char= ch
#\-
)))
1308 (error 'namestring-parse-error
1309 :complaint
"logical namestring character which ~
1310 is not alphanumeric or hyphen:~% ~S"
1312 :namestring word
:offset i
))))
1313 (coerce word
'string
))) ; why not simple-string?
1315 ;;; Given a logical host or string, return a logical host. If ERROR-P
1316 ;;; is NIL, then return NIL when no such host exists.
1317 (defun find-logical-host (thing &optional
(errorp t
))
1320 (let ((found (gethash (logical-word-or-lose thing
)
1322 (if (or found
(not errorp
))
1324 ;; This is the error signalled from e.g.
1325 ;; LOGICAL-PATHNAME-TRANSLATIONS when host is not a defined
1326 ;; host, and ANSI specifies that that's a TYPE-ERROR.
1327 (error 'simple-type-error
1329 ;; God only knows what ANSI expects us to use for
1330 ;; the EXPECTED-TYPE here. Maybe this will be OK..
1332 '(and string
(satisfies logical-pathname-translations
))
1333 :format-control
"logical host not yet defined: ~S"
1334 :format-arguments
(list thing
)))))
1335 (logical-host thing
)))
1337 ;;; Given a logical host name or host, return a logical host, creating
1338 ;;; a new one if necessary.
1339 (defun intern-logical-host (thing)
1340 (declare (values logical-host
))
1341 (or (find-logical-host thing nil
)
1342 (let* ((name (logical-word-or-lose thing
))
1343 (new (make-logical-host :name name
)))
1344 (setf (gethash name
*logical-hosts
*) new
)
1347 ;;;; logical pathname parsing
1349 ;;; Deal with multi-char wildcards in a logical pathname token.
1350 (defun maybe-make-logical-pattern (namestring chunks
)
1351 (let ((chunk (caar chunks
)))
1352 (collect ((pattern))
1354 (len (length chunk
)))
1355 (declare (fixnum last-pos
))
1357 (when (= last-pos len
) (return))
1358 (let ((pos (or (position #\
* chunk
:start last-pos
) len
)))
1359 (if (= pos last-pos
)
1361 (error 'namestring-parse-error
1362 :complaint
"double asterisk inside of logical ~
1365 :namestring namestring
1366 :offset
(+ (cdar chunks
) pos
)))
1367 (pattern (subseq chunk last-pos pos
)))
1370 (pattern :multi-char-wild
))
1371 (setq last-pos
(1+ pos
)))))
1374 (make-pattern (pattern))
1375 (let ((x (car (pattern))))
1376 (if (eq x
:multi-char-wild
)
1380 ;;; Return a list of conses where the CDR is the start position and
1381 ;;; the CAR is a string (token) or character (punctuation.)
1382 (defun logical-chunkify (namestr start end
)
1384 (do ((i start
(1+ i
))
1388 (chunks (cons (nstring-upcase (subseq namestr prev end
)) prev
))))
1389 (let ((ch (schar namestr i
)))
1390 (unless (or (alpha-char-p ch
) (digit-char-p ch
)
1391 (member ch
'(#\-
#\
*)))
1393 (chunks (cons (nstring-upcase (subseq namestr prev i
)) prev
)))
1395 (unless (member ch
'(#\
; #\: #\.))
1396 (error 'namestring-parse-error
1397 :complaint
"illegal character for logical pathname:~% ~S"
1401 (chunks (cons ch i
)))))
1404 ;;; Break up a logical-namestring, always a string, into its
1405 ;;; constituent parts.
1406 (defun parse-logical-namestring (namestr start end
)
1407 (declare (type simple-string namestr
)
1408 (type index start end
))
1409 (collect ((directory))
1414 (labels ((expecting (what chunks
)
1415 (unless (and chunks
(simple-string-p (caar chunks
)))
1416 (error 'namestring-parse-error
1417 :complaint
"expecting ~A, got ~:[nothing~;~S~]."
1418 :args
(list what
(caar chunks
) (caar chunks
))
1420 :offset
(if chunks
(cdar chunks
) end
)))
1422 (parse-host (chunks)
1423 (case (caadr chunks
)
1426 (find-logical-host (expecting "a host name" chunks
)))
1427 (parse-relative (cddr chunks
)))
1429 (parse-relative chunks
))))
1430 (parse-relative (chunks)
1433 (directory :relative
)
1434 (parse-directory (cdr chunks
)))
1436 (directory :absolute
) ; Assumption! Maybe revoked later.
1437 (parse-directory chunks
))))
1438 (parse-directory (chunks)
1439 (case (caadr chunks
)
1442 (let ((res (expecting "a directory name" chunks
)))
1443 (cond ((string= res
"..") :up
)
1444 ((string= res
"**") :wild-inferiors
)
1446 (maybe-make-logical-pattern namestr chunks
)))))
1447 (parse-directory (cddr chunks
)))
1449 (parse-name chunks
))))
1450 (parse-name (chunks)
1452 (expecting "a file name" chunks
)
1453 (setq name
(maybe-make-logical-pattern namestr chunks
))
1454 (expecting-dot (cdr chunks
))))
1455 (expecting-dot (chunks)
1457 (unless (eql (caar chunks
) #\.
)
1458 (error 'namestring-parse-error
1459 :complaint
"expecting a dot, got ~S."
1460 :args
(list (caar chunks
))
1462 :offset
(cdar chunks
)))
1464 (parse-version (cdr chunks
))
1465 (parse-type (cdr chunks
)))))
1466 (parse-type (chunks)
1467 (expecting "a file type" chunks
)
1468 (setq type
(maybe-make-logical-pattern namestr chunks
))
1469 (expecting-dot (cdr chunks
)))
1470 (parse-version (chunks)
1471 (let ((str (expecting "a positive integer, * or NEWEST"
1474 ((string= str
"*") (setq version
:wild
))
1475 ((string= str
"NEWEST") (setq version
:newest
))
1477 (multiple-value-bind (res pos
)
1478 (parse-integer str
:junk-allowed t
)
1479 (unless (and res
(plusp res
))
1480 (error 'namestring-parse-error
1481 :complaint
"expected a positive integer, ~
1485 :offset
(+ pos
(cdar chunks
))))
1486 (setq version res
)))))
1488 (error 'namestring-parse-error
1489 :complaint
"extra stuff after end of file name"
1491 :offset
(cdadr chunks
)))))
1492 (parse-host (logical-chunkify namestr start end
)))
1493 (values host
:unspecific
(directory) name type version
))))
1495 ;;; We can't initialize this yet because not all host methods are
1497 (defvar *logical-pathname-defaults
*)
1499 (defun logical-pathname (pathspec)
1501 "Converts the pathspec argument to a logical-pathname and returns it."
1502 (declare (type (or logical-pathname string stream
) pathspec
)
1503 (values logical-pathname
))
1504 (if (typep pathspec
'logical-pathname
)
1506 (let ((res (parse-namestring pathspec nil
*logical-pathname-defaults
*)))
1507 (when (eq (%pathname-host res
)
1508 (%pathname-host
*logical-pathname-defaults
*))
1509 (error "This logical namestring does not specify a host:~% ~S"
1513 ;;;; logical pathname unparsing
1515 (defun unparse-logical-directory (pathname)
1516 (declare (type pathname pathname
))
1518 (let ((directory (%pathname-directory pathname
)))
1520 (ecase (pop directory
)
1521 (:absolute
) ; nothing special
1522 (:relative
(pieces ";")))
1523 (dolist (dir directory
)
1524 (cond ((or (stringp dir
) (pattern-p dir
))
1525 (pieces (unparse-logical-piece dir
))
1529 ((eq dir
:wild-inferiors
)
1532 (error "invalid directory component: ~S" dir
))))))
1533 (apply #'concatenate
'simple-string
(pieces))))
1535 (defun unparse-logical-piece (thing)
1537 ((member :wild
) "*")
1538 (simple-string thing
)
1540 (collect ((strings))
1541 (dolist (piece (pattern-pieces thing
))
1543 (simple-string (strings piece
))
1545 (cond ((eq piece
:wild-inferiors
)
1547 ((eq piece
:multi-char-wild
)
1549 (t (error "invalid keyword: ~S" piece
))))))
1550 (apply #'concatenate
'simple-string
(strings))))))
1552 (defun unparse-logical-file (pathname)
1553 (declare (type pathname pathname
))
1554 (collect ((strings))
1555 (let* ((name (%pathname-name pathname
))
1556 (type (%pathname-type pathname
))
1557 (version (%pathname-version pathname
))
1558 (type-supplied (not (or (null type
) (eq type
:unspecific
))))
1559 (version-supplied (not (or (null version
)
1560 (eq version
:unspecific
)))))
1562 (when (and (null type
)
1563 (typep name
'string
)
1564 (position #\. name
:start
1))
1565 (error "too many dots in the name: ~S" pathname
))
1566 (strings (unparse-logical-piece name
)))
1569 (error "cannot specify the type without a file: ~S" pathname
))
1570 (when (typep type
'string
)
1571 (when (position #\. type
)
1572 (error "type component can't have a #\. inside: ~S" pathname
)))
1574 (strings (unparse-logical-piece type
)))
1575 (when version-supplied
1576 (unless type-supplied
1577 (error "cannot specify the version without a type: ~S" pathname
))
1579 ((member :newest
) (strings ".NEWEST"))
1580 ((member :wild
) (strings ".*"))
1581 (fixnum (strings ".") (strings (format nil
"~D" version
))))))
1582 (apply #'concatenate
'simple-string
(strings))))
1584 ;;; Unparse a logical pathname string.
1585 (defun unparse-enough-namestring (pathname defaults
)
1586 (let* ((path-directory (pathname-directory pathname
))
1587 (def-directory (pathname-directory defaults
))
1589 ;; Go down the directory lists to see what matches. What's
1590 ;; left is what we want, more or less.
1591 (cond ((and (eq (first path-directory
) (first def-directory
))
1592 (eq (first path-directory
) :absolute
))
1593 ;; Both paths are :ABSOLUTE, so find where the
1594 ;; common parts end and return what's left
1595 (do* ((p (rest path-directory
) (rest p
))
1596 (d (rest def-directory
) (rest d
)))
1597 ((or (endp p
) (endp d
)
1598 (not (equal (first p
) (first d
))))
1601 ;; At least one path is :RELATIVE, so just return the
1602 ;; original path. If the original path is :RELATIVE,
1603 ;; then that's the right one. If PATH-DIRECTORY is
1604 ;; :ABSOLUTE, we want to return that except when
1605 ;; DEF-DIRECTORY is :ABSOLUTE, as handled above. so return
1606 ;; the original directory.
1608 (unparse-logical-namestring
1609 (make-pathname :host
(pathname-host pathname
)
1610 :directory enough-directory
1611 :name
(pathname-name pathname
)
1612 :type
(pathname-type pathname
)
1613 :version
(pathname-version pathname
)))))
1615 (defun unparse-logical-namestring (pathname)
1616 (declare (type logical-pathname pathname
))
1617 (concatenate 'simple-string
1618 (logical-host-name (%pathname-host pathname
)) ":"
1619 (unparse-logical-directory pathname
)
1620 (unparse-logical-file pathname
)))
1622 ;;;; logical pathname translations
1624 ;;; Verify that the list of translations consists of lists and prepare
1625 ;;; canonical translations. (Parse pathnames and expand out wildcards
1627 (defun canonicalize-logical-pathname-translations (translation-list host
)
1628 (declare (type list translation-list
) (type host host
)
1630 (mapcar (lambda (translation)
1631 (destructuring-bind (from to
) translation
1632 (list (if (typep from
'logical-pathname
)
1634 (parse-namestring from host
))
1638 (defun logical-pathname-translations (host)
1640 "Return the (logical) host object argument's list of translations."
1641 (declare (type (or string logical-host
) host
)
1643 (logical-host-translations (find-logical-host host
)))
1645 (defun (setf logical-pathname-translations
) (translations host
)
1647 "Set the translations list for the logical host argument."
1648 (declare (type (or string logical-host
) host
)
1649 (type list translations
)
1651 (let ((host (intern-logical-host host
)))
1652 (setf (logical-host-canon-transls host
)
1653 (canonicalize-logical-pathname-translations translations host
))
1654 (setf (logical-host-translations host
) translations
)))
1656 (defun translate-logical-pathname (pathname &key
)
1658 "Translate PATHNAME to a physical pathname, which is returned."
1659 (declare (type pathname-designator pathname
)
1660 (values (or null pathname
)))
1663 (dolist (x (logical-host-canon-transls (%pathname-host pathname
))
1664 (error 'simple-file-error
1666 :format-control
"no translation for ~S"
1667 :format-arguments
(list pathname
)))
1668 (destructuring-bind (from to
) x
1669 (when (pathname-match-p pathname from
)
1670 (return (translate-logical-pathname
1671 (translate-pathname pathname from to
)))))))
1673 (t (translate-logical-pathname (pathname pathname
)))))
1675 (defvar *logical-pathname-defaults
*
1676 (%make-logical-pathname
1677 (make-logical-host :name
(logical-word-or-lose "BOGUS"))
1678 :unspecific nil nil nil nil
))
1680 (defun load-logical-pathname-translations (host)
1682 (declare (type string host
)
1683 (values (member t nil
)))
1684 (if (find-logical-host host nil
)
1685 ;; This host is already defined, all is well and good.
1687 ;; ANSI: "The specific nature of the search is
1688 ;; implementation-defined." SBCL: doesn't search at all
1690 ;; FIXME: now that we have a SYS host that the system uses, it
1691 ;; might be cute to search in "SYS:TRANSLATIONS;<name>.LISP"
1692 (error "logical host ~S not found" host
)))