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

Contents of /src/code/pathname.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5