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")
14 ;;; Take a string and return a list of cons cells that mark the char
15 ;;; separated subseq. The first value is true if absolute directories
17 (defun split-at-slashes (namestr start end
)
18 (declare (type simple-string namestr
)
19 (type index start end
))
20 (let ((absolute (and (/= start end
)
21 (char= (schar namestr start
) #\
/))))
24 ;; Next, split the remainder into slash-separated chunks.
27 (let ((slash (position #\
/ namestr
:start start
:end end
)))
28 (pieces (cons start
(or slash end
)))
31 (setf start
(1+ slash
))))
32 (values absolute
(pieces)))))
34 (defun parse-unix-namestring (namestring start end
)
35 (declare (type simple-string namestring
)
36 (type index start end
))
37 (setf namestring
(coerce namestring
'simple-string
))
38 (multiple-value-bind (absolute pieces
)
39 (split-at-slashes namestring start end
)
40 (multiple-value-bind (name type version
)
41 (let* ((tail (car (last pieces
)))
42 (tail-start (car tail
))
43 (tail-end (cdr tail
)))
44 (unless (= tail-start tail-end
)
45 (setf pieces
(butlast pieces
))
46 (extract-name-type-and-version namestring tail-start tail-end
)))
49 (let ((position (position-if (lambda (char)
50 (or (char= char
(code-char 0))
54 (error 'namestring-parse-error
55 :complaint
"can't embed #\\Nul or #\\/ in Unix namestring"
56 :namestring namestring
58 ;; Now we have everything we want. So return it.
59 (values nil
; no host for Unix namestrings
60 nil
; no device for Unix namestrings
62 (dolist (piece pieces
)
63 (let ((piece-start (car piece
))
64 (piece-end (cdr piece
)))
65 (unless (= piece-start piece-end
)
66 (cond ((string= namestring
".."
70 ((string= namestring
"**"
73 (dirs :wild-inferiors
))
75 (dirs (maybe-make-pattern namestring
79 (cons :absolute
(dirs)))
81 (cons :relative
(dirs)))
88 (defun parse-native-unix-namestring (namestring start end
)
89 (declare (type simple-string namestring
)
90 (type index start end
))
91 (setf namestring
(coerce namestring
'simple-string
))
92 (multiple-value-bind (absolute ranges
)
93 (split-at-slashes namestring start end
)
94 (let* ((components (loop for
((start . end
) . rest
) on ranges
95 for piece
= (subseq namestring start end
)
96 collect
(if (and (string= piece
"..") rest
)
100 (let* ((end (first (last components
)))
101 (dot (position #\. end
:from-end t
)))
102 ;; FIXME: can we get this dot-interpretation knowledge
103 ;; from existing code? EXTRACT-NAME-TYPE-AND-VERSION
104 ;; does slightly more work than that.
109 (list (subseq end
0 dot
) (subseq end
(1+ dot
))))
114 (cons (if absolute
:absolute
:relative
) (butlast components
))
115 (first name-and-type
)
116 (second name-and-type
)
119 (/show0
"filesys.lisp 300")
121 (defun unparse-unix-host (pathname)
122 (declare (type pathname pathname
)
124 ;; this host designator needs to be recognized as a physical host in
125 ;; PARSE-NAMESTRING. Until sbcl-0.7.3.x, we had "Unix" here, but
126 ;; that's a valid Logical Hostname, so that's a bad choice. -- CSR,
130 (defun unparse-unix-piece (thing)
134 (let* ((srclen (length thing
))
137 (case (schar thing i
)
140 (let ((result (make-string dstlen
))
142 (dotimes (src srclen
)
143 (let ((char (schar thing src
)))
146 (setf (schar result dst
) #\\)
148 (setf (schar result dst
) char
)
153 (dolist (piece (pattern-pieces thing
))
167 (strings (cdr piece
))
170 (error "invalid pattern piece: ~S" piece
))))))
175 (defun unparse-unix-directory-list (directory)
176 (declare (type list directory
))
179 (ecase (pop directory
)
185 (dolist (dir directory
)
190 (error ":BACK cannot be represented in namestrings."))
191 ((member :wild-inferiors
)
193 ((or simple-string pattern
(member :wild
))
194 (pieces (unparse-unix-piece dir
))
197 (error "invalid directory component: ~S" dir
)))))
198 (apply #'concatenate
'simple-string
(pieces))))
200 (defun unparse-unix-directory (pathname)
201 (declare (type pathname pathname
))
202 (unparse-unix-directory-list (%pathname-directory pathname
)))
204 (defun unparse-unix-file (pathname)
205 (declare (type pathname pathname
))
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 Unix).
214 (when (and (null type
)
217 (position #\. name
:start
1))
218 (error "too many dots in the name: ~S" pathname
))
219 (when (and (typep name
'string
)
221 (error "name is of length 0: ~S" pathname
))
222 (strings (unparse-unix-piece 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
)))
230 (strings (unparse-unix-piece type
))))
231 (apply #'concatenate
'simple-string
(strings))))
233 (/show0
"filesys.lisp 406")
235 (defun unparse-unix-namestring (pathname)
236 (declare (type pathname pathname
))
237 (concatenate 'simple-string
238 (unparse-unix-directory pathname
)
239 (unparse-unix-file pathname
)))
241 (defun unparse-native-unix-namestring (pathname)
242 (declare (type pathname pathname
))
243 (let ((directory (pathname-directory pathname
))
244 (name (pathname-name pathname
))
245 (type (pathname-type pathname
)))
247 (with-output-to-string (s)
249 (ecase (car directory
)
250 (:absolute
(write-char #\
/ s
))
252 (dolist (piece (cdr directory
))
254 ((member :up
) (write-string ".." s
))
255 (string (write-string piece s
))
256 (t (error "ungood piece in NATIVE-NAMESTRING: ~S" piece
)))
259 (unless (stringp name
)
260 (error "non-STRING name in NATIVE-NAMESTRING: ~S" name
))
261 (write-string name s
)
263 (unless (stringp type
)
264 (error "non-STRING type in NATIVE-NAMESTRING: ~S" name
))
266 (write-string type s
))))
269 (defun unparse-unix-enough (pathname defaults
)
270 (declare (type pathname pathname defaults
))
272 (error "~S cannot be represented relative to ~S."
275 (let* ((pathname-directory (%pathname-directory pathname
))
276 (defaults-directory (%pathname-directory defaults
))
277 (prefix-len (length defaults-directory
))
279 (cond ((null pathname-directory
) '(:relative
))
280 ((eq (car pathname-directory
) :relative
)
282 ((and (> prefix-len
0)
283 (>= (length pathname-directory
) prefix-len
)
284 (compare-component (subseq pathname-directory
287 ;; Pathname starts with a prefix of default. So
288 ;; just use a relative directory from then on out.
289 (cons :relative
(nthcdr prefix-len pathname-directory
)))
290 ((eq (car pathname-directory
) :absolute
)
291 ;; We are an absolute pathname, so we can just use it.
294 (bug "Bad fallthrough in ~S" 'unparse-unix-enough
)))))
295 (strings (unparse-unix-directory-list result-directory
)))
296 (let* ((pathname-type (%pathname-type pathname
))
297 (type-needed (and pathname-type
298 (not (eq pathname-type
:unspecific
))))
299 (pathname-name (%pathname-name pathname
))
300 (name-needed (or type-needed
302 (not (compare-component pathname-name
306 (unless pathname-name
(lose))
307 (when (and (null pathname-type
)
308 (typep pathname-name
'simple-string
)
309 (position #\. pathname-name
:start
1))
310 (error "too many dots in the name: ~S" pathname
))
311 (strings (unparse-unix-piece pathname-name
)))
313 (when (or (null pathname-type
) (eq pathname-type
:unspecific
))
315 (when (typep pathname-type
'simple-string
)
316 (when (position #\. pathname-type
)
317 (error "type component can't have a #\. inside: ~S" pathname
)))
319 (strings (unparse-unix-piece pathname-type
))))
320 (apply #'concatenate
'simple-string
(strings)))))
322 (defun simplify-unix-namestring (src)
323 (declare (type simple-string src
))
324 (let* ((src-len (length src
))
325 (dst (make-string src-len
:element-type
'character
))
329 (macrolet ((deposit (char)
331 (setf (schar dst dst-len
) ,char
)
333 (dotimes (src-index src-len
)
334 (let ((char (schar src src-index
)))
335 (cond ((char= char
#\.
)
342 ;; either ``/...' or ``...//...'
344 (setf last-slash dst-len
)
347 ;; either ``./...'' or ``..././...''
352 ((and last-slash
(not (zerop last-slash
)))
353 ;; There is something before this ..
354 (let ((prev-prev-slash
355 (position #\
/ dst
:end last-slash
:from-end t
)))
356 (cond ((and (= (+ (or prev-prev-slash
0) 2)
358 (char= (schar dst
(- last-slash
2)) #\.
)
359 (char= (schar dst
(1- last-slash
)) #\.
))
360 ;; The something before this .. is another ..
362 (setf last-slash dst-len
))
364 ;; The something is some directory or other.
369 (setf last-slash prev-prev-slash
)))))
371 ;; There is nothing before this .., so we need to keep it
372 (setf last-slash dst-len
)
375 ;; something other than a dot between slashes
376 (setf last-slash dst-len
)
381 (setf (schar dst dst-len
) char
)
383 (when (and last-slash
(not (zerop last-slash
)))
386 ;; We've got ``foobar/.''
389 ;; We've got ``foobar/..''
390 (unless (and (>= last-slash
2)
391 (char= (schar dst
(1- last-slash
)) #\.
)
392 (char= (schar dst
(- last-slash
2)) #\.
)
394 (char= (schar dst
(- last-slash
3)) #\
/)))
395 (let ((prev-prev-slash
396 (position #\
/ dst
:end last-slash
:from-end t
)))
398 (setf dst-len
(1+ prev-prev-slash
))
399 (return-from simplify-unix-namestring
400 (coerce "./" 'simple-string
))))))))
401 (cond ((zerop dst-len
)
406 (subseq dst
0 dst-len
)))))