Support long file names on Windows; more CRT function avoidance
[sbcl.git] / src / 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 (missing-arg) :type function)
21   (parse-native (missing-arg) :type function)
22   (unparse (missing-arg) :type function)
23   (unparse-native (missing-arg) :type function)
24   (unparse-host (missing-arg) :type function)
25   (unparse-directory (missing-arg) :type function)
26   (unparse-file (missing-arg) :type function)
27   (unparse-enough (missing-arg) :type function)
28   (unparse-directory-separator (missing-arg) :type simple-string)
29   (customary-case (missing-arg) :type (member :upper :lower)))
30
31 (def!method print-object ((host host) stream)
32   (print-unreadable-object (host stream :type t :identity t)))
33
34 (def!struct (logical-host
35              (:make-load-form-fun make-logical-host-load-form-fun)
36              (:include host
37                        (parse #'parse-logical-namestring)
38                        (parse-native
39                         (lambda (&rest x)
40                           (error "called PARSE-NATIVE-NAMESTRING using a ~
41                                   logical host: ~S" (first x))))
42                        (unparse #'unparse-logical-namestring)
43                        (unparse-native
44                         (lambda (&rest x)
45                           (error "called NATIVE-NAMESTRING using a ~
46                                   logical host: ~S" (first x))))
47                        (unparse-host
48                         (lambda (x)
49                           (logical-host-name (%pathname-host x))))
50                        (unparse-directory #'unparse-logical-directory)
51                        (unparse-file #'unparse-logical-file)
52                        (unparse-enough #'unparse-enough-namestring)
53                        (unparse-directory-separator ";")
54                        (customary-case :upper)))
55   (name "" :type simple-string)
56   (translations nil :type list)
57   (canon-transls nil :type list))
58
59 (def!method print-object ((logical-host logical-host) stream)
60   (print-unreadable-object (logical-host stream :type t)
61     (prin1 (logical-host-name logical-host) stream)))
62
63 (defun make-logical-host-load-form-fun (logical-host)
64   (values `(find-logical-host ',(logical-host-name logical-host))
65           nil))
66
67 ;;; A PATTERN is a list of entries and wildcards used for pattern
68 ;;; matches of translations.
69 (def!struct (pattern (:constructor make-pattern (pieces)))
70   (pieces nil :type list))
71 \f
72 ;;;; PATHNAME structures
73
74 ;;; the various magic tokens that are allowed to appear in pretty much
75 ;;; all pathname components
76 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
77   (def!type pathname-component-tokens ()
78     '(member nil :unspecific :wild :unc)))
79
80 (sb!xc:defstruct (pathname (:conc-name %pathname-)
81                            (:constructor %make-pathname (host
82                                                          device
83                                                          directory
84                                                          name
85                                                          type
86                                                          version))
87                            (:predicate pathnamep))
88   ;; the host (at present either a UNIX or logical host)
89   (host nil :type (or host null))
90   ;; the name of a logical or physical device holding files
91   (device nil :type (or simple-string pathname-component-tokens))
92   ;; a list of strings that are the component subdirectory components
93   (directory nil :type list)
94   ;; the filename
95   (name nil :type (or simple-string pattern pathname-component-tokens))
96   ;; the type extension of the file
97   (type nil :type (or simple-string pattern pathname-component-tokens))
98   ;; the version number of the file, a positive integer (not supported
99   ;; on standard Unix filesystems)
100   (version nil :type (or integer pathname-component-tokens (member :newest))))
101
102 ;;; Logical pathnames have the following format:
103 ;;;
104 ;;; logical-namestring ::=
105 ;;;      [host ":"] [";"] {directory ";"}* [name] ["." type ["." version]]
106 ;;;
107 ;;; host ::= word
108 ;;; directory ::= word | wildcard-word | **
109 ;;; name ::= word | wildcard-word
110 ;;; type ::= word | wildcard-word
111 ;;; version ::= pos-int | newest | NEWEST | *
112 ;;; word ::= {uppercase-letter | digit | -}+
113 ;;; wildcard-word ::= [word] '* {word '*}* [word]
114 ;;; pos-int ::= integer > 0
115 ;;;
116 ;;; Physical pathnames include all these slots and a device slot.
117
118 ;;; Logical pathnames are a subclass of PATHNAME. Their class
119 ;;; relations are mimicked using structures for efficiency.
120 (sb!xc:defstruct (logical-pathname (:conc-name %logical-pathname-)
121                                    (:include pathname)
122                                    (:constructor %make-logical-pathname
123                                                  (host
124                                                   device
125                                                   directory
126                                                   name
127                                                   type
128                                                   version))))
129
130 ;;; This is used both for Unix and Windows: while we accept both
131 ;;; \ and / as directory separators on Windows, we print our
132 ;;; own always with /, which is much less confusing what with
133 ;;; being \ needing to be escaped.
134 (defun unparse-physical-directory (pathname)
135   (declare (pathname pathname))
136   (unparse-physical-directory-list (%pathname-directory pathname)))
137
138 (defun unparse-physical-directory-list (directory)
139   (declare (list directory))
140   (collect ((pieces))
141     (when directory
142       (ecase (pop directory)
143        (:absolute
144         (let ((next (pop directory)))
145           (cond ((eq :home next)
146                  (pieces "~"))
147                 ((and (consp next) (eq :home (car next)))
148                  (pieces "~")
149                  (pieces (second next)))
150                 ((and (plusp (length next)) (char= #\~ (char next 0)))
151                  ;; The only place we need to escape the tilde.
152                  (pieces "\\")
153                  (pieces next))
154                 (next
155                  (push next directory)))
156           (pieces "/")))
157         (:relative))
158       (dolist (dir directory)
159         (typecase dir
160          ((member :up)
161           (pieces "../"))
162          ((member :back)
163           (error ":BACK cannot be represented in namestrings."))
164          ((member :wild-inferiors)
165           (pieces "**/"))
166          ((or simple-string pattern (member :wild))
167           (pieces (unparse-physical-piece dir))
168           (pieces "/"))
169          (t
170           (error "invalid directory component: ~S" dir)))))
171     (apply #'concatenate 'simple-string (pieces))))