0.9.3.20:
[sbcl.git] / src / code / target-pathname.lisp
blob8c0f0fa12bbdb89bbdeef16b806cf6fa671b2b95
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 ;;;; UNIX-HOST stuff
18 (def!struct (unix-host
19 (:make-load-form-fun make-unix-host-load-form)
20 (:include host
21 (parse #'parse-unix-namestring)
22 (unparse #'unparse-unix-namestring)
23 (unparse-host #'unparse-unix-host)
24 (unparse-directory #'unparse-unix-directory)
25 (unparse-file #'unparse-unix-file)
26 (unparse-enough #'unparse-unix-enough)
27 (customary-case :lower))))
29 (defvar *unix-host* (make-unix-host))
31 (defun make-unix-host-load-form (host)
32 (declare (ignore host))
33 '*unix-host*)
35 ;;; Return a value suitable, e.g., for preinitializing
36 ;;; *DEFAULT-PATHNAME-DEFAULTS* before *DEFAULT-PATHNAME-DEFAULTS* is
37 ;;; initialized (at which time we can't safely call e.g. #'PATHNAME).
38 (defun make-trivial-default-pathname ()
39 (%make-pathname *unix-host* nil nil nil nil :newest))
41 ;;; pathname methods
43 (def!method print-object ((pathname pathname) stream)
44 (let ((namestring (handler-case (namestring pathname)
45 (error nil))))
46 (if namestring
47 (format stream
48 (if (or *print-readably* *print-escape*)
49 "#P~S"
50 "~A")
51 (coerce namestring '(simple-array character (*))))
52 (print-unreadable-object (pathname stream :type t)
53 (format stream
54 "~@<(with no namestring) ~_:HOST ~S ~_:DEVICE ~S ~_:DIRECTORY ~S ~
55 ~_:NAME ~S ~_:TYPE ~S ~_:VERSION ~S~:>"
56 (%pathname-host pathname)
57 (%pathname-device pathname)
58 (%pathname-directory pathname)
59 (%pathname-name pathname)
60 (%pathname-type pathname)
61 (%pathname-version pathname))))))
63 (def!method make-load-form ((pathname pathname) &optional environment)
64 (make-load-form-saving-slots pathname :environment environment))
66 ;;; A pathname is logical if the host component is a logical host.
67 ;;; This constructor is used to make an instance of the correct type
68 ;;; from parsed arguments.
69 (defun %make-maybe-logical-pathname (host device directory name type version)
70 ;; We canonicalize logical pathname components to uppercase. ANSI
71 ;; doesn't strictly require this, leaving it up to the implementor;
72 ;; but the arguments given in the X3J13 cleanup issue
73 ;; PATHNAME-LOGICAL:ADD seem compelling: we should canonicalize the
74 ;; case, and uppercase is the ordinary way to do that.
75 (flet ((upcase-maybe (x) (typecase x (string (logical-word-or-lose x)) (t x))))
76 (if (typep host 'logical-host)
77 (%make-logical-pathname host
78 :unspecific
79 (mapcar #'upcase-maybe directory)
80 (upcase-maybe name)
81 (upcase-maybe type)
82 version)
83 (progn
84 (aver (eq host *unix-host*))
85 (%make-pathname host device directory name type version)))))
87 ;;; Hash table searching maps a logical pathname's host to its
88 ;;; physical pathname translation.
89 (defvar *logical-hosts* (make-hash-table :test 'equal))
91 ;;;; patterns
93 (def!method make-load-form ((pattern pattern) &optional environment)
94 (make-load-form-saving-slots pattern :environment environment))
96 (def!method print-object ((pattern pattern) stream)
97 (print-unreadable-object (pattern stream :type t)
98 (if *print-pretty*
99 (let ((*print-escape* t))
100 (pprint-fill stream (pattern-pieces pattern) nil))
101 (prin1 (pattern-pieces pattern) stream))))
103 (defun pattern= (pattern1 pattern2)
104 (declare (type pattern pattern1 pattern2))
105 (let ((pieces1 (pattern-pieces pattern1))
106 (pieces2 (pattern-pieces pattern2)))
107 (and (= (length pieces1) (length pieces2))
108 (every (lambda (piece1 piece2)
109 (typecase piece1
110 (simple-string
111 (and (simple-string-p piece2)
112 (string= piece1 piece2)))
113 (cons
114 (and (consp piece2)
115 (eq (car piece1) (car piece2))
116 (string= (cdr piece1) (cdr piece2))))
118 (eq piece1 piece2))))
119 pieces1
120 pieces2))))
122 ;;; If the string matches the pattern returns the multiple values T
123 ;;; and a list of the matched strings.
124 (defun pattern-matches (pattern string)
125 (declare (type pattern pattern)
126 (type simple-string string))
127 (let ((len (length string)))
128 (labels ((maybe-prepend (subs cur-sub chars)
129 (if cur-sub
130 (let* ((len (length chars))
131 (new (make-string len))
132 (index len))
133 (dolist (char chars)
134 (setf (schar new (decf index)) char))
135 (cons new subs))
136 subs))
137 (matches (pieces start subs cur-sub chars)
138 (if (null pieces)
139 (if (= start len)
140 (values t (maybe-prepend subs cur-sub chars))
141 (values nil nil))
142 (let ((piece (car pieces)))
143 (etypecase piece
144 (simple-string
145 (let ((end (+ start (length piece))))
146 (and (<= end len)
147 (string= piece string
148 :start2 start :end2 end)
149 (matches (cdr pieces) end
150 (maybe-prepend subs cur-sub chars)
151 nil nil))))
152 (list
153 (ecase (car piece)
154 (:character-set
155 (and (< start len)
156 (let ((char (schar string start)))
157 (if (find char (cdr piece) :test #'char=)
158 (matches (cdr pieces) (1+ start) subs t
159 (cons char chars))))))))
160 ((member :single-char-wild)
161 (and (< start len)
162 (matches (cdr pieces) (1+ start) subs t
163 (cons (schar string start) chars))))
164 ((member :multi-char-wild)
165 (multiple-value-bind (won new-subs)
166 (matches (cdr pieces) start subs t chars)
167 (if won
168 (values t new-subs)
169 (and (< start len)
170 (matches pieces (1+ start) subs t
171 (cons (schar string start)
172 chars)))))))))))
173 (multiple-value-bind (won subs)
174 (matches (pattern-pieces pattern) 0 nil nil nil)
175 (values won (reverse subs))))))
177 ;;; PATHNAME-MATCH-P for directory components
178 (defun directory-components-match (thing wild)
179 (or (eq thing wild)
180 (eq wild :wild)
181 ;; If THING has a null directory, assume that it matches
182 ;; (:ABSOLUTE :WILD-INFERIORS) or (:RELATIVE :WILD-INFERIORS).
183 (and (consp wild)
184 (null thing)
185 (member (first wild) '(:absolute :relative))
186 (eq (second wild) :wild-inferiors))
187 (and (consp wild)
188 (let ((wild1 (first wild)))
189 (if (eq wild1 :wild-inferiors)
190 (let ((wild-subdirs (rest wild)))
191 (or (null wild-subdirs)
192 (loop
193 (when (directory-components-match thing wild-subdirs)
194 (return t))
195 (pop thing)
196 (unless thing (return nil)))))
197 (and (consp thing)
198 (components-match (first thing) wild1)
199 (directory-components-match (rest thing)
200 (rest wild))))))))
202 ;;; Return true if pathname component THING is matched by WILD. (not
203 ;;; commutative)
204 (defun components-match (thing wild)
205 (declare (type (or pattern symbol simple-string integer) thing wild))
206 (or (eq thing wild)
207 (eq wild :wild)
208 (typecase thing
209 (simple-string
210 ;; String is matched by itself, a matching pattern or :WILD.
211 (typecase wild
212 (pattern
213 (values (pattern-matches wild thing)))
214 (simple-string
215 (string= thing wild))))
216 (pattern
217 ;; A pattern is only matched by an identical pattern.
218 (and (pattern-p wild) (pattern= thing wild)))
219 (integer
220 ;; An integer (version number) is matched by :WILD or the
221 ;; same integer. This branch will actually always be NIL as
222 ;; long as the version is a fixnum.
223 (eql thing wild)))))
225 ;;; a predicate for comparing two pathname slot component sub-entries
226 (defun compare-component (this that)
227 (or (eql this that)
228 (typecase this
229 (simple-string
230 (and (simple-string-p that)
231 (string= this that)))
232 (pattern
233 (and (pattern-p that)
234 (pattern= this that)))
235 (cons
236 (and (consp that)
237 (compare-component (car this) (car that))
238 (compare-component (cdr this) (cdr that)))))))
240 ;;;; pathname functions
242 (defun pathname= (pathname1 pathname2)
243 (declare (type pathname pathname1)
244 (type pathname pathname2))
245 (and (eq (%pathname-host pathname1)
246 (%pathname-host pathname2))
247 (compare-component (%pathname-device pathname1)
248 (%pathname-device pathname2))
249 (compare-component (%pathname-directory pathname1)
250 (%pathname-directory pathname2))
251 (compare-component (%pathname-name pathname1)
252 (%pathname-name pathname2))
253 (compare-component (%pathname-type pathname1)
254 (%pathname-type pathname2))
255 (or (eq (%pathname-host pathname1) *unix-host*)
256 (compare-component (%pathname-version pathname1)
257 (%pathname-version pathname2)))))
259 ;;; Convert PATHNAME-DESIGNATOR (a pathname, or string, or
260 ;;; stream), into a pathname in pathname.
262 ;;; FIXME: was rewritten, should be tested (or rewritten again, this
263 ;;; time using ONCE-ONLY, *then* tested)
264 ;;; FIXME: become SB!XC:DEFMACRO inside EVAL-WHEN (COMPILE EVAL)?
265 (defmacro with-pathname ((pathname pathname-designator) &body body)
266 (let ((pd0 (gensym)))
267 `(let* ((,pd0 ,pathname-designator)
268 (,pathname (etypecase ,pd0
269 (pathname ,pd0)
270 (string (parse-namestring ,pd0))
271 (file-stream (file-name ,pd0)))))
272 ,@body)))
274 ;;; Convert the var, a host or string name for a host, into a
275 ;;; LOGICAL-HOST structure or nil if not defined.
277 ;;; pw notes 1/12/97 this potentially useful macro is not used anywhere
278 ;;; and 'find-host' is not defined. 'find-logical-host' seems to be needed.
280 (defmacro with-host ((var expr) &body body)
281 `(let ((,var (let ((,var ,expr))
282 (typecase ,var
283 (logical-host ,var)
284 (string (find-logical-host ,var nil))
285 (t nil)))))
286 ,@body))
289 (defun pathname (thing)
290 #!+sb-doc
291 "Convert thing (a pathname, string or stream) into a pathname."
292 (declare (type pathname-designator thing))
293 (with-pathname (pathname thing)
294 pathname))
296 ;;; Change the case of thing if DIDDLE-P.
297 (defun maybe-diddle-case (thing diddle-p)
298 (if (and diddle-p (not (or (symbolp thing) (integerp thing))))
299 (labels ((check-for (pred in)
300 (typecase in
301 (pattern
302 (dolist (piece (pattern-pieces in))
303 (when (typecase piece
304 (simple-string
305 (check-for pred piece))
306 (cons
307 (case (car piece)
308 (:character-set
309 (check-for pred (cdr piece))))))
310 (return t))))
311 (list
312 (dolist (x in)
313 (when (check-for pred x)
314 (return t))))
315 (simple-string
316 (dotimes (i (length in))
317 (when (funcall pred (schar in i))
318 (return t))))
319 (t nil)))
320 (diddle-with (fun thing)
321 (typecase thing
322 (pattern
323 (make-pattern
324 (mapcar (lambda (piece)
325 (typecase piece
326 (simple-string
327 (funcall fun piece))
328 (cons
329 (case (car piece)
330 (:character-set
331 (cons :character-set
332 (funcall fun (cdr piece))))
334 piece)))
336 piece)))
337 (pattern-pieces thing))))
338 (list
339 (mapcar fun thing))
340 (simple-string
341 (funcall fun thing))
343 thing))))
344 (let ((any-uppers (check-for #'upper-case-p thing))
345 (any-lowers (check-for #'lower-case-p thing)))
346 (cond ((and any-uppers any-lowers)
347 ;; mixed case, stays the same
348 thing)
349 (any-uppers
350 ;; all uppercase, becomes all lower case
351 (diddle-with (lambda (x) (if (stringp x)
352 (string-downcase x)
353 x)) thing))
354 (any-lowers
355 ;; all lowercase, becomes all upper case
356 (diddle-with (lambda (x) (if (stringp x)
357 (string-upcase x)
358 x)) thing))
360 ;; no letters? I guess just leave it.
361 thing))))
362 thing))
364 (defun merge-directories (dir1 dir2 diddle-case)
365 (if (or (eq (car dir1) :absolute)
366 (null dir2))
367 dir1
368 (let ((results nil))
369 (flet ((add (dir)
370 (if (and (eq dir :back)
371 results
372 (not (member (car results)
373 '(:back :wild-inferiors))))
374 (pop results)
375 (push dir results))))
376 (dolist (dir (maybe-diddle-case dir2 diddle-case))
377 (add dir))
378 (dolist (dir (cdr dir1))
379 (add dir)))
380 (reverse results))))
382 (defun merge-pathnames (pathname
383 &optional
384 (defaults *default-pathname-defaults*)
385 (default-version :newest))
386 #!+sb-doc
387 "Construct a filled in pathname by completing the unspecified components
388 from the defaults."
389 (declare (type pathname-designator pathname)
390 (type pathname-designator defaults)
391 (values pathname))
392 (with-pathname (defaults defaults)
393 (let ((pathname (let ((*default-pathname-defaults* defaults))
394 (pathname pathname))))
395 (let* ((default-host (%pathname-host defaults))
396 (pathname-host (%pathname-host pathname))
397 (diddle-case
398 (and default-host pathname-host
399 (not (eq (host-customary-case default-host)
400 (host-customary-case pathname-host))))))
401 (%make-maybe-logical-pathname
402 (or pathname-host default-host)
403 (or (%pathname-device pathname)
404 (maybe-diddle-case (%pathname-device defaults)
405 diddle-case))
406 (merge-directories (%pathname-directory pathname)
407 (%pathname-directory defaults)
408 diddle-case)
409 (or (%pathname-name pathname)
410 (maybe-diddle-case (%pathname-name defaults)
411 diddle-case))
412 (or (%pathname-type pathname)
413 (maybe-diddle-case (%pathname-type defaults)
414 diddle-case))
415 (or (%pathname-version pathname)
416 (and (not (%pathname-name pathname)) (%pathname-version defaults))
417 default-version))))))
419 (defun import-directory (directory diddle-case)
420 (etypecase directory
421 (null nil)
422 ((member :wild) '(:absolute :wild-inferiors))
423 ((member :unspecific) '(:relative))
424 (list
425 (collect ((results))
426 (results (pop directory))
427 (dolist (piece directory)
428 (cond ((member piece '(:wild :wild-inferiors :up :back))
429 (results piece))
430 ((or (simple-string-p piece) (pattern-p piece))
431 (results (maybe-diddle-case piece diddle-case)))
432 ((stringp piece)
433 (results (maybe-diddle-case (coerce piece 'simple-string)
434 diddle-case)))
436 (error "~S is not allowed as a directory component." piece))))
437 (results)))
438 (simple-string
439 `(:absolute
440 ,(maybe-diddle-case directory diddle-case)))
441 (string
442 `(:absolute
443 ,(maybe-diddle-case (coerce directory 'simple-string)
444 diddle-case)))))
446 (defun make-pathname (&key host
447 (device nil devp)
448 (directory nil dirp)
449 (name nil namep)
450 (type nil typep)
451 (version nil versionp)
452 defaults
453 (case :local))
454 #!+sb-doc
455 "Makes a new pathname from the component arguments. Note that host is
456 a host-structure or string."
457 (declare (type (or string host pathname-component-tokens) host)
458 (type (or string pathname-component-tokens) device)
459 (type (or list string pattern pathname-component-tokens) directory)
460 (type (or string pattern pathname-component-tokens) name type)
461 (type (or integer pathname-component-tokens (member :newest))
462 version)
463 (type (or pathname-designator null) defaults)
464 (type (member :common :local) case))
465 (let* ((defaults (when defaults
466 (with-pathname (defaults defaults) defaults)))
467 (default-host (if defaults
468 (%pathname-host defaults)
469 (pathname-host *default-pathname-defaults*)))
470 ;; Raymond Toy writes: CLHS says make-pathname can take a
471 ;; string (as a logical-host) for the host part. We map that
472 ;; string into the corresponding logical host structure.
474 ;; Paul Werkowski writes:
475 ;; HyperSpec says for the arg to MAKE-PATHNAME;
476 ;; "host---a valid physical pathname host. ..."
477 ;; where it probably means -- a valid pathname host.
478 ;; "valid pathname host n. a valid physical pathname host or
479 ;; a valid logical pathname host."
480 ;; and defines
481 ;; "valid physical pathname host n. any of a string,
482 ;; a list of strings, or the symbol :unspecific,
483 ;; that is recognized by the implementation as the name of a host."
484 ;; "valid logical pathname host n. a string that has been defined
485 ;; as the name of a logical host. ..."
486 ;; HS is silent on what happens if the :HOST arg is NOT one of these.
487 ;; It seems an error message is appropriate.
488 (host (typecase host
489 (host host) ; A valid host, use it.
490 ((string 0) *unix-host*) ; "" cannot be a logical host
491 (string (find-logical-host host t)) ; logical-host or lose.
492 (t default-host))) ; unix-host
493 (diddle-args (and (eq (host-customary-case host) :lower)
494 (eq case :common)))
495 (diddle-defaults
496 (not (eq (host-customary-case host)
497 (host-customary-case default-host))))
498 (dev (if devp device (if defaults (%pathname-device defaults))))
499 (dir (import-directory directory diddle-args))
500 (ver (cond
501 (versionp version)
502 (defaults (%pathname-version defaults))
503 (t nil))))
504 (when (and defaults (not dirp))
505 (setf dir
506 (merge-directories dir
507 (%pathname-directory defaults)
508 diddle-defaults)))
510 (macrolet ((pick (var varp field)
511 `(cond ((or (simple-string-p ,var)
512 (pattern-p ,var))
513 (maybe-diddle-case ,var diddle-args))
514 ((stringp ,var)
515 (maybe-diddle-case (coerce ,var 'simple-string)
516 diddle-args))
517 (,varp
518 (maybe-diddle-case ,var diddle-args))
519 (defaults
520 (maybe-diddle-case (,field defaults)
521 diddle-defaults))
523 nil))))
524 (%make-maybe-logical-pathname host
525 dev ; forced to :UNSPECIFIC when logical
527 (pick name namep %pathname-name)
528 (pick type typep %pathname-type)
529 ver))))
531 (defun pathname-host (pathname &key (case :local))
532 #!+sb-doc
533 "Return PATHNAME's host."
534 (declare (type pathname-designator pathname)
535 (type (member :local :common) case)
536 (values host)
537 (ignore case))
538 (with-pathname (pathname pathname)
539 (%pathname-host pathname)))
541 (defun pathname-device (pathname &key (case :local))
542 #!+sb-doc
543 "Return PATHNAME's device."
544 (declare (type pathname-designator pathname)
545 (type (member :local :common) case))
546 (with-pathname (pathname pathname)
547 (maybe-diddle-case (%pathname-device pathname)
548 (and (eq case :common)
549 (eq (host-customary-case
550 (%pathname-host pathname))
551 :lower)))))
553 (defun pathname-directory (pathname &key (case :local))
554 #!+sb-doc
555 "Return PATHNAME's directory."
556 (declare (type pathname-designator pathname)
557 (type (member :local :common) case))
558 (with-pathname (pathname pathname)
559 (maybe-diddle-case (%pathname-directory pathname)
560 (and (eq case :common)
561 (eq (host-customary-case
562 (%pathname-host pathname))
563 :lower)))))
564 (defun pathname-name (pathname &key (case :local))
565 #!+sb-doc
566 "Return PATHNAME's name."
567 (declare (type pathname-designator pathname)
568 (type (member :local :common) case))
569 (with-pathname (pathname pathname)
570 (maybe-diddle-case (%pathname-name pathname)
571 (and (eq case :common)
572 (eq (host-customary-case
573 (%pathname-host pathname))
574 :lower)))))
576 (defun pathname-type (pathname &key (case :local))
577 #!+sb-doc
578 "Return PATHNAME's type."
579 (declare (type pathname-designator pathname)
580 (type (member :local :common) case))
581 (with-pathname (pathname pathname)
582 (maybe-diddle-case (%pathname-type pathname)
583 (and (eq case :common)
584 (eq (host-customary-case
585 (%pathname-host pathname))
586 :lower)))))
588 (defun pathname-version (pathname)
589 #!+sb-doc
590 "Return PATHNAME's version."
591 (declare (type pathname-designator pathname))
592 (with-pathname (pathname pathname)
593 (%pathname-version pathname)))
595 ;;;; namestrings
597 ;;; Handle the case for PARSE-NAMESTRING parsing a potentially
598 ;;; syntactically valid logical namestring with an explicit host.
600 ;;; This then isn't fully general -- we are relying on the fact that
601 ;;; we will only pass to parse-namestring namestring with an explicit
602 ;;; logical host, so that we can pass the host return from
603 ;;; parse-logical-namestring through to %PARSE-NAMESTRING as a truth
604 ;;; value. Yeah, this is probably a KLUDGE - CSR, 2002-04-18
605 (defun parseable-logical-namestring-p (namestr start end)
606 (catch 'exit
607 (handler-bind
608 ((namestring-parse-error (lambda (c)
609 (declare (ignore c))
610 (throw 'exit nil))))
611 (let ((colon (position #\: namestr :start start :end end)))
612 (when colon
613 (let ((potential-host
614 (logical-word-or-lose (subseq namestr start colon))))
615 ;; depending on the outcome of CSR comp.lang.lisp post
616 ;; "can PARSE-NAMESTRING create logical hosts", we may need
617 ;; to do things with potential-host (create it
618 ;; temporarily, parse the namestring and unintern the
619 ;; logical host potential-host on failure.
620 (declare (ignore potential-host))
621 (let ((result
622 (handler-bind
623 ((simple-type-error (lambda (c)
624 (declare (ignore c))
625 (throw 'exit nil))))
626 (parse-logical-namestring namestr start end))))
627 ;; if we got this far, we should have an explicit host
628 ;; (first return value of parse-logical-namestring)
629 (aver result)
630 result)))))))
632 ;;; Handle the case where PARSE-NAMESTRING is actually parsing a
633 ;;; namestring. We pick off the :JUNK-ALLOWED case then find a host to
634 ;;; use for parsing, call the parser, then check whether the host matches.
635 (defun %parse-namestring (namestr host defaults start end junk-allowed)
636 (declare (type (or host null) host)
637 (type string namestr)
638 (type index start)
639 (type (or index null) end))
640 (cond
641 (junk-allowed
642 (handler-case
643 (%parse-namestring namestr host defaults start end nil)
644 (namestring-parse-error (condition)
645 (values nil (namestring-parse-error-offset condition)))))
647 (let* ((end (%check-vector-sequence-bounds namestr start end)))
648 (multiple-value-bind (new-host device directory file type version)
649 ;; Comments below are quotes from the HyperSpec
650 ;; PARSE-NAMESTRING entry, reproduced here to demonstrate
651 ;; that we actually have to do things this way rather than
652 ;; some possibly more logical way. - CSR, 2002-04-18
653 (cond
654 ;; "If host is a logical host then thing is parsed as a
655 ;; logical pathname namestring on the host."
656 (host (funcall (host-parse host) namestr start end))
657 ;; "If host is nil and thing is a syntactically valid
658 ;; logical pathname namestring containing an explicit
659 ;; host, then it is parsed as a logical pathname
660 ;; namestring."
661 ((parseable-logical-namestring-p namestr start end)
662 (parse-logical-namestring namestr start end))
663 ;; "If host is nil, default-pathname is a logical
664 ;; pathname, and thing is a syntactically valid logical
665 ;; pathname namestring without an explicit host, then it
666 ;; is parsed as a logical pathname namestring on the
667 ;; host that is the host component of default-pathname."
669 ;; "Otherwise, the parsing of thing is
670 ;; implementation-defined."
672 ;; Both clauses are handled here, as the default
673 ;; *DEFAULT-PATHNAME-DEFAULTS has a SB-IMPL::UNIX-HOST
674 ;; for a host.
675 ((pathname-host defaults)
676 (funcall (host-parse (pathname-host defaults))
677 namestr
678 start
679 end))
680 ;; I don't think we should ever get here, as the default
681 ;; host will always have a non-null HOST, given that we
682 ;; can't create a new pathname without going through
683 ;; *DEFAULT-PATHNAME-DEFAULTS*, which has a non-null
684 ;; host...
685 (t (bug "Fallen through COND in %PARSE-NAMESTRING")))
686 (when (and host new-host (not (eq new-host host)))
687 (error 'simple-type-error
688 :datum new-host
689 ;; Note: ANSI requires that this be a TYPE-ERROR,
690 ;; but there seems to be no completely correct
691 ;; value to use for TYPE-ERROR-EXPECTED-TYPE.
692 ;; Instead, we return a sort of "type error allowed
693 ;; type", trying to say "it would be OK if you
694 ;; passed NIL as the host value" but not mentioning
695 ;; that a matching string would be OK too.
696 :expected-type 'null
697 :format-control
698 "The host in the namestring, ~S,~@
699 does not match the explicit HOST argument, ~S."
700 :format-arguments (list new-host host)))
701 (let ((pn-host (or new-host host (pathname-host defaults))))
702 (values (%make-maybe-logical-pathname
703 pn-host device directory file type version)
704 end)))))))
706 ;;; If NAMESTR begins with a colon-terminated, defined, logical host,
707 ;;; then return that host, otherwise return NIL.
708 (defun extract-logical-host-prefix (namestr start end)
709 (declare (type simple-string namestr)
710 (type index start end)
711 (values (or logical-host null)))
712 (let ((colon-pos (position #\: namestr :start start :end end)))
713 (if colon-pos
714 (values (gethash (nstring-upcase (subseq namestr start colon-pos))
715 *logical-hosts*))
716 nil)))
718 (defun parse-namestring (thing
719 &optional
720 host
721 (defaults *default-pathname-defaults*)
722 &key (start 0) end junk-allowed)
723 (declare (type pathname-designator thing defaults)
724 (type (or list host string (member :unspecific)) host)
725 (type index start)
726 (type (or index null) end)
727 (type (or t null) junk-allowed)
728 (values (or null pathname) (or null index)))
729 ;; Generally, redundant specification of information in software,
730 ;; whether in code or in comments, is bad. However, the ANSI spec
731 ;; for this is messy enough that it's hard to hold in short-term
732 ;; memory, so I've recorded these redundant notes on the
733 ;; implications of the ANSI spec.
735 ;; According to the ANSI spec, HOST can be a valid pathname host, or
736 ;; a logical host, or NIL.
738 ;; A valid pathname host can be a valid physical pathname host or a
739 ;; valid logical pathname host.
741 ;; A valid physical pathname host is "any of a string, a list of
742 ;; strings, or the symbol :UNSPECIFIC, that is recognized by the
743 ;; implementation as the name of a host". In SBCL as of 0.6.9.8,
744 ;; that means :UNSPECIFIC: though someday we might want to
745 ;; generalize it to allow strings like "RTFM.MIT.EDU" or lists like
746 ;; '("RTFM" "MIT" "EDU"), that's not supported now.
748 ;; A valid logical pathname host is a string which has been defined as
749 ;; the name of a logical host, as with LOAD-LOGICAL-PATHNAME-TRANSLATIONS.
751 ;; A logical host is an object of implementation-dependent nature. In
752 ;; SBCL, it's a member of the HOST class (a subclass of STRUCTURE-OBJECT).
753 (let ((found-host (etypecase host
754 ((string 0)
755 ;; This is a special host. It's not valid as a
756 ;; logical host, so it is a sensible thing to
757 ;; designate the physical Unix host object. So
758 ;; we do that.
759 *unix-host*)
760 (string
761 ;; In general ANSI-compliant Common Lisps, a
762 ;; string might also be a physical pathname host,
763 ;; but ANSI leaves this up to the implementor,
764 ;; and in SBCL we don't do it, so it must be a
765 ;; logical host.
766 (find-logical-host host))
767 ((or null (member :unspecific))
768 ;; CLHS says that HOST=:UNSPECIFIC has
769 ;; implementation-defined behavior. We
770 ;; just turn it into NIL.
771 nil)
772 (list
773 ;; ANSI also allows LISTs to designate hosts,
774 ;; but leaves its interpretation
775 ;; implementation-defined. Our interpretation
776 ;; is that it's unsupported.:-|
777 (error "A LIST representing a pathname host is not ~
778 supported in this implementation:~% ~S"
779 host))
780 (host
781 host)))
782 ;; According to ANSI defaults may be any valid pathname designator
783 (defaults (etypecase defaults
784 (pathname
785 defaults)
786 (string
787 (aver (pathnamep *default-pathname-defaults*))
788 (parse-namestring defaults))
789 (stream
790 (truename defaults)))))
791 (declare (type (or null host) found-host)
792 (type pathname defaults))
793 (etypecase thing
794 (simple-string
795 (%parse-namestring thing found-host defaults start end junk-allowed))
796 (string
797 (%parse-namestring (coerce thing 'simple-string)
798 found-host defaults start end junk-allowed))
799 (pathname
800 (let ((defaulted-host (or found-host (%pathname-host defaults))))
801 (declare (type host defaulted-host))
802 (unless (eq defaulted-host (%pathname-host thing))
803 (error "The HOST argument doesn't match the pathname host:~% ~
804 ~S and ~S."
805 defaulted-host (%pathname-host thing))))
806 (values thing start))
807 (stream
808 (let ((name (file-name thing)))
809 (unless name
810 (error "can't figure out the file associated with stream:~% ~S"
811 thing))
812 (values name nil))))))
814 (defun namestring (pathname)
815 #!+sb-doc
816 "Construct the full (name)string form of the pathname."
817 (declare (type pathname-designator pathname))
818 (with-pathname (pathname pathname)
819 (when pathname
820 (let ((host (%pathname-host pathname)))
821 (unless host
822 (error "can't determine the namestring for pathnames with no ~
823 host:~% ~S" pathname))
824 (funcall (host-unparse host) pathname)))))
826 (defun host-namestring (pathname)
827 #!+sb-doc
828 "Return a string representation of the name of the host in the pathname."
829 (declare (type pathname-designator pathname))
830 (with-pathname (pathname pathname)
831 (let ((host (%pathname-host pathname)))
832 (if host
833 (funcall (host-unparse-host host) pathname)
834 (error
835 "can't determine the namestring for pathnames with no host:~% ~S"
836 pathname)))))
838 (defun directory-namestring (pathname)
839 #!+sb-doc
840 "Return a string representation of the directories used in the pathname."
841 (declare (type pathname-designator pathname))
842 (with-pathname (pathname pathname)
843 (let ((host (%pathname-host pathname)))
844 (if host
845 (funcall (host-unparse-directory host) pathname)
846 (error
847 "can't determine the namestring for pathnames with no host:~% ~S"
848 pathname)))))
850 (defun file-namestring (pathname)
851 #!+sb-doc
852 "Return a string representation of the name used in the pathname."
853 (declare (type pathname-designator pathname))
854 (with-pathname (pathname pathname)
855 (let ((host (%pathname-host pathname)))
856 (if host
857 (funcall (host-unparse-file host) pathname)
858 (error
859 "can't determine the namestring for pathnames with no host:~% ~S"
860 pathname)))))
862 (defun enough-namestring (pathname
863 &optional
864 (defaults *default-pathname-defaults*))
865 #!+sb-doc
866 "Return an abbreviated pathname sufficent to identify the pathname relative
867 to the defaults."
868 (declare (type pathname-designator pathname))
869 (with-pathname (pathname pathname)
870 (let ((host (%pathname-host pathname)))
871 (if host
872 (with-pathname (defaults defaults)
873 (funcall (host-unparse-enough host) pathname defaults))
874 (error
875 "can't determine the namestring for pathnames with no host:~% ~S"
876 pathname)))))
878 ;;;; wild pathnames
880 (defun wild-pathname-p (pathname &optional field-key)
881 #!+sb-doc
882 "Predicate for determining whether pathname contains any wildcards."
883 (declare (type pathname-designator pathname)
884 (type (member nil :host :device :directory :name :type :version)
885 field-key))
886 (with-pathname (pathname pathname)
887 (flet ((frob (x)
888 (or (pattern-p x) (member x '(:wild :wild-inferiors)))))
889 (ecase field-key
890 ((nil)
891 (or (wild-pathname-p pathname :host)
892 (wild-pathname-p pathname :device)
893 (wild-pathname-p pathname :directory)
894 (wild-pathname-p pathname :name)
895 (wild-pathname-p pathname :type)
896 (wild-pathname-p pathname :version)))
897 (:host (frob (%pathname-host pathname)))
898 (:device (frob (%pathname-host pathname)))
899 (:directory (some #'frob (%pathname-directory pathname)))
900 (:name (frob (%pathname-name pathname)))
901 (:type (frob (%pathname-type pathname)))
902 (:version (frob (%pathname-version pathname)))))))
904 (defun pathname-match-p (in-pathname in-wildname)
905 #!+sb-doc
906 "Pathname matches the wildname template?"
907 (declare (type pathname-designator in-pathname))
908 (with-pathname (pathname in-pathname)
909 (with-pathname (wildname in-wildname)
910 (macrolet ((frob (field &optional (op 'components-match))
911 `(or (null (,field wildname))
912 (,op (,field pathname) (,field wildname)))))
913 (and (or (null (%pathname-host wildname))
914 (eq (%pathname-host wildname) (%pathname-host pathname)))
915 (frob %pathname-device)
916 (frob %pathname-directory directory-components-match)
917 (frob %pathname-name)
918 (frob %pathname-type)
919 (or (eq (%pathname-host wildname) *unix-host*)
920 (frob %pathname-version)))))))
922 ;;; Place the substitutions into the pattern and return the string or pattern
923 ;;; that results. If DIDDLE-CASE is true, we diddle the result case as well,
924 ;;; in case we are translating between hosts with difference conventional case.
925 ;;; The second value is the tail of subs with all of the values that we used up
926 ;;; stripped off. Note that PATTERN-MATCHES matches all consecutive wildcards
927 ;;; as a single string, so we ignore subsequent contiguous wildcards.
928 (defun substitute-into (pattern subs diddle-case)
929 (declare (type pattern pattern)
930 (type list subs)
931 (values (or simple-string pattern) list))
932 (let ((in-wildcard nil)
933 (pieces nil)
934 (strings nil))
935 (dolist (piece (pattern-pieces pattern))
936 (cond ((simple-string-p piece)
937 (push piece strings)
938 (setf in-wildcard nil))
939 (in-wildcard)
941 (setf in-wildcard t)
942 (unless subs
943 (error "not enough wildcards in FROM pattern to match ~
944 TO pattern:~% ~S"
945 pattern))
946 (let ((sub (pop subs)))
947 (typecase sub
948 (pattern
949 (when strings
950 (push (apply #'concatenate 'simple-string
951 (nreverse strings))
952 pieces))
953 (dolist (piece (pattern-pieces sub))
954 (push piece pieces)))
955 (simple-string
956 (push sub strings))
958 (error "can't substitute this into the middle of a word:~
959 ~% ~S"
960 sub)))))))
962 (when strings
963 (push (apply #'concatenate 'simple-string (nreverse strings))
964 pieces))
965 (values
966 (maybe-diddle-case
967 (if (and pieces (simple-string-p (car pieces)) (null (cdr pieces)))
968 (car pieces)
969 (make-pattern (nreverse pieces)))
970 diddle-case)
971 subs)))
973 ;;; Called when we can't see how source and from matched.
974 (defun didnt-match-error (source from)
975 (error "Pathname components from SOURCE and FROM args to TRANSLATE-PATHNAME~@
976 did not match:~% ~S ~S"
977 source from))
979 ;;; Do TRANSLATE-COMPONENT for all components except host, directory
980 ;;; and version.
981 (defun translate-component (source from to diddle-case)
982 (typecase to
983 (pattern
984 (typecase from
985 (pattern
986 (typecase source
987 (pattern
988 (if (pattern= from source)
989 source
990 (didnt-match-error source from)))
991 (simple-string
992 (multiple-value-bind (won subs) (pattern-matches from source)
993 (if won
994 (values (substitute-into to subs diddle-case))
995 (didnt-match-error source from))))
997 (maybe-diddle-case source diddle-case))))
998 ((member :wild)
999 (values (substitute-into to (list source) diddle-case)))
1001 (if (components-match source from)
1002 (maybe-diddle-case source diddle-case)
1003 (didnt-match-error source from)))))
1004 ((member nil :wild)
1005 (maybe-diddle-case source diddle-case))
1007 (if (components-match source from)
1009 (didnt-match-error source from)))))
1011 ;;; Return a list of all the things that we want to substitute into the TO
1012 ;;; pattern (the things matched by from on source.) When From contains
1013 ;;; :WILD-INFERIORS, the result contains a sublist of the matched source
1014 ;;; subdirectories.
1015 (defun compute-directory-substitutions (orig-source orig-from)
1016 (let ((source orig-source)
1017 (from orig-from))
1018 (collect ((subs))
1019 (loop
1020 (unless source
1021 (unless (every (lambda (x) (eq x :wild-inferiors)) from)
1022 (didnt-match-error orig-source orig-from))
1023 (subs ())
1024 (return))
1025 (unless from (didnt-match-error orig-source orig-from))
1026 (let ((from-part (pop from))
1027 (source-part (pop source)))
1028 (typecase from-part
1029 (pattern
1030 (typecase source-part
1031 (pattern
1032 (if (pattern= from-part source-part)
1033 (subs source-part)
1034 (didnt-match-error orig-source orig-from)))
1035 (simple-string
1036 (multiple-value-bind (won new-subs)
1037 (pattern-matches from-part source-part)
1038 (if won
1039 (dolist (sub new-subs)
1040 (subs sub))
1041 (didnt-match-error orig-source orig-from))))
1043 (didnt-match-error orig-source orig-from))))
1044 ((member :wild)
1045 (subs source-part))
1046 ((member :wild-inferiors)
1047 (let ((remaining-source (cons source-part source)))
1048 (collect ((res))
1049 (loop
1050 (when (directory-components-match remaining-source from)
1051 (return))
1052 (unless remaining-source
1053 (didnt-match-error orig-source orig-from))
1054 (res (pop remaining-source)))
1055 (subs (res))
1056 (setq source remaining-source))))
1057 (simple-string
1058 (unless (and (simple-string-p source-part)
1059 (string= from-part source-part))
1060 (didnt-match-error orig-source orig-from)))
1062 (didnt-match-error orig-source orig-from)))))
1063 (subs))))
1065 ;;; This is called by TRANSLATE-PATHNAME on the directory components
1066 ;;; of its argument pathnames to produce the result directory
1067 ;;; component. If this leaves the directory NIL, we return the source
1068 ;;; directory. The :RELATIVE or :ABSOLUTE is taken from the source
1069 ;;; directory, except if TO is :ABSOLUTE, in which case the result
1070 ;;; will be :ABSOLUTE.
1071 (defun translate-directories (source from to diddle-case)
1072 (if (not (and source to from))
1073 (or (and to (null source) (remove :wild-inferiors to))
1074 (mapcar (lambda (x) (maybe-diddle-case x diddle-case)) source))
1075 (collect ((res))
1076 ;; If TO is :ABSOLUTE, the result should still be :ABSOLUTE.
1077 (res (if (eq (first to) :absolute)
1078 :absolute
1079 (first source)))
1080 (let ((subs-left (compute-directory-substitutions (rest source)
1081 (rest from))))
1082 (dolist (to-part (rest to))
1083 (typecase to-part
1084 ((member :wild)
1085 (aver subs-left)
1086 (let ((match (pop subs-left)))
1087 (when (listp match)
1088 (error ":WILD-INFERIORS is not paired in from and to ~
1089 patterns:~% ~S ~S" from to))
1090 (res (maybe-diddle-case match diddle-case))))
1091 ((member :wild-inferiors)
1092 (aver subs-left)
1093 (let ((match (pop subs-left)))
1094 (unless (listp match)
1095 (error ":WILD-INFERIORS not paired in from and to ~
1096 patterns:~% ~S ~S" from to))
1097 (dolist (x match)
1098 (res (maybe-diddle-case x diddle-case)))))
1099 (pattern
1100 (multiple-value-bind
1101 (new new-subs-left)
1102 (substitute-into to-part subs-left diddle-case)
1103 (setf subs-left new-subs-left)
1104 (res new)))
1105 (t (res to-part)))))
1106 (res))))
1108 (defun translate-pathname (source from-wildname to-wildname &key)
1109 #!+sb-doc
1110 "Use the source pathname to translate the from-wildname's wild and
1111 unspecified elements into a completed to-pathname based on the to-wildname."
1112 (declare (type pathname-designator source from-wildname to-wildname))
1113 (with-pathname (source source)
1114 (with-pathname (from from-wildname)
1115 (with-pathname (to to-wildname)
1116 (let* ((source-host (%pathname-host source))
1117 (from-host (%pathname-host from))
1118 (to-host (%pathname-host to))
1119 (diddle-case
1120 (and source-host to-host
1121 (not (eq (host-customary-case source-host)
1122 (host-customary-case to-host))))))
1123 (macrolet ((frob (field &optional (op 'translate-component))
1124 `(let ((result (,op (,field source)
1125 (,field from)
1126 (,field to)
1127 diddle-case)))
1128 (if (eq result :error)
1129 (error "~S doesn't match ~S." source from)
1130 result))))
1131 (%make-maybe-logical-pathname
1132 (or to-host source-host)
1133 (frob %pathname-device)
1134 (frob %pathname-directory translate-directories)
1135 (frob %pathname-name)
1136 (frob %pathname-type)
1137 (if (eq from-host *unix-host*)
1138 (if (eq (%pathname-version to) :wild)
1139 (%pathname-version from)
1140 (%pathname-version to))
1141 (frob %pathname-version)))))))))
1143 ;;;; logical pathname support. ANSI 92-102 specification.
1144 ;;;;
1145 ;;;; As logical-pathname translations are loaded they are
1146 ;;;; canonicalized as patterns to enable rapid efficient translation
1147 ;;;; into physical pathnames.
1149 ;;;; utilities
1151 ;;; Canonicalize a logical pathname word by uppercasing it checking that it
1152 ;;; contains only legal characters.
1153 (defun logical-word-or-lose (word)
1154 (declare (string word))
1155 (when (string= word "")
1156 (error 'namestring-parse-error
1157 :complaint "Attempted to treat invalid logical hostname ~
1158 as a logical host:~% ~S"
1159 :args (list word)
1160 :namestring word :offset 0))
1161 (let ((word (string-upcase word)))
1162 (dotimes (i (length word))
1163 (let ((ch (schar word i)))
1164 (unless (and (typep ch 'standard-char)
1165 (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 :args (list ch)
1170 :namestring word :offset i))))
1171 (coerce word 'base-string)))
1173 ;;; Given a logical host or string, return a logical host. If ERROR-P
1174 ;;; is NIL, then return NIL when no such host exists.
1175 (defun find-logical-host (thing &optional (errorp t))
1176 (etypecase thing
1177 (string
1178 (let ((found (gethash (logical-word-or-lose thing)
1179 *logical-hosts*)))
1180 (if (or found (not errorp))
1181 found
1182 ;; This is the error signalled from e.g.
1183 ;; LOGICAL-PATHNAME-TRANSLATIONS when host is not a defined
1184 ;; host, and ANSI specifies that that's a TYPE-ERROR.
1185 (error 'simple-type-error
1186 :datum thing
1187 ;; God only knows what ANSI expects us to use for
1188 ;; the EXPECTED-TYPE here. Maybe this will be OK..
1189 :expected-type
1190 '(and string (satisfies logical-pathname-translations))
1191 :format-control "logical host not yet defined: ~S"
1192 :format-arguments (list thing)))))
1193 (logical-host thing)))
1195 ;;; Given a logical host name or host, return a logical host, creating
1196 ;;; a new one if necessary.
1197 (defun intern-logical-host (thing)
1198 (declare (values logical-host))
1199 (or (find-logical-host thing nil)
1200 (let* ((name (logical-word-or-lose thing))
1201 (new (make-logical-host :name name)))
1202 (setf (gethash name *logical-hosts*) new)
1203 new)))
1205 ;;;; logical pathname parsing
1207 ;;; Deal with multi-char wildcards in a logical pathname token.
1208 (defun maybe-make-logical-pattern (namestring chunks)
1209 (let ((chunk (caar chunks)))
1210 (collect ((pattern))
1211 (let ((last-pos 0)
1212 (len (length chunk)))
1213 (declare (fixnum last-pos))
1214 (loop
1215 (when (= last-pos len) (return))
1216 (let ((pos (or (position #\* chunk :start last-pos) len)))
1217 (if (= pos last-pos)
1218 (when (pattern)
1219 (error 'namestring-parse-error
1220 :complaint "double asterisk inside of logical ~
1221 word: ~S"
1222 :args (list chunk)
1223 :namestring namestring
1224 :offset (+ (cdar chunks) pos)))
1225 (pattern (subseq chunk last-pos pos)))
1226 (if (= pos len)
1227 (return)
1228 (pattern :multi-char-wild))
1229 (setq last-pos (1+ pos)))))
1230 (aver (pattern))
1231 (if (cdr (pattern))
1232 (make-pattern (pattern))
1233 (let ((x (car (pattern))))
1234 (if (eq x :multi-char-wild)
1235 :wild
1236 x))))))
1238 ;;; Return a list of conses where the CDR is the start position and
1239 ;;; the CAR is a string (token) or character (punctuation.)
1240 (defun logical-chunkify (namestr start end)
1241 (collect ((chunks))
1242 (do ((i start (1+ i))
1243 (prev 0))
1244 ((= i end)
1245 (when (> end prev)
1246 (chunks (cons (nstring-upcase (subseq namestr prev end)) prev))))
1247 (let ((ch (schar namestr i)))
1248 (unless (or (alpha-char-p ch) (digit-char-p ch)
1249 (member ch '(#\- #\*)))
1250 (when (> i prev)
1251 (chunks (cons (nstring-upcase (subseq namestr prev i)) prev)))
1252 (setq prev (1+ i))
1253 (unless (member ch '(#\; #\: #\.))
1254 (error 'namestring-parse-error
1255 :complaint "illegal character for logical pathname:~% ~S"
1256 :args (list ch)
1257 :namestring namestr
1258 :offset i))
1259 (chunks (cons ch i)))))
1260 (chunks)))
1262 ;;; Break up a logical-namestring, always a string, into its
1263 ;;; constituent parts.
1264 (defun parse-logical-namestring (namestr start end)
1265 (declare (type simple-string namestr)
1266 (type index start end))
1267 (collect ((directory))
1268 (let ((host nil)
1269 (name nil)
1270 (type nil)
1271 (version nil))
1272 (labels ((expecting (what chunks)
1273 (unless (and chunks (simple-string-p (caar chunks)))
1274 (error 'namestring-parse-error
1275 :complaint "expecting ~A, got ~:[nothing~;~S~]."
1276 :args (list what (caar chunks) (caar chunks))
1277 :namestring namestr
1278 :offset (if chunks (cdar chunks) end)))
1279 (caar chunks))
1280 (parse-host (chunks)
1281 (case (caadr chunks)
1282 (#\:
1283 (setq host
1284 (find-logical-host (expecting "a host name" chunks)))
1285 (parse-relative (cddr chunks)))
1287 (parse-relative chunks))))
1288 (parse-relative (chunks)
1289 (case (caar chunks)
1290 (#\;
1291 (directory :relative)
1292 (parse-directory (cdr chunks)))
1294 (directory :absolute) ; Assumption! Maybe revoked later.
1295 (parse-directory chunks))))
1296 (parse-directory (chunks)
1297 (case (caadr chunks)
1298 (#\;
1299 (directory
1300 (let ((res (expecting "a directory name" chunks)))
1301 (cond ((string= res "..") :up)
1302 ((string= res "**") :wild-inferiors)
1304 (maybe-make-logical-pattern namestr chunks)))))
1305 (parse-directory (cddr chunks)))
1307 (parse-name chunks))))
1308 (parse-name (chunks)
1309 (when chunks
1310 (expecting "a file name" chunks)
1311 (setq name (maybe-make-logical-pattern namestr chunks))
1312 (expecting-dot (cdr chunks))))
1313 (expecting-dot (chunks)
1314 (when chunks
1315 (unless (eql (caar chunks) #\.)
1316 (error 'namestring-parse-error
1317 :complaint "expecting a dot, got ~S."
1318 :args (list (caar chunks))
1319 :namestring namestr
1320 :offset (cdar chunks)))
1321 (if type
1322 (parse-version (cdr chunks))
1323 (parse-type (cdr chunks)))))
1324 (parse-type (chunks)
1325 (expecting "a file type" chunks)
1326 (setq type (maybe-make-logical-pattern namestr chunks))
1327 (expecting-dot (cdr chunks)))
1328 (parse-version (chunks)
1329 (let ((str (expecting "a positive integer, * or NEWEST"
1330 chunks)))
1331 (cond
1332 ((string= str "*") (setq version :wild))
1333 ((string= str "NEWEST") (setq version :newest))
1335 (multiple-value-bind (res pos)
1336 (parse-integer str :junk-allowed t)
1337 (unless (and res (plusp res))
1338 (error 'namestring-parse-error
1339 :complaint "expected a positive integer, ~
1340 got ~S"
1341 :args (list str)
1342 :namestring namestr
1343 :offset (+ pos (cdar chunks))))
1344 (setq version res)))))
1345 (when (cdr chunks)
1346 (error 'namestring-parse-error
1347 :complaint "extra stuff after end of file name"
1348 :namestring namestr
1349 :offset (cdadr chunks)))))
1350 (parse-host (logical-chunkify namestr start end)))
1351 (values host :unspecific (directory) name type version))))
1353 ;;; We can't initialize this yet because not all host methods are
1354 ;;; loaded yet.
1355 (defvar *logical-pathname-defaults*)
1357 (defun logical-pathname (pathspec)
1358 #!+sb-doc
1359 "Converts the pathspec argument to a logical-pathname and returns it."
1360 (declare (type (or logical-pathname string stream) pathspec)
1361 (values logical-pathname))
1362 (if (typep pathspec 'logical-pathname)
1363 pathspec
1364 (let ((res (parse-namestring pathspec nil *logical-pathname-defaults*)))
1365 (when (eq (%pathname-host res)
1366 (%pathname-host *logical-pathname-defaults*))
1367 (error "This logical namestring does not specify a host:~% ~S"
1368 pathspec))
1369 res)))
1371 ;;;; logical pathname unparsing
1373 (defun unparse-logical-directory (pathname)
1374 (declare (type pathname pathname))
1375 (collect ((pieces))
1376 (let ((directory (%pathname-directory pathname)))
1377 (when directory
1378 (ecase (pop directory)
1379 (:absolute) ; nothing special
1380 (:relative (pieces ";")))
1381 (dolist (dir directory)
1382 (cond ((or (stringp dir) (pattern-p dir))
1383 (pieces (unparse-logical-piece dir))
1384 (pieces ";"))
1385 ((eq dir :wild)
1386 (pieces "*;"))
1387 ((eq dir :wild-inferiors)
1388 (pieces "**;"))
1390 (error "invalid directory component: ~S" dir))))))
1391 (apply #'concatenate 'simple-string (pieces))))
1393 (defun unparse-logical-piece (thing)
1394 (etypecase thing
1395 ((member :wild) "*")
1396 (simple-string thing)
1397 (pattern
1398 (collect ((strings))
1399 (dolist (piece (pattern-pieces thing))
1400 (etypecase piece
1401 (simple-string (strings piece))
1402 (keyword
1403 (cond ((eq piece :wild-inferiors)
1404 (strings "**"))
1405 ((eq piece :multi-char-wild)
1406 (strings "*"))
1407 (t (error "invalid keyword: ~S" piece))))))
1408 (apply #'concatenate 'simple-string (strings))))))
1410 (defun unparse-logical-file (pathname)
1411 (declare (type pathname pathname))
1412 (collect ((strings))
1413 (let* ((name (%pathname-name pathname))
1414 (type (%pathname-type pathname))
1415 (version (%pathname-version pathname))
1416 (type-supplied (not (or (null type) (eq type :unspecific))))
1417 (version-supplied (not (or (null version)
1418 (eq version :unspecific)))))
1419 (when name
1420 (when (and (null type) (position #\. name :start 1))
1421 (error "too many dots in the name: ~S" pathname))
1422 (strings (unparse-logical-piece name)))
1423 (when type-supplied
1424 (unless name
1425 (error "cannot specify the type without a file: ~S" pathname))
1426 (when (typep type 'simple-string)
1427 (when (position #\. type)
1428 (error "type component can't have a #\. inside: ~S" pathname)))
1429 (strings ".")
1430 (strings (unparse-logical-piece type)))
1431 (when version-supplied
1432 (unless type-supplied
1433 (error "cannot specify the version without a type: ~S" pathname))
1434 (etypecase version
1435 ((member :newest) (strings ".NEWEST"))
1436 ((member :wild) (strings ".*"))
1437 (fixnum (strings ".") (strings (format nil "~D" version))))))
1438 (apply #'concatenate 'simple-string (strings))))
1440 ;;; Unparse a logical pathname string.
1441 (defun unparse-enough-namestring (pathname defaults)
1442 (let* ((path-directory (pathname-directory pathname))
1443 (def-directory (pathname-directory defaults))
1444 (enough-directory
1445 ;; Go down the directory lists to see what matches. What's
1446 ;; left is what we want, more or less.
1447 (cond ((and (eq (first path-directory) (first def-directory))
1448 (eq (first path-directory) :absolute))
1449 ;; Both paths are :ABSOLUTE, so find where the
1450 ;; common parts end and return what's left
1451 (do* ((p (rest path-directory) (rest p))
1452 (d (rest def-directory) (rest d)))
1453 ((or (endp p) (endp d)
1454 (not (equal (first p) (first d))))
1455 `(:relative ,@p))))
1457 ;; At least one path is :RELATIVE, so just return the
1458 ;; original path. If the original path is :RELATIVE,
1459 ;; then that's the right one. If PATH-DIRECTORY is
1460 ;; :ABSOLUTE, we want to return that except when
1461 ;; DEF-DIRECTORY is :ABSOLUTE, as handled above. so return
1462 ;; the original directory.
1463 path-directory))))
1464 (unparse-logical-namestring
1465 (make-pathname :host (pathname-host pathname)
1466 :directory enough-directory
1467 :name (pathname-name pathname)
1468 :type (pathname-type pathname)
1469 :version (pathname-version pathname)))))
1471 (defun unparse-logical-namestring (pathname)
1472 (declare (type logical-pathname pathname))
1473 (concatenate 'simple-string
1474 (logical-host-name (%pathname-host pathname)) ":"
1475 (unparse-logical-directory pathname)
1476 (unparse-logical-file pathname)))
1478 ;;;; logical pathname translations
1480 ;;; Verify that the list of translations consists of lists and prepare
1481 ;;; canonical translations. (Parse pathnames and expand out wildcards
1482 ;;; into patterns.)
1483 (defun canonicalize-logical-pathname-translations (translation-list host)
1484 (declare (type list translation-list) (type host host)
1485 (values list))
1486 (mapcar (lambda (translation)
1487 (destructuring-bind (from to) translation
1488 (list (if (typep from 'logical-pathname)
1489 from
1490 (parse-namestring from host))
1491 (pathname to))))
1492 translation-list))
1494 (defun logical-pathname-translations (host)
1495 #!+sb-doc
1496 "Return the (logical) host object argument's list of translations."
1497 (declare (type (or string logical-host) host)
1498 (values list))
1499 (logical-host-translations (find-logical-host host)))
1501 (defun (setf logical-pathname-translations) (translations host)
1502 #!+sb-doc
1503 "Set the translations list for the logical host argument."
1504 (declare (type (or string logical-host) host)
1505 (type list translations)
1506 (values list))
1507 (let ((host (intern-logical-host host)))
1508 (setf (logical-host-canon-transls host)
1509 (canonicalize-logical-pathname-translations translations host))
1510 (setf (logical-host-translations host) translations)))
1512 (defun translate-logical-pathname (pathname &key)
1513 #!+sb-doc
1514 "Translate PATHNAME to a physical pathname, which is returned."
1515 (declare (type pathname-designator pathname)
1516 (values (or null pathname)))
1517 (typecase pathname
1518 (logical-pathname
1519 (dolist (x (logical-host-canon-transls (%pathname-host pathname))
1520 (error 'simple-file-error
1521 :pathname pathname
1522 :format-control "no translation for ~S"
1523 :format-arguments (list pathname)))
1524 (destructuring-bind (from to) x
1525 (when (pathname-match-p pathname from)
1526 (return (translate-logical-pathname
1527 (translate-pathname pathname from to)))))))
1528 (pathname pathname)
1529 (t (translate-logical-pathname (pathname pathname)))))
1531 (defvar *logical-pathname-defaults*
1532 (%make-logical-pathname
1533 (make-logical-host :name (logical-word-or-lose "BOGUS"))
1534 :unspecific nil nil nil nil))
1536 (defun load-logical-pathname-translations (host)
1537 #!+sb-doc
1538 (declare (type string host)
1539 (values (member t nil)))
1540 (if (find-logical-host host nil)
1541 ;; This host is already defined, all is well and good.
1543 ;; ANSI: "The specific nature of the search is
1544 ;; implementation-defined." SBCL: doesn't search at all
1546 ;; FIXME: now that we have a SYS host that the system uses, it
1547 ;; might be cute to search in "SYS:TRANSLATIONS;<name>.LISP"
1548 (error "logical host ~S not found" host)))