| 1 | /* Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2004, 2006, 2008, |
| 2 | * 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc. |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 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. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 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 |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 17 | * 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | |
| 21 | \f |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | # include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #include <string.h> |
| 28 | #include <stdio.h> |
| 29 | |
| 30 | #include "libguile/_scm.h" |
| 31 | #include "libguile/private-gc.h" /* scm_getenv_int */ |
| 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" |
| 41 | #include "libguile/modules.h" |
| 42 | #include "libguile/chars.h" |
| 43 | #include "libguile/srfi-13.h" |
| 44 | |
| 45 | #include "libguile/validate.h" |
| 46 | #include "libguile/load.h" |
| 47 | #include "libguile/fluids.h" |
| 48 | |
| 49 | #include "libguile/vm.h" /* for load-compiled/vm */ |
| 50 | |
| 51 | #include <sys/types.h> |
| 52 | #include <sys/stat.h> |
| 53 | #include <unistd.h> |
| 54 | |
| 55 | #ifdef HAVE_PWD_H |
| 56 | #include <pwd.h> |
| 57 | #endif /* HAVE_PWD_H */ |
| 58 | |
| 59 | #ifndef R_OK |
| 60 | #define R_OK 4 |
| 61 | #endif |
| 62 | |
| 63 | #include <stat-time.h> |
| 64 | |
| 65 | \f |
| 66 | /* Loading a file, given an absolute filename. */ |
| 67 | |
| 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 | |
| 72 | /* The current reader (a fluid). */ |
| 73 | static SCM the_reader = SCM_BOOL_F; |
| 74 | |
| 75 | |
| 76 | SCM_DEFINE (scm_primitive_load, "primitive-load", 1, 0, 0, |
| 77 | (SCM filename), |
| 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.") |
| 85 | #define FUNC_NAME s_scm_primitive_load |
| 86 | { |
| 87 | SCM hook = *scm_loc_load_hook; |
| 88 | SCM ret = SCM_UNSPECIFIED; |
| 89 | |
| 90 | SCM_VALIDATE_STRING (1, filename); |
| 91 | if (scm_is_true (hook) && scm_is_false (scm_procedure_p (hook))) |
| 92 | SCM_MISC_ERROR ("value of %load-hook is neither a procedure nor #f", |
| 93 | SCM_EOL); |
| 94 | |
| 95 | if (!scm_is_false (hook)) |
| 96 | scm_call_1 (hook, filename); |
| 97 | |
| 98 | { |
| 99 | SCM port; |
| 100 | |
| 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 | |
| 106 | scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE); |
| 107 | scm_i_dynwind_current_load_port (port); |
| 108 | |
| 109 | while (1) |
| 110 | { |
| 111 | SCM reader, form; |
| 112 | |
| 113 | /* Lookup and use the current reader to read the next |
| 114 | expression. */ |
| 115 | reader = scm_fluid_ref (the_reader); |
| 116 | if (scm_is_false (reader)) |
| 117 | form = scm_read (port); |
| 118 | else |
| 119 | form = scm_call_1 (reader, port); |
| 120 | |
| 121 | if (SCM_EOF_OBJECT_P (form)) |
| 122 | break; |
| 123 | |
| 124 | ret = scm_primitive_eval_x (form); |
| 125 | } |
| 126 | |
| 127 | scm_dynwind_end (); |
| 128 | scm_close_port (port); |
| 129 | } |
| 130 | return ret; |
| 131 | } |
| 132 | #undef FUNC_NAME |
| 133 | |
| 134 | SCM |
| 135 | scm_c_primitive_load (const char *filename) |
| 136 | { |
| 137 | return scm_primitive_load (scm_from_locale_string (filename)); |
| 138 | } |
| 139 | |
| 140 | \f |
| 141 | /* Builtin path to scheme library files. */ |
| 142 | #ifdef SCM_PKGDATA_DIR |
| 143 | SCM_DEFINE (scm_sys_package_data_dir, "%package-data-dir", 0, 0, 0, |
| 144 | (), |
| 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}.") |
| 148 | #define FUNC_NAME s_scm_sys_package_data_dir |
| 149 | { |
| 150 | return scm_from_locale_string (SCM_PKGDATA_DIR); |
| 151 | } |
| 152 | #undef FUNC_NAME |
| 153 | #endif /* SCM_PKGDATA_DIR */ |
| 154 | |
| 155 | #ifdef SCM_LIBRARY_DIR |
| 156 | SCM_DEFINE (scm_sys_library_dir, "%library-dir", 0,0,0, |
| 157 | (), |
| 158 | "Return the directory where the Guile Scheme library files are installed.\n" |
| 159 | "E.g., may return \"/usr/share/guile/1.3.5\".") |
| 160 | #define FUNC_NAME s_scm_sys_library_dir |
| 161 | { |
| 162 | return scm_from_locale_string (SCM_LIBRARY_DIR); |
| 163 | } |
| 164 | #undef FUNC_NAME |
| 165 | #endif /* SCM_LIBRARY_DIR */ |
| 166 | |
| 167 | #ifdef SCM_SITE_DIR |
| 168 | SCM_DEFINE (scm_sys_site_dir, "%site-dir", 0,0,0, |
| 169 | (), |
| 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 "\".") |
| 173 | #define FUNC_NAME s_scm_sys_site_dir |
| 174 | { |
| 175 | return scm_from_locale_string (SCM_SITE_DIR); |
| 176 | } |
| 177 | #undef FUNC_NAME |
| 178 | #endif /* SCM_SITE_DIR */ |
| 179 | |
| 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 */ |
| 192 | |
| 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 | |
| 206 | |
| 207 | \f |
| 208 | /* Initializing the load path, and searching it. */ |
| 209 | |
| 210 | /* List of names of directories we search for files to load. */ |
| 211 | static SCM *scm_loc_load_path; |
| 212 | |
| 213 | /* List of extensions we try adding to the filenames. */ |
| 214 | static SCM *scm_loc_load_extensions; |
| 215 | |
| 216 | /* Like %load-path and %load-extensions, but for compiled files. */ |
| 217 | static SCM *scm_loc_load_compiled_path; |
| 218 | static SCM *scm_loc_load_compiled_extensions; |
| 219 | |
| 220 | /* Whether we should try to auto-compile. */ |
| 221 | static SCM *scm_loc_load_should_auto_compile; |
| 222 | |
| 223 | /* Whether to treat all auto-compiled files as stale. */ |
| 224 | static SCM *scm_loc_fresh_auto_compile; |
| 225 | |
| 226 | /* The fallback path for auto-compilation */ |
| 227 | static SCM *scm_loc_compile_fallback_path; |
| 228 | |
| 229 | /* Ellipsis: "..." */ |
| 230 | static SCM scm_ellipsis; |
| 231 | |
| 232 | SCM_DEFINE (scm_parse_path, "parse-path", 1, 1, 0, |
| 233 | (SCM path, SCM tail), |
| 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.") |
| 238 | #define FUNC_NAME s_scm_parse_path |
| 239 | { |
| 240 | #ifdef __MINGW32__ |
| 241 | SCM sep = SCM_MAKE_CHAR (';'); |
| 242 | #else |
| 243 | SCM sep = SCM_MAKE_CHAR (':'); |
| 244 | #endif |
| 245 | |
| 246 | if (SCM_UNBNDP (tail)) |
| 247 | tail = SCM_EOL; |
| 248 | return (scm_is_false (path) |
| 249 | ? tail |
| 250 | : scm_append_x (scm_list_2 (scm_string_split (path, sep), tail))); |
| 251 | } |
| 252 | #undef FUNC_NAME |
| 253 | |
| 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 | |
| 280 | |
| 281 | /* Initialize the global variable %load-path, given the value of the |
| 282 | SCM_SITE_DIR and SCM_LIBRARY_DIR preprocessor symbols and the |
| 283 | GUILE_LOAD_PATH environment variable. */ |
| 284 | void |
| 285 | scm_init_load_path () |
| 286 | { |
| 287 | char *env; |
| 288 | SCM path = SCM_EOL; |
| 289 | SCM cpath = SCM_EOL; |
| 290 | |
| 291 | #ifdef SCM_LIBRARY_DIR |
| 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 |
| 300 | path = scm_list_4 (scm_from_locale_string (SCM_LIBRARY_DIR), |
| 301 | scm_from_locale_string (SCM_SITE_DIR), |
| 302 | scm_from_locale_string (SCM_GLOBAL_SITE_DIR), |
| 303 | scm_from_locale_string (SCM_PKGDATA_DIR)); |
| 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 |
| 312 | { |
| 313 | cpath = scm_list_2 (scm_from_locale_string (SCM_CCACHE_DIR), |
| 314 | scm_from_locale_string (SCM_SITE_CCACHE_DIR)); |
| 315 | } |
| 316 | |
| 317 | #endif /* SCM_LIBRARY_DIR */ |
| 318 | |
| 319 | { |
| 320 | char cachedir[1024]; |
| 321 | char *e; |
| 322 | #ifdef HAVE_GETPWENT |
| 323 | struct passwd *pwd; |
| 324 | #endif |
| 325 | |
| 326 | #define FALLBACK_DIR \ |
| 327 | "guile/ccache/" SCM_EFFECTIVE_VERSION "-" SCM_OBJCODE_MACHINE_VERSION_STRING |
| 328 | |
| 329 | if ((e = getenv ("XDG_CACHE_HOME"))) |
| 330 | snprintf (cachedir, sizeof(cachedir), "%s/" FALLBACK_DIR, e); |
| 331 | else if ((e = getenv ("HOME"))) |
| 332 | snprintf (cachedir, sizeof(cachedir), "%s/.cache/" FALLBACK_DIR, e); |
| 333 | #ifdef HAVE_GETPWENT |
| 334 | else if ((pwd = getpwuid (getuid ())) && pwd->pw_dir) |
| 335 | snprintf (cachedir, sizeof(cachedir), "%s/.cache/" FALLBACK_DIR, |
| 336 | pwd->pw_dir); |
| 337 | #endif /* HAVE_GETPWENT */ |
| 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__ */ |
| 344 | else |
| 345 | cachedir[0] = 0; |
| 346 | |
| 347 | if (cachedir[0]) |
| 348 | *scm_loc_compile_fallback_path = scm_from_locale_string (cachedir); |
| 349 | } |
| 350 | |
| 351 | env = getenv ("GUILE_LOAD_PATH"); |
| 352 | if (env) |
| 353 | path = scm_parse_path_with_ellipsis (scm_from_locale_string (env), path); |
| 354 | |
| 355 | env = getenv ("GUILE_LOAD_COMPILED_PATH"); |
| 356 | if (env) |
| 357 | cpath = scm_parse_path_with_ellipsis (scm_from_locale_string (env), cpath); |
| 358 | |
| 359 | *scm_loc_load_path = path; |
| 360 | *scm_loc_load_compiled_path = cpath; |
| 361 | } |
| 362 | |
| 363 | SCM scm_listofnullstr; |
| 364 | |
| 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; |
| 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 | |
| 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 | |
| 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 | { |
| 464 | if (scm_is_eq (c, SCM_MAKE_CHAR ('/'))) |
| 465 | return 1; |
| 466 | #ifdef __MINGW32__ |
| 467 | if (scm_is_eq (c, SCM_MAKE_CHAR ('\\'))) |
| 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 |
| 486 | is_absolute_file_name (SCM filename) |
| 487 | { |
| 488 | size_t filename_len = scm_c_string_length (filename); |
| 489 | |
| 490 | if (filename_len >= 1 |
| 491 | && is_file_name_separator (scm_c_string_ref (filename, 0)) |
| 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 |
| 497 | && is_file_name_separator (scm_c_string_ref (filename, 1)) |
| 498 | #endif |
| 499 | ) |
| 500 | return 1; |
| 501 | if (filename_len >= 3 |
| 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))) |
| 505 | return 1; |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | /* Search PATH for a directory containing a file named FILENAME. |
| 510 | The file must be readable, and not a directory. |
| 511 | If we find one, return its full pathname; otherwise, return #f. |
| 512 | If FILENAME is absolute, return it unchanged. |
| 513 | We also fill *stat_buf corresponding to the returned pathname. |
| 514 | If given, EXTENSIONS is a list of strings; for each directory |
| 515 | in PATH, we search for FILENAME concatenated with each EXTENSION. */ |
| 516 | static SCM |
| 517 | search_path (SCM path, SCM filename, SCM extensions, SCM require_exts, |
| 518 | struct stat *stat_buf) |
| 519 | { |
| 520 | struct stringbuf buf; |
| 521 | char *filename_chars; |
| 522 | size_t filename_len; |
| 523 | SCM result = SCM_BOOL_F; |
| 524 | |
| 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)); |
| 531 | |
| 532 | scm_dynwind_begin (0); |
| 533 | |
| 534 | filename_chars = scm_to_locale_string (filename); |
| 535 | filename_len = strlen (filename_chars); |
| 536 | scm_dynwind_free (filename_chars); |
| 537 | |
| 538 | /* If FILENAME is absolute and is still valid, return it unchanged. */ |
| 539 | if (is_absolute_file_name (filename)) |
| 540 | { |
| 541 | if ((scm_is_false (require_exts) || |
| 542 | scm_c_string_has_an_ext (filename_chars, filename_len, |
| 543 | extensions)) |
| 544 | && stat (filename_chars, stat_buf) == 0 |
| 545 | && !(stat_buf->st_mode & S_IFDIR)) |
| 546 | result = filename; |
| 547 | goto end; |
| 548 | } |
| 549 | |
| 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 | { |
| 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... */ |
| 566 | goto end; |
| 567 | } |
| 568 | /* This filename already has an extension, so cancel the |
| 569 | list of extensions. */ |
| 570 | extensions = SCM_EOL; |
| 571 | break; |
| 572 | } |
| 573 | else if (is_file_name_separator (SCM_MAKE_CHAR (*endp))) |
| 574 | /* This filename has no extension, so keep the current list |
| 575 | of extensions. */ |
| 576 | break; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | /* This simplifies the loop below a bit. |
| 581 | */ |
| 582 | if (scm_is_null (extensions)) |
| 583 | extensions = scm_listofnullstr; |
| 584 | |
| 585 | buf.buf_len = 512; |
| 586 | buf.buf = scm_malloc (buf.buf_len); |
| 587 | scm_dynwind_unwind_handler (stringbuf_free, &buf, SCM_F_WIND_EXPLICITLY); |
| 588 | |
| 589 | /* Try every path element. |
| 590 | */ |
| 591 | for (; scm_is_pair (path); path = SCM_CDR (path)) |
| 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 | |
| 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); |
| 605 | |
| 606 | stringbuf_cat (&buf, filename_chars); |
| 607 | sans_ext_len = buf.ptr - buf.buf; |
| 608 | |
| 609 | /* Try every extension. */ |
| 610 | for (exts = extensions; scm_is_pair (exts); exts = SCM_CDR (exts)) |
| 611 | { |
| 612 | SCM ext = SCM_CAR (exts); |
| 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 | |
| 620 | if (stat (buf.buf, stat_buf) == 0 |
| 621 | && ! (stat_buf->st_mode & S_IFDIR)) |
| 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 | } |
| 631 | |
| 632 | if (!SCM_NULL_OR_NIL_P (path)) |
| 633 | scm_wrong_type_arg_msg (NULL, 0, path, "proper list"); |
| 634 | |
| 635 | end: |
| 636 | scm_dynwind_end (); |
| 637 | return result; |
| 638 | } |
| 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" |
| 646 | "If given, @var{rest} is a list of extension strings; for each\n" |
| 647 | "directory in @var{path}, we search for @var{filename}\n" |
| 648 | "concatenated with each extension.") |
| 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 | } |
| 695 | #undef FUNC_NAME |
| 696 | |
| 697 | |
| 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. */ |
| 702 | SCM_DEFINE (scm_sys_search_load_path, "%search-load-path", 1, 0, 0, |
| 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.") |
| 711 | #define FUNC_NAME s_scm_sys_search_load_path |
| 712 | { |
| 713 | struct stat stat_buf; |
| 714 | |
| 715 | SCM_VALIDATE_STRING (1, filename); |
| 716 | |
| 717 | return search_path (*scm_loc_load_path, filename, *scm_loc_load_extensions, |
| 718 | SCM_BOOL_F, &stat_buf); |
| 719 | } |
| 720 | #undef FUNC_NAME |
| 721 | |
| 722 | |
| 723 | /* Return true if COMPILED_FILENAME is newer than source file |
| 724 | FULL_FILENAME, false otherwise. */ |
| 725 | static int |
| 726 | compiled_is_fresh (SCM full_filename, SCM compiled_filename, |
| 727 | struct stat *stat_source, struct stat *stat_compiled) |
| 728 | { |
| 729 | int compiled_is_newer; |
| 730 | struct timespec source_mtime, compiled_mtime; |
| 731 | |
| 732 | source_mtime = get_stat_mtime (stat_source); |
| 733 | compiled_mtime = get_stat_mtime (stat_compiled); |
| 734 | |
| 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 |
| 740 | { |
| 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 ()); |
| 747 | } |
| 748 | |
| 749 | return compiled_is_newer; |
| 750 | } |
| 751 | |
| 752 | SCM_KEYWORD (kw_env, "env"); |
| 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"); |
| 757 | |
| 758 | static SCM |
| 759 | do_try_auto_compile (void *data) |
| 760 | { |
| 761 | SCM source = PTR2SCM (data); |
| 762 | SCM comp_mod, compile_file; |
| 763 | |
| 764 | scm_puts (";;; compiling ", scm_current_error_port ()); |
| 765 | scm_display (source, scm_current_error_port ()); |
| 766 | scm_newline (scm_current_error_port ()); |
| 767 | |
| 768 | comp_mod = scm_c_resolve_module ("system base compile"); |
| 769 | compile_file = scm_module_variable (comp_mod, sym_compile_file); |
| 770 | |
| 771 | if (scm_is_true (compile_file)) |
| 772 | { |
| 773 | /* Auto-compile in the context of the current module. */ |
| 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 | |
| 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 ()); |
| 802 | scm_puts ("\n;;; is part of the compiler; skipping auto-compilation\n", |
| 803 | scm_current_error_port ()); |
| 804 | return SCM_BOOL_F; |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | static SCM |
| 809 | auto_compile_catch_handler (void *data, SCM tag, SCM throw_args) |
| 810 | { |
| 811 | SCM source = PTR2SCM (data); |
| 812 | SCM oport, lines; |
| 813 | |
| 814 | oport = scm_open_output_string (); |
| 815 | scm_print_exception (oport, SCM_BOOL_F, tag, throw_args); |
| 816 | |
| 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 ()); |
| 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 | { |
| 826 | scm_puts (";;; ", scm_current_warning_port ()); |
| 827 | scm_display (scm_car (lines), scm_current_warning_port ()); |
| 828 | scm_newline (scm_current_warning_port ()); |
| 829 | } |
| 830 | |
| 831 | scm_close_port (oport); |
| 832 | |
| 833 | return SCM_BOOL_F; |
| 834 | } |
| 835 | |
| 836 | SCM_DEFINE (scm_sys_warn_auto_compilation_enabled, "%warn-auto-compilation-enabled", 0, 0, 0, |
| 837 | (void), "") |
| 838 | #define FUNC_NAME s_scm_sys_warn_auto_compilation_enabled |
| 839 | { |
| 840 | static int message_shown = 0; |
| 841 | |
| 842 | if (!message_shown) |
| 843 | { |
| 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", |
| 846 | scm_current_warning_port ()); |
| 847 | message_shown = 1; |
| 848 | } |
| 849 | |
| 850 | return SCM_UNSPECIFIED; |
| 851 | } |
| 852 | #undef FUNC_NAME |
| 853 | |
| 854 | static SCM |
| 855 | scm_try_auto_compile (SCM source) |
| 856 | { |
| 857 | if (scm_is_false (*scm_loc_load_should_auto_compile)) |
| 858 | return SCM_BOOL_F; |
| 859 | |
| 860 | scm_sys_warn_auto_compilation_enabled (); |
| 861 | return scm_c_catch (SCM_BOOL_T, |
| 862 | do_try_auto_compile, |
| 863 | SCM2PTR (source), |
| 864 | auto_compile_catch_handler, |
| 865 | SCM2PTR (source), |
| 866 | NULL, NULL); |
| 867 | } |
| 868 | |
| 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. */ |
| 873 | static SCM |
| 874 | canonical_suffix (SCM fname) |
| 875 | { |
| 876 | SCM canon; |
| 877 | |
| 878 | /* CANON should be absolute. */ |
| 879 | canon = scm_canonicalize_path (fname); |
| 880 | |
| 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; |
| 899 | } |
| 900 | |
| 901 | SCM_DEFINE (scm_primitive_load_path, "primitive-load-path", 0, 0, 1, |
| 902 | (SCM args), |
| 903 | "Search @var{%load-path} for the file named @var{filename} and\n" |
| 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.") |
| 911 | #define FUNC_NAME s_scm_primitive_load_path |
| 912 | { |
| 913 | SCM filename, exception_on_not_found; |
| 914 | SCM full_filename, compiled_filename; |
| 915 | int compiled_is_fallback = 0; |
| 916 | SCM hook = *scm_loc_load_hook; |
| 917 | struct stat stat_source, stat_compiled; |
| 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); |
| 922 | |
| 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 | |
| 946 | if (SCM_UNBNDP (exception_on_not_found)) |
| 947 | exception_on_not_found = SCM_BOOL_T; |
| 948 | |
| 949 | full_filename = search_path (*scm_loc_load_path, filename, |
| 950 | *scm_loc_load_extensions, SCM_BOOL_F, |
| 951 | &stat_source); |
| 952 | |
| 953 | compiled_filename = |
| 954 | search_path (*scm_loc_load_compiled_path, filename, |
| 955 | *scm_loc_load_compiled_extensions, SCM_BOOL_T, |
| 956 | &stat_compiled); |
| 957 | |
| 958 | if (scm_is_false (compiled_filename) |
| 959 | && scm_is_true (full_filename) |
| 960 | && scm_is_true (*scm_loc_compile_fallback_path) |
| 961 | && scm_is_false (*scm_loc_fresh_auto_compile) |
| 962 | && scm_is_pair (*scm_loc_load_compiled_extensions) |
| 963 | && scm_is_string (scm_car (*scm_loc_load_compiled_extensions))) |
| 964 | { |
| 965 | SCM fallback; |
| 966 | char *fallback_chars; |
| 967 | |
| 968 | fallback = scm_string_append |
| 969 | (scm_list_3 (*scm_loc_compile_fallback_path, |
| 970 | canonical_suffix (full_filename), |
| 971 | scm_car (*scm_loc_load_compiled_extensions))); |
| 972 | |
| 973 | fallback_chars = scm_to_locale_string (fallback); |
| 974 | if (stat (fallback_chars, &stat_compiled) == 0) |
| 975 | { |
| 976 | compiled_filename = fallback; |
| 977 | compiled_is_fallback = 1; |
| 978 | } |
| 979 | free (fallback_chars); |
| 980 | } |
| 981 | |
| 982 | if (scm_is_false (full_filename) && scm_is_false (compiled_filename)) |
| 983 | { |
| 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 |
| 989 | SCM_MISC_ERROR ("Unable to find file ~S in load path", |
| 990 | scm_list_1 (filename)); |
| 991 | } |
| 992 | |
| 993 | if (!scm_is_false (hook)) |
| 994 | scm_call_1 (hook, (scm_is_true (full_filename) |
| 995 | ? full_filename : compiled_filename)); |
| 996 | |
| 997 | if (scm_is_false (full_filename) |
| 998 | || (scm_is_true (compiled_filename) |
| 999 | && compiled_is_fresh (full_filename, compiled_filename, |
| 1000 | &stat_source, &stat_compiled))) |
| 1001 | return scm_load_compiled_with_vm (compiled_filename); |
| 1002 | |
| 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) |
| 1008 | && scm_is_false (*scm_loc_fresh_auto_compile) |
| 1009 | && scm_is_pair (*scm_loc_load_compiled_extensions) |
| 1010 | && scm_is_string (scm_car (*scm_loc_load_compiled_extensions))) |
| 1011 | { |
| 1012 | SCM fallback; |
| 1013 | char *fallback_chars; |
| 1014 | int stat_ret; |
| 1015 | |
| 1016 | fallback = scm_string_append |
| 1017 | (scm_list_3 (*scm_loc_compile_fallback_path, |
| 1018 | canonical_suffix (full_filename), |
| 1019 | scm_car (*scm_loc_load_compiled_extensions))); |
| 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)) |
| 1027 | { |
| 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 ()); |
| 1031 | return scm_load_compiled_with_vm (fallback); |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | /* Otherwise, we bottom out here. */ |
| 1036 | { |
| 1037 | SCM freshly_compiled = scm_try_auto_compile (full_filename); |
| 1038 | |
| 1039 | if (scm_is_true (freshly_compiled)) |
| 1040 | return scm_load_compiled_with_vm (freshly_compiled); |
| 1041 | else |
| 1042 | return scm_primitive_load (full_filename); |
| 1043 | } |
| 1044 | } |
| 1045 | #undef FUNC_NAME |
| 1046 | |
| 1047 | SCM |
| 1048 | scm_c_primitive_load_path (const char *filename) |
| 1049 | { |
| 1050 | return scm_primitive_load_path (scm_from_locale_string (filename)); |
| 1051 | } |
| 1052 | |
| 1053 | void |
| 1054 | scm_init_eval_in_scheme (void) |
| 1055 | { |
| 1056 | SCM eval_scm, eval_go; |
| 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); |
| 1065 | |
| 1066 | if (scm_is_true (eval_scm) && scm_is_true (eval_go) |
| 1067 | && compiled_is_fresh (eval_scm, eval_go, |
| 1068 | &stat_source, &stat_compiled)) |
| 1069 | scm_load_compiled_with_vm (eval_go); |
| 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; |
| 1073 | } |
| 1074 | |
| 1075 | \f |
| 1076 | /* Information about the build environment. */ |
| 1077 | |
| 1078 | SCM_VARIABLE_INIT (sys_host_type, "%host-type", |
| 1079 | scm_from_locale_string (HOST_TYPE)); |
| 1080 | |
| 1081 | |
| 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; |
| 1088 | SCM *loc = SCM_VARIABLE_LOC (scm_c_define ("%guile-build-info", SCM_EOL)); |
| 1089 | unsigned long i; |
| 1090 | |
| 1091 | for (i = 0; i < (sizeof (info) / sizeof (info[0])); i++) |
| 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 | } |
| 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 |
| 1112 | } |
| 1113 | |
| 1114 | \f |
| 1115 | void |
| 1116 | scm_init_load () |
| 1117 | { |
| 1118 | scm_listofnullstr = scm_list_1 (scm_nullstr); |
| 1119 | scm_loc_load_path = SCM_VARIABLE_LOC (scm_c_define ("%load-path", SCM_EOL)); |
| 1120 | scm_loc_load_extensions |
| 1121 | = SCM_VARIABLE_LOC (scm_c_define ("%load-extensions", |
| 1122 | scm_list_2 (scm_from_locale_string (".scm"), |
| 1123 | scm_nullstr))); |
| 1124 | scm_loc_load_compiled_path |
| 1125 | = SCM_VARIABLE_LOC (scm_c_define ("%load-compiled-path", SCM_EOL)); |
| 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")))); |
| 1129 | scm_loc_load_hook = SCM_VARIABLE_LOC (scm_c_define ("%load-hook", SCM_BOOL_F)); |
| 1130 | |
| 1131 | scm_loc_compile_fallback_path |
| 1132 | = SCM_VARIABLE_LOC (scm_c_define ("%compile-fallback-path", SCM_BOOL_F)); |
| 1133 | scm_loc_load_should_auto_compile |
| 1134 | = SCM_VARIABLE_LOC (scm_c_define ("%load-should-auto-compile", SCM_BOOL_F)); |
| 1135 | scm_loc_fresh_auto_compile |
| 1136 | = SCM_VARIABLE_LOC (scm_c_define ("%fresh-auto-compile", SCM_BOOL_F)); |
| 1137 | |
| 1138 | scm_ellipsis = scm_from_latin1_string ("..."); |
| 1139 | |
| 1140 | the_reader = scm_make_fluid_with_default (SCM_BOOL_F); |
| 1141 | scm_c_define("current-reader", the_reader); |
| 1142 | |
| 1143 | scm_c_define ("load-compiled", |
| 1144 | scm_c_make_gsubr ("load-compiled/vm", 1, 0, 0, |
| 1145 | scm_load_compiled_with_vm)); |
| 1146 | |
| 1147 | init_build_info (); |
| 1148 | |
| 1149 | #include "libguile/load.x" |
| 1150 | } |
| 1151 | |
| 1152 | void |
| 1153 | scm_init_load_should_auto_compile () |
| 1154 | { |
| 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 | } |
| 1173 | } |
| 1174 | |
| 1175 | |
| 1176 | |
| 1177 | /* |
| 1178 | Local Variables: |
| 1179 | c-file-style: "gnu" |
| 1180 | End: |
| 1181 | */ |