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

Contents of /src/code/pathname.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5