2003-03-15 Glen Nakamura <glen@imodulo.com>
[official-gcc.git] / gcc / java / jcf-path.c
blob8771e6fd879ff8f79fd6295603f873559e351eee
1 /* Handle CLASSPATH, -classpath, and path searching.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc. */
26 /* Written by Tom Tromey <tromey@cygnus.com>, October 1998. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
33 #include <dirent.h>
35 #include "jcf.h"
37 /* By default, colon separates directories in a path. */
38 #ifndef PATH_SEPARATOR
39 #define PATH_SEPARATOR ':'
40 #endif
42 #ifndef DIR_SEPARATOR
43 #define DIR_SEPARATOR '/'
44 #endif
46 #ifndef DIR_UP
47 #define DIR_UP ".."
48 #endif
52 /* Possible flag values. */
53 #define FLAG_SYSTEM 1
54 #define FLAG_ZIP 2
56 /* We keep linked lists of directory names. A ``directory'' can be
57 either an ordinary directory or a .zip file. */
58 struct entry
60 char *name;
61 int flags;
62 struct entry *next;
65 static void free_entry (struct entry **);
66 static void append_entry (struct entry **, struct entry *);
67 static void add_entry (struct entry **, const char *, int);
68 static void add_path (struct entry **, const char *, int);
70 /* We support several different ways to set the class path.
72 built-in system directory (only libgcj.jar)
73 CLASSPATH environment variable
74 -classpath option overrides $CLASSPATH
75 -CLASSPATH option is a synonym for -classpath (for compatibility)
76 -bootclasspath overrides built-in
77 -extdirs sets the extensions directory path (overrides built-in)
78 -I prepends path to list
80 We implement this by keeping several path lists, and then simply
81 ignoring the ones which are not relevant. */
83 /* This holds all the -I directories. */
84 static struct entry *include_dirs;
86 /* This holds the CLASSPATH environment variable. */
87 static struct entry *classpath_env;
89 /* This holds the -classpath command-line option. */
90 static struct entry *classpath_user;
92 /* This holds the default directories. Some of these will have the
93 "system" flag set. */
94 static struct entry *sys_dirs;
96 /* This holds the extensions path entries. */
97 static struct entry *extensions;
99 /* This is the sealed list. It is just a combination of other lists. */
100 static struct entry *sealed;
102 /* We keep track of the longest path we've seen. */
103 static int longest_path = 0;
107 static void
108 free_entry (struct entry **entp)
110 struct entry *e, *n;
112 for (e = *entp; e; e = n)
114 n = e->next;
115 free (e->name);
116 free (e);
118 *entp = NULL;
121 static void
122 append_entry (struct entry **entp, struct entry *ent)
124 /* It doesn't matter if this is slow, since it is run only at
125 startup, and then infrequently. */
126 struct entry *e;
128 /* Find end of list. */
129 for (e = *entp; e && e->next; e = e->next)
132 if (e)
133 e->next = ent;
134 else
135 *entp = ent;
138 static void
139 add_entry (struct entry **entp, const char *filename, int is_system)
141 int len;
142 struct entry *n;
144 n = ALLOC (sizeof (struct entry));
145 n->flags = is_system ? FLAG_SYSTEM : 0;
146 n->next = NULL;
148 len = strlen (filename);
150 if (len > 4 && (COMPARE_FILENAMES (filename + len - 4, ".zip") == 0
151 || COMPARE_FILENAMES (filename + len - 4, ".jar") == 0))
153 n->flags |= FLAG_ZIP;
154 /* If the user uses -classpath then he'll have to include
155 libgcj.jar in the value. We check for this in a simplistic
156 way. Symlinks will fool this test. This is only used for
157 -MM and -MMD, so it probably isn't terribly important. */
158 if (! COMPARE_FILENAMES (filename, LIBGCJ_ZIP_FILE))
159 n->flags |= FLAG_SYSTEM;
162 /* Note that we add a trailing separator to `.zip' names as well.
163 This is a little hack that lets the searching code in jcf-io.c
164 work more easily. Eww. */
165 if (! IS_DIR_SEPARATOR (filename[len - 1]))
167 char *f2 = alloca (len + 2);
168 strcpy (f2, filename);
169 f2[len] = DIR_SEPARATOR;
170 f2[len + 1] = '\0';
171 n->name = xstrdup (f2);
172 ++len;
174 else
175 n->name = xstrdup (filename);
177 if (len > longest_path)
178 longest_path = len;
180 append_entry (entp, n);
183 static void
184 add_path (struct entry **entp, const char *cp, int is_system)
186 const char *startp, *endp;
188 if (cp)
190 char *buf = alloca (strlen (cp) + 3);
191 startp = endp = cp;
192 while (1)
194 if (! *endp || *endp == PATH_SEPARATOR)
196 if (endp == startp)
198 buf[0] = '.';
199 buf[1] = DIR_SEPARATOR;
200 buf[2] = '\0';
202 else
204 strncpy (buf, startp, endp - startp);
205 buf[endp - startp] = '\0';
207 add_entry (entp, buf, is_system);
208 if (! *endp)
209 break;
210 ++endp;
211 startp = endp;
213 else
214 ++endp;
219 static int init_done = 0;
221 /* Initialize the path module. */
222 void
223 jcf_path_init (void)
225 char *cp;
226 char *try, sep[2];
227 struct stat stat_b;
228 int found = 0, len;
230 if (init_done)
231 return;
232 init_done = 1;
234 sep[0] = DIR_SEPARATOR;
235 sep[1] = '\0';
237 GET_ENVIRONMENT (cp, "GCC_EXEC_PREFIX");
238 if (cp)
240 try = alloca (strlen (cp) + 50);
241 /* The exec prefix can be something like
242 /usr/local/bin/../lib/gcc-lib/. We want to change this
243 into a pointer to the share/java directory. We support two
244 configurations: one where prefix and exec-prefix are the
245 same, and one where exec-prefix is `prefix/SOMETHING'. */
246 strcpy (try, cp);
247 strcat (try, DIR_UP);
248 strcat (try, sep);
249 strcat (try, DIR_UP);
250 strcat (try, sep);
251 len = strlen (try);
253 strcpy (try + len, "share");
254 strcat (try, sep);
255 strcat (try, "java");
256 strcat (try, sep);
257 strcat (try, "libgcj-" DEFAULT_TARGET_VERSION ".jar");
258 if (! stat (try, &stat_b))
260 add_entry (&sys_dirs, try, 1);
261 found = 1;
262 strcpy (&try[strlen (try)
263 - strlen ("libgcj-" DEFAULT_TARGET_VERSION ".jar")],
264 sep);
265 strcat (try, "ext");
266 strcat (try, sep);
267 if (! stat (try, &stat_b))
268 jcf_path_extdirs_arg (try);
270 else
272 strcpy (try + len, DIR_UP);
273 strcat (try, sep);
274 strcat (try, "share");
275 strcat (try, sep);
276 strcat (try, "java");
277 strcat (try, sep);
278 strcat (try, "libgcj-" DEFAULT_TARGET_VERSION ".jar");
279 if (! stat (try, &stat_b))
281 add_entry (&sys_dirs, try, 1);
282 found = 1;
283 strcpy (&try[strlen (try)
284 - strlen ("libgcj-" DEFAULT_TARGET_VERSION ".jar")],
285 sep);
286 strcat (try, "ext");
287 strcat (try, sep);
288 if (! stat (try, &stat_b))
289 jcf_path_extdirs_arg (try);
293 if (! found)
295 /* Desperation: use the installed one. */
296 char *extdirs;
297 add_entry (&sys_dirs, LIBGCJ_ZIP_FILE, 1);
298 extdirs = alloca (strlen (LIBGCJ_ZIP_FILE) + 1);
299 strcpy (extdirs, LIBGCJ_ZIP_FILE);
300 strcpy (&extdirs[strlen (LIBGCJ_ZIP_FILE)
301 - strlen ("libgcj-" DEFAULT_TARGET_VERSION ".jar")],
302 "ext");
303 strcat (extdirs, sep);
304 if (! stat (extdirs, &stat_b))
305 jcf_path_extdirs_arg (extdirs);
308 GET_ENVIRONMENT (cp, "CLASSPATH");
309 add_path (&classpath_env, cp, 0);
312 /* Call this when -classpath is seen on the command line.
313 This overrides only the $CLASSPATH environment variable.
315 void
316 jcf_path_classpath_arg (const char *path)
318 free_entry (&classpath_user);
319 add_path (&classpath_user, path, 0);
322 /* Call this when -bootclasspath is seen on the command line.
324 void
325 jcf_path_bootclasspath_arg (const char *path)
327 free_entry (&sys_dirs);
328 add_path (&sys_dirs, path, 1);
331 /* Call this when -extdirs is seen on the command line.
333 void
334 jcf_path_extdirs_arg (const char *cp)
336 const char *startp, *endp;
338 free_entry (&extensions);
340 if (cp)
342 char *buf = alloca (strlen (cp) + 3);
343 startp = endp = cp;
344 while (1)
346 if (! *endp || *endp == PATH_SEPARATOR)
348 if (endp == startp)
349 return;
351 strncpy (buf, startp, endp - startp);
352 buf[endp - startp] = '\0';
355 DIR *dirp = NULL;
356 int dirname_length = strlen (buf);
358 dirp = opendir (buf);
359 if (dirp == NULL)
360 return;
362 for (;;)
364 struct dirent *direntp = readdir (dirp);
366 if (!direntp)
367 break;
369 if (direntp->d_name[0] != '.')
371 char *name = alloca (dirname_length
372 + strlen (direntp->d_name) + 2);
373 strcpy (name, buf);
374 if (! IS_DIR_SEPARATOR (name[dirname_length-1]))
376 name[dirname_length] = DIR_SEPARATOR;
377 name[dirname_length+1] = 0;
379 strcat (name, direntp->d_name);
380 add_entry (&extensions, name, 0);
385 if (! *endp)
386 break;
387 ++endp;
388 startp = endp;
390 else
391 ++endp;
396 /* Call this when -I is seen on the command line. */
397 void
398 jcf_path_include_arg (const char *path)
400 add_entry (&include_dirs, path, 0);
403 /* We `seal' the path by linking everything into one big list. Then
404 we provide a way to iterate through the sealed list. If PRINT is
405 true then we print the final class path to stderr. */
406 void
407 jcf_path_seal (int print)
409 struct entry *secondary;
411 sealed = include_dirs;
412 include_dirs = NULL;
414 if (classpath_user)
416 secondary = classpath_user;
417 classpath_user = NULL;
419 else
421 if (! classpath_env)
422 add_entry (&classpath_env, ".", 0);
424 secondary = classpath_env;
425 classpath_env = NULL;
429 free_entry (&classpath_user);
430 free_entry (&classpath_env);
432 append_entry (&sealed, secondary);
433 append_entry (&sealed, sys_dirs);
434 append_entry (&sealed, extensions);
435 sys_dirs = NULL;
436 extensions = NULL;
438 if (print)
440 struct entry *ent;
441 fprintf (stderr, "Class path starts here:\n");
442 for (ent = sealed; ent; ent = ent->next)
444 fprintf (stderr, " %s", ent->name);
445 if ((ent->flags & FLAG_SYSTEM))
446 fprintf (stderr, " (system)");
447 if ((ent->flags & FLAG_ZIP))
448 fprintf (stderr, " (zip)");
449 fprintf (stderr, "\n");
454 void *
455 jcf_path_start (void)
457 return (void *) sealed;
460 void *
461 jcf_path_next (void *x)
463 struct entry *ent = (struct entry *) x;
464 return (void *) ent->next;
467 /* We guarantee that the return path will either be a zip file, or it
468 will end with a directory separator. */
469 char *
470 jcf_path_name (void *x)
472 struct entry *ent = (struct entry *) x;
473 return ent->name;
477 jcf_path_is_zipfile (void *x)
479 struct entry *ent = (struct entry *) x;
480 return (ent->flags & FLAG_ZIP);
484 jcf_path_is_system (void *x)
486 struct entry *ent = (struct entry *) x;
487 return (ent->flags & FLAG_SYSTEM);
491 jcf_path_max_len (void)
493 return longest_path;