Renamed the "frames" that are related to dynamic-wind to "dynamic
[bpt/guile.git] / libguile / load.c
CommitLineData
cc95e00a 1/* Copyright (C) 1995,1996,1998,1999,2000,2001, 2004 Free Software Foundation, Inc.
0f2d19dd 2 *
73be1d9e
MV
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.
0f2d19dd 7 *
73be1d9e
MV
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.
0f2d19dd 12 *
73be1d9e
MV
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
92205699 15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
73be1d9e 16 */
1bbd0b84 17
1bbd0b84 18
0f2d19dd
JB
19\f
20
cbd41c89
RB
21#if HAVE_CONFIG_H
22# include <config.h>
23#endif
24
13070bd3 25#include <string.h>
7d04d68b 26#include <stdio.h>
13070bd3 27
a0599745
MD
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"
f33b174d 38#include "libguile/modules.h"
c96d76b8 39#include "libguile/lang.h"
7d04d68b 40#include "libguile/chars.h"
c44ca4fe 41#include "libguile/srfi-13.h"
a0599745
MD
42
43#include "libguile/validate.h"
44#include "libguile/load.h"
ec3a8ace 45#include "libguile/fluids.h"
06721500
JB
46
47#include <sys/types.h>
48#include <sys/stat.h>
49
50#ifdef HAVE_UNISTD_H
51#include <unistd.h>
52#endif /* HAVE_UNISTD_H */
53
54#ifndef R_OK
55#define R_OK 4
56#endif
0f2d19dd
JB
57
58\f
06721500 59/* Loading a file, given an absolute filename. */
0f2d19dd 60
26544b96
JB
61/* Hook to run when we load a file, perhaps to announce the fact somewhere.
62 Applied to the full name of the file. */
63static SCM *scm_loc_load_hook;
64
ec3a8ace
NJ
65/* The current reader (a fluid). */
66static SCM the_reader = SCM_BOOL_F;
67static size_t the_reader_fluid_num = 0;
68
3b3b36dd 69SCM_DEFINE (scm_primitive_load, "primitive-load", 1, 0, 0,
1bbd0b84 70 (SCM filename),
67e8151b
MG
71 "Load the file named @var{filename} and evaluate its contents in\n"
72 "the top-level environment. The load paths are not searched;\n"
73 "@var{filename} must either be a full pathname or be a pathname\n"
74 "relative to the current directory. If the variable\n"
75 "@code{%load-hook} is defined, it should be bound to a procedure\n"
76 "that will be called before any code is loaded. See the\n"
77 "documentation for @code{%load-hook} later in this section.")
1bbd0b84 78#define FUNC_NAME s_scm_primitive_load
0f2d19dd 79{
26544b96 80 SCM hook = *scm_loc_load_hook;
a6d9e5ab 81 SCM_VALIDATE_STRING (1, filename);
bc36d050 82 if (scm_is_true (hook) && scm_is_false (scm_procedure_p (hook)))
2ade72d7
DH
83 SCM_MISC_ERROR ("value of %load-hook is neither a procedure nor #f",
84 SCM_EOL);
26544b96 85
bc36d050 86 if (!scm_is_false (hook))
fdc28395 87 scm_call_1 (hook, filename);
26544b96 88
1bbd0b84 89 { /* scope */
b5623573 90 SCM port = scm_open_file (filename, scm_from_locale_string ("r"));
661ae7ab
MV
91 scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
92 scm_i_dynwind_current_load_port (port);
b5623573
MV
93
94 while (1)
95 {
ec3a8ace
NJ
96 SCM reader, form;
97
98 /* Lookup and use the current reader to read the next
99 expression. */
100 reader = SCM_FAST_FLUID_REF (the_reader_fluid_num);
101 if (reader == SCM_BOOL_F)
102 form = scm_read (port);
103 else
104 form = scm_call_1 (reader, port);
105
b5623573
MV
106 if (SCM_EOF_OBJECT_P (form))
107 break;
ec3a8ace 108
b5623573
MV
109 scm_primitive_eval_x (form);
110 }
111
661ae7ab 112 scm_dynwind_end ();
0f2d19dd
JB
113 scm_close_port (port);
114 }
b59b97ba 115 return SCM_UNSPECIFIED;
0f2d19dd 116}
1bbd0b84 117#undef FUNC_NAME
0f2d19dd 118
c519b272
MV
119SCM
120scm_c_primitive_load (const char *filename)
121{
cc95e00a 122 return scm_primitive_load (scm_from_locale_string (filename));
c519b272
MV
123}
124
0f2d19dd 125\f
3feedb00
MD
126/* Builtin path to scheme library files. */
127#ifdef SCM_PKGDATA_DIR
a1ec6916 128SCM_DEFINE (scm_sys_package_data_dir, "%package-data-dir", 0, 0, 0,
1bbd0b84 129 (),
b380b885
MD
130 "Return the name of the directory where Scheme packages, modules and\n"
131 "libraries are kept. On most Unix systems, this will be\n"
132 "@samp{/usr/local/share/guile}.")
1bbd0b84 133#define FUNC_NAME s_scm_sys_package_data_dir
3feedb00 134{
cc95e00a 135 return scm_from_locale_string (SCM_PKGDATA_DIR);
3feedb00 136}
1bbd0b84 137#undef FUNC_NAME
3feedb00
MD
138#endif /* SCM_PKGDATA_DIR */
139
e8e9b690 140#ifdef SCM_LIBRARY_DIR
a1ec6916 141SCM_DEFINE (scm_sys_library_dir, "%library-dir", 0,0,0,
e8e9b690 142 (),
b380b885
MD
143 "Return the directory where the Guile Scheme library files are installed.\n"
144 "E.g., may return \"/usr/share/guile/1.3.5\".")
e8e9b690
GB
145#define FUNC_NAME s_scm_sys_library_dir
146{
cc95e00a 147 return scm_from_locale_string (SCM_LIBRARY_DIR);
e8e9b690
GB
148}
149#undef FUNC_NAME
150#endif /* SCM_LIBRARY_DIR */
151
152#ifdef SCM_SITE_DIR
a1ec6916 153SCM_DEFINE (scm_sys_site_dir, "%site-dir", 0,0,0,
e8e9b690 154 (),
b380b885
MD
155 "Return the directory where the Guile site files are installed.\n"
156 "E.g., may return \"/usr/share/guile/site\".")
e8e9b690
GB
157#define FUNC_NAME s_scm_sys_site_dir
158{
cc95e00a 159 return scm_from_locale_string (SCM_SITE_DIR);
e8e9b690
GB
160}
161#undef FUNC_NAME
162#endif /* SCM_SITE_DIR */
163
164
165
3feedb00 166\f
06721500 167/* Initializing the load path, and searching it. */
0f2d19dd 168
26544b96 169/* List of names of directories we search for files to load. */
06721500
JB
170static SCM *scm_loc_load_path;
171
26544b96
JB
172/* List of extensions we try adding to the filenames. */
173static SCM *scm_loc_load_extensions;
174
01cddfc1 175
a1ec6916 176SCM_DEFINE (scm_parse_path, "parse-path", 1, 1, 0,
1bbd0b84 177 (SCM path, SCM tail),
d91788cb
MG
178 "Parse @var{path}, which is expected to be a colon-separated\n"
179 "string, into a list and return the resulting list with\n"
180 "@var{tail} appended. If @var{path} is @code{#f}, @var{tail}\n"
181 "is returned.")
1bbd0b84 182#define FUNC_NAME s_scm_parse_path
04e8fb0a 183{
7d04d68b
MV
184#ifdef __MINGW32__
185 SCM sep = SCM_MAKE_CHAR (';');
186#else
187 SCM sep = SCM_MAKE_CHAR (':');
188#endif
189
04e8fb0a
MD
190 if (SCM_UNBNDP (tail))
191 tail = SCM_EOL;
7888309b 192 return (scm_is_false (path)
04e8fb0a 193 ? tail
7d04d68b 194 : scm_append_x (scm_list_2 (scm_string_split (path, sep), tail)));
04e8fb0a 195}
1bbd0b84 196#undef FUNC_NAME
04e8fb0a
MD
197
198
06721500 199/* Initialize the global variable %load-path, given the value of the
3feedb00 200 SCM_SITE_DIR and SCM_LIBRARY_DIR preprocessor symbols and the
01cddfc1 201 GUILE_LOAD_PATH environment variable. */
0f2d19dd 202void
06721500
JB
203scm_init_load_path ()
204{
11c5e0bf 205 char *env;
06721500
JB
206 SCM path = SCM_EOL;
207
3feedb00 208#ifdef SCM_LIBRARY_DIR
cc95e00a
MV
209 path = scm_list_3 (scm_from_locale_string (SCM_SITE_DIR),
210 scm_from_locale_string (SCM_LIBRARY_DIR),
211 scm_from_locale_string (SCM_PKGDATA_DIR));
3feedb00 212#endif /* SCM_LIBRARY_DIR */
06721500 213
11c5e0bf
MV
214 env = getenv ("GUILE_LOAD_PATH");
215 if (env)
216 path = scm_parse_path (scm_from_locale_string (env), path);
01cddfc1 217
06721500
JB
218 *scm_loc_load_path = path;
219}
220
04e8fb0a 221SCM scm_listofnullstr;
06721500 222
7d04d68b
MV
223/* Utility functions for assembling C strings in a buffer.
224 */
225
226struct stringbuf {
227 char *buf, *ptr;
228 size_t buf_len;
229};
230
231static void
232stringbuf_free (void *data)
233{
234 struct stringbuf *buf = (struct stringbuf *)data;
235 free (buf->buf);
236}
237
238static void
239stringbuf_grow (struct stringbuf *buf)
240{
241 size_t ptroff = buf->ptr - buf->buf;
242 buf->buf_len *= 2;
7d04d68b
MV
243 buf->buf = scm_realloc (buf->buf, buf->buf_len);
244 buf->ptr = buf->buf + ptroff;
245}
246
247static void
248stringbuf_cat_locale_string (struct stringbuf *buf, SCM str)
249{
250 size_t max_len = buf->buf_len - (buf->ptr - buf->buf) - 1;
251 size_t len = scm_to_locale_stringbuf (str, buf->ptr, max_len);
252 if (len > max_len)
253 {
254 /* buffer is too small, double its size and try again.
255 */
256 stringbuf_grow (buf);
257 stringbuf_cat_locale_string (buf, str);
258 }
259 else
260 {
261 /* string fits, terminate it and check for embedded '\0'.
262 */
263 buf->ptr[len] = '\0';
264 if (strlen (buf->ptr) != len)
265 scm_misc_error (NULL,
266 "string contains #\\nul character: ~S",
267 scm_list_1 (str));
268 buf->ptr += len;
269 }
270}
271
272static void
273stringbuf_cat (struct stringbuf *buf, char *str)
274{
275 size_t max_len = buf->buf_len - (buf->ptr - buf->buf) - 1;
276 size_t len = strlen (str);
277 if (len > max_len)
278 {
279 /* buffer is too small, double its size and try again.
280 */
281 stringbuf_grow (buf);
282 stringbuf_cat (buf, str);
283 }
284 else
285 {
286 /* string fits, copy it into buffer.
287 */
288 strcpy (buf->ptr, str);
289 buf->ptr += len;
290 }
291}
292
293
04e8fb0a 294/* Search PATH for a directory containing a file named FILENAME.
06721500 295 The file must be readable, and not a directory.
26544b96 296 If we find one, return its full filename; otherwise, return #f.
56384176
JB
297 If FILENAME is absolute, return it unchanged.
298 If given, EXTENSIONS is a list of strings; for each directory
299 in PATH, we search for FILENAME concatenated with each EXTENSION. */
3b3b36dd 300SCM_DEFINE (scm_search_path, "search-path", 2, 1, 0,
1bbd0b84 301 (SCM path, SCM filename, SCM extensions),
d91788cb
MG
302 "Search @var{path} for a directory containing a file named\n"
303 "@var{filename}. The file must be readable, and not a directory.\n"
304 "If we find one, return its full filename; otherwise, return\n"
305 "@code{#f}. If @var{filename} is absolute, return it unchanged.\n"
306 "If given, @var{extensions} is a list of strings; for each\n"
307 "directory in @var{path}, we search for @var{filename}\n"
308 "concatenated with each @var{extension}.")
1bbd0b84 309#define FUNC_NAME s_scm_search_path
06721500 310{
7d04d68b 311 struct stringbuf buf;
56384176 312 char *filename_chars;
7d04d68b
MV
313 size_t filename_len;
314 SCM result = SCM_BOOL_F;
06721500 315
04e8fb0a 316 if (SCM_UNBNDP (extensions))
56384176 317 extensions = SCM_EOL;
56384176 318
661ae7ab 319 scm_dynwind_begin (0);
7d04d68b
MV
320
321 filename_chars = scm_to_locale_string (filename);
322 filename_len = strlen (filename_chars);
661ae7ab 323 scm_dynwind_free (filename_chars);
06721500 324
26544b96 325 /* If FILENAME is absolute, return it unchanged. */
2e945bcc
SJ
326#ifdef __MINGW32__
327 if (((filename_len >= 1) &&
328 (filename_chars[0] == '/' || filename_chars[0] == '\\')) ||
329 ((filename_len >= 3) && filename_chars[1] == ':' &&
330 ((filename_chars[0] >= 'a' && filename_chars[0] <= 'z') ||
331 (filename_chars[0] >= 'A' && filename_chars[0] <= 'Z')) &&
332 (filename_chars[2] == '/' || filename_chars[2] == '\\')))
333#else
56384176 334 if (filename_len >= 1 && filename_chars[0] == '/')
2e945bcc 335#endif
7d04d68b 336 {
661ae7ab 337 scm_dynwind_end ();
7d04d68b
MV
338 return filename;
339 }
26544b96 340
56384176
JB
341 /* If FILENAME has an extension, don't try to add EXTENSIONS to it. */
342 {
343 char *endp;
344
345 for (endp = filename_chars + filename_len - 1;
346 endp >= filename_chars;
347 endp--)
348 {
349 if (*endp == '.')
350 {
351 /* This filename already has an extension, so cancel the
352 list of extensions. */
353 extensions = SCM_EOL;
354 break;
355 }
2e945bcc
SJ
356#ifdef __MINGW32__
357 else if (*endp == '/' || *endp == '\\')
358#else
56384176 359 else if (*endp == '/')
2e945bcc 360#endif
56384176
JB
361 /* This filename has no extension, so keep the current list
362 of extensions. */
363 break;
364 }
365 }
366
7d04d68b
MV
367 /* This simplifies the loop below a bit.
368 */
d2e53ed6 369 if (scm_is_null (extensions))
7d04d68b 370 extensions = scm_listofnullstr;
06721500 371
7d04d68b
MV
372 buf.buf_len = 512;
373 buf.buf = scm_malloc (buf.buf_len);
661ae7ab 374 scm_dynwind_unwind_handler (stringbuf_free, &buf, SCM_F_WIND_EXPLICITLY);
0a74e31d 375
7d04d68b
MV
376 /* Try every path element.
377 */
d2e53ed6 378 for (; scm_is_pair (path); path = SCM_CDR (path))
7d04d68b
MV
379 {
380 SCM dir = SCM_CAR (path);
381 SCM exts;
382 size_t sans_ext_len;
383
384 buf.ptr = buf.buf;
385 stringbuf_cat_locale_string (&buf, dir);
386
387 /* Concatenate the path name and the filename. */
388
2e945bcc 389#ifdef __MINGW32__
5a6d139b 390 if ((buf.ptr > buf.buf) && (buf.ptr[-1] != '/') && (buf.ptr[-1] != '\\'))
2e945bcc 391#else
5a6d139b 392 if ((buf.ptr > buf.buf) && (buf.ptr[-1] != '/'))
2e945bcc 393#endif
7d04d68b
MV
394 stringbuf_cat (&buf, "/");
395
396 stringbuf_cat (&buf, filename_chars);
397 sans_ext_len = buf.ptr - buf.buf;
398
399 /* Try every extension. */
d2e53ed6 400 for (exts = extensions; scm_is_pair (exts); exts = SCM_CDR (exts))
7d04d68b
MV
401 {
402 SCM ext = SCM_CAR (exts);
403 struct stat mode;
404
405 buf.ptr = buf.buf + sans_ext_len;
406 stringbuf_cat_locale_string (&buf, ext);
407
408 /* If the file exists at all, we should return it. If the
409 file is inaccessible, then that's an error. */
410
7d04d68b
MV
411 if (stat (buf.buf, &mode) == 0
412 && ! (mode.st_mode & S_IFDIR))
413 {
414 result = scm_from_locale_string (buf.buf);
415 goto end;
416 }
417 }
418
419 if (!SCM_NULL_OR_NIL_P (exts))
420 scm_wrong_type_arg_msg (NULL, 0, extensions, "proper list");
421 }
56384176 422
7d04d68b
MV
423 if (!SCM_NULL_OR_NIL_P (path))
424 scm_wrong_type_arg_msg (NULL, 0, path, "proper list");
56384176 425
7d04d68b 426 end:
661ae7ab 427 scm_dynwind_end ();
7d04d68b 428 return result;
06721500 429}
1bbd0b84 430#undef FUNC_NAME
06721500
JB
431
432
04e8fb0a
MD
433/* Search %load-path for a directory containing a file named FILENAME.
434 The file must be readable, and not a directory.
435 If we find one, return its full filename; otherwise, return #f.
436 If FILENAME is absolute, return it unchanged. */
3b3b36dd 437SCM_DEFINE (scm_sys_search_load_path, "%search-load-path", 1, 0, 0,
67e8151b
MG
438 (SCM filename),
439 "Search @var{%load-path} for the file named @var{filename},\n"
440 "which must be readable by the current user. If @var{filename}\n"
441 "is found in the list of paths to search or is an absolute\n"
442 "pathname, return its full pathname. Otherwise, return\n"
443 "@code{#f}. Filenames may have any of the optional extensions\n"
444 "in the @code{%load-extensions} list; @code{%search-load-path}\n"
445 "will try each extension automatically.")
1bbd0b84 446#define FUNC_NAME s_scm_sys_search_load_path
04e8fb0a
MD
447{
448 SCM path = *scm_loc_load_path;
449 SCM exts = *scm_loc_load_extensions;
a6d9e5ab 450 SCM_VALIDATE_STRING (1, filename);
1bbd0b84 451
2ade72d7
DH
452 if (scm_ilength (path) < 0)
453 SCM_MISC_ERROR ("%load-path is not a proper list", SCM_EOL);
454 if (scm_ilength (exts) < 0)
455 SCM_MISC_ERROR ("%load-extension list is not a proper list", SCM_EOL);
1bbd0b84 456 return scm_search_path (path, filename, exts);
04e8fb0a 457}
1bbd0b84 458#undef FUNC_NAME
04e8fb0a
MD
459
460
3b3b36dd 461SCM_DEFINE (scm_primitive_load_path, "primitive-load-path", 1, 0, 0,
67e8151b
MG
462 (SCM filename),
463 "Search @var{%load-path} for the file named @var{filename} and\n"
464 "load it into the top-level environment. If @var{filename} is a\n"
465 "relative pathname and is not found in the list of search paths,\n"
466 "an error is signalled.")
1bbd0b84 467#define FUNC_NAME s_scm_primitive_load_path
06721500 468{
26544b96
JB
469 SCM full_filename;
470
26544b96
JB
471 full_filename = scm_sys_search_load_path (filename);
472
7888309b 473 if (scm_is_false (full_filename))
ffa747a6
MV
474 SCM_MISC_ERROR ("Unable to find file ~S in load path",
475 scm_list_1 (filename));
26544b96 476
deca31e1 477 return scm_primitive_load (full_filename);
06721500 478}
1bbd0b84 479#undef FUNC_NAME
06721500 480
c519b272
MV
481SCM
482scm_c_primitive_load_path (const char *filename)
483{
cc95e00a 484 return scm_primitive_load_path (scm_from_locale_string (filename));
c519b272
MV
485}
486
06721500 487\f
e151bee6 488/* Information about the build environment. */
06721500 489
e151bee6
JB
490/* Initialize the scheme variable %guile-build-info, based on data
491 provided by the Makefile, via libpath.h. */
492static void
493init_build_info ()
494{
495 static struct { char *name; char *value; } info[] = SCM_BUILD_INFO;
86d31dfe 496 SCM *loc = SCM_VARIABLE_LOC (scm_c_define ("%guile-build-info", SCM_EOL));
c014a02e 497 unsigned long i;
e151bee6
JB
498
499 for (i = 0; i < (sizeof (info) / sizeof (info[0])); i++)
cc95e00a
MV
500 {
501 SCM key = scm_from_locale_symbol (info[i].name);
502 SCM val = scm_from_locale_string (info[i].value);
503 *loc = scm_acons (key, val, *loc);
504 }
e151bee6
JB
505}
506
e151bee6 507\f
0f2d19dd
JB
508void
509scm_init_load ()
0f2d19dd 510{
1afff620 511 scm_listofnullstr = scm_permanent_object (scm_list_1 (scm_nullstr));
86d31dfe 512 scm_loc_load_path = SCM_VARIABLE_LOC (scm_c_define ("%load-path", SCM_EOL));
26544b96 513 scm_loc_load_extensions
86d31dfe 514 = SCM_VARIABLE_LOC (scm_c_define ("%load-extensions",
cc95e00a
MV
515 scm_list_2 (scm_from_locale_string (".scm"),
516 scm_nullstr)));
86d31dfe 517 scm_loc_load_hook = SCM_VARIABLE_LOC (scm_c_define ("%load-hook", SCM_BOOL_F));
06721500 518
ec3a8ace
NJ
519 the_reader = scm_make_fluid ();
520 the_reader_fluid_num = SCM_FLUID_NUM (the_reader);
521 SCM_FAST_FLUID_SET_X (the_reader_fluid_num, SCM_BOOL_F);
522 scm_c_define("current-reader", the_reader);
523
e151bee6
JB
524 init_build_info ();
525
a0599745 526#include "libguile/load.x"
0f2d19dd 527}
89e00824
ML
528
529/*
530 Local Variables:
531 c-file-style: "gnu"
532 End:
533*/