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 (define-symbol-macro +long-file-name-prefix+ (quote "\\\\?\\"))
15 (define-symbol-macro +unc-file-name-prefix+ (quote "\\\\?\\UNC"))
17 (defun extract-device (namestr start end)
18 (declare (type simple-string namestr)
19 (type index start end))
20 (if (>= end (+ start 2))
21 (let ((c0 (char namestr start))
22 (c1 (char namestr (1+ start))))
23 (cond ((and (eql c1 #\:) (alpha-char-p c0))
24 ;; "X:" style, saved as X
25 (values (string (char namestr start)) (+ start 2)))
26 ((and (member c0 '(#\/ #\\)) (eql c0 c1) (>= end (+ start 3)))
27 ;; "//UNC" style, saved as :UNC device, with host and share
28 ;; becoming directory components.
29 (values :unc (+ start 1)))
34 (defun split-at-slashes-and-backslashes (namestr start end)
35 (declare (type simple-string namestr)
36 (type index start end))
37 ;; FIXME: There is a fundamental brokenness in using the same
38 ;; character as escape character and directory separator in
39 ;; non-native pathnames. (PATHNAME-DIRECTORY #P"\\*/") should
40 ;; probably be (:RELATIVE "*") everywhere, but on Windows it's
41 ;; (:ABSOLUTE :WILD)! See lp#673625.
42 (let ((absolute (and (/= start end)
43 (or (char= (schar namestr start) #\/)
44 (char= (schar namestr start) #\\)))))
47 ;; Next, split the remainder into slash-separated chunks.
50 (let ((slash (position-if (lambda (c)
53 namestr :start start :end end)))
54 (pieces (cons start (or slash end)))
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)))
77 (let ((position (position-if (lambda (char)
78 (or (char= char (code-char 0))
82 (error 'namestring-parse-error
83 :complaint "can't embed #\\Nul or #\\/ in Windows namestring"
84 :namestring namestring
88 ;; Deal with ~ and ~user.
90 (destructuring-bind (start . end) (car pieces)
91 (when (and (not absolute)
93 (string= namestring "~"
97 (if (> end (1+ start))
98 (setf home (list :home (subseq namestring (1+ start) end)))
102 ;; Now we have everything we want. So return it.
103 (values nil ; no host for Win32 namestrings
106 (dolist (piece pieces)
107 (let ((piece-start (car piece))
108 (piece-end (cdr piece)))
109 (unless (= piece-start piece-end)
110 (cond ((string= namestring ".."
114 ((string= namestring "**"
117 (dirs :wild-inferiors))
119 (dirs (maybe-make-pattern namestring
124 (list* :absolute home (dirs))
125 (cons :absolute (dirs))))
127 (cons :relative (dirs)))
134 (defun parse-native-win32-namestring (namestring start end as-directory)
135 (declare (type simple-string namestring)
136 (type index start end))
137 (setf namestring (coerce namestring 'simple-string))
138 (multiple-value-bind (device new-start)
139 (cond ((= (length +unc-file-name-prefix+)
140 (mismatch +unc-file-name-prefix+ namestring
142 (values :unc (+ start (length +unc-file-name-prefix+))))
143 ((= (length +long-file-name-prefix+)
144 (mismatch +long-file-name-prefix+ namestring
146 (extract-device namestring
147 (+ start (length +long-file-name-prefix+))
149 (t (extract-device namestring start end)))
150 (multiple-value-bind (absolute ranges)
151 (split-at-slashes-and-backslashes namestring new-start end)
152 (let* ((components (loop for ((start . end) . rest) on ranges
153 for piece = (subseq namestring start end)
154 collect (if (and (string= piece "..") rest)
157 (directory (if (and as-directory
158 (string/= "" (car (last components))))
160 (butlast components)))
163 (let* ((end (first (last components)))
164 (dot (position #\. end :from-end t)))
165 ;; FIXME: can we get this dot-interpretation knowledge
166 ;; from existing code? EXTRACT-NAME-TYPE-AND-VERSION
167 ;; does slightly more work than that.
172 (list (subseq end 0 dot) (subseq end (1+ dot))))
177 (cons (if absolute :absolute :relative) directory)
178 (first name-and-type)
179 (second name-and-type)
184 (defun unparse-win32-host (pathname)
185 (declare (type pathname pathname)
187 ;; FIXME: same as UNPARSE-UNIX-HOST. That's probably not good.
190 (defun unparse-win32-device (pathname &optional native)
191 (declare (type pathname pathname))
192 (let ((device (pathname-device pathname))
193 (directory (pathname-directory pathname)))
194 (cond ((or (null device) (eq device :unspecific))
197 (if native "\\" "/"))
198 ((and (= 1 (length device)) (alpha-char-p (char device 0)))
199 (concatenate 'simple-string device ":"))
200 ((and (consp directory) (eq :relative (car directory)))
201 (error "No printed representation for a relative UNC pathname."))
204 (concatenate 'simple-string "\\\\" device)
205 (concatenate 'simple-string "//" device))))))
207 (defun unparse-win32-file (pathname)
208 (declare (type pathname pathname))
210 (let* ((name (%pathname-name pathname))
211 (type (%pathname-type pathname))
212 (type-supplied (not (or (null type) (eq type :unspecific)))))
213 ;; Note: by ANSI 19.3.1.1.5, we ignore the version slot when
214 ;; translating logical pathnames to a filesystem without
215 ;; versions (like Win32).
217 (when (and (null type)
220 (position #\. name :start 1))
221 (error "too many dots in the name: ~S" pathname))
222 (when (and (typep name 'string)
224 (error "name is of length 0: ~S" pathname))
225 (strings (unparse-physical-piece name)))
228 (error "cannot specify the type without a file: ~S" pathname))
229 (when (typep type 'simple-string)
230 (when (position #\. type)
231 (error "type component can't have a #\. inside: ~S" pathname)))
233 (strings (unparse-physical-piece type))))
234 (apply #'concatenate 'simple-string (strings))))
236 (defun unparse-win32-namestring (pathname)
237 (declare (type pathname pathname))
238 (concatenate 'simple-string
239 (unparse-win32-device pathname)
240 (unparse-physical-directory pathname)
241 (unparse-win32-file pathname)))
243 (defun unparse-native-win32-namestring (pathname as-file)
244 (declare (type pathname pathname))
245 (let* ((device (pathname-device pathname))
246 (directory (pathname-directory pathname))
247 (name (pathname-name pathname))
248 (name-present-p (typep name '(not (member nil :unspecific))))
249 (name-string (if name-present-p name ""))
250 (type (pathname-type pathname))
251 (type-present-p (typep type '(not (member nil :unspecific))))
252 (type-string (if type-present-p type ""))
253 (absolutep (and device (eql :absolute (car directory)))))
256 (when (and absolutep (member :up directory))
257 ;; employ merge-pathnames to parse :BACKs into which we turn :UPs
261 (make-pathname :defaults pathname :directory '(:relative))
262 (make-pathname :defaults pathname
263 :directory (substitute :back :up directory))))))
265 (with-output-to-string (s)
267 (write-string (case device
268 (:unc +unc-file-name-prefix+)
269 (otherwise +long-file-name-prefix+)) s))
270 (when (or (not absolutep) (not (member device '(:unc nil))))
271 (write-string (unparse-win32-device pathname t) s))
273 (ecase (pop directory)
275 (let ((next (pop directory)))
276 (cond ((eq :home next)
277 (write-string (user-homedir-namestring) s))
278 ((and (consp next) (eq :home (car next)))
279 (let ((where (user-homedir-namestring (second next))))
281 (write-string where s)
282 (error "User homedir unknown for: ~S"
285 (push next directory)))
288 (loop for (piece . subdirs) on directory
290 ((member :up) (write-string ".." s))
291 (string (write-string piece s))
292 (t (error "ungood directory segment in NATIVE-NAMESTRING: ~S"
294 if (or subdirs (stringp name))
295 do (write-char #\\ s)
301 (unless (stringp name-string) ;some kind of wild field
302 (error "ungood name component in NATIVE-NAMESTRING: ~S" name))
303 (write-string name-string s)
305 (unless (stringp type-string) ;some kind of wild field
306 (error "ungood type component in NATIVE-NAMESTRING: ~S" type))
308 (write-string type-string s)))
309 (when type-present-p ;
311 "type component without a name component in NATIVE-NAMESTRING: ~S"
314 (let ((string (get-output-stream-string s)))
315 (return-from unparse-native-win32-namestring
316 (cond ((< (- 260 12) (length string))
317 ;; KLUDGE: account for additional length of 8.3 name to make
318 ;; directories always accessible
319 (coerce string 'simple-string))
322 (subseq string (1- (length +unc-file-name-prefix+)))
324 (t (subseq string (length +long-file-name-prefix+))))))))
328 (defun unparse-win32-enough (pathname defaults)
329 (declare (type pathname pathname defaults))
331 (error "~S cannot be represented relative to ~S."
334 (let* ((pathname-directory (%pathname-directory pathname))
335 (defaults-directory (%pathname-directory defaults))
336 (prefix-len (length defaults-directory))
338 (cond ((null pathname-directory) '(:relative))
339 ((eq (car pathname-directory) :relative)
341 ((and (> prefix-len 0)
342 (>= (length pathname-directory) prefix-len)
343 (compare-component (subseq pathname-directory
346 ;; Pathname starts with a prefix of default. So
347 ;; just use a relative directory from then on out.
348 (cons :relative (nthcdr prefix-len pathname-directory)))
349 ((eq (car pathname-directory) :absolute)
350 ;; We are an absolute pathname, so we can just use it.
353 (bug "Bad fallthrough in ~S" 'unparse-unix-enough)))))
354 (strings (unparse-physical-directory-list result-directory)))
355 (let* ((pathname-type (%pathname-type pathname))
356 (type-needed (and pathname-type
357 (not (eq pathname-type :unspecific))))
358 (pathname-name (%pathname-name pathname))
359 (name-needed (or type-needed
361 (not (compare-component pathname-name
365 (unless pathname-name (lose))
366 (when (and (null pathname-type)
367 (typep pathname-name 'simple-string)
368 (position #\. pathname-name :start 1))
369 (error "too many dots in the name: ~S" pathname))
370 (strings (unparse-physical-piece pathname-name)))
372 (when (or (null pathname-type) (eq pathname-type :unspecific))
374 (when (typep pathname-type 'simple-string)
375 (when (position #\. pathname-type)
376 (error "type component can't have a #\. inside: ~S" pathname)))
378 (strings (unparse-physical-piece pathname-type))))
379 (apply #'concatenate 'simple-string (strings)))))
381 ;; FIXME: This has been converted rather blindly from the Unix
382 ;; version, with no reference to any Windows docs what so ever.
383 (defun simplify-win32-namestring (src)
384 (declare (type simple-string src))
385 (let* ((src-len (length src))
386 (dst (make-string src-len :element-type 'character))
390 (flet ((deposit (char)
391 (setf (schar dst dst-len) char)
395 (dotimes (src-index src-len)
396 (let ((char (schar src src-index)))
397 (cond ((char= char #\.)
404 ;; either ``/...' or ``...//...'
406 (setf last-slash dst-len)
409 ;; either ``./...'' or ``..././...''
414 ((and last-slash (not (zerop last-slash)))
415 ;; There is something before this ..
416 (let ((prev-prev-slash
417 (position-if #'slashp dst :end last-slash :from-end t)))
418 (cond ((and (= (+ (or prev-prev-slash 0) 2)
420 (char= (schar dst (- last-slash 2)) #\.)
421 (char= (schar dst (1- last-slash)) #\.))
422 ;; The something before this .. is another ..
424 (setf last-slash dst-len))
426 ;; The something is some directory or other.
431 (setf last-slash prev-prev-slash)))))
433 ;; There is nothing before this .., so we need to keep it
434 (setf last-slash dst-len)
437 ;; something other than a dot between slashes
438 (setf last-slash dst-len)
443 (setf (schar dst dst-len) char)
446 (when (and last-slash (not (zerop last-slash)))
449 ;; We've got ``foobar/.''
452 ;; We've got ``foobar/..''
453 (unless (and (>= last-slash 2)
454 (char= (schar dst (1- last-slash)) #\.)
455 (char= (schar dst (- last-slash 2)) #\.)
457 (slashp (schar dst (- last-slash 3)))))
458 (let ((prev-prev-slash
459 (position-if #'slashp dst :end last-slash :from-end t)))
461 (setf dst-len (1+ prev-prev-slash))
462 (return-from simplify-win32-namestring
463 (coerce ".\\" 'simple-string)))))))))
464 (cond ((zerop dst-len)
469 (subseq dst 0 dst-len)))))