/[cmucl]/src/code/pathname.lisp
ViewVC logotype

Contents of /src/code/pathname.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.86 - (show annotations)
Fri Apr 4 15:11:13 2008 UTC (6 years ago) by rtoy
Branch: MAIN
CVS Tags: snapshot-2008-08, snapshot-2008-09, sse2-packed-2008-11-12, snapshot-2008-05, snapshot-2008-06, snapshot-2008-07, sse2-base, sse2-packed-base, release-19f-pre1, snapshot-2008-12, snapshot-2008-11, release-19f-base, merge-sse2-packed, merge-with-19f, RELEASE_19f, snapshot-2008-10, sse2-checkpoint-2008-10-01, sse2-merge-with-2008-11, sse2-merge-with-2008-10, unicode-utf16-base, snapshot-2009-02, snapshot-2009-01
Branch point for: RELEASE-19F-BRANCH, sse2-packed-branch, unicode-utf16-branch, sse2-branch
Changes since 1.85: +22 -9 lines
o Pathname printer was producing an error for (MAKE-PATHNAME :HOST NIL
  :TYPE "foo").  This is because PATHNAME-HOST was signaling an error
  for a host of NIL.  We check for a host of NIL now, and bypass the
  call to PATHNAME-HOST.  This allows the pathname printer to print
  #P(:HOST NIL :TYPE "foo"), as we want.

  (The error comes from host-pathname.  NIL is a valid result of
  host-pathname, so we could change that, but I think that has other
  implications, one of which is (MAKE-PATHNAME :HOST NIL :NAME "foo"
  :TYPE "lisp") gets printed as #P"foo.lisp", which is wrong, since
  that pathname has a unix-host host.)

o Fix merging of version in MAKE-PATHNAME.  CLHS MERGE-PATHNAMES says
  if the pathname name is given, the version is not affected by the
  default pathname.
