1 | ;;;-*-Mode: LISP; Package: CCL -*- |
---|
2 | ;;; |
---|
3 | ;;; Copyright (C) 1994-2001 Digitool, Inc |
---|
4 | ;;; Portions copyright (C) 2001 Clozure Associates |
---|
5 | ;;; This file is part of OpenMCL. |
---|
6 | ;;; |
---|
7 | ;;; OpenMCL is licensed under the terms of the Lisp Lesser GNU Public |
---|
8 | ;;; License , known as the LLGPL and distributed with OpenMCL as the |
---|
9 | ;;; file "LICENSE". The LLGPL consists of a preamble and the LGPL, |
---|
10 | ;;; which is distributed with OpenMCL as the file "LGPL". Where these |
---|
11 | ;;; conflict, the preamble takes precedence. |
---|
12 | ;;; |
---|
13 | ;;; OpenMCL is referenced in the preamble as the "LIBRARY." |
---|
14 | ;;; |
---|
15 | ;;; The LLGPL is also available online at |
---|
16 | ;;; http://opensource.franz.com/preamble.html |
---|
17 | |
---|
18 | ;; L1-files.lisp - Object oriented file stuff |
---|
19 | |
---|
20 | (in-package "CCL") |
---|
21 | |
---|
22 | (defconstant $paramErr -50) ; put this with the rest when we find the rest |
---|
23 | |
---|
24 | (defconstant pathname-case-type '(member :common :local :studly)) |
---|
25 | (defconstant pathname-arg-type '(or string pathname stream)) |
---|
26 | |
---|
27 | (defmacro signal-file-error (err-num &optional pathname &rest args) |
---|
28 | `(%signal-file-error ,err-num |
---|
29 | ,@(if pathname (list pathname)) |
---|
30 | ,@(if args args))) |
---|
31 | |
---|
32 | (defun %signal-file-error (err-num &optional pathname args) |
---|
33 | (declare (fixnum err-num)) |
---|
34 | (let* ((err-code (logior (ash 2 16) (the fixnum (logand #xffff (the fixnum err-num)))))) |
---|
35 | (funcall (if (< err-num 0) '%errno-disp '%err-disp) |
---|
36 | err-code |
---|
37 | pathname |
---|
38 | args))) |
---|
39 | |
---|
40 | |
---|
41 | (defvar %logical-host-translations% '()) |
---|
42 | (defvar *load-pathname* nil |
---|
43 | "the defaulted pathname that LOAD is currently loading") |
---|
44 | (defvar *load-truename* nil |
---|
45 | "the TRUENAME of the file that LOAD is currently loading") |
---|
46 | |
---|
47 | |
---|
48 | (defparameter *default-pathname-defaults* |
---|
49 | (let* ((hide-from-compile-file (%cons-pathname nil nil nil))) |
---|
50 | hide-from-compile-file)) |
---|
51 | |
---|
52 | ;Right now, the only way it's used is that an explicit ";" expands into it. |
---|
53 | ;Used to merge with it before going to ROM. Might be worth to bring that back, |
---|
54 | ;it doesn't hurt anything if you don't set it. |
---|
55 | ;(defparameter *working-directory* (%cons-pathname nil nil nil)) |
---|
56 | |
---|
57 | ;These come in useful... We should use them consistently and then document them, |
---|
58 | ;thereby earning the eternal gratitude of any users who find themselves with a |
---|
59 | ;ton of "foo.CL" files... |
---|
60 | (defparameter *.fasl-pathname* |
---|
61 | (%cons-pathname nil nil |
---|
62 | #.(pathname-type |
---|
63 | (backend-target-fasl-pathname *target-backend*)))) |
---|
64 | |
---|
65 | (defparameter *.lisp-pathname* (%cons-pathname nil nil "lisp")) |
---|
66 | |
---|
67 | (defun if-exists (if-exists filename &optional (prompt "Create ...")) |
---|
68 | (case if-exists |
---|
69 | (:error (signal-file-error (- #$EEXIST) filename)) |
---|
70 | ((:dialog) (overwrite-dialog filename prompt)) |
---|
71 | ((nil) nil) |
---|
72 | ((:ignored :overwrite :append :supersede :rename-and-delete :new-version :rename) filename) |
---|
73 | (t (report-bad-arg if-exists '(member :error :dialog nil :ignored :overwrite :append :supersede :rename-and-delete))))) |
---|
74 | |
---|
75 | (defun if-does-not-exist (if-does-not-exist filename) |
---|
76 | (case if-does-not-exist |
---|
77 | (:error (signal-file-error (- #$ENOENT) filename)) ; (%err-disp $err-no-file filename)) |
---|
78 | (:create filename) |
---|
79 | ((nil) (return-from if-does-not-exist nil)) |
---|
80 | (t (report-bad-arg if-does-not-exist '(member :error :create nil))))) |
---|
81 | |
---|
82 | |
---|
83 | (defun native-translated-namestring (path) |
---|
84 | (let ((name (translated-namestring path))) |
---|
85 | ;; Check that no quoted /'s |
---|
86 | (when (%path-mem-last-quoted "/" name) |
---|
87 | (signal-file-error $xbadfilenamechar name #\/)) |
---|
88 | ;; Check that no unquoted wildcards. |
---|
89 | (when (%path-mem-last "*" name) |
---|
90 | (signal-file-error $xillwild name)) |
---|
91 | (namestring-unquote name))) |
---|
92 | |
---|
93 | (defun native-untranslated-namestring (path) |
---|
94 | (let ((name (namestring (translate-logical-pathname path)))) |
---|
95 | ;; Check that no quoted /'s |
---|
96 | (when (%path-mem-last-quoted "/" name) |
---|
97 | (signal-file-error $xbadfilenamechar name #\/)) |
---|
98 | ;; Check that no unquoted wildcards. |
---|
99 | (when (%path-mem-last "*" name) |
---|
100 | (signal-file-error $xillwild name)) |
---|
101 | (namestring-unquote name))) |
---|
102 | |
---|
103 | ;; Reverse of above, take native namestring and make a Lisp pathname. |
---|
104 | (defun native-to-pathname (name) |
---|
105 | (pathname (%path-std-quotes name nil "*;:"))) |
---|
106 | |
---|
107 | (defun native-to-directory-pathname (name) |
---|
108 | (make-directory-pathname :device nil :directory (%path-std-quotes name nil "*;:"))) |
---|
109 | |
---|
110 | ;;; Make a pathname which names the specified directory; use |
---|
111 | ;;; explict :NAME, :TYPE, and :VERSION components of NIL. |
---|
112 | (defun make-directory-pathname (&key host device directory) |
---|
113 | (make-pathname :host host |
---|
114 | :device device |
---|
115 | :directory directory |
---|
116 | :name nil |
---|
117 | :type nil |
---|
118 | :version nil)) |
---|
119 | |
---|
120 | |
---|
121 | |
---|
122 | (defun namestring-unquote (name) |
---|
123 | (let ((esc *pathname-escape-character*)) |
---|
124 | (if (position esc name) |
---|
125 | (multiple-value-bind (sstr start end) (get-sstring name) |
---|
126 | (let ((result (make-string (%i- end start) :element-type 'base-char)) |
---|
127 | (dest 0)) |
---|
128 | (loop |
---|
129 | (let ((pos (or (position esc sstr :start start :end end) end))) |
---|
130 | (while (%i< start pos) |
---|
131 | (setf (%schar result dest) (%schar sstr start) |
---|
132 | start (%i+ start 1) |
---|
133 | dest (%i+ dest 1))) |
---|
134 | (when (eq pos end) |
---|
135 | (return nil)) |
---|
136 | (setq start (%i+ pos 1)))) |
---|
137 | (shrink-vector result dest))) |
---|
138 | name))) |
---|
139 | |
---|
140 | (defun translated-namestring (path) |
---|
141 | (namestring (translate-logical-pathname (merge-pathnames path)))) |
---|
142 | |
---|
143 | |
---|
144 | (defun truename (path) |
---|
145 | "Return the pathname for the actual file described by PATHNAME. |
---|
146 | An error of type FILE-ERROR is signalled if no such file exists, |
---|
147 | or the pathname is wild. |
---|
148 | |
---|
149 | Under Unix, the TRUENAME of a broken symlink is considered to be |
---|
150 | the name of the broken symlink itself." |
---|
151 | (or (probe-file path) |
---|
152 | (signal-file-error $err-no-file path))) |
---|
153 | |
---|
154 | (defun probe-file (path) |
---|
155 | "Return a pathname which is the truename of the file if it exists, or NIL |
---|
156 | otherwise. An error of type FILE-ERROR is signaled if pathname is wild." |
---|
157 | (when (wild-pathname-p path) |
---|
158 | (error 'file-error :error-type "Inappropriate use of wild pathname ~s" |
---|
159 | :pathname path)) |
---|
160 | (let* ((native (native-translated-namestring path)) |
---|
161 | (realpath (%realpath native)) |
---|
162 | (kind (if realpath (%unix-file-kind realpath)))) |
---|
163 | ;; Darwin's #_realpath will happily return non-nil for |
---|
164 | ;; files that don't exist. I don't think that |
---|
165 | ;; %UNIX-FILE-KIND would do so. |
---|
166 | (when kind |
---|
167 | (if (eq kind :directory) |
---|
168 | (unless (eq (aref realpath (1- (length realpath))) #\/) |
---|
169 | (setq realpath (%str-cat realpath "/")))) |
---|
170 | (if realpath |
---|
171 | (native-to-pathname realpath) |
---|
172 | nil)))) |
---|
173 | |
---|
174 | (defun cwd (path) |
---|
175 | (multiple-value-bind (realpath kind) (%probe-file-x (native-translated-namestring path)) |
---|
176 | (if kind |
---|
177 | (if (eq kind :directory) |
---|
178 | (let* ((error (%chdir realpath))) |
---|
179 | (if (eql error 0) |
---|
180 | (mac-default-directory) |
---|
181 | (signal-file-error error path))) |
---|
182 | (error "~S is not a directory pathname." path)) |
---|
183 | (error "Invalid pathname : ~s." path)))) |
---|
184 | |
---|
185 | (defun create-file (path &key (if-exists :error) (create-directory t)) |
---|
186 | (native-to-pathname (%create-file path :if-exists if-exists |
---|
187 | :create-directory create-directory))) |
---|
188 | (defun %create-file (path &key |
---|
189 | (if-exists :error) |
---|
190 | (create-directory t)) |
---|
191 | (when create-directory |
---|
192 | (create-directory path)) |
---|
193 | (when (directory-pathname-p path) |
---|
194 | (return-from %create-file (probe-file-x path))) |
---|
195 | (assert (or (eql if-exists :overwrite) (not (probe-file path))) () |
---|
196 | "~s ~s not implemented yet" :if-exists if-exists) |
---|
197 | (let* ((unix-name (native-translated-namestring path)) |
---|
198 | (fd (fd-open unix-name (logior #$O_WRONLY #$O_CREAT #$O_TRUNC)))) |
---|
199 | (if (< fd 0) |
---|
200 | (signal-file-error fd path) |
---|
201 | (fd-close fd)) |
---|
202 | (%realpath unix-name))) |
---|
203 | |
---|
204 | |
---|
205 | ;; The following assumptions are deeply embedded in all our pathname code: |
---|
206 | ;; (1) Non-logical pathname host is always :unspecific. |
---|
207 | ;; (2) Logical pathname host is never :unspecific. |
---|
208 | ;; (3) Logical pathname host can however be NIL, e.g. "foo;bar;baz". |
---|
209 | |
---|
210 | (defun %pathname-host (pathname) |
---|
211 | (if (logical-pathname-p pathname) |
---|
212 | (%logical-pathname-host pathname) |
---|
213 | :unspecific)) |
---|
214 | |
---|
215 | (defun %pathname-version (pathname) |
---|
216 | (if (logical-pathname-p pathname) |
---|
217 | (%logical-pathname-version pathname) |
---|
218 | (%physical-pathname-version pathname))) |
---|
219 | |
---|
220 | |
---|
221 | |
---|
222 | (defun pathname-host (thing) ; redefined later in this file |
---|
223 | (declare (ignore thing)) |
---|
224 | :unspecific) |
---|
225 | |
---|
226 | (defun pathname-version (thing) ; redefined later in this file |
---|
227 | (declare (ignore thing)) |
---|
228 | nil) |
---|
229 | |
---|
230 | (defmethod print-object ((pathname pathname) stream) |
---|
231 | (let ((flags (if (logical-pathname-p pathname) 4 |
---|
232 | (%i+ (if (eq (%pathname-type pathname) ':unspecific) 1 0) |
---|
233 | (if (equal (%pathname-name pathname) "") 2 0)))) |
---|
234 | (name (namestring pathname))) |
---|
235 | (if (and (not *print-readably*) (not *print-escape*)) |
---|
236 | (write-string name stream) |
---|
237 | (progn |
---|
238 | (format stream (if (or *print-escape* (eql flags 0)) "#P" "#~DP") flags) |
---|
239 | (write-escaped-string name stream #\"))))) |
---|
240 | |
---|
241 | |
---|
242 | (defun mac-default-directory () |
---|
243 | (let* ((native-name (current-directory-name)) |
---|
244 | (len (length native-name))) |
---|
245 | (declare (fixnum len)) |
---|
246 | (when (and (> len 1) |
---|
247 | (not (eq #\/ (schar native-name (1- len))))) |
---|
248 | (setq native-name (%str-cat native-name "/"))) |
---|
249 | (native-to-pathname native-name))) |
---|
250 | |
---|
251 | |
---|
252 | |
---|
253 | |
---|
254 | ; I thought I wanted to call this from elsewhere but perhaps not |
---|
255 | (defun absolute-directory-list (dirlist) |
---|
256 | ; just make relative absolute and remove ups where possible |
---|
257 | (when (eq (car dirlist) :relative) |
---|
258 | (let ((default (mac-default-directory)) default-dir) |
---|
259 | (when default |
---|
260 | (setq default-dir (%pathname-directory default)) |
---|
261 | (when default-dir |
---|
262 | (setq dirlist (append default-dir (cdr dirlist))))))) |
---|
263 | (when (memq :up dirlist) |
---|
264 | (setq dirlist (remove-up (copy-list dirlist)))) |
---|
265 | dirlist) |
---|
266 | |
---|
267 | ; destructively mungs dir |
---|
268 | (defun remove-up (dir) |
---|
269 | (setq dir (delete "." dir :test #'string=)) |
---|
270 | (let ((n 0) |
---|
271 | (last nil) |
---|
272 | (sub dir) |
---|
273 | has-abs kept-up) |
---|
274 | ;; from %std-directory-component we get dir with :relative/:absolute stripped |
---|
275 | (when (memq :up dir) |
---|
276 | (when (memq (car dir) '(:relative :absolute)) |
---|
277 | (setq sub (cdr dir) n 1 has-abs t)) |
---|
278 | (do () ((null sub)) |
---|
279 | (cond ((eq (car sub) :up) |
---|
280 | (cond ((or (eq n 0) |
---|
281 | (and (stringp last)(string= last "**")) |
---|
282 | (eq last :wild-inferiors) |
---|
283 | kept-up |
---|
284 | (and has-abs (eq n 1))) |
---|
285 | ;; up after "**" stays, initial :up stays, how bout 2 :ups |
---|
286 | (setq kept-up t) |
---|
287 | ) |
---|
288 | ((eq n 1) (setq dir (cddr dir) kept-up nil n -1)) |
---|
289 | (t (rplacd (nthcdr (- n 2) dir) (cdr sub)) |
---|
290 | (setq n (- n 2) kept-up nil)))) |
---|
291 | (t (setq kept-up nil))) |
---|
292 | (setq last (car sub) |
---|
293 | n (1+ n) |
---|
294 | sub (cdr sub)))) |
---|
295 | dir)) |
---|
296 | |
---|
297 | (defun namestring (path) |
---|
298 | "Construct the full (name)string form of the pathname." |
---|
299 | (%str-cat (host-namestring path) |
---|
300 | (directory-namestring path) |
---|
301 | (file-namestring path))) |
---|
302 | |
---|
303 | (defun host-namestring (path) |
---|
304 | "Return a string representation of the name of the host in the pathname." |
---|
305 | (let ((host (pathname-host path))) |
---|
306 | (if (and host (neq host :unspecific)) (%str-cat host ":") ""))) |
---|
307 | |
---|
308 | (defun directory-namestring (path) |
---|
309 | "Return a string representation of the directories used in the pathname." |
---|
310 | (%directory-list-namestring (pathname-directory path) |
---|
311 | (neq (pathname-host path) :unspecific))) |
---|
312 | |
---|
313 | (defun ensure-directory-namestring (string) |
---|
314 | (let* ((len (length string))) |
---|
315 | (if (and (> len 1) |
---|
316 | (not (eql (char string (1- len)) #\/))) |
---|
317 | (concatenate 'string string "/") |
---|
318 | string))) |
---|
319 | |
---|
320 | (defun %directory-list-namestring (list &optional logical-p) |
---|
321 | (if (null list) |
---|
322 | "" |
---|
323 | (let ((len (if (eq (car list) (if logical-p :relative :absolute)) 1 0)) |
---|
324 | |
---|
325 | result) |
---|
326 | (declare (fixnum len)(optimize (speed 3)(safety 0))) |
---|
327 | (dolist (s (%cdr list)) |
---|
328 | (case s |
---|
329 | (:wild (setq len (+ len 2))) |
---|
330 | (:wild-inferiors (setq len (+ len 3))) |
---|
331 | (:up (setq len (+ len 3))) |
---|
332 | (t ;This assumes that special chars in dir components are escaped, |
---|
333 | ;otherwise would have to pre-scan for escapes here. |
---|
334 | (setq len (+ len 1 (length s)))))) |
---|
335 | (setq result |
---|
336 | (make-string len)) |
---|
337 | (let ((i 0) |
---|
338 | (sep (if logical-p #\; #\/))) |
---|
339 | (declare (fixnum i)) |
---|
340 | (when (eq (%car list) (if logical-p :relative :absolute)) |
---|
341 | (setf (%schar result 0) sep) |
---|
342 | (setq i 1)) |
---|
343 | (dolist (s (%cdr list)) |
---|
344 | (case s |
---|
345 | (:wild (setq s "*")) |
---|
346 | (:wild-inferiors (setq s "**")) |
---|
347 | ;; There is no :up in logical pathnames, so this must be native |
---|
348 | (:up (setq s ".."))) |
---|
349 | (let ((len (length s))) |
---|
350 | (declare (fixnum len)) |
---|
351 | (move-string-bytes s result 0 i len) |
---|
352 | (setq i (+ i len))) |
---|
353 | (setf (%schar result i) sep) |
---|
354 | (setq i (1+ i)))) |
---|
355 | result))) |
---|
356 | |
---|
357 | (defun file-namestring (path) |
---|
358 | "Return a string representation of the name used in the pathname." |
---|
359 | (let* ((path (pathname path)) |
---|
360 | (name (pathname-name path)) |
---|
361 | (type (pathname-type path)) |
---|
362 | (version (if (typep path 'logical-pathname) (pathname-version path)))) |
---|
363 | (file-namestring-from-parts name type version))) |
---|
364 | |
---|
365 | (defun file-namestring-from-parts (name type version) |
---|
366 | (when (eq version :unspecific) (setq version nil)) |
---|
367 | (when (eq type :unspecific) (setq type nil)) |
---|
368 | (%str-cat (case name |
---|
369 | ((nil :unspecific) "") |
---|
370 | (:wild "*") |
---|
371 | (t (%path-std-quotes name nil "."))) |
---|
372 | (if (or type version) |
---|
373 | (%str-cat (case type |
---|
374 | ((nil) ".") |
---|
375 | (:wild ".*") |
---|
376 | (t (%str-cat "." (%path-std-quotes type nil ".")))) |
---|
377 | (case version |
---|
378 | ((nil) "") |
---|
379 | (:newest ".newest") |
---|
380 | (:wild ".*") |
---|
381 | (t (%str-cat "." (if (fixnump version) |
---|
382 | (%integer-to-string version) |
---|
383 | version))))) |
---|
384 | ""))) |
---|
385 | |
---|
386 | (defun enough-namestring (path &optional (defaults *default-pathname-defaults*)) |
---|
387 | "Return an abbreviated pathname sufficent to identify the pathname relative |
---|
388 | to the defaults." |
---|
389 | (if (null defaults) |
---|
390 | (namestring path) |
---|
391 | (let* ((dir (pathname-directory path)) |
---|
392 | (nam (pathname-name path)) |
---|
393 | (typ (pathname-type path)) |
---|
394 | (ver (pathname-version path)) |
---|
395 | (host (pathname-host path)) |
---|
396 | (logical-p (neq host :unspecific)) |
---|
397 | (default-dir (pathname-directory defaults))) |
---|
398 | ;; enough-host-namestring |
---|
399 | (setq host (if (and host |
---|
400 | (neq host :unspecific) |
---|
401 | (not (equalp host (pathname-host defaults)))) |
---|
402 | (%str-cat host ":") |
---|
403 | "")) |
---|
404 | ;; enough-directory-namestring |
---|
405 | (cond ((equalp dir default-dir) |
---|
406 | (setq dir '(:relative))) |
---|
407 | ((and dir default-dir |
---|
408 | (eq (car dir) :absolute) (eq (car default-dir) :absolute)) |
---|
409 | ; maybe make it relative to defaults |
---|
410 | (do ((p1 (cdr dir) (cdr p1)) |
---|
411 | (p2 (cdr default-dir) (cdr p2))) |
---|
412 | ((or (null p2) (null p1) (not (equalp (car p1) (car p2)))) |
---|
413 | (when (and (null p2) (neq p1 (cdr dir))) |
---|
414 | (setq dir (cons :relative p1))))))) |
---|
415 | (setq dir (%directory-list-namestring dir logical-p)) |
---|
416 | ;; enough-file-namestring |
---|
417 | (when (or (equalp ver (pathname-version defaults)) |
---|
418 | (not logical-p)) |
---|
419 | (setq ver nil)) |
---|
420 | (when (and (null ver) (equalp typ (pathname-type defaults))) |
---|
421 | (setq typ nil)) |
---|
422 | (when (and (null typ) (equalp nam (pathname-name defaults))) |
---|
423 | (setq nam nil)) |
---|
424 | (setq nam (file-namestring-from-parts nam typ ver)) |
---|
425 | (%str-cat host dir nam)))) |
---|
426 | |
---|
427 | (defun cons-pathname (dir name type &optional host version) |
---|
428 | (if (neq host :unspecific) |
---|
429 | (%cons-logical-pathname dir name type host version) |
---|
430 | (%cons-pathname dir name type version))) |
---|
431 | |
---|
432 | (defun pathname (path) |
---|
433 | "Convert thing (a pathname, string or stream) into a pathname." |
---|
434 | (etypecase path |
---|
435 | (pathname path) |
---|
436 | (stream (%path-from-stream path)) |
---|
437 | (string (string-to-pathname path)))) |
---|
438 | |
---|
439 | (defun %path-from-stream (stream) |
---|
440 | (or (stream-filename stream) (error "Can't determine pathname of ~S ." stream))) ; ??? |
---|
441 | |
---|
442 | ;Like (pathname stream) except returns NIL rather than error when there's no |
---|
443 | ;filename associated with the stream. |
---|
444 | (defun stream-pathname (stream &aux (path (stream-filename stream))) |
---|
445 | (when path (pathname path))) |
---|
446 | |
---|
447 | (defun string-to-pathname (string &optional (start 0) (end (length string)) |
---|
448 | (reference-host nil) |
---|
449 | (defaults *default-pathname-defaults*)) |
---|
450 | (require-type reference-host '(or null string)) |
---|
451 | (multiple-value-bind (sstr start end) (get-sstring string start end) |
---|
452 | (if (and (> end start) |
---|
453 | (eql (schar sstr start) #\~)) |
---|
454 | (setq sstr (tilde-expand (subseq sstr start end)) |
---|
455 | start 0 |
---|
456 | end (length sstr))) |
---|
457 | (let (directory name type host version (start-pos start) (end-pos end) has-slashes) |
---|
458 | (multiple-value-setq (host start-pos has-slashes) (pathname-host-sstr sstr start-pos end-pos)) |
---|
459 | (cond ((and host (neq host :unspecific)) |
---|
460 | (when (and reference-host (not (string-equal reference-host host))) |
---|
461 | (error "Host in ~S does not match requested host ~S" |
---|
462 | (%substr sstr start end) reference-host))) |
---|
463 | ((or reference-host |
---|
464 | (and defaults |
---|
465 | (neq (setq reference-host (pathname-host defaults)) :unspecific))) |
---|
466 | ;;If either a reference-host is specified or defaults is a logical pathname |
---|
467 | ;; then the string must be interpreted as a logical pathname. |
---|
468 | (when has-slashes |
---|
469 | (error "Illegal logical namestring ~S" (%substr sstr start end))) |
---|
470 | (setq host reference-host))) |
---|
471 | (multiple-value-setq (directory start-pos) (pathname-directory-sstr sstr start-pos end-pos host)) |
---|
472 | (unless (eq host :unspecific) |
---|
473 | (multiple-value-setq (version end-pos) (pathname-version-sstr sstr start-pos end-pos))) |
---|
474 | (multiple-value-setq (type end-pos) (pathname-type-sstr sstr start-pos end-pos)) |
---|
475 | ;; now everything else is the name |
---|
476 | (unless (eq start-pos end-pos) |
---|
477 | (setq name (%std-name-component (%substr sstr start-pos end-pos)))) |
---|
478 | (if (eq host :unspecific) |
---|
479 | (%cons-pathname directory name type version) |
---|
480 | (%cons-logical-pathname directory name type host version))))) |
---|
481 | |
---|
482 | (defun parse-namestring (thing &optional host (defaults *default-pathname-defaults*) |
---|
483 | &key (start 0) end junk-allowed) |
---|
484 | (declare (ignore junk-allowed)) |
---|
485 | (unless (typep thing 'string) |
---|
486 | (let* ((path (pathname thing)) |
---|
487 | (pathname-host (pathname-host path))) |
---|
488 | (when (and host pathname-host |
---|
489 | (or (eq pathname-host :unspecific) ;physical |
---|
490 | (not (string-equal host pathname-host)))) |
---|
491 | (error "Host in ~S does not match requested host ~S" path host)) |
---|
492 | (return-from parse-namestring (values path start)))) |
---|
493 | (when host |
---|
494 | (verify-logical-host-name host)) |
---|
495 | (setq end (check-sequence-bounds thing start end)) |
---|
496 | (values (string-to-pathname thing start end host defaults) end)) |
---|
497 | |
---|
498 | |
---|
499 | |
---|
500 | (defun make-pathname (&key (host nil host-p) |
---|
501 | device |
---|
502 | (directory nil directory-p) |
---|
503 | (name nil name-p) |
---|
504 | (type nil type-p) |
---|
505 | (version nil version-p) |
---|
506 | (defaults nil defaults-p) case |
---|
507 | &aux path) |
---|
508 | "Makes a new pathname from the component arguments. Note that host is |
---|
509 | a host-structure or string." |
---|
510 | (declare (ignore device)) |
---|
511 | (when case (setq case (require-type case pathname-case-type))) |
---|
512 | (if (null host-p) |
---|
513 | (let ((defaulted-defaults (if defaults-p defaults *default-pathname-defaults*))) |
---|
514 | (setq host (if defaulted-defaults |
---|
515 | (pathname-host defaulted-defaults) |
---|
516 | :unspecific))) |
---|
517 | (unless host (setq host :unspecific))) |
---|
518 | (if directory-p |
---|
519 | (setq directory (%std-directory-component directory host))) |
---|
520 | (if (and defaults (not directory-p)) |
---|
521 | (setq directory (pathname-directory defaults))) |
---|
522 | (setq name |
---|
523 | (if name-p |
---|
524 | (%std-name-component name) |
---|
525 | (and defaults (pathname-name defaults)))) |
---|
526 | (setq type |
---|
527 | (if type-p |
---|
528 | (%std-type-component type) |
---|
529 | (and defaults (pathname-type defaults)))) |
---|
530 | (setq version (if version-p |
---|
531 | (%logical-version-component version) |
---|
532 | (if name-p |
---|
533 | nil |
---|
534 | (and defaults (pathname-version defaults))))) |
---|
535 | (setq path |
---|
536 | (if (eq host :unspecific) |
---|
537 | (%cons-pathname directory name type version) |
---|
538 | (%cons-logical-pathname |
---|
539 | (or directory |
---|
540 | (unless directory-p '(:absolute))) |
---|
541 | name type host version))) |
---|
542 | (when (and (eq (car directory) :absolute) |
---|
543 | (member (cadr directory) '(:up :back))) |
---|
544 | (error 'simple-file-error :pathname path :error-type "Second element of absolute directory component in ~s is ~s" :format-arguments (list (cadr directory)))) |
---|
545 | (let* ((after-wif (cadr (member :wild-inferiors directory)))) |
---|
546 | (when (member after-wif '(:up :back)) |
---|
547 | (error 'simple-file-error :pathname path :error-type "Directory component in ~s contains :WILD-INFERIORS followed by ~s" :format-arguments (list after-wif)))) |
---|
548 | |
---|
549 | (when (and case (neq case :local)) |
---|
550 | (setf (%pathname-directory path) (%reverse-component-case (%pathname-directory path) case) |
---|
551 | (%pathname-name path) (%reverse-component-case (%pathname-name path) case) |
---|
552 | (%pathname-type path) (%reverse-component-case (%pathname-type path) case))) |
---|
553 | path) |
---|
554 | |
---|
555 | ;;; In portable CL, if the :directory argument to make pathname is a |
---|
556 | ;;; string, it should be the name of a top-level directory and should |
---|
557 | ;;; not contain any punctuation characters such as "/" or ";". In |
---|
558 | ;;; MCL a string :directory argument with slashes or semi-colons will |
---|
559 | ;;; be parsed as a directory in the obvious way. |
---|
560 | (defun %std-directory-component (directory host) |
---|
561 | (cond ((null directory) nil) |
---|
562 | ((eq directory :wild) '(:absolute :wild-inferiors)) |
---|
563 | ((stringp directory) (%directory-string-list directory 0 (length directory) host)) |
---|
564 | ((listp directory) |
---|
565 | ;Standardize the directory list, taking care not to cons if nothing |
---|
566 | ;needs to be changed. |
---|
567 | (let ((names (%cdr directory)) (new-names ())) |
---|
568 | (do ((nn names (%cdr nn))) |
---|
569 | ((null nn) (setq new-names (if new-names (nreverse new-names) names))) |
---|
570 | (let* ((name (car nn)) |
---|
571 | (new-name (%std-directory-part name))) |
---|
572 | (unless (eq name new-name) |
---|
573 | (unless new-names |
---|
574 | (do ((new-nn names (%cdr new-nn))) |
---|
575 | ((eq new-nn nn)) |
---|
576 | (push (%car new-nn) new-names)))) |
---|
577 | (when (or new-names (neq name new-name)) |
---|
578 | (push new-name new-names)))) |
---|
579 | (when (memq :up (or new-names names)) |
---|
580 | (setq new-names (remove-up (copy-list (or new-names names))))) |
---|
581 | (ecase (%car directory) |
---|
582 | (:relative |
---|
583 | (cond (new-names ; Just (:relative) is the same as NIL. - no it isnt |
---|
584 | (if (eq new-names names) |
---|
585 | directory |
---|
586 | (cons ':relative new-names))) |
---|
587 | (t directory))) |
---|
588 | (:absolute |
---|
589 | (cond ((null new-names) directory) ; But just (:absolute) IS the same as NIL |
---|
590 | ((eq new-names names) directory) |
---|
591 | (t (cons ':absolute new-names))))))) |
---|
592 | (t (report-bad-arg directory '(or string list (member :wild)))))) |
---|
593 | |
---|
594 | (defun %std-directory-part (name) |
---|
595 | (case name |
---|
596 | ((:wild :wild-inferiors :up) name) |
---|
597 | (:back :up) |
---|
598 | (t (cond ((string= name "*") :wild) |
---|
599 | ((string= name "**") :wild-inferiors) |
---|
600 | ((string= name "..") :up) |
---|
601 | (t (%path-std-quotes name "/:;*" "/:;")))))) |
---|
602 | |
---|
603 | ; this will allow creation of garbage pathname "foo:bar;bas:" do we care? |
---|
604 | (defun merge-pathnames (path &optional (defaults *default-pathname-defaults*) |
---|
605 | (default-version :newest)) |
---|
606 | "Construct a filled in pathname by completing the unspecified components |
---|
607 | from the defaults." |
---|
608 | ;(declare (ignore default-version)) |
---|
609 | (when (not (pathnamep path))(setq path (pathname path))) |
---|
610 | (when (and defaults (not (pathnamep defaults)))(setq defaults (pathname defaults))) |
---|
611 | (let* ((path-dir (pathname-directory path)) |
---|
612 | (path-host (pathname-host path)) |
---|
613 | (path-name (pathname-name path)) |
---|
614 | (path-type (pathname-type path)) |
---|
615 | (default-dir (and defaults (pathname-directory defaults))) |
---|
616 | (default-host (and defaults (pathname-host defaults))) |
---|
617 | ; take host from defaults iff path-dir is logical or absent - huh? |
---|
618 | (host (cond ((or (null path-host) ; added 7/96 |
---|
619 | (and (eq path-host :unspecific) |
---|
620 | (or (null path-dir) |
---|
621 | (null (cdr path-dir)) |
---|
622 | (and (eq :relative (car path-dir)) |
---|
623 | (not (memq default-host '(nil :unspecific))))))) |
---|
624 | |
---|
625 | default-host) |
---|
626 | (t path-host))) |
---|
627 | (dir (cond ((null path-dir) default-dir) |
---|
628 | ((null default-dir) path-dir) |
---|
629 | ((eq (car path-dir) ':relative) |
---|
630 | (let ((the-dir (append default-dir (%cdr path-dir)))) |
---|
631 | (when (memq ':up the-dir)(setq the-dir (remove-up (copy-list the-dir)))) |
---|
632 | the-dir)) |
---|
633 | (t path-dir))) |
---|
634 | (nam (or path-name |
---|
635 | (and defaults (pathname-name defaults)))) |
---|
636 | (typ (or path-type |
---|
637 | (and defaults (pathname-type defaults)))) |
---|
638 | (version (or (pathname-version path) |
---|
639 | (cond ((not path-name) |
---|
640 | (or (and defaults (pathname-version defaults)) |
---|
641 | default-version)) |
---|
642 | (t default-version))))) |
---|
643 | (if (and (pathnamep path) |
---|
644 | (eq dir (%pathname-directory path)) |
---|
645 | (eq nam path-name) |
---|
646 | (eq typ (%pathname-type path)) |
---|
647 | (eq host path-host) |
---|
648 | (eq version (pathname-version path))) |
---|
649 | path |
---|
650 | (cons-pathname dir nam typ host version)))) |
---|
651 | |
---|
652 | (defun directory-pathname-p (path) |
---|
653 | (let ((name (pathname-name path))(type (pathname-type path))) |
---|
654 | (and (or (null name) (eq name :unspecific) (%izerop (length name))) |
---|
655 | (or (null type) (eq type :unspecific))))) |
---|
656 | |
---|
657 | ;In CCL, a pathname is logical if and only if pathname-host is not :unspecific. |
---|
658 | (defun pathname-host (thing &key case) |
---|
659 | "Return PATHNAME's host." |
---|
660 | (when (streamp thing)(setq thing (%path-from-stream thing))) |
---|
661 | (when case (setq case (require-type case pathname-case-type))) |
---|
662 | (let ((name |
---|
663 | (typecase thing |
---|
664 | (logical-pathname (%logical-pathname-host thing)) |
---|
665 | (pathname :unspecific) |
---|
666 | (string (multiple-value-bind (sstr start end) (get-sstring thing) |
---|
667 | (pathname-host-sstr sstr start end))) |
---|
668 | (t (report-bad-arg thing pathname-arg-type))))) |
---|
669 | (if (and case (neq case :local)) |
---|
670 | (progn |
---|
671 | (when (and (eq case :common) (neq name :unspecific)) (setq case :logical)) |
---|
672 | (%reverse-component-case name case)) |
---|
673 | name))) |
---|
674 | |
---|
675 | (defun pathname-host-sstr (sstr start end &optional no-check) |
---|
676 | ;; A pathname with any (unescaped) /'s is always a physical pathname. |
---|
677 | ;; Otherwise, if the pathname has either a : or a ;, then it's always logical. |
---|
678 | ;; Otherwise, it's probably physical. |
---|
679 | ;; Return :unspecific for physical, host string or nil for a logical. |
---|
680 | (let* ((slash (%path-mem "/" sstr start end)) |
---|
681 | (pos (and (not slash) (%path-mem ":;" sstr start end))) |
---|
682 | (pos-char (and pos (%schar sstr pos))) |
---|
683 | (host (and (eql pos-char #\:) (%substr sstr start pos)))) |
---|
684 | (cond (host |
---|
685 | (unless (or no-check (logical-host-p host)) |
---|
686 | (error "~S is not a defined logical host" host)) |
---|
687 | (values host (%i+ pos 1) nil)) |
---|
688 | ((eql pos-char #\;) ; logical pathname with missing host |
---|
689 | (values nil start nil)) |
---|
690 | (t ;else a physical pathname. |
---|
691 | (values :unspecific start slash))))) |
---|
692 | |
---|
693 | |
---|
694 | (defun pathname-device (thing &key case) |
---|
695 | "Return PATHNAME's device." |
---|
696 | (declare (ignore case)) |
---|
697 | (cond ((typep (pathname thing) 'logical-pathname) :unspecific))) |
---|
698 | |
---|
699 | |
---|
700 | |
---|
701 | ;A directory is either NIL or a (possibly wildcarded) string ending in "/" or ";" |
---|
702 | ;Quoted /'s are allowed at this stage, though will get an error when go to the |
---|
703 | ;filesystem. |
---|
704 | (defun pathname-directory (path &key case) |
---|
705 | "Return PATHNAME's directory." |
---|
706 | (when (streamp path) (setq path (%path-from-stream path))) |
---|
707 | (when case (setq case (require-type case pathname-case-type))) |
---|
708 | (let* ((logical-p nil) |
---|
709 | (names (typecase path |
---|
710 | (logical-pathname (setq logical-p t) (%pathname-directory path)) |
---|
711 | (pathname (%pathname-directory path)) |
---|
712 | (string |
---|
713 | (multiple-value-bind (sstr start end) (get-sstring path) |
---|
714 | #+no |
---|
715 | (if (and (> end start) |
---|
716 | (eql (schar sstr start) #\~)) |
---|
717 | (setq sstr (tilde-expand (subseq sstr start end)) |
---|
718 | start 0 |
---|
719 | end (length sstr))) |
---|
720 | (multiple-value-bind (host pos2) (pathname-host-sstr sstr start end) |
---|
721 | (unless (eq host :unspecific) (setq logical-p t)) |
---|
722 | (pathname-directory-sstr sstr pos2 end host)))) |
---|
723 | (t (report-bad-arg path pathname-arg-type))))) |
---|
724 | (if (and case (neq case :local)) |
---|
725 | (progn |
---|
726 | (when (and (eq case :common) logical-p) (setq case :logical)) |
---|
727 | (%reverse-component-case names case)) |
---|
728 | names))) |
---|
729 | |
---|
730 | ;; Must match pathname-directory-end below |
---|
731 | (defun pathname-directory-sstr (sstr start end host) |
---|
732 | (if (and (eq host :unspecific) |
---|
733 | (> end start) |
---|
734 | (eql (schar sstr start) #\~)) |
---|
735 | (setq sstr (tilde-expand (subseq sstr start end)) |
---|
736 | start 0 |
---|
737 | end (length sstr))) |
---|
738 | (let ((pos (%path-mem-last (if (eq host :unspecific) "/" ";") sstr start end))) |
---|
739 | (if pos |
---|
740 | (values |
---|
741 | (%directory-string-list sstr start (setq pos (%i+ pos 1)) host) |
---|
742 | pos) |
---|
743 | (values (and (neq host :unspecific) |
---|
744 | (neq start end) |
---|
745 | '(:absolute)) |
---|
746 | start)))) |
---|
747 | |
---|
748 | ;; Must match pathname-directory-sstr above |
---|
749 | (defun pathname-directory-end (sstr start end) |
---|
750 | (multiple-value-bind (host pos2) (pathname-host-sstr sstr start end) |
---|
751 | (let ((pos (%path-mem-last (if (eq host :unspecific) "/" ";") sstr pos2 end))) |
---|
752 | (if pos |
---|
753 | (values (%i+ pos 1) host) |
---|
754 | (values pos2 host))))) |
---|
755 | |
---|
756 | (defun %directory-string-list (sstr start &optional (end (length sstr)) host) |
---|
757 | ;; Should use host to split by / vs. ; but for now suport both for either host, |
---|
758 | ;; like the mac version. It means that ';' has to be quoted in unix pathnames. |
---|
759 | (declare (ignore host)) |
---|
760 | ;This must cons up a fresh list, %expand-logical-directory rplacd's it. |
---|
761 | (labels ((std-part (sstr start end) |
---|
762 | (%std-directory-part (if (and (eq start 0) (eq end (length sstr))) |
---|
763 | sstr (%substr sstr start end)))) |
---|
764 | (split (sstr start end) |
---|
765 | (unless (eql start end) |
---|
766 | (let ((pos (%path-mem "/;" sstr start end))) |
---|
767 | (if (eq pos start) |
---|
768 | (split sstr (%i+ start 1) end) ;; treat multiple ////'s as one. |
---|
769 | (cons (std-part sstr start (or pos end)) |
---|
770 | (when pos |
---|
771 | (split sstr (%i+ pos 1) end)))))))) |
---|
772 | (unless (eq start end) |
---|
773 | (let* ((slash-pos (%path-mem "/" sstr start end)) |
---|
774 | (semi-pos (%path-mem ";" sstr start end)) |
---|
775 | (pos (or slash-pos semi-pos))) |
---|
776 | ; this never did anything sensible but did not signal an error |
---|
777 | (when (and slash-pos semi-pos) |
---|
778 | (error "Illegal directory string ~s" (%substr sstr start end))) |
---|
779 | (if (null pos) |
---|
780 | (list :relative (std-part sstr start end)) |
---|
781 | (let ((pos-char (%schar sstr pos))) |
---|
782 | (cons (if (eq pos start) |
---|
783 | (if (eq pos-char #\/) ':absolute ':relative) |
---|
784 | (if (eq pos-char #\/) ':relative ':absolute)) |
---|
785 | (split sstr start end)))))))) |
---|
786 | |
---|
787 | (defun pathname-version (path) |
---|
788 | "Return PATHNAME's version." |
---|
789 | (when (streamp path) (setq path (%path-from-stream path))) |
---|
790 | (typecase path |
---|
791 | (logical-pathname (%logical-pathname-version path)) |
---|
792 | (pathname (%physical-pathname-version path)) |
---|
793 | (string |
---|
794 | (multiple-value-bind (sstr start end) (get-sstring path) |
---|
795 | (multiple-value-bind (newstart host) (pathname-directory-end sstr start end) |
---|
796 | (if (eq host :unspecific) |
---|
797 | nil |
---|
798 | (pathname-version-sstr sstr newstart end))))) |
---|
799 | (t (report-bad-arg path pathname-arg-type)))) |
---|
800 | |
---|
801 | (defun pathname-version-sstr (sstr start end) |
---|
802 | (declare (fixnum start end)) |
---|
803 | (let ((pos (%path-mem-last "." sstr start end))) |
---|
804 | (if (and pos (%i> pos start) (%path-mem "." sstr start (%i- pos 1))) |
---|
805 | (values (%std-version-component (%substr sstr (%i+ pos 1) end)) pos) |
---|
806 | (values nil end)))) |
---|
807 | |
---|
808 | (defun %std-version-component (v) |
---|
809 | (cond ((or (null v) (eq v :unspecific)) v) |
---|
810 | ((eq v :wild) "*") |
---|
811 | ((string= v "") :unspecific) |
---|
812 | ((string-equal v "newest") :newest) |
---|
813 | ((every #'digit-char-p v) (parse-integer v)) |
---|
814 | (t (%path-std-quotes v "./:;*" "./:;")))) |
---|
815 | |
---|
816 | |
---|
817 | ;A name is either NIL or a (possibly wildcarded, possibly empty) string. |
---|
818 | ;Quoted /'s are allowed at this stage, though will get an error if go to the |
---|
819 | ;filesystem. |
---|
820 | (defun pathname-name (path &key case) |
---|
821 | "Return PATHNAME's name." |
---|
822 | (when (streamp path) (setq path (%path-from-stream path))) |
---|
823 | (when case (setq case (require-type case pathname-case-type))) |
---|
824 | (let* ((logical-p nil) |
---|
825 | (name (typecase path |
---|
826 | (logical-pathname (setq logical-p t) (%pathname-name path)) |
---|
827 | (pathname (%pathname-name path)) |
---|
828 | (string |
---|
829 | (multiple-value-bind (sstr start end) (get-sstring path) |
---|
830 | (multiple-value-bind (newstart host) (pathname-directory-end sstr start end) |
---|
831 | (setq start newstart) |
---|
832 | (unless (eq host :unspecific) |
---|
833 | (setq logical-p t) |
---|
834 | (setq end (nth-value 1 (pathname-version-sstr sstr start end)))) |
---|
835 | ;; TODO: -->> Need to make an exception so that ".emacs" is name with no type. |
---|
836 | ;; -->> Need to make an exception so that foo/.. is a directory pathname, |
---|
837 | ;; for native. |
---|
838 | (setq end (or (%path-mem-last "." sstr start end) end));; strip off type |
---|
839 | (unless (eq start end) |
---|
840 | (%std-name-component (%substr sstr start end)))))) |
---|
841 | (t (report-bad-arg path pathname-arg-type))))) |
---|
842 | (if (and case (neq case :local)) |
---|
843 | (progn |
---|
844 | (when (and (eq case :common) logical-p) (setq case :logical)) |
---|
845 | (%reverse-component-case name case)) |
---|
846 | name))) |
---|
847 | |
---|
848 | (defun %std-name-component (name) |
---|
849 | (cond ((or (null name) (eq name :unspecific) (eq name :wild)) name) |
---|
850 | ((equal name "*") :wild) |
---|
851 | (t (%path-std-quotes name "/:;*" "/:;")))) |
---|
852 | |
---|
853 | ;A type is either NIL or a (possibly wildcarded, possibly empty) string. |
---|
854 | ;Quoted :'s are allowed at this stage, though will get an error if go to the |
---|
855 | ;filesystem. |
---|
856 | (defun pathname-type (path &key case) |
---|
857 | "Return PATHNAME's type." |
---|
858 | (when (streamp path) (setq path (%path-from-stream path))) |
---|
859 | (when case (setq case (require-type case pathname-case-type))) |
---|
860 | (let* ((logical-p nil) |
---|
861 | (name (typecase path |
---|
862 | (logical-pathname (setq logical-p t) (%pathname-type path)) |
---|
863 | (pathname (%pathname-type path)) |
---|
864 | (string |
---|
865 | (multiple-value-bind (sstr start end) (get-sstring path) |
---|
866 | (multiple-value-bind (newstart host) (pathname-directory-end sstr start end) |
---|
867 | (setq start newstart) |
---|
868 | (unless (eq host :unspecific) |
---|
869 | (setq logical-p t) |
---|
870 | (setq end (nth-value 1 (pathname-version-sstr sstr start end)))) |
---|
871 | ;; TODO: -->> Need to make an exception so that ".emacs" is name with no type. |
---|
872 | ;; -->> Need to make an exception so that foo/.. is a directory pathname, |
---|
873 | ;; for native. |
---|
874 | (pathname-type-sstr sstr start end)))) |
---|
875 | (t (report-bad-arg path pathname-arg-type))))) |
---|
876 | (if (and case (neq case :local)) |
---|
877 | (progn |
---|
878 | (when (and (eq case :common) logical-p) (setq case :logical)) |
---|
879 | (%reverse-component-case name case)) |
---|
880 | name))) |
---|
881 | |
---|
882 | ; assumes dir & version if any has been stripped away |
---|
883 | (defun pathname-type-sstr (sstr start end) |
---|
884 | (let ((pos (%path-mem-last "." sstr start end))) |
---|
885 | (if pos |
---|
886 | (values (%std-type-component (%substr sstr (%i+ 1 pos) end)) pos) |
---|
887 | (values nil end)))) |
---|
888 | |
---|
889 | (defun %std-type-component (type) |
---|
890 | (cond ((or (null type) (eq type :unspecific) (eq type :wild)) type) |
---|
891 | ((equal type "*") :wild) |
---|
892 | (t (%path-std-quotes type "./:;*" "./:;")))) |
---|
893 | |
---|
894 | (defun %std-name-and-type (native) |
---|
895 | (let* ((end (length native)) |
---|
896 | (pos (position #\. native :from-end t)) |
---|
897 | (type (and pos |
---|
898 | (%path-std-quotes (%substr native (%i+ 1 pos) end) |
---|
899 | nil "/:;*"))) |
---|
900 | (name (unless (eq (or pos end) 0) |
---|
901 | (%path-std-quotes (if pos (%substr native 0 pos) native) |
---|
902 | nil "/:;*")))) |
---|
903 | (values name type))) |
---|
904 | |
---|
905 | (defun %reverse-component-case (name case) |
---|
906 | (cond ((not (stringp name)) |
---|
907 | (if (listp name) |
---|
908 | (mapcar #'(lambda (name) (%reverse-component-case name case)) name) |
---|
909 | name)) |
---|
910 | #+advanced-studlification-feature |
---|
911 | ((eq case :studly) (string-studlify name)) |
---|
912 | ((eq case :logical) |
---|
913 | (if (every #'(lambda (ch) (not (lower-case-p ch))) name) |
---|
914 | name |
---|
915 | (string-upcase name))) |
---|
916 | (t ; like %read-idiocy but non-destructive - need it be? |
---|
917 | (let ((which nil) |
---|
918 | (len (length name))) |
---|
919 | (dotimes (i len) |
---|
920 | (let ((c (%schar name i))) |
---|
921 | (if (alpha-char-p c) |
---|
922 | (if (upper-case-p c) |
---|
923 | (progn |
---|
924 | (when (eq which :lower)(return-from %reverse-component-case name)) |
---|
925 | (setq which :upper)) |
---|
926 | (progn |
---|
927 | (when (eq which :upper)(return-from %reverse-component-case name)) |
---|
928 | (setq which :lower)))))) |
---|
929 | (case which |
---|
930 | (:lower (string-upcase name)) |
---|
931 | (:upper (string-downcase name)) |
---|
932 | (t name)))))) |
---|
933 | |
---|
934 | ;;;;;;; String-with-quotes utilities |
---|
935 | (defun %path-mem-last-quoted (chars sstr &optional (start 0) (end (length sstr))) |
---|
936 | (while (%i< start end) |
---|
937 | (when (and (%%str-member (%schar sstr (setq end (%i- end 1))) chars) |
---|
938 | (%path-quoted-p sstr end start)) |
---|
939 | (return-from %path-mem-last-quoted end)))) |
---|
940 | |
---|
941 | (defun %path-mem-last (chars sstr &optional (start 0) (end (length sstr))) |
---|
942 | (while (%i< start end) |
---|
943 | (when (and (%%str-member (%schar sstr (setq end (%i- end 1))) chars) |
---|
944 | (not (%path-quoted-p sstr end start))) |
---|
945 | (return-from %path-mem-last end)))) |
---|
946 | |
---|
947 | (defun %path-mem (chars sstr &optional (start 0) (end (length sstr))) |
---|
948 | (let ((one-char (when (eq (length chars) 1) (%schar chars 0)))) |
---|
949 | (while (%i< start end) |
---|
950 | (let ((char (%schar sstr start))) |
---|
951 | (when (if one-char (eq char one-char)(%%str-member char chars)) |
---|
952 | (return-from %path-mem start)) |
---|
953 | (when (eq char *pathname-escape-character*) |
---|
954 | (setq start (%i+ start 1))) |
---|
955 | (setq start (%i+ start 1)))))) |
---|
956 | |
---|
957 | ; these for \: meaning this aint a logical host. Only legal for top level dir |
---|
958 | |
---|
959 | (defun %path-unquote-one-quoted (chars sstr &optional (start 0)(end (length sstr))) |
---|
960 | (let ((pos (%path-mem-last-quoted chars sstr start end))) |
---|
961 | (when (and pos (neq pos 1)) |
---|
962 | (cond ((or (%path-mem chars sstr start (1- pos)) |
---|
963 | (%path-mem-last-quoted chars sstr start (1- pos))) |
---|
964 | nil) |
---|
965 | (t (%str-cat (%substr sstr start (1- pos))(%substr sstr pos end))))))) |
---|
966 | |
---|
967 | (defun %path-one-quoted-p (chars sstr &optional (start 0)(end (length sstr))) |
---|
968 | (let ((pos (%path-mem-last-quoted chars sstr start end))) |
---|
969 | (when (and pos (neq pos 1)) |
---|
970 | (not (or (%path-mem-last-quoted chars sstr start (1- pos)) |
---|
971 | (%path-mem chars sstr start (1- pos))))))) |
---|
972 | |
---|
973 | (defun %path-quoted-p (sstr pos start &aux (esc *pathname-escape-character*) (q nil)) |
---|
974 | (while (and (%i> pos start) (eq (%schar sstr (setq pos (%i- pos 1))) esc)) |
---|
975 | (setq q (not q))) |
---|
976 | q) |
---|
977 | |
---|
978 | |
---|
979 | |
---|
980 | ;Standardize pathname quoting, so can do EQUAL. |
---|
981 | ;; Note that this can't be used to remove quotes because it |
---|
982 | ;; always keeps the escape character quoted. |
---|
983 | (defun %path-std-quotes (arg keep-quoted make-quoted) |
---|
984 | (when (symbolp arg) |
---|
985 | (error "Invalid pathname component ~S" arg)) |
---|
986 | (let* ((str arg) |
---|
987 | (esc *pathname-escape-character*) |
---|
988 | (end (length str)) |
---|
989 | res-str char) |
---|
990 | (multiple-value-bind (sstr start)(array-data-and-offset str) |
---|
991 | (setq end (+ start end)) |
---|
992 | (let ((i start)) |
---|
993 | (until (eq i end) |
---|
994 | (setq char (%schar sstr i)) |
---|
995 | (cond ((or (%%str-member char make-quoted) |
---|
996 | (and (null keep-quoted) (eq char esc))) |
---|
997 | (unless res-str |
---|
998 | (setq res-str (make-array (%i- end start) |
---|
999 | :element-type (array-element-type sstr) |
---|
1000 | :adjustable t :fill-pointer 0)) |
---|
1001 | (do ((j start (%i+ j 1))) ((eq j i)) |
---|
1002 | (vector-push-extend (%schar sstr j) res-str))) |
---|
1003 | (vector-push-extend esc res-str)) |
---|
1004 | ((neq char esc) nil) |
---|
1005 | ((eq (setq i (%i+ i 1)) end) |
---|
1006 | (error "Malformed pathname component string ~S" str)) |
---|
1007 | ((or (eq (setq char (%schar sstr i)) esc) |
---|
1008 | (%%str-member char keep-quoted)) |
---|
1009 | (when res-str (vector-push-extend esc res-str))) |
---|
1010 | (t |
---|
1011 | (unless res-str |
---|
1012 | (setq res-str (make-array (%i- end start) |
---|
1013 | :element-type (array-element-type sstr) |
---|
1014 | :adjustable t :fill-pointer 0)) |
---|
1015 | (do ((j start (%i+ j 1)) (end (%i- i 1))) ((eq j end)) |
---|
1016 | (vector-push-extend (%schar sstr j) res-str))))) |
---|
1017 | (when res-str (vector-push-extend char res-str)) |
---|
1018 | (setq i (%i+ i 1))) |
---|
1019 | (ensure-simple-string (or res-str str)))))) |
---|
1020 | |
---|
1021 | |
---|
1022 | |
---|
1023 | (defun %%str-member (char string) |
---|
1024 | (locally (declare (optimize (speed 3)(safety 0))) |
---|
1025 | (dotimes (i (the fixnum (length string))) |
---|
1026 | (when (eq (%schar string i) char) |
---|
1027 | (return i))))) |
---|
1028 | |
---|
1029 | |
---|
1030 | (defun file-write-date (path) |
---|
1031 | "Return file's creation date, or NIL if it doesn't exist. |
---|
1032 | An error of type file-error is signaled if file is a wild pathname" |
---|
1033 | (%file-write-date (native-translated-namestring path))) |
---|
1034 | |
---|
1035 | (defun file-author (path) |
---|
1036 | "Return the file author as a string, or NIL if the author cannot be |
---|
1037 | determined. Signal an error of type FILE-ERROR if FILE doesn't exist, |
---|
1038 | or FILE is a wild pathname." |
---|
1039 | (%file-author (native-translated-namestring path))) |
---|
1040 | |
---|
1041 | (defun touch (path) |
---|
1042 | (if (not (probe-file path)) |
---|
1043 | (progn |
---|
1044 | (ensure-directories-exist path) |
---|
1045 | (if (or (pathname-name path) |
---|
1046 | (pathname-type path)) |
---|
1047 | (create-file path))) |
---|
1048 | (%utimes (native-translated-namestring path))) |
---|
1049 | t) |
---|
1050 | |
---|
1051 | |
---|
1052 | ;-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_ |
---|
1053 | ; load, require, provide |
---|
1054 | |
---|
1055 | (defun find-load-file (file-name) |
---|
1056 | (let ((full-name (full-pathname file-name :no-error nil)) |
---|
1057 | (kind nil)) |
---|
1058 | (when full-name |
---|
1059 | (let ((file-type (pathname-type full-name))) |
---|
1060 | (if (and file-type (neq file-type :unspecific)) |
---|
1061 | (values (probe-file full-name) file-name file-name) |
---|
1062 | (let* ((source (merge-pathnames file-name *.lisp-pathname*)) |
---|
1063 | (fasl (merge-pathnames file-name *.fasl-pathname*)) |
---|
1064 | (true-source (probe-file source)) |
---|
1065 | (true-fasl (probe-file fasl))) |
---|
1066 | (cond (true-source |
---|
1067 | (if (and true-fasl |
---|
1068 | (> (file-write-date true-fasl) |
---|
1069 | (file-write-date true-source))) |
---|
1070 | (values true-fasl fasl source) |
---|
1071 | (values true-source source source))) |
---|
1072 | (true-fasl |
---|
1073 | (values true-fasl fasl fasl)) |
---|
1074 | ((and (multiple-value-setq (full-name kind) |
---|
1075 | (let* ((realpath (%realpath (native-translated-namestring full-name)))) |
---|
1076 | (if realpath |
---|
1077 | (%probe-file-x realpath )))) |
---|
1078 | (eq kind :file)) |
---|
1079 | (values full-name file-name file-name))))))))) |
---|
1080 | |
---|
1081 | |
---|
1082 | |
---|
1083 | |
---|
1084 | |
---|
1085 | (defun load (file-name &key (verbose *load-verbose*) |
---|
1086 | (print *load-print*) |
---|
1087 | (if-does-not-exist :error) |
---|
1088 | (external-format :default)) |
---|
1089 | "Load the file given by FILESPEC into the Lisp environment, returning |
---|
1090 | T on success. |
---|
1091 | |
---|
1092 | Extension: :PRINT :SOURCE means print source as well as value" |
---|
1093 | (loop |
---|
1094 | (restart-case |
---|
1095 | (return (%load file-name verbose print if-does-not-exist external-format)) |
---|
1096 | (retry-load () |
---|
1097 | :report (lambda (stream) (format stream "Retry loading ~s" file-name))) |
---|
1098 | (skip-load () |
---|
1099 | :report (lambda (stream) (format stream "Skip loading ~s" file-name)) |
---|
1100 | (return nil)) |
---|
1101 | (load-other () |
---|
1102 | :report (lambda (stream) (format stream "Load other file instead of ~s" file-name)) |
---|
1103 | (return |
---|
1104 | (load (choose-file-dialog) |
---|
1105 | :verbose verbose |
---|
1106 | :print print |
---|
1107 | :if-does-not-exist if-does-not-exist)))))) |
---|
1108 | |
---|
1109 | |
---|
1110 | (defun %load (file-name verbose print if-does-not-exist external-format) |
---|
1111 | (let ((*load-pathname* file-name) |
---|
1112 | (*load-truename* file-name) |
---|
1113 | (source-file file-name) |
---|
1114 | constructed-source-file |
---|
1115 | ;; Don't bind these: let OPTIMIZE proclamations/declamations |
---|
1116 | ;; persist, unless debugging. |
---|
1117 | #| |
---|
1118 | (*nx-speed* *nx-speed*) |
---|
1119 | (*nx-space* *nx-space*) |
---|
1120 | (*nx-safety* *nx-safety*) |
---|
1121 | (*nx-debug* *nx-debug*) |
---|
1122 | (*nx-cspeed* *nx-cspeed*) |
---|
1123 | |# |
---|
1124 | ) |
---|
1125 | (declare (special *load-pathname* *load-truename*)) |
---|
1126 | (when (typep file-name 'string-input-stream) |
---|
1127 | (when verbose |
---|
1128 | (format t "~&;Loading from stream ~S..." file-name) |
---|
1129 | (force-output)) |
---|
1130 | (let ((*package* *package*) |
---|
1131 | (*readtable* *readtable*)) |
---|
1132 | (load-from-stream file-name print)) |
---|
1133 | (return-from %load file-name)) |
---|
1134 | (unless (streamp file-name) |
---|
1135 | (multiple-value-setq (*load-truename* *load-pathname* source-file) |
---|
1136 | (find-load-file (merge-pathnames file-name))) |
---|
1137 | (when (not *load-truename*) |
---|
1138 | (return-from %load (if if-does-not-exist |
---|
1139 | (signal-file-error $err-no-file file-name)))) |
---|
1140 | (setq file-name *load-truename*)) |
---|
1141 | (let* ((*package* *package*) |
---|
1142 | (*readtable* *readtable*) |
---|
1143 | (*loading-files* (cons file-name (specialv *loading-files*))) |
---|
1144 | (*loading-file-source-file* (namestring source-file))) ;reset by fasload to logical name stored in the file? |
---|
1145 | (declare (special *loading-files* *loading-file-source-file*)) |
---|
1146 | (unwind-protect |
---|
1147 | (progn |
---|
1148 | (when verbose |
---|
1149 | (format t "~&;Loading ~S..." *load-pathname*) |
---|
1150 | (force-output)) |
---|
1151 | (cond ((fasl-file-p file-name) |
---|
1152 | (flet ((attempt-load (file-name) |
---|
1153 | (multiple-value-bind (winp err) |
---|
1154 | (%fasload (native-translated-namestring file-name)) |
---|
1155 | (if (not winp) |
---|
1156 | (%err-disp err))))) |
---|
1157 | (let ((*fasload-print* print)) |
---|
1158 | (declare (special *fasload-print*)) |
---|
1159 | (setq constructed-source-file (make-pathname :defaults file-name :type (pathname-type *.lisp-pathname*))) |
---|
1160 | (when (equalp source-file *load-truename*) |
---|
1161 | (when (probe-file constructed-source-file) |
---|
1162 | (setq source-file constructed-source-file))) |
---|
1163 | (if (and source-file |
---|
1164 | (not (equalp source-file file-name)) |
---|
1165 | (probe-file source-file)) |
---|
1166 | ;;really need restart-case-if instead of duplicating code below |
---|
1167 | (restart-case |
---|
1168 | (attempt-load file-name) |
---|
1169 | #+ignore |
---|
1170 | (load-other () :report (lambda (x) (format s "load other file")) |
---|
1171 | (return-from |
---|
1172 | %load |
---|
1173 | (%load (choose-file-dialog) verbose print if-does-not-exist))) |
---|
1174 | (load-source |
---|
1175 | () |
---|
1176 | :report (lambda (s) |
---|
1177 | (format s "Attempt to load ~s instead of ~s" |
---|
1178 | source-file *load-pathname*)) |
---|
1179 | (return-from |
---|
1180 | %load |
---|
1181 | (%load source-file verbose print if-does-not-exist external-format)))) |
---|
1182 | ;;duplicated code |
---|
1183 | (attempt-load file-name))))) |
---|
1184 | (t |
---|
1185 | (with-open-file (stream file-name |
---|
1186 | :element-type 'base-char |
---|
1187 | :external-format external-format) |
---|
1188 | (load-from-stream stream print)))))))) |
---|
1189 | file-name) |
---|
1190 | |
---|
1191 | (defun load-from-stream (stream print &aux (eof-val (list ())) val) |
---|
1192 | (with-compilation-unit (:override nil) ; try this for included files |
---|
1193 | (let ((env (new-lexical-environment (new-definition-environment 'eval)))) |
---|
1194 | (%rplacd (defenv.type (lexenv.parent-env env)) *outstanding-deferred-warnings*) |
---|
1195 | (while (neq eof-val (setq val (read stream nil eof-val))) |
---|
1196 | (when (eq print :source) (format t "~&Source: ~S~%" val)) |
---|
1197 | (setq val (cheap-eval-in-environment val env)) |
---|
1198 | (when print |
---|
1199 | (format t "~&~A~S~%" (if (eq print :source) "Value: " "") val)))))) |
---|
1200 | |
---|
1201 | (defun include (filename) |
---|
1202 | (load |
---|
1203 | (if (null *loading-files*) |
---|
1204 | filename |
---|
1205 | (merge-pathnames filename (directory-namestring (car *loading-files*)))))) |
---|
1206 | |
---|
1207 | (%fhave '%include #'include) |
---|
1208 | |
---|
1209 | (defun delete-file (path) |
---|
1210 | "Delete the specified FILE." |
---|
1211 | (let* ((namestring (native-translated-namestring path))) |
---|
1212 | (when (%realpath namestring) |
---|
1213 | (let* ((err (%delete-file namestring))) |
---|
1214 | (or (eql 0 err) (signal-file-error err path)))))) |
---|
1215 | |
---|
1216 | (defvar *known-backends* ()) |
---|
1217 | |
---|
1218 | (defun fasl-file-p (pathname) |
---|
1219 | (let* ((type (pathname-type pathname))) |
---|
1220 | (or (and (null *known-backends*) |
---|
1221 | (equal type (pathname-type *.fasl-pathname*))) |
---|
1222 | (dolist (b *known-backends*) |
---|
1223 | (when (equal type (pathname-type (backend-target-fasl-pathname b))) |
---|
1224 | (return t))) |
---|
1225 | (ignore-errors |
---|
1226 | (with-open-file (f pathname |
---|
1227 | :direction :input |
---|
1228 | :element-type '(unsigned-byte 8)) |
---|
1229 | ;; Assume that (potential) FASL files start with #xFF00 (big-endian), |
---|
1230 | ;; and that source files don't. |
---|
1231 | (and (eql (read-byte f nil nil) #xff) |
---|
1232 | (eql (read-byte f nil nil) #x00))))))) |
---|
1233 | |
---|
1234 | (defun provide (module) |
---|
1235 | "Adds a new module name to *MODULES* indicating that it has been loaded. |
---|
1236 | Module-name is a string designator" |
---|
1237 | (pushnew (string module) *modules* :test #'string=) |
---|
1238 | module) |
---|
1239 | |
---|
1240 | (defparameter *loading-modules* () "Internal. Prevents circularity") |
---|
1241 | (defparameter *module-provider-functions* '(module-provide-search-path) |
---|
1242 | "A list of functions called by REQUIRE to satisfy an unmet dependency. |
---|
1243 | Each function receives a module name as a single argument; if the function knows how to load that module, it should do so, add the module's name as a string to *MODULES* (perhaps by calling PROVIDE) and return non-NIL." |
---|
1244 | ) |
---|
1245 | |
---|
1246 | (defun module-provide-search-path (module) |
---|
1247 | ;; (format *debug-io* "trying module-provide-search-path~%") |
---|
1248 | (let* ((module-name (string module)) |
---|
1249 | (pathname (find-module-pathnames module-name))) |
---|
1250 | (when pathname |
---|
1251 | (if (consp pathname) |
---|
1252 | (dolist (path pathname) (load path)) |
---|
1253 | (load pathname)) |
---|
1254 | (provide module)))) |
---|
1255 | |
---|
1256 | (defun require (module &optional pathname) |
---|
1257 | "Loads a module, unless it already has been loaded. PATHNAMES, if supplied, |
---|
1258 | is a designator for a list of pathnames to be loaded if the/Users/gz/OpenMCL/trunk/ccl/examples/cocoa/easygui.lisp module |
---|
1259 | needs to be. If PATHNAMES is not supplied, functions from the list |
---|
1260 | *MODULE-PROVIDER-FUNCTIONS* are called in order with MODULE-NAME |
---|
1261 | as an argument, until one of them returns non-NIL. User code is |
---|
1262 | responsible for calling PROVIDE to indicate a successful load of the |
---|
1263 | module." |
---|
1264 | (let* ((str (string module)) |
---|
1265 | (original-modules (copy-list *modules*))) |
---|
1266 | (unless (or (member str *modules* :test #'string=) |
---|
1267 | (member str *loading-modules* :test #'string=)) |
---|
1268 | ;; The check of (and binding of) *LOADING-MODULES* is a |
---|
1269 | ;; traditional defense against circularity. (Another |
---|
1270 | ;; defense is not having circularity, of course.) The |
---|
1271 | ;; effect is that if something's in the process of being |
---|
1272 | ;; REQUIREd and it's REQUIREd again (transitively), |
---|
1273 | ;; the inner REQUIRE is a no-op. |
---|
1274 | (let ((*loading-modules* (cons str *loading-modules*))) |
---|
1275 | (if pathname |
---|
1276 | (dolist (path (if (atom pathname) (list pathname) pathname)) |
---|
1277 | (load path)) |
---|
1278 | (unless (some (lambda (p) (funcall p module)) |
---|
1279 | *module-provider-functions*) |
---|
1280 | (error "Module ~A was not provided by any function on ~S." module '*module-provider-functions*))))) |
---|
1281 | (values module |
---|
1282 | (set-difference *modules* original-modules)))) |
---|
1283 | |
---|
1284 | (defun find-module-pathnames (module) |
---|
1285 | "Returns the file or list of files making up the module" |
---|
1286 | (let ((mod-path (make-pathname :name (string-downcase module) :defaults nil)) path) |
---|
1287 | (dolist (path-cand *module-search-path* nil) |
---|
1288 | (let ((mod-cand (merge-pathnames mod-path path-cand))) |
---|
1289 | (if (wild-pathname-p path-cand) |
---|
1290 | (let* ((untyped-p (member (pathname-type mod-cand) '(nil :unspecific))) |
---|
1291 | (matches (if untyped-p |
---|
1292 | (or (directory (merge-pathnames mod-cand *.lisp-pathname*)) |
---|
1293 | (directory (merge-pathnames mod-cand *.fasl-pathname*))) |
---|
1294 | (directory mod-cand)))) |
---|
1295 | (when (and matches (null (cdr matches))) |
---|
1296 | (return (if untyped-p |
---|
1297 | (make-pathname :type nil :defaults (car matches)) |
---|
1298 | (car matches))))) |
---|
1299 | (when (setq path (find-load-file (merge-pathnames mod-path path-cand))) |
---|
1300 | (return path))))))) |
---|
1301 | |
---|
1302 | (defun wild-pathname-p (pathname &optional field-key) |
---|
1303 | "Predicate for determining whether pathname contains any wildcards." |
---|
1304 | (flet ((wild-p (name) (or (eq name :wild) |
---|
1305 | (eq name :wild-inferiors) |
---|
1306 | (and (stringp name) (%path-mem "*" name))))) |
---|
1307 | (case field-key |
---|
1308 | ((nil) |
---|
1309 | (or (some #'wild-p (pathname-directory pathname)) |
---|
1310 | (wild-p (pathname-name pathname)) |
---|
1311 | (wild-p (pathname-type pathname)) |
---|
1312 | (wild-p (pathname-version pathname)))) |
---|
1313 | (:host nil) |
---|
1314 | (:device nil) |
---|
1315 | (:directory (some #'wild-p (pathname-directory pathname))) |
---|
1316 | (:name (wild-p (pathname-name pathname))) |
---|
1317 | (:type (wild-p (pathname-type pathname))) |
---|
1318 | (:version (wild-p (pathname-version pathname))) |
---|
1319 | (t (wild-pathname-p pathname |
---|
1320 | (require-type field-key |
---|
1321 | '(member nil :host :device |
---|
1322 | :directory :name :type :version))))))) |
---|