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

Contents of /src/code/pathname.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5