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

Contents of /src/code/pathname.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5