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))
18 (def!struct (unix-host
19 (:make-load-form-fun make-unix-host-load-form)
21 (parse #'parse-unix-namestring)
22 (unparse #'unparse-unix-namestring)
23 (unparse-host #'unparse-unix-host)
24 (unparse-directory #'unparse-unix-directory)
25 (unparse-file #'unparse-unix-file)
26 (unparse-enough #'unparse-unix-enough)
27 (customary-case :lower))))
29 (defvar *unix-host* (make-unix-host))
31 (defun make-unix-host-load-form (host)
32 (declare (ignore host))
35 ;;; Return a value suitable, e.g., for preinitializing
36 ;;; *DEFAULT-PATHNAME-DEFAULTS* before *DEFAULT-PATHNAME-DEFAULTS* is
37 ;;; initialized (at which time we can't safely call e.g. #'PATHNAME).
38 (defun make-trivial-default-pathname ()
39 (%make-pathname *unix-host* nil nil nil nil :newest))
43 (def!method print-object ((pathname pathname) stream)
44 (let ((namestring (handler-case (namestring pathname)
47 (format stream "#P~S" namestring)
48 (print-unreadable-object (pathname stream :type t)
50 "~@<(with no namestring) ~_:HOST ~S ~_:DEVICE ~S ~_:DIRECTORY ~S ~
51 ~_:NAME ~S ~_:TYPE ~S ~_:VERSION ~S~:>"
52 (%pathname-host pathname)
53 (%pathname-device pathname)
54 (%pathname-directory pathname)
55 (%pathname-name pathname)
56 (%pathname-type pathname)
57 (%pathname-version pathname))))))
59 (def!method make-load-form ((pathname pathname) &optional environment)
60 (make-load-form-saving-slots pathname :environment environment))
62 ;;; The potential conflict with search lists requires isolating the
63 ;;; printed representation to use the i/o macro #.(logical-pathname
64 ;;; <path-designator>).
66 ;;; FIXME: We don't use search lists any more, so that comment is
68 (def!method print-object ((pathname logical-pathname) stream)
69 (let ((namestring (handler-case (namestring pathname)
72 (format stream "#.(CL:LOGICAL-PATHNAME ~S)" namestring)
73 (print-unreadable-object (pathname stream :type t)
76 "~_:HOST ~S ~_:DIRECTORY ~S ~_:FILE ~S ~_:NAME ~S ~_:VERSION ~S"
77 (%pathname-host pathname)
78 (%pathname-directory pathname)
79 (%pathname-name pathname)
80 (%pathname-type pathname)
81 (%pathname-version pathname))))))
83 ;;; A pathname is logical if the host component is a logical host.
84 ;;; This constructor is used to make an instance of the correct type
85 ;;; from parsed arguments.
86 (defun %make-maybe-logical-pathname (host device directory name type version)
87 ;; We canonicalize logical pathname components to uppercase. ANSI
88 ;; doesn't strictly require this, leaving it up to the implementor;
89 ;; but the arguments given in the X3J13 cleanup issue
90 ;; PATHNAME-LOGICAL:ADD seem compelling: we should canonicalize the
91 ;; case, and uppercase is the ordinary way to do that.
92 (flet ((upcase-maybe (x) (typecase x (string (logical-word-or-lose x)) (t x))))
93 (if (typep host 'logical-host)
94 (%make-logical-pathname host
96 (mapcar #'upcase-maybe directory)
100 (%make-pathname host device directory name type version))))
102 ;;; Hash table searching maps a logical pathname's host to its
103 ;;; physical pathname translation.
104 (defvar *logical-hosts* (make-hash-table :test 'equal))
108 (def!method make-load-form ((pattern pattern) &optional environment)
109 (make-load-form-saving-slots pattern :environment environment))
111 (def!method print-object ((pattern pattern) stream)
112 (print-unreadable-object (pattern stream :type t)
114 (let ((*print-escape* t))
115 (pprint-fill stream (pattern-pieces pattern) nil))
116 (prin1 (pattern-pieces pattern) stream))))
118 (defun pattern= (pattern1 pattern2)
119 (declare (type pattern pattern1 pattern2))
120 (let ((pieces1 (pattern-pieces pattern1))
121 (pieces2 (pattern-pieces pattern2)))
122 (and (= (length pieces1) (length pieces2))
123 (every (lambda (piece1 piece2)
126 (and (simple-string-p piece2)
127 (string= piece1 piece2)))
130 (eq (car piece1) (car piece2))
131 (string= (cdr piece1) (cdr piece2))))
133 (eq piece1 piece2))))
137 ;;; If the string matches the pattern returns the multiple values T
138 ;;; and a list of the matched strings.
139 (defun pattern-matches (pattern string)
140 (declare (type pattern pattern)
141 (type simple-string string))
142 (let ((len (length string)))
143 (labels ((maybe-prepend (subs cur-sub chars)
145 (let* ((len (length chars))
146 (new (make-string len))
149 (setf (schar new (decf index)) char))
152 (matches (pieces start subs cur-sub chars)
155 (values t (maybe-prepend subs cur-sub chars))
157 (let ((piece (car pieces)))
160 (let ((end (+ start (length piece))))
162 (string= piece string
163 :start2 start :end2 end)
164 (matches (cdr pieces) end
165 (maybe-prepend subs cur-sub chars)
171 (let ((char (schar string start)))
172 (if (find char (cdr piece) :test #'char=)
173 (matches (cdr pieces) (1+ start) subs t
174 (cons char chars))))))))
175 ((member :single-char-wild)
177 (matches (cdr pieces) (1+ start) subs t
178 (cons (schar string start) chars))))
179 ((member :multi-char-wild)
180 (multiple-value-bind (won new-subs)
181 (matches (cdr pieces) start subs t chars)
185 (matches pieces (1+ start) subs t
186 (cons (schar string start)
188 (multiple-value-bind (won subs)
189 (matches (pattern-pieces pattern) 0 nil nil nil)
190 (values won (reverse subs))))))
192 ;;; PATHNAME-MATCH-P for directory components
193 (defun directory-components-match (thing wild)
196 ;; If THING has a null directory, assume that it matches
197 ;; (:ABSOLUTE :WILD-INFERIORS) or (:RELATIVE :WILD-INFERIORS).
200 (member (first wild) '(:absolute :relative))
201 (eq (second wild) :wild-inferiors))
203 (let ((wild1 (first wild)))
204 (if (eq wild1 :wild-inferiors)
205 (let ((wild-subdirs (rest wild)))
206 (or (null wild-subdirs)
208 (when (directory-components-match thing wild-subdirs)
211 (unless thing (return nil)))))
213 (components-match (first thing) wild1)
214 (directory-components-match (rest thing)
217 ;;; Return true if pathname component THING is matched by WILD. (not
219 (defun components-match (thing wild)
220 (declare (type (or pattern symbol simple-string integer) thing wild))
225 ;; String is matched by itself, a matching pattern or :WILD.
228 (values (pattern-matches wild thing)))
230 (string= thing wild))))
232 ;; A pattern is only matched by an identical pattern.
233 (and (pattern-p wild) (pattern= thing wild)))
235 ;; An integer (version number) is matched by :WILD or the
236 ;; same integer. This branch will actually always be NIL as
237 ;; long as the version is a fixnum.
240 ;;; a predicate for comparing two pathname slot component sub-entries
241 (defun compare-component (this that)
245 (and (simple-string-p that)
246 (string= this that)))
248 (and (pattern-p that)
249 (pattern= this that)))
252 (compare-component (car this) (car that))
253 (compare-component (cdr this) (cdr that)))))))
255 ;;;; pathname functions
257 (defun pathname= (pathname1 pathname2)
258 (declare (type pathname pathname1)
259 (type pathname pathname2))
260 (and (eq (%pathname-host pathname1)
261 (%pathname-host pathname2))
262 (compare-component (%pathname-device pathname1)
263 (%pathname-device pathname2))
264 (compare-component (%pathname-directory pathname1)
265 (%pathname-directory pathname2))
266 (compare-component (%pathname-name pathname1)
267 (%pathname-name pathname2))
268 (compare-component (%pathname-type pathname1)
269 (%pathname-type pathname2))
270 (compare-component (%pathname-version pathname1)
271 (%pathname-version pathname2))))
273 ;;; Convert PATHNAME-DESIGNATOR (a pathname, or string, or
274 ;;; stream), into a pathname in pathname.
276 ;;; FIXME: was rewritten, should be tested (or rewritten again, this
277 ;;; time using ONCE-ONLY, *then* tested)
278 ;;; FIXME: become SB!XC:DEFMACRO inside EVAL-WHEN (COMPILE EVAL)?
279 (defmacro with-pathname ((pathname pathname-designator) &body body)
280 (let ((pd0 (gensym)))
281 `(let* ((,pd0 ,pathname-designator)
282 (,pathname (etypecase ,pd0
284 (string (parse-namestring ,pd0))
285 (file-stream (file-name ,pd0)))))
288 ;;; Convert the var, a host or string name for a host, into a
289 ;;; LOGICAL-HOST structure or nil if not defined.
291 ;;; pw notes 1/12/97 this potentially useful macro is not used anywhere
292 ;;; and 'find-host' is not defined. 'find-logical-host' seems to be needed.
294 (defmacro with-host ((var expr) &body body)
295 `(let ((,var (let ((,var ,expr))
298 (string (find-logical-host ,var nil))
303 (defun pathname (thing)
305 "Convert thing (a pathname, string or stream) into a pathname."
306 (declare (type pathname-designator thing))
307 (with-pathname (pathname thing)
310 ;;; Change the case of thing if DIDDLE-P.
311 (defun maybe-diddle-case (thing diddle-p)
312 (if (and diddle-p (not (or (symbolp thing) (integerp thing))))
313 (labels ((check-for (pred in)
316 (dolist (piece (pattern-pieces in))
317 (when (typecase piece
319 (check-for pred piece))
323 (check-for pred (cdr piece))))))
327 (when (check-for pred x)
330 (dotimes (i (length in))
331 (when (funcall pred (schar in i))
334 (diddle-with (fun thing)
338 (mapcar (lambda (piece)
346 (funcall fun (cdr piece))))
351 (pattern-pieces thing))))
358 (let ((any-uppers (check-for #'upper-case-p thing))
359 (any-lowers (check-for #'lower-case-p thing)))
360 (cond ((and any-uppers any-lowers)
361 ;; mixed case, stays the same
364 ;; all uppercase, becomes all lower case
365 (diddle-with (lambda (x) (if (stringp x)
369 ;; all lowercase, becomes all upper case
370 (diddle-with (lambda (x) (if (stringp x)
374 ;; no letters? I guess just leave it.
378 (defun merge-directories (dir1 dir2 diddle-case)
379 (if (or (eq (car dir1) :absolute)
384 (if (and (eq dir :back)
386 (not (eq (car results) :back)))
388 (push dir results))))
389 (dolist (dir (maybe-diddle-case dir2 diddle-case))
391 (dolist (dir (cdr dir1))
395 (defun merge-pathnames (pathname
397 (defaults *default-pathname-defaults*)
398 (default-version :newest))
400 "Construct a filled in pathname by completing the unspecified components
402 (declare (type pathname-designator pathname)
403 (type pathname-designator defaults)
405 (with-pathname (defaults defaults)
406 (let ((pathname (let ((*default-pathname-defaults* defaults))
407 (pathname pathname))))
408 (let* ((default-host (%pathname-host defaults))
409 (pathname-host (%pathname-host pathname))
411 (and default-host pathname-host
412 (not (eq (host-customary-case default-host)
413 (host-customary-case pathname-host))))))
414 (%make-maybe-logical-pathname
415 (or pathname-host default-host)
416 (or (%pathname-device pathname)
417 (maybe-diddle-case (%pathname-device defaults)
419 (merge-directories (%pathname-directory pathname)
420 (%pathname-directory defaults)
422 (or (%pathname-name pathname)
423 (maybe-diddle-case (%pathname-name defaults)
425 (or (%pathname-type pathname)
426 (maybe-diddle-case (%pathname-type defaults)
428 (or (%pathname-version pathname)
429 default-version))))))
431 (defun import-directory (directory diddle-case)
434 ((member :wild) '(:absolute :wild-inferiors))
435 ((member :unspecific) '(:relative))
438 (results (pop directory))
439 (dolist (piece directory)
440 (cond ((member piece '(:wild :wild-inferiors :up :back))
442 ((or (simple-string-p piece) (pattern-p piece))
443 (results (maybe-diddle-case piece diddle-case)))
445 (results (maybe-diddle-case (coerce piece 'simple-string)
448 (error "~S is not allowed as a directory component." piece))))
452 ,(maybe-diddle-case directory diddle-case)))
455 ,(maybe-diddle-case (coerce directory 'simple-string)
458 (defun make-pathname (&key host
463 (version nil versionp)
467 "Makes a new pathname from the component arguments. Note that host is
468 a host-structure or string."
469 (declare (type (or string host pathname-component-tokens) host)
470 (type (or string pathname-component-tokens) device)
471 (type (or list string pattern pathname-component-tokens) directory)
472 (type (or string pattern pathname-component-tokens) name type)
473 (type (or integer pathname-component-tokens (member :newest))
475 (type (or pathname-designator null) defaults)
476 (type (member :common :local) case))
477 (let* ((defaults (when defaults
478 (with-pathname (defaults defaults) defaults)))
479 (default-host (if defaults
480 (%pathname-host defaults)
481 (pathname-host *default-pathname-defaults*)))
482 ;; Raymond Toy writes: CLHS says make-pathname can take a
483 ;; string (as a logical-host) for the host part. We map that
484 ;; string into the corresponding logical host structure.
486 ;; Paul Werkowski writes:
487 ;; HyperSpec says for the arg to MAKE-PATHNAME;
488 ;; "host---a valid physical pathname host. ..."
489 ;; where it probably means -- a valid pathname host.
490 ;; "valid pathname host n. a valid physical pathname host or
491 ;; a valid logical pathname host."
493 ;; "valid physical pathname host n. any of a string,
494 ;; a list of strings, or the symbol :unspecific,
495 ;; that is recognized by the implementation as the name of a host."
496 ;; "valid logical pathname host n. a string that has been defined
497 ;; as the name of a logical host. ..."
498 ;; HS is silent on what happens if the :HOST arg is NOT one of these.
499 ;; It seems an error message is appropriate.
501 (host host) ; A valid host, use it.
502 ((string 0) *unix-host*) ; "" cannot be a logical host
503 (string (find-logical-host host t)) ; logical-host or lose.
504 (t default-host))) ; unix-host
505 (diddle-args (and (eq (host-customary-case host) :lower)
508 (not (eq (host-customary-case host)
509 (host-customary-case default-host))))
510 (dev (if devp device (if defaults (%pathname-device defaults))))
511 (dir (import-directory directory diddle-args))
514 (defaults (%pathname-version defaults))
516 (when (and defaults (not dirp))
518 (merge-directories dir
519 (%pathname-directory defaults)
522 (macrolet ((pick (var varp field)
523 `(cond ((or (simple-string-p ,var)
525 (maybe-diddle-case ,var diddle-args))
527 (maybe-diddle-case (coerce ,var 'simple-string)
530 (maybe-diddle-case ,var diddle-args))
532 (maybe-diddle-case (,field defaults)
536 (%make-maybe-logical-pathname host
537 dev ; forced to :UNSPECIFIC when logical
539 (pick name namep %pathname-name)
540 (pick type typep %pathname-type)
543 (defun pathname-host (pathname &key (case :local))
545 "Return PATHNAME's host."
546 (declare (type pathname-designator pathname)
547 (type (member :local :common) case)
550 (with-pathname (pathname pathname)
551 (%pathname-host pathname)))
553 (defun pathname-device (pathname &key (case :local))
555 "Return PATHNAME's device."
556 (declare (type pathname-designator pathname)
557 (type (member :local :common) case))
558 (with-pathname (pathname pathname)
559 (maybe-diddle-case (%pathname-device pathname)
560 (and (eq case :common)
561 (eq (host-customary-case
562 (%pathname-host pathname))
565 (defun pathname-directory (pathname &key (case :local))
567 "Return PATHNAME's directory."
568 (declare (type pathname-designator pathname)
569 (type (member :local :common) case))
570 (with-pathname (pathname pathname)
571 (maybe-diddle-case (%pathname-directory pathname)
572 (and (eq case :common)
573 (eq (host-customary-case
574 (%pathname-host pathname))
576 (defun pathname-name (pathname &key (case :local))
578 "Return PATHNAME's name."
579 (declare (type pathname-designator pathname)
580 (type (member :local :common) case))
581 (with-pathname (pathname pathname)
582 (maybe-diddle-case (%pathname-name pathname)
583 (and (eq case :common)
584 (eq (host-customary-case
585 (%pathname-host pathname))
588 (defun pathname-type (pathname &key (case :local))
590 "Return PATHNAME's type."
591 (declare (type pathname-designator pathname)
592 (type (member :local :common) case))
593 (with-pathname (pathname pathname)
594 (maybe-diddle-case (%pathname-type pathname)
595 (and (eq case :common)
596 (eq (host-customary-case
597 (%pathname-host pathname))
600 (defun pathname-version (pathname)
602 "Return PATHNAME's version."
603 (declare (type pathname-designator pathname))
604 (with-pathname (pathname pathname)
605 (%pathname-version pathname)))
609 ;;; Handle the case for PARSE-NAMESTRING parsing a potentially
610 ;;; syntactically valid logical namestring with an explicit host.
612 ;;; This then isn't fully general -- we are relying on the fact that
613 ;;; we will only pass to parse-namestring namestring with an explicit
614 ;;; logical host, so that we can pass the host return from
615 ;;; parse-logical-namestring through to %PARSE-NAMESTRING as a truth
616 ;;; value. Yeah, this is probably a KLUDGE - CSR, 2002-04-18
617 (defun parseable-logical-namestring-p (namestr start end)
620 ((namestring-parse-error (lambda (c)
623 (let ((colon (position #\: namestr :start start :end end)))
625 (let ((potential-host
626 (logical-word-or-lose (subseq namestr start colon))))
627 ;; depending on the outcome of CSR comp.lang.lisp post
628 ;; "can PARSE-NAMESTRING create logical hosts", we may need
629 ;; to do things with potential-host (create it
630 ;; temporarily, parse the namestring and unintern the
631 ;; logical host potential-host on failure.
632 (declare (ignore potential-host))
635 ((simple-type-error (lambda (c)
638 (parse-logical-namestring namestr start end))))
639 ;; if we got this far, we should have an explicit host
640 ;; (first return value of parse-logical-namestring)
644 ;;; Handle the case where PARSE-NAMESTRING is actually parsing a
645 ;;; namestring. We pick off the :JUNK-ALLOWED case then find a host to
646 ;;; use for parsing, call the parser, then check whether the host matches.
647 (defun %parse-namestring (namestr host defaults start end junk-allowed)
648 (declare (type (or host null) host)
649 (type string namestr)
651 (type (or index null) end))
655 (%parse-namestring namestr host defaults start end nil)
656 (namestring-parse-error (condition)
657 (values nil (namestring-parse-error-offset condition)))))
659 (let* ((end (%check-vector-sequence-bounds namestr start end)))
660 (multiple-value-bind (new-host device directory file type version)
661 ;; Comments below are quotes from the HyperSpec
662 ;; PARSE-NAMESTRING entry, reproduced here to demonstrate
663 ;; that we actually have to do things this way rather than
664 ;; some possibly more logical way. - CSR, 2002-04-18
666 ;; "If host is a logical host then thing is parsed as a
667 ;; logical pathname namestring on the host."
668 (host (funcall (host-parse host) namestr start end))
669 ;; "If host is nil and thing is a syntactically valid
670 ;; logical pathname namestring containing an explicit
671 ;; host, then it is parsed as a logical pathname
673 ((parseable-logical-namestring-p namestr start end)
674 (parse-logical-namestring namestr start end))
675 ;; "If host is nil, default-pathname is a logical
676 ;; pathname, and thing is a syntactically valid logical
677 ;; pathname namestring without an explicit host, then it
678 ;; is parsed as a logical pathname namestring on the
679 ;; host that is the host component of default-pathname."
681 ;; "Otherwise, the parsing of thing is
682 ;; implementation-defined."
684 ;; Both clauses are handled here, as the default
685 ;; *DEFAULT-PATHNAME-DEFAULTS has a SB-IMPL::UNIX-HOST
687 ((pathname-host defaults)
688 (funcall (host-parse (pathname-host defaults))
692 ;; I don't think we should ever get here, as the default
693 ;; host will always have a non-null HOST, given that we
694 ;; can't create a new pathname without going through
695 ;; *DEFAULT-PATHNAME-DEFAULTS*, which has a non-null
697 (t (bug "Fallen through COND in %PARSE-NAMESTRING")))
698 (when (and host new-host (not (eq new-host host)))
699 (error 'simple-type-error
701 ;; Note: ANSI requires that this be a TYPE-ERROR,
702 ;; but there seems to be no completely correct
703 ;; value to use for TYPE-ERROR-EXPECTED-TYPE.
704 ;; Instead, we return a sort of "type error allowed
705 ;; type", trying to say "it would be OK if you
706 ;; passed NIL as the host value" but not mentioning
707 ;; that a matching string would be OK too.
710 "The host in the namestring, ~S,~@
711 does not match the explicit HOST argument, ~S."
712 :format-arguments (list new-host host)))
713 (let ((pn-host (or new-host host (pathname-host defaults))))
714 (values (%make-maybe-logical-pathname
715 pn-host device directory file type version)
718 ;;; If NAMESTR begins with a colon-terminated, defined, logical host,
719 ;;; then return that host, otherwise return NIL.
720 (defun extract-logical-host-prefix (namestr start end)
721 (declare (type simple-base-string namestr)
722 (type index start end)
723 (values (or logical-host null)))
724 (let ((colon-pos (position #\: namestr :start start :end end)))
726 (values (gethash (nstring-upcase (subseq namestr start colon-pos))
730 (defun parse-namestring (thing
733 (defaults *default-pathname-defaults*)
734 &key (start 0) end junk-allowed)
735 (declare (type pathname-designator thing)
736 (type (or list host string (member :unspecific)) host)
737 (type pathname defaults)
739 (type (or index null) end)
740 (type (or t null) junk-allowed)
741 (values (or null pathname) (or null index)))
742 ;; Generally, redundant specification of information in software,
743 ;; whether in code or in comments, is bad. However, the ANSI spec
744 ;; for this is messy enough that it's hard to hold in short-term
745 ;; memory, so I've recorded these redundant notes on the
746 ;; implications of the ANSI spec.
748 ;; According to the ANSI spec, HOST can be a valid pathname host, or
749 ;; a logical host, or NIL.
751 ;; A valid pathname host can be a valid physical pathname host or a
752 ;; valid logical pathname host.
754 ;; A valid physical pathname host is "any of a string, a list of
755 ;; strings, or the symbol :UNSPECIFIC, that is recognized by the
756 ;; implementation as the name of a host". In SBCL as of 0.6.9.8,
757 ;; that means :UNSPECIFIC: though someday we might want to
758 ;; generalize it to allow strings like "RTFM.MIT.EDU" or lists like
759 ;; '("RTFM" "MIT" "EDU"), that's not supported now.
761 ;; A valid logical pathname host is a string which has been defined as
762 ;; the name of a logical host, as with LOAD-LOGICAL-PATHNAME-TRANSLATIONS.
764 ;; A logical host is an object of implementation-dependent nature. In
765 ;; SBCL, it's a member of the HOST class (a subclass of STRUCTURE-OBJECT).
766 (let ((found-host (etypecase host
768 ;; This is a special host. It's not valid as a
769 ;; logical host, so it is a sensible thing to
770 ;; designate the physical Unix host object. So
774 ;; In general ANSI-compliant Common Lisps, a
775 ;; string might also be a physical pathname host,
776 ;; but ANSI leaves this up to the implementor,
777 ;; and in SBCL we don't do it, so it must be a
779 (find-logical-host host))
780 ((or null (member :unspecific))
781 ;; CLHS says that HOST=:UNSPECIFIC has
782 ;; implementation-defined behavior. We
783 ;; just turn it into NIL.
786 ;; ANSI also allows LISTs to designate hosts,
787 ;; but leaves its interpretation
788 ;; implementation-defined. Our interpretation
789 ;; is that it's unsupported.:-|
790 (error "A LIST representing a pathname host is not ~
791 supported in this implementation:~% ~S"
795 (declare (type (or null host) found-host))
798 (%parse-namestring thing found-host defaults start end junk-allowed))
800 (%parse-namestring (coerce thing 'simple-string)
801 found-host defaults start end junk-allowed))
803 (let ((defaulted-host (or found-host (%pathname-host defaults))))
804 (declare (type host defaulted-host))
805 (unless (eq defaulted-host (%pathname-host thing))
806 (error "The HOST argument doesn't match the pathname host:~% ~
808 defaulted-host (%pathname-host thing))))
809 (values thing start))
811 (let ((name (file-name thing)))
813 (error "can't figure out the file associated with stream:~% ~S"
815 (values name nil))))))
817 (defun namestring (pathname)
819 "Construct the full (name)string form of the pathname."
820 (declare (type pathname-designator pathname)
821 (values (or null simple-base-string)))
822 (with-pathname (pathname pathname)
824 (let ((host (%pathname-host pathname)))
826 (error "can't determine the namestring for pathnames with no ~
827 host:~% ~S" pathname))
828 (funcall (host-unparse host) pathname)))))
830 (defun host-namestring (pathname)
832 "Return a string representation of the name of the host in the pathname."
833 (declare (type pathname-designator pathname)
834 (values (or null simple-base-string)))
835 (with-pathname (pathname pathname)
836 (let ((host (%pathname-host pathname)))
838 (funcall (host-unparse-host host) pathname)
840 "can't determine the namestring for pathnames with no host:~% ~S"
843 (defun directory-namestring (pathname)
845 "Return a string representation of the directories used in the pathname."
846 (declare (type pathname-designator pathname)
847 (values (or null simple-base-string)))
848 (with-pathname (pathname pathname)
849 (let ((host (%pathname-host pathname)))
851 (funcall (host-unparse-directory host) pathname)
853 "can't determine the namestring for pathnames with no host:~% ~S"
856 (defun file-namestring (pathname)
858 "Return a string representation of the name used in the pathname."
859 (declare (type pathname-designator pathname)
860 (values (or null simple-base-string)))
861 (with-pathname (pathname pathname)
862 (let ((host (%pathname-host pathname)))
864 (funcall (host-unparse-file host) pathname)
866 "can't determine the namestring for pathnames with no host:~% ~S"
869 (defun enough-namestring (pathname
871 (defaults *default-pathname-defaults*))
873 "Return an abbreviated pathname sufficent to identify the pathname relative
875 (declare (type pathname-designator pathname))
876 (with-pathname (pathname pathname)
877 (let ((host (%pathname-host pathname)))
879 (with-pathname (defaults defaults)
880 (funcall (host-unparse-enough host) pathname defaults))
882 "can't determine the namestring for pathnames with no host:~% ~S"
887 (defun wild-pathname-p (pathname &optional field-key)
889 "Predicate for determining whether pathname contains any wildcards."
890 (declare (type pathname-designator pathname)
891 (type (member nil :host :device :directory :name :type :version)
893 (with-pathname (pathname pathname)
895 (or (pattern-p x) (member x '(:wild :wild-inferiors)))))
898 (or (wild-pathname-p pathname :host)
899 (wild-pathname-p pathname :device)
900 (wild-pathname-p pathname :directory)
901 (wild-pathname-p pathname :name)
902 (wild-pathname-p pathname :type)
903 (wild-pathname-p pathname :version)))
904 (:host (frob (%pathname-host pathname)))
905 (:device (frob (%pathname-host pathname)))
906 (:directory (some #'frob (%pathname-directory pathname)))
907 (:name (frob (%pathname-name pathname)))
908 (:type (frob (%pathname-type pathname)))
909 (:version (frob (%pathname-version pathname)))))))
911 (defun pathname-match-p (in-pathname in-wildname)
913 "Pathname matches the wildname template?"
914 (declare (type pathname-designator in-pathname))
915 (with-pathname (pathname in-pathname)
916 (with-pathname (wildname in-wildname)
917 (macrolet ((frob (field &optional (op 'components-match))
918 `(or (null (,field wildname))
919 (,op (,field pathname) (,field wildname)))))
920 (and (or (null (%pathname-host wildname))
921 (eq (%pathname-host wildname) (%pathname-host pathname)))
922 (frob %pathname-device)
923 (frob %pathname-directory directory-components-match)
924 (frob %pathname-name)
925 (frob %pathname-type)
926 (frob %pathname-version))))))
928 ;;; Place the substitutions into the pattern and return the string or pattern
929 ;;; that results. If DIDDLE-CASE is true, we diddle the result case as well,
930 ;;; in case we are translating between hosts with difference conventional case.
931 ;;; The second value is the tail of subs with all of the values that we used up
932 ;;; stripped off. Note that PATTERN-MATCHES matches all consecutive wildcards
933 ;;; as a single string, so we ignore subsequent contiguous wildcards.
934 (defun substitute-into (pattern subs diddle-case)
935 (declare (type pattern pattern)
937 (values (or simple-base-string pattern) list))
938 (let ((in-wildcard nil)
941 (dolist (piece (pattern-pieces pattern))
942 (cond ((simple-string-p piece)
944 (setf in-wildcard nil))
949 (error "not enough wildcards in FROM pattern to match ~
952 (let ((sub (pop subs)))
956 (push (apply #'concatenate 'simple-string
959 (dolist (piece (pattern-pieces sub))
960 (push piece pieces)))
964 (error "can't substitute this into the middle of a word:~
969 (push (apply #'concatenate 'simple-string (nreverse strings))
973 (if (and pieces (simple-string-p (car pieces)) (null (cdr pieces)))
975 (make-pattern (nreverse pieces)))
979 ;;; Called when we can't see how source and from matched.
980 (defun didnt-match-error (source from)
981 (error "Pathname components from SOURCE and FROM args to TRANSLATE-PATHNAME~@
982 did not match:~% ~S ~S"
985 ;;; Do TRANSLATE-COMPONENT for all components except host and directory.
986 (defun translate-component (source from to diddle-case)
993 (if (pattern= from source)
995 (didnt-match-error source from)))
997 (multiple-value-bind (won subs) (pattern-matches from source)
999 (values (substitute-into to subs diddle-case))
1000 (didnt-match-error source from))))
1002 (maybe-diddle-case source diddle-case))))
1004 (values (substitute-into to (list source) diddle-case)))
1006 (if (components-match source from)
1007 (maybe-diddle-case source diddle-case)
1008 (didnt-match-error source from)))))
1010 (maybe-diddle-case source diddle-case))
1012 (if (components-match source from)
1014 (didnt-match-error source from)))))
1016 ;;; Return a list of all the things that we want to substitute into the TO
1017 ;;; pattern (the things matched by from on source.) When From contains
1018 ;;; :WILD-INFERIORS, the result contains a sublist of the matched source
1020 (defun compute-directory-substitutions (orig-source orig-from)
1021 (let ((source orig-source)
1026 (unless (every (lambda (x) (eq x :wild-inferiors)) from)
1027 (didnt-match-error orig-source orig-from))
1030 (unless from (didnt-match-error orig-source orig-from))
1031 (let ((from-part (pop from))
1032 (source-part (pop source)))
1035 (typecase source-part
1037 (if (pattern= from-part source-part)
1039 (didnt-match-error orig-source orig-from)))
1041 (multiple-value-bind (won new-subs)
1042 (pattern-matches from-part source-part)
1044 (dolist (sub new-subs)
1046 (didnt-match-error orig-source orig-from))))
1048 (didnt-match-error orig-source orig-from))))
1051 ((member :wild-inferiors)
1052 (let ((remaining-source (cons source-part source)))
1055 (when (directory-components-match remaining-source from)
1057 (unless remaining-source
1058 (didnt-match-error orig-source orig-from))
1059 (res (pop remaining-source)))
1061 (setq source remaining-source))))
1063 (unless (and (simple-string-p source-part)
1064 (string= from-part source-part))
1065 (didnt-match-error orig-source orig-from)))
1067 (didnt-match-error orig-source orig-from)))))
1070 ;;; This is called by TRANSLATE-PATHNAME on the directory components
1071 ;;; of its argument pathnames to produce the result directory
1072 ;;; component. If this leaves the directory NIL, we return the source
1073 ;;; directory. The :RELATIVE or :ABSOLUTE is taken from the source
1074 ;;; directory, except if TO is :ABSOLUTE, in which case the result
1075 ;;; will be :ABSOLUTE.
1076 (defun translate-directories (source from to diddle-case)
1077 (if (not (and source to from))
1078 (or (and to (null source) (remove :wild-inferiors to))
1079 (mapcar (lambda (x) (maybe-diddle-case x diddle-case)) source))
1081 ;; If TO is :ABSOLUTE, the result should still be :ABSOLUTE.
1082 (res (if (eq (first to) :absolute)
1085 (let ((subs-left (compute-directory-substitutions (rest source)
1087 (dolist (to-part (rest to))
1091 (let ((match (pop subs-left)))
1093 (error ":WILD-INFERIORS is not paired in from and to ~
1094 patterns:~% ~S ~S" from to))
1095 (res (maybe-diddle-case match diddle-case))))
1096 ((member :wild-inferiors)
1098 (let ((match (pop subs-left)))
1099 (unless (listp match)
1100 (error ":WILD-INFERIORS not paired in from and to ~
1101 patterns:~% ~S ~S" from to))
1103 (res (maybe-diddle-case x diddle-case)))))
1105 (multiple-value-bind
1107 (substitute-into to-part subs-left diddle-case)
1108 (setf subs-left new-subs-left)
1110 (t (res to-part)))))
1113 (defun translate-pathname (source from-wildname to-wildname &key)
1115 "Use the source pathname to translate the from-wildname's wild and
1116 unspecified elements into a completed to-pathname based on the to-wildname."
1117 (declare (type pathname-designator source from-wildname to-wildname))
1118 (with-pathname (source source)
1119 (with-pathname (from from-wildname)
1120 (with-pathname (to to-wildname)
1121 (let* ((source-host (%pathname-host source))
1122 (to-host (%pathname-host to))
1124 (and source-host to-host
1125 (not (eq (host-customary-case source-host)
1126 (host-customary-case to-host))))))
1127 (macrolet ((frob (field &optional (op 'translate-component))
1128 `(let ((result (,op (,field source)
1132 (if (eq result :error)
1133 (error "~S doesn't match ~S." source from)
1135 (%make-maybe-logical-pathname
1136 (or to-host source-host)
1137 (frob %pathname-device)
1138 (frob %pathname-directory translate-directories)
1139 (frob %pathname-name)
1140 (frob %pathname-type)
1141 (frob %pathname-version))))))))
1143 ;;;; logical pathname support. ANSI 92-102 specification.
1145 ;;;; As logical-pathname translations are loaded they are
1146 ;;;; canonicalized as patterns to enable rapid efficient translation
1147 ;;;; into physical pathnames.
1151 ;;; Canonicalize a logical pathname word by uppercasing it checking that it
1152 ;;; contains only legal characters.
1153 (defun logical-word-or-lose (word)
1154 (declare (string word))
1155 (when (string= word "")
1156 (error 'namestring-parse-error
1157 :complaint "Attempted to treat invalid logical hostname ~
1158 as a logical host:~% ~S"
1160 :namestring word :offset 0))
1161 (let ((word (string-upcase word)))
1162 (dotimes (i (length word))
1163 (let ((ch (schar word i)))
1164 (unless (or (alpha-char-p ch) (digit-char-p ch) (char= ch #\-))
1165 (error 'namestring-parse-error
1166 :complaint "logical namestring character which ~
1167 is not alphanumeric or hyphen:~% ~S"
1169 :namestring word :offset i))))
1172 ;;; Given a logical host or string, return a logical host. If ERROR-P
1173 ;;; is NIL, then return NIL when no such host exists.
1174 (defun find-logical-host (thing &optional (errorp t))
1177 (let ((found (gethash (logical-word-or-lose thing)
1179 (if (or found (not errorp))
1181 ;; This is the error signalled from e.g.
1182 ;; LOGICAL-PATHNAME-TRANSLATIONS when host is not a defined
1183 ;; host, and ANSI specifies that that's a TYPE-ERROR.
1184 (error 'simple-type-error
1186 ;; God only knows what ANSI expects us to use for
1187 ;; the EXPECTED-TYPE here. Maybe this will be OK..
1189 '(and string (satisfies logical-pathname-translations))
1190 :format-control "logical host not yet defined: ~S"
1191 :format-arguments (list thing)))))
1192 (logical-host thing)))
1194 ;;; Given a logical host name or host, return a logical host, creating
1195 ;;; a new one if necessary.
1196 (defun intern-logical-host (thing)
1197 (declare (values logical-host))
1198 (or (find-logical-host thing nil)
1199 (let* ((name (logical-word-or-lose thing))
1200 (new (make-logical-host :name name)))
1201 (setf (gethash name *logical-hosts*) new)
1204 ;;;; logical pathname parsing
1206 ;;; Deal with multi-char wildcards in a logical pathname token.
1207 (defun maybe-make-logical-pattern (namestring chunks)
1208 (let ((chunk (caar chunks)))
1209 (collect ((pattern))
1211 (len (length chunk)))
1212 (declare (fixnum last-pos))
1214 (when (= last-pos len) (return))
1215 (let ((pos (or (position #\* chunk :start last-pos) len)))
1216 (if (= pos last-pos)
1218 (error 'namestring-parse-error
1219 :complaint "double asterisk inside of logical ~
1222 :namestring namestring
1223 :offset (+ (cdar chunks) pos)))
1224 (pattern (subseq chunk last-pos pos)))
1227 (pattern :multi-char-wild))
1228 (setq last-pos (1+ pos)))))
1231 (make-pattern (pattern))
1232 (let ((x (car (pattern))))
1233 (if (eq x :multi-char-wild)
1237 ;;; Return a list of conses where the CDR is the start position and
1238 ;;; the CAR is a string (token) or character (punctuation.)
1239 (defun logical-chunkify (namestr start end)
1241 (do ((i start (1+ i))
1245 (chunks (cons (nstring-upcase (subseq namestr prev end)) prev))))
1246 (let ((ch (schar namestr i)))
1247 (unless (or (alpha-char-p ch) (digit-char-p ch)
1248 (member ch '(#\- #\*)))
1250 (chunks (cons (nstring-upcase (subseq namestr prev i)) prev)))
1252 (unless (member ch '(#\; #\: #\.))
1253 (error 'namestring-parse-error
1254 :complaint "illegal character for logical pathname:~% ~S"
1258 (chunks (cons ch i)))))
1261 ;;; Break up a logical-namestring, always a string, into its
1262 ;;; constituent parts.
1263 (defun parse-logical-namestring (namestr start end)
1264 (declare (type simple-base-string namestr)
1265 (type index start end))
1266 (collect ((directory))
1271 (labels ((expecting (what chunks)
1272 (unless (and chunks (simple-string-p (caar chunks)))
1273 (error 'namestring-parse-error
1274 :complaint "expecting ~A, got ~:[nothing~;~S~]."
1275 :args (list what (caar chunks) (caar chunks))
1277 :offset (if chunks (cdar chunks) end)))
1279 (parse-host (chunks)
1280 (case (caadr chunks)
1283 (find-logical-host (expecting "a host name" chunks)))
1284 (parse-relative (cddr chunks)))
1286 (parse-relative chunks))))
1287 (parse-relative (chunks)
1290 (directory :relative)
1291 (parse-directory (cdr chunks)))
1293 (directory :absolute) ; Assumption! Maybe revoked later.
1294 (parse-directory chunks))))
1295 (parse-directory (chunks)
1296 (case (caadr chunks)
1299 (let ((res (expecting "a directory name" chunks)))
1300 (cond ((string= res "..") :up)
1301 ((string= res "**") :wild-inferiors)
1303 (maybe-make-logical-pattern namestr chunks)))))
1304 (parse-directory (cddr chunks)))
1306 (parse-name chunks))))
1307 (parse-name (chunks)
1309 (expecting "a file name" chunks)
1310 (setq name (maybe-make-logical-pattern namestr chunks))
1311 (expecting-dot (cdr chunks))))
1312 (expecting-dot (chunks)
1314 (unless (eql (caar chunks) #\.)
1315 (error 'namestring-parse-error
1316 :complaint "expecting a dot, got ~S."
1317 :args (list (caar chunks))
1319 :offset (cdar chunks)))
1321 (parse-version (cdr chunks))
1322 (parse-type (cdr chunks)))))
1323 (parse-type (chunks)
1324 (expecting "a file type" chunks)
1325 (setq type (maybe-make-logical-pattern namestr chunks))
1326 (expecting-dot (cdr chunks)))
1327 (parse-version (chunks)
1328 (let ((str (expecting "a positive integer, * or NEWEST"
1331 ((string= str "*") (setq version :wild))
1332 ((string= str "NEWEST") (setq version :newest))
1334 (multiple-value-bind (res pos)
1335 (parse-integer str :junk-allowed t)
1336 (unless (and res (plusp res))
1337 (error 'namestring-parse-error
1338 :complaint "expected a positive integer, ~
1342 :offset (+ pos (cdar chunks))))
1343 (setq version res)))))
1345 (error 'namestring-parse-error
1346 :complaint "extra stuff after end of file name"
1348 :offset (cdadr chunks)))))
1349 (parse-host (logical-chunkify namestr start end)))
1350 (values host :unspecific (directory) name type version))))
1352 ;;; We can't initialize this yet because not all host methods are
1354 (defvar *logical-pathname-defaults*)
1356 (defun logical-pathname (pathspec)
1358 "Converts the pathspec argument to a logical-pathname and returns it."
1359 (declare (type (or logical-pathname string stream) pathspec)
1360 (values logical-pathname))
1361 (if (typep pathspec 'logical-pathname)
1363 (let ((res (parse-namestring pathspec nil *logical-pathname-defaults*)))
1364 (when (eq (%pathname-host res)
1365 (%pathname-host *logical-pathname-defaults*))
1366 (error "This logical namestring does not specify a host:~% ~S"
1370 ;;;; logical pathname unparsing
1372 (defun unparse-logical-directory (pathname)
1373 (declare (type pathname pathname))
1375 (let ((directory (%pathname-directory pathname)))
1377 (ecase (pop directory)
1378 (:absolute) ; nothing special
1379 (:relative (pieces ";")))
1380 (dolist (dir directory)
1381 (cond ((or (stringp dir) (pattern-p dir))
1382 (pieces (unparse-logical-piece dir))
1386 ((eq dir :wild-inferiors)
1389 (error "invalid directory component: ~S" dir))))))
1390 (apply #'concatenate 'simple-string (pieces))))
1392 (defun unparse-logical-piece (thing)
1394 (simple-string thing)
1396 (collect ((strings))
1397 (dolist (piece (pattern-pieces thing))
1399 (simple-string (strings piece))
1401 (cond ((eq piece :wild-inferiors)
1403 ((eq piece :multi-char-wild)
1405 (t (error "invalid keyword: ~S" piece))))))
1406 (apply #'concatenate 'simple-string (strings))))))
1408 ;;; Unparse a logical pathname string.
1409 (defun unparse-enough-namestring (pathname defaults)
1410 (let* ((path-directory (pathname-directory pathname))
1411 (def-directory (pathname-directory defaults))
1413 ;; Go down the directory lists to see what matches. What's
1414 ;; left is what we want, more or less.
1415 (cond ((and (eq (first path-directory) (first def-directory))
1416 (eq (first path-directory) :absolute))
1417 ;; Both paths are :ABSOLUTE, so find where the
1418 ;; common parts end and return what's left
1419 (do* ((p (rest path-directory) (rest p))
1420 (d (rest def-directory) (rest d)))
1421 ((or (endp p) (endp d)
1422 (not (equal (first p) (first d))))
1425 ;; At least one path is :RELATIVE, so just return the
1426 ;; original path. If the original path is :RELATIVE,
1427 ;; then that's the right one. If PATH-DIRECTORY is
1428 ;; :ABSOLUTE, we want to return that except when
1429 ;; DEF-DIRECTORY is :ABSOLUTE, as handled above. so return
1430 ;; the original directory.
1432 (make-pathname :host (pathname-host pathname)
1433 :directory enough-directory
1434 :name (pathname-name pathname)
1435 :type (pathname-type pathname)
1436 :version (pathname-version pathname))))
1438 (defun unparse-logical-namestring (pathname)
1439 (declare (type logical-pathname pathname))
1440 (concatenate 'simple-string
1441 (logical-host-name (%pathname-host pathname)) ":"
1442 (unparse-logical-directory pathname)
1443 (unparse-unix-file pathname)))
1445 ;;;; logical pathname translations
1447 ;;; Verify that the list of translations consists of lists and prepare
1448 ;;; canonical translations. (Parse pathnames and expand out wildcards
1450 (defun canonicalize-logical-pathname-translations (translation-list host)
1451 (declare (type list translation-list) (type host host)
1453 (mapcar (lambda (translation)
1454 (destructuring-bind (from to) translation
1455 (list (if (typep from 'logical-pathname)
1457 (parse-namestring from host))
1461 (defun logical-pathname-translations (host)
1463 "Return the (logical) host object argument's list of translations."
1464 (declare (type (or string logical-host) host)
1466 (logical-host-translations (find-logical-host host)))
1468 (defun (setf logical-pathname-translations) (translations host)
1470 "Set the translations list for the logical host argument."
1471 (declare (type (or string logical-host) host)
1472 (type list translations)
1474 (let ((host (intern-logical-host host)))
1475 (setf (logical-host-canon-transls host)
1476 (canonicalize-logical-pathname-translations translations host))
1477 (setf (logical-host-translations host) translations)))
1479 (defun translate-logical-pathname (pathname &key)
1481 "Translate PATHNAME to a physical pathname, which is returned."
1482 (declare (type pathname-designator pathname)
1483 (values (or null pathname)))
1486 (dolist (x (logical-host-canon-transls (%pathname-host pathname))
1487 (error 'simple-file-error
1489 :format-control "no translation for ~S"
1490 :format-arguments (list pathname)))
1491 (destructuring-bind (from to) x
1492 (when (pathname-match-p pathname from)
1493 (return (translate-logical-pathname
1494 (translate-pathname pathname from to)))))))
1496 (t (translate-logical-pathname (pathname pathname)))))
1498 (defvar *logical-pathname-defaults*
1499 (%make-logical-pathname (make-logical-host :name "BOGUS")
1506 (defun load-logical-pathname-translations (host)
1508 (declare (type string host)
1509 (values (member t nil)))
1510 (if (find-logical-host host nil)
1511 ;; This host is already defined, all is well and good.
1513 ;; ANSI: "The specific nature of the search is
1514 ;; implementation-defined." SBCL: doesn't search at all
1515 (error "logical host ~S not found" host)))