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

Contents of /src/code/pathname.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5