* load.h (scm_internal_parse_path, scm_parse_path,
[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"
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
d7834b91
MD
73void
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
151scm_parse_path (char *path, SCM tail)
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
06721500 174/* Initialize the global variable %load-path, given the value of the
3feedb00 175 SCM_SITE_DIR and SCM_LIBRARY_DIR preprocessor symbols and the
01cddfc1 176 GUILE_LOAD_PATH environment variable. */
0f2d19dd 177void
06721500
JB
178scm_init_load_path ()
179{
180 SCM path = SCM_EOL;
181
3feedb00 182#ifdef SCM_LIBRARY_DIR
55407879
TP
183 path = scm_listify (scm_makfrom0str (SCM_SITE_DIR),
184 scm_makfrom0str (SCM_LIBRARY_DIR),
185 scm_makfrom0str (SCM_PKGDATA_DIR),
186 SCM_UNDEFINED);
3feedb00 187#endif /* SCM_LIBRARY_DIR */
06721500 188
01cddfc1
JB
189 /* For compatibility, we still check this, but give a warning. */
190 {
191 char *p = getenv ("SCHEME_LOAD_PATH");
192 if (p && p[0] != '\0')
06721500 193 {
01cddfc1
JB
194 fprintf (stderr, "guile: warning: SCHEME_LOAD_PATH variable will be"
195 " removed by Guile 1.4;\n"
196 " use GUILE_LOAD_PATH instead\n");
197 path = scm_parse_path (p, path);
06721500
JB
198 }
199 }
200
01cddfc1
JB
201 path = scm_parse_path (getenv ("GUILE_LOAD_PATH"), path);
202
06721500
JB
203 *scm_loc_load_path = path;
204}
205
206
207/* Search %load-path for a directory containing a file named FILENAME.
208 The file must be readable, and not a directory.
26544b96
JB
209 If we find one, return its full filename; otherwise, return #f.
210 If FILENAME is absolute, return it unchanged. */
06721500
JB
211SCM_PROC(s_sys_search_load_path, "%search-load-path", 1, 0, 0, scm_sys_search_load_path);
212SCM
213scm_sys_search_load_path (filename)
214 SCM filename;
215{
216 SCM path = *scm_loc_load_path;
26544b96 217 SCM exts = *scm_loc_load_extensions;
06721500 218 char *buf;
06721500 219 int filename_len;
26544b96
JB
220 int max_path_len;
221 int max_ext_len;
06721500
JB
222
223 SCM_ASSERT (SCM_NIMP (filename) && SCM_ROSTRINGP (filename), filename,
224 SCM_ARG1, s_sys_search_load_path);
26544b96
JB
225 SCM_ASSERT (scm_ilength (path) >= 0, path, "load path is not a proper list",
226 s_sys_search_load_path);
227 SCM_ASSERT (scm_ilength (exts) >= 0, exts,
228 "load extension list is not a proper list",
229 s_sys_search_load_path);
06721500
JB
230 filename_len = SCM_ROLENGTH (filename);
231
26544b96
JB
232 /* If FILENAME is absolute, return it unchanged. */
233 if (filename_len >= 1
234 && SCM_ROCHARS (filename)[0] == '/')
235 return filename;
236
237 /* Find the length of the longest element of path. */
238 {
239 SCM walk;
240
241 max_path_len = 0;
242 for (walk = path; SCM_NIMP (walk); walk = SCM_CDR (walk))
243 {
244 SCM elt = SCM_CAR (walk);
245 SCM_ASSERT (SCM_NIMP (elt) && SCM_ROSTRINGP (elt), elt,
246 "load path is not a list of strings",
247 s_sys_search_load_path);
248 if (SCM_LENGTH (elt) > max_path_len)
249 max_path_len = SCM_LENGTH (elt);
250 }
251 }
252
253 /* Find the length of the longest element of the load extensions
254 list. */
255 {
256 SCM walk;
257
258 max_ext_len = 0;
259 for (walk = exts; SCM_NIMP (walk); walk = SCM_CDR (walk))
260 {
261 SCM elt = SCM_CAR (walk);
262 SCM_ASSERT (SCM_NIMP (elt) && SCM_ROSTRINGP (elt), elt,
263 "load extension list is not a list of strings",
264 s_sys_search_load_path);
265 if (SCM_LENGTH (elt) > max_ext_len)
266 max_ext_len = SCM_LENGTH (elt);
267 }
268 }
269
06721500
JB
270 SCM_DEFER_INTS;
271
26544b96
JB
272 buf = scm_must_malloc (max_path_len + 1 + filename_len + max_ext_len + 1,
273 s_sys_search_load_path);
06721500 274
26544b96
JB
275 /* Try every path element. At this point, we know it's a proper
276 list of strings. */
277 for (; SCM_NIMP (path); path = SCM_CDR (path))
06721500 278 {
26544b96
JB
279 SCM path_elt = SCM_CAR (path);
280
281 /* Try every extension. At this point, we know it's a proper
282 list of strings. */
283 for (exts = *scm_loc_load_extensions;
284 SCM_NIMP (exts);
285 exts = SCM_CDR (exts))
06721500 286 {
26544b96
JB
287 SCM ext_elt = SCM_CAR (exts);
288 int i;
289
290 /* Concatenate the path name, the filename, and the extension. */
291 i = SCM_ROLENGTH (path_elt);
292 memcpy (buf, SCM_ROCHARS (path_elt), i);
b35d06a4
MD
293 if (i >= 1 && buf[i - 1] != '/')
294 buf[i++] = '/';
26544b96
JB
295 memcpy (buf + i, SCM_ROCHARS (filename), filename_len);
296 i += filename_len;
297 memcpy (buf + i, SCM_ROCHARS (ext_elt), SCM_LENGTH (ext_elt));
298 i += SCM_LENGTH (ext_elt);
299 buf[i] = '\0';
06721500
JB
300
301 {
302 struct stat mode;
303
304 if (stat (buf, &mode) >= 0
305 && ! (mode.st_mode & S_IFDIR)
306 && access (buf, R_OK) == 0)
307 {
26544b96 308 SCM result = scm_makfromstr (buf, i, 0);
06721500
JB
309 scm_must_free (buf);
310 SCM_ALLOW_INTS;
311 return result;
312 }
313 }
314 }
06721500
JB
315 }
316
317 scm_must_free (buf);
318 SCM_ALLOW_INTS;
319 return SCM_BOOL_F;
320}
321
322
deca31e1 323SCM_PROC(s_primitive_load_path, "primitive-load-path", 1, 0, 0, scm_primitive_load_path);
06721500 324SCM
deca31e1 325scm_primitive_load_path (filename)
06721500 326 SCM filename;
06721500 327{
26544b96
JB
328 SCM full_filename;
329
330 SCM_ASSERT (SCM_NIMP (filename) && SCM_ROSTRINGP (filename), filename,
331 SCM_ARG1, s_primitive_load_path);
332
333 full_filename = scm_sys_search_load_path (filename);
334
dbece3a2
GH
335 if (SCM_FALSEP (full_filename))
336 {
26544b96
JB
337 int absolute = (SCM_LENGTH (filename) >= 1
338 && SCM_ROCHARS (filename)[0] == '/');
523f5266 339 scm_misc_error (s_primitive_load_path,
26544b96
JB
340 (absolute
341 ? "Unable to load file %S"
342 : "Unable to find file %S in load path"),
343 scm_listify (filename, SCM_UNDEFINED));
dbece3a2 344 }
26544b96 345
deca31e1 346 return scm_primitive_load (full_filename);
06721500
JB
347}
348
b35d06a4
MD
349/* The following function seems trivial - and indeed it is. Its
350 * existence is motivated by its ability to evaluate expressions
351 * without copying them first (as is done in "eval").
352 */
353
354SCM_SYMBOL (scm_end_of_file_key, "end-of-file");
355
deca31e1 356SCM_PROC (s_read_and_eval_x, "read-and-eval!", 0, 1, 0, scm_read_and_eval_x);
b35d06a4
MD
357
358SCM
deca31e1 359scm_read_and_eval_x (port)
b35d06a4 360 SCM port;
b35d06a4 361{
deca31e1 362 SCM form = scm_read (port);
0c32d76c 363 if (SCM_EOF_OBJECT_P (form))
b35d06a4
MD
364 scm_ithrow (scm_end_of_file_key, SCM_EOL, 1);
365 return scm_eval_x (form);
366}
367
06721500 368\f
e151bee6 369/* Information about the build environment. */
06721500 370
e151bee6
JB
371/* Initialize the scheme variable %guile-build-info, based on data
372 provided by the Makefile, via libpath.h. */
373static void
374init_build_info ()
375{
376 static struct { char *name; char *value; } info[] = SCM_BUILD_INFO;
377 SCM *loc = SCM_CDRLOC (scm_sysintern ("%guile-build-info", SCM_EOL));
378 int i;
379
380 for (i = 0; i < (sizeof (info) / sizeof (info[0])); i++)
381 *loc = scm_acons (SCM_CAR (scm_intern0 (info[i].name)),
382 scm_makfrom0str (info[i].value),
383 *loc);
384}
385
386
387\f
0f2d19dd
JB
388void
389scm_init_load ()
0f2d19dd 390{
25d8012c 391 scm_loc_load_path = SCM_CDRLOC(scm_sysintern("%load-path", SCM_EOL));
26544b96
JB
392 scm_loc_load_extensions
393 = SCM_CDRLOC(scm_sysintern("%load-extensions",
394 scm_listify (scm_makfrom0str (""),
395 scm_makfrom0str (".scm"),
396 SCM_UNDEFINED)));
397 scm_loc_load_hook = SCM_CDRLOC(scm_sysintern("%load-hook", SCM_BOOL_F));
06721500 398
e151bee6
JB
399 init_build_info ();
400
0f2d19dd
JB
401#include "load.x"
402}