(scm_primitive_load): Use scm_i_frame_current_load_port instead of
[bpt/guile.git] / libguile / load.c
1 /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2004 Free Software Foundation, Inc.
2 *
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.
7 *
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.
12 *
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
16 */
17
18
19 \f
20
21 #if HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <string.h>
26 #include <stdio.h>
27
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"
42
43 #include "libguile/validate.h"
44 #include "libguile/load.h"
45
46 #include <sys/types.h>
47 #include <sys/stat.h>
48
49 #ifdef HAVE_UNISTD_H
50 #include <unistd.h>
51 #endif /* HAVE_UNISTD_H */
52
53 #ifndef R_OK
54 #define R_OK 4
55 #endif
56
57 \f
58 /* Loading a file, given an absolute filename. */
59
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;
63
64 SCM_DEFINE (scm_primitive_load, "primitive-load", 1, 0, 0,
65 (SCM filename),
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
74 {
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",
79 SCM_EOL);
80
81 if (!scm_is_false (hook))
82 scm_call_1 (hook, filename);
83
84 { /* scope */
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);
88
89 while (1)
90 {
91 SCM form = scm_read (port);
92 if (SCM_EOF_OBJECT_P (form))
93 break;
94 scm_primitive_eval_x (form);
95 }
96
97 scm_frame_end ();
98 scm_close_port (port);
99 }
100 return SCM_UNSPECIFIED;
101 }
102 #undef FUNC_NAME
103
104 SCM
105 scm_c_primitive_load (const char *filename)
106 {
107 return scm_primitive_load (scm_from_locale_string (filename));
108 }
109
110 \f
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,
114 (),
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
119 {
120 return scm_from_locale_string (SCM_PKGDATA_DIR);
121 }
122 #undef FUNC_NAME
123 #endif /* SCM_PKGDATA_DIR */
124
125 #ifdef SCM_LIBRARY_DIR
126 SCM_DEFINE (scm_sys_library_dir, "%library-dir", 0,0,0,
127 (),
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
131 {
132 return scm_from_locale_string (SCM_LIBRARY_DIR);
133 }
134 #undef FUNC_NAME
135 #endif /* SCM_LIBRARY_DIR */
136
137 #ifdef SCM_SITE_DIR
138 SCM_DEFINE (scm_sys_site_dir, "%site-dir", 0,0,0,
139 (),
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
143 {
144 return scm_from_locale_string (SCM_SITE_DIR);
145 }
146 #undef FUNC_NAME
147 #endif /* SCM_SITE_DIR */
148
149
150
151 \f
152 /* Initializing the load path, and searching it. */
153
154 /* List of names of directories we search for files to load. */
155 static SCM *scm_loc_load_path;
156
157 /* List of extensions we try adding to the filenames. */
158 static SCM *scm_loc_load_extensions;
159
160
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"
166 "is returned.")
167 #define FUNC_NAME s_scm_parse_path
168 {
169 #ifdef __MINGW32__
170 SCM sep = SCM_MAKE_CHAR (';');
171 #else
172 SCM sep = SCM_MAKE_CHAR (':');
173 #endif
174
175 if (SCM_UNBNDP (tail))
176 tail = SCM_EOL;
177 return (scm_is_false (path)
178 ? tail
179 : scm_append_x (scm_list_2 (scm_string_split (path, sep), tail)));
180 }
181 #undef FUNC_NAME
182
183
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. */
187 void
188 scm_init_load_path ()
189 {
190 char *env;
191 SCM path = SCM_EOL;
192
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 */
198
199 env = getenv ("GUILE_LOAD_PATH");
200 if (env)
201 path = scm_parse_path (scm_from_locale_string (env), path);
202
203 *scm_loc_load_path = path;
204 }
205
206 SCM scm_listofnullstr;
207
208 /* Utility functions for assembling C strings in a buffer.
209 */
210
211 struct stringbuf {
212 char *buf, *ptr;
213 size_t buf_len;
214 };
215
216 static void
217 stringbuf_free (void *data)
218 {
219 struct stringbuf *buf = (struct stringbuf *)data;
220 free (buf->buf);
221 }
222
223 static void
224 stringbuf_grow (struct stringbuf *buf)
225 {
226 size_t ptroff = buf->ptr - buf->buf;
227 buf->buf_len *= 2;
228 buf->buf = scm_realloc (buf->buf, buf->buf_len);
229 buf->ptr = buf->buf + ptroff;
230 }
231
232 static void
233 stringbuf_cat_locale_string (struct stringbuf *buf, SCM str)
234 {
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);
237 if (len > max_len)
238 {
239 /* buffer is too small, double its size and try again.
240 */
241 stringbuf_grow (buf);
242 stringbuf_cat_locale_string (buf, str);
243 }
244 else
245 {
246 /* string fits, terminate it and check for embedded '\0'.
247 */
248 buf->ptr[len] = '\0';
249 if (strlen (buf->ptr) != len)
250 scm_misc_error (NULL,
251 "string contains #\\nul character: ~S",
252 scm_list_1 (str));
253 buf->ptr += len;
254 }
255 }
256
257 static void
258 stringbuf_cat (struct stringbuf *buf, char *str)
259 {
260 size_t max_len = buf->buf_len - (buf->ptr - buf->buf) - 1;
261 size_t len = strlen (str);
262 if (len > max_len)
263 {
264 /* buffer is too small, double its size and try again.
265 */
266 stringbuf_grow (buf);
267 stringbuf_cat (buf, str);
268 }
269 else
270 {
271 /* string fits, copy it into buffer.
272 */
273 strcpy (buf->ptr, str);
274 buf->ptr += len;
275 }
276 }
277
278
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
295 {
296 struct stringbuf buf;
297 char *filename_chars;
298 size_t filename_len;
299 SCM result = SCM_BOOL_F;
300
301 if (SCM_UNBNDP (extensions))
302 extensions = SCM_EOL;
303
304 scm_frame_begin (0);
305
306 filename_chars = scm_to_locale_string (filename);
307 filename_len = strlen (filename_chars);
308 scm_frame_free (filename_chars);
309
310 /* If FILENAME is absolute, return it unchanged. */
311 #ifdef __MINGW32__
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] == '\\')))
318 #else
319 if (filename_len >= 1 && filename_chars[0] == '/')
320 #endif
321 {
322 scm_frame_end ();
323 return filename;
324 }
325
326 /* If FILENAME has an extension, don't try to add EXTENSIONS to it. */
327 {
328 char *endp;
329
330 for (endp = filename_chars + filename_len - 1;
331 endp >= filename_chars;
332 endp--)
333 {
334 if (*endp == '.')
335 {
336 /* This filename already has an extension, so cancel the
337 list of extensions. */
338 extensions = SCM_EOL;
339 break;
340 }
341 #ifdef __MINGW32__
342 else if (*endp == '/' || *endp == '\\')
343 #else
344 else if (*endp == '/')
345 #endif
346 /* This filename has no extension, so keep the current list
347 of extensions. */
348 break;
349 }
350 }
351
352 /* This simplifies the loop below a bit.
353 */
354 if (scm_is_null (extensions))
355 extensions = scm_listofnullstr;
356
357 buf.buf_len = 512;
358 buf.buf = scm_malloc (buf.buf_len);
359 scm_frame_unwind_handler (stringbuf_free, &buf, SCM_F_WIND_EXPLICITLY);
360
361 /* Try every path element.
362 */
363 for (; scm_is_pair (path); path = SCM_CDR (path))
364 {
365 SCM dir = SCM_CAR (path);
366 SCM exts;
367 size_t sans_ext_len;
368
369 buf.ptr = buf.buf;
370 stringbuf_cat_locale_string (&buf, dir);
371
372 /* Concatenate the path name and the filename. */
373
374 #ifdef __MINGW32__
375 if (buf.ptr > buf.buf && buf.ptr[-1] != '/' && buf.ptr[-1] != '\\')
376 #else
377 if (buf.ptr > buf.buf >= 1 && buf.ptr[-1] != '/')
378 #endif
379 stringbuf_cat (&buf, "/");
380
381 stringbuf_cat (&buf, filename_chars);
382 sans_ext_len = buf.ptr - buf.buf;
383
384 /* Try every extension. */
385 for (exts = extensions; scm_is_pair (exts); exts = SCM_CDR (exts))
386 {
387 SCM ext = SCM_CAR (exts);
388 struct stat mode;
389
390 buf.ptr = buf.buf + sans_ext_len;
391 stringbuf_cat_locale_string (&buf, ext);
392
393 /* If the file exists at all, we should return it. If the
394 file is inaccessible, then that's an error. */
395
396 if (stat (buf.buf, &mode) == 0
397 && ! (mode.st_mode & S_IFDIR))
398 {
399 result = scm_from_locale_string (buf.buf);
400 goto end;
401 }
402 }
403
404 if (!SCM_NULL_OR_NIL_P (exts))
405 scm_wrong_type_arg_msg (NULL, 0, extensions, "proper list");
406 }
407
408 if (!SCM_NULL_OR_NIL_P (path))
409 scm_wrong_type_arg_msg (NULL, 0, path, "proper list");
410
411 end:
412 scm_frame_end ();
413 return result;
414 }
415 #undef FUNC_NAME
416
417
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,
423 (SCM filename),
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
432 {
433 SCM path = *scm_loc_load_path;
434 SCM exts = *scm_loc_load_extensions;
435 SCM_VALIDATE_STRING (1, filename);
436
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);
442 }
443 #undef FUNC_NAME
444
445
446 SCM_DEFINE (scm_primitive_load_path, "primitive-load-path", 1, 0, 0,
447 (SCM filename),
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
453 {
454 SCM full_filename;
455
456 full_filename = scm_sys_search_load_path (filename);
457
458 if (scm_is_false (full_filename))
459 SCM_MISC_ERROR ("Unable to find file ~S in load path",
460 scm_list_1 (filename));
461
462 return scm_primitive_load (full_filename);
463 }
464 #undef FUNC_NAME
465
466 SCM
467 scm_c_primitive_load_path (const char *filename)
468 {
469 return scm_primitive_load_path (scm_from_locale_string (filename));
470 }
471
472 \f
473 /* Information about the build environment. */
474
475 /* Initialize the scheme variable %guile-build-info, based on data
476 provided by the Makefile, via libpath.h. */
477 static void
478 init_build_info ()
479 {
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));
482 unsigned long i;
483
484 for (i = 0; i < (sizeof (info) / sizeof (info[0])); i++)
485 {
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);
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_from_locale_string (".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 #include "libguile/load.x"
507 }
508
509 /*
510 Local Variables:
511 c-file-style: "gnu"
512 End:
513 */