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