load.c 33 KB

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