re PR target/8343 ([m68k] [3.2 regression] m68k-elf/rtems ICE at instantiate_virtual_...
[official-gcc.git] / gcc / java / jcf-path.c
blobddb31a53b65f756204bbd3e4730852cbb4819dbf
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 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 -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 (entp)
109 struct entry **entp;
111 struct entry *e, *n;
113 for (e = *entp; e; e = n)
115 n = e->next;
116 free (e->name);
117 free (e);
119 *entp = NULL;
122 static void
123 append_entry (entp, ent)
124 struct entry **entp;
125 struct entry *ent;
127 /* It doesn't matter if this is slow, since it is run only at
128 startup, and then infrequently. */
129 struct entry *e;
131 /* Find end of list. */
132 for (e = *entp; e && e->next; e = e->next)
135 if (e)
136 e->next = ent;
137 else
138 *entp = ent;
141 static void
142 add_entry (entp, filename, is_system)
143 struct entry **entp;
144 const char *filename;
145 int is_system;
147 int len;
148 struct entry *n;
150 n = ALLOC (sizeof (struct entry));
151 n->flags = is_system ? FLAG_SYSTEM : 0;
152 n->next = NULL;
154 len = strlen (filename);
155 if (len > 4 && (strcmp (filename + len - 4, ".zip") == 0
156 || strcmp (filename + len - 4, ".jar") == 0))
158 n->flags |= FLAG_ZIP;
159 /* If the user uses -classpath then he'll have to include
160 libgcj.jar in the value. We check for this in a simplistic
161 way. Symlinks will fool this test. This is only used for
162 -MM and -MMD, so it probably isn't terribly important. */
163 if (! strcmp (filename, LIBGCJ_ZIP_FILE))
164 n->flags |= FLAG_SYSTEM;
167 /* Note that we add a trailing separator to `.zip' names as well.
168 This is a little hack that lets the searching code in jcf-io.c
169 work more easily. Eww. */
170 if (filename[len - 1] != '/' && filename[len - 1] != DIR_SEPARATOR)
172 char *f2 = alloca (len + 2);
173 strcpy (f2, filename);
174 f2[len] = DIR_SEPARATOR;
175 f2[len + 1] = '\0';
176 n->name = xstrdup (f2);
177 ++len;
179 else
180 n->name = xstrdup (filename);
182 if (len > longest_path)
183 longest_path = len;
185 append_entry (entp, n);
188 static void
189 add_path (entp, cp, is_system)
190 struct entry **entp;
191 const char *cp;
192 int is_system;
194 const char *startp, *endp;
196 if (cp)
198 char *buf = alloca (strlen (cp) + 3);
199 startp = endp = cp;
200 while (1)
202 if (! *endp || *endp == PATH_SEPARATOR)
204 if (endp == startp)
206 buf[0] = '.';
207 buf[1] = DIR_SEPARATOR;
208 buf[2] = '\0';
210 else
212 strncpy (buf, startp, endp - startp);
213 buf[endp - startp] = '\0';
215 add_entry (entp, buf, is_system);
216 if (! *endp)
217 break;
218 ++endp;
219 startp = endp;
221 else
222 ++endp;
227 static int init_done = 0;
229 /* Initialize the path module. */
230 void
231 jcf_path_init ()
233 char *cp;
234 char *try, sep[2];
235 struct stat stat_b;
236 int found = 0, len;
238 if (init_done)
239 return;
240 init_done = 1;
242 sep[0] = DIR_SEPARATOR;
243 sep[1] = '\0';
245 GET_ENVIRONMENT (cp, "GCC_EXEC_PREFIX");
246 if (cp)
248 try = alloca (strlen (cp) + 50);
249 /* The exec prefix can be something like
250 /usr/local/bin/../lib/gcc-lib/. We want to change this
251 into a pointer to the share/java directory. We support two
252 configurations: one where prefix and exec-prefix are the
253 same, and one where exec-prefix is `prefix/SOMETHING'. */
254 strcpy (try, cp);
255 strcat (try, DIR_UP);
256 strcat (try, sep);
257 strcat (try, DIR_UP);
258 strcat (try, sep);
259 len = strlen (try);
261 strcpy (try + len, "share");
262 strcat (try, sep);
263 strcat (try, "java");
264 strcat (try, sep);
265 strcat (try, "libgcj-" DEFAULT_TARGET_VERSION ".jar");
266 if (! stat (try, &stat_b))
268 add_entry (&sys_dirs, try, 1);
269 found = 1;
270 strcpy (&try[strlen (try)
271 - strlen ("libgcj-" DEFAULT_TARGET_VERSION ".jar")],
272 sep);
273 strcat (try, "ext");
274 strcat (try, sep);
275 if (! stat (try, &stat_b))
276 jcf_path_extdirs_arg (try);
278 else
280 strcpy (try + len, DIR_UP);
281 strcat (try, sep);
282 strcat (try, "share");
283 strcat (try, sep);
284 strcat (try, "java");
285 strcat (try, sep);
286 strcat (try, "libgcj-" DEFAULT_TARGET_VERSION ".jar");
287 if (! stat (try, &stat_b))
289 add_entry (&sys_dirs, try, 1);
290 found = 1;
291 strcpy (&try[strlen (try)
292 - strlen ("libgcj-" DEFAULT_TARGET_VERSION ".jar")],
293 sep);
294 strcat (try, "ext");
295 strcat (try, sep);
296 if (! stat (try, &stat_b))
297 jcf_path_extdirs_arg (try);
301 if (! found)
303 /* Desperation: use the installed one. */
304 char *extdirs;
305 add_entry (&sys_dirs, LIBGCJ_ZIP_FILE, 1);
306 extdirs = alloca (strlen (LIBGCJ_ZIP_FILE) + 1);
307 strcpy (extdirs, LIBGCJ_ZIP_FILE);
308 strcpy (&extdirs[strlen (LIBGCJ_ZIP_FILE)
309 - strlen ("libgcj-" DEFAULT_TARGET_VERSION ".jar")],
310 "ext");
311 strcat (extdirs, sep);
312 if (! stat (extdirs, &stat_b))
313 jcf_path_extdirs_arg (extdirs);
316 GET_ENVIRONMENT (cp, "CLASSPATH");
317 add_path (&classpath_env, cp, 0);
320 /* Call this when -classpath is seen on the command line.
321 This overrides only the $CLASSPATH environment variable.
323 void
324 jcf_path_classpath_arg (path)
325 const char *path;
327 free_entry (&classpath_user);
328 add_path (&classpath_user, path, 0);
331 /* Call this when -bootclasspath is seen on the command line.
333 void
334 jcf_path_bootclasspath_arg (path)
335 const char *path;
337 free_entry (&sys_dirs);
338 add_path (&sys_dirs, path, 1);
341 /* Call this when -extdirs is seen on the command line.
343 void
344 jcf_path_extdirs_arg (cp)
345 const char *cp;
347 const char *startp, *endp;
349 free_entry (&extensions);
351 if (cp)
353 char *buf = alloca (strlen (cp) + 3);
354 startp = endp = cp;
355 while (1)
357 if (! *endp || *endp == PATH_SEPARATOR)
359 if (endp == startp)
360 return;
362 strncpy (buf, startp, endp - startp);
363 buf[endp - startp] = '\0';
366 DIR *dirp = NULL;
367 int dirname_length = strlen (buf);
369 dirp = opendir (buf);
370 if (dirp == NULL)
371 return;
373 for (;;)
375 struct dirent *direntp = readdir (dirp);
377 if (!direntp)
378 break;
380 if (direntp->d_name[0] != '.')
382 char *name = alloca (dirname_length
383 + strlen (direntp->d_name) + 2);
384 strcpy (name, buf);
385 if (name[dirname_length-1] != DIR_SEPARATOR)
387 name[dirname_length] = DIR_SEPARATOR;
388 name[dirname_length+1] = 0;
390 strcat (name, direntp->d_name);
391 add_entry (&extensions, name, 0);
396 if (! *endp)
397 break;
398 ++endp;
399 startp = endp;
401 else
402 ++endp;
407 /* Call this when -I is seen on the command line. */
408 void
409 jcf_path_include_arg (path)
410 const char *path;
412 add_entry (&include_dirs, path, 0);
415 /* We `seal' the path by linking everything into one big list. Then
416 we provide a way to iterate through the sealed list. If PRINT is
417 true then we print the final class path to stderr. */
418 void
419 jcf_path_seal (print)
420 int print;
422 struct entry *secondary;
424 sealed = include_dirs;
425 include_dirs = NULL;
427 if (classpath_user)
429 secondary = classpath_user;
430 classpath_user = NULL;
432 else
434 if (! classpath_env)
435 add_entry (&classpath_env, ".", 0);
437 secondary = classpath_env;
438 classpath_env = NULL;
442 free_entry (&classpath_user);
443 free_entry (&classpath_env);
445 append_entry (&sealed, secondary);
446 append_entry (&sealed, sys_dirs);
447 append_entry (&sealed, extensions);
448 sys_dirs = NULL;
449 extensions = NULL;
451 if (print)
453 struct entry *ent;
454 fprintf (stderr, "Class path starts here:\n");
455 for (ent = sealed; ent; ent = ent->next)
457 fprintf (stderr, " %s", ent->name);
458 if ((ent->flags & FLAG_SYSTEM))
459 fprintf (stderr, " (system)");
460 if ((ent->flags & FLAG_ZIP))
461 fprintf (stderr, " (zip)");
462 fprintf (stderr, "\n");
467 void *
468 jcf_path_start ()
470 return (void *) sealed;
473 void *
474 jcf_path_next (x)
475 void *x;
477 struct entry *ent = (struct entry *) x;
478 return (void *) ent->next;
481 /* We guarantee that the return path will either be a zip file, or it
482 will end with a directory separator. */
483 char *
484 jcf_path_name (x)
485 void *x;
487 struct entry *ent = (struct entry *) x;
488 return ent->name;
492 jcf_path_is_zipfile (x)
493 void *x;
495 struct entry *ent = (struct entry *) x;
496 return (ent->flags & FLAG_ZIP);
500 jcf_path_is_system (x)
501 void *x;
503 struct entry *ent = (struct entry *) x;
504 return (ent->flags & FLAG_SYSTEM);
508 jcf_path_max_len ()
510 return longest_path;