Changed license terms to the plain LGPL thru-out.
[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);
2ade72d7
DH
96 if (!SCM_FALSEP (hook) && !SCM_EQ_P (scm_procedure_p (hook), SCM_BOOL_T))
97 SCM_MISC_ERROR ("value of %load-hook is neither a procedure nor #f",
98 SCM_EOL);
26544b96 99
54778cd3 100 if (! SCM_FALSEP (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--;
191 while (scan >= path && *scan != ':');
36284627 192 tail = scm_cons (scm_mem2string (scan + 1, elt_end - (scan + 1)),
01cddfc1
JB
193 tail);
194 elt_end = scan;
195 } while (scan >= path);
196 }
197
198 return tail;
199}
200
201
a1ec6916 202SCM_DEFINE (scm_parse_path, "parse-path", 1, 1, 0,
1bbd0b84 203 (SCM path, SCM tail),
d91788cb
MG
204 "Parse @var{path}, which is expected to be a colon-separated\n"
205 "string, into a list and return the resulting list with\n"
206 "@var{tail} appended. If @var{path} is @code{#f}, @var{tail}\n"
207 "is returned.")
1bbd0b84 208#define FUNC_NAME s_scm_parse_path
04e8fb0a 209{
a6d9e5ab 210 SCM_ASSERT (SCM_FALSEP (path) || (SCM_STRINGP (path)),
04e8fb0a 211 path,
1bbd0b84 212 SCM_ARG1, FUNC_NAME);
04e8fb0a
MD
213 if (SCM_UNBNDP (tail))
214 tail = SCM_EOL;
215 return (SCM_FALSEP (path)
216 ? tail
34f0f2b8 217 : scm_internal_parse_path (SCM_STRING_CHARS (path), tail));
04e8fb0a 218}
1bbd0b84 219#undef FUNC_NAME
04e8fb0a
MD
220
221
06721500 222/* Initialize the global variable %load-path, given the value of the
3feedb00 223 SCM_SITE_DIR and SCM_LIBRARY_DIR preprocessor symbols and the
01cddfc1 224 GUILE_LOAD_PATH environment variable. */
0f2d19dd 225void
06721500
JB
226scm_init_load_path ()
227{
228 SCM path = SCM_EOL;
229
3feedb00 230#ifdef SCM_LIBRARY_DIR
1afff620
KN
231 path = scm_list_3 (scm_makfrom0str (SCM_SITE_DIR),
232 scm_makfrom0str (SCM_LIBRARY_DIR),
233 scm_makfrom0str (SCM_PKGDATA_DIR));
3feedb00 234#endif /* SCM_LIBRARY_DIR */
06721500 235
04e8fb0a 236 path = scm_internal_parse_path (getenv ("GUILE_LOAD_PATH"), path);
01cddfc1 237
06721500
JB
238 *scm_loc_load_path = path;
239}
240
04e8fb0a 241SCM scm_listofnullstr;
06721500 242
04e8fb0a 243/* Search PATH for a directory containing a file named FILENAME.
06721500 244 The file must be readable, and not a directory.
26544b96 245 If we find one, return its full filename; otherwise, return #f.
56384176
JB
246 If FILENAME is absolute, return it unchanged.
247 If given, EXTENSIONS is a list of strings; for each directory
248 in PATH, we search for FILENAME concatenated with each EXTENSION. */
3b3b36dd 249SCM_DEFINE (scm_search_path, "search-path", 2, 1, 0,
1bbd0b84 250 (SCM path, SCM filename, SCM extensions),
d91788cb
MG
251 "Search @var{path} for a directory containing a file named\n"
252 "@var{filename}. The file must be readable, and not a directory.\n"
253 "If we find one, return its full filename; otherwise, return\n"
254 "@code{#f}. If @var{filename} is absolute, return it unchanged.\n"
255 "If given, @var{extensions} is a list of strings; for each\n"
256 "directory in @var{path}, we search for @var{filename}\n"
257 "concatenated with each @var{extension}.")
1bbd0b84 258#define FUNC_NAME s_scm_search_path
06721500 259{
56384176 260 char *filename_chars;
06721500 261 int filename_len;
56384176
JB
262 size_t max_path_len; /* maximum length of any PATH element */
263 size_t max_ext_len; /* maximum length of any EXTENSIONS element */
06721500 264
34d19ef6 265 SCM_VALIDATE_LIST (1, path);
a6d9e5ab 266 SCM_VALIDATE_STRING (2, filename);
04e8fb0a 267 if (SCM_UNBNDP (extensions))
56384176 268 extensions = SCM_EOL;
04e8fb0a 269 else
34d19ef6 270 SCM_VALIDATE_LIST (3, extensions);
56384176 271
34f0f2b8 272 filename_chars = SCM_STRING_CHARS (filename);
a6d9e5ab 273 filename_len = SCM_STRING_LENGTH (filename);
06721500 274
26544b96 275 /* If FILENAME is absolute, return it unchanged. */
56384176 276 if (filename_len >= 1 && filename_chars[0] == '/')
26544b96
JB
277 return filename;
278
279 /* Find the length of the longest element of path. */
280 {
281 SCM walk;
282
283 max_path_len = 0;
c96d76b8 284 for (walk = path; !SCM_NULL_OR_NIL_P (walk); walk = SCM_CDR (walk))
26544b96
JB
285 {
286 SCM elt = SCM_CAR (walk);
2ade72d7
DH
287 SCM_ASSERT_TYPE (SCM_STRINGP (elt), path, 1, FUNC_NAME,
288 "list of strings");
a6d9e5ab
DH
289 if (SCM_STRING_LENGTH (elt) > max_path_len)
290 max_path_len = SCM_STRING_LENGTH (elt);
26544b96
JB
291 }
292 }
293
56384176
JB
294 /* If FILENAME has an extension, don't try to add EXTENSIONS to it. */
295 {
296 char *endp;
297
298 for (endp = filename_chars + filename_len - 1;
299 endp >= filename_chars;
300 endp--)
301 {
302 if (*endp == '.')
303 {
304 /* This filename already has an extension, so cancel the
305 list of extensions. */
306 extensions = SCM_EOL;
307 break;
308 }
309 else if (*endp == '/')
310 /* This filename has no extension, so keep the current list
311 of extensions. */
312 break;
313 }
314 }
315
26544b96
JB
316 /* Find the length of the longest element of the load extensions
317 list. */
1bbd0b84 318 { /* scope */
26544b96
JB
319 SCM walk;
320
321 max_ext_len = 0;
c96d76b8 322 for (walk = extensions; !SCM_NULL_OR_NIL_P (walk); walk = SCM_CDR (walk))
26544b96
JB
323 {
324 SCM elt = SCM_CAR (walk);
2ade72d7
DH
325 SCM_ASSERT_TYPE (SCM_STRINGP (elt), elt, 3, FUNC_NAME,
326 "list of strings");
a6d9e5ab
DH
327 if (SCM_STRING_LENGTH (elt) > max_ext_len)
328 max_ext_len = SCM_STRING_LENGTH (elt);
26544b96
JB
329 }
330 }
331
06721500
JB
332 SCM_DEFER_INTS;
333
1bbd0b84 334 { /* scope */
56384176 335 SCM result = SCM_BOOL_F;
1be6b49c 336 size_t buf_size = max_path_len + 1 + filename_len + max_ext_len + 1;
4c9419ac 337 char *buf = scm_malloc (buf_size);
06721500 338
56384176 339 /* This simplifies the loop below a bit. */
c96d76b8 340 if (SCM_NULL_OR_NIL_P (extensions))
56384176 341 extensions = scm_listofnullstr;
0a74e31d 342
56384176
JB
343 /* Try every path element. At this point, we know the path is a
344 proper list of strings. */
c96d76b8 345 for (; !SCM_NULL_OR_NIL_P (path); path = SCM_CDR (path))
56384176 346 {
1be6b49c 347 size_t len;
56384176
JB
348 SCM dir = SCM_CAR (path);
349 SCM exts;
350
351 /* Concatenate the path name and the filename. */
a6d9e5ab 352 len = SCM_STRING_LENGTH (dir);
34f0f2b8 353 memcpy (buf, SCM_STRING_CHARS (dir), len);
56384176
JB
354 if (len >= 1 && buf[len - 1] != '/')
355 buf[len++] = '/';
356 memcpy (buf + len, filename_chars, filename_len);
357 len += filename_len;
358
359 /* Try every extension. At this point, we know the extension
360 list is a proper, nonempty list of strings. */
c96d76b8 361 for (exts = extensions; !SCM_NULL_OR_NIL_P (exts); exts = SCM_CDR (exts))
56384176
JB
362 {
363 SCM ext = SCM_CAR (exts);
1be6b49c 364 size_t ext_len = SCM_STRING_LENGTH (ext);
56384176
JB
365 struct stat mode;
366
367 /* Concatenate the extension. */
34f0f2b8 368 memcpy (buf + len, SCM_STRING_CHARS (ext), ext_len);
56384176
JB
369 buf[len + ext_len] = '\0';
370
371 /* If the file exists at all, we should return it. If the
372 file is inaccessible, then that's an error. */
373 if (stat (buf, &mode) == 0
374 && ! (mode.st_mode & S_IFDIR))
375 {
36284627 376 result = scm_mem2string (buf, len + ext_len);
56384176
JB
377 goto end;
378 }
379 }
380 }
381
382 end:
4c9419ac 383 free (buf);
56384176
JB
384 SCM_ALLOW_INTS;
385 return result;
386 }
06721500 387}
1bbd0b84 388#undef FUNC_NAME
06721500
JB
389
390
04e8fb0a
MD
391/* Search %load-path for a directory containing a file named FILENAME.
392 The file must be readable, and not a directory.
393 If we find one, return its full filename; otherwise, return #f.
394 If FILENAME is absolute, return it unchanged. */
3b3b36dd 395SCM_DEFINE (scm_sys_search_load_path, "%search-load-path", 1, 0, 0,
67e8151b
MG
396 (SCM filename),
397 "Search @var{%load-path} for the file named @var{filename},\n"
398 "which must be readable by the current user. If @var{filename}\n"
399 "is found in the list of paths to search or is an absolute\n"
400 "pathname, return its full pathname. Otherwise, return\n"
401 "@code{#f}. Filenames may have any of the optional extensions\n"
402 "in the @code{%load-extensions} list; @code{%search-load-path}\n"
403 "will try each extension automatically.")
1bbd0b84 404#define FUNC_NAME s_scm_sys_search_load_path
04e8fb0a
MD
405{
406 SCM path = *scm_loc_load_path;
407 SCM exts = *scm_loc_load_extensions;
a6d9e5ab 408 SCM_VALIDATE_STRING (1, filename);
1bbd0b84 409
2ade72d7
DH
410 if (scm_ilength (path) < 0)
411 SCM_MISC_ERROR ("%load-path is not a proper list", SCM_EOL);
412 if (scm_ilength (exts) < 0)
413 SCM_MISC_ERROR ("%load-extension list is not a proper list", SCM_EOL);
1bbd0b84 414 return scm_search_path (path, filename, exts);
04e8fb0a 415}
1bbd0b84 416#undef FUNC_NAME
04e8fb0a
MD
417
418
3b3b36dd 419SCM_DEFINE (scm_primitive_load_path, "primitive-load-path", 1, 0, 0,
67e8151b
MG
420 (SCM filename),
421 "Search @var{%load-path} for the file named @var{filename} and\n"
422 "load it into the top-level environment. If @var{filename} is a\n"
423 "relative pathname and is not found in the list of search paths,\n"
424 "an error is signalled.")
1bbd0b84 425#define FUNC_NAME s_scm_primitive_load_path
06721500 426{
26544b96
JB
427 SCM full_filename;
428
a6d9e5ab 429 SCM_VALIDATE_STRING (1, filename);
26544b96
JB
430
431 full_filename = scm_sys_search_load_path (filename);
432
dbece3a2
GH
433 if (SCM_FALSEP (full_filename))
434 {
a6d9e5ab 435 int absolute = (SCM_STRING_LENGTH (filename) >= 1
34f0f2b8 436 && SCM_STRING_CHARS (filename)[0] == '/');
5d2d2ffc 437 SCM_MISC_ERROR ((absolute
70d63753
GB
438 ? "Unable to load file ~S"
439 : "Unable to find file ~S in load path"),
1afff620 440 scm_list_1 (filename));
dbece3a2 441 }
26544b96 442
deca31e1 443 return scm_primitive_load (full_filename);
06721500 444}
1bbd0b84 445#undef FUNC_NAME
06721500 446
c519b272
MV
447SCM
448scm_c_primitive_load_path (const char *filename)
449{
450 return scm_primitive_load_path (scm_makfrom0str (filename));
451}
452
06721500 453\f
e151bee6 454/* Information about the build environment. */
06721500 455
e151bee6
JB
456/* Initialize the scheme variable %guile-build-info, based on data
457 provided by the Makefile, via libpath.h. */
458static void
459init_build_info ()
460{
461 static struct { char *name; char *value; } info[] = SCM_BUILD_INFO;
86d31dfe 462 SCM *loc = SCM_VARIABLE_LOC (scm_c_define ("%guile-build-info", SCM_EOL));
c014a02e 463 unsigned long i;
e151bee6
JB
464
465 for (i = 0; i < (sizeof (info) / sizeof (info[0])); i++)
38ae064c 466 *loc = scm_acons (scm_str2symbol (info[i].name),
e151bee6
JB
467 scm_makfrom0str (info[i].value),
468 *loc);
469}
470
471
472\f
0f2d19dd
JB
473void
474scm_init_load ()
0f2d19dd 475{
1afff620 476 scm_listofnullstr = scm_permanent_object (scm_list_1 (scm_nullstr));
86d31dfe 477 scm_loc_load_path = SCM_VARIABLE_LOC (scm_c_define ("%load-path", SCM_EOL));
26544b96 478 scm_loc_load_extensions
86d31dfe 479 = SCM_VARIABLE_LOC (scm_c_define ("%load-extensions",
1afff620
KN
480 scm_list_2 (scm_makfrom0str (".scm"),
481 scm_nullstr)));
86d31dfe 482 scm_loc_load_hook = SCM_VARIABLE_LOC (scm_c_define ("%load-hook", SCM_BOOL_F));
06721500 483
e151bee6
JB
484 init_build_info ();
485
a0599745 486#include "libguile/load.x"
0f2d19dd 487}
89e00824
ML
488
489/*
490 Local Variables:
491 c-file-style: "gnu"
492 End:
493*/