1 ;;; -*- Package: LISP -*-
2 ;;; **********************************************************************
3 ;;; This code was written as part of the CMU Common Lisp project at
4 ;;; Carnegie Mellon University, and has been placed in the public domain.
5 ;;;
6 (ext:file-comment
7 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/pathname.lisp,v 1.86 2008/04/04 15:11:13 rtoy Exp $")
8 ;;;
9 ;;; **********************************************************************
10 ;;;
11 ;;; Machine/filesystem independent pathname functions for CMU Common Lisp.
12 ;;;
13 ;;; Written by William Lott, Paul Gleichauf and Rob MacLachlan.
14 ;;; Earlier version written by Jim Large and Rob MacLachlan
15 ;;;
16 ;;; **********************************************************************
17
18 (in-package "LISP")
19
20 (export '(pathname pathnamep logical-pathname
21 parse-namestring merge-pathnames make-pathname
22 pathname-host pathname-device pathname-directory pathname-name
23 pathname-type pathname-version namestring file-namestring
24 directory-namestring host-namestring enough-namestring
25 wild-pathname-p pathname-match-p translate-pathname
26 translate-logical-pathname logical-pathname-translations
27 load-logical-pathname-translations *default-pathname-defaults*))
28
29 (in-package "EXTENSIONS")
30 (export '(search-list search-list-defined-p clear-search-list
31 enumerate-search-list *autoload-translations*))
32
33 (in-package "LISP")
34
35 (defvar *autoload-translations* nil
36 "When non-nil, attempt to load \"library:<host>.translations\" to resolve
37 an otherwise undefined logical host.")
38
39
40 ;;;; HOST structures
41
42 ;;; The host structure holds the functions that both parse the pathname
43 ;;; information into sturcture slot entries, and after translation the inverse
44 ;;; (unparse) functions.
45 ;;;
46 (defstruct (host
47 (:print-function %print-host))
48 (parse (required-argument) :type function)
49 (unparse (required-argument) :type function)
50 (unparse-host (required-argument) :type function)
51 (unparse-directory (required-argument) :type function)
52 (unparse-file (required-argument) :type function)
53 (unparse-enough (required-argument) :type function)
54 (customary-case (required-argument) :type (member :upper :lower)))
55
56 ;;; %PRINT-HOST -- Internal
57 ;;;
58 (defun %print-host (host stream depth)
59 (declare (ignore depth))
60 (print-unreadable-object (host stream :type t)))
61
62 (defun %print-logical-host (host stream depth)
63 (declare (ignore depth))
64 (print-unreadable-object (host stream :type t :identity t)
65 (write (logical-host-name host) :stream stream)))
66
67 (defstruct (logical-host
68 (:include host
69 (:parse #'parse-logical-namestring)
70 (:unparse #'unparse-logical-namestring)
71 (:unparse-host
72 #'(lambda (x) (logical-host-name (%pathname-host x))))
73 (:unparse-directory #'unparse-logical-directory)
74 (:unparse-file #'unparse-unix-file)
75 (:unparse-enough #'unparse-enough-namestring)
76 (:customary-case :upper))
77 (:print-function %print-logical-host)
78 (:make-load-form-fun make-logical-host-load-form-fun))
79 (name "" :type simple-base-string)
80 (translations nil :type list)
81 (canon-transls nil :type list))
82
83 (defun make-logical-host-load-form-fun (logical-host)
84 (values `(find-logical-host ',(logical-host-name logical-host))
85 nil))
86
87 ;;; The various magic tokens that are allowed to appear in pretty much all
88 ;;; pathname components.
89 ;;;
90 (deftype component-tokens ()
91 '(member nil :unspecific :wild))
92
93 ;;;; Pathname structures
94
95 (defstruct (pathname
96 (:conc-name %pathname-)
97 (:print-function %print-pathname)
98 (:constructor
99 %make-pathname (host device directory name type version))
100 (:predicate pathnamep)
101 (:make-load-form-fun :just-dump-it-normally))
102 ;; Slot holds the host, at present either a UNIX or logical host.
103 (host nil :type (or host null))
104 ;; Device is the name of a logical or physical device holding files.
105 (device nil :type (or simple-string component-tokens))
106 ;; A list of strings that are the component subdirectory components.
107 (directory nil :type list)
108 ;; The filename.
109 (name nil :type (or simple-string pattern component-tokens))
110 ;; The type extension of the file.
111 (type nil :type (or simple-string pattern component-tokens))
112 ;; The version number of the file, a positive integer, but not supported
113 ;; on standard UNIX filesystems.
114 (version nil :type (or integer component-tokens (member :newest))))
115
116 ;;; %PRINT-PATHNAME -- Internal
117 ;;;
118 ;;; The printed representation of the pathname structure.
119 ;;;
120 (defun %print-pathname (pathname stream depth)
121 (declare (ignore depth))
122 (let* ((host (%pathname-host pathname))
123 (namestring (if host
124 (handler-case (namestring pathname)
125 (error nil))
126 nil)))
127 (cond (namestring
128 (if (or *print-escape* *print-readably*)
129 (format stream "#P~S" namestring)
130 (format stream "~A" namestring)))
131 (t
132 (let ((device (%pathname-device pathname))
133 (directory (%pathname-directory pathname))
134 (name (%pathname-name pathname))
135 (type (%pathname-type pathname))
136 (version (%pathname-version pathname)))
137 (cond ((every #'(lambda (d)
138 (or (stringp d)
139 (symbolp d)))
140 (cdr directory))
141 ;; A CMUCL extension. If we have an unprintable
142 ;; pathname, convert it to a form that would be
143 ;; suitable as args to MAKE-PATHNAME to recreate
144 ;; the pathname.
145 ;;
146 ;; We don't handle search-lists because we don't
147 ;; currently have a readable syntax for
148 ;; search-lists.
149 (collect ((result))
150 (unless (eq host *unix-host*)
151 (result :host)
152 (result (if host
153 (pathname-host pathname)
154 nil)))
155 (when device
156 (result :device)
157 (result device))
158 (when directory
159 (result :directory)
160 (result directory))
161 (when name
162 (result :name)
163 (result name))
164 (when type
165 (result :type)
166 (result type))
167 (when version
168 (result :version)
169 (result version))
170 (format stream "#P~S" (result))))
171 (*print-readably*
172 (error 'print-not-readable :object pathname))
173 (t
174 (funcall (formatter "#<Unprintable pathname,~:_ Host=~S,~:_ Device=~S,~:_ ~
175 Directory=~S,~:_ Name=~S,~:_ Type=~S,~:_ Version=~S>")
176 stream
177 (%pathname-host pathname)
178 (%pathname-device pathname)
179 (%pathname-directory pathname)
180 (%pathname-name pathname)
181 (%pathname-type pathname)
182 (%pathname-version pathname)))))))))
183
184 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
185 ;;;
186 ;;; Logical pathnames have the following format:
187 ;;;
188 ;;; logical-namestring ::=
189 ;;; [host ":"] [";"] {directory ";"}* [name] ["." type ["." version]]
190 ;;;
191 ;;; host ::= word
192 ;;; directory ::= word | wildcard-word | **
193 ;;; name ::= word | wildcard-word
194 ;;; type ::= word | wildcard-word
195 ;;; version ::= pos-int | newest | NEWEST | *
196 ;;; word ::= {uppercase-letter | digit | -}+
197 ;;; wildcard-word ::= [word] '* {word '*}* [word]
198 ;;; pos-int ::= integer > 0
199 ;;;
200 ;;; Physical pathnames include all these slots and a device slot.
201 ;;;
202 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
203
204 ;; Logical pathnames are a subclass of pathname, their class relations are
205 ;; mimiced using structures for efficency.
206
207 (defstruct (logical-pathname
208 (:conc-name %logical-pathname-)
209 (:print-function %print-logical-pathname)
210 (:include pathname)
211 (:constructor %make-logical-pathname
212 (host device directory name type version))
213 (:make-load-form-fun :just-dump-it-normally)))
214
215 (declaim (freeze-type logical-pathname logical-host))
216
217 ;;; %PRINT-LOGICAL-PATHNAME -- Internal
218 ;;;
219 ;;; The printed representation of the logical-pathname structure.
220 ;;; The potential conflict with search-lists requires isolating the printed
221 ;;; representation to use the i/o macro #.(logical-pathname <path-designator>).
222 ;;;
223 ;;; Is there really a potential conflict? CMUCL does not allow
224 ;;; search-list hosts and logical pathname hosts have the same name.
225 ;;; Hence, if we use #P to print them out, we can always read them
226 ;;; back without confusion. (rtoy)
227 ;;;
228 (defun %print-logical-pathname (pathname stream depth)
229 (declare (ignore depth))
230 (let ((namestring (handler-case (namestring pathname)
231 (error nil))))
232 (cond (namestring
233 (if (or *print-escape* *print-readably*)
234 (format stream "#P~S" namestring)
235 (format stream "~A" namestring)))
236 (*print-readably*
237 (error 'print-not-readable :object pathname))
238 (t
239 (funcall (formatter "#<Unprintable pathname, Host=~S, ~
240 Directory=~S, File=~S, Name=~S, Version=~S>")
241 stream
242 (%pathname-host pathname)
243 (%pathname-directory pathname)
244 (%pathname-name pathname)
245 (%pathname-type pathname)
246 (%pathname-version pathname))))))
247
248 ;;; %MAKE-PATHNAME-OBJECT -- internal
249 ;;;
250 ;;; A pathname is logical if the host component is a logical-host.
251 ;;; This constructor is used to make an instance of the correct type
252 ;;; from parsed arguments.
253
254 (defun %make-pathname-object (host device directory name type version)
255 (if (typep host 'logical-host)
256 (flet ((upcasify (thing)
257 (typecase thing
258 (list
259 (mapcar #'(lambda (x)
260 (if (stringp x)
261 (string-upcase x)
262 x))
263 thing))
264 (simple-base-string
265 (string-upcase thing))
266 (t
267 thing))))
268 (%make-logical-pathname host :unspecific
269 (upcasify directory)
270 (upcasify name)
271 (upcasify type)
272 (upcasify version)))
273 (%make-pathname host device directory name type version)))
274
275 ;;; *LOGICAL-HOSTS* --internal.
276 ;;;
277 ;;; Hash table searching maps a logical-pathname's host to their physical
278 ;;; pathname translation.
279
280 (defvar *logical-hosts* (make-hash-table :test #'equal))
281
282 ;;; PATH-DESIGNATOR -- internal type
283 ;;;
284 (deftype path-designator ()
285 "A path specification, either a string, file-stream or pathname."
286 ;; This used to be stream, not file-stream, but ANSI CL says a
287 ;; pathname designator is a string, a pathname or a stream
288 ;; associated with a file. In the places we use path-designator, we
289 ;; are really talking about ANSI pathname designators.
290 '(or string file-stream pathname))
291
292
293 ;;;; Patterns
294
295 ;;; Patterns are a list of entries and wildcards used for pattern matches
296 ;;; of translations.
297
298 (defstruct (pattern
299 (:print-function %print-pattern)
300 (:make-load-form-fun :just-dump-it-normally)
301 (:constructor make-pattern (pieces)))
302 (pieces nil :type list))
303
304 ;;; %PRINT-PATTERN -- Internal
305 ;;;
306 (defun %print-pattern (pattern stream depth)
307 (declare (ignore depth))
308 (print-unreadable-object (pattern stream :type t)
309 (if *print-pretty*
310 (let ((*print-escape* t))
311 (pprint-fill stream (pattern-pieces pattern) nil))
312 (prin1 (pattern-pieces pattern) stream))))
313
314 ;;; PATTERN= -- Internal
315 ;;;
316 (defun pattern= (pattern1 pattern2)
317 (declare (type pattern pattern1 pattern2))
318 (let ((pieces1 (pattern-pieces pattern1))
319 (pieces2 (pattern-pieces pattern2)))
320 (and (= (length pieces1) (length pieces2))
321 (every #'(lambda (piece1 piece2)
322 (typecase piece1
323 (simple-string
324 (and (simple-string-p piece2)
325 (string= piece1 piece2)))
326 (cons
327 (and (consp piece2)
328 (eq (car piece1) (car piece2))
329 (string= (cdr piece1) (cdr piece2))))
330 (t
331 (eq piece1 piece2))))
332 pieces1
333 pieces2))))
334
335 ;;; PATTERN-MATCHES -- Internal
336 ;;;
337 ;;; If the string matches the pattern returns the multiple values T and a
338 ;;; list of the matched strings.
339 ;;;
340 (defun pattern-matches (pattern string)
341 (declare (type pattern pattern)
342 (type simple-string string))
343 (let ((len (length string)))
344 (labels ((maybe-prepend (subs cur-sub chars)
345 (if cur-sub
346 (let* ((len (length chars))
347 (new (make-string len))
348 (index len))
349 (dolist (char chars)
350 (setf (schar new (decf index)) char))
351 (cons new subs))
352 subs))
353 (matches (pieces start subs cur-sub chars)
354 (if (null pieces)
355 (if (= start len)
356 (values t (maybe-prepend subs cur-sub chars))
357 (values nil nil))
358 (let ((piece (car pieces)))
359 (etypecase piece
360 (simple-string
361 (let ((end (+ start (length piece))))
362 (and (<= end len)
363 (string= piece string
364 :start2 start :end2 end)
365 (matches (cdr pieces) end
366 (maybe-prepend subs cur-sub chars)
367 nil nil))))
368 (list
369 (ecase (car piece)
370 (:character-set
371 (and (< start len)
372 (let ((char (schar string start)))
373 (if (find char (second piece) :test #'char=)
374 (matches (cdr pieces) (1+ start) subs t
375 (cons char chars))))))))
376 ((member :single-char-wild)
377 (and (< start len)
378 (matches (cdr pieces) (1+ start) subs t
379 (cons (schar string start) chars))))
380 ((member :multi-char-wild)
381 (multiple-value-bind
382 (won new-subs)
383 (matches (cdr pieces) start subs t chars)
384 (if won
385 (values t new-subs)
386 (and (< start len)
387 (matches pieces (1+ start) subs t
388 (cons (schar string start)
389 chars)))))))))))
390 (multiple-value-bind
391 (won subs)
392 (matches (pattern-pieces pattern) 0 nil nil nil)
393 (values won (reverse subs))))))
394
395
396 ;;; DIRECTORY-COMPONENTS-MATCH -- Internal
397 ;;;
398 ;;; Pathname-match-p for directory components. If thing is empty
399 ;;; then it matches :wild, (:absolute :wild-inferiors), or (:relative
400 ;;; :wild-inferiors).
401 ;;;
402 (defun directory-components-match (thing wild)
403 (or (eq thing wild)
404 (eq wild :wild)
405 (and (consp wild)
406 (let ((wild1 (first wild)))
407 (cond ((and (null thing) (member wild1 '(:absolute :relative)))
408 (equal (rest wild) '(:wild-inferiors)))
409 ((eq wild1 :wild-inferiors)
410 (let ((wild-subdirs (rest wild)))
411 (or (null wild-subdirs)
412 (loop
413 (when (directory-components-match thing
414 wild-subdirs)
415 (return t))
416 (pop thing)
417 (unless thing (return nil))))))
418 ((consp thing)
419 (and (components-match (first thing) wild1)
420 (directory-components-match (rest thing)
421 (rest wild)))))))))
422
423
424 ;;; COMPONENTS-MATCH -- Internal
425 ;;;
426 ;;; Return true if pathname component Thing is matched by Wild. Not
427 ;;; commutative.
428 ;;;
429 (defun components-match (thing wild)
430 (declare (type (or pattern symbol simple-string integer) thing wild))
431 (or (eq thing wild)
432 (eq wild :wild)
433 (typecase thing
434 (simple-base-string
435 ;; String is matched by itself, a matching pattern or :WILD.
436 (typecase wild
437 (pattern
438 (values (pattern-matches wild thing)))
439 (simple-base-string
440 (string= thing wild))))
441 (pattern
442 ;; A pattern is only matched by an identical pattern.
443 (and (pattern-p wild) (pattern= thing wild)))
444 (integer
445 ;; an integer (version number) is matched by :WILD or the same
446 ;; integer. This branch will actually always be NIL as long is the
447 ;; version is a fixnum.
448 (eql thing wild)))))
449
450
451 ;;; COMPARE-COMPONENT -- Internal
452 ;;;
453 ;;; A predicate for comparing two pathname slot component sub-entries.
454 ;;;
455 (defun compare-component (this that)
456 (or (eql this that)
457 (typecase this
458 (simple-string
459 (and (simple-string-p that)
460 (string= this that)))
461 (pattern
462 (and (pattern-p that)
463 (pattern= this that)))
464 (cons
465 (and (consp that)
466 (compare-component (car this) (car that))
467 (compare-component (cdr this) (cdr that)))))))
468
469 ;; Compare the version component. We treat NIL to be EQUAL to
470 ;; :NEWEST.
471 (defun compare-version-component (this that)
472 (or (eql this that)
473 (and (null this) (eq that :newest))
474 (and (null that) (eq this :newest))))
475
476 ;;;; Pathname functions.
477
478 ;;; Implementation determined defaults to pathname slots.
479
480 (defvar *default-pathname-defaults*)
481
482 ;;; PATHNAME= -- Internal
483 ;;;
484 (defun pathname= (pathname1 pathname2)
485 (declare (type pathname pathname1)
486 (type pathname pathname2))
487 (and (eq (%pathname-host pathname1)
488 (%pathname-host pathname2))
489 (compare-component (%pathname-device pathname1)
490 (%pathname-device pathname2))
491 (compare-component (%pathname-directory pathname1)
492 (%pathname-directory pathname2))
493 (compare-component (%pathname-name pathname1)
494 (%pathname-name pathname2))
495 (compare-component (%pathname-type pathname1)
496 (%pathname-type pathname2))
497 (compare-version-component (%pathname-version pathname1)
498 (%pathname-version pathname2))))
499
500 ;;; WITH-PATHNAME -- Internal
501 ;;; Converts the expr, a pathname designator (a pathname, or string, or
502 ;;; stream), into a pathname.
503 ;;;
504 (defmacro with-pathname ((var expr) &body body)
505 `(let ((,var (let ((,var ,expr))
506 (etypecase ,var
507 (pathname ,var)
508 (string (parse-namestring ,var))
509 (file-stream (file-name ,var))
510 (stream:file-simple-stream (file-name ,var))))))
511 ,@body))
512
513 ;;; WITH-HOST -- Internal
514 ;;;
515 ;;; Converts the var, a host or string name for a host, into a logical-host
516 ;;; structure or nil if not defined.
517 ;;;
518 ;;; pw notes 1/12/97 this potentially useful macro is not used anywhere
519 ;;; and 'find-host' is not defined. 'find-logical-host' seems to be needed.
520 ;;;
521 (defmacro with-host ((var expr) &body body)
522 `(let ((,var (let ((,var ,expr))
523 (typecase ,var
524 (logical-host ,var)
525 (string (find-logical-host ,var nil))
526 (t nil)))))
527 ,@body))
528
529 ;;; PATHNAME -- Interface
530 ;;;
531 (defun pathname (thing)
532 "Convert thing (a pathname, string or stream) into a pathname."
533 (declare (type path-designator thing))
534 (with-pathname (pathname thing)
535 pathname))
536
537 ;;; MAYBE-DIDDLE-CASE -- Internal
538 ;;;
539 ;;; Change the case of thing if diddle-p T.
540 ;;;
541 (defun maybe-diddle-case (thing diddle-p)
542 (if (and diddle-p (not (or (symbolp thing) (integerp thing))))
543 (labels ((check-for (pred in)
544 (typecase in
545 (pattern
546 (dolist (piece (pattern-pieces in))
547 (when (typecase piece
548 (simple-string
549 (check-for pred piece))
550 (cons
551 (case (car in)
552 (:character-set
553 (check-for pred (cdr in))))))
554 (return t))))
555 (list
556 (dolist (x in)
557 (when (check-for pred x)
558 (return t))))
559 (simple-base-string
560 (dotimes (i (length in))
561 (when (funcall pred (schar in i))
562 (return t))))
563 (t nil)))
564 (diddle-with (fun thing)
565 (typecase thing
566 (pattern
567 (make-pattern
568 (mapcar #'(lambda (piece)
569 (typecase piece
570 (simple-base-string
571 (funcall fun piece))
572 (cons
573 (case (car piece)
574 (:character-set
575 (cons :character-set
576 (funcall fun (cdr piece))))
577 (t
578 piece)))
579 (t
580 piece)))
581 (pattern-pieces thing))))
582 (list
583 (mapcar fun thing))
584 (simple-base-string
585 (funcall fun thing))
586 (t
587 thing))))
588 (let ((any-uppers (check-for #'upper-case-p thing))
589 (any-lowers (check-for #'lower-case-p thing)))
590 (cond ((and any-uppers any-lowers)
591 ;; Mixed case, stays the same.
592 thing)
593 (any-uppers
594 ;; All uppercase, becomes all lower case.
595 (diddle-with #'(lambda (x) (if (stringp x)
596 (string-downcase x)
597 x)) thing))
598 (any-lowers
599 ;; All lowercase, becomes all upper case.
600 (diddle-with #'(lambda (x) (if (stringp x)
601 (string-upcase x)
602 x)) thing))
603 (t
604 ;; No letters? I guess just leave it.
605 thing))))
606 thing))
607
608
609 ;;; MERGE-DIRECTORIES -- Internal
610 ;;;
611 (defun merge-directories (dir1 dir2 diddle-case)
612 (if (or (eq (car dir1) :absolute)
613 (null dir2))
614 dir1
615 (let ((results nil))
616 (flet ((add (dir)
617 (if (and (eq dir :back)
618 results
619 (not (eq (car results) :back)))
620 (pop results)
621 (push dir results))))
622 (dolist (dir (maybe-diddle-case dir2 diddle-case))
623 (add dir))
624 (dolist (dir (cdr dir1))
625 (add dir)))
626 (reverse results))))
627
628 ;;; MERGE-PATHNAMES -- Interface
629 ;;;
630 (defun merge-pathnames (pathname
631 &optional
632 (defaults *default-pathname-defaults*)
633 (default-version :newest))
634 "Construct a filled in pathname by completing the unspecified components
635 from the defaults."
636 (declare (type path-designator pathname)
637 (type path-designator defaults)
638 (values pathname))
639 (with-pathname (defaults defaults)
640 (let ((pathname (let ((*default-pathname-defaults* defaults))
641 (pathname pathname))))
642 (let* ((default-host (%pathname-host defaults))
643 (pathname-host (%pathname-host pathname))
644 (diddle-case
645 (and default-host pathname-host
646 (not (eq (host-customary-case default-host)
647 (host-customary-case pathname-host))))))
648 (%make-pathname-object
649 (or pathname-host default-host)
650 (or (%pathname-device pathname)
651 (maybe-diddle-case (%pathname-device defaults)
652 diddle-case))
653 (merge-directories (%pathname-directory pathname)
654 (%pathname-directory defaults)
655 diddle-case)
656 (or (%pathname-name pathname)
657 (maybe-diddle-case (%pathname-name defaults)
658 diddle-case))
659 (or (%pathname-type pathname)
660 (maybe-diddle-case (%pathname-type defaults)
661 diddle-case))
662 (or (if (null (%pathname-name pathname))
663 (or (%pathname-version pathname)
664 (%pathname-version defaults))
665 (%pathname-version pathname))
666 default-version))))))
667
668 ;;; IMPORT-DIRECTORY -- Internal
669 ;;;
670 (defun import-directory (directory diddle-case)
671 (etypecase directory
672 (null nil)
673 ((member :wild) '(:absolute :wild-inferiors))
674 ((member :unspecific) '(:relative))
675 (list
676 (collect ((results))
677 (ecase (pop directory)
678 (:absolute
679 (results :absolute)
680 (when (search-list-p (car directory))
681 (results (pop directory))))
682 (:relative
683 (results :relative)))
684 (dolist (piece directory)
685 (cond ((member piece '(:wild :wild-inferiors :up :back))
686 (results piece))
687 ((or (simple-string-p piece) (pattern-p piece))
688 (results (maybe-diddle-case piece diddle-case)))
689 ((stringp piece)
690 (results (maybe-diddle-case (coerce piece 'simple-string)
691 diddle-case)))
692 (t
693 (error "~S is not allowed as a directory component." piece))))
694 (results)))
695 (simple-string
696 `(:absolute
697 ,(maybe-diddle-case directory diddle-case)))
698 (string
699 `(:absolute
700 ,(maybe-diddle-case (coerce directory 'simple-string)
701 diddle-case)))))
702
703 ;;; MAKE-PATHNAME -- Interface
704 ;;;
705 (defun make-pathname (&key (host nil hostp)
706 (device nil devp)
707 (directory nil dirp)
708 (name nil namep)
709 (type nil typep)
710 (version nil versionp)
711 defaults
712 (case :local))
713 "Makes a new pathname from the component arguments. Note that host is
714 a host-structure or string."
715 (declare (type (or null string host component-tokens) host)
716 (type (or string component-tokens) device)
717 (type (or list string pattern component-tokens) directory)
718 (type (or string pattern component-tokens) name type)
719 (type (or integer component-tokens (member :newest)) version)
720 (type (or path-designator null) defaults)
721 (type (member :common :local) case))
722 (let* ((defaults (when defaults
723 (with-pathname (defaults defaults) defaults)))
724 (default-host (if defaults
725 (%pathname-host defaults)
726 (%pathname-host *default-pathname-defaults*)))
727 ;; toy@rtp.ericsson.se: CLHS says make-pathname can take a
728 ;; string (as a logical-host) for the host part. We map that
729 ;; string into the corresponding logical host structure.
730
731 ;; pw@snoopy.mv.com:
732 ;; HyperSpec says for the arg to MAKE-PATHNAME;
733 ;; "host---a valid physical pathname host. ..."
734 ;; where it probably means -- a valid pathname host.
735 ;; "valid pathname host n. a valid physical pathname host or
736 ;; a valid logical pathname host."
737 ;; and defines
738 ;; "valid physical pathname host n. any of a string,
739 ;; a list of strings, or the symbol :unspecific,
740 ;; that is recognized by the implementation as the name of a host."
741 ;; "valid logical pathname host n. a string that has been defined
742 ;; as the name of a logical host. ..."
743 ;; HS is silent on what happens if the :host arg is NOT one of these.
744 ;; It seems an error message is appropriate.
745 (host (typecase host
746 (host host) ; A valid host, use it.
747 ((string 0) default-host) ; "" cannot be a logical host
748 (string (find-logical-host host t)) ; logical-host or lose.
749 (t
750 ;; If the user specifically set :host to be NIL, use
751 ;; it. Otherwise, we use the default host.
752 (if (and hostp (null host))
753 nil
754 default-host))))
755 (diddle-args
756 ;; What to do if no host is given? Can't figure out the
757 ;; customary case, so I (rtoy) am going to assume we don't
758 ;; need to diddle args.
759 (and host (eq (host-customary-case host) :lower)
760 (eq case :common)))
761 (diddle-defaults
762 ;; Same for diddle-defaults. Do nothing if no host was given.
763 (and host (not (eq (host-customary-case host)
764 (host-customary-case default-host)))))
765 (dev (if devp device (if defaults (%pathname-device defaults))))
766 (dir (import-directory directory diddle-args))
767 ;; CLHS MERGE-PATHNAMES (via MAKE-PATHNAME) says
768 ;;
769 ;; If pathname does not specify a name, then the version, if
770 ;; not provided, will come from default-pathname, just like
771 ;; the other components. If pathname does specify a name,
772 ;; then the version is not affected by default-pathname. If
773 ;; this process leaves the version missing, the
774 ;; default-version is used.
775 (ver (cond
776 (versionp version)
777 (namep version)
778 (defaults (%pathname-version defaults))
779 (t nil))))
780 (when (and defaults (not dirp))
781 (setf dir
782 (merge-directories dir
783 (%pathname-directory defaults)
784 diddle-defaults)))
785
786 ;; A bit of sanity checking on user arguments.
787 (flet ((check-component-validity (name name-or-type)
788 (when (stringp name)
789 (let ((unix-directory-separator #\/))
790 (when (eq host (%pathname-host *default-pathname-defaults*))
791 (when (find unix-directory-separator name)
792 (warn "Silly argument for a unix ~A: ~S"
793 name-or-type name)))))))
794 (check-component-validity name :pathname-name)
795 (check-component-validity type :pathname-type)
796 (mapc #'(lambda (d)
797 (check-component-validity d :directory))
798 (cdr dir))
799 (when (and (stringp name)
800 (or (and (string= name "..")
801 (not type))
802 (and (string= name ".")
803 (not type))))
804 ;;
805 (warn "Silly argument for a unix PATHNAME-NAME: ~S" name)))
806
807 ;; More sanity checking
808 (when dir
809 ;; Try to canonicalize the directory component. :absolute
810 ;; followed by a bunch of "/" deletes the leading "/"'s.
811 ;; :relative followed by "." anywhere gets them all deleted.
812 (ecase (first dir)
813 (:absolute
814 (do ((p (cdr dir) (cdr p)))
815 ((or (null p)
816 (not (equal "/" (car p))))
817 (setf (cdr dir) p))))
818 (:relative
819 (setf (cdr dir) (delete "." (cdr dir) :test #'equal)))))
820 ;; CLHS 19.2.2.4.3 says :absolute or :wild-inferiors immediately
821 ;; followed by :up or :back signals a file-error.
822
823 (let ((d (or (member :wild-inferiors dir)
824 (member :absolute dir))))
825 (when (and d (rest d) (member (second d) '(:up :back)))
826 ;; What should we put in the for pathname part of file-error?
827 ;; I'm just going to use the directory but removing the
828 ;; offending :up or :back. Or would just (make-pathname) be
829 ;; enough?
830 ;;
831 ;; Or instead of checking here, we could check whenever we
832 ;; "use" the file system (CLHS 19.2.2.4.3). But that's a lot
833 ;; harder to do.
834 (error 'simple-file-error
835 :pathname (make-pathname :directory (remove-if #'(lambda (x)
836 (member x '(:up :back)))
837 dir))
838 :format-control "Illegal pathname: ~
839 Directory with ~S immediately followed by ~S"
840 :format-arguments (list (first d) (second d)))))
841
842 (macrolet ((pick (var varp field)
843 `(cond ((or (simple-string-p ,var)
844 (pattern-p ,var))
845 (maybe-diddle-case ,var diddle-args))
846 ((stringp ,var)
847 (maybe-diddle-case (coerce ,var 'simple-string)
848 diddle-args))
849 (,varp
850 (maybe-diddle-case ,var diddle-args))
851 (defaults
852 (maybe-diddle-case (,field defaults)
853 diddle-defaults))
854 (t
855 nil))))
856 (%make-pathname-object host
857 dev ; forced to :unspecific when logical-host
858 dir
859 (pick name namep %pathname-name)
860 (pick type typep %pathname-type)
861 ver))))
862
863 ;;; PATHNAME-HOST -- Interface
864 ;;;
865 (defun pathname-host (pathname &key (case :local))
866 "Accessor for the pathname's host."
867 (declare (type path-designator pathname)
868 (type (member :local :common) case)
869 (values (or string null))
870 (ignore case))
871 (host-namestring pathname))
872
873 ;;; PATHNAME-DEVICE -- Interface
874 ;;;
875 (defun pathname-device (pathname &key (case :local))
876 "Accessor for pathname's device."
877 (declare (type path-designator pathname)
878 (type (member :local :common) case))
879 (with-pathname (pathname pathname)
880 (maybe-diddle-case (%pathname-device pathname)
881 (and (eq case :common)
882 (eq (host-customary-case
883 (%pathname-host pathname))
884 :lower)))))
885
886 ;;; PATHNAME-DIRECTORY -- Interface
887 ;;;
888 (defun pathname-directory (pathname &key (case :local))
889 "Accessor for the pathname's directory list."
890 (declare (type path-designator pathname)
891 (type (member :local :common) case))
892 (with-pathname (pathname pathname)
893 (maybe-diddle-case (%pathname-directory pathname)
894 (and (eq case :common)
895 (eq (host-customary-case
896 (%pathname-host pathname))
897 :lower)))))
898 ;;; PATHNAME-NAME -- Interface
899 ;;;
900 (defun pathname-name (pathname &key (case :local))
901 "Accessor for the pathname's name."
902 (declare (type path-designator pathname)
903 (type (member :local :common) case))
904 (with-pathname (pathname pathname)
905 (maybe-diddle-case (%pathname-name pathname)
906 (and (eq case :common)
907 (eq (host-customary-case
908 (%pathname-host pathname))
909 :lower)))))
910
911 ;;; PATHNAME-TYPE
912 ;;;
913 (defun pathname-type (pathname &key (case :local))
914 "Accessor for the pathname's name."
915 (declare (type path-designator pathname)
916 (type (member :local :common) case))
917 (with-pathname (pathname pathname)
918 (maybe-diddle-case (%pathname-type pathname)
919 (and (eq case :common)
920 (eq (host-customary-case
921 (%pathname-host pathname))
922 :lower)))))
923
924 ;;; PATHNAME-VERSION
925 ;;;
926 (defun pathname-version (pathname)
927 "Accessor for the pathname's version."
928 (declare (type path-designator pathname))
929 (with-pathname (pathname pathname)
930 (%pathname-version pathname)))
931
932
933 ;;;; Namestrings
934
935 ;;; %PRINT-NAMESTRING-PARSE-ERROR -- Internal
936 ;;;
937 (defun %print-namestring-parse-error (condition stream)
938 (format stream "Parse error in namestring: ~?~% ~A~% ~V@T^"
939 (namestring-parse-error-complaint condition)
940 (namestring-parse-error-arguments condition)
941 (namestring-parse-error-namestring condition)
942 (namestring-parse-error-offset condition)))
943
944 (define-condition namestring-parse-error (parse-error type-error)
945 ((complaint :reader namestring-parse-error-complaint :initarg :complaint)
946 (arguments :reader namestring-parse-error-arguments :initarg :arguments
947 :initform nil)
948 (namestring :reader namestring-parse-error-namestring :initarg :namestring)
949 (offset :reader namestring-parse-error-offset :initarg :offset))
950 (:report %print-namestring-parse-error))
951
952
953 ;;; %PARSE-NAMESTRING -- Internal
954 ;;;
955 ;;; Handle the case where parse-namestring is actually parsing a namestring.
956 ;;; We pick off the :JUNK-ALLOWED case then find a host to use for parsing,
957 ;;; call the parser, then check if the host matches.
958 ;;;
959 (defun %parse-namestring (namestr host defaults start end junk-allowed)
960 (declare (type string namestr)
961 (type (or host string null) host)
962 (type pathname defaults)
963 (type index start)
964 (type (or index null) end))
965 (if junk-allowed
966 (handler-case
967 (%parse-namestring namestr host defaults start end nil)
968 (namestring-parse-error (condition)
969 (values nil (namestring-parse-error-offset condition))))
970 (let* ((end (or end (length namestr)))
971 (host (if (and host (stringp host))
972 (find-logical-host host)
973 host))
974 (default-host (%pathname-host defaults))
975 (parse-host (or host
976 (extract-logical-host-prefix namestr start end)
977 default-host)))
978 (unless parse-host
979 (error "When Host arg is not supplied, Defaults arg must ~
980 have a non-null PATHNAME-HOST."))
981
982 (multiple-value-bind
983 (new-host device directory file type version)
984 (funcall (host-parse parse-host) namestr start end)
985 (when (and host new-host (not (eq new-host host)))
986 (error "Host in namestring: ~S~@
987 does not match explicit host argument: ~S"
988 namestr host))
989 (let ((pn-host (or new-host parse-host)))
990 (values (%make-pathname-object
991 pn-host device directory file type version)
992 end))))))
993
994
995 ;;; EXTRACT-LOGICAL-HOST-PREFIX -- Internal
996 ;;;
997 ;;; If namestr begins with a colon-terminated, defined, logical host, then
998 ;;; return that host, otherwise return NIL.
999 ;;;
1000 (defun extract-logical-host-prefix (namestr start end)
1001 (declare (type simple-base-string namestr)
1002 (type index start end)
1003 (values (or logical-host null)))
1004 (let ((colon-pos (position #\: namestr :start start :end end)))
1005 (if colon-pos
1006 (find-logical-host (nstring-upcase (subseq namestr start colon-pos))
1007 nil)
1008 nil)))
1009
1010 ;;; PARSE-NAMESTRING -- Interface
1011 ;;;
1012 (defun parse-namestring (thing
1013 &optional host (defaults *default-pathname-defaults*)
1014 &key (start 0) end junk-allowed)
1015 "Converts pathname, a pathname designator, into a pathname structure,
1016 for a physical pathname, returns the printed representation. Host may be
1017 a physical host structure or host namestring."
1018 (declare (type path-designator thing)
1019 (type (or list string host (member :unspecific)) host)
1020 (type path-designator defaults)
1021 (type index start)
1022 (type (or index null) end))
1023 ;; Generally, redundant specification of information in software,
1024 ;; whether in code or in comments, is bad. However, the ANSI spec
1025 ;; for this is messy enough that it's hard to hold in short-term
1026 ;; memory, so I've recorded these redundant notes on the
1027 ;; implications of the ANSI spec.
1028 ;;
1029 ;; According to the ANSI spec, HOST can be a valid pathname host, or
1030 ;; a logical host, or NIL.
1031 ;;
1032 ;; A valid pathname host can be a valid physical pathname host or a
1033 ;; valid logical pathname host.
1034 ;;
1035 ;; A valid physical pathname host is "any of a string, a list of
1036 ;; strings, or the symbol :UNSPECIFIC, that is recognized by the
1037 ;; implementation as the name of a host". In SBCL as of 0.6.9.8,
1038 ;; that means :UNSPECIFIC: though someday we might want to
1039 ;; generalize it to allow strings like "RTFM.MIT.EDU" or lists like
1040 ;; '("RTFM" "MIT" "EDU"), that's not supported now.
1041 ;;
1042 ;; A valid logical pathname host is a string which has been defined as
1043 ;; the name of a logical host, as with LOAD-LOGICAL-PATHNAME-TRANSLATIONS.
1044 ;;
1045 (let ((host (etypecase host
1046 ((string 0)
1047 ;; This is a special host. It's not valid as a
1048 ;; logical host, so it is a sensible thing to
1049 ;; designate the physical Unix host object. So
1050 ;; we do that.
1051 *unix-host*)
1052 (string
1053 ;; In general ANSI-compliant Common Lisps, a
1054 ;; string might also be a physical pathname host,
1055 ;; but ANSI leaves this up to the implementor,
1056 ;; and in CMUCL we don't do it, so it must be a
1057 ;; logical host.
1058 (find-logical-host host))
1059 ((or null (member :unspecific))
1060 ;; CLHS says that HOST=:UNSPECIFIC has
1061 ;; implementation-defined behavior. We
1062 ;; just turn it into NIL.
1063 nil)
1064 (list
1065 ;; ANSI also allows LISTs to designate hosts,
1066 ;; but leaves its interpretation
1067 ;; implementation-defined. Our interpretation
1068 ;; is that it's unsupported.:-|
1069 (error "A LIST representing a pathname host is not ~
1070 supported in this implementation:~% ~S"
1071 host))
1072 (host
1073 host))))
1074 (declare (type (or null host) host))
1075 (with-pathname (defaults defaults)
1076 (etypecase thing
1077 (simple-string
1078 (%parse-namestring thing host defaults start end junk-allowed))
1079 (string
1080 (%parse-namestring (coerce thing 'simple-string)
1081 host defaults start end junk-allowed))
1082 (pathname
1083 (let ((host (if host host (%pathname-host defaults))))
1084 (unless (eq host (%pathname-host thing))
1085 (error "Hosts do not match: ~S and ~S."
1086 host (%pathname-host thing))))
1087 (values thing start))
1088 (stream
1089 (let ((name (file-name thing)))
1090 (unless name
1091 (error 'simple-type-error
1092 :datum thing
1093 :expected-type 'pathname
1094 :format-control "Can't figure out the file associated with stream:~% ~S"
1095 :format-arguments (list thing)))
1096 (values name nil)))))))
1097
1098
1099 ;;; NAMESTRING -- Interface
1100 ;;;
1101 (defun namestring (pathname)
1102 "Construct the full (name)string form of the pathname."
1103 (declare (type path-designator pathname)
1104 (values (or null simple-base-string)))
1105 (with-pathname (pathname pathname)
1106 (when pathname
1107 (let ((host (or (%pathname-host pathname)
1108 ;; Is this what we really want? Does a NIL host
1109 ;; really mean to get it from *d-p-d* and, if
1110 ;; that's NIL, use *unix-host*?
1111 (%pathname-host *default-pathname-defaults*)
1112 *unix-host*)
1113 ))
1114 (unless host
1115 (error "Cannot determine the namestring for pathnames with no ~
1116 host:~% ~S" pathname))
1117 (funcall (host-unparse host) pathname)))))
1118
1119
1120 ;;; HOST-NAMESTRING -- Interface
1121 ;;;
1122 (defun host-namestring (pathname)
1123 "Returns a string representation of the name of the host in the pathname."
1124 (declare (type path-designator pathname)
1125 (values (or null simple-base-string)))
1126 (with-pathname (pathname pathname)
1127 (let ((host (%pathname-host pathname)))
1128 (if host
1129 (funcall (host-unparse-host host) pathname)
1130 (error
1131 "Cannot determine the namestring for pathnames with no host:~% ~S"
1132 pathname)))))
1133
1134 ;;; DIRECTORY-NAMESTRING -- Interface
1135 ;;;
1136 (defun directory-namestring (pathname)
1137 "Returns a string representation of the directories used in the pathname."
1138 (declare (type path-designator pathname)
1139 (values (or null simple-base-string)))
1140 (with-pathname (pathname pathname)
1141 (let ((host (%pathname-host pathname)))
1142 (if host
1143 (funcall (host-unparse-directory host) pathname)
1144 (error
1145 "Cannot determine the namestring for pathnames with no host:~% ~S"
1146 pathname)))))
1147
1148 ;;; FILE-NAMESTRING -- Interface
1149 ;;;
1150 (defun file-namestring (pathname)
1151 "Returns a string representation of the name used in the pathname."
1152 (declare (type path-designator pathname)
1153 (values (or null simple-base-string)))
1154 (with-pathname (pathname pathname)
1155 (let ((host (%pathname-host pathname)))
1156 (if host
1157 (funcall (host-unparse-file host) pathname)
1158 (error
1159 "Cannot determine the namestring for pathnames with no host:~% ~S"
1160 pathname)))))
1161
1162 ;;; ENOUGH-NAMESTRING -- Interface
1163 ;;;
1164 (defun enough-namestring (pathname
1165 &optional (defaults *default-pathname-defaults*))
1166 "Returns an abbreviated pathname sufficent to identify the pathname relative
1167 to the defaults."
1168 (declare (type path-designator pathname defaults))
1169 (with-pathname (pathname pathname)
1170 (let ((host (%pathname-host pathname)))
1171 (if host
1172 (with-pathname (defaults defaults)
1173 ;; Give up if the hosts are different. I (rtoy) don't
1174 ;; think it makes sense to do anything if the hosts are
1175 ;; different.
1176 (if (equal host (%pathname-host defaults))
1177 (funcall (host-unparse-enough host) pathname defaults)
1178 (namestring pathname)))
1179 (error
1180 "Cannot determine the namestring for pathnames with no host:~% ~S"
1181 pathname)))))
1182
1183
1184 ;;;; Wild pathnames.
1185
1186 ;;; WILD-PATHNAME-P -- Interface
1187 ;;;
1188 (defun wild-pathname-p (pathname &optional field-key)
1189 "Predicate for determining whether pathname contains any wildcards."
1190 (declare (type path-designator pathname)
1191 (type (member nil :host :device :directory :name :type :version)
1192 field-key))
1193 (with-pathname (pathname pathname)
1194 (flet ((frob (x)
1195 (or (pattern-p x) (member x '(:wild :wild-inferiors)))))
1196 (ecase field-key
1197 ((nil)
1198 (or (wild-pathname-p pathname :host)
1199 (wild-pathname-p pathname :device)
1200 (wild-pathname-p pathname :directory)
1201 (wild-pathname-p pathname :name)
1202 (wild-pathname-p pathname :type)
1203 (wild-pathname-p pathname :version)))
1204 (:host (frob (%pathname-host pathname)))
1205 (:device (frob (%pathname-device pathname)))
1206 (:directory (some #'frob (%pathname-directory pathname)))
1207 (:name (frob (%pathname-name pathname)))
1208 (:type (frob (%pathname-type pathname)))
1209 (:version (frob (%pathname-version pathname)))))))
1210
1211
1212 ;;; PATHNAME-MATCH-P -- Interface
1213 ;;;
1214 (defun pathname-match-p (in-pathname in-wildname)
1215 "Pathname matches the wildname template?"
1216 (declare (type path-designator in-pathname)
1217 ;; Not path-designator because a file-stream can't have a
1218 ;; wild pathname.
1219 (type (or string pathname) in-wildname))
1220 (with-pathname (pathname in-pathname)
1221 (with-pathname (wildname in-wildname)
1222 (macrolet ((frob (field &optional (op 'components-match ))
1223 `(or (null (,field wildname))
1224 (,op (,field pathname) (,field wildname)))))
1225 (and (or (null (%pathname-host wildname))
1226 (eq (%pathname-host wildname) (%pathname-host pathname)))
1227 (frob %pathname-device)
1228 (frob %pathname-directory directory-components-match)
1229 (frob %pathname-name)
1230 (frob %pathname-type)
1231 (frob %pathname-version))))))
1232
1233
1234 ;;; SUBSTITUTE-INTO -- Internal
1235 ;;;
1236 ;;; Place the substitutions into the pattern and return the string or pattern
1237 ;;; that results. If DIDDLE-CASE is true, we diddle the result case as well,
1238 ;;; in case we are translating between hosts with difference conventional case.
1239 ;;; The second value is the tail of subs with all of the values that we used up
1240 ;;; stripped off. Note that PATTERN-MATCHES matches all consecutive wildcards
1241 ;;; as a single string, so we ignore subsequent contiguous wildcards.
1242 ;;;
1243 (defun substitute-into (pattern subs diddle-case)
1244 (declare (type pattern pattern)
1245 (type list subs)
1246 (values (or simple-base-string pattern) list))
1247 (let ((in-wildcard nil)
1248 (pieces nil)
1249 (strings nil))
1250 (dolist (piece (pattern-pieces pattern))
1251 (cond ((simple-string-p piece)
1252 (push piece strings)
1253 (setf in-wildcard nil))
1254 (in-wildcard)
1255 (t
1256 (setf in-wildcard t)
1257 (unless subs
1258 (error "Not enough wildcards in FROM pattern to match ~
1259 TO pattern:~% ~S"
1260 pattern))
1261 (let ((sub (pop subs)))
1262 (typecase sub
1263 (pattern
1264 (when strings
1265 (push (apply #'concatenate 'simple-string
1266 (nreverse strings))
1267 pieces))
1268 (dolist (piece (pattern-pieces sub))
1269 (push piece pieces)))
1270 (simple-string
1271 (push sub strings))
1272 (t
1273 (error "Can't substitute this into the middle of a word:~
1274 ~% ~S"
1275 sub)))))))
1276
1277 (when strings
1278 (push (apply #'concatenate 'simple-string (nreverse strings))
1279 pieces))
1280 (values
1281 (maybe-diddle-case
1282 (if (and pieces (simple-string-p (car pieces)) (null (cdr pieces)))
1283 (car pieces)
1284 (make-pattern (nreverse pieces)))
1285 diddle-case)
1286 subs)))
1287
1288
1289 ;;; DIDNT-MATCH-ERROR -- Internal
1290 ;;;
1291 ;;; Called when we can't see how source and from matched.
1292 ;;;
1293 (defun didnt-match-error (source from)
1294 (error "Pathname components from Source and From args to TRANSLATE-PATHNAME~@
1295 did not match:~% ~S ~S"
1296 source from))
1297
1298
1299 ;;; TRANSLATE-COMPONENT -- Internal
1300 ;;;
1301 ;;; Do TRANSLATE-COMPONENT for all components except host and directory.
1302 ;;;
1303 (defun translate-component (source from to diddle-case)
1304 (typecase to
1305 (pattern
1306 (typecase from
1307 (pattern
1308 (typecase source
1309 (pattern
1310 (if (pattern= from source)
1311 source
1312 (didnt-match-error source from)))
1313 (simple-string
1314 (multiple-value-bind
1315 (won subs)
1316 (pattern-matches from source)
1317 (if won
1318 (values (substitute-into to subs diddle-case))
1319 (didnt-match-error source from))))
1320 (t
1321 (maybe-diddle-case source diddle-case))))
1322 ((member :wild)
1323 (values (substitute-into to (list source) diddle-case)))
1324 (t
1325 (if (components-match source from)
1326 (maybe-diddle-case source diddle-case)
1327 (didnt-match-error source from)))))
1328 ((member nil :wild)
1329 (maybe-diddle-case source diddle-case))
1330 (t
1331 (if (components-match source from)
1332 to
1333 (didnt-match-error source from)))))
1334
1335
1336 ;;; COMPUTE-DIRECTORY-SUBSTITUTIONS -- Internal
1337 ;;;
1338 ;;; Return a list of all the things that we want to substitute into the TO
1339 ;;; pattern (the things matched by from on source.) When From contains
1340 ;;; :WILD-INFERIORS, the result contains a sublist of the matched source
1341 ;;; subdirectories.
1342 ;;;
1343 (defun compute-directory-substitutions (orig-source orig-from)
1344 (let ((source orig-source)
1345 (from orig-from))
1346 (collect ((subs))
1347 (loop
1348 (unless source
1349 (unless (every #'(lambda (x) (eq x :wild-inferiors)) from)
1350 (didnt-match-error orig-source orig-from))
1351 (subs ())
1352 (return))
1353 (unless from (didnt-match-error orig-source orig-from))
1354 (let ((from-part (pop from))
1355 (source-part (pop source)))
1356 (typecase from-part
1357 (pattern
1358 (typecase source-part
1359 (pattern
1360 (if (pattern= from-part source-part)
1361 (subs source-part)
1362 (didnt-match-error orig-source orig-from)))
1363 (simple-string
1364 (multiple-value-bind
1365 (won new-subs)
1366 (pattern-matches from-part source-part)
1367 (if won
1368 (dolist (sub new-subs)
1369 (subs sub))
1370 (didnt-match-error orig-source orig-from))))
1371 (t
1372 (didnt-match-error orig-source orig-from))))
1373 ((member :wild)
1374 (subs source-part))
1375 ((member :wild-inferiors)
1376 (let ((remaining-source (cons source-part source)))
1377 (collect ((res))
1378 (loop
1379 (when (directory-components-match remaining-source from)
1380 (return))
1381 (unless remaining-source
1382 (didnt-match-error orig-source orig-from))
1383 (res (pop remaining-source)))
1384 (subs (res))
1385 (setq source remaining-source))))
1386 (simple-string
1387 (unless (and (simple-string-p source-part)
1388 (string= from-part source-part))
1389 (didnt-match-error orig-source orig-from)))
1390 (t
1391 (didnt-match-error orig-source orig-from)))))
1392 (subs))))
1393
1394
1395 ;;; TRANSLATE-DIRECTORIES -- Internal
1396 ;;;
1397 ;;; Called by TRANSLATE-PATHNAME on the directory components of its argument
1398 ;;; pathanames to produce the result directory component. If any leaves the
1399 ;;; directory NIL, we return the source directory. The :RELATIVE or :ABSOLUTE
1400 ;;; is taken from the source directory, except if TO is :ABSOLUTE, in which
1401 ;;; case the result will be :ABSOLUTE.
1402 ;;;
1403 (defun translate-directories (source from to diddle-case)
1404 (if (not (and source to from))
1405 (let ((source (mapcar #'(lambda (x) (maybe-diddle-case x diddle-case))
1406 source)))
1407 (if (null to)
1408 source
1409 (collect ((res))
1410 (res (cond ((null source) (first to))
1411 ((eq (first to) :absolute) :absolute)
1412 (t (first source))))
1413 (let ((match (rest source)))
1414 (dolist (to-part (rest to))
1415 (cond ((eq to-part :wild)
1416 (when match
1417 (res (first match))
1418 (setf match nil)))
1419 ((eq to-part :wild-inferiors)
1420 (when match
1421 (dolist (src-part match)
1422 (res src-part))
1423 (setf match nil)))
1424 (t
1425 (res to-part)))))
1426 (res))))
1427 (collect ((res))
1428 (res (if (eq (first to) :absolute)
1429 :absolute
1430 (first source)))
1431 (let ((subs-left (compute-directory-substitutions (rest source)
1432 (rest from))))
1433 (dolist (to-part (rest to))
1434 (typecase to-part
1435 ((member :wild)
1436 (assert subs-left)
1437 (let ((match (pop subs-left)))
1438 (when (listp match)
1439 (error ":WILD-INFERIORS not paired in from and to ~
1440 patterns:~% ~S ~S" from to))
1441 (res (maybe-diddle-case match diddle-case))))
1442 ((member :wild-inferiors)
1443 (assert subs-left)
1444 (let ((match (pop subs-left)))
1445 (unless (listp match)
1446 (error ":WILD-INFERIORS not paired in from and to ~
1447 patterns:~% ~S ~S" from to))
1448 (dolist (x match)
1449 (res (maybe-diddle-case x diddle-case)))))
1450 (pattern
1451 (multiple-value-bind
1452 (new new-subs-left)
1453 (substitute-into to-part subs-left diddle-case)
1454 (setf subs-left new-subs-left)
1455 (res new)))
1456 (t (res to-part)))))
1457 (res))))
1458
1459
1460 ;;; TRANSLATE-PATHNAME -- Interface
1461 ;;;
1462 (defun translate-pathname (source from-wildname to-wildname &key)
1463 "Use the source pathname to translate the from-wildname's wild and
1464 unspecified elements into a completed to-pathname based on the to-wildname."
1465 (declare (type path-designator source from-wildname to-wildname))
1466 (with-pathname (source source)
1467 (with-pathname (from from-wildname)
1468 (with-pathname (to to-wildname)
1469 (unless (pathname-match-p source from)
1470 (didnt-match-error source from))
1471 (let* ((source-host (%pathname-host source))
1472 (to-host (%pathname-host to))
1473 (diddle-case
1474 (and source-host to-host
1475 (not (eq (host-customary-case source-host)
1476 (host-customary-case to-host))))))
1477 (macrolet ((frob (field &optional (op 'translate-component))
1478 `(let ((result (,op (,field source)
1479 (,field from)
1480 (,field to)
1481 diddle-case)))
1482 (if (eq result :error)
1483 (error "~S doesn't match ~S" source from)
1484 result))))
1485 (%make-pathname-object
1486 (or to-host source-host)
1487 (frob %pathname-device)
1488 (frob %pathname-directory translate-directories)
1489 (frob %pathname-name)
1490 (frob %pathname-type)
1491 (frob %pathname-version))))))))
1492
1493
1494 ;;;; Search lists.
1495
1496 ;;; The SEARCH-LIST structure.
1497 ;;;
1498 (defstruct (search-list
1499 (:print-function %print-search-list)
1500 (:make-load-form-fun
1501 (lambda (search-list)
1502 (values `(intern-search-list ',(search-list-name search-list))
1503 nil))))
1504 ;;
1505 ;; The name of this search-list. Always stored in lowercase.
1506 (name (required-argument) :type simple-string)
1507 ;;
1508 ;; T if this search-list has been defined. Otherwise NIL.
1509 (defined nil :type (member t nil))
1510 ;;
1511 ;; The list of expansions for this search-list. Each expansion is the list
1512 ;; of directory components to use in place of this search-list.
1513 (expansions nil :type list))
1514
1515 (defun %print-search-list (sl stream depth)
1516 (declare (ignore depth))
1517 (print-unreadable-object (sl stream :type t)
1518 (write-string (search-list-name sl) stream)))
1519
1520 ;;; *SEARCH-LISTS* -- internal.
1521 ;;;
1522 ;;; Hash table mapping search-list names to search-list structures.
1523 ;;;
1524 (defvar *search-lists* (make-hash-table :test #'equal))
1525
1526 ;;; FIND-SEARCH-LIST -- internal
1527 ;;;
1528 (defun find-search-list (name &optional (flame-not-found-p t))
1529 (let ((search-list (gethash (string-downcase name) *search-lists*)))
1530 (if search-list search-list
1531 (when flame-not-found-p
1532 (error "Search-list ~a not defined." name)))))
1533
1534 ;;; INTERN-SEARCH-LIST -- internal interface.
1535 ;;;
1536 ;;; When search-lists are encountered in namestrings, they are converted to
1537 ;;; search-list structures right then, instead of waiting until the search
1538 ;;; list used. This allows us to verify ahead of time that there are no
1539 ;;; circularities and makes expansion much quicker.
1540 ;;;
1541 (defun intern-search-list (name)
1542 (let ((name (string-downcase name)))
1543 (or (gethash name *search-lists*)
1544 (let ((new (make-search-list :name name)))
1545 (setf (gethash name *search-lists*) new)
1546 new))))
1547
1548 ;;; CLEAR-SEARCH-LIST -- public.
1549 ;;;
1550 ;;; Clear the definition. Note: we can't remove it from the hash-table
1551 ;;; because there may be pathnames still refering to it. So we just clear
1552 ;;; out the expansions and set defined to NIL.
1553 ;;;
1554 (defun clear-search-list (name)
1555 "Clear the current definition for the search-list NAME. Returns T if such
1556 a definition existed, and NIL if not."
1557 (let* ((name (string-downcase name))
1558 (search-list (gethash name *search-lists*)))
1559 (when (and search-list (search-list-defined search-list))
1560 (setf (search-list-defined search-list) nil)
1561 (setf (search-list-expansions search-list) nil)
1562 t)))
1563
1564 ;;; CLEAR-ALL-SEARCH-LISTS -- sorta public.
1565 ;;;
1566 ;;; Again, we can't actually remove the entries from the hash-table, so we
1567 ;;; just mark them as being undefined.
1568 ;;;
1569 (defun clear-all-search-lists ()
1570 "Clear the definition for all search-lists. Only use this if you know
1571 what you are doing."
1572 (maphash #'(lambda (name search-list)
1573 (declare (ignore name))
1574 (setf (search-list-defined search-list) nil)
1575 (setf (search-list-expansions search-list) nil))
1576 *search-lists*)
1577 nil)
1578
1579 ;;; EXTRACT-SEARCH-LIST -- internal.
1580 ;;;
1581 ;;; Extract the search-list from PATHNAME and return it. If PATHNAME
1582 ;;; doesn't start with a search-list, then either error (if FLAME-IF-NONE
1583 ;;; is true) or return NIL (if FLAME-IF-NONE is false).
1584 ;;;
1585 (defun extract-search-list (pathname flame-if-none)
1586 (with-pathname (pathname pathname)
1587 (let* ((directory (%pathname-directory pathname))
1588 (search-list (cadr directory)))
1589 (cond ((search-list-p search-list)
1590 search-list)
1591 (flame-if-none
1592 (error "~S doesn't start with a search-list." pathname))
1593 (t
1594 nil)))))
1595
1596 ;;; SEARCH-LIST -- public.
1597 ;;;
1598 ;;; We have to convert the internal form of the search-list back into a
1599 ;;; bunch of pathnames.
1600 ;;;
1601 (defun search-list (pathname)
1602 "Return the expansions for the search-list starting PATHNAME. If PATHNAME
1603 does not start with a search-list, then an error is signaled. If
1604 the search-list has not been defined yet, then an error is signaled.
1605 The expansion for a search-list can be set with SETF."
1606 (with-pathname (pathname pathname)
1607 (let ((search-list (extract-search-list pathname t))
1608 (host (%pathname-host pathname)))
1609 (if (search-list-defined search-list)
1610 (mapcar #'(lambda (directory)
1611 (make-pathname :host host
1612 :directory (cons :absolute directory)))
1613 (search-list-expansions search-list))
1614 (error "Search list ~S has not been defined yet." pathname)))))
1615
1616 ;;; SEARCH-LIST-DEFINED-P -- public.
1617 ;;;
1618 (defun search-list-defined-p (pathname)
1619 "Returns T if the search-list starting PATHNAME is currently defined, and
1620 NIL otherwise. An error is signaled if PATHNAME does not start with a
1621 search-list."
1622 (with-pathname (pathname pathname)
1623 (search-list-defined (extract-search-list pathname t))))
1624
1625 ;;; %SET-SEARCH-LIST -- public setf method
1626 ;;;
1627 ;;; Set the expansion for the search-list in PATHNAME. If this would result
1628 ;;; in any circularities, we flame out. If anything goes wrong, we leave the
1629 ;;; old defintion intact.
1630 ;;;
1631 (defun %set-search-list (pathname values)
1632 (let ((search-list (extract-search-list pathname t)))
1633 (labels
1634 ((check (target-list path)
1635 (when (eq search-list target-list)
1636 (error "That would result in a circularity:~% ~
1637 ~A~{ -> ~A~} -> ~A"
1638 (search-list-name search-list)
1639 (reverse path)
1640 (search-list-name target-list)))
1641 (when (search-list-p target-list)
1642 (push (search-list-name target-list) path)
1643 (dolist (expansion (search-list-expansions target-list))
1644 (check (car expansion) path))))
1645 (convert (pathname)
1646 (with-pathname (pathname pathname)
1647 (when (or (pathname-name pathname)
1648 (pathname-type pathname)
1649 (pathname-version pathname))
1650 (error "Search-lists cannot expand into pathnames that have ~
1651 a name, type, or ~%version specified:~% ~S"
1652 pathname))
1653 (let ((directory (pathname-directory pathname)))
1654 (let ((expansion
1655 (if directory
1656 (ecase (car directory)
1657 (:absolute (cdr directory))
1658 (:relative (cons (intern-search-list "default")
1659 (cdr directory))))
1660 (list (intern-search-list "default")))))
1661 (check (car expansion) nil)
1662 expansion)))))
1663 (setf (search-list-expansions search-list)
1664 (if (listp values)
1665 (mapcar #'convert values)
1666 (list (convert values)))))
1667 (setf (search-list-defined search-list) t))
1668 values)
1669
1670 ;;; ENUMERATE-SEARCH-LIST -- public.
1671 ;;;
1672 (defmacro enumerate-search-list ((var pathname &optional result) &body body)
1673 "Execute BODY with VAR bound to each successive possible expansion for
1674 PATHNAME and then return RESULT. Note: if PATHNAME does not contain a
1675 search-list, then BODY is executed exactly once. Everything is wrapped
1676 in a block named NIL, so RETURN can be used to terminate early. Note:
1677 VAR is *not* bound inside of RESULT."
1678 (let ((body-name (gensym)))
1679 `(block nil
1680 (flet ((,body-name (,var)
1681 ,@body))
1682 (%enumerate-search-list ,pathname #',body-name)
1683 ,result))))
1684
1685 (defun %enumerate-search-list (pathname function)
1686 (let* ((pathname (if (logical-pathname-p pathname)
1687 (translate-logical-pathname pathname)
1688 pathname))
1689 (search-list (extract-search-list pathname nil)))
1690 (cond
1691 ((not search-list)
1692 (funcall function pathname))
1693 ((not (search-list-defined search-list))
1694 (error "Undefined search list: ~A"
1695 (search-list-name search-list)))
1696 (t
1697 (let ((tail (cddr (pathname-directory pathname))))
1698 (dolist (expansion
1699 (search-list-expansions search-list))
1700 (%enumerate-search-list (make-pathname :defaults pathname
1701 :directory
1702 (cons :absolute
1703 (append expansion
1704 tail)))
1705 function)))))))
1706
1707
1708 ;;;; Logical pathname support. ANSI 92-102 specification.
1709 ;;;; As logical-pathname translations are loaded they are canonicalized as
1710 ;;;; patterns to enable rapid efficent translation into physical pathnames.
1711
1712 ;;;; Utilities:
1713
1714 ;;; LOGICAL-WORD-OR-LOSE -- Internal
1715 ;;;
1716 ;;; Canonicalize a logical pathanme word by uppercasing it checking that it
1717 ;;; contains only legal characters.
1718 ;;;
1719 (defun logical-word-or-lose (word)
1720 (declare (string word))
1721 (let ((word (string-upcase word)))
1722 (dotimes (i (length word))
1723 (let ((ch (schar word i)))
1724 (unless (or (alpha-char-p ch) (digit-char-p ch) (char= ch #\-))
1725 (error 'namestring-parse-error
1726 :complaint "Logical namestring character ~
1727 is not alphanumeric or hyphen:~% ~S"
1728 :arguments (list ch)
1729 :namestring word :offset i))))
1730 word))
1731
1732
1733 ;;; FIND-LOGICAL-HOST -- Internal
1734 ;;;
1735 ;;; Given a logical host or string, return a logical host. If Error-p is
1736 ;;; NIL, then return NIL when no such host exists.
1737 ;;;
1738 (defun find-logical-host (thing &optional (errorp t))
1739 (etypecase thing
1740 (string
1741 (let* ((valid-hostname
1742 (catch 'error-bailout
1743 (handler-bind
1744 ((namestring-parse-error
1745 (lambda(c)(declare (ignore c))
1746 (unless errorp
1747 (throw 'error-bailout nil)))))
1748 (logical-word-or-lose thing))))
1749 (found
1750 (and valid-hostname
1751 (or (gethash valid-hostname *logical-hosts*)
1752 (and *autoload-translations*
1753 (ignore-errors
1754 (load-logical-pathname-translations
1755 valid-hostname))
1756 (gethash valid-hostname *logical-hosts*))))))
1757 (if (or found (not errorp))
1758 found
1759 (error 'simple-file-error
1760 :pathname thing
1761 :format-control "Logical host not yet defined: ~S"
1762 :format-arguments (list thing)))))
1763 (logical-host thing)))
1764
1765 ;;; INTERN-LOGICAL-HOST -- Internal
1766 ;;;
1767 ;;; Given a logical host name or host, return a logical host, creating a new
1768 ;;; one if necessary.
1769 ;;;
1770 (defun intern-logical-host (thing)
1771 (declare (values logical-host))
1772 (or (find-logical-host thing nil)
1773 (let* ((name (logical-word-or-lose thing))
1774 (new (make-logical-host :name name)))
1775 (setf (gethash name *logical-hosts*) new)
1776 new)))
1777
1778
1779 ;;;; Logical pathname parsing:
1780
1781 ;;; MAYBE-MAKE-LOGICAL-PATTERN -- Internal
1782 ;;;
1783 ;;; Deal with multi-char wildcards in a logical pathname token.
1784 ;;;
1785 (defun maybe-make-logical-pattern (namestring chunks)
1786 (let ((chunk (caar chunks)))
1787 (collect ((pattern))
1788 (let ((last-pos 0)
1789 (len (length chunk)))
1790 (declare (fixnum last-pos))
1791 (loop
1792 (when (= last-pos len) (return))
1793 (let ((pos (or (position #\* chunk :start last-pos) len)))
1794 (if (= pos last-pos)
1795 (when (pattern)
1796 (error 'namestring-parse-error
1797 :complaint "Double asterisk inside of logical ~
1798 word: ~S"
1799 :arguments (list chunk)
1800 :namestring namestring
1801 :offset (+ (cdar chunks) pos)))
1802 (pattern (subseq chunk last-pos pos)))
1803 (if (= pos len)
1804 (return)
1805 (pattern :multi-char-wild))
1806 (setq last-pos (1+ pos)))))
1807 (assert (pattern))
1808 (if (cdr (pattern))
1809 (make-pattern (pattern))
1810 (let ((x (car (pattern))))
1811 (if (eq x :multi-char-wild)
1812 :wild
1813 x))))))
1814
1815
1816 ;;; LOGICAL-CHUNKIFY -- Internal
1817 ;;;
1818 ;;; Return a list of conses where the cdr is the start position and the car
1819 ;;; is a string (token) or character (punctuation.)
1820 ;;;
1821 (defun logical-chunkify (namestr start end)
1822 (collect ((chunks))
1823 (do ((i start (1+ i))
1824 (prev start))
1825 ((= i end)
1826 (when (> end prev)
1827 (chunks (cons (nstring-upcase (subseq namestr prev end)) prev))))
1828 (let ((ch (schar namestr i)))
1829 (unless (or (alpha-char-p ch) (digit-char-p ch)
1830 (member ch '(#\- #\*)))
1831 (when (> i prev)
1832 (chunks (cons (nstring-upcase (subseq namestr prev i)) prev)))
1833 (setq prev (1+ i))
1834 (unless (member ch '(#\; #\: #\.))
1835 (error 'namestring-parse-error
1836 :complaint "Illegal character for logical pathname:~% ~S"
1837 :arguments (list ch)
1838 :namestring namestr
1839 :offset i))
1840 (chunks (cons ch i)))))
1841 (chunks)))
1842
1843
1844 ;;; PARSE-LOGICAL-NAMESTRING -- Internal
1845 ;;;
1846 ;;; Break up a logical-namestring, always a string, into its constituent
1847 ;;; parts.
1848 ;;;
1849 (defun parse-logical-namestring (namestr start end)
1850 (declare (type simple-base-string namestr)
1851 (type index start end))
1852 (collect ((directory))
1853 (let ((host nil)
1854 (name nil)
1855 (type nil)
1856 (version nil))
1857 (labels ((expecting (what chunks)
1858 (unless (and chunks (simple-string-p (caar chunks)))
1859 (error 'namestring-parse-error
1860 :complaint "Expecting ~A, got ~:[nothing~;~:*~S~]."
1861 :arguments (list what (caar chunks))
1862 :namestring namestr
1863 :offset (if chunks (cdar chunks) end)))
1864 (caar chunks))
1865 (parse-host (chunks)
1866 (case (caadr chunks)
1867 (#\:
1868 (setq host
1869 (find-logical-host (expecting "a host name" chunks)))
1870 (parse-relative (cddr chunks)))
1871 (t
1872 (parse-relative chunks))))
1873 (parse-relative (chunks)
1874 (case (caar chunks)
1875 (#\;
1876 (directory :relative)
1877 (parse-directory (cdr chunks)))
1878 (t
1879 (directory :absolute) ; Assumption! Maybe revoked later.
1880 (parse-directory chunks))))
1881 (parse-directory (chunks)
1882 (case (caadr chunks)
1883 (#\;
1884 (directory
1885 (let ((res (expecting "a directory name" chunks)))
1886 (cond ((string= res "..") :up)
1887 ((string= res "**") :wild-inferiors)
1888 (t
1889 (maybe-make-logical-pattern namestr chunks)))))
1890 (parse-directory (cddr chunks)))
1891 (t
1892 (parse-name chunks))))
1893 (parse-name (chunks)
1894 (when chunks
1895 (expecting "a file name" chunks)
1896 (setq name (maybe-make-logical-pattern namestr chunks))
1897 (expecting-dot (cdr chunks))))
1898 (expecting-dot (chunks)
1899 (when chunks
1900 (unless (eql (caar chunks) #\.)
1901 (error 'namestring-parse-error
1902 :complaint "Expecting a dot, got ~S."
1903 :arguments (list (caar chunks))
1904 :namestring namestr
1905 :offset (cdar chunks)))
1906 (if type
1907 (parse-version (cdr chunks))
1908 (parse-type (cdr chunks)))))
1909 (parse-type (chunks)
1910 (expecting "a file type" chunks)
1911 (setq type (maybe-make-logical-pattern namestr chunks))
1912 (expecting-dot (cdr chunks)))
1913 (parse-version (chunks)
1914 (let ((str (expecting "a positive integer, * or NEWEST"
1915 chunks)))
1916 (cond
1917 ((string= str "*") (setq version :wild))
1918 ((string= str "NEWEST") (setq version :newest))
1919 (t
1920 (multiple-value-bind
1921 (res pos)
1922 (parse-integer str :junk-allowed t)
1923 (unless (and res (plusp res))
1924 (error 'namestring-parse-error
1925 :complaint "Expected a positive integer, ~
1926 got ~S"
1927 :arguments (list str)
1928 :namestring namestr
1929 :offset (+ pos (cdar chunks))))
1930 (setq version res)))))
1931 (when (cdr chunks)
1932 (error 'namestring-parse-error
1933 :complaint "Extra stuff after end of file name."
1934 :namestring namestr
1935 :offset (cdadr chunks)))))
1936 (parse-host (logical-chunkify namestr start end)))
1937 (values host :unspecific
1938 (directory)
1939 name type version))))
1940
1941
1942 ;;; Can't defvar here because not all host methods are loaded yet.
1943 (declaim (special *logical-pathname-defaults*))
1944
1945 (defun logical-pathname-namestring-p (pathspec)
1946 ;; Checks to see if pathspec is a logical pathname or not.
1947 (let ((res (parse-namestring pathspec nil *logical-pathname-defaults*)))
1948 ;; Even though *logical-pathname-defaults* has a host with name
1949 ;; BOGUS, the user can still use BOGUS as a logical host because
1950 ;; we compare with EQ here, so the users BOGUS host is never our
1951 ;; BOGUS host.
1952 (values (not (eq (%pathname-host res)
1953 (%pathname-host *logical-pathname-defaults*)))
1954 res)))
1955
1956 ;;; LOGICAL-PATHNAME -- Public
1957 ;;;
1958 (defun logical-pathname (pathspec)
1959 "Converts the pathspec argument to a logical-pathname and returns it."
1960 (declare (type (or logical-pathname string stream) pathspec)
1961 (values logical-pathname))
1962 (if (typep pathspec 'logical-pathname)
1963 pathspec
1964 (multiple-value-bind (logical-p res)
1965 (logical-pathname-namestring-p pathspec)
1966 (unless logical-p
1967 (error
1968 'simple-type-error
1969 :format-control "Logical namestring does not specify a host:~% ~S"
1970 :format-arguments (list pathspec)
1971 :datum pathspec
1972 :expected-type '(satisfies logical-pathname-namestring-p)))
1973 ;; Make sure the result is a logical pathname. This can
1974 ;; happen if the pathspec is stream, and the stream was opened
1975 ;; with a pathname instead of a logical-pathname.
1976 (check-type res logical-pathname)
1977 res)))
1978
1979
1980
1981 ;;;; Logical pathname unparsing:
1982
1983 ;;; UNPARSE-LOGICAL-DIRECTORY -- Internal
1984 ;;;
1985 (defun unparse-logical-directory (pathname)
1986 (declare (type pathname pathname))
1987 (collect ((pieces))
1988 (let ((directory (%pathname-directory pathname)))
1989 (when directory
1990 (ecase (pop directory)
1991 (:absolute) ;; Nothing special.
1992 (:relative (pieces ";")))
1993 (dolist (dir directory)
1994 (cond ((or (stringp dir) (pattern-p dir))
1995 (pieces (unparse-logical-piece dir))
1996 (pieces ";"))
1997 ((eq dir :wild)
1998 (pieces "*;"))
1999 ((eq dir :wild-inferiors)
2000 (pieces "**;"))
2001 (t
2002 (error "Invalid directory component: ~S" dir))))))
2003 (apply #'concatenate 'simple-string (pieces))))
2004
2005
2006 ;;; UNPARSE-LOGICAL-PIECE -- Internal
2007 ;;;
2008 (defun unparse-logical-piece (thing)
2009 (etypecase thing
2010 (simple-string thing)
2011 (pattern
2012 (collect ((strings))
2013 (dolist (piece (pattern-pieces thing))
2014 (etypecase piece
2015 (simple-string (strings piece))
2016 (keyword
2017 (cond ((eq piece :wild-inferiors)
2018 (strings "**"))
2019 ((eq piece :multi-char-wild)
2020 (strings "*"))
2021 (t (error "Invalid keyword: ~S" piece))))))
2022 (apply #'concatenate 'simple-string (strings))))))
2023
2024 ;;; UNPARSE-ENOUGH-NAMESTRING -- Internal
2025 ;;;
2026 (defun unparse-enough-namestring (pathname defaults)
2027 (let* ((path-dir (pathname-directory pathname))
2028 (def-dir (pathname-directory defaults))
2029 (enough-dir
2030 ;; Go down the directory lists to see what matches. What's
2031 ;; left is what we want, more or less. But there has to be
2032 ;; something in common.
2033 (cond ((and (eq (first path-dir) (first def-dir))
2034 (eq (first path-dir) :absolute)
2035 (second path-dir)
2036 (second def-dir)
2037 (equal (second path-dir) (second def-dir)))
2038 ;; Both paths are :absolute, so find where the common
2039 ;; parts end and return what's left
2040 (do* ((p (rest path-dir) (rest p))
2041 (d (rest def-dir) (rest d)))
2042 ((or (endp p) (endp d)
2043 (not (equal (first p) (first d))))
2044 `(:relative ,@p))))
2045 (t
2046 ;; Both paths are absolute, but there's nothing in
2047 ;; common, so return the original. Or one path is
2048 ;; :relative, so just return the original path. If
2049 ;; the original path is :relative, then that's the
2050 ;; right one. If PATH-DIR is :absolute, we want to
2051 ;; return that except when DEF-DIR is :absolute, as
2052 ;; handled above. so return the original directory.
2053 path-dir))))
2054 (namestring (make-pathname :host (%pathname-host pathname)
2055 :directory enough-dir
2056 :name (pathname-name pathname)
2057 :type (pathname-type pathname)
2058 :version (pathname-version pathname)))))
2059
2060 ;;; UNPARSE-LOGICAL-NAMESTRING -- Internal
2061 ;;;
2062 (defun unparse-logical-namestring (pathname)
2063 (declare (type logical-pathname pathname))
2064 (concatenate 'simple-string
2065 (logical-host-name (%pathname-host pathname)) ":"
2066 (unparse-logical-directory pathname)
2067 (unparse-unix-file pathname)))
2068
2069
2070 ;;;; Logical pathname translations:
2071
2072 ;;; CANONICALIZE-LOGICAL-PATHNAME-TRANSLATIONS -- Internal
2073 ;;;
2074 ;;; Verify that the list of translations consists of lists and prepare
2075 ;;; canonical translations (parse pathnames and expand out wildcards into
2076 ;;; patterns).
2077 ;;;
2078 (defun canonicalize-logical-pathname-translations (transl-list host)
2079 (declare (type list transl-list) (type host host)
2080 (values list))
2081 (collect ((res))
2082 (dolist (tr transl-list)
2083 (unless (and (consp tr) (= (length tr) 2))
2084 (error "Logical pathname translation is not a two-list:~% ~S"
2085 tr))
2086 (let ((from (first tr)))
2087 (res (list (if (typep from 'logical-pathname)
2088 from
2089 (parse-namestring from host))
2090 (pathname (second tr))))))
2091 (res)))
2092
2093
2094 ;;; LOGICAL-PATHNAME-TRANSLATIONS -- Public
2095 ;;;
2096 (defun logical-pathname-translations (host)
2097 "Return the (logical) host object argument's list of translations."
2098 (declare (type (or string logical-host) host)
2099 (values list))
2100 (logical-host-translations (find-logical-host host)))
2101
2102 ;;; (SETF LOGICAL-PATHNAME-TRANSLATIONS) -- Public
2103 ;;;
2104 (defun (setf logical-pathname-translations) (translations host)
2105 "Set the translations list for the logical host argument.
2106 Return translations."
2107 (declare (type (or string logical-host) host)
2108 (type list translations)
2109 (values list))
2110
2111 (let ((maybe-search-list-host (concatenate 'string host ":")))
2112 (when (and (not (logical-pathname-p (pathname maybe-search-list-host)))
2113 (search-list-defined-p maybe-search-list-host))
2114 (cerror "Clobber search-list host with logical pathname host"
2115 "~S names a CMUCL search-list"
2116 host)))
2117 (let ((host (intern-logical-host host)))
2118 (setf (logical-host-canon-transls host)
2119 (canonicalize-logical-pathname-translations translations host))
2120 (setf (logical-host-translations host) translations)))
2121
2122
2123 ;;; The search mechanism for loading pathname translations uses the CMUCL
2124 ;;; extension of search-lists. The user can add to the library: search-list
2125 ;;; using setf. The file for translations should have the name defined by
2126 ;;; the hostname (a string) and with type component "translations".
2127
2128 ;;; LOAD-LOGICAL-PATHNAME-TRANSLATIONS -- Public
2129 ;;;
2130 (defun load-logical-pathname-translations (host)
2131 "Search for a logical pathname named host, if not already defined. If already
2132 defined no attempt to find or load a definition is attempted and NIL is
2133 returned. If host is not already defined, but definition is found and loaded
2134 successfully, T is returned, else error."
2135 (declare (type string host)
2136 (values (member t nil)))
2137 (let ((*autoload-translations* nil))
2138 (unless (or (string-equal host "library")
2139 (find-logical-host host nil))
2140 (with-open-file (in-str (make-pathname :defaults "library:"
2141 :name (string-downcase host)
2142 :type "translations"))
2143 (if *load-verbose*
2144 (format *error-output*
2145 ";; Loading pathname translations from ~A~%"
2146 (namestring (truename in-str))))
2147 (setf (logical-pathname-translations host) (read in-str)))
2148 t)))
2149
2150 ;;; TRANSLATE-LOGICAL-PATHNAME -- Public
2151 ;;;
2152 (defun translate-logical-pathname (pathname &key)
2153 "Translates pathname to a physical pathname, which is returned."
2154 (declare (type path-designator pathname)
2155 (values (or null pathname)))
2156 (typecase pathname
2157 (logical-pathname
2158 (dolist (x (logical-host-canon-transls (%pathname-host pathname))
2159 (error 'simple-file-error
2160 :pathname pathname
2161 :format-control "No translation for ~S"
2162 :format-arguments (list pathname)))
2163 (destructuring-bind (from to) x
2164 (when (pathname-match-p pathname from)
2165 (return (translate-logical-pathname
2166 (translate-pathname pathname from to)))))))
2167 (pathname pathname)
2168 (stream (translate-logical-pathname (pathname pathname)))
2169 (t (translate-logical-pathname (pathname pathname)))))
2170
2171 (defvar *logical-pathname-defaults*
2172 (%make-logical-pathname (make-logical-host :name "BOGUS") :unspecific
2173 nil nil nil nil))

  ViewVC Help
Powered by ViewVC 1.1.5