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

Contents of /src/code/pathname.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.73 - (show annotations)
Wed May 11 13:51:09 2005 UTC (8 years, 11 months ago) by rtoy
Branch: MAIN
CVS Tags: release-19b-pre1, release-19b-pre2, snapshot-2005-07, release-19b-base, snapshot-2005-06, snapshot-2005-09, snapshot-2005-08
Branch point for: release-19b-branch
Changes since 1.72: +16 -2 lines
Supply a pathname for simple-file-error in make-pathname when we have
:absolute or :wild-inferiors followed by :up or :back.

Not exactly sure what pathname to use here since we don't accept these
pathnames.  So we use the given pathname with the :up/:back deleted.
And the format strings gives more info about what happened.

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

  ViewVC Help
Powered by ViewVC 1.1.5