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. */
31 /* Some boilerplate that really belongs in a header. */
33 #ifndef GET_ENV_PATH_LIST
34 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
37 /* By default, colon separates directories in a path. */
38 #ifndef PATH_SEPARATOR
39 #define PATH_SEPARATOR ':'
43 #define DIR_SEPARATOR '/'
52 /* Possible flag values. */
56 /* We keep linked lists of directory names. A ``directory'' can be
57 either an ordinary directory or a .zip file. */
65 static void free_entry PARAMS ((struct entry **));
66 static void append_entry PARAMS ((struct entry **, struct entry *));
67 static void add_entry PARAMS ((struct entry **, const char *, int));
68 static void add_path PARAMS ((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 -I prepends path to list
79 We implement this by keeping several path lists, and then simply
80 ignoring the ones which are not relevant. */
82 /* This holds all the -I directories. */
83 static struct entry *include_dirs;
85 /* This holds the CLASSPATH environment variable. */
86 static struct entry *classpath_env;
88 /* This holds the -classpath command-line option. */
89 static struct entry *classpath_user;
91 /* This holds the default directories. Some of these will have the
93 static struct entry *sys_dirs;
95 /* This is the sealed list. It is just a combination of other lists. */
96 static struct entry *sealed;
98 /* We keep track of the longest path we've seen. */
99 static int longest_path = 0;
109 for (e = *entp; e; e = n)
119 append_entry (entp, ent)
123 /* It doesn't matter if this is slow, since it is run only at
124 startup, and then infrequently. */
127 /* Find end of list. */
128 for (e = *entp; e && e->next; e = e->next)
138 add_entry (entp, filename, is_system)
140 const char *filename;
146 n = (struct entry *) ALLOC (sizeof (struct entry));
147 n->flags = is_system ? FLAG_SYSTEM : 0;
150 len = strlen (filename);
151 if (len > 4 && (strcmp (filename + len - 4, ".zip") == 0
152 || strcmp (filename + len - 4, ".jar") == 0))
154 n->flags |= FLAG_ZIP;
155 /* If the user uses -classpath then he'll have to include
156 libgcj.jar in the value. We check for this in a simplistic
157 way. Symlinks will fool this test. This is only used for
158 -MM and -MMD, so it probably isn't terribly important. */
159 if (! strcmp (filename, LIBGCJ_ZIP_FILE))
160 n->flags |= FLAG_SYSTEM;
163 /* Note that we add a trailing separator to `.zip' names as well.
164 This is a little hack that lets the searching code in jcf-io.c
165 work more easily. Eww. */
166 if (filename[len - 1] != '/' && filename[len - 1] != DIR_SEPARATOR)
168 char *f2 = (char *) alloca (len + 2);
169 strcpy (f2, filename);
170 f2[len] = DIR_SEPARATOR;
172 n->name = xstrdup (f2);
176 n->name = xstrdup (filename);
178 if (len > longest_path)
181 append_entry (entp, n);
185 add_path (entp, cp, is_system)
190 const char *startp, *endp;
194 char *buf = (char *) alloca (strlen (cp) + 3);
198 if (! *endp || *endp == PATH_SEPARATOR)
203 buf[1] = DIR_SEPARATOR;
208 strncpy (buf, startp, endp - startp);
209 buf[endp - startp] = '\0';
211 add_entry (entp, buf, is_system);
223 static int init_done = 0;
225 /* Initialize the path module. */
238 sep[0] = DIR_SEPARATOR;
241 GET_ENV_PATH_LIST (cp, "GCC_EXEC_PREFIX");
244 try = alloca (strlen (cp) + 50);
245 /* The exec prefix can be something like
246 /usr/local/bin/../lib/gcc-lib/. We want to change this
247 into a pointer to the share directory. We support two
248 configurations: one where prefix and exec-prefix are the
249 same, and one where exec-prefix is `prefix/SOMETHING'. */
251 strcat (try, DIR_UP);
253 strcat (try, DIR_UP);
257 strcpy (try + len, "share");
259 strcat (try, "libgcj.jar");
260 if (! stat (try, &stat_b))
262 add_entry (&sys_dirs, try, 1);
267 strcpy (try + len, DIR_UP);
269 strcat (try, "share");
271 strcat (try, "libgcj.jar");
272 if (! stat (try, &stat_b))
274 add_entry (&sys_dirs, try, 1);
281 /* Desperation: use the installed one. */
282 add_entry (&sys_dirs, LIBGCJ_ZIP_FILE, 1);
285 GET_ENV_PATH_LIST (cp, "CLASSPATH");
286 add_path (&classpath_env, cp, 0);
289 /* Call this when -classpath is seen on the command line.
290 This overrides only the $CLASSPATH environment variable.
293 jcf_path_classpath_arg (path)
296 free_entry (&classpath_user);
297 add_path (&classpath_user, path, 0);
300 /* Call this when -bootclasspath is seen on the command line.
303 jcf_path_bootclasspath_arg (path)
306 free_entry (&sys_dirs);
307 add_path (&sys_dirs, path, 1);
310 /* Call this when -I is seen on the command line. */
312 jcf_path_include_arg (path)
315 add_entry (&include_dirs, path, 0);
318 /* We `seal' the path by linking everything into one big list. Then
319 we provide a way to iterate through the sealed list. If PRINT is
320 true then we print the final class path to stderr. */
322 jcf_path_seal (print)
325 struct entry *secondary;
327 sealed = include_dirs;
332 secondary = classpath_user;
333 classpath_user = NULL;
338 add_entry (&classpath_env, ".", 0);
340 secondary = classpath_env;
341 classpath_env = NULL;
345 free_entry (&classpath_user);
346 free_entry (&classpath_env);
348 append_entry (&sealed, secondary);
349 append_entry (&sealed, sys_dirs);
355 fprintf (stderr, "Class path starts here:\n");
356 for (ent = sealed; ent; ent = ent->next)
358 fprintf (stderr, " %s", ent->name);
359 if ((ent->flags & FLAG_SYSTEM))
360 fprintf (stderr, " (system)");
361 if ((ent->flags & FLAG_ZIP))
362 fprintf (stderr, " (zip)");
363 fprintf (stderr, "\n");
371 return (void *) sealed;
378 struct entry *ent = (struct entry *) x;
379 return (void *) ent->next;
382 /* We guarantee that the return path will either be a zip file, or it
383 will end with a directory separator. */
388 struct entry *ent = (struct entry *) x;
393 jcf_path_is_zipfile (x)
396 struct entry *ent = (struct entry *) x;
397 return (ent->flags & FLAG_ZIP);
401 jcf_path_is_system (x)
404 struct entry *ent = (struct entry *) x;
405 return (ent->flags & FLAG_SYSTEM);