load.c 36 KB

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