1 /* Copyright (C) 1995,1996,1998 Free Software Foundation, Inc.
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)
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.
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
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
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.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
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.
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.
40 * If you do not wish that, delete this exception notice. */
55 #include <sys/types.h>
60 #endif /* HAVE_UNISTD_H */
67 /* Loading a file, given an absolute filename. */
69 /* Hook to run when we load a file, perhaps to announce the fact somewhere.
70 Applied to the full name of the file. */
71 static SCM
*scm_loc_load_hook
;
74 swap_port (void *data
)
76 SCM
*save_port
= data
, tmp
= scm_cur_loadp
;
77 scm_cur_loadp
= *save_port
;
84 SCM port
= (SCM
) data
, form
;
87 form
= scm_read (port
);
88 if (SCM_EOF_OBJECT_P (form
))
92 return SCM_UNSPECIFIED
;
95 SCM_PROC(s_primitive_load
, "primitive-load", 1, 0, 0, scm_primitive_load
);
97 scm_primitive_load (filename
)
100 SCM hook
= *scm_loc_load_hook
;
101 SCM_ASSERT (SCM_NIMP (filename
) && SCM_ROSTRINGP (filename
), filename
,
102 SCM_ARG1
, s_primitive_load
);
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",
108 if (hook
!= SCM_BOOL_F
)
109 scm_apply (hook
, scm_listify (filename
, SCM_UNDEFINED
), SCM_EOL
);
113 port
= scm_open_file (filename
,
114 scm_makfromstr ("r", (scm_sizet
) sizeof (char), 0));
116 scm_internal_dynamic_wind (swap_port
,
121 scm_close_port (port
);
123 return SCM_UNSPECIFIED
;
127 /* Builtin path to scheme library files. */
128 #ifdef SCM_PKGDATA_DIR
129 SCM_PROC (s_sys_package_data_dir
, "%package-data-dir", 0, 0, 0, scm_sys_package_data_dir
);
131 scm_sys_package_data_dir ()
133 return scm_makfrom0str (SCM_PKGDATA_DIR
);
135 #endif /* SCM_PKGDATA_DIR */
138 /* Initializing the load path, and searching it. */
140 /* List of names of directories we search for files to load. */
141 static SCM
*scm_loc_load_path
;
143 /* List of extensions we try adding to the filenames. */
144 static SCM
*scm_loc_load_extensions
;
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. */
151 scm_internal_parse_path (char *path
, SCM tail
)
153 if (path
&& path
[0] != '\0')
155 char *scan
, *elt_end
;
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
);
161 /* Scan back to the beginning of the current element. */
163 while (scan
>= path
&& *scan
!= ':');
164 tail
= scm_cons (scm_makfromstr (scan
+ 1, elt_end
- (scan
+ 1), 0),
167 } while (scan
>= path
);
174 SCM_PROC (s_parse_path
, "parse-path", 1, 1, 0, scm_parse_path
);
177 scm_parse_path (SCM path
, SCM tail
)
179 SCM_ASSERT (SCM_FALSEP (path
) || (SCM_NIMP (path
) && SCM_ROSTRINGP (path
)),
183 if (SCM_UNBNDP (tail
))
185 return (SCM_FALSEP (path
)
187 : scm_internal_parse_path (SCM_ROCHARS (path
), tail
));
191 /* Initialize the global variable %load-path, given the value of the
192 SCM_SITE_DIR and SCM_LIBRARY_DIR preprocessor symbols and the
193 GUILE_LOAD_PATH environment variable. */
195 scm_init_load_path ()
199 #ifdef SCM_LIBRARY_DIR
200 path
= scm_listify (scm_makfrom0str (SCM_SITE_DIR
),
201 scm_makfrom0str (SCM_LIBRARY_DIR
),
202 scm_makfrom0str (SCM_PKGDATA_DIR
),
204 #endif /* SCM_LIBRARY_DIR */
206 path
= scm_internal_parse_path (getenv ("GUILE_LOAD_PATH"), path
);
208 *scm_loc_load_path
= path
;
211 SCM scm_listofnullstr
;
213 /* Search PATH for a directory containing a file named FILENAME.
214 The file must be readable, and not a directory.
215 If we find one, return its full filename; otherwise, return #f.
216 If FILENAME is absolute, return it unchanged.
217 If given, EXTENSIONS is a list of strings; for each directory
218 in PATH, we search for FILENAME concatenated with each EXTENSION. */
219 SCM_PROC(s_search_path
, "search-path", 2, 1, 0, scm_search_path
);
221 scm_search_path (path
, filename
, extensions
)
226 char *filename_chars
;
228 size_t max_path_len
; /* maximum length of any PATH element */
229 size_t max_ext_len
; /* maximum length of any EXTENSIONS element */
231 SCM_ASSERT (scm_ilength (path
) >= 0, path
, SCM_ARG1
, s_search_path
);
232 SCM_ASSERT (SCM_NIMP (filename
) && SCM_ROSTRINGP (filename
), filename
,
233 SCM_ARG2
, s_search_path
);
234 if (SCM_UNBNDP (extensions
))
235 extensions
= SCM_EOL
;
237 SCM_ASSERT (scm_ilength (extensions
) >= 0, extensions
,
238 SCM_ARG3
, s_search_path
);
240 filename_chars
= SCM_ROCHARS (filename
);
241 filename_len
= SCM_ROLENGTH (filename
);
243 /* If FILENAME is absolute, return it unchanged. */
244 if (filename_len
>= 1 && filename_chars
[0] == '/')
247 /* Find the length of the longest element of path. */
252 for (walk
= path
; SCM_NIMP (walk
); walk
= SCM_CDR (walk
))
254 SCM elt
= SCM_CAR (walk
);
255 SCM_ASSERT (SCM_NIMP (elt
) && SCM_ROSTRINGP (elt
), elt
,
256 "path is not a list of strings",
258 if (SCM_ROLENGTH (elt
) > max_path_len
)
259 max_path_len
= SCM_ROLENGTH (elt
);
263 /* If FILENAME has an extension, don't try to add EXTENSIONS to it. */
267 for (endp
= filename_chars
+ filename_len
- 1;
268 endp
>= filename_chars
;
273 /* This filename already has an extension, so cancel the
274 list of extensions. */
275 extensions
= SCM_EOL
;
278 else if (*endp
== '/')
279 /* This filename has no extension, so keep the current list
285 /* Find the length of the longest element of the load extensions
291 for (walk
= extensions
; SCM_NIMP (walk
); walk
= SCM_CDR (walk
))
293 SCM elt
= SCM_CAR (walk
);
294 SCM_ASSERT (SCM_NIMP (elt
) && SCM_ROSTRINGP (elt
), elt
,
295 "extension list is not a list of strings",
297 if (SCM_ROLENGTH (elt
) > max_ext_len
)
298 max_ext_len
= SCM_ROLENGTH (elt
);
305 SCM result
= SCM_BOOL_F
;
306 int buf_size
= max_path_len
+ 1 + filename_len
+ max_ext_len
+ 1;
307 char *buf
= scm_must_malloc (buf_size
, s_search_path
);
309 /* This simplifies the loop below a bit. */
310 if (SCM_NULLP (extensions
))
311 extensions
= scm_listofnullstr
;
313 /* Try every path element. At this point, we know the path is a
314 proper list of strings. */
315 for (; SCM_NIMP (path
); path
= SCM_CDR (path
))
318 SCM dir
= SCM_CAR (path
);
321 /* Concatenate the path name and the filename. */
322 len
= SCM_ROLENGTH (dir
);
323 memcpy (buf
, SCM_ROCHARS (dir
), len
);
324 if (len
>= 1 && buf
[len
- 1] != '/')
326 memcpy (buf
+ len
, filename_chars
, filename_len
);
329 /* Try every extension. At this point, we know the extension
330 list is a proper, nonempty list of strings. */
331 for (exts
= extensions
; SCM_NIMP (exts
); exts
= SCM_CDR (exts
))
333 SCM ext
= SCM_CAR (exts
);
334 int ext_len
= SCM_ROLENGTH (ext
);
337 /* Concatenate the extension. */
338 memcpy (buf
+ len
, SCM_ROCHARS (ext
), ext_len
);
339 buf
[len
+ ext_len
] = '\0';
341 /* If the file exists at all, we should return it. If the
342 file is inaccessible, then that's an error. */
343 if (stat (buf
, &mode
) == 0
344 && ! (mode
.st_mode
& S_IFDIR
))
346 result
= scm_makfromstr (buf
, len
+ ext_len
, 0);
354 scm_done_malloc (- buf_size
);
361 /* Search %load-path for a directory containing a file named FILENAME.
362 The file must be readable, and not a directory.
363 If we find one, return its full filename; otherwise, return #f.
364 If FILENAME is absolute, return it unchanged. */
365 SCM_PROC(s_sys_search_load_path
, "%search-load-path", 1, 0, 0, scm_sys_search_load_path
);
367 scm_sys_search_load_path (filename
)
370 SCM path
= *scm_loc_load_path
;
371 SCM exts
= *scm_loc_load_extensions
;
372 SCM_ASSERT (SCM_NIMP (filename
) && SCM_ROSTRINGP (filename
), filename
,
373 SCM_ARG1
, s_sys_search_load_path
);
374 SCM_ASSERT (scm_ilength (path
) >= 0, path
, "load path is not a proper list",
375 s_sys_search_load_path
);
376 SCM_ASSERT (scm_ilength (exts
) >= 0, exts
,
377 "load extension list is not a proper list",
378 s_sys_search_load_path
);
379 return scm_search_path (path
,
385 SCM_PROC(s_primitive_load_path
, "primitive-load-path", 1, 0, 0, scm_primitive_load_path
);
387 scm_primitive_load_path (filename
)
392 SCM_ASSERT (SCM_NIMP (filename
) && SCM_ROSTRINGP (filename
), filename
,
393 SCM_ARG1
, s_primitive_load_path
);
395 full_filename
= scm_sys_search_load_path (filename
);
397 if (SCM_FALSEP (full_filename
))
399 int absolute
= (SCM_ROLENGTH (filename
) >= 1
400 && SCM_ROCHARS (filename
)[0] == '/');
401 scm_misc_error (s_primitive_load_path
,
403 ? "Unable to load file %S"
404 : "Unable to find file %S in load path"),
405 scm_listify (filename
, SCM_UNDEFINED
));
408 return scm_primitive_load (full_filename
);
411 /* The following function seems trivial - and indeed it is. Its
412 * existence is motivated by its ability to evaluate expressions
413 * without copying them first (as is done in "eval").
416 SCM_SYMBOL (scm_end_of_file_key
, "end-of-file");
418 SCM_PROC (s_read_and_eval_x
, "read-and-eval!", 0, 1, 0, scm_read_and_eval_x
);
421 scm_read_and_eval_x (port
)
424 SCM form
= scm_read (port
);
425 if (SCM_EOF_OBJECT_P (form
))
426 scm_ithrow (scm_end_of_file_key
, SCM_EOL
, 1);
427 return scm_eval_x (form
);
431 /* Information about the build environment. */
433 /* Initialize the scheme variable %guile-build-info, based on data
434 provided by the Makefile, via libpath.h. */
438 static struct { char *name
; char *value
; } info
[] = SCM_BUILD_INFO
;
439 SCM
*loc
= SCM_CDRLOC (scm_sysintern ("%guile-build-info", SCM_EOL
));
442 for (i
= 0; i
< (sizeof (info
) / sizeof (info
[0])); i
++)
443 *loc
= scm_acons (SCM_CAR (scm_intern0 (info
[i
].name
)),
444 scm_makfrom0str (info
[i
].value
),
453 scm_listofnullstr
= scm_permanent_object (SCM_LIST1 (scm_nullstr
));
454 scm_loc_load_path
= SCM_CDRLOC (scm_sysintern ("%load-path", SCM_EOL
));
455 scm_loc_load_extensions
456 = SCM_CDRLOC (scm_sysintern ("%load-extensions",
457 scm_listify (scm_makfrom0str (".scm"),
458 scm_makfrom0str (""),
460 scm_loc_load_hook
= SCM_CDRLOC (scm_sysintern ("%load-hook", SCM_BOOL_F
));