1 /* Handle CLASSPATH, -classpath, and path searching.
3 Copyright (C) 1998, 1999 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 '/'
48 /* Possible flag values. */
52 /* We keep linked lists of directory names. A ``directory'' can be
53 either an ordinary directory or a .zip file. */
61 /* We support several different ways to set the class path.
63 built-in system directory (only libgcj.zip)
64 CLASSPATH environment variable
65 -CLASSPATH overrides CLASSPATH
66 -classpath option - overrides CLASSPATH, -CLASSPATH, and built-in
67 -I prepends path to list
69 We implement this by keeping several path lists, and then simply
70 ignoring the ones which are not relevant. */
72 /* This holds all the -I directories. */
73 static struct entry
*include_dirs
;
75 /* This holds the CLASSPATH environment variable. */
76 static struct entry
*classpath_env
;
78 /* This holds the -CLASSPATH command-line option. */
79 static struct entry
*classpath_u
;
81 /* This holds the -classpath command-line option. */
82 static struct entry
*classpath_l
;
84 /* This holds the default directories. Some of these will have the
86 static struct entry
*sys_dirs
;
88 /* This is the sealed list. It is just a combination of other lists. */
89 static struct entry
*sealed
;
91 /* We keep track of the longest path we've seen. */
92 static int longest_path
= 0;
102 for (e
= *entp
; e
; e
= n
)
112 append_entry (entp
, ent
)
116 /* It doesn't matter if this is slow, since it is run only at
117 startup, and then infrequently. */
120 /* Find end of list. */
121 for (e
= *entp
; e
&& e
->next
; e
= e
->next
)
131 add_entry (entp
, filename
, is_system
)
139 n
= (struct entry
*) ALLOC (sizeof (struct entry
));
140 n
->flags
= is_system
? FLAG_SYSTEM
: 0;
143 len
= strlen (filename
);
144 if (len
> 4 && (strcmp (filename
+ len
- 4, ".zip") == 0
145 || strcmp (filename
+ len
- 4, ".jar") == 0))
147 n
->flags
|= FLAG_ZIP
;
148 /* If the user uses -classpath then he'll have to include
149 libgcj.zip in the value. We check for this in a simplistic
150 way. Symlinks will fool this test. This is only used for
151 -MM and -MMD, so it probably isn't terribly important. */
152 if (! strcmp (filename
, LIBGCJ_ZIP_FILE
))
153 n
->flags
|= FLAG_SYSTEM
;
156 /* Note that we add a trailing separator to `.zip' names as well.
157 This is a little hack that lets the searching code in jcf-io.c
158 work more easily. Eww. */
159 if (filename
[len
- 1] != '/' && filename
[len
- 1] != DIR_SEPARATOR
)
161 char *f2
= (char *) alloca (len
+ 2);
162 strcpy (f2
, filename
);
163 f2
[len
] = DIR_SEPARATOR
;
165 n
->name
= strdup (f2
);
169 n
->name
= strdup (filename
);
171 if (len
> longest_path
)
174 append_entry (entp
, n
);
178 add_path (entp
, cp
, is_system
)
187 char *buf
= (char *) alloca (strlen (cp
) + 3);
191 if (! *endp
|| *endp
== PATH_SEPARATOR
)
196 buf
[1] = DIR_SEPARATOR
;
201 strncpy (buf
, startp
, endp
- startp
);
202 buf
[endp
- startp
] = '\0';
204 add_entry (entp
, buf
, is_system
);
216 /* Initialize the path module. */
222 add_entry (&sys_dirs
, ".", 0);
223 add_entry (&sys_dirs
, LIBGCJ_ZIP_FILE
, 1);
225 GET_ENV_PATH_LIST (cp
, "CLASSPATH");
226 add_path (&classpath_env
, cp
, 0);
229 /* Call this when -classpath is seen on the command line. */
231 jcf_path_classpath_arg (path
)
234 free_entry (&classpath_l
);
235 add_path (&classpath_l
, path
, 0);
238 /* Call this when -CLASSPATH is seen on the command line. */
240 jcf_path_CLASSPATH_arg (path
)
243 free_entry (&classpath_u
);
244 add_path (&classpath_u
, path
, 0);
247 /* Call this when -I is seen on the command line. */
249 jcf_path_include_arg (path
)
252 add_entry (&include_dirs
, path
, 0);
255 /* We `seal' the path by linking everything into one big list. Then
256 we provide a way to iterate through the sealed list. */
261 struct entry
*secondary
;
263 sealed
= include_dirs
;
268 secondary
= classpath_l
;
272 else if (classpath_u
)
274 secondary
= classpath_u
;
279 secondary
= classpath_env
;
280 classpath_env
= NULL
;
283 free_entry (&classpath_l
);
284 free_entry (&classpath_u
);
285 free_entry (&classpath_env
);
287 append_entry (&sealed
, secondary
);
291 append_entry (&sealed
, sys_dirs
);
295 free_entry (&sys_dirs
);
301 return (void *) sealed
;
308 struct entry
*ent
= (struct entry
*) x
;
309 return (void *) ent
->next
;
312 /* We guarantee that the return path will either be a zip file, or it
313 will end with a directory separator. */
318 struct entry
*ent
= (struct entry
*) x
;
323 jcf_path_is_zipfile (x
)
326 struct entry
*ent
= (struct entry
*) x
;
327 return (ent
->flags
& FLAG_ZIP
);
331 jcf_path_is_system (x
)
334 struct entry
*ent
= (struct entry
*) x
;
335 return (ent
->flags
& FLAG_SYSTEM
);