load.c 42 KB

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