1 ;;;; pathname parsing for Unix 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")
16 (parse #'parse-unix-namestring
)
17 (parse-native #'parse-native-unix-namestring
)
18 (unparse #'unparse-unix-namestring
)
19 (unparse-native #'unparse-native-unix-namestring
)
20 (unparse-host #'unparse-unix-host
)
21 (unparse-directory #'unparse-unix-directory
)
22 (unparse-file #'unparse-unix-file
)
23 (unparse-enough #'unparse-unix-enough
)
24 (unparse-directory-separator "/")
25 (simplify-namestring #'simplify-unix-namestring
)
26 (customary-case :lower
))))
28 (defvar *physical-host
* (make-unix-host))
30 ;;; Take a string and return a list of cons cells that mark the char
31 ;;; separated subseq. The first value is true if absolute directories
33 (defun split-at-slashes (namestr start end
)
34 (declare (type simple-string namestr
)
35 (type index start end
))
36 (let ((absolute (and (/= start end
)
37 (char= (schar namestr start
) #\
/))))
40 ;; Next, split the remainder into slash-separated chunks.
43 (let ((slash (position #\
/ namestr
:start start
:end end
)))
44 (pieces (cons start
(or slash end
)))
47 (setf start
(1+ slash
))))
48 (values absolute
(pieces)))))
50 (defun parse-unix-namestring (namestring start end
)
51 (declare (type simple-string namestring
)
52 (type index start end
))
53 (setf namestring
(coerce namestring
'simple-string
))
54 (multiple-value-bind (absolute pieces
)
55 (split-at-slashes namestring start end
)
56 (multiple-value-bind (name type version
)
57 (let* ((tail (car (last pieces
)))
58 (tail-start (car tail
))
59 (tail-end (cdr tail
)))
60 (unless (= tail-start tail-end
)
61 (setf pieces
(butlast pieces
))
62 (extract-name-type-and-version namestring tail-start tail-end
#\\)))
65 (let ((position (position-if (lambda (char)
66 (or (char= char
(code-char 0))
70 (error 'namestring-parse-error
71 :complaint
"can't embed #\\Nul or #\\/ in Unix namestring"
72 :namestring namestring
76 ;; Deal with ~ and ~user
78 (destructuring-bind (start . end
) (car pieces
)
79 (when (and (not absolute
)
81 (string= namestring
"~"
85 (if (> end
(1+ start
))
86 (setf home
(list :home
(subseq namestring
(1+ start
) end
)))
90 ;; Now we have everything we want. So return it.
91 (values nil
; no host for Unix namestrings
92 nil
; no device for Unix namestrings
94 (dolist (piece pieces
)
95 (let ((piece-start (car piece
))
96 (piece-end (cdr piece
)))
97 (unless (= piece-start piece-end
)
98 (cond ((string= namestring
".."
102 ((string= namestring
"**"
105 (dirs :wild-inferiors
))
107 (dirs (maybe-make-pattern namestring
113 (list* :absolute home
(dirs))
114 (cons :absolute
(dirs))))
116 (cons :relative
(dirs)))
123 (defun parse-native-unix-namestring (namestring start end as-directory
)
124 (declare (type simple-string namestring
)
125 (type index start end
))
126 (setf namestring
(coerce namestring
'simple-string
))
127 (multiple-value-bind (absolute ranges
)
128 (split-at-slashes namestring start end
)
129 (let* ((components (loop for
((start . end
) . rest
) on ranges
130 for piece
= (subseq namestring start end
)
131 collect
(if (and (string= piece
"..") rest
)
134 (directory (if (and as-directory
135 (string/= "" (car (last components
))))
137 (butlast components
)))
140 (let* ((end (first (last components
)))
141 (dot (position #\. end
:from-end t
)))
142 ;; FIXME: can we get this dot-interpretation knowledge
143 ;; from existing code? EXTRACT-NAME-TYPE-AND-VERSION
144 ;; does slightly more work than that.
149 (list (subseq end
0 dot
) (subseq end
(1+ dot
))))
154 (cons (if absolute
:absolute
:relative
) directory
)
155 (first name-and-type
)
156 (second name-and-type
)
159 (/show0
"filesys.lisp 300")
161 (defun unparse-unix-host (pathname)
162 (declare (type pathname pathname
)
164 ;; this host designator needs to be recognized as a physical host in
165 ;; PARSE-NAMESTRING. Until sbcl-0.7.3.x, we had "Unix" here, but
166 ;; that's a valid Logical Hostname, so that's a bad choice. -- CSR,
170 (defun unparse-unix-directory (pathname)
171 (unparse-physical-directory pathname
#\\))
173 (defun unparse-unix-file (pathname)
174 (declare (type pathname pathname
))
176 (let* ((name (%pathname-name pathname
))
177 (type (%pathname-type pathname
))
178 (type-supplied (not (or (null type
) (eq type
:unspecific
)))))
179 ;; Note: by ANSI 19.3.1.1.5, we ignore the version slot when
180 ;; translating logical pathnames to a filesystem without
181 ;; versions (like Unix).
183 (when (and (null type
)
186 (position #\. name
:start
1))
187 (error "too many dots in the name: ~S" pathname
))
188 (when (and (typep name
'string
)
190 (error "name is of length 0: ~S" pathname
))
191 (strings (unparse-physical-piece name
#\\)))
194 (error "cannot specify the type without a file: ~S" pathname
))
195 (when (typep type
'simple-string
)
196 (when (position #\. type
)
197 (error "type component can't have a #\. inside: ~S" pathname
)))
199 (strings (unparse-physical-piece type
#\\))))
200 (apply #'concatenate
'simple-string
(strings))))
202 (/show0
"filesys.lisp 406")
204 (defun unparse-unix-namestring (pathname)
205 (declare (type pathname pathname
))
206 (concatenate 'simple-string
207 (unparse-unix-directory pathname
)
208 (unparse-unix-file pathname
)))
210 (defun unparse-native-unix-namestring (pathname as-file
)
211 (declare (type pathname pathname
))
212 (let* ((directory (pathname-directory pathname
))
213 (name (pathname-name pathname
))
214 (name-present-p (typep name
'(not (member nil
:unspecific
))))
215 (name-string (if name-present-p name
""))
216 (type (pathname-type pathname
))
217 (type-present-p (typep type
'(not (member nil
:unspecific
))))
218 (type-string (if type-present-p type
"")))
221 (with-simple-output-to-string (s)
223 (ecase (pop directory
)
225 (let ((next (pop directory
)))
226 (cond ((eq :home next
)
227 (write-string (user-homedir-namestring) s
))
228 ((and (consp next
) (eq :home
(car next
)))
229 (let ((where (user-homedir-namestring (second next
))))
231 (write-string where s
)
232 (error "User homedir unknown for: ~S." (second next
)))))
234 (push next directory
)))
237 (loop for
(piece . subdirs
) on directory
240 (write-string ".." s
))
242 (write-string piece s
))
244 (error "Bad directory segment in NATIVE-NAMESTRING: ~S."
246 if
(or subdirs
(stringp name
))
247 do
(write-char #\
/ s
)
253 (unless (stringp name-string
) ;some kind of wild field
254 (error "Bad name component in NATIVE-NAMESTRING: ~S." name
))
255 (write-string name-string s
)
257 (unless (stringp type-string
) ;some kind of wild field
258 (error "Bad type component in NATIVE-NAMESTRING: ~S." type
))
260 (write-string type-string s
)))
261 (when type-present-p
; type without a name
263 "Type component without a name component in NATIVE-NAMESTRING: ~S."
266 (defun unparse-unix-enough (pathname defaults
)
267 (declare (type pathname pathname defaults
))
269 (error "~S cannot be represented relative to ~S."
272 (let* ((pathname-directory (%pathname-directory pathname
))
273 (defaults-directory (%pathname-directory defaults
))
274 (prefix-len (length defaults-directory
))
276 (cond ((null pathname-directory
) '(:relative
))
277 ((eq (car pathname-directory
) :relative
)
279 ((and (> prefix-len
0)
280 (>= (length pathname-directory
) prefix-len
)
281 (compare-component (subseq pathname-directory
284 ;; Pathname starts with a prefix of default. So
285 ;; just use a relative directory from then on out.
286 (cons :relative
(nthcdr prefix-len pathname-directory
)))
287 ((eq (car pathname-directory
) :absolute
)
288 ;; We are an absolute pathname, so we can just use it.
291 (bug "Bad fallthrough in ~S" 'unparse-unix-enough
)))))
292 (strings (unparse-physical-directory-list result-directory
#\\)))
293 (let* ((pathname-type (%pathname-type pathname
))
294 (type-needed (and pathname-type
295 (not (eq pathname-type
:unspecific
))))
296 (pathname-name (%pathname-name pathname
))
297 (name-needed (or type-needed
299 (not (compare-component pathname-name
303 (unless pathname-name
(lose))
304 (when (and (null pathname-type
)
305 (typep pathname-name
'simple-string
)
306 (position #\. pathname-name
:start
1))
307 (error "too many dots in the name: ~S" pathname
))
308 (strings (unparse-physical-piece pathname-name
#\\)))
310 (when (or (null pathname-type
) (eq pathname-type
:unspecific
))
312 (when (typep pathname-type
'simple-string
)
313 (when (position #\. pathname-type
)
314 (error "type component can't have a #\. inside: ~S" pathname
)))
316 (strings (unparse-physical-piece pathname-type
#\\))))
317 (apply #'concatenate
'simple-string
(strings)))))
319 (defun simplify-unix-namestring (src)
320 (declare (type simple-string src
))
321 (let* ((src-len (length src
))
322 (dst (make-string src-len
:element-type
'character
))
326 (macrolet ((deposit (char)
328 (setf (schar dst dst-len
) ,char
)
330 (dotimes (src-index src-len
)
331 (let ((char (schar src src-index
)))
332 (cond ((char= char
#\.
)
339 ;; either ``/...' or ``...//...'
341 (setf last-slash dst-len
)
344 ;; either ``./...'' or ``..././...''
349 ((and last-slash
(not (zerop last-slash
)))
350 ;; There is something before this ..
351 (let ((prev-prev-slash
352 (position #\
/ dst
:end last-slash
:from-end t
)))
353 (cond ((and (= (+ (or prev-prev-slash
0) 2)
355 (char= (schar dst
(- last-slash
2)) #\.
)
356 (char= (schar dst
(1- last-slash
)) #\.
))
357 ;; The something before this .. is another ..
359 (setf last-slash dst-len
))
361 ;; The something is some directory or other.
366 (setf last-slash prev-prev-slash
)))))
368 ;; There is nothing before this .., so we need to keep it
369 (setf last-slash dst-len
)
372 ;; something other than a dot between slashes
373 (setf last-slash dst-len
)
378 (setf (schar dst dst-len
) char
)
380 (when (and last-slash
(not (zerop last-slash
)))
383 ;; We've got ``foobar/.''
386 ;; We've got ``foobar/..''
387 (unless (and (>= last-slash
2)
388 (char= (schar dst
(1- last-slash
)) #\.
)
389 (char= (schar dst
(- last-slash
2)) #\.
)
391 (char= (schar dst
(- last-slash
3)) #\
/)))
392 (let ((prev-prev-slash
393 (position #\
/ dst
:end last-slash
:from-end t
)))
395 (setf dst-len
(1+ prev-prev-slash
))
396 (return-from simplify-unix-namestring
397 (coerce "./" 'simple-string
))))))))
398 (cond ((zerop dst-len
)
403 (subseq dst
0 dst-len
)))))