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

Contents of /src/code/pathname.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5