1 /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2004 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library 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 GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "libguile/_scm.h"
29 #include "libguile/libpath.h"
30 #include "libguile/fports.h"
31 #include "libguile/read.h"
32 #include "libguile/eval.h"
33 #include "libguile/throw.h"
34 #include "libguile/alist.h"
35 #include "libguile/dynwind.h"
36 #include "libguile/root.h"
37 #include "libguile/strings.h"
38 #include "libguile/modules.h"
39 #include "libguile/lang.h"
40 #include "libguile/chars.h"
41 #include "libguile/srfi-13.h"
43 #include "libguile/validate.h"
44 #include "libguile/load.h"
46 #include <sys/types.h>
51 #endif /* HAVE_UNISTD_H */
58 /* Loading a file, given an absolute filename. */
60 /* Hook to run when we load a file, perhaps to announce the fact somewhere.
61 Applied to the full name of the file. */
62 static SCM
*scm_loc_load_hook
;
64 SCM_DEFINE (scm_primitive_load
, "primitive-load", 1, 0, 0,
66 "Load the file named @var{filename} and evaluate its contents in\n"
67 "the top-level environment. The load paths are not searched;\n"
68 "@var{filename} must either be a full pathname or be a pathname\n"
69 "relative to the current directory. If the variable\n"
70 "@code{%load-hook} is defined, it should be bound to a procedure\n"
71 "that will be called before any code is loaded. See the\n"
72 "documentation for @code{%load-hook} later in this section.")
73 #define FUNC_NAME s_scm_primitive_load
75 SCM hook
= *scm_loc_load_hook
;
76 SCM_VALIDATE_STRING (1, filename
);
77 if (scm_is_true (hook
) && scm_is_false (scm_procedure_p (hook
)))
78 SCM_MISC_ERROR ("value of %load-hook is neither a procedure nor #f",
81 if (!scm_is_false (hook
))
82 scm_call_1 (hook
, filename
);
85 SCM port
= scm_open_file (filename
, scm_from_locale_string ("r"));
86 scm_frame_begin (SCM_F_FRAME_REWINDABLE
);
87 scm_i_frame_current_load_port (port
);
91 SCM form
= scm_read (port
);
92 if (SCM_EOF_OBJECT_P (form
))
94 scm_primitive_eval_x (form
);
98 scm_close_port (port
);
100 return SCM_UNSPECIFIED
;
105 scm_c_primitive_load (const char *filename
)
107 return scm_primitive_load (scm_from_locale_string (filename
));
111 /* Builtin path to scheme library files. */
112 #ifdef SCM_PKGDATA_DIR
113 SCM_DEFINE (scm_sys_package_data_dir
, "%package-data-dir", 0, 0, 0,
115 "Return the name of the directory where Scheme packages, modules and\n"
116 "libraries are kept. On most Unix systems, this will be\n"
117 "@samp{/usr/local/share/guile}.")
118 #define FUNC_NAME s_scm_sys_package_data_dir
120 return scm_from_locale_string (SCM_PKGDATA_DIR
);
123 #endif /* SCM_PKGDATA_DIR */
125 #ifdef SCM_LIBRARY_DIR
126 SCM_DEFINE (scm_sys_library_dir
, "%library-dir", 0,0,0,
128 "Return the directory where the Guile Scheme library files are installed.\n"
129 "E.g., may return \"/usr/share/guile/1.3.5\".")
130 #define FUNC_NAME s_scm_sys_library_dir
132 return scm_from_locale_string (SCM_LIBRARY_DIR
);
135 #endif /* SCM_LIBRARY_DIR */
138 SCM_DEFINE (scm_sys_site_dir
, "%site-dir", 0,0,0,
140 "Return the directory where the Guile site files are installed.\n"
141 "E.g., may return \"/usr/share/guile/site\".")
142 #define FUNC_NAME s_scm_sys_site_dir
144 return scm_from_locale_string (SCM_SITE_DIR
);
147 #endif /* SCM_SITE_DIR */
152 /* Initializing the load path, and searching it. */
154 /* List of names of directories we search for files to load. */
155 static SCM
*scm_loc_load_path
;
157 /* List of extensions we try adding to the filenames. */
158 static SCM
*scm_loc_load_extensions
;
161 SCM_DEFINE (scm_parse_path
, "parse-path", 1, 1, 0,
162 (SCM path
, SCM tail
),
163 "Parse @var{path}, which is expected to be a colon-separated\n"
164 "string, into a list and return the resulting list with\n"
165 "@var{tail} appended. If @var{path} is @code{#f}, @var{tail}\n"
167 #define FUNC_NAME s_scm_parse_path
170 SCM sep
= SCM_MAKE_CHAR (';');
172 SCM sep
= SCM_MAKE_CHAR (':');
175 if (SCM_UNBNDP (tail
))
177 return (scm_is_false (path
)
179 : scm_append_x (scm_list_2 (scm_string_split (path
, sep
), tail
)));
184 /* Initialize the global variable %load-path, given the value of the
185 SCM_SITE_DIR and SCM_LIBRARY_DIR preprocessor symbols and the
186 GUILE_LOAD_PATH environment variable. */
188 scm_init_load_path ()
193 #ifdef SCM_LIBRARY_DIR
194 path
= scm_list_3 (scm_from_locale_string (SCM_SITE_DIR
),
195 scm_from_locale_string (SCM_LIBRARY_DIR
),
196 scm_from_locale_string (SCM_PKGDATA_DIR
));
197 #endif /* SCM_LIBRARY_DIR */
199 env
= getenv ("GUILE_LOAD_PATH");
201 path
= scm_parse_path (scm_from_locale_string (env
), path
);
203 *scm_loc_load_path
= path
;
206 SCM scm_listofnullstr
;
208 /* Utility functions for assembling C strings in a buffer.
217 stringbuf_free (void *data
)
219 struct stringbuf
*buf
= (struct stringbuf
*)data
;
224 stringbuf_grow (struct stringbuf
*buf
)
226 size_t ptroff
= buf
->ptr
- buf
->buf
;
228 buf
->buf
= scm_realloc (buf
->buf
, buf
->buf_len
);
229 buf
->ptr
= buf
->buf
+ ptroff
;
233 stringbuf_cat_locale_string (struct stringbuf
*buf
, SCM str
)
235 size_t max_len
= buf
->buf_len
- (buf
->ptr
- buf
->buf
) - 1;
236 size_t len
= scm_to_locale_stringbuf (str
, buf
->ptr
, max_len
);
239 /* buffer is too small, double its size and try again.
241 stringbuf_grow (buf
);
242 stringbuf_cat_locale_string (buf
, str
);
246 /* string fits, terminate it and check for embedded '\0'.
248 buf
->ptr
[len
] = '\0';
249 if (strlen (buf
->ptr
) != len
)
250 scm_misc_error (NULL
,
251 "string contains #\\nul character: ~S",
258 stringbuf_cat (struct stringbuf
*buf
, char *str
)
260 size_t max_len
= buf
->buf_len
- (buf
->ptr
- buf
->buf
) - 1;
261 size_t len
= strlen (str
);
264 /* buffer is too small, double its size and try again.
266 stringbuf_grow (buf
);
267 stringbuf_cat (buf
, str
);
271 /* string fits, copy it into buffer.
273 strcpy (buf
->ptr
, str
);
279 /* Search PATH for a directory containing a file named FILENAME.
280 The file must be readable, and not a directory.
281 If we find one, return its full filename; otherwise, return #f.
282 If FILENAME is absolute, return it unchanged.
283 If given, EXTENSIONS is a list of strings; for each directory
284 in PATH, we search for FILENAME concatenated with each EXTENSION. */
285 SCM_DEFINE (scm_search_path
, "search-path", 2, 1, 0,
286 (SCM path
, SCM filename
, SCM extensions
),
287 "Search @var{path} for a directory containing a file named\n"
288 "@var{filename}. The file must be readable, and not a directory.\n"
289 "If we find one, return its full filename; otherwise, return\n"
290 "@code{#f}. If @var{filename} is absolute, return it unchanged.\n"
291 "If given, @var{extensions} is a list of strings; for each\n"
292 "directory in @var{path}, we search for @var{filename}\n"
293 "concatenated with each @var{extension}.")
294 #define FUNC_NAME s_scm_search_path
296 struct stringbuf buf
;
297 char *filename_chars
;
299 SCM result
= SCM_BOOL_F
;
301 if (SCM_UNBNDP (extensions
))
302 extensions
= SCM_EOL
;
306 filename_chars
= scm_to_locale_string (filename
);
307 filename_len
= strlen (filename_chars
);
308 scm_frame_free (filename_chars
);
310 /* If FILENAME is absolute, return it unchanged. */
312 if (((filename_len
>= 1) &&
313 (filename_chars
[0] == '/' || filename_chars
[0] == '\\')) ||
314 ((filename_len
>= 3) && filename_chars
[1] == ':' &&
315 ((filename_chars
[0] >= 'a' && filename_chars
[0] <= 'z') ||
316 (filename_chars
[0] >= 'A' && filename_chars
[0] <= 'Z')) &&
317 (filename_chars
[2] == '/' || filename_chars
[2] == '\\')))
319 if (filename_len
>= 1 && filename_chars
[0] == '/')
326 /* If FILENAME has an extension, don't try to add EXTENSIONS to it. */
330 for (endp
= filename_chars
+ filename_len
- 1;
331 endp
>= filename_chars
;
336 /* This filename already has an extension, so cancel the
337 list of extensions. */
338 extensions
= SCM_EOL
;
342 else if (*endp
== '/' || *endp
== '\\')
344 else if (*endp
== '/')
346 /* This filename has no extension, so keep the current list
352 /* This simplifies the loop below a bit.
354 if (scm_is_null (extensions
))
355 extensions
= scm_listofnullstr
;
358 buf
.buf
= scm_malloc (buf
.buf_len
);
359 scm_frame_unwind_handler (stringbuf_free
, &buf
, SCM_F_WIND_EXPLICITLY
);
361 /* Try every path element.
363 for (; scm_is_pair (path
); path
= SCM_CDR (path
))
365 SCM dir
= SCM_CAR (path
);
370 stringbuf_cat_locale_string (&buf
, dir
);
372 /* Concatenate the path name and the filename. */
375 if (buf
.ptr
> buf
.buf
&& buf
.ptr
[-1] != '/' && buf
.ptr
[-1] != '\\')
377 if (buf
.ptr
> buf
.buf
>= 1 && buf
.ptr
[-1] != '/')
379 stringbuf_cat (&buf
, "/");
381 stringbuf_cat (&buf
, filename_chars
);
382 sans_ext_len
= buf
.ptr
- buf
.buf
;
384 /* Try every extension. */
385 for (exts
= extensions
; scm_is_pair (exts
); exts
= SCM_CDR (exts
))
387 SCM ext
= SCM_CAR (exts
);
390 buf
.ptr
= buf
.buf
+ sans_ext_len
;
391 stringbuf_cat_locale_string (&buf
, ext
);
393 /* If the file exists at all, we should return it. If the
394 file is inaccessible, then that's an error. */
396 if (stat (buf
.buf
, &mode
) == 0
397 && ! (mode
.st_mode
& S_IFDIR
))
399 result
= scm_from_locale_string (buf
.buf
);
404 if (!SCM_NULL_OR_NIL_P (exts
))
405 scm_wrong_type_arg_msg (NULL
, 0, extensions
, "proper list");
408 if (!SCM_NULL_OR_NIL_P (path
))
409 scm_wrong_type_arg_msg (NULL
, 0, path
, "proper list");
418 /* Search %load-path for a directory containing a file named FILENAME.
419 The file must be readable, and not a directory.
420 If we find one, return its full filename; otherwise, return #f.
421 If FILENAME is absolute, return it unchanged. */
422 SCM_DEFINE (scm_sys_search_load_path
, "%search-load-path", 1, 0, 0,
424 "Search @var{%load-path} for the file named @var{filename},\n"
425 "which must be readable by the current user. If @var{filename}\n"
426 "is found in the list of paths to search or is an absolute\n"
427 "pathname, return its full pathname. Otherwise, return\n"
428 "@code{#f}. Filenames may have any of the optional extensions\n"
429 "in the @code{%load-extensions} list; @code{%search-load-path}\n"
430 "will try each extension automatically.")
431 #define FUNC_NAME s_scm_sys_search_load_path
433 SCM path
= *scm_loc_load_path
;
434 SCM exts
= *scm_loc_load_extensions
;
435 SCM_VALIDATE_STRING (1, filename
);
437 if (scm_ilength (path
) < 0)
438 SCM_MISC_ERROR ("%load-path is not a proper list", SCM_EOL
);
439 if (scm_ilength (exts
) < 0)
440 SCM_MISC_ERROR ("%load-extension list is not a proper list", SCM_EOL
);
441 return scm_search_path (path
, filename
, exts
);
446 SCM_DEFINE (scm_primitive_load_path
, "primitive-load-path", 1, 0, 0,
448 "Search @var{%load-path} for the file named @var{filename} and\n"
449 "load it into the top-level environment. If @var{filename} is a\n"
450 "relative pathname and is not found in the list of search paths,\n"
451 "an error is signalled.")
452 #define FUNC_NAME s_scm_primitive_load_path
456 full_filename
= scm_sys_search_load_path (filename
);
458 if (scm_is_false (full_filename
))
459 SCM_MISC_ERROR ("Unable to find file ~S in load path",
460 scm_list_1 (filename
));
462 return scm_primitive_load (full_filename
);
467 scm_c_primitive_load_path (const char *filename
)
469 return scm_primitive_load_path (scm_from_locale_string (filename
));
473 /* Information about the build environment. */
475 /* Initialize the scheme variable %guile-build-info, based on data
476 provided by the Makefile, via libpath.h. */
480 static struct { char *name
; char *value
; } info
[] = SCM_BUILD_INFO
;
481 SCM
*loc
= SCM_VARIABLE_LOC (scm_c_define ("%guile-build-info", SCM_EOL
));
484 for (i
= 0; i
< (sizeof (info
) / sizeof (info
[0])); i
++)
486 SCM key
= scm_from_locale_symbol (info
[i
].name
);
487 SCM val
= scm_from_locale_string (info
[i
].value
);
488 *loc
= scm_acons (key
, val
, *loc
);
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_from_locale_string (".scm"),
502 scm_loc_load_hook
= SCM_VARIABLE_LOC (scm_c_define ("%load-hook", SCM_BOOL_F
));
506 #include "libguile/load.x"