1.0.9.43: .PV-CELL., use .PV. directly
[sbcl.git] / src / code / unix-pathname.lisp
blobdc842b1306055143e782f00737ab9c090e117376
1 ;;;; pathname parsing for Unix 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 ;;; 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
16 ;;; location.
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) #\/))))
22 (when absolute
23 (incf start))
24 ;; Next, split the remainder into slash-separated chunks.
25 (collect ((pieces))
26 (loop
27 (let ((slash (position #\/ namestr :start start :end end)))
28 (pieces (cons start (or slash end)))
29 (unless slash
30 (return))
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)))
48 (when (stringp name)
49 (let ((position (position-if (lambda (char)
50 (or (char= char (code-char 0))
51 (char= char #\/)))
52 name)))
53 (when position
54 (error 'namestring-parse-error
55 :complaint "can't embed #\\Nul or #\\/ in Unix namestring"
56 :namestring namestring
57 :offset position))))
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
61 (collect ((dirs))
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 ".."
67 :start1 piece-start
68 :end1 piece-end)
69 (dirs :up))
70 ((string= namestring "**"
71 :start1 piece-start
72 :end1 piece-end)
73 (dirs :wild-inferiors))
75 (dirs (maybe-make-pattern namestring
76 piece-start
77 piece-end)))))))
78 (cond (absolute
79 (cons :absolute (dirs)))
80 ((dirs)
81 (cons :relative (dirs)))
83 nil)))
84 name
85 type
86 version))))
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)
97 :up
98 piece)))
99 (name-and-type
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.
105 (cond
106 ((string= end "")
107 (list nil nil))
108 ((and dot (> dot 0))
109 (list (subseq end 0 dot) (subseq end (1+ dot))))
111 (list end nil))))))
112 (values nil
114 (cons (if absolute :absolute :relative) (butlast components))
115 (first name-and-type)
116 (second name-and-type)
117 nil))))
119 (/show0 "filesys.lisp 300")
121 (defun unparse-unix-host (pathname)
122 (declare (type pathname pathname)
123 (ignore 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,
127 ;; 2002-05-09
130 (defun unparse-unix-piece (thing)
131 (etypecase thing
132 ((member :wild) "*")
133 (simple-string
134 (let* ((srclen (length thing))
135 (dstlen srclen))
136 (dotimes (i srclen)
137 (case (schar thing i)
138 ((#\* #\? #\[)
139 (incf dstlen))))
140 (let ((result (make-string dstlen))
141 (dst 0))
142 (dotimes (src srclen)
143 (let ((char (schar thing src)))
144 (case char
145 ((#\* #\? #\[)
146 (setf (schar result dst) #\\)
147 (incf dst)))
148 (setf (schar result dst) char)
149 (incf dst)))
150 result)))
151 (pattern
152 (collect ((strings))
153 (dolist (piece (pattern-pieces thing))
154 (etypecase piece
155 (simple-string
156 (strings piece))
157 (symbol
158 (ecase piece
159 (:multi-char-wild
160 (strings "*"))
161 (:single-char-wild
162 (strings "?"))))
163 (cons
164 (case (car piece)
165 (:character-set
166 (strings "[")
167 (strings (cdr piece))
168 (strings "]"))
170 (error "invalid pattern piece: ~S" piece))))))
171 (apply #'concatenate
172 'simple-string
173 (strings))))))
175 (defun unparse-unix-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-unix-piece dir))
195 (pieces "/"))
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))
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 Unix).
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-unix-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-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)))
246 (coerce
247 (with-output-to-string (s)
248 (when directory
249 (ecase (car directory)
250 (:absolute (write-char #\/ s))
251 (:relative)))
252 (dolist (piece (cdr directory))
253 (typecase piece
254 ((member :up) (write-string ".." s))
255 (string (write-string piece s))
256 (t (error "ungood piece in NATIVE-NAMESTRING: ~S" piece)))
257 (write-char #\/ s))
258 (when name
259 (unless (stringp name)
260 (error "non-STRING name in NATIVE-NAMESTRING: ~S" name))
261 (write-string name s)
262 (when type
263 (unless (stringp type)
264 (error "non-STRING type in NATIVE-NAMESTRING: ~S" name))
265 (write-char #\. s)
266 (write-string type s))))
267 'simple-string)))
269 (defun unparse-unix-enough (pathname defaults)
270 (declare (type pathname pathname defaults))
271 (flet ((lose ()
272 (error "~S cannot be represented relative to ~S."
273 pathname defaults)))
274 (collect ((strings))
275 (let* ((pathname-directory (%pathname-directory pathname))
276 (defaults-directory (%pathname-directory defaults))
277 (prefix-len (length defaults-directory))
278 (result-directory
279 (cond ((null pathname-directory) '(:relative))
280 ((eq (car pathname-directory) :relative)
281 pathname-directory)
282 ((and (> prefix-len 0)
283 (>= (length pathname-directory) prefix-len)
284 (compare-component (subseq pathname-directory
285 0 prefix-len)
286 defaults-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.
292 pathname-directory)
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
301 (and pathname-name
302 (not (compare-component pathname-name
303 (%pathname-name
304 defaults)))))))
305 (when name-needed
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)))
312 (when type-needed
313 (when (or (null pathname-type) (eq pathname-type :unspecific))
314 (lose))
315 (when (typep pathname-type 'simple-string)
316 (when (position #\. pathname-type)
317 (error "type component can't have a #\. inside: ~S" pathname)))
318 (strings ".")
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))
326 (dst-len 0)
327 (dots 0)
328 (last-slash nil))
329 (macrolet ((deposit (char)
330 `(progn
331 (setf (schar dst dst-len) ,char)
332 (incf dst-len))))
333 (dotimes (src-index src-len)
334 (let ((char (schar src src-index)))
335 (cond ((char= char #\.)
336 (when dots
337 (incf dots))
338 (deposit char))
339 ((char= char #\/)
340 (case dots
342 ;; either ``/...' or ``...//...'
343 (unless last-slash
344 (setf last-slash dst-len)
345 (deposit char)))
347 ;; either ``./...'' or ``..././...''
348 (decf dst-len))
350 ;; We've found ..
351 (cond
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)
357 last-slash)
358 (char= (schar dst (- last-slash 2)) #\.)
359 (char= (schar dst (1- last-slash)) #\.))
360 ;; The something before this .. is another ..
361 (deposit char)
362 (setf last-slash dst-len))
364 ;; The something is some directory or other.
365 (setf dst-len
366 (if prev-prev-slash
367 (1+ prev-prev-slash)
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)
373 (deposit char))))
375 ;; something other than a dot between slashes
376 (setf last-slash dst-len)
377 (deposit char)))
378 (setf dots 0))
380 (setf dots nil)
381 (setf (schar dst dst-len) char)
382 (incf dst-len))))))
383 (when (and last-slash (not (zerop last-slash)))
384 (case dots
386 ;; We've got ``foobar/.''
387 (decf dst-len))
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)) #\.)
393 (or (= last-slash 2)
394 (char= (schar dst (- last-slash 3)) #\/)))
395 (let ((prev-prev-slash
396 (position #\/ dst :end last-slash :from-end t)))
397 (if prev-prev-slash
398 (setf dst-len (1+ prev-prev-slash))
399 (return-from simplify-unix-namestring
400 (coerce "./" 'simple-string))))))))
401 (cond ((zerop dst-len)
402 "./")
403 ((= dst-len src-len)
404 dst)
406 (subseq dst 0 dst-len)))))