Fix make-array transforms.
[sbcl.git] / code / pathname.lisp
1 ;;;; the known-to-the-cross-compiler part of PATHNAME logic
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 \f
14 ;;;; data types used by pathnames
15
16 ;;; The HOST structure holds the functions that both parse the
17 ;;; pathname information into structure slot entries, and after
18 ;;; translation the inverse (unparse) functions.
19 (def!struct (host (:constructor nil))
20   (parse (required-argument) :type function)
21   (unparse (required-argument) :type function)
22   (unparse-host (required-argument) :type function)
23   (unparse-directory (required-argument) :type function)
24   (unparse-file (required-argument) :type function)
25   (unparse-enough (required-argument) :type function)
26   (customary-case (required-argument) :type (member :upper :lower)))
27
28 (def!struct (logical-host
29              (:make-load-form-fun make-logical-host-load-form-fun)
30              (:include host
31                        (:parse #'parse-logical-namestring)
32                        (:unparse #'unparse-logical-namestring)
33                        (:unparse-host
34                         (lambda (x)
35                           (logical-host-name (%pathname-host x))))
36                        (:unparse-directory #'unparse-logical-directory)
37                        (:unparse-file #'unparse-unix-file)
38                        (:unparse-enough #'unparse-enough-namestring)
39                        (:customary-case :upper)))
40   (name "" :type simple-base-string)
41   (translations nil :type list)
42   (canon-transls nil :type list))
43
44 (def!method print-object ((logical-host logical-host) stream)
45   (print-unreadable-object (logical-host stream :type t)
46     (prin1 (logical-host-name logical-host) stream)))
47
48 ;;; What would it mean to dump a logical host and reload it into
49 ;;; another Lisp image? It's not clear, so we don't support it.
50 (defun make-logical-host-load-form-fun (logical-host)
51   (error "~@<A logical host can't be dumped as a constant: ~2I~_~S~:>"
52          logical-host))
53
54 ;;; A PATTERN is a list of entries and wildcards used for pattern
55 ;;; matches of translations.
56 (sb!xc:defstruct (pattern (:constructor make-pattern (pieces)))
57   (pieces nil :type list))
58 \f
59 ;;;; PATHNAME structures
60
61 ;;; the various magic tokens that are allowed to appear in pretty much
62 ;;; all pathname components
63 (sb!xc:deftype pathname-component-tokens ()
64   '(member nil :unspecific :wild))
65
66 (sb!xc:defstruct (pathname (:conc-name %pathname-)
67                            (:constructor %make-pathname (host
68                                                          device
69                                                          directory
70                                                          name
71                                                          type
72                                                          version))
73                            (:predicate pathnamep))
74   ;; the host (at present either a UNIX or logical host)
75   (host nil :type (or host null))
76   ;; the name of a logical or physical device holding files
77   (device nil :type (or simple-string pathname-component-tokens))
78   ;; a list of strings that are the component subdirectory components
79   (directory nil :type list)
80   ;; the filename
81   (name nil :type (or simple-string pattern pathname-component-tokens))
82   ;; the type extension of the file
83   (type nil :type (or simple-string pattern pathname-component-tokens))
84   ;; the version number of the file, a positive integer (not supported
85   ;; on standard Unix filesystems)
86   (version nil :type (or integer pathname-component-tokens (member :newest))))
87
88 ;;; Logical pathnames have the following format:
89 ;;;
90 ;;; logical-namestring ::=
91 ;;;      [host ":"] [";"] {directory ";"}* [name] ["." type ["." version]]
92 ;;;
93 ;;; host ::= word
94 ;;; directory ::= word | wildcard-word | **
95 ;;; name ::= word | wildcard-word
96 ;;; type ::= word | wildcard-word
97 ;;; version ::= pos-int | newest | NEWEST | *
98 ;;; word ::= {uppercase-letter | digit | -}+
99 ;;; wildcard-word ::= [word] '* {word '*}* [word]
100 ;;; pos-int ::= integer > 0
101 ;;;
102 ;;; Physical pathnames include all these slots and a device slot.
103
104 ;;; Logical pathnames are a subclass of PATHNAME. Their class
105 ;;; relations are mimicked using structures for efficency.
106 (sb!xc:defstruct (logical-pathname (:conc-name %logical-pathname-)
107                                    (:include pathname)
108                                    (:constructor %make-logical-pathname
109                                                  (host
110                                                   device
111                                                   directory
112                                                   name
113                                                   type
114                                                   version))))
115 \f
116 (defmacro-mundanely enumerate-search-list ((var pathname &optional result)
117                                            &body body)
118   #!+sb-doc
119   "Execute BODY with VAR bound to each successive possible expansion for
120    PATHNAME and then return RESULT. Note: if PATHNAME does not contain a
121    search-list, then BODY is executed exactly once. Everything is wrapped
122    in a block named NIL, so RETURN can be used to terminate early. Note:
123    VAR is *not* bound inside of RESULT."
124   (let ((body-name (gensym)))
125     `(block nil
126        (flet ((,body-name (,var)
127                 ,@body))
128          (%enumerate-search-list ,pathname #',body-name)
129          ,result))))
130