1 | /* |
---|
2 | * Load.java |
---|
3 | * |
---|
4 | * Copyright (C) 2002-2007 Peter Graves |
---|
5 | * $Id: Load.java 12254 2009-11-06 20:07:54Z ehuelsmann $ |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
20 | * |
---|
21 | * As a special exception, the copyright holders of this library give you |
---|
22 | * permission to link this library with independent modules to produce an |
---|
23 | * executable, regardless of the license terms of these independent |
---|
24 | * modules, and to copy and distribute the resulting executable under |
---|
25 | * terms of your choice, provided that you also meet, for each linked |
---|
26 | * independent module, the terms and conditions of the license of that |
---|
27 | * module. An independent module is a module which is not derived from |
---|
28 | * or based on this library. If you modify this library, you may extend |
---|
29 | * this exception to your version of the library, but you are not |
---|
30 | * obligated to do so. If you do not wish to do so, delete this |
---|
31 | * exception statement from your version. |
---|
32 | */ |
---|
33 | |
---|
34 | package org.armedbear.lisp; |
---|
35 | |
---|
36 | import java.io.ByteArrayInputStream; |
---|
37 | import java.io.ByteArrayOutputStream; |
---|
38 | import java.io.File; |
---|
39 | import java.io.FileInputStream; |
---|
40 | import java.io.FileNotFoundException; |
---|
41 | import java.io.IOException; |
---|
42 | import java.io.InputStream; |
---|
43 | import java.net.URL; |
---|
44 | import java.net.URLDecoder; |
---|
45 | import java.util.zip.ZipEntry; |
---|
46 | import java.util.zip.ZipException; |
---|
47 | import java.util.zip.ZipFile; |
---|
48 | import java.util.zip.ZipInputStream; |
---|
49 | |
---|
50 | public final class Load extends Lisp |
---|
51 | { |
---|
52 | public static final LispObject load(String filename) |
---|
53 | |
---|
54 | { |
---|
55 | final LispThread thread = LispThread.currentThread(); |
---|
56 | return load(new Pathname(filename), |
---|
57 | filename, |
---|
58 | Symbol.LOAD_VERBOSE.symbolValue(thread) != NIL, |
---|
59 | Symbol.LOAD_PRINT.symbolValue(thread) != NIL, |
---|
60 | true); |
---|
61 | } |
---|
62 | |
---|
63 | private static final File findLoadableFile(final String filename, |
---|
64 | final String dir) |
---|
65 | { |
---|
66 | File file = new File(dir, filename); |
---|
67 | if (!file.isFile()) { |
---|
68 | String extension = getExtension(filename); |
---|
69 | if (extension == null) { |
---|
70 | // No extension specified. Try appending ".lisp" or ".abcl". |
---|
71 | File lispFile = new File(dir, filename.concat(".lisp")); |
---|
72 | File abclFile = new File(dir, filename.concat(".abcl")); |
---|
73 | if (lispFile.isFile() && abclFile.isFile()) { |
---|
74 | if (abclFile.lastModified() > lispFile.lastModified()) { |
---|
75 | return abclFile; |
---|
76 | } else { |
---|
77 | return lispFile; |
---|
78 | } |
---|
79 | } else if (abclFile.isFile()) { |
---|
80 | return abclFile; |
---|
81 | } else if (lispFile.isFile()) { |
---|
82 | return lispFile; |
---|
83 | } |
---|
84 | } |
---|
85 | } else |
---|
86 | return file; // the file exists |
---|
87 | return null; // this is the error case: the file does not exist |
---|
88 | // no need to check again at the caller |
---|
89 | } |
---|
90 | |
---|
91 | public static final LispObject load(Pathname pathname, |
---|
92 | String filename, |
---|
93 | boolean verbose, |
---|
94 | boolean print, |
---|
95 | boolean ifDoesNotExist) |
---|
96 | { |
---|
97 | return load(pathname, filename, verbose, print, ifDoesNotExist, false); |
---|
98 | } |
---|
99 | |
---|
100 | public static final LispObject load(Pathname pathname, |
---|
101 | String filename, |
---|
102 | boolean verbose, |
---|
103 | boolean print, |
---|
104 | boolean ifDoesNotExist, |
---|
105 | boolean returnLastResult) |
---|
106 | |
---|
107 | { |
---|
108 | String dir = null; |
---|
109 | if (!Utilities.isFilenameAbsolute(filename)) { |
---|
110 | dir = coerceToPathname(Symbol.DEFAULT_PATHNAME_DEFAULTS |
---|
111 | .symbolValue()).getNamestring(); |
---|
112 | } |
---|
113 | |
---|
114 | String zipFileName = null; |
---|
115 | String zipEntryName = null; |
---|
116 | if (filename.startsWith("jar:file:")) { |
---|
117 | String s = new String(filename); |
---|
118 | s = s.substring(9); |
---|
119 | int index = s.lastIndexOf('!'); |
---|
120 | if (index >= 0) { |
---|
121 | zipFileName = s.substring(0, index); |
---|
122 | zipEntryName = s.substring(index + 1); |
---|
123 | if (zipEntryName.length() > 0 && zipEntryName.charAt(0) == '/') |
---|
124 | zipEntryName = zipEntryName.substring(1); |
---|
125 | if (Utilities.isPlatformWindows) { |
---|
126 | if (zipFileName.length() > 0 && zipFileName.charAt(0) == '/') |
---|
127 | zipFileName = zipFileName.substring(1); |
---|
128 | } |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | File file = findLoadableFile(filename, dir); |
---|
133 | if (null == file && null == zipFileName) { |
---|
134 | if (ifDoesNotExist) |
---|
135 | return error(new FileError("File not found: " + filename, pathname)); |
---|
136 | else |
---|
137 | return NIL; |
---|
138 | } |
---|
139 | |
---|
140 | if (checkZipFile(file)) { |
---|
141 | // Either we are loading a packed FASL (i.e. ZIP with suffix ".abcl") |
---|
142 | // Or we are loading from a JAR archive |
---|
143 | if (".abcl".equals(getExtension(file.getPath()))) { |
---|
144 | // So we adjust the value passed to |
---|
145 | // loadFileFromStream() to get any further loading |
---|
146 | // within this invocation of LOAD to work properly. |
---|
147 | filename = file.getPath(); |
---|
148 | } |
---|
149 | zipFileName = file.getPath(); |
---|
150 | zipEntryName = file.getName(); |
---|
151 | } |
---|
152 | |
---|
153 | String truename = filename; |
---|
154 | ZipFile zipfile = null; |
---|
155 | |
---|
156 | boolean packedFASL = false; |
---|
157 | |
---|
158 | InputStream in = null; |
---|
159 | if (zipFileName != null) { |
---|
160 | try { |
---|
161 | zipfile = ZipCache.getZip(zipFileName); |
---|
162 | } |
---|
163 | catch (Throwable t) { |
---|
164 | return error (new FileError("Zip file not found: " + filename, pathname)); |
---|
165 | } |
---|
166 | ZipEntry entry = zipfile.getEntry(zipEntryName); |
---|
167 | if (null == entry) { |
---|
168 | // try appending "._" to base filename |
---|
169 | int index = zipEntryName.lastIndexOf('.'); |
---|
170 | if (-1 == index) index = zipEntryName.length(); |
---|
171 | zipEntryName = zipEntryName.substring(0, index).concat("._"); |
---|
172 | entry = zipfile.getEntry(zipEntryName); |
---|
173 | } |
---|
174 | if (null == entry) { |
---|
175 | // try appending ".abcl" to base filename |
---|
176 | int index = zipEntryName.lastIndexOf('.'); |
---|
177 | if (index == -1) |
---|
178 | index = zipEntryName.length(); |
---|
179 | zipEntryName = zipEntryName.substring(0, index).concat(".abcl"); |
---|
180 | entry = zipfile.getEntry(zipEntryName); |
---|
181 | if (entry != null) |
---|
182 | packedFASL = true; |
---|
183 | } |
---|
184 | if (null == entry) { |
---|
185 | // Try looking for ".lisp" |
---|
186 | int i = zipEntryName.lastIndexOf('.'); |
---|
187 | if (i == -1) { |
---|
188 | i = zipEntryName.length(); |
---|
189 | } |
---|
190 | zipEntryName = zipEntryName.substring(0, i).concat(".lisp"); |
---|
191 | entry = zipfile.getEntry(zipEntryName); |
---|
192 | if (entry == null) { |
---|
193 | return error(new LispError("Failed to find " + zipEntryName + " in " |
---|
194 | + zipFileName + ".")); |
---|
195 | } |
---|
196 | } |
---|
197 | |
---|
198 | if (null == entry) { |
---|
199 | return error(new FileError("Can't find zip file entry " |
---|
200 | + zipEntryName, pathname)); |
---|
201 | } |
---|
202 | if (".abcl".equals(getExtension(zipEntryName))) { |
---|
203 | packedFASL = true; |
---|
204 | } |
---|
205 | if (packedFASL) { |
---|
206 | // If we are loading a packed FASL from the JAR we |
---|
207 | // have to decompress it first, and seek for the '._' |
---|
208 | // init FASL. |
---|
209 | int i = zipEntryName.lastIndexOf('.'); |
---|
210 | String subZipEntryName = zipEntryName.substring(0, i).concat("._"); |
---|
211 | in = Utilities.getZippedZipEntryAsInputStream(zipfile, |
---|
212 | zipEntryName, |
---|
213 | subZipEntryName); |
---|
214 | } else { |
---|
215 | try { |
---|
216 | in = zipfile.getInputStream(entry); |
---|
217 | } |
---|
218 | catch (IOException e) { |
---|
219 | return error(new LispError(e.getMessage())); |
---|
220 | } |
---|
221 | } |
---|
222 | } else { |
---|
223 | try { |
---|
224 | in = new FileInputStream(file); |
---|
225 | truename = file.getCanonicalPath(); |
---|
226 | } |
---|
227 | catch (FileNotFoundException e) { |
---|
228 | if (ifDoesNotExist) |
---|
229 | return error(new FileError("File not found: " + filename, |
---|
230 | pathname)); |
---|
231 | else |
---|
232 | return NIL; |
---|
233 | } |
---|
234 | catch (IOException e) { |
---|
235 | return error(new LispError(e.getMessage())); |
---|
236 | } |
---|
237 | } |
---|
238 | try { |
---|
239 | |
---|
240 | return loadFileFromStream(null, truename, |
---|
241 | new Stream(in, Symbol.CHARACTER), |
---|
242 | verbose, print, false, returnLastResult); |
---|
243 | } |
---|
244 | catch (FaslVersionMismatch e) { |
---|
245 | FastStringBuffer sb = |
---|
246 | new FastStringBuffer("Incorrect fasl version: "); |
---|
247 | sb.append(truename); |
---|
248 | return error(new SimpleError(sb.toString())); |
---|
249 | } |
---|
250 | finally { |
---|
251 | if (in != null) { |
---|
252 | try { |
---|
253 | in.close(); |
---|
254 | } |
---|
255 | catch (IOException e) { |
---|
256 | return error(new LispError(e.getMessage())); |
---|
257 | } |
---|
258 | } |
---|
259 | if (zipfile != null) { |
---|
260 | try { |
---|
261 | ZipCache.removeZip(zipfile.getName()); |
---|
262 | } |
---|
263 | catch (IOException e) { |
---|
264 | return error(new LispError(e.getMessage())); |
---|
265 | } |
---|
266 | } |
---|
267 | } |
---|
268 | } |
---|
269 | |
---|
270 | public static final LispObject loadSystemFile(String filename) |
---|
271 | |
---|
272 | { |
---|
273 | final LispThread thread = LispThread.currentThread(); |
---|
274 | return loadSystemFile(filename, |
---|
275 | Symbol.LOAD_VERBOSE.symbolValue(thread) != NIL, |
---|
276 | Symbol.LOAD_PRINT.symbolValue(thread) != NIL, |
---|
277 | false); |
---|
278 | } |
---|
279 | |
---|
280 | public static final LispObject loadSystemFile(String filename, boolean auto) |
---|
281 | |
---|
282 | { |
---|
283 | LispThread thread = LispThread.currentThread(); |
---|
284 | if (auto) { |
---|
285 | SpecialBinding lastSpecialBinding = thread.lastSpecialBinding; |
---|
286 | thread.bindSpecial(Symbol.CURRENT_READTABLE, |
---|
287 | STANDARD_READTABLE.symbolValue(thread)); |
---|
288 | thread.bindSpecial(Symbol._PACKAGE_, PACKAGE_CL_USER); |
---|
289 | try { |
---|
290 | return loadSystemFile(filename, |
---|
291 | _AUTOLOAD_VERBOSE_.symbolValue(thread) != NIL, |
---|
292 | Symbol.LOAD_PRINT.symbolValue(thread) != NIL, |
---|
293 | auto); |
---|
294 | } |
---|
295 | finally { |
---|
296 | thread.lastSpecialBinding = lastSpecialBinding; |
---|
297 | } |
---|
298 | } else { |
---|
299 | return loadSystemFile(filename, |
---|
300 | Symbol.LOAD_VERBOSE.symbolValue(thread) != NIL, |
---|
301 | Symbol.LOAD_PRINT.symbolValue(thread) != NIL, |
---|
302 | auto); |
---|
303 | } |
---|
304 | } |
---|
305 | |
---|
306 | public static final LispObject loadSystemFile(final String filename, |
---|
307 | boolean verbose, |
---|
308 | boolean print, |
---|
309 | boolean auto) |
---|
310 | |
---|
311 | { |
---|
312 | final int ARRAY_SIZE = 2; |
---|
313 | String[] candidates = new String[ARRAY_SIZE]; |
---|
314 | final String extension = getExtension(filename); |
---|
315 | if (extension == null) { |
---|
316 | // No extension specified. |
---|
317 | candidates[0] = filename + '.' + COMPILE_FILE_TYPE; |
---|
318 | candidates[1] = filename.concat(".lisp"); |
---|
319 | } else if (extension.equals(".abcl")) { |
---|
320 | candidates[0] = filename; |
---|
321 | candidates[1] = |
---|
322 | filename.substring(0, filename.length() - 5).concat(".lisp"); |
---|
323 | } else |
---|
324 | candidates[0] = filename; |
---|
325 | InputStream in = null; |
---|
326 | Pathname pathname = null; |
---|
327 | String truename = null; |
---|
328 | for (int i = 0; i < ARRAY_SIZE; i++) { |
---|
329 | String s = candidates[i]; |
---|
330 | if (s == null) |
---|
331 | break; |
---|
332 | ZipFile zipfile = null; |
---|
333 | final String dir = Site.getLispHome(); |
---|
334 | try { |
---|
335 | if (dir != null) { |
---|
336 | File file = new File(dir, s); |
---|
337 | if (file.isFile()) { |
---|
338 | // File exists. For system files, we know the extension |
---|
339 | // will be .abcl if it is a compiled file. |
---|
340 | String ext = getExtension(s); |
---|
341 | if (ext.equalsIgnoreCase(".abcl")) { |
---|
342 | try { |
---|
343 | zipfile = ZipCache.getZip(file.getPath()); |
---|
344 | String name = file.getName(); |
---|
345 | int index = name.lastIndexOf('.'); |
---|
346 | Debug.assertTrue(index >= 0); |
---|
347 | name = name.substring(0, index).concat("._"); |
---|
348 | ZipEntry entry = zipfile.getEntry(name); |
---|
349 | if (entry != null) { |
---|
350 | in = zipfile.getInputStream(entry); |
---|
351 | truename = file.getCanonicalPath(); |
---|
352 | } |
---|
353 | } |
---|
354 | catch (ZipException e) { |
---|
355 | // Fall through. |
---|
356 | } |
---|
357 | catch (Throwable t) { |
---|
358 | Debug.trace(t); |
---|
359 | in = null; |
---|
360 | // Fall through. |
---|
361 | } |
---|
362 | } |
---|
363 | if (in == null) { |
---|
364 | try { |
---|
365 | in = new FileInputStream(file); |
---|
366 | truename = file.getCanonicalPath(); |
---|
367 | } |
---|
368 | catch (IOException e) { |
---|
369 | in = null; |
---|
370 | } |
---|
371 | } |
---|
372 | } |
---|
373 | } else { |
---|
374 | URL url = Lisp.class.getResource(s); |
---|
375 | if (url != null) { |
---|
376 | try { |
---|
377 | in = url.openStream(); |
---|
378 | if ("jar".equals(url.getProtocol())) |
---|
379 | pathname = new Pathname(url); |
---|
380 | truename = getPath(url); |
---|
381 | } |
---|
382 | catch (IOException e) { |
---|
383 | in = null; |
---|
384 | } |
---|
385 | } |
---|
386 | } |
---|
387 | if (in != null) { |
---|
388 | final LispThread thread = LispThread.currentThread(); |
---|
389 | final SpecialBinding lastSpecialBinding = thread.lastSpecialBinding; |
---|
390 | thread.bindSpecial(_WARN_ON_REDEFINITION_, NIL); |
---|
391 | try { |
---|
392 | return loadFileFromStream(pathname, truename, |
---|
393 | new Stream(in, Symbol.CHARACTER), |
---|
394 | verbose, print, auto); |
---|
395 | } |
---|
396 | catch (FaslVersionMismatch e) { |
---|
397 | FastStringBuffer sb = |
---|
398 | new FastStringBuffer("; Incorrect fasl version: "); |
---|
399 | sb.append(truename); |
---|
400 | System.err.println(sb.toString()); |
---|
401 | } |
---|
402 | finally { |
---|
403 | thread.lastSpecialBinding = lastSpecialBinding; |
---|
404 | try { |
---|
405 | in.close(); |
---|
406 | } |
---|
407 | catch (IOException e) { |
---|
408 | return error(new LispError(e.getMessage())); |
---|
409 | } |
---|
410 | } |
---|
411 | } |
---|
412 | } |
---|
413 | finally { |
---|
414 | if (zipfile != null) { |
---|
415 | try { |
---|
416 | ZipCache.removeZip(zipfile.getName()); |
---|
417 | } |
---|
418 | catch (IOException e) { |
---|
419 | return error(new LispError(e.getMessage())); |
---|
420 | } |
---|
421 | } |
---|
422 | } |
---|
423 | } |
---|
424 | return error(new LispError("File not found: " + filename)); |
---|
425 | } |
---|
426 | |
---|
427 | // ### *fasl-version* |
---|
428 | // internal symbol |
---|
429 | private static final Symbol _FASL_VERSION_ = |
---|
430 | exportConstant("*FASL-VERSION*", PACKAGE_SYS, Fixnum.getInstance(33)); |
---|
431 | |
---|
432 | // ### *fasl-anonymous-package* |
---|
433 | // internal symbol |
---|
434 | /** |
---|
435 | * This variable gets bound to a package with no name in which the |
---|
436 | * reader can intern its uninterned symbols. |
---|
437 | * |
---|
438 | */ |
---|
439 | public static final Symbol _FASL_ANONYMOUS_PACKAGE_ = |
---|
440 | internSpecial("*FASL-ANONYMOUS-PACKAGE*", PACKAGE_SYS, NIL); |
---|
441 | |
---|
442 | // ### init-fasl |
---|
443 | private static final Primitive INIT_FASL = |
---|
444 | new Primitive("init-fasl", PACKAGE_SYS, true, "&key version") |
---|
445 | { |
---|
446 | @Override |
---|
447 | public LispObject execute(LispObject first, LispObject second) |
---|
448 | |
---|
449 | { |
---|
450 | if (first == Keyword.VERSION) { |
---|
451 | if (second.eql(_FASL_VERSION_.getSymbolValue())) { |
---|
452 | // OK |
---|
453 | final LispThread thread = LispThread.currentThread(); |
---|
454 | thread.bindSpecial(_FASL_ANONYMOUS_PACKAGE_, NIL); |
---|
455 | thread.bindSpecial(_SOURCE_, NIL); |
---|
456 | return faslLoadStream(thread); |
---|
457 | } |
---|
458 | } |
---|
459 | throw new FaslVersionMismatch(second); |
---|
460 | } |
---|
461 | }; |
---|
462 | |
---|
463 | private static final LispObject loadFileFromStream(LispObject pathname, |
---|
464 | String truename, |
---|
465 | Stream in, |
---|
466 | boolean verbose, |
---|
467 | boolean print, |
---|
468 | boolean auto) |
---|
469 | { |
---|
470 | return loadFileFromStream(pathname, truename, in, verbose, print, auto, false); |
---|
471 | } |
---|
472 | |
---|
473 | private static final LispObject loadFileFromStream(LispObject pathname, |
---|
474 | String truename, |
---|
475 | Stream in, |
---|
476 | boolean verbose, |
---|
477 | boolean print, |
---|
478 | boolean auto, |
---|
479 | boolean returnLastResult) |
---|
480 | |
---|
481 | { |
---|
482 | long start = System.currentTimeMillis(); |
---|
483 | final LispThread thread = LispThread.currentThread(); |
---|
484 | final SpecialBinding lastSpecialBinding = thread.lastSpecialBinding; |
---|
485 | // "LOAD binds *READTABLE* and *PACKAGE* to the values they held before |
---|
486 | // loading the file." |
---|
487 | thread.bindSpecialToCurrentValue(Symbol.CURRENT_READTABLE); |
---|
488 | thread.bindSpecialToCurrentValue(Symbol._PACKAGE_); |
---|
489 | int loadDepth = Fixnum.getValue(_LOAD_DEPTH_.symbolValue(thread)); |
---|
490 | thread.bindSpecial(_LOAD_DEPTH_, Fixnum.getInstance(++loadDepth)); |
---|
491 | // Compiler policy. |
---|
492 | thread.bindSpecialToCurrentValue(_SPEED_); |
---|
493 | thread.bindSpecialToCurrentValue(_SPACE_); |
---|
494 | thread.bindSpecialToCurrentValue(_SAFETY_); |
---|
495 | thread.bindSpecialToCurrentValue(_DEBUG_); |
---|
496 | thread.bindSpecialToCurrentValue(_EXPLAIN_); |
---|
497 | final String prefix = getLoadVerbosePrefix(loadDepth); |
---|
498 | try { |
---|
499 | if (pathname == null && truename != null) |
---|
500 | pathname = Pathname.parseNamestring(truename); |
---|
501 | thread.bindSpecial(Symbol.LOAD_PATHNAME, |
---|
502 | pathname != null ? pathname : NIL); |
---|
503 | thread.bindSpecial(Symbol.LOAD_TRUENAME, |
---|
504 | pathname != null ? pathname : NIL); |
---|
505 | thread.bindSpecial(_SOURCE_, |
---|
506 | pathname != null ? pathname : NIL); |
---|
507 | if (verbose) { |
---|
508 | Stream out = getStandardOutput(); |
---|
509 | out.freshLine(); |
---|
510 | out._writeString(prefix); |
---|
511 | out._writeString(auto ? " Autoloading " : " Loading "); |
---|
512 | out._writeString(truename != null ? truename : "stream"); |
---|
513 | out._writeLine(" ..."); |
---|
514 | out._finishOutput(); |
---|
515 | LispObject result = loadStream(in, print, thread, returnLastResult); |
---|
516 | long elapsed = System.currentTimeMillis() - start; |
---|
517 | out.freshLine(); |
---|
518 | out._writeString(prefix); |
---|
519 | out._writeString(auto ? " Autoloaded " : " Loaded "); |
---|
520 | out._writeString(truename != null ? truename : "stream"); |
---|
521 | out._writeString(" ("); |
---|
522 | out._writeString(String.valueOf(((float)elapsed)/1000)); |
---|
523 | out._writeLine(" seconds)"); |
---|
524 | out._finishOutput(); |
---|
525 | return result; |
---|
526 | } else |
---|
527 | return loadStream(in, print, thread, returnLastResult); |
---|
528 | } |
---|
529 | finally { |
---|
530 | thread.lastSpecialBinding = lastSpecialBinding; |
---|
531 | } |
---|
532 | } |
---|
533 | |
---|
534 | public static String getLoadVerbosePrefix(int loadDepth) |
---|
535 | { |
---|
536 | FastStringBuffer sb = new FastStringBuffer(";"); |
---|
537 | for (int i = loadDepth - 1; i-- > 0;) |
---|
538 | sb.append(' '); |
---|
539 | return sb.toString(); |
---|
540 | } |
---|
541 | |
---|
542 | private static final LispObject loadStream(Stream in, boolean print, |
---|
543 | LispThread thread) |
---|
544 | { |
---|
545 | return loadStream(in, print, thread, false); |
---|
546 | } |
---|
547 | |
---|
548 | private static final LispObject loadStream(Stream in, boolean print, |
---|
549 | LispThread thread, boolean returnLastResult) |
---|
550 | |
---|
551 | { |
---|
552 | SpecialBinding lastSpecialBinding = thread.lastSpecialBinding; |
---|
553 | thread.bindSpecial(_LOAD_STREAM_, in); |
---|
554 | SpecialBinding sourcePositionBinding = |
---|
555 | new SpecialBinding(_SOURCE_POSITION_, Fixnum.ZERO, |
---|
556 | thread.lastSpecialBinding); |
---|
557 | thread.lastSpecialBinding = sourcePositionBinding; |
---|
558 | try { |
---|
559 | final Environment env = new Environment(); |
---|
560 | LispObject result = NIL; |
---|
561 | while (true) { |
---|
562 | sourcePositionBinding.value = Fixnum.getInstance(in.getOffset()); |
---|
563 | LispObject obj = in.read(false, EOF, false, thread); |
---|
564 | if (obj == EOF) |
---|
565 | break; |
---|
566 | result = eval(obj, env, thread); |
---|
567 | if (print) { |
---|
568 | Stream out = |
---|
569 | checkCharacterOutputStream(Symbol.STANDARD_OUTPUT.symbolValue(thread)); |
---|
570 | out._writeLine(result.writeToString()); |
---|
571 | out._finishOutput(); |
---|
572 | } |
---|
573 | } |
---|
574 | if(returnLastResult) { |
---|
575 | return result; |
---|
576 | } else { |
---|
577 | return T; |
---|
578 | } |
---|
579 | } |
---|
580 | finally { |
---|
581 | thread.lastSpecialBinding = lastSpecialBinding; |
---|
582 | } |
---|
583 | } |
---|
584 | |
---|
585 | private static final LispObject faslLoadStream(LispThread thread) |
---|
586 | |
---|
587 | { |
---|
588 | Stream in = (Stream) _LOAD_STREAM_.symbolValue(thread); |
---|
589 | final Environment env = new Environment(); |
---|
590 | final SpecialBinding lastSpecialBinding = thread.lastSpecialBinding; |
---|
591 | LispObject result = NIL; |
---|
592 | try { |
---|
593 | thread.bindSpecial(_FASL_ANONYMOUS_PACKAGE_, new Package()); |
---|
594 | while (true) { |
---|
595 | LispObject obj = in.faslRead(false, EOF, true, thread); |
---|
596 | if (obj == EOF) |
---|
597 | break; |
---|
598 | result = eval(obj, env, thread); |
---|
599 | } |
---|
600 | } |
---|
601 | finally { |
---|
602 | thread.lastSpecialBinding = lastSpecialBinding; |
---|
603 | } |
---|
604 | return result; |
---|
605 | //There's no point in using here the returnLastResult flag like in |
---|
606 | //loadStream(): this function is only called from init-fasl, which is |
---|
607 | //only called from load, which already has its own policy for choosing |
---|
608 | //whether to return T or the last value. |
---|
609 | } |
---|
610 | |
---|
611 | // Returns extension including leading '.' |
---|
612 | private static final String getExtension(String filename) |
---|
613 | { |
---|
614 | int index = filename.lastIndexOf('.'); |
---|
615 | if (index < 0) |
---|
616 | return null; |
---|
617 | if (index < filename.lastIndexOf(File.separatorChar)) |
---|
618 | return null; // Last dot was in path part of filename. |
---|
619 | return filename.substring(index); |
---|
620 | } |
---|
621 | |
---|
622 | private static final String getPath(URL url) |
---|
623 | { |
---|
624 | if (url != null) { |
---|
625 | String path; |
---|
626 | try { |
---|
627 | path = URLDecoder.decode(url.getPath(),"UTF-8"); |
---|
628 | } |
---|
629 | catch (java.io.UnsupportedEncodingException uee) { |
---|
630 | // Can't happen: every Java is supposed to support |
---|
631 | // at least UTF-8 encoding |
---|
632 | path = null; |
---|
633 | } |
---|
634 | if (path != null) { |
---|
635 | if (Utilities.isPlatformWindows) { |
---|
636 | if (path.length() > 0 && path.charAt(0) == '/') |
---|
637 | path = path.substring(1); |
---|
638 | } |
---|
639 | return path; |
---|
640 | } |
---|
641 | } |
---|
642 | return null; |
---|
643 | } |
---|
644 | |
---|
645 | private static final boolean checkZipFile(File file) |
---|
646 | { |
---|
647 | InputStream in = null; |
---|
648 | try { |
---|
649 | in = new FileInputStream(file); |
---|
650 | byte[] bytes = new byte[4]; |
---|
651 | int bytesRead = in.read(bytes); |
---|
652 | return (bytesRead == 4 |
---|
653 | && bytes[0] == 0x50 |
---|
654 | && bytes[1] == 0x4b |
---|
655 | && bytes[2] == 0x03 |
---|
656 | && bytes[3] == 0x04); |
---|
657 | } |
---|
658 | catch (Throwable t) { |
---|
659 | return false; |
---|
660 | } |
---|
661 | finally { |
---|
662 | if (in != null) { |
---|
663 | try { |
---|
664 | in.close(); |
---|
665 | } |
---|
666 | catch (Throwable t) {} |
---|
667 | } |
---|
668 | } |
---|
669 | } |
---|
670 | |
---|
671 | // ### %load filespec verbose print if-does-not-exist => generalized-boolean |
---|
672 | private static final Primitive _LOAD = |
---|
673 | new Primitive("%load", PACKAGE_SYS, false, |
---|
674 | "filespec verbose print if-does-not-exist") |
---|
675 | { |
---|
676 | @Override |
---|
677 | public LispObject execute(LispObject filespec, LispObject verbose, |
---|
678 | LispObject print, LispObject ifDoesNotExist) |
---|
679 | { |
---|
680 | return load(filespec, verbose, print, ifDoesNotExist, NIL); |
---|
681 | } |
---|
682 | }; |
---|
683 | |
---|
684 | // ### %load-returning-last-result filespec verbose print if-does-not-exist => object |
---|
685 | private static final Primitive _LOAD_RETURNING_LAST_RESULT = |
---|
686 | new Primitive("%load-returning-last-result", PACKAGE_SYS, false, |
---|
687 | "filespec verbose print if-does-not-exist") |
---|
688 | { |
---|
689 | @Override |
---|
690 | public LispObject execute(LispObject filespec, LispObject verbose, |
---|
691 | LispObject print, LispObject ifDoesNotExist) |
---|
692 | { |
---|
693 | return load(filespec, verbose, print, ifDoesNotExist, T); |
---|
694 | } |
---|
695 | }; |
---|
696 | |
---|
697 | private static final LispObject load(LispObject filespec, |
---|
698 | LispObject verbose, |
---|
699 | LispObject print, |
---|
700 | LispObject ifDoesNotExist, |
---|
701 | LispObject returnLastResult) |
---|
702 | { |
---|
703 | if (filespec instanceof Stream) { |
---|
704 | if (((Stream)filespec).isOpen()) { |
---|
705 | LispObject pathname; |
---|
706 | if (filespec instanceof FileStream) |
---|
707 | pathname = ((FileStream)filespec).getPathname(); |
---|
708 | else |
---|
709 | pathname = NIL; |
---|
710 | String truename; |
---|
711 | if (pathname instanceof Pathname) |
---|
712 | truename = ((Pathname)pathname).getNamestring(); |
---|
713 | else |
---|
714 | truename = null; |
---|
715 | return loadFileFromStream(pathname, |
---|
716 | truename, |
---|
717 | (Stream) filespec, |
---|
718 | verbose != NIL, |
---|
719 | print != NIL, |
---|
720 | false, |
---|
721 | returnLastResult != NIL); |
---|
722 | } |
---|
723 | // If stream is closed, fall through... |
---|
724 | } |
---|
725 | Pathname pathname = coerceToPathname(filespec); |
---|
726 | if (pathname instanceof LogicalPathname) |
---|
727 | pathname = LogicalPathname.translateLogicalPathname((LogicalPathname)pathname); |
---|
728 | return load(pathname, |
---|
729 | pathname.getNamestring(), |
---|
730 | verbose != NIL, |
---|
731 | print != NIL, |
---|
732 | ifDoesNotExist != NIL, |
---|
733 | returnLastResult != NIL); |
---|
734 | } |
---|
735 | |
---|
736 | // ### load-system-file |
---|
737 | private static final Primitive LOAD_SYSTEM_FILE = |
---|
738 | new Primitive("load-system-file", PACKAGE_SYS, true) |
---|
739 | { |
---|
740 | @Override |
---|
741 | public LispObject execute(LispObject arg) |
---|
742 | { |
---|
743 | final LispThread thread = LispThread.currentThread(); |
---|
744 | return loadSystemFile(arg.getStringValue(), |
---|
745 | Symbol.LOAD_VERBOSE.symbolValue(thread) != NIL, |
---|
746 | Symbol.LOAD_PRINT.symbolValue(thread) != NIL, |
---|
747 | false); |
---|
748 | } |
---|
749 | }; |
---|
750 | |
---|
751 | private static class FaslVersionMismatch extends Error |
---|
752 | { |
---|
753 | private final LispObject version; |
---|
754 | |
---|
755 | public FaslVersionMismatch(LispObject version) |
---|
756 | { |
---|
757 | this.version = version; |
---|
758 | } |
---|
759 | |
---|
760 | public LispObject getVersion() |
---|
761 | { |
---|
762 | return version; |
---|
763 | } |
---|
764 | } |
---|
765 | } |
---|