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

Contents of /src/code/pathname.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5