(do_read_without_guile): Use the "raw_data" passed in
[bpt/guile.git] / libguile / load.c
CommitLineData
cc95e00a 1/* Copyright (C) 1995,1996,1998,1999,2000,2001, 2004 Free Software Foundation, Inc.
0f2d19dd 2 *
73be1d9e
MV
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.
0f2d19dd 7 *
73be1d9e
MV
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.
0f2d19dd 12 *
73be1d9e
MV
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
92205699 15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
73be1d9e 16 */
1bbd0b84 17
1bbd0b84 18
0f2d19dd
JB
19\f
20
cbd41c89
RB
21#if HAVE_CONFIG_H
22# include <config.h>
23#endif
24
13070bd3 25#include <string.h>
7d04d68b 26#include <stdio.h>
13070bd3 27
a0599745
MD
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"
f33b174d 38#include "libguile/modules.h"
c96d76b8 39#include "libguile/lang.h"
7d04d68b 40#include "libguile/chars.h"
c44ca4fe 41#include "libguile/srfi-13.h"
a0599745
MD
42
43#include "libguile/validate.h"
44#include "libguile/load.h"
06721500
JB
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
0f2d19dd
JB
56
57\f
06721500 58/* Loading a file, given an absolute filename. */
0f2d19dd 59
26544b96
JB
60/* Hook to run when we load a file, perhaps to announce the fact somewhere.
61 Applied to the full name of the file. */
62static SCM *scm_loc_load_hook;
63
3b3b36dd 64SCM_DEFINE (scm_primitive_load, "primitive-load", 1, 0, 0,
1bbd0b84 65 (SCM filename),
67e8151b
MG
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.")
1bbd0b84 73#define FUNC_NAME s_scm_primitive_load
0f2d19dd 74{
26544b96 75 SCM hook = *scm_loc_load_hook;
a6d9e5ab 76 SCM_VALIDATE_STRING (1, filename);
bc36d050 77 if (scm_is_true (hook) && scm_is_false (scm_procedure_p (hook)))
2ade72d7
DH
78 SCM_MISC_ERROR ("value of %load-hook is neither a procedure nor #f",
79 SCM_EOL);
26544b96 80
bc36d050 81 if (!scm_is_false (hook))
fdc28395 82 scm_call_1 (hook, filename);
26544b96 83
1bbd0b84 84 { /* scope */
b5623573
MV
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 ();
0f2d19dd
JB
98 scm_close_port (port);
99 }
b59b97ba 100 return SCM_UNSPECIFIED;
0f2d19dd 101}
1bbd0b84 102#undef FUNC_NAME
0f2d19dd 103
c519b272
MV
104SCM
105scm_c_primitive_load (const char *filename)
106{
cc95e00a 107 return scm_primitive_load (scm_from_locale_string (filename));
c519b272
MV
108}
109
0f2d19dd 110\f
3feedb00
MD
111/* Builtin path to scheme library files. */
112#ifdef SCM_PKGDATA_DIR
a1ec6916 113SCM_DEFINE (scm_sys_package_data_dir, "%package-data-dir", 0, 0, 0,
1bbd0b84 114 (),
b380b885
MD
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}.")
1bbd0b84 118#define FUNC_NAME s_scm_sys_package_data_dir
3feedb00 119{
cc95e00a 120 return scm_from_locale_string (SCM_PKGDATA_DIR);
3feedb00 121}
1bbd0b84 122#undef FUNC_NAME
3feedb00
MD
123#endif /* SCM_PKGDATA_DIR */
124
e8e9b690 125#ifdef SCM_LIBRARY_DIR
a1ec6916 126SCM_DEFINE (scm_sys_library_dir, "%library-dir", 0,0,0,
e8e9b690 127 (),
b380b885
MD
128 "Return the directory where the Guile Scheme library files are installed.\n"
129 "E.g., may return \"/usr/share/guile/1.3.5\".")
e8e9b690
GB
130#define FUNC_NAME s_scm_sys_library_dir
131{
cc95e00a 132 return scm_from_locale_string (SCM_LIBRARY_DIR);
e8e9b690
GB
133}
134#undef FUNC_NAME
135#endif /* SCM_LIBRARY_DIR */
136
137#ifdef SCM_SITE_DIR
a1ec6916 138SCM_DEFINE (scm_sys_site_dir, "%site-dir", 0,0,0,
e8e9b690 139 (),
b380b885
MD
140 "Return the directory where the Guile site files are installed.\n"
141 "E.g., may return \"/usr/share/guile/site\".")
e8e9b690
GB
142#define FUNC_NAME s_scm_sys_site_dir
143{
cc95e00a 144 return scm_from_locale_string (SCM_SITE_DIR);
e8e9b690
GB
145}
146#undef FUNC_NAME
147#endif /* SCM_SITE_DIR */
148
149
150
3feedb00 151\f
06721500 152/* Initializing the load path, and searching it. */
0f2d19dd 153
26544b96 154/* List of names of directories we search for files to load. */
06721500
JB
155static SCM *scm_loc_load_path;
156
26544b96
JB
157/* List of extensions we try adding to the filenames. */
158static SCM *scm_loc_load_extensions;
159
01cddfc1 160
a1ec6916 161SCM_DEFINE (scm_parse_path, "parse-path", 1, 1, 0,
1bbd0b84 162 (SCM path, SCM tail),
d91788cb
MG
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.")
1bbd0b84 167#define FUNC_NAME s_scm_parse_path
04e8fb0a 168{
7d04d68b
MV
169#ifdef __MINGW32__
170 SCM sep = SCM_MAKE_CHAR (';');
171#else
172 SCM sep = SCM_MAKE_CHAR (':');
173#endif
174
04e8fb0a
MD
175 if (SCM_UNBNDP (tail))
176 tail = SCM_EOL;
7888309b 177 return (scm_is_false (path)
04e8fb0a 178 ? tail
7d04d68b 179 : scm_append_x (scm_list_2 (scm_string_split (path, sep), tail)));
04e8fb0a 180}
1bbd0b84 181#undef FUNC_NAME
04e8fb0a
MD
182
183
06721500 184/* Initialize the global variable %load-path, given the value of the
3feedb00 185 SCM_SITE_DIR and SCM_LIBRARY_DIR preprocessor symbols and the
01cddfc1 186 GUILE_LOAD_PATH environment variable. */
0f2d19dd 187void
06721500
JB
188scm_init_load_path ()
189{
11c5e0bf 190 char *env;
06721500
JB
191 SCM path = SCM_EOL;
192
3feedb00 193#ifdef SCM_LIBRARY_DIR
cc95e00a
MV
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));
3feedb00 197#endif /* SCM_LIBRARY_DIR */
06721500 198
11c5e0bf
MV
199 env = getenv ("GUILE_LOAD_PATH");
200 if (env)
201 path = scm_parse_path (scm_from_locale_string (env), path);
01cddfc1 202
06721500
JB
203 *scm_loc_load_path = path;
204}
205
04e8fb0a 206SCM scm_listofnullstr;
06721500 207
7d04d68b
MV
208/* Utility functions for assembling C strings in a buffer.
209 */
210
211struct stringbuf {
212 char *buf, *ptr;
213 size_t buf_len;
214};
215
216static void
217stringbuf_free (void *data)
218{
219 struct stringbuf *buf = (struct stringbuf *)data;
220 free (buf->buf);
221}
222
223static void
224stringbuf_grow (struct stringbuf *buf)
225{
226 size_t ptroff = buf->ptr - buf->buf;
227 buf->buf_len *= 2;
7d04d68b
MV
228 buf->buf = scm_realloc (buf->buf, buf->buf_len);
229 buf->ptr = buf->buf + ptroff;
230}
231
232static void
233stringbuf_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
257static void
258stringbuf_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
04e8fb0a 279/* Search PATH for a directory containing a file named FILENAME.
06721500 280 The file must be readable, and not a directory.
26544b96 281 If we find one, return its full filename; otherwise, return #f.
56384176
JB
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. */
3b3b36dd 285SCM_DEFINE (scm_search_path, "search-path", 2, 1, 0,
1bbd0b84 286 (SCM path, SCM filename, SCM extensions),
d91788cb
MG
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}.")
1bbd0b84 294#define FUNC_NAME s_scm_search_path
06721500 295{
7d04d68b 296 struct stringbuf buf;
56384176 297 char *filename_chars;
7d04d68b
MV
298 size_t filename_len;
299 SCM result = SCM_BOOL_F;
06721500 300
04e8fb0a 301 if (SCM_UNBNDP (extensions))
56384176 302 extensions = SCM_EOL;
56384176 303
7d04d68b
MV
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);
06721500 309
26544b96 310 /* If FILENAME is absolute, return it unchanged. */
2e945bcc
SJ
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
56384176 319 if (filename_len >= 1 && filename_chars[0] == '/')
2e945bcc 320#endif
7d04d68b
MV
321 {
322 scm_frame_end ();
323 return filename;
324 }
26544b96 325
56384176
JB
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 }
2e945bcc
SJ
341#ifdef __MINGW32__
342 else if (*endp == '/' || *endp == '\\')
343#else
56384176 344 else if (*endp == '/')
2e945bcc 345#endif
56384176
JB
346 /* This filename has no extension, so keep the current list
347 of extensions. */
348 break;
349 }
350 }
351
7d04d68b
MV
352 /* This simplifies the loop below a bit.
353 */
d2e53ed6 354 if (scm_is_null (extensions))
7d04d68b 355 extensions = scm_listofnullstr;
06721500 356
7d04d68b
MV
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);
0a74e31d 360
7d04d68b
MV
361 /* Try every path element.
362 */
d2e53ed6 363 for (; scm_is_pair (path); path = SCM_CDR (path))
7d04d68b
MV
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
2e945bcc 374#ifdef __MINGW32__
5a6d139b 375 if ((buf.ptr > buf.buf) && (buf.ptr[-1] != '/') && (buf.ptr[-1] != '\\'))
2e945bcc 376#else
5a6d139b 377 if ((buf.ptr > buf.buf) && (buf.ptr[-1] != '/'))
2e945bcc 378#endif
7d04d68b
MV
379 stringbuf_cat (&buf, "/");
380
381 stringbuf_cat (&buf, filename_chars);
382 sans_ext_len = buf.ptr - buf.buf;
383
384 /* Try every extension. */
d2e53ed6 385 for (exts = extensions; scm_is_pair (exts); exts = SCM_CDR (exts))
7d04d68b
MV
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
7d04d68b
MV
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 }
56384176 407
7d04d68b
MV
408 if (!SCM_NULL_OR_NIL_P (path))
409 scm_wrong_type_arg_msg (NULL, 0, path, "proper list");
56384176 410
7d04d68b
MV
411 end:
412 scm_frame_end ();
413 return result;
06721500 414}
1bbd0b84 415#undef FUNC_NAME
06721500
JB
416
417
04e8fb0a
MD
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. */
3b3b36dd 422SCM_DEFINE (scm_sys_search_load_path, "%search-load-path", 1, 0, 0,
67e8151b
MG
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.")
1bbd0b84 431#define FUNC_NAME s_scm_sys_search_load_path
04e8fb0a
MD
432{
433 SCM path = *scm_loc_load_path;
434 SCM exts = *scm_loc_load_extensions;
a6d9e5ab 435 SCM_VALIDATE_STRING (1, filename);
1bbd0b84 436
2ade72d7
DH
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);
1bbd0b84 441 return scm_search_path (path, filename, exts);
04e8fb0a 442}
1bbd0b84 443#undef FUNC_NAME
04e8fb0a
MD
444
445
3b3b36dd 446SCM_DEFINE (scm_primitive_load_path, "primitive-load-path", 1, 0, 0,
67e8151b
MG
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.")
1bbd0b84 452#define FUNC_NAME s_scm_primitive_load_path
06721500 453{
26544b96
JB
454 SCM full_filename;
455
26544b96
JB
456 full_filename = scm_sys_search_load_path (filename);
457
7888309b 458 if (scm_is_false (full_filename))
ffa747a6
MV
459 SCM_MISC_ERROR ("Unable to find file ~S in load path",
460 scm_list_1 (filename));
26544b96 461
deca31e1 462 return scm_primitive_load (full_filename);
06721500 463}
1bbd0b84 464#undef FUNC_NAME
06721500 465
c519b272
MV
466SCM
467scm_c_primitive_load_path (const char *filename)
468{
cc95e00a 469 return scm_primitive_load_path (scm_from_locale_string (filename));
c519b272
MV
470}
471
06721500 472\f
e151bee6 473/* Information about the build environment. */
06721500 474
e151bee6
JB
475/* Initialize the scheme variable %guile-build-info, based on data
476 provided by the Makefile, via libpath.h. */
477static void
478init_build_info ()
479{
480 static struct { char *name; char *value; } info[] = SCM_BUILD_INFO;
86d31dfe 481 SCM *loc = SCM_VARIABLE_LOC (scm_c_define ("%guile-build-info", SCM_EOL));
c014a02e 482 unsigned long i;
e151bee6
JB
483
484 for (i = 0; i < (sizeof (info) / sizeof (info[0])); i++)
cc95e00a
MV
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 }
e151bee6
JB
490}
491
e151bee6 492\f
0f2d19dd
JB
493void
494scm_init_load ()
0f2d19dd 495{
1afff620 496 scm_listofnullstr = scm_permanent_object (scm_list_1 (scm_nullstr));
86d31dfe 497 scm_loc_load_path = SCM_VARIABLE_LOC (scm_c_define ("%load-path", SCM_EOL));
26544b96 498 scm_loc_load_extensions
86d31dfe 499 = SCM_VARIABLE_LOC (scm_c_define ("%load-extensions",
cc95e00a
MV
500 scm_list_2 (scm_from_locale_string (".scm"),
501 scm_nullstr)));
86d31dfe 502 scm_loc_load_hook = SCM_VARIABLE_LOC (scm_c_define ("%load-hook", SCM_BOOL_F));
06721500 503
e151bee6
JB
504 init_build_info ();
505
a0599745 506#include "libguile/load.x"
0f2d19dd 507}
89e00824
ML
508
509/*
510 Local Variables:
511 c-file-style: "gnu"
512 End:
513*/