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