0.6.9.5:
[sbcl.git] / src / code / target-pathname.lisp
1 ;;;; machine/filesystem-independent pathname functions
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
11
12 (in-package "SB!IMPL")
13
14 #!-sb-fluid (declaim (freeze-type logical-pathname logical-host))
15 \f
16 ;;; host methods
17
18 (def!method print-object ((host host) stream)
19   (print-unreadable-object (host stream :type t :identity t)))
20 \f
21 ;;; pathname methods
22
23 (def!method print-object ((pathname pathname) stream)
24   (let ((namestring (handler-case (namestring pathname)
25                       (error nil))))
26     (if namestring
27         (format stream "#P~S" namestring)
28         ;; FIXME: This code was rewritten and should be tested. (How does
29         ;; control get to this case anyhow? Perhaps we could just punt it?)
30         (print-unreadable-object (pathname stream :type t)
31           (format stream
32                   "(with no namestring) :HOST ~S :DEVICE ~S :DIRECTORY ~S ~
33                   :NAME ~S :TYPE ~S :VERSION ~S"
34                   (%pathname-host pathname)
35                   (%pathname-device pathname)
36                   (%pathname-directory pathname)
37                   (%pathname-name pathname)
38                   (%pathname-type pathname)
39                   (%pathname-version pathname))))))
40
41 (def!method make-load-form ((pathname pathname) &optional environment)
42   (make-load-form-saving-slots pathname :environment environment))
43
44 ;;; The potential conflict with search-lists requires isolating the printed
45 ;;; representation to use the i/o macro #.(logical-pathname <path-designator>).
46 ;;;
47 ;;; FIXME: We don't use search lists any more, so that comment is stale, right?
48 (def!method print-object ((pathname logical-pathname) stream)
49   (let ((namestring (handler-case (namestring pathname)
50                       (error nil))))
51     (if namestring
52         (format stream "#.(logical-pathname ~S)" namestring)
53         (print-unreadable-object (pathname stream :type t)
54           (format stream
55                   ":HOST ~S :DIRECTORY ~S :FILE ~S :NAME=~S :VERSION ~S"
56                   (%pathname-host pathname)
57                   (%pathname-directory pathname)
58                   (%pathname-name pathname)
59                   (%pathname-type pathname)
60                   (%pathname-version pathname))))))
61 \f
62 ;;; A pathname is logical if the host component is a logical-host.
63 ;;; This constructor is used to make an instance of the correct type
64 ;;; from parsed arguments.
65 (defun %make-pathname-object (host device directory name type version)
66   (if (typep host 'logical-host)
67       (%make-logical-pathname host :unspecific directory name type version)
68       (%make-pathname    host device      directory name type version)))
69
70 ;;; Hash table searching maps a logical-pathname's host to their physical
71 ;;; pathname translation.
72 (defvar *logical-hosts* (make-hash-table :test 'equal))
73 \f
74 ;;;; patterns
75
76 (def!method make-load-form ((pattern pattern) &optional environment)
77   (make-load-form-saving-slots pattern :environment environment))
78
79 (def!method print-object ((pattern pattern) stream)
80   (print-unreadable-object (pattern stream :type t)
81     (if *print-pretty*
82         (let ((*print-escape* t))
83           (pprint-fill stream (pattern-pieces pattern) nil))
84         (prin1 (pattern-pieces pattern) stream))))
85
86 (defun pattern= (pattern1 pattern2)
87   (declare (type pattern pattern1 pattern2))
88   (let ((pieces1 (pattern-pieces pattern1))
89         (pieces2 (pattern-pieces pattern2)))
90     (and (= (length pieces1) (length pieces2))
91          (every #'(lambda (piece1 piece2)
92                     (typecase piece1
93                       (simple-string
94                        (and (simple-string-p piece2)
95                             (string= piece1 piece2)))
96                       (cons
97                        (and (consp piece2)
98                             (eq (car piece1) (car piece2))
99                             (string= (cdr piece1) (cdr piece2))))
100                       (t
101                        (eq piece1 piece2))))
102                 pieces1
103                 pieces2))))
104
105 ;;; If the string matches the pattern returns the multiple values T and a
106 ;;; list of the matched strings.
107 (defun pattern-matches (pattern string)
108   (declare (type pattern pattern)
109            (type simple-string string))
110   (let ((len (length string)))
111     (labels ((maybe-prepend (subs cur-sub chars)
112                (if cur-sub
113                    (let* ((len (length chars))
114                           (new (make-string len))
115                           (index len))
116                      (dolist (char chars)
117                        (setf (schar new (decf index)) char))
118                      (cons new subs))
119                    subs))
120              (matches (pieces start subs cur-sub chars)
121                (if (null pieces)
122                    (if (= start len)
123                        (values t (maybe-prepend subs cur-sub chars))
124                        (values nil nil))
125                    (let ((piece (car pieces)))
126                      (etypecase piece
127                        (simple-string
128                         (let ((end (+ start (length piece))))
129                           (and (<= end len)
130                                (string= piece string
131                                         :start2 start :end2 end)
132                                (matches (cdr pieces) end
133                                         (maybe-prepend subs cur-sub chars)
134                                         nil nil))))
135                        (list
136                         (ecase (car piece)
137                           (:character-set
138                            (and (< start len)
139                                 (let ((char (schar string start)))
140                                   (if (find char (cdr piece) :test #'char=)
141                                       (matches (cdr pieces) (1+ start) subs t
142                                                (cons char chars))))))))
143                        ((member :single-char-wild)
144                         (and (< start len)
145                              (matches (cdr pieces) (1+ start) subs t
146                                       (cons (schar string start) chars))))
147                        ((member :multi-char-wild)
148                         (multiple-value-bind (won new-subs)
149                             (matches (cdr pieces) start subs t chars)
150                           (if won
151                               (values t new-subs)
152                               (and (< start len)
153                                    (matches pieces (1+ start) subs t
154                                             (cons (schar string start)
155                                                   chars)))))))))))
156       (multiple-value-bind (won subs)
157           (matches (pattern-pieces pattern) 0 nil nil nil)
158         (values won (reverse subs))))))
159
160 ;;; Pathname-match-p for directory components.
161 (defun directory-components-match (thing wild)
162   (or (eq thing wild)
163       (eq wild :wild)
164       (and (consp wild)
165            (let ((wild1 (first wild)))
166              (if (eq wild1 :wild-inferiors)
167                  (let ((wild-subdirs (rest wild)))
168                    (or (null wild-subdirs)
169                        (loop
170                          (when (directory-components-match thing wild-subdirs)
171                            (return t))
172                          (pop thing)
173                          (unless thing (return nil)))))
174                  (and (consp thing)
175                       (components-match (first thing) wild1)
176                       (directory-components-match (rest thing)
177                                                   (rest wild))))))))
178
179 ;;; Return true if pathname component THING is matched by WILD. (not
180 ;;; commutative)
181 (defun components-match (thing wild)
182   (declare (type (or pattern symbol simple-string integer) thing wild))
183   (or (eq thing wild)
184       (eq wild :wild)
185       (typecase thing
186         (simple-base-string
187          ;; String is matched by itself, a matching pattern or :WILD.
188          (typecase wild
189            (pattern
190             (values (pattern-matches wild thing)))
191            (simple-base-string
192             (string= thing wild))))
193         (pattern
194          ;; A pattern is only matched by an identical pattern.
195          (and (pattern-p wild) (pattern= thing wild)))
196         (integer
197          ;; an integer (version number) is matched by :WILD or the same
198          ;; integer. This branch will actually always be NIL as long as the
199          ;; version is a fixnum.
200          (eql thing wild)))))
201
202 ;;; A predicate for comparing two pathname slot component sub-entries.
203 (defun compare-component (this that)
204   (or (eql this that)
205       (typecase this
206         (simple-string
207          (and (simple-string-p that)
208               (string= this that)))
209         (pattern
210          (and (pattern-p that)
211               (pattern= this that)))
212         (cons
213          (and (consp that)
214               (compare-component (car this) (car that))
215               (compare-component (cdr this) (cdr that)))))))
216 \f
217 ;;;; pathname functions
218
219 (defun pathname= (pathname1 pathname2)
220   (declare (type pathname pathname1)
221            (type pathname pathname2))
222   (and (eq (%pathname-host pathname1)
223            (%pathname-host pathname2))
224        (compare-component (%pathname-device pathname1)
225                           (%pathname-device pathname2))
226        (compare-component (%pathname-directory pathname1)
227                           (%pathname-directory pathname2))
228        (compare-component (%pathname-name pathname1)
229                           (%pathname-name pathname2))
230        (compare-component (%pathname-type pathname1)
231                           (%pathname-type pathname2))
232        (compare-component (%pathname-version pathname1)
233                           (%pathname-version pathname2))))
234
235 ;;; Convert PATHNAME-DESIGNATOR (a pathname, or string, or
236 ;;; stream), into a pathname in pathname.
237 ;;;
238 ;;; FIXME: was rewritten, should be tested (or rewritten again, this
239 ;;; time using ONCE-ONLY, *then* tested)
240 ;;; FIXME: become SB!XC:DEFMACRO inside EVAL-WHEN (COMPILE EVAL)?
241 (defmacro with-pathname ((pathname pathname-designator) &body body)
242   (let ((pd0 (gensym)))
243     `(let* ((,pd0 ,pathname-designator)
244             (,pathname (etypecase ,pd0
245                          (pathname ,pd0)
246                          (string (parse-namestring ,pd0))
247                          (stream (file-name ,pd0)))))
248        ,@body)))
249
250 ;;; Converts the var, a host or string name for a host, into a logical-host
251 ;;; structure or nil if not defined.
252 ;;;
253 ;;; pw notes 1/12/97 this potentially useful macro is not used anywhere
254 ;;; and 'find-host' is not defined. 'find-logical-host' seems to be needed.
255 #|
256 (defmacro with-host ((var expr) &body body)
257   `(let ((,var (let ((,var ,expr))
258                  (typecase ,var
259                    (logical-host ,var)
260                    (string (find-logical-host ,var nil))
261                    (t nil)))))
262      ,@body))
263 |#
264
265 (defun pathname (thing)
266   #!+sb-doc
267   "Convert thing (a pathname, string or stream) into a pathname."
268   (declare (type pathname-designator thing))
269   (with-pathname (pathname thing)
270     pathname))
271
272 ;;; Change the case of thing if DIDDLE-P.
273 (defun maybe-diddle-case (thing diddle-p)
274   (if (and diddle-p (not (or (symbolp thing) (integerp thing))))
275       (labels ((check-for (pred in)
276                  (typecase in
277                    (pattern
278                     (dolist (piece (pattern-pieces in))
279                       (when (typecase piece
280                               (simple-string
281                                (check-for pred piece))
282                               (cons
283                                (case (car in)
284                                  (:character-set
285                                   (check-for pred (cdr in))))))
286                         (return t))))
287                    (list
288                     (dolist (x in)
289                       (when (check-for pred x)
290                         (return t))))
291                    (simple-base-string
292                     (dotimes (i (length in))
293                       (when (funcall pred (schar in i))
294                         (return t))))
295                    (t nil)))
296                (diddle-with (fun thing)
297                  (typecase thing
298                    (pattern
299                     (make-pattern
300                      (mapcar #'(lambda (piece)
301                                  (typecase piece
302                                    (simple-base-string
303                                     (funcall fun piece))
304                                    (cons
305                                     (case (car piece)
306                                       (:character-set
307                                        (cons :character-set
308                                              (funcall fun (cdr piece))))
309                                       (t
310                                        piece)))
311                                    (t
312                                     piece)))
313                              (pattern-pieces thing))))
314                    (list
315                     (mapcar fun thing))
316                    (simple-base-string
317                     (funcall fun thing))
318                    (t
319                     thing))))
320         (let ((any-uppers (check-for #'upper-case-p thing))
321               (any-lowers (check-for #'lower-case-p thing)))
322           (cond ((and any-uppers any-lowers)
323                  ;; Mixed case, stays the same.
324                  thing)
325                 (any-uppers
326                  ;; All uppercase, becomes all lower case.
327                  (diddle-with #'(lambda (x) (if (stringp x)
328                                                 (string-downcase x)
329                                                 x)) thing))
330                 (any-lowers
331                  ;; All lowercase, becomes all upper case.
332                  (diddle-with #'(lambda (x) (if (stringp x)
333                                                 (string-upcase x)
334                                                 x)) thing))
335                 (t
336                  ;; No letters?  I guess just leave it.
337                  thing))))
338       thing))
339
340 (defun merge-directories (dir1 dir2 diddle-case)
341   (if (or (eq (car dir1) :absolute)
342           (null dir2))
343       dir1
344       (let ((results nil))
345         (flet ((add (dir)
346                  (if (and (eq dir :back)
347                           results
348                           (not (eq (car results) :back)))
349                      (pop results)
350                      (push dir results))))
351           (dolist (dir (maybe-diddle-case dir2 diddle-case))
352             (add dir))
353           (dolist (dir (cdr dir1))
354             (add dir)))
355         (reverse results))))
356
357 (defun merge-pathnames (pathname
358                         &optional
359                         (defaults *default-pathname-defaults*)
360                         (default-version :newest))
361   #!+sb-doc
362   "Construct a filled in pathname by completing the unspecified components
363    from the defaults."
364   (declare (type pathname-designator pathname)
365            (type pathname-designator defaults)
366            (values pathname))
367   (with-pathname (defaults defaults)
368     (let ((pathname (let ((*default-pathname-defaults* defaults))
369                       (pathname pathname))))
370       (let* ((default-host (%pathname-host defaults))
371              (pathname-host (%pathname-host pathname))
372              (diddle-case
373               (and default-host pathname-host
374                    (not (eq (host-customary-case default-host)
375                             (host-customary-case pathname-host))))))
376         (%make-pathname-object
377          (or pathname-host default-host)
378          (or (%pathname-device pathname)
379              (maybe-diddle-case (%pathname-device defaults)
380                                 diddle-case))
381          (merge-directories (%pathname-directory pathname)
382                             (%pathname-directory defaults)
383                             diddle-case)
384          (or (%pathname-name pathname)
385              (maybe-diddle-case (%pathname-name defaults)
386                                 diddle-case))
387          (or (%pathname-type pathname)
388              (maybe-diddle-case (%pathname-type defaults)
389                                 diddle-case))
390          (or (%pathname-version pathname)
391              default-version))))))
392
393 (defun import-directory (directory diddle-case)
394   (etypecase directory
395     (null nil)
396     ((member :wild) '(:absolute :wild-inferiors))
397     ((member :unspecific) '(:relative))
398     (list
399      (collect ((results))
400        (ecase (pop directory)
401          (:absolute
402           (results :absolute)
403           (when (search-list-p (car directory))
404             (results (pop directory))))
405          (:relative
406           (results :relative)))
407        (dolist (piece directory)
408          (cond ((member piece '(:wild :wild-inferiors :up :back))
409                 (results piece))
410                ((or (simple-string-p piece) (pattern-p piece))
411                 (results (maybe-diddle-case piece diddle-case)))
412                ((stringp piece)
413                 (results (maybe-diddle-case (coerce piece 'simple-string)
414                                             diddle-case)))
415                (t
416                 (error "~S is not allowed as a directory component." piece))))
417        (results)))
418     (simple-string
419      `(:absolute
420        ,(maybe-diddle-case directory diddle-case)))
421     (string
422      `(:absolute
423        ,(maybe-diddle-case (coerce directory 'simple-string)
424                            diddle-case)))))
425
426 (defun make-pathname (&key host
427                            (device nil devp)
428                            (directory nil dirp)
429                            (name nil namep)
430                            (type nil typep)
431                            (version nil versionp)
432                            defaults
433                            (case :local))
434   #!+sb-doc
435   "Makes a new pathname from the component arguments. Note that host is
436 a host-structure or string."
437   (declare (type (or string host component-tokens) host)
438            (type (or string component-tokens) device)
439            (type (or list string pattern component-tokens) directory)
440            (type (or string pattern component-tokens) name type)
441            (type (or integer component-tokens (member :newest)) version)
442            (type (or pathname-designator null) defaults)
443            (type (member :common :local) case))
444   (let* ((defaults (when defaults
445                      (with-pathname (defaults defaults) defaults)))
446          (default-host (if defaults
447                            (%pathname-host defaults)
448                            (pathname-host *default-pathname-defaults*)))
449          ;; toy@rtp.ericsson.se: CLHS says make-pathname can take a
450          ;; string (as a logical-host) for the host part. We map that
451          ;; string into the corresponding logical host structure.
452
453          ;; pw@snoopy.mv.com:
454          ;; HyperSpec says for the arg to MAKE-PATHNAME;
455          ;; "host---a valid physical pathname host. ..."
456          ;; where it probably means -- a valid pathname host.
457          ;; "valid pathname host n. a valid physical pathname host or
458          ;; a valid logical pathname host."
459          ;; and defines
460          ;; "valid physical pathname host n. any of a string,
461          ;; a list of strings, or the symbol :unspecific,
462          ;; that is recognized by the implementation as the name of a host."
463          ;; "valid logical pathname host n. a string that has been defined
464          ;; as the name of a logical host. ..."
465          ;; HS is silent on what happens if the :host arg is NOT one of these.
466          ;; It seems an error message is appropriate.
467          (host (typecase host
468                  (host host)            ; A valid host, use it.
469                  (string (find-logical-host host t)) ; logical-host or lose.
470                  (t default-host)))     ; unix-host
471          (diddle-args (and (eq (host-customary-case host) :lower)
472                            (eq case :common)))
473          (diddle-defaults
474           (not (eq (host-customary-case host)
475                    (host-customary-case default-host))))
476          (dev (if devp device (if defaults (%pathname-device defaults))))
477          (dir (import-directory directory diddle-args))
478          (ver (cond
479                (versionp version)
480                (defaults (%pathname-version defaults))
481                (t nil))))
482     (when (and defaults (not dirp))
483       (setf dir
484             (merge-directories dir
485                                (%pathname-directory defaults)
486                                diddle-defaults)))
487
488     (macrolet ((pick (var varp field)
489                  `(cond ((or (simple-string-p ,var)
490                              (pattern-p ,var))
491                          (maybe-diddle-case ,var diddle-args))
492                         ((stringp ,var)
493                          (maybe-diddle-case (coerce ,var 'simple-string)
494                                             diddle-args))
495                         (,varp
496                          (maybe-diddle-case ,var diddle-args))
497                         (defaults
498                          (maybe-diddle-case (,field defaults)
499                                             diddle-defaults))
500                         (t
501                          nil))))
502       (%make-pathname-object host
503                              dev ; forced to :unspecific when logical-host
504                              dir
505                              (pick name namep %pathname-name)
506                              (pick type typep %pathname-type)
507                              ver))))
508
509 (defun pathname-host (pathname &key (case :local))
510   #!+sb-doc
511   "Accessor for the pathname's host."
512   (declare (type pathname-designator pathname)
513            (type (member :local :common) case)
514            (values host)
515            (ignore case))
516   (with-pathname (pathname pathname)
517     (%pathname-host pathname)))
518
519 (defun pathname-device (pathname &key (case :local))
520   #!+sb-doc
521   "Accessor for pathname's device."
522   (declare (type pathname-designator pathname)
523            (type (member :local :common) case))
524   (with-pathname (pathname pathname)
525     (maybe-diddle-case (%pathname-device pathname)
526                        (and (eq case :common)
527                             (eq (host-customary-case
528                                  (%pathname-host pathname))
529                                 :lower)))))
530
531 (defun pathname-directory (pathname &key (case :local))
532   #!+sb-doc
533   "Accessor for the pathname's directory list."
534   (declare (type pathname-designator pathname)
535            (type (member :local :common) case))
536   (with-pathname (pathname pathname)
537     (maybe-diddle-case (%pathname-directory pathname)
538                        (and (eq case :common)
539                             (eq (host-customary-case
540                                  (%pathname-host pathname))
541                                 :lower)))))
542 (defun pathname-name (pathname &key (case :local))
543   #!+sb-doc
544   "Accessor for the pathname's name."
545   (declare (type pathname-designator pathname)
546            (type (member :local :common) case))
547   (with-pathname (pathname pathname)
548     (maybe-diddle-case (%pathname-name pathname)
549                        (and (eq case :common)
550                             (eq (host-customary-case
551                                  (%pathname-host pathname))
552                                 :lower)))))
553
554 ;;; PATHNAME-TYPE
555 (defun pathname-type (pathname &key (case :local))
556   #!+sb-doc
557   "Accessor for the pathname's name."
558   (declare (type pathname-designator pathname)
559            (type (member :local :common) case))
560   (with-pathname (pathname pathname)
561     (maybe-diddle-case (%pathname-type pathname)
562                        (and (eq case :common)
563                             (eq (host-customary-case
564                                  (%pathname-host pathname))
565                                 :lower)))))
566
567 ;;; PATHNAME-VERSION
568 (defun pathname-version (pathname)
569   #!+sb-doc
570   "Accessor for the pathname's version."
571   (declare (type pathname-designator pathname))
572   (with-pathname (pathname pathname)
573     (%pathname-version pathname)))
574 \f
575 ;;;; namestrings
576
577 (defun %print-namestring-parse-error (condition stream)
578   (format stream "Parse error in namestring: ~?~%  ~A~%  ~V@T^"
579           (namestring-parse-error-complaint condition)
580           (namestring-parse-error-arguments condition)
581           (namestring-parse-error-namestring condition)
582           (namestring-parse-error-offset condition)))
583
584 ;;; Handle the case where PARSE-NAMESTRING is actually parsing a
585 ;;; namestring. We pick off the :JUNK-ALLOWED case then find a host to
586 ;;; use for parsing, call the parser, then check whether the host
587 ;;; matches.
588 (defun %parse-namestring (namestr host defaults start end junk-allowed)
589   (declare (type (or host null) host) (type string namestr)
590            (type index start) (type (or index null) end))
591   (if junk-allowed
592       (handler-case
593           (%parse-namestring namestr host defaults start end nil)
594         (namestring-parse-error (condition)
595           (values nil (namestring-parse-error-offset condition))))
596       (let* ((end (or end (length namestr)))
597              (parse-host (or host
598                              (extract-logical-host-prefix namestr start end)
599                              (pathname-host defaults))))
600         (unless parse-host
601           (error "When HOST argument is not supplied, DEFAULTS arg must ~
602                   have a non-null PATHNAME-HOST."))
603
604         (multiple-value-bind (new-host device directory file type version)
605             (funcall (host-parse parse-host) namestr start end)
606           (when (and host new-host (not (eq new-host host)))
607             (error "The host in the namestring, ~S,~@
608                     does not match explicit host argument: ~S"
609                    host))
610           (let ((pn-host (or new-host parse-host)))
611             (values (%make-pathname-object
612                      pn-host device directory file type version)
613                     end))))))
614
615 ;;; If namestr begins with a colon-terminated, defined, logical host, then
616 ;;; return that host, otherwise return NIL.
617 (defun extract-logical-host-prefix (namestr start end)
618   (declare (type simple-base-string namestr)
619            (type index start end)
620            (values (or logical-host null)))
621   (let ((colon-pos (position #\: namestr :start start :end end)))
622     (if colon-pos
623         (values (gethash (nstring-upcase (subseq namestr start colon-pos))
624                          *logical-hosts*))
625         nil)))
626
627 (defun parse-namestring (thing
628                          &optional
629                          host
630                          (defaults *default-pathname-defaults*)
631                          &key (start 0) end junk-allowed)
632   #!+sb-doc
633   "Converts pathname, a pathname designator, into a pathname structure,
634    for a physical pathname, returns the printed representation. Host may be
635    a physical host structure or host namestring."
636   (declare (type pathname-designator thing)
637            (type (or null host) host)
638            (type pathname defaults)
639            (type index start)
640            (type (or index null) end)
641            (type (or t null) junk-allowed)
642            (values (or null pathname) (or null index)))
643     (typecase thing
644       (simple-string
645        (%parse-namestring thing host defaults start end junk-allowed))
646       (string
647        (%parse-namestring (coerce thing 'simple-string)
648                           host defaults start end junk-allowed))
649       (pathname
650        (let ((host (if host host (%pathname-host defaults))))
651          (unless (eq host (%pathname-host thing))
652            (error "Hosts do not match: ~S and ~S."
653                   host (%pathname-host thing))))
654        (values thing start))
655       (stream
656        (let ((name (file-name thing)))
657          (unless name
658            (error "can't figure out the file associated with stream:~%  ~S"
659                   thing))
660          name))))
661
662 (defun namestring (pathname)
663   #!+sb-doc
664   "Construct the full (name)string form of the pathname."
665   (declare (type pathname-designator pathname)
666            (values (or null simple-base-string)))
667   (with-pathname (pathname pathname)
668     (when pathname
669       (let ((host (%pathname-host pathname)))
670         (unless host
671           (error "can't determine the namestring for pathnames with no ~
672                   host:~%  ~S" pathname))
673         (funcall (host-unparse host) pathname)))))
674
675 (defun host-namestring (pathname)
676   #!+sb-doc
677   "Returns a string representation of the name of the host in the pathname."
678   (declare (type pathname-designator pathname)
679            (values (or null simple-base-string)))
680   (with-pathname (pathname pathname)
681     (let ((host (%pathname-host pathname)))
682       (if host
683           (funcall (host-unparse-host host) pathname)
684           (error
685            "can't determine the namestring for pathnames with no host:~%  ~S"
686            pathname)))))
687
688 (defun directory-namestring (pathname)
689   #!+sb-doc
690   "Returns a string representation of the directories used in the pathname."
691   (declare (type pathname-designator pathname)
692            (values (or null simple-base-string)))
693   (with-pathname (pathname pathname)
694     (let ((host (%pathname-host pathname)))
695       (if host
696           (funcall (host-unparse-directory host) pathname)
697           (error
698            "can't determine the namestring for pathnames with no host:~%  ~S"
699            pathname)))))
700
701 (defun file-namestring (pathname)
702   #!+sb-doc
703   "Returns a string representation of the name used in the pathname."
704   (declare (type pathname-designator pathname)
705            (values (or null simple-base-string)))
706   (with-pathname (pathname pathname)
707     (let ((host (%pathname-host pathname)))
708       (if host
709           (funcall (host-unparse-file host) pathname)
710           (error
711            "can't determine the namestring for pathnames with no host:~%  ~S"
712            pathname)))))
713
714 (defun enough-namestring (pathname
715                           &optional
716                           (defaults *default-pathname-defaults*))
717   #!+sb-doc
718   "Returns an abbreviated pathname sufficent to identify the pathname relative
719    to the defaults."
720   (declare (type pathname-designator pathname))
721   (with-pathname (pathname pathname)
722     (let ((host (%pathname-host pathname)))
723       (if host
724           (with-pathname (defaults defaults)
725             (funcall (host-unparse-enough host) pathname defaults))
726           (error
727            "can't determine the namestring for pathnames with no host:~%  ~S"
728            pathname)))))
729 \f
730 ;;;; wild pathnames
731
732 (defun wild-pathname-p (pathname &optional field-key)
733   #!+sb-doc
734   "Predicate for determining whether pathname contains any wildcards."
735   (declare (type pathname-designator pathname)
736            (type (member nil :host :device :directory :name :type :version)
737                  field-key))
738   (with-pathname (pathname pathname)
739     (flet ((frob (x)
740              (or (pattern-p x) (member x '(:wild :wild-inferiors)))))
741       (ecase field-key
742         ((nil)
743          (or (wild-pathname-p pathname :host)
744              (wild-pathname-p pathname :device)
745              (wild-pathname-p pathname :directory)
746              (wild-pathname-p pathname :name)
747              (wild-pathname-p pathname :type)
748              (wild-pathname-p pathname :version)))
749         (:host (frob (%pathname-host pathname)))
750         (:device (frob (%pathname-host pathname)))
751         (:directory (some #'frob (%pathname-directory pathname)))
752         (:name (frob (%pathname-name pathname)))
753         (:type (frob (%pathname-type pathname)))
754         (:version (frob (%pathname-version pathname)))))))
755
756 (defun pathname-match-p (in-pathname in-wildname)
757   #!+sb-doc
758   "Pathname matches the wildname template?"
759   (declare (type pathname-designator in-pathname))
760   (with-pathname (pathname in-pathname)
761     (with-pathname (wildname in-wildname)
762       (macrolet ((frob (field &optional (op 'components-match ))
763                    `(or (null (,field wildname))
764                         (,op (,field pathname) (,field wildname)))))
765         (and (or (null (%pathname-host wildname))
766                  (eq (%pathname-host wildname) (%pathname-host pathname)))
767              (frob %pathname-device)
768              (frob %pathname-directory directory-components-match)
769              (frob %pathname-name)
770              (frob %pathname-type)
771              (frob %pathname-version))))))
772
773 ;;; Place the substitutions into the pattern and return the string or pattern
774 ;;; that results. If DIDDLE-CASE is true, we diddle the result case as well,
775 ;;; in case we are translating between hosts with difference conventional case.
776 ;;; The second value is the tail of subs with all of the values that we used up
777 ;;; stripped off. Note that PATTERN-MATCHES matches all consecutive wildcards
778 ;;; as a single string, so we ignore subsequent contiguous wildcards.
779 (defun substitute-into (pattern subs diddle-case)
780   (declare (type pattern pattern)
781            (type list subs)
782            (values (or simple-base-string pattern)))
783   (let ((in-wildcard nil)
784         (pieces nil)
785         (strings nil))
786     (dolist (piece (pattern-pieces pattern))
787       (cond ((simple-string-p piece)
788              (push piece strings)
789              (setf in-wildcard nil))
790             (in-wildcard)
791             (t
792              (setf in-wildcard t)
793              (unless subs
794                (error "not enough wildcards in FROM pattern to match ~
795                        TO pattern:~%  ~S"
796                       pattern))
797              (let ((sub (pop subs)))
798                (typecase sub
799                  (pattern
800                   (when strings
801                     (push (apply #'concatenate 'simple-string
802                                  (nreverse strings))
803                           pieces))
804                   (dolist (piece (pattern-pieces sub))
805                     (push piece pieces)))
806                  (simple-string
807                   (push sub strings))
808                  (t
809                   (error "can't substitute this into the middle of a word:~
810                           ~%  ~S"
811                          sub)))))))
812
813     (when strings
814       (push (apply #'concatenate 'simple-string (nreverse strings))
815             pieces))
816     (values
817      (maybe-diddle-case
818       (if (and pieces (simple-string-p (car pieces)) (null (cdr pieces)))
819           (car pieces)
820           (make-pattern (nreverse pieces)))
821       diddle-case)
822      subs)))
823
824 ;;; Called when we can't see how source and from matched.
825 (defun didnt-match-error (source from)
826   (error "Pathname components from SOURCE and FROM args to TRANSLATE-PATHNAME~@
827           did not match:~%  ~S ~S"
828          source from))
829
830 ;;; Do TRANSLATE-COMPONENT for all components except host and directory.
831 (defun translate-component (source from to diddle-case)
832   (typecase to
833     (pattern
834      (typecase from
835        (pattern
836         (typecase source
837           (pattern
838            (if (pattern= from source)
839                source
840                (didnt-match-error source from)))
841           (simple-string
842            (multiple-value-bind (won subs) (pattern-matches from source)
843              (if won
844                  (values (substitute-into to subs diddle-case))
845                  (didnt-match-error source from))))
846           (t
847            (maybe-diddle-case source diddle-case))))
848        ((member :wild)
849         (values (substitute-into to (list source) diddle-case)))
850        (t
851         (if (components-match source from)
852             (maybe-diddle-case source diddle-case)
853             (didnt-match-error source from)))))
854     ((member nil :wild)
855      (maybe-diddle-case source diddle-case))
856     (t
857      (if (components-match source from)
858          to
859          (didnt-match-error source from)))))
860
861 ;;; Return a list of all the things that we want to substitute into the TO
862 ;;; pattern (the things matched by from on source.)  When From contains
863 ;;; :WILD-INFERIORS, the result contains a sublist of the matched source
864 ;;; subdirectories.
865 (defun compute-directory-substitutions (orig-source orig-from)
866   (let ((source orig-source)
867         (from orig-from))
868     (collect ((subs))
869       (loop
870         (unless source
871           (unless (every #'(lambda (x) (eq x :wild-inferiors)) from)
872             (didnt-match-error orig-source orig-from))
873           (subs ())
874           (return))
875         (unless from (didnt-match-error orig-source orig-from))
876         (let ((from-part (pop from))
877               (source-part (pop source)))
878           (typecase from-part
879             (pattern
880              (typecase source-part
881                (pattern
882                 (if (pattern= from-part source-part)
883                     (subs source-part)
884                     (didnt-match-error orig-source orig-from)))
885                (simple-string
886                 (multiple-value-bind (won new-subs)
887                     (pattern-matches from-part source-part)
888                   (if won
889                       (dolist (sub new-subs)
890                         (subs sub))
891                       (didnt-match-error orig-source orig-from))))
892                (t
893                 (didnt-match-error orig-source orig-from))))
894             ((member :wild)
895              (subs source-part))
896             ((member :wild-inferiors)
897              (let ((remaining-source (cons source-part source)))
898                (collect ((res))
899                  (loop
900                    (when (directory-components-match remaining-source from)
901                      (return))
902                    (unless remaining-source
903                      (didnt-match-error orig-source orig-from))
904                    (res (pop remaining-source)))
905                  (subs (res))
906                  (setq source remaining-source))))
907             (simple-string
908              (unless (and (simple-string-p source-part)
909                           (string= from-part source-part))
910                (didnt-match-error orig-source orig-from)))
911             (t
912              (didnt-match-error orig-source orig-from)))))
913       (subs))))
914
915 ;;; Called by TRANSLATE-PATHNAME on the directory components of its argument
916 ;;; pathanames to produce the result directory component. If any leaves the
917 ;;; directory NIL, we return the source directory. The :RELATIVE or :ABSOLUTE
918 ;;; is always taken from the source directory.
919 (defun translate-directories (source from to diddle-case)
920   (if (not (and source to from))
921       (or to
922           (mapcar #'(lambda (x) (maybe-diddle-case x diddle-case)) source))
923       (collect ((res))
924         (res (first source))
925         (let ((subs-left (compute-directory-substitutions (rest source)
926                                                           (rest from))))
927           (dolist (to-part (rest to))
928             (typecase to-part
929               ((member :wild)
930                (assert subs-left)
931                (let ((match (pop subs-left)))
932                  (when (listp match)
933                    (error ":WILD-INFERIORS not paired in from and to ~
934                            patterns:~%  ~S ~S" from to))
935                  (res (maybe-diddle-case match diddle-case))))
936               ((member :wild-inferiors)
937                (assert subs-left)
938                (let ((match (pop subs-left)))
939                  (unless (listp match)
940                    (error ":WILD-INFERIORS not paired in from and to ~
941                            patterns:~%  ~S ~S" from to))
942                  (dolist (x match)
943                    (res (maybe-diddle-case x diddle-case)))))
944               (pattern
945                (multiple-value-bind (new new-subs-left)
946                    (substitute-into to-part subs-left diddle-case)
947                  (setf subs-left new-subs-left)
948                  (res new)))
949               (t (res to-part)))))
950         (res))))
951
952 (defun translate-pathname (source from-wildname to-wildname &key)
953   #!+sb-doc
954   "Use the source pathname to translate the from-wildname's wild and
955    unspecified elements into a completed to-pathname based on the to-wildname."
956   (declare (type pathname-designator source from-wildname to-wildname))
957   (with-pathname (source source)
958     (with-pathname (from from-wildname)
959       (with-pathname (to to-wildname)
960           (let* ((source-host (%pathname-host source))
961                  (to-host (%pathname-host to))
962                  (diddle-case
963                   (and source-host to-host
964                        (not (eq (host-customary-case source-host)
965                                 (host-customary-case to-host))))))
966             (macrolet ((frob (field &optional (op 'translate-component))
967                          `(let ((result (,op (,field source)
968                                              (,field from)
969                                              (,field to)
970                                              diddle-case)))
971                             (if (eq result :error)
972                                 (error "~S doesn't match ~S." source from)
973                                 result))))
974               (%make-pathname-object
975                (or to-host source-host)
976                (frob %pathname-device)
977                (frob %pathname-directory translate-directories)
978                (frob %pathname-name)
979                (frob %pathname-type)
980                (frob %pathname-version))))))))
981 \f
982 ;;;; search lists
983
984 (def!struct (search-list (:make-load-form-fun
985                           (lambda (s)
986                             (values `(intern-search-list
987                                       ',(search-list-name s))
988                                     nil))))
989   ;; The name of this search-list. Always stored in lowercase.
990   (name (required-argument) :type simple-string)
991   ;; T if this search-list has been defined. Otherwise NIL.
992   (defined nil :type (member t nil))
993   ;; The list of expansions for this search-list. Each expansion is the list
994   ;; of directory components to use in place of this search-list.
995   (expansions nil :type list))
996 (def!method print-object ((sl search-list) stream)
997   (print-unreadable-object (sl stream :type t)
998     (write-string (search-list-name sl) stream)))
999
1000 ;;; a hash table mapping search-list names to search-list structures
1001 (defvar *search-lists* (make-hash-table :test 'equal))
1002
1003 ;;; When search-lists are encountered in namestrings, they are converted to
1004 ;;; search-list structures right then, instead of waiting until the search
1005 ;;; list used. This allows us to verify ahead of time that there are no
1006 ;;; circularities and makes expansion much quicker.
1007 (defun intern-search-list (name)
1008   (let ((name (string-downcase name)))
1009     (or (gethash name *search-lists*)
1010         (let ((new (make-search-list :name name)))
1011           (setf (gethash name *search-lists*) new)
1012           new))))
1013
1014 ;;; Clear the definition. Note: we can't remove it from the hash-table
1015 ;;; because there may be pathnames still refering to it. So we just clear
1016 ;;; out the expansions and ste defined to NIL.
1017 (defun clear-search-list (name)
1018   #!+sb-doc
1019   "Clear the current definition for the search-list NAME. Returns T if such
1020    a definition existed, and NIL if not."
1021   (let* ((name (string-downcase name))
1022          (search-list (gethash name *search-lists*)))
1023     (when (and search-list (search-list-defined search-list))
1024       (setf (search-list-defined search-list) nil)
1025       (setf (search-list-expansions search-list) nil)
1026       t)))
1027
1028 ;;; Again, we can't actually remove the entries from the hash-table, so we
1029 ;;; just mark them as being undefined.
1030 (defun clear-all-search-lists ()
1031   #!+sb-doc
1032   "Clear the definition for all search-lists. Only use this if you know
1033    what you are doing."
1034   (maphash #'(lambda (name search-list)
1035                (declare (ignore name))
1036                (setf (search-list-defined search-list) nil)
1037                (setf (search-list-expansions search-list) nil))
1038            *search-lists*)
1039   nil)
1040
1041 ;;; Extract the search-list from PATHNAME and return it. If PATHNAME
1042 ;;; doesn't start with a search-list, then either error (if FLAME-IF-NONE
1043 ;;; is true) or return NIL (if FLAME-IF-NONE is false).
1044 (defun extract-search-list (pathname flame-if-none)
1045   (with-pathname (pathname pathname)
1046     (let* ((directory (%pathname-directory pathname))
1047            (search-list (cadr directory)))
1048       (cond ((search-list-p search-list)
1049              search-list)
1050             (flame-if-none
1051              (error "~S doesn't start with a search-list." pathname))
1052             (t
1053              nil)))))
1054
1055 ;;; We have to convert the internal form of the search-list back into a
1056 ;;; bunch of pathnames.
1057 (defun search-list (pathname)
1058   #!+sb-doc
1059   "Return the expansions for the search-list starting PATHNAME. If PATHNAME
1060    does not start with a search-list, then an error is signaled. If
1061    the search-list has not been defined yet, then an error is signaled.
1062    The expansion for a search-list can be set with SETF."
1063   (with-pathname (pathname pathname)
1064     (let ((search-list (extract-search-list pathname t))
1065           (host (pathname-host pathname)))
1066       (if (search-list-defined search-list)
1067           (mapcar #'(lambda (directory)
1068                       (make-pathname :host host
1069                                      :directory (cons :absolute directory)))
1070                   (search-list-expansions search-list))
1071           (error "Search list ~S has not been defined yet." pathname)))))
1072
1073 (defun search-list-defined-p (pathname)
1074   #!+sb-doc
1075   "Returns T if the search-list starting PATHNAME is currently defined, and
1076    NIL otherwise. An error is signaled if PATHNAME does not start with a
1077    search-list."
1078   (with-pathname (pathname pathname)
1079     (search-list-defined (extract-search-list pathname t))))
1080
1081 ;;; Set the expansion for the search-list in PATHNAME. If this would result
1082 ;;; in any circularities, we flame out. If anything goes wrong, we leave the
1083 ;;; old definition intact.
1084 (defun %set-search-list (pathname values)
1085   (let ((search-list (extract-search-list pathname t)))
1086     (labels
1087         ((check (target-list path)
1088            (when (eq search-list target-list)
1089              (error "That would result in a circularity:~%  ~
1090                      ~A~{ -> ~A~} -> ~A"
1091                     (search-list-name search-list)
1092                     (reverse path)
1093                     (search-list-name target-list)))
1094            (when (search-list-p target-list)
1095              (push (search-list-name target-list) path)
1096              (dolist (expansion (search-list-expansions target-list))
1097                (check (car expansion) path))))
1098          (convert (pathname)
1099            (with-pathname (pathname pathname)
1100              (when (or (pathname-name pathname)
1101                        (pathname-type pathname)
1102                        (pathname-version pathname))
1103                (error "Search-lists cannot expand into pathnames that have ~
1104                        a name, type, or ~%version specified:~%  ~S"
1105                       pathname))
1106              (let ((directory (pathname-directory pathname)))
1107                (let ((expansion
1108                       (if directory
1109                           (ecase (car directory)
1110                             (:absolute (cdr directory))
1111                             (:relative (cons (intern-search-list "default")
1112                                              (cdr directory))))
1113                           (list (intern-search-list "default")))))
1114                  (check (car expansion) nil)
1115                  expansion)))))
1116       (setf (search-list-expansions search-list)
1117             (if (listp values)
1118               (mapcar #'convert values)
1119               (list (convert values)))))
1120     (setf (search-list-defined search-list) t))
1121   values)
1122
1123 (defun %enumerate-search-list (pathname function)
1124   (/show0 "entering %ENUMERATE-SEARCH-LIST")
1125   (let* ((pathname (if (typep pathname 'logical-pathname)
1126                        (translate-logical-pathname pathname)
1127                        pathname))
1128          (search-list (extract-search-list pathname nil)))
1129     (/show0 "PATHNAME and SEARCH-LIST computed")
1130     (cond
1131      ((not search-list)
1132       (/show0 "no search list")
1133       (funcall function pathname))
1134      ((not (search-list-defined search-list))
1135       (/show0 "undefined search list")
1136       (error "undefined search list: ~A"
1137              (search-list-name search-list)))
1138      (t
1139       (/show0 "general case")
1140       (let ((tail (cddr (pathname-directory pathname))))
1141         (/show0 "TAIL computed")
1142         (dolist (expansion
1143                  (search-list-expansions search-list))
1144           (/show0 "tail recursing in %ENUMERATE-SEARCH-LIST")
1145           (%enumerate-search-list (make-pathname :defaults pathname
1146                                                  :directory
1147                                                  (cons :absolute
1148                                                        (append expansion
1149                                                                tail)))
1150                                   function)))))))
1151 \f
1152 ;;;;  logical pathname support. ANSI 92-102 specification.
1153 ;;;;  As logical-pathname translations are loaded they are canonicalized as
1154 ;;;;  patterns to enable rapid efficent translation into physical pathnames.
1155
1156 ;;;; utilities
1157
1158 ;;; Canonicalize a logical pathanme word by uppercasing it checking that it
1159 ;;; contains only legal characters.
1160 (defun logical-word-or-lose (word)
1161   (declare (string word))
1162   (let ((word (string-upcase word)))
1163     (dotimes (i (length word))
1164       (let ((ch (schar word i)))
1165         (unless (or (alpha-char-p ch) (digit-char-p ch) (char= ch #\-))
1166           (error 'namestring-parse-error
1167                  :complaint "logical namestring character which ~
1168                              is not alphanumeric or hyphen:~%  ~S"
1169                  :arguments (list ch)
1170                  :namestring word :offset i))))
1171     word))
1172
1173 ;;; Given a logical host or string, return a logical host. If Error-p is
1174 ;;; NIL, then return NIL when no such host exists.
1175 (defun find-logical-host (thing &optional (errorp t))
1176   (etypecase thing
1177     (string
1178      (let ((found (gethash (logical-word-or-lose thing)
1179                            *logical-hosts*)))
1180        (if (or found (not errorp))
1181            found
1182            (error 'simple-file-error
1183                   :pathname thing
1184                   :format-control "logical host not yet defined: ~S"
1185                   :format-arguments (list thing)))))
1186     (logical-host thing)))
1187
1188 ;;; Given a logical host name or host, return a logical host, creating a new
1189 ;;; one if necessary.
1190 (defun intern-logical-host (thing)
1191   (declare (values logical-host))
1192   (or (find-logical-host thing nil)
1193       (let* ((name (logical-word-or-lose thing))
1194              (new (make-logical-host :name name)))
1195         (setf (gethash name *logical-hosts*) new)
1196         new)))
1197 \f
1198 ;;;; logical pathname parsing
1199
1200 ;;; Deal with multi-char wildcards in a logical pathname token.
1201 (defun maybe-make-logical-pattern (namestring chunks)
1202   (let ((chunk (caar chunks)))
1203     (collect ((pattern))
1204       (let ((last-pos 0)
1205             (len (length chunk)))
1206         (declare (fixnum last-pos))
1207         (loop
1208           (when (= last-pos len) (return))
1209           (let ((pos (or (position #\* chunk :start last-pos) len)))
1210             (if (= pos last-pos)
1211                 (when (pattern)
1212                   (error 'namestring-parse-error
1213                          :complaint "double asterisk inside of logical ~
1214                                      word: ~S"
1215                          :arguments (list chunk)
1216                          :namestring namestring
1217                          :offset (+ (cdar chunks) pos)))
1218                 (pattern (subseq chunk last-pos pos)))
1219             (if (= pos len)
1220                 (return)
1221                 (pattern :multi-char-wild))
1222             (setq last-pos (1+ pos)))))
1223         (assert (pattern))
1224         (if (cdr (pattern))
1225             (make-pattern (pattern))
1226             (let ((x (car (pattern))))
1227               (if (eq x :multi-char-wild)
1228                   :wild
1229                   x))))))
1230
1231 ;;; Return a list of conses where the cdr is the start position and the car
1232 ;;; is a string (token) or character (punctuation.)
1233 (defun logical-chunkify (namestr start end)
1234   (collect ((chunks))
1235     (do ((i start (1+ i))
1236          (prev 0))
1237         ((= i end)
1238          (when (> end prev)
1239             (chunks (cons (nstring-upcase (subseq namestr prev end)) prev))))
1240       (let ((ch (schar namestr i)))
1241         (unless (or (alpha-char-p ch) (digit-char-p ch)
1242                     (member ch '(#\- #\*)))
1243           (when (> i prev)
1244             (chunks (cons (nstring-upcase (subseq namestr prev i)) prev)))
1245           (setq prev (1+ i))
1246           (unless (member ch '(#\; #\: #\.))
1247             (error 'namestring-parse-error
1248                    :complaint "illegal character for logical pathname:~%  ~S"
1249                    :arguments (list ch)
1250                    :namestring namestr
1251                    :offset i))
1252           (chunks (cons ch i)))))
1253     (chunks)))
1254
1255 ;;; Break up a logical-namestring, always a string, into its constituent parts.
1256 (defun parse-logical-namestring (namestr start end)
1257   (declare (type simple-base-string namestr)
1258            (type index start end))
1259   (collect ((directory))
1260     (let ((host nil)
1261           (name nil)
1262           (type nil)
1263           (version nil))
1264       (labels ((expecting (what chunks)
1265                  (unless (and chunks (simple-string-p (caar chunks)))
1266                    (error 'namestring-parse-error
1267                           :complaint "expecting ~A, got ~:[nothing~;~S~]."
1268                           :arguments (list what (caar chunks))
1269                           :namestring namestr
1270                           :offset (if chunks (cdar chunks) end)))
1271                  (caar chunks))
1272                (parse-host (chunks)
1273                  (case (caadr chunks)
1274                    (#\:
1275                     (setq host
1276                           (find-logical-host (expecting "a host name" chunks)))
1277                     (parse-relative (cddr chunks)))
1278                    (t
1279                     (parse-relative chunks))))
1280                (parse-relative (chunks)
1281                  (case (caar chunks)
1282                    (#\;
1283                     (directory :relative)
1284                     (parse-directory (cdr chunks)))
1285                    (t
1286                     (directory :absolute) ; Assumption! Maybe revoked later.
1287                     (parse-directory chunks))))
1288                (parse-directory (chunks)
1289                  (case (caadr chunks)
1290                    (#\;
1291                     (directory
1292                      (let ((res (expecting "a directory name" chunks)))
1293                        (cond ((string= res "..") :up)
1294                              ((string= res "**") :wild-inferiors)
1295                              (t
1296                               (maybe-make-logical-pattern namestr chunks)))))
1297                     (parse-directory (cddr chunks)))
1298                    (t
1299                     (parse-name chunks))))
1300                (parse-name (chunks)
1301                  (when chunks
1302                    (expecting "a file name" chunks)
1303                    (setq name (maybe-make-logical-pattern namestr chunks))
1304                    (expecting-dot (cdr chunks))))
1305                (expecting-dot (chunks)
1306                  (when chunks
1307                    (unless (eql (caar chunks) #\.)
1308                      (error 'namestring-parse-error
1309                             :complaint "expecting a dot, got ~S."
1310                             :arguments (list (caar chunks))
1311                             :namestring namestr
1312                             :offset (cdar chunks)))
1313                    (if type
1314                        (parse-version (cdr chunks))
1315                        (parse-type (cdr chunks)))))
1316                (parse-type (chunks)
1317                  (expecting "a file type" chunks)
1318                  (setq type (maybe-make-logical-pattern namestr chunks))
1319                  (expecting-dot (cdr chunks)))
1320                (parse-version (chunks)
1321                  (let ((str (expecting "a positive integer, * or NEWEST"
1322                                        chunks)))
1323                    (cond
1324                     ((string= str "*") (setq version :wild))
1325                     ((string= str "NEWEST") (setq version :newest))
1326                     (t
1327                      (multiple-value-bind (res pos)
1328                          (parse-integer str :junk-allowed t)
1329                        (unless (and res (plusp res))
1330                          (error 'namestring-parse-error
1331                                 :complaint "expected a positive integer, ~
1332                                             got ~S"
1333                                 :arguments (list str)
1334                                 :namestring namestr
1335                                 :offset (+ pos (cdar chunks))))
1336                        (setq version res)))))
1337                  (when (cdr chunks)
1338                    (error 'namestring-parse-error
1339                           :complaint "extra stuff after end of file name"
1340                           :namestring namestr
1341                           :offset (cdadr chunks)))))
1342         (parse-host (logical-chunkify namestr start end)))
1343       (values host :unspecific
1344               (and (not (equal (directory)'(:absolute)))(directory))
1345               name type version))))
1346
1347 ;;; can't defvar here because not all host methods are loaded yet
1348 (declaim (special *logical-pathname-defaults*))
1349
1350 (defun logical-pathname (pathspec)
1351   #!+sb-doc
1352   "Converts the pathspec argument to a logical-pathname and returns it."
1353   (declare (type (or logical-pathname string stream) pathspec)
1354            (values logical-pathname))
1355   (if (typep pathspec 'logical-pathname)
1356       pathspec
1357       (let ((res (parse-namestring pathspec nil *logical-pathname-defaults*)))
1358         (when (eq (%pathname-host res)
1359                   (%pathname-host *logical-pathname-defaults*))
1360           (error "This logical namestring does not specify a host:~%  ~S"
1361                  pathspec))
1362         res)))
1363 \f
1364 ;;;; logical pathname unparsing
1365
1366 (defun unparse-logical-directory (pathname)
1367   (declare (type pathname pathname))
1368   (collect ((pieces))
1369     (let ((directory (%pathname-directory pathname)))
1370       (when directory
1371         (ecase (pop directory)
1372           (:absolute)    ;; Nothing special.
1373           (:relative (pieces ";")))
1374         (dolist (dir directory)
1375           (cond ((or (stringp dir) (pattern-p dir))
1376                  (pieces (unparse-logical-piece dir))
1377                  (pieces ";"))
1378                 ((eq dir :wild)
1379                  (pieces "*;"))
1380                 ((eq dir :wild-inferiors)
1381                  (pieces "**;"))
1382                 (t
1383                  (error "invalid directory component: ~S" dir))))))
1384     (apply #'concatenate 'simple-string (pieces))))
1385
1386 (defun unparse-logical-piece (thing)
1387   (etypecase thing
1388     (simple-string thing)
1389     (pattern
1390      (collect ((strings))
1391        (dolist (piece (pattern-pieces thing))
1392          (etypecase piece
1393            (simple-string (strings piece))
1394            (keyword
1395             (cond ((eq piece :wild-inferiors)
1396                    (strings "**"))
1397                   ((eq piece :multi-char-wild)
1398                    (strings "*"))
1399                   (t (error "invalid keyword: ~S" piece))))))
1400        (apply #'concatenate 'simple-string (strings))))))
1401
1402 (defun unparse-logical-namestring (pathname)
1403   (declare (type logical-pathname pathname))
1404   (concatenate 'simple-string
1405                (logical-host-name (%pathname-host pathname)) ":"
1406                (unparse-logical-directory pathname)
1407                (unparse-unix-file pathname)))
1408 \f
1409 ;;;; logical pathname translations
1410
1411 ;;; Verify that the list of translations consists of lists and prepare
1412 ;;; canonical translations (parse pathnames and expand out wildcards into
1413 ;;; patterns).
1414 (defun canonicalize-logical-pathname-translations (transl-list host)
1415   (declare (type list transl-list) (type host host)
1416            (values list))
1417   (collect ((res))
1418     (dolist (tr transl-list)
1419       (unless (and (consp tr) (= (length tr) 2))
1420         (error "This logical pathname translation is not a two-list:~%  ~S"
1421                tr))
1422       (let ((from (first tr)))
1423         (res (list (if (typep from 'logical-pathname)
1424                        from
1425                        (parse-namestring from host))
1426                    (pathname (second tr))))))
1427     (res)))
1428
1429 (defun logical-pathname-translations (host)
1430   #!+sb-doc
1431   "Return the (logical) host object argument's list of translations."
1432   (declare (type (or string logical-host) host)
1433            (values list))
1434   (logical-host-translations (find-logical-host host)))
1435
1436 (defun (setf logical-pathname-translations) (translations host)
1437   #!+sb-doc
1438   "Set the translations list for the logical host argument.
1439    Return translations."
1440   (declare (type (or string logical-host) host)
1441            (type list translations)
1442            (values list))
1443
1444   (let ((host (intern-logical-host host)))
1445     (setf (logical-host-canon-transls host)
1446           (canonicalize-logical-pathname-translations translations host))
1447     (setf (logical-host-translations host) translations)))
1448
1449 ;;; The search mechanism for loading pathname translations uses the CMU CL
1450 ;;; extension of search-lists. The user can add to the "library:" search-list
1451 ;;; using setf. The file for translations should have the name defined by
1452 ;;; the hostname (a string) and with type component "translations".
1453
1454 (defun load-logical-pathname-translations (host)
1455   #!+sb-doc
1456   "Search for a logical pathname named host, if not already defined. If already
1457    defined no attempt to find or load a definition is attempted and NIL is
1458    returned. If host is not already defined, but definition is found and loaded
1459    successfully, T is returned, else error."
1460   (declare (type string host)
1461            (values (member t nil)))
1462   (unless (find-logical-host host nil)
1463     (with-open-file (in-str (make-pathname :defaults "library:"
1464                                            :name host
1465                                            :type "translations"))
1466       (if *load-verbose*
1467           (format *error-output*
1468                   ";; loading pathname translations from ~A~%"
1469                   (namestring (truename in-str))))
1470       (setf (logical-pathname-translations host) (read in-str)))
1471     t))
1472
1473 (defun translate-logical-pathname (pathname &key)
1474   #!+sb-doc
1475   "Translates pathname to a physical pathname, which is returned."
1476   (declare (type pathname-designator pathname)
1477            (values (or null pathname)))
1478   (typecase pathname
1479     (logical-pathname
1480      (dolist (x (logical-host-canon-transls (%pathname-host pathname))
1481                 (error 'simple-file-error
1482                        :pathname pathname
1483                        :format-control "no translation for ~S"
1484                        :format-arguments (list pathname)))
1485        (destructuring-bind (from to) x
1486          (when (pathname-match-p pathname from)
1487            (return (translate-logical-pathname
1488                     (translate-pathname pathname from to)))))))
1489     (pathname pathname)
1490     (stream (translate-logical-pathname (pathname pathname)))
1491     (t (translate-logical-pathname (logical-pathname pathname)))))
1492
1493 (defvar *logical-pathname-defaults*
1494   (%make-logical-pathname (make-logical-host :name "BOGUS")
1495                           :unspecific
1496                           nil
1497                           nil
1498                           nil
1499                           nil))