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

Contents of /src/code/pathname.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5