Call the QuickThreads library libqthreads.a, not libqt.a. The old
[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
06721500 124/* Initialize the global variable %load-path, given the value of the
3feedb00
MD
125 SCM_SITE_DIR and SCM_LIBRARY_DIR preprocessor symbols and the
126 SCHEME_LOAD_PATH environment variable. */
0f2d19dd 127void
06721500
JB
128scm_init_load_path ()
129{
130 SCM path = SCM_EOL;
131
3feedb00 132#ifdef SCM_LIBRARY_DIR
55407879
TP
133 path = scm_listify (scm_makfrom0str (SCM_SITE_DIR),
134 scm_makfrom0str (SCM_LIBRARY_DIR),
135 scm_makfrom0str (SCM_PKGDATA_DIR),
136 SCM_UNDEFINED);
3feedb00 137#endif /* SCM_LIBRARY_DIR */
06721500
JB
138
139 {
140 char *path_string = getenv ("SCHEME_LOAD_PATH");
141
142 if (path_string && path_string[0] != '\0')
143 {
144 char *scan, *elt_end;
145
146 /* Scan backwards from the end of the string, to help
147 construct the list in the right order. */
148 scan = elt_end = path_string + strlen (path_string);
149 do {
150 /* Scan back to the beginning of the current element. */
151 do scan--;
152 while (scan >= path_string && *scan != ':');
153 path = scm_cons (scm_makfromstr (scan + 1, elt_end - (scan + 1), 0),
154 path);
155 elt_end = scan;
156 } while (scan >= path_string);
157 }
158 }
159
160 *scm_loc_load_path = path;
161}
162
163
164/* Search %load-path for a directory containing a file named FILENAME.
165 The file must be readable, and not a directory.
26544b96
JB
166 If we find one, return its full filename; otherwise, return #f.
167 If FILENAME is absolute, return it unchanged. */
06721500
JB
168SCM_PROC(s_sys_search_load_path, "%search-load-path", 1, 0, 0, scm_sys_search_load_path);
169SCM
170scm_sys_search_load_path (filename)
171 SCM filename;
172{
173 SCM path = *scm_loc_load_path;
26544b96 174 SCM exts = *scm_loc_load_extensions;
06721500 175 char *buf;
06721500 176 int filename_len;
26544b96
JB
177 int max_path_len;
178 int max_ext_len;
06721500
JB
179
180 SCM_ASSERT (SCM_NIMP (filename) && SCM_ROSTRINGP (filename), filename,
181 SCM_ARG1, s_sys_search_load_path);
26544b96
JB
182 SCM_ASSERT (scm_ilength (path) >= 0, path, "load path is not a proper list",
183 s_sys_search_load_path);
184 SCM_ASSERT (scm_ilength (exts) >= 0, exts,
185 "load extension list is not a proper list",
186 s_sys_search_load_path);
06721500
JB
187 filename_len = SCM_ROLENGTH (filename);
188
26544b96
JB
189 /* If FILENAME is absolute, return it unchanged. */
190 if (filename_len >= 1
191 && SCM_ROCHARS (filename)[0] == '/')
192 return filename;
193
194 /* Find the length of the longest element of path. */
195 {
196 SCM walk;
197
198 max_path_len = 0;
199 for (walk = path; SCM_NIMP (walk); walk = SCM_CDR (walk))
200 {
201 SCM elt = SCM_CAR (walk);
202 SCM_ASSERT (SCM_NIMP (elt) && SCM_ROSTRINGP (elt), elt,
203 "load path is not a list of strings",
204 s_sys_search_load_path);
205 if (SCM_LENGTH (elt) > max_path_len)
206 max_path_len = SCM_LENGTH (elt);
207 }
208 }
209
210 /* Find the length of the longest element of the load extensions
211 list. */
212 {
213 SCM walk;
214
215 max_ext_len = 0;
216 for (walk = exts; SCM_NIMP (walk); walk = SCM_CDR (walk))
217 {
218 SCM elt = SCM_CAR (walk);
219 SCM_ASSERT (SCM_NIMP (elt) && SCM_ROSTRINGP (elt), elt,
220 "load extension list is not a list of strings",
221 s_sys_search_load_path);
222 if (SCM_LENGTH (elt) > max_ext_len)
223 max_ext_len = SCM_LENGTH (elt);
224 }
225 }
226
06721500
JB
227 SCM_DEFER_INTS;
228
26544b96
JB
229 buf = scm_must_malloc (max_path_len + 1 + filename_len + max_ext_len + 1,
230 s_sys_search_load_path);
06721500 231
26544b96
JB
232 /* Try every path element. At this point, we know it's a proper
233 list of strings. */
234 for (; SCM_NIMP (path); path = SCM_CDR (path))
06721500 235 {
26544b96
JB
236 SCM path_elt = SCM_CAR (path);
237
238 /* Try every extension. At this point, we know it's a proper
239 list of strings. */
240 for (exts = *scm_loc_load_extensions;
241 SCM_NIMP (exts);
242 exts = SCM_CDR (exts))
06721500 243 {
26544b96
JB
244 SCM ext_elt = SCM_CAR (exts);
245 int i;
246
247 /* Concatenate the path name, the filename, and the extension. */
248 i = SCM_ROLENGTH (path_elt);
249 memcpy (buf, SCM_ROCHARS (path_elt), i);
b35d06a4
MD
250 if (i >= 1 && buf[i - 1] != '/')
251 buf[i++] = '/';
26544b96
JB
252 memcpy (buf + i, SCM_ROCHARS (filename), filename_len);
253 i += filename_len;
254 memcpy (buf + i, SCM_ROCHARS (ext_elt), SCM_LENGTH (ext_elt));
255 i += SCM_LENGTH (ext_elt);
256 buf[i] = '\0';
06721500
JB
257
258 {
259 struct stat mode;
260
261 if (stat (buf, &mode) >= 0
262 && ! (mode.st_mode & S_IFDIR)
263 && access (buf, R_OK) == 0)
264 {
26544b96 265 SCM result = scm_makfromstr (buf, i, 0);
06721500
JB
266 scm_must_free (buf);
267 SCM_ALLOW_INTS;
268 return result;
269 }
270 }
271 }
06721500
JB
272 }
273
274 scm_must_free (buf);
275 SCM_ALLOW_INTS;
276 return SCM_BOOL_F;
277}
278
279
deca31e1 280SCM_PROC(s_primitive_load_path, "primitive-load-path", 1, 0, 0, scm_primitive_load_path);
06721500 281SCM
deca31e1 282scm_primitive_load_path (filename)
06721500 283 SCM filename;
06721500 284{
26544b96
JB
285 SCM full_filename;
286
287 SCM_ASSERT (SCM_NIMP (filename) && SCM_ROSTRINGP (filename), filename,
288 SCM_ARG1, s_primitive_load_path);
289
290 full_filename = scm_sys_search_load_path (filename);
291
dbece3a2
GH
292 if (SCM_FALSEP (full_filename))
293 {
26544b96
JB
294 int absolute = (SCM_LENGTH (filename) >= 1
295 && SCM_ROCHARS (filename)[0] == '/');
523f5266 296 scm_misc_error (s_primitive_load_path,
26544b96
JB
297 (absolute
298 ? "Unable to load file %S"
299 : "Unable to find file %S in load path"),
300 scm_listify (filename, SCM_UNDEFINED));
dbece3a2 301 }
26544b96 302
deca31e1 303 return scm_primitive_load (full_filename);
06721500
JB
304}
305
b35d06a4
MD
306/* The following function seems trivial - and indeed it is. Its
307 * existence is motivated by its ability to evaluate expressions
308 * without copying them first (as is done in "eval").
309 */
310
311SCM_SYMBOL (scm_end_of_file_key, "end-of-file");
312
deca31e1 313SCM_PROC (s_read_and_eval_x, "read-and-eval!", 0, 1, 0, scm_read_and_eval_x);
b35d06a4
MD
314
315SCM
deca31e1 316scm_read_and_eval_x (port)
b35d06a4 317 SCM port;
b35d06a4 318{
deca31e1 319 SCM form = scm_read (port);
0c32d76c 320 if (SCM_EOF_OBJECT_P (form))
b35d06a4
MD
321 scm_ithrow (scm_end_of_file_key, SCM_EOL, 1);
322 return scm_eval_x (form);
323}
324
06721500 325\f
e151bee6 326/* Information about the build environment. */
06721500 327
e151bee6
JB
328/* Initialize the scheme variable %guile-build-info, based on data
329 provided by the Makefile, via libpath.h. */
330static void
331init_build_info ()
332{
333 static struct { char *name; char *value; } info[] = SCM_BUILD_INFO;
334 SCM *loc = SCM_CDRLOC (scm_sysintern ("%guile-build-info", SCM_EOL));
335 int i;
336
337 for (i = 0; i < (sizeof (info) / sizeof (info[0])); i++)
338 *loc = scm_acons (SCM_CAR (scm_intern0 (info[i].name)),
339 scm_makfrom0str (info[i].value),
340 *loc);
341}
342
343
344\f
0f2d19dd
JB
345void
346scm_init_load ()
0f2d19dd 347{
25d8012c 348 scm_loc_load_path = SCM_CDRLOC(scm_sysintern("%load-path", SCM_EOL));
26544b96
JB
349 scm_loc_load_extensions
350 = SCM_CDRLOC(scm_sysintern("%load-extensions",
351 scm_listify (scm_makfrom0str (""),
352 scm_makfrom0str (".scm"),
353 SCM_UNDEFINED)));
354 scm_loc_load_hook = SCM_CDRLOC(scm_sysintern("%load-hook", SCM_BOOL_F));
06721500 355
e151bee6
JB
356 init_build_info ();
357
0f2d19dd
JB
358#include "load.x"
359}