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

Contents of /src/code/pathname.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5