jcf-path.c (jcf_path_init): Allocate 1 more byte for string.
[official-gcc.git] / gcc / java / jcf-path.c
blobb68a538f45b7ba73881c3316335dfadd94a02dc3
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)
8 any later version.
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. */
26 #include "config.h"
27 #include "system.h"
29 #include <dirent.h>
31 #include "jcf.h"
33 /* Some boilerplate that really belongs in a header. */
35 #ifndef GET_ENV_PATH_LIST
36 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
37 #endif
39 /* By default, colon separates directories in a path. */
40 #ifndef PATH_SEPARATOR
41 #define PATH_SEPARATOR ':'
42 #endif
44 #ifndef DIR_SEPARATOR
45 #define DIR_SEPARATOR '/'
46 #endif
48 #ifndef DIR_UP
49 #define DIR_UP ".."
50 #endif
54 /* Possible flag values. */
55 #define FLAG_SYSTEM 1
56 #define FLAG_ZIP 2
58 /* We keep linked lists of directory names. A ``directory'' can be
59 either an ordinary directory or a .zip file. */
60 struct entry
62 char *name;
63 int flags;
64 struct entry *next;
67 static void free_entry PARAMS ((struct entry **));
68 static void append_entry PARAMS ((struct entry **, struct entry *));
69 static void add_entry PARAMS ((struct entry **, const char *, int));
70 static void add_path PARAMS ((struct entry **, const char *, int));
72 /* We support several different ways to set the class path.
74 built-in system directory (only libgcj.jar)
75 CLASSPATH environment variable
76 -classpath option overrides $CLASSPATH
77 -CLASSPATH option is a synonym for -classpath (for compatibility)
78 -bootclasspath overrides built-in
79 -extdirs sets the extensions directory path (overrides built-in)
80 -I prepends path to list
82 We implement this by keeping several path lists, and then simply
83 ignoring the ones which are not relevant. */
85 /* This holds all the -I directories. */
86 static struct entry *include_dirs;
88 /* This holds the CLASSPATH environment variable. */
89 static struct entry *classpath_env;
91 /* This holds the -classpath command-line option. */
92 static struct entry *classpath_user;
94 /* This holds the default directories. Some of these will have the
95 "system" flag set. */
96 static struct entry *sys_dirs;
98 /* This holds the extensions path entries. */
99 static struct entry *extensions;
101 /* This is the sealed list. It is just a combination of other lists. */
102 static struct entry *sealed;
104 /* We keep track of the longest path we've seen. */
105 static int longest_path = 0;
109 static void
110 free_entry (entp)
111 struct entry **entp;
113 struct entry *e, *n;
115 for (e = *entp; e; e = n)
117 n = e->next;
118 free (e->name);
119 free (e);
121 *entp = NULL;
124 static void
125 append_entry (entp, ent)
126 struct entry **entp;
127 struct entry *ent;
129 /* It doesn't matter if this is slow, since it is run only at
130 startup, and then infrequently. */
131 struct entry *e;
133 /* Find end of list. */
134 for (e = *entp; e && e->next; e = e->next)
137 if (e)
138 e->next = ent;
139 else
140 *entp = ent;
143 static void
144 add_entry (entp, filename, is_system)
145 struct entry **entp;
146 const char *filename;
147 int is_system;
149 int len;
150 struct entry *n;
152 n = (struct entry *) ALLOC (sizeof (struct entry));
153 n->flags = is_system ? FLAG_SYSTEM : 0;
154 n->next = NULL;
156 len = strlen (filename);
157 if (len > 4 && (strcmp (filename + len - 4, ".zip") == 0
158 || strcmp (filename + len - 4, ".jar") == 0))
160 n->flags |= FLAG_ZIP;
161 /* If the user uses -classpath then he'll have to include
162 libgcj.jar in the value. We check for this in a simplistic
163 way. Symlinks will fool this test. This is only used for
164 -MM and -MMD, so it probably isn't terribly important. */
165 if (! strcmp (filename, LIBGCJ_ZIP_FILE))
166 n->flags |= FLAG_SYSTEM;
169 /* Note that we add a trailing separator to `.zip' names as well.
170 This is a little hack that lets the searching code in jcf-io.c
171 work more easily. Eww. */
172 if (filename[len - 1] != '/' && filename[len - 1] != DIR_SEPARATOR)
174 char *f2 = (char *) alloca (len + 2);
175 strcpy (f2, filename);
176 f2[len] = DIR_SEPARATOR;
177 f2[len + 1] = '\0';
178 n->name = xstrdup (f2);
179 ++len;
181 else
182 n->name = xstrdup (filename);
184 if (len > longest_path)
185 longest_path = len;
187 append_entry (entp, n);
190 static void
191 add_path (entp, cp, is_system)
192 struct entry **entp;
193 const char *cp;
194 int is_system;
196 const char *startp, *endp;
198 if (cp)
200 char *buf = (char *) alloca (strlen (cp) + 3);
201 startp = endp = cp;
202 while (1)
204 if (! *endp || *endp == PATH_SEPARATOR)
206 if (endp == startp)
208 buf[0] = '.';
209 buf[1] = DIR_SEPARATOR;
210 buf[2] = '\0';
212 else
214 strncpy (buf, startp, endp - startp);
215 buf[endp - startp] = '\0';
217 add_entry (entp, buf, is_system);
218 if (! *endp)
219 break;
220 ++endp;
221 startp = endp;
223 else
224 ++endp;
229 static int init_done = 0;
231 /* Initialize the path module. */
232 void
233 jcf_path_init ()
235 char *cp;
236 char *try, sep[2];
237 struct stat stat_b;
238 int found = 0, len;
240 if (init_done)
241 return;
242 init_done = 1;
244 sep[0] = DIR_SEPARATOR;
245 sep[1] = '\0';
247 GET_ENV_PATH_LIST (cp, "GCC_EXEC_PREFIX");
248 if (cp)
250 try = alloca (strlen (cp) + 50);
251 /* The exec prefix can be something like
252 /usr/local/bin/../lib/gcc-lib/. We want to change this
253 into a pointer to the share/java directory. We support two
254 configurations: one where prefix and exec-prefix are the
255 same, and one where exec-prefix is `prefix/SOMETHING'. */
256 strcpy (try, cp);
257 strcat (try, DIR_UP);
258 strcat (try, sep);
259 strcat (try, DIR_UP);
260 strcat (try, sep);
261 len = strlen (try);
263 strcpy (try + len, "share");
264 strcat (try, sep);
265 strcat (try, "java");
266 strcat (try, sep);
267 strcat (try, "libgcj-" DEFAULT_TARGET_VERSION ".jar");
268 if (! stat (try, &stat_b))
270 add_entry (&sys_dirs, try, 1);
271 found = 1;
272 strcpy (&try[strlen (try)
273 - strlen ("libgcj-" DEFAULT_TARGET_VERSION ".jar")],
274 sep);
275 strcat (try, "ext");
276 strcat (try, sep);
277 if (! stat (try, &stat_b))
278 jcf_path_extdirs_arg (try);
280 else
282 strcpy (try + len, DIR_UP);
283 strcat (try, sep);
284 strcat (try, "share");
285 strcat (try, sep);
286 strcat (try, "java");
287 strcat (try, sep);
288 strcat (try, "libgcj-" DEFAULT_TARGET_VERSION ".jar");
289 if (! stat (try, &stat_b))
291 add_entry (&sys_dirs, try, 1);
292 found = 1;
293 strcpy (&try[strlen (try)
294 - strlen ("libgcj-" DEFAULT_TARGET_VERSION ".jar")],
295 sep);
296 strcat (try, "ext");
297 strcat (try, sep);
298 if (! stat (try, &stat_b))
299 jcf_path_extdirs_arg (try);
303 if (! found)
305 /* Desperation: use the installed one. */
306 char *extdirs;
307 add_entry (&sys_dirs, LIBGCJ_ZIP_FILE, 1);
308 extdirs = (char *) alloca (strlen (LIBGCJ_ZIP_FILE) + 1);
309 strcpy (extdirs, LIBGCJ_ZIP_FILE);
310 strcpy (&extdirs[strlen (LIBGCJ_ZIP_FILE)
311 - strlen ("libgcj-" DEFAULT_TARGET_VERSION ".jar")],
312 "ext");
313 strcat (extdirs, sep);
314 if (! stat (extdirs, &stat_b))
315 jcf_path_extdirs_arg (extdirs);
318 GET_ENV_PATH_LIST (cp, "CLASSPATH");
319 add_path (&classpath_env, cp, 0);
322 /* Call this when -classpath is seen on the command line.
323 This overrides only the $CLASSPATH environment variable.
325 void
326 jcf_path_classpath_arg (path)
327 const char *path;
329 free_entry (&classpath_user);
330 add_path (&classpath_user, path, 0);
333 /* Call this when -bootclasspath is seen on the command line.
335 void
336 jcf_path_bootclasspath_arg (path)
337 const char *path;
339 free_entry (&sys_dirs);
340 add_path (&sys_dirs, path, 1);
343 /* Call this when -extdirs is seen on the command line.
345 void
346 jcf_path_extdirs_arg (cp)
347 const char *cp;
349 const char *startp, *endp;
351 free_entry (&extensions);
353 if (cp)
355 char *buf = (char *) alloca (strlen (cp) + 3);
356 startp = endp = cp;
357 while (1)
359 if (! *endp || *endp == PATH_SEPARATOR)
361 if (endp == startp)
362 return;
364 strncpy (buf, startp, endp - startp);
365 buf[endp - startp] = '\0';
368 DIR *dirp = NULL;
369 int dirname_length = strlen (buf);
371 dirp = opendir (buf);
372 if (dirp == NULL)
373 return;
375 for (;;)
377 struct dirent *direntp = readdir (dirp);
379 if (!direntp)
380 break;
382 if (direntp->d_name[0] != '.')
384 char *name =
385 (char *) alloca (dirname_length
386 + strlen (direntp->d_name) + 2);
387 strcpy (name, buf);
388 if (name[dirname_length-1] != DIR_SEPARATOR)
390 name[dirname_length] = DIR_SEPARATOR;
391 name[dirname_length+1] = 0;
393 strcat (name, direntp->d_name);
394 add_entry (&extensions, name, 0);
399 if (! *endp)
400 break;
401 ++endp;
402 startp = endp;
404 else
405 ++endp;
410 /* Call this when -I is seen on the command line. */
411 void
412 jcf_path_include_arg (path)
413 const char *path;
415 add_entry (&include_dirs, path, 0);
418 /* We `seal' the path by linking everything into one big list. Then
419 we provide a way to iterate through the sealed list. If PRINT is
420 true then we print the final class path to stderr. */
421 void
422 jcf_path_seal (print)
423 int print;
425 struct entry *secondary;
427 sealed = include_dirs;
428 include_dirs = NULL;
430 if (classpath_user)
432 secondary = classpath_user;
433 classpath_user = NULL;
435 else
437 if (! classpath_env)
438 add_entry (&classpath_env, ".", 0);
440 secondary = classpath_env;
441 classpath_env = NULL;
445 free_entry (&classpath_user);
446 free_entry (&classpath_env);
448 append_entry (&sealed, secondary);
449 append_entry (&sealed, sys_dirs);
450 append_entry (&sealed, extensions);
451 sys_dirs = NULL;
452 extensions = NULL;
454 if (print)
456 struct entry *ent;
457 fprintf (stderr, "Class path starts here:\n");
458 for (ent = sealed; ent; ent = ent->next)
460 fprintf (stderr, " %s", ent->name);
461 if ((ent->flags & FLAG_SYSTEM))
462 fprintf (stderr, " (system)");
463 if ((ent->flags & FLAG_ZIP))
464 fprintf (stderr, " (zip)");
465 fprintf (stderr, "\n");
470 void *
471 jcf_path_start ()
473 return (void *) sealed;
476 void *
477 jcf_path_next (x)
478 void *x;
480 struct entry *ent = (struct entry *) x;
481 return (void *) ent->next;
484 /* We guarantee that the return path will either be a zip file, or it
485 will end with a directory separator. */
486 char *
487 jcf_path_name (x)
488 void *x;
490 struct entry *ent = (struct entry *) x;
491 return ent->name;
495 jcf_path_is_zipfile (x)
496 void *x;
498 struct entry *ent = (struct entry *) x;
499 return (ent->flags & FLAG_ZIP);
503 jcf_path_is_system (x)
504 void *x;
506 struct entry *ent = (struct entry *) x;
507 return (ent->flags & FLAG_SYSTEM);
511 jcf_path_max_len ()
513 return longest_path;