]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/java/jcf-path.c
1 /* Handle CLASSPATH, -classpath, and path searching.
3 Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
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)
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.
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.
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. */
24 /* Written by Tom Tromey <tromey@cygnus.com>, October 1998. */
28 #include "coretypes.h"
35 /* By default, colon separates directories in a path. */
36 #ifndef PATH_SEPARATOR
37 #define PATH_SEPARATOR ':'
41 #define DIR_SEPARATOR '/'
50 /* Possible flag values. */
54 /* We keep linked lists of directory names. A ``directory'' can be
55 either an ordinary directory or a .zip file. */
63 static void free_entry
PARAMS ((struct entry
**));
64 static void append_entry
PARAMS ((struct entry
**, struct entry
*));
65 static void add_entry
PARAMS ((struct entry
**, const char *, int));
66 static void add_path
PARAMS ((struct entry
**, const char *, int));
68 /* We support several different ways to set the class path.
70 built-in system directory (only libgcj.jar)
71 CLASSPATH environment variable
72 -classpath option overrides $CLASSPATH
73 -CLASSPATH option is a synonym for -classpath (for compatibility)
74 -bootclasspath overrides built-in
75 -extdirs sets the extensions directory path (overrides built-in)
76 -I prepends path to list
78 We implement this by keeping several path lists, and then simply
79 ignoring the ones which are not relevant. */
81 /* This holds all the -I directories. */
82 static struct entry
*include_dirs
;
84 /* This holds the CLASSPATH environment variable. */
85 static struct entry
*classpath_env
;
87 /* This holds the -classpath command-line option. */
88 static struct entry
*classpath_user
;
90 /* This holds the default directories. Some of these will have the
92 static struct entry
*sys_dirs
;
94 /* This holds the extensions path entries. */
95 static struct entry
*extensions
;
97 /* This is the sealed list. It is just a combination of other lists. */
98 static struct entry
*sealed
;
100 /* We keep track of the longest path we've seen. */
101 static int longest_path
= 0;
111 for (e
= *entp
; e
; e
= n
)
121 append_entry (entp
, ent
)
125 /* It doesn't matter if this is slow, since it is run only at
126 startup, and then infrequently. */
129 /* Find end of list. */
130 for (e
= *entp
; e
&& e
->next
; e
= e
->next
)
140 add_entry (entp
, filename
, is_system
)
142 const char *filename
;
148 n
= ALLOC (sizeof (struct entry
));
149 n
->flags
= is_system
? FLAG_SYSTEM
: 0;
152 len
= strlen (filename
);
153 if (len
> 4 && (strcmp (filename
+ len
- 4, ".zip") == 0
154 || strcmp (filename
+ len
- 4, ".jar") == 0))
156 n
->flags
|= FLAG_ZIP
;
157 /* If the user uses -classpath then he'll have to include
158 libgcj.jar in the value. We check for this in a simplistic
159 way. Symlinks will fool this test. This is only used for
160 -MM and -MMD, so it probably isn't terribly important. */
161 if (! strcmp (filename
, LIBGCJ_ZIP_FILE
))
162 n
->flags
|= FLAG_SYSTEM
;
165 /* Note that we add a trailing separator to `.zip' names as well.
166 This is a little hack that lets the searching code in jcf-io.c
167 work more easily. Eww. */
168 if (filename
[len
- 1] != '/' && filename
[len
- 1] != DIR_SEPARATOR
)
170 char *f2
= alloca (len
+ 2);
171 strcpy (f2
, filename
);
172 f2
[len
] = DIR_SEPARATOR
;
174 n
->name
= xstrdup (f2
);
178 n
->name
= xstrdup (filename
);
180 if (len
> longest_path
)
183 append_entry (entp
, n
);
187 add_path (entp
, cp
, is_system
)
192 const char *startp
, *endp
;
196 char *buf
= alloca (strlen (cp
) + 3);
200 if (! *endp
|| *endp
== PATH_SEPARATOR
)
205 buf
[1] = DIR_SEPARATOR
;
210 strncpy (buf
, startp
, endp
- startp
);
211 buf
[endp
- startp
] = '\0';
213 add_entry (entp
, buf
, is_system
);
225 static int init_done
= 0;
227 /* Initialize the path module. */
240 sep
[0] = DIR_SEPARATOR
;
243 GET_ENVIRONMENT (cp
, "GCC_EXEC_PREFIX");
246 try = alloca (strlen (cp
) + 50);
247 /* The exec prefix can be something like
248 /usr/local/bin/../lib/gcc-lib/. We want to change this
249 into a pointer to the share/java directory. We support two
250 configurations: one where prefix and exec-prefix are the
251 same, and one where exec-prefix is `prefix/SOMETHING'. */
253 strcat (try, DIR_UP
);
255 strcat (try, DIR_UP
);
259 strcpy (try + len
, "share");
261 strcat (try, "java");
263 strcat (try, "libgcj-" DEFAULT_TARGET_VERSION
".jar");
264 if (! stat (try, &stat_b
))
266 add_entry (&sys_dirs
, try, 1);
268 strcpy (&try[strlen (try)
269 - strlen ("libgcj-" DEFAULT_TARGET_VERSION
".jar")],
273 if (! stat (try, &stat_b
))
274 jcf_path_extdirs_arg (try);
278 strcpy (try + len
, DIR_UP
);
280 strcat (try, "share");
282 strcat (try, "java");
284 strcat (try, "libgcj-" DEFAULT_TARGET_VERSION
".jar");
285 if (! stat (try, &stat_b
))
287 add_entry (&sys_dirs
, try, 1);
289 strcpy (&try[strlen (try)
290 - strlen ("libgcj-" DEFAULT_TARGET_VERSION
".jar")],
294 if (! stat (try, &stat_b
))
295 jcf_path_extdirs_arg (try);
301 /* Desperation: use the installed one. */
303 add_entry (&sys_dirs
, LIBGCJ_ZIP_FILE
, 1);
304 extdirs
= alloca (strlen (LIBGCJ_ZIP_FILE
) + 1);
305 strcpy (extdirs
, LIBGCJ_ZIP_FILE
);
306 strcpy (&extdirs
[strlen (LIBGCJ_ZIP_FILE
)
307 - strlen ("libgcj-" DEFAULT_TARGET_VERSION
".jar")],
309 strcat (extdirs
, sep
);
310 if (! stat (extdirs
, &stat_b
))
311 jcf_path_extdirs_arg (extdirs
);
314 GET_ENVIRONMENT (cp
, "CLASSPATH");
315 add_path (&classpath_env
, cp
, 0);
318 /* Call this when -classpath is seen on the command line.
319 This overrides only the $CLASSPATH environment variable.
322 jcf_path_classpath_arg (path
)
325 free_entry (&classpath_user
);
326 add_path (&classpath_user
, path
, 0);
329 /* Call this when -bootclasspath is seen on the command line.
332 jcf_path_bootclasspath_arg (path
)
335 free_entry (&sys_dirs
);
336 add_path (&sys_dirs
, path
, 1);
339 /* Call this when -extdirs is seen on the command line.
342 jcf_path_extdirs_arg (cp
)
345 const char *startp
, *endp
;
347 free_entry (&extensions
);
351 char *buf
= alloca (strlen (cp
) + 3);
355 if (! *endp
|| *endp
== PATH_SEPARATOR
)
360 strncpy (buf
, startp
, endp
- startp
);
361 buf
[endp
- startp
] = '\0';
365 int dirname_length
= strlen (buf
);
367 dirp
= opendir (buf
);
373 struct dirent
*direntp
= readdir (dirp
);
378 if (direntp
->d_name
[0] != '.')
380 char *name
= alloca (dirname_length
381 + strlen (direntp
->d_name
) + 2);
383 if (name
[dirname_length
-1] != DIR_SEPARATOR
)
385 name
[dirname_length
] = DIR_SEPARATOR
;
386 name
[dirname_length
+1] = 0;
388 strcat (name
, direntp
->d_name
);
389 add_entry (&extensions
, name
, 0);
405 /* Call this when -I is seen on the command line. */
407 jcf_path_include_arg (path
)
410 add_entry (&include_dirs
, path
, 0);
413 /* We `seal' the path by linking everything into one big list. Then
414 we provide a way to iterate through the sealed list. If PRINT is
415 true then we print the final class path to stderr. */
417 jcf_path_seal (print
)
420 struct entry
*secondary
;
422 sealed
= include_dirs
;
427 secondary
= classpath_user
;
428 classpath_user
= NULL
;
433 add_entry (&classpath_env
, ".", 0);
435 secondary
= classpath_env
;
436 classpath_env
= NULL
;
440 free_entry (&classpath_user
);
441 free_entry (&classpath_env
);
443 append_entry (&sealed
, secondary
);
444 append_entry (&sealed
, sys_dirs
);
445 append_entry (&sealed
, extensions
);
452 fprintf (stderr
, "Class path starts here:\n");
453 for (ent
= sealed
; ent
; ent
= ent
->next
)
455 fprintf (stderr
, " %s", ent
->name
);
456 if ((ent
->flags
& FLAG_SYSTEM
))
457 fprintf (stderr
, " (system)");
458 if ((ent
->flags
& FLAG_ZIP
))
459 fprintf (stderr
, " (zip)");
460 fprintf (stderr
, "\n");
468 return (void *) sealed
;
475 struct entry
*ent
= (struct entry
*) x
;
476 return (void *) ent
->next
;
479 /* We guarantee that the return path will either be a zip file, or it
480 will end with a directory separator. */
485 struct entry
*ent
= (struct entry
*) x
;
490 jcf_path_is_zipfile (x
)
493 struct entry
*ent
= (struct entry
*) x
;
494 return (ent
->flags
& FLAG_ZIP
);
498 jcf_path_is_system (x
)
501 struct entry
*ent
= (struct entry
*) x
;
502 return (ent
->flags
& FLAG_SYSTEM
);