*** 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),
67e8151b
MG
106 "Load the file named @var{filename} and evaluate its contents in\n"
107 "the top-level environment. The load paths are not searched;\n"
108 "@var{filename} must either be a full pathname or be a pathname\n"
109 "relative to the current directory. If the variable\n"
110 "@code{%load-hook} is defined, it should be bound to a procedure\n"
111 "that will be called before any code is loaded. See the\n"
112 "documentation for @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);
2ade72d7
DH
117 if (!SCM_FALSEP (hook) && !SCM_EQ_P (scm_procedure_p (hook), SCM_BOOL_T))
118 SCM_MISC_ERROR ("value of %load-hook is neither a procedure nor #f",
119 SCM_EOL);
26544b96 120
54778cd3 121 if (! SCM_FALSEP (hook))
fdc28395 122 scm_call_1 (hook, filename);
26544b96 123
1bbd0b84 124 { /* scope */
d7834b91 125 SCM port, save_port;
36284627 126 port = scm_open_file (filename, scm_mem2string ("r", sizeof (char)));
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 138
c519b272
MV
139SCM
140scm_c_primitive_load (const char *filename)
141{
142 return scm_primitive_load (scm_makfrom0str (filename));
143}
144
0f2d19dd 145\f
3feedb00
MD
146/* Builtin path to scheme library files. */
147#ifdef SCM_PKGDATA_DIR
a1ec6916 148SCM_DEFINE (scm_sys_package_data_dir, "%package-data-dir", 0, 0, 0,
1bbd0b84 149 (),
b380b885
MD
150 "Return the name of the directory where Scheme packages, modules and\n"
151 "libraries are kept. On most Unix systems, this will be\n"
152 "@samp{/usr/local/share/guile}.")
1bbd0b84 153#define FUNC_NAME s_scm_sys_package_data_dir
3feedb00
MD
154{
155 return scm_makfrom0str (SCM_PKGDATA_DIR);
156}
1bbd0b84 157#undef FUNC_NAME
3feedb00
MD
158#endif /* SCM_PKGDATA_DIR */
159
e8e9b690 160#ifdef SCM_LIBRARY_DIR
a1ec6916 161SCM_DEFINE (scm_sys_library_dir, "%library-dir", 0,0,0,
e8e9b690 162 (),
b380b885
MD
163 "Return the directory where the Guile Scheme library files are installed.\n"
164 "E.g., may return \"/usr/share/guile/1.3.5\".")
e8e9b690
GB
165#define FUNC_NAME s_scm_sys_library_dir
166{
167 return scm_makfrom0str(SCM_LIBRARY_DIR);
168}
169#undef FUNC_NAME
170#endif /* SCM_LIBRARY_DIR */
171
172#ifdef SCM_SITE_DIR
a1ec6916 173SCM_DEFINE (scm_sys_site_dir, "%site-dir", 0,0,0,
e8e9b690 174 (),
b380b885
MD
175 "Return the directory where the Guile site files are installed.\n"
176 "E.g., may return \"/usr/share/guile/site\".")
e8e9b690
GB
177#define FUNC_NAME s_scm_sys_site_dir
178{
179 return scm_makfrom0str(SCM_SITE_DIR);
180}
181#undef FUNC_NAME
182#endif /* SCM_SITE_DIR */
183
184
185
3feedb00 186\f
06721500 187/* Initializing the load path, and searching it. */
0f2d19dd 188
26544b96 189/* List of names of directories we search for files to load. */
06721500
JB
190static SCM *scm_loc_load_path;
191
26544b96
JB
192/* List of extensions we try adding to the filenames. */
193static SCM *scm_loc_load_extensions;
194
01cddfc1
JB
195
196/* Parse the null-terminated string PATH as if it were a standard path
197 environment variable (i.e. a colon-separated list of strings), and
198 prepend the elements to TAIL. */
199SCM
04e8fb0a 200scm_internal_parse_path (char *path, SCM tail)
01cddfc1
JB
201{
202 if (path && path[0] != '\0')
203 {
204 char *scan, *elt_end;
205
206 /* Scan backwards from the end of the string, to help
207 construct the list in the right order. */
208 scan = elt_end = path + strlen (path);
209 do {
210 /* Scan back to the beginning of the current element. */
211 do scan--;
212 while (scan >= path && *scan != ':');
36284627 213 tail = scm_cons (scm_mem2string (scan + 1, elt_end - (scan + 1)),
01cddfc1
JB
214 tail);
215 elt_end = scan;
216 } while (scan >= path);
217 }
218
219 return tail;
220}
221
222
a1ec6916 223SCM_DEFINE (scm_parse_path, "parse-path", 1, 1, 0,
1bbd0b84 224 (SCM path, SCM tail),
d91788cb
MG
225 "Parse @var{path}, which is expected to be a colon-separated\n"
226 "string, into a list and return the resulting list with\n"
227 "@var{tail} appended. If @var{path} is @code{#f}, @var{tail}\n"
228 "is returned.")
1bbd0b84 229#define FUNC_NAME s_scm_parse_path
04e8fb0a 230{
a6d9e5ab 231 SCM_ASSERT (SCM_FALSEP (path) || (SCM_STRINGP (path)),
04e8fb0a 232 path,
1bbd0b84 233 SCM_ARG1, FUNC_NAME);
04e8fb0a
MD
234 if (SCM_UNBNDP (tail))
235 tail = SCM_EOL;
236 return (SCM_FALSEP (path)
237 ? tail
34f0f2b8 238 : scm_internal_parse_path (SCM_STRING_CHARS (path), tail));
04e8fb0a 239}
1bbd0b84 240#undef FUNC_NAME
04e8fb0a
MD
241
242
06721500 243/* Initialize the global variable %load-path, given the value of the
3feedb00 244 SCM_SITE_DIR and SCM_LIBRARY_DIR preprocessor symbols and the
01cddfc1 245 GUILE_LOAD_PATH environment variable. */
0f2d19dd 246void
06721500
JB
247scm_init_load_path ()
248{
249 SCM path = SCM_EOL;
250
3feedb00 251#ifdef SCM_LIBRARY_DIR
1afff620
KN
252 path = scm_list_3 (scm_makfrom0str (SCM_SITE_DIR),
253 scm_makfrom0str (SCM_LIBRARY_DIR),
254 scm_makfrom0str (SCM_PKGDATA_DIR));
3feedb00 255#endif /* SCM_LIBRARY_DIR */
06721500 256
04e8fb0a 257 path = scm_internal_parse_path (getenv ("GUILE_LOAD_PATH"), path);
01cddfc1 258
06721500
JB
259 *scm_loc_load_path = path;
260}
261
04e8fb0a 262SCM scm_listofnullstr;
06721500 263
04e8fb0a 264/* Search PATH for a directory containing a file named FILENAME.
06721500 265 The file must be readable, and not a directory.
26544b96 266 If we find one, return its full filename; otherwise, return #f.
56384176
JB
267 If FILENAME is absolute, return it unchanged.
268 If given, EXTENSIONS is a list of strings; for each directory
269 in PATH, we search for FILENAME concatenated with each EXTENSION. */
3b3b36dd 270SCM_DEFINE (scm_search_path, "search-path", 2, 1, 0,
1bbd0b84 271 (SCM path, SCM filename, SCM extensions),
d91788cb
MG
272 "Search @var{path} for a directory containing a file named\n"
273 "@var{filename}. The file must be readable, and not a directory.\n"
274 "If we find one, return its full filename; otherwise, return\n"
275 "@code{#f}. If @var{filename} is absolute, return it unchanged.\n"
276 "If given, @var{extensions} is a list of strings; for each\n"
277 "directory in @var{path}, we search for @var{filename}\n"
278 "concatenated with each @var{extension}.")
1bbd0b84 279#define FUNC_NAME s_scm_search_path
06721500 280{
56384176 281 char *filename_chars;
06721500 282 int filename_len;
56384176
JB
283 size_t max_path_len; /* maximum length of any PATH element */
284 size_t max_ext_len; /* maximum length of any EXTENSIONS element */
06721500 285
3b3b36dd 286 SCM_VALIDATE_LIST (1,path);
a6d9e5ab 287 SCM_VALIDATE_STRING (2, filename);
04e8fb0a 288 if (SCM_UNBNDP (extensions))
56384176 289 extensions = SCM_EOL;
04e8fb0a 290 else
3b3b36dd 291 SCM_VALIDATE_LIST (3,extensions);
56384176 292
34f0f2b8 293 filename_chars = SCM_STRING_CHARS (filename);
a6d9e5ab 294 filename_len = SCM_STRING_LENGTH (filename);
06721500 295
26544b96 296 /* If FILENAME is absolute, return it unchanged. */
56384176 297 if (filename_len >= 1 && filename_chars[0] == '/')
26544b96
JB
298 return filename;
299
300 /* Find the length of the longest element of path. */
301 {
302 SCM walk;
303
304 max_path_len = 0;
a6d9e5ab 305 for (walk = path; !SCM_NULLP (walk); walk = SCM_CDR (walk))
26544b96
JB
306 {
307 SCM elt = SCM_CAR (walk);
2ade72d7
DH
308 SCM_ASSERT_TYPE (SCM_STRINGP (elt), path, 1, FUNC_NAME,
309 "list of strings");
a6d9e5ab
DH
310 if (SCM_STRING_LENGTH (elt) > max_path_len)
311 max_path_len = SCM_STRING_LENGTH (elt);
26544b96
JB
312 }
313 }
314
56384176
JB
315 /* If FILENAME has an extension, don't try to add EXTENSIONS to it. */
316 {
317 char *endp;
318
319 for (endp = filename_chars + filename_len - 1;
320 endp >= filename_chars;
321 endp--)
322 {
323 if (*endp == '.')
324 {
325 /* This filename already has an extension, so cancel the
326 list of extensions. */
327 extensions = SCM_EOL;
328 break;
329 }
330 else if (*endp == '/')
331 /* This filename has no extension, so keep the current list
332 of extensions. */
333 break;
334 }
335 }
336
26544b96
JB
337 /* Find the length of the longest element of the load extensions
338 list. */
1bbd0b84 339 { /* scope */
26544b96
JB
340 SCM walk;
341
342 max_ext_len = 0;
a6d9e5ab 343 for (walk = extensions; !SCM_NULLP (walk); walk = SCM_CDR (walk))
26544b96
JB
344 {
345 SCM elt = SCM_CAR (walk);
2ade72d7
DH
346 SCM_ASSERT_TYPE (SCM_STRINGP (elt), elt, 3, FUNC_NAME,
347 "list of strings");
a6d9e5ab
DH
348 if (SCM_STRING_LENGTH (elt) > max_ext_len)
349 max_ext_len = SCM_STRING_LENGTH (elt);
26544b96
JB
350 }
351 }
352
06721500
JB
353 SCM_DEFER_INTS;
354
1bbd0b84 355 { /* scope */
56384176 356 SCM result = SCM_BOOL_F;
1be6b49c 357 size_t buf_size = max_path_len + 1 + filename_len + max_ext_len + 1;
1bbd0b84 358 char *buf = SCM_MUST_MALLOC (buf_size);
06721500 359
56384176
JB
360 /* This simplifies the loop below a bit. */
361 if (SCM_NULLP (extensions))
362 extensions = scm_listofnullstr;
0a74e31d 363
56384176
JB
364 /* Try every path element. At this point, we know the path is a
365 proper list of strings. */
a6d9e5ab 366 for (; !SCM_NULLP (path); path = SCM_CDR (path))
56384176 367 {
1be6b49c 368 size_t len;
56384176
JB
369 SCM dir = SCM_CAR (path);
370 SCM exts;
371
372 /* Concatenate the path name and the filename. */
a6d9e5ab 373 len = SCM_STRING_LENGTH (dir);
34f0f2b8 374 memcpy (buf, SCM_STRING_CHARS (dir), len);
56384176
JB
375 if (len >= 1 && buf[len - 1] != '/')
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. */
a6d9e5ab 382 for (exts = extensions; !SCM_NULLP (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:
404 scm_must_free (buf);
1be6b49c 405 scm_done_free (buf_size);
56384176
JB
406 SCM_ALLOW_INTS;
407 return result;
408 }
06721500 409}
1bbd0b84 410#undef FUNC_NAME
06721500
JB
411
412
04e8fb0a
MD
413/* Search %load-path for a directory containing a file named FILENAME.
414 The file must be readable, and not a directory.
415 If we find one, return its full filename; otherwise, return #f.
416 If FILENAME is absolute, return it unchanged. */
3b3b36dd 417SCM_DEFINE (scm_sys_search_load_path, "%search-load-path", 1, 0, 0,
67e8151b
MG
418 (SCM filename),
419 "Search @var{%load-path} for the file named @var{filename},\n"
420 "which must be readable by the current user. If @var{filename}\n"
421 "is found in the list of paths to search or is an absolute\n"
422 "pathname, return its full pathname. Otherwise, return\n"
423 "@code{#f}. Filenames may have any of the optional extensions\n"
424 "in the @code{%load-extensions} list; @code{%search-load-path}\n"
425 "will try each extension automatically.")
1bbd0b84 426#define FUNC_NAME s_scm_sys_search_load_path
04e8fb0a
MD
427{
428 SCM path = *scm_loc_load_path;
429 SCM exts = *scm_loc_load_extensions;
a6d9e5ab 430 SCM_VALIDATE_STRING (1, filename);
1bbd0b84 431
2ade72d7
DH
432 if (scm_ilength (path) < 0)
433 SCM_MISC_ERROR ("%load-path is not a proper list", SCM_EOL);
434 if (scm_ilength (exts) < 0)
435 SCM_MISC_ERROR ("%load-extension list is not a proper list", SCM_EOL);
1bbd0b84 436 return scm_search_path (path, filename, exts);
04e8fb0a 437}
1bbd0b84 438#undef FUNC_NAME
04e8fb0a
MD
439
440
3b3b36dd 441SCM_DEFINE (scm_primitive_load_path, "primitive-load-path", 1, 0, 0,
67e8151b
MG
442 (SCM filename),
443 "Search @var{%load-path} for the file named @var{filename} and\n"
444 "load it into the top-level environment. If @var{filename} is a\n"
445 "relative pathname and is not found in the list of search paths,\n"
446 "an error is signalled.")
1bbd0b84 447#define FUNC_NAME s_scm_primitive_load_path
06721500 448{
26544b96
JB
449 SCM full_filename;
450
a6d9e5ab 451 SCM_VALIDATE_STRING (1, filename);
26544b96
JB
452
453 full_filename = scm_sys_search_load_path (filename);
454
dbece3a2
GH
455 if (SCM_FALSEP (full_filename))
456 {
a6d9e5ab 457 int absolute = (SCM_STRING_LENGTH (filename) >= 1
34f0f2b8 458 && SCM_STRING_CHARS (filename)[0] == '/');
5d2d2ffc 459 SCM_MISC_ERROR ((absolute
70d63753
GB
460 ? "Unable to load file ~S"
461 : "Unable to find file ~S in load path"),
1afff620 462 scm_list_1 (filename));
dbece3a2 463 }
26544b96 464
deca31e1 465 return scm_primitive_load (full_filename);
06721500 466}
1bbd0b84 467#undef FUNC_NAME
06721500 468
c519b272
MV
469SCM
470scm_c_primitive_load_path (const char *filename)
471{
472 return scm_primitive_load_path (scm_makfrom0str (filename));
473}
474
f33b174d
MD
475#if SCM_DEBUG_DEPRECATED == 0
476
477/* Eval now copies source properties, so this function is no longer required.
b35d06a4
MD
478 */
479
480SCM_SYMBOL (scm_end_of_file_key, "end-of-file");
481
a1ec6916 482SCM_DEFINE (scm_read_and_eval_x, "read-and-eval!", 0, 1, 0,
1bbd0b84 483 (SCM port),
b380b885
MD
484 "Read a form from @var{port} (standard input by default), and evaluate it\n"
485 "(memoizing it in the process) in the top-level environment. If no data\n"
486 "is left to be read from @var{port}, an @code{end-of-file} error is\n"
487 "signalled.")
1bbd0b84 488#define FUNC_NAME s_scm_read_and_eval_x
b35d06a4 489{
deca31e1 490 SCM form = scm_read (port);
0c32d76c 491 if (SCM_EOF_OBJECT_P (form))
b35d06a4 492 scm_ithrow (scm_end_of_file_key, SCM_EOL, 1);
aa767bc5 493 return scm_eval_x (form, scm_current_module ());
b35d06a4 494}
1bbd0b84 495#undef FUNC_NAME
b35d06a4 496
f33b174d
MD
497#endif
498
06721500 499\f
e151bee6 500/* Information about the build environment. */
06721500 501
e151bee6
JB
502/* Initialize the scheme variable %guile-build-info, based on data
503 provided by the Makefile, via libpath.h. */
504static void
505init_build_info ()
506{
507 static struct { char *name; char *value; } info[] = SCM_BUILD_INFO;
86d31dfe 508 SCM *loc = SCM_VARIABLE_LOC (scm_c_define ("%guile-build-info", SCM_EOL));
c014a02e 509 unsigned long i;
e151bee6
JB
510
511 for (i = 0; i < (sizeof (info) / sizeof (info[0])); i++)
38ae064c 512 *loc = scm_acons (scm_str2symbol (info[i].name),
e151bee6
JB
513 scm_makfrom0str (info[i].value),
514 *loc);
515}
516
517
518\f
0f2d19dd
JB
519void
520scm_init_load ()
0f2d19dd 521{
1afff620 522 scm_listofnullstr = scm_permanent_object (scm_list_1 (scm_nullstr));
86d31dfe 523 scm_loc_load_path = SCM_VARIABLE_LOC (scm_c_define ("%load-path", SCM_EOL));
26544b96 524 scm_loc_load_extensions
86d31dfe 525 = SCM_VARIABLE_LOC (scm_c_define ("%load-extensions",
1afff620
KN
526 scm_list_2 (scm_makfrom0str (".scm"),
527 scm_nullstr)));
86d31dfe 528 scm_loc_load_hook = SCM_VARIABLE_LOC (scm_c_define ("%load-hook", SCM_BOOL_F));
06721500 529
e151bee6
JB
530 init_build_info ();
531
8dc9439f 532#ifndef SCM_MAGIC_SNARFER
a0599745 533#include "libguile/load.x"
8dc9439f 534#endif
0f2d19dd 535}
89e00824
ML
536
537/*
538 Local Variables:
539 c-file-style: "gnu"
540 End:
541*/