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