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

Contents of /src/code/pathname.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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