*** empty log message ***
[bpt/guile.git] / libguile / load.c
CommitLineData
2ade72d7 1/* Copyright (C) 1995,1996,1998,1999,2000,2001 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
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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
DH
25#include <string.h>
26
a0599745
MD
27#include "libguile/_scm.h"
28#include "libguile/libpath.h"
29#include "libguile/fports.h"
30#include "libguile/read.h"
31#include "libguile/eval.h"
32#include "libguile/throw.h"
33#include "libguile/alist.h"
34#include "libguile/dynwind.h"
35#include "libguile/root.h"
36#include "libguile/strings.h"
f33b174d 37#include "libguile/modules.h"
c96d76b8 38#include "libguile/lang.h"
a0599745
MD
39
40#include "libguile/validate.h"
41#include "libguile/load.h"
06721500
JB
42
43#include <sys/types.h>
44#include <sys/stat.h>
45
46#ifdef HAVE_UNISTD_H
47#include <unistd.h>
48#endif /* HAVE_UNISTD_H */
49
50#ifndef R_OK
51#define R_OK 4
52#endif
0f2d19dd
JB
53
54\f
06721500 55/* Loading a file, given an absolute filename. */
0f2d19dd 56
26544b96
JB
57/* Hook to run when we load a file, perhaps to announce the fact somewhere.
58 Applied to the full name of the file. */
59static SCM *scm_loc_load_hook;
60
c7023c69 61static void
d7834b91
MD
62swap_port (void *data)
63{
64 SCM *save_port = data, tmp = scm_cur_loadp;
65 scm_cur_loadp = *save_port;
66 *save_port = tmp;
67}
68
69static SCM
70load (void *data)
71{
54778cd3 72 SCM port = SCM_PACK (data);
d7834b91
MD
73 while (1)
74 {
54778cd3 75 SCM form = scm_read (port);
d7834b91
MD
76 if (SCM_EOF_OBJECT_P (form))
77 break;
f1b7a066 78 scm_primitive_eval_x (form);
d7834b91
MD
79 }
80 return SCM_UNSPECIFIED;
81}
82
3b3b36dd 83SCM_DEFINE (scm_primitive_load, "primitive-load", 1, 0, 0,
1bbd0b84 84 (SCM filename),
67e8151b
MG
85 "Load the file named @var{filename} and evaluate its contents in\n"
86 "the top-level environment. The load paths are not searched;\n"
87 "@var{filename} must either be a full pathname or be a pathname\n"
88 "relative to the current directory. If the variable\n"
89 "@code{%load-hook} is defined, it should be bound to a procedure\n"
90 "that will be called before any code is loaded. See the\n"
91 "documentation for @code{%load-hook} later in this section.")
1bbd0b84 92#define FUNC_NAME s_scm_primitive_load
0f2d19dd 93{
26544b96 94 SCM hook = *scm_loc_load_hook;
a6d9e5ab 95 SCM_VALIDATE_STRING (1, filename);
7888309b 96 if (scm_is_true (hook) && !SCM_EQ_P (scm_procedure_p (hook), SCM_BOOL_T))
2ade72d7
DH
97 SCM_MISC_ERROR ("value of %load-hook is neither a procedure nor #f",
98 SCM_EOL);
26544b96 99
7888309b 100 if (! scm_is_false (hook))
fdc28395 101 scm_call_1 (hook, filename);
26544b96 102
1bbd0b84 103 { /* scope */
d7834b91 104 SCM port, save_port;
36284627 105 port = scm_open_file (filename, scm_mem2string ("r", sizeof (char)));
d7834b91
MD
106 save_port = port;
107 scm_internal_dynamic_wind (swap_port,
108 load,
109 swap_port,
54778cd3 110 (void *) SCM_UNPACK (port),
d7834b91 111 &save_port);
0f2d19dd
JB
112 scm_close_port (port);
113 }
b59b97ba 114 return SCM_UNSPECIFIED;
0f2d19dd 115}
1bbd0b84 116#undef FUNC_NAME
0f2d19dd 117
c519b272
MV
118SCM
119scm_c_primitive_load (const char *filename)
120{
121 return scm_primitive_load (scm_makfrom0str (filename));
122}
123
0f2d19dd 124\f
3feedb00
MD
125/* Builtin path to scheme library files. */
126#ifdef SCM_PKGDATA_DIR
a1ec6916 127SCM_DEFINE (scm_sys_package_data_dir, "%package-data-dir", 0, 0, 0,
1bbd0b84 128 (),
b380b885
MD
129 "Return the name of the directory where Scheme packages, modules and\n"
130 "libraries are kept. On most Unix systems, this will be\n"
131 "@samp{/usr/local/share/guile}.")
1bbd0b84 132#define FUNC_NAME s_scm_sys_package_data_dir
3feedb00
MD
133{
134 return scm_makfrom0str (SCM_PKGDATA_DIR);
135}
1bbd0b84 136#undef FUNC_NAME
3feedb00
MD
137#endif /* SCM_PKGDATA_DIR */
138
e8e9b690 139#ifdef SCM_LIBRARY_DIR
a1ec6916 140SCM_DEFINE (scm_sys_library_dir, "%library-dir", 0,0,0,
e8e9b690 141 (),
b380b885
MD
142 "Return the directory where the Guile Scheme library files are installed.\n"
143 "E.g., may return \"/usr/share/guile/1.3.5\".")
e8e9b690
GB
144#define FUNC_NAME s_scm_sys_library_dir
145{
146 return scm_makfrom0str(SCM_LIBRARY_DIR);
147}
148#undef FUNC_NAME
149#endif /* SCM_LIBRARY_DIR */
150
151#ifdef SCM_SITE_DIR
a1ec6916 152SCM_DEFINE (scm_sys_site_dir, "%site-dir", 0,0,0,
e8e9b690 153 (),
b380b885
MD
154 "Return the directory where the Guile site files are installed.\n"
155 "E.g., may return \"/usr/share/guile/site\".")
e8e9b690
GB
156#define FUNC_NAME s_scm_sys_site_dir
157{
158 return scm_makfrom0str(SCM_SITE_DIR);
159}
160#undef FUNC_NAME
161#endif /* SCM_SITE_DIR */
162
163
164
3feedb00 165\f
06721500 166/* Initializing the load path, and searching it. */
0f2d19dd 167
26544b96 168/* List of names of directories we search for files to load. */
06721500
JB
169static SCM *scm_loc_load_path;
170
26544b96
JB
171/* List of extensions we try adding to the filenames. */
172static SCM *scm_loc_load_extensions;
173
01cddfc1
JB
174
175/* Parse the null-terminated string PATH as if it were a standard path
176 environment variable (i.e. a colon-separated list of strings), and
177 prepend the elements to TAIL. */
178SCM
04e8fb0a 179scm_internal_parse_path (char *path, SCM tail)
01cddfc1
JB
180{
181 if (path && path[0] != '\0')
182 {
183 char *scan, *elt_end;
184
185 /* Scan backwards from the end of the string, to help
186 construct the list in the right order. */
187 scan = elt_end = path + strlen (path);
188 do {
189 /* Scan back to the beginning of the current element. */
190 do scan--;
2e945bcc
SJ
191#ifdef __MINGW32__
192 while (scan >= path && *scan != ';');
193#else
01cddfc1 194 while (scan >= path && *scan != ':');
2e945bcc 195#endif
36284627 196 tail = scm_cons (scm_mem2string (scan + 1, elt_end - (scan + 1)),
01cddfc1
JB
197 tail);
198 elt_end = scan;
199 } while (scan >= path);
200 }
201
202 return tail;
203}
204
205
a1ec6916 206SCM_DEFINE (scm_parse_path, "parse-path", 1, 1, 0,
1bbd0b84 207 (SCM path, SCM tail),
d91788cb
MG
208 "Parse @var{path}, which is expected to be a colon-separated\n"
209 "string, into a list and return the resulting list with\n"
210 "@var{tail} appended. If @var{path} is @code{#f}, @var{tail}\n"
211 "is returned.")
1bbd0b84 212#define FUNC_NAME s_scm_parse_path
04e8fb0a 213{
7888309b 214 SCM_ASSERT (scm_is_false (path) || (SCM_STRINGP (path)),
04e8fb0a 215 path,
1bbd0b84 216 SCM_ARG1, FUNC_NAME);
04e8fb0a
MD
217 if (SCM_UNBNDP (tail))
218 tail = SCM_EOL;
7888309b 219 return (scm_is_false (path)
04e8fb0a 220 ? tail
34f0f2b8 221 : scm_internal_parse_path (SCM_STRING_CHARS (path), tail));
04e8fb0a 222}
1bbd0b84 223#undef FUNC_NAME
04e8fb0a
MD
224
225
06721500 226/* Initialize the global variable %load-path, given the value of the
3feedb00 227 SCM_SITE_DIR and SCM_LIBRARY_DIR preprocessor symbols and the
01cddfc1 228 GUILE_LOAD_PATH environment variable. */
0f2d19dd 229void
06721500
JB
230scm_init_load_path ()
231{
232 SCM path = SCM_EOL;
233
3feedb00 234#ifdef SCM_LIBRARY_DIR
1afff620
KN
235 path = scm_list_3 (scm_makfrom0str (SCM_SITE_DIR),
236 scm_makfrom0str (SCM_LIBRARY_DIR),
237 scm_makfrom0str (SCM_PKGDATA_DIR));
3feedb00 238#endif /* SCM_LIBRARY_DIR */
06721500 239
04e8fb0a 240 path = scm_internal_parse_path (getenv ("GUILE_LOAD_PATH"), path);
01cddfc1 241
06721500
JB
242 *scm_loc_load_path = path;
243}
244
04e8fb0a 245SCM scm_listofnullstr;
06721500 246
04e8fb0a 247/* Search PATH for a directory containing a file named FILENAME.
06721500 248 The file must be readable, and not a directory.
26544b96 249 If we find one, return its full filename; otherwise, return #f.
56384176
JB
250 If FILENAME is absolute, return it unchanged.
251 If given, EXTENSIONS is a list of strings; for each directory
252 in PATH, we search for FILENAME concatenated with each EXTENSION. */
3b3b36dd 253SCM_DEFINE (scm_search_path, "search-path", 2, 1, 0,
1bbd0b84 254 (SCM path, SCM filename, SCM extensions),
d91788cb
MG
255 "Search @var{path} for a directory containing a file named\n"
256 "@var{filename}. The file must be readable, and not a directory.\n"
257 "If we find one, return its full filename; otherwise, return\n"
258 "@code{#f}. If @var{filename} is absolute, return it unchanged.\n"
259 "If given, @var{extensions} is a list of strings; for each\n"
260 "directory in @var{path}, we search for @var{filename}\n"
261 "concatenated with each @var{extension}.")
1bbd0b84 262#define FUNC_NAME s_scm_search_path
06721500 263{
56384176 264 char *filename_chars;
06721500 265 int filename_len;
56384176
JB
266 size_t max_path_len; /* maximum length of any PATH element */
267 size_t max_ext_len; /* maximum length of any EXTENSIONS element */
06721500 268
34d19ef6 269 SCM_VALIDATE_LIST (1, path);
a6d9e5ab 270 SCM_VALIDATE_STRING (2, filename);
04e8fb0a 271 if (SCM_UNBNDP (extensions))
56384176 272 extensions = SCM_EOL;
04e8fb0a 273 else
34d19ef6 274 SCM_VALIDATE_LIST (3, extensions);
56384176 275
34f0f2b8 276 filename_chars = SCM_STRING_CHARS (filename);
a6d9e5ab 277 filename_len = SCM_STRING_LENGTH (filename);
06721500 278
26544b96 279 /* If FILENAME is absolute, return it unchanged. */
2e945bcc
SJ
280#ifdef __MINGW32__
281 if (((filename_len >= 1) &&
282 (filename_chars[0] == '/' || filename_chars[0] == '\\')) ||
283 ((filename_len >= 3) && filename_chars[1] == ':' &&
284 ((filename_chars[0] >= 'a' && filename_chars[0] <= 'z') ||
285 (filename_chars[0] >= 'A' && filename_chars[0] <= 'Z')) &&
286 (filename_chars[2] == '/' || filename_chars[2] == '\\')))
287#else
56384176 288 if (filename_len >= 1 && filename_chars[0] == '/')
2e945bcc 289#endif
26544b96
JB
290 return filename;
291
292 /* Find the length of the longest element of path. */
293 {
294 SCM walk;
295
296 max_path_len = 0;
c96d76b8 297 for (walk = path; !SCM_NULL_OR_NIL_P (walk); walk = SCM_CDR (walk))
26544b96
JB
298 {
299 SCM elt = SCM_CAR (walk);
2ade72d7
DH
300 SCM_ASSERT_TYPE (SCM_STRINGP (elt), path, 1, FUNC_NAME,
301 "list of strings");
a6d9e5ab
DH
302 if (SCM_STRING_LENGTH (elt) > max_path_len)
303 max_path_len = SCM_STRING_LENGTH (elt);
26544b96
JB
304 }
305 }
306
56384176
JB
307 /* If FILENAME has an extension, don't try to add EXTENSIONS to it. */
308 {
309 char *endp;
310
311 for (endp = filename_chars + filename_len - 1;
312 endp >= filename_chars;
313 endp--)
314 {
315 if (*endp == '.')
316 {
317 /* This filename already has an extension, so cancel the
318 list of extensions. */
319 extensions = SCM_EOL;
320 break;
321 }
2e945bcc
SJ
322#ifdef __MINGW32__
323 else if (*endp == '/' || *endp == '\\')
324#else
56384176 325 else if (*endp == '/')
2e945bcc 326#endif
56384176
JB
327 /* This filename has no extension, so keep the current list
328 of extensions. */
329 break;
330 }
331 }
332
26544b96
JB
333 /* Find the length of the longest element of the load extensions
334 list. */
1bbd0b84 335 { /* scope */
26544b96
JB
336 SCM walk;
337
338 max_ext_len = 0;
c96d76b8 339 for (walk = extensions; !SCM_NULL_OR_NIL_P (walk); walk = SCM_CDR (walk))
26544b96
JB
340 {
341 SCM elt = SCM_CAR (walk);
2ade72d7
DH
342 SCM_ASSERT_TYPE (SCM_STRINGP (elt), elt, 3, FUNC_NAME,
343 "list of strings");
a6d9e5ab
DH
344 if (SCM_STRING_LENGTH (elt) > max_ext_len)
345 max_ext_len = SCM_STRING_LENGTH (elt);
26544b96
JB
346 }
347 }
348
06721500
JB
349 SCM_DEFER_INTS;
350
1bbd0b84 351 { /* scope */
56384176 352 SCM result = SCM_BOOL_F;
1be6b49c 353 size_t buf_size = max_path_len + 1 + filename_len + max_ext_len + 1;
4c9419ac 354 char *buf = scm_malloc (buf_size);
06721500 355
56384176 356 /* This simplifies the loop below a bit. */
c96d76b8 357 if (SCM_NULL_OR_NIL_P (extensions))
56384176 358 extensions = scm_listofnullstr;
0a74e31d 359
56384176
JB
360 /* Try every path element. At this point, we know the path is a
361 proper list of strings. */
c96d76b8 362 for (; !SCM_NULL_OR_NIL_P (path); path = SCM_CDR (path))
56384176 363 {
1be6b49c 364 size_t len;
56384176
JB
365 SCM dir = SCM_CAR (path);
366 SCM exts;
367
368 /* Concatenate the path name and the filename. */
a6d9e5ab 369 len = SCM_STRING_LENGTH (dir);
34f0f2b8 370 memcpy (buf, SCM_STRING_CHARS (dir), len);
2e945bcc
SJ
371#ifdef __MINGW32__
372 if (len >= 1 && buf[len - 1] != '/' && buf[len - 1] != '\\')
373#else
56384176 374 if (len >= 1 && buf[len - 1] != '/')
2e945bcc 375#endif
56384176
JB
376 buf[len++] = '/';
377 memcpy (buf + len, filename_chars, filename_len);
378 len += filename_len;
379
380 /* Try every extension. At this point, we know the extension
381 list is a proper, nonempty list of strings. */
c96d76b8 382 for (exts = extensions; !SCM_NULL_OR_NIL_P (exts); exts = SCM_CDR (exts))
56384176
JB
383 {
384 SCM ext = SCM_CAR (exts);
1be6b49c 385 size_t ext_len = SCM_STRING_LENGTH (ext);
56384176
JB
386 struct stat mode;
387
388 /* Concatenate the extension. */
34f0f2b8 389 memcpy (buf + len, SCM_STRING_CHARS (ext), ext_len);
56384176
JB
390 buf[len + ext_len] = '\0';
391
392 /* If the file exists at all, we should return it. If the
393 file is inaccessible, then that's an error. */
394 if (stat (buf, &mode) == 0
395 && ! (mode.st_mode & S_IFDIR))
396 {
36284627 397 result = scm_mem2string (buf, len + ext_len);
56384176
JB
398 goto end;
399 }
400 }
401 }
402
403 end:
4c9419ac 404 free (buf);
56384176
JB
405 SCM_ALLOW_INTS;
406 return result;
407 }
06721500 408}
1bbd0b84 409#undef FUNC_NAME
06721500
JB
410
411
04e8fb0a
MD
412/* Search %load-path for a directory containing a file named FILENAME.
413 The file must be readable, and not a directory.
414 If we find one, return its full filename; otherwise, return #f.
415 If FILENAME is absolute, return it unchanged. */
3b3b36dd 416SCM_DEFINE (scm_sys_search_load_path, "%search-load-path", 1, 0, 0,
67e8151b
MG
417 (SCM filename),
418 "Search @var{%load-path} for the file named @var{filename},\n"
419 "which must be readable by the current user. If @var{filename}\n"
420 "is found in the list of paths to search or is an absolute\n"
421 "pathname, return its full pathname. Otherwise, return\n"
422 "@code{#f}. Filenames may have any of the optional extensions\n"
423 "in the @code{%load-extensions} list; @code{%search-load-path}\n"
424 "will try each extension automatically.")
1bbd0b84 425#define FUNC_NAME s_scm_sys_search_load_path
04e8fb0a
MD
426{
427 SCM path = *scm_loc_load_path;
428 SCM exts = *scm_loc_load_extensions;
a6d9e5ab 429 SCM_VALIDATE_STRING (1, filename);
1bbd0b84 430
2ade72d7
DH
431 if (scm_ilength (path) < 0)
432 SCM_MISC_ERROR ("%load-path is not a proper list", SCM_EOL);
433 if (scm_ilength (exts) < 0)
434 SCM_MISC_ERROR ("%load-extension list is not a proper list", SCM_EOL);
1bbd0b84 435 return scm_search_path (path, filename, exts);
04e8fb0a 436}
1bbd0b84 437#undef FUNC_NAME
04e8fb0a
MD
438
439
3b3b36dd 440SCM_DEFINE (scm_primitive_load_path, "primitive-load-path", 1, 0, 0,
67e8151b
MG
441 (SCM filename),
442 "Search @var{%load-path} for the file named @var{filename} and\n"
443 "load it into the top-level environment. If @var{filename} is a\n"
444 "relative pathname and is not found in the list of search paths,\n"
445 "an error is signalled.")
1bbd0b84 446#define FUNC_NAME s_scm_primitive_load_path
06721500 447{
26544b96
JB
448 SCM full_filename;
449
a6d9e5ab 450 SCM_VALIDATE_STRING (1, filename);
26544b96
JB
451
452 full_filename = scm_sys_search_load_path (filename);
453
7888309b 454 if (scm_is_false (full_filename))
dbece3a2 455 {
a6d9e5ab 456 int absolute = (SCM_STRING_LENGTH (filename) >= 1
2e945bcc
SJ
457#ifdef __MINGW32__
458 && (SCM_STRING_CHARS (filename)[0] == '/' ||
459 SCM_STRING_CHARS (filename)[0] == '\\'));
460#else
34f0f2b8 461 && SCM_STRING_CHARS (filename)[0] == '/');
2e945bcc 462#endif
5d2d2ffc 463 SCM_MISC_ERROR ((absolute
70d63753
GB
464 ? "Unable to load file ~S"
465 : "Unable to find file ~S in load path"),
1afff620 466 scm_list_1 (filename));
dbece3a2 467 }
26544b96 468
deca31e1 469 return scm_primitive_load (full_filename);
06721500 470}
1bbd0b84 471#undef FUNC_NAME
06721500 472
c519b272
MV
473SCM
474scm_c_primitive_load_path (const char *filename)
475{
476 return scm_primitive_load_path (scm_makfrom0str (filename));
477}
478
06721500 479\f
e151bee6 480/* Information about the build environment. */
06721500 481
e151bee6
JB
482/* Initialize the scheme variable %guile-build-info, based on data
483 provided by the Makefile, via libpath.h. */
484static void
485init_build_info ()
486{
487 static struct { char *name; char *value; } info[] = SCM_BUILD_INFO;
86d31dfe 488 SCM *loc = SCM_VARIABLE_LOC (scm_c_define ("%guile-build-info", SCM_EOL));
c014a02e 489 unsigned long i;
e151bee6
JB
490
491 for (i = 0; i < (sizeof (info) / sizeof (info[0])); i++)
38ae064c 492 *loc = scm_acons (scm_str2symbol (info[i].name),
e151bee6
JB
493 scm_makfrom0str (info[i].value),
494 *loc);
495}
496
497
498\f
0f2d19dd
JB
499void
500scm_init_load ()
0f2d19dd 501{
1afff620 502 scm_listofnullstr = scm_permanent_object (scm_list_1 (scm_nullstr));
86d31dfe 503 scm_loc_load_path = SCM_VARIABLE_LOC (scm_c_define ("%load-path", SCM_EOL));
26544b96 504 scm_loc_load_extensions
86d31dfe 505 = SCM_VARIABLE_LOC (scm_c_define ("%load-extensions",
1afff620
KN
506 scm_list_2 (scm_makfrom0str (".scm"),
507 scm_nullstr)));
86d31dfe 508 scm_loc_load_hook = SCM_VARIABLE_LOC (scm_c_define ("%load-hook", SCM_BOOL_F));
06721500 509
e151bee6
JB
510 init_build_info ();
511
a0599745 512#include "libguile/load.x"
0f2d19dd 513}
89e00824
ML
514
515/*
516 Local Variables:
517 c-file-style: "gnu"
518 End:
519*/