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

Contents of /src/code/pathname.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5