1 /* Copyright (C) 1995,1996 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, 675 Mass Ave, Cambridge, MA 02139, USA.
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
20 * The exception is that, if you link the GUILE library with other files
21 * to produce an executable, this does not by itself cause the
22 * resulting executable to be covered by the GNU General Public License.
23 * Your use of that executable is in no way restricted on account of
24 * linking the GUILE library code into it.
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
29 * This exception applies only to the code released by the
30 * Free Software Foundation under the name GUILE. If you copy
31 * code from other Free Software Foundation releases into a copy of
32 * GUILE, as the General Public License permits, the exception does
33 * not apply to the code that you add in this way. To avoid misleading
34 * anyone as to the status of such modified files, you must delete
35 * this exception notice from them.
37 * If you write modifications of your own for GUILE, it is your choice
38 * whether to permit this exception to apply to your modifications.
39 * If you do not wish that, delete this exception notice.
52 #include <sys/types.h>
57 #endif /* HAVE_UNISTD_H */
64 /* Loading a file, given an absolute filename. */
66 SCM_PROC(s_sys_try_load
, "primitive-load", 1, 2, 0, scm_sys_try_load
);
68 scm_sys_try_load (filename
, case_insensitive_p
, sharp
)
70 SCM case_insensitive_p
;
73 SCM_ASSERT (SCM_NIMP (filename
) && SCM_ROSTRINGP (filename
), filename
,
74 SCM_ARG1
, s_sys_try_load
);
77 port
= scm_open_file (filename
,
78 scm_makfromstr ("r", (scm_sizet
) sizeof (char), 0));
81 form
= scm_read (port
, case_insensitive_p
, sharp
);
82 if (SCM_EOF_VAL
== form
)
86 scm_close_port (port
);
88 return SCM_UNSPECIFIED
;
92 /* Initializing the load path, and searching it. */
94 static SCM
*scm_loc_load_path
;
96 /* Initialize the global variable %load-path, given the value of the
97 LIBRARY_PATH preprocessor symbol and the SCHEME_LOAD_PATH
98 environment variable. */
100 scm_init_load_path ()
105 path
= scm_cons (scm_makfrom0str (LIBRARY_PATH
), path
);
106 #endif /* LIBRARY_PATH */
109 char *path_string
= getenv ("SCHEME_LOAD_PATH");
111 if (path_string
&& path_string
[0] != '\0')
113 char *scan
, *elt_end
;
115 /* Scan backwards from the end of the string, to help
116 construct the list in the right order. */
117 scan
= elt_end
= path_string
+ strlen (path_string
);
119 /* Scan back to the beginning of the current element. */
121 while (scan
>= path_string
&& *scan
!= ':');
122 path
= scm_cons (scm_makfromstr (scan
+ 1, elt_end
- (scan
+ 1), 0),
125 } while (scan
>= path_string
);
129 *scm_loc_load_path
= path
;
133 /* Search %load-path for a directory containing a file named FILENAME.
134 The file must be readable, and not a directory.
135 If we find one, return its full filename; otherwise, return #f. */
136 SCM_PROC(s_sys_search_load_path
, "%search-load-path", 1, 0, 0, scm_sys_search_load_path
);
138 scm_sys_search_load_path (filename
)
141 SCM path
= *scm_loc_load_path
;
146 SCM_ASSERT (SCM_NIMP (filename
) && SCM_ROSTRINGP (filename
), filename
,
147 SCM_ARG1
, s_sys_search_load_path
);
148 filename_len
= SCM_ROLENGTH (filename
);
153 buf
= scm_must_malloc (buf_size
, s_sys_search_load_path
);
155 while (SCM_NIMP (path
) && SCM_CONSP (path
))
157 SCM elt
= SCM_CAR (path
);
158 if (SCM_NIMP (elt
) && SCM_ROSTRINGP (elt
))
160 int len
= SCM_ROLENGTH (elt
) + 1 + filename_len
;
162 if (len
+ 1 > buf_size
)
164 int old_size
= buf_size
;
166 buf
= scm_must_realloc (buf
, old_size
, buf_size
,
167 s_sys_search_load_path
);
170 memcpy (buf
, SCM_ROCHARS (elt
), SCM_ROLENGTH (elt
));
171 buf
[SCM_ROLENGTH (elt
)] = '/';
172 memcpy (buf
+ SCM_ROLENGTH (elt
) + 1,
173 SCM_ROCHARS (filename
), filename_len
);
179 if (stat (buf
, &mode
) >= 0
180 && ! (mode
.st_mode
& S_IFDIR
)
181 && access (buf
, R_OK
) == 0)
183 SCM result
= scm_makfromstr (buf
, len
, 0);
191 path
= SCM_CDR (path
);
200 SCM_PROC(s_sys_try_load_path
, "%try-load-path", 1, 2, 0,scm_sys_try_load_path
);
202 scm_sys_try_load_path (filename
, case_insensitive_p
, sharp
)
204 SCM case_insensitive_p
;
207 SCM full_filename
= scm_sys_search_load_path (filename
);
208 if (SCM_FALSEP (full_filename
))
210 scm_error (scm_misc_error_key
,
212 "Unable to find file %S in %S",
213 scm_listify (filename
, *scm_loc_load_path
, SCM_UNDEFINED
),
216 return scm_sys_try_load (full_filename
, case_insensitive_p
, sharp
);
224 scm_loc_load_path
= &SCM_CDR(scm_sysintern("%load-path", SCM_EOL
));