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

Contents of /src/code/pathname.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.21 - (show annotations)
Wed Aug 4 10:40:25 1993 UTC (20 years, 8 months ago) by ram
Branch: MAIN
Changes since 1.20: +8 -17 lines
Fixed things now that FILE-NAME returns a pathname, not a namestring.
Removed value-cell hack from SEARCH-LIST structure.
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.21 1993/08/04 10:40: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 (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 (let ((name (file-name thing)))
833 (unless name
834 (error "Can't figure out the file associated with stream:~% ~S"
835 thing))
836 name))))
837
838
839 ;;; NAMESTRING -- Interface
840 ;;;
841 (defun namestring (pathname)
842 "Construct the full (name)string form of the pathname."
843 (declare (type path-designator pathname)
844 (values (or null simple-base-string)))
845 (with-pathname (pathname pathname)
846 (let ((host (%pathname-host pathname)))
847 (unless host
848 (error "Cannot determine the namestring for pathnames with no ~
849 host:~% ~S" pathname))
850 (funcall (host-unparse host) pathname))))
851
852
853 ;;; HOST-NAMESTRING -- Interface
854 ;;;
855 (defun host-namestring (pathname)
856 "Returns a string representation of the name of the host in the pathname."
857 (declare (type path-designator pathname)
858 (values (or null simple-base-string)))
859 (with-pathname (pathname pathname)
860 (let ((host (%pathname-host pathname)))
861 (if host
862 (funcall (host-unparse-host host) pathname)
863 (error
864 "Cannot determine the namestring for pathnames with no host:~% ~S"
865 pathname)))))
866
867 ;;; DIRECTORY-NAMESTRING -- Interface
868 ;;;
869 (defun directory-namestring (pathname)
870 "Returns a string representation of the directories used in the pathname."
871 (declare (type path-designator pathname)
872 (values (or null simple-base-string)))
873 (with-pathname (pathname pathname)
874 (let ((host (%pathname-host pathname)))
875 (if host
876 (funcall (host-unparse-directory host) pathname)
877 (error
878 "Cannot determine the namestring for pathnames with no host:~% ~S"
879 pathname)))))
880
881 ;;; FILE-NAMESTRING -- Interface
882 ;;;
883 (defun file-namestring (pathname)
884 "Returns a string representation of the name used in the pathname."
885 (declare (type path-designator pathname)
886 (values (or null simple-base-string)))
887 (with-pathname (pathname pathname)
888 (let ((host (%pathname-host pathname)))
889 (if host
890 (funcall (host-unparse-file host) pathname)
891 (error
892 "Cannot determine the namestring for pathnames with no host:~% ~S"
893 pathname)))))
894
895 ;;; ENOUGH-NAMESTRING -- Interface
896 ;;;
897 (defun enough-namestring (pathname
898 &optional (defaults *default-pathname-defaults*))
899 "Returns an abbreviated pathname sufficent to identify the pathname relative
900 to the defaults."
901 (declare (type path-designator pathname))
902 (with-pathname (pathname pathname)
903 (let ((host (%pathname-host pathname)))
904 (if host
905 (with-pathname (defaults defaults)
906 (funcall (host-unparse-enough host) pathname defaults))
907 (error
908 "Cannot determine the namestring for pathnames with no host:~% ~S"
909 pathname)))))
910
911
912 ;;;; Wild pathnames.
913
914 ;;; WILD-PATHNAME-P -- Interface
915 ;;;
916 (defun wild-pathname-p (pathname &optional field-key)
917 "Predicate for determining whether pathname contains any wildcards."
918 (declare (type path-designator pathname)
919 (type (member nil :host :device :directory :name :type :version)
920 field-key))
921 (with-pathname (pathname pathname)
922 (flet ((frob (x)
923 (or (pattern-p x) (member x '(:wild :wild-inferiors)))))
924 (ecase field-key
925 ((nil)
926 (or (wild-pathname-p pathname :host)
927 (wild-pathname-p pathname :device)
928 (wild-pathname-p pathname :directory)
929 (wild-pathname-p pathname :name)
930 (wild-pathname-p pathname :type)
931 (wild-pathname-p pathname :version)))
932 (:host (frob (%pathname-host pathname)))
933 (:device (frob (%pathname-host pathname)))
934 (:directory (some #'frob (%pathname-directory pathname)))
935 (:name (frob (%pathname-name pathname)))
936 (:type (frob (%pathname-type pathname)))
937 (:version (frob (%pathname-version pathname)))))))
938
939
940 ;;; PATHNAME-MATCH-P -- Interface
941 ;;;
942 (defun pathname-match-p (in-pathname in-wildname)
943 "Pathname matches the wildname template?"
944 (declare (type path-designator in-pathname))
945 (with-pathname (pathname in-pathname)
946 (with-pathname (wildname in-wildname)
947 (macrolet ((frob (field &optional (op 'components-match ))
948 `(or (null (,field wildname))
949 (,op (,field pathname) (,field wildname)))))
950 (and (or (null (%pathname-host wildname))
951 (eq (%pathname-host wildname) (%pathname-host pathname)))
952 (frob %pathname-device)
953 (frob %pathname-directory directory-components-match)
954 (frob %pathname-name)
955 (frob %pathname-type)
956 (frob %pathname-version))))))
957
958
959 ;;; SUBSTITUTE-INTO -- Internal
960 ;;;
961 ;;; Place the substitutions into the pattern and return the string or pattern
962 ;;; that results. If DIDDLE-CASE is true, we diddle the result case as well,
963 ;;; in case we are translating between hosts with difference conventional case.
964 ;;; The second value is the tail of subs with all of the values that we used up
965 ;;; stripped off. Note that PATTERN-MATCHES matches all consecutive wildcards
966 ;;; as a single string, so we ignore subsequent contiguous wildcards.
967 ;;;
968 (defun substitute-into (pattern subs diddle-case)
969 (declare (type pattern pattern)
970 (type list subs)
971 (values (or simple-base-string pattern)))
972 (let ((in-wildcard nil)
973 (pieces nil)
974 (strings nil))
975 (dolist (piece (pattern-pieces pattern))
976 (cond ((simple-string-p piece)
977 (push piece strings)
978 (setf in-wildcard nil))
979 (in-wildcard)
980 (t
981 (setf in-wildcard t)
982 (unless subs
983 (error "Not enough wildcards in FROM pattern to match ~
984 TO pattern:~% ~S"
985 pattern))
986 (let ((sub (pop subs)))
987 (typecase sub
988 (pattern
989 (when strings
990 (push (apply #'concatenate 'simple-string
991 (nreverse strings))
992 pieces))
993 (dolist (piece (pattern-pieces sub))
994 (push piece pieces)))
995 (simple-string
996 (push sub strings))
997 (t
998 (error "Can't substitute this into the middle of a word:~
999 ~% ~S"
1000 sub)))))))
1001
1002 (when strings
1003 (push (apply #'concatenate 'simple-string (nreverse strings))
1004 pieces))
1005 (values
1006 (maybe-diddle-case
1007 (if (and pieces (simple-string-p (car pieces)) (null (cdr pieces)))
1008 (car pieces)
1009 (make-pattern (nreverse pieces)))
1010 diddle-case)
1011 subs)))
1012
1013
1014 ;;; DIDNT-MATCH-ERROR -- Internal
1015 ;;;
1016 ;;; Called when we can't see how source and from matched.
1017 ;;;
1018 (defun didnt-match-error (source from)
1019 (error "Pathname components from Source and From args to TRANSLATE-PATHNAME~@
1020 did not match:~% ~S ~S"
1021 source from))
1022
1023
1024 ;;; TRANSLATE-COMPONENT -- Internal
1025 ;;;
1026 ;;; Do TRANSLATE-COMPONENT for all components except host and directory.
1027 ;;;
1028 (defun translate-component (source from to diddle-case)
1029 (typecase to
1030 (pattern
1031 (typecase from
1032 (pattern
1033 (typecase source
1034 (pattern
1035 (if (pattern= from source)
1036 source
1037 (didnt-match-error source from)))
1038 (simple-string
1039 (multiple-value-bind
1040 (won subs)
1041 (pattern-matches from source)
1042 (if won
1043 (values (substitute-into to subs diddle-case))
1044 (didnt-match-error source from))))
1045 (t
1046 (maybe-diddle-case source diddle-case))))
1047 ((member :wild)
1048 (values (substitute-into to (list source) diddle-case)))
1049 (t
1050 (if (components-match source from)
1051 (maybe-diddle-case source diddle-case)
1052 (didnt-match-error source from)))))
1053 ((member nil :wild)
1054 (maybe-diddle-case source diddle-case))
1055 (t
1056 (if (components-match source from)
1057 to
1058 (didnt-match-error source from)))))
1059
1060
1061 ;;; COMPUTE-DIRECTORY-SUBSTITUTIONS -- Internal
1062 ;;;
1063 ;;; Return a list of all the things that we want to substitute into the TO
1064 ;;; pattern (the things matched by from on source.) When From contains
1065 ;;; :WILD-INFERIORS, the result contains a sublist of the matched source
1066 ;;; subdirectories.
1067 ;;;
1068 (defun compute-directory-substitutions (orig-source orig-from)
1069 (let ((source orig-source)
1070 (from orig-from))
1071 (collect ((subs))
1072 (loop
1073 (unless source
1074 (unless (every #'(lambda (x) (eq x :wild-inferiors)) from)
1075 (didnt-match-error orig-source orig-from))
1076 (subs ())
1077 (return))
1078 (unless from (didnt-match-error orig-source orig-from))
1079 (let ((from-part (pop from))
1080 (source-part (pop source)))
1081 (typecase from-part
1082 (pattern
1083 (typecase source-part
1084 (pattern
1085 (if (pattern= from-part source-part)
1086 (subs source-part)
1087 (didnt-match-error orig-source orig-from)))
1088 (simple-string
1089 (multiple-value-bind
1090 (won new-subs)
1091 (pattern-matches from-part source-part)
1092 (if won
1093 (dolist (sub new-subs)
1094 (subs sub))
1095 (didnt-match-error orig-source orig-from))))
1096 (t
1097 (didnt-match-error orig-source orig-from))))
1098 ((member :wild)
1099 (subs source-part))
1100 ((member :wild-inferiors)
1101 (let ((remaining-source (cons source-part source)))
1102 (collect ((res))
1103 (loop
1104 (when (directory-components-match remaining-source from)
1105 (return))
1106 (unless remaining-source
1107 (didnt-match-error orig-source orig-from))
1108 (res (pop remaining-source)))
1109 (subs (res))
1110 (setq source remaining-source))))
1111 (simple-string
1112 (unless (and (simple-string-p source-part)
1113 (string= from-part source-part))
1114 (didnt-match-error orig-source orig-from)))
1115 (t
1116 (didnt-match-error orig-source orig-from)))))
1117 (subs))))
1118
1119
1120 ;;; TRANSLATE-DIRECTORIES -- Internal
1121 ;;;
1122 ;;; Called by TRANSLATE-PATHNAME on the directory components of its argument
1123 ;;; pathanames to produce the result directory component. If any leaves the
1124 ;;; directory NIL, we return the source directory. The :RELATIVE or :ABSOLUTE
1125 ;;; is always taken from the source directory.
1126 ;;;
1127 (defun translate-directories (source from to diddle-case)
1128 (if (not (and source to from))
1129 (or to
1130 (mapcar #'(lambda (x) (maybe-diddle-case x diddle-case)) source))
1131 (collect ((res))
1132 (res (first source))
1133 (let ((subs-left (compute-directory-substitutions (rest source)
1134 (rest from))))
1135 (dolist (to-part (rest to))
1136 (typecase to-part
1137 ((member :wild)
1138 (assert subs-left)
1139 (let ((match (pop subs-left)))
1140 (when (listp match)
1141 (error ":WILD-INFERIORS not paired in from and to ~
1142 patterns:~% ~S ~S" from to))
1143 (maybe-diddle-case match diddle-case)))
1144 ((member :wild-inferiors)
1145 (assert subs-left)
1146 (let ((match (pop subs-left)))
1147 (unless (listp match)
1148 (error ":WILD-INFERIORS not paired in from and to ~
1149 patterns:~% ~S ~S" from to))
1150 (dolist (x match)
1151 (res (maybe-diddle-case x diddle-case)))))
1152 (pattern
1153 (multiple-value-bind
1154 (new new-subs-left)
1155 (substitute-into to-part subs-left diddle-case)
1156 (setf subs-left new-subs-left)
1157 new))
1158 (t (res to-part)))))
1159 (res))))
1160
1161
1162 ;;; TRANSLATE-PATHNAME -- Interface
1163 ;;;
1164 (defun translate-pathname (source from-wildname to-wildname &key)
1165 "Use the source pathname to translate the from-wildname's wild and
1166 unspecified elements into a completed to-pathname based on the to-wildname."
1167 (declare (type path-designator source from-wildname to-wildname))
1168 (with-pathname (source source)
1169 (with-pathname (from from-wildname)
1170 (with-pathname (to to-wildname)
1171 (let* ((source-host (%pathname-host source))
1172 (to-host (%pathname-host to))
1173 (diddle-case
1174 (and source-host to-host
1175 (not (eq (host-customary-case source-host)
1176 (host-customary-case to-host))))))
1177 (macrolet ((frob (field &optional (op 'translate-component))
1178 `(let ((result (,op (,field source)
1179 (,field from)
1180 (,field to)
1181 diddle-case)))
1182 (if (eq result :error)
1183 (error "~S doesn't match ~S" source from)
1184 result))))
1185 (%make-pathname (or to-host source-host)
1186 (frob %pathname-device)
1187 (frob %pathname-directory translate-directories)
1188 (frob %pathname-name)
1189 (frob %pathname-type)
1190 (frob %pathname-version))))))))
1191
1192
1193 ;;;; Search lists.
1194
1195 ;;; The SEARCH-LIST structure.
1196 ;;;
1197 (defstruct (search-list
1198 (:print-function %print-search-list)
1199 (:make-load-form-fun
1200 (lambda (search-list)
1201 (values `(intern-search-list ',(search-list-name search-list))
1202 nil))))
1203 ;;
1204 ;; The name of this search-list. Always stored in lowercase.
1205 (name (required-argument) :type simple-string)
1206 ;;
1207 ;; T if this search-list has been defined. Otherwise NIL.
1208 (defined nil :type (member t nil))
1209 ;;
1210 ;; The list of expansions for this search-list. Each expansion is the list
1211 ;; of directory components to use in place of this search-list.
1212 (expansions nil :type list))
1213
1214 (defun %print-search-list (sl stream depth)
1215 (declare (ignore depth))
1216 (print-unreadable-object (sl stream :type t)
1217 (write-string (search-list-name sl) stream)))
1218
1219 ;;; *SEARCH-LISTS* -- internal.
1220 ;;;
1221 ;;; Hash table mapping search-list names to search-list structures.
1222 ;;;
1223 (defvar *search-lists* (make-hash-table :test #'equal))
1224
1225 ;;; INTERN-SEARCH-LIST -- internal interface.
1226 ;;;
1227 ;;; When search-lists are encountered in namestrings, they are converted to
1228 ;;; search-list structures right then, instead of waiting until the search
1229 ;;; list used. This allows us to verify ahead of time that there are no
1230 ;;; circularities and makes expansion much quicker.
1231 ;;;
1232 (defun intern-search-list (name)
1233 (let ((name (string-downcase name)))
1234 (or (gethash name *search-lists*)
1235 (let ((new (make-search-list :name name)))
1236 (setf (gethash name *search-lists*) new)
1237 new))))
1238
1239 ;;; CLEAR-SEARCH-LIST -- public.
1240 ;;;
1241 ;;; Clear the definition. Note: we can't remove it from the hash-table
1242 ;;; because there may be pathnames still refering to it. So we just clear
1243 ;;; out the expansions and ste defined to NIL.
1244 ;;;
1245 (defun clear-search-list (name)
1246 "Clear the current definition for the search-list NAME. Returns T if such
1247 a definition existed, and NIL if not."
1248 (let* ((name (string-downcase name))
1249 (search-list (gethash name *search-lists*)))
1250 (when (and search-list (search-list-defined search-list))
1251 (setf (search-list-defined search-list) nil)
1252 (setf (search-list-expansions search-list) nil)
1253 t)))
1254
1255 ;;; CLEAR-ALL-SEARCH-LISTS -- sorta public.
1256 ;;;
1257 ;;; Again, we can't actually remove the entries from the hash-table, so we
1258 ;;; just mark them as being undefined.
1259 ;;;
1260 (defun clear-all-search-lists ()
1261 "Clear the definition for all search-lists. Only use this if you know
1262 what you are doing."
1263 (maphash #'(lambda (name search-list)
1264 (declare (ignore name))
1265 (setf (search-list-defined search-list) nil)
1266 (setf (search-list-expansions search-list) nil))
1267 *search-lists*)
1268 nil)
1269
1270 ;;; EXTRACT-SEARCH-LIST -- internal.
1271 ;;;
1272 ;;; Extract the search-list from PATHNAME and return it. If PATHNAME
1273 ;;; doesn't start with a search-list, then either error (if FLAME-IF-NONE
1274 ;;; is true) or return NIL (if FLAME-IF-NONE is false).
1275 ;;;
1276 (defun extract-search-list (pathname flame-if-none)
1277 (with-pathname (pathname pathname)
1278 (let* ((directory (%pathname-directory pathname))
1279 (search-list (cadr directory)))
1280 (cond ((search-list-p search-list)
1281 search-list)
1282 (flame-if-none
1283 (error "~S doesn't start with a search-list." pathname))
1284 (t
1285 nil)))))
1286
1287 ;;; SEARCH-LIST -- public.
1288 ;;;
1289 ;;; We have to convert the internal form of the search-list back into a
1290 ;;; bunch of pathnames.
1291 ;;;
1292 (defun search-list (pathname)
1293 "Return the expansions for the search-list starting PATHNAME. If PATHNAME
1294 does not start with a search-list, then an error is signaled. If
1295 the search-list has not been defined yet, then an error is signaled.
1296 The expansion for a search-list can be set with SETF."
1297 (with-pathname (pathname pathname)
1298 (let ((search-list (extract-search-list pathname t))
1299 (host (pathname-host pathname)))
1300 (if (search-list-defined search-list)
1301 (mapcar #'(lambda (directory)
1302 (make-pathname :host host
1303 :directory (cons :absolute directory)))
1304 (search-list-expansions search-list))
1305 (error "Search list ~S has not been defined yet." pathname)))))
1306
1307 ;;; SEARCH-LIST-DEFINED-P -- public.
1308 ;;;
1309 (defun search-list-defined-p (pathname)
1310 "Returns T if the search-list starting PATHNAME is currently defined, and
1311 NIL otherwise. An error is signaled if PATHNAME does not start with a
1312 search-list."
1313 (with-pathname (pathname pathname)
1314 (search-list-defined (extract-search-list pathname t))))
1315
1316 ;;; %SET-SEARCH-LIST -- public setf method
1317 ;;;
1318 ;;; Set the expansion for the search-list in PATHNAME. If this would result
1319 ;;; in any circularities, we flame out. If anything goes wrong, we leave the
1320 ;;; old defintion intact.
1321 ;;;
1322 (defun %set-search-list (pathname values)
1323 (let ((search-list (extract-search-list pathname t)))
1324 (labels
1325 ((check (target-list path)
1326 (when (eq search-list target-list)
1327 (error "That would result in a circularity:~% ~
1328 ~A~{ -> ~A~} -> ~A"
1329 (search-list-name search-list)
1330 (reverse path)
1331 (search-list-name target-list)))
1332 (when (search-list-p target-list)
1333 (push (search-list-name target-list) path)
1334 (dolist (expansion (search-list-expansions target-list))
1335 (check (car expansion) path))))
1336 (convert (pathname)
1337 (with-pathname (pathname pathname)
1338 (when (or (pathname-name pathname)
1339 (pathname-type pathname)
1340 (pathname-version pathname))
1341 (error "Search-lists cannot expand into pathnames that have ~
1342 a name, type, or ~%version specified:~% ~S"
1343 pathname))
1344 (let ((directory (pathname-directory pathname)))
1345 (let ((expansion
1346 (if directory
1347 (ecase (car directory)
1348 (:absolute (cdr directory))
1349 (:relative (cons (intern-search-list "default")
1350 (cdr directory))))
1351 (list (intern-search-list "default")))))
1352 (check (car expansion) nil)
1353 expansion)))))
1354 (setf (search-list-expansions search-list)
1355 (if (listp values)
1356 (mapcar #'convert values)
1357 (list (convert values)))))
1358 (setf (search-list-defined search-list) t))
1359 values)
1360
1361 ;;; ENUMERATE-SEARCH-LIST -- public.
1362 ;;;
1363 (defmacro enumerate-search-list ((var pathname &optional result) &body body)
1364 "Execute BODY with VAR bound to each successive possible expansion for
1365 PATHNAME and then return RESULT. Note: if PATHNAME does not contain a
1366 search-list, then BODY is executed exactly once. Everything is wrapped
1367 in a block named NIL, so RETURN can be used to terminate early. Note:
1368 VAR is *not* bound inside of RESULT."
1369 (let ((body-name (gensym)))
1370 `(block nil
1371 (flet ((,body-name (,var)
1372 ,@body))
1373 (%enumerate-search-list ,pathname #',body-name)
1374 ,result))))
1375
1376 (defun %enumerate-search-list (pathname function)
1377 (let* ((pathname (translate-logical-pathname pathname))
1378 (search-list (extract-search-list pathname nil)))
1379 (cond
1380 ((not search-list)
1381 (funcall function pathname))
1382 ((not (search-list-defined search-list))
1383 (error "Undefined search list: ~A"
1384 (search-list-name search-list)))
1385 (t
1386 (let ((tail (cddr (pathname-directory pathname))))
1387 (dolist (expansion
1388 (search-list-expansions search-list))
1389 (%enumerate-search-list (make-pathname :defaults pathname
1390 :directory
1391 (cons :absolute
1392 (append expansion
1393 tail)))
1394 function)))))))
1395
1396
1397 ;;;; Logical pathname support. ANSI 92-102 specification.
1398 ;;;; As logical-pathname translations are loaded they are canonicalized as
1399 ;;;; patterns to enable rapid efficent translation into physical pathnames.
1400
1401 ;;;; Utilities:
1402
1403 ;;; LOGICAL-WORD-OR-LOSE -- Internal
1404 ;;;
1405 ;;; Canonicalize a logical pathanme word by uppercasing it checking that it
1406 ;;; contains only legal characters.
1407 ;;;
1408 (defun logical-word-or-lose (word)
1409 (declare (string word))
1410 (let ((word (string-upcase word)))
1411 (dotimes (i (length word))
1412 (let ((ch (schar word i)))
1413 (unless (or (alpha-char-p ch) (digit-char-p ch) (char= ch #\-))
1414 (error 'namestring-parse-error
1415 :complaint "Logical namestring character ~
1416 is not alphanumeric or hyphen:~% ~S"
1417 :arguments (list ch)
1418 :namestring word :offset i))))
1419 word))
1420
1421
1422 ;;; FIND-LOGICAL-HOST -- Internal
1423 ;;;
1424 ;;; Given a logical host or string, return a logical host. If Error-p is
1425 ;;; NIL, then return NIL when no such host exists.
1426 ;;;
1427 (defun find-logical-host (thing &optional (errorp t))
1428 (etypecase thing
1429 (string
1430 (let ((found (gethash (logical-word-or-lose thing)
1431 *logical-hosts*)))
1432 (if (or found (not errorp))
1433 found
1434 (error "Logical host not yet defined: ~S" thing))))
1435 (logical-host thing)))
1436
1437
1438 ;;; INTERN-LOGICAL-HOST -- Internal
1439 ;;;
1440 ;;; Given a logical host name or host, return a logical host, creating a new
1441 ;;; one if necessary.
1442 ;;;
1443 (defun intern-logical-host (thing)
1444 (declare (values logical-host))
1445 (or (find-logical-host thing nil)
1446 (let* ((name (logical-word-or-lose thing))
1447 (new (make-logical-host :name name)))
1448 (setf (gethash name *logical-hosts*) new)
1449 new)))
1450
1451
1452 ;;;; Logical pathname parsing:
1453
1454 ;;; MAYBE-MAKE-LOGICAL-PATTERN -- Internal
1455 ;;;
1456 ;;; Deal with multi-char wildcards in a logical pathname token.
1457 ;;;
1458 (defun maybe-make-logical-pattern (namestring chunks)
1459 (let ((chunk (caar chunks)))
1460 (collect ((pattern))
1461 (let ((last-pos 0)
1462 (len (length chunk)))
1463 (declare (fixnum last-pos))
1464 (loop
1465 (when (= last-pos len) (return))
1466 (let ((pos (or (position #\* chunk :start last-pos) len)))
1467 (if (= pos last-pos)
1468 (when (pattern)
1469 (error 'namestring-parse-error
1470 :complaint "Double asterisk inside of logical ~
1471 word: ~S"
1472 :arguments (list chunk)
1473 :namestring namestring
1474 :offset (+ (cdar chunks) pos)))
1475 (pattern (subseq chunk last-pos pos)))
1476 (if (= pos len)
1477 (return)
1478 (pattern :multi-char-wild))
1479 (setq last-pos (1+ pos)))))
1480 (assert (pattern))
1481 (if (cdr (pattern))
1482 (make-pattern (pattern))
1483 (let ((x (car (pattern))))
1484 (if (eq x :multi-char-wild)
1485 :wild
1486 x))))))
1487
1488
1489 ;;; LOGICAL-CHUNKIFY -- Internal
1490 ;;;
1491 ;;; Return a list of conses where the cdr is the start position and the car
1492 ;;; is a string (token) or character (punctuation.)
1493 ;;;
1494 (defun logical-chunkify (namestr start end)
1495 (collect ((chunks))
1496 (do ((i start (1+ i))
1497 (prev 0))
1498 ((= i end)
1499 (when (> end prev)
1500 (chunks (cons (nstring-upcase (subseq namestr prev end)) prev))))
1501 (let ((ch (schar namestr i)))
1502 (unless (or (alpha-char-p ch) (digit-char-p ch)
1503 (member ch '(#\- #\*)))
1504 (when (> i prev)
1505 (chunks (cons (nstring-upcase (subseq namestr prev i)) prev)))
1506 (setq prev (1+ i))
1507 (unless (member ch '(#\; #\: #\.))
1508 (error 'namestring-parse-error
1509 :complaint "Illegal character for logical pathname:~% ~S"
1510 :arguments (list ch)
1511 :namestring namestr
1512 :offset i))
1513 (chunks (cons ch i)))))
1514 (chunks)))
1515
1516
1517 ;;; PARSE-LOGICAL-NAMESTRING -- Internal
1518 ;;;
1519 ;;; Break up a logical-namestring, always a string, into its constituent
1520 ;;; parts.
1521 ;;;
1522 (defun parse-logical-namestring (namestr start end)
1523 (declare (type simple-base-string namestr)
1524 (type index start end))
1525 (collect ((directory))
1526 (let ((host nil)
1527 (name nil)
1528 (type nil)
1529 (version nil))
1530 (labels ((expecting (what chunks)
1531 (unless (and chunks (simple-string-p (caar chunks)))
1532 (error 'namestring-parse-error
1533 :complaint "Expecting ~A, got ~:[nothing~;~S~]."
1534 :arguments (list what (caar chunks))
1535 :namestring namestr
1536 :offset (if chunks (cdar chunks) end)))
1537 (caar chunks))
1538 (parse-host (chunks)
1539 (case (caadr chunks)
1540 (#\:
1541 (setq host
1542 (find-logical-host (expecting "a host name" chunks)))
1543 (parse-relative (cddr chunks)))
1544 (t
1545 (parse-relative chunks))))
1546 (parse-relative (chunks)
1547 (case (caar chunks)
1548 (#\;
1549 (directory :relative)
1550 (parse-directory (cdr chunks)))
1551 (t
1552 (directory :absolute)
1553 (parse-directory chunks))))
1554 (parse-directory (chunks)
1555 (case (caadr chunks)
1556 (#\;
1557 (directory
1558 (let ((res (expecting "a directory name" chunks)))
1559 (cond ((string= res "..") :up)
1560 ((string= res "**") :wild-inferiors)
1561 (t
1562 (maybe-make-logical-pattern namestr chunks)))))
1563 (parse-directory (cddr chunks)))
1564 (t
1565 (parse-name chunks))))
1566 (parse-name (chunks)
1567 (when chunks
1568 (expecting "a file name" chunks)
1569 (setq name (maybe-make-logical-pattern namestr chunks))
1570 (expecting-dot (cdr chunks))))
1571 (expecting-dot (chunks)
1572 (when chunks
1573 (unless (eql (caar chunks) #\.)
1574 (error 'namestring-parse-error
1575 :complaint "Expecting a dot, got ~S."
1576 :arguments (list (caar chunks))
1577 :namestring namestr
1578 :offset (cdar chunks)))
1579 (if type
1580 (parse-version (cdr chunks))
1581 (parse-type (cdr chunks)))))
1582 (parse-type (chunks)
1583 (expecting "a file type" chunks)
1584 (setq type (maybe-make-logical-pattern namestr chunks))
1585 (expecting-dot (cdr chunks)))
1586 (parse-version (chunks)
1587 (let ((str (expecting "a positive integer, * or NEWEST"
1588 chunks)))
1589 (cond
1590 ((string= str "*") (setq version :wild))
1591 ((string= str "NEWEST") (setq version :newest))
1592 (t
1593 (multiple-value-bind
1594 (res pos)
1595 (parse-integer str :junk-allowed t)
1596 (unless (and res (plusp res))
1597 (error 'namestring-parse-error
1598 :complaint "Expected a positive integer, ~
1599 got ~S"
1600 :arguments (list str)
1601 :namestring namestr
1602 :offset (+ pos (cdar chunks))))
1603 (setq version res)))))
1604 (when (cdr chunks)
1605 (error 'namestring-parse-error
1606 :complaint "Extra stuff after end of file name."
1607 :namestring namestr
1608 :offset (cdadr chunks)))))
1609 (parse-host (logical-chunkify namestr start end)))
1610 (values host :unspecific (directory) name type version))))
1611
1612
1613 ;;; Can't defvar here because not all host methods are loaded yet.
1614 (declaim (special *logical-pathname-defaults*))
1615
1616 ;;; LOGICAL-PATHNAME -- Public
1617 ;;;
1618 (defun logical-pathname (pathspec)
1619 "Converts the pathspec argument to a logical-pathname and returns it."
1620 (declare (type (or logical-pathname string stream) pathspec)
1621 (values logical-pathname))
1622 (if (typep pathspec 'logical-pathname)
1623 pathspec
1624 (let ((res (parse-namestring pathspec nil *logical-pathname-defaults*)))
1625 (when (eq (%pathname-host res)
1626 (%pathname-host *logical-pathname-defaults*))
1627 (error "Logical namestring does not specify a host:~% ~S"
1628 pathspec))
1629 res)))
1630
1631
1632 ;;;; Logical pathname unparsing:
1633
1634 ;;; UNPARSE-LOGICAL-DIRECTORY -- Internal
1635 ;;;
1636 (defun unparse-logical-directory (pathname)
1637 (declare (type pathname pathname))
1638 (collect ((pieces))
1639 (let ((directory (%pathname-directory pathname)))
1640 (when directory
1641 (ecase (pop directory)
1642 (:absolute) ;; Nothing special.
1643 (:relative (pieces ";")))
1644 (dolist (dir directory)
1645 (cond ((or (stringp dir) (pattern-p dir))
1646 (pieces (unparse-logical-piece dir))
1647 (pieces ";"))
1648 ((eq dir :wild)
1649 (pieces "*;"))
1650 ((eq dir :wild-inferiors)
1651 (pieces "**;"))
1652 (t
1653 (error "Invalid directory component: ~S" dir))))))
1654 (apply #'concatenate 'simple-string (pieces))))
1655
1656
1657 ;;; UNPARSE-LOGICAL-PIECE -- Internal
1658 ;;;
1659 (defun unparse-logical-piece (thing)
1660 (etypecase thing
1661 (simple-string thing)
1662 (pattern
1663 (collect ((strings))
1664 (dolist (piece (pattern-pieces thing))
1665 (etypecase piece
1666 (simple-string (strings piece))
1667 (keyword
1668 (cond ((eq piece :wild-inferiors)
1669 (strings "**"))
1670 ((eq piece :multi-char-wild)
1671 (strings "*"))
1672 (t (error "Invalid keyword: ~S" piece))))))
1673 (apply #'concatenate 'simple-string (strings))))))
1674
1675
1676 ;;; UNPARSE-LOGICAL-NAMESTRING -- Internal
1677 ;;;
1678 (defun unparse-logical-namestring (pathname)
1679 (declare (type logical-pathname pathname))
1680 (concatenate 'simple-string
1681 (logical-host-name (%pathname-host pathname)) ":"
1682 (unparse-logical-directory pathname)
1683 (unparse-unix-file pathname)))
1684
1685
1686 ;;;; Logical pathname translations:
1687
1688 ;;; CANONICALIZE-LOGICAL-PATHNAME-TRANSLATIONS -- Internal
1689 ;;;
1690 ;;; Verify that the list of translations consists of lists and prepare
1691 ;;; canonical translations (parse pathnames and expand out wildcards into
1692 ;;; patterns).
1693 ;;;
1694 (defun canonicalize-logical-pathname-translations (transl-list host)
1695 (declare (type list transl-list) (type host host)
1696 (values list))
1697 (collect ((res))
1698 (dolist (tr transl-list)
1699 (unless (and (consp tr) (= (length tr) 2))
1700 (error "Logical pathname translation is not a two-list:~% ~S"
1701 tr))
1702 (let ((from (first tr)))
1703 (res (list (if (typep from 'logical-pathname)
1704 from
1705 (parse-namestring from host))
1706 (pathname (second tr))))))
1707 (res)))
1708
1709
1710 ;;; LOGICAL-PATHNAME-TRANSLATIONS -- Public
1711 ;;;
1712 (defun logical-pathname-translations (host)
1713 "Return the (logical) host object argument's list of translations."
1714 (declare (type (or string logical-host) host)
1715 (values list))
1716 (logical-host-translations (find-logical-host host)))
1717
1718
1719 ;;; (SETF LOGICAL-PATHNAME-TRANSLATIONS) -- Public
1720 ;;;
1721 (defun (setf logical-pathname-translations) (translations host)
1722 "Set the translations list for the logical host argument.
1723 Return translations."
1724 (declare (type (or string logical-host) host)
1725 (type list translations)
1726 (values list))
1727
1728 (let ((host (intern-logical-host host)))
1729 (setf (logical-host-canon-transls host)
1730 (canonicalize-logical-pathname-translations translations host))
1731 (setf (logical-host-translations host) translations)))
1732
1733
1734 ;;; The search mechanism for loading pathname translations uses the CMUCL
1735 ;;; extension of search-lists. The user can add to the library: search-list
1736 ;;; using setf. The file for translations should have the name defined by
1737 ;;; the hostname (a string) and with type component "translations".
1738
1739 ;;; LOAD-LOGICAL-PATHNAME-TRANSLATIONS -- Public
1740 ;;;
1741 (defun load-logical-pathname-translations (host)
1742 "Search for a logical pathname named host, if not already defined. If already
1743 defined no attempt to find or load a definition is attempted and NIL is
1744 returned. If host is not already defined, but definition is found and loaded
1745 successfully, T is returned, else error."
1746 (declare (type string host)
1747 (values (member t nil)))
1748 (unless (find-logical-host host nil)
1749 (with-open-file (in-str (make-pathname :defaults "library:"
1750 :name host
1751 :type "translations"))
1752 (format *error-output*
1753 ";; Loading pathname translations from ~A~%"
1754 (namestring (truename in-str)))
1755 (setf (logical-pathname-translations host) (read in-str)))
1756 t))
1757
1758
1759 ;;; TRANSLATE-LOGICAL-PATHNAME -- Public
1760 ;;;
1761 (defun translate-logical-pathname (pathname &key)
1762 "Translates pathname to a physical pathname, which is returned."
1763 (declare (type path-designator pathname)
1764 (values (or null pathname)))
1765 (typecase pathname
1766 (logical-pathname
1767 (dolist (x (logical-host-canon-transls (%pathname-host pathname))
1768 (error "No translation for ~S" pathname))
1769 (destructuring-bind (from to) x
1770 (when (pathname-match-p pathname from)
1771 (return (translate-logical-pathname
1772 (translate-pathname pathname from to)))))))
1773 (pathname pathname)
1774 (t (translate-logical-pathname (logical-pathname pathname)))))
1775
1776 (defvar *logical-pathname-defaults*
1777 (%make-logical-pathname (make-logical-host :name "BOGUS") :unspecific
1778 nil nil nil nil))

  ViewVC Help
Powered by ViewVC 1.1.5