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

Contents of /src/code/pathname.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.61 - (hide annotations)
Sat Jun 7 17:56:28 2003 UTC (10 years, 10 months ago) by toy
Branch: MAIN
Changes since 1.60: +3 -2 lines
More simple-streams updates from Paul Foley:

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

  ViewVC Help
Powered by ViewVC 1.1.5