gencgc: Don't use defconstant for DYNAMIC-SPACE-END
[sbcl.git] / src / code / target-pathname.lisp
blob865be72a341882279cc02b5c52ec535e0c6a441e
1 ;;;; machine/filesystem-independent pathname functions
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!IMPL")
14 #!-sb-fluid (declaim (freeze-type logical-pathname logical-host))
16 ;;; To be initialized in unix/win32-pathname.lisp
17 (defvar *physical-host*)
19 ;;; Return a value suitable, e.g., for preinitializing
20 ;;; *DEFAULT-PATHNAME-DEFAULTS* before *DEFAULT-PATHNAME-DEFAULTS* is
21 ;;; initialized (at which time we can't safely call e.g. #'PATHNAME).
22 (defun make-trivial-default-pathname ()
23 (%%make-pathname *physical-host* nil nil nil nil :newest))
25 ;;; pathname methods
27 ;;; SXHASH does a really poor job on pathname directory, especially if in your
28 ;;; environment, the directories of interest are all many levels down from the
29 ;;; filesystem root- every directory in your work space might hash to the same
30 ;;; value under SXHASH. Mixing in all pieces of the directory path solves that.
31 (defun pathname-dir-hash (directory)
32 (let ((hash (sxhash (car directory))))
33 (dolist (piece (cdr directory) hash)
34 (mixf hash (sxhash piece)))))
36 (defmethod print-object ((pathname pathname) stream)
37 (let ((namestring (handler-case (namestring pathname)
38 (error nil))))
39 (if namestring
40 (format stream
41 (if (or *print-readably* *print-escape*)
42 "#P~S"
43 "~A")
44 (coerce namestring '(simple-array character (*))))
45 (print-unreadable-object (pathname stream :type t)
46 (format stream
47 "~@<(with no namestring) ~_:HOST ~S ~_:DEVICE ~S ~_:DIRECTORY ~S ~
48 ~_:NAME ~S ~_:TYPE ~S ~_:VERSION ~S~:>"
49 (%pathname-host pathname)
50 (%pathname-device pathname)
51 (%pathname-directory pathname)
52 (%pathname-name pathname)
53 (%pathname-type pathname)
54 (%pathname-version pathname))))))
56 (defmethod make-load-form ((pathname pathname) &optional environment)
57 (make-load-form-saving-slots pathname :environment environment))
59 ;;; A pathname is logical if the host component is a logical host.
60 ;;; This constructor is used to make an instance of the correct type
61 ;;; from parsed arguments.
62 (defun %make-maybe-logical-pathname (host device directory name type version)
63 ;; We canonicalize logical pathname components to uppercase. ANSI
64 ;; doesn't strictly require this, leaving it up to the implementor;
65 ;; but the arguments given in the X3J13 cleanup issue
66 ;; PATHNAME-LOGICAL:ADD seem compelling: we should canonicalize the
67 ;; case, and uppercase is the ordinary way to do that.
68 (flet ((upcase-maybe (x) (typecase x (string (logical-word-or-lose x)) (t x))))
69 (if (typep host 'logical-host)
70 (%make-logical-pathname host
71 :unspecific
72 (mapcar #'upcase-maybe directory)
73 (upcase-maybe name)
74 (upcase-maybe type)
75 version)
76 (progn
77 (aver (eq host *physical-host*))
78 (%make-pathname host device directory name type version)))))
80 ;;; Hash table searching maps a logical pathname's host to its
81 ;;; physical pathname translation.
82 (defvar *logical-hosts* (make-hash-table :test 'equal :synchronized t))
84 ;;;; patterns
86 (defmethod make-load-form ((pattern pattern) &optional environment)
87 (make-load-form-saving-slots pattern :environment environment))
89 (defmethod print-object ((pattern pattern) stream)
90 (print-unreadable-object (pattern stream :type t)
91 (if *print-pretty*
92 (let ((*print-escape* t))
93 (pprint-fill stream (pattern-pieces pattern) nil))
94 (prin1 (pattern-pieces pattern) stream))))
96 (defun pattern= (pattern1 pattern2)
97 (declare (type pattern pattern1 pattern2))
98 (let ((pieces1 (pattern-pieces pattern1))
99 (pieces2 (pattern-pieces pattern2)))
100 (and (= (length pieces1) (length pieces2))
101 (every (lambda (piece1 piece2)
102 (typecase piece1
103 (simple-string
104 (and (simple-string-p piece2)
105 (string= piece1 piece2)))
106 (cons
107 (and (consp piece2)
108 (eq (car piece1) (car piece2))
109 (string= (cdr piece1) (cdr piece2))))
111 (eq piece1 piece2))))
112 pieces1
113 pieces2))))
115 ;;; If the string matches the pattern returns the multiple values T
116 ;;; and a list of the matched strings.
117 (defun pattern-matches (pattern string)
118 (declare (type pattern pattern)
119 (type simple-string string))
120 (let ((len (length string)))
121 (labels ((maybe-prepend (subs cur-sub chars)
122 (if cur-sub
123 (let* ((len (length chars))
124 (new (make-string len))
125 (index len))
126 (dolist (char chars)
127 (setf (schar new (decf index)) char))
128 (cons new subs))
129 subs))
130 (matches (pieces start subs cur-sub chars)
131 (if (null pieces)
132 (if (= start len)
133 (values t (maybe-prepend subs cur-sub chars))
134 (values nil nil))
135 (let ((piece (car pieces)))
136 (etypecase piece
137 (simple-string
138 (let ((end (+ start (length piece))))
139 (and (<= end len)
140 (string= piece string
141 :start2 start :end2 end)
142 (matches (cdr pieces) end
143 (maybe-prepend subs cur-sub chars)
144 nil nil))))
145 (list
146 (ecase (car piece)
147 (:character-set
148 (and (< start len)
149 (let ((char (schar string start)))
150 (if (find char (cdr piece) :test #'char=)
151 (matches (cdr pieces) (1+ start) subs t
152 (cons char chars))))))))
153 ((member :single-char-wild)
154 (and (< start len)
155 (matches (cdr pieces) (1+ start) subs t
156 (cons (schar string start) chars))))
157 ((member :multi-char-wild)
158 (multiple-value-bind (won new-subs)
159 (matches (cdr pieces) start subs t chars)
160 (if won
161 (values t new-subs)
162 (and (< start len)
163 (matches pieces (1+ start) subs t
164 (cons (schar string start)
165 chars)))))))))))
166 (multiple-value-bind (won subs)
167 (matches (pattern-pieces pattern) 0 nil nil nil)
168 (values won (reverse subs))))))
170 ;;; PATHNAME-MATCH-P for directory components
171 (defun directory-components-match (thing wild)
172 (or (eq thing wild)
173 (eq wild :wild)
174 ;; If THING has a null directory, assume that it matches
175 ;; (:ABSOLUTE :WILD-INFERIORS) or (:RELATIVE :WILD-INFERIORS).
176 (and (consp wild)
177 (null thing)
178 (member (first wild) '(:absolute :relative))
179 (eq (second wild) :wild-inferiors))
180 (and (consp wild)
181 (let ((wild1 (first wild)))
182 (if (eq wild1 :wild-inferiors)
183 (let ((wild-subdirs (rest wild)))
184 (or (null wild-subdirs)
185 (loop
186 (when (directory-components-match thing wild-subdirs)
187 (return t))
188 (pop thing)
189 (unless thing (return nil)))))
190 (and (consp thing)
191 (components-match (first thing) wild1)
192 (directory-components-match (rest thing)
193 (rest wild))))))))
195 ;;; Return true if pathname component THING is matched by WILD. (not
196 ;;; commutative)
197 (defun components-match (thing wild)
198 (declare (type (or pattern symbol simple-string integer) thing wild))
199 (or (eq thing wild)
200 (eq wild :wild)
201 (typecase thing
202 (simple-string
203 ;; String is matched by itself, a matching pattern or :WILD.
204 (typecase wild
205 (pattern
206 (values (pattern-matches wild thing)))
207 (simple-string
208 (string= thing wild))))
209 (pattern
210 ;; A pattern is only matched by an identical pattern.
211 (and (pattern-p wild) (pattern= thing wild)))
212 (integer
213 ;; An integer (version number) is matched by :WILD or the
214 ;; same integer. This branch will actually always be NIL as
215 ;; long as the version is a fixnum.
216 (eql thing wild)))))
218 ;;; a predicate for comparing two pathname slot component sub-entries
219 (defun compare-component (this that)
220 (or (eql this that)
221 (typecase this
222 (simple-string
223 (and (simple-string-p that)
224 (string= this that)))
225 (pattern
226 (and (pattern-p that)
227 (pattern= this that)))
228 (cons
229 (and (consp that)
230 (compare-component (car this) (car that))
231 (compare-component (cdr this) (cdr that)))))))
233 ;;;; pathname functions
235 (defun pathname= (pathname1 pathname2)
236 (declare (type pathname pathname1)
237 (type pathname pathname2))
238 (or (eq pathname1 pathname2)
239 (and (eq (%pathname-host pathname1)
240 (%pathname-host pathname2))
241 (= (%pathname-dir-hash pathname1) (%pathname-dir-hash pathname2))
242 (= (%pathname-stem-hash pathname1) (%pathname-stem-hash pathname2))
243 (compare-component (%pathname-device pathname1)
244 (%pathname-device pathname2))
245 (compare-component (%pathname-directory pathname1)
246 (%pathname-directory pathname2))
247 (compare-component (%pathname-name pathname1)
248 (%pathname-name pathname2))
249 (compare-component (%pathname-type pathname1)
250 (%pathname-type pathname2))
251 (or (eq (%pathname-host pathname1) *physical-host*)
252 (compare-component (%pathname-version pathname1)
253 (%pathname-version pathname2))))))
255 ;;; This is conceptually like (DEFUN-CACHED (%MAKE-PATHNAME ...))
256 ;;; except that we try hard never to evict entries until SAVE-LISP-AND-DIE.
257 ;;; Entries can still be kicked out randomly though.
258 ;;; A two-level lookup is used- it works better than mixing all
259 ;;; pathname components into a hash key.
260 (define-load-time-global *pathnames* (make-array 211 :initial-element nil))
261 (defglobal *pathnames-lock* (sb!thread:make-mutex :name "Pathnames"))
263 (defun %make-pathname (host device directory name type version)
264 (if (or device (neq host *physical-host*))
265 (%%make-pathname host device directory name type version)
266 (let* ((table *pathnames*)
267 (index (rem (pathname-dir-hash directory) (length table)))
268 (dir-holder
269 ;; Candidates is a list of ((dir . contents) ...)
270 (loop named outer
271 with candidates = (svref table index) and new = nil
273 (let ((n-candidates 0))
274 (dolist (candidate candidates)
275 (incf n-candidates)
276 (when (compare-component (car candidate) directory)
277 (return-from outer candidate)))
278 (unless new
279 (setq new (cons directory (make-array 3 :initial-element nil))))
280 (cond ((< n-candidates 10)
281 (let* ((cell (cons new candidates))
282 (actual-old (cas (svref table index) candidates cell)))
283 (when (eq actual-old candidates)
284 (return-from outer new))
285 (setq candidates actual-old)))
287 ;; Clobber this cache entry, losing all directories in it.
288 ;; Hopefully this doesn't happen often.
289 #+nil (format t "~&*** Pathname cache overflow: ~D ~S~%"
290 index (mapcar 'car candidates))
291 (setf (svref table index) (list new))
292 (return-from outer new)))))))
293 (flet ((matchp (stem-hash candidates)
294 (let ((n-candidates 0))
295 (dolist (pathname candidates (values nil n-candidates))
296 (when (and (= (%pathname-stem-hash pathname) stem-hash)
297 (compare-component (%pathname-version pathname) version)
298 (compare-component (%pathname-name pathname) name)
299 (compare-component (%pathname-type pathname) type))
300 (return (values pathname 0)))
301 (incf n-candidates)))))
302 ;; We have tests asserting that the distinction between :NEWEST
303 ;; and NIL is preserved, though there is no effective difference.
304 (binding* ((stem-hash (mix (sxhash name) (sxhash type)))
305 (vector (the simple-vector (cdr dir-holder)))
306 (index (rem stem-hash (length vector)))
307 (candidates (svref vector index))
308 ((found n-candidates) (matchp stem-hash candidates)))
309 (when found
310 (return-from %make-pathname found))
311 ;; Optimistically assuming that the pathname won't be found
312 ;; on the double-check, allocate it now
313 (let ((pathname (%%make-pathname *physical-host* nil (car dir-holder)
314 name type version)))
315 (sb!thread::with-system-mutex (*pathnames-lock*)
316 (when (>= n-candidates 10)
317 ;; Rehash into a larger vector
318 (let* ((old-len (length vector))
319 (new-len (+ old-len 4))
320 (new-vector (make-array new-len :initial-element nil)))
321 (dovector (list vector)
322 (dolist (p list)
323 (push p (svref new-vector (rem (%pathname-stem-hash p)
324 new-len)))))
325 (rplacd dir-holder new-vector)
326 (setq vector new-vector
327 index (rem stem-hash new-len)
328 candidates (svref vector index))))
329 (let ((found (matchp stem-hash candidates)))
330 (if found
331 (setq pathname found)
332 (push pathname (svref vector index)))))
333 pathname))))))
335 ;;; Convert PATHNAME-DESIGNATOR (a pathname, or string, or
336 ;;; stream), into a pathname in pathname.
338 ;;; FIXME: was rewritten, should be tested (or rewritten again, this
339 ;;; time using ONCE-ONLY, *then* tested)
340 (eval-when (:compile-toplevel :execute)
341 (sb!xc:defmacro with-pathname ((pathname pathname-designator) &body body)
342 (let ((pd0 (gensym)))
343 `(let* ((,pd0 ,pathname-designator)
344 (,pathname (etypecase ,pd0
345 (pathname ,pd0)
346 (string (parse-namestring ,pd0))
347 (file-stream (file-name ,pd0)))))
348 ,@body)))
350 (sb!xc:defmacro with-native-pathname ((pathname pathname-designator) &body body)
351 (let ((pd0 (gensym)))
352 `(let* ((,pd0 ,pathname-designator)
353 (,pathname (etypecase ,pd0
354 (pathname ,pd0)
355 (string (parse-native-namestring ,pd0))
356 ;; FIXME
357 #+nil
358 (file-stream (file-name ,pd0)))))
359 ,@body)))
361 (sb!xc:defmacro with-host ((host host-designator) &body body)
362 ;; Generally, redundant specification of information in software,
363 ;; whether in code or in comments, is bad. However, the ANSI spec
364 ;; for this is messy enough that it's hard to hold in short-term
365 ;; memory, so I've recorded these redundant notes on the
366 ;; implications of the ANSI spec.
368 ;; According to the ANSI spec, HOST can be a valid pathname host, or
369 ;; a logical host, or NIL.
371 ;; A valid pathname host can be a valid physical pathname host or a
372 ;; valid logical pathname host.
374 ;; A valid physical pathname host is "any of a string, a list of
375 ;; strings, or the symbol :UNSPECIFIC, that is recognized by the
376 ;; implementation as the name of a host". In SBCL as of 0.6.9.8,
377 ;; that means :UNSPECIFIC: though someday we might want to
378 ;; generalize it to allow strings like "RTFM.MIT.EDU" or lists like
379 ;; '("RTFM" "MIT" "EDU"), that's not supported now.
381 ;; A valid logical pathname host is a string which has been defined as
382 ;; the name of a logical host, as with LOAD-LOGICAL-PATHNAME-TRANSLATIONS.
384 ;; A logical host is an object of implementation-dependent nature. In
385 ;; SBCL, it's a member of the HOST class (a subclass of STRUCTURE-OBJECT).
386 (let ((hd0 (gensym)))
387 `(let* ((,hd0 ,host-designator)
388 (,host (etypecase ,hd0
389 ((string 0)
390 ;; This is a special host. It's not valid as a
391 ;; logical host, so it is a sensible thing to
392 ;; designate the physical host object. So we do
393 ;; that.
394 *physical-host*)
395 (string
396 ;; In general ANSI-compliant Common Lisps, a
397 ;; string might also be a physical pathname
398 ;; host, but ANSI leaves this up to the
399 ;; implementor, and in SBCL we don't do it, so
400 ;; it must be a logical host.
401 (find-logical-host ,hd0))
402 ((or null (member :unspecific))
403 ;; CLHS says that HOST=:UNSPECIFIC has
404 ;; implementation-defined behavior. We
405 ;; just turn it into NIL.
406 nil)
407 (list
408 ;; ANSI also allows LISTs to designate hosts,
409 ;; but leaves its interpretation
410 ;; implementation-defined. Our interpretation
411 ;; is that it's unsupported.:-|
412 (error "A LIST representing a pathname host is not ~
413 supported in this implementation:~% ~S"
414 ,hd0))
415 (host ,hd0))))
416 ,@body)))
417 ) ; EVAL-WHEN
419 (defun find-host (host-designator &optional (errorp t))
420 (with-host (host host-designator)
421 (when (and errorp (not host))
422 (error "Couldn't find host: ~S" host-designator))
423 host))
425 (defun pathname (pathspec)
426 "Convert PATHSPEC (a pathname designator) into a pathname."
427 (declare (type pathname-designator pathspec))
428 (with-pathname (pathname pathspec)
429 pathname))
431 (defun native-pathname (pathspec)
432 "Convert PATHSPEC (a pathname designator) into a pathname, assuming
433 the operating system native pathname conventions."
434 (with-native-pathname (pathname pathspec)
435 pathname))
437 ;;; Change the case of thing if DIDDLE-P.
438 (defun maybe-diddle-case (thing diddle-p)
439 (if (and diddle-p (not (or (symbolp thing) (integerp thing))))
440 (labels ((check-for (pred in)
441 (typecase in
442 (pattern
443 (dolist (piece (pattern-pieces in))
444 (when (typecase piece
445 (simple-string
446 (check-for pred piece))
447 (cons
448 (case (car piece)
449 (:character-set
450 (check-for pred (cdr piece))))))
451 (return t))))
452 (list
453 (dolist (x in)
454 (when (check-for pred x)
455 (return t))))
456 (simple-string
457 (dotimes (i (length in))
458 (when (funcall pred (schar in i))
459 (return t))))
460 (t nil)))
461 (diddle-with (fun thing)
462 (typecase thing
463 (pattern
464 (make-pattern
465 (mapcar (lambda (piece)
466 (typecase piece
467 (simple-string
468 (funcall fun piece))
469 (cons
470 (case (car piece)
471 (:character-set
472 (cons :character-set
473 (funcall fun (cdr piece))))
475 piece)))
477 piece)))
478 (pattern-pieces thing))))
479 (list
480 (mapcar fun thing))
481 (simple-string
482 (funcall fun thing))
484 thing))))
485 (let ((any-uppers (check-for #'upper-case-p thing))
486 (any-lowers (check-for #'lower-case-p thing)))
487 (cond ((and any-uppers any-lowers)
488 ;; mixed case, stays the same
489 thing)
490 (any-uppers
491 ;; all uppercase, becomes all lower case
492 (diddle-with (lambda (x) (if (stringp x)
493 (string-downcase x)
494 x)) thing))
495 (any-lowers
496 ;; all lowercase, becomes all upper case
497 (diddle-with (lambda (x) (if (stringp x)
498 (string-upcase x)
499 x)) thing))
501 ;; no letters? I guess just leave it.
502 thing))))
503 thing))
505 (defun merge-directories (dir1 dir2 diddle-case)
506 (if (or (eq (car dir1) :absolute)
507 (null dir2))
508 dir1
509 (let ((results nil))
510 (flet ((add (dir)
511 (if (and (eq dir :back)
512 results
513 (typep (car results) '(or string pattern
514 (member :wild :wild-inferiors))))
515 (pop results)
516 (push dir results))))
517 (dolist (dir (maybe-diddle-case dir2 diddle-case))
518 (add dir))
519 (dolist (dir (cdr dir1))
520 (add dir)))
521 (reverse results))))
523 (defun merge-pathnames (pathname
524 &optional
525 (defaults *default-pathname-defaults*)
526 (default-version :newest))
527 "Construct a filled in pathname by completing the unspecified components
528 from the defaults."
529 (declare (type pathname-designator pathname)
530 (type pathname-designator defaults)
531 (values pathname))
532 (with-pathname (defaults defaults)
533 (let ((pathname (let ((*default-pathname-defaults* defaults))
534 (pathname pathname))))
535 (let* ((default-host (%pathname-host defaults))
536 (pathname-host (%pathname-host pathname))
537 (diddle-case
538 (and default-host pathname-host
539 (not (eq (host-customary-case default-host)
540 (host-customary-case pathname-host)))))
541 (directory (merge-directories (%pathname-directory pathname)
542 (%pathname-directory defaults)
543 diddle-case)))
544 (%make-maybe-logical-pathname
545 (or pathname-host default-host)
546 (and ;; The device of ~/ shouldn't be merged,
547 ;; because the expansion may have a different device
548 (not (and (>= (length directory) 2)
549 (eql (car directory) :absolute)
550 (eql (cadr directory) :home)))
551 (or (%pathname-device pathname)
552 (maybe-diddle-case (%pathname-device defaults)
553 diddle-case)))
554 directory
555 (or (%pathname-name pathname)
556 (maybe-diddle-case (%pathname-name defaults)
557 diddle-case))
558 (or (%pathname-type pathname)
559 (maybe-diddle-case (%pathname-type defaults)
560 diddle-case))
561 (or (%pathname-version pathname)
562 (and (not (%pathname-name pathname)) (%pathname-version defaults))
563 default-version))))))
565 (defun import-directory (directory diddle-case)
566 (etypecase directory
567 (null nil)
568 ((member :wild) '(:absolute :wild-inferiors))
569 ((member :unspecific) '(:relative))
570 (list
571 (let ((root (pop directory))
572 results)
573 (if (member root '(:relative :absolute))
574 (push root results)
575 (error "List of directory components must start with ~S or ~S."
576 :absolute :relative))
577 (when directory
578 (let ((next (car directory)))
579 (when (or (eq :home next)
580 (typep next '(cons (eql :home) (cons string null))))
581 (push (pop directory) results)))
582 (dolist (piece directory)
583 (typecase piece
584 ((member :wild :wild-inferiors :up)
585 (push piece results))
586 ((member :back)
587 (if (typep (car results) '(or string pattern
588 (member :wild :wild-inferiors)))
589 (pop results)
590 (push piece results)))
591 ((or string pattern)
592 (when (typep piece '(and string (not simple-array)))
593 (setq piece (coerce piece 'simple-string)))
594 ;; Unix namestrings allow embedded "//" within them. Consecutive
595 ;; slashes are treated as one, which is weird but often convenient.
596 ;; However, preserving empty directory components:
597 ;; - is unaesthetic
598 ;; - makes (NAMESTRING (MAKE-PATHNAME :DIRECTORY '(:RELATIVE "" "d")))
599 ;; visually indistinguishable from the absolute pathname "/d/"
600 ;; - can causes a pathname equality test to return NIL
601 ;; on semantically equivalent pathnames. This can happen for
602 ;; other reasons, but fewer false negatives is better.
603 (unless (and (stringp piece) (zerop (length piece)))
604 (push (maybe-diddle-case piece diddle-case) results)))
606 (error "~S is not allowed as a directory component." piece)))))
607 (nreverse results)))
608 (string
609 (cond ((zerop (length directory)) `(:absolute))
611 (when (typep directory '(not simple-array))
612 (setq directory (coerce directory 'simple-string)))
613 `(:absolute ,(maybe-diddle-case directory diddle-case)))))))
615 (defun make-pathname (&key host
616 (device nil devp)
617 (directory nil dirp)
618 (name nil namep)
619 (type nil typep)
620 (version nil versionp)
621 defaults
622 (case :local))
623 "Makes a new pathname from the component arguments. Note that host is
624 a host-structure or string."
625 (declare (type (or string host pathname-component-tokens) host)
626 (type (or string pathname-component-tokens) device)
627 (type (or list string pattern pathname-component-tokens) directory)
628 (type (or string pattern pathname-component-tokens) name type)
629 (type (or integer pathname-component-tokens (member :newest))
630 version)
631 (type (or pathname-designator null) defaults)
632 (type (member :common :local) case))
633 (let* ((defaults (when defaults
634 (with-pathname (defaults defaults) defaults)))
635 (default-host (if defaults
636 (%pathname-host defaults)
637 (pathname-host *default-pathname-defaults*)))
638 ;; Raymond Toy writes: CLHS says make-pathname can take a
639 ;; string (as a logical-host) for the host part. We map that
640 ;; string into the corresponding logical host structure.
642 ;; Paul Werkowski writes:
643 ;; HyperSpec says for the arg to MAKE-PATHNAME;
644 ;; "host---a valid physical pathname host. ..."
645 ;; where it probably means -- a valid pathname host.
646 ;; "valid pathname host n. a valid physical pathname host or
647 ;; a valid logical pathname host."
648 ;; and defines
649 ;; "valid physical pathname host n. any of a string,
650 ;; a list of strings, or the symbol :unspecific,
651 ;; that is recognized by the implementation as the name of a host."
652 ;; "valid logical pathname host n. a string that has been defined
653 ;; as the name of a logical host. ..."
654 ;; HS is silent on what happens if the :HOST arg is NOT one of these.
655 ;; It seems an error message is appropriate.
656 (host (or (find-host host nil) default-host))
657 (diddle-args (and (eq (host-customary-case host) :lower)
658 (eq case :common)))
659 (diddle-defaults
660 (not (eq (host-customary-case host)
661 (host-customary-case default-host))))
662 (dev (if devp device (if defaults (%pathname-device defaults))))
663 (dir (import-directory directory diddle-args))
664 (ver (cond
665 (versionp version)
666 (defaults (%pathname-version defaults))
667 (t nil))))
668 (when (and defaults (not dirp))
669 (setf dir
670 (merge-directories dir
671 (%pathname-directory defaults)
672 diddle-defaults)))
674 (macrolet ((pick (var varp field)
675 `(cond ((or (simple-string-p ,var)
676 (pattern-p ,var))
677 (maybe-diddle-case ,var diddle-args))
678 ((stringp ,var)
679 (maybe-diddle-case (coerce ,var 'simple-string)
680 diddle-args))
681 (,varp
682 (maybe-diddle-case ,var diddle-args))
683 (defaults
684 (maybe-diddle-case (,field defaults)
685 diddle-defaults))
687 nil))))
688 (%make-maybe-logical-pathname host
689 dev ; forced to :UNSPECIFIC when logical
691 (pick name namep %pathname-name)
692 (pick type typep %pathname-type)
693 ver))))
695 (defun pathname-host (pathname &key (case :local))
696 "Return PATHNAME's host."
697 (declare (type pathname-designator pathname)
698 (type (member :local :common) case)
699 (values host)
700 (ignore case))
701 (with-pathname (pathname pathname)
702 (%pathname-host pathname)))
704 (defun pathname-device (pathname &key (case :local))
705 "Return PATHNAME's device."
706 (declare (type pathname-designator pathname)
707 (type (member :local :common) case))
708 (with-pathname (pathname pathname)
709 (maybe-diddle-case (%pathname-device pathname)
710 (and (eq case :common)
711 (eq (host-customary-case
712 (%pathname-host pathname))
713 :lower)))))
715 (defun pathname-directory (pathname &key (case :local))
716 "Return PATHNAME's directory."
717 (declare (type pathname-designator pathname)
718 (type (member :local :common) case))
719 (with-pathname (pathname pathname)
720 (maybe-diddle-case (%pathname-directory pathname)
721 (and (eq case :common)
722 (eq (host-customary-case
723 (%pathname-host pathname))
724 :lower)))))
725 (defun pathname-name (pathname &key (case :local))
726 "Return PATHNAME's name."
727 (declare (type pathname-designator pathname)
728 (type (member :local :common) case))
729 (with-pathname (pathname pathname)
730 (maybe-diddle-case (%pathname-name pathname)
731 (and (eq case :common)
732 (eq (host-customary-case
733 (%pathname-host pathname))
734 :lower)))))
736 (defun pathname-type (pathname &key (case :local))
737 "Return PATHNAME's type."
738 (declare (type pathname-designator pathname)
739 (type (member :local :common) case))
740 (with-pathname (pathname pathname)
741 (maybe-diddle-case (%pathname-type pathname)
742 (and (eq case :common)
743 (eq (host-customary-case
744 (%pathname-host pathname))
745 :lower)))))
747 (defun pathname-version (pathname)
748 "Return PATHNAME's version."
749 (declare (type pathname-designator pathname))
750 (with-pathname (pathname pathname)
751 (%pathname-version pathname)))
753 ;;;; namestrings
755 ;;; Handle the case for PARSE-NAMESTRING parsing a potentially
756 ;;; syntactically valid logical namestring with an explicit host.
758 ;;; This then isn't fully general -- we are relying on the fact that
759 ;;; we will only pass to parse-namestring namestring with an explicit
760 ;;; logical host, so that we can pass the host return from
761 ;;; parse-logical-namestring through to %PARSE-NAMESTRING as a truth
762 ;;; value. Yeah, this is probably a KLUDGE - CSR, 2002-04-18
763 (defun parseable-logical-namestring-p (namestr start end)
764 (catch 'exit
765 (handler-bind
766 ((namestring-parse-error (lambda (c)
767 (declare (ignore c))
768 (throw 'exit nil))))
769 (let ((colon (position #\: namestr :start start :end end)))
770 (when colon
771 (let ((potential-host
772 (logical-word-or-lose (subseq namestr start colon))))
773 ;; depending on the outcome of CSR comp.lang.lisp post
774 ;; "can PARSE-NAMESTRING create logical hosts", we may need
775 ;; to do things with potential-host (create it
776 ;; temporarily, parse the namestring and unintern the
777 ;; logical host potential-host on failure.
778 (declare (ignore potential-host))
779 (let ((result
780 (handler-bind
781 ((simple-type-error (lambda (c)
782 (declare (ignore c))
783 (throw 'exit nil))))
784 (parse-logical-namestring namestr start end))))
785 ;; if we got this far, we should have an explicit host
786 ;; (first return value of parse-logical-namestring)
787 (aver result)
788 result)))))))
790 ;;; Handle the case where PARSE-NAMESTRING is actually parsing a
791 ;;; namestring. We pick off the :JUNK-ALLOWED case then find a host to
792 ;;; use for parsing, call the parser, then check whether the host matches.
793 (defun %parse-namestring (namestr host defaults start end junk-allowed)
794 (declare (type (or host null) host)
795 (type string namestr)
796 (type index start)
797 (type (or index null) end))
798 (cond
799 (junk-allowed
800 (handler-case
801 (%parse-namestring namestr host defaults start end nil)
802 (namestring-parse-error (condition)
803 (values nil (namestring-parse-error-offset condition)))))
805 (let* ((end (%check-vector-sequence-bounds namestr start end)))
806 (multiple-value-bind (new-host device directory file type version)
807 ;; Comments below are quotes from the HyperSpec
808 ;; PARSE-NAMESTRING entry, reproduced here to demonstrate
809 ;; that we actually have to do things this way rather than
810 ;; some possibly more logical way. - CSR, 2002-04-18
811 (cond
812 ;; "If host is a logical host then thing is parsed as a
813 ;; logical pathname namestring on the host."
814 (host (funcall (host-parse host) namestr start end))
815 ;; "If host is nil and thing is a syntactically valid
816 ;; logical pathname namestring containing an explicit
817 ;; host, then it is parsed as a logical pathname
818 ;; namestring."
819 ((parseable-logical-namestring-p namestr start end)
820 (parse-logical-namestring namestr start end))
821 ;; "If host is nil, default-pathname is a logical
822 ;; pathname, and thing is a syntactically valid logical
823 ;; pathname namestring without an explicit host, then it
824 ;; is parsed as a logical pathname namestring on the
825 ;; host that is the host component of default-pathname."
827 ;; "Otherwise, the parsing of thing is
828 ;; implementation-defined."
830 ;; Both clauses are handled here, as the default
831 ;; *DEFAULT-PATHNAME-DEFAULTS* has a SB-IMPL::UNIX-HOST
832 ;; for a host.
833 ((pathname-host defaults)
834 (funcall (host-parse (pathname-host defaults))
835 namestr
836 start
837 end))
838 ;; I don't think we should ever get here, as the default
839 ;; host will always have a non-null HOST, given that we
840 ;; can't create a new pathname without going through
841 ;; *DEFAULT-PATHNAME-DEFAULTS*, which has a non-null
842 ;; host...
843 (t (bug "Fallen through COND in %PARSE-NAMESTRING")))
844 (when (and host new-host (not (eq new-host host)))
845 (error 'simple-type-error
846 :datum new-host
847 ;; Note: ANSI requires that this be a TYPE-ERROR,
848 ;; but there seems to be no completely correct
849 ;; value to use for TYPE-ERROR-EXPECTED-TYPE.
850 ;; Instead, we return a sort of "type error allowed
851 ;; type", trying to say "it would be OK if you
852 ;; passed NIL as the host value" but not mentioning
853 ;; that a matching string would be OK too.
854 :expected-type 'null
855 :format-control
856 "The host in the namestring, ~S,~@
857 does not match the explicit HOST argument, ~S."
858 :format-arguments (list new-host host)))
859 (let ((pn-host (or new-host host (pathname-host defaults))))
860 (values (%make-maybe-logical-pathname
861 pn-host device directory file type version)
862 end)))))))
864 ;;; If NAMESTR begins with a colon-terminated, defined, logical host,
865 ;;; then return that host, otherwise return NIL.
866 (defun extract-logical-host-prefix (namestr start end)
867 (declare (type simple-string namestr)
868 (type index start end)
869 (values (or logical-host null)))
870 (let ((colon-pos (position #\: namestr :start start :end end)))
871 (if colon-pos
872 (values (gethash (nstring-upcase (subseq namestr start colon-pos))
873 *logical-hosts*))
874 nil)))
876 (defun parse-namestring (thing
877 &optional
878 host
879 (defaults *default-pathname-defaults*)
880 &key (start 0) end junk-allowed)
881 (declare (ftype (function * (values (or null pathname) (or null index)))
882 %parse-namestring))
883 (with-host (found-host host)
884 (let (;; According to ANSI defaults may be any valid pathname designator
885 (defaults (etypecase defaults
886 (pathname
887 defaults)
888 (string
889 (aver (pathnamep *default-pathname-defaults*))
890 (parse-namestring defaults))
891 (stream
892 (truename defaults)))))
893 (declare (type pathname defaults))
894 (etypecase thing
895 (string
896 (with-array-data ((thing thing) (start start) (end end)
897 :check-fill-pointer t)
898 (multiple-value-bind (pathname position)
899 (%parse-namestring thing found-host defaults start end junk-allowed)
900 (values pathname (- position start)))))
901 (pathname
902 (let ((defaulted-host (or found-host (%pathname-host defaults))))
903 (declare (type host defaulted-host))
904 (unless (eq defaulted-host (%pathname-host thing))
905 (error "The HOST argument doesn't match the pathname host:~% ~
906 ~S and ~S."
907 defaulted-host (%pathname-host thing))))
908 (values thing start))
909 (stream
910 (let ((name (file-name thing)))
911 (unless name
912 (error "can't figure out the file associated with stream:~% ~S"
913 thing))
914 (values name nil)))))))
916 (defun %parse-native-namestring (namestr host defaults start end junk-allowed
917 as-directory)
918 (declare (type (or host null) host)
919 (type string namestr)
920 (type index start)
921 (type (or index null) end))
922 (cond
923 (junk-allowed
924 (handler-case
925 (%parse-native-namestring namestr host defaults start end nil as-directory)
926 (namestring-parse-error (condition)
927 (values nil (namestring-parse-error-offset condition)))))
929 (let* ((end (%check-vector-sequence-bounds namestr start end)))
930 (multiple-value-bind (new-host device directory file type version)
931 (cond
932 (host
933 (funcall (host-parse-native host) namestr start end as-directory))
934 ((pathname-host defaults)
935 (funcall (host-parse-native (pathname-host defaults))
936 namestr
937 start
939 as-directory))
940 ;; I don't think we should ever get here, as the default
941 ;; host will always have a non-null HOST, given that we
942 ;; can't create a new pathname without going through
943 ;; *DEFAULT-PATHNAME-DEFAULTS*, which has a non-null
944 ;; host...
945 (t (bug "Fallen through COND in %PARSE-NAMESTRING")))
946 (when (and host new-host (not (eq new-host host)))
947 (error 'simple-type-error
948 :datum new-host
949 :expected-type `(or null (eql ,host))
950 :format-control
951 "The host in the namestring, ~S,~@
952 does not match the explicit HOST argument, ~S."
953 :format-arguments (list new-host host)))
954 (let ((pn-host (or new-host host (pathname-host defaults))))
955 (values (%make-pathname
956 pn-host device directory file type version)
957 end)))))))
959 (defun parse-native-namestring (thing
960 &optional
961 host
962 (defaults *default-pathname-defaults*)
963 &key (start 0) end junk-allowed
964 as-directory)
965 "Convert THING into a pathname, using the native conventions
966 appropriate for the pathname host HOST, or if not specified the
967 host of DEFAULTS. If THING is a string, the parse is bounded by
968 START and END, and error behaviour is controlled by JUNK-ALLOWED,
969 as with PARSE-NAMESTRING. For file systems whose native
970 conventions allow directories to be indicated as files, if
971 AS-DIRECTORY is true, return a pathname denoting THING as a
972 directory."
973 (declare (type pathname-designator thing defaults)
974 (type (or list host string (member :unspecific)) host)
975 (type index start)
976 (type (or index null) end)
977 (type (or t null) junk-allowed)
978 (values (or null pathname) (or null index)))
979 (declare (ftype (function * (values (or null pathname) (or null index)))
980 %parse-native-namestring))
981 (with-host (found-host host)
982 (let ((defaults (etypecase defaults
983 (pathname
984 defaults)
985 (string
986 (aver (pathnamep *default-pathname-defaults*))
987 (parse-native-namestring defaults))
988 (stream
989 (truename defaults)))))
990 (declare (type pathname defaults))
991 (etypecase thing
992 (string
993 (with-array-data ((thing thing) (start start) (end end)
994 :check-fill-pointer t)
995 (multiple-value-bind (pathname position)
996 (%parse-native-namestring thing
997 found-host defaults start end junk-allowed
998 as-directory)
999 (values pathname (- position start)))))
1000 (pathname
1001 (let ((defaulted-host (or found-host (%pathname-host defaults))))
1002 (declare (type host defaulted-host))
1003 (unless (eq defaulted-host (%pathname-host thing))
1004 (error "The HOST argument doesn't match the pathname host:~% ~
1005 ~S and ~S."
1006 defaulted-host (%pathname-host thing))))
1007 (values thing start))
1008 (stream
1009 ;; FIXME
1010 (let ((name (file-name thing)))
1011 (unless name
1012 (error "can't figure out the file associated with stream:~% ~S"
1013 thing))
1014 (values name nil)))))))
1016 (defun namestring (pathname)
1017 "Construct the full (name)string form of the pathname."
1018 (declare (type pathname-designator pathname))
1019 (with-pathname (pathname pathname)
1020 (when pathname
1021 (or (%pathname-namestring pathname)
1022 (let ((host (%pathname-host pathname)))
1023 (if (not host)
1024 (error
1025 "can't determine the namestring for pathnames with no host:~% ~S"
1026 pathname)
1027 (setf (%pathname-namestring pathname)
1028 (logically-readonlyize
1029 (possibly-base-stringize
1030 (funcall (host-unparse host) pathname))))))))))
1032 (defun native-namestring (pathname &key as-file)
1033 "Construct the full native (name)string form of PATHNAME. For
1034 file systems whose native conventions allow directories to be
1035 indicated as files, if AS-FILE is true and the name, type, and
1036 version components of PATHNAME are all NIL or :UNSPECIFIC,
1037 construct a string that names the directory according to the file
1038 system's syntax for files."
1039 (declare (type pathname-designator pathname))
1040 (with-native-pathname (pathname pathname)
1041 (when pathname
1042 (let ((host (%pathname-host pathname)))
1043 (unless host
1044 (error "can't determine the native namestring for pathnames with no ~
1045 host:~% ~S" pathname))
1046 (funcall (host-unparse-native host) pathname as-file)))))
1048 (defun host-namestring (pathname)
1049 "Return a string representation of the name of the host in the pathname."
1050 (declare (type pathname-designator pathname))
1051 (with-pathname (pathname pathname)
1052 (let ((host (%pathname-host pathname)))
1053 (if host
1054 (funcall (host-unparse-host host) pathname)
1055 (error
1056 "can't determine the namestring for pathnames with no host:~% ~S"
1057 pathname)))))
1059 (defun directory-namestring (pathname)
1060 "Return a string representation of the directories used in the pathname."
1061 (declare (type pathname-designator pathname))
1062 (with-pathname (pathname pathname)
1063 (let ((host (%pathname-host pathname)))
1064 (if host
1065 (funcall (host-unparse-directory host) pathname)
1066 (error
1067 "can't determine the namestring for pathnames with no host:~% ~S"
1068 pathname)))))
1070 (defun file-namestring (pathname)
1071 "Return a string representation of the name used in the pathname."
1072 (declare (type pathname-designator pathname))
1073 (with-pathname (pathname pathname)
1074 (let ((host (%pathname-host pathname)))
1075 (if host
1076 (funcall (host-unparse-file host) pathname)
1077 (error
1078 "can't determine the namestring for pathnames with no host:~% ~S"
1079 pathname)))))
1081 (defun enough-namestring (pathname
1082 &optional
1083 (defaults *default-pathname-defaults*))
1084 "Return an abbreviated pathname sufficient to identify the pathname relative
1085 to the defaults."
1086 (declare (type pathname-designator pathname))
1087 (with-pathname (pathname pathname)
1088 (let ((host (%pathname-host pathname)))
1089 (if host
1090 (with-pathname (defaults defaults)
1091 (funcall (host-unparse-enough host) pathname defaults))
1092 (error
1093 "can't determine the namestring for pathnames with no host:~% ~S"
1094 pathname)))))
1096 ;;;; wild pathnames
1098 (defun wild-pathname-p (pathname &optional field-key)
1099 "Predicate for determining whether pathname contains any wildcards."
1100 (declare (type pathname-designator pathname)
1101 (type (member nil :host :device :directory :name :type :version)
1102 field-key))
1103 (with-pathname (pathname pathname)
1104 (flet ((frob (x)
1105 (or (pattern-p x) (member x '(:wild :wild-inferiors)))))
1106 (ecase field-key
1107 ((nil)
1108 (or (wild-pathname-p pathname :host)
1109 (wild-pathname-p pathname :device)
1110 (wild-pathname-p pathname :directory)
1111 (wild-pathname-p pathname :name)
1112 (wild-pathname-p pathname :type)
1113 (wild-pathname-p pathname :version)))
1114 (:host (frob (%pathname-host pathname)))
1115 (:device (frob (%pathname-host pathname)))
1116 (:directory (some #'frob (%pathname-directory pathname)))
1117 (:name (frob (%pathname-name pathname)))
1118 (:type (frob (%pathname-type pathname)))
1119 (:version (frob (%pathname-version pathname)))))))
1121 (defun pathname-match-p (in-pathname in-wildname)
1122 "Pathname matches the wildname template?"
1123 (declare (type pathname-designator in-pathname))
1124 (with-pathname (pathname in-pathname)
1125 (with-pathname (wildname in-wildname)
1126 (macrolet ((frob (field &optional (op 'components-match))
1127 `(or (null (,field wildname))
1128 (,op (,field pathname) (,field wildname)))))
1129 (and (or (null (%pathname-host wildname))
1130 (eq (%pathname-host wildname) (%pathname-host pathname)))
1131 (frob %pathname-device)
1132 (frob %pathname-directory directory-components-match)
1133 (frob %pathname-name)
1134 (frob %pathname-type)
1135 (or (eq (%pathname-host wildname) *physical-host*)
1136 (frob %pathname-version)))))))
1138 ;;; Place the substitutions into the pattern and return the string or pattern
1139 ;;; that results. If DIDDLE-CASE is true, we diddle the result case as well,
1140 ;;; in case we are translating between hosts with difference conventional case.
1141 ;;; The second value is the tail of subs with all of the values that we used up
1142 ;;; stripped off. Note that PATTERN-MATCHES matches all consecutive wildcards
1143 ;;; as a single string, so we ignore subsequent contiguous wildcards.
1144 (defun substitute-into (pattern subs diddle-case)
1145 (declare (type pattern pattern)
1146 (type list subs)
1147 (values (or simple-string pattern) list))
1148 (let ((in-wildcard nil)
1149 (pieces nil)
1150 (strings nil))
1151 (dolist (piece (pattern-pieces pattern))
1152 (cond ((simple-string-p piece)
1153 (push piece strings)
1154 (setf in-wildcard nil))
1155 (in-wildcard)
1157 (setf in-wildcard t)
1158 (unless subs
1159 (error "not enough wildcards in FROM pattern to match ~
1160 TO pattern:~% ~S"
1161 pattern))
1162 (let ((sub (pop subs)))
1163 (typecase sub
1164 (pattern
1165 (when strings
1166 (push (apply #'concatenate 'simple-string
1167 (nreverse strings))
1168 pieces))
1169 (dolist (piece (pattern-pieces sub))
1170 (push piece pieces)))
1171 (simple-string
1172 (push sub strings))
1174 (error "can't substitute this into the middle of a word:~
1175 ~% ~S"
1176 sub)))))))
1178 (when strings
1179 (push (apply #'concatenate 'simple-string (nreverse strings))
1180 pieces))
1181 (values
1182 (maybe-diddle-case
1183 (if (and pieces (simple-string-p (car pieces)) (null (cdr pieces)))
1184 (car pieces)
1185 (make-pattern (nreverse pieces)))
1186 diddle-case)
1187 subs)))
1189 ;;; Called when we can't see how source and from matched.
1190 (defun didnt-match-error (source from)
1191 (error "Pathname components from SOURCE and FROM args to TRANSLATE-PATHNAME~@
1192 did not match:~% ~S ~S"
1193 source from))
1195 ;;; Do TRANSLATE-COMPONENT for all components except host, directory
1196 ;;; and version.
1197 (defun translate-component (source from to diddle-case)
1198 (typecase to
1199 (pattern
1200 (typecase from
1201 (pattern
1202 (typecase source
1203 (pattern
1204 (if (pattern= from source)
1205 source
1206 (didnt-match-error source from)))
1207 (simple-string
1208 (multiple-value-bind (won subs) (pattern-matches from source)
1209 (if won
1210 (values (substitute-into to subs diddle-case))
1211 (didnt-match-error source from))))
1213 (maybe-diddle-case source diddle-case))))
1214 ((member :wild)
1215 (values (substitute-into to (list source) diddle-case)))
1217 (if (components-match source from)
1218 (maybe-diddle-case source diddle-case)
1219 (didnt-match-error source from)))))
1220 ((member nil :wild)
1221 (maybe-diddle-case source diddle-case))
1223 (if (components-match source from)
1225 (didnt-match-error source from)))))
1227 ;;; Return a list of all the things that we want to substitute into the TO
1228 ;;; pattern (the things matched by from on source.) When From contains
1229 ;;; :WILD-INFERIORS, the result contains a sublist of the matched source
1230 ;;; subdirectories.
1231 (defun compute-directory-substitutions (orig-source orig-from)
1232 (let ((source orig-source)
1233 (from orig-from))
1234 (collect ((subs))
1235 (loop
1236 (unless source
1237 (unless (every (lambda (x) (eq x :wild-inferiors)) from)
1238 (didnt-match-error orig-source orig-from))
1239 (subs ())
1240 (return))
1241 (unless from (didnt-match-error orig-source orig-from))
1242 (let ((from-part (pop from))
1243 (source-part (pop source)))
1244 (typecase from-part
1245 (pattern
1246 (typecase source-part
1247 (pattern
1248 (if (pattern= from-part source-part)
1249 (subs source-part)
1250 (didnt-match-error orig-source orig-from)))
1251 (simple-string
1252 (multiple-value-bind (won new-subs)
1253 (pattern-matches from-part source-part)
1254 (if won
1255 (dolist (sub new-subs)
1256 (subs sub))
1257 (didnt-match-error orig-source orig-from))))
1259 (didnt-match-error orig-source orig-from))))
1260 ((member :wild)
1261 (subs source-part))
1262 ((member :wild-inferiors)
1263 (let ((remaining-source (cons source-part source)))
1264 (collect ((res))
1265 (loop
1266 (when (directory-components-match remaining-source from)
1267 (return))
1268 (unless remaining-source
1269 (didnt-match-error orig-source orig-from))
1270 (res (pop remaining-source)))
1271 (subs (res))
1272 (setq source remaining-source))))
1273 (simple-string
1274 (unless (and (simple-string-p source-part)
1275 (string= from-part source-part))
1276 (didnt-match-error orig-source orig-from)))
1278 (didnt-match-error orig-source orig-from)))))
1279 (subs))))
1281 ;;; This is called by TRANSLATE-PATHNAME on the directory components
1282 ;;; of its argument pathnames to produce the result directory
1283 ;;; component. If this leaves the directory NIL, we return the source
1284 ;;; directory. The :RELATIVE or :ABSOLUTE is taken from the source
1285 ;;; directory, except if TO is :ABSOLUTE, in which case the result
1286 ;;; will be :ABSOLUTE.
1287 (defun translate-directories (source from to diddle-case)
1288 (if (not (and source to from))
1289 (or (and to (null source) (remove :wild-inferiors to))
1290 (mapcar (lambda (x) (maybe-diddle-case x diddle-case)) source))
1291 (collect ((res))
1292 ;; If TO is :ABSOLUTE, the result should still be :ABSOLUTE.
1293 (res (if (eq (first to) :absolute)
1294 :absolute
1295 (first source)))
1296 (let ((subs-left (compute-directory-substitutions (rest source)
1297 (rest from))))
1298 (dolist (to-part (rest to))
1299 (typecase to-part
1300 ((member :wild)
1301 (aver subs-left)
1302 (let ((match (pop subs-left)))
1303 (when (listp match)
1304 (error ":WILD-INFERIORS is not paired in from and to ~
1305 patterns:~% ~S ~S" from to))
1306 (res (maybe-diddle-case match diddle-case))))
1307 ((member :wild-inferiors)
1308 (aver subs-left)
1309 (let ((match (pop subs-left)))
1310 (unless (listp match)
1311 (error ":WILD-INFERIORS not paired in from and to ~
1312 patterns:~% ~S ~S" from to))
1313 (dolist (x match)
1314 (res (maybe-diddle-case x diddle-case)))))
1315 (pattern
1316 (multiple-value-bind
1317 (new new-subs-left)
1318 (substitute-into to-part subs-left diddle-case)
1319 (setf subs-left new-subs-left)
1320 (res new)))
1321 (t (res to-part)))))
1322 (res))))
1324 (defun translate-pathname (source from-wildname to-wildname &key)
1325 "Use the source pathname to translate the from-wildname's wild and
1326 unspecified elements into a completed to-pathname based on the to-wildname."
1327 (declare (type pathname-designator source from-wildname to-wildname))
1328 (with-pathname (source source)
1329 (with-pathname (from from-wildname)
1330 (with-pathname (to to-wildname)
1331 (let* ((source-host (%pathname-host source))
1332 (from-host (%pathname-host from))
1333 (to-host (%pathname-host to))
1334 (diddle-case
1335 (and source-host to-host
1336 (not (eq (host-customary-case source-host)
1337 (host-customary-case to-host))))))
1338 (macrolet ((frob (field &optional (op 'translate-component))
1339 `(let ((result (,op (,field source)
1340 (,field from)
1341 (,field to)
1342 diddle-case)))
1343 (if (eq result :error)
1344 (error "~S doesn't match ~S." source from)
1345 result))))
1346 (%make-maybe-logical-pathname
1347 (or to-host source-host)
1348 (frob %pathname-device)
1349 (frob %pathname-directory translate-directories)
1350 (frob %pathname-name)
1351 (frob %pathname-type)
1352 (if (eq from-host *physical-host*)
1353 (if (or (eq (%pathname-version to) :wild)
1354 (eq (%pathname-version to) nil))
1355 (%pathname-version source)
1356 (%pathname-version to))
1357 (frob %pathname-version)))))))))
1359 ;;;; logical pathname support. ANSI 92-102 specification.
1360 ;;;;
1361 ;;;; As logical-pathname translations are loaded they are
1362 ;;;; canonicalized as patterns to enable rapid efficient translation
1363 ;;;; into physical pathnames.
1365 ;;;; utilities
1367 (defun simplify-namestring (namestring &optional host)
1368 (funcall (host-simplify-namestring
1369 (or host
1370 (pathname-host (sane-default-pathname-defaults))))
1371 namestring))
1373 ;;; Canonicalize a logical pathname word by uppercasing it checking that it
1374 ;;; contains only legal characters.
1375 (defun logical-word-or-lose (word)
1376 (declare (string word))
1377 (when (string= word "")
1378 (error 'namestring-parse-error
1379 :complaint "Attempted to treat invalid logical hostname ~
1380 as a logical host:~% ~S"
1381 :args (list word)
1382 :namestring word :offset 0))
1383 (let ((word (string-upcase word)))
1384 (dotimes (i (length word))
1385 (let ((ch (schar word i)))
1386 (unless (and (typep ch 'standard-char)
1387 (or (alpha-char-p ch) (digit-char-p ch) (char= ch #\-)))
1388 (error 'namestring-parse-error
1389 :complaint "logical namestring character which ~
1390 is not alphanumeric or hyphen:~% ~S"
1391 :args (list ch)
1392 :namestring word :offset i))))
1393 (coerce word 'string))) ; why not simple-string?
1395 ;;; Given a logical host or string, return a logical host. If ERROR-P
1396 ;;; is NIL, then return NIL when no such host exists.
1397 (defun find-logical-host (thing &optional (errorp t))
1398 (etypecase thing
1399 (string
1400 (let ((found (gethash (logical-word-or-lose thing)
1401 *logical-hosts*)))
1402 (if (or found (not errorp))
1403 found
1404 ;; This is the error signalled from e.g.
1405 ;; LOGICAL-PATHNAME-TRANSLATIONS when host is not a defined
1406 ;; host, and ANSI specifies that that's a TYPE-ERROR.
1407 (error 'simple-type-error
1408 :datum thing
1409 ;; God only knows what ANSI expects us to use for
1410 ;; the EXPECTED-TYPE here. Maybe this will be OK..
1411 :expected-type
1412 '(and string (satisfies logical-pathname-translations))
1413 :format-control "logical host not yet defined: ~S"
1414 :format-arguments (list thing)))))
1415 (logical-host thing)))
1417 ;;; Given a logical host name or host, return a logical host, creating
1418 ;;; a new one if necessary.
1419 (defun intern-logical-host (thing)
1420 (with-locked-system-table (*logical-hosts*)
1421 (or (find-logical-host thing nil)
1422 (let* ((name (logical-word-or-lose thing))
1423 (new (make-logical-host :name name)))
1424 (setf (gethash name *logical-hosts*) new)
1425 new))))
1427 ;;;; logical pathname parsing
1429 ;;; Deal with multi-char wildcards in a logical pathname token.
1430 (defun maybe-make-logical-pattern (namestring chunks)
1431 (let ((chunk (caar chunks)))
1432 (collect ((pattern))
1433 (let ((last-pos 0)
1434 (len (length chunk)))
1435 (declare (fixnum last-pos))
1436 (loop
1437 (when (= last-pos len) (return))
1438 (let ((pos (or (position #\* chunk :start last-pos) len)))
1439 (if (= pos last-pos)
1440 (when (pattern)
1441 (error 'namestring-parse-error
1442 :complaint "double asterisk inside of logical ~
1443 word: ~S"
1444 :args (list chunk)
1445 :namestring namestring
1446 :offset (+ (cdar chunks) pos)))
1447 (pattern (subseq chunk last-pos pos)))
1448 (if (= pos len)
1449 (return)
1450 (pattern :multi-char-wild))
1451 (setq last-pos (1+ pos)))))
1452 (aver (pattern))
1453 (if (cdr (pattern))
1454 (make-pattern (pattern))
1455 (let ((x (car (pattern))))
1456 (if (eq x :multi-char-wild)
1457 :wild
1458 x))))))
1460 ;;; Return a list of conses where the CDR is the start position and
1461 ;;; the CAR is a string (token) or character (punctuation.)
1462 (defun logical-chunkify (namestr start end)
1463 (collect ((chunks))
1464 (do ((i start (1+ i))
1465 (prev start))
1466 ((= i end)
1467 (when (> end prev)
1468 (chunks (cons (nstring-upcase (subseq namestr prev end)) prev))))
1469 (let ((ch (schar namestr i)))
1470 (unless (or (alpha-char-p ch) (digit-char-p ch)
1471 (member ch '(#\- #\*)))
1472 (when (> i prev)
1473 (chunks (cons (nstring-upcase (subseq namestr prev i)) prev)))
1474 (setq prev (1+ i))
1475 (unless (member ch '(#\; #\: #\.))
1476 (error 'namestring-parse-error
1477 :complaint "illegal character for logical pathname:~% ~S"
1478 :args (list ch)
1479 :namestring namestr
1480 :offset i))
1481 (chunks (cons ch i)))))
1482 (chunks)))
1484 ;;; Break up a logical-namestring, always a string, into its
1485 ;;; constituent parts.
1486 (defun parse-logical-namestring (namestr start end)
1487 (declare (type simple-string namestr)
1488 (type index start end))
1489 (collect ((directory))
1490 (let ((host nil)
1491 (name nil)
1492 (type nil)
1493 (version nil))
1494 (labels ((expecting (what chunks)
1495 (unless (and chunks (simple-string-p (caar chunks)))
1496 (error 'namestring-parse-error
1497 :complaint "expecting ~A, got ~:[nothing~;~S~]."
1498 :args (list what (caar chunks) (caar chunks))
1499 :namestring namestr
1500 :offset (if chunks (cdar chunks) end)))
1501 (caar chunks))
1502 (parse-host (chunks)
1503 (case (caadr chunks)
1504 (#\:
1505 (setq host
1506 (find-logical-host (expecting "a host name" chunks)))
1507 (parse-relative (cddr chunks)))
1509 (parse-relative chunks))))
1510 (parse-relative (chunks)
1511 (case (caar chunks)
1512 (#\;
1513 (directory :relative)
1514 (parse-directory (cdr chunks)))
1516 (directory :absolute) ; Assumption! Maybe revoked later.
1517 (parse-directory chunks))))
1518 (parse-directory (chunks)
1519 (case (caadr chunks)
1520 (#\;
1521 (directory
1522 (let ((res (expecting "a directory name" chunks)))
1523 (cond ((string= res "..") :up)
1524 ((string= res "**") :wild-inferiors)
1526 (maybe-make-logical-pattern namestr chunks)))))
1527 (parse-directory (cddr chunks)))
1529 (parse-name chunks))))
1530 (parse-name (chunks)
1531 (when chunks
1532 (expecting "a file name" chunks)
1533 (setq name (maybe-make-logical-pattern namestr chunks))
1534 (expecting-dot (cdr chunks))))
1535 (expecting-dot (chunks)
1536 (when chunks
1537 (unless (eql (caar chunks) #\.)
1538 (error 'namestring-parse-error
1539 :complaint "expecting a dot, got ~S."
1540 :args (list (caar chunks))
1541 :namestring namestr
1542 :offset (cdar chunks)))
1543 (if type
1544 (parse-version (cdr chunks))
1545 (parse-type (cdr chunks)))))
1546 (parse-type (chunks)
1547 (expecting "a file type" chunks)
1548 (setq type (maybe-make-logical-pattern namestr chunks))
1549 (expecting-dot (cdr chunks)))
1550 (parse-version (chunks)
1551 (let ((str (expecting "a positive integer, * or NEWEST"
1552 chunks)))
1553 (cond
1554 ((string= str "*") (setq version :wild))
1555 ((string= str "NEWEST") (setq version :newest))
1557 (multiple-value-bind (res pos)
1558 (parse-integer str :junk-allowed t)
1559 (unless (and res (plusp res))
1560 (error 'namestring-parse-error
1561 :complaint "expected a positive integer, ~
1562 got ~S"
1563 :args (list str)
1564 :namestring namestr
1565 :offset (+ pos (cdar chunks))))
1566 (setq version res)))))
1567 (when (cdr chunks)
1568 (error 'namestring-parse-error
1569 :complaint "extra stuff after end of file name"
1570 :namestring namestr
1571 :offset (cdadr chunks)))))
1572 (parse-host (logical-chunkify namestr start end)))
1573 (values host :unspecific (directory) name type version))))
1575 ;;; We can't initialize this yet because not all host methods are
1576 ;;; loaded yet.
1577 (defvar *logical-pathname-defaults*)
1579 (defun logical-namestring-p (x)
1580 (and (stringp x)
1581 (ignore-errors
1582 (typep (pathname x) 'logical-pathname))))
1584 (deftype logical-namestring ()
1585 `(satisfies logical-namestring-p))
1587 (defun logical-pathname (pathspec)
1588 "Converts the pathspec argument to a logical-pathname and returns it."
1589 (declare (type (or logical-pathname string stream) pathspec)
1590 (values logical-pathname))
1591 (if (typep pathspec 'logical-pathname)
1592 pathspec
1593 (flet ((oops (problem)
1594 (error 'simple-type-error
1595 :datum pathspec
1596 :expected-type 'logical-namestring
1597 :format-control "~S is not a valid logical namestring:~% ~A"
1598 :format-arguments (list pathspec problem))))
1599 (let ((res (handler-case
1600 (parse-namestring pathspec nil *logical-pathname-defaults*)
1601 (error (e) (oops e)))))
1602 (when (eq (%pathname-host res)
1603 (%pathname-host *logical-pathname-defaults*))
1604 (oops "no host specified"))
1605 res))))
1607 ;;;; logical pathname unparsing
1609 (defun unparse-logical-directory (pathname)
1610 (declare (type pathname pathname))
1611 (collect ((pieces))
1612 (let ((directory (%pathname-directory pathname)))
1613 (when directory
1614 (ecase (pop directory)
1615 (:absolute) ; nothing special
1616 (:relative (pieces ";")))
1617 (dolist (dir directory)
1618 (cond ((or (stringp dir) (pattern-p dir))
1619 (pieces (unparse-logical-piece dir))
1620 (pieces ";"))
1621 ((eq dir :wild)
1622 (pieces "*;"))
1623 ((eq dir :wild-inferiors)
1624 (pieces "**;"))
1626 (error "invalid directory component: ~S" dir))))))
1627 (apply #'concatenate 'simple-string (pieces))))
1629 (defun unparse-logical-piece (thing)
1630 (etypecase thing
1631 ((member :wild) "*")
1632 (simple-string thing)
1633 (pattern
1634 (collect ((strings))
1635 (dolist (piece (pattern-pieces thing))
1636 (etypecase piece
1637 (simple-string (strings piece))
1638 (keyword
1639 (cond ((eq piece :wild-inferiors)
1640 (strings "**"))
1641 ((eq piece :multi-char-wild)
1642 (strings "*"))
1643 (t (error "invalid keyword: ~S" piece))))))
1644 (apply #'concatenate 'simple-string (strings))))))
1646 (defun unparse-logical-file (pathname)
1647 (declare (type pathname pathname))
1648 (collect ((strings))
1649 (let* ((name (%pathname-name pathname))
1650 (type (%pathname-type pathname))
1651 (version (%pathname-version pathname))
1652 (type-supplied (not (or (null type) (eq type :unspecific))))
1653 (version-supplied (not (or (null version)
1654 (eq version :unspecific)))))
1655 (when name
1656 (when (and (null type)
1657 (typep name 'string)
1658 (position #\. name :start 1))
1659 (error "too many dots in the name: ~S" pathname))
1660 (strings (unparse-logical-piece name)))
1661 (when type-supplied
1662 (unless name
1663 (error "cannot specify the type without a file: ~S" pathname))
1664 (when (typep type 'string)
1665 (when (position #\. type)
1666 (error "type component can't have a #\. inside: ~S" pathname)))
1667 (strings ".")
1668 (strings (unparse-logical-piece type)))
1669 (when version-supplied
1670 (unless type-supplied
1671 (error "cannot specify the version without a type: ~S" pathname))
1672 (etypecase version
1673 ((member :newest) (strings ".NEWEST"))
1674 ((member :wild) (strings ".*"))
1675 (fixnum (strings ".") (strings (format nil "~D" version))))))
1676 (apply #'concatenate 'simple-string (strings))))
1678 ;;; Unparse a logical pathname string.
1679 (defun unparse-enough-namestring (pathname defaults)
1680 (let* ((path-directory (pathname-directory pathname))
1681 (def-directory (pathname-directory defaults))
1682 (enough-directory
1683 ;; Go down the directory lists to see what matches. What's
1684 ;; left is what we want, more or less.
1685 (cond ((and (eq (first path-directory) (first def-directory))
1686 (eq (first path-directory) :absolute))
1687 ;; Both paths are :ABSOLUTE, so find where the
1688 ;; common parts end and return what's left
1689 (do* ((p (rest path-directory) (rest p))
1690 (d (rest def-directory) (rest d)))
1691 ((or (endp p) (endp d)
1692 (not (equal (first p) (first d))))
1693 `(:relative ,@p))))
1695 ;; At least one path is :RELATIVE, so just return the
1696 ;; original path. If the original path is :RELATIVE,
1697 ;; then that's the right one. If PATH-DIRECTORY is
1698 ;; :ABSOLUTE, we want to return that except when
1699 ;; DEF-DIRECTORY is :ABSOLUTE, as handled above. so return
1700 ;; the original directory.
1701 path-directory))))
1702 (unparse-logical-namestring
1703 (make-pathname :host (pathname-host pathname)
1704 :directory enough-directory
1705 :name (pathname-name pathname)
1706 :type (pathname-type pathname)
1707 :version (pathname-version pathname)))))
1709 (defun unparse-logical-namestring (pathname)
1710 (declare (type logical-pathname pathname))
1711 (concatenate 'simple-string
1712 (logical-host-name (%pathname-host pathname)) ":"
1713 (unparse-logical-directory pathname)
1714 (unparse-logical-file pathname)))
1716 ;;;; logical pathname translations
1718 ;;; Verify that the list of translations consists of lists and prepare
1719 ;;; canonical translations. (Parse pathnames and expand out wildcards
1720 ;;; into patterns.)
1721 (defun canonicalize-logical-pathname-translations (translation-list host)
1722 (declare (type list translation-list) (type host host)
1723 (values list))
1724 (mapcar (lambda (translation)
1725 (destructuring-bind (from to) translation
1726 (list (if (typep from 'logical-pathname)
1727 from
1728 (parse-namestring from host))
1729 (pathname to))))
1730 translation-list))
1732 (defun logical-pathname-translations (host)
1733 "Return the (logical) host object argument's list of translations."
1734 (declare (type (or string logical-host) host)
1735 (values list))
1736 (logical-host-translations (find-logical-host host)))
1738 (defun (setf logical-pathname-translations) (translations host)
1739 "Set the translations list for the logical host argument."
1740 (declare (type (or string logical-host) host)
1741 (type list translations)
1742 (values list))
1743 (let ((host (intern-logical-host host)))
1744 (setf (logical-host-canon-transls host)
1745 (canonicalize-logical-pathname-translations translations host))
1746 (setf (logical-host-translations host) translations)))
1748 (defun translate-logical-pathname (pathname &key)
1749 "Translate PATHNAME to a physical pathname, which is returned."
1750 (declare (type pathname-designator pathname)
1751 (values (or null pathname)))
1752 (typecase pathname
1753 (logical-pathname
1754 (dolist (x (logical-host-canon-transls (%pathname-host pathname))
1755 (error 'simple-file-error
1756 :pathname pathname
1757 :format-control "no translation for ~S"
1758 :format-arguments (list pathname)))
1759 (destructuring-bind (from to) x
1760 (when (pathname-match-p pathname from)
1761 (return (translate-logical-pathname
1762 (translate-pathname pathname from to)))))))
1763 (pathname pathname)
1764 (t (translate-logical-pathname (pathname pathname)))))
1766 (defvar *logical-pathname-defaults*
1767 (%make-logical-pathname
1768 (make-logical-host :name (logical-word-or-lose "BOGUS"))
1769 :unspecific nil nil nil nil))
1771 (defun load-logical-pathname-translations (host)
1772 "Reads logical pathname translations from SYS:SITE;HOST.TRANSLATIONS.NEWEST,
1773 with HOST replaced by the supplied parameter. Returns T on success.
1775 If HOST is already defined as logical pathname host, no file is loaded and NIL
1776 is returned.
1778 The file should contain a single form, suitable for use with
1779 \(SETF LOGICAL-PATHNAME-TRANSLATIONS).
1781 Note: behaviour of this function is highly implementation dependent, and
1782 historically it used to be a no-op in SBCL -- the current approach is somewhat
1783 experimental and subject to change."
1784 (declare (type string host)
1785 (values (member t nil)))
1786 (if (find-logical-host host nil)
1787 ;; This host is already defined, all is well and good.
1789 ;; ANSI: "The specific nature of the search is
1790 ;; implementation-defined."
1791 (prog1 t
1792 (setf (logical-pathname-translations host)
1793 (with-open-file (lpt (make-pathname :host "SYS"
1794 :directory '(:absolute "SITE")
1795 :name host
1796 :type "TRANSLATIONS"
1797 :version :newest))
1798 (read lpt))))))
1800 (defun !pathname-cold-init ()
1801 (let* ((sys *default-pathname-defaults*)
1802 (src
1803 (merge-pathnames
1804 (make-pathname :directory '(:relative "src" :wild-inferiors)
1805 :name :wild :type :wild)
1806 sys))
1807 (contrib
1808 (merge-pathnames
1809 (make-pathname :directory '(:relative "contrib" :wild-inferiors)
1810 :name :wild :type :wild)
1811 sys))
1812 (output
1813 (merge-pathnames
1814 (make-pathname :directory '(:relative "output" :wild-inferiors)
1815 :name :wild :type :wild)
1816 sys)))
1817 (setf (logical-pathname-translations "SYS")
1818 `(("SYS:SRC;**;*.*.*" ,src)
1819 ("SYS:CONTRIB;**;*.*.*" ,contrib)
1820 ("SYS:OUTPUT;**;*.*.*" ,output)))))
1822 (defun set-sbcl-source-location (pathname)
1823 "Initialize the SYS logical host based on PATHNAME, which should be
1824 the top-level directory of the SBCL sources. This will replace any
1825 existing translations for \"SYS:SRC;\", \"SYS:CONTRIB;\", and
1826 \"SYS:OUTPUT;\". Other \"SYS:\" translations are preserved."
1827 (let ((truename (truename pathname))
1828 (current-translations
1829 (remove-if (lambda (translation)
1830 (or (pathname-match-p "SYS:SRC;" translation)
1831 (pathname-match-p "SYS:CONTRIB;" translation)
1832 (pathname-match-p "SYS:OUTPUT;" translation)))
1833 (logical-pathname-translations "SYS")
1834 :key #'first)))
1835 (flet ((physical-target (component)
1836 (merge-pathnames
1837 (make-pathname :directory (list :relative component
1838 :wild-inferiors)
1839 :name :wild
1840 :type :wild)
1841 truename)))
1842 (setf (logical-pathname-translations "SYS")
1843 `(("SYS:SRC;**;*.*.*" ,(physical-target "src"))
1844 ("SYS:CONTRIB;**;*.*.*" ,(physical-target "contrib"))
1845 ("SYS:OUTPUT;**;*.*.*" ,(physical-target "output"))
1846 ,@current-translations)))))