1 ;;;; pathname parsing for Win32 filesystems
3 ;;;; This software is part of the SBCL system. See the README file for
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) (>= end (+ start 3)))
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)
34 (values (subseq namestr (+ start 2) p) p)))
39 (defun split-at-slashes-and-backslashes (namestr start end)
40 (declare (type simple-string namestr)
41 (type index start end))
42 ;; FIXME: There is a fundamental brokenness in using the same
43 ;; character as escape character and directory separator in
44 ;; non-native pathnames. (PATHNAME-DIRECTORY #P"\\*/") should
45 ;; probably be (:RELATIVE "*") everywhere, but on Windows it's
46 ;; (:ABSOLUTE :WILD)! See lp#673625.
47 (let ((absolute (and (/= start end)
48 (or (char= (schar namestr start) #\/)
49 (char= (schar namestr start) #\\)))))
52 ;; Next, split the remainder into slash-separated chunks.
55 (let ((slash (position-if (lambda (c)
58 namestr :start start :end end)))
59 (pieces (cons start (or slash end)))
62 (setf start (1+ slash))))
63 (values absolute (pieces)))))
65 (defun parse-win32-namestring (namestring start end)
66 (declare (type simple-string namestring)
67 (type index start end))
68 (setf namestring (coerce namestring 'simple-string))
69 (multiple-value-bind (device new-start)
70 (extract-device namestring start end)
71 (multiple-value-bind (absolute pieces)
72 (split-at-slashes-and-backslashes namestring new-start end)
73 (multiple-value-bind (name type version)
74 (let* ((tail (car (last pieces)))
75 (tail-start (car tail))
76 (tail-end (cdr tail)))
77 (unless (= tail-start tail-end)
78 (setf pieces (butlast pieces))
79 (extract-name-type-and-version namestring tail-start tail-end)))
82 (let ((position (position-if (lambda (char)
83 (or (char= char (code-char 0))
87 (error 'namestring-parse-error
88 :complaint "can't embed #\\Nul or #\\/ in Unix namestring"
89 :namestring namestring
93 ;; Deal with ~ and ~user.
95 (destructuring-bind (start . end) (car pieces)
96 (when (and (not absolute)
98 (string= namestring "~"
102 (if (> end (1+ start))
103 (setf home (list :home (subseq namestring (1+ start) end)))
107 ;; Now we have everything we want. So return it.
108 (values nil ; no host for Win32 namestrings
111 (dolist (piece pieces)
112 (let ((piece-start (car piece))
113 (piece-end (cdr piece)))
114 (unless (= piece-start piece-end)
115 (cond ((string= namestring ".."
119 ((string= namestring "**"
122 (dirs :wild-inferiors))
124 (dirs (maybe-make-pattern namestring
129 (list* :absolute home (dirs))
130 (cons :absolute (dirs))))
132 (cons :relative (dirs)))
139 (defun parse-native-win32-namestring (namestring start end as-directory)
140 (declare (type simple-string namestring)
141 (type index start end))
142 (setf namestring (coerce namestring 'simple-string))
143 (multiple-value-bind (device new-start)
144 (extract-device namestring start end)
145 (multiple-value-bind (absolute ranges)
146 (split-at-slashes-and-backslashes namestring new-start end)
147 (let* ((components (loop for ((start . end) . rest) on ranges
148 for piece = (subseq namestring start end)
149 collect (if (and (string= piece "..") rest)
152 (directory (if (and as-directory
153 (string/= "" (car (last components))))
155 (butlast components)))
158 (let* ((end (first (last components)))
159 (dot (position #\. end :from-end t)))
160 ;; FIXME: can we get this dot-interpretation knowledge
161 ;; from existing code? EXTRACT-NAME-TYPE-AND-VERSION
162 ;; does slightly more work than that.
167 (list (subseq end 0 dot) (subseq end (1+ dot))))
172 (cons (if absolute :absolute :relative) directory)
173 (first name-and-type)
174 (second name-and-type)
179 (defun unparse-win32-host (pathname)
180 (declare (type pathname pathname)
182 ;; FIXME: same as UNPARSE-UNIX-HOST. That's probably not good.
185 (defun unparse-win32-device (pathname &optional native)
186 (declare (type pathname pathname))
187 (let ((device (pathname-device pathname))
188 (directory (pathname-directory pathname)))
189 (cond ((or (null device) (eq device :unspecific))
191 ((and (= 1 (length device)) (alpha-char-p (char device 0)))
192 (concatenate 'simple-string device ":"))
193 ((and (consp directory) (eq :relative (car directory)))
194 (error "No printed representation for a relative UNC pathname."))
197 (concatenate 'simple-string "\\\\" device)
198 (concatenate 'simple-string "//" device))))))
200 (defun unparse-win32-file (pathname)
201 (declare (type pathname pathname))
203 (let* ((name (%pathname-name pathname))
204 (type (%pathname-type pathname))
205 (type-supplied (not (or (null type) (eq type :unspecific)))))
206 ;; Note: by ANSI 19.3.1.1.5, we ignore the version slot when
207 ;; translating logical pathnames to a filesystem without
208 ;; versions (like Win32).
210 (when (and (null type)
213 (position #\. name :start 1))
214 (error "too many dots in the name: ~S" pathname))
215 (when (and (typep name 'string)
217 (error "name is of length 0: ~S" pathname))
218 (strings (unparse-physical-piece name)))
221 (error "cannot specify the type without a file: ~S" pathname))
222 (when (typep type 'simple-string)
223 (when (position #\. type)
224 (error "type component can't have a #\. inside: ~S" pathname)))
226 (strings (unparse-physical-piece type))))
227 (apply #'concatenate 'simple-string (strings))))
229 (defun unparse-win32-namestring (pathname)
230 (declare (type pathname pathname))
231 (concatenate 'simple-string
232 (unparse-win32-device pathname)
233 (unparse-physical-directory pathname)
234 (unparse-win32-file pathname)))
236 (defun unparse-native-win32-namestring (pathname as-file)
237 (declare (type pathname pathname))
238 (let* ((device (pathname-device pathname))
239 (directory (pathname-directory pathname))
240 (name (pathname-name pathname))
241 (name-present-p (typep name '(not (member nil :unspecific))))
242 (name-string (if name-present-p name ""))
243 (type (pathname-type pathname))
244 (type-present-p (typep type '(not (member nil :unspecific))))
245 (type-string (if type-present-p type "")))
249 (with-output-to-string (s)
251 (write-string (unparse-win32-device pathname t) s))
253 (ecase (pop directory)
255 (let ((next (pop directory)))
256 (cond ((eq :home next)
257 (write-string (user-homedir-namestring) s))
258 ((and (consp next) (eq :home (car next)))
259 (let ((where (user-homedir-namestring (second next))))
261 (write-string where s)
262 (error "User homedir unknown for: ~S" (second next)))))
264 (push next directory)))
267 (loop for (piece . subdirs) on directory
269 ((member :up) (write-string ".." s))
270 (string (write-string piece s))
271 (t (error "ungood directory segment in NATIVE-NAMESTRING: ~S"
273 if (or subdirs (stringp name))
274 do (write-char #\\ s)
280 (unless (stringp name-string) ;some kind of wild field
281 (error "ungood name component in NATIVE-NAMESTRING: ~S" name))
282 (write-string name-string s)
284 (unless (stringp type-string) ;some kind of wild field
285 (error "ungood type component in NATIVE-NAMESTRING: ~S" type))
287 (write-string type-string s)))
288 (when type-present-p ;
290 "type component without a name component in NATIVE-NAMESTRING: ~S"
295 (defun unparse-win32-enough (pathname defaults)
296 (declare (type pathname pathname defaults))
298 (error "~S cannot be represented relative to ~S."
301 (let* ((pathname-directory (%pathname-directory pathname))
302 (defaults-directory (%pathname-directory defaults))
303 (prefix-len (length defaults-directory))
305 (cond ((null pathname-directory) '(:relative))
306 ((eq (car pathname-directory) :relative)
308 ((and (> prefix-len 0)
309 (>= (length pathname-directory) prefix-len)
310 (compare-component (subseq pathname-directory
313 ;; Pathname starts with a prefix of default. So
314 ;; just use a relative directory from then on out.
315 (cons :relative (nthcdr prefix-len pathname-directory)))
316 ((eq (car pathname-directory) :absolute)
317 ;; We are an absolute pathname, so we can just use it.
320 (bug "Bad fallthrough in ~S" 'unparse-unix-enough)))))
321 (strings (unparse-physical-directory-list result-directory)))
322 (let* ((pathname-type (%pathname-type pathname))
323 (type-needed (and pathname-type
324 (not (eq pathname-type :unspecific))))
325 (pathname-name (%pathname-name pathname))
326 (name-needed (or type-needed
328 (not (compare-component pathname-name
332 (unless pathname-name (lose))
333 (when (and (null pathname-type)
334 (typep pathname-name 'simple-string)
335 (position #\. pathname-name :start 1))
336 (error "too many dots in the name: ~S" pathname))
337 (strings (unparse-physical-piece pathname-name)))
339 (when (or (null pathname-type) (eq pathname-type :unspecific))
341 (when (typep pathname-type 'simple-string)
342 (when (position #\. pathname-type)
343 (error "type component can't have a #\. inside: ~S" pathname)))
345 (strings (unparse-physical-piece pathname-type))))
346 (apply #'concatenate 'simple-string (strings)))))
348 ;; FIXME: This has been converted rather blindly from the Unix
349 ;; version, with no reference to any Windows docs what so ever.
350 (defun simplify-win32-namestring (src)
351 (declare (type simple-string src))
352 (let* ((src-len (length src))
353 (dst (make-string src-len :element-type 'character))
357 (flet ((deposit (char)
358 (setf (schar dst dst-len) char)
362 (dotimes (src-index src-len)
363 (let ((char (schar src src-index)))
364 (cond ((char= char #\.)
371 ;; either ``/...' or ``...//...'
373 (setf last-slash dst-len)
376 ;; either ``./...'' or ``..././...''
381 ((and last-slash (not (zerop last-slash)))
382 ;; There is something before this ..
383 (let ((prev-prev-slash
384 (position-if #'slashp dst :end last-slash :from-end t)))
385 (cond ((and (= (+ (or prev-prev-slash 0) 2)
387 (char= (schar dst (- last-slash 2)) #\.)
388 (char= (schar dst (1- last-slash)) #\.))
389 ;; The something before this .. is another ..
391 (setf last-slash dst-len))
393 ;; The something is some directory or other.
398 (setf last-slash prev-prev-slash)))))
400 ;; There is nothing before this .., so we need to keep it
401 (setf last-slash dst-len)
404 ;; something other than a dot between slashes
405 (setf last-slash dst-len)
410 (setf (schar dst dst-len) char)
413 (when (and last-slash (not (zerop last-slash)))
416 ;; We've got ``foobar/.''
419 ;; We've got ``foobar/..''
420 (unless (and (>= last-slash 2)
421 (char= (schar dst (1- last-slash)) #\.)
422 (char= (schar dst (- last-slash 2)) #\.)
424 (slashp (schar dst (- last-slash 3)))))
425 (let ((prev-prev-slash
426 (position-if #'slashp dst :end last-slash :from-end t)))
428 (setf dst-len (1+ prev-prev-slash))
429 (return-from simplify-win32-namestring
430 (coerce ".\\" 'simple-string)))))))))
431 (cond ((zerop dst-len)
436 (subseq dst 0 dst-len)))))