2008-07-01 Jerry DeLisle <jvdelisle@gcc.gnu.org>
[official-gcc.git] / gcc / java / jcf-path.c
blob7e21770ec5116be8266dee945e0ff857f8e55fbe
1 /* Handle CLASSPATH, -classpath, and path searching.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006,
3 2007, 2008 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 3, 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 COPYING3. If not see
19 <http://www.gnu.org/licenses/>.
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc. */
25 /* Written by Tom Tromey <tromey@cygnus.com>, October 1998. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
32 #include <dirent.h>
34 #include "jcf.h"
36 #ifndef DIR_UP
37 #define DIR_UP ".."
38 #endif
42 /* Possible flag values. */
43 #define FLAG_SYSTEM 1
44 #define FLAG_ZIP 2
46 /* We keep linked lists of directory names. A ``directory'' can be
47 either an ordinary directory or a .zip file. */
48 struct entry
50 char *name;
51 int flags;
52 struct entry *next;
55 static void free_entry (struct entry **);
56 static void append_entry (struct entry **, struct entry *);
57 static void add_entry (struct entry **, const char *, int);
58 static void add_path (struct entry **, const char *, int);
60 /* We support several different ways to set the class path.
62 built-in system directory (only libgcj.jar)
63 CLASSPATH environment variable
64 -classpath option overrides $CLASSPATH
65 -CLASSPATH option is a synonym for -classpath (for compatibility)
66 -bootclasspath overrides built-in
67 -extdirs sets the extensions directory path (overrides built-in)
68 -I prepends path to list
70 We implement this by keeping several path lists, and then simply
71 ignoring the ones which are not relevant. */
73 /* This holds all the -I directories. */
74 static struct entry *include_dirs;
76 /* This holds the CLASSPATH environment variable. */
77 static struct entry *classpath_env;
79 /* This holds the -classpath command-line option. */
80 static struct entry *classpath_user;
82 /* This holds the default directories. Some of these will have the
83 "system" flag set. */
84 static struct entry *sys_dirs;
86 /* This holds the extensions path entries. */
87 static struct entry *extensions;
89 /* This is the sealed list. It is just a combination of other lists. */
90 static struct entry *sealed;
92 /* We keep track of the longest path we've seen. */
93 static int longest_path = 0;
97 static void
98 free_entry (struct entry **entp)
100 struct entry *e, *n;
102 for (e = *entp; e; e = n)
104 n = e->next;
105 free (e->name);
106 free (e);
108 *entp = NULL;
111 static void
112 append_entry (struct entry **entp, struct entry *ent)
114 /* It doesn't matter if this is slow, since it is run only at
115 startup, and then infrequently. */
116 struct entry *e;
118 /* Find end of list. */
119 for (e = *entp; e && e->next; e = e->next)
122 if (e)
123 e->next = ent;
124 else
125 *entp = ent;
128 static void
129 add_entry (struct entry **entp, const char *filename, int is_system)
131 int len;
132 struct entry *n;
134 n = XNEW (struct entry);
135 n->flags = is_system ? FLAG_SYSTEM : 0;
136 n->next = NULL;
138 len = strlen (filename);
140 if (len > 4 && (FILENAME_CMP (filename + len - 4, ".zip") == 0
141 || FILENAME_CMP (filename + len - 4, ".jar") == 0))
143 n->flags |= FLAG_ZIP;
144 /* If the user uses -classpath then he'll have to include
145 libgcj.jar in the value. We check for this in a simplistic
146 way. Symlinks will fool this test. This is only used for
147 -MM and -MMD, so it probably isn't terribly important. */
148 if (! FILENAME_CMP (filename, LIBGCJ_ZIP_FILE))
149 n->flags |= FLAG_SYSTEM;
152 /* Note that we add a trailing separator to `.zip' names as well.
153 This is a little hack that lets the searching code in jcf-io.c
154 work more easily. Eww. */
155 if (! IS_DIR_SEPARATOR (filename[len - 1]))
157 char *f2 = (char *) alloca (len + 2);
158 strcpy (f2, filename);
159 f2[len] = DIR_SEPARATOR;
160 f2[len + 1] = '\0';
161 n->name = xstrdup (f2);
162 ++len;
164 else
165 n->name = xstrdup (filename);
167 if (len > longest_path)
168 longest_path = len;
170 append_entry (entp, n);
173 static void
174 add_path (struct entry **entp, const char *cp, int is_system)
176 const char *startp, *endp;
178 if (cp)
180 char *buf = (char *) alloca (strlen (cp) + 3);
181 startp = endp = cp;
182 while (1)
184 if (! *endp || *endp == PATH_SEPARATOR)
186 if (endp == startp)
188 buf[0] = '.';
189 buf[1] = DIR_SEPARATOR;
190 buf[2] = '\0';
192 else
194 strncpy (buf, startp, endp - startp);
195 buf[endp - startp] = '\0';
197 add_entry (entp, buf, is_system);
198 if (! *endp)
199 break;
200 ++endp;
201 startp = endp;
203 else
204 ++endp;
209 static int init_done = 0;
211 /* Initialize the path module. */
212 void
213 jcf_path_init (void)
215 char *cp;
216 char *attempt, sep[2];
217 struct stat stat_b;
218 int found = 0, len;
220 if (init_done)
221 return;
222 init_done = 1;
224 sep[0] = DIR_SEPARATOR;
225 sep[1] = '\0';
227 GET_ENVIRONMENT (cp, "GCC_EXEC_PREFIX");
228 if (cp)
230 attempt = (char *) alloca (strlen (cp) + 50);
231 /* The exec prefix can be something like
232 /usr/local/bin/../lib/gcc-lib/. We want to change this
233 into a pointer to the share/java directory. We support two
234 configurations: one where prefix and exec-prefix are the
235 same, and one where exec-prefix is `prefix/SOMETHING'. */
236 strcpy (attempt, cp);
237 strcat (attempt, DIR_UP);
238 strcat (attempt, sep);
239 strcat (attempt, DIR_UP);
240 strcat (attempt, sep);
241 len = strlen (attempt);
243 strcpy (attempt + len, "share");
244 strcat (attempt, sep);
245 strcat (attempt, "java");
246 strcat (attempt, sep);
247 strcat (attempt, "libgcj-" DEFAULT_TARGET_VERSION ".jar");
248 if (! stat (attempt, &stat_b))
250 add_entry (&sys_dirs, attempt, 1);
251 found = 1;
252 strcpy (&attempt[strlen (attempt)
253 - strlen ("libgcj-" DEFAULT_TARGET_VERSION ".jar")],
254 sep);
255 strcat (attempt, "ext");
256 strcat (attempt, sep);
257 if (! stat (attempt, &stat_b))
258 jcf_path_extdirs_arg (attempt);
260 else
262 strcpy (attempt + len, DIR_UP);
263 strcat (attempt, sep);
264 strcat (attempt, "share");
265 strcat (attempt, sep);
266 strcat (attempt, "java");
267 strcat (attempt, sep);
268 strcat (attempt, "libgcj-" DEFAULT_TARGET_VERSION ".jar");
269 if (! stat (attempt, &stat_b))
271 add_entry (&sys_dirs, attempt, 1);
272 found = 1;
273 strcpy (&attempt[strlen (attempt)
274 - strlen ("libgcj-" DEFAULT_TARGET_VERSION ".jar")],
275 sep);
276 strcat (attempt, "ext");
277 strcat (attempt, sep);
278 if (! stat (attempt, &stat_b))
279 jcf_path_extdirs_arg (attempt);
283 if (! found)
285 /* Desperation: use the installed one. */
286 char *extdirs;
287 add_entry (&sys_dirs, LIBGCJ_ZIP_FILE, 1);
288 extdirs = (char *) alloca (strlen (LIBGCJ_ZIP_FILE) + 1);
289 strcpy (extdirs, LIBGCJ_ZIP_FILE);
290 strcpy (&extdirs[strlen (LIBGCJ_ZIP_FILE)
291 - strlen ("libgcj-" DEFAULT_TARGET_VERSION ".jar")],
292 "ext");
293 strcat (extdirs, sep);
294 if (! stat (extdirs, &stat_b))
295 jcf_path_extdirs_arg (extdirs);
298 GET_ENVIRONMENT (cp, "CLASSPATH");
299 add_path (&classpath_env, cp, 0);
302 /* Call this when -classpath is seen on the command line.
303 This overrides only the $CLASSPATH environment variable.
305 void
306 jcf_path_classpath_arg (const char *path)
308 free_entry (&classpath_user);
309 add_path (&classpath_user, path, 0);
312 /* Call this when -bootclasspath is seen on the command line.
314 void
315 jcf_path_bootclasspath_arg (const char *path)
317 free_entry (&sys_dirs);
318 add_path (&sys_dirs, path, 1);
321 /* Call this when -extdirs is seen on the command line.
323 void
324 jcf_path_extdirs_arg (const char *cp)
326 const char *startp, *endp;
328 free_entry (&extensions);
330 if (cp)
332 char *buf = (char *) alloca (strlen (cp) + 3);
333 startp = endp = cp;
334 while (1)
336 if (! *endp || *endp == PATH_SEPARATOR)
338 if (endp == startp)
339 return;
341 strncpy (buf, startp, endp - startp);
342 buf[endp - startp] = '\0';
345 DIR *dirp = NULL;
346 int dirname_length = strlen (buf);
348 dirp = opendir (buf);
349 if (dirp == NULL)
350 return;
352 for (;;)
354 struct dirent *direntp = readdir (dirp);
356 if (!direntp)
357 break;
359 if (direntp->d_name[0] != '.')
361 char *name = (char *) alloca (dirname_length
362 + strlen (direntp->d_name) + 2);
363 strcpy (name, buf);
364 if (! IS_DIR_SEPARATOR (name[dirname_length-1]))
366 name[dirname_length] = DIR_SEPARATOR;
367 name[dirname_length+1] = 0;
369 strcat (name, direntp->d_name);
370 add_entry (&extensions, name, 0);
373 if (dirp)
374 closedir (dirp);
377 if (! *endp)
378 break;
379 ++endp;
380 startp = endp;
382 else
383 ++endp;
388 /* Call this when -I is seen on the command line. */
389 void
390 jcf_path_include_arg (const char *path)
392 add_entry (&include_dirs, path, 0);
395 /* We `seal' the path by linking everything into one big list. Then
396 we provide a way to iterate through the sealed list. If PRINT is
397 true then we print the final class path to stderr. */
398 void
399 jcf_path_seal (int print)
401 struct entry *secondary;
403 sealed = include_dirs;
404 include_dirs = NULL;
406 if (classpath_user)
408 secondary = classpath_user;
409 classpath_user = NULL;
411 else
413 if (! classpath_env)
414 add_entry (&classpath_env, ".", 0);
416 secondary = classpath_env;
417 classpath_env = NULL;
421 free_entry (&classpath_user);
422 free_entry (&classpath_env);
424 append_entry (&sealed, secondary);
425 append_entry (&sealed, sys_dirs);
426 append_entry (&sealed, extensions);
427 sys_dirs = NULL;
428 extensions = NULL;
430 if (print)
432 struct entry *ent;
433 fprintf (stderr, "Class path starts here:\n");
434 for (ent = sealed; ent; ent = ent->next)
436 fprintf (stderr, " %s", ent->name);
437 if ((ent->flags & FLAG_SYSTEM))
438 fprintf (stderr, " (system)");
439 if ((ent->flags & FLAG_ZIP))
440 fprintf (stderr, " (zip)");
441 fprintf (stderr, "\n");
446 void *
447 jcf_path_start (void)
449 return (void *) sealed;
452 void *
453 jcf_path_next (void *x)
455 struct entry *ent = (struct entry *) x;
456 return (void *) ent->next;
459 static const char
460 PATH_SEPARATOR_STR[] = {PATH_SEPARATOR, '\0'};
462 char *
463 jcf_path_compute (const char *prefix)
465 struct entry *iter;
466 char *result;
467 int length = strlen (prefix) + 1;
468 int first;
470 for (iter = sealed; iter != NULL; iter = iter->next)
471 length += strlen (iter->name) + 1;
473 result = (char *) xmalloc (length);
474 strcpy (result, prefix);
475 first = 1;
476 for (iter = sealed; iter != NULL; iter = iter->next)
478 if (! first)
479 strcat (result, PATH_SEPARATOR_STR);
480 first = 0;
481 strcat (result, iter->name);
482 /* Ugly: we want to strip the '/' from zip entries when
483 computing a string classpath. */
484 if ((iter->flags & FLAG_ZIP) != 0)
485 result[strlen (result) - 1] = '\0';
488 return result;
491 /* We guarantee that the return path will either be a zip file, or it
492 will end with a directory separator. */
493 char *
494 jcf_path_name (void *x)
496 struct entry *ent = (struct entry *) x;
497 return ent->name;
501 jcf_path_is_zipfile (void *x)
503 struct entry *ent = (struct entry *) x;
504 return (ent->flags & FLAG_ZIP);
508 jcf_path_is_system (void *x)
510 struct entry *ent = (struct entry *) x;
511 return (ent->flags & FLAG_SYSTEM);
515 jcf_path_max_len (void)
517 return longest_path;