2014-04-14 Martin Jambor <mjambor@suse.cz>
[official-gcc.git] / gcc / java / jcf-path.c
blob026bcb851606de7eded8fc5b3d8fff7f11a35259
1 /* Handle CLASSPATH, -classpath, and path searching.
2 Copyright (C) 1998-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>.
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. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
30 #include <dirent.h>
32 #include "jcf.h"
34 #ifndef DIR_UP
35 #define DIR_UP ".."
36 #endif
40 /* Possible flag values. */
41 #define FLAG_SYSTEM 1
42 #define FLAG_ZIP 2
44 /* We keep linked lists of directory names. A ``directory'' can be
45 either an ordinary directory or a .zip file. */
46 struct entry
48 char *name;
49 int flags;
50 struct entry *next;
53 static void free_entry (struct entry **);
54 static void append_entry (struct entry **, struct entry *);
55 static void add_entry (struct entry **, const char *, int);
56 static void add_path (struct entry **, const char *, int);
58 /* We support several different ways to set the class path.
60 built-in system directory (only libgcj.jar)
61 CLASSPATH environment variable
62 -classpath option overrides $CLASSPATH
63 -CLASSPATH option is a synonym for -classpath (for compatibility)
64 -bootclasspath overrides built-in
65 -extdirs sets the extensions directory path (overrides built-in)
66 -I prepends path to list
68 We implement this by keeping several path lists, and then simply
69 ignoring the ones which are not relevant. */
71 /* This holds all the -I directories. */
72 static struct entry *include_dirs;
74 /* This holds the CLASSPATH environment variable. */
75 static struct entry *classpath_env;
77 /* This holds the -classpath command-line option. */
78 static struct entry *classpath_user;
80 /* This holds the default directories. Some of these will have the
81 "system" flag set. */
82 static struct entry *sys_dirs;
84 /* This holds the extensions path entries. */
85 static struct entry *extensions;
87 /* This is the sealed list. It is just a combination of other lists. */
88 static struct entry *sealed;
90 /* We keep track of the longest path we've seen. */
91 static int longest_path = 0;
95 static void
96 free_entry (struct entry **entp)
98 struct entry *e, *n;
100 for (e = *entp; e; e = n)
102 n = e->next;
103 free (e->name);
104 free (e);
106 *entp = NULL;
109 static void
110 append_entry (struct entry **entp, struct entry *ent)
112 /* It doesn't matter if this is slow, since it is run only at
113 startup, and then infrequently. */
114 struct entry *e;
116 /* Find end of list. */
117 for (e = *entp; e && e->next; e = e->next)
120 if (e)
121 e->next = ent;
122 else
123 *entp = ent;
126 static void
127 add_entry (struct entry **entp, const char *filename, int is_system)
129 int len;
130 struct entry *n;
132 n = XNEW (struct entry);
133 n->flags = is_system ? FLAG_SYSTEM : 0;
134 n->next = NULL;
136 len = strlen (filename);
138 if (len > 4 && (FILENAME_CMP (filename + len - 4, ".zip") == 0
139 || FILENAME_CMP (filename + len - 4, ".jar") == 0))
141 n->flags |= FLAG_ZIP;
142 /* If the user uses -classpath then he'll have to include
143 libgcj.jar in the value. We check for this in a simplistic
144 way. Symlinks will fool this test. This is only used for
145 -MM and -MMD, so it probably isn't terribly important. */
146 if (! FILENAME_CMP (filename, LIBGCJ_ZIP_FILE))
147 n->flags |= FLAG_SYSTEM;
150 /* Note that we add a trailing separator to `.zip' names as well.
151 This is a little hack that lets the searching code in jcf-io.c
152 work more easily. Eww. */
153 if (! IS_DIR_SEPARATOR (filename[len - 1]))
155 char *f2 = (char *) alloca (len + 2);
156 strcpy (f2, filename);
157 f2[len] = DIR_SEPARATOR;
158 f2[len + 1] = '\0';
159 n->name = xstrdup (f2);
160 ++len;
162 else
163 n->name = xstrdup (filename);
165 if (len > longest_path)
166 longest_path = len;
168 append_entry (entp, n);
171 static void
172 add_path (struct entry **entp, const char *cp, int is_system)
174 const char *startp, *endp;
176 if (cp)
178 char *buf = (char *) alloca (strlen (cp) + 3);
179 startp = endp = cp;
180 while (1)
182 if (! *endp || *endp == PATH_SEPARATOR)
184 if (endp == startp)
186 buf[0] = '.';
187 buf[1] = DIR_SEPARATOR;
188 buf[2] = '\0';
190 else
192 strncpy (buf, startp, endp - startp);
193 buf[endp - startp] = '\0';
195 add_entry (entp, buf, is_system);
196 if (! *endp)
197 break;
198 ++endp;
199 startp = endp;
201 else
202 ++endp;
207 static int init_done = 0;
209 /* Initialize the path module. */
210 void
211 jcf_path_init (void)
213 char *cp;
214 char *attempt, sep[2];
215 struct stat stat_b;
216 int found = 0, len;
218 if (init_done)
219 return;
220 init_done = 1;
222 sep[0] = DIR_SEPARATOR;
223 sep[1] = '\0';
225 cp = getenv ("GCC_EXEC_PREFIX");
226 if (cp)
228 attempt = (char *) alloca (strlen (cp) + 50);
229 /* The exec prefix can be something like
230 /usr/local/bin/../lib/gcc-lib/. We want to change this
231 into a pointer to the share/java directory. We support two
232 configurations: one where prefix and exec-prefix are the
233 same, and one where exec-prefix is `prefix/SOMETHING'. */
234 strcpy (attempt, cp);
235 strcat (attempt, DIR_UP);
236 strcat (attempt, sep);
237 strcat (attempt, DIR_UP);
238 strcat (attempt, sep);
239 len = strlen (attempt);
241 strcpy (attempt + len, "share");
242 strcat (attempt, sep);
243 strcat (attempt, "java");
244 strcat (attempt, sep);
245 strcat (attempt, "libgcj-" DEFAULT_TARGET_VERSION ".jar");
246 if (! stat (attempt, &stat_b))
248 add_entry (&sys_dirs, attempt, 1);
249 found = 1;
250 strcpy (&attempt[strlen (attempt)
251 - strlen ("libgcj-" DEFAULT_TARGET_VERSION ".jar")],
252 sep);
253 strcat (attempt, "ext");
254 strcat (attempt, sep);
255 if (! stat (attempt, &stat_b))
256 jcf_path_extdirs_arg (attempt);
258 else
260 strcpy (attempt + len, DIR_UP);
261 strcat (attempt, sep);
262 strcat (attempt, "share");
263 strcat (attempt, sep);
264 strcat (attempt, "java");
265 strcat (attempt, sep);
266 strcat (attempt, "libgcj-" DEFAULT_TARGET_VERSION ".jar");
267 if (! stat (attempt, &stat_b))
269 add_entry (&sys_dirs, attempt, 1);
270 found = 1;
271 strcpy (&attempt[strlen (attempt)
272 - strlen ("libgcj-" DEFAULT_TARGET_VERSION ".jar")],
273 sep);
274 strcat (attempt, "ext");
275 strcat (attempt, sep);
276 if (! stat (attempt, &stat_b))
277 jcf_path_extdirs_arg (attempt);
281 if (! found)
283 /* Desperation: use the installed one. */
284 char *extdirs;
285 add_entry (&sys_dirs, LIBGCJ_ZIP_FILE, 1);
286 extdirs = (char *) alloca (strlen (LIBGCJ_ZIP_FILE) + 1);
287 strcpy (extdirs, LIBGCJ_ZIP_FILE);
288 strcpy (&extdirs[strlen (LIBGCJ_ZIP_FILE)
289 - strlen ("libgcj-" DEFAULT_TARGET_VERSION ".jar")],
290 "ext");
291 strcat (extdirs, sep);
292 if (! stat (extdirs, &stat_b))
293 jcf_path_extdirs_arg (extdirs);
296 cp = getenv ("CLASSPATH");
297 add_path (&classpath_env, cp, 0);
300 /* Call this when -classpath is seen on the command line.
301 This overrides only the $CLASSPATH environment variable.
303 void
304 jcf_path_classpath_arg (const char *path)
306 free_entry (&classpath_user);
307 add_path (&classpath_user, path, 0);
310 /* Call this when -bootclasspath is seen on the command line.
312 void
313 jcf_path_bootclasspath_arg (const char *path)
315 free_entry (&sys_dirs);
316 add_path (&sys_dirs, path, 1);
319 /* Call this when -extdirs is seen on the command line.
321 void
322 jcf_path_extdirs_arg (const char *cp)
324 const char *startp, *endp;
326 free_entry (&extensions);
328 if (cp)
330 char *buf = (char *) alloca (strlen (cp) + 3);
331 startp = endp = cp;
332 while (1)
334 if (! *endp || *endp == PATH_SEPARATOR)
336 if (endp == startp)
337 return;
339 strncpy (buf, startp, endp - startp);
340 buf[endp - startp] = '\0';
343 DIR *dirp = NULL;
344 int dirname_length = strlen (buf);
346 dirp = opendir (buf);
347 if (dirp == NULL)
348 return;
350 for (;;)
352 struct dirent *direntp = readdir (dirp);
354 if (!direntp)
355 break;
357 if (direntp->d_name[0] != '.')
359 char *name = (char *) alloca (dirname_length
360 + strlen (direntp->d_name) + 2);
361 strcpy (name, buf);
362 if (! IS_DIR_SEPARATOR (name[dirname_length-1]))
364 name[dirname_length] = DIR_SEPARATOR;
365 name[dirname_length+1] = 0;
367 strcat (name, direntp->d_name);
368 add_entry (&extensions, name, 0);
371 if (dirp)
372 closedir (dirp);
375 if (! *endp)
376 break;
377 ++endp;
378 startp = endp;
380 else
381 ++endp;
386 /* Call this when -I is seen on the command line. */
387 void
388 jcf_path_include_arg (const char *path)
390 add_entry (&include_dirs, path, 0);
393 /* We `seal' the path by linking everything into one big list. Then
394 we provide a way to iterate through the sealed list. If PRINT is
395 true then we print the final class path to stderr. */
396 void
397 jcf_path_seal (int print)
399 struct entry *secondary;
401 sealed = include_dirs;
402 include_dirs = NULL;
404 if (classpath_user)
406 secondary = classpath_user;
407 classpath_user = NULL;
409 else
411 if (! classpath_env)
412 add_entry (&classpath_env, ".", 0);
414 secondary = classpath_env;
415 classpath_env = NULL;
419 free_entry (&classpath_user);
420 free_entry (&classpath_env);
422 append_entry (&sealed, secondary);
423 append_entry (&sealed, sys_dirs);
424 append_entry (&sealed, extensions);
425 sys_dirs = NULL;
426 extensions = NULL;
428 if (print)
430 struct entry *ent;
431 fprintf (stderr, "Class path starts here:\n");
432 for (ent = sealed; ent; ent = ent->next)
434 fprintf (stderr, " %s", ent->name);
435 if ((ent->flags & FLAG_SYSTEM))
436 fprintf (stderr, " (system)");
437 if ((ent->flags & FLAG_ZIP))
438 fprintf (stderr, " (zip)");
439 fprintf (stderr, "\n");
444 void *
445 jcf_path_start (void)
447 return (void *) sealed;
450 void *
451 jcf_path_next (void *x)
453 struct entry *ent = (struct entry *) x;
454 return (void *) ent->next;
457 static const char
458 PATH_SEPARATOR_STR[] = {PATH_SEPARATOR, '\0'};
460 char *
461 jcf_path_compute (const char *prefix)
463 struct entry *iter;
464 char *result;
465 int length = strlen (prefix) + 1;
466 int first;
468 for (iter = sealed; iter != NULL; iter = iter->next)
469 length += strlen (iter->name) + 1;
471 result = (char *) xmalloc (length);
472 strcpy (result, prefix);
473 first = 1;
474 for (iter = sealed; iter != NULL; iter = iter->next)
476 if (! first)
477 strcat (result, PATH_SEPARATOR_STR);
478 first = 0;
479 strcat (result, iter->name);
480 /* Ugly: we want to strip the '/' from zip entries when
481 computing a string classpath. */
482 if ((iter->flags & FLAG_ZIP) != 0)
483 result[strlen (result) - 1] = '\0';
486 return result;
489 /* We guarantee that the return path will either be a zip file, or it
490 will end with a directory separator. */
491 char *
492 jcf_path_name (void *x)
494 struct entry *ent = (struct entry *) x;
495 return ent->name;
499 jcf_path_is_zipfile (void *x)
501 struct entry *ent = (struct entry *) x;
502 return (ent->flags & FLAG_ZIP);
506 jcf_path_is_system (void *x)
508 struct entry *ent = (struct entry *) x;
509 return (ent->flags & FLAG_SYSTEM);
513 jcf_path_max_len (void)
515 return longest_path;