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