Eliminate style-warning about undefined type GLOBAL-VAR
[sbcl.git] / src / code / unix-pathname.lisp
blob8b2adcd12d2f5d3beece55d0e26e3847ec2b9d9a
1 ;;;; pathname parsing for Unix filesystems
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.
12 (in-package "SB!IMPL")
14 (def!struct (unix-host
15 (:make-load-form-fun make-host-load-form)
16 (:include host
17 (parse #'parse-unix-namestring)
18 (parse-native #'parse-native-unix-namestring)
19 (unparse #'unparse-unix-namestring)
20 (unparse-native #'unparse-native-unix-namestring)
21 (unparse-host #'unparse-unix-host)
22 (unparse-directory #'unparse-unix-directory)
23 (unparse-file #'unparse-unix-file)
24 (unparse-enough #'unparse-unix-enough)
25 (unparse-directory-separator "/")
26 (simplify-namestring #'simplify-unix-namestring)
27 (customary-case :lower))))
29 (defvar *physical-host* (make-unix-host))
31 ;;; Take a string and return a list of cons cells that mark the char
32 ;;; separated subseq. The first value is true if absolute directories
33 ;;; location.
34 (defun split-at-slashes (namestr start end)
35 (declare (type simple-string namestr)
36 (type index start end))
37 (let ((absolute (and (/= start end)
38 (char= (schar namestr start) #\/))))
39 (when absolute
40 (incf start))
41 ;; Next, split the remainder into slash-separated chunks.
42 (collect ((pieces))
43 (loop
44 (let ((slash (position #\/ namestr :start start :end end)))
45 (pieces (cons start (or slash end)))
46 (unless slash
47 (return))
48 (setf start (1+ slash))))
49 (values absolute (pieces)))))
51 (defun parse-unix-namestring (namestring start end)
52 (declare (type simple-string namestring)
53 (type index start end))
54 (setf namestring (coerce namestring 'simple-string))
55 (multiple-value-bind (absolute pieces)
56 (split-at-slashes namestring start end)
57 (multiple-value-bind (name type version)
58 (let* ((tail (car (last pieces)))
59 (tail-start (car tail))
60 (tail-end (cdr tail)))
61 (unless (= tail-start tail-end)
62 (setf pieces (butlast pieces))
63 (extract-name-type-and-version namestring tail-start tail-end #\\)))
65 (when (stringp name)
66 (let ((position (position-if (lambda (char)
67 (or (char= char (code-char 0))
68 (char= char #\/)))
69 name)))
70 (when position
71 (error 'namestring-parse-error
72 :complaint "can't embed #\\Nul or #\\/ in Unix namestring"
73 :namestring namestring
74 :offset position))))
76 (let (home)
77 ;; Deal with ~ and ~user
78 (when (car pieces)
79 (destructuring-bind (start . end) (car pieces)
80 (when (and (not absolute)
81 (not (eql start end))
82 (string= namestring "~"
83 :start1 start
84 :end1 (1+ start)))
85 (setf absolute t)
86 (if (> end (1+ start))
87 (setf home (list :home (subseq namestring (1+ start) end)))
88 (setf home :home))
89 (pop pieces))))
91 ;; Now we have everything we want. So return it.
92 (values nil ; no host for Unix namestrings
93 nil ; no device for Unix namestrings
94 (collect ((dirs))
95 (dolist (piece pieces)
96 (let ((piece-start (car piece))
97 (piece-end (cdr piece)))
98 (unless (= piece-start piece-end)
99 (cond ((string= namestring ".."
100 :start1 piece-start
101 :end1 piece-end)
102 (dirs :up))
103 ((string= namestring "**"
104 :start1 piece-start
105 :end1 piece-end)
106 (dirs :wild-inferiors))
108 (dirs (maybe-make-pattern namestring
109 piece-start
110 piece-end
111 #\\)))))))
112 (cond (absolute
113 (if home
114 (list* :absolute home (dirs))
115 (cons :absolute (dirs))))
116 ((dirs)
117 (cons :relative (dirs)))
119 nil)))
120 name
121 type
122 version)))))
124 (defun parse-native-unix-namestring (namestring start end as-directory)
125 (declare (type simple-string namestring)
126 (type index start end))
127 (setf namestring (coerce namestring 'simple-string))
128 (multiple-value-bind (absolute ranges)
129 (split-at-slashes namestring start end)
130 (let* ((components (loop for ((start . end) . rest) on ranges
131 for piece = (subseq namestring start end)
132 collect (if (and (string= piece "..") rest)
134 piece)))
135 (directory (if (and as-directory
136 (string/= "" (car (last components))))
137 components
138 (butlast components)))
139 (name-and-type
140 (unless as-directory
141 (let* ((end (first (last components)))
142 (dot (position #\. end :from-end t)))
143 ;; FIXME: can we get this dot-interpretation knowledge
144 ;; from existing code? EXTRACT-NAME-TYPE-AND-VERSION
145 ;; does slightly more work than that.
146 (cond
147 ((string= end "")
148 (list nil nil))
149 ((and dot (> dot 0))
150 (list (subseq end 0 dot) (subseq end (1+ dot))))
152 (list end nil)))))))
153 (values nil
155 (cons (if absolute :absolute :relative) directory)
156 (first name-and-type)
157 (second name-and-type)
158 nil))))
160 (/show0 "filesys.lisp 300")
162 (defun unparse-unix-host (pathname)
163 (declare (type pathname pathname)
164 (ignore pathname))
165 ;; this host designator needs to be recognized as a physical host in
166 ;; PARSE-NAMESTRING. Until sbcl-0.7.3.x, we had "Unix" here, but
167 ;; that's a valid Logical Hostname, so that's a bad choice. -- CSR,
168 ;; 2002-05-09
171 (defun unparse-unix-directory (pathname)
172 (unparse-physical-directory pathname #\\))
174 (defun unparse-unix-file (pathname)
175 (declare (type pathname pathname))
176 (collect ((strings))
177 (let* ((name (%pathname-name pathname))
178 (type (%pathname-type pathname))
179 (type-supplied (not (or (null type) (eq type :unspecific)))))
180 ;; Note: by ANSI 19.3.1.1.5, we ignore the version slot when
181 ;; translating logical pathnames to a filesystem without
182 ;; versions (like Unix).
183 (when name
184 (when (and (null type)
185 (typep name 'string)
186 (> (length name) 0)
187 (position #\. name :start 1))
188 (error "too many dots in the name: ~S" pathname))
189 (when (and (typep name 'string)
190 (string= name ""))
191 (error "name is of length 0: ~S" pathname))
192 (strings (unparse-physical-piece name #\\)))
193 (when type-supplied
194 (unless name
195 (error "cannot specify the type without a file: ~S" pathname))
196 (when (typep type 'simple-string)
197 (when (position #\. type)
198 (error "type component can't have a #\. inside: ~S" pathname)))
199 (strings ".")
200 (strings (unparse-physical-piece type #\\))))
201 (apply #'concatenate 'simple-string (strings))))
203 (/show0 "filesys.lisp 406")
205 (defun unparse-unix-namestring (pathname)
206 (declare (type pathname pathname))
207 (concatenate 'simple-string
208 (unparse-unix-directory pathname)
209 (unparse-unix-file pathname)))
211 (defun unparse-native-unix-namestring (pathname as-file)
212 (declare (type pathname pathname))
213 (let* ((directory (pathname-directory pathname))
214 (name (pathname-name pathname))
215 (name-present-p (typep name '(not (member nil :unspecific))))
216 (name-string (if name-present-p name ""))
217 (type (pathname-type pathname))
218 (type-present-p (typep type '(not (member nil :unspecific))))
219 (type-string (if type-present-p type "")))
220 (when name-present-p
221 (setf as-file nil))
222 (with-simple-output-to-string (s)
223 (when directory
224 (ecase (pop directory)
225 (:absolute
226 (let ((next (pop directory)))
227 (cond ((eq :home next)
228 (write-string (user-homedir-namestring) s))
229 ((and (consp next) (eq :home (car next)))
230 (let ((where (user-homedir-namestring (second next))))
231 (if where
232 (write-string where s)
233 (error "User homedir unknown for: ~S." (second next)))))
234 (next
235 (push next directory)))
236 (write-char #\/ s)))
237 (:relative)))
238 (loop for (piece . subdirs) on directory
239 do (typecase piece
240 ((member :up :back)
241 (write-string ".." s))
242 (string
243 (write-string piece s))
245 (error "Bad directory segment in NATIVE-NAMESTRING: ~S."
246 piece)))
247 if (or subdirs (stringp name))
248 do (write-char #\/ s)
249 else
250 do (unless as-file
251 (write-char #\/ s)))
252 (if name-present-p
253 (progn
254 (unless (stringp name-string) ;some kind of wild field
255 (error "Bad name component in NATIVE-NAMESTRING: ~S." name))
256 (write-string name-string s)
257 (when type-present-p
258 (unless (stringp type-string) ;some kind of wild field
259 (error "Bad type component in NATIVE-NAMESTRING: ~S." type))
260 (write-char #\. s)
261 (write-string type-string s)))
262 (when type-present-p ; type without a name
263 (error
264 "Type component without a name component in NATIVE-NAMESTRING: ~S."
265 type))))))
267 (defun unparse-unix-enough (pathname defaults)
268 (declare (type pathname pathname defaults))
269 (flet ((lose ()
270 (error "~S cannot be represented relative to ~S."
271 pathname defaults)))
272 (collect ((strings))
273 (let* ((pathname-directory (%pathname-directory pathname))
274 (defaults-directory (%pathname-directory defaults))
275 (prefix-len (length defaults-directory))
276 (result-directory
277 (cond ((null pathname-directory) '(:relative))
278 ((eq (car pathname-directory) :relative)
279 pathname-directory)
280 ((and (> prefix-len 0)
281 (>= (length pathname-directory) prefix-len)
282 (compare-component (subseq pathname-directory
283 0 prefix-len)
284 defaults-directory))
285 ;; Pathname starts with a prefix of default. So
286 ;; just use a relative directory from then on out.
287 (cons :relative (nthcdr prefix-len pathname-directory)))
288 ((eq (car pathname-directory) :absolute)
289 ;; We are an absolute pathname, so we can just use it.
290 pathname-directory)
292 (bug "Bad fallthrough in ~S" 'unparse-unix-enough)))))
293 (strings (unparse-physical-directory-list result-directory #\\)))
294 (let* ((pathname-type (%pathname-type pathname))
295 (type-needed (and pathname-type
296 (not (eq pathname-type :unspecific))))
297 (pathname-name (%pathname-name pathname))
298 (name-needed (or type-needed
299 (and pathname-name
300 (not (compare-component pathname-name
301 (%pathname-name
302 defaults)))))))
303 (when name-needed
304 (unless pathname-name (lose))
305 (when (and (null pathname-type)
306 (typep pathname-name 'simple-string)
307 (position #\. pathname-name :start 1))
308 (error "too many dots in the name: ~S" pathname))
309 (strings (unparse-physical-piece pathname-name #\\)))
310 (when type-needed
311 (when (or (null pathname-type) (eq pathname-type :unspecific))
312 (lose))
313 (when (typep pathname-type 'simple-string)
314 (when (position #\. pathname-type)
315 (error "type component can't have a #\. inside: ~S" pathname)))
316 (strings ".")
317 (strings (unparse-physical-piece pathname-type #\\))))
318 (apply #'concatenate 'simple-string (strings)))))
320 (defun simplify-unix-namestring (src)
321 (declare (type simple-string src))
322 (let* ((src-len (length src))
323 (dst (make-string src-len :element-type 'character))
324 (dst-len 0)
325 (dots 0)
326 (last-slash nil))
327 (macrolet ((deposit (char)
328 `(progn
329 (setf (schar dst dst-len) ,char)
330 (incf dst-len))))
331 (dotimes (src-index src-len)
332 (let ((char (schar src src-index)))
333 (cond ((char= char #\.)
334 (when dots
335 (incf dots))
336 (deposit char))
337 ((char= char #\/)
338 (case dots
340 ;; either ``/...' or ``...//...'
341 (unless last-slash
342 (setf last-slash dst-len)
343 (deposit char)))
345 ;; either ``./...'' or ``..././...''
346 (decf dst-len))
348 ;; We've found ..
349 (cond
350 ((and last-slash (not (zerop last-slash)))
351 ;; There is something before this ..
352 (let ((prev-prev-slash
353 (position #\/ dst :end last-slash :from-end t)))
354 (cond ((and (= (+ (or prev-prev-slash 0) 2)
355 last-slash)
356 (char= (schar dst (- last-slash 2)) #\.)
357 (char= (schar dst (1- last-slash)) #\.))
358 ;; The something before this .. is another ..
359 (deposit char)
360 (setf last-slash dst-len))
362 ;; The something is some directory or other.
363 (setf dst-len
364 (if prev-prev-slash
365 (1+ prev-prev-slash)
367 (setf last-slash prev-prev-slash)))))
369 ;; There is nothing before this .., so we need to keep it
370 (setf last-slash dst-len)
371 (deposit char))))
373 ;; something other than a dot between slashes
374 (setf last-slash dst-len)
375 (deposit char)))
376 (setf dots 0))
378 (setf dots nil)
379 (setf (schar dst dst-len) char)
380 (incf dst-len))))))
381 (when (and last-slash (not (zerop last-slash)))
382 (case dots
384 ;; We've got ``foobar/.''
385 (decf dst-len))
387 ;; We've got ``foobar/..''
388 (unless (and (>= last-slash 2)
389 (char= (schar dst (1- last-slash)) #\.)
390 (char= (schar dst (- last-slash 2)) #\.)
391 (or (= last-slash 2)
392 (char= (schar dst (- last-slash 3)) #\/)))
393 (let ((prev-prev-slash
394 (position #\/ dst :end last-slash :from-end t)))
395 (if prev-prev-slash
396 (setf dst-len (1+ prev-prev-slash))
397 (return-from simplify-unix-namestring
398 (coerce "./" 'simple-string))))))))
399 (cond ((zerop dst-len)
400 "./")
401 ((= dst-len src-len)
402 dst)
404 (subseq dst 0 dst-len)))))