1.0.28.61: partial re-implementation of DIRECTORY
[sbcl.git] / src / code / win32-pathname.lisp
blob80ea08849c1984829ea74829e68e380964466e5e
1 ;;;; pathname parsing for Win32 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 (defun extract-device (namestr start end)
15 (declare (type simple-string namestr)
16 (type index start end))
17 (if (>= end (+ start 2))
18 (let ((c0 (char namestr start))
19 (c1 (char namestr (1+ start))))
20 (cond ((and (eql c1 #\:) (alpha-char-p c0))
21 ;; "X:" style, saved as X
22 (values (string (char namestr start)) (+ start 2)))
23 ((and (member c0 '(#\/ #\\)) (eql c0 c1))
24 ;; "//UNC" style, saved as UNC
25 ;; FIXME: at unparsing time we tell these apart by length,
26 ;; which seems a bit lossy -- presumably one-letter UNC
27 ;; hosts can exist as well. That seems a less troublesome
28 ;; restriction than disallowing UNC hosts whose names match
29 ;; logical pathname hosts... Time will tell -- both LispWorks
30 ;; and ACL use the host component for UNC hosts, so maybe
31 ;; we will end up there as well.
32 (let ((p (or (position c0 namestr :start (+ start 3) :end end)
33 end)))
34 (values (subseq namestr (+ start 2) p) p)))
36 (values nil start))))
37 (values nil start)))
39 (defun split-at-slashes-and-backslashes (namestr start end)
40 (declare (type simple-string namestr)
41 (type index start end))
42 (let ((absolute (and (/= start end)
43 (or (char= (schar namestr start) #\/)
44 (char= (schar namestr start) #\\)))))
45 (when absolute
46 (incf start))
47 ;; Next, split the remainder into slash-separated chunks.
48 (collect ((pieces))
49 (loop
50 (let ((slash (position-if (lambda (c)
51 (or (char= c #\/)
52 (char= c #\\)))
53 namestr :start start :end end)))
54 (pieces (cons start (or slash end)))
55 (unless slash
56 (return))
57 (setf start (1+ slash))))
58 (values absolute (pieces)))))
60 (defun parse-win32-namestring (namestring start end)
61 (declare (type simple-string namestring)
62 (type index start end))
63 (setf namestring (coerce namestring 'simple-string))
64 (multiple-value-bind (device new-start)
65 (extract-device namestring start end)
66 (multiple-value-bind (absolute pieces)
67 (split-at-slashes-and-backslashes namestring new-start end)
68 (multiple-value-bind (name type version)
69 (let* ((tail (car (last pieces)))
70 (tail-start (car tail))
71 (tail-end (cdr tail)))
72 (unless (= tail-start tail-end)
73 (setf pieces (butlast pieces))
74 (extract-name-type-and-version namestring tail-start tail-end)))
76 (when (stringp name)
77 (let ((position (position-if (lambda (char)
78 (or (char= char (code-char 0))
79 (char= char #\/)))
80 name)))
81 (when position
82 (error 'namestring-parse-error
83 :complaint "can't embed #\\Nul or #\\/ in Unix namestring"
84 :namestring namestring
85 :offset position))))
86 ;; Now we have everything we want. So return it.
87 (values nil ; no host for Win32 namestrings
88 device
89 (collect ((dirs))
90 (dolist (piece pieces)
91 (let ((piece-start (car piece))
92 (piece-end (cdr piece)))
93 (unless (= piece-start piece-end)
94 (cond ((string= namestring ".."
95 :start1 piece-start
96 :end1 piece-end)
97 (dirs :up))
98 ((string= namestring "**"
99 :start1 piece-start
100 :end1 piece-end)
101 (dirs :wild-inferiors))
103 (dirs (maybe-make-pattern namestring
104 piece-start
105 piece-end)))))))
106 (cond (absolute
107 (cons :absolute (dirs)))
108 ((dirs)
109 (cons :relative (dirs)))
111 nil)))
112 name
113 type
114 version)))))
116 (defun parse-native-win32-namestring (namestring start end as-directory)
117 (declare (type simple-string namestring)
118 (type index start end))
119 (setf namestring (coerce namestring 'simple-string))
120 (multiple-value-bind (device new-start)
121 (extract-device namestring start end)
122 (multiple-value-bind (absolute ranges)
123 (split-at-slashes-and-backslashes namestring new-start end)
124 (let* ((components (loop for ((start . end) . rest) on ranges
125 for piece = (subseq namestring start end)
126 collect (if (and (string= piece "..") rest)
128 piece)))
129 (directory (if (and as-directory
130 (string/= "" (car (last components))))
131 components
132 (butlast components)))
133 (name-and-type
134 (unless as-directory
135 (let* ((end (first (last components)))
136 (dot (position #\. end :from-end t)))
137 ;; FIXME: can we get this dot-interpretation knowledge
138 ;; from existing code? EXTRACT-NAME-TYPE-AND-VERSION
139 ;; does slightly more work than that.
140 (cond
141 ((string= end "")
142 (list nil nil))
143 ((and dot (> dot 0))
144 (list (subseq end 0 dot) (subseq end (1+ dot))))
146 (list end nil)))))))
147 (values nil
148 device
149 (cons (if absolute :absolute :relative) directory)
150 (first name-and-type)
151 (second name-and-type)
152 nil)))))
156 (defun unparse-win32-host (pathname)
157 (declare (type pathname pathname)
158 (ignore pathname))
159 ;; FIXME: same as UNPARSE-UNIX-HOST. That's probably not good.
162 (defun unparse-win32-device (pathname)
163 (declare (type pathname pathname))
164 (let ((device (pathname-device pathname))
165 (directory (pathname-directory pathname)))
166 (cond ((or (null device) (eq device :unspecific))
168 ((= 1 (length device))
169 (concatenate 'simple-string device ":"))
170 ((and (consp directory) (eq :relative (car directory)))
171 (error "No printed representation for a relative UNC pathname."))
173 (concatenate 'simple-string "\\\\" device)))))
175 (defun unparse-win32-directory-list (directory)
176 (declare (type list directory))
177 (collect ((pieces))
178 (when directory
179 (ecase (pop directory)
180 (:absolute
181 (pieces "\\"))
182 (:relative
183 ;; nothing special
185 (dolist (dir directory)
186 (typecase dir
187 ((member :up)
188 (pieces "..\\"))
189 ((member :back)
190 (error ":BACK cannot be represented in namestrings."))
191 ((member :wild-inferiors)
192 (pieces "**\\"))
193 ((or simple-string pattern (member :wild))
194 (pieces (unparse-physical-piece dir))
195 (pieces "\\"))
197 (error "invalid directory component: ~S" dir)))))
198 (apply #'concatenate 'simple-string (pieces))))
200 (defun unparse-win32-directory (pathname)
201 (declare (type pathname pathname))
202 (unparse-win32-directory-list (%pathname-directory pathname)))
204 (defun unparse-win32-file (pathname)
205 (declare (type pathname pathname))
206 (collect ((strings))
207 (let* ((name (%pathname-name pathname))
208 (type (%pathname-type pathname))
209 (type-supplied (not (or (null type) (eq type :unspecific)))))
210 ;; Note: by ANSI 19.3.1.1.5, we ignore the version slot when
211 ;; translating logical pathnames to a filesystem without
212 ;; versions (like Win32).
213 (when name
214 (when (and (null type)
215 (typep name 'string)
216 (> (length name) 0)
217 (position #\. name :start 1))
218 (error "too many dots in the name: ~S" pathname))
219 (when (and (typep name 'string)
220 (string= name ""))
221 (error "name is of length 0: ~S" pathname))
222 (strings (unparse-physical-piece name)))
223 (when type-supplied
224 (unless name
225 (error "cannot specify the type without a file: ~S" pathname))
226 (when (typep type 'simple-string)
227 (when (position #\. type)
228 (error "type component can't have a #\. inside: ~S" pathname)))
229 (strings ".")
230 (strings (unparse-physical-piece type))))
231 (apply #'concatenate 'simple-string (strings))))
233 (defun unparse-win32-namestring (pathname)
234 (declare (type pathname pathname))
235 (concatenate 'simple-string
236 (unparse-win32-device pathname)
237 (unparse-win32-directory pathname)
238 (unparse-win32-file pathname)))
240 (defun unparse-native-win32-namestring (pathname as-file)
241 (declare (type pathname pathname))
242 (let* ((device (pathname-device pathname))
243 (directory (pathname-directory pathname))
244 (name (pathname-name pathname))
245 (name-present-p (typep name '(not (member nil :unspecific))))
246 (name-string (if name-present-p name ""))
247 (type (pathname-type pathname))
248 (type-present-p (typep type '(not (member nil :unspecific))))
249 (type-string (if type-present-p type "")))
250 (when name-present-p
251 (setf as-file nil))
252 (coerce
253 (with-output-to-string (s)
254 (when device
255 (write-string (unparse-win32-device pathname) s))
256 (when directory
257 (ecase (car directory)
258 (:absolute (write-char #\\ s))
259 (:relative)))
260 (loop for (piece . subdirs) on (cdr directory)
261 do (typecase piece
262 ((member :up) (write-string ".." s))
263 (string (write-string piece s))
264 (t (error "ungood directory segment in NATIVE-NAMESTRING: ~S"
265 piece)))
266 if (or subdirs (stringp name))
267 do (write-char #\\ s)
268 else
269 do (unless as-file
270 (write-char #\\ s)))
271 (if name-present-p
272 (progn
273 (unless (stringp name-string) ;some kind of wild field
274 (error "ungood name component in NATIVE-NAMESTRING: ~S" name))
275 (write-string name-string s)
276 (when type-present-p
277 (unless (stringp type-string) ;some kind of wild field
278 (error "ungood type component in NATIVE-NAMESTRING: ~S" type))
279 (write-char #\. s)
280 (write-string type-string s)))
281 (when type-present-p ;
282 (error
283 "type component without a name component in NATIVE-NAMESTRING: ~S"
284 type))))
285 'simple-string)))
287 ;;; FIXME.
288 (defun unparse-win32-enough (pathname defaults)
289 (declare (type pathname pathname defaults))
290 (flet ((lose ()
291 (error "~S cannot be represented relative to ~S."
292 pathname defaults)))
293 (collect ((strings))
294 (let* ((pathname-directory (%pathname-directory pathname))
295 (defaults-directory (%pathname-directory defaults))
296 (prefix-len (length defaults-directory))
297 (result-directory
298 (cond ((null pathname-directory) '(:relative))
299 ((eq (car pathname-directory) :relative)
300 pathname-directory)
301 ((and (> prefix-len 0)
302 (>= (length pathname-directory) prefix-len)
303 (compare-component (subseq pathname-directory
304 0 prefix-len)
305 defaults-directory))
306 ;; Pathname starts with a prefix of default. So
307 ;; just use a relative directory from then on out.
308 (cons :relative (nthcdr prefix-len pathname-directory)))
309 ((eq (car pathname-directory) :absolute)
310 ;; We are an absolute pathname, so we can just use it.
311 pathname-directory)
313 (bug "Bad fallthrough in ~S" 'unparse-unix-enough)))))
314 (strings (unparse-unix-directory-list result-directory)))
315 (let* ((pathname-type (%pathname-type pathname))
316 (type-needed (and pathname-type
317 (not (eq pathname-type :unspecific))))
318 (pathname-name (%pathname-name pathname))
319 (name-needed (or type-needed
320 (and pathname-name
321 (not (compare-component pathname-name
322 (%pathname-name
323 defaults)))))))
324 (when name-needed
325 (unless pathname-name (lose))
326 (when (and (null pathname-type)
327 (typep pathname-name 'simple-string)
328 (position #\. pathname-name :start 1))
329 (error "too many dots in the name: ~S" pathname))
330 (strings (unparse-physical-piece pathname-name)))
331 (when type-needed
332 (when (or (null pathname-type) (eq pathname-type :unspecific))
333 (lose))
334 (when (typep pathname-type 'simple-string)
335 (when (position #\. pathname-type)
336 (error "type component can't have a #\. inside: ~S" pathname)))
337 (strings ".")
338 (strings (unparse-physical-piece pathname-type))))
339 (apply #'concatenate 'simple-string (strings)))))
341 ;; FIXME: This has been converted rather blindly from the Unix
342 ;; version, with no reference to any Windows docs what so ever.
343 (defun simplify-win32-namestring (src)
344 (declare (type simple-string src))
345 (let* ((src-len (length src))
346 (dst (make-string src-len :element-type 'character))
347 (dst-len 0)
348 (dots 0)
349 (last-slash nil))
350 (flet ((deposit (char)
351 (setf (schar dst dst-len) char)
352 (incf dst-len))
353 (slashp (char)
354 (find char "\\/")))
355 (dotimes (src-index src-len)
356 (let ((char (schar src src-index)))
357 (cond ((char= char #\.)
358 (when dots
359 (incf dots))
360 (deposit char))
361 ((slashp char)
362 (case dots
364 ;; either ``/...' or ``...//...'
365 (unless last-slash
366 (setf last-slash dst-len)
367 (deposit char)))
369 ;; either ``./...'' or ``..././...''
370 (decf dst-len))
372 ;; We've found ..
373 (cond
374 ((and last-slash (not (zerop last-slash)))
375 ;; There is something before this ..
376 (let ((prev-prev-slash
377 (position-if #'slashp dst :end last-slash :from-end t)))
378 (cond ((and (= (+ (or prev-prev-slash 0) 2)
379 last-slash)
380 (char= (schar dst (- last-slash 2)) #\.)
381 (char= (schar dst (1- last-slash)) #\.))
382 ;; The something before this .. is another ..
383 (deposit char)
384 (setf last-slash dst-len))
386 ;; The something is some directory or other.
387 (setf dst-len
388 (if prev-prev-slash
389 (1+ prev-prev-slash)
391 (setf last-slash prev-prev-slash)))))
393 ;; There is nothing before this .., so we need to keep it
394 (setf last-slash dst-len)
395 (deposit char))))
397 ;; something other than a dot between slashes
398 (setf last-slash dst-len)
399 (deposit char)))
400 (setf dots 0))
402 (setf dots nil)
403 (setf (schar dst dst-len) char)
404 (incf dst-len)))))
405 ;; ...finish off
406 (when (and last-slash (not (zerop last-slash)))
407 (case dots
409 ;; We've got ``foobar/.''
410 (decf dst-len))
412 ;; We've got ``foobar/..''
413 (unless (and (>= last-slash 2)
414 (char= (schar dst (1- last-slash)) #\.)
415 (char= (schar dst (- last-slash 2)) #\.)
416 (or (= last-slash 2)
417 (slashp (schar dst (- last-slash 3)))))
418 (let ((prev-prev-slash
419 (position-if #'slashp dst :end last-slash :from-end t)))
420 (if prev-prev-slash
421 (setf dst-len (1+ prev-prev-slash))
422 (return-from simplify-win32-namestring
423 (coerce ".\\" 'simple-string)))))))))
424 (cond ((zerop dst-len)
425 ".\\")
426 ((= dst-len src-len)
427 dst)
429 (subseq dst 0 dst-len)))))