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

Contents of /src/code/pathname.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5