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

Contents of /src/code/pathname.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5