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

Contents of /src/code/pathname.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5