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