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