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

Contents of /src/code/pathname.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.48 - (show annotations)
Tue Mar 27 20:23:45 2001 UTC (13 years ago) by pw
Branch: MAIN
Changes since 1.47: +9 -4 lines
Marco Antoniotti says:

PARSE-NAMESTRING is supposed to accept also a string for the optional
HOST argument.  Right now this is not the case.

The following patch fixes this and seems to do the right thing when
HOST is a defined logical pathname host.

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

  ViewVC Help
Powered by ViewVC 1.1.5