* init.c (scm_boot_guile_1): Removed condition around
[bpt/guile.git] / libguile / load.c
CommitLineData
0f2d19dd
JB
1/* Copyright (C) 1995,1996 Free Software Foundation, Inc.
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"
20e6290e
JB
51
52#include "load.h"
06721500
JB
53
54#include <sys/types.h>
55#include <sys/stat.h>
56
57#ifdef HAVE_UNISTD_H
58#include <unistd.h>
59#endif /* HAVE_UNISTD_H */
60
61#ifndef R_OK
62#define R_OK 4
63#endif
0f2d19dd
JB
64
65\f
06721500 66/* Loading a file, given an absolute filename. */
0f2d19dd 67
26544b96
JB
68/* Hook to run when we load a file, perhaps to announce the fact somewhere.
69 Applied to the full name of the file. */
70static SCM *scm_loc_load_hook;
71
deca31e1 72SCM_PROC(s_primitive_load, "primitive-load", 1, 0, 0, scm_primitive_load);
0f2d19dd 73SCM
deca31e1 74scm_primitive_load (filename)
0f2d19dd 75 SCM filename;
0f2d19dd 76{
26544b96 77 SCM hook = *scm_loc_load_hook;
06721500 78 SCM_ASSERT (SCM_NIMP (filename) && SCM_ROSTRINGP (filename), filename,
523f5266 79 SCM_ARG1, s_primitive_load);
26544b96
JB
80 SCM_ASSERT (hook == SCM_BOOL_F
81 || (scm_procedure_p (hook) == SCM_BOOL_T),
82 hook, "value of %load-hook is neither a procedure nor #f",
83 s_primitive_load);
84
85 if (hook != SCM_BOOL_F)
86 scm_apply (hook, scm_listify (filename, SCM_UNDEFINED), SCM_EOL);
87
0f2d19dd
JB
88 {
89 SCM form, port;
90 port = scm_open_file (filename,
91 scm_makfromstr ("r", (scm_sizet) sizeof (char), 0));
0f2d19dd
JB
92 while (1)
93 {
deca31e1 94 form = scm_read (port);
0c32d76c 95 if (SCM_EOF_OBJECT_P (form))
0f2d19dd
JB
96 break;
97 scm_eval_x (form);
98 }
99 scm_close_port (port);
100 }
b59b97ba 101 return SCM_UNSPECIFIED;
0f2d19dd
JB
102}
103
104\f
3feedb00
MD
105/* Builtin path to scheme library files. */
106#ifdef SCM_PKGDATA_DIR
107SCM_PROC (s_sys_package_data_dir, "%package-data-dir", 0, 0, 0, scm_sys_package_data_dir);
108SCM
109scm_sys_package_data_dir ()
110{
111 return scm_makfrom0str (SCM_PKGDATA_DIR);
112}
113#endif /* SCM_PKGDATA_DIR */
114
115\f
06721500 116/* Initializing the load path, and searching it. */
0f2d19dd 117
26544b96 118/* List of names of directories we search for files to load. */
06721500
JB
119static SCM *scm_loc_load_path;
120
26544b96
JB
121/* List of extensions we try adding to the filenames. */
122static SCM *scm_loc_load_extensions;
123
01cddfc1
JB
124
125/* Parse the null-terminated string PATH as if it were a standard path
126 environment variable (i.e. a colon-separated list of strings), and
127 prepend the elements to TAIL. */
128SCM
129scm_parse_path (char *path, SCM tail)
130{
131 if (path && path[0] != '\0')
132 {
133 char *scan, *elt_end;
134
135 /* Scan backwards from the end of the string, to help
136 construct the list in the right order. */
137 scan = elt_end = path + strlen (path);
138 do {
139 /* Scan back to the beginning of the current element. */
140 do scan--;
141 while (scan >= path && *scan != ':');
142 tail = scm_cons (scm_makfromstr (scan + 1, elt_end - (scan + 1), 0),
143 tail);
144 elt_end = scan;
145 } while (scan >= path);
146 }
147
148 return tail;
149}
150
151
06721500 152/* Initialize the global variable %load-path, given the value of the
3feedb00 153 SCM_SITE_DIR and SCM_LIBRARY_DIR preprocessor symbols and the
01cddfc1 154 GUILE_LOAD_PATH environment variable. */
0f2d19dd 155void
06721500
JB
156scm_init_load_path ()
157{
158 SCM path = SCM_EOL;
159
3feedb00 160#ifdef SCM_LIBRARY_DIR
55407879
TP
161 path = scm_listify (scm_makfrom0str (SCM_SITE_DIR),
162 scm_makfrom0str (SCM_LIBRARY_DIR),
163 scm_makfrom0str (SCM_PKGDATA_DIR),
164 SCM_UNDEFINED);
3feedb00 165#endif /* SCM_LIBRARY_DIR */
06721500 166
01cddfc1
JB
167 /* For compatibility, we still check this, but give a warning. */
168 {
169 char *p = getenv ("SCHEME_LOAD_PATH");
170 if (p && p[0] != '\0')
06721500 171 {
01cddfc1
JB
172 fprintf (stderr, "guile: warning: SCHEME_LOAD_PATH variable will be"
173 " removed by Guile 1.4;\n"
174 " use GUILE_LOAD_PATH instead\n");
175 path = scm_parse_path (p, path);
06721500
JB
176 }
177 }
178
01cddfc1
JB
179 path = scm_parse_path (getenv ("GUILE_LOAD_PATH"), path);
180
06721500
JB
181 *scm_loc_load_path = path;
182}
183
184
185/* Search %load-path for a directory containing a file named FILENAME.
186 The file must be readable, and not a directory.
26544b96
JB
187 If we find one, return its full filename; otherwise, return #f.
188 If FILENAME is absolute, return it unchanged. */
06721500
JB
189SCM_PROC(s_sys_search_load_path, "%search-load-path", 1, 0, 0, scm_sys_search_load_path);
190SCM
191scm_sys_search_load_path (filename)
192 SCM filename;
193{
194 SCM path = *scm_loc_load_path;
26544b96 195 SCM exts = *scm_loc_load_extensions;
06721500 196 char *buf;
06721500 197 int filename_len;
26544b96
JB
198 int max_path_len;
199 int max_ext_len;
06721500
JB
200
201 SCM_ASSERT (SCM_NIMP (filename) && SCM_ROSTRINGP (filename), filename,
202 SCM_ARG1, s_sys_search_load_path);
26544b96
JB
203 SCM_ASSERT (scm_ilength (path) >= 0, path, "load path is not a proper list",
204 s_sys_search_load_path);
205 SCM_ASSERT (scm_ilength (exts) >= 0, exts,
206 "load extension list is not a proper list",
207 s_sys_search_load_path);
06721500
JB
208 filename_len = SCM_ROLENGTH (filename);
209
26544b96
JB
210 /* If FILENAME is absolute, return it unchanged. */
211 if (filename_len >= 1
212 && SCM_ROCHARS (filename)[0] == '/')
213 return filename;
214
215 /* Find the length of the longest element of path. */
216 {
217 SCM walk;
218
219 max_path_len = 0;
220 for (walk = path; SCM_NIMP (walk); walk = SCM_CDR (walk))
221 {
222 SCM elt = SCM_CAR (walk);
223 SCM_ASSERT (SCM_NIMP (elt) && SCM_ROSTRINGP (elt), elt,
224 "load path is not a list of strings",
225 s_sys_search_load_path);
226 if (SCM_LENGTH (elt) > max_path_len)
227 max_path_len = SCM_LENGTH (elt);
228 }
229 }
230
231 /* Find the length of the longest element of the load extensions
232 list. */
233 {
234 SCM walk;
235
236 max_ext_len = 0;
237 for (walk = exts; SCM_NIMP (walk); walk = SCM_CDR (walk))
238 {
239 SCM elt = SCM_CAR (walk);
240 SCM_ASSERT (SCM_NIMP (elt) && SCM_ROSTRINGP (elt), elt,
241 "load extension list is not a list of strings",
242 s_sys_search_load_path);
243 if (SCM_LENGTH (elt) > max_ext_len)
244 max_ext_len = SCM_LENGTH (elt);
245 }
246 }
247
06721500
JB
248 SCM_DEFER_INTS;
249
26544b96
JB
250 buf = scm_must_malloc (max_path_len + 1 + filename_len + max_ext_len + 1,
251 s_sys_search_load_path);
06721500 252
26544b96
JB
253 /* Try every path element. At this point, we know it's a proper
254 list of strings. */
255 for (; SCM_NIMP (path); path = SCM_CDR (path))
06721500 256 {
26544b96
JB
257 SCM path_elt = SCM_CAR (path);
258
259 /* Try every extension. At this point, we know it's a proper
260 list of strings. */
261 for (exts = *scm_loc_load_extensions;
262 SCM_NIMP (exts);
263 exts = SCM_CDR (exts))
06721500 264 {
26544b96
JB
265 SCM ext_elt = SCM_CAR (exts);
266 int i;
267
268 /* Concatenate the path name, the filename, and the extension. */
269 i = SCM_ROLENGTH (path_elt);
270 memcpy (buf, SCM_ROCHARS (path_elt), i);
b35d06a4
MD
271 if (i >= 1 && buf[i - 1] != '/')
272 buf[i++] = '/';
26544b96
JB
273 memcpy (buf + i, SCM_ROCHARS (filename), filename_len);
274 i += filename_len;
275 memcpy (buf + i, SCM_ROCHARS (ext_elt), SCM_LENGTH (ext_elt));
276 i += SCM_LENGTH (ext_elt);
277 buf[i] = '\0';
06721500
JB
278
279 {
280 struct stat mode;
281
282 if (stat (buf, &mode) >= 0
283 && ! (mode.st_mode & S_IFDIR)
284 && access (buf, R_OK) == 0)
285 {
26544b96 286 SCM result = scm_makfromstr (buf, i, 0);
06721500
JB
287 scm_must_free (buf);
288 SCM_ALLOW_INTS;
289 return result;
290 }
291 }
292 }
06721500
JB
293 }
294
295 scm_must_free (buf);
296 SCM_ALLOW_INTS;
297 return SCM_BOOL_F;
298}
299
300
deca31e1 301SCM_PROC(s_primitive_load_path, "primitive-load-path", 1, 0, 0, scm_primitive_load_path);
06721500 302SCM
deca31e1 303scm_primitive_load_path (filename)
06721500 304 SCM filename;
06721500 305{
26544b96
JB
306 SCM full_filename;
307
308 SCM_ASSERT (SCM_NIMP (filename) && SCM_ROSTRINGP (filename), filename,
309 SCM_ARG1, s_primitive_load_path);
310
311 full_filename = scm_sys_search_load_path (filename);
312
dbece3a2
GH
313 if (SCM_FALSEP (full_filename))
314 {
26544b96
JB
315 int absolute = (SCM_LENGTH (filename) >= 1
316 && SCM_ROCHARS (filename)[0] == '/');
523f5266 317 scm_misc_error (s_primitive_load_path,
26544b96
JB
318 (absolute
319 ? "Unable to load file %S"
320 : "Unable to find file %S in load path"),
321 scm_listify (filename, SCM_UNDEFINED));
dbece3a2 322 }
26544b96 323
deca31e1 324 return scm_primitive_load (full_filename);
06721500
JB
325}
326
b35d06a4
MD
327/* The following function seems trivial - and indeed it is. Its
328 * existence is motivated by its ability to evaluate expressions
329 * without copying them first (as is done in "eval").
330 */
331
332SCM_SYMBOL (scm_end_of_file_key, "end-of-file");
333
deca31e1 334SCM_PROC (s_read_and_eval_x, "read-and-eval!", 0, 1, 0, scm_read_and_eval_x);
b35d06a4
MD
335
336SCM
deca31e1 337scm_read_and_eval_x (port)
b35d06a4 338 SCM port;
b35d06a4 339{
deca31e1 340 SCM form = scm_read (port);
0c32d76c 341 if (SCM_EOF_OBJECT_P (form))
b35d06a4
MD
342 scm_ithrow (scm_end_of_file_key, SCM_EOL, 1);
343 return scm_eval_x (form);
344}
345
06721500 346\f
e151bee6 347/* Information about the build environment. */
06721500 348
e151bee6
JB
349/* Initialize the scheme variable %guile-build-info, based on data
350 provided by the Makefile, via libpath.h. */
351static void
352init_build_info ()
353{
354 static struct { char *name; char *value; } info[] = SCM_BUILD_INFO;
355 SCM *loc = SCM_CDRLOC (scm_sysintern ("%guile-build-info", SCM_EOL));
356 int i;
357
358 for (i = 0; i < (sizeof (info) / sizeof (info[0])); i++)
359 *loc = scm_acons (SCM_CAR (scm_intern0 (info[i].name)),
360 scm_makfrom0str (info[i].value),
361 *loc);
362}
363
364
365\f
0f2d19dd
JB
366void
367scm_init_load ()
0f2d19dd 368{
25d8012c 369 scm_loc_load_path = SCM_CDRLOC(scm_sysintern("%load-path", SCM_EOL));
26544b96
JB
370 scm_loc_load_extensions
371 = SCM_CDRLOC(scm_sysintern("%load-extensions",
372 scm_listify (scm_makfrom0str (""),
373 scm_makfrom0str (".scm"),
374 SCM_UNDEFINED)));
375 scm_loc_load_hook = SCM_CDRLOC(scm_sysintern("%load-hook", SCM_BOOL_F));
06721500 376
e151bee6
JB
377 init_build_info ();
378
0f2d19dd
JB
379#include "load.x"
380}