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

Contents of /src/code/pathname.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.89 - (show annotations)
Sun Jan 31 16:10:35 2010 UTC (4 years, 2 months ago) by rtoy
Branch: MAIN
CVS Tags: intl-2-branch-base, pre-merge-intl-branch, snapshot-2010-03, snapshot-2010-02, intl-branch-base
Branch point for: intl-branch, intl-2-branch
Changes since 1.88: +3 -6 lines
Adjust TRANSLATE-DIRECTORIES to make the result be a relative or
absolute directory according to whether the TO directory is relative
or absolute.  Previously, the result was taken from the SOURCE
directory, unless the TO directory was absolute.

See cmucl-help, 2010/01/29.
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.89 2010/01/31 16:10:35 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 (cdr 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
649 :host (or pathname-host default-host)
650 :device (or (%pathname-device pathname)
651 (maybe-diddle-case (%pathname-device defaults)
652 diddle-case))
653 :directory (merge-directories (%pathname-directory pathname)
654 (%pathname-directory defaults)
655 diddle-case)
656 :name (or (%pathname-name pathname)
657 (maybe-diddle-case (%pathname-name defaults)
658 diddle-case))
659 :type (or (%pathname-type pathname)
660 (maybe-diddle-case (%pathname-type defaults)
661 diddle-case))
662 :version (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 ;; CLHS 19.2.2.1.2.2 says: "should receive and yield strings in
894 ;; component values"
895 ;;
896 ;; We take this to mean it applies to each component of the
897 ;; directory individually. This also matches the example in the
898 ;; entry for PATHNAME-HOST.
899 (let ((diddle-p (and (eq case :common)
900 (eq (host-customary-case
901 (%pathname-host pathname))
902 :lower))))
903 (mapcar #'(lambda (piece)
904 (maybe-diddle-case piece diddle-p))
905 (%pathname-directory pathname)))))
906
907 ;;; PATHNAME-NAME -- Interface
908 ;;;
909 (defun pathname-name (pathname &key (case :local))
910 "Accessor for the pathname's name."
911 (declare (type path-designator pathname)
912 (type (member :local :common) case))
913 (with-pathname (pathname pathname)
914 (maybe-diddle-case (%pathname-name pathname)
915 (and (eq case :common)
916 (eq (host-customary-case
917 (%pathname-host pathname))
918 :lower)))))
919
920 ;;; PATHNAME-TYPE
921 ;;;
922 (defun pathname-type (pathname &key (case :local))
923 "Accessor for the pathname's name."
924 (declare (type path-designator pathname)
925 (type (member :local :common) case))
926 (with-pathname (pathname pathname)
927 (maybe-diddle-case (%pathname-type pathname)
928 (and (eq case :common)
929 (eq (host-customary-case
930 (%pathname-host pathname))
931 :lower)))))
932
933 ;;; PATHNAME-VERSION
934 ;;;
935 (defun pathname-version (pathname)
936 "Accessor for the pathname's version."
937 (declare (type path-designator pathname))
938 (with-pathname (pathname pathname)
939 (%pathname-version pathname)))
940
941
942 ;;;; Namestrings
943
944 ;;; %PRINT-NAMESTRING-PARSE-ERROR -- Internal
945 ;;;
946 (defun %print-namestring-parse-error (condition stream)
947 (format stream "Parse error in namestring: ~?~% ~A~% ~V@T^"
948 (namestring-parse-error-complaint condition)
949 (namestring-parse-error-arguments condition)
950 (namestring-parse-error-namestring condition)
951 (namestring-parse-error-offset condition)))
952
953 (define-condition namestring-parse-error (parse-error type-error)
954 ((complaint :reader namestring-parse-error-complaint :initarg :complaint)
955 (arguments :reader namestring-parse-error-arguments :initarg :arguments
956 :initform nil)
957 (namestring :reader namestring-parse-error-namestring :initarg :namestring)
958 (offset :reader namestring-parse-error-offset :initarg :offset))
959 (:report %print-namestring-parse-error))
960
961
962 ;;; %PARSE-NAMESTRING -- Internal
963 ;;;
964 ;;; Handle the case where parse-namestring is actually parsing a namestring.
965 ;;; We pick off the :JUNK-ALLOWED case then find a host to use for parsing,
966 ;;; call the parser, then check if the host matches.
967 ;;;
968 (defun %parse-namestring (namestr host defaults start end junk-allowed)
969 (declare (type string namestr)
970 (type (or host string null) host)
971 (type pathname defaults)
972 (type index start)
973 (type (or index null) end))
974 (if junk-allowed
975 (handler-case
976 (%parse-namestring namestr host defaults start end nil)
977 (namestring-parse-error (condition)
978 (values nil (namestring-parse-error-offset condition))))
979 (let* ((end (or end (length namestr)))
980 (host (if (and host (stringp host))
981 (find-logical-host host)
982 host))
983 (default-host (%pathname-host defaults))
984 (parse-host (or host
985 (extract-logical-host-prefix namestr start end)
986 default-host)))
987 (unless parse-host
988 (error "When Host arg is not supplied, Defaults arg must ~
989 have a non-null PATHNAME-HOST."))
990
991 (multiple-value-bind
992 (new-host device directory file type version)
993 (funcall (host-parse parse-host) namestr start end)
994 (when (and host new-host (not (eq new-host host)))
995 (error "Host in namestring: ~S~@
996 does not match explicit host argument: ~S"
997 namestr host))
998 (let ((pn-host (or new-host parse-host)))
999 (values (%make-pathname-object
1000 pn-host device directory file type version)
1001 end))))))
1002
1003
1004 ;;; EXTRACT-LOGICAL-HOST-PREFIX -- Internal
1005 ;;;
1006 ;;; If namestr begins with a colon-terminated, defined, logical host, then
1007 ;;; return that host, otherwise return NIL.
1008 ;;;
1009 (defun extract-logical-host-prefix (namestr start end)
1010 (declare (type simple-base-string namestr)
1011 (type index start end)
1012 (values (or logical-host null)))
1013 (let ((colon-pos (position #\: namestr :start start :end end)))
1014 (if colon-pos
1015 (find-logical-host (nstring-upcase (subseq namestr start colon-pos))
1016 nil)
1017 nil)))
1018
1019 ;;; PARSE-NAMESTRING -- Interface
1020 ;;;
1021 (defun parse-namestring (thing
1022 &optional host (defaults *default-pathname-defaults*)
1023 &key (start 0) end junk-allowed)
1024 "Converts pathname, a pathname designator, into a pathname structure,
1025 for a physical pathname, returns the printed representation. Host may be
1026 a physical host structure or host namestring."
1027 (declare (type path-designator thing)
1028 (type (or list string host (member :unspecific)) host)
1029 (type path-designator defaults)
1030 (type index start)
1031 (type (or index null) end))
1032 ;; Generally, redundant specification of information in software,
1033 ;; whether in code or in comments, is bad. However, the ANSI spec
1034 ;; for this is messy enough that it's hard to hold in short-term
1035 ;; memory, so I've recorded these redundant notes on the
1036 ;; implications of the ANSI spec.
1037 ;;
1038 ;; According to the ANSI spec, HOST can be a valid pathname host, or
1039 ;; a logical host, or NIL.
1040 ;;
1041 ;; A valid pathname host can be a valid physical pathname host or a
1042 ;; valid logical pathname host.
1043 ;;
1044 ;; A valid physical pathname host is "any of a string, a list of
1045 ;; strings, or the symbol :UNSPECIFIC, that is recognized by the
1046 ;; implementation as the name of a host". In SBCL as of 0.6.9.8,
1047 ;; that means :UNSPECIFIC: though someday we might want to
1048 ;; generalize it to allow strings like "RTFM.MIT.EDU" or lists like
1049 ;; '("RTFM" "MIT" "EDU"), that's not supported now.
1050 ;;
1051 ;; A valid logical pathname host is a string which has been defined as
1052 ;; the name of a logical host, as with LOAD-LOGICAL-PATHNAME-TRANSLATIONS.
1053 ;;
1054 (let ((host (etypecase host
1055 ((string 0)
1056 ;; This is a special host. It's not valid as a
1057 ;; logical host, so it is a sensible thing to
1058 ;; designate the physical Unix host object. So
1059 ;; we do that.
1060 *unix-host*)
1061 (string
1062 ;; In general ANSI-compliant Common Lisps, a
1063 ;; string might also be a physical pathname host,
1064 ;; but ANSI leaves this up to the implementor,
1065 ;; and in CMUCL we don't do it, so it must be a
1066 ;; logical host.
1067 (find-logical-host host))
1068 ((or null (member :unspecific))
1069 ;; CLHS says that HOST=:UNSPECIFIC has
1070 ;; implementation-defined behavior. We
1071 ;; just turn it into NIL.
1072 nil)
1073 (list
1074 ;; ANSI also allows LISTs to designate hosts,
1075 ;; but leaves its interpretation
1076 ;; implementation-defined. Our interpretation
1077 ;; is that it's unsupported.:-|
1078 (error "A LIST representing a pathname host is not ~
1079 supported in this implementation:~% ~S"
1080 host))
1081 (host
1082 host))))
1083 (declare (type (or null host) host))
1084 (with-pathname (defaults defaults)
1085 (etypecase thing
1086 (simple-string
1087 (%parse-namestring thing host defaults start end junk-allowed))
1088 (string
1089 (%parse-namestring (coerce thing 'simple-string)
1090 host defaults start end junk-allowed))
1091 (pathname
1092 (let ((host (if host host (%pathname-host defaults))))
1093 (unless (eq host (%pathname-host thing))
1094 (error "Hosts do not match: ~S and ~S."
1095 host (%pathname-host thing))))
1096 (values thing start))
1097 (stream
1098 (let ((name (file-name thing)))
1099 (unless name
1100 (error 'simple-type-error
1101 :datum thing
1102 :expected-type 'pathname
1103 :format-control "Can't figure out the file associated with stream:~% ~S"
1104 :format-arguments (list thing)))
1105 (values name nil)))))))
1106
1107
1108 ;;; NAMESTRING -- Interface
1109 ;;;
1110 (defun namestring (pathname)
1111 "Construct the full (name)string form of the pathname."
1112 (declare (type path-designator pathname)
1113 (values (or null simple-base-string)))
1114 (with-pathname (pathname pathname)
1115 (when pathname
1116 (let ((host (or (%pathname-host pathname)
1117 ;; Is this what we really want? Does a NIL host
1118 ;; really mean to get it from *d-p-d* and, if
1119 ;; that's NIL, use *unix-host*?
1120 (%pathname-host *default-pathname-defaults*)
1121 *unix-host*)
1122 ))
1123 (unless host
1124 (error "Cannot determine the namestring for pathnames with no ~
1125 host:~% ~S" pathname))
1126 (funcall (host-unparse host) pathname)))))
1127
1128
1129 ;;; HOST-NAMESTRING -- Interface
1130 ;;;
1131 (defun host-namestring (pathname)
1132 "Returns a string representation of the name of the host in the pathname."
1133 (declare (type path-designator pathname)
1134 (values (or null simple-base-string)))
1135 (with-pathname (pathname pathname)
1136 (let ((host (%pathname-host pathname)))
1137 (if host
1138 (funcall (host-unparse-host host) pathname)
1139 (error
1140 "Cannot determine the namestring for pathnames with no host:~% ~S"
1141 pathname)))))
1142
1143 ;;; DIRECTORY-NAMESTRING -- Interface
1144 ;;;
1145 (defun directory-namestring (pathname)
1146 "Returns a string representation of the directories used in the pathname."
1147 (declare (type path-designator pathname)
1148 (values (or null simple-base-string)))
1149 (with-pathname (pathname pathname)
1150 (let ((host (%pathname-host pathname)))
1151 (if host
1152 (funcall (host-unparse-directory host) pathname)
1153 (error
1154 "Cannot determine the namestring for pathnames with no host:~% ~S"
1155 pathname)))))
1156
1157 ;;; FILE-NAMESTRING -- Interface
1158 ;;;
1159 (defun file-namestring (pathname)
1160 "Returns a string representation of the name used in the pathname."
1161 (declare (type path-designator pathname)
1162 (values (or null simple-base-string)))
1163 (with-pathname (pathname pathname)
1164 (let ((host (%pathname-host pathname)))
1165 (if host
1166 (funcall (host-unparse-file host) pathname)
1167 (error
1168 "Cannot determine the namestring for pathnames with no host:~% ~S"
1169 pathname)))))
1170
1171 ;;; ENOUGH-NAMESTRING -- Interface
1172 ;;;
1173 (defun enough-namestring (pathname
1174 &optional (defaults *default-pathname-defaults*))
1175 "Returns an abbreviated pathname sufficent to identify the pathname relative
1176 to the defaults."
1177 (declare (type path-designator pathname defaults))
1178 (with-pathname (pathname pathname)
1179 (let ((host (%pathname-host pathname)))
1180 (if host
1181 (with-pathname (defaults defaults)
1182 ;; Give up if the hosts are different. I (rtoy) don't
1183 ;; think it makes sense to do anything if the hosts are
1184 ;; different.
1185 (if (equal host (%pathname-host defaults))
1186 (funcall (host-unparse-enough host) pathname defaults)
1187 (namestring pathname)))
1188 (error
1189 "Cannot determine the namestring for pathnames with no host:~% ~S"
1190 pathname)))))
1191
1192
1193 ;;;; Wild pathnames.
1194
1195 ;;; WILD-PATHNAME-P -- Interface
1196 ;;;
1197 (defun wild-pathname-p (pathname &optional field-key)
1198 "Predicate for determining whether pathname contains any wildcards."
1199 (declare (type path-designator pathname)
1200 (type (member nil :host :device :directory :name :type :version)
1201 field-key))
1202 (with-pathname (pathname pathname)
1203 (flet ((frob (x)
1204 (or (pattern-p x) (member x '(:wild :wild-inferiors)))))
1205 (ecase field-key
1206 ((nil)
1207 (or (wild-pathname-p pathname :host)
1208 (wild-pathname-p pathname :device)
1209 (wild-pathname-p pathname :directory)
1210 (wild-pathname-p pathname :name)
1211 (wild-pathname-p pathname :type)
1212 (wild-pathname-p pathname :version)))
1213 (:host (frob (%pathname-host pathname)))
1214 (:device (frob (%pathname-device pathname)))
1215 (:directory (some #'frob (%pathname-directory pathname)))
1216 (:name (frob (%pathname-name pathname)))
1217 (:type (frob (%pathname-type pathname)))
1218 (:version (frob (%pathname-version pathname)))))))
1219
1220
1221 ;;; PATHNAME-MATCH-P -- Interface
1222 ;;;
1223 (defun pathname-match-p (in-pathname in-wildname)
1224 "Pathname matches the wildname template?"
1225 (declare (type path-designator in-pathname)
1226 ;; Not path-designator because a file-stream can't have a
1227 ;; wild pathname.
1228 (type (or string pathname) in-wildname))
1229 (with-pathname (pathname in-pathname)
1230 (with-pathname (wildname in-wildname)
1231 (macrolet ((frob (field &optional (op 'components-match ))
1232 `(or (null (,field wildname))
1233 (,op (,field pathname) (,field wildname)))))
1234 (and (or (null (%pathname-host wildname))
1235 (eq (%pathname-host wildname) (%pathname-host pathname)))
1236 (frob %pathname-device)
1237 (frob %pathname-directory directory-components-match)
1238 (frob %pathname-name)
1239 (frob %pathname-type)
1240 (frob %pathname-version))))))
1241
1242
1243 ;;; SUBSTITUTE-INTO -- Internal
1244 ;;;
1245 ;;; Place the substitutions into the pattern and return the string or pattern
1246 ;;; that results. If DIDDLE-CASE is true, we diddle the result case as well,
1247 ;;; in case we are translating between hosts with difference conventional case.
1248 ;;; The second value is the tail of subs with all of the values that we used up
1249 ;;; stripped off. Note that PATTERN-MATCHES matches all consecutive wildcards
1250 ;;; as a single string, so we ignore subsequent contiguous wildcards.
1251 ;;;
1252 (defun substitute-into (pattern subs diddle-case)
1253 (declare (type pattern pattern)
1254 (type list subs)
1255 (values (or simple-base-string pattern) list))
1256 (let ((in-wildcard nil)
1257 (pieces nil)
1258 (strings nil))
1259 (dolist (piece (pattern-pieces pattern))
1260 (cond ((simple-string-p piece)
1261 (push piece strings)
1262 (setf in-wildcard nil))
1263 (in-wildcard)
1264 (t
1265 (setf in-wildcard t)
1266 (unless subs
1267 (error "Not enough wildcards in FROM pattern to match ~
1268 TO pattern:~% ~S"
1269 pattern))
1270 (let ((sub (pop subs)))
1271 (typecase sub
1272 (pattern
1273 (when strings
1274 (push (apply #'concatenate 'simple-string
1275 (nreverse strings))
1276 pieces))
1277 (dolist (piece (pattern-pieces sub))
1278 (push piece pieces)))
1279 (simple-string
1280 (push sub strings))
1281 (t
1282 (error "Can't substitute this into the middle of a word:~
1283 ~% ~S"
1284 sub)))))))
1285
1286 (when strings
1287 (push (apply #'concatenate 'simple-string (nreverse strings))
1288 pieces))
1289 (values
1290 (maybe-diddle-case
1291 (if (and pieces (simple-string-p (car pieces)) (null (cdr pieces)))
1292 (car pieces)
1293 (make-pattern (nreverse pieces)))
1294 diddle-case)
1295 subs)))
1296
1297
1298 ;;; DIDNT-MATCH-ERROR -- Internal
1299 ;;;
1300 ;;; Called when we can't see how source and from matched.
1301 ;;;
1302 (defun didnt-match-error (source from)
1303 (error "Pathname components from Source and From args to TRANSLATE-PATHNAME~@
1304 did not match:~% ~S ~S"
1305 source from))
1306
1307
1308 ;;; TRANSLATE-COMPONENT -- Internal
1309 ;;;
1310 ;;; Do TRANSLATE-COMPONENT for all components except host and directory.
1311 ;;;
1312 (defun translate-component (source from to diddle-case)
1313 (typecase to
1314 (pattern
1315 (typecase from
1316 (pattern
1317 (typecase source
1318 (pattern
1319 (if (pattern= from source)
1320 source
1321 (didnt-match-error source from)))
1322 (simple-string
1323 (multiple-value-bind
1324 (won subs)
1325 (pattern-matches from source)
1326 (if won
1327 (values (substitute-into to subs diddle-case))
1328 (didnt-match-error source from))))
1329 (t
1330 (maybe-diddle-case source diddle-case))))
1331 ((member :wild)
1332 (values (substitute-into to (list source) diddle-case)))
1333 (t
1334 (if (components-match source from)
1335 (maybe-diddle-case source diddle-case)
1336 (didnt-match-error source from)))))
1337 ((member nil :wild)
1338 (maybe-diddle-case source diddle-case))
1339 (t
1340 (if (components-match source from)
1341 to
1342 (didnt-match-error source from)))))
1343
1344
1345 ;;; COMPUTE-DIRECTORY-SUBSTITUTIONS -- Internal
1346 ;;;
1347 ;;; Return a list of all the things that we want to substitute into the TO
1348 ;;; pattern (the things matched by from on source.) When From contains
1349 ;;; :WILD-INFERIORS, the result contains a sublist of the matched source
1350 ;;; subdirectories.
1351 ;;;
1352 (defun compute-directory-substitutions (orig-source orig-from)
1353 (let ((source orig-source)
1354 (from orig-from))
1355 (collect ((subs))
1356 (loop
1357 (unless source
1358 (unless (every #'(lambda (x) (eq x :wild-inferiors)) from)
1359 (didnt-match-error orig-source orig-from))
1360 (subs ())
1361 (return))
1362 (unless from (didnt-match-error orig-source orig-from))
1363 (let ((from-part (pop from))
1364 (source-part (pop source)))
1365 (typecase from-part
1366 (pattern
1367 (typecase source-part
1368 (pattern
1369 (if (pattern= from-part source-part)
1370 (subs source-part)
1371 (didnt-match-error orig-source orig-from)))
1372 (simple-string
1373 (multiple-value-bind
1374 (won new-subs)
1375 (pattern-matches from-part source-part)
1376 (if won
1377 (dolist (sub new-subs)
1378 (subs sub))
1379 (didnt-match-error orig-source orig-from))))
1380 (t
1381 (didnt-match-error orig-source orig-from))))
1382 ((member :wild)
1383 (subs source-part))
1384 ((member :wild-inferiors)
1385 (let ((remaining-source (cons source-part source)))
1386 (collect ((res))
1387 (loop
1388 (when (directory-components-match remaining-source from)
1389 (return))
1390 (unless remaining-source
1391 (didnt-match-error orig-source orig-from))
1392 (res (pop remaining-source)))
1393 (subs (res))
1394 (setq source remaining-source))))
1395 (simple-string
1396 (unless (and (simple-string-p source-part)
1397 (string= from-part source-part))
1398 (didnt-match-error orig-source orig-from)))
1399 (t
1400 (didnt-match-error orig-source orig-from)))))
1401 (subs))))
1402
1403
1404 ;;; TRANSLATE-DIRECTORIES -- Internal
1405 ;;;
1406 ;;; Called by TRANSLATE-PATHNAME on the directory components of its argument
1407 ;;; pathanames to produce the result directory component. If any leaves the
1408 ;;; directory NIL, we return the source directory. The :RELATIVE or :ABSOLUTE
1409 ;;; is taken from the TO directory.
1410 ;;;
1411 (defun translate-directories (source from to diddle-case)
1412 (if (not (and source to from))
1413 (let ((source (mapcar #'(lambda (x) (maybe-diddle-case x diddle-case))
1414 source)))
1415 (if (null to)
1416 source
1417 (collect ((res))
1418 (res (cond ((null source) (first to))
1419 ((eq (first to) :absolute) :absolute)
1420 (t (first source))))
1421 (let ((match (rest source)))
1422 (dolist (to-part (rest to))
1423 (cond ((eq to-part :wild)
1424 (when match
1425 (res (first match))
1426 (setf match nil)))
1427 ((eq to-part :wild-inferiors)
1428 (when match
1429 (dolist (src-part match)
1430 (res src-part))
1431 (setf match nil)))
1432 (t
1433 (res to-part)))))
1434 (res))))
1435 (collect ((res))
1436 (res (first to))
1437 (let ((subs-left (compute-directory-substitutions (rest source)
1438 (rest from))))
1439 (dolist (to-part (rest to))
1440 (typecase to-part
1441 ((member :wild)
1442 (assert subs-left)
1443 (let ((match (pop subs-left)))
1444 (when (listp match)
1445 (error ":WILD-INFERIORS not paired in from and to ~
1446 patterns:~% ~S ~S" from to))
1447 (res (maybe-diddle-case match diddle-case))))
1448 ((member :wild-inferiors)
1449 (assert subs-left)
1450 (let ((match (pop subs-left)))
1451 (unless (listp match)
1452 (error ":WILD-INFERIORS not paired in from and to ~
1453 patterns:~% ~S ~S" from to))
1454 (dolist (x match)
1455 (res (maybe-diddle-case x diddle-case)))))
1456 (pattern
1457 (multiple-value-bind
1458 (new new-subs-left)
1459 (substitute-into to-part subs-left diddle-case)
1460 (setf subs-left new-subs-left)
1461 (res new)))
1462 (t (res to-part)))))
1463 (res))))
1464
1465
1466 ;;; TRANSLATE-PATHNAME -- Interface
1467 ;;;
1468 (defun translate-pathname (source from-wildname to-wildname &key)
1469 "Use the source pathname to translate the from-wildname's wild and
1470 unspecified elements into a completed to-pathname based on the to-wildname."
1471 (declare (type path-designator source from-wildname to-wildname))
1472 (with-pathname (source source)
1473 (with-pathname (from from-wildname)
1474 (with-pathname (to to-wildname)
1475 (unless (pathname-match-p source from)
1476 (didnt-match-error source from))
1477 (let* ((source-host (%pathname-host source))
1478 (to-host (%pathname-host to))
1479 (diddle-case
1480 (and source-host to-host
1481 (not (eq (host-customary-case source-host)
1482 (host-customary-case to-host))))))
1483 (macrolet ((frob (field &optional (op 'translate-component))
1484 `(let ((result (,op (,field source)
1485 (,field from)
1486 (,field to)
1487 diddle-case)))
1488 (if (eq result :error)
1489 (error "~S doesn't match ~S" source from)
1490 result))))
1491 (%make-pathname-object
1492 (or to-host source-host)
1493 (frob %pathname-device)
1494 (frob %pathname-directory translate-directories)
1495 (frob %pathname-name)
1496 (frob %pathname-type)
1497 (frob %pathname-version))))))))
1498
1499
1500 ;;;; Search lists.
1501
1502 ;;; The SEARCH-LIST structure.
1503 ;;;
1504 (defstruct (search-list
1505 (:print-function %print-search-list)
1506 (:make-load-form-fun
1507 (lambda (search-list)
1508 (values `(intern-search-list ',(search-list-name search-list))
1509 nil))))
1510 ;;
1511 ;; The name of this search-list. Always stored in lowercase.
1512 (name (required-argument) :type simple-string)
1513 ;;
1514 ;; T if this search-list has been defined. Otherwise NIL.
1515 (defined nil :type (member t nil))
1516 ;;
1517 ;; The list of expansions for this search-list. Each expansion is the list
1518 ;; of directory components to use in place of this search-list.
1519 (expansions nil :type list))
1520
1521 (defun %print-search-list (sl stream depth)
1522 (declare (ignore depth))
1523 (print-unreadable-object (sl stream :type t)
1524 (write-string (search-list-name sl) stream)))
1525
1526 ;;; *SEARCH-LISTS* -- internal.
1527 ;;;
1528 ;;; Hash table mapping search-list names to search-list structures.
1529 ;;;
1530 (defvar *search-lists* (make-hash-table :test #'equal))
1531
1532 ;;; FIND-SEARCH-LIST -- internal
1533 ;;;
1534 (defun find-search-list (name &optional (flame-not-found-p t))
1535 (let ((search-list (gethash (string-downcase name) *search-lists*)))
1536 (if search-list search-list
1537 (when flame-not-found-p
1538 (error "Search-list ~a not defined." name)))))
1539
1540 ;;; INTERN-SEARCH-LIST -- internal interface.
1541 ;;;
1542 ;;; When search-lists are encountered in namestrings, they are converted to
1543 ;;; search-list structures right then, instead of waiting until the search
1544 ;;; list used. This allows us to verify ahead of time that there are no
1545 ;;; circularities and makes expansion much quicker.
1546 ;;;
1547 (defun intern-search-list (name)
1548 (let ((name (string-downcase name)))
1549 (or (gethash name *search-lists*)
1550 (let ((new (make-search-list :name name)))
1551 (setf (gethash name *search-lists*) new)
1552 new))))
1553
1554 ;;; CLEAR-SEARCH-LIST -- public.
1555 ;;;
1556 ;;; Clear the definition. Note: we can't remove it from the hash-table
1557 ;;; because there may be pathnames still refering to it. So we just clear
1558 ;;; out the expansions and set defined to NIL.
1559 ;;;
1560 (defun clear-search-list (name)
1561 "Clear the current definition for the search-list NAME. Returns T if such
1562 a definition existed, and NIL if not."
1563 (let* ((name (string-downcase name))
1564 (search-list (gethash name *search-lists*)))
1565 (when (and search-list (search-list-defined search-list))
1566 (setf (search-list-defined search-list) nil)
1567 (setf (search-list-expansions search-list) nil)
1568 t)))
1569
1570 ;;; CLEAR-ALL-SEARCH-LISTS -- sorta public.
1571 ;;;
1572 ;;; Again, we can't actually remove the entries from the hash-table, so we
1573 ;;; just mark them as being undefined.
1574 ;;;
1575 (defun clear-all-search-lists ()
1576 "Clear the definition for all search-lists. Only use this if you know
1577 what you are doing."
1578 (maphash #'(lambda (name search-list)
1579 (declare (ignore name))
1580 (setf (search-list-defined search-list) nil)
1581 (setf (search-list-expansions search-list) nil))
1582 *search-lists*)
1583 nil)
1584
1585 ;;; EXTRACT-SEARCH-LIST -- internal.
1586 ;;;
1587 ;;; Extract the search-list from PATHNAME and return it. If PATHNAME
1588 ;;; doesn't start with a search-list, then either error (if FLAME-IF-NONE
1589 ;;; is true) or return NIL (if FLAME-IF-NONE is false).
1590 ;;;
1591 (defun extract-search-list (pathname flame-if-none)
1592 (with-pathname (pathname pathname)
1593 (let* ((directory (%pathname-directory pathname))
1594 (search-list (cadr directory)))
1595 (cond ((search-list-p search-list)
1596 search-list)
1597 (flame-if-none
1598 (error "~S doesn't start with a search-list." pathname))
1599 (t
1600 nil)))))
1601
1602 ;;; SEARCH-LIST -- public.
1603 ;;;
1604 ;;; We have to convert the internal form of the search-list back into a
1605 ;;; bunch of pathnames.
1606 ;;;
1607 (defun search-list (pathname)
1608 "Return the expansions for the search-list starting PATHNAME. If PATHNAME
1609 does not start with a search-list, then an error is signaled. If
1610 the search-list has not been defined yet, then an error is signaled.
1611 The expansion for a search-list can be set with SETF."
1612 (with-pathname (pathname pathname)
1613 (let ((search-list (extract-search-list pathname t))
1614 (host (%pathname-host pathname)))
1615 (if (search-list-defined search-list)
1616 (mapcar #'(lambda (directory)
1617 (make-pathname :host host
1618 :directory (cons :absolute directory)))
1619 (search-list-expansions search-list))
1620 (error "Search list ~S has not been defined yet." pathname)))))
1621
1622 ;;; SEARCH-LIST-DEFINED-P -- public.
1623 ;;;
1624 (defun search-list-defined-p (pathname)
1625 "Returns T if the search-list starting PATHNAME is currently defined, and
1626 NIL otherwise. An error is signaled if PATHNAME does not start with a
1627 search-list."
1628 (with-pathname (pathname pathname)
1629 (search-list-defined (extract-search-list pathname t))))
1630
1631 ;;; %SET-SEARCH-LIST -- public setf method
1632 ;;;
1633 ;;; Set the expansion for the search-list in PATHNAME. If this would result
1634 ;;; in any circularities, we flame out. If anything goes wrong, we leave the
1635 ;;; old defintion intact.
1636 ;;;
1637 (defun %set-search-list (pathname values)
1638 (let ((search-list (extract-search-list pathname t)))
1639 (labels
1640 ((check (target-list path)
1641 (when (eq search-list target-list)
1642 (error "That would result in a circularity:~% ~
1643 ~A~{ -> ~A~} -> ~A"
1644 (search-list-name search-list)
1645 (reverse path)
1646 (search-list-name target-list)))
1647 (when (search-list-p target-list)
1648 (push (search-list-name target-list) path)
1649 (dolist (expansion (search-list-expansions target-list))
1650 (check (car expansion) path))))
1651 (convert (pathname)
1652 (with-pathname (pathname pathname)
1653 (when (or (pathname-name pathname)
1654 (pathname-type pathname)
1655 (pathname-version pathname))
1656 (error "Search-lists cannot expand into pathnames that have ~
1657 a name, type, or ~%version specified:~% ~S"
1658 pathname))
1659 (let ((directory (pathname-directory pathname)))
1660 (let ((expansion
1661 (if directory
1662 (ecase (car directory)
1663 (:absolute (cdr directory))
1664 (:relative (cons (intern-search-list "default")
1665 (cdr directory))))
1666 (list (intern-search-list "default")))))
1667 (check (car expansion) nil)
1668 expansion)))))
1669 (setf (search-list-expansions search-list)
1670 (if (listp values)
1671 (mapcar #'convert values)
1672 (list (convert values)))))
1673 (setf (search-list-defined search-list) t))
1674 values)
1675
1676 ;;; ENUMERATE-SEARCH-LIST -- public.
1677 ;;;
1678 (defmacro enumerate-search-list ((var pathname &optional result) &body body)
1679 "Execute BODY with VAR bound to each successive possible expansion for
1680 PATHNAME and then return RESULT. Note: if PATHNAME does not contain a
1681 search-list, then BODY is executed exactly once. Everything is wrapped
1682 in a block named NIL, so RETURN can be used to terminate early. Note:
1683 VAR is *not* bound inside of RESULT."
1684 (let ((body-name (gensym)))
1685 `(block nil
1686 (flet ((,body-name (,var)
1687 ,@body))
1688 (%enumerate-search-list ,pathname #',body-name)
1689 ,result))))
1690
1691 (defun %enumerate-search-list (pathname function)
1692 (let* ((pathname (if (logical-pathname-p pathname)
1693 (translate-logical-pathname pathname)
1694 pathname))
1695 (search-list (extract-search-list pathname nil)))
1696 (cond
1697 ((not search-list)
1698 (funcall function pathname))
1699 ((not (search-list-defined search-list))
1700 (error "Undefined search list: ~A"
1701 (search-list-name search-list)))
1702 (t
1703 (let ((tail (cddr (pathname-directory pathname))))
1704 (dolist (expansion
1705 (search-list-expansions search-list))
1706 (%enumerate-search-list (make-pathname :defaults pathname
1707 :directory
1708 (cons :absolute
1709 (append expansion
1710 tail)))
1711 function)))))))
1712
1713
1714 ;;;; Logical pathname support. ANSI 92-102 specification.
1715 ;;;; As logical-pathname translations are loaded they are canonicalized as
1716 ;;;; patterns to enable rapid efficent translation into physical pathnames.
1717
1718 ;;;; Utilities:
1719
1720 ;;; LOGICAL-WORD-OR-LOSE -- Internal
1721 ;;;
1722 ;;; Canonicalize a logical pathanme word by uppercasing it checking that it
1723 ;;; contains only legal characters.
1724 ;;;
1725 (defun logical-word-or-lose (word)
1726 (declare (string word))
1727 (let ((word (string-upcase word)))
1728 (dotimes (i (length word))
1729 (let ((ch (schar word i)))
1730 (unless (or (alpha-char-p ch) (digit-char-p ch) (char= ch #\-))
1731 (error 'namestring-parse-error
1732 :complaint "Logical namestring character ~
1733 is not alphanumeric or hyphen:~% ~S"
1734 :arguments (list ch)
1735 :namestring word :offset i))))
1736 word))
1737
1738
1739 ;;; FIND-LOGICAL-HOST -- Internal
1740 ;;;
1741 ;;; Given a logical host or string, return a logical host. If Error-p is
1742 ;;; NIL, then return NIL when no such host exists.
1743 ;;;
1744 (defun find-logical-host (thing &optional (errorp t))
1745 (etypecase thing
1746 (string
1747 (let* ((valid-hostname
1748 (catch 'error-bailout
1749 (handler-bind
1750 ((namestring-parse-error
1751 (lambda(c)(declare (ignore c))
1752 (unless errorp
1753 (throw 'error-bailout nil)))))
1754 (logical-word-or-lose thing))))
1755 (found
1756 (and valid-hostname
1757 (or (gethash valid-hostname *logical-hosts*)
1758 (and *autoload-translations*
1759 (ignore-errors
1760 (load-logical-pathname-translations
1761 valid-hostname))
1762 (gethash valid-hostname *logical-hosts*))))))
1763 (if (or found (not errorp))
1764 found
1765 (error 'simple-file-error
1766 :pathname thing
1767 :format-control "Logical host not yet defined: ~S"
1768 :format-arguments (list thing)))))
1769 (logical-host thing)))
1770
1771 ;;; INTERN-LOGICAL-HOST -- Internal
1772 ;;;
1773 ;;; Given a logical host name or host, return a logical host, creating a new
1774 ;;; one if necessary.
1775 ;;;
1776 (defun intern-logical-host (thing)
1777 (declare (values logical-host))
1778 (or (find-logical-host thing nil)
1779 (let* ((name (logical-word-or-lose thing))
1780 (new (make-logical-host :name name)))
1781 (setf (gethash name *logical-hosts*) new)
1782 new)))
1783
1784
1785 ;;;; Logical pathname parsing:
1786
1787 ;;; MAYBE-MAKE-LOGICAL-PATTERN -- Internal
1788 ;;;
1789 ;;; Deal with multi-char wildcards in a logical pathname token.
1790 ;;;
1791 (defun maybe-make-logical-pattern (namestring chunks)
1792 (let ((chunk (caar chunks)))
1793 (collect ((pattern))
1794 (let ((last-pos 0)
1795 (len (length chunk)))
1796 (declare (fixnum last-pos))
1797 (loop
1798 (when (= last-pos len) (return))
1799 (let ((pos (or (position #\* chunk :start last-pos) len)))
1800 (if (= pos last-pos)
1801 (when (pattern)
1802 (error 'namestring-parse-error
1803 :complaint "Double asterisk inside of logical ~
1804 word: ~S"
1805 :arguments (list chunk)
1806 :namestring namestring
1807 :offset (+ (cdar chunks) pos)))
1808 (pattern (subseq chunk last-pos pos)))
1809 (if (= pos len)
1810 (return)
1811 (pattern :multi-char-wild))
1812 (setq last-pos (1+ pos)))))
1813 (assert (pattern))
1814 (if (cdr (pattern))
1815 (make-pattern (pattern))
1816 (let ((x (car (pattern))))
1817 (if (eq x :multi-char-wild)
1818 :wild
1819 x))))))
1820
1821
1822 ;;; LOGICAL-CHUNKIFY -- Internal
1823 ;;;
1824 ;;; Return a list of conses where the cdr is the start position and the car
1825 ;;; is a string (token) or character (punctuation.)
1826 ;;;
1827 (defun logical-chunkify (namestr start end)
1828 (collect ((chunks))
1829 (do ((i start (1+ i))
1830 (prev start))
1831 ((= i end)
1832 (when (> end prev)
1833 (chunks (cons (nstring-upcase (subseq namestr prev end)) prev))))
1834 (let ((ch (schar namestr i)))
1835 (unless (or (alpha-char-p ch) (digit-char-p ch)
1836 (member ch '(#\- #\*)))
1837 (when (> i prev)
1838 (chunks (cons (nstring-upcase (subseq namestr prev i)) prev)))
1839 (setq prev (1+ i))
1840 (unless (member ch '(#\; #\: #\.))
1841 (error 'namestring-parse-error
1842 :complaint "Illegal character for logical pathname:~% ~S"
1843 :arguments (list ch)
1844 :namestring namestr
1845 :offset i))
1846 (chunks (cons ch i)))))
1847 (chunks)))
1848
1849
1850 ;;; PARSE-LOGICAL-NAMESTRING -- Internal
1851 ;;;
1852 ;;; Break up a logical-namestring, always a string, into its constituent
1853 ;;; parts.
1854 ;;;
1855 (defun parse-logical-namestring (namestr start end)
1856 (declare (type simple-base-string namestr)
1857 (type index start end))
1858 (collect ((directory))
1859 (let ((host nil)
1860 (name nil)
1861 (type nil)
1862 (version nil))
1863 (labels ((expecting (what chunks)
1864 (unless (and chunks (simple-string-p (caar chunks)))
1865 (error 'namestring-parse-error
1866 :complaint "Expecting ~A, got ~:[nothing~;~:*~S~]."
1867 :arguments (list what (caar chunks))
1868 :namestring namestr
1869 :offset (if chunks (cdar chunks) end)))
1870 (caar chunks))
1871 (parse-host (chunks)
1872 (case (caadr chunks)
1873 (#\:
1874 (setq host
1875 (find-logical-host (expecting "a host name" chunks)))
1876 (parse-relative (cddr chunks)))
1877 (t
1878 (parse-relative chunks))))
1879 (parse-relative (chunks)
1880 (case (caar chunks)
1881 (#\;
1882 (directory :relative)
1883 (parse-directory (cdr chunks)))
1884 (t
1885 (directory :absolute) ; Assumption! Maybe revoked later.
1886 (parse-directory chunks))))
1887 (parse-directory (chunks)
1888 (case (caadr chunks)
1889 (#\;
1890 (directory
1891 (let ((res (expecting "a directory name" chunks)))
1892 (cond ((string= res "..") :up)
1893 ((string= res "**") :wild-inferiors)
1894 (t
1895 (maybe-make-logical-pattern namestr chunks)))))
1896 (parse-directory (cddr chunks)))
1897 (t
1898 (parse-name chunks))))
1899 (parse-name (chunks)
1900 (when chunks
1901 (expecting "a file name" chunks)
1902 (setq name (maybe-make-logical-pattern namestr chunks))
1903 (expecting-dot (cdr chunks))))
1904 (expecting-dot (chunks)
1905 (when chunks
1906 (unless (eql (caar chunks) #\.)
1907 (error 'namestring-parse-error
1908 :complaint "Expecting a dot, got ~S."
1909 :arguments (list (caar chunks))
1910 :namestring namestr
1911 :offset (cdar chunks)))
1912 (if type
1913 (parse-version (cdr chunks))
1914 (parse-type (cdr chunks)))))
1915 (parse-type (chunks)
1916 (expecting "a file type" chunks)
1917 (setq type (maybe-make-logical-pattern namestr chunks))
1918 (expecting-dot (cdr chunks)))
1919 (parse-version (chunks)
1920 (let ((str (expecting "a positive integer, * or NEWEST"
1921 chunks)))
1922 (cond
1923 ((string= str "*") (setq version :wild))
1924 ((string= str "NEWEST") (setq version :newest))
1925 (t
1926 (multiple-value-bind
1927 (res pos)
1928 (parse-integer str :junk-allowed t)
1929 (unless (and res (plusp res))
1930 (error 'namestring-parse-error
1931 :complaint "Expected a positive integer, ~
1932 got ~S"
1933 :arguments (list str)
1934 :namestring namestr
1935 :offset (+ pos (cdar chunks))))
1936 (setq version res)))))
1937 (when (cdr chunks)
1938 (error 'namestring-parse-error
1939 :complaint "Extra stuff after end of file name."
1940 :namestring namestr
1941 :offset (cdadr chunks)))))
1942 (parse-host (logical-chunkify namestr start end)))
1943 (values host :unspecific
1944 (directory)
1945 name type version))))
1946
1947
1948 ;;; Can't defvar here because not all host methods are loaded yet.
1949 (declaim (special *logical-pathname-defaults*))
1950
1951 (defun logical-pathname-namestring-p (pathspec)
1952 ;; Checks to see if pathspec is a logical pathname or not.
1953 (let ((res (parse-namestring pathspec nil *logical-pathname-defaults*)))
1954 ;; Even though *logical-pathname-defaults* has a host with name
1955 ;; BOGUS, the user can still use BOGUS as a logical host because
1956 ;; we compare with EQ here, so the users BOGUS host is never our
1957 ;; BOGUS host.
1958 (values (not (eq (%pathname-host res)
1959 (%pathname-host *logical-pathname-defaults*)))
1960 res)))
1961
1962 ;;; LOGICAL-PATHNAME -- Public
1963 ;;;
1964 (defun logical-pathname (pathspec)
1965 "Converts the pathspec argument to a logical-pathname and returns it."
1966 (declare (type (or logical-pathname string stream) pathspec)
1967 (values logical-pathname))
1968 (if (typep pathspec 'logical-pathname)
1969 pathspec
1970 (multiple-value-bind (logical-p res)
1971 (logical-pathname-namestring-p pathspec)
1972 (unless logical-p
1973 (error
1974 'simple-type-error
1975 :format-control "Logical namestring does not specify a host:~% ~S"
1976 :format-arguments (list pathspec)
1977 :datum pathspec
1978 :expected-type '(satisfies logical-pathname-namestring-p)))
1979 ;; Make sure the result is a logical pathname. This can
1980 ;; happen if the pathspec is stream, and the stream was opened
1981 ;; with a pathname instead of a logical-pathname.
1982 (check-type res logical-pathname)
1983 res)))
1984
1985
1986
1987 ;;;; Logical pathname unparsing:
1988
1989 ;;; UNPARSE-LOGICAL-DIRECTORY -- Internal
1990 ;;;
1991 (defun unparse-logical-directory (pathname)
1992 (declare (type pathname pathname))
1993 (collect ((pieces))
1994 (let ((directory (%pathname-directory pathname)))
1995 (when directory
1996 (ecase (pop directory)
1997 (:absolute) ;; Nothing special.
1998 (:relative (pieces ";")))
1999 (dolist (dir directory)
2000 (cond ((or (stringp dir) (pattern-p dir))
2001 (pieces (unparse-logical-piece dir))
2002 (pieces ";"))
2003 ((eq dir :wild)
2004 (pieces "*;"))
2005 ((eq dir :wild-inferiors)
2006 (pieces "**;"))
2007 (t
2008 (error "Invalid directory component: ~S" dir))))))
2009 (apply #'concatenate 'simple-string (pieces))))
2010
2011
2012 ;;; UNPARSE-LOGICAL-PIECE -- Internal
2013 ;;;
2014 (defun unparse-logical-piece (thing)
2015 (etypecase thing
2016 (simple-string thing)
2017 (pattern
2018 (collect ((strings))
2019 (dolist (piece (pattern-pieces thing))
2020 (etypecase piece
2021 (simple-string (strings piece))
2022 (keyword
2023 (cond ((eq piece :wild-inferiors)
2024 (strings "**"))
2025 ((eq piece :multi-char-wild)
2026 (strings "*"))
2027 (t (error "Invalid keyword: ~S" piece))))))
2028 (apply #'concatenate 'simple-string (strings))))))
2029
2030 ;;; UNPARSE-ENOUGH-NAMESTRING -- Internal
2031 ;;;
2032 (defun unparse-enough-namestring (pathname defaults)
2033 (let* ((path-dir (pathname-directory pathname))
2034 (def-dir (pathname-directory defaults))
2035 (enough-dir
2036 ;; Go down the directory lists to see what matches. What's
2037 ;; left is what we want, more or less. But there has to be
2038 ;; something in common.
2039 (cond ((and (eq (first path-dir) (first def-dir))
2040 (eq (first path-dir) :absolute)
2041 (second path-dir)
2042 (second def-dir)
2043 (equal (second path-dir) (second def-dir)))
2044 ;; Both paths are :absolute, so find where the common
2045 ;; parts end and return what's left
2046 (do* ((p (rest path-dir) (rest p))
2047 (d (rest def-dir) (rest d)))
2048 ((or (endp p) (endp d)
2049 (not (equal (first p) (first d))))
2050 `(:relative ,@p))))
2051 (t
2052 ;; Both paths are absolute, but there's nothing in
2053 ;; common, so return the original. Or one path is
2054 ;; :relative, so just return the original path. If
2055 ;; the original path is :relative, then that's the
2056 ;; right one. If PATH-DIR is :absolute, we want to
2057 ;; return that except when DEF-DIR is :absolute, as
2058 ;; handled above. so return the original directory.
2059 path-dir))))
2060 (namestring (make-pathname :host (%pathname-host pathname)
2061 :directory enough-dir
2062 :name (pathname-name pathname)
2063 :type (pathname-type pathname)
2064 :version (pathname-version pathname)))))
2065
2066 ;;; UNPARSE-LOGICAL-NAMESTRING -- Internal
2067 ;;;
2068 (defun unparse-logical-namestring (pathname)
2069 (declare (type logical-pathname pathname))
2070 (concatenate 'simple-string
2071 (logical-host-name (%pathname-host pathname)) ":"
2072 (unparse-logical-directory pathname)
2073 (unparse-unix-file pathname)))
2074
2075
2076 ;;;; Logical pathname translations:
2077
2078 ;;; CANONICALIZE-LOGICAL-PATHNAME-TRANSLATIONS -- Internal
2079 ;;;
2080 ;;; Verify that the list of translations consists of lists and prepare
2081 ;;; canonical translations (parse pathnames and expand out wildcards into
2082 ;;; patterns).
2083 ;;;
2084 (defun canonicalize-logical-pathname-translations (transl-list host)
2085 (declare (type list transl-list) (type host host)
2086 (values list))
2087 (collect ((res))
2088 (dolist (tr transl-list)
2089 (unless (and (consp tr) (= (length tr) 2))
2090 (error "Logical pathname translation is not a two-list:~% ~S"
2091 tr))
2092 (let ((from (first tr)))
2093 (res (list (if (typep from 'logical-pathname)
2094 from
2095 (parse-namestring from host))
2096 (pathname (second tr))))))
2097 (res)))
2098
2099
2100 ;;; LOGICAL-PATHNAME-TRANSLATIONS -- Public
2101 ;;;
2102 (defun logical-pathname-translations (host)
2103 "Return the (logical) host object argument's list of translations."
2104 (declare (type (or string logical-host) host)
2105 (values list))
2106 (logical-host-translations (find-logical-host host)))
2107
2108 ;;; (SETF LOGICAL-PATHNAME-TRANSLATIONS) -- Public
2109 ;;;
2110 (defun (setf logical-pathname-translations) (translations host)
2111 "Set the translations list for the logical host argument.
2112 Return translations."
2113 (declare (type (or string logical-host) host)
2114 (type list translations)
2115 (values list))
2116
2117 (let ((maybe-search-list-host (concatenate 'string host ":")))
2118 (when (and (not (logical-pathname-p (pathname maybe-search-list-host)))
2119 (search-list-defined-p maybe-search-list-host))
2120 (cerror "Clobber search-list host with logical pathname host"
2121 "~S names a CMUCL search-list"
2122 host)))
2123 (let ((host (intern-logical-host host)))
2124 (setf (logical-host-canon-transls host)
2125 (canonicalize-logical-pathname-translations translations host))
2126 (setf (logical-host-translations host) translations)))
2127
2128
2129 ;;; The search mechanism for loading pathname translations uses the CMUCL
2130 ;;; extension of search-lists. The user can add to the library: search-list
2131 ;;; using setf. The file for translations should have the name defined by
2132 ;;; the hostname (a string) and with type component "translations".
2133
2134 ;;; LOAD-LOGICAL-PATHNAME-TRANSLATIONS -- Public
2135 ;;;
2136 (defun load-logical-pathname-translations (host)
2137 "Search for a logical pathname named host, if not already defined. If already
2138 defined no attempt to find or load a definition is attempted and NIL is
2139 returned. If host is not already defined, but definition is found and loaded
2140 successfully, T is returned, else error."
2141 (declare (type string host)
2142 (values (member t nil)))
2143 (let ((*autoload-translations* nil))
2144 (unless (or (string-equal host "library")
2145 (find-logical-host host nil))
2146 (with-open-file (in-str (make-pathname :defaults "library:"
2147 :name (string-downcase host)
2148 :type "translations"))
2149 (if *load-verbose*
2150 (format *error-output*
2151 ";; Loading pathname translations from ~A~%"
2152 (namestring (truename in-str))))
2153 (setf (logical-pathname-translations host) (read in-str)))
2154 t)))
2155
2156 ;;; TRANSLATE-LOGICAL-PATHNAME -- Public
2157 ;;;
2158 (defun translate-logical-pathname (pathname &key)
2159 "Translates pathname to a physical pathname, which is returned."
2160 (declare (type path-designator pathname)
2161 (values (or null pathname)))
2162 (typecase pathname
2163 (logical-pathname
2164 (dolist (x (logical-host-canon-transls (%pathname-host pathname))
2165 (error 'simple-file-error
2166 :pathname pathname
2167 :format-control "No translation for ~S"
2168 :format-arguments (list pathname)))
2169 (destructuring-bind (from to) x
2170 (when (pathname-match-p pathname from)
2171 (return (translate-logical-pathname
2172 (translate-pathname pathname from to)))))))
2173 (pathname pathname)
2174 (stream (translate-logical-pathname (pathname pathname)))
2175 (t (translate-logical-pathname (pathname pathname)))))
2176
2177 (defvar *logical-pathname-defaults*
2178 (%make-logical-pathname (make-logical-host :name "BOGUS") :unspecific
2179 nil nil nil nil))

  ViewVC Help
Powered by ViewVC 1.1.5