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!method print-object ((host host) stream)
19 (print-unreadable-object (host stream :type t :identity t)))
23 (def!method print-object ((pathname pathname) stream)
24 (let ((namestring (handler-case (namestring pathname)
27 (format stream "#P~S" namestring)
28 ;; FIXME: This code was rewritten and should be tested. (How does
29 ;; control get to this case anyhow? Perhaps we could just punt it?)
30 (print-unreadable-object (pathname stream :type t)
32 "(with no namestring) :HOST ~S :DEVICE ~S :DIRECTORY ~S ~
33 :NAME ~S :TYPE ~S :VERSION ~S"
34 (%pathname-host pathname)
35 (%pathname-device pathname)
36 (%pathname-directory pathname)
37 (%pathname-name pathname)
38 (%pathname-type pathname)
39 (%pathname-version pathname))))))
41 (def!method make-load-form ((pathname pathname) &optional environment)
42 (make-load-form-saving-slots pathname :environment environment))
44 ;;; The potential conflict with search-lists requires isolating the printed
45 ;;; representation to use the i/o macro #.(logical-pathname <path-designator>).
47 ;;; FIXME: We don't use search lists any more, so that comment is stale, right?
48 (def!method print-object ((pathname logical-pathname) stream)
49 (let ((namestring (handler-case (namestring pathname)
52 (format stream "#.(logical-pathname ~S)" namestring)
53 (print-unreadable-object (pathname stream :type t)
55 ":HOST ~S :DIRECTORY ~S :FILE ~S :NAME=~S :VERSION ~S"
56 (%pathname-host pathname)
57 (%pathname-directory pathname)
58 (%pathname-name pathname)
59 (%pathname-type pathname)
60 (%pathname-version pathname))))))
62 ;;; A pathname is logical if the host component is a logical-host.
63 ;;; This constructor is used to make an instance of the correct type
64 ;;; from parsed arguments.
65 (defun %make-pathname-object (host device directory name type version)
66 (if (typep host 'logical-host)
67 (%make-logical-pathname host :unspecific directory name type version)
68 (%make-pathname host device directory name type version)))
70 ;;; Hash table searching maps a logical-pathname's host to their physical
71 ;;; pathname translation.
72 (defvar *logical-hosts* (make-hash-table :test 'equal))
76 (def!method make-load-form ((pattern pattern) &optional environment)
77 (make-load-form-saving-slots pattern :environment environment))
79 (def!method print-object ((pattern pattern) stream)
80 (print-unreadable-object (pattern stream :type t)
82 (let ((*print-escape* t))
83 (pprint-fill stream (pattern-pieces pattern) nil))
84 (prin1 (pattern-pieces pattern) stream))))
86 (defun pattern= (pattern1 pattern2)
87 (declare (type pattern pattern1 pattern2))
88 (let ((pieces1 (pattern-pieces pattern1))
89 (pieces2 (pattern-pieces pattern2)))
90 (and (= (length pieces1) (length pieces2))
91 (every #'(lambda (piece1 piece2)
94 (and (simple-string-p piece2)
95 (string= piece1 piece2)))
98 (eq (car piece1) (car piece2))
99 (string= (cdr piece1) (cdr piece2))))
101 (eq piece1 piece2))))
105 ;;; If the string matches the pattern returns the multiple values T and a
106 ;;; list of the matched strings.
107 (defun pattern-matches (pattern string)
108 (declare (type pattern pattern)
109 (type simple-string string))
110 (let ((len (length string)))
111 (labels ((maybe-prepend (subs cur-sub chars)
113 (let* ((len (length chars))
114 (new (make-string len))
117 (setf (schar new (decf index)) char))
120 (matches (pieces start subs cur-sub chars)
123 (values t (maybe-prepend subs cur-sub chars))
125 (let ((piece (car pieces)))
128 (let ((end (+ start (length piece))))
130 (string= piece string
131 :start2 start :end2 end)
132 (matches (cdr pieces) end
133 (maybe-prepend subs cur-sub chars)
139 (let ((char (schar string start)))
140 (if (find char (cdr piece) :test #'char=)
141 (matches (cdr pieces) (1+ start) subs t
142 (cons char chars))))))))
143 ((member :single-char-wild)
145 (matches (cdr pieces) (1+ start) subs t
146 (cons (schar string start) chars))))
147 ((member :multi-char-wild)
148 (multiple-value-bind (won new-subs)
149 (matches (cdr pieces) start subs t chars)
153 (matches pieces (1+ start) subs t
154 (cons (schar string start)
156 (multiple-value-bind (won subs)
157 (matches (pattern-pieces pattern) 0 nil nil nil)
158 (values won (reverse subs))))))
160 ;;; Pathname-match-p for directory components.
161 (defun directory-components-match (thing wild)
165 (let ((wild1 (first wild)))
166 (if (eq wild1 :wild-inferiors)
167 (let ((wild-subdirs (rest wild)))
168 (or (null wild-subdirs)
170 (when (directory-components-match thing wild-subdirs)
173 (unless thing (return nil)))))
175 (components-match (first thing) wild1)
176 (directory-components-match (rest thing)
179 ;;; Return true if pathname component THING is matched by WILD. (not
181 (defun components-match (thing wild)
182 (declare (type (or pattern symbol simple-string integer) thing wild))
187 ;; String is matched by itself, a matching pattern or :WILD.
190 (values (pattern-matches wild thing)))
192 (string= thing wild))))
194 ;; A pattern is only matched by an identical pattern.
195 (and (pattern-p wild) (pattern= thing wild)))
197 ;; an integer (version number) is matched by :WILD or the same
198 ;; integer. This branch will actually always be NIL as long as the
199 ;; version is a fixnum.
202 ;;; A predicate for comparing two pathname slot component sub-entries.
203 (defun compare-component (this that)
207 (and (simple-string-p that)
208 (string= this that)))
210 (and (pattern-p that)
211 (pattern= this that)))
214 (compare-component (car this) (car that))
215 (compare-component (cdr this) (cdr that)))))))
217 ;;;; pathname functions
219 (defun pathname= (pathname1 pathname2)
220 (declare (type pathname pathname1)
221 (type pathname pathname2))
222 (and (eq (%pathname-host pathname1)
223 (%pathname-host pathname2))
224 (compare-component (%pathname-device pathname1)
225 (%pathname-device pathname2))
226 (compare-component (%pathname-directory pathname1)
227 (%pathname-directory pathname2))
228 (compare-component (%pathname-name pathname1)
229 (%pathname-name pathname2))
230 (compare-component (%pathname-type pathname1)
231 (%pathname-type pathname2))
232 (compare-component (%pathname-version pathname1)
233 (%pathname-version pathname2))))
235 ;;; Convert PATHNAME-DESIGNATOR (a pathname, or string, or
236 ;;; stream), into a pathname in pathname.
238 ;;; FIXME: was rewritten, should be tested (or rewritten again, this
239 ;;; time using ONCE-ONLY, *then* tested)
240 ;;; FIXME: become SB!XC:DEFMACRO inside EVAL-WHEN (COMPILE EVAL)?
241 (defmacro with-pathname ((pathname pathname-designator) &body body)
242 (let ((pd0 (gensym)))
243 `(let* ((,pd0 ,pathname-designator)
244 (,pathname (etypecase ,pd0
246 (string (parse-namestring ,pd0))
247 (stream (file-name ,pd0)))))
250 ;;; Converts the var, a host or string name for a host, into a logical-host
251 ;;; structure or nil if not defined.
253 ;;; pw notes 1/12/97 this potentially useful macro is not used anywhere
254 ;;; and 'find-host' is not defined. 'find-logical-host' seems to be needed.
256 (defmacro with-host ((var expr) &body body)
257 `(let ((,var (let ((,var ,expr))
260 (string (find-logical-host ,var nil))
265 (defun pathname (thing)
267 "Convert thing (a pathname, string or stream) into a pathname."
268 (declare (type pathname-designator thing))
269 (with-pathname (pathname thing)
272 ;;; Change the case of thing if DIDDLE-P.
273 (defun maybe-diddle-case (thing diddle-p)
274 (if (and diddle-p (not (or (symbolp thing) (integerp thing))))
275 (labels ((check-for (pred in)
278 (dolist (piece (pattern-pieces in))
279 (when (typecase piece
281 (check-for pred piece))
285 (check-for pred (cdr in))))))
289 (when (check-for pred x)
292 (dotimes (i (length in))
293 (when (funcall pred (schar in i))
296 (diddle-with (fun thing)
300 (mapcar #'(lambda (piece)
308 (funcall fun (cdr piece))))
313 (pattern-pieces thing))))
320 (let ((any-uppers (check-for #'upper-case-p thing))
321 (any-lowers (check-for #'lower-case-p thing)))
322 (cond ((and any-uppers any-lowers)
323 ;; Mixed case, stays the same.
326 ;; All uppercase, becomes all lower case.
327 (diddle-with #'(lambda (x) (if (stringp x)
331 ;; All lowercase, becomes all upper case.
332 (diddle-with #'(lambda (x) (if (stringp x)
336 ;; No letters? I guess just leave it.
340 (defun merge-directories (dir1 dir2 diddle-case)
341 (if (or (eq (car dir1) :absolute)
346 (if (and (eq dir :back)
348 (not (eq (car results) :back)))
350 (push dir results))))
351 (dolist (dir (maybe-diddle-case dir2 diddle-case))
353 (dolist (dir (cdr dir1))
357 (defun merge-pathnames (pathname
359 (defaults *default-pathname-defaults*)
360 (default-version :newest))
362 "Construct a filled in pathname by completing the unspecified components
364 (declare (type pathname-designator pathname)
365 (type pathname-designator defaults)
367 (with-pathname (defaults defaults)
368 (let ((pathname (let ((*default-pathname-defaults* defaults))
369 (pathname pathname))))
370 (let* ((default-host (%pathname-host defaults))
371 (pathname-host (%pathname-host pathname))
373 (and default-host pathname-host
374 (not (eq (host-customary-case default-host)
375 (host-customary-case pathname-host))))))
376 (%make-pathname-object
377 (or pathname-host default-host)
378 (or (%pathname-device pathname)
379 (maybe-diddle-case (%pathname-device defaults)
381 (merge-directories (%pathname-directory pathname)
382 (%pathname-directory defaults)
384 (or (%pathname-name pathname)
385 (maybe-diddle-case (%pathname-name defaults)
387 (or (%pathname-type pathname)
388 (maybe-diddle-case (%pathname-type defaults)
390 (or (%pathname-version pathname)
391 default-version))))))
393 (defun import-directory (directory diddle-case)
396 ((member :wild) '(:absolute :wild-inferiors))
397 ((member :unspecific) '(:relative))
400 (ecase (pop directory)
403 (when (search-list-p (car directory))
404 (results (pop directory))))
406 (results :relative)))
407 (dolist (piece directory)
408 (cond ((member piece '(:wild :wild-inferiors :up :back))
410 ((or (simple-string-p piece) (pattern-p piece))
411 (results (maybe-diddle-case piece diddle-case)))
413 (results (maybe-diddle-case (coerce piece 'simple-string)
416 (error "~S is not allowed as a directory component." piece))))
420 ,(maybe-diddle-case directory diddle-case)))
423 ,(maybe-diddle-case (coerce directory 'simple-string)
426 (defun make-pathname (&key host
431 (version nil versionp)
435 "Makes a new pathname from the component arguments. Note that host is
436 a host-structure or string."
437 (declare (type (or string host component-tokens) host)
438 (type (or string component-tokens) device)
439 (type (or list string pattern component-tokens) directory)
440 (type (or string pattern component-tokens) name type)
441 (type (or integer component-tokens (member :newest)) version)
442 (type (or pathname-designator null) defaults)
443 (type (member :common :local) case))
444 (let* ((defaults (when defaults
445 (with-pathname (defaults defaults) defaults)))
446 (default-host (if defaults
447 (%pathname-host defaults)
448 (pathname-host *default-pathname-defaults*)))
449 ;; toy@rtp.ericsson.se: CLHS says make-pathname can take a
450 ;; string (as a logical-host) for the host part. We map that
451 ;; string into the corresponding logical host structure.
454 ;; HyperSpec says for the arg to MAKE-PATHNAME;
455 ;; "host---a valid physical pathname host. ..."
456 ;; where it probably means -- a valid pathname host.
457 ;; "valid pathname host n. a valid physical pathname host or
458 ;; a valid logical pathname host."
460 ;; "valid physical pathname host n. any of a string,
461 ;; a list of strings, or the symbol :unspecific,
462 ;; that is recognized by the implementation as the name of a host."
463 ;; "valid logical pathname host n. a string that has been defined
464 ;; as the name of a logical host. ..."
465 ;; HS is silent on what happens if the :host arg is NOT one of these.
466 ;; It seems an error message is appropriate.
468 (host host) ; A valid host, use it.
469 (string (find-logical-host host t)) ; logical-host or lose.
470 (t default-host))) ; unix-host
471 (diddle-args (and (eq (host-customary-case host) :lower)
474 (not (eq (host-customary-case host)
475 (host-customary-case default-host))))
476 (dev (if devp device (if defaults (%pathname-device defaults))))
477 (dir (import-directory directory diddle-args))
480 (defaults (%pathname-version defaults))
482 (when (and defaults (not dirp))
484 (merge-directories dir
485 (%pathname-directory defaults)
488 (macrolet ((pick (var varp field)
489 `(cond ((or (simple-string-p ,var)
491 (maybe-diddle-case ,var diddle-args))
493 (maybe-diddle-case (coerce ,var 'simple-string)
496 (maybe-diddle-case ,var diddle-args))
498 (maybe-diddle-case (,field defaults)
502 (%make-pathname-object host
503 dev ; forced to :unspecific when logical-host
505 (pick name namep %pathname-name)
506 (pick type typep %pathname-type)
509 (defun pathname-host (pathname &key (case :local))
511 "Accessor for the pathname's host."
512 (declare (type pathname-designator pathname)
513 (type (member :local :common) case)
516 (with-pathname (pathname pathname)
517 (%pathname-host pathname)))
519 (defun pathname-device (pathname &key (case :local))
521 "Accessor for pathname's device."
522 (declare (type pathname-designator pathname)
523 (type (member :local :common) case))
524 (with-pathname (pathname pathname)
525 (maybe-diddle-case (%pathname-device pathname)
526 (and (eq case :common)
527 (eq (host-customary-case
528 (%pathname-host pathname))
531 (defun pathname-directory (pathname &key (case :local))
533 "Accessor for the pathname's directory list."
534 (declare (type pathname-designator pathname)
535 (type (member :local :common) case))
536 (with-pathname (pathname pathname)
537 (maybe-diddle-case (%pathname-directory pathname)
538 (and (eq case :common)
539 (eq (host-customary-case
540 (%pathname-host pathname))
542 (defun pathname-name (pathname &key (case :local))
544 "Accessor for the pathname's name."
545 (declare (type pathname-designator pathname)
546 (type (member :local :common) case))
547 (with-pathname (pathname pathname)
548 (maybe-diddle-case (%pathname-name pathname)
549 (and (eq case :common)
550 (eq (host-customary-case
551 (%pathname-host pathname))
555 (defun pathname-type (pathname &key (case :local))
557 "Accessor for the pathname's name."
558 (declare (type pathname-designator pathname)
559 (type (member :local :common) case))
560 (with-pathname (pathname pathname)
561 (maybe-diddle-case (%pathname-type pathname)
562 (and (eq case :common)
563 (eq (host-customary-case
564 (%pathname-host pathname))
568 (defun pathname-version (pathname)
570 "Accessor for the pathname's version."
571 (declare (type pathname-designator pathname))
572 (with-pathname (pathname pathname)
573 (%pathname-version pathname)))
577 (defun %print-namestring-parse-error (condition stream)
578 (format stream "Parse error in namestring: ~?~% ~A~% ~V@T^"
579 (namestring-parse-error-complaint condition)
580 (namestring-parse-error-arguments condition)
581 (namestring-parse-error-namestring condition)
582 (namestring-parse-error-offset condition)))
584 ;;; Handle the case where PARSE-NAMESTRING is actually parsing a
585 ;;; namestring. We pick off the :JUNK-ALLOWED case then find a host to
586 ;;; use for parsing, call the parser, then check whether the host
588 (defun %parse-namestring (namestr host defaults start end junk-allowed)
589 (declare (type (or host null) host) (type string namestr)
590 (type index start) (type (or index null) end))
593 (%parse-namestring namestr host defaults start end nil)
594 (namestring-parse-error (condition)
595 (values nil (namestring-parse-error-offset condition))))
596 (let* ((end (or end (length namestr)))
598 (extract-logical-host-prefix namestr start end)
599 (pathname-host defaults))))
601 (error "When HOST argument is not supplied, DEFAULTS arg must ~
602 have a non-null PATHNAME-HOST."))
604 (multiple-value-bind (new-host device directory file type version)
605 (funcall (host-parse parse-host) namestr start end)
606 (when (and host new-host (not (eq new-host host)))
607 (error "The host in the namestring, ~S,~@
608 does not match explicit host argument: ~S"
610 (let ((pn-host (or new-host parse-host)))
611 (values (%make-pathname-object
612 pn-host device directory file type version)
615 ;;; If namestr begins with a colon-terminated, defined, logical host, then
616 ;;; return that host, otherwise return NIL.
617 (defun extract-logical-host-prefix (namestr start end)
618 (declare (type simple-base-string namestr)
619 (type index start end)
620 (values (or logical-host null)))
621 (let ((colon-pos (position #\: namestr :start start :end end)))
623 (values (gethash (nstring-upcase (subseq namestr start colon-pos))
627 (defun parse-namestring (thing
630 (defaults *default-pathname-defaults*)
631 &key (start 0) end junk-allowed)
633 "Converts pathname, a pathname designator, into a pathname structure,
634 for a physical pathname, returns the printed representation. Host may be
635 a physical host structure or host namestring."
636 (declare (type pathname-designator thing)
637 (type (or null host) host)
638 (type pathname defaults)
640 (type (or index null) end)
641 (type (or t null) junk-allowed)
642 (values (or null pathname) (or null index)))
645 (%parse-namestring thing host defaults start end junk-allowed))
647 (%parse-namestring (coerce thing 'simple-string)
648 host defaults start end junk-allowed))
650 (let ((host (if host host (%pathname-host defaults))))
651 (unless (eq host (%pathname-host thing))
652 (error "Hosts do not match: ~S and ~S."
653 host (%pathname-host thing))))
654 (values thing start))
656 (let ((name (file-name thing)))
658 (error "can't figure out the file associated with stream:~% ~S"
662 (defun namestring (pathname)
664 "Construct the full (name)string form of the pathname."
665 (declare (type pathname-designator pathname)
666 (values (or null simple-base-string)))
667 (with-pathname (pathname pathname)
669 (let ((host (%pathname-host pathname)))
671 (error "can't determine the namestring for pathnames with no ~
672 host:~% ~S" pathname))
673 (funcall (host-unparse host) pathname)))))
675 (defun host-namestring (pathname)
677 "Returns a string representation of the name of the host in the pathname."
678 (declare (type pathname-designator pathname)
679 (values (or null simple-base-string)))
680 (with-pathname (pathname pathname)
681 (let ((host (%pathname-host pathname)))
683 (funcall (host-unparse-host host) pathname)
685 "can't determine the namestring for pathnames with no host:~% ~S"
688 (defun directory-namestring (pathname)
690 "Returns a string representation of the directories used in the pathname."
691 (declare (type pathname-designator pathname)
692 (values (or null simple-base-string)))
693 (with-pathname (pathname pathname)
694 (let ((host (%pathname-host pathname)))
696 (funcall (host-unparse-directory host) pathname)
698 "can't determine the namestring for pathnames with no host:~% ~S"
701 (defun file-namestring (pathname)
703 "Returns a string representation of the name used in the pathname."
704 (declare (type pathname-designator pathname)
705 (values (or null simple-base-string)))
706 (with-pathname (pathname pathname)
707 (let ((host (%pathname-host pathname)))
709 (funcall (host-unparse-file host) pathname)
711 "can't determine the namestring for pathnames with no host:~% ~S"
714 (defun enough-namestring (pathname
716 (defaults *default-pathname-defaults*))
718 "Returns an abbreviated pathname sufficent to identify the pathname relative
720 (declare (type pathname-designator pathname))
721 (with-pathname (pathname pathname)
722 (let ((host (%pathname-host pathname)))
724 (with-pathname (defaults defaults)
725 (funcall (host-unparse-enough host) pathname defaults))
727 "can't determine the namestring for pathnames with no host:~% ~S"
732 (defun wild-pathname-p (pathname &optional field-key)
734 "Predicate for determining whether pathname contains any wildcards."
735 (declare (type pathname-designator pathname)
736 (type (member nil :host :device :directory :name :type :version)
738 (with-pathname (pathname pathname)
740 (or (pattern-p x) (member x '(:wild :wild-inferiors)))))
743 (or (wild-pathname-p pathname :host)
744 (wild-pathname-p pathname :device)
745 (wild-pathname-p pathname :directory)
746 (wild-pathname-p pathname :name)
747 (wild-pathname-p pathname :type)
748 (wild-pathname-p pathname :version)))
749 (:host (frob (%pathname-host pathname)))
750 (:device (frob (%pathname-host pathname)))
751 (:directory (some #'frob (%pathname-directory pathname)))
752 (:name (frob (%pathname-name pathname)))
753 (:type (frob (%pathname-type pathname)))
754 (:version (frob (%pathname-version pathname)))))))
756 (defun pathname-match-p (in-pathname in-wildname)
758 "Pathname matches the wildname template?"
759 (declare (type pathname-designator in-pathname))
760 (with-pathname (pathname in-pathname)
761 (with-pathname (wildname in-wildname)
762 (macrolet ((frob (field &optional (op 'components-match ))
763 `(or (null (,field wildname))
764 (,op (,field pathname) (,field wildname)))))
765 (and (or (null (%pathname-host wildname))
766 (eq (%pathname-host wildname) (%pathname-host pathname)))
767 (frob %pathname-device)
768 (frob %pathname-directory directory-components-match)
769 (frob %pathname-name)
770 (frob %pathname-type)
771 (frob %pathname-version))))))
773 ;;; Place the substitutions into the pattern and return the string or pattern
774 ;;; that results. If DIDDLE-CASE is true, we diddle the result case as well,
775 ;;; in case we are translating between hosts with difference conventional case.
776 ;;; The second value is the tail of subs with all of the values that we used up
777 ;;; stripped off. Note that PATTERN-MATCHES matches all consecutive wildcards
778 ;;; as a single string, so we ignore subsequent contiguous wildcards.
779 (defun substitute-into (pattern subs diddle-case)
780 (declare (type pattern pattern)
782 (values (or simple-base-string pattern)))
783 (let ((in-wildcard nil)
786 (dolist (piece (pattern-pieces pattern))
787 (cond ((simple-string-p piece)
789 (setf in-wildcard nil))
794 (error "not enough wildcards in FROM pattern to match ~
797 (let ((sub (pop subs)))
801 (push (apply #'concatenate 'simple-string
804 (dolist (piece (pattern-pieces sub))
805 (push piece pieces)))
809 (error "can't substitute this into the middle of a word:~
814 (push (apply #'concatenate 'simple-string (nreverse strings))
818 (if (and pieces (simple-string-p (car pieces)) (null (cdr pieces)))
820 (make-pattern (nreverse pieces)))
824 ;;; Called when we can't see how source and from matched.
825 (defun didnt-match-error (source from)
826 (error "Pathname components from SOURCE and FROM args to TRANSLATE-PATHNAME~@
827 did not match:~% ~S ~S"
830 ;;; Do TRANSLATE-COMPONENT for all components except host and directory.
831 (defun translate-component (source from to diddle-case)
838 (if (pattern= from source)
840 (didnt-match-error source from)))
842 (multiple-value-bind (won subs) (pattern-matches from source)
844 (values (substitute-into to subs diddle-case))
845 (didnt-match-error source from))))
847 (maybe-diddle-case source diddle-case))))
849 (values (substitute-into to (list source) diddle-case)))
851 (if (components-match source from)
852 (maybe-diddle-case source diddle-case)
853 (didnt-match-error source from)))))
855 (maybe-diddle-case source diddle-case))
857 (if (components-match source from)
859 (didnt-match-error source from)))))
861 ;;; Return a list of all the things that we want to substitute into the TO
862 ;;; pattern (the things matched by from on source.) When From contains
863 ;;; :WILD-INFERIORS, the result contains a sublist of the matched source
865 (defun compute-directory-substitutions (orig-source orig-from)
866 (let ((source orig-source)
871 (unless (every #'(lambda (x) (eq x :wild-inferiors)) from)
872 (didnt-match-error orig-source orig-from))
875 (unless from (didnt-match-error orig-source orig-from))
876 (let ((from-part (pop from))
877 (source-part (pop source)))
880 (typecase source-part
882 (if (pattern= from-part source-part)
884 (didnt-match-error orig-source orig-from)))
886 (multiple-value-bind (won new-subs)
887 (pattern-matches from-part source-part)
889 (dolist (sub new-subs)
891 (didnt-match-error orig-source orig-from))))
893 (didnt-match-error orig-source orig-from))))
896 ((member :wild-inferiors)
897 (let ((remaining-source (cons source-part source)))
900 (when (directory-components-match remaining-source from)
902 (unless remaining-source
903 (didnt-match-error orig-source orig-from))
904 (res (pop remaining-source)))
906 (setq source remaining-source))))
908 (unless (and (simple-string-p source-part)
909 (string= from-part source-part))
910 (didnt-match-error orig-source orig-from)))
912 (didnt-match-error orig-source orig-from)))))
915 ;;; Called by TRANSLATE-PATHNAME on the directory components of its argument
916 ;;; pathanames to produce the result directory component. If any leaves the
917 ;;; directory NIL, we return the source directory. The :RELATIVE or :ABSOLUTE
918 ;;; is always taken from the source directory.
919 (defun translate-directories (source from to diddle-case)
920 (if (not (and source to from))
922 (mapcar #'(lambda (x) (maybe-diddle-case x diddle-case)) source))
925 (let ((subs-left (compute-directory-substitutions (rest source)
927 (dolist (to-part (rest to))
931 (let ((match (pop subs-left)))
933 (error ":WILD-INFERIORS not paired in from and to ~
934 patterns:~% ~S ~S" from to))
935 (res (maybe-diddle-case match diddle-case))))
936 ((member :wild-inferiors)
938 (let ((match (pop subs-left)))
939 (unless (listp match)
940 (error ":WILD-INFERIORS not paired in from and to ~
941 patterns:~% ~S ~S" from to))
943 (res (maybe-diddle-case x diddle-case)))))
945 (multiple-value-bind (new new-subs-left)
946 (substitute-into to-part subs-left diddle-case)
947 (setf subs-left new-subs-left)
952 (defun translate-pathname (source from-wildname to-wildname &key)
954 "Use the source pathname to translate the from-wildname's wild and
955 unspecified elements into a completed to-pathname based on the to-wildname."
956 (declare (type pathname-designator source from-wildname to-wildname))
957 (with-pathname (source source)
958 (with-pathname (from from-wildname)
959 (with-pathname (to to-wildname)
960 (let* ((source-host (%pathname-host source))
961 (to-host (%pathname-host to))
963 (and source-host to-host
964 (not (eq (host-customary-case source-host)
965 (host-customary-case to-host))))))
966 (macrolet ((frob (field &optional (op 'translate-component))
967 `(let ((result (,op (,field source)
971 (if (eq result :error)
972 (error "~S doesn't match ~S." source from)
974 (%make-pathname-object
975 (or to-host source-host)
976 (frob %pathname-device)
977 (frob %pathname-directory translate-directories)
978 (frob %pathname-name)
979 (frob %pathname-type)
980 (frob %pathname-version))))))))
984 (def!struct (search-list (:make-load-form-fun
986 (values `(intern-search-list
987 ',(search-list-name s))
989 ;; The name of this search-list. Always stored in lowercase.
990 (name (required-argument) :type simple-string)
991 ;; T if this search-list has been defined. Otherwise NIL.
992 (defined nil :type (member t nil))
993 ;; The list of expansions for this search-list. Each expansion is the list
994 ;; of directory components to use in place of this search-list.
995 (expansions nil :type list))
996 (def!method print-object ((sl search-list) stream)
997 (print-unreadable-object (sl stream :type t)
998 (write-string (search-list-name sl) stream)))
1000 ;;; a hash table mapping search-list names to search-list structures
1001 (defvar *search-lists* (make-hash-table :test 'equal))
1003 ;;; When search-lists are encountered in namestrings, they are converted to
1004 ;;; search-list structures right then, instead of waiting until the search
1005 ;;; list used. This allows us to verify ahead of time that there are no
1006 ;;; circularities and makes expansion much quicker.
1007 (defun intern-search-list (name)
1008 (let ((name (string-downcase name)))
1009 (or (gethash name *search-lists*)
1010 (let ((new (make-search-list :name name)))
1011 (setf (gethash name *search-lists*) new)
1014 ;;; Clear the definition. Note: we can't remove it from the hash-table
1015 ;;; because there may be pathnames still refering to it. So we just clear
1016 ;;; out the expansions and ste defined to NIL.
1017 (defun clear-search-list (name)
1019 "Clear the current definition for the search-list NAME. Returns T if such
1020 a definition existed, and NIL if not."
1021 (let* ((name (string-downcase name))
1022 (search-list (gethash name *search-lists*)))
1023 (when (and search-list (search-list-defined search-list))
1024 (setf (search-list-defined search-list) nil)
1025 (setf (search-list-expansions search-list) nil)
1028 ;;; Again, we can't actually remove the entries from the hash-table, so we
1029 ;;; just mark them as being undefined.
1030 (defun clear-all-search-lists ()
1032 "Clear the definition for all search-lists. Only use this if you know
1033 what you are doing."
1034 (maphash #'(lambda (name search-list)
1035 (declare (ignore name))
1036 (setf (search-list-defined search-list) nil)
1037 (setf (search-list-expansions search-list) nil))
1041 ;;; Extract the search-list from PATHNAME and return it. If PATHNAME
1042 ;;; doesn't start with a search-list, then either error (if FLAME-IF-NONE
1043 ;;; is true) or return NIL (if FLAME-IF-NONE is false).
1044 (defun extract-search-list (pathname flame-if-none)
1045 (with-pathname (pathname pathname)
1046 (let* ((directory (%pathname-directory pathname))
1047 (search-list (cadr directory)))
1048 (cond ((search-list-p search-list)
1051 (error "~S doesn't start with a search-list." pathname))
1055 ;;; We have to convert the internal form of the search-list back into a
1056 ;;; bunch of pathnames.
1057 (defun search-list (pathname)
1059 "Return the expansions for the search-list starting PATHNAME. If PATHNAME
1060 does not start with a search-list, then an error is signaled. If
1061 the search-list has not been defined yet, then an error is signaled.
1062 The expansion for a search-list can be set with SETF."
1063 (with-pathname (pathname pathname)
1064 (let ((search-list (extract-search-list pathname t))
1065 (host (pathname-host pathname)))
1066 (if (search-list-defined search-list)
1067 (mapcar #'(lambda (directory)
1068 (make-pathname :host host
1069 :directory (cons :absolute directory)))
1070 (search-list-expansions search-list))
1071 (error "Search list ~S has not been defined yet." pathname)))))
1073 (defun search-list-defined-p (pathname)
1075 "Returns T if the search-list starting PATHNAME is currently defined, and
1076 NIL otherwise. An error is signaled if PATHNAME does not start with a
1078 (with-pathname (pathname pathname)
1079 (search-list-defined (extract-search-list pathname t))))
1081 ;;; Set the expansion for the search-list in PATHNAME. If this would result
1082 ;;; in any circularities, we flame out. If anything goes wrong, we leave the
1083 ;;; old definition intact.
1084 (defun %set-search-list (pathname values)
1085 (let ((search-list (extract-search-list pathname t)))
1087 ((check (target-list path)
1088 (when (eq search-list target-list)
1089 (error "That would result in a circularity:~% ~
1091 (search-list-name search-list)
1093 (search-list-name target-list)))
1094 (when (search-list-p target-list)
1095 (push (search-list-name target-list) path)
1096 (dolist (expansion (search-list-expansions target-list))
1097 (check (car expansion) path))))
1099 (with-pathname (pathname pathname)
1100 (when (or (pathname-name pathname)
1101 (pathname-type pathname)
1102 (pathname-version pathname))
1103 (error "Search-lists cannot expand into pathnames that have ~
1104 a name, type, or ~%version specified:~% ~S"
1106 (let ((directory (pathname-directory pathname)))
1109 (ecase (car directory)
1110 (:absolute (cdr directory))
1111 (:relative (cons (intern-search-list "default")
1113 (list (intern-search-list "default")))))
1114 (check (car expansion) nil)
1116 (setf (search-list-expansions search-list)
1118 (mapcar #'convert values)
1119 (list (convert values)))))
1120 (setf (search-list-defined search-list) t))
1123 (defun %enumerate-search-list (pathname function)
1124 (/show0 "entering %ENUMERATE-SEARCH-LIST")
1125 (let* ((pathname (if (typep pathname 'logical-pathname)
1126 (translate-logical-pathname pathname)
1128 (search-list (extract-search-list pathname nil)))
1129 (/show0 "PATHNAME and SEARCH-LIST computed")
1132 (/show0 "no search list")
1133 (funcall function pathname))
1134 ((not (search-list-defined search-list))
1135 (/show0 "undefined search list")
1136 (error "undefined search list: ~A"
1137 (search-list-name search-list)))
1139 (/show0 "general case")
1140 (let ((tail (cddr (pathname-directory pathname))))
1141 (/show0 "TAIL computed")
1143 (search-list-expansions search-list))
1144 (/show0 "tail recursing in %ENUMERATE-SEARCH-LIST")
1145 (%enumerate-search-list (make-pathname :defaults pathname
1152 ;;;; logical pathname support. ANSI 92-102 specification.
1153 ;;;; As logical-pathname translations are loaded they are canonicalized as
1154 ;;;; patterns to enable rapid efficent translation into physical pathnames.
1158 ;;; Canonicalize a logical pathanme word by uppercasing it checking that it
1159 ;;; contains only legal characters.
1160 (defun logical-word-or-lose (word)
1161 (declare (string word))
1162 (let ((word (string-upcase word)))
1163 (dotimes (i (length word))
1164 (let ((ch (schar word i)))
1165 (unless (or (alpha-char-p ch) (digit-char-p ch) (char= ch #\-))
1166 (error 'namestring-parse-error
1167 :complaint "logical namestring character which ~
1168 is not alphanumeric or hyphen:~% ~S"
1169 :arguments (list ch)
1170 :namestring word :offset i))))
1173 ;;; Given a logical host or string, return a logical host. If Error-p is
1174 ;;; NIL, then return NIL when no such host exists.
1175 (defun find-logical-host (thing &optional (errorp t))
1178 (let ((found (gethash (logical-word-or-lose thing)
1180 (if (or found (not errorp))
1182 (error 'simple-file-error
1184 :format-control "logical host not yet defined: ~S"
1185 :format-arguments (list thing)))))
1186 (logical-host thing)))
1188 ;;; Given a logical host name or host, return a logical host, creating a new
1189 ;;; one if necessary.
1190 (defun intern-logical-host (thing)
1191 (declare (values logical-host))
1192 (or (find-logical-host thing nil)
1193 (let* ((name (logical-word-or-lose thing))
1194 (new (make-logical-host :name name)))
1195 (setf (gethash name *logical-hosts*) new)
1198 ;;;; logical pathname parsing
1200 ;;; Deal with multi-char wildcards in a logical pathname token.
1201 (defun maybe-make-logical-pattern (namestring chunks)
1202 (let ((chunk (caar chunks)))
1203 (collect ((pattern))
1205 (len (length chunk)))
1206 (declare (fixnum last-pos))
1208 (when (= last-pos len) (return))
1209 (let ((pos (or (position #\* chunk :start last-pos) len)))
1210 (if (= pos last-pos)
1212 (error 'namestring-parse-error
1213 :complaint "double asterisk inside of logical ~
1215 :arguments (list chunk)
1216 :namestring namestring
1217 :offset (+ (cdar chunks) pos)))
1218 (pattern (subseq chunk last-pos pos)))
1221 (pattern :multi-char-wild))
1222 (setq last-pos (1+ pos)))))
1225 (make-pattern (pattern))
1226 (let ((x (car (pattern))))
1227 (if (eq x :multi-char-wild)
1231 ;;; Return a list of conses where the cdr is the start position and the car
1232 ;;; is a string (token) or character (punctuation.)
1233 (defun logical-chunkify (namestr start end)
1235 (do ((i start (1+ i))
1239 (chunks (cons (nstring-upcase (subseq namestr prev end)) prev))))
1240 (let ((ch (schar namestr i)))
1241 (unless (or (alpha-char-p ch) (digit-char-p ch)
1242 (member ch '(#\- #\*)))
1244 (chunks (cons (nstring-upcase (subseq namestr prev i)) prev)))
1246 (unless (member ch '(#\; #\: #\.))
1247 (error 'namestring-parse-error
1248 :complaint "illegal character for logical pathname:~% ~S"
1249 :arguments (list ch)
1252 (chunks (cons ch i)))))
1255 ;;; Break up a logical-namestring, always a string, into its constituent parts.
1256 (defun parse-logical-namestring (namestr start end)
1257 (declare (type simple-base-string namestr)
1258 (type index start end))
1259 (collect ((directory))
1264 (labels ((expecting (what chunks)
1265 (unless (and chunks (simple-string-p (caar chunks)))
1266 (error 'namestring-parse-error
1267 :complaint "expecting ~A, got ~:[nothing~;~S~]."
1268 :arguments (list what (caar chunks))
1270 :offset (if chunks (cdar chunks) end)))
1272 (parse-host (chunks)
1273 (case (caadr chunks)
1276 (find-logical-host (expecting "a host name" chunks)))
1277 (parse-relative (cddr chunks)))
1279 (parse-relative chunks))))
1280 (parse-relative (chunks)
1283 (directory :relative)
1284 (parse-directory (cdr chunks)))
1286 (directory :absolute) ; Assumption! Maybe revoked later.
1287 (parse-directory chunks))))
1288 (parse-directory (chunks)
1289 (case (caadr chunks)
1292 (let ((res (expecting "a directory name" chunks)))
1293 (cond ((string= res "..") :up)
1294 ((string= res "**") :wild-inferiors)
1296 (maybe-make-logical-pattern namestr chunks)))))
1297 (parse-directory (cddr chunks)))
1299 (parse-name chunks))))
1300 (parse-name (chunks)
1302 (expecting "a file name" chunks)
1303 (setq name (maybe-make-logical-pattern namestr chunks))
1304 (expecting-dot (cdr chunks))))
1305 (expecting-dot (chunks)
1307 (unless (eql (caar chunks) #\.)
1308 (error 'namestring-parse-error
1309 :complaint "expecting a dot, got ~S."
1310 :arguments (list (caar chunks))
1312 :offset (cdar chunks)))
1314 (parse-version (cdr chunks))
1315 (parse-type (cdr chunks)))))
1316 (parse-type (chunks)
1317 (expecting "a file type" chunks)
1318 (setq type (maybe-make-logical-pattern namestr chunks))
1319 (expecting-dot (cdr chunks)))
1320 (parse-version (chunks)
1321 (let ((str (expecting "a positive integer, * or NEWEST"
1324 ((string= str "*") (setq version :wild))
1325 ((string= str "NEWEST") (setq version :newest))
1327 (multiple-value-bind (res pos)
1328 (parse-integer str :junk-allowed t)
1329 (unless (and res (plusp res))
1330 (error 'namestring-parse-error
1331 :complaint "expected a positive integer, ~
1333 :arguments (list str)
1335 :offset (+ pos (cdar chunks))))
1336 (setq version res)))))
1338 (error 'namestring-parse-error
1339 :complaint "extra stuff after end of file name"
1341 :offset (cdadr chunks)))))
1342 (parse-host (logical-chunkify namestr start end)))
1343 (values host :unspecific
1344 (and (not (equal (directory)'(:absolute)))(directory))
1345 name type version))))
1347 ;;; can't defvar here because not all host methods are loaded yet
1348 (declaim (special *logical-pathname-defaults*))
1350 (defun logical-pathname (pathspec)
1352 "Converts the pathspec argument to a logical-pathname and returns it."
1353 (declare (type (or logical-pathname string stream) pathspec)
1354 (values logical-pathname))
1355 (if (typep pathspec 'logical-pathname)
1357 (let ((res (parse-namestring pathspec nil *logical-pathname-defaults*)))
1358 (when (eq (%pathname-host res)
1359 (%pathname-host *logical-pathname-defaults*))
1360 (error "This logical namestring does not specify a host:~% ~S"
1364 ;;;; logical pathname unparsing
1366 (defun unparse-logical-directory (pathname)
1367 (declare (type pathname pathname))
1369 (let ((directory (%pathname-directory pathname)))
1371 (ecase (pop directory)
1372 (:absolute) ;; Nothing special.
1373 (:relative (pieces ";")))
1374 (dolist (dir directory)
1375 (cond ((or (stringp dir) (pattern-p dir))
1376 (pieces (unparse-logical-piece dir))
1380 ((eq dir :wild-inferiors)
1383 (error "invalid directory component: ~S" dir))))))
1384 (apply #'concatenate 'simple-string (pieces))))
1386 (defun unparse-logical-piece (thing)
1388 (simple-string thing)
1390 (collect ((strings))
1391 (dolist (piece (pattern-pieces thing))
1393 (simple-string (strings piece))
1395 (cond ((eq piece :wild-inferiors)
1397 ((eq piece :multi-char-wild)
1399 (t (error "invalid keyword: ~S" piece))))))
1400 (apply #'concatenate 'simple-string (strings))))))
1402 (defun unparse-logical-namestring (pathname)
1403 (declare (type logical-pathname pathname))
1404 (concatenate 'simple-string
1405 (logical-host-name (%pathname-host pathname)) ":"
1406 (unparse-logical-directory pathname)
1407 (unparse-unix-file pathname)))
1409 ;;;; logical pathname translations
1411 ;;; Verify that the list of translations consists of lists and prepare
1412 ;;; canonical translations (parse pathnames and expand out wildcards into
1414 (defun canonicalize-logical-pathname-translations (transl-list host)
1415 (declare (type list transl-list) (type host host)
1418 (dolist (tr transl-list)
1419 (unless (and (consp tr) (= (length tr) 2))
1420 (error "This logical pathname translation is not a two-list:~% ~S"
1422 (let ((from (first tr)))
1423 (res (list (if (typep from 'logical-pathname)
1425 (parse-namestring from host))
1426 (pathname (second tr))))))
1429 (defun logical-pathname-translations (host)
1431 "Return the (logical) host object argument's list of translations."
1432 (declare (type (or string logical-host) host)
1434 (logical-host-translations (find-logical-host host)))
1436 (defun (setf logical-pathname-translations) (translations host)
1438 "Set the translations list for the logical host argument.
1439 Return translations."
1440 (declare (type (or string logical-host) host)
1441 (type list translations)
1444 (let ((host (intern-logical-host host)))
1445 (setf (logical-host-canon-transls host)
1446 (canonicalize-logical-pathname-translations translations host))
1447 (setf (logical-host-translations host) translations)))
1449 ;;; The search mechanism for loading pathname translations uses the CMU CL
1450 ;;; extension of search-lists. The user can add to the "library:" search-list
1451 ;;; using setf. The file for translations should have the name defined by
1452 ;;; the hostname (a string) and with type component "translations".
1454 (defun load-logical-pathname-translations (host)
1456 "Search for a logical pathname named host, if not already defined. If already
1457 defined no attempt to find or load a definition is attempted and NIL is
1458 returned. If host is not already defined, but definition is found and loaded
1459 successfully, T is returned, else error."
1460 (declare (type string host)
1461 (values (member t nil)))
1462 (unless (find-logical-host host nil)
1463 (with-open-file (in-str (make-pathname :defaults "library:"
1465 :type "translations"))
1467 (format *error-output*
1468 ";; loading pathname translations from ~A~%"
1469 (namestring (truename in-str))))
1470 (setf (logical-pathname-translations host) (read in-str)))
1473 (defun translate-logical-pathname (pathname &key)
1475 "Translates pathname to a physical pathname, which is returned."
1476 (declare (type pathname-designator pathname)
1477 (values (or null pathname)))
1480 (dolist (x (logical-host-canon-transls (%pathname-host pathname))
1481 (error 'simple-file-error
1483 :format-control "no translation for ~S"
1484 :format-arguments (list pathname)))
1485 (destructuring-bind (from to) x
1486 (when (pathname-match-p pathname from)
1487 (return (translate-logical-pathname
1488 (translate-pathname pathname from to)))))))
1490 (stream (translate-logical-pathname (pathname pathname)))
1491 (t (translate-logical-pathname (logical-pathname pathname)))))
1493 (defvar *logical-pathname-defaults*
1494 (%make-logical-pathname (make-logical-host :name "BOGUS")