FSF GCC merge 02/23/03
[official-gcc.git] / gcc / java / jcf-path.c
blob33f4f80f3c41c233bb86cb6e6c48f694d85f16cd
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);
149 if (len > 4 && (strcmp (filename + len - 4, ".zip") == 0
150 || strcmp (filename + len - 4, ".jar") == 0))
152 n->flags |= FLAG_ZIP;
153 /* If the user uses -classpath then he'll have to include
154 libgcj.jar in the value. We check for this in a simplistic
155 way. Symlinks will fool this test. This is only used for
156 -MM and -MMD, so it probably isn't terribly important. */
157 if (! strcmp (filename, LIBGCJ_ZIP_FILE))
158 n->flags |= FLAG_SYSTEM;
161 /* Note that we add a trailing separator to `.zip' names as well.
162 This is a little hack that lets the searching code in jcf-io.c
163 work more easily. Eww. */
164 if (filename[len - 1] != '/' && filename[len - 1] != DIR_SEPARATOR)
166 char *f2 = alloca (len + 2);
167 strcpy (f2, filename);
168 f2[len] = DIR_SEPARATOR;
169 f2[len + 1] = '\0';
170 n->name = xstrdup (f2);
171 ++len;
173 else
174 n->name = xstrdup (filename);
176 if (len > longest_path)
177 longest_path = len;
179 append_entry (entp, n);
182 static void
183 add_path (struct entry **entp, const char *cp, int is_system)
185 const char *startp, *endp;
187 if (cp)
189 char *buf = alloca (strlen (cp) + 3);
190 startp = endp = cp;
191 while (1)
193 if (! *endp || *endp == PATH_SEPARATOR)
195 if (endp == startp)
197 buf[0] = '.';
198 buf[1] = DIR_SEPARATOR;
199 buf[2] = '\0';
201 else
203 strncpy (buf, startp, endp - startp);
204 buf[endp - startp] = '\0';
206 add_entry (entp, buf, is_system);
207 if (! *endp)
208 break;
209 ++endp;
210 startp = endp;
212 else
213 ++endp;
218 static int init_done = 0;
220 /* Initialize the path module. */
221 void
222 jcf_path_init (void)
224 char *cp;
225 char *try, sep[2];
226 struct stat stat_b;
227 int found = 0, len;
229 if (init_done)
230 return;
231 init_done = 1;
233 sep[0] = DIR_SEPARATOR;
234 sep[1] = '\0';
236 GET_ENVIRONMENT (cp, "GCC_EXEC_PREFIX");
237 if (cp)
239 try = alloca (strlen (cp) + 50);
240 /* The exec prefix can be something like
241 /usr/local/bin/../lib/gcc-lib/. We want to change this
242 into a pointer to the share/java directory. We support two
243 configurations: one where prefix and exec-prefix are the
244 same, and one where exec-prefix is `prefix/SOMETHING'. */
245 strcpy (try, cp);
246 strcat (try, DIR_UP);
247 strcat (try, sep);
248 strcat (try, DIR_UP);
249 strcat (try, sep);
250 len = strlen (try);
252 strcpy (try + len, "share");
253 strcat (try, sep);
254 strcat (try, "java");
255 strcat (try, sep);
256 strcat (try, "libgcj-" DEFAULT_TARGET_VERSION ".jar");
257 if (! stat (try, &stat_b))
259 add_entry (&sys_dirs, try, 1);
260 found = 1;
261 strcpy (&try[strlen (try)
262 - strlen ("libgcj-" DEFAULT_TARGET_VERSION ".jar")],
263 sep);
264 strcat (try, "ext");
265 strcat (try, sep);
266 if (! stat (try, &stat_b))
267 jcf_path_extdirs_arg (try);
269 else
271 strcpy (try + len, DIR_UP);
272 strcat (try, sep);
273 strcat (try, "share");
274 strcat (try, sep);
275 strcat (try, "java");
276 strcat (try, sep);
277 strcat (try, "libgcj-" DEFAULT_TARGET_VERSION ".jar");
278 if (! stat (try, &stat_b))
280 add_entry (&sys_dirs, try, 1);
281 found = 1;
282 strcpy (&try[strlen (try)
283 - strlen ("libgcj-" DEFAULT_TARGET_VERSION ".jar")],
284 sep);
285 strcat (try, "ext");
286 strcat (try, sep);
287 if (! stat (try, &stat_b))
288 jcf_path_extdirs_arg (try);
292 if (! found)
294 /* Desperation: use the installed one. */
295 char *extdirs;
296 add_entry (&sys_dirs, LIBGCJ_ZIP_FILE, 1);
297 extdirs = alloca (strlen (LIBGCJ_ZIP_FILE) + 1);
298 strcpy (extdirs, LIBGCJ_ZIP_FILE);
299 strcpy (&extdirs[strlen (LIBGCJ_ZIP_FILE)
300 - strlen ("libgcj-" DEFAULT_TARGET_VERSION ".jar")],
301 "ext");
302 strcat (extdirs, sep);
303 if (! stat (extdirs, &stat_b))
304 jcf_path_extdirs_arg (extdirs);
307 GET_ENVIRONMENT (cp, "CLASSPATH");
308 add_path (&classpath_env, cp, 0);
311 /* Call this when -classpath is seen on the command line.
312 This overrides only the $CLASSPATH environment variable.
314 void
315 jcf_path_classpath_arg (const char *path)
317 free_entry (&classpath_user);
318 add_path (&classpath_user, path, 0);
321 /* Call this when -bootclasspath is seen on the command line.
323 void
324 jcf_path_bootclasspath_arg (const char *path)
326 free_entry (&sys_dirs);
327 add_path (&sys_dirs, path, 1);
330 /* Call this when -extdirs is seen on the command line.
332 void
333 jcf_path_extdirs_arg (const char *cp)
335 const char *startp, *endp;
337 free_entry (&extensions);
339 if (cp)
341 char *buf = alloca (strlen (cp) + 3);
342 startp = endp = cp;
343 while (1)
345 if (! *endp || *endp == PATH_SEPARATOR)
347 if (endp == startp)
348 return;
350 strncpy (buf, startp, endp - startp);
351 buf[endp - startp] = '\0';
354 DIR *dirp = NULL;
355 int dirname_length = strlen (buf);
357 dirp = opendir (buf);
358 if (dirp == NULL)
359 return;
361 for (;;)
363 struct dirent *direntp = readdir (dirp);
365 if (!direntp)
366 break;
368 if (direntp->d_name[0] != '.')
370 char *name = alloca (dirname_length
371 + strlen (direntp->d_name) + 2);
372 strcpy (name, buf);
373 if (name[dirname_length-1] != DIR_SEPARATOR)
375 name[dirname_length] = DIR_SEPARATOR;
376 name[dirname_length+1] = 0;
378 strcat (name, direntp->d_name);
379 add_entry (&extensions, name, 0);
384 if (! *endp)
385 break;
386 ++endp;
387 startp = endp;
389 else
390 ++endp;
395 /* Call this when -I is seen on the command line. */
396 void
397 jcf_path_include_arg (const char *path)
399 add_entry (&include_dirs, path, 0);
402 /* We `seal' the path by linking everything into one big list. Then
403 we provide a way to iterate through the sealed list. If PRINT is
404 true then we print the final class path to stderr. */
405 void
406 jcf_path_seal (int print)
408 struct entry *secondary;
410 sealed = include_dirs;
411 include_dirs = NULL;
413 if (classpath_user)
415 secondary = classpath_user;
416 classpath_user = NULL;
418 else
420 if (! classpath_env)
421 add_entry (&classpath_env, ".", 0);
423 secondary = classpath_env;
424 classpath_env = NULL;
428 free_entry (&classpath_user);
429 free_entry (&classpath_env);
431 append_entry (&sealed, secondary);
432 append_entry (&sealed, sys_dirs);
433 append_entry (&sealed, extensions);
434 sys_dirs = NULL;
435 extensions = NULL;
437 if (print)
439 struct entry *ent;
440 fprintf (stderr, "Class path starts here:\n");
441 for (ent = sealed; ent; ent = ent->next)
443 fprintf (stderr, " %s", ent->name);
444 if ((ent->flags & FLAG_SYSTEM))
445 fprintf (stderr, " (system)");
446 if ((ent->flags & FLAG_ZIP))
447 fprintf (stderr, " (zip)");
448 fprintf (stderr, "\n");
453 void *
454 jcf_path_start (void)
456 return (void *) sealed;
459 void *
460 jcf_path_next (void *x)
462 struct entry *ent = (struct entry *) x;
463 return (void *) ent->next;
466 /* We guarantee that the return path will either be a zip file, or it
467 will end with a directory separator. */
468 char *
469 jcf_path_name (void *x)
471 struct entry *ent = (struct entry *) x;
472 return ent->name;
476 jcf_path_is_zipfile (void *x)
478 struct entry *ent = (struct entry *) x;
479 return (ent->flags & FLAG_ZIP);
483 jcf_path_is_system (void *x)
485 struct entry *ent = (struct entry *) x;
486 return (ent->flags & FLAG_SYSTEM);
490 jcf_path_max_len (void)
492 return longest_path;