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