Commit | Line | Data |
---|---|---|
c12da2be | 1 | /* Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2004, 2006, 2008, |
f6fd2c03 | 2 | * 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc. |
0f2d19dd | 3 | * |
73be1d9e | 4 | * This library is free software; you can redistribute it and/or |
53befeb7 NJ |
5 | * modify it under the terms of the GNU Lesser General Public License |
6 | * as published by the Free Software Foundation; either version 3 of | |
7 | * the License, or (at your option) any later version. | |
0f2d19dd | 8 | * |
53befeb7 NJ |
9 | * This library is distributed in the hope that it will be useful, but |
10 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
73be1d9e MV |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | * Lesser General Public License for more details. | |
0f2d19dd | 13 | * |
73be1d9e MV |
14 | * You should have received a copy of the GNU Lesser General Public |
15 | * License along with this library; if not, write to the Free Software | |
53befeb7 NJ |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
17 | * 02110-1301 USA | |
73be1d9e | 18 | */ |
1bbd0b84 | 19 | |
1bbd0b84 | 20 | |
0f2d19dd JB |
21 | \f |
22 | ||
dbb605f5 | 23 | #ifdef HAVE_CONFIG_H |
cbd41c89 RB |
24 | # include <config.h> |
25 | #endif | |
26 | ||
13070bd3 | 27 | #include <string.h> |
7d04d68b | 28 | #include <stdio.h> |
13070bd3 | 29 | |
a0599745 | 30 | #include "libguile/_scm.h" |
655aadf4 | 31 | #include "libguile/private-gc.h" /* scm_getenv_int */ |
a0599745 MD |
32 | #include "libguile/libpath.h" |
33 | #include "libguile/fports.h" | |
34 | #include "libguile/read.h" | |
35 | #include "libguile/eval.h" | |
36 | #include "libguile/throw.h" | |
37 | #include "libguile/alist.h" | |
38 | #include "libguile/dynwind.h" | |
39 | #include "libguile/root.h" | |
40 | #include "libguile/strings.h" | |
f33b174d | 41 | #include "libguile/modules.h" |
7d04d68b | 42 | #include "libguile/chars.h" |
c44ca4fe | 43 | #include "libguile/srfi-13.h" |
a0599745 MD |
44 | |
45 | #include "libguile/validate.h" | |
46 | #include "libguile/load.h" | |
ec3a8ace | 47 | #include "libguile/fluids.h" |
06721500 | 48 | |
22f4ee48 AW |
49 | #include "libguile/vm.h" /* for load-compiled/vm */ |
50 | ||
06721500 JB |
51 | #include <sys/types.h> |
52 | #include <sys/stat.h> | |
53 | ||
54 | #ifdef HAVE_UNISTD_H | |
55 | #include <unistd.h> | |
56 | #endif /* HAVE_UNISTD_H */ | |
57 | ||
5b197db8 AW |
58 | #ifdef HAVE_PWD_H |
59 | #include <pwd.h> | |
60 | #endif /* HAVE_PWD_H */ | |
61 | ||
06721500 JB |
62 | #ifndef R_OK |
63 | #define R_OK 4 | |
64 | #endif | |
0f2d19dd | 65 | |
abca59fe LC |
66 | #include <stat-time.h> |
67 | ||
0f2d19dd | 68 | \f |
06721500 | 69 | /* Loading a file, given an absolute filename. */ |
0f2d19dd | 70 | |
26544b96 JB |
71 | /* Hook to run when we load a file, perhaps to announce the fact somewhere. |
72 | Applied to the full name of the file. */ | |
73 | static SCM *scm_loc_load_hook; | |
74 | ||
ec3a8ace NJ |
75 | /* The current reader (a fluid). */ |
76 | static SCM the_reader = SCM_BOOL_F; | |
8b039053 | 77 | |
ec3a8ace | 78 | |
3b3b36dd | 79 | SCM_DEFINE (scm_primitive_load, "primitive-load", 1, 0, 0, |
1bbd0b84 | 80 | (SCM filename), |
67e8151b MG |
81 | "Load the file named @var{filename} and evaluate its contents in\n" |
82 | "the top-level environment. The load paths are not searched;\n" | |
83 | "@var{filename} must either be a full pathname or be a pathname\n" | |
84 | "relative to the current directory. If the variable\n" | |
85 | "@code{%load-hook} is defined, it should be bound to a procedure\n" | |
86 | "that will be called before any code is loaded. See the\n" | |
87 | "documentation for @code{%load-hook} later in this section.") | |
1bbd0b84 | 88 | #define FUNC_NAME s_scm_primitive_load |
0f2d19dd | 89 | { |
26544b96 | 90 | SCM hook = *scm_loc_load_hook; |
017eb4a6 | 91 | SCM ret = SCM_UNSPECIFIED; |
017eb4a6 | 92 | |
a6d9e5ab | 93 | SCM_VALIDATE_STRING (1, filename); |
bc36d050 | 94 | if (scm_is_true (hook) && scm_is_false (scm_procedure_p (hook))) |
2ade72d7 DH |
95 | SCM_MISC_ERROR ("value of %load-hook is neither a procedure nor #f", |
96 | SCM_EOL); | |
26544b96 | 97 | |
bc36d050 | 98 | if (!scm_is_false (hook)) |
fdc28395 | 99 | scm_call_1 (hook, filename); |
26544b96 | 100 | |
017eb4a6 AW |
101 | { |
102 | SCM port; | |
103 | ||
4101d14f MW |
104 | port = scm_open_file_with_encoding (filename, |
105 | scm_from_latin1_string ("r"), | |
106 | SCM_BOOL_T, /* guess_encoding */ | |
107 | scm_from_latin1_string ("UTF-8")); | |
108 | ||
661ae7ab MV |
109 | scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE); |
110 | scm_i_dynwind_current_load_port (port); | |
f8a1c9a8 | 111 | |
b5623573 MV |
112 | while (1) |
113 | { | |
ec3a8ace NJ |
114 | SCM reader, form; |
115 | ||
116 | /* Lookup and use the current reader to read the next | |
117 | expression. */ | |
8b039053 | 118 | reader = scm_fluid_ref (the_reader); |
393baa8a | 119 | if (scm_is_false (reader)) |
ec3a8ace NJ |
120 | form = scm_read (port); |
121 | else | |
122 | form = scm_call_1 (reader, port); | |
123 | ||
b5623573 MV |
124 | if (SCM_EOF_OBJECT_P (form)) |
125 | break; | |
ec3a8ace | 126 | |
017eb4a6 | 127 | ret = scm_primitive_eval_x (form); |
b5623573 MV |
128 | } |
129 | ||
661ae7ab | 130 | scm_dynwind_end (); |
0f2d19dd JB |
131 | scm_close_port (port); |
132 | } | |
017eb4a6 | 133 | return ret; |
0f2d19dd | 134 | } |
1bbd0b84 | 135 | #undef FUNC_NAME |
0f2d19dd | 136 | |
c519b272 MV |
137 | SCM |
138 | scm_c_primitive_load (const char *filename) | |
139 | { | |
cc95e00a | 140 | return scm_primitive_load (scm_from_locale_string (filename)); |
c519b272 MV |
141 | } |
142 | ||
0f2d19dd | 143 | \f |
3feedb00 MD |
144 | /* Builtin path to scheme library files. */ |
145 | #ifdef SCM_PKGDATA_DIR | |
a1ec6916 | 146 | SCM_DEFINE (scm_sys_package_data_dir, "%package-data-dir", 0, 0, 0, |
1bbd0b84 | 147 | (), |
b380b885 MD |
148 | "Return the name of the directory where Scheme packages, modules and\n" |
149 | "libraries are kept. On most Unix systems, this will be\n" | |
150 | "@samp{/usr/local/share/guile}.") | |
1bbd0b84 | 151 | #define FUNC_NAME s_scm_sys_package_data_dir |
3feedb00 | 152 | { |
cc95e00a | 153 | return scm_from_locale_string (SCM_PKGDATA_DIR); |
3feedb00 | 154 | } |
1bbd0b84 | 155 | #undef FUNC_NAME |
3feedb00 MD |
156 | #endif /* SCM_PKGDATA_DIR */ |
157 | ||
e8e9b690 | 158 | #ifdef SCM_LIBRARY_DIR |
a1ec6916 | 159 | SCM_DEFINE (scm_sys_library_dir, "%library-dir", 0,0,0, |
e8e9b690 | 160 | (), |
b380b885 MD |
161 | "Return the directory where the Guile Scheme library files are installed.\n" |
162 | "E.g., may return \"/usr/share/guile/1.3.5\".") | |
e8e9b690 GB |
163 | #define FUNC_NAME s_scm_sys_library_dir |
164 | { | |
cc95e00a | 165 | return scm_from_locale_string (SCM_LIBRARY_DIR); |
e8e9b690 GB |
166 | } |
167 | #undef FUNC_NAME | |
168 | #endif /* SCM_LIBRARY_DIR */ | |
169 | ||
170 | #ifdef SCM_SITE_DIR | |
a1ec6916 | 171 | SCM_DEFINE (scm_sys_site_dir, "%site-dir", 0,0,0, |
e8e9b690 | 172 | (), |
bc325e76 AW |
173 | "Return the directory where users should install Scheme code for use\n" |
174 | "with this version of Guile.\n\n" | |
175 | "E.g., may return \"/usr/share/guile/site/" SCM_EFFECTIVE_VERSION "\".") | |
e8e9b690 GB |
176 | #define FUNC_NAME s_scm_sys_site_dir |
177 | { | |
cc95e00a | 178 | return scm_from_locale_string (SCM_SITE_DIR); |
e8e9b690 GB |
179 | } |
180 | #undef FUNC_NAME | |
181 | #endif /* SCM_SITE_DIR */ | |
182 | ||
bc325e76 AW |
183 | #ifdef SCM_GLOBAL_SITE_DIR |
184 | SCM_DEFINE (scm_sys_global_site_dir, "%global-site-dir", 0,0,0, | |
185 | (), | |
186 | "Return the directory where users should install Scheme code for use\n" | |
187 | "with all versions of Guile.\n\n" | |
188 | "E.g., may return \"/usr/share/guile/site\".") | |
189 | #define FUNC_NAME s_scm_sys_global_site_dir | |
190 | { | |
191 | return scm_from_locale_string (SCM_GLOBAL_SITE_DIR); | |
192 | } | |
193 | #undef FUNC_NAME | |
194 | #endif /* SCM_GLOBAL_SITE_DIR */ | |
e8e9b690 | 195 | |
988ca6b2 JE |
196 | #ifdef SCM_SITE_CCACHE_DIR |
197 | SCM_DEFINE (scm_sys_site_ccache_dir, "%site-ccache-dir", 0,0,0, | |
198 | (), | |
199 | "Return the directory where users should install compiled\n" | |
200 | "@code{.go} files for use with this version of Guile.\n\n" | |
201 | "E.g., may return \"/usr/lib/guile/" SCM_EFFECTIVE_VERSION "/site-ccache\".") | |
202 | #define FUNC_NAME s_scm_sys_site_ccache_dir | |
203 | { | |
204 | return scm_from_locale_string (SCM_SITE_CCACHE_DIR); | |
205 | } | |
206 | #undef FUNC_NAME | |
207 | #endif /* SCM_SITE_CCACHE_DIR */ | |
208 | ||
e8e9b690 | 209 | |
3feedb00 | 210 | \f |
06721500 | 211 | /* Initializing the load path, and searching it. */ |
0f2d19dd | 212 | |
26544b96 | 213 | /* List of names of directories we search for files to load. */ |
06721500 JB |
214 | static SCM *scm_loc_load_path; |
215 | ||
26544b96 JB |
216 | /* List of extensions we try adding to the filenames. */ |
217 | static SCM *scm_loc_load_extensions; | |
218 | ||
5b197db8 AW |
219 | /* Like %load-path and %load-extensions, but for compiled files. */ |
220 | static SCM *scm_loc_load_compiled_path; | |
22f4ee48 AW |
221 | static SCM *scm_loc_load_compiled_extensions; |
222 | ||
ee001750 | 223 | /* Whether we should try to auto-compile. */ |
6f06e8d3 | 224 | static SCM *scm_loc_load_should_auto_compile; |
01cddfc1 | 225 | |
1e56cff2 AW |
226 | /* Whether to treat all auto-compiled files as stale. */ |
227 | static SCM *scm_loc_fresh_auto_compile; | |
228 | ||
6f06e8d3 | 229 | /* The fallback path for auto-compilation */ |
5ea401bf | 230 | static SCM *scm_loc_compile_fallback_path; |
01cddfc1 | 231 | |
bd31bce6 MW |
232 | /* Ellipsis: "..." */ |
233 | static SCM scm_ellipsis; | |
234 | ||
a1ec6916 | 235 | SCM_DEFINE (scm_parse_path, "parse-path", 1, 1, 0, |
1bbd0b84 | 236 | (SCM path, SCM tail), |
d91788cb MG |
237 | "Parse @var{path}, which is expected to be a colon-separated\n" |
238 | "string, into a list and return the resulting list with\n" | |
239 | "@var{tail} appended. If @var{path} is @code{#f}, @var{tail}\n" | |
240 | "is returned.") | |
1bbd0b84 | 241 | #define FUNC_NAME s_scm_parse_path |
04e8fb0a | 242 | { |
7d04d68b MV |
243 | #ifdef __MINGW32__ |
244 | SCM sep = SCM_MAKE_CHAR (';'); | |
245 | #else | |
246 | SCM sep = SCM_MAKE_CHAR (':'); | |
247 | #endif | |
248 | ||
04e8fb0a MD |
249 | if (SCM_UNBNDP (tail)) |
250 | tail = SCM_EOL; | |
7888309b | 251 | return (scm_is_false (path) |
04e8fb0a | 252 | ? tail |
7d04d68b | 253 | : scm_append_x (scm_list_2 (scm_string_split (path, sep), tail))); |
04e8fb0a | 254 | } |
1bbd0b84 | 255 | #undef FUNC_NAME |
04e8fb0a | 256 | |
bd31bce6 MW |
257 | SCM_DEFINE (scm_parse_path_with_ellipsis, "parse-path-with-ellipsis", 2, 0, 0, |
258 | (SCM path, SCM base), | |
259 | "Parse @var{path}, which is expected to be a colon-separated\n" | |
260 | "string, into a list and return the resulting list with\n" | |
261 | "@var{base} (a list) spliced in place of the @code{...} path\n" | |
262 | "component, if present, or else @var{base} is added to the end.\n" | |
263 | "If @var{path} is @code{#f}, @var{base} is returned.") | |
264 | #define FUNC_NAME s_scm_parse_path_with_ellipsis | |
265 | { | |
266 | SCM lst = scm_parse_path (path, SCM_EOL); | |
267 | SCM walk = lst; | |
268 | SCM *prev = &lst; | |
269 | ||
270 | while (!scm_is_null (walk) && | |
271 | scm_is_false (scm_equal_p (scm_car (walk), scm_ellipsis))) | |
272 | { | |
273 | prev = SCM_CDRLOC (walk); | |
274 | walk = *prev; | |
275 | } | |
276 | *prev = scm_is_null (walk) | |
277 | ? base | |
278 | : scm_append (scm_list_2 (base, scm_cdr (walk))); | |
279 | return lst; | |
280 | } | |
281 | #undef FUNC_NAME | |
282 | ||
04e8fb0a | 283 | |
06721500 | 284 | /* Initialize the global variable %load-path, given the value of the |
3feedb00 | 285 | SCM_SITE_DIR and SCM_LIBRARY_DIR preprocessor symbols and the |
01cddfc1 | 286 | GUILE_LOAD_PATH environment variable. */ |
0f2d19dd | 287 | void |
06721500 JB |
288 | scm_init_load_path () |
289 | { | |
11c5e0bf | 290 | char *env; |
06721500 | 291 | SCM path = SCM_EOL; |
5b197db8 | 292 | SCM cpath = SCM_EOL; |
06721500 | 293 | |
3feedb00 | 294 | #ifdef SCM_LIBRARY_DIR |
02b84691 AW |
295 | env = getenv ("GUILE_SYSTEM_PATH"); |
296 | if (env && strcmp (env, "") == 0) | |
297 | /* special-case interpret system-path=="" as meaning no system path instead | |
298 | of '("") */ | |
299 | ; | |
300 | else if (env) | |
301 | path = scm_parse_path (scm_from_locale_string (env), path); | |
302 | else | |
bc325e76 | 303 | path = scm_list_4 (scm_from_locale_string (SCM_LIBRARY_DIR), |
3c98a49c | 304 | scm_from_locale_string (SCM_SITE_DIR), |
bc325e76 | 305 | scm_from_locale_string (SCM_GLOBAL_SITE_DIR), |
02b84691 | 306 | scm_from_locale_string (SCM_PKGDATA_DIR)); |
5b197db8 AW |
307 | |
308 | env = getenv ("GUILE_SYSTEM_COMPILED_PATH"); | |
309 | if (env && strcmp (env, "") == 0) | |
310 | /* like above */ | |
311 | ; | |
312 | else if (env) | |
313 | cpath = scm_parse_path (scm_from_locale_string (env), cpath); | |
314 | else | |
9957641b AW |
315 | { |
316 | cpath = scm_list_2 (scm_from_locale_string (SCM_CCACHE_DIR), | |
317 | scm_from_locale_string (SCM_SITE_CCACHE_DIR)); | |
318 | } | |
3c997c4b | 319 | |
3feedb00 | 320 | #endif /* SCM_LIBRARY_DIR */ |
06721500 | 321 | |
3c997c4b | 322 | { |
179fe336 AW |
323 | char cachedir[1024]; |
324 | char *e; | |
325 | #ifdef HAVE_GETPWENT | |
326 | struct passwd *pwd; | |
327 | #endif | |
5b197db8 | 328 | |
6e5c02b8 AW |
329 | #define FALLBACK_DIR \ |
330 | "guile/ccache/" SCM_EFFECTIVE_VERSION "-" SCM_OBJCODE_MACHINE_VERSION_STRING | |
179fe336 AW |
331 | |
332 | if ((e = getenv ("XDG_CACHE_HOME"))) | |
48a0fe4d | 333 | snprintf (cachedir, sizeof(cachedir), "%s/" FALLBACK_DIR, e); |
179fe336 AW |
334 | else if ((e = getenv ("HOME"))) |
335 | snprintf (cachedir, sizeof(cachedir), "%s/.cache/" FALLBACK_DIR, e); | |
5b197db8 | 336 | #ifdef HAVE_GETPWENT |
179fe336 AW |
337 | else if ((pwd = getpwuid (getuid ())) && pwd->pw_dir) |
338 | snprintf (cachedir, sizeof(cachedir), "%s/.cache/" FALLBACK_DIR, | |
339 | pwd->pw_dir); | |
5b197db8 | 340 | #endif /* HAVE_GETPWENT */ |
284019a2 JN |
341 | #ifdef __MINGW32__ |
342 | else if ((e = getenv ("LOCALAPPDATA"))) | |
343 | snprintf (cachedir, sizeof (cachedir), "%s/.cache/" FALLBACK_DIR, e); | |
344 | else if ((e = getenv ("APPDATA"))) | |
345 | snprintf (cachedir, sizeof (cachedir), "%s/.cache/" FALLBACK_DIR, e); | |
346 | #endif /* __MINGW32__ */ | |
179fe336 AW |
347 | else |
348 | cachedir[0] = 0; | |
349 | ||
350 | if (cachedir[0]) | |
351 | *scm_loc_compile_fallback_path = scm_from_locale_string (cachedir); | |
3c997c4b | 352 | } |
06721500 | 353 | |
11c5e0bf MV |
354 | env = getenv ("GUILE_LOAD_PATH"); |
355 | if (env) | |
bd31bce6 | 356 | path = scm_parse_path_with_ellipsis (scm_from_locale_string (env), path); |
01cddfc1 | 357 | |
5b197db8 AW |
358 | env = getenv ("GUILE_LOAD_COMPILED_PATH"); |
359 | if (env) | |
bd31bce6 | 360 | cpath = scm_parse_path_with_ellipsis (scm_from_locale_string (env), cpath); |
5b197db8 | 361 | |
06721500 | 362 | *scm_loc_load_path = path; |
5b197db8 | 363 | *scm_loc_load_compiled_path = cpath; |
06721500 JB |
364 | } |
365 | ||
04e8fb0a | 366 | SCM scm_listofnullstr; |
06721500 | 367 | |
7d04d68b MV |
368 | /* Utility functions for assembling C strings in a buffer. |
369 | */ | |
370 | ||
371 | struct stringbuf { | |
372 | char *buf, *ptr; | |
373 | size_t buf_len; | |
374 | }; | |
375 | ||
376 | static void | |
377 | stringbuf_free (void *data) | |
378 | { | |
379 | struct stringbuf *buf = (struct stringbuf *)data; | |
380 | free (buf->buf); | |
381 | } | |
382 | ||
383 | static void | |
384 | stringbuf_grow (struct stringbuf *buf) | |
385 | { | |
386 | size_t ptroff = buf->ptr - buf->buf; | |
387 | buf->buf_len *= 2; | |
7d04d68b MV |
388 | buf->buf = scm_realloc (buf->buf, buf->buf_len); |
389 | buf->ptr = buf->buf + ptroff; | |
390 | } | |
391 | ||
392 | static void | |
393 | stringbuf_cat_locale_string (struct stringbuf *buf, SCM str) | |
394 | { | |
395 | size_t max_len = buf->buf_len - (buf->ptr - buf->buf) - 1; | |
396 | size_t len = scm_to_locale_stringbuf (str, buf->ptr, max_len); | |
397 | if (len > max_len) | |
398 | { | |
399 | /* buffer is too small, double its size and try again. | |
400 | */ | |
401 | stringbuf_grow (buf); | |
402 | stringbuf_cat_locale_string (buf, str); | |
403 | } | |
404 | else | |
405 | { | |
406 | /* string fits, terminate it and check for embedded '\0'. | |
407 | */ | |
408 | buf->ptr[len] = '\0'; | |
409 | if (strlen (buf->ptr) != len) | |
410 | scm_misc_error (NULL, | |
411 | "string contains #\\nul character: ~S", | |
412 | scm_list_1 (str)); | |
413 | buf->ptr += len; | |
414 | } | |
415 | } | |
416 | ||
417 | static void | |
418 | stringbuf_cat (struct stringbuf *buf, char *str) | |
419 | { | |
420 | size_t max_len = buf->buf_len - (buf->ptr - buf->buf) - 1; | |
421 | size_t len = strlen (str); | |
422 | if (len > max_len) | |
423 | { | |
424 | /* buffer is too small, double its size and try again. | |
425 | */ | |
426 | stringbuf_grow (buf); | |
427 | stringbuf_cat (buf, str); | |
428 | } | |
429 | else | |
430 | { | |
431 | /* string fits, copy it into buffer. | |
432 | */ | |
433 | strcpy (buf->ptr, str); | |
434 | buf->ptr += len; | |
435 | } | |
436 | } | |
437 | ||
438 | ||
22f4ee48 AW |
439 | static int |
440 | scm_c_string_has_an_ext (char *str, size_t len, SCM extensions) | |
441 | { | |
442 | for (; !scm_is_null (extensions); extensions = SCM_CDR (extensions)) | |
443 | { | |
444 | char *ext; | |
445 | size_t extlen; | |
446 | int match; | |
447 | ext = scm_to_locale_string (SCM_CAR (extensions)); | |
448 | extlen = strlen (ext); | |
449 | match = (len > extlen && str[len - extlen - 1] == '.' | |
450 | && strncmp (str + (len - extlen), ext, extlen) == 0); | |
451 | free (ext); | |
452 | if (match) | |
453 | return 1; | |
454 | } | |
455 | return 0; | |
456 | } | |
457 | ||
4bab7f01 AW |
458 | #ifdef __MINGW32__ |
459 | #define FILE_NAME_SEPARATOR_STRING "\\" | |
460 | #else | |
461 | #define FILE_NAME_SEPARATOR_STRING "/" | |
462 | #endif | |
463 | ||
464 | static int | |
465 | is_file_name_separator (SCM c) | |
466 | { | |
e6a7a86d | 467 | if (scm_is_eq (c, SCM_MAKE_CHAR ('/'))) |
4bab7f01 AW |
468 | return 1; |
469 | #ifdef __MINGW32__ | |
e6a7a86d | 470 | if (scm_is_eq (c, SCM_MAKE_CHAR ('\\'))) |
4bab7f01 AW |
471 | return 1; |
472 | #endif | |
473 | return 0; | |
474 | } | |
475 | ||
476 | static int | |
477 | is_drive_letter (SCM c) | |
478 | { | |
479 | #ifdef __MINGW32__ | |
480 | if (SCM_CHAR (c) >= 'a' && SCM_CHAR (c) <= 'z') | |
481 | return 1; | |
482 | else if (SCM_CHAR (c) >= 'A' && SCM_CHAR (c) <= 'Z') | |
483 | return 1; | |
484 | #endif | |
485 | return 0; | |
486 | } | |
487 | ||
488 | static int | |
e20cec74 | 489 | is_absolute_file_name (SCM filename) |
4bab7f01 | 490 | { |
e20cec74 AW |
491 | size_t filename_len = scm_c_string_length (filename); |
492 | ||
4bab7f01 | 493 | if (filename_len >= 1 |
e20cec74 | 494 | && is_file_name_separator (scm_c_string_ref (filename, 0)) |
4bab7f01 AW |
495 | #ifdef __MINGW32__ |
496 | /* On Windows, one initial separator indicates a drive-relative | |
497 | path. Two separators indicate a Universal Naming Convention | |
498 | (UNC) path. UNC paths are always absolute. */ | |
499 | && filename_len >= 2 | |
e20cec74 | 500 | && is_file_name_separator (scm_c_string_ref (filename, 1)) |
4bab7f01 AW |
501 | #endif |
502 | ) | |
503 | return 1; | |
504 | if (filename_len >= 3 | |
e20cec74 AW |
505 | && is_drive_letter (scm_c_string_ref (filename, 0)) |
506 | && scm_is_eq (scm_c_string_ref (filename, 1), SCM_MAKE_CHAR (':')) | |
507 | && is_file_name_separator (scm_c_string_ref (filename, 2))) | |
4bab7f01 AW |
508 | return 1; |
509 | return 0; | |
510 | } | |
511 | ||
04e8fb0a | 512 | /* Search PATH for a directory containing a file named FILENAME. |
06721500 | 513 | The file must be readable, and not a directory. |
c12da2be | 514 | If we find one, return its full pathname; otherwise, return #f. |
56384176 | 515 | If FILENAME is absolute, return it unchanged. |
c12da2be | 516 | We also fill *stat_buf corresponding to the returned pathname. |
56384176 JB |
517 | If given, EXTENSIONS is a list of strings; for each directory |
518 | in PATH, we search for FILENAME concatenated with each EXTENSION. */ | |
a6e1e050 AW |
519 | static SCM |
520 | search_path (SCM path, SCM filename, SCM extensions, SCM require_exts, | |
521 | struct stat *stat_buf) | |
06721500 | 522 | { |
7d04d68b | 523 | struct stringbuf buf; |
56384176 | 524 | char *filename_chars; |
7d04d68b MV |
525 | size_t filename_len; |
526 | SCM result = SCM_BOOL_F; | |
06721500 | 527 | |
a6e1e050 AW |
528 | if (scm_ilength (path) < 0) |
529 | scm_misc_error ("%search-path", "path is not a proper list: ~a", | |
530 | scm_list_1 (path)); | |
531 | if (scm_ilength (extensions) < 0) | |
532 | scm_misc_error ("%search-path", "bad extensions list: ~a", | |
533 | scm_list_1 (extensions)); | |
22f4ee48 | 534 | |
661ae7ab | 535 | scm_dynwind_begin (0); |
7d04d68b MV |
536 | |
537 | filename_chars = scm_to_locale_string (filename); | |
538 | filename_len = strlen (filename_chars); | |
661ae7ab | 539 | scm_dynwind_free (filename_chars); |
06721500 | 540 | |
c12da2be | 541 | /* If FILENAME is absolute and is still valid, return it unchanged. */ |
e20cec74 | 542 | if (is_absolute_file_name (filename)) |
7d04d68b | 543 | { |
c12da2be MW |
544 | if ((scm_is_false (require_exts) || |
545 | scm_c_string_has_an_ext (filename_chars, filename_len, | |
22f4ee48 | 546 | extensions)) |
c12da2be MW |
547 | && stat (filename_chars, stat_buf) == 0 |
548 | && !(stat_buf->st_mode & S_IFDIR)) | |
549 | result = filename; | |
550 | goto end; | |
7d04d68b | 551 | } |
26544b96 | 552 | |
56384176 JB |
553 | /* If FILENAME has an extension, don't try to add EXTENSIONS to it. */ |
554 | { | |
555 | char *endp; | |
556 | ||
557 | for (endp = filename_chars + filename_len - 1; | |
558 | endp >= filename_chars; | |
559 | endp--) | |
560 | { | |
561 | if (*endp == '.') | |
562 | { | |
22f4ee48 AW |
563 | if (scm_is_true (require_exts) && |
564 | !scm_c_string_has_an_ext (filename_chars, filename_len, | |
565 | extensions)) | |
566 | { | |
567 | /* This filename has an extension, but not one of the right | |
568 | ones... */ | |
c12da2be | 569 | goto end; |
22f4ee48 | 570 | } |
56384176 JB |
571 | /* This filename already has an extension, so cancel the |
572 | list of extensions. */ | |
573 | extensions = SCM_EOL; | |
574 | break; | |
575 | } | |
4bab7f01 | 576 | else if (is_file_name_separator (SCM_MAKE_CHAR (*endp))) |
56384176 JB |
577 | /* This filename has no extension, so keep the current list |
578 | of extensions. */ | |
579 | break; | |
580 | } | |
581 | } | |
582 | ||
7d04d68b MV |
583 | /* This simplifies the loop below a bit. |
584 | */ | |
d2e53ed6 | 585 | if (scm_is_null (extensions)) |
7d04d68b | 586 | extensions = scm_listofnullstr; |
06721500 | 587 | |
7d04d68b MV |
588 | buf.buf_len = 512; |
589 | buf.buf = scm_malloc (buf.buf_len); | |
661ae7ab | 590 | scm_dynwind_unwind_handler (stringbuf_free, &buf, SCM_F_WIND_EXPLICITLY); |
0a74e31d | 591 | |
7d04d68b MV |
592 | /* Try every path element. |
593 | */ | |
d2e53ed6 | 594 | for (; scm_is_pair (path); path = SCM_CDR (path)) |
7d04d68b MV |
595 | { |
596 | SCM dir = SCM_CAR (path); | |
597 | SCM exts; | |
598 | size_t sans_ext_len; | |
599 | ||
600 | buf.ptr = buf.buf; | |
601 | stringbuf_cat_locale_string (&buf, dir); | |
602 | ||
603 | /* Concatenate the path name and the filename. */ | |
604 | ||
4bab7f01 AW |
605 | if (buf.ptr > buf.buf |
606 | && !is_file_name_separator (SCM_MAKE_CHAR (buf.ptr[-1]))) | |
607 | stringbuf_cat (&buf, FILE_NAME_SEPARATOR_STRING); | |
7d04d68b MV |
608 | |
609 | stringbuf_cat (&buf, filename_chars); | |
610 | sans_ext_len = buf.ptr - buf.buf; | |
611 | ||
612 | /* Try every extension. */ | |
d2e53ed6 | 613 | for (exts = extensions; scm_is_pair (exts); exts = SCM_CDR (exts)) |
7d04d68b MV |
614 | { |
615 | SCM ext = SCM_CAR (exts); | |
7d04d68b MV |
616 | |
617 | buf.ptr = buf.buf + sans_ext_len; | |
618 | stringbuf_cat_locale_string (&buf, ext); | |
619 | ||
620 | /* If the file exists at all, we should return it. If the | |
621 | file is inaccessible, then that's an error. */ | |
622 | ||
a6e1e050 AW |
623 | if (stat (buf.buf, stat_buf) == 0 |
624 | && ! (stat_buf->st_mode & S_IFDIR)) | |
7d04d68b MV |
625 | { |
626 | result = scm_from_locale_string (buf.buf); | |
627 | goto end; | |
628 | } | |
629 | } | |
630 | ||
631 | if (!SCM_NULL_OR_NIL_P (exts)) | |
632 | scm_wrong_type_arg_msg (NULL, 0, extensions, "proper list"); | |
633 | } | |
56384176 | 634 | |
7d04d68b MV |
635 | if (!SCM_NULL_OR_NIL_P (path)) |
636 | scm_wrong_type_arg_msg (NULL, 0, path, "proper list"); | |
56384176 | 637 | |
7d04d68b | 638 | end: |
661ae7ab | 639 | scm_dynwind_end (); |
7d04d68b | 640 | return result; |
06721500 | 641 | } |
a6e1e050 AW |
642 | |
643 | SCM_DEFINE (scm_search_path, "search-path", 2, 0, 1, | |
644 | (SCM path, SCM filename, SCM rest), | |
645 | "Search @var{path} for a directory containing a file named\n" | |
646 | "@var{filename}. The file must be readable, and not a directory.\n" | |
647 | "If we find one, return its full filename; otherwise, return\n" | |
648 | "@code{#f}. If @var{filename} is absolute, return it unchanged.\n" | |
b7e64f8b | 649 | "If given, @var{rest} is a list of extension strings; for each\n" |
a6e1e050 | 650 | "directory in @var{path}, we search for @var{filename}\n" |
b7e64f8b | 651 | "concatenated with each extension.") |
a6e1e050 AW |
652 | #define FUNC_NAME s_scm_search_path |
653 | { | |
654 | SCM extensions, require_exts; | |
655 | struct stat stat_buf; | |
656 | ||
657 | if (SCM_UNBNDP (rest) || scm_is_null (rest)) | |
658 | { | |
659 | /* Called either by Scheme code that didn't provide the optional | |
660 | arguments, or C code that used the Guile 1.8 signature (2 required, | |
661 | 1 optional arg) and passed '() or nothing as the EXTENSIONS | |
662 | argument. */ | |
663 | extensions = SCM_EOL; | |
664 | require_exts = SCM_UNDEFINED; | |
665 | } | |
666 | else | |
667 | { | |
668 | if (scm_is_null (SCM_CAR (rest)) || scm_is_pair (SCM_CAR (rest))) | |
669 | { | |
670 | /* Called by Scheme code written for 1.9. */ | |
671 | extensions = SCM_CAR (rest); | |
672 | if (scm_is_null (SCM_CDR (rest))) | |
673 | require_exts = SCM_UNDEFINED; | |
674 | else | |
675 | { | |
676 | require_exts = SCM_CADR (rest); | |
677 | if (SCM_UNLIKELY (!scm_is_null (SCM_CDDR (rest)))) | |
678 | scm_wrong_num_args (scm_from_locale_string (FUNC_NAME)); | |
679 | } | |
680 | } | |
681 | else | |
682 | { | |
683 | /* Called by C code that uses the 1.8 signature, i.e., which | |
684 | expects the 3rd argument to be EXTENSIONS. */ | |
685 | extensions = rest; | |
686 | require_exts = SCM_UNDEFINED; | |
687 | } | |
688 | } | |
689 | ||
690 | if (SCM_UNBNDP (extensions)) | |
691 | extensions = SCM_EOL; | |
692 | ||
693 | if (SCM_UNBNDP (require_exts)) | |
694 | require_exts = SCM_BOOL_F; | |
695 | ||
696 | return search_path (path, filename, extensions, require_exts, &stat_buf); | |
697 | } | |
1bbd0b84 | 698 | #undef FUNC_NAME |
06721500 JB |
699 | |
700 | ||
04e8fb0a MD |
701 | /* Search %load-path for a directory containing a file named FILENAME. |
702 | The file must be readable, and not a directory. | |
703 | If we find one, return its full filename; otherwise, return #f. | |
704 | If FILENAME is absolute, return it unchanged. */ | |
3b3b36dd | 705 | SCM_DEFINE (scm_sys_search_load_path, "%search-load-path", 1, 0, 0, |
67e8151b MG |
706 | (SCM filename), |
707 | "Search @var{%load-path} for the file named @var{filename},\n" | |
708 | "which must be readable by the current user. If @var{filename}\n" | |
709 | "is found in the list of paths to search or is an absolute\n" | |
710 | "pathname, return its full pathname. Otherwise, return\n" | |
711 | "@code{#f}. Filenames may have any of the optional extensions\n" | |
712 | "in the @code{%load-extensions} list; @code{%search-load-path}\n" | |
713 | "will try each extension automatically.") | |
1bbd0b84 | 714 | #define FUNC_NAME s_scm_sys_search_load_path |
04e8fb0a | 715 | { |
a6e1e050 AW |
716 | struct stat stat_buf; |
717 | ||
a6d9e5ab | 718 | SCM_VALIDATE_STRING (1, filename); |
1bbd0b84 | 719 | |
a6e1e050 AW |
720 | return search_path (*scm_loc_load_path, filename, *scm_loc_load_extensions, |
721 | SCM_BOOL_F, &stat_buf); | |
04e8fb0a | 722 | } |
1bbd0b84 | 723 | #undef FUNC_NAME |
04e8fb0a MD |
724 | |
725 | ||
abca59fe | 726 | /* Return true if COMPILED_FILENAME is newer than source file |
a6e1e050 | 727 | FULL_FILENAME, false otherwise. */ |
1d022387 | 728 | static int |
a6e1e050 AW |
729 | compiled_is_fresh (SCM full_filename, SCM compiled_filename, |
730 | struct stat *stat_source, struct stat *stat_compiled) | |
1d022387 | 731 | { |
722a4fb9 | 732 | int compiled_is_newer; |
a6e1e050 | 733 | struct timespec source_mtime, compiled_mtime; |
1d022387 | 734 | |
a6e1e050 AW |
735 | source_mtime = get_stat_mtime (stat_source); |
736 | compiled_mtime = get_stat_mtime (stat_compiled); | |
fefd60ba | 737 | |
a6e1e050 AW |
738 | if (source_mtime.tv_sec < compiled_mtime.tv_sec |
739 | || (source_mtime.tv_sec == compiled_mtime.tv_sec | |
740 | && source_mtime.tv_nsec <= compiled_mtime.tv_nsec)) | |
741 | compiled_is_newer = 1; | |
742 | else | |
1d022387 | 743 | { |
a6e1e050 AW |
744 | compiled_is_newer = 0; |
745 | scm_puts (";;; note: source file ", scm_current_error_port ()); | |
746 | scm_display (full_filename, scm_current_error_port ()); | |
747 | scm_puts ("\n;;; newer than compiled ", scm_current_error_port ()); | |
748 | scm_display (compiled_filename, scm_current_error_port ()); | |
749 | scm_puts ("\n", scm_current_error_port ()); | |
1d022387 | 750 | } |
abca59fe LC |
751 | |
752 | return compiled_is_newer; | |
1d022387 AW |
753 | } |
754 | ||
87c595c7 | 755 | SCM_KEYWORD (kw_env, "env"); |
5a79300f LC |
756 | SCM_KEYWORD (kw_opts, "opts"); |
757 | ||
758 | SCM_SYMBOL (sym_compile_file, "compile-file"); | |
759 | SCM_SYMBOL (sym_auto_compilation_options, "%auto-compilation-options"); | |
87c595c7 | 760 | |
1d022387 | 761 | static SCM |
6f06e8d3 | 762 | do_try_auto_compile (void *data) |
1d022387 | 763 | { |
3c997c4b AW |
764 | SCM source = PTR2SCM (data); |
765 | SCM comp_mod, compile_file; | |
ee001750 AW |
766 | |
767 | scm_puts (";;; compiling ", scm_current_error_port ()); | |
3c997c4b | 768 | scm_display (source, scm_current_error_port ()); |
ee001750 AW |
769 | scm_newline (scm_current_error_port ()); |
770 | ||
771 | comp_mod = scm_c_resolve_module ("system base compile"); | |
5a79300f | 772 | compile_file = scm_module_variable (comp_mod, sym_compile_file); |
ee001750 | 773 | |
3c997c4b AW |
774 | if (scm_is_true (compile_file)) |
775 | { | |
87c595c7 | 776 | /* Auto-compile in the context of the current module. */ |
5a79300f LC |
777 | SCM res, opts; |
778 | SCM args[5]; | |
779 | ||
780 | opts = scm_module_variable (scm_the_root_module (), | |
781 | sym_auto_compilation_options); | |
782 | if (SCM_VARIABLEP (opts)) | |
783 | opts = SCM_VARIABLE_REF (opts); | |
784 | else | |
785 | opts = SCM_EOL; | |
786 | ||
787 | args[0] = source; | |
788 | args[1] = kw_opts; | |
789 | args[2] = opts; | |
790 | args[3] = kw_env; | |
791 | args[4] = scm_current_module (); | |
792 | ||
793 | /* Assume `*current-warning-prefix*' has an appropriate value. */ | |
794 | res = scm_call_n (scm_variable_ref (compile_file), args, 5); | |
795 | ||
3c997c4b AW |
796 | scm_puts (";;; compiled ", scm_current_error_port ()); |
797 | scm_display (res, scm_current_error_port ()); | |
798 | scm_newline (scm_current_error_port ()); | |
799 | return res; | |
800 | } | |
801 | else | |
802 | { | |
803 | scm_puts (";;; it seems ", scm_current_error_port ()); | |
804 | scm_display (source, scm_current_error_port ()); | |
6f06e8d3 | 805 | scm_puts ("\n;;; is part of the compiler; skipping auto-compilation\n", |
3c997c4b AW |
806 | scm_current_error_port ()); |
807 | return SCM_BOOL_F; | |
808 | } | |
ee001750 AW |
809 | } |
810 | ||
811 | static SCM | |
6f06e8d3 | 812 | auto_compile_catch_handler (void *data, SCM tag, SCM throw_args) |
ee001750 | 813 | { |
3c997c4b | 814 | SCM source = PTR2SCM (data); |
669ea4eb AW |
815 | SCM oport, lines; |
816 | ||
817 | oport = scm_open_output_string (); | |
818 | scm_print_exception (oport, SCM_BOOL_F, tag, throw_args); | |
819 | ||
2c27dd57 AW |
820 | scm_puts (";;; WARNING: compilation of ", scm_current_warning_port ()); |
821 | scm_display (source, scm_current_warning_port ()); | |
822 | scm_puts (" failed:\n", scm_current_warning_port ()); | |
669ea4eb AW |
823 | |
824 | lines = scm_string_split (scm_get_output_string (oport), | |
825 | SCM_MAKE_CHAR ('\n')); | |
826 | for (; scm_is_pair (lines); lines = scm_cdr (lines)) | |
827 | if (scm_c_string_length (scm_car (lines))) | |
828 | { | |
2c27dd57 AW |
829 | scm_puts (";;; ", scm_current_warning_port ()); |
830 | scm_display (scm_car (lines), scm_current_warning_port ()); | |
831 | scm_newline (scm_current_warning_port ()); | |
669ea4eb AW |
832 | } |
833 | ||
834 | scm_close_port (oport); | |
835 | ||
1d022387 AW |
836 | return SCM_BOOL_F; |
837 | } | |
838 | ||
6f06e8d3 | 839 | SCM_DEFINE (scm_sys_warn_auto_compilation_enabled, "%warn-auto-compilation-enabled", 0, 0, 0, |
9591a2b0 | 840 | (void), "") |
6f06e8d3 | 841 | #define FUNC_NAME s_scm_sys_warn_auto_compilation_enabled |
ee001750 AW |
842 | { |
843 | static int message_shown = 0; | |
ee001750 AW |
844 | |
845 | if (!message_shown) | |
846 | { | |
6f06e8d3 AW |
847 | scm_puts (";;; note: auto-compilation is enabled, set GUILE_AUTO_COMPILE=0\n" |
848 | ";;; or pass the --no-auto-compile argument to disable.\n", | |
2c27dd57 | 849 | scm_current_warning_port ()); |
ee001750 AW |
850 | message_shown = 1; |
851 | } | |
852 | ||
9591a2b0 AW |
853 | return SCM_UNSPECIFIED; |
854 | } | |
15058484 | 855 | #undef FUNC_NAME |
9591a2b0 | 856 | |
9591a2b0 | 857 | static SCM |
6f06e8d3 | 858 | scm_try_auto_compile (SCM source) |
9591a2b0 | 859 | { |
6f06e8d3 | 860 | if (scm_is_false (*scm_loc_load_should_auto_compile)) |
9591a2b0 AW |
861 | return SCM_BOOL_F; |
862 | ||
6f06e8d3 | 863 | scm_sys_warn_auto_compilation_enabled (); |
ee001750 | 864 | return scm_c_catch (SCM_BOOL_T, |
6f06e8d3 | 865 | do_try_auto_compile, |
3c997c4b | 866 | SCM2PTR (source), |
6f06e8d3 | 867 | auto_compile_catch_handler, |
3c997c4b | 868 | SCM2PTR (source), |
ee001750 AW |
869 | NULL, NULL); |
870 | } | |
871 | ||
4bab7f01 AW |
872 | /* The auto-compilation code will residualize a .go file in the cache |
873 | dir: by default, $HOME/.cache/guile/2.0/ccache/PATH.go. This | |
874 | function determines the PATH to use as a key into the compilation | |
875 | cache. See also (system base compile):compiled-file-name. */ | |
6934d9e7 | 876 | static SCM |
e4f6e855 | 877 | canonical_suffix (SCM fname) |
6934d9e7 | 878 | { |
e4f6e855 | 879 | SCM canon; |
e4f6e855 | 880 | |
4bab7f01 | 881 | /* CANON should be absolute. */ |
e4f6e855 | 882 | canon = scm_canonicalize_path (fname); |
6934d9e7 | 883 | |
4bab7f01 AW |
884 | #ifdef __MINGW32__ |
885 | { | |
886 | size_t len = scm_c_string_length (canon); | |
887 | ||
888 | /* On Windows, an absolute file name that doesn't start with a | |
889 | separator starts with a drive component. Transform the drive | |
890 | component to a file name element: c:\foo -> \c\foo. */ | |
891 | if (len >= 2 | |
892 | && is_absolute_file_name (canon) | |
893 | && !is_file_name_separator (scm_c_string_ref (canon, 0))) | |
894 | return scm_string_append | |
895 | (scm_list_3 (scm_from_latin1_string (FILE_NAME_SEPARATOR_STRING), | |
896 | scm_c_substring (canon, 0, 1), | |
897 | scm_c_substring (canon, 2, len))); | |
898 | } | |
899 | #endif | |
900 | ||
901 | return canon; | |
6934d9e7 AW |
902 | } |
903 | ||
31ab99de LC |
904 | SCM_DEFINE (scm_primitive_load_path, "primitive-load-path", 0, 0, 1, |
905 | (SCM args), | |
67e8151b | 906 | "Search @var{%load-path} for the file named @var{filename} and\n" |
f6fd2c03 AW |
907 | "load it into the top-level environment.\n\n" |
908 | "If @var{filename} is a relative pathname and is not found in\n" | |
909 | "the list of search paths, one of three things may happen,\n" | |
910 | "depending on the optional second argument,\n" | |
911 | "@var{exception_on_not_found}. If it is @code{#f}, @code{#f}\n" | |
912 | "will be returned. If it is a procedure, it will be called\n" | |
913 | "with no arguments. Otherwise an error is signalled.") | |
1bbd0b84 | 914 | #define FUNC_NAME s_scm_primitive_load_path |
06721500 | 915 | { |
31ab99de | 916 | SCM filename, exception_on_not_found; |
22f4ee48 | 917 | SCM full_filename, compiled_filename; |
628132c5 | 918 | int compiled_is_fallback = 0; |
dcada7d8 | 919 | SCM hook = *scm_loc_load_hook; |
a6e1e050 | 920 | struct stat stat_source, stat_compiled; |
dcada7d8 AW |
921 | |
922 | if (scm_is_true (hook) && scm_is_false (scm_procedure_p (hook))) | |
923 | SCM_MISC_ERROR ("value of %load-hook is neither a procedure nor #f", | |
924 | SCM_EOL); | |
26544b96 | 925 | |
31ab99de LC |
926 | if (scm_is_string (args)) |
927 | { | |
928 | /* C code written for 1.8 and earlier expects this function to take a | |
929 | single argument (the file name). */ | |
930 | filename = args; | |
931 | exception_on_not_found = SCM_UNDEFINED; | |
932 | } | |
933 | else | |
934 | { | |
935 | /* Starting from 1.9, this function takes 1 required and 1 optional | |
936 | argument. */ | |
937 | long len; | |
938 | ||
939 | SCM_VALIDATE_LIST_COPYLEN (SCM_ARG1, args, len); | |
940 | if (len < 1 || len > 2) | |
941 | scm_error_num_args_subr (FUNC_NAME); | |
942 | ||
943 | filename = SCM_CAR (args); | |
944 | SCM_VALIDATE_STRING (SCM_ARG1, filename); | |
945 | ||
946 | exception_on_not_found = len > 1 ? SCM_CADR (args) : SCM_UNDEFINED; | |
947 | } | |
948 | ||
0fb81f95 AW |
949 | if (SCM_UNBNDP (exception_on_not_found)) |
950 | exception_on_not_found = SCM_BOOL_T; | |
26544b96 | 951 | |
a6e1e050 AW |
952 | full_filename = search_path (*scm_loc_load_path, filename, |
953 | *scm_loc_load_extensions, SCM_BOOL_F, | |
954 | &stat_source); | |
eba5ea7a | 955 | |
88cbb421 | 956 | compiled_filename = |
a6e1e050 AW |
957 | search_path (*scm_loc_load_compiled_path, filename, |
958 | *scm_loc_load_compiled_extensions, SCM_BOOL_T, | |
959 | &stat_compiled); | |
88cbb421 | 960 | |
5ea401bf AW |
961 | if (scm_is_false (compiled_filename) |
962 | && scm_is_true (full_filename) | |
3c997c4b | 963 | && scm_is_true (*scm_loc_compile_fallback_path) |
1e56cff2 | 964 | && scm_is_false (*scm_loc_fresh_auto_compile) |
3c997c4b AW |
965 | && scm_is_pair (*scm_loc_load_compiled_extensions) |
966 | && scm_is_string (scm_car (*scm_loc_load_compiled_extensions))) | |
5ea401bf | 967 | { |
a6e1e050 AW |
968 | SCM fallback; |
969 | char *fallback_chars; | |
970 | ||
971 | fallback = scm_string_append | |
3c997c4b | 972 | (scm_list_3 (*scm_loc_compile_fallback_path, |
e4f6e855 | 973 | canonical_suffix (full_filename), |
3c997c4b | 974 | scm_car (*scm_loc_load_compiled_extensions))); |
a6e1e050 AW |
975 | |
976 | fallback_chars = scm_to_locale_string (fallback); | |
977 | if (stat (fallback_chars, &stat_compiled) == 0) | |
628132c5 AW |
978 | { |
979 | compiled_filename = fallback; | |
980 | compiled_is_fallback = 1; | |
981 | } | |
a6e1e050 | 982 | free (fallback_chars); |
5ea401bf AW |
983 | } |
984 | ||
22f4ee48 | 985 | if (scm_is_false (full_filename) && scm_is_false (compiled_filename)) |
0fb81f95 | 986 | { |
f6fd2c03 AW |
987 | if (scm_is_true (scm_procedure_p (exception_on_not_found))) |
988 | return scm_call_0 (exception_on_not_found); | |
989 | else if (scm_is_false (exception_on_not_found)) | |
990 | return SCM_BOOL_F; | |
991 | else | |
0fb81f95 AW |
992 | SCM_MISC_ERROR ("Unable to find file ~S in load path", |
993 | scm_list_1 (filename)); | |
0fb81f95 | 994 | } |
22f4ee48 | 995 | |
dcada7d8 AW |
996 | if (!scm_is_false (hook)) |
997 | scm_call_1 (hook, (scm_is_true (full_filename) | |
998 | ? full_filename : compiled_filename)); | |
999 | ||
1d022387 AW |
1000 | if (scm_is_false (full_filename) |
1001 | || (scm_is_true (compiled_filename) | |
a6e1e050 AW |
1002 | && compiled_is_fresh (full_filename, compiled_filename, |
1003 | &stat_source, &stat_compiled))) | |
22f4ee48 AW |
1004 | return scm_load_compiled_with_vm (compiled_filename); |
1005 | ||
628132c5 AW |
1006 | /* Perhaps there was the installed .go that was stale, but our fallback is |
1007 | fresh. Let's try that. Duplicating code, but perhaps that's OK. */ | |
1008 | ||
1009 | if (!compiled_is_fallback | |
1010 | && scm_is_true (*scm_loc_compile_fallback_path) | |
1e56cff2 | 1011 | && scm_is_false (*scm_loc_fresh_auto_compile) |
628132c5 AW |
1012 | && scm_is_pair (*scm_loc_load_compiled_extensions) |
1013 | && scm_is_string (scm_car (*scm_loc_load_compiled_extensions))) | |
1014 | { | |
a6e1e050 AW |
1015 | SCM fallback; |
1016 | char *fallback_chars; | |
1017 | int stat_ret; | |
1018 | ||
1019 | fallback = scm_string_append | |
628132c5 | 1020 | (scm_list_3 (*scm_loc_compile_fallback_path, |
e4f6e855 | 1021 | canonical_suffix (full_filename), |
628132c5 | 1022 | scm_car (*scm_loc_load_compiled_extensions))); |
a6e1e050 AW |
1023 | |
1024 | fallback_chars = scm_to_locale_string (fallback); | |
1025 | stat_ret = stat (fallback_chars, &stat_compiled); | |
1026 | free (fallback_chars); | |
1027 | ||
1028 | if (stat_ret == 0 && compiled_is_fresh (full_filename, fallback, | |
1029 | &stat_source, &stat_compiled)) | |
628132c5 | 1030 | { |
2c27dd57 AW |
1031 | scm_puts (";;; found fresh local cache at ", scm_current_warning_port ()); |
1032 | scm_display (fallback, scm_current_warning_port ()); | |
1033 | scm_newline (scm_current_warning_port ()); | |
5950cc3f | 1034 | return scm_load_compiled_with_vm (fallback); |
628132c5 | 1035 | } |
3c997c4b | 1036 | } |
628132c5 AW |
1037 | |
1038 | /* Otherwise, we bottom out here. */ | |
22f4ee48 | 1039 | { |
6f06e8d3 | 1040 | SCM freshly_compiled = scm_try_auto_compile (full_filename); |
22f4ee48 | 1041 | |
628132c5 AW |
1042 | if (scm_is_true (freshly_compiled)) |
1043 | return scm_load_compiled_with_vm (freshly_compiled); | |
22f4ee48 | 1044 | else |
628132c5 | 1045 | return scm_primitive_load (full_filename); |
22f4ee48 | 1046 | } |
06721500 | 1047 | } |
1bbd0b84 | 1048 | #undef FUNC_NAME |
06721500 | 1049 | |
c519b272 MV |
1050 | SCM |
1051 | scm_c_primitive_load_path (const char *filename) | |
1052 | { | |
31ab99de | 1053 | return scm_primitive_load_path (scm_from_locale_string (filename)); |
c519b272 MV |
1054 | } |
1055 | ||
5f161164 AW |
1056 | void |
1057 | scm_init_eval_in_scheme (void) | |
1058 | { | |
1059 | SCM eval_scm, eval_go; | |
a6e1e050 AW |
1060 | struct stat stat_source, stat_compiled; |
1061 | ||
1062 | eval_scm = search_path (*scm_loc_load_path, | |
1063 | scm_from_locale_string ("ice-9/eval.scm"), | |
1064 | SCM_EOL, SCM_BOOL_F, &stat_source); | |
1065 | eval_go = search_path (*scm_loc_load_compiled_path, | |
1066 | scm_from_locale_string ("ice-9/eval.go"), | |
1067 | SCM_EOL, SCM_BOOL_F, &stat_compiled); | |
5f161164 AW |
1068 | |
1069 | if (scm_is_true (eval_scm) && scm_is_true (eval_go) | |
a6e1e050 AW |
1070 | && compiled_is_fresh (eval_scm, eval_go, |
1071 | &stat_source, &stat_compiled)) | |
5f161164 | 1072 | scm_load_compiled_with_vm (eval_go); |
ed1bf2c8 AW |
1073 | else |
1074 | /* if we have no eval.go, we shouldn't load any compiled code at all */ | |
1075 | *scm_loc_load_compiled_path = SCM_EOL; | |
5f161164 AW |
1076 | } |
1077 | ||
06721500 | 1078 | \f |
e151bee6 | 1079 | /* Information about the build environment. */ |
06721500 | 1080 | |
d7a22073 LC |
1081 | SCM_VARIABLE_INIT (sys_host_type, "%host-type", |
1082 | scm_from_locale_string (HOST_TYPE)); | |
1083 | ||
1084 | ||
e151bee6 JB |
1085 | /* Initialize the scheme variable %guile-build-info, based on data |
1086 | provided by the Makefile, via libpath.h. */ | |
1087 | static void | |
1088 | init_build_info () | |
1089 | { | |
1090 | static struct { char *name; char *value; } info[] = SCM_BUILD_INFO; | |
86d31dfe | 1091 | SCM *loc = SCM_VARIABLE_LOC (scm_c_define ("%guile-build-info", SCM_EOL)); |
c014a02e | 1092 | unsigned long i; |
e151bee6 JB |
1093 | |
1094 | for (i = 0; i < (sizeof (info) / sizeof (info[0])); i++) | |
cc95e00a MV |
1095 | { |
1096 | SCM key = scm_from_locale_symbol (info[i].name); | |
1097 | SCM val = scm_from_locale_string (info[i].value); | |
1098 | *loc = scm_acons (key, val, *loc); | |
1099 | } | |
26ac1e3f AW |
1100 | #ifdef PACKAGE_PACKAGER |
1101 | *loc = scm_acons (scm_from_latin1_symbol ("packager"), | |
1102 | scm_from_latin1_string (PACKAGE_PACKAGER), | |
1103 | *loc); | |
1104 | #endif | |
1105 | #ifdef PACKAGE_PACKAGER_VERSION | |
1106 | *loc = scm_acons (scm_from_latin1_symbol ("packager-version"), | |
1107 | scm_from_latin1_string (PACKAGE_PACKAGER_VERSION), | |
1108 | *loc); | |
1109 | #endif | |
1110 | #ifdef PACKAGE_PACKAGER_BUG_REPORTS | |
1111 | *loc = scm_acons (scm_from_latin1_symbol ("packager-bug-reports"), | |
1112 | scm_from_latin1_string (PACKAGE_PACKAGER_BUG_REPORTS), | |
1113 | *loc); | |
1114 | #endif | |
e151bee6 JB |
1115 | } |
1116 | ||
e151bee6 | 1117 | \f |
0f2d19dd JB |
1118 | void |
1119 | scm_init_load () | |
0f2d19dd | 1120 | { |
f39448c5 | 1121 | scm_listofnullstr = scm_list_1 (scm_nullstr); |
86d31dfe | 1122 | scm_loc_load_path = SCM_VARIABLE_LOC (scm_c_define ("%load-path", SCM_EOL)); |
26544b96 | 1123 | scm_loc_load_extensions |
86d31dfe | 1124 | = SCM_VARIABLE_LOC (scm_c_define ("%load-extensions", |
cc95e00a MV |
1125 | scm_list_2 (scm_from_locale_string (".scm"), |
1126 | scm_nullstr))); | |
5b197db8 AW |
1127 | scm_loc_load_compiled_path |
1128 | = SCM_VARIABLE_LOC (scm_c_define ("%load-compiled-path", SCM_EOL)); | |
22f4ee48 AW |
1129 | scm_loc_load_compiled_extensions |
1130 | = SCM_VARIABLE_LOC (scm_c_define ("%load-compiled-extensions", | |
1131 | scm_list_1 (scm_from_locale_string (".go")))); | |
86d31dfe | 1132 | scm_loc_load_hook = SCM_VARIABLE_LOC (scm_c_define ("%load-hook", SCM_BOOL_F)); |
06721500 | 1133 | |
5ea401bf AW |
1134 | scm_loc_compile_fallback_path |
1135 | = SCM_VARIABLE_LOC (scm_c_define ("%compile-fallback-path", SCM_BOOL_F)); | |
6f06e8d3 AW |
1136 | scm_loc_load_should_auto_compile |
1137 | = SCM_VARIABLE_LOC (scm_c_define ("%load-should-auto-compile", SCM_BOOL_F)); | |
1e56cff2 AW |
1138 | scm_loc_fresh_auto_compile |
1139 | = SCM_VARIABLE_LOC (scm_c_define ("%fresh-auto-compile", SCM_BOOL_F)); | |
5ea401bf | 1140 | |
bd31bce6 MW |
1141 | scm_ellipsis = scm_from_latin1_string ("..."); |
1142 | ||
c81c2ad3 | 1143 | the_reader = scm_make_fluid_with_default (SCM_BOOL_F); |
ec3a8ace NJ |
1144 | scm_c_define("current-reader", the_reader); |
1145 | ||
a6029b97 AW |
1146 | scm_c_define ("load-compiled", |
1147 | scm_c_make_gsubr ("load-compiled/vm", 1, 0, 0, | |
1148 | scm_load_compiled_with_vm)); | |
1149 | ||
e151bee6 JB |
1150 | init_build_info (); |
1151 | ||
a0599745 | 1152 | #include "libguile/load.x" |
0f2d19dd | 1153 | } |
89e00824 | 1154 | |
6128f34c | 1155 | void |
6f06e8d3 | 1156 | scm_init_load_should_auto_compile () |
6128f34c | 1157 | { |
1e56cff2 AW |
1158 | char *auto_compile = getenv ("GUILE_AUTO_COMPILE"); |
1159 | ||
1160 | if (auto_compile && strcmp (auto_compile, "0") == 0) | |
1161 | { | |
1162 | *scm_loc_load_should_auto_compile = SCM_BOOL_F; | |
1163 | *scm_loc_fresh_auto_compile = SCM_BOOL_F; | |
1164 | } | |
1165 | /* Allow "freshen" also. */ | |
1166 | else if (auto_compile && strncmp (auto_compile, "fresh", 5) == 0) | |
1167 | { | |
1168 | *scm_loc_load_should_auto_compile = SCM_BOOL_T; | |
1169 | *scm_loc_fresh_auto_compile = SCM_BOOL_T; | |
1170 | } | |
1171 | else | |
1172 | { | |
1173 | *scm_loc_load_should_auto_compile = SCM_BOOL_T; | |
1174 | *scm_loc_fresh_auto_compile = SCM_BOOL_F; | |
1175 | } | |
6128f34c AW |
1176 | } |
1177 | ||
1178 | ||
1179 | ||
89e00824 ML |
1180 | /* |
1181 | Local Variables: | |
1182 | c-file-style: "gnu" | |
1183 | End: | |
1184 | */ |