load.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2004, 2006, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <string.h>
  22. #include <stdio.h>
  23. #include "libguile/_scm.h"
  24. #include "libguile/private-gc.h" /* scm_getenv_int */
  25. #include "libguile/libpath.h"
  26. #include "libguile/fports.h"
  27. #include "libguile/read.h"
  28. #include "libguile/eval.h"
  29. #include "libguile/throw.h"
  30. #include "libguile/alist.h"
  31. #include "libguile/dynwind.h"
  32. #include "libguile/root.h"
  33. #include "libguile/strings.h"
  34. #include "libguile/modules.h"
  35. #include "libguile/chars.h"
  36. #include "libguile/srfi-13.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. #ifdef HAVE_UNISTD_H
  44. #include <unistd.h>
  45. #endif /* HAVE_UNISTD_H */
  46. #ifdef HAVE_PWD_H
  47. #include <pwd.h>
  48. #endif /* HAVE_PWD_H */
  49. #ifndef R_OK
  50. #define R_OK 4
  51. #endif
  52. #include <stat-time.h>
  53. /* Loading a file, given an absolute filename. */
  54. /* Hook to run when we load a file, perhaps to announce the fact somewhere.
  55. Applied to the full name of the file. */
  56. static SCM *scm_loc_load_hook;
  57. /* The current reader (a fluid). */
  58. static SCM the_reader = SCM_BOOL_F;
  59. SCM_DEFINE (scm_primitive_load, "primitive-load", 1, 0, 0,
  60. (SCM filename),
  61. "Load the file named @var{filename} and evaluate its contents in\n"
  62. "the top-level environment. The load paths are not searched;\n"
  63. "@var{filename} must either be a full pathname or be a pathname\n"
  64. "relative to the current directory. If the variable\n"
  65. "@code{%load-hook} is defined, it should be bound to a procedure\n"
  66. "that will be called before any code is loaded. See the\n"
  67. "documentation for @code{%load-hook} later in this section.")
  68. #define FUNC_NAME s_scm_primitive_load
  69. {
  70. SCM hook = *scm_loc_load_hook;
  71. char *encoding;
  72. SCM_VALIDATE_STRING (1, filename);
  73. if (scm_is_true (hook) && scm_is_false (scm_procedure_p (hook)))
  74. SCM_MISC_ERROR ("value of %load-hook is neither a procedure nor #f",
  75. SCM_EOL);
  76. if (!scm_is_false (hook))
  77. scm_call_1 (hook, filename);
  78. { /* scope */
  79. SCM port = scm_open_file (filename, scm_from_locale_string ("r"));
  80. scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
  81. scm_i_dynwind_current_load_port (port);
  82. encoding = scm_i_scan_for_encoding (port);
  83. if (encoding)
  84. scm_i_set_port_encoding_x (port, encoding);
  85. else
  86. /* The file has no encoding declared. We'll presume UTF-8, like
  87. compile-file does. */
  88. scm_i_set_port_encoding_x (port, "UTF-8");
  89. while (1)
  90. {
  91. SCM reader, form;
  92. /* Lookup and use the current reader to read the next
  93. expression. */
  94. reader = scm_fluid_ref (the_reader);
  95. if (scm_is_false (reader))
  96. form = scm_read (port);
  97. else
  98. form = scm_call_1 (reader, port);
  99. if (SCM_EOF_OBJECT_P (form))
  100. break;
  101. scm_primitive_eval_x (form);
  102. }
  103. scm_dynwind_end ();
  104. scm_close_port (port);
  105. }
  106. return SCM_UNSPECIFIED;
  107. }
  108. #undef FUNC_NAME
  109. SCM
  110. scm_c_primitive_load (const char *filename)
  111. {
  112. return scm_primitive_load (scm_from_locale_string (filename));
  113. }
  114. /* Builtin path to scheme library files. */
  115. #ifdef SCM_PKGDATA_DIR
  116. SCM_DEFINE (scm_sys_package_data_dir, "%package-data-dir", 0, 0, 0,
  117. (),
  118. "Return the name of the directory where Scheme packages, modules and\n"
  119. "libraries are kept. On most Unix systems, this will be\n"
  120. "@samp{/usr/local/share/guile}.")
  121. #define FUNC_NAME s_scm_sys_package_data_dir
  122. {
  123. return scm_from_locale_string (SCM_PKGDATA_DIR);
  124. }
  125. #undef FUNC_NAME
  126. #endif /* SCM_PKGDATA_DIR */
  127. #ifdef SCM_LIBRARY_DIR
  128. SCM_DEFINE (scm_sys_library_dir, "%library-dir", 0,0,0,
  129. (),
  130. "Return the directory where the Guile Scheme library files are installed.\n"
  131. "E.g., may return \"/usr/share/guile/1.3.5\".")
  132. #define FUNC_NAME s_scm_sys_library_dir
  133. {
  134. return scm_from_locale_string (SCM_LIBRARY_DIR);
  135. }
  136. #undef FUNC_NAME
  137. #endif /* SCM_LIBRARY_DIR */
  138. #ifdef SCM_SITE_DIR
  139. SCM_DEFINE (scm_sys_site_dir, "%site-dir", 0,0,0,
  140. (),
  141. "Return the directory where users should install Scheme code for use\n"
  142. "with this version of Guile.\n\n"
  143. "E.g., may return \"/usr/share/guile/site/" SCM_EFFECTIVE_VERSION "\".")
  144. #define FUNC_NAME s_scm_sys_site_dir
  145. {
  146. return scm_from_locale_string (SCM_SITE_DIR);
  147. }
  148. #undef FUNC_NAME
  149. #endif /* SCM_SITE_DIR */
  150. #ifdef SCM_GLOBAL_SITE_DIR
  151. SCM_DEFINE (scm_sys_global_site_dir, "%global-site-dir", 0,0,0,
  152. (),
  153. "Return the directory where users should install Scheme code for use\n"
  154. "with all versions of Guile.\n\n"
  155. "E.g., may return \"/usr/share/guile/site\".")
  156. #define FUNC_NAME s_scm_sys_global_site_dir
  157. {
  158. return scm_from_locale_string (SCM_GLOBAL_SITE_DIR);
  159. }
  160. #undef FUNC_NAME
  161. #endif /* SCM_GLOBAL_SITE_DIR */
  162. /* Initializing the load path, and searching it. */
  163. /* List of names of directories we search for files to load. */
  164. static SCM *scm_loc_load_path;
  165. /* List of extensions we try adding to the filenames. */
  166. static SCM *scm_loc_load_extensions;
  167. /* Like %load-path and %load-extensions, but for compiled files. */
  168. static SCM *scm_loc_load_compiled_path;
  169. static SCM *scm_loc_load_compiled_extensions;
  170. /* Whether we should try to auto-compile. */
  171. static SCM *scm_loc_load_should_auto_compile;
  172. /* Whether to treat all auto-compiled files as stale. */
  173. static SCM *scm_loc_fresh_auto_compile;
  174. /* The fallback path for auto-compilation */
  175. static SCM *scm_loc_compile_fallback_path;
  176. SCM_DEFINE (scm_parse_path, "parse-path", 1, 1, 0,
  177. (SCM path, SCM tail),
  178. "Parse @var{path}, which is expected to be a colon-separated\n"
  179. "string, into a list and return the resulting list with\n"
  180. "@var{tail} appended. If @var{path} is @code{#f}, @var{tail}\n"
  181. "is returned.")
  182. #define FUNC_NAME s_scm_parse_path
  183. {
  184. #ifdef __MINGW32__
  185. SCM sep = SCM_MAKE_CHAR (';');
  186. #else
  187. SCM sep = SCM_MAKE_CHAR (':');
  188. #endif
  189. if (SCM_UNBNDP (tail))
  190. tail = SCM_EOL;
  191. return (scm_is_false (path)
  192. ? tail
  193. : scm_append_x (scm_list_2 (scm_string_split (path, sep), tail)));
  194. }
  195. #undef FUNC_NAME
  196. /* Initialize the global variable %load-path, given the value of the
  197. SCM_SITE_DIR and SCM_LIBRARY_DIR preprocessor symbols and the
  198. GUILE_LOAD_PATH environment variable. */
  199. void
  200. scm_init_load_path ()
  201. {
  202. char *env;
  203. SCM path = SCM_EOL;
  204. SCM cpath = SCM_EOL;
  205. #ifdef SCM_LIBRARY_DIR
  206. env = getenv ("GUILE_SYSTEM_PATH");
  207. if (env && strcmp (env, "") == 0)
  208. /* special-case interpret system-path=="" as meaning no system path instead
  209. of '("") */
  210. ;
  211. else if (env)
  212. path = scm_parse_path (scm_from_locale_string (env), path);
  213. else
  214. path = scm_list_4 (scm_from_locale_string (SCM_LIBRARY_DIR),
  215. scm_from_locale_string (SCM_SITE_DIR),
  216. scm_from_locale_string (SCM_GLOBAL_SITE_DIR),
  217. scm_from_locale_string (SCM_PKGDATA_DIR));
  218. env = getenv ("GUILE_SYSTEM_COMPILED_PATH");
  219. if (env && strcmp (env, "") == 0)
  220. /* like above */
  221. ;
  222. else if (env)
  223. cpath = scm_parse_path (scm_from_locale_string (env), cpath);
  224. else
  225. cpath = scm_cons (scm_from_locale_string (SCM_CCACHE_DIR), cpath);
  226. #endif /* SCM_LIBRARY_DIR */
  227. {
  228. char cachedir[1024];
  229. char *e;
  230. #ifdef HAVE_GETPWENT
  231. struct passwd *pwd;
  232. #endif
  233. #define FALLBACK_DIR \
  234. "guile/ccache/" SCM_EFFECTIVE_VERSION "-" SCM_OBJCODE_MACHINE_VERSION_STRING
  235. if ((e = getenv ("XDG_CACHE_HOME")))
  236. snprintf (cachedir, sizeof(cachedir), "%s/" FALLBACK_DIR, e);
  237. else if ((e = getenv ("HOME")))
  238. snprintf (cachedir, sizeof(cachedir), "%s/.cache/" FALLBACK_DIR, e);
  239. #ifdef HAVE_GETPWENT
  240. else if ((pwd = getpwuid (getuid ())) && pwd->pw_dir)
  241. snprintf (cachedir, sizeof(cachedir), "%s/.cache/" FALLBACK_DIR,
  242. pwd->pw_dir);
  243. #endif /* HAVE_GETPWENT */
  244. else
  245. cachedir[0] = 0;
  246. if (cachedir[0])
  247. *scm_loc_compile_fallback_path = scm_from_locale_string (cachedir);
  248. }
  249. env = getenv ("GUILE_LOAD_PATH");
  250. if (env)
  251. path = scm_parse_path (scm_from_locale_string (env), path);
  252. env = getenv ("GUILE_LOAD_COMPILED_PATH");
  253. if (env)
  254. cpath = scm_parse_path (scm_from_locale_string (env), cpath);
  255. *scm_loc_load_path = path;
  256. *scm_loc_load_compiled_path = cpath;
  257. }
  258. SCM scm_listofnullstr;
  259. /* Utility functions for assembling C strings in a buffer.
  260. */
  261. struct stringbuf {
  262. char *buf, *ptr;
  263. size_t buf_len;
  264. };
  265. static void
  266. stringbuf_free (void *data)
  267. {
  268. struct stringbuf *buf = (struct stringbuf *)data;
  269. free (buf->buf);
  270. }
  271. static void
  272. stringbuf_grow (struct stringbuf *buf)
  273. {
  274. size_t ptroff = buf->ptr - buf->buf;
  275. buf->buf_len *= 2;
  276. buf->buf = scm_realloc (buf->buf, buf->buf_len);
  277. buf->ptr = buf->buf + ptroff;
  278. }
  279. static void
  280. stringbuf_cat_locale_string (struct stringbuf *buf, SCM str)
  281. {
  282. size_t max_len = buf->buf_len - (buf->ptr - buf->buf) - 1;
  283. size_t len = scm_to_locale_stringbuf (str, buf->ptr, max_len);
  284. if (len > max_len)
  285. {
  286. /* buffer is too small, double its size and try again.
  287. */
  288. stringbuf_grow (buf);
  289. stringbuf_cat_locale_string (buf, str);
  290. }
  291. else
  292. {
  293. /* string fits, terminate it and check for embedded '\0'.
  294. */
  295. buf->ptr[len] = '\0';
  296. if (strlen (buf->ptr) != len)
  297. scm_misc_error (NULL,
  298. "string contains #\\nul character: ~S",
  299. scm_list_1 (str));
  300. buf->ptr += len;
  301. }
  302. }
  303. static void
  304. stringbuf_cat (struct stringbuf *buf, char *str)
  305. {
  306. size_t max_len = buf->buf_len - (buf->ptr - buf->buf) - 1;
  307. size_t len = strlen (str);
  308. if (len > max_len)
  309. {
  310. /* buffer is too small, double its size and try again.
  311. */
  312. stringbuf_grow (buf);
  313. stringbuf_cat (buf, str);
  314. }
  315. else
  316. {
  317. /* string fits, copy it into buffer.
  318. */
  319. strcpy (buf->ptr, str);
  320. buf->ptr += len;
  321. }
  322. }
  323. static int
  324. scm_c_string_has_an_ext (char *str, size_t len, SCM extensions)
  325. {
  326. for (; !scm_is_null (extensions); extensions = SCM_CDR (extensions))
  327. {
  328. char *ext;
  329. size_t extlen;
  330. int match;
  331. ext = scm_to_locale_string (SCM_CAR (extensions));
  332. extlen = strlen (ext);
  333. match = (len > extlen && str[len - extlen - 1] == '.'
  334. && strncmp (str + (len - extlen), ext, extlen) == 0);
  335. free (ext);
  336. if (match)
  337. return 1;
  338. }
  339. return 0;
  340. }
  341. /* Search PATH for a directory containing a file named FILENAME.
  342. The file must be readable, and not a directory.
  343. If we find one, return its full filename; otherwise, return #f.
  344. If FILENAME is absolute, return it unchanged.
  345. If given, EXTENSIONS is a list of strings; for each directory
  346. in PATH, we search for FILENAME concatenated with each EXTENSION. */
  347. SCM_DEFINE (scm_search_path, "search-path", 2, 0, 1,
  348. (SCM path, SCM filename, SCM rest),
  349. "Search @var{path} for a directory containing a file named\n"
  350. "@var{filename}. The file must be readable, and not a directory.\n"
  351. "If we find one, return its full filename; otherwise, return\n"
  352. "@code{#f}. If @var{filename} is absolute, return it unchanged.\n"
  353. "If given, @var{extensions} is a list of strings; for each\n"
  354. "directory in @var{path}, we search for @var{filename}\n"
  355. "concatenated with each @var{extension}.")
  356. #define FUNC_NAME s_scm_search_path
  357. {
  358. struct stringbuf buf;
  359. char *filename_chars;
  360. size_t filename_len;
  361. SCM extensions, require_exts;
  362. SCM result = SCM_BOOL_F;
  363. if (SCM_UNBNDP (rest) || scm_is_null (rest))
  364. {
  365. /* Called either by Scheme code that didn't provide the optional
  366. arguments, or C code that used the Guile 1.8 signature (2 required,
  367. 1 optional arg) and passed '() or nothing as the EXTENSIONS
  368. argument. */
  369. extensions = SCM_EOL;
  370. require_exts = SCM_UNDEFINED;
  371. }
  372. else
  373. {
  374. if (scm_is_null (SCM_CAR (rest)) || scm_is_pair (SCM_CAR (rest)))
  375. {
  376. /* Called by Scheme code written for 1.9. */
  377. extensions = SCM_CAR (rest);
  378. if (scm_is_null (SCM_CDR (rest)))
  379. require_exts = SCM_UNDEFINED;
  380. else
  381. {
  382. require_exts = SCM_CADR (rest);
  383. if (SCM_UNLIKELY (!scm_is_null (SCM_CDDR (rest))))
  384. scm_wrong_num_args (scm_from_locale_string (FUNC_NAME));
  385. }
  386. }
  387. else
  388. {
  389. /* Called by C code that uses the 1.8 signature, i.e., which
  390. expects the 3rd argument to be EXTENSIONS. */
  391. extensions = rest;
  392. require_exts = SCM_UNDEFINED;
  393. }
  394. }
  395. if (SCM_UNBNDP (extensions))
  396. extensions = SCM_EOL;
  397. SCM_VALIDATE_LIST (3, extensions);
  398. if (SCM_UNBNDP (require_exts))
  399. require_exts = SCM_BOOL_F;
  400. scm_dynwind_begin (0);
  401. filename_chars = scm_to_locale_string (filename);
  402. filename_len = strlen (filename_chars);
  403. scm_dynwind_free (filename_chars);
  404. /* If FILENAME is absolute, return it unchanged. */
  405. #ifdef __MINGW32__
  406. if (((filename_len >= 1) &&
  407. (filename_chars[0] == '/' || filename_chars[0] == '\\')) ||
  408. ((filename_len >= 3) && filename_chars[1] == ':' &&
  409. ((filename_chars[0] >= 'a' && filename_chars[0] <= 'z') ||
  410. (filename_chars[0] >= 'A' && filename_chars[0] <= 'Z')) &&
  411. (filename_chars[2] == '/' || filename_chars[2] == '\\')))
  412. #else
  413. if (filename_len >= 1 && filename_chars[0] == '/')
  414. #endif
  415. {
  416. SCM res = filename;
  417. if (scm_is_true (require_exts) &&
  418. !scm_c_string_has_an_ext (filename_chars, filename_len,
  419. extensions))
  420. res = SCM_BOOL_F;
  421. scm_dynwind_end ();
  422. return res;
  423. }
  424. /* If FILENAME has an extension, don't try to add EXTENSIONS to it. */
  425. {
  426. char *endp;
  427. for (endp = filename_chars + filename_len - 1;
  428. endp >= filename_chars;
  429. endp--)
  430. {
  431. if (*endp == '.')
  432. {
  433. if (scm_is_true (require_exts) &&
  434. !scm_c_string_has_an_ext (filename_chars, filename_len,
  435. extensions))
  436. {
  437. /* This filename has an extension, but not one of the right
  438. ones... */
  439. scm_dynwind_end ();
  440. return SCM_BOOL_F;
  441. }
  442. /* This filename already has an extension, so cancel the
  443. list of extensions. */
  444. extensions = SCM_EOL;
  445. break;
  446. }
  447. #ifdef __MINGW32__
  448. else if (*endp == '/' || *endp == '\\')
  449. #else
  450. else if (*endp == '/')
  451. #endif
  452. /* This filename has no extension, so keep the current list
  453. of extensions. */
  454. break;
  455. }
  456. }
  457. /* This simplifies the loop below a bit.
  458. */
  459. if (scm_is_null (extensions))
  460. extensions = scm_listofnullstr;
  461. buf.buf_len = 512;
  462. buf.buf = scm_malloc (buf.buf_len);
  463. scm_dynwind_unwind_handler (stringbuf_free, &buf, SCM_F_WIND_EXPLICITLY);
  464. /* Try every path element.
  465. */
  466. for (; scm_is_pair (path); path = SCM_CDR (path))
  467. {
  468. SCM dir = SCM_CAR (path);
  469. SCM exts;
  470. size_t sans_ext_len;
  471. buf.ptr = buf.buf;
  472. stringbuf_cat_locale_string (&buf, dir);
  473. /* Concatenate the path name and the filename. */
  474. #ifdef __MINGW32__
  475. if ((buf.ptr > buf.buf) && (buf.ptr[-1] != '/') && (buf.ptr[-1] != '\\'))
  476. #else
  477. if ((buf.ptr > buf.buf) && (buf.ptr[-1] != '/'))
  478. #endif
  479. stringbuf_cat (&buf, "/");
  480. stringbuf_cat (&buf, filename_chars);
  481. sans_ext_len = buf.ptr - buf.buf;
  482. /* Try every extension. */
  483. for (exts = extensions; scm_is_pair (exts); exts = SCM_CDR (exts))
  484. {
  485. SCM ext = SCM_CAR (exts);
  486. struct stat mode;
  487. buf.ptr = buf.buf + sans_ext_len;
  488. stringbuf_cat_locale_string (&buf, ext);
  489. /* If the file exists at all, we should return it. If the
  490. file is inaccessible, then that's an error. */
  491. if (stat (buf.buf, &mode) == 0
  492. && ! (mode.st_mode & S_IFDIR))
  493. {
  494. result = scm_from_locale_string (buf.buf);
  495. goto end;
  496. }
  497. }
  498. if (!SCM_NULL_OR_NIL_P (exts))
  499. scm_wrong_type_arg_msg (NULL, 0, extensions, "proper list");
  500. }
  501. if (!SCM_NULL_OR_NIL_P (path))
  502. scm_wrong_type_arg_msg (NULL, 0, path, "proper list");
  503. end:
  504. scm_dynwind_end ();
  505. return result;
  506. }
  507. #undef FUNC_NAME
  508. /* Search %load-path for a directory containing a file named FILENAME.
  509. The file must be readable, and not a directory.
  510. If we find one, return its full filename; otherwise, return #f.
  511. If FILENAME is absolute, return it unchanged. */
  512. SCM_DEFINE (scm_sys_search_load_path, "%search-load-path", 1, 0, 0,
  513. (SCM filename),
  514. "Search @var{%load-path} for the file named @var{filename},\n"
  515. "which must be readable by the current user. If @var{filename}\n"
  516. "is found in the list of paths to search or is an absolute\n"
  517. "pathname, return its full pathname. Otherwise, return\n"
  518. "@code{#f}. Filenames may have any of the optional extensions\n"
  519. "in the @code{%load-extensions} list; @code{%search-load-path}\n"
  520. "will try each extension automatically.")
  521. #define FUNC_NAME s_scm_sys_search_load_path
  522. {
  523. SCM path = *scm_loc_load_path;
  524. SCM exts = *scm_loc_load_extensions;
  525. SCM_VALIDATE_STRING (1, filename);
  526. if (scm_ilength (path) < 0)
  527. SCM_MISC_ERROR ("%load-path is not a proper list", SCM_EOL);
  528. if (scm_ilength (exts) < 0)
  529. SCM_MISC_ERROR ("%load-extension list is not a proper list", SCM_EOL);
  530. return scm_search_path (path, filename, exts);
  531. }
  532. #undef FUNC_NAME
  533. /* Return true if COMPILED_FILENAME is newer than source file
  534. FULL_FILENAME, false otherwise. Also return false if one of the
  535. files cannot be stat'd. */
  536. static int
  537. compiled_is_fresh (SCM full_filename, SCM compiled_filename)
  538. {
  539. char *source, *compiled;
  540. struct stat stat_source, stat_compiled;
  541. int compiled_is_newer;
  542. source = scm_to_locale_string (full_filename);
  543. compiled = scm_to_locale_string (compiled_filename);
  544. if (stat (source, &stat_source) == 0
  545. && stat (compiled, &stat_compiled) == 0)
  546. {
  547. struct timespec source_mtime, compiled_mtime;
  548. source_mtime = get_stat_mtime (&stat_source);
  549. compiled_mtime = get_stat_mtime (&stat_compiled);
  550. if (source_mtime.tv_sec < compiled_mtime.tv_sec
  551. || (source_mtime.tv_sec == compiled_mtime.tv_sec
  552. && source_mtime.tv_nsec <= compiled_mtime.tv_nsec))
  553. compiled_is_newer = 1;
  554. else
  555. {
  556. compiled_is_newer = 0;
  557. scm_puts (";;; note: source file ", scm_current_error_port ());
  558. scm_puts (source, scm_current_error_port ());
  559. scm_puts ("\n;;; newer than compiled ", scm_current_error_port ());
  560. scm_puts (compiled, scm_current_error_port ());
  561. scm_puts ("\n", scm_current_error_port ());
  562. }
  563. }
  564. else
  565. /* At least one of the files isn't accessible. */
  566. compiled_is_newer = 0;
  567. free (source);
  568. free (compiled);
  569. return compiled_is_newer;
  570. }
  571. SCM_KEYWORD (kw_env, "env");
  572. SCM_KEYWORD (kw_opts, "opts");
  573. SCM_SYMBOL (sym_compile_file, "compile-file");
  574. SCM_SYMBOL (sym_auto_compilation_options, "%auto-compilation-options");
  575. static SCM
  576. do_try_auto_compile (void *data)
  577. {
  578. SCM source = PTR2SCM (data);
  579. SCM comp_mod, compile_file;
  580. scm_puts (";;; compiling ", scm_current_error_port ());
  581. scm_display (source, scm_current_error_port ());
  582. scm_newline (scm_current_error_port ());
  583. comp_mod = scm_c_resolve_module ("system base compile");
  584. compile_file = scm_module_variable (comp_mod, sym_compile_file);
  585. if (scm_is_true (compile_file))
  586. {
  587. /* Auto-compile in the context of the current module. */
  588. SCM res, opts;
  589. SCM args[5];
  590. opts = scm_module_variable (scm_the_root_module (),
  591. sym_auto_compilation_options);
  592. if (SCM_VARIABLEP (opts))
  593. opts = SCM_VARIABLE_REF (opts);
  594. else
  595. opts = SCM_EOL;
  596. args[0] = source;
  597. args[1] = kw_opts;
  598. args[2] = opts;
  599. args[3] = kw_env;
  600. args[4] = scm_current_module ();
  601. /* Assume `*current-warning-prefix*' has an appropriate value. */
  602. res = scm_call_n (scm_variable_ref (compile_file), args, 5);
  603. scm_puts (";;; compiled ", scm_current_error_port ());
  604. scm_display (res, scm_current_error_port ());
  605. scm_newline (scm_current_error_port ());
  606. return res;
  607. }
  608. else
  609. {
  610. scm_puts (";;; it seems ", scm_current_error_port ());
  611. scm_display (source, scm_current_error_port ());
  612. scm_puts ("\n;;; is part of the compiler; skipping auto-compilation\n",
  613. scm_current_error_port ());
  614. return SCM_BOOL_F;
  615. }
  616. }
  617. static SCM
  618. auto_compile_catch_handler (void *data, SCM tag, SCM throw_args)
  619. {
  620. SCM source = PTR2SCM (data);
  621. scm_puts (";;; WARNING: compilation of ", scm_current_error_port ());
  622. scm_display (source, scm_current_error_port ());
  623. scm_puts (" failed:\n", scm_current_error_port ());
  624. scm_puts (";;; key ", scm_current_error_port ());
  625. scm_write (tag, scm_current_error_port ());
  626. scm_puts (", throw args ", scm_current_error_port ());
  627. scm_write (throw_args, scm_current_error_port ());
  628. scm_newline (scm_current_error_port ());
  629. return SCM_BOOL_F;
  630. }
  631. SCM_DEFINE (scm_sys_warn_auto_compilation_enabled, "%warn-auto-compilation-enabled", 0, 0, 0,
  632. (void), "")
  633. #define FUNC_NAME s_scm_sys_warn_auto_compilation_enabled
  634. {
  635. static int message_shown = 0;
  636. if (!message_shown)
  637. {
  638. scm_puts (";;; note: auto-compilation is enabled, set GUILE_AUTO_COMPILE=0\n"
  639. ";;; or pass the --no-auto-compile argument to disable.\n",
  640. scm_current_error_port ());
  641. message_shown = 1;
  642. }
  643. return SCM_UNSPECIFIED;
  644. }
  645. #undef FUNC_NAME
  646. static SCM
  647. scm_try_auto_compile (SCM source)
  648. {
  649. if (scm_is_false (*scm_loc_load_should_auto_compile))
  650. return SCM_BOOL_F;
  651. scm_sys_warn_auto_compilation_enabled ();
  652. return scm_c_catch (SCM_BOOL_T,
  653. do_try_auto_compile,
  654. SCM2PTR (source),
  655. auto_compile_catch_handler,
  656. SCM2PTR (source),
  657. NULL, NULL);
  658. }
  659. SCM_DEFINE (scm_primitive_load_path, "primitive-load-path", 0, 0, 1,
  660. (SCM args),
  661. "Search @var{%load-path} for the file named @var{filename} and\n"
  662. "load it into the top-level environment. If @var{filename} is a\n"
  663. "relative pathname and is not found in the list of search paths,\n"
  664. "an error is signalled, unless the optional argument\n"
  665. "@var{exception_on_not_found} is @code{#f}, in which case\n"
  666. "@code{#f} is returned instead.")
  667. #define FUNC_NAME s_scm_primitive_load_path
  668. {
  669. SCM filename, exception_on_not_found;
  670. SCM full_filename, compiled_filename;
  671. int compiled_is_fallback = 0;
  672. if (scm_is_string (args))
  673. {
  674. /* C code written for 1.8 and earlier expects this function to take a
  675. single argument (the file name). */
  676. filename = args;
  677. exception_on_not_found = SCM_UNDEFINED;
  678. }
  679. else
  680. {
  681. /* Starting from 1.9, this function takes 1 required and 1 optional
  682. argument. */
  683. long len;
  684. SCM_VALIDATE_LIST_COPYLEN (SCM_ARG1, args, len);
  685. if (len < 1 || len > 2)
  686. scm_error_num_args_subr (FUNC_NAME);
  687. filename = SCM_CAR (args);
  688. SCM_VALIDATE_STRING (SCM_ARG1, filename);
  689. exception_on_not_found = len > 1 ? SCM_CADR (args) : SCM_UNDEFINED;
  690. }
  691. if (SCM_UNBNDP (exception_on_not_found))
  692. exception_on_not_found = SCM_BOOL_T;
  693. full_filename = scm_sys_search_load_path (filename);
  694. if (scm_is_string (full_filename))
  695. full_filename = scm_canonicalize_path (full_filename);
  696. compiled_filename =
  697. scm_search_path (*scm_loc_load_compiled_path,
  698. filename,
  699. scm_list_2 (*scm_loc_load_compiled_extensions,
  700. SCM_BOOL_T));
  701. if (scm_is_false (compiled_filename)
  702. && scm_is_true (full_filename)
  703. && scm_is_true (*scm_loc_compile_fallback_path)
  704. && scm_is_false (*scm_loc_fresh_auto_compile)
  705. && scm_is_pair (*scm_loc_load_compiled_extensions)
  706. && scm_is_string (scm_car (*scm_loc_load_compiled_extensions)))
  707. {
  708. SCM fallback = scm_string_append
  709. (scm_list_3 (*scm_loc_compile_fallback_path,
  710. full_filename,
  711. scm_car (*scm_loc_load_compiled_extensions)));
  712. if (scm_is_true (scm_stat (fallback, SCM_BOOL_F)))
  713. {
  714. compiled_filename = fallback;
  715. compiled_is_fallback = 1;
  716. }
  717. }
  718. if (scm_is_false (full_filename) && scm_is_false (compiled_filename))
  719. {
  720. if (scm_is_true (exception_on_not_found))
  721. SCM_MISC_ERROR ("Unable to find file ~S in load path",
  722. scm_list_1 (filename));
  723. else
  724. return SCM_BOOL_F;
  725. }
  726. if (scm_is_false (full_filename)
  727. || (scm_is_true (compiled_filename)
  728. && compiled_is_fresh (full_filename, compiled_filename)))
  729. return scm_load_compiled_with_vm (compiled_filename);
  730. /* Perhaps there was the installed .go that was stale, but our fallback is
  731. fresh. Let's try that. Duplicating code, but perhaps that's OK. */
  732. if (!compiled_is_fallback
  733. && scm_is_true (*scm_loc_compile_fallback_path)
  734. && scm_is_false (*scm_loc_fresh_auto_compile)
  735. && scm_is_pair (*scm_loc_load_compiled_extensions)
  736. && scm_is_string (scm_car (*scm_loc_load_compiled_extensions)))
  737. {
  738. SCM fallback = scm_string_append
  739. (scm_list_3 (*scm_loc_compile_fallback_path,
  740. full_filename,
  741. scm_car (*scm_loc_load_compiled_extensions)));
  742. if (scm_is_true (scm_stat (fallback, SCM_BOOL_F))
  743. && compiled_is_fresh (full_filename, fallback))
  744. {
  745. scm_puts (";;; found fresh local cache at ", scm_current_error_port ());
  746. scm_display (fallback, scm_current_error_port ());
  747. scm_newline (scm_current_error_port ());
  748. return scm_load_compiled_with_vm (fallback);
  749. }
  750. }
  751. /* Otherwise, we bottom out here. */
  752. {
  753. SCM freshly_compiled = scm_try_auto_compile (full_filename);
  754. if (scm_is_true (freshly_compiled))
  755. return scm_load_compiled_with_vm (freshly_compiled);
  756. else
  757. return scm_primitive_load (full_filename);
  758. }
  759. }
  760. #undef FUNC_NAME
  761. SCM
  762. scm_c_primitive_load_path (const char *filename)
  763. {
  764. return scm_primitive_load_path (scm_from_locale_string (filename));
  765. }
  766. void
  767. scm_init_eval_in_scheme (void)
  768. {
  769. SCM eval_scm, eval_go;
  770. eval_scm = scm_search_path (*scm_loc_load_path,
  771. scm_from_locale_string ("ice-9/eval.scm"),
  772. SCM_EOL);
  773. eval_go = scm_search_path (*scm_loc_load_compiled_path,
  774. scm_from_locale_string ("ice-9/eval.go"),
  775. SCM_EOL);
  776. if (scm_is_true (eval_scm) && scm_is_true (eval_go)
  777. && compiled_is_fresh (eval_scm, eval_go))
  778. scm_load_compiled_with_vm (eval_go);
  779. else
  780. /* if we have no eval.go, we shouldn't load any compiled code at all */
  781. *scm_loc_load_compiled_path = SCM_EOL;
  782. }
  783. /* Information about the build environment. */
  784. SCM_VARIABLE_INIT (sys_host_type, "%host-type",
  785. scm_from_locale_string (HOST_TYPE));
  786. /* Initialize the scheme variable %guile-build-info, based on data
  787. provided by the Makefile, via libpath.h. */
  788. static void
  789. init_build_info ()
  790. {
  791. static struct { char *name; char *value; } info[] = SCM_BUILD_INFO;
  792. SCM *loc = SCM_VARIABLE_LOC (scm_c_define ("%guile-build-info", SCM_EOL));
  793. unsigned long i;
  794. for (i = 0; i < (sizeof (info) / sizeof (info[0])); i++)
  795. {
  796. SCM key = scm_from_locale_symbol (info[i].name);
  797. SCM val = scm_from_locale_string (info[i].value);
  798. *loc = scm_acons (key, val, *loc);
  799. }
  800. #ifdef PACKAGE_PACKAGER
  801. *loc = scm_acons (scm_from_latin1_symbol ("packager"),
  802. scm_from_latin1_string (PACKAGE_PACKAGER),
  803. *loc);
  804. #endif
  805. #ifdef PACKAGE_PACKAGER_VERSION
  806. *loc = scm_acons (scm_from_latin1_symbol ("packager-version"),
  807. scm_from_latin1_string (PACKAGE_PACKAGER_VERSION),
  808. *loc);
  809. #endif
  810. #ifdef PACKAGE_PACKAGER_BUG_REPORTS
  811. *loc = scm_acons (scm_from_latin1_symbol ("packager-bug-reports"),
  812. scm_from_latin1_string (PACKAGE_PACKAGER_BUG_REPORTS),
  813. *loc);
  814. #endif
  815. }
  816. void
  817. scm_init_load ()
  818. {
  819. scm_listofnullstr = scm_list_1 (scm_nullstr);
  820. scm_loc_load_path = SCM_VARIABLE_LOC (scm_c_define ("%load-path", SCM_EOL));
  821. scm_loc_load_extensions
  822. = SCM_VARIABLE_LOC (scm_c_define ("%load-extensions",
  823. scm_list_2 (scm_from_locale_string (".scm"),
  824. scm_nullstr)));
  825. scm_loc_load_compiled_path
  826. = SCM_VARIABLE_LOC (scm_c_define ("%load-compiled-path", SCM_EOL));
  827. scm_loc_load_compiled_extensions
  828. = SCM_VARIABLE_LOC (scm_c_define ("%load-compiled-extensions",
  829. scm_list_1 (scm_from_locale_string (".go"))));
  830. scm_loc_load_hook = SCM_VARIABLE_LOC (scm_c_define ("%load-hook", SCM_BOOL_F));
  831. scm_loc_compile_fallback_path
  832. = SCM_VARIABLE_LOC (scm_c_define ("%compile-fallback-path", SCM_BOOL_F));
  833. scm_loc_load_should_auto_compile
  834. = SCM_VARIABLE_LOC (scm_c_define ("%load-should-auto-compile", SCM_BOOL_F));
  835. scm_loc_fresh_auto_compile
  836. = SCM_VARIABLE_LOC (scm_c_define ("%fresh-auto-compile", SCM_BOOL_F));
  837. the_reader = scm_make_fluid ();
  838. scm_fluid_set_x (the_reader, SCM_BOOL_F);
  839. scm_c_define("current-reader", the_reader);
  840. scm_c_define ("load-compiled",
  841. scm_c_make_gsubr ("load-compiled/vm", 1, 0, 0,
  842. scm_load_compiled_with_vm));
  843. init_build_info ();
  844. #include "libguile/load.x"
  845. }
  846. void
  847. scm_init_load_should_auto_compile ()
  848. {
  849. char *auto_compile = getenv ("GUILE_AUTO_COMPILE");
  850. if (auto_compile && strcmp (auto_compile, "0") == 0)
  851. {
  852. *scm_loc_load_should_auto_compile = SCM_BOOL_F;
  853. *scm_loc_fresh_auto_compile = SCM_BOOL_F;
  854. }
  855. /* Allow "freshen" also. */
  856. else if (auto_compile && strncmp (auto_compile, "fresh", 5) == 0)
  857. {
  858. *scm_loc_load_should_auto_compile = SCM_BOOL_T;
  859. *scm_loc_fresh_auto_compile = SCM_BOOL_T;
  860. }
  861. else
  862. {
  863. *scm_loc_load_should_auto_compile = SCM_BOOL_T;
  864. *scm_loc_fresh_auto_compile = SCM_BOOL_F;
  865. }
  866. }
  867. /*
  868. Local Variables:
  869. c-file-style: "gnu"
  870. End:
  871. */