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