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

Contents of /src/code/pathname.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.42 - (hide annotations)
Thu Feb 22 20:49:43 2001 UTC (13 years, 2 months ago) by pw
Branch: MAIN
Changes since 1.41: +27 -19 lines
From Mike McDonald

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

  ViewVC Help
Powered by ViewVC 1.1.5