1.0.43.75: pathnames: both Unix and Win32 use UNPARSE-PHYSICAL-DIRECTORY
[sbcl.git] / src / code / unix-pathname.lisp
1 ;;;; pathname parsing for Unix filesystems
2
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.
11
12 (in-package "SB!IMPL")
13
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)))))
33
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)))
47
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))
74                             (t
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)))
82                       (t
83                        nil)))
84               name
85               type
86               version))))
87
88 (defun parse-native-unix-namestring (namestring start end as-directory)
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            (directory (if (and as-directory
100                                (string/= "" (car (last components))))
101                           components
102                           (butlast components)))
103            (name-and-type
104             (unless as-directory
105               (let* ((end (first (last components)))
106                      (dot (position #\. end :from-end t)))
107                 ;; FIXME: can we get this dot-interpretation knowledge
108                 ;; from existing code?  EXTRACT-NAME-TYPE-AND-VERSION
109                 ;; does slightly more work than that.
110                 (cond
111                   ((string= end "")
112                    (list nil nil))
113                   ((and dot (> dot 0))
114                    (list (subseq end 0 dot) (subseq end (1+ dot))))
115                   (t
116                    (list end nil)))))))
117       (values nil
118               nil
119               (cons (if absolute :absolute :relative) directory)
120               (first name-and-type)
121               (second name-and-type)
122               nil))))
123
124 (/show0 "filesys.lisp 300")
125
126 (defun unparse-unix-host (pathname)
127   (declare (type pathname pathname)
128            (ignore pathname))
129   ;; this host designator needs to be recognized as a physical host in
130   ;; PARSE-NAMESTRING. Until sbcl-0.7.3.x, we had "Unix" here, but
131   ;; that's a valid Logical Hostname, so that's a bad choice. -- CSR,
132   ;; 2002-05-09
133   "")
134
135 (defun unparse-unix-file (pathname)
136   (declare (type pathname pathname))
137   (collect ((strings))
138     (let* ((name (%pathname-name pathname))
139            (type (%pathname-type pathname))
140            (type-supplied (not (or (null type) (eq type :unspecific)))))
141       ;; Note: by ANSI 19.3.1.1.5, we ignore the version slot when
142       ;; translating logical pathnames to a filesystem without
143       ;; versions (like Unix).
144       (when name
145         (when (and (null type)
146                    (typep name 'string)
147                    (> (length name) 0)
148                    (position #\. name :start 1))
149           (error "too many dots in the name: ~S" pathname))
150         (when (and (typep name 'string)
151                    (string= name ""))
152           (error "name is of length 0: ~S" pathname))
153         (strings (unparse-physical-piece name)))
154       (when type-supplied
155         (unless name
156           (error "cannot specify the type without a file: ~S" pathname))
157         (when (typep type 'simple-string)
158           (when (position #\. type)
159             (error "type component can't have a #\. inside: ~S" pathname)))
160         (strings ".")
161         (strings (unparse-physical-piece type))))
162     (apply #'concatenate 'simple-string (strings))))
163
164 (/show0 "filesys.lisp 406")
165
166 (defun unparse-unix-namestring (pathname)
167   (declare (type pathname pathname))
168   (concatenate 'simple-string
169                (unparse-physical-directory pathname)
170                (unparse-unix-file pathname)))
171
172 (defun unparse-native-unix-namestring (pathname as-file)
173   (declare (type pathname pathname))
174   (let* ((directory (pathname-directory pathname))
175          (name (pathname-name pathname))
176          (name-present-p (typep name '(not (member nil :unspecific))))
177          (name-string (if name-present-p name ""))
178          (type (pathname-type pathname))
179          (type-present-p (typep type '(not (member nil :unspecific))))
180          (type-string (if type-present-p type "")))
181     (when name-present-p
182       (setf as-file nil))
183     (coerce
184      (with-output-to-string (s)
185        (when directory
186          (ecase (car directory)
187            (:absolute (write-char #\/ s))
188            (:relative)))
189        (loop for (piece . subdirs) on (cdr directory)
190           do (typecase piece
191                ((member :up) (write-string ".." s))
192                (string (write-string piece s))
193                (t (error "ungood directory segment in NATIVE-NAMESTRING: ~S"
194                          piece)))
195           if (or subdirs (stringp name))
196           do (write-char #\/ s)
197           else
198           do (unless as-file
199                (write-char #\/ s)))
200        (if name-present-p
201            (progn
202              (unless (stringp name-string) ;some kind of wild field
203                (error "ungood name component in NATIVE-NAMESTRING: ~S" name))
204              (write-string name-string s)
205              (when type-present-p
206                (unless (stringp type-string) ;some kind of wild field
207                  (error "ungood type component in NATIVE-NAMESTRING: ~S" type))
208                (write-char #\. s)
209                (write-string type-string s)))
210            (when type-present-p ; type without a name
211              (error
212               "type component without a name component in NATIVE-NAMESTRING: ~S"
213               type))))
214      'simple-string)))
215
216 (defun unparse-unix-enough (pathname defaults)
217   (declare (type pathname pathname defaults))
218   (flet ((lose ()
219            (error "~S cannot be represented relative to ~S."
220                   pathname defaults)))
221     (collect ((strings))
222       (let* ((pathname-directory (%pathname-directory pathname))
223              (defaults-directory (%pathname-directory defaults))
224              (prefix-len (length defaults-directory))
225              (result-directory
226               (cond ((null pathname-directory) '(:relative))
227                     ((eq (car pathname-directory) :relative)
228                      pathname-directory)
229                     ((and (> prefix-len 0)
230                           (>= (length pathname-directory) prefix-len)
231                           (compare-component (subseq pathname-directory
232                                                      0 prefix-len)
233                                              defaults-directory))
234                      ;; Pathname starts with a prefix of default. So
235                      ;; just use a relative directory from then on out.
236                      (cons :relative (nthcdr prefix-len pathname-directory)))
237                     ((eq (car pathname-directory) :absolute)
238                      ;; We are an absolute pathname, so we can just use it.
239                      pathname-directory)
240                     (t
241                      (bug "Bad fallthrough in ~S" 'unparse-unix-enough)))))
242         (strings (unparse-physical-directory-list result-directory)))
243       (let* ((pathname-type (%pathname-type pathname))
244              (type-needed (and pathname-type
245                                (not (eq pathname-type :unspecific))))
246              (pathname-name (%pathname-name pathname))
247              (name-needed (or type-needed
248                               (and pathname-name
249                                    (not (compare-component pathname-name
250                                                            (%pathname-name
251                                                             defaults)))))))
252         (when name-needed
253           (unless pathname-name (lose))
254           (when (and (null pathname-type)
255                      (typep pathname-name 'simple-string)
256                      (position #\. pathname-name :start 1))
257             (error "too many dots in the name: ~S" pathname))
258           (strings (unparse-physical-piece pathname-name)))
259         (when type-needed
260           (when (or (null pathname-type) (eq pathname-type :unspecific))
261             (lose))
262           (when (typep pathname-type 'simple-string)
263             (when (position #\. pathname-type)
264               (error "type component can't have a #\. inside: ~S" pathname)))
265           (strings ".")
266           (strings (unparse-physical-piece pathname-type))))
267       (apply #'concatenate 'simple-string (strings)))))
268
269 (defun simplify-unix-namestring (src)
270   (declare (type simple-string src))
271   (let* ((src-len (length src))
272          (dst (make-string src-len :element-type 'character))
273          (dst-len 0)
274          (dots 0)
275          (last-slash nil))
276     (macrolet ((deposit (char)
277                  `(progn
278                     (setf (schar dst dst-len) ,char)
279                     (incf dst-len))))
280       (dotimes (src-index src-len)
281         (let ((char (schar src src-index)))
282           (cond ((char= char #\.)
283                  (when dots
284                    (incf dots))
285                  (deposit char))
286                 ((char= char #\/)
287                  (case dots
288                    (0
289                     ;; either ``/...' or ``...//...'
290                     (unless last-slash
291                       (setf last-slash dst-len)
292                       (deposit char)))
293                    (1
294                     ;; either ``./...'' or ``..././...''
295                     (decf dst-len))
296                    (2
297                     ;; We've found ..
298                     (cond
299                       ((and last-slash (not (zerop last-slash)))
300                        ;; There is something before this ..
301                        (let ((prev-prev-slash
302                               (position #\/ dst :end last-slash :from-end t)))
303                          (cond ((and (= (+ (or prev-prev-slash 0) 2)
304                                         last-slash)
305                                      (char= (schar dst (- last-slash 2)) #\.)
306                                      (char= (schar dst (1- last-slash)) #\.))
307                                 ;; The something before this .. is another ..
308                                 (deposit char)
309                                 (setf last-slash dst-len))
310                                (t
311                                 ;; The something is some directory or other.
312                                 (setf dst-len
313                                       (if prev-prev-slash
314                                           (1+ prev-prev-slash)
315                                           0))
316                                 (setf last-slash prev-prev-slash)))))
317                       (t
318                        ;; There is nothing before this .., so we need to keep it
319                        (setf last-slash dst-len)
320                        (deposit char))))
321                    (t
322                     ;; something other than a dot between slashes
323                     (setf last-slash dst-len)
324                     (deposit char)))
325                  (setf dots 0))
326                 (t
327                  (setf dots nil)
328                  (setf (schar dst dst-len) char)
329                  (incf dst-len))))))
330     (when (and last-slash (not (zerop last-slash)))
331       (case dots
332         (1
333          ;; We've got  ``foobar/.''
334          (decf dst-len))
335         (2
336          ;; We've got ``foobar/..''
337          (unless (and (>= last-slash 2)
338                       (char= (schar dst (1- last-slash)) #\.)
339                       (char= (schar dst (- last-slash 2)) #\.)
340                       (or (= last-slash 2)
341                           (char= (schar dst (- last-slash 3)) #\/)))
342            (let ((prev-prev-slash
343                   (position #\/ dst :end last-slash :from-end t)))
344              (if prev-prev-slash
345                  (setf dst-len (1+ prev-prev-slash))
346                  (return-from simplify-unix-namestring
347                    (coerce "./" 'simple-string))))))))
348     (cond ((zerop dst-len)
349            "./")
350           ((= dst-len src-len)
351            dst)
352           (t
353            (subseq dst 0 dst-len)))))