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

Contents of /src/code/pathname.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5