]>
Commit | Line | Data |
---|---|---|
8603f9c5 TT |
1 | /* Handle CLASSPATH, -classpath, and path searching. |
2 | ||
3 | Copyright (C) 1998 Free Software Foundation, Inc. | |
4 | ||
5 | This program is free software; you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 2, or (at your option) | |
8 | any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with GNU CC; see the file COPYING. If not, write to | |
17 | the Free Software Foundation, 59 Temple Place - Suite 330, | |
18 | Boston, MA 02111-1307, USA. | |
19 | ||
20 | Java and all Java-based marks are trademarks or registered trademarks | |
21 | of Sun Microsystems, Inc. in the United States and other countries. | |
22 | The Free Software Foundation is independent of Sun Microsystems, Inc. */ | |
23 | ||
24 | /* Written by Tom Tromey <tromey@cygnus.com>, October 1998. */ | |
25 | ||
26 | #include <config.h> | |
27 | #include "system.h" | |
28 | ||
29 | #include "jcf.h" | |
30 | ||
31 | /* Some boilerplate that really belongs in a header. */ | |
32 | ||
33 | #ifndef GET_ENV_PATH_LIST | |
34 | #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0) | |
35 | #endif | |
36 | ||
37 | /* By default, colon separates directories in a path. */ | |
38 | #ifndef PATH_SEPARATOR | |
39 | #define PATH_SEPARATOR ':' | |
40 | #endif | |
41 | ||
42 | #ifndef DIR_SEPARATOR | |
43 | #define DIR_SEPARATOR '/' | |
44 | #endif | |
45 | ||
46 | \f | |
47 | ||
48 | /* Possible flag values. */ | |
49 | #define FLAG_SYSTEM 1 | |
50 | #define FLAG_ZIP 2 | |
51 | ||
52 | /* We keep linked lists of directory names. A ``directory'' can be | |
53 | either an ordinary directory or a .zip file. */ | |
54 | struct entry | |
55 | { | |
56 | char *name; | |
57 | int flags; | |
58 | struct entry *next; | |
59 | }; | |
60 | ||
61 | /* We support several different ways to set the class path. | |
62 | ||
63 | built-in system directory (only libjava.zip) | |
64 | CLASSPATH environment variable | |
65 | -CLASSPATH overrides CLASSPATH | |
66 | -classpath option - overrides CLASSPATH, -CLASSPATH, and built-in | |
67 | -I prepends path to list | |
68 | ||
69 | We implement this by keeping several path lists, and then simply | |
70 | ignoring the ones which are not relevant. */ | |
71 | ||
72 | /* This holds all the -I directories. */ | |
73 | static struct entry *include_dirs; | |
74 | ||
75 | /* This holds the CLASSPATH environment variable. */ | |
76 | static struct entry *classpath_env; | |
77 | ||
78 | /* This holds the -CLASSPATH command-line option. */ | |
79 | static struct entry *classpath_u; | |
80 | ||
81 | /* This holds the -classpath command-line option. */ | |
82 | static struct entry *classpath_l; | |
83 | ||
84 | /* This holds the default directories. Some of these will have the | |
85 | "system" flag set. */ | |
86 | static struct entry *sys_dirs; | |
87 | ||
88 | /* This is the sealed list. It is just a combination of other lists. */ | |
89 | static struct entry *sealed; | |
90 | ||
91 | /* We keep track of the longest path we've seen. */ | |
92 | static int longest_path = 0; | |
93 | ||
94 | \f | |
95 | ||
96 | static void | |
97 | free_entry (entp) | |
98 | struct entry **entp; | |
99 | { | |
100 | struct entry *e, *n; | |
101 | ||
102 | for (e = *entp; e; e = n) | |
103 | { | |
104 | n = e->next; | |
105 | free (e->name); | |
106 | free (e); | |
107 | } | |
108 | *entp = NULL; | |
109 | } | |
110 | ||
111 | static void | |
112 | append_entry (entp, ent) | |
113 | struct entry **entp; | |
114 | struct entry *ent; | |
115 | { | |
116 | /* It doesn't matter if this is slow, since it is run only at | |
117 | startup, and then infrequently. */ | |
118 | struct entry *e; | |
119 | ||
120 | /* Find end of list. */ | |
121 | for (e = *entp; e && e->next; e = e->next) | |
122 | ; | |
123 | ||
124 | if (e) | |
125 | e->next = ent; | |
126 | else | |
127 | *entp = ent; | |
128 | } | |
129 | ||
130 | static void | |
131 | add_entry (entp, filename, is_system) | |
132 | struct entry **entp; | |
133 | char *filename; | |
134 | int is_system; | |
135 | { | |
136 | int len; | |
137 | struct entry *n; | |
138 | ||
139 | n = (struct entry *) ALLOC (sizeof (struct entry)); | |
140 | n->flags = is_system ? FLAG_SYSTEM : 0; | |
141 | n->next = NULL; | |
142 | ||
143 | len = strlen (filename); | |
144 | if (len > 4 && ! strcmp (filename - 4, ".zip")) | |
145 | { | |
146 | n->flags |= FLAG_ZIP; | |
147 | /* If the user uses -classpath then he'll have to include | |
148 | libjava.zip in the value. We check for this in a simplistic | |
149 | way. Symlinks will fool this test. This is only used for | |
150 | -MM and -MMD, so it probably isn't terribly important. */ | |
151 | if (! strcmp (filename, LIBJAVA_ZIP_FILE)) | |
152 | n->flags |= FLAG_SYSTEM; | |
153 | } | |
154 | ||
71f6a8e2 TT |
155 | if (! (n->flags & FLAG_ZIP) |
156 | && filename[len - 1] != '/' | |
157 | && filename[len - 1] != DIR_SEPARATOR) | |
8603f9c5 TT |
158 | { |
159 | char *f2 = (char *) alloca (len + 1); | |
160 | strcpy (f2, filename); | |
161 | f2[len] = DIR_SEPARATOR; | |
162 | f2[len + 1] = '\0'; | |
163 | n->name = strdup (f2); | |
164 | ++len; | |
165 | } | |
166 | else | |
167 | n->name = strdup (filename); | |
168 | ||
169 | if (len > longest_path) | |
170 | longest_path = len; | |
171 | ||
172 | append_entry (entp, n); | |
173 | } | |
174 | ||
175 | static void | |
176 | add_path (entp, cp, is_system) | |
177 | struct entry **entp; | |
178 | char *cp; | |
179 | int is_system; | |
180 | { | |
181 | char *startp, *endp; | |
182 | ||
183 | if (cp) | |
184 | { | |
185 | char *buf = (char *) alloca (strlen (cp) + 3); | |
186 | startp = endp = cp; | |
187 | while (1) | |
188 | { | |
189 | if (! *endp || *endp == PATH_SEPARATOR) | |
190 | { | |
8603f9c5 TT |
191 | if (endp == startp) |
192 | { | |
193 | buf[0] = '.'; | |
194 | buf[1] = DIR_SEPARATOR; | |
195 | buf[2] = '\0'; | |
196 | } | |
71f6a8e2 | 197 | else |
8603f9c5 | 198 | { |
71f6a8e2 TT |
199 | strncpy (buf, startp, endp - startp); |
200 | buf[endp - startp] = '\0'; | |
8603f9c5 | 201 | } |
8603f9c5 TT |
202 | add_entry (entp, buf, is_system); |
203 | if (! *endp) | |
204 | break; | |
205 | ++endp; | |
206 | startp = endp; | |
207 | } | |
208 | else | |
209 | ++endp; | |
210 | } | |
211 | } | |
212 | } | |
213 | ||
214 | /* Initialize the path module. */ | |
215 | void | |
216 | jcf_path_init () | |
217 | { | |
218 | char *cp; | |
219 | ||
220 | add_entry (&sys_dirs, ".", 0); | |
221 | add_entry (&sys_dirs, LIBJAVA_ZIP_FILE, 1); | |
222 | ||
223 | GET_ENV_PATH_LIST (cp, "CLASSPATH"); | |
224 | add_path (&classpath_env, cp, 0); | |
225 | } | |
226 | ||
227 | /* Call this when -classpath is seen on the command line. */ | |
228 | void | |
229 | jcf_path_classpath_arg (path) | |
230 | char *path; | |
231 | { | |
232 | free_entry (&classpath_l); | |
233 | add_path (&classpath_l, path, 0); | |
234 | } | |
235 | ||
236 | /* Call this when -CLASSPATH is seen on the command line. */ | |
237 | void | |
238 | jcf_path_CLASSPATH_arg (path) | |
239 | char *path; | |
240 | { | |
241 | free_entry (&classpath_u); | |
242 | add_path (&classpath_u, path, 0); | |
243 | } | |
244 | ||
245 | /* Call this when -I is seen on the command line. */ | |
246 | void | |
247 | jcf_path_include_arg (path) | |
248 | char *path; | |
249 | { | |
250 | add_entry (&include_dirs, path, 0); | |
251 | } | |
252 | ||
253 | /* We `seal' the path by linking everything into one big list. Then | |
254 | we provide a way to iterate through the sealed list. */ | |
255 | void | |
256 | jcf_path_seal () | |
257 | { | |
258 | int do_system = 1; | |
259 | struct entry *secondary; | |
260 | ||
261 | sealed = include_dirs; | |
262 | include_dirs = NULL; | |
263 | ||
264 | if (classpath_l) | |
265 | { | |
266 | secondary = classpath_l; | |
267 | classpath_l = NULL; | |
268 | do_system = 0; | |
269 | } | |
270 | else if (classpath_u) | |
271 | { | |
272 | secondary = classpath_u; | |
273 | classpath_u = NULL; | |
274 | } | |
275 | else | |
276 | { | |
277 | secondary = classpath_env; | |
278 | classpath_env = NULL; | |
279 | } | |
280 | ||
281 | free_entry (&classpath_l); | |
282 | free_entry (&classpath_u); | |
283 | free_entry (&classpath_env); | |
284 | ||
285 | append_entry (&sealed, secondary); | |
286 | ||
287 | if (do_system) | |
288 | { | |
289 | append_entry (&sealed, sys_dirs); | |
290 | sys_dirs = NULL; | |
291 | } | |
292 | else | |
293 | free_entry (&sys_dirs); | |
294 | } | |
295 | ||
296 | void * | |
297 | jcf_path_start () | |
298 | { | |
299 | return (void *) sealed; | |
300 | } | |
301 | ||
302 | void * | |
303 | jcf_path_next (x) | |
304 | void *x; | |
305 | { | |
306 | struct entry *ent = (struct entry *) x; | |
307 | return (void *) ent->next; | |
308 | } | |
309 | ||
310 | /* We guarantee that the return path will either be a zip file, or it | |
311 | will end with a directory separator. */ | |
312 | char * | |
313 | jcf_path_name (x) | |
314 | void *x; | |
315 | { | |
316 | struct entry *ent = (struct entry *) x; | |
317 | return ent->name; | |
318 | } | |
319 | ||
320 | int | |
321 | jcf_path_is_zipfile (x) | |
322 | void *x; | |
323 | { | |
324 | struct entry *ent = (struct entry *) x; | |
325 | return (ent->flags & FLAG_ZIP); | |
326 | } | |
327 | ||
328 | int | |
329 | jcf_path_is_system (x) | |
330 | void *x; | |
331 | { | |
332 | struct entry *ent = (struct entry *) x; | |
333 | return (ent->flags & FLAG_SYSTEM); | |
334 | } | |
335 | ||
336 | int | |
337 | jcf_path_max_len () | |
338 | { | |
339 | return longest_path; | |
340 | } |