load.c 42 KB

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