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