revert the ill-considered part of the 2001-05-24 changes
[bpt/guile.git] / libguile / load.c
1 /* Copyright (C) 1995,1996,1998,1999,2000,2001 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 <string.h>
48
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"
59 #include "libguile/modules.h"
60
61 #include "libguile/validate.h"
62 #include "libguile/load.h"
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
74
75 \f
76 /* Loading a file, given an absolute filename. */
77
78 /* Hook to run when we load a file, perhaps to announce the fact somewhere.
79 Applied to the full name of the file. */
80 static SCM *scm_loc_load_hook;
81
82 static void
83 swap_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
90 static SCM
91 load (void *data)
92 {
93 SCM port = SCM_PACK (data);
94 while (1)
95 {
96 SCM form = scm_read (port);
97 if (SCM_EOF_OBJECT_P (form))
98 break;
99 scm_primitive_eval_x (form);
100 }
101 return SCM_UNSPECIFIED;
102 }
103
104 SCM_DEFINE (scm_primitive_load, "primitive-load", 1, 0, 0,
105 (SCM filename),
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.")
113 #define FUNC_NAME s_scm_primitive_load
114 {
115 SCM hook = *scm_loc_load_hook;
116 SCM_VALIDATE_STRING (1, filename);
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);
120
121 if (! SCM_FALSEP (hook))
122 scm_apply (hook, SCM_LIST1 (filename), SCM_EOL);
123
124 { /* scope */
125 SCM port, save_port;
126 port = scm_open_file (filename,
127 scm_makfromstr ("r", (size_t) sizeof (char), 0));
128 save_port = port;
129 scm_internal_dynamic_wind (swap_port,
130 load,
131 swap_port,
132 (void *) SCM_UNPACK (port),
133 &save_port);
134 scm_close_port (port);
135 }
136 return SCM_UNSPECIFIED;
137 }
138 #undef FUNC_NAME
139
140 \f
141 /* Builtin path to scheme library files. */
142 #ifdef SCM_PKGDATA_DIR
143 SCM_DEFINE (scm_sys_package_data_dir, "%package-data-dir", 0, 0, 0,
144 (),
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}.")
148 #define FUNC_NAME s_scm_sys_package_data_dir
149 {
150 return scm_makfrom0str (SCM_PKGDATA_DIR);
151 }
152 #undef FUNC_NAME
153 #endif /* SCM_PKGDATA_DIR */
154
155 #ifdef SCM_LIBRARY_DIR
156 SCM_DEFINE (scm_sys_library_dir, "%library-dir", 0,0,0,
157 (),
158 "Return the directory where the Guile Scheme library files are installed.\n"
159 "E.g., may return \"/usr/share/guile/1.3.5\".")
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
168 SCM_DEFINE (scm_sys_site_dir, "%site-dir", 0,0,0,
169 (),
170 "Return the directory where the Guile site files are installed.\n"
171 "E.g., may return \"/usr/share/guile/site\".")
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
181 \f
182 /* Initializing the load path, and searching it. */
183
184 /* List of names of directories we search for files to load. */
185 static SCM *scm_loc_load_path;
186
187 /* List of extensions we try adding to the filenames. */
188 static SCM *scm_loc_load_extensions;
189
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. */
194 SCM
195 scm_internal_parse_path (char *path, SCM tail)
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
218 SCM_DEFINE (scm_parse_path, "parse-path", 1, 1, 0,
219 (SCM path, SCM tail),
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.")
224 #define FUNC_NAME s_scm_parse_path
225 {
226 SCM_ASSERT (SCM_FALSEP (path) || (SCM_STRINGP (path)),
227 path,
228 SCM_ARG1, FUNC_NAME);
229 if (SCM_UNBNDP (tail))
230 tail = SCM_EOL;
231 return (SCM_FALSEP (path)
232 ? tail
233 : scm_internal_parse_path (SCM_STRING_CHARS (path), tail));
234 }
235 #undef FUNC_NAME
236
237
238 /* Initialize the global variable %load-path, given the value of the
239 SCM_SITE_DIR and SCM_LIBRARY_DIR preprocessor symbols and the
240 GUILE_LOAD_PATH environment variable. */
241 void
242 scm_init_load_path ()
243 {
244 SCM path = SCM_EOL;
245
246 #ifdef SCM_LIBRARY_DIR
247 path = SCM_LIST3 (scm_makfrom0str (SCM_SITE_DIR),
248 scm_makfrom0str (SCM_LIBRARY_DIR),
249 scm_makfrom0str (SCM_PKGDATA_DIR));
250 #endif /* SCM_LIBRARY_DIR */
251
252 path = scm_internal_parse_path (getenv ("GUILE_LOAD_PATH"), path);
253
254 *scm_loc_load_path = path;
255 }
256
257 SCM scm_listofnullstr;
258
259 /* Search PATH for a directory containing a file named FILENAME.
260 The file must be readable, and not a directory.
261 If we find one, return its full filename; otherwise, return #f.
262 If FILENAME is absolute, return it unchanged.
263 If given, EXTENSIONS is a list of strings; for each directory
264 in PATH, we search for FILENAME concatenated with each EXTENSION. */
265 SCM_DEFINE (scm_search_path, "search-path", 2, 1, 0,
266 (SCM path, SCM filename, SCM extensions),
267 "Search @var{path} for a directory containing a file named\n"
268 "@var{filename}. The file must be readable, and not a directory.\n"
269 "If we find one, return its full filename; otherwise, return\n"
270 "@code{#f}. If @var{filename} is absolute, return it unchanged.\n"
271 "If given, @var{extensions} is a list of strings; for each\n"
272 "directory in @var{path}, we search for @var{filename}\n"
273 "concatenated with each @var{extension}.")
274 #define FUNC_NAME s_scm_search_path
275 {
276 char *filename_chars;
277 int filename_len;
278 size_t max_path_len; /* maximum length of any PATH element */
279 size_t max_ext_len; /* maximum length of any EXTENSIONS element */
280
281 SCM_VALIDATE_LIST (1,path);
282 SCM_VALIDATE_STRING (2, filename);
283 if (SCM_UNBNDP (extensions))
284 extensions = SCM_EOL;
285 else
286 SCM_VALIDATE_LIST (3,extensions);
287
288 filename_chars = SCM_STRING_CHARS (filename);
289 filename_len = SCM_STRING_LENGTH (filename);
290
291 /* If FILENAME is absolute, return it unchanged. */
292 if (filename_len >= 1 && filename_chars[0] == '/')
293 return filename;
294
295 /* Find the length of the longest element of path. */
296 {
297 SCM walk;
298
299 max_path_len = 0;
300 for (walk = path; !SCM_NULLP (walk); walk = SCM_CDR (walk))
301 {
302 SCM elt = SCM_CAR (walk);
303 SCM_ASSERT_TYPE (SCM_STRINGP (elt), path, 1, FUNC_NAME,
304 "list of strings");
305 if (SCM_STRING_LENGTH (elt) > max_path_len)
306 max_path_len = SCM_STRING_LENGTH (elt);
307 }
308 }
309
310 /* If FILENAME has an extension, don't try to add EXTENSIONS to it. */
311 {
312 char *endp;
313
314 for (endp = filename_chars + filename_len - 1;
315 endp >= filename_chars;
316 endp--)
317 {
318 if (*endp == '.')
319 {
320 /* This filename already has an extension, so cancel the
321 list of extensions. */
322 extensions = SCM_EOL;
323 break;
324 }
325 else if (*endp == '/')
326 /* This filename has no extension, so keep the current list
327 of extensions. */
328 break;
329 }
330 }
331
332 /* Find the length of the longest element of the load extensions
333 list. */
334 { /* scope */
335 SCM walk;
336
337 max_ext_len = 0;
338 for (walk = extensions; !SCM_NULLP (walk); walk = SCM_CDR (walk))
339 {
340 SCM elt = SCM_CAR (walk);
341 SCM_ASSERT_TYPE (SCM_STRINGP (elt), elt, 3, FUNC_NAME,
342 "list of strings");
343 if (SCM_STRING_LENGTH (elt) > max_ext_len)
344 max_ext_len = SCM_STRING_LENGTH (elt);
345 }
346 }
347
348 SCM_DEFER_INTS;
349
350 { /* scope */
351 SCM result = SCM_BOOL_F;
352 size_t buf_size = max_path_len + 1 + filename_len + max_ext_len + 1;
353 char *buf = SCM_MUST_MALLOC (buf_size);
354
355 /* This simplifies the loop below a bit. */
356 if (SCM_NULLP (extensions))
357 extensions = scm_listofnullstr;
358
359 /* Try every path element. At this point, we know the path is a
360 proper list of strings. */
361 for (; !SCM_NULLP (path); path = SCM_CDR (path))
362 {
363 size_t len;
364 SCM dir = SCM_CAR (path);
365 SCM exts;
366
367 /* Concatenate the path name and the filename. */
368 len = SCM_STRING_LENGTH (dir);
369 memcpy (buf, SCM_STRING_CHARS (dir), len);
370 if (len >= 1 && buf[len - 1] != '/')
371 buf[len++] = '/';
372 memcpy (buf + len, filename_chars, filename_len);
373 len += filename_len;
374
375 /* Try every extension. At this point, we know the extension
376 list is a proper, nonempty list of strings. */
377 for (exts = extensions; !SCM_NULLP (exts); exts = SCM_CDR (exts))
378 {
379 SCM ext = SCM_CAR (exts);
380 size_t ext_len = SCM_STRING_LENGTH (ext);
381 struct stat mode;
382
383 /* Concatenate the extension. */
384 memcpy (buf + len, SCM_STRING_CHARS (ext), ext_len);
385 buf[len + ext_len] = '\0';
386
387 /* If the file exists at all, we should return it. If the
388 file is inaccessible, then that's an error. */
389 if (stat (buf, &mode) == 0
390 && ! (mode.st_mode & S_IFDIR))
391 {
392 result = scm_makfromstr (buf, len + ext_len, 0);
393 goto end;
394 }
395 }
396 }
397
398 end:
399 scm_must_free (buf);
400 scm_done_free (buf_size);
401 SCM_ALLOW_INTS;
402 return result;
403 }
404 }
405 #undef FUNC_NAME
406
407
408 /* Search %load-path for a directory containing a file named FILENAME.
409 The file must be readable, and not a directory.
410 If we find one, return its full filename; otherwise, return #f.
411 If FILENAME is absolute, return it unchanged. */
412 SCM_DEFINE (scm_sys_search_load_path, "%search-load-path", 1, 0, 0,
413 (SCM filename),
414 "Search @var{%load-path} for the file named @var{filename},\n"
415 "which must be readable by the current user. If @var{filename}\n"
416 "is found in the list of paths to search or is an absolute\n"
417 "pathname, return its full pathname. Otherwise, return\n"
418 "@code{#f}. Filenames may have any of the optional extensions\n"
419 "in the @code{%load-extensions} list; @code{%search-load-path}\n"
420 "will try each extension automatically.")
421 #define FUNC_NAME s_scm_sys_search_load_path
422 {
423 SCM path = *scm_loc_load_path;
424 SCM exts = *scm_loc_load_extensions;
425 SCM_VALIDATE_STRING (1, filename);
426
427 if (scm_ilength (path) < 0)
428 SCM_MISC_ERROR ("%load-path is not a proper list", SCM_EOL);
429 if (scm_ilength (exts) < 0)
430 SCM_MISC_ERROR ("%load-extension list is not a proper list", SCM_EOL);
431 return scm_search_path (path, filename, exts);
432 }
433 #undef FUNC_NAME
434
435
436 SCM_DEFINE (scm_primitive_load_path, "primitive-load-path", 1, 0, 0,
437 (SCM filename),
438 "Search @var{%load-path} for the file named @var{filename} and\n"
439 "load it into the top-level environment. If @var{filename} is a\n"
440 "relative pathname and is not found in the list of search paths,\n"
441 "an error is signalled.")
442 #define FUNC_NAME s_scm_primitive_load_path
443 {
444 SCM full_filename;
445
446 SCM_VALIDATE_STRING (1, filename);
447
448 full_filename = scm_sys_search_load_path (filename);
449
450 if (SCM_FALSEP (full_filename))
451 {
452 int absolute = (SCM_STRING_LENGTH (filename) >= 1
453 && SCM_STRING_CHARS (filename)[0] == '/');
454 SCM_MISC_ERROR ((absolute
455 ? "Unable to load file ~S"
456 : "Unable to find file ~S in load path"),
457 SCM_LIST1 (filename));
458 }
459
460 return scm_primitive_load (full_filename);
461 }
462 #undef FUNC_NAME
463
464 #if SCM_DEBUG_DEPRECATED == 0
465
466 /* Eval now copies source properties, so this function is no longer required.
467 */
468
469 SCM_SYMBOL (scm_end_of_file_key, "end-of-file");
470
471 SCM_DEFINE (scm_read_and_eval_x, "read-and-eval!", 0, 1, 0,
472 (SCM port),
473 "Read a form from @var{port} (standard input by default), and evaluate it\n"
474 "(memoizing it in the process) in the top-level environment. If no data\n"
475 "is left to be read from @var{port}, an @code{end-of-file} error is\n"
476 "signalled.")
477 #define FUNC_NAME s_scm_read_and_eval_x
478 {
479 SCM form = scm_read (port);
480 if (SCM_EOF_OBJECT_P (form))
481 scm_ithrow (scm_end_of_file_key, SCM_EOL, 1);
482 return scm_eval_x (form, scm_current_module ());
483 }
484 #undef FUNC_NAME
485
486 #endif
487
488 \f
489 /* Information about the build environment. */
490
491 /* Initialize the scheme variable %guile-build-info, based on data
492 provided by the Makefile, via libpath.h. */
493 static void
494 init_build_info ()
495 {
496 static struct { char *name; char *value; } info[] = SCM_BUILD_INFO;
497 SCM *loc = SCM_VARIABLE_LOC (scm_c_define ("%guile-build-info", SCM_EOL));
498 unsigned long i;
499
500 for (i = 0; i < (sizeof (info) / sizeof (info[0])); i++)
501 *loc = scm_acons (scm_str2symbol (info[i].name),
502 scm_makfrom0str (info[i].value),
503 *loc);
504 }
505
506
507 \f
508 void
509 scm_init_load ()
510 {
511 scm_listofnullstr = scm_permanent_object (SCM_LIST1 (scm_nullstr));
512 scm_loc_load_path = SCM_VARIABLE_LOC (scm_c_define ("%load-path", SCM_EOL));
513 scm_loc_load_extensions
514 = SCM_VARIABLE_LOC (scm_c_define ("%load-extensions",
515 SCM_LIST2 (scm_makfrom0str (".scm"),
516 scm_nullstr)));
517 scm_loc_load_hook = SCM_VARIABLE_LOC (scm_c_define ("%load-hook", SCM_BOOL_F));
518
519 init_build_info ();
520
521 #ifndef SCM_MAGIC_SNARFER
522 #include "libguile/load.x"
523 #endif
524 }
525
526 /*
527 Local Variables:
528 c-file-style: "gnu"
529 End:
530 */