1 /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2004, 2006, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
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.
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
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
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29 #include "libguile/_scm.h"
30 #include "libguile/private-gc.h" /* scm_getenv_int */
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"
40 #include "libguile/modules.h"
41 #include "libguile/chars.h"
42 #include "libguile/srfi-13.h"
44 #include "libguile/validate.h"
45 #include "libguile/load.h"
46 #include "libguile/fluids.h"
48 #include "libguile/vm.h" /* for load-compiled/vm */
50 #include <sys/types.h>
55 #endif /* HAVE_UNISTD_H */
59 #endif /* HAVE_PWD_H */
65 #include <stat-time.h>
68 /* Loading a file, given an absolute filename. */
70 /* Hook to run when we load a file, perhaps to announce the fact somewhere.
71 Applied to the full name of the file. */
72 static SCM
*scm_loc_load_hook
;
74 /* The current reader (a fluid). */
75 static SCM the_reader
= SCM_BOOL_F
;
78 SCM_DEFINE (scm_primitive_load
, "primitive-load", 1, 0, 0,
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.")
87 #define FUNC_NAME s_scm_primitive_load
89 SCM hook
= *scm_loc_load_hook
;
91 SCM_VALIDATE_STRING (1, filename
);
92 if (scm_is_true (hook
) && scm_is_false (scm_procedure_p (hook
)))
93 SCM_MISC_ERROR ("value of %load-hook is neither a procedure nor #f",
96 if (!scm_is_false (hook
))
97 scm_call_1 (hook
, filename
);
100 SCM port
= scm_open_file (filename
, scm_from_locale_string ("r"));
101 scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE
);
102 scm_i_dynwind_current_load_port (port
);
104 encoding
= scm_i_scan_for_encoding (port
);
106 scm_i_set_port_encoding_x (port
, encoding
);
108 /* The file has no encoding declared. We'll presume Latin-1. */
109 scm_i_set_port_encoding_x (port
, NULL
);
115 /* Lookup and use the current reader to read the next
117 reader
= scm_fluid_ref (the_reader
);
118 if (reader
== SCM_BOOL_F
)
119 form
= scm_read (port
);
121 form
= scm_call_1 (reader
, port
);
123 if (SCM_EOF_OBJECT_P (form
))
126 scm_primitive_eval_x (form
);
130 scm_close_port (port
);
132 return SCM_UNSPECIFIED
;
137 scm_c_primitive_load (const char *filename
)
139 return scm_primitive_load (scm_from_locale_string (filename
));
143 /* Builtin path to scheme library files. */
144 #ifdef SCM_PKGDATA_DIR
145 SCM_DEFINE (scm_sys_package_data_dir
, "%package-data-dir", 0, 0, 0,
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}.")
150 #define FUNC_NAME s_scm_sys_package_data_dir
152 return scm_from_locale_string (SCM_PKGDATA_DIR
);
155 #endif /* SCM_PKGDATA_DIR */
157 #ifdef SCM_LIBRARY_DIR
158 SCM_DEFINE (scm_sys_library_dir
, "%library-dir", 0,0,0,
160 "Return the directory where the Guile Scheme library files are installed.\n"
161 "E.g., may return \"/usr/share/guile/1.3.5\".")
162 #define FUNC_NAME s_scm_sys_library_dir
164 return scm_from_locale_string (SCM_LIBRARY_DIR
);
167 #endif /* SCM_LIBRARY_DIR */
170 SCM_DEFINE (scm_sys_site_dir
, "%site-dir", 0,0,0,
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
"\".")
175 #define FUNC_NAME s_scm_sys_site_dir
177 return scm_from_locale_string (SCM_SITE_DIR
);
180 #endif /* SCM_SITE_DIR */
182 #ifdef SCM_GLOBAL_SITE_DIR
183 SCM_DEFINE (scm_sys_global_site_dir
, "%global-site-dir", 0,0,0,
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
190 return scm_from_locale_string (SCM_GLOBAL_SITE_DIR
);
193 #endif /* SCM_GLOBAL_SITE_DIR */
197 /* Initializing the load path, and searching it. */
199 /* List of names of directories we search for files to load. */
200 static SCM
*scm_loc_load_path
;
202 /* List of extensions we try adding to the filenames. */
203 static SCM
*scm_loc_load_extensions
;
205 /* Like %load-path and %load-extensions, but for compiled files. */
206 static SCM
*scm_loc_load_compiled_path
;
207 static SCM
*scm_loc_load_compiled_extensions
;
209 /* Whether we should try to auto-compile. */
210 static SCM
*scm_loc_load_should_auto_compile
;
212 /* The fallback path for auto-compilation */
213 static SCM
*scm_loc_compile_fallback_path
;
215 SCM_DEFINE (scm_parse_path
, "parse-path", 1, 1, 0,
216 (SCM path
, SCM tail
),
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"
221 #define FUNC_NAME s_scm_parse_path
224 SCM sep
= SCM_MAKE_CHAR (';');
226 SCM sep
= SCM_MAKE_CHAR (':');
229 if (SCM_UNBNDP (tail
))
231 return (scm_is_false (path
)
233 : scm_append_x (scm_list_2 (scm_string_split (path
, sep
), tail
)));
238 /* Initialize the global variable %load-path, given the value of the
239 SCM_SITE_DIR and SCM_LIBRARY_DIR preprocessor symbols and the
240 GUILE_LOAD_PATH environment variable. */
242 scm_init_load_path ()
248 #ifdef SCM_LIBRARY_DIR
249 env
= getenv ("GUILE_SYSTEM_PATH");
250 if (env
&& strcmp (env
, "") == 0)
251 /* special-case interpret system-path=="" as meaning no system path instead
255 path
= scm_parse_path (scm_from_locale_string (env
), path
);
257 path
= scm_list_4 (scm_from_locale_string (SCM_LIBRARY_DIR
),
258 scm_from_locale_string (SCM_SITE_DIR
),
259 scm_from_locale_string (SCM_GLOBAL_SITE_DIR
),
260 scm_from_locale_string (SCM_PKGDATA_DIR
));
262 env
= getenv ("GUILE_SYSTEM_COMPILED_PATH");
263 if (env
&& strcmp (env
, "") == 0)
267 cpath
= scm_parse_path (scm_from_locale_string (env
), cpath
);
269 cpath
= scm_cons (scm_from_locale_string (SCM_CCACHE_DIR
), cpath
);
271 #endif /* SCM_LIBRARY_DIR */
280 #define FALLBACK_DIR \
281 "guile/ccache/" SCM_EFFECTIVE_VERSION "-" SCM_OBJCODE_MACHINE_VERSION_STRING
283 if ((e
= getenv ("XDG_CACHE_HOME")))
284 snprintf (cachedir
, sizeof(cachedir
), "%s/" FALLBACK_DIR
, e
);
285 else if ((e
= getenv ("HOME")))
286 snprintf (cachedir
, sizeof(cachedir
), "%s/.cache/" FALLBACK_DIR
, e
);
288 else if ((pwd
= getpwuid (getuid ())) && pwd
->pw_dir
)
289 snprintf (cachedir
, sizeof(cachedir
), "%s/.cache/" FALLBACK_DIR
,
291 #endif /* HAVE_GETPWENT */
296 *scm_loc_compile_fallback_path
= scm_from_locale_string (cachedir
);
299 env
= getenv ("GUILE_LOAD_PATH");
301 path
= scm_parse_path (scm_from_locale_string (env
), path
);
303 env
= getenv ("GUILE_LOAD_COMPILED_PATH");
305 cpath
= scm_parse_path (scm_from_locale_string (env
), cpath
);
307 *scm_loc_load_path
= path
;
308 *scm_loc_load_compiled_path
= cpath
;
311 SCM scm_listofnullstr
;
313 /* Utility functions for assembling C strings in a buffer.
322 stringbuf_free (void *data
)
324 struct stringbuf
*buf
= (struct stringbuf
*)data
;
329 stringbuf_grow (struct stringbuf
*buf
)
331 size_t ptroff
= buf
->ptr
- buf
->buf
;
333 buf
->buf
= scm_realloc (buf
->buf
, buf
->buf_len
);
334 buf
->ptr
= buf
->buf
+ ptroff
;
338 stringbuf_cat_locale_string (struct stringbuf
*buf
, SCM str
)
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
);
344 /* buffer is too small, double its size and try again.
346 stringbuf_grow (buf
);
347 stringbuf_cat_locale_string (buf
, str
);
351 /* string fits, terminate it and check for embedded '\0'.
353 buf
->ptr
[len
] = '\0';
354 if (strlen (buf
->ptr
) != len
)
355 scm_misc_error (NULL
,
356 "string contains #\\nul character: ~S",
363 stringbuf_cat (struct stringbuf
*buf
, char *str
)
365 size_t max_len
= buf
->buf_len
- (buf
->ptr
- buf
->buf
) - 1;
366 size_t len
= strlen (str
);
369 /* buffer is too small, double its size and try again.
371 stringbuf_grow (buf
);
372 stringbuf_cat (buf
, str
);
376 /* string fits, copy it into buffer.
378 strcpy (buf
->ptr
, str
);
385 scm_c_string_has_an_ext (char *str
, size_t len
, SCM extensions
)
387 for (; !scm_is_null (extensions
); extensions
= SCM_CDR (extensions
))
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);
403 /* Search PATH for a directory containing a file named FILENAME.
404 The file must be readable, and not a directory.
405 If we find one, return its full filename; otherwise, return #f.
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. */
409 SCM_DEFINE (scm_search_path
, "search-path", 2, 0, 1,
410 (SCM path
, SCM filename
, SCM rest
),
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}.")
418 #define FUNC_NAME s_scm_search_path
420 struct stringbuf buf
;
421 char *filename_chars
;
423 SCM extensions
, require_exts
;
424 SCM result
= SCM_BOOL_F
;
426 if (SCM_UNBNDP (rest
) || scm_is_null (rest
))
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,
430 1 optional arg) and passed '() or nothing as the EXTENSIONS
432 extensions
= SCM_EOL
;
433 require_exts
= SCM_UNDEFINED
;
437 if (scm_is_null (SCM_CAR (rest
)) || scm_is_pair (SCM_CAR (rest
)))
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
;
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
));
452 /* Called by C code that uses the 1.8 signature, i.e., which
453 expects the 3rd argument to be EXTENSIONS. */
455 require_exts
= SCM_UNDEFINED
;
459 if (SCM_UNBNDP (extensions
))
460 extensions
= SCM_EOL
;
462 SCM_VALIDATE_LIST (3, extensions
);
464 if (SCM_UNBNDP (require_exts
))
465 require_exts
= SCM_BOOL_F
;
467 scm_dynwind_begin (0);
469 filename_chars
= scm_to_locale_string (filename
);
470 filename_len
= strlen (filename_chars
);
471 scm_dynwind_free (filename_chars
);
473 /* If FILENAME is absolute, return it unchanged. */
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] == '\\')))
482 if (filename_len
>= 1 && filename_chars
[0] == '/')
486 if (scm_is_true (require_exts
) &&
487 !scm_c_string_has_an_ext (filename_chars
, filename_len
,
495 /* If FILENAME has an extension, don't try to add EXTENSIONS to it. */
499 for (endp
= filename_chars
+ filename_len
- 1;
500 endp
>= filename_chars
;
505 if (scm_is_true (require_exts
) &&
506 !scm_c_string_has_an_ext (filename_chars
, filename_len
,
509 /* This filename has an extension, but not one of the right
514 /* This filename already has an extension, so cancel the
515 list of extensions. */
516 extensions
= SCM_EOL
;
520 else if (*endp
== '/' || *endp
== '\\')
522 else if (*endp
== '/')
524 /* This filename has no extension, so keep the current list
530 /* This simplifies the loop below a bit.
532 if (scm_is_null (extensions
))
533 extensions
= scm_listofnullstr
;
536 buf
.buf
= scm_malloc (buf
.buf_len
);
537 scm_dynwind_unwind_handler (stringbuf_free
, &buf
, SCM_F_WIND_EXPLICITLY
);
539 /* Try every path element.
541 for (; scm_is_pair (path
); path
= SCM_CDR (path
))
543 SCM dir
= SCM_CAR (path
);
548 stringbuf_cat_locale_string (&buf
, dir
);
550 /* Concatenate the path name and the filename. */
553 if ((buf
.ptr
> buf
.buf
) && (buf
.ptr
[-1] != '/') && (buf
.ptr
[-1] != '\\'))
555 if ((buf
.ptr
> buf
.buf
) && (buf
.ptr
[-1] != '/'))
557 stringbuf_cat (&buf
, "/");
559 stringbuf_cat (&buf
, filename_chars
);
560 sans_ext_len
= buf
.ptr
- buf
.buf
;
562 /* Try every extension. */
563 for (exts
= extensions
; scm_is_pair (exts
); exts
= SCM_CDR (exts
))
565 SCM ext
= SCM_CAR (exts
);
568 buf
.ptr
= buf
.buf
+ sans_ext_len
;
569 stringbuf_cat_locale_string (&buf
, ext
);
571 /* If the file exists at all, we should return it. If the
572 file is inaccessible, then that's an error. */
574 if (stat (buf
.buf
, &mode
) == 0
575 && ! (mode
.st_mode
& S_IFDIR
))
577 result
= scm_from_locale_string (buf
.buf
);
582 if (!SCM_NULL_OR_NIL_P (exts
))
583 scm_wrong_type_arg_msg (NULL
, 0, extensions
, "proper list");
586 if (!SCM_NULL_OR_NIL_P (path
))
587 scm_wrong_type_arg_msg (NULL
, 0, path
, "proper list");
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. */
600 SCM_DEFINE (scm_sys_search_load_path
, "%search-load-path", 1, 0, 0,
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.")
609 #define FUNC_NAME s_scm_sys_search_load_path
611 SCM path
= *scm_loc_load_path
;
612 SCM exts
= *scm_loc_load_extensions
;
613 SCM_VALIDATE_STRING (1, filename
);
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
);
619 return scm_search_path (path
, filename
, exts
);
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. */
628 compiled_is_fresh (SCM full_filename
, SCM compiled_filename
)
630 char *source
, *compiled
;
631 struct stat stat_source
, stat_compiled
;
632 int compiled_is_newer
;
634 source
= scm_to_locale_string (full_filename
);
635 compiled
= scm_to_locale_string (compiled_filename
);
637 if (stat (source
, &stat_source
) == 0
638 && stat (compiled
, &stat_compiled
) == 0)
640 struct timespec source_mtime
, compiled_mtime
;
642 source_mtime
= get_stat_mtime (&stat_source
);
643 compiled_mtime
= get_stat_mtime (&stat_compiled
);
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;
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 ());
660 /* At least one of the files isn't accessible. */
661 compiled_is_newer
= 0;
666 return compiled_is_newer
;
669 SCM_KEYWORD (kw_env
, "env");
672 do_try_auto_compile (void *data
)
674 SCM source
= PTR2SCM (data
);
675 SCM comp_mod
, compile_file
;
677 scm_puts (";;; compiling ", scm_current_error_port ());
678 scm_display (source
, scm_current_error_port ());
679 scm_newline (scm_current_error_port ());
681 comp_mod
= scm_c_resolve_module ("system base compile");
682 compile_file
= scm_module_variable
683 (comp_mod
, scm_from_latin1_symbol ("compile-file"));
685 if (scm_is_true (compile_file
))
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 ());
690 scm_puts (";;; compiled ", scm_current_error_port ());
691 scm_display (res
, scm_current_error_port ());
692 scm_newline (scm_current_error_port ());
697 scm_puts (";;; it seems ", scm_current_error_port ());
698 scm_display (source
, scm_current_error_port ());
699 scm_puts ("\n;;; is part of the compiler; skipping auto-compilation\n",
700 scm_current_error_port ());
706 auto_compile_catch_handler (void *data
, SCM tag
, SCM throw_args
)
708 SCM source
= PTR2SCM (data
);
709 scm_puts (";;; WARNING: compilation of ", scm_current_error_port ());
710 scm_display (source
, scm_current_error_port ());
711 scm_puts (" failed:\n", scm_current_error_port ());
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 ());
720 SCM_DEFINE (scm_sys_warn_auto_compilation_enabled
, "%warn-auto-compilation-enabled", 0, 0, 0,
722 #define FUNC_NAME s_scm_sys_warn_auto_compilation_enabled
724 static int message_shown
= 0;
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",
730 scm_current_error_port ());
734 return SCM_UNSPECIFIED
;
739 scm_try_auto_compile (SCM source
)
741 if (scm_is_false (*scm_loc_load_should_auto_compile
))
744 scm_sys_warn_auto_compilation_enabled ();
745 return scm_c_catch (SCM_BOOL_T
,
748 auto_compile_catch_handler
,
753 SCM_DEFINE (scm_primitive_load_path
, "primitive-load-path", 0, 0, 1,
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"
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.")
761 #define FUNC_NAME s_scm_primitive_load_path
763 SCM filename
, exception_on_not_found
;
764 SCM full_filename
, compiled_filename
;
765 int compiled_is_fallback
= 0;
767 if (scm_is_string (args
))
769 /* C code written for 1.8 and earlier expects this function to take a
770 single argument (the file name). */
772 exception_on_not_found
= SCM_UNDEFINED
;
776 /* Starting from 1.9, this function takes 1 required and 1 optional
780 SCM_VALIDATE_LIST_COPYLEN (SCM_ARG1
, args
, len
);
781 if (len
< 1 || len
> 2)
782 scm_error_num_args_subr (FUNC_NAME
);
784 filename
= SCM_CAR (args
);
785 SCM_VALIDATE_STRING (SCM_ARG1
, filename
);
787 exception_on_not_found
= len
> 1 ? SCM_CADR (args
) : SCM_UNDEFINED
;
790 if (SCM_UNBNDP (exception_on_not_found
))
791 exception_on_not_found
= SCM_BOOL_T
;
793 full_filename
= scm_sys_search_load_path (filename
);
794 if (scm_is_string (full_filename
))
795 full_filename
= scm_canonicalize_path (full_filename
);
798 scm_search_path (*scm_loc_load_compiled_path
,
800 scm_list_2 (*scm_loc_load_compiled_extensions
,
803 if (scm_is_false (compiled_filename
)
804 && scm_is_true (full_filename
)
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
)))
809 SCM fallback
= scm_string_append
810 (scm_list_3 (*scm_loc_compile_fallback_path
,
812 scm_car (*scm_loc_load_compiled_extensions
)));
813 if (scm_is_true (scm_stat (fallback
, SCM_BOOL_F
)))
815 compiled_filename
= fallback
;
816 compiled_is_fallback
= 1;
820 if (scm_is_false (full_filename
) && scm_is_false (compiled_filename
))
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
));
829 if (scm_is_false (full_filename
)
830 || (scm_is_true (compiled_filename
)
831 && compiled_is_fresh (full_filename
, compiled_filename
)))
832 return scm_load_compiled_with_vm (compiled_filename
);
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. */
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
)))
842 SCM fallback
= scm_string_append
843 (scm_list_3 (*scm_loc_compile_fallback_path
,
845 scm_car (*scm_loc_load_compiled_extensions
)));
846 if (scm_is_true (scm_stat (fallback
, SCM_BOOL_F
))
847 && compiled_is_fresh (full_filename
, fallback
))
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 ());
852 return scm_load_compiled_with_vm (fallback
);
856 /* Otherwise, we bottom out here. */
858 SCM freshly_compiled
= scm_try_auto_compile (full_filename
);
860 if (scm_is_true (freshly_compiled
))
861 return scm_load_compiled_with_vm (freshly_compiled
);
863 return scm_primitive_load (full_filename
);
869 scm_c_primitive_load_path (const char *filename
)
871 return scm_primitive_load_path (scm_from_locale_string (filename
));
875 scm_init_eval_in_scheme (void)
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"),
881 eval_go
= scm_search_path (*scm_loc_load_compiled_path
,
882 scm_from_locale_string ("ice-9/eval.go"),
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
);
889 /* if we have no eval.go, we shouldn't load any compiled code at all */
890 *scm_loc_load_compiled_path
= SCM_EOL
;
894 /* Information about the build environment. */
896 SCM_VARIABLE_INIT (sys_host_type
, "%host-type",
897 scm_from_locale_string (HOST_TYPE
));
900 /* Initialize the scheme variable %guile-build-info, based on data
901 provided by the Makefile, via libpath.h. */
905 static struct { char *name
; char *value
; } info
[] = SCM_BUILD_INFO
;
906 SCM
*loc
= SCM_VARIABLE_LOC (scm_c_define ("%guile-build-info", SCM_EOL
));
909 for (i
= 0; i
< (sizeof (info
) / sizeof (info
[0])); i
++)
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
);
921 scm_listofnullstr
= scm_list_1 (scm_nullstr
);
922 scm_loc_load_path
= SCM_VARIABLE_LOC (scm_c_define ("%load-path", SCM_EOL
));
923 scm_loc_load_extensions
924 = SCM_VARIABLE_LOC (scm_c_define ("%load-extensions",
925 scm_list_2 (scm_from_locale_string (".scm"),
927 scm_loc_load_compiled_path
928 = SCM_VARIABLE_LOC (scm_c_define ("%load-compiled-path", SCM_EOL
));
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"))));
932 scm_loc_load_hook
= SCM_VARIABLE_LOC (scm_c_define ("%load-hook", SCM_BOOL_F
));
934 scm_loc_compile_fallback_path
935 = SCM_VARIABLE_LOC (scm_c_define ("%compile-fallback-path", SCM_BOOL_F
));
936 scm_loc_load_should_auto_compile
937 = SCM_VARIABLE_LOC (scm_c_define ("%load-should-auto-compile", SCM_BOOL_F
));
939 the_reader
= scm_make_fluid ();
940 scm_fluid_set_x (the_reader
, SCM_BOOL_F
);
941 scm_c_define("current-reader", the_reader
);
943 scm_c_define ("load-compiled",
944 scm_c_make_gsubr ("load-compiled/vm", 1, 0, 0,
945 scm_load_compiled_with_vm
));
949 #include "libguile/load.x"
953 scm_init_load_should_auto_compile ()
955 *scm_loc_load_should_auto_compile
=
956 scm_from_bool (scm_getenv_int ("GUILE_AUTO_COMPILE", 1));