load.c 42 KB

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