load.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  1. /* Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2004, 2006, 2008,
  2. * 2009, 2010, 2011, 2012, 2013 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/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. SCM ret = SCM_UNSPECIFIED;
  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. {
  79. SCM port;
  80. port = scm_open_file_with_encoding (filename,
  81. scm_from_latin1_string ("r"),
  82. SCM_BOOL_T, /* guess_encoding */
  83. scm_from_latin1_string ("UTF-8"));
  84. scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
  85. scm_i_dynwind_current_load_port (port);
  86. while (1)
  87. {
  88. SCM reader, form;
  89. /* Lookup and use the current reader to read the next
  90. expression. */
  91. reader = scm_fluid_ref (the_reader);
  92. if (scm_is_false (reader))
  93. form = scm_read (port);
  94. else
  95. form = scm_call_1 (reader, port);
  96. if (SCM_EOF_OBJECT_P (form))
  97. break;
  98. ret = scm_primitive_eval_x (form);
  99. }
  100. scm_dynwind_end ();
  101. scm_close_port (port);
  102. }
  103. return ret;
  104. }
  105. #undef FUNC_NAME
  106. SCM
  107. scm_c_primitive_load (const char *filename)
  108. {
  109. return scm_primitive_load (scm_from_locale_string (filename));
  110. }
  111. /* Builtin path to scheme library files. */
  112. #ifdef SCM_PKGDATA_DIR
  113. SCM_DEFINE (scm_sys_package_data_dir, "%package-data-dir", 0, 0, 0,
  114. (),
  115. "Return the name of the directory where Scheme packages, modules and\n"
  116. "libraries are kept. On most Unix systems, this will be\n"
  117. "@samp{/usr/local/share/guile}.")
  118. #define FUNC_NAME s_scm_sys_package_data_dir
  119. {
  120. return scm_from_locale_string (SCM_PKGDATA_DIR);
  121. }
  122. #undef FUNC_NAME
  123. #endif /* SCM_PKGDATA_DIR */
  124. #ifdef SCM_LIBRARY_DIR
  125. SCM_DEFINE (scm_sys_library_dir, "%library-dir", 0,0,0,
  126. (),
  127. "Return the directory where the Guile Scheme library files are installed.\n"
  128. "E.g., may return \"/usr/share/guile/1.3.5\".")
  129. #define FUNC_NAME s_scm_sys_library_dir
  130. {
  131. return scm_from_locale_string (SCM_LIBRARY_DIR);
  132. }
  133. #undef FUNC_NAME
  134. #endif /* SCM_LIBRARY_DIR */
  135. #ifdef SCM_SITE_DIR
  136. SCM_DEFINE (scm_sys_site_dir, "%site-dir", 0,0,0,
  137. (),
  138. "Return the directory where users should install Scheme code for use\n"
  139. "with this version of Guile.\n\n"
  140. "E.g., may return \"/usr/share/guile/site/" SCM_EFFECTIVE_VERSION "\".")
  141. #define FUNC_NAME s_scm_sys_site_dir
  142. {
  143. return scm_from_locale_string (SCM_SITE_DIR);
  144. }
  145. #undef FUNC_NAME
  146. #endif /* SCM_SITE_DIR */
  147. #ifdef SCM_GLOBAL_SITE_DIR
  148. SCM_DEFINE (scm_sys_global_site_dir, "%global-site-dir", 0,0,0,
  149. (),
  150. "Return the directory where users should install Scheme code for use\n"
  151. "with all versions of Guile.\n\n"
  152. "E.g., may return \"/usr/share/guile/site\".")
  153. #define FUNC_NAME s_scm_sys_global_site_dir
  154. {
  155. return scm_from_locale_string (SCM_GLOBAL_SITE_DIR);
  156. }
  157. #undef FUNC_NAME
  158. #endif /* SCM_GLOBAL_SITE_DIR */
  159. #ifdef SCM_SITE_CCACHE_DIR
  160. SCM_DEFINE (scm_sys_site_ccache_dir, "%site-ccache-dir", 0,0,0,
  161. (),
  162. "Return the directory where users should install compiled\n"
  163. "@code{.go} files for use with this version of Guile.\n\n"
  164. "E.g., may return \"/usr/lib/guile/" SCM_EFFECTIVE_VERSION "/site-ccache\".")
  165. #define FUNC_NAME s_scm_sys_site_ccache_dir
  166. {
  167. return scm_from_locale_string (SCM_SITE_CCACHE_DIR);
  168. }
  169. #undef FUNC_NAME
  170. #endif /* SCM_SITE_CCACHE_DIR */
  171. /* Initializing the load path, and searching it. */
  172. /* List of names of directories we search for files to load. */
  173. static SCM *scm_loc_load_path;
  174. /* List of extensions we try adding to the filenames. */
  175. static SCM *scm_loc_load_extensions;
  176. /* Like %load-path and %load-extensions, but for compiled files. */
  177. static SCM *scm_loc_load_compiled_path;
  178. static SCM *scm_loc_load_compiled_extensions;
  179. /* Whether we should try to auto-compile. */
  180. static SCM *scm_loc_load_should_auto_compile;
  181. /* Whether to treat all auto-compiled files as stale. */
  182. static SCM *scm_loc_fresh_auto_compile;
  183. /* The fallback path for auto-compilation */
  184. static SCM *scm_loc_compile_fallback_path;
  185. /* Ellipsis: "..." */
  186. static SCM scm_ellipsis;
  187. SCM_DEFINE (scm_parse_path, "parse-path", 1, 1, 0,
  188. (SCM path, SCM tail),
  189. "Parse @var{path}, which is expected to be a colon-separated\n"
  190. "string, into a list and return the resulting list with\n"
  191. "@var{tail} appended. If @var{path} is @code{#f}, @var{tail}\n"
  192. "is returned.")
  193. #define FUNC_NAME s_scm_parse_path
  194. {
  195. #ifdef __MINGW32__
  196. SCM sep = SCM_MAKE_CHAR (';');
  197. #else
  198. SCM sep = SCM_MAKE_CHAR (':');
  199. #endif
  200. if (SCM_UNBNDP (tail))
  201. tail = SCM_EOL;
  202. return (scm_is_false (path)
  203. ? tail
  204. : scm_append_x (scm_list_2 (scm_string_split (path, sep), tail)));
  205. }
  206. #undef FUNC_NAME
  207. SCM_DEFINE (scm_parse_path_with_ellipsis, "parse-path-with-ellipsis", 2, 0, 0,
  208. (SCM path, SCM base),
  209. "Parse @var{path}, which is expected to be a colon-separated\n"
  210. "string, into a list and return the resulting list with\n"
  211. "@var{base} (a list) spliced in place of the @code{...} path\n"
  212. "component, if present, or else @var{base} is added to the end.\n"
  213. "If @var{path} is @code{#f}, @var{base} is returned.")
  214. #define FUNC_NAME s_scm_parse_path_with_ellipsis
  215. {
  216. SCM lst = scm_parse_path (path, SCM_EOL);
  217. SCM walk = lst;
  218. SCM *prev = &lst;
  219. while (!scm_is_null (walk) &&
  220. scm_is_false (scm_equal_p (scm_car (walk), scm_ellipsis)))
  221. {
  222. prev = SCM_CDRLOC (walk);
  223. walk = *prev;
  224. }
  225. *prev = scm_is_null (walk)
  226. ? base
  227. : scm_append (scm_list_2 (base, scm_cdr (walk)));
  228. return lst;
  229. }
  230. #undef FUNC_NAME
  231. /* Initialize the global variable %load-path, given the value of the
  232. SCM_SITE_DIR and SCM_LIBRARY_DIR preprocessor symbols and the
  233. GUILE_LOAD_PATH environment variable. */
  234. void
  235. scm_init_load_path ()
  236. {
  237. char *env;
  238. SCM path = SCM_EOL;
  239. SCM cpath = SCM_EOL;
  240. #ifdef SCM_LIBRARY_DIR
  241. env = getenv ("GUILE_SYSTEM_PATH");
  242. if (env && strcmp (env, "") == 0)
  243. /* special-case interpret system-path=="" as meaning no system path instead
  244. of '("") */
  245. ;
  246. else if (env)
  247. path = scm_parse_path (scm_from_locale_string (env), path);
  248. else
  249. path = scm_list_4 (scm_from_locale_string (SCM_LIBRARY_DIR),
  250. scm_from_locale_string (SCM_SITE_DIR),
  251. scm_from_locale_string (SCM_GLOBAL_SITE_DIR),
  252. scm_from_locale_string (SCM_PKGDATA_DIR));
  253. env = getenv ("GUILE_SYSTEM_COMPILED_PATH");
  254. if (env && strcmp (env, "") == 0)
  255. /* like above */
  256. ;
  257. else if (env)
  258. cpath = scm_parse_path (scm_from_locale_string (env), cpath);
  259. else
  260. {
  261. cpath = scm_list_2 (scm_from_locale_string (SCM_CCACHE_DIR),
  262. scm_from_locale_string (SCM_SITE_CCACHE_DIR));
  263. }
  264. #endif /* SCM_LIBRARY_DIR */
  265. {
  266. char cachedir[1024];
  267. char *e;
  268. #ifdef HAVE_GETPWENT
  269. struct passwd *pwd;
  270. #endif
  271. #define FALLBACK_DIR \
  272. "guile/ccache/" SCM_EFFECTIVE_VERSION "-" SCM_OBJCODE_MACHINE_VERSION_STRING
  273. if ((e = getenv ("XDG_CACHE_HOME")))
  274. snprintf (cachedir, sizeof(cachedir), "%s/" FALLBACK_DIR, e);
  275. else if ((e = getenv ("HOME")))
  276. snprintf (cachedir, sizeof(cachedir), "%s/.cache/" FALLBACK_DIR, e);
  277. #ifdef HAVE_GETPWENT
  278. else if ((pwd = getpwuid (getuid ())) && pwd->pw_dir)
  279. snprintf (cachedir, sizeof(cachedir), "%s/.cache/" FALLBACK_DIR,
  280. pwd->pw_dir);
  281. #endif /* HAVE_GETPWENT */
  282. #ifdef __MINGW32__
  283. else if ((e = getenv ("LOCALAPPDATA")))
  284. snprintf (cachedir, sizeof (cachedir), "%s/.cache/" FALLBACK_DIR, e);
  285. else if ((e = getenv ("APPDATA")))
  286. snprintf (cachedir, sizeof (cachedir), "%s/.cache/" FALLBACK_DIR, e);
  287. #endif /* __MINGW32__ */
  288. else
  289. cachedir[0] = 0;
  290. if (cachedir[0])
  291. *scm_loc_compile_fallback_path = scm_from_locale_string (cachedir);
  292. }
  293. env = getenv ("GUILE_LOAD_PATH");
  294. if (env)
  295. path = scm_parse_path_with_ellipsis (scm_from_locale_string (env), path);
  296. env = getenv ("GUILE_LOAD_COMPILED_PATH");
  297. if (env)
  298. cpath = scm_parse_path_with_ellipsis (scm_from_locale_string (env), cpath);
  299. *scm_loc_load_path = path;
  300. *scm_loc_load_compiled_path = cpath;
  301. }
  302. SCM scm_listofnullstr;
  303. /* Utility functions for assembling C strings in a buffer.
  304. */
  305. struct stringbuf {
  306. char *buf, *ptr;
  307. size_t buf_len;
  308. };
  309. static void
  310. stringbuf_free (void *data)
  311. {
  312. struct stringbuf *buf = (struct stringbuf *)data;
  313. free (buf->buf);
  314. }
  315. static void
  316. stringbuf_grow (struct stringbuf *buf)
  317. {
  318. size_t ptroff = buf->ptr - buf->buf;
  319. buf->buf_len *= 2;
  320. buf->buf = scm_realloc (buf->buf, buf->buf_len);
  321. buf->ptr = buf->buf + ptroff;
  322. }
  323. static void
  324. stringbuf_cat_locale_string (struct stringbuf *buf, SCM str)
  325. {
  326. size_t max_len = buf->buf_len - (buf->ptr - buf->buf) - 1;
  327. size_t len = scm_to_locale_stringbuf (str, buf->ptr, max_len);
  328. if (len > max_len)
  329. {
  330. /* buffer is too small, double its size and try again.
  331. */
  332. stringbuf_grow (buf);
  333. stringbuf_cat_locale_string (buf, str);
  334. }
  335. else
  336. {
  337. /* string fits, terminate it and check for embedded '\0'.
  338. */
  339. buf->ptr[len] = '\0';
  340. if (strlen (buf->ptr) != len)
  341. scm_misc_error (NULL,
  342. "string contains #\\nul character: ~S",
  343. scm_list_1 (str));
  344. buf->ptr += len;
  345. }
  346. }
  347. static void
  348. stringbuf_cat (struct stringbuf *buf, char *str)
  349. {
  350. size_t max_len = buf->buf_len - (buf->ptr - buf->buf) - 1;
  351. size_t len = strlen (str);
  352. if (len > max_len)
  353. {
  354. /* buffer is too small, double its size and try again.
  355. */
  356. stringbuf_grow (buf);
  357. stringbuf_cat (buf, str);
  358. }
  359. else
  360. {
  361. /* string fits, copy it into buffer.
  362. */
  363. strcpy (buf->ptr, str);
  364. buf->ptr += len;
  365. }
  366. }
  367. static int
  368. scm_c_string_has_an_ext (char *str, size_t len, SCM extensions)
  369. {
  370. for (; !scm_is_null (extensions); extensions = SCM_CDR (extensions))
  371. {
  372. char *ext;
  373. size_t extlen;
  374. int match;
  375. ext = scm_to_locale_string (SCM_CAR (extensions));
  376. extlen = strlen (ext);
  377. match = (len > extlen && str[len - extlen - 1] == '.'
  378. && strncmp (str + (len - extlen), ext, extlen) == 0);
  379. free (ext);
  380. if (match)
  381. return 1;
  382. }
  383. return 0;
  384. }
  385. #ifdef __MINGW32__
  386. #define FILE_NAME_SEPARATOR_STRING "\\"
  387. #else
  388. #define FILE_NAME_SEPARATOR_STRING "/"
  389. #endif
  390. static int
  391. is_file_name_separator (SCM c)
  392. {
  393. if (scm_is_eq (c, SCM_MAKE_CHAR ('/')))
  394. return 1;
  395. #ifdef __MINGW32__
  396. if (scm_is_eq (c, SCM_MAKE_CHAR ('\\')))
  397. return 1;
  398. #endif
  399. return 0;
  400. }
  401. static int
  402. is_drive_letter (SCM c)
  403. {
  404. #ifdef __MINGW32__
  405. if (SCM_CHAR (c) >= 'a' && SCM_CHAR (c) <= 'z')
  406. return 1;
  407. else if (SCM_CHAR (c) >= 'A' && SCM_CHAR (c) <= 'Z')
  408. return 1;
  409. #endif
  410. return 0;
  411. }
  412. static int
  413. is_absolute_file_name (SCM filename)
  414. {
  415. size_t filename_len = scm_c_string_length (filename);
  416. if (filename_len >= 1
  417. && is_file_name_separator (scm_c_string_ref (filename, 0))
  418. #ifdef __MINGW32__
  419. /* On Windows, one initial separator indicates a drive-relative
  420. path. Two separators indicate a Universal Naming Convention
  421. (UNC) path. UNC paths are always absolute. */
  422. && filename_len >= 2
  423. && is_file_name_separator (scm_c_string_ref (filename, 1))
  424. #endif
  425. )
  426. return 1;
  427. if (filename_len >= 3
  428. && is_drive_letter (scm_c_string_ref (filename, 0))
  429. && scm_is_eq (scm_c_string_ref (filename, 1), SCM_MAKE_CHAR (':'))
  430. && is_file_name_separator (scm_c_string_ref (filename, 2)))
  431. return 1;
  432. return 0;
  433. }
  434. /* Search PATH for a directory containing a file named FILENAME.
  435. The file must be readable, and not a directory.
  436. If we find one, return its full pathname; otherwise, return #f.
  437. If FILENAME is absolute, return it unchanged.
  438. We also fill *stat_buf corresponding to the returned pathname.
  439. If given, EXTENSIONS is a list of strings; for each directory
  440. in PATH, we search for FILENAME concatenated with each EXTENSION. */
  441. static SCM
  442. search_path (SCM path, SCM filename, SCM extensions, SCM require_exts,
  443. struct stat *stat_buf)
  444. {
  445. struct stringbuf buf;
  446. char *filename_chars;
  447. size_t filename_len;
  448. SCM result = SCM_BOOL_F;
  449. if (scm_ilength (path) < 0)
  450. scm_misc_error ("%search-path", "path is not a proper list: ~a",
  451. scm_list_1 (path));
  452. if (scm_ilength (extensions) < 0)
  453. scm_misc_error ("%search-path", "bad extensions list: ~a",
  454. scm_list_1 (extensions));
  455. scm_dynwind_begin (0);
  456. filename_chars = scm_to_locale_string (filename);
  457. filename_len = strlen (filename_chars);
  458. scm_dynwind_free (filename_chars);
  459. /* If FILENAME is absolute and is still valid, return it unchanged. */
  460. if (is_absolute_file_name (filename))
  461. {
  462. if ((scm_is_false (require_exts) ||
  463. scm_c_string_has_an_ext (filename_chars, filename_len,
  464. extensions))
  465. && stat (filename_chars, stat_buf) == 0
  466. && !(stat_buf->st_mode & S_IFDIR))
  467. result = filename;
  468. goto end;
  469. }
  470. /* If FILENAME has an extension, don't try to add EXTENSIONS to it. */
  471. {
  472. char *endp;
  473. for (endp = filename_chars + filename_len - 1;
  474. endp >= filename_chars;
  475. endp--)
  476. {
  477. if (*endp == '.')
  478. {
  479. if (scm_is_true (require_exts) &&
  480. !scm_c_string_has_an_ext (filename_chars, filename_len,
  481. extensions))
  482. {
  483. /* This filename has an extension, but not one of the right
  484. ones... */
  485. goto end;
  486. }
  487. /* This filename already has an extension, so cancel the
  488. list of extensions. */
  489. extensions = SCM_EOL;
  490. break;
  491. }
  492. else if (is_file_name_separator (SCM_MAKE_CHAR (*endp)))
  493. /* This filename has no extension, so keep the current list
  494. of extensions. */
  495. break;
  496. }
  497. }
  498. /* This simplifies the loop below a bit.
  499. */
  500. if (scm_is_null (extensions))
  501. extensions = scm_listofnullstr;
  502. buf.buf_len = 512;
  503. buf.buf = scm_malloc (buf.buf_len);
  504. scm_dynwind_unwind_handler (stringbuf_free, &buf, SCM_F_WIND_EXPLICITLY);
  505. /* Try every path element.
  506. */
  507. for (; scm_is_pair (path); path = SCM_CDR (path))
  508. {
  509. SCM dir = SCM_CAR (path);
  510. SCM exts;
  511. size_t sans_ext_len;
  512. buf.ptr = buf.buf;
  513. stringbuf_cat_locale_string (&buf, dir);
  514. /* Concatenate the path name and the filename. */
  515. if (buf.ptr > buf.buf
  516. && !is_file_name_separator (SCM_MAKE_CHAR (buf.ptr[-1])))
  517. stringbuf_cat (&buf, FILE_NAME_SEPARATOR_STRING);
  518. stringbuf_cat (&buf, filename_chars);
  519. sans_ext_len = buf.ptr - buf.buf;
  520. /* Try every extension. */
  521. for (exts = extensions; scm_is_pair (exts); exts = SCM_CDR (exts))
  522. {
  523. SCM ext = SCM_CAR (exts);
  524. buf.ptr = buf.buf + sans_ext_len;
  525. stringbuf_cat_locale_string (&buf, ext);
  526. /* If the file exists at all, we should return it. If the
  527. file is inaccessible, then that's an error. */
  528. if (stat (buf.buf, stat_buf) == 0
  529. && ! (stat_buf->st_mode & S_IFDIR))
  530. {
  531. result = scm_from_locale_string (buf.buf);
  532. goto end;
  533. }
  534. }
  535. if (!SCM_NULL_OR_NIL_P (exts))
  536. scm_wrong_type_arg_msg (NULL, 0, extensions, "proper list");
  537. }
  538. if (!SCM_NULL_OR_NIL_P (path))
  539. scm_wrong_type_arg_msg (NULL, 0, path, "proper list");
  540. end:
  541. scm_dynwind_end ();
  542. return result;
  543. }
  544. SCM_DEFINE (scm_search_path, "search-path", 2, 0, 1,
  545. (SCM path, SCM filename, SCM rest),
  546. "Search @var{path} for a directory containing a file named\n"
  547. "@var{filename}. The file must be readable, and not a directory.\n"
  548. "If we find one, return its full filename; otherwise, return\n"
  549. "@code{#f}. If @var{filename} is absolute, return it unchanged.\n"
  550. "If given, @var{rest} is a list of extension strings; for each\n"
  551. "directory in @var{path}, we search for @var{filename}\n"
  552. "concatenated with each extension.")
  553. #define FUNC_NAME s_scm_search_path
  554. {
  555. SCM extensions, require_exts;
  556. struct stat stat_buf;
  557. if (SCM_UNBNDP (rest) || scm_is_null (rest))
  558. {
  559. /* Called either by Scheme code that didn't provide the optional
  560. arguments, or C code that used the Guile 1.8 signature (2 required,
  561. 1 optional arg) and passed '() or nothing as the EXTENSIONS
  562. argument. */
  563. extensions = SCM_EOL;
  564. require_exts = SCM_UNDEFINED;
  565. }
  566. else
  567. {
  568. if (scm_is_null (SCM_CAR (rest)) || scm_is_pair (SCM_CAR (rest)))
  569. {
  570. /* Called by Scheme code written for 1.9. */
  571. extensions = SCM_CAR (rest);
  572. if (scm_is_null (SCM_CDR (rest)))
  573. require_exts = SCM_UNDEFINED;
  574. else
  575. {
  576. require_exts = SCM_CADR (rest);
  577. if (SCM_UNLIKELY (!scm_is_null (SCM_CDDR (rest))))
  578. scm_wrong_num_args (scm_from_locale_string (FUNC_NAME));
  579. }
  580. }
  581. else
  582. {
  583. /* Called by C code that uses the 1.8 signature, i.e., which
  584. expects the 3rd argument to be EXTENSIONS. */
  585. extensions = rest;
  586. require_exts = SCM_UNDEFINED;
  587. }
  588. }
  589. if (SCM_UNBNDP (extensions))
  590. extensions = SCM_EOL;
  591. if (SCM_UNBNDP (require_exts))
  592. require_exts = SCM_BOOL_F;
  593. return search_path (path, filename, extensions, require_exts, &stat_buf);
  594. }
  595. #undef FUNC_NAME
  596. /* Search %load-path for a directory containing a file named FILENAME.
  597. The file must be readable, and not a directory.
  598. If we find one, return its full filename; otherwise, return #f.
  599. If FILENAME is absolute, return it unchanged. */
  600. SCM_DEFINE (scm_sys_search_load_path, "%search-load-path", 1, 0, 0,
  601. (SCM filename),
  602. "Search @var{%load-path} for the file named @var{filename},\n"
  603. "which must be readable by the current user. If @var{filename}\n"
  604. "is found in the list of paths to search or is an absolute\n"
  605. "pathname, return its full pathname. Otherwise, return\n"
  606. "@code{#f}. Filenames may have any of the optional extensions\n"
  607. "in the @code{%load-extensions} list; @code{%search-load-path}\n"
  608. "will try each extension automatically.")
  609. #define FUNC_NAME s_scm_sys_search_load_path
  610. {
  611. struct stat stat_buf;
  612. SCM_VALIDATE_STRING (1, filename);
  613. return search_path (*scm_loc_load_path, filename, *scm_loc_load_extensions,
  614. SCM_BOOL_F, &stat_buf);
  615. }
  616. #undef FUNC_NAME
  617. /* Return true if COMPILED_FILENAME is newer than source file
  618. FULL_FILENAME, false otherwise. */
  619. static int
  620. compiled_is_fresh (SCM full_filename, SCM compiled_filename,
  621. struct stat *stat_source, struct stat *stat_compiled)
  622. {
  623. int compiled_is_newer;
  624. struct timespec source_mtime, compiled_mtime;
  625. source_mtime = get_stat_mtime (stat_source);
  626. compiled_mtime = get_stat_mtime (stat_compiled);
  627. if (source_mtime.tv_sec < compiled_mtime.tv_sec
  628. || (source_mtime.tv_sec == compiled_mtime.tv_sec
  629. && source_mtime.tv_nsec <= compiled_mtime.tv_nsec))
  630. compiled_is_newer = 1;
  631. else
  632. {
  633. compiled_is_newer = 0;
  634. scm_puts_unlocked (";;; note: source file ", scm_current_error_port ());
  635. scm_display (full_filename, scm_current_error_port ());
  636. scm_puts_unlocked ("\n;;; newer than compiled ", scm_current_error_port ());
  637. scm_display (compiled_filename, scm_current_error_port ());
  638. scm_puts_unlocked ("\n", scm_current_error_port ());
  639. }
  640. return compiled_is_newer;
  641. }
  642. SCM_KEYWORD (kw_env, "env");
  643. SCM_KEYWORD (kw_opts, "opts");
  644. SCM_SYMBOL (sym_compile_file, "compile-file");
  645. SCM_SYMBOL (sym_auto_compilation_options, "%auto-compilation-options");
  646. static SCM
  647. do_try_auto_compile (void *data)
  648. {
  649. SCM source = SCM_PACK_POINTER (data);
  650. SCM comp_mod, compile_file;
  651. scm_puts_unlocked (";;; compiling ", scm_current_error_port ());
  652. scm_display (source, scm_current_error_port ());
  653. scm_newline (scm_current_error_port ());
  654. comp_mod = scm_c_resolve_module ("system base compile");
  655. compile_file = scm_module_variable (comp_mod, sym_compile_file);
  656. if (scm_is_true (compile_file))
  657. {
  658. /* Auto-compile in the context of the current module. */
  659. SCM res, opts;
  660. SCM args[5];
  661. opts = scm_module_variable (scm_the_root_module (),
  662. sym_auto_compilation_options);
  663. if (SCM_VARIABLEP (opts))
  664. opts = SCM_VARIABLE_REF (opts);
  665. else
  666. opts = SCM_EOL;
  667. args[0] = source;
  668. args[1] = kw_opts;
  669. args[2] = opts;
  670. args[3] = kw_env;
  671. args[4] = scm_current_module ();
  672. /* Assume `*current-warning-prefix*' has an appropriate value. */
  673. res = scm_call_n (scm_variable_ref (compile_file), args, 5);
  674. scm_puts_unlocked (";;; compiled ", scm_current_error_port ());
  675. scm_display (res, scm_current_error_port ());
  676. scm_newline (scm_current_error_port ());
  677. return res;
  678. }
  679. else
  680. {
  681. scm_puts_unlocked (";;; it seems ", scm_current_error_port ());
  682. scm_display (source, scm_current_error_port ());
  683. scm_puts_unlocked ("\n;;; is part of the compiler; skipping auto-compilation\n",
  684. scm_current_error_port ());
  685. return SCM_BOOL_F;
  686. }
  687. }
  688. static SCM
  689. auto_compile_catch_handler (void *data, SCM tag, SCM throw_args)
  690. {
  691. SCM source = SCM_PACK_POINTER (data);
  692. SCM oport, lines;
  693. oport = scm_open_output_string ();
  694. scm_print_exception (oport, SCM_BOOL_F, tag, throw_args);
  695. scm_puts_unlocked (";;; WARNING: compilation of ", scm_current_warning_port ());
  696. scm_display (source, scm_current_warning_port ());
  697. scm_puts_unlocked (" failed:\n", scm_current_warning_port ());
  698. lines = scm_string_split (scm_get_output_string (oport),
  699. SCM_MAKE_CHAR ('\n'));
  700. for (; scm_is_pair (lines); lines = scm_cdr (lines))
  701. if (scm_c_string_length (scm_car (lines)))
  702. {
  703. scm_puts_unlocked (";;; ", scm_current_warning_port ());
  704. scm_display (scm_car (lines), scm_current_warning_port ());
  705. scm_newline (scm_current_warning_port ());
  706. }
  707. scm_close_port (oport);
  708. return SCM_BOOL_F;
  709. }
  710. SCM_DEFINE (scm_sys_warn_auto_compilation_enabled, "%warn-auto-compilation-enabled", 0, 0, 0,
  711. (void), "")
  712. #define FUNC_NAME s_scm_sys_warn_auto_compilation_enabled
  713. {
  714. static int message_shown = 0;
  715. if (!message_shown)
  716. {
  717. scm_puts_unlocked (";;; note: auto-compilation is enabled, set GUILE_AUTO_COMPILE=0\n"
  718. ";;; or pass the --no-auto-compile argument to disable.\n",
  719. scm_current_warning_port ());
  720. message_shown = 1;
  721. }
  722. return SCM_UNSPECIFIED;
  723. }
  724. #undef FUNC_NAME
  725. static SCM
  726. scm_try_auto_compile (SCM source)
  727. {
  728. if (scm_is_false (*scm_loc_load_should_auto_compile))
  729. return SCM_BOOL_F;
  730. scm_sys_warn_auto_compilation_enabled ();
  731. return scm_c_catch (SCM_BOOL_T,
  732. do_try_auto_compile,
  733. SCM_UNPACK_POINTER (source),
  734. auto_compile_catch_handler,
  735. SCM_UNPACK_POINTER (source),
  736. NULL, NULL);
  737. }
  738. /* The auto-compilation code will residualize a .go file in the cache
  739. dir: by default, $HOME/.cache/guile/2.0/ccache/PATH.go. This
  740. function determines the PATH to use as a key into the compilation
  741. cache. See also (system base compile):compiled-file-name. */
  742. static SCM
  743. canonical_suffix (SCM fname)
  744. {
  745. SCM canon;
  746. /* CANON should be absolute. */
  747. canon = scm_canonicalize_path (fname);
  748. #ifdef __MINGW32__
  749. {
  750. size_t len = scm_c_string_length (canon);
  751. /* On Windows, an absolute file name that doesn't start with a
  752. separator starts with a drive component. Transform the drive
  753. component to a file name element: c:\foo -> \c\foo. */
  754. if (len >= 2
  755. && is_absolute_file_name (canon)
  756. && !is_file_name_separator (scm_c_string_ref (canon, 0)))
  757. return scm_string_append
  758. (scm_list_3 (scm_from_latin1_string (FILE_NAME_SEPARATOR_STRING),
  759. scm_c_substring (canon, 0, 1),
  760. scm_c_substring (canon, 2, len)));
  761. }
  762. #endif
  763. return canon;
  764. }
  765. SCM_DEFINE (scm_primitive_load_path, "primitive-load-path", 0, 0, 1,
  766. (SCM args),
  767. "Search @var{%load-path} for the file named @var{filename} and\n"
  768. "load it into the top-level environment.\n\n"
  769. "If @var{filename} is a relative pathname and is not found in\n"
  770. "the list of search paths, one of three things may happen,\n"
  771. "depending on the optional second argument,\n"
  772. "@var{exception_on_not_found}. If it is @code{#f}, @code{#f}\n"
  773. "will be returned. If it is a procedure, it will be called\n"
  774. "with no arguments. Otherwise an error is signalled.")
  775. #define FUNC_NAME s_scm_primitive_load_path
  776. {
  777. SCM filename, exception_on_not_found;
  778. SCM full_filename, compiled_filename;
  779. int compiled_is_fallback = 0;
  780. SCM hook = *scm_loc_load_hook;
  781. struct stat stat_source, stat_compiled;
  782. if (scm_is_true (hook) && scm_is_false (scm_procedure_p (hook)))
  783. SCM_MISC_ERROR ("value of %load-hook is neither a procedure nor #f",
  784. SCM_EOL);
  785. if (scm_is_string (args))
  786. {
  787. /* C code written for 1.8 and earlier expects this function to take a
  788. single argument (the file name). */
  789. filename = args;
  790. exception_on_not_found = SCM_UNDEFINED;
  791. }
  792. else
  793. {
  794. /* Starting from 1.9, this function takes 1 required and 1 optional
  795. argument. */
  796. long len;
  797. SCM_VALIDATE_LIST_COPYLEN (SCM_ARG1, args, len);
  798. if (len < 1 || len > 2)
  799. scm_error_num_args_subr (FUNC_NAME);
  800. filename = SCM_CAR (args);
  801. SCM_VALIDATE_STRING (SCM_ARG1, filename);
  802. exception_on_not_found = len > 1 ? SCM_CADR (args) : SCM_UNDEFINED;
  803. }
  804. if (SCM_UNBNDP (exception_on_not_found))
  805. exception_on_not_found = SCM_BOOL_T;
  806. full_filename = search_path (*scm_loc_load_path, filename,
  807. *scm_loc_load_extensions, SCM_BOOL_F,
  808. &stat_source);
  809. compiled_filename =
  810. search_path (*scm_loc_load_compiled_path, filename,
  811. *scm_loc_load_compiled_extensions, SCM_BOOL_T,
  812. &stat_compiled);
  813. if (scm_is_false (compiled_filename)
  814. && scm_is_true (full_filename)
  815. && scm_is_true (*scm_loc_compile_fallback_path)
  816. && scm_is_false (*scm_loc_fresh_auto_compile)
  817. && scm_is_pair (*scm_loc_load_compiled_extensions)
  818. && scm_is_string (scm_car (*scm_loc_load_compiled_extensions)))
  819. {
  820. SCM fallback;
  821. char *fallback_chars;
  822. fallback = scm_string_append
  823. (scm_list_3 (*scm_loc_compile_fallback_path,
  824. canonical_suffix (full_filename),
  825. scm_car (*scm_loc_load_compiled_extensions)));
  826. fallback_chars = scm_to_locale_string (fallback);
  827. if (stat (fallback_chars, &stat_compiled) == 0)
  828. {
  829. compiled_filename = fallback;
  830. compiled_is_fallback = 1;
  831. }
  832. free (fallback_chars);
  833. }
  834. if (scm_is_false (full_filename) && scm_is_false (compiled_filename))
  835. {
  836. if (scm_is_true (scm_procedure_p (exception_on_not_found)))
  837. return scm_call_0 (exception_on_not_found);
  838. else if (scm_is_false (exception_on_not_found))
  839. return SCM_BOOL_F;
  840. else
  841. SCM_MISC_ERROR ("Unable to find file ~S in load path",
  842. scm_list_1 (filename));
  843. }
  844. if (!scm_is_false (hook))
  845. scm_call_1 (hook, (scm_is_true (full_filename)
  846. ? full_filename : compiled_filename));
  847. if (scm_is_false (full_filename)
  848. || (scm_is_true (compiled_filename)
  849. && compiled_is_fresh (full_filename, compiled_filename,
  850. &stat_source, &stat_compiled)))
  851. return scm_load_compiled_with_vm (compiled_filename);
  852. /* Perhaps there was the installed .go that was stale, but our fallback is
  853. fresh. Let's try that. Duplicating code, but perhaps that's OK. */
  854. if (!compiled_is_fallback
  855. && scm_is_true (*scm_loc_compile_fallback_path)
  856. && scm_is_false (*scm_loc_fresh_auto_compile)
  857. && scm_is_pair (*scm_loc_load_compiled_extensions)
  858. && scm_is_string (scm_car (*scm_loc_load_compiled_extensions)))
  859. {
  860. SCM fallback;
  861. char *fallback_chars;
  862. int stat_ret;
  863. fallback = scm_string_append
  864. (scm_list_3 (*scm_loc_compile_fallback_path,
  865. canonical_suffix (full_filename),
  866. scm_car (*scm_loc_load_compiled_extensions)));
  867. fallback_chars = scm_to_locale_string (fallback);
  868. stat_ret = stat (fallback_chars, &stat_compiled);
  869. free (fallback_chars);
  870. if (stat_ret == 0 && compiled_is_fresh (full_filename, fallback,
  871. &stat_source, &stat_compiled))
  872. {
  873. scm_puts_unlocked (";;; found fresh local cache at ", scm_current_warning_port ());
  874. scm_display (fallback, scm_current_warning_port ());
  875. scm_newline (scm_current_warning_port ());
  876. return scm_load_compiled_with_vm (fallback);
  877. }
  878. }
  879. /* Otherwise, we bottom out here. */
  880. {
  881. SCM freshly_compiled = scm_try_auto_compile (full_filename);
  882. if (scm_is_true (freshly_compiled))
  883. return scm_load_compiled_with_vm (freshly_compiled);
  884. else
  885. return scm_primitive_load (full_filename);
  886. }
  887. }
  888. #undef FUNC_NAME
  889. SCM
  890. scm_c_primitive_load_path (const char *filename)
  891. {
  892. return scm_primitive_load_path (scm_from_locale_string (filename));
  893. }
  894. void
  895. scm_init_eval_in_scheme (void)
  896. {
  897. SCM eval_scm, eval_go;
  898. struct stat stat_source, stat_compiled;
  899. eval_scm = search_path (*scm_loc_load_path,
  900. scm_from_locale_string ("ice-9/eval.scm"),
  901. SCM_EOL, SCM_BOOL_F, &stat_source);
  902. eval_go = search_path (*scm_loc_load_compiled_path,
  903. scm_from_locale_string ("ice-9/eval.go"),
  904. SCM_EOL, SCM_BOOL_F, &stat_compiled);
  905. if (scm_is_true (eval_scm) && scm_is_true (eval_go)
  906. && compiled_is_fresh (eval_scm, eval_go,
  907. &stat_source, &stat_compiled))
  908. scm_load_compiled_with_vm (eval_go);
  909. else
  910. /* if we have no eval.go, we shouldn't load any compiled code at all */
  911. *scm_loc_load_compiled_path = SCM_EOL;
  912. }
  913. /* Information about the build environment. */
  914. SCM_VARIABLE_INIT (sys_host_type, "%host-type",
  915. scm_from_locale_string (HOST_TYPE));
  916. /* Initialize the scheme variable %guile-build-info, based on data
  917. provided by the Makefile, via libpath.h. */
  918. static void
  919. init_build_info ()
  920. {
  921. static struct { char *name; char *value; } info[] = SCM_BUILD_INFO;
  922. SCM *loc = SCM_VARIABLE_LOC (scm_c_define ("%guile-build-info", SCM_EOL));
  923. unsigned long i;
  924. for (i = 0; i < (sizeof (info) / sizeof (info[0])); i++)
  925. {
  926. SCM key = scm_from_utf8_symbol (info[i].name);
  927. SCM val = scm_from_locale_string (info[i].value);
  928. *loc = scm_acons (key, val, *loc);
  929. }
  930. #ifdef PACKAGE_PACKAGER
  931. *loc = scm_acons (scm_from_latin1_symbol ("packager"),
  932. scm_from_latin1_string (PACKAGE_PACKAGER),
  933. *loc);
  934. #endif
  935. #ifdef PACKAGE_PACKAGER_VERSION
  936. *loc = scm_acons (scm_from_latin1_symbol ("packager-version"),
  937. scm_from_latin1_string (PACKAGE_PACKAGER_VERSION),
  938. *loc);
  939. #endif
  940. #ifdef PACKAGE_PACKAGER_BUG_REPORTS
  941. *loc = scm_acons (scm_from_latin1_symbol ("packager-bug-reports"),
  942. scm_from_latin1_string (PACKAGE_PACKAGER_BUG_REPORTS),
  943. *loc);
  944. #endif
  945. }
  946. void
  947. scm_init_load ()
  948. {
  949. scm_listofnullstr = scm_list_1 (scm_nullstr);
  950. scm_loc_load_path = SCM_VARIABLE_LOC (scm_c_define ("%load-path", SCM_EOL));
  951. scm_loc_load_extensions
  952. = SCM_VARIABLE_LOC (scm_c_define ("%load-extensions",
  953. scm_list_2 (scm_from_locale_string (".scm"),
  954. scm_nullstr)));
  955. scm_loc_load_compiled_path
  956. = SCM_VARIABLE_LOC (scm_c_define ("%load-compiled-path", SCM_EOL));
  957. scm_loc_load_compiled_extensions
  958. = SCM_VARIABLE_LOC (scm_c_define ("%load-compiled-extensions",
  959. scm_list_1 (scm_from_locale_string (".go"))));
  960. scm_loc_load_hook = SCM_VARIABLE_LOC (scm_c_define ("%load-hook", SCM_BOOL_F));
  961. scm_loc_compile_fallback_path
  962. = SCM_VARIABLE_LOC (scm_c_define ("%compile-fallback-path", SCM_BOOL_F));
  963. scm_loc_load_should_auto_compile
  964. = SCM_VARIABLE_LOC (scm_c_define ("%load-should-auto-compile", SCM_BOOL_F));
  965. scm_loc_fresh_auto_compile
  966. = SCM_VARIABLE_LOC (scm_c_define ("%fresh-auto-compile", SCM_BOOL_F));
  967. scm_ellipsis = scm_from_latin1_string ("...");
  968. the_reader = scm_make_fluid_with_default (SCM_BOOL_F);
  969. scm_c_define("current-reader", the_reader);
  970. scm_c_define ("load-compiled",
  971. scm_c_make_gsubr ("load-compiled/vm", 1, 0, 0,
  972. scm_load_compiled_with_vm));
  973. init_build_info ();
  974. #include "libguile/load.x"
  975. }
  976. void
  977. scm_init_load_should_auto_compile ()
  978. {
  979. char *auto_compile = getenv ("GUILE_AUTO_COMPILE");
  980. if (auto_compile && strcmp (auto_compile, "0") == 0)
  981. {
  982. *scm_loc_load_should_auto_compile = SCM_BOOL_F;
  983. *scm_loc_fresh_auto_compile = SCM_BOOL_F;
  984. }
  985. /* Allow "freshen" also. */
  986. else if (auto_compile && strncmp (auto_compile, "fresh", 5) == 0)
  987. {
  988. *scm_loc_load_should_auto_compile = SCM_BOOL_T;
  989. *scm_loc_fresh_auto_compile = SCM_BOOL_T;
  990. }
  991. else
  992. {
  993. *scm_loc_load_should_auto_compile = SCM_BOOL_T;
  994. *scm_loc_fresh_auto_compile = SCM_BOOL_F;
  995. }
  996. }
  997. /*
  998. Local Variables:
  999. c-file-style: "gnu"
  1000. End:
  1001. */