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

Contents of /src/code/pathname.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.86.2.3 - (hide annotations)
Mon Mar 16 21:10:55 2009 UTC (5 years, 1 month ago) by rtoy
Branch: unicode-utf16-branch
CVS Tags: unicode-utf16-sync-label-2009-03-16, unicode-utf16-char-support-2009-03-26, unicode-utf16-char-support-2009-03-25
Changes since 1.86.2.2: +15 -6 lines
Sync to CVS HEAD, tag label-2009-03-16.

But don't sync Config.FreeBSD_gencgc, Config.x86_freebsd,
FreeBSD-os.c, and FreeBSD-os.h.  Let Alex sync those later.

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