Merge commit 'origin/master' into vm
[bpt/guile.git] / libguile / load.c
1 /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2004, 2006 Free Software Foundation, Inc.
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18
19 \f
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <string.h>
26 #include <stdio.h>
27
28 #include "libguile/_scm.h"
29 #include "libguile/libpath.h"
30 #include "libguile/fports.h"
31 #include "libguile/read.h"
32 #include "libguile/eval.h"
33 #include "libguile/throw.h"
34 #include "libguile/alist.h"
35 #include "libguile/dynwind.h"
36 #include "libguile/root.h"
37 #include "libguile/strings.h"
38 #include "libguile/modules.h"
39 #include "libguile/lang.h"
40 #include "libguile/chars.h"
41 #include "libguile/srfi-13.h"
42
43 #include "libguile/validate.h"
44 #include "libguile/load.h"
45 #include "libguile/fluids.h"
46
47 #include "libguile/vm.h" /* for load-compiled/vm */
48
49 #include <sys/types.h>
50 #include <sys/stat.h>
51
52 #ifdef HAVE_UNISTD_H
53 #include <unistd.h>
54 #endif /* HAVE_UNISTD_H */
55
56 #ifndef R_OK
57 #define R_OK 4
58 #endif
59
60 \f
61 /* Loading a file, given an absolute filename. */
62
63 /* Hook to run when we load a file, perhaps to announce the fact somewhere.
64 Applied to the full name of the file. */
65 static SCM *scm_loc_load_hook;
66
67 /* The current reader (a fluid). */
68 static SCM the_reader = SCM_BOOL_F;
69 static size_t the_reader_fluid_num = 0;
70
71 SCM_DEFINE (scm_primitive_load, "primitive-load", 1, 0, 0,
72 (SCM filename),
73 "Load the file named @var{filename} and evaluate its contents in\n"
74 "the top-level environment. The load paths are not searched;\n"
75 "@var{filename} must either be a full pathname or be a pathname\n"
76 "relative to the current directory. If the variable\n"
77 "@code{%load-hook} is defined, it should be bound to a procedure\n"
78 "that will be called before any code is loaded. See the\n"
79 "documentation for @code{%load-hook} later in this section.")
80 #define FUNC_NAME s_scm_primitive_load
81 {
82 SCM hook = *scm_loc_load_hook;
83 SCM_VALIDATE_STRING (1, filename);
84 if (scm_is_true (hook) && scm_is_false (scm_procedure_p (hook)))
85 SCM_MISC_ERROR ("value of %load-hook is neither a procedure nor #f",
86 SCM_EOL);
87
88 if (!scm_is_false (hook))
89 scm_call_1 (hook, filename);
90
91 { /* scope */
92 SCM port = scm_open_file (filename, scm_from_locale_string ("r"));
93 scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
94 scm_i_dynwind_current_load_port (port);
95
96 while (1)
97 {
98 SCM reader, form;
99
100 /* Lookup and use the current reader to read the next
101 expression. */
102 reader = SCM_FAST_FLUID_REF (the_reader_fluid_num);
103 if (reader == SCM_BOOL_F)
104 form = scm_read (port);
105 else
106 form = scm_call_1 (reader, port);
107
108 if (SCM_EOF_OBJECT_P (form))
109 break;
110
111 scm_primitive_eval_x (form);
112 }
113
114 scm_dynwind_end ();
115 scm_close_port (port);
116 }
117 return SCM_UNSPECIFIED;
118 }
119 #undef FUNC_NAME
120
121 SCM
122 scm_c_primitive_load (const char *filename)
123 {
124 return scm_primitive_load (scm_from_locale_string (filename));
125 }
126
127 \f
128 /* Builtin path to scheme library files. */
129 #ifdef SCM_PKGDATA_DIR
130 SCM_DEFINE (scm_sys_package_data_dir, "%package-data-dir", 0, 0, 0,
131 (),
132 "Return the name of the directory where Scheme packages, modules and\n"
133 "libraries are kept. On most Unix systems, this will be\n"
134 "@samp{/usr/local/share/guile}.")
135 #define FUNC_NAME s_scm_sys_package_data_dir
136 {
137 return scm_from_locale_string (SCM_PKGDATA_DIR);
138 }
139 #undef FUNC_NAME
140 #endif /* SCM_PKGDATA_DIR */
141
142 #ifdef SCM_LIBRARY_DIR
143 SCM_DEFINE (scm_sys_library_dir, "%library-dir", 0,0,0,
144 (),
145 "Return the directory where the Guile Scheme library files are installed.\n"
146 "E.g., may return \"/usr/share/guile/1.3.5\".")
147 #define FUNC_NAME s_scm_sys_library_dir
148 {
149 return scm_from_locale_string (SCM_LIBRARY_DIR);
150 }
151 #undef FUNC_NAME
152 #endif /* SCM_LIBRARY_DIR */
153
154 #ifdef SCM_SITE_DIR
155 SCM_DEFINE (scm_sys_site_dir, "%site-dir", 0,0,0,
156 (),
157 "Return the directory where the Guile site files are installed.\n"
158 "E.g., may return \"/usr/share/guile/site\".")
159 #define FUNC_NAME s_scm_sys_site_dir
160 {
161 return scm_from_locale_string (SCM_SITE_DIR);
162 }
163 #undef FUNC_NAME
164 #endif /* SCM_SITE_DIR */
165
166
167
168 \f
169 /* Initializing the load path, and searching it. */
170
171 /* List of names of directories we search for files to load. */
172 static SCM *scm_loc_load_path;
173
174 /* List of extensions we try adding to the filenames. */
175 static SCM *scm_loc_load_extensions;
176
177 /* Like %load-extensions, but for compiled files. */
178 static SCM *scm_loc_load_compiled_extensions;
179
180
181 SCM_DEFINE (scm_parse_path, "parse-path", 1, 1, 0,
182 (SCM path, SCM tail),
183 "Parse @var{path}, which is expected to be a colon-separated\n"
184 "string, into a list and return the resulting list with\n"
185 "@var{tail} appended. If @var{path} is @code{#f}, @var{tail}\n"
186 "is returned.")
187 #define FUNC_NAME s_scm_parse_path
188 {
189 #ifdef __MINGW32__
190 SCM sep = SCM_MAKE_CHAR (';');
191 #else
192 SCM sep = SCM_MAKE_CHAR (':');
193 #endif
194
195 if (SCM_UNBNDP (tail))
196 tail = SCM_EOL;
197 return (scm_is_false (path)
198 ? tail
199 : scm_append_x (scm_list_2 (scm_string_split (path, sep), tail)));
200 }
201 #undef FUNC_NAME
202
203
204 /* Initialize the global variable %load-path, given the value of the
205 SCM_SITE_DIR and SCM_LIBRARY_DIR preprocessor symbols and the
206 GUILE_LOAD_PATH environment variable. */
207 void
208 scm_init_load_path ()
209 {
210 char *env;
211 SCM path = SCM_EOL;
212
213 #ifdef SCM_LIBRARY_DIR
214 env = getenv ("GUILE_SYSTEM_PATH");
215 if (env && strcmp (env, "") == 0)
216 /* special-case interpret system-path=="" as meaning no system path instead
217 of '("") */
218 ;
219 else if (env)
220 path = scm_parse_path (scm_from_locale_string (env), path);
221 else
222 path = scm_list_3 (scm_from_locale_string (SCM_SITE_DIR),
223 scm_from_locale_string (SCM_LIBRARY_DIR),
224 scm_from_locale_string (SCM_PKGDATA_DIR));
225 #endif /* SCM_LIBRARY_DIR */
226
227 env = getenv ("GUILE_LOAD_PATH");
228 if (env)
229 path = scm_parse_path (scm_from_locale_string (env), path);
230
231 *scm_loc_load_path = path;
232 }
233
234 SCM scm_listofnullstr;
235
236 /* Utility functions for assembling C strings in a buffer.
237 */
238
239 struct stringbuf {
240 char *buf, *ptr;
241 size_t buf_len;
242 };
243
244 static void
245 stringbuf_free (void *data)
246 {
247 struct stringbuf *buf = (struct stringbuf *)data;
248 free (buf->buf);
249 }
250
251 static void
252 stringbuf_grow (struct stringbuf *buf)
253 {
254 size_t ptroff = buf->ptr - buf->buf;
255 buf->buf_len *= 2;
256 buf->buf = scm_realloc (buf->buf, buf->buf_len);
257 buf->ptr = buf->buf + ptroff;
258 }
259
260 static void
261 stringbuf_cat_locale_string (struct stringbuf *buf, SCM str)
262 {
263 size_t max_len = buf->buf_len - (buf->ptr - buf->buf) - 1;
264 size_t len = scm_to_locale_stringbuf (str, buf->ptr, max_len);
265 if (len > max_len)
266 {
267 /* buffer is too small, double its size and try again.
268 */
269 stringbuf_grow (buf);
270 stringbuf_cat_locale_string (buf, str);
271 }
272 else
273 {
274 /* string fits, terminate it and check for embedded '\0'.
275 */
276 buf->ptr[len] = '\0';
277 if (strlen (buf->ptr) != len)
278 scm_misc_error (NULL,
279 "string contains #\\nul character: ~S",
280 scm_list_1 (str));
281 buf->ptr += len;
282 }
283 }
284
285 static void
286 stringbuf_cat (struct stringbuf *buf, char *str)
287 {
288 size_t max_len = buf->buf_len - (buf->ptr - buf->buf) - 1;
289 size_t len = strlen (str);
290 if (len > max_len)
291 {
292 /* buffer is too small, double its size and try again.
293 */
294 stringbuf_grow (buf);
295 stringbuf_cat (buf, str);
296 }
297 else
298 {
299 /* string fits, copy it into buffer.
300 */
301 strcpy (buf->ptr, str);
302 buf->ptr += len;
303 }
304 }
305
306
307 static int
308 scm_c_string_has_an_ext (char *str, size_t len, SCM extensions)
309 {
310 for (; !scm_is_null (extensions); extensions = SCM_CDR (extensions))
311 {
312 char *ext;
313 size_t extlen;
314 int match;
315 ext = scm_to_locale_string (SCM_CAR (extensions));
316 extlen = strlen (ext);
317 match = (len > extlen && str[len - extlen - 1] == '.'
318 && strncmp (str + (len - extlen), ext, extlen) == 0);
319 free (ext);
320 if (match)
321 return 1;
322 }
323 return 0;
324 }
325
326 /* Search PATH for a directory containing a file named FILENAME.
327 The file must be readable, and not a directory.
328 If we find one, return its full filename; otherwise, return #f.
329 If FILENAME is absolute, return it unchanged.
330 If given, EXTENSIONS is a list of strings; for each directory
331 in PATH, we search for FILENAME concatenated with each EXTENSION. */
332 SCM_DEFINE (scm_search_path, "search-path", 2, 2, 0,
333 (SCM path, SCM filename, SCM extensions, SCM require_exts),
334 "Search @var{path} for a directory containing a file named\n"
335 "@var{filename}. The file must be readable, and not a directory.\n"
336 "If we find one, return its full filename; otherwise, return\n"
337 "@code{#f}. If @var{filename} is absolute, return it unchanged.\n"
338 "If given, @var{extensions} is a list of strings; for each\n"
339 "directory in @var{path}, we search for @var{filename}\n"
340 "concatenated with each @var{extension}.")
341 #define FUNC_NAME s_scm_search_path
342 {
343 struct stringbuf buf;
344 char *filename_chars;
345 size_t filename_len;
346 SCM result = SCM_BOOL_F;
347
348 if (SCM_UNBNDP (extensions))
349 extensions = SCM_EOL;
350
351 if (SCM_UNBNDP (require_exts))
352 require_exts = SCM_BOOL_F;
353
354 scm_dynwind_begin (0);
355
356 filename_chars = scm_to_locale_string (filename);
357 filename_len = strlen (filename_chars);
358 scm_dynwind_free (filename_chars);
359
360 /* If FILENAME is absolute, return it unchanged. */
361 #ifdef __MINGW32__
362 if (((filename_len >= 1) &&
363 (filename_chars[0] == '/' || filename_chars[0] == '\\')) ||
364 ((filename_len >= 3) && filename_chars[1] == ':' &&
365 ((filename_chars[0] >= 'a' && filename_chars[0] <= 'z') ||
366 (filename_chars[0] >= 'A' && filename_chars[0] <= 'Z')) &&
367 (filename_chars[2] == '/' || filename_chars[2] == '\\')))
368 #else
369 if (filename_len >= 1 && filename_chars[0] == '/')
370 #endif
371 {
372 SCM res = filename;
373 if (scm_is_true (require_exts) &&
374 !scm_c_string_has_an_ext (filename_chars, filename_len,
375 extensions))
376 res = SCM_BOOL_F;
377
378 scm_dynwind_end ();
379 return res;
380 }
381
382 /* If FILENAME has an extension, don't try to add EXTENSIONS to it. */
383 {
384 char *endp;
385
386 for (endp = filename_chars + filename_len - 1;
387 endp >= filename_chars;
388 endp--)
389 {
390 if (*endp == '.')
391 {
392 if (scm_is_true (require_exts) &&
393 !scm_c_string_has_an_ext (filename_chars, filename_len,
394 extensions))
395 {
396 /* This filename has an extension, but not one of the right
397 ones... */
398 scm_dynwind_end ();
399 return SCM_BOOL_F;
400 }
401 /* This filename already has an extension, so cancel the
402 list of extensions. */
403 extensions = SCM_EOL;
404 break;
405 }
406 #ifdef __MINGW32__
407 else if (*endp == '/' || *endp == '\\')
408 #else
409 else if (*endp == '/')
410 #endif
411 /* This filename has no extension, so keep the current list
412 of extensions. */
413 break;
414 }
415 }
416
417 /* This simplifies the loop below a bit.
418 */
419 if (scm_is_null (extensions))
420 extensions = scm_listofnullstr;
421
422 buf.buf_len = 512;
423 buf.buf = scm_malloc (buf.buf_len);
424 scm_dynwind_unwind_handler (stringbuf_free, &buf, SCM_F_WIND_EXPLICITLY);
425
426 /* Try every path element.
427 */
428 for (; scm_is_pair (path); path = SCM_CDR (path))
429 {
430 SCM dir = SCM_CAR (path);
431 SCM exts;
432 size_t sans_ext_len;
433
434 buf.ptr = buf.buf;
435 stringbuf_cat_locale_string (&buf, dir);
436
437 /* Concatenate the path name and the filename. */
438
439 #ifdef __MINGW32__
440 if ((buf.ptr > buf.buf) && (buf.ptr[-1] != '/') && (buf.ptr[-1] != '\\'))
441 #else
442 if ((buf.ptr > buf.buf) && (buf.ptr[-1] != '/'))
443 #endif
444 stringbuf_cat (&buf, "/");
445
446 stringbuf_cat (&buf, filename_chars);
447 sans_ext_len = buf.ptr - buf.buf;
448
449 /* Try every extension. */
450 for (exts = extensions; scm_is_pair (exts); exts = SCM_CDR (exts))
451 {
452 SCM ext = SCM_CAR (exts);
453 struct stat mode;
454
455 buf.ptr = buf.buf + sans_ext_len;
456 stringbuf_cat_locale_string (&buf, ext);
457
458 /* If the file exists at all, we should return it. If the
459 file is inaccessible, then that's an error. */
460
461 if (stat (buf.buf, &mode) == 0
462 && ! (mode.st_mode & S_IFDIR))
463 {
464 result = scm_from_locale_string (buf.buf);
465 goto end;
466 }
467 }
468
469 if (!SCM_NULL_OR_NIL_P (exts))
470 scm_wrong_type_arg_msg (NULL, 0, extensions, "proper list");
471 }
472
473 if (!SCM_NULL_OR_NIL_P (path))
474 scm_wrong_type_arg_msg (NULL, 0, path, "proper list");
475
476 end:
477 scm_dynwind_end ();
478 return result;
479 }
480 #undef FUNC_NAME
481
482
483 /* Search %load-path for a directory containing a file named FILENAME.
484 The file must be readable, and not a directory.
485 If we find one, return its full filename; otherwise, return #f.
486 If FILENAME is absolute, return it unchanged. */
487 SCM_DEFINE (scm_sys_search_load_path, "%search-load-path", 1, 0, 0,
488 (SCM filename),
489 "Search @var{%load-path} for the file named @var{filename},\n"
490 "which must be readable by the current user. If @var{filename}\n"
491 "is found in the list of paths to search or is an absolute\n"
492 "pathname, return its full pathname. Otherwise, return\n"
493 "@code{#f}. Filenames may have any of the optional extensions\n"
494 "in the @code{%load-extensions} list; @code{%search-load-path}\n"
495 "will try each extension automatically.")
496 #define FUNC_NAME s_scm_sys_search_load_path
497 {
498 SCM path = *scm_loc_load_path;
499 SCM exts = *scm_loc_load_extensions;
500 SCM_VALIDATE_STRING (1, filename);
501
502 if (scm_ilength (path) < 0)
503 SCM_MISC_ERROR ("%load-path is not a proper list", SCM_EOL);
504 if (scm_ilength (exts) < 0)
505 SCM_MISC_ERROR ("%load-extension list is not a proper list", SCM_EOL);
506 return scm_search_path (path, filename, exts, SCM_UNDEFINED);
507 }
508 #undef FUNC_NAME
509
510
511 SCM_DEFINE (scm_primitive_load_path, "primitive-load-path", 1, 0, 0,
512 (SCM filename),
513 "Search @var{%load-path} for the file named @var{filename} and\n"
514 "load it into the top-level environment. If @var{filename} is a\n"
515 "relative pathname and is not found in the list of search paths,\n"
516 "an error is signalled.")
517 #define FUNC_NAME s_scm_primitive_load_path
518 {
519 SCM full_filename, compiled_filename;
520
521 full_filename = scm_sys_search_load_path (filename);
522 compiled_filename = scm_search_path (*scm_loc_load_path,
523 filename,
524 *scm_loc_load_compiled_extensions,
525 SCM_BOOL_T);
526
527 if (scm_is_false (full_filename) && scm_is_false (compiled_filename))
528 SCM_MISC_ERROR ("Unable to find file ~S in load path",
529 scm_list_1 (filename));
530
531 if (scm_is_false (compiled_filename))
532 return scm_primitive_load (full_filename);
533
534 if (scm_is_false (full_filename))
535 return scm_load_compiled_with_vm (compiled_filename);
536
537 {
538 char *source, *compiled;
539 struct stat stat_source, stat_compiled;
540
541 source = scm_to_locale_string (full_filename);
542 compiled = scm_to_locale_string (compiled_filename);
543
544 if (stat (source, &stat_source) == 0
545 && stat (compiled, &stat_compiled) == 0
546 && stat_source.st_mtime <= stat_compiled.st_mtime)
547 {
548 free (source);
549 free (compiled);
550 return scm_load_compiled_with_vm (compiled_filename);
551 }
552 else
553 {
554 scm_puts (";;; note: source file ", scm_current_error_port ());
555 scm_puts (source, scm_current_error_port ());
556 scm_puts (" newer than compiled ", scm_current_error_port ());
557 scm_puts (compiled, scm_current_error_port ());
558 scm_puts ("\n", scm_current_error_port ());
559 free (source);
560 free (compiled);
561 return scm_primitive_load (full_filename);
562 }
563 }
564 }
565 #undef FUNC_NAME
566
567 SCM
568 scm_c_primitive_load_path (const char *filename)
569 {
570 return scm_primitive_load_path (scm_from_locale_string (filename));
571 }
572
573 \f
574 /* Information about the build environment. */
575
576 /* Initialize the scheme variable %guile-build-info, based on data
577 provided by the Makefile, via libpath.h. */
578 static void
579 init_build_info ()
580 {
581 static struct { char *name; char *value; } info[] = SCM_BUILD_INFO;
582 SCM *loc = SCM_VARIABLE_LOC (scm_c_define ("%guile-build-info", SCM_EOL));
583 unsigned long i;
584
585 for (i = 0; i < (sizeof (info) / sizeof (info[0])); i++)
586 {
587 SCM key = scm_from_locale_symbol (info[i].name);
588 SCM val = scm_from_locale_string (info[i].value);
589 *loc = scm_acons (key, val, *loc);
590 }
591 }
592
593 \f
594 void
595 scm_init_load ()
596 {
597 scm_listofnullstr = scm_permanent_object (scm_list_1 (scm_nullstr));
598 scm_loc_load_path = SCM_VARIABLE_LOC (scm_c_define ("%load-path", SCM_EOL));
599 scm_loc_load_extensions
600 = SCM_VARIABLE_LOC (scm_c_define ("%load-extensions",
601 scm_list_2 (scm_from_locale_string (".scm"),
602 scm_nullstr)));
603 scm_loc_load_compiled_extensions
604 = SCM_VARIABLE_LOC (scm_c_define ("%load-compiled-extensions",
605 scm_list_1 (scm_from_locale_string (".go"))));
606 scm_loc_load_hook = SCM_VARIABLE_LOC (scm_c_define ("%load-hook", SCM_BOOL_F));
607
608 the_reader = scm_make_fluid ();
609 the_reader_fluid_num = SCM_FLUID_NUM (the_reader);
610 SCM_FAST_FLUID_SET_X (the_reader_fluid_num, SCM_BOOL_F);
611 scm_c_define("current-reader", the_reader);
612
613 init_build_info ();
614
615 #include "libguile/load.x"
616 }
617
618 /*
619 Local Variables:
620 c-file-style: "gnu"
621 End:
622 */