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