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

Contents of /src/code/pathname.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5