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

Contents of /src/code/pathname.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.63 - (show annotations)
Sun Jan 18 14:06:40 2004 UTC (10 years, 3 months ago) by toy
Branch: MAIN
Changes since 1.62: +14 -1 lines
o MAKE-PATHNAME signals an error for the invalid combinations (CLHS
  19.2.2.4.3):  :absolute or :wild-inferiors immediately followed by
  :back or :up.  (From Paul Dietz's ANSI tests.)

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

  ViewVC Help
Powered by ViewVC 1.1.5