*** empty log message ***
[bpt/guile.git] / libguile / load.c
CommitLineData
7dc6e754 1/* Copyright (C) 1995,1996,1998 Free Software Foundation, Inc.
0f2d19dd
JB
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
82892bed
JB
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
0f2d19dd
JB
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.
82892bed 40 * If you do not wish that, delete this exception notice. */
0f2d19dd
JB
41\f
42
43#include <stdio.h>
44#include "_scm.h"
06721500 45#include "libpath.h"
20e6290e
JB
46#include "fports.h"
47#include "read.h"
48#include "eval.h"
b35d06a4 49#include "throw.h"
e151bee6 50#include "alist.h"
d7834b91 51#include "dynwind.h"
20e6290e
JB
52
53#include "load.h"
06721500
JB
54
55#include <sys/types.h>
56#include <sys/stat.h>
57
58#ifdef HAVE_UNISTD_H
59#include <unistd.h>
60#endif /* HAVE_UNISTD_H */
61
62#ifndef R_OK
63#define R_OK 4
64#endif
0f2d19dd
JB
65
66\f
06721500 67/* Loading a file, given an absolute filename. */
0f2d19dd 68
26544b96
JB
69/* Hook to run when we load a file, perhaps to announce the fact somewhere.
70 Applied to the full name of the file. */
71static SCM *scm_loc_load_hook;
72
c7023c69 73static void
d7834b91
MD
74swap_port (void *data)
75{
76 SCM *save_port = data, tmp = scm_cur_loadp;
77 scm_cur_loadp = *save_port;
78 *save_port = tmp;
79}
80
81static SCM
82load (void *data)
83{
84 SCM port = (SCM) data, form;
85 while (1)
86 {
87 form = scm_read (port);
88 if (SCM_EOF_OBJECT_P (form))
89 break;
90 scm_eval_x (form);
91 }
92 return SCM_UNSPECIFIED;
93}
94
deca31e1 95SCM_PROC(s_primitive_load, "primitive-load", 1, 0, 0, scm_primitive_load);
0f2d19dd 96SCM
deca31e1 97scm_primitive_load (filename)
0f2d19dd 98 SCM filename;
0f2d19dd 99{
26544b96 100 SCM hook = *scm_loc_load_hook;
06721500 101 SCM_ASSERT (SCM_NIMP (filename) && SCM_ROSTRINGP (filename), filename,
523f5266 102 SCM_ARG1, s_primitive_load);
26544b96
JB
103 SCM_ASSERT (hook == SCM_BOOL_F
104 || (scm_procedure_p (hook) == SCM_BOOL_T),
105 hook, "value of %load-hook is neither a procedure nor #f",
106 s_primitive_load);
107
108 if (hook != SCM_BOOL_F)
109 scm_apply (hook, scm_listify (filename, SCM_UNDEFINED), SCM_EOL);
110
0f2d19dd 111 {
d7834b91 112 SCM port, save_port;
0f2d19dd
JB
113 port = scm_open_file (filename,
114 scm_makfromstr ("r", (scm_sizet) sizeof (char), 0));
d7834b91
MD
115 save_port = port;
116 scm_internal_dynamic_wind (swap_port,
117 load,
118 swap_port,
119 (void *) port,
120 &save_port);
0f2d19dd
JB
121 scm_close_port (port);
122 }
b59b97ba 123 return SCM_UNSPECIFIED;
0f2d19dd
JB
124}
125
126\f
3feedb00
MD
127/* Builtin path to scheme library files. */
128#ifdef SCM_PKGDATA_DIR
129SCM_PROC (s_sys_package_data_dir, "%package-data-dir", 0, 0, 0, scm_sys_package_data_dir);
130SCM
131scm_sys_package_data_dir ()
132{
133 return scm_makfrom0str (SCM_PKGDATA_DIR);
134}
135#endif /* SCM_PKGDATA_DIR */
136
137\f
06721500 138/* Initializing the load path, and searching it. */
0f2d19dd 139
26544b96 140/* List of names of directories we search for files to load. */
06721500
JB
141static SCM *scm_loc_load_path;
142
26544b96
JB
143/* List of extensions we try adding to the filenames. */
144static SCM *scm_loc_load_extensions;
145
01cddfc1
JB
146
147/* Parse the null-terminated string PATH as if it were a standard path
148 environment variable (i.e. a colon-separated list of strings), and
149 prepend the elements to TAIL. */
150SCM
04e8fb0a 151scm_internal_parse_path (char *path, SCM tail)
01cddfc1
JB
152{
153 if (path && path[0] != '\0')
154 {
155 char *scan, *elt_end;
156
157 /* Scan backwards from the end of the string, to help
158 construct the list in the right order. */
159 scan = elt_end = path + strlen (path);
160 do {
161 /* Scan back to the beginning of the current element. */
162 do scan--;
163 while (scan >= path && *scan != ':');
164 tail = scm_cons (scm_makfromstr (scan + 1, elt_end - (scan + 1), 0),
165 tail);
166 elt_end = scan;
167 } while (scan >= path);
168 }
169
170 return tail;
171}
172
173
04e8fb0a
MD
174SCM_PROC (s_parse_path, "parse-path", 1, 1, 0, scm_parse_path);
175
176SCM
177scm_parse_path (SCM path, SCM tail)
178{
179 SCM_ASSERT (SCM_FALSEP (path) || (SCM_NIMP (path) && SCM_ROSTRINGP (path)),
180 path,
181 SCM_ARG1,
182 s_parse_path);
183 if (SCM_UNBNDP (tail))
184 tail = SCM_EOL;
185 return (SCM_FALSEP (path)
186 ? tail
187 : scm_internal_parse_path (SCM_ROCHARS (path), tail));
188}
189
190
06721500 191/* Initialize the global variable %load-path, given the value of the
3feedb00 192 SCM_SITE_DIR and SCM_LIBRARY_DIR preprocessor symbols and the
01cddfc1 193 GUILE_LOAD_PATH environment variable. */
0f2d19dd 194void
06721500
JB
195scm_init_load_path ()
196{
197 SCM path = SCM_EOL;
198
3feedb00 199#ifdef SCM_LIBRARY_DIR
55407879
TP
200 path = scm_listify (scm_makfrom0str (SCM_SITE_DIR),
201 scm_makfrom0str (SCM_LIBRARY_DIR),
202 scm_makfrom0str (SCM_PKGDATA_DIR),
203 SCM_UNDEFINED);
3feedb00 204#endif /* SCM_LIBRARY_DIR */
06721500 205
04e8fb0a 206 path = scm_internal_parse_path (getenv ("GUILE_LOAD_PATH"), path);
01cddfc1 207
06721500
JB
208 *scm_loc_load_path = path;
209}
210
04e8fb0a 211SCM scm_listofnullstr;
06721500 212
04e8fb0a 213/* Search PATH for a directory containing a file named FILENAME.
06721500 214 The file must be readable, and not a directory.
26544b96
JB
215 If we find one, return its full filename; otherwise, return #f.
216 If FILENAME is absolute, return it unchanged. */
04e8fb0a 217SCM_PROC(s_search_path, "search-path", 2, 1, 0, scm_search_path);
06721500 218SCM
04e8fb0a
MD
219scm_search_path (path, filename, extensions)
220 SCM path;
06721500 221 SCM filename;
04e8fb0a 222 SCM extensions;
06721500 223{
0a74e31d 224 char *buf, *endp;
06721500 225 int filename_len;
c7023c69
JB
226 size_t max_path_len;
227 size_t max_ext_len;
0a74e31d 228 SCM result = SCM_BOOL_F;
06721500 229
04e8fb0a 230 SCM_ASSERT (scm_ilength (path) >= 0, path, SCM_ARG1, s_search_path);
06721500 231 SCM_ASSERT (SCM_NIMP (filename) && SCM_ROSTRINGP (filename), filename,
04e8fb0a
MD
232 SCM_ARG2, s_search_path);
233 if (SCM_UNBNDP (extensions))
234 extensions = scm_listofnullstr;
235 else
236 SCM_ASSERT (scm_ilength (extensions) >= 0, extensions,
237 SCM_ARG3, s_search_path);
06721500
JB
238 filename_len = SCM_ROLENGTH (filename);
239
26544b96
JB
240 /* If FILENAME is absolute, return it unchanged. */
241 if (filename_len >= 1
242 && SCM_ROCHARS (filename)[0] == '/')
243 return filename;
244
245 /* Find the length of the longest element of path. */
246 {
247 SCM walk;
248
249 max_path_len = 0;
250 for (walk = path; SCM_NIMP (walk); walk = SCM_CDR (walk))
251 {
252 SCM elt = SCM_CAR (walk);
253 SCM_ASSERT (SCM_NIMP (elt) && SCM_ROSTRINGP (elt), elt,
04e8fb0a
MD
254 "path is not a list of strings",
255 s_search_path);
0a74e31d
MD
256 if (SCM_ROLENGTH (elt) > max_path_len)
257 max_path_len = SCM_ROLENGTH (elt);
26544b96
JB
258 }
259 }
260
261 /* Find the length of the longest element of the load extensions
262 list. */
263 {
264 SCM walk;
265
266 max_ext_len = 0;
04e8fb0a 267 for (walk = extensions; SCM_NIMP (walk); walk = SCM_CDR (walk))
26544b96
JB
268 {
269 SCM elt = SCM_CAR (walk);
270 SCM_ASSERT (SCM_NIMP (elt) && SCM_ROSTRINGP (elt), elt,
04e8fb0a
MD
271 "extension list is not a list of strings",
272 s_search_path);
0a74e31d
MD
273 if (SCM_ROLENGTH (elt) > max_ext_len)
274 max_ext_len = SCM_ROLENGTH (elt);
26544b96
JB
275 }
276 }
277
0a74e31d
MD
278 /* Remove the same extension that FILENAME already has. */
279 for (endp = SCM_ROCHARS (filename) + filename_len - 1;
280 endp > SCM_ROCHARS (filename) && *endp != '/'; endp--)
281 if (*endp == '.')
282 {
283 extensions = scm_delete (scm_makfromstr (endp, SCM_ROCHARS (filename)
284 + filename_len - endp, 0),
285 extensions);
286 break;
287 }
288
06721500
JB
289 SCM_DEFER_INTS;
290
26544b96 291 buf = scm_must_malloc (max_path_len + 1 + filename_len + max_ext_len + 1,
04e8fb0a 292 s_search_path);
06721500 293
26544b96
JB
294 /* Try every path element. At this point, we know it's a proper
295 list of strings. */
296 for (; SCM_NIMP (path); path = SCM_CDR (path))
06721500 297 {
0a74e31d
MD
298 int len;
299 SCM dir = SCM_CAR (path), exts;
300
301 /* Concatenate the path name and the filename. */
302 len = SCM_ROLENGTH (dir);
303 memcpy (buf, SCM_ROCHARS (dir), len);
304 if (len >= 1 && buf[len - 1] != '/')
305 buf[len++] = '/';
306 memcpy (buf + len, SCM_ROCHARS (filename), filename_len);
307 endp = buf + len + filename_len;
26544b96
JB
308
309 /* Try every extension. At this point, we know it's a proper
310 list of strings. */
04e8fb0a 311 for (exts = extensions; SCM_NIMP (exts); exts = SCM_CDR (exts))
06721500 312 {
0a74e31d
MD
313 SCM ext = SCM_CAR (exts);
314 struct stat mode;
315
316 /* Concatenate the extension. */
317 len = SCM_ROLENGTH (ext);
318 memcpy (endp, SCM_ROCHARS (ext), len);
319 *(endp + len) = '\0';
320
321 if (stat (buf, &mode) == 0
322 && ! (mode.st_mode & S_IFDIR)
323 && access (buf, R_OK) == 0)
324 {
325 result = scm_makfromstr (buf, endp + len - buf, 0);
326 goto end;
327 }
06721500 328 }
06721500 329 }
0a74e31d
MD
330
331 end:
06721500
JB
332 scm_must_free (buf);
333 SCM_ALLOW_INTS;
0a74e31d 334 return result;
06721500
JB
335}
336
337
04e8fb0a
MD
338/* Search %load-path for a directory containing a file named FILENAME.
339 The file must be readable, and not a directory.
340 If we find one, return its full filename; otherwise, return #f.
341 If FILENAME is absolute, return it unchanged. */
342SCM_PROC(s_sys_search_load_path, "%search-load-path", 1, 0, 0, scm_sys_search_load_path);
343SCM
344scm_sys_search_load_path (filename)
345 SCM filename;
346{
347 SCM path = *scm_loc_load_path;
348 SCM exts = *scm_loc_load_extensions;
349 SCM_ASSERT (SCM_NIMP (filename) && SCM_ROSTRINGP (filename), filename,
350 SCM_ARG1, s_sys_search_load_path);
351 SCM_ASSERT (scm_ilength (path) >= 0, path, "load path is not a proper list",
352 s_sys_search_load_path);
353 SCM_ASSERT (scm_ilength (exts) >= 0, exts,
354 "load extension list is not a proper list",
355 s_sys_search_load_path);
356 return scm_search_path (path,
357 filename,
358 exts);
359}
360
361
deca31e1 362SCM_PROC(s_primitive_load_path, "primitive-load-path", 1, 0, 0, scm_primitive_load_path);
06721500 363SCM
deca31e1 364scm_primitive_load_path (filename)
06721500 365 SCM filename;
06721500 366{
26544b96
JB
367 SCM full_filename;
368
369 SCM_ASSERT (SCM_NIMP (filename) && SCM_ROSTRINGP (filename), filename,
370 SCM_ARG1, s_primitive_load_path);
371
372 full_filename = scm_sys_search_load_path (filename);
373
dbece3a2
GH
374 if (SCM_FALSEP (full_filename))
375 {
0a74e31d 376 int absolute = (SCM_ROLENGTH (filename) >= 1
26544b96 377 && SCM_ROCHARS (filename)[0] == '/');
523f5266 378 scm_misc_error (s_primitive_load_path,
26544b96
JB
379 (absolute
380 ? "Unable to load file %S"
381 : "Unable to find file %S in load path"),
382 scm_listify (filename, SCM_UNDEFINED));
dbece3a2 383 }
26544b96 384
deca31e1 385 return scm_primitive_load (full_filename);
06721500
JB
386}
387
b35d06a4
MD
388/* The following function seems trivial - and indeed it is. Its
389 * existence is motivated by its ability to evaluate expressions
390 * without copying them first (as is done in "eval").
391 */
392
393SCM_SYMBOL (scm_end_of_file_key, "end-of-file");
394
deca31e1 395SCM_PROC (s_read_and_eval_x, "read-and-eval!", 0, 1, 0, scm_read_and_eval_x);
b35d06a4
MD
396
397SCM
deca31e1 398scm_read_and_eval_x (port)
b35d06a4 399 SCM port;
b35d06a4 400{
deca31e1 401 SCM form = scm_read (port);
0c32d76c 402 if (SCM_EOF_OBJECT_P (form))
b35d06a4
MD
403 scm_ithrow (scm_end_of_file_key, SCM_EOL, 1);
404 return scm_eval_x (form);
405}
406
06721500 407\f
e151bee6 408/* Information about the build environment. */
06721500 409
e151bee6
JB
410/* Initialize the scheme variable %guile-build-info, based on data
411 provided by the Makefile, via libpath.h. */
412static void
413init_build_info ()
414{
415 static struct { char *name; char *value; } info[] = SCM_BUILD_INFO;
416 SCM *loc = SCM_CDRLOC (scm_sysintern ("%guile-build-info", SCM_EOL));
c7023c69 417 unsigned int i;
e151bee6
JB
418
419 for (i = 0; i < (sizeof (info) / sizeof (info[0])); i++)
420 *loc = scm_acons (SCM_CAR (scm_intern0 (info[i].name)),
421 scm_makfrom0str (info[i].value),
422 *loc);
423}
424
425
426\f
0f2d19dd
JB
427void
428scm_init_load ()
0f2d19dd 429{
04e8fb0a
MD
430 scm_listofnullstr = scm_permanent_object (SCM_LIST1 (scm_nullstr));
431 scm_loc_load_path = SCM_CDRLOC (scm_sysintern ("%load-path", SCM_EOL));
26544b96 432 scm_loc_load_extensions
04e8fb0a 433 = SCM_CDRLOC (scm_sysintern ("%load-extensions",
0a74e31d
MD
434 scm_listify (scm_makfrom0str (".scm"),
435 scm_makfrom0str (""),
04e8fb0a
MD
436 SCM_UNDEFINED)));
437 scm_loc_load_hook = SCM_CDRLOC (scm_sysintern ("%load-hook", SCM_BOOL_F));
06721500 438
e151bee6
JB
439 init_build_info ();
440
0f2d19dd
JB
441#include "load.x"
442}