(gh_set_substr): Made src const.
[bpt/guile.git] / libguile / load.c
CommitLineData
2ade72d7 1/* Copyright (C) 1995,1996,1998,1999,2000,2001 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
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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
MV
40#include "libguile/chars.h"
41#include "libguile/strop.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
c7023c69 64static void
d7834b91
MD
65swap_port (void *data)
66{
67 SCM *save_port = data, tmp = scm_cur_loadp;
68 scm_cur_loadp = *save_port;
69 *save_port = tmp;
70}
71
72static SCM
73load (void *data)
74{
54778cd3 75 SCM port = SCM_PACK (data);
d7834b91
MD
76 while (1)
77 {
54778cd3 78 SCM form = scm_read (port);
d7834b91
MD
79 if (SCM_EOF_OBJECT_P (form))
80 break;
f1b7a066 81 scm_primitive_eval_x (form);
d7834b91
MD
82 }
83 return SCM_UNSPECIFIED;
84}
85
3b3b36dd 86SCM_DEFINE (scm_primitive_load, "primitive-load", 1, 0, 0,
1bbd0b84 87 (SCM filename),
67e8151b
MG
88 "Load the file named @var{filename} and evaluate its contents in\n"
89 "the top-level environment. The load paths are not searched;\n"
90 "@var{filename} must either be a full pathname or be a pathname\n"
91 "relative to the current directory. If the variable\n"
92 "@code{%load-hook} is defined, it should be bound to a procedure\n"
93 "that will be called before any code is loaded. See the\n"
94 "documentation for @code{%load-hook} later in this section.")
1bbd0b84 95#define FUNC_NAME s_scm_primitive_load
0f2d19dd 96{
26544b96 97 SCM hook = *scm_loc_load_hook;
a6d9e5ab 98 SCM_VALIDATE_STRING (1, filename);
bc36d050 99 if (scm_is_true (hook) && scm_is_false (scm_procedure_p (hook)))
2ade72d7
DH
100 SCM_MISC_ERROR ("value of %load-hook is neither a procedure nor #f",
101 SCM_EOL);
26544b96 102
bc36d050 103 if (!scm_is_false (hook))
fdc28395 104 scm_call_1 (hook, filename);
26544b96 105
1bbd0b84 106 { /* scope */
d7834b91 107 SCM port, save_port;
36284627 108 port = scm_open_file (filename, scm_mem2string ("r", sizeof (char)));
d7834b91
MD
109 save_port = port;
110 scm_internal_dynamic_wind (swap_port,
111 load,
112 swap_port,
54778cd3 113 (void *) SCM_UNPACK (port),
d7834b91 114 &save_port);
0f2d19dd
JB
115 scm_close_port (port);
116 }
b59b97ba 117 return SCM_UNSPECIFIED;
0f2d19dd 118}
1bbd0b84 119#undef FUNC_NAME
0f2d19dd 120
c519b272
MV
121SCM
122scm_c_primitive_load (const char *filename)
123{
124 return scm_primitive_load (scm_makfrom0str (filename));
125}
126
0f2d19dd 127\f
3feedb00
MD
128/* Builtin path to scheme library files. */
129#ifdef SCM_PKGDATA_DIR
a1ec6916 130SCM_DEFINE (scm_sys_package_data_dir, "%package-data-dir", 0, 0, 0,
1bbd0b84 131 (),
b380b885
MD
132 "Return the name of the directory where Scheme packages, modules and\n"
133 "libraries are kept. On most Unix systems, this will be\n"
134 "@samp{/usr/local/share/guile}.")
1bbd0b84 135#define FUNC_NAME s_scm_sys_package_data_dir
3feedb00
MD
136{
137 return scm_makfrom0str (SCM_PKGDATA_DIR);
138}
1bbd0b84 139#undef FUNC_NAME
3feedb00
MD
140#endif /* SCM_PKGDATA_DIR */
141
e8e9b690 142#ifdef SCM_LIBRARY_DIR
a1ec6916 143SCM_DEFINE (scm_sys_library_dir, "%library-dir", 0,0,0,
e8e9b690 144 (),
b380b885
MD
145 "Return the directory where the Guile Scheme library files are installed.\n"
146 "E.g., may return \"/usr/share/guile/1.3.5\".")
e8e9b690
GB
147#define FUNC_NAME s_scm_sys_library_dir
148{
149 return scm_makfrom0str(SCM_LIBRARY_DIR);
150}
151#undef FUNC_NAME
152#endif /* SCM_LIBRARY_DIR */
153
154#ifdef SCM_SITE_DIR
a1ec6916 155SCM_DEFINE (scm_sys_site_dir, "%site-dir", 0,0,0,
e8e9b690 156 (),
b380b885
MD
157 "Return the directory where the Guile site files are installed.\n"
158 "E.g., may return \"/usr/share/guile/site\".")
e8e9b690
GB
159#define FUNC_NAME s_scm_sys_site_dir
160{
161 return scm_makfrom0str(SCM_SITE_DIR);
162}
163#undef FUNC_NAME
164#endif /* SCM_SITE_DIR */
165
166
167
3feedb00 168\f
06721500 169/* Initializing the load path, and searching it. */
0f2d19dd 170
26544b96 171/* List of names of directories we search for files to load. */
06721500
JB
172static SCM *scm_loc_load_path;
173
26544b96
JB
174/* List of extensions we try adding to the filenames. */
175static SCM *scm_loc_load_extensions;
176
01cddfc1 177
a1ec6916 178SCM_DEFINE (scm_parse_path, "parse-path", 1, 1, 0,
1bbd0b84 179 (SCM path, SCM tail),
d91788cb
MG
180 "Parse @var{path}, which is expected to be a colon-separated\n"
181 "string, into a list and return the resulting list with\n"
182 "@var{tail} appended. If @var{path} is @code{#f}, @var{tail}\n"
183 "is returned.")
1bbd0b84 184#define FUNC_NAME s_scm_parse_path
04e8fb0a 185{
7d04d68b
MV
186#ifdef __MINGW32__
187 SCM sep = SCM_MAKE_CHAR (';');
188#else
189 SCM sep = SCM_MAKE_CHAR (':');
190#endif
191
04e8fb0a
MD
192 if (SCM_UNBNDP (tail))
193 tail = SCM_EOL;
7888309b 194 return (scm_is_false (path)
04e8fb0a 195 ? tail
7d04d68b 196 : scm_append_x (scm_list_2 (scm_string_split (path, sep), tail)));
04e8fb0a 197}
1bbd0b84 198#undef FUNC_NAME
04e8fb0a
MD
199
200
06721500 201/* Initialize the global variable %load-path, given the value of the
3feedb00 202 SCM_SITE_DIR and SCM_LIBRARY_DIR preprocessor symbols and the
01cddfc1 203 GUILE_LOAD_PATH environment variable. */
0f2d19dd 204void
06721500
JB
205scm_init_load_path ()
206{
11c5e0bf 207 char *env;
06721500
JB
208 SCM path = SCM_EOL;
209
3feedb00 210#ifdef SCM_LIBRARY_DIR
1afff620
KN
211 path = scm_list_3 (scm_makfrom0str (SCM_SITE_DIR),
212 scm_makfrom0str (SCM_LIBRARY_DIR),
213 scm_makfrom0str (SCM_PKGDATA_DIR));
3feedb00 214#endif /* SCM_LIBRARY_DIR */
06721500 215
11c5e0bf
MV
216 env = getenv ("GUILE_LOAD_PATH");
217 if (env)
218 path = scm_parse_path (scm_from_locale_string (env), path);
01cddfc1 219
06721500
JB
220 *scm_loc_load_path = path;
221}
222
04e8fb0a 223SCM scm_listofnullstr;
06721500 224
7d04d68b
MV
225/* Utility functions for assembling C strings in a buffer.
226 */
227
228struct stringbuf {
229 char *buf, *ptr;
230 size_t buf_len;
231};
232
233static void
234stringbuf_free (void *data)
235{
236 struct stringbuf *buf = (struct stringbuf *)data;
237 free (buf->buf);
238}
239
240static void
241stringbuf_grow (struct stringbuf *buf)
242{
243 size_t ptroff = buf->ptr - buf->buf;
244 buf->buf_len *= 2;
7d04d68b
MV
245 buf->buf = scm_realloc (buf->buf, buf->buf_len);
246 buf->ptr = buf->buf + ptroff;
247}
248
249static void
250stringbuf_cat_locale_string (struct stringbuf *buf, SCM str)
251{
252 size_t max_len = buf->buf_len - (buf->ptr - buf->buf) - 1;
253 size_t len = scm_to_locale_stringbuf (str, buf->ptr, max_len);
254 if (len > max_len)
255 {
256 /* buffer is too small, double its size and try again.
257 */
258 stringbuf_grow (buf);
259 stringbuf_cat_locale_string (buf, str);
260 }
261 else
262 {
263 /* string fits, terminate it and check for embedded '\0'.
264 */
265 buf->ptr[len] = '\0';
266 if (strlen (buf->ptr) != len)
267 scm_misc_error (NULL,
268 "string contains #\\nul character: ~S",
269 scm_list_1 (str));
270 buf->ptr += len;
271 }
272}
273
274static void
275stringbuf_cat (struct stringbuf *buf, char *str)
276{
277 size_t max_len = buf->buf_len - (buf->ptr - buf->buf) - 1;
278 size_t len = strlen (str);
279 if (len > max_len)
280 {
281 /* buffer is too small, double its size and try again.
282 */
283 stringbuf_grow (buf);
284 stringbuf_cat (buf, str);
285 }
286 else
287 {
288 /* string fits, copy it into buffer.
289 */
290 strcpy (buf->ptr, str);
291 buf->ptr += len;
292 }
293}
294
295
04e8fb0a 296/* Search PATH for a directory containing a file named FILENAME.
06721500 297 The file must be readable, and not a directory.
26544b96 298 If we find one, return its full filename; otherwise, return #f.
56384176
JB
299 If FILENAME is absolute, return it unchanged.
300 If given, EXTENSIONS is a list of strings; for each directory
301 in PATH, we search for FILENAME concatenated with each EXTENSION. */
3b3b36dd 302SCM_DEFINE (scm_search_path, "search-path", 2, 1, 0,
1bbd0b84 303 (SCM path, SCM filename, SCM extensions),
d91788cb
MG
304 "Search @var{path} for a directory containing a file named\n"
305 "@var{filename}. The file must be readable, and not a directory.\n"
306 "If we find one, return its full filename; otherwise, return\n"
307 "@code{#f}. If @var{filename} is absolute, return it unchanged.\n"
308 "If given, @var{extensions} is a list of strings; for each\n"
309 "directory in @var{path}, we search for @var{filename}\n"
310 "concatenated with each @var{extension}.")
1bbd0b84 311#define FUNC_NAME s_scm_search_path
06721500 312{
7d04d68b 313 struct stringbuf buf;
56384176 314 char *filename_chars;
7d04d68b
MV
315 size_t filename_len;
316 SCM result = SCM_BOOL_F;
06721500 317
04e8fb0a 318 if (SCM_UNBNDP (extensions))
56384176 319 extensions = SCM_EOL;
56384176 320
7d04d68b
MV
321 scm_frame_begin (0);
322
323 filename_chars = scm_to_locale_string (filename);
324 filename_len = strlen (filename_chars);
325 scm_frame_free (filename_chars);
06721500 326
26544b96 327 /* If FILENAME is absolute, return it unchanged. */
2e945bcc
SJ
328#ifdef __MINGW32__
329 if (((filename_len >= 1) &&
330 (filename_chars[0] == '/' || filename_chars[0] == '\\')) ||
331 ((filename_len >= 3) && filename_chars[1] == ':' &&
332 ((filename_chars[0] >= 'a' && filename_chars[0] <= 'z') ||
333 (filename_chars[0] >= 'A' && filename_chars[0] <= 'Z')) &&
334 (filename_chars[2] == '/' || filename_chars[2] == '\\')))
335#else
56384176 336 if (filename_len >= 1 && filename_chars[0] == '/')
2e945bcc 337#endif
7d04d68b
MV
338 {
339 scm_frame_end ();
340 return filename;
341 }
26544b96 342
56384176
JB
343 /* If FILENAME has an extension, don't try to add EXTENSIONS to it. */
344 {
345 char *endp;
346
347 for (endp = filename_chars + filename_len - 1;
348 endp >= filename_chars;
349 endp--)
350 {
351 if (*endp == '.')
352 {
353 /* This filename already has an extension, so cancel the
354 list of extensions. */
355 extensions = SCM_EOL;
356 break;
357 }
2e945bcc
SJ
358#ifdef __MINGW32__
359 else if (*endp == '/' || *endp == '\\')
360#else
56384176 361 else if (*endp == '/')
2e945bcc 362#endif
56384176
JB
363 /* This filename has no extension, so keep the current list
364 of extensions. */
365 break;
366 }
367 }
368
7d04d68b
MV
369 /* This simplifies the loop below a bit.
370 */
371 if (SCM_NULLP (extensions))
372 extensions = scm_listofnullstr;
06721500 373
7d04d68b
MV
374 buf.buf_len = 512;
375 buf.buf = scm_malloc (buf.buf_len);
376 scm_frame_unwind_handler (stringbuf_free, &buf, SCM_F_WIND_EXPLICITLY);
0a74e31d 377
7d04d68b
MV
378 /* Try every path element.
379 */
380 for (; SCM_CONSP (path); path = SCM_CDR (path))
381 {
382 SCM dir = SCM_CAR (path);
383 SCM exts;
384 size_t sans_ext_len;
385
386 buf.ptr = buf.buf;
387 stringbuf_cat_locale_string (&buf, dir);
388
389 /* Concatenate the path name and the filename. */
390
2e945bcc 391#ifdef __MINGW32__
7d04d68b 392 if (buf.ptr > buf.buf && buf.ptr[-1] != '/' && buf.ptr[-1] != '\\')
2e945bcc 393#else
7d04d68b 394 if (buf.ptr > buf.buf >= 1 && buf.ptr[-1] != '/')
2e945bcc 395#endif
7d04d68b
MV
396 stringbuf_cat (&buf, "/");
397
398 stringbuf_cat (&buf, filename_chars);
399 sans_ext_len = buf.ptr - buf.buf;
400
401 /* Try every extension. */
402 for (exts = extensions; SCM_CONSP (exts); exts = SCM_CDR (exts))
403 {
404 SCM ext = SCM_CAR (exts);
405 struct stat mode;
406
407 buf.ptr = buf.buf + sans_ext_len;
408 stringbuf_cat_locale_string (&buf, ext);
409
410 /* If the file exists at all, we should return it. If the
411 file is inaccessible, then that's an error. */
412
7d04d68b
MV
413 if (stat (buf.buf, &mode) == 0
414 && ! (mode.st_mode & S_IFDIR))
415 {
416 result = scm_from_locale_string (buf.buf);
417 goto end;
418 }
419 }
420
421 if (!SCM_NULL_OR_NIL_P (exts))
422 scm_wrong_type_arg_msg (NULL, 0, extensions, "proper list");
423 }
56384176 424
7d04d68b
MV
425 if (!SCM_NULL_OR_NIL_P (path))
426 scm_wrong_type_arg_msg (NULL, 0, path, "proper list");
56384176 427
7d04d68b
MV
428 end:
429 scm_frame_end ();
430 return result;
06721500 431}
1bbd0b84 432#undef FUNC_NAME
06721500
JB
433
434
04e8fb0a
MD
435/* Search %load-path for a directory containing a file named FILENAME.
436 The file must be readable, and not a directory.
437 If we find one, return its full filename; otherwise, return #f.
438 If FILENAME is absolute, return it unchanged. */
3b3b36dd 439SCM_DEFINE (scm_sys_search_load_path, "%search-load-path", 1, 0, 0,
67e8151b
MG
440 (SCM filename),
441 "Search @var{%load-path} for the file named @var{filename},\n"
442 "which must be readable by the current user. If @var{filename}\n"
443 "is found in the list of paths to search or is an absolute\n"
444 "pathname, return its full pathname. Otherwise, return\n"
445 "@code{#f}. Filenames may have any of the optional extensions\n"
446 "in the @code{%load-extensions} list; @code{%search-load-path}\n"
447 "will try each extension automatically.")
1bbd0b84 448#define FUNC_NAME s_scm_sys_search_load_path
04e8fb0a
MD
449{
450 SCM path = *scm_loc_load_path;
451 SCM exts = *scm_loc_load_extensions;
a6d9e5ab 452 SCM_VALIDATE_STRING (1, filename);
1bbd0b84 453
2ade72d7
DH
454 if (scm_ilength (path) < 0)
455 SCM_MISC_ERROR ("%load-path is not a proper list", SCM_EOL);
456 if (scm_ilength (exts) < 0)
457 SCM_MISC_ERROR ("%load-extension list is not a proper list", SCM_EOL);
1bbd0b84 458 return scm_search_path (path, filename, exts);
04e8fb0a 459}
1bbd0b84 460#undef FUNC_NAME
04e8fb0a
MD
461
462
3b3b36dd 463SCM_DEFINE (scm_primitive_load_path, "primitive-load-path", 1, 0, 0,
67e8151b
MG
464 (SCM filename),
465 "Search @var{%load-path} for the file named @var{filename} and\n"
466 "load it into the top-level environment. If @var{filename} is a\n"
467 "relative pathname and is not found in the list of search paths,\n"
468 "an error is signalled.")
1bbd0b84 469#define FUNC_NAME s_scm_primitive_load_path
06721500 470{
26544b96
JB
471 SCM full_filename;
472
26544b96
JB
473 full_filename = scm_sys_search_load_path (filename);
474
7888309b 475 if (scm_is_false (full_filename))
ffa747a6
MV
476 SCM_MISC_ERROR ("Unable to find file ~S in load path",
477 scm_list_1 (filename));
26544b96 478
deca31e1 479 return scm_primitive_load (full_filename);
06721500 480}
1bbd0b84 481#undef FUNC_NAME
06721500 482
c519b272
MV
483SCM
484scm_c_primitive_load_path (const char *filename)
485{
486 return scm_primitive_load_path (scm_makfrom0str (filename));
487}
488
06721500 489\f
e151bee6 490/* Information about the build environment. */
06721500 491
e151bee6
JB
492/* Initialize the scheme variable %guile-build-info, based on data
493 provided by the Makefile, via libpath.h. */
494static void
495init_build_info ()
496{
497 static struct { char *name; char *value; } info[] = SCM_BUILD_INFO;
86d31dfe 498 SCM *loc = SCM_VARIABLE_LOC (scm_c_define ("%guile-build-info", SCM_EOL));
c014a02e 499 unsigned long i;
e151bee6
JB
500
501 for (i = 0; i < (sizeof (info) / sizeof (info[0])); i++)
38ae064c 502 *loc = scm_acons (scm_str2symbol (info[i].name),
e151bee6
JB
503 scm_makfrom0str (info[i].value),
504 *loc);
505}
506
507
508\f
0f2d19dd
JB
509void
510scm_init_load ()
0f2d19dd 511{
1afff620 512 scm_listofnullstr = scm_permanent_object (scm_list_1 (scm_nullstr));
86d31dfe 513 scm_loc_load_path = SCM_VARIABLE_LOC (scm_c_define ("%load-path", SCM_EOL));
26544b96 514 scm_loc_load_extensions
86d31dfe 515 = SCM_VARIABLE_LOC (scm_c_define ("%load-extensions",
1afff620
KN
516 scm_list_2 (scm_makfrom0str (".scm"),
517 scm_nullstr)));
86d31dfe 518 scm_loc_load_hook = SCM_VARIABLE_LOC (scm_c_define ("%load-hook", SCM_BOOL_F));
06721500 519
e151bee6
JB
520 init_build_info ();
521
a0599745 522#include "libguile/load.x"
0f2d19dd 523}
89e00824
ML
524
525/*
526 Local Variables:
527 c-file-style: "gnu"
528 End:
529*/