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 | |
15 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
16 | * | |
17 | * As a special exception, the Free Software Foundation gives permission | |
18 | * for additional uses of the text contained in its release of GUILE. | |
19 | * | |
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. | |
25 | * | |
26 | * This exception does not however invalidate any other reasons why | |
27 | * the executable file might be covered by the GNU General Public License. | |
28 | * | |
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. | |
36 | * | |
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. | |
40 | */ | |
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" | |
49 | ||
50 | #include "load.h" | |
06721500 JB |
51 | |
52 | #include <sys/types.h> | |
53 | #include <sys/stat.h> | |
54 | ||
55 | #ifdef HAVE_UNISTD_H | |
56 | #include <unistd.h> | |
57 | #endif /* HAVE_UNISTD_H */ | |
58 | ||
59 | #ifndef R_OK | |
60 | #define R_OK 4 | |
61 | #endif | |
0f2d19dd JB |
62 | |
63 | \f | |
06721500 | 64 | /* Loading a file, given an absolute filename. */ |
0f2d19dd JB |
65 | |
66 | SCM_PROC(s_sys_try_load, "%try-load", 1, 2, 0, scm_sys_try_load); | |
0f2d19dd | 67 | SCM |
06721500 | 68 | scm_sys_try_load (filename, case_insensitive_p, sharp) |
0f2d19dd | 69 | SCM filename; |
06721500 | 70 | SCM case_insensitive_p; |
0f2d19dd | 71 | SCM sharp; |
0f2d19dd | 72 | { |
06721500 JB |
73 | SCM_ASSERT (SCM_NIMP (filename) && SCM_ROSTRINGP (filename), filename, |
74 | SCM_ARG1, s_sys_try_load); | |
0f2d19dd JB |
75 | { |
76 | SCM form, port; | |
77 | port = scm_open_file (filename, | |
78 | scm_makfromstr ("r", (scm_sizet) sizeof (char), 0)); | |
79 | if (SCM_FALSEP (port)) | |
80 | return SCM_BOOL_F; | |
81 | while (1) | |
82 | { | |
06721500 | 83 | form = scm_read (port, case_insensitive_p, sharp); |
0f2d19dd JB |
84 | if (SCM_EOF_VAL == form) |
85 | break; | |
86 | scm_eval_x (form); | |
87 | } | |
88 | scm_close_port (port); | |
89 | } | |
90 | return SCM_BOOL_T; | |
91 | } | |
92 | ||
93 | \f | |
06721500 | 94 | /* Initializing the load path, and searching it. */ |
0f2d19dd | 95 | |
06721500 JB |
96 | static SCM *scm_loc_load_path; |
97 | ||
98 | /* Initialize the global variable %load-path, given the value of the | |
99 | LIBRARY_PATH preprocessor symbol and the SCHEME_LOAD_PATH | |
100 | environment variable. */ | |
0f2d19dd | 101 | void |
06721500 JB |
102 | scm_init_load_path () |
103 | { | |
104 | SCM path = SCM_EOL; | |
105 | ||
106 | #ifdef LIBRARY_PATH | |
107 | path = scm_cons (scm_makfrom0str (LIBRARY_PATH), path); | |
108 | #endif /* LIBRARY_PATH */ | |
109 | ||
110 | { | |
111 | char *path_string = getenv ("SCHEME_LOAD_PATH"); | |
112 | ||
113 | if (path_string && path_string[0] != '\0') | |
114 | { | |
115 | char *scan, *elt_end; | |
116 | ||
117 | /* Scan backwards from the end of the string, to help | |
118 | construct the list in the right order. */ | |
119 | scan = elt_end = path_string + strlen (path_string); | |
120 | do { | |
121 | /* Scan back to the beginning of the current element. */ | |
122 | do scan--; | |
123 | while (scan >= path_string && *scan != ':'); | |
124 | path = scm_cons (scm_makfromstr (scan + 1, elt_end - (scan + 1), 0), | |
125 | path); | |
126 | elt_end = scan; | |
127 | } while (scan >= path_string); | |
128 | } | |
129 | } | |
130 | ||
131 | *scm_loc_load_path = path; | |
132 | } | |
133 | ||
134 | ||
135 | /* Search %load-path for a directory containing a file named FILENAME. | |
136 | The file must be readable, and not a directory. | |
137 | If we find one, return its full filename; otherwise, return #f. */ | |
138 | SCM_PROC(s_sys_search_load_path, "%search-load-path", 1, 0, 0, scm_sys_search_load_path); | |
139 | SCM | |
140 | scm_sys_search_load_path (filename) | |
141 | SCM filename; | |
142 | { | |
143 | SCM path = *scm_loc_load_path; | |
144 | char *buf; | |
145 | int buf_size; | |
146 | int filename_len; | |
147 | ||
148 | SCM_ASSERT (SCM_NIMP (filename) && SCM_ROSTRINGP (filename), filename, | |
149 | SCM_ARG1, s_sys_search_load_path); | |
150 | filename_len = SCM_ROLENGTH (filename); | |
151 | ||
152 | SCM_DEFER_INTS; | |
153 | ||
154 | buf_size = 80; | |
155 | buf = scm_must_malloc (buf_size, s_sys_search_load_path); | |
156 | ||
157 | while (SCM_NIMP (path) && SCM_CONSP (path)) | |
158 | { | |
159 | SCM elt = SCM_CAR (path); | |
160 | if (SCM_NIMP (elt) && SCM_ROSTRINGP (elt)) | |
161 | { | |
162 | int len = SCM_ROLENGTH (elt) + 1 + filename_len; | |
163 | ||
164 | if (len + 1 > buf_size) | |
165 | { | |
166 | int old_size = buf_size; | |
167 | buf_size = len + 50; | |
168 | buf = scm_must_realloc (buf, old_size, buf_size, | |
169 | s_sys_search_load_path); | |
170 | } | |
171 | ||
172 | memcpy (buf, SCM_ROCHARS (elt), SCM_ROLENGTH (elt)); | |
173 | buf[SCM_ROLENGTH (elt)] = '/'; | |
174 | memcpy (buf + SCM_ROLENGTH (elt) + 1, | |
175 | SCM_ROCHARS (filename), filename_len); | |
176 | buf[len] = '\0'; | |
177 | ||
178 | { | |
179 | struct stat mode; | |
180 | ||
181 | if (stat (buf, &mode) >= 0 | |
182 | && ! (mode.st_mode & S_IFDIR) | |
183 | && access (buf, R_OK) == 0) | |
184 | { | |
185 | SCM result = scm_makfromstr (buf, len, 0); | |
186 | scm_must_free (buf); | |
187 | SCM_ALLOW_INTS; | |
188 | return result; | |
189 | } | |
190 | } | |
191 | } | |
192 | ||
193 | path = SCM_CDR (path); | |
194 | } | |
195 | ||
196 | scm_must_free (buf); | |
197 | SCM_ALLOW_INTS; | |
198 | return SCM_BOOL_F; | |
199 | } | |
200 | ||
201 | ||
202 | SCM_PROC(s_sys_try_load_path, "%try-load-path", 1, 2, 0,scm_sys_try_load_path); | |
203 | SCM | |
204 | scm_sys_try_load_path (filename, case_insensitive_p, sharp) | |
205 | SCM filename; | |
206 | SCM case_insensitive_p; | |
207 | SCM sharp; | |
208 | { | |
209 | SCM full_filename = scm_sys_search_load_path (filename); | |
210 | ||
211 | if (SCM_NIMP (full_filename) && SCM_ROSTRINGP (full_filename)) | |
212 | return scm_sys_try_load (full_filename, case_insensitive_p, sharp); | |
213 | else | |
214 | return scm_sys_try_load (filename, case_insensitive_p, sharp); | |
215 | } | |
216 | ||
217 | \f | |
218 | ||
0f2d19dd JB |
219 | void |
220 | scm_init_load () | |
0f2d19dd | 221 | { |
06721500 JB |
222 | scm_loc_load_path = &SCM_CDR(scm_sysintern("%load-path", SCM_EOL)); |
223 | ||
0f2d19dd JB |
224 | #include "load.x" |
225 | } |