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