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

Contents of /src/code/pathname.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.50 - (show annotations)
Mon May 14 13:13:39 2001 UTC (12 years, 11 months ago) by pw
Branch: MAIN
Changes since 1.49: +17 -9 lines
Fix FIND-LOGICAL-HOST to honor optional errorp argument even when
'thing' has invalid LP host syntax (and a colon 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.50 2001/05/14 13:13:39 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 (when (stringp name)
680 (let ((unix-directory-separator #\/))
681 (when (eq host (pathname-host *default-pathname-defaults*))
682 (when (find unix-directory-separator name)
683 (warn "Silly argument for a unix ~A: ~S"
684 name-or-type name)))))))
685 (check-component-validity name :pathname-name)
686 (check-component-validity type :pathname-type))
687
688 (macrolet ((pick (var varp field)
689 `(cond ((or (simple-string-p ,var)
690 (pattern-p ,var))
691 (maybe-diddle-case ,var diddle-args))
692 ((stringp ,var)
693 (maybe-diddle-case (coerce ,var 'simple-string)
694 diddle-args))
695 (,varp
696 (maybe-diddle-case ,var diddle-args))
697 (defaults
698 (maybe-diddle-case (,field defaults)
699 diddle-defaults))
700 (t
701 nil))))
702 (%make-pathname-object host
703 dev ; forced to :unspecific when logical-host
704 dir
705 (pick name namep %pathname-name)
706 (pick type typep %pathname-type)
707 ver))))
708
709 ;;; PATHNAME-HOST -- Interface
710 ;;;
711 (defun pathname-host (pathname &key (case :local))
712 "Accessor for the pathname's host."
713 (declare (type path-designator pathname)
714 (type (member :local :common) case)
715 (values host)
716 (ignore case))
717 (with-pathname (pathname pathname)
718 (%pathname-host pathname)))
719
720 ;;; PATHNAME-DEVICE -- Interface
721 ;;;
722 (defun pathname-device (pathname &key (case :local))
723 "Accessor for pathname's device."
724 (declare (type path-designator pathname)
725 (type (member :local :common) case))
726 (with-pathname (pathname pathname)
727 (maybe-diddle-case (%pathname-device pathname)
728 (and (eq case :common)
729 (eq (host-customary-case
730 (%pathname-host pathname))
731 :lower)))))
732
733 ;;; PATHNAME-DIRECTORY -- Interface
734 ;;;
735 (defun pathname-directory (pathname &key (case :local))
736 "Accessor for the pathname's directory list."
737 (declare (type path-designator pathname)
738 (type (member :local :common) case))
739 (with-pathname (pathname pathname)
740 (maybe-diddle-case (%pathname-directory pathname)
741 (and (eq case :common)
742 (eq (host-customary-case
743 (%pathname-host pathname))
744 :lower)))))
745 ;;; PATHNAME-NAME -- Interface
746 ;;;
747 (defun pathname-name (pathname &key (case :local))
748 "Accessor for the pathname's name."
749 (declare (type path-designator pathname)
750 (type (member :local :common) case))
751 (with-pathname (pathname pathname)
752 (maybe-diddle-case (%pathname-name pathname)
753 (and (eq case :common)
754 (eq (host-customary-case
755 (%pathname-host pathname))
756 :lower)))))
757
758 ;;; PATHNAME-TYPE
759 ;;;
760 (defun pathname-type (pathname &key (case :local))
761 "Accessor for the pathname's name."
762 (declare (type path-designator pathname)
763 (type (member :local :common) case))
764 (with-pathname (pathname pathname)
765 (maybe-diddle-case (%pathname-type pathname)
766 (and (eq case :common)
767 (eq (host-customary-case
768 (%pathname-host pathname))
769 :lower)))))
770
771 ;;; PATHNAME-VERSION
772 ;;;
773 (defun pathname-version (pathname)
774 "Accessor for the pathname's version."
775 (declare (type path-designator pathname))
776 (with-pathname (pathname pathname)
777 (%pathname-version pathname)))
778
779
780 ;;;; Namestrings
781
782 ;;; %PRINT-NAMESTRING-PARSE-ERROR -- Internal
783 ;;;
784 (defun %print-namestring-parse-error (condition stream)
785 (format stream "Parse error in namestring: ~?~% ~A~% ~V@T^"
786 (namestring-parse-error-complaint condition)
787 (namestring-parse-error-arguments condition)
788 (namestring-parse-error-namestring condition)
789 (namestring-parse-error-offset condition)))
790
791 (define-condition namestring-parse-error (parse-error)
792 ((complaint :reader namestring-parse-error-complaint :initarg :complaint)
793 (arguments :reader namestring-parse-error-arguments :initarg :arguments
794 :initform nil)
795 (namestring :reader namestring-parse-error-namestring :initarg :namestring)
796 (offset :reader namestring-parse-error-offset :initarg :offset))
797 (:report %print-namestring-parse-error))
798
799
800 ;;; %PARSE-NAMESTRING -- Internal
801 ;;;
802 ;;; Handle the case where parse-namestring is actually parsing a namestring.
803 ;;; We pick off the :JUNK-ALLOWED case then find a host to use for parsing,
804 ;;; call the parser, then check if the host matches.
805 ;;;
806 (defun %parse-namestring (namestr host defaults start end junk-allowed)
807 (declare (type string namestr)
808 (type (or host string null) host)
809 (type pathname defaults)
810 (type index start)
811 (type (or index null) end))
812 (if junk-allowed
813 (handler-case
814 (%parse-namestring namestr host defaults start end nil)
815 (namestring-parse-error (condition)
816 (values nil (namestring-parse-error-offset condition))))
817 (let* ((end (or end (length namestr)))
818 (host (if (and host (stringp host))
819 (find-logical-host host)
820 host))
821 (default-host (pathname-host defaults))
822 (parse-host (or host
823 (extract-logical-host-prefix namestr start end)
824 default-host)))
825 (unless parse-host
826 (error "When Host arg is not supplied, Defaults arg must ~
827 have a non-null PATHNAME-HOST."))
828
829 (multiple-value-bind
830 (new-host device directory file type version)
831 (funcall (host-parse parse-host) namestr start end)
832 (when (and host new-host (not (eq new-host host)))
833 (error "Host in namestring: ~S~@
834 does not match explicit host argument: ~S"
835 namestr host))
836 (let ((pn-host (or new-host parse-host)))
837 (values (%make-pathname-object
838 pn-host device directory file type version)
839 end))))))
840
841
842 ;;; EXTRACT-LOGICAL-HOST-PREFIX -- Internal
843 ;;;
844 ;;; If namestr begins with a colon-terminated, defined, logical host, then
845 ;;; return that host, otherwise return NIL.
846 ;;;
847 (defun extract-logical-host-prefix (namestr start end)
848 (declare (type simple-base-string namestr)
849 (type index start end)
850 (values (or logical-host null)))
851 (let ((colon-pos (position #\: namestr :start start :end end)))
852 (if colon-pos
853 (find-logical-host (nstring-upcase (subseq namestr start colon-pos))
854 nil)
855 nil)))
856
857 ;;; PARSE-NAMESTRING -- Interface
858 ;;;
859 (defun parse-namestring (thing
860 &optional host (defaults *default-pathname-defaults*)
861 &key (start 0) end junk-allowed)
862 "Converts pathname, a pathname designator, into a pathname structure,
863 for a physical pathname, returns the printed representation. Host may be
864 a physical host structure or host namestring."
865 (declare (type path-designator thing)
866 (type (or null string host) host)
867 (type pathname defaults)
868 (type index start)
869 (type (or index null) end))
870 (etypecase thing
871 (simple-string
872 (%parse-namestring thing host defaults start end junk-allowed))
873 (string
874 (%parse-namestring (coerce thing 'simple-string)
875 host defaults start end junk-allowed))
876 (pathname
877 (let ((host (if host host (%pathname-host defaults))))
878 (unless (eq host (%pathname-host thing))
879 (error "Hosts do not match: ~S and ~S."
880 host (%pathname-host thing))))
881 (values thing start))
882 (stream
883 (let ((name (file-name thing)))
884 (unless name
885 (error "Can't figure out the file associated with stream:~% ~S"
886 thing))
887 (values name nil)))))
888
889
890 ;;; NAMESTRING -- Interface
891 ;;;
892 (defun namestring (pathname)
893 "Construct the full (name)string form of the pathname."
894 (declare (type path-designator pathname)
895 (values (or null simple-base-string)))
896 (with-pathname (pathname pathname)
897 (when pathname
898 (let ((host (%pathname-host pathname)))
899 (unless host
900 (error "Cannot determine the namestring for pathnames with no ~
901 host:~% ~S" pathname))
902 (funcall (host-unparse host) pathname)))))
903
904
905 ;;; HOST-NAMESTRING -- Interface
906 ;;;
907 (defun host-namestring (pathname)
908 "Returns a string representation of the name of the host in the pathname."
909 (declare (type path-designator pathname)
910 (values (or null simple-base-string)))
911 (with-pathname (pathname pathname)
912 (let ((host (%pathname-host pathname)))
913 (if host
914 (funcall (host-unparse-host host) pathname)
915 (error
916 "Cannot determine the namestring for pathnames with no host:~% ~S"
917 pathname)))))
918
919 ;;; DIRECTORY-NAMESTRING -- Interface
920 ;;;
921 (defun directory-namestring (pathname)
922 "Returns a string representation of the directories used in the pathname."
923 (declare (type path-designator pathname)
924 (values (or null simple-base-string)))
925 (with-pathname (pathname pathname)
926 (let ((host (%pathname-host pathname)))
927 (if host
928 (funcall (host-unparse-directory host) pathname)
929 (error
930 "Cannot determine the namestring for pathnames with no host:~% ~S"
931 pathname)))))
932
933 ;;; FILE-NAMESTRING -- Interface
934 ;;;
935 (defun file-namestring (pathname)
936 "Returns a string representation of the name used in the pathname."
937 (declare (type path-designator pathname)
938 (values (or null simple-base-string)))
939 (with-pathname (pathname pathname)
940 (let ((host (%pathname-host pathname)))
941 (if host
942 (funcall (host-unparse-file host) pathname)
943 (error
944 "Cannot determine the namestring for pathnames with no host:~% ~S"
945 pathname)))))
946
947 ;;; ENOUGH-NAMESTRING -- Interface
948 ;;;
949 (defun enough-namestring (pathname
950 &optional (defaults *default-pathname-defaults*))
951 "Returns an abbreviated pathname sufficent to identify the pathname relative
952 to the defaults."
953 (declare (type path-designator pathname))
954 (with-pathname (pathname pathname)
955 (let ((host (%pathname-host pathname)))
956 (if host
957 (with-pathname (defaults defaults)
958 (funcall (host-unparse-enough host) pathname defaults))
959 (error
960 "Cannot determine the namestring for pathnames with no host:~% ~S"
961 pathname)))))
962
963
964 ;;;; Wild pathnames.
965
966 ;;; WILD-PATHNAME-P -- Interface
967 ;;;
968 (defun wild-pathname-p (pathname &optional field-key)
969 "Predicate for determining whether pathname contains any wildcards."
970 (declare (type path-designator pathname)
971 (type (member nil :host :device :directory :name :type :version)
972 field-key))
973 (with-pathname (pathname pathname)
974 (flet ((frob (x)
975 (or (pattern-p x) (member x '(:wild :wild-inferiors)))))
976 (ecase field-key
977 ((nil)
978 (or (wild-pathname-p pathname :host)
979 (wild-pathname-p pathname :device)
980 (wild-pathname-p pathname :directory)
981 (wild-pathname-p pathname :name)
982 (wild-pathname-p pathname :type)
983 (wild-pathname-p pathname :version)))
984 (:host (frob (%pathname-host pathname)))
985 (:device (frob (%pathname-device pathname)))
986 (:directory (some #'frob (%pathname-directory pathname)))
987 (:name (frob (%pathname-name pathname)))
988 (:type (frob (%pathname-type pathname)))
989 (:version (frob (%pathname-version pathname)))))))
990
991
992 ;;; PATHNAME-MATCH-P -- Interface
993 ;;;
994 (defun pathname-match-p (in-pathname in-wildname)
995 "Pathname matches the wildname template?"
996 (declare (type path-designator in-pathname))
997 (with-pathname (pathname in-pathname)
998 (with-pathname (wildname in-wildname)
999 (macrolet ((frob (field &optional (op 'components-match ))
1000 `(or (null (,field wildname))
1001 (,op (,field pathname) (,field wildname)))))
1002 (and (or (null (%pathname-host wildname))
1003 (eq (%pathname-host wildname) (%pathname-host pathname)))
1004 (frob %pathname-device)
1005 (frob %pathname-directory directory-components-match)
1006 (frob %pathname-name)
1007 (frob %pathname-type)
1008 (frob %pathname-version))))))
1009
1010
1011 ;;; SUBSTITUTE-INTO -- Internal
1012 ;;;
1013 ;;; Place the substitutions into the pattern and return the string or pattern
1014 ;;; that results. If DIDDLE-CASE is true, we diddle the result case as well,
1015 ;;; in case we are translating between hosts with difference conventional case.
1016 ;;; The second value is the tail of subs with all of the values that we used up
1017 ;;; stripped off. Note that PATTERN-MATCHES matches all consecutive wildcards
1018 ;;; as a single string, so we ignore subsequent contiguous wildcards.
1019 ;;;
1020 (defun substitute-into (pattern subs diddle-case)
1021 (declare (type pattern pattern)
1022 (type list subs)
1023 (values (or simple-base-string pattern) list))
1024 (let ((in-wildcard nil)
1025 (pieces nil)
1026 (strings nil))
1027 (dolist (piece (pattern-pieces pattern))
1028 (cond ((simple-string-p piece)
1029 (push piece strings)
1030 (setf in-wildcard nil))
1031 (in-wildcard)
1032 (t
1033 (setf in-wildcard t)
1034 (unless subs
1035 (error "Not enough wildcards in FROM pattern to match ~
1036 TO pattern:~% ~S"
1037 pattern))
1038 (let ((sub (pop subs)))
1039 (typecase sub
1040 (pattern
1041 (when strings
1042 (push (apply #'concatenate 'simple-string
1043 (nreverse strings))
1044 pieces))
1045 (dolist (piece (pattern-pieces sub))
1046 (push piece pieces)))
1047 (simple-string
1048 (push sub strings))
1049 (t
1050 (error "Can't substitute this into the middle of a word:~
1051 ~% ~S"
1052 sub)))))))
1053
1054 (when strings
1055 (push (apply #'concatenate 'simple-string (nreverse strings))
1056 pieces))
1057 (values
1058 (maybe-diddle-case
1059 (if (and pieces (simple-string-p (car pieces)) (null (cdr pieces)))
1060 (car pieces)
1061 (make-pattern (nreverse pieces)))
1062 diddle-case)
1063 subs)))
1064
1065
1066 ;;; DIDNT-MATCH-ERROR -- Internal
1067 ;;;
1068 ;;; Called when we can't see how source and from matched.
1069 ;;;
1070 (defun didnt-match-error (source from)
1071 (error "Pathname components from Source and From args to TRANSLATE-PATHNAME~@
1072 did not match:~% ~S ~S"
1073 source from))
1074
1075
1076 ;;; TRANSLATE-COMPONENT -- Internal
1077 ;;;
1078 ;;; Do TRANSLATE-COMPONENT for all components except host and directory.
1079 ;;;
1080 (defun translate-component (source from to diddle-case)
1081 (typecase to
1082 (pattern
1083 (typecase from
1084 (pattern
1085 (typecase source
1086 (pattern
1087 (if (pattern= from source)
1088 source
1089 (didnt-match-error source from)))
1090 (simple-string
1091 (multiple-value-bind
1092 (won subs)
1093 (pattern-matches from source)
1094 (if won
1095 (values (substitute-into to subs diddle-case))
1096 (didnt-match-error source from))))
1097 (t
1098 (maybe-diddle-case source diddle-case))))
1099 ((member :wild)
1100 (values (substitute-into to (list source) diddle-case)))
1101 (t
1102 (if (components-match source from)
1103 (maybe-diddle-case source diddle-case)
1104 (didnt-match-error source from)))))
1105 ((member nil :wild)
1106 (maybe-diddle-case source diddle-case))
1107 (t
1108 (if (components-match source from)
1109 to
1110 (didnt-match-error source from)))))
1111
1112
1113 ;;; COMPUTE-DIRECTORY-SUBSTITUTIONS -- Internal
1114 ;;;
1115 ;;; Return a list of all the things that we want to substitute into the TO
1116 ;;; pattern (the things matched by from on source.) When From contains
1117 ;;; :WILD-INFERIORS, the result contains a sublist of the matched source
1118 ;;; subdirectories.
1119 ;;;
1120 (defun compute-directory-substitutions (orig-source orig-from)
1121 (let ((source orig-source)
1122 (from orig-from))
1123 (collect ((subs))
1124 (loop
1125 (unless source
1126 (unless (every #'(lambda (x) (eq x :wild-inferiors)) from)
1127 (didnt-match-error orig-source orig-from))
1128 (subs ())
1129 (return))
1130 (unless from (didnt-match-error orig-source orig-from))
1131 (let ((from-part (pop from))
1132 (source-part (pop source)))
1133 (typecase from-part
1134 (pattern
1135 (typecase source-part
1136 (pattern
1137 (if (pattern= from-part source-part)
1138 (subs source-part)
1139 (didnt-match-error orig-source orig-from)))
1140 (simple-string
1141 (multiple-value-bind
1142 (won new-subs)
1143 (pattern-matches from-part source-part)
1144 (if won
1145 (dolist (sub new-subs)
1146 (subs sub))
1147 (didnt-match-error orig-source orig-from))))
1148 (t
1149 (didnt-match-error orig-source orig-from))))
1150 ((member :wild)
1151 (subs source-part))
1152 ((member :wild-inferiors)
1153 (let ((remaining-source (cons source-part source)))
1154 (collect ((res))
1155 (loop
1156 (when (directory-components-match remaining-source from)
1157 (return))
1158 (unless remaining-source
1159 (didnt-match-error orig-source orig-from))
1160 (res (pop remaining-source)))
1161 (subs (res))
1162 (setq source remaining-source))))
1163 (simple-string
1164 (unless (and (simple-string-p source-part)
1165 (string= from-part source-part))
1166 (didnt-match-error orig-source orig-from)))
1167 (t
1168 (didnt-match-error orig-source orig-from)))))
1169 (subs))))
1170
1171
1172 ;;; TRANSLATE-DIRECTORIES -- Internal
1173 ;;;
1174 ;;; Called by TRANSLATE-PATHNAME on the directory components of its argument
1175 ;;; pathanames to produce the result directory component. If any leaves the
1176 ;;; directory NIL, we return the source directory. The :RELATIVE or :ABSOLUTE
1177 ;;; is taken from the source directory, except if TO is :ABSOLUTE, in which
1178 ;;; case the result will be :ABSOLUTE.
1179 ;;;
1180 (defun translate-directories (source from to diddle-case)
1181 (if (not (and source to from))
1182 (let ((source (mapcar #'(lambda (x) (maybe-diddle-case x diddle-case))
1183 source)))
1184 (if (null to)
1185 source
1186 (collect ((res))
1187 (res (cond ((null source) (first to))
1188 ((eq (first to) :absolute) :absolute)
1189 (t (first source))))
1190 (let ((match (rest source)))
1191 (dolist (to-part (rest to))
1192 (cond ((eq to-part :wild)
1193 (when match
1194 (res (first match))
1195 (setf match nil)))
1196 ((eq to-part :wild-inferiors)
1197 (when match
1198 (dolist (src-part match)
1199 (res src-part))
1200 (setf match nil)))
1201 (t
1202 (res to-part)))))
1203 (res))))
1204 (collect ((res))
1205 (res (if (eq (first to) :absolute)
1206 :absolute
1207 (first source)))
1208 (let ((subs-left (compute-directory-substitutions (rest source)
1209 (rest from))))
1210 (dolist (to-part (rest to))
1211 (typecase to-part
1212 ((member :wild)
1213 (assert subs-left)
1214 (let ((match (pop subs-left)))
1215 (when (listp match)
1216 (error ":WILD-INFERIORS not paired in from and to ~
1217 patterns:~% ~S ~S" from to))
1218 (res (maybe-diddle-case match diddle-case))))
1219 ((member :wild-inferiors)
1220 (assert subs-left)
1221 (let ((match (pop subs-left)))
1222 (unless (listp match)
1223 (error ":WILD-INFERIORS not paired in from and to ~
1224 patterns:~% ~S ~S" from to))
1225 (dolist (x match)
1226 (res (maybe-diddle-case x diddle-case)))))
1227 (pattern
1228 (multiple-value-bind
1229 (new new-subs-left)
1230 (substitute-into to-part subs-left diddle-case)
1231 (setf subs-left new-subs-left)
1232 (res new)))
1233 (t (res to-part)))))
1234 (res))))
1235
1236
1237 ;;; TRANSLATE-PATHNAME -- Interface
1238 ;;;
1239 (defun translate-pathname (source from-wildname to-wildname &key)
1240 "Use the source pathname to translate the from-wildname's wild and
1241 unspecified elements into a completed to-pathname based on the to-wildname."
1242 (declare (type path-designator source from-wildname to-wildname))
1243 (with-pathname (source source)
1244 (with-pathname (from from-wildname)
1245 (with-pathname (to to-wildname)
1246 (let* ((source-host (%pathname-host source))
1247 (to-host (%pathname-host to))
1248 (diddle-case
1249 (and source-host to-host
1250 (not (eq (host-customary-case source-host)
1251 (host-customary-case to-host))))))
1252 (macrolet ((frob (field &optional (op 'translate-component))
1253 `(let ((result (,op (,field source)
1254 (,field from)
1255 (,field to)
1256 diddle-case)))
1257 (if (eq result :error)
1258 (error "~S doesn't match ~S" source from)
1259 result))))
1260 (%make-pathname-object
1261 (or to-host source-host)
1262 (frob %pathname-device)
1263 (frob %pathname-directory translate-directories)
1264 (frob %pathname-name)
1265 (frob %pathname-type)
1266 (frob %pathname-version))))))))
1267
1268
1269 ;;;; Search lists.
1270
1271 ;;; The SEARCH-LIST structure.
1272 ;;;
1273 (defstruct (search-list
1274 (:print-function %print-search-list)
1275 (:make-load-form-fun
1276 (lambda (search-list)
1277 (values `(intern-search-list ',(search-list-name search-list))
1278 nil))))
1279 ;;
1280 ;; The name of this search-list. Always stored in lowercase.
1281 (name (required-argument) :type simple-string)
1282 ;;
1283 ;; T if this search-list has been defined. Otherwise NIL.
1284 (defined nil :type (member t nil))
1285 ;;
1286 ;; The list of expansions for this search-list. Each expansion is the list
1287 ;; of directory components to use in place of this search-list.
1288 (expansions nil :type list))
1289
1290 (defun %print-search-list (sl stream depth)
1291 (declare (ignore depth))
1292 (print-unreadable-object (sl stream :type t)
1293 (write-string (search-list-name sl) stream)))
1294
1295 ;;; *SEARCH-LISTS* -- internal.
1296 ;;;
1297 ;;; Hash table mapping search-list names to search-list structures.
1298 ;;;
1299 (defvar *search-lists* (make-hash-table :test #'equal))
1300
1301 ;;; FIND-SEARCH-LIST -- internal
1302 ;;;
1303 (defun find-search-list (name &optional (flame-not-found-p t))
1304 (let ((search-list (gethash (string-downcase name) *search-lists*)))
1305 (if search-list search-list
1306 (when flame-not-found-p
1307 (error "Search-list ~a not defined." name)))))
1308
1309 ;;; INTERN-SEARCH-LIST -- internal interface.
1310 ;;;
1311 ;;; When search-lists are encountered in namestrings, they are converted to
1312 ;;; search-list structures right then, instead of waiting until the search
1313 ;;; list used. This allows us to verify ahead of time that there are no
1314 ;;; circularities and makes expansion much quicker.
1315 ;;;
1316 (defun intern-search-list (name)
1317 (let ((name (string-downcase name)))
1318 (or (gethash name *search-lists*)
1319 (let ((new (make-search-list :name name)))
1320 (setf (gethash name *search-lists*) new)
1321 new))))
1322
1323 ;;; CLEAR-SEARCH-LIST -- public.
1324 ;;;
1325 ;;; Clear the definition. Note: we can't remove it from the hash-table
1326 ;;; because there may be pathnames still refering to it. So we just clear
1327 ;;; out the expansions and set defined to NIL.
1328 ;;;
1329 (defun clear-search-list (name)
1330 "Clear the current definition for the search-list NAME. Returns T if such
1331 a definition existed, and NIL if not."
1332 (let* ((name (string-downcase name))
1333 (search-list (gethash name *search-lists*)))
1334 (when (and search-list (search-list-defined search-list))
1335 (setf (search-list-defined search-list) nil)
1336 (setf (search-list-expansions search-list) nil)
1337 t)))
1338
1339 ;;; CLEAR-ALL-SEARCH-LISTS -- sorta public.
1340 ;;;
1341 ;;; Again, we can't actually remove the entries from the hash-table, so we
1342 ;;; just mark them as being undefined.
1343 ;;;
1344 (defun clear-all-search-lists ()
1345 "Clear the definition for all search-lists. Only use this if you know
1346 what you are doing."
1347 (maphash #'(lambda (name search-list)
1348 (declare (ignore name))
1349 (setf (search-list-defined search-list) nil)
1350 (setf (search-list-expansions search-list) nil))
1351 *search-lists*)
1352 nil)
1353
1354 ;;; EXTRACT-SEARCH-LIST -- internal.
1355 ;;;
1356 ;;; Extract the search-list from PATHNAME and return it. If PATHNAME
1357 ;;; doesn't start with a search-list, then either error (if FLAME-IF-NONE
1358 ;;; is true) or return NIL (if FLAME-IF-NONE is false).
1359 ;;;
1360 (defun extract-search-list (pathname flame-if-none)
1361 (with-pathname (pathname pathname)
1362 (let* ((directory (%pathname-directory pathname))
1363 (search-list (cadr directory)))
1364 (cond ((search-list-p search-list)
1365 search-list)
1366 (flame-if-none
1367 (error "~S doesn't start with a search-list." pathname))
1368 (t
1369 nil)))))
1370
1371 ;;; SEARCH-LIST -- public.
1372 ;;;
1373 ;;; We have to convert the internal form of the search-list back into a
1374 ;;; bunch of pathnames.
1375 ;;;
1376 (defun search-list (pathname)
1377 "Return the expansions for the search-list starting PATHNAME. If PATHNAME
1378 does not start with a search-list, then an error is signaled. If
1379 the search-list has not been defined yet, then an error is signaled.
1380 The expansion for a search-list can be set with SETF."
1381 (with-pathname (pathname pathname)
1382 (let ((search-list (extract-search-list pathname t))
1383 (host (pathname-host pathname)))
1384 (if (search-list-defined search-list)
1385 (mapcar #'(lambda (directory)
1386 (make-pathname :host host
1387 :directory (cons :absolute directory)))
1388 (search-list-expansions search-list))
1389 (error "Search list ~S has not been defined yet." pathname)))))
1390
1391 ;;; SEARCH-LIST-DEFINED-P -- public.
1392 ;;;
1393 (defun search-list-defined-p (pathname)
1394 "Returns T if the search-list starting PATHNAME is currently defined, and
1395 NIL otherwise. An error is signaled if PATHNAME does not start with a
1396 search-list."
1397 (with-pathname (pathname pathname)
1398 (search-list-defined (extract-search-list pathname t))))
1399
1400 ;;; %SET-SEARCH-LIST -- public setf method
1401 ;;;
1402 ;;; Set the expansion for the search-list in PATHNAME. If this would result
1403 ;;; in any circularities, we flame out. If anything goes wrong, we leave the
1404 ;;; old defintion intact.
1405 ;;;
1406 (defun %set-search-list (pathname values)
1407 (let ((search-list (extract-search-list pathname t)))
1408 (labels
1409 ((check (target-list path)
1410 (when (eq search-list target-list)
1411 (error "That would result in a circularity:~% ~
1412 ~A~{ -> ~A~} -> ~A"
1413 (search-list-name search-list)
1414 (reverse path)
1415 (search-list-name target-list)))
1416 (when (search-list-p target-list)
1417 (push (search-list-name target-list) path)
1418 (dolist (expansion (search-list-expansions target-list))
1419 (check (car expansion) path))))
1420 (convert (pathname)
1421 (with-pathname (pathname pathname)
1422 (when (or (pathname-name pathname)
1423 (pathname-type pathname)
1424 (pathname-version pathname))
1425 (error "Search-lists cannot expand into pathnames that have ~
1426 a name, type, or ~%version specified:~% ~S"
1427 pathname))
1428 (let ((directory (pathname-directory pathname)))
1429 (let ((expansion
1430 (if directory
1431 (ecase (car directory)
1432 (:absolute (cdr directory))
1433 (:relative (cons (intern-search-list "default")
1434 (cdr directory))))
1435 (list (intern-search-list "default")))))
1436 (check (car expansion) nil)
1437 expansion)))))
1438 (setf (search-list-expansions search-list)
1439 (if (listp values)
1440 (mapcar #'convert values)
1441 (list (convert values)))))
1442 (setf (search-list-defined search-list) t))
1443 values)
1444
1445 ;;; ENUMERATE-SEARCH-LIST -- public.
1446 ;;;
1447 (defmacro enumerate-search-list ((var pathname &optional result) &body body)
1448 "Execute BODY with VAR bound to each successive possible expansion for
1449 PATHNAME and then return RESULT. Note: if PATHNAME does not contain a
1450 search-list, then BODY is executed exactly once. Everything is wrapped
1451 in a block named NIL, so RETURN can be used to terminate early. Note:
1452 VAR is *not* bound inside of RESULT."
1453 (let ((body-name (gensym)))
1454 `(block nil
1455 (flet ((,body-name (,var)
1456 ,@body))
1457 (%enumerate-search-list ,pathname #',body-name)
1458 ,result))))
1459
1460 (defun %enumerate-search-list (pathname function)
1461 (let* ((pathname (if (logical-pathname-p pathname)
1462 (translate-logical-pathname pathname)
1463 pathname))
1464 (search-list (extract-search-list pathname nil)))
1465 (cond
1466 ((not search-list)
1467 (funcall function pathname))
1468 ((not (search-list-defined search-list))
1469 (error "Undefined search list: ~A"
1470 (search-list-name search-list)))
1471 (t
1472 (let ((tail (cddr (pathname-directory pathname))))
1473 (dolist (expansion
1474 (search-list-expansions search-list))
1475 (%enumerate-search-list (make-pathname :defaults pathname
1476 :directory
1477 (cons :absolute
1478 (append expansion
1479 tail)))
1480 function)))))))
1481
1482
1483 ;;;; Logical pathname support. ANSI 92-102 specification.
1484 ;;;; As logical-pathname translations are loaded they are canonicalized as
1485 ;;;; patterns to enable rapid efficent translation into physical pathnames.
1486
1487 ;;;; Utilities:
1488
1489 ;;; LOGICAL-WORD-OR-LOSE -- Internal
1490 ;;;
1491 ;;; Canonicalize a logical pathanme word by uppercasing it checking that it
1492 ;;; contains only legal characters.
1493 ;;;
1494 (defun logical-word-or-lose (word)
1495 (declare (string word))
1496 (let ((word (string-upcase word)))
1497 (dotimes (i (length word))
1498 (let ((ch (schar word i)))
1499 (unless (or (alpha-char-p ch) (digit-char-p ch) (char= ch #\-))
1500 (error 'namestring-parse-error
1501 :complaint "Logical namestring character ~
1502 is not alphanumeric or hyphen:~% ~S"
1503 :arguments (list ch)
1504 :namestring word :offset i))))
1505 word))
1506
1507
1508 ;;; FIND-LOGICAL-HOST -- Internal
1509 ;;;
1510 ;;; Given a logical host or string, return a logical host. If Error-p is
1511 ;;; NIL, then return NIL when no such host exists.
1512 ;;;
1513 (defun find-logical-host (thing &optional (errorp t))
1514 (etypecase thing
1515 (string
1516 (let* ((valid-hostname
1517 (catch 'error-bailout
1518 (handler-bind
1519 ((namestring-parse-error
1520 (lambda(c)(declare (ignore c))
1521 (unless errorp
1522 (throw 'error-bailout nil)))))
1523 (logical-word-or-lose thing))))
1524 (found
1525 (and valid-hostname
1526 (or (gethash valid-hostname *logical-hosts*)
1527 (and *autoload-translations*
1528 (ignore-errors
1529 (load-logical-pathname-translationsvalid-hostname))
1530 (gethash valid-hostname *logical-hosts*))))))
1531 (if (or found (not errorp))
1532 found
1533 (error 'simple-file-error
1534 :pathname thing
1535 :format-control "Logical host not yet defined: ~S"
1536 :format-arguments (list thing)))))
1537 (logical-host thing)))
1538
1539 ;;; INTERN-LOGICAL-HOST -- Internal
1540 ;;;
1541 ;;; Given a logical host name or host, return a logical host, creating a new
1542 ;;; one if necessary.
1543 ;;;
1544 (defun intern-logical-host (thing)
1545 (declare (values logical-host))
1546 (or (find-logical-host thing nil)
1547 (let* ((name (logical-word-or-lose thing))
1548 (new (make-logical-host :name name)))
1549 (setf (gethash name *logical-hosts*) new)
1550 new)))
1551
1552
1553 ;;;; Logical pathname parsing:
1554
1555 ;;; MAYBE-MAKE-LOGICAL-PATTERN -- Internal
1556 ;;;
1557 ;;; Deal with multi-char wildcards in a logical pathname token.
1558 ;;;
1559 (defun maybe-make-logical-pattern (namestring chunks)
1560 (let ((chunk (caar chunks)))
1561 (collect ((pattern))
1562 (let ((last-pos 0)
1563 (len (length chunk)))
1564 (declare (fixnum last-pos))
1565 (loop
1566 (when (= last-pos len) (return))
1567 (let ((pos (or (position #\* chunk :start last-pos) len)))
1568 (if (= pos last-pos)
1569 (when (pattern)
1570 (error 'namestring-parse-error
1571 :complaint "Double asterisk inside of logical ~
1572 word: ~S"
1573 :arguments (list chunk)
1574 :namestring namestring
1575 :offset (+ (cdar chunks) pos)))
1576 (pattern (subseq chunk last-pos pos)))
1577 (if (= pos len)
1578 (return)
1579 (pattern :multi-char-wild))
1580 (setq last-pos (1+ pos)))))
1581 (assert (pattern))
1582 (if (cdr (pattern))
1583 (make-pattern (pattern))
1584 (let ((x (car (pattern))))
1585 (if (eq x :multi-char-wild)
1586 :wild
1587 x))))))
1588
1589
1590 ;;; LOGICAL-CHUNKIFY -- Internal
1591 ;;;
1592 ;;; Return a list of conses where the cdr is the start position and the car
1593 ;;; is a string (token) or character (punctuation.)
1594 ;;;
1595 (defun logical-chunkify (namestr start end)
1596 (collect ((chunks))
1597 (do ((i start (1+ i))
1598 (prev 0))
1599 ((= i end)
1600 (when (> end prev)
1601 (chunks (cons (nstring-upcase (subseq namestr prev end)) prev))))
1602 (let ((ch (schar namestr i)))
1603 (unless (or (alpha-char-p ch) (digit-char-p ch)
1604 (member ch '(#\- #\*)))
1605 (when (> i prev)
1606 (chunks (cons (nstring-upcase (subseq namestr prev i)) prev)))
1607 (setq prev (1+ i))
1608 (unless (member ch '(#\; #\: #\.))
1609 (error 'namestring-parse-error
1610 :complaint "Illegal character for logical pathname:~% ~S"
1611 :arguments (list ch)
1612 :namestring namestr
1613 :offset i))
1614 (chunks (cons ch i)))))
1615 (chunks)))
1616
1617
1618 ;;; PARSE-LOGICAL-NAMESTRING -- Internal
1619 ;;;
1620 ;;; Break up a logical-namestring, always a string, into its constituent
1621 ;;; parts.
1622 ;;;
1623 (defun parse-logical-namestring (namestr start end)
1624 (declare (type simple-base-string namestr)
1625 (type index start end))
1626 (collect ((directory))
1627 (let ((host nil)
1628 (name nil)
1629 (type nil)
1630 (version nil))
1631 (labels ((expecting (what chunks)
1632 (unless (and chunks (simple-string-p (caar chunks)))
1633 (error 'namestring-parse-error
1634 :complaint "Expecting ~A, got ~:[nothing~;~S~]."
1635 :arguments (list what (caar chunks))
1636 :namestring namestr
1637 :offset (if chunks (cdar chunks) end)))
1638 (caar chunks))
1639 (parse-host (chunks)
1640 (case (caadr chunks)
1641 (#\:
1642 (setq host
1643 (find-logical-host (expecting "a host name" chunks)))
1644 (parse-relative (cddr chunks)))
1645 (t
1646 (parse-relative chunks))))
1647 (parse-relative (chunks)
1648 (case (caar chunks)
1649 (#\;
1650 (directory :relative)
1651 (parse-directory (cdr chunks)))
1652 (t
1653 (directory :absolute) ; Assumption! Maybe revoked later.
1654 (parse-directory chunks))))
1655 (parse-directory (chunks)
1656 (case (caadr chunks)
1657 (#\;
1658 (directory
1659 (let ((res (expecting "a directory name" chunks)))
1660 (cond ((string= res "..") :up)
1661 ((string= res "**") :wild-inferiors)
1662 (t
1663 (maybe-make-logical-pattern namestr chunks)))))
1664 (parse-directory (cddr chunks)))
1665 (t
1666 (parse-name chunks))))
1667 (parse-name (chunks)
1668 (when chunks
1669 (expecting "a file name" chunks)
1670 (setq name (maybe-make-logical-pattern namestr chunks))
1671 (expecting-dot (cdr chunks))))
1672 (expecting-dot (chunks)
1673 (when chunks
1674 (unless (eql (caar chunks) #\.)
1675 (error 'namestring-parse-error
1676 :complaint "Expecting a dot, got ~S."
1677 :arguments (list (caar chunks))
1678 :namestring namestr
1679 :offset (cdar chunks)))
1680 (if type
1681 (parse-version (cdr chunks))
1682 (parse-type (cdr chunks)))))
1683 (parse-type (chunks)
1684 (expecting "a file type" chunks)
1685 (setq type (maybe-make-logical-pattern namestr chunks))
1686 (expecting-dot (cdr chunks)))
1687 (parse-version (chunks)
1688 (let ((str (expecting "a positive integer, * or NEWEST"
1689 chunks)))
1690 (cond
1691 ((string= str "*") (setq version :wild))
1692 ((string= str "NEWEST") (setq version :newest))
1693 (t
1694 (multiple-value-bind
1695 (res pos)
1696 (parse-integer str :junk-allowed t)
1697 (unless (and res (plusp res))
1698 (error 'namestring-parse-error
1699 :complaint "Expected a positive integer, ~
1700 got ~S"
1701 :arguments (list str)
1702 :namestring namestr
1703 :offset (+ pos (cdar chunks))))
1704 (setq version res)))))
1705 (when (cdr chunks)
1706 (error 'namestring-parse-error
1707 :complaint "Extra stuff after end of file name."
1708 :namestring namestr
1709 :offset (cdadr chunks)))))
1710 (parse-host (logical-chunkify namestr start end)))
1711 (values host :unspecific
1712 (and (not (equal (directory)'(:absolute)))(directory))
1713 name type version))))
1714
1715
1716 ;;; Can't defvar here because not all host methods are loaded yet.
1717 (declaim (special *logical-pathname-defaults*))
1718
1719 ;;; LOGICAL-PATHNAME -- Public
1720 ;;;
1721 (defun logical-pathname (pathspec)
1722 "Converts the pathspec argument to a logical-pathname and returns it."
1723 (declare (type (or logical-pathname string stream) pathspec)
1724 (values logical-pathname))
1725 (if (typep pathspec 'logical-pathname)
1726 pathspec
1727 (let ((res (parse-namestring pathspec nil *logical-pathname-defaults*)))
1728 (when (eq (%pathname-host res)
1729 (%pathname-host *logical-pathname-defaults*))
1730 (error
1731 'simple-type-error
1732 :format-control "Logical namestring does not specify a host:~% ~S"
1733 :format-arguments (list pathspec)
1734 :datum pathspec
1735 :expected-type '(satisfies logical-pathname-namestring-p)))
1736 res)))
1737
1738
1739
1740 ;;;; Logical pathname unparsing:
1741
1742 ;;; UNPARSE-LOGICAL-DIRECTORY -- Internal
1743 ;;;
1744 (defun unparse-logical-directory (pathname)
1745 (declare (type pathname pathname))
1746 (collect ((pieces))
1747 (let ((directory (%pathname-directory pathname)))
1748 (when directory
1749 (ecase (pop directory)
1750 (:absolute) ;; Nothing special.
1751 (:relative (pieces ";")))
1752 (dolist (dir directory)
1753 (cond ((or (stringp dir) (pattern-p dir))
1754 (pieces (unparse-logical-piece dir))
1755 (pieces ";"))
1756 ((eq dir :wild)
1757 (pieces "*;"))
1758 ((eq dir :wild-inferiors)
1759 (pieces "**;"))
1760 (t
1761 (error "Invalid directory component: ~S" dir))))))
1762 (apply #'concatenate 'simple-string (pieces))))
1763
1764
1765 ;;; UNPARSE-LOGICAL-PIECE -- Internal
1766 ;;;
1767 (defun unparse-logical-piece (thing)
1768 (etypecase thing
1769 (simple-string thing)
1770 (pattern
1771 (collect ((strings))
1772 (dolist (piece (pattern-pieces thing))
1773 (etypecase piece
1774 (simple-string (strings piece))
1775 (keyword
1776 (cond ((eq piece :wild-inferiors)
1777 (strings "**"))
1778 ((eq piece :multi-char-wild)
1779 (strings "*"))
1780 (t (error "Invalid keyword: ~S" piece))))))
1781 (apply #'concatenate 'simple-string (strings))))))
1782
1783 ;;; UNPARSE-ENOUGH-NAMESTRING -- Internal
1784 ;;;
1785 (defun unparse-enough-namestring (pathname defaults)
1786 (let* ((path-dir (pathname-directory pathname))
1787 (def-dir (pathname-directory defaults))
1788 (enough-dir
1789 ;; Go down the directory lists to see what matches. What's
1790 ;; left is what we want, more or less.
1791 (cond ((and (eq (first path-dir) (first def-dir))
1792 (eq (first path-dir) :absolute))
1793 ;; Both paths are :absolute, so find where the common
1794 ;; parts end and return what's left
1795 (do* ((p (rest path-dir) (rest p))
1796 (d (rest def-dir) (rest d)))
1797 ((or (endp p) (endp d)
1798 (not (equal (first p) (first d))))
1799 `(:relative ,@p))))
1800 (t
1801 ;; At least one path is :relative, so just return the
1802 ;; original path. If the original path is :relative,
1803 ;; then that's the right one. If PATH-DIR is
1804 ;; :absolute, we want to return that except when
1805 ;; DEF-DIR is :absolute, as handled above. so return
1806 ;; the original directory.
1807 path-dir))))
1808 (make-pathname :host (pathname-host pathname)
1809 :directory enough-dir
1810 :name (pathname-name pathname)
1811 :type (pathname-type pathname)
1812 :version (pathname-version pathname))))
1813
1814 ;;; UNPARSE-LOGICAL-NAMESTRING -- Internal
1815 ;;;
1816 (defun unparse-logical-namestring (pathname)
1817 (declare (type logical-pathname pathname))
1818 (concatenate 'simple-string
1819 (logical-host-name (%pathname-host pathname)) ":"
1820 (unparse-logical-directory pathname)
1821 (unparse-unix-file pathname)))
1822
1823
1824 ;;;; Logical pathname translations:
1825
1826 ;;; CANONICALIZE-LOGICAL-PATHNAME-TRANSLATIONS -- Internal
1827 ;;;
1828 ;;; Verify that the list of translations consists of lists and prepare
1829 ;;; canonical translations (parse pathnames and expand out wildcards into
1830 ;;; patterns).
1831 ;;;
1832 (defun canonicalize-logical-pathname-translations (transl-list host)
1833 (declare (type list transl-list) (type host host)
1834 (values list))
1835 (collect ((res))
1836 (dolist (tr transl-list)
1837 (unless (and (consp tr) (= (length tr) 2))
1838 (error "Logical pathname translation is not a two-list:~% ~S"
1839 tr))
1840 (let ((from (first tr)))
1841 (res (list (if (typep from 'logical-pathname)
1842 from
1843 (parse-namestring from host))
1844 (pathname (second tr))))))
1845 (res)))
1846
1847
1848 ;;; LOGICAL-PATHNAME-TRANSLATIONS -- Public
1849 ;;;
1850 (defun logical-pathname-translations (host)
1851 "Return the (logical) host object argument's list of translations."
1852 (declare (type (or string logical-host) host)
1853 (values list))
1854 (logical-host-translations (find-logical-host host)))
1855
1856 ;;; (SETF LOGICAL-PATHNAME-TRANSLATIONS) -- Public
1857 ;;;
1858 (defun (setf logical-pathname-translations) (translations host)
1859 "Set the translations list for the logical host argument.
1860 Return translations."
1861 (declare (type (or string logical-host) host)
1862 (type list translations)
1863 (values list))
1864
1865 (let ((host (intern-logical-host host)))
1866 (setf (logical-host-canon-transls host)
1867 (canonicalize-logical-pathname-translations translations host))
1868 (setf (logical-host-translations host) translations)))
1869
1870
1871 ;;; The search mechanism for loading pathname translations uses the CMUCL
1872 ;;; extension of search-lists. The user can add to the library: search-list
1873 ;;; using setf. The file for translations should have the name defined by
1874 ;;; the hostname (a string) and with type component "translations".
1875
1876 ;;; LOAD-LOGICAL-PATHNAME-TRANSLATIONS -- Public
1877 ;;;
1878 (defun load-logical-pathname-translations (host)
1879 "Search for a logical pathname named host, if not already defined. If already
1880 defined no attempt to find or load a definition is attempted and NIL is
1881 returned. If host is not already defined, but definition is found and loaded
1882 successfully, T is returned, else error."
1883 (declare (type string host)
1884 (values (member t nil)))
1885 (let ((*autoload-translations* nil))
1886 (unless (or (string-equal host "library")
1887 (find-logical-host host nil))
1888 (with-open-file (in-str (make-pathname :defaults "library:"
1889 :name (string-downcase host)
1890 :type "translations"))
1891 (if *load-verbose*
1892 (format *error-output*
1893 ";; Loading pathname translations from ~A~%"
1894 (namestring (truename in-str))))
1895 (setf (logical-pathname-translations host) (read in-str)))
1896 t)))
1897
1898 ;;; TRANSLATE-LOGICAL-PATHNAME -- Public
1899 ;;;
1900 (defun translate-logical-pathname (pathname &key)
1901 "Translates pathname to a physical pathname, which is returned."
1902 (declare (type path-designator pathname)
1903 (values (or null pathname)))
1904 (typecase pathname
1905 (logical-pathname
1906 (dolist (x (logical-host-canon-transls (%pathname-host pathname))
1907 (error 'simple-file-error
1908 :pathname pathname
1909 :format-control "No translation for ~S"
1910 :format-arguments (list pathname)))
1911 (destructuring-bind (from to) x
1912 (when (pathname-match-p pathname from)
1913 (return (translate-logical-pathname
1914 (translate-pathname pathname from to)))))))
1915 (pathname pathname)
1916 (stream (translate-logical-pathname (pathname pathname)))
1917 (t (translate-logical-pathname (logical-pathname pathname)))))
1918
1919 (defvar *logical-pathname-defaults*
1920 (%make-logical-pathname (make-logical-host :name "BOGUS") :unspecific
1921 nil nil nil nil))

  ViewVC Help
Powered by ViewVC 1.1.5