OSDN Git Service

2000-01-11 Mumit Khan <khan@xraylith.wisc.edu>
[pf3gnuchains/gcc-fork.git] / gcc / java / jcf-path.c
1 /* Handle CLASSPATH, -classpath, and path searching.
2
3    Copyright (C) 1998, 1999  Free Software Foundation, Inc.
4
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.
9
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.
14
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.  
19
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.  */
23
24 /* Written by Tom Tromey <tromey@cygnus.com>, October 1998.  */
25
26 #include "config.h"
27 #include "system.h"
28
29 #include "jcf.h"
30
31 /* Some boilerplate that really belongs in a header.  */
32
33 #ifndef GET_ENV_PATH_LIST
34 #define GET_ENV_PATH_LIST(VAR,NAME)     do { (VAR) = getenv (NAME); } while (0)
35 #endif
36
37 #ifndef DIR_UP
38 #define DIR_UP ".."
39 #endif
40
41 \f
42
43 /* Possible flag values.  */
44 #define FLAG_SYSTEM 1
45 #define FLAG_ZIP    2
46
47 /* We keep linked lists of directory names.  A ``directory'' can be
48    either an ordinary directory or a .zip file.  */
49 struct entry
50 {
51   char *name;
52   int flags;
53   struct entry *next;
54 };
55
56 static void free_entry PROTO ((struct entry **));
57 static void append_entry PROTO ((struct entry **, struct entry *));
58 static void add_entry PROTO ((struct entry **, const char *, int));
59 static void add_path PROTO ((struct entry **, const char *, int));
60
61 /* We support several different ways to set the class path.
62
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
68
69    We implement this by keeping several path lists, and then simply
70    ignoring the ones which are not relevant.  */
71
72 /* This holds all the -I directories.  */
73 static struct entry *include_dirs;
74
75 /* This holds the CLASSPATH environment variable.  */
76 static struct entry *classpath_env;
77
78 /* This holds the -CLASSPATH command-line option.  */
79 static struct entry *classpath_u;
80
81 /* This holds the -classpath command-line option.  */
82 static struct entry *classpath_l;
83
84 /* This holds the default directories.  Some of these will have the
85    "system" flag set.  */
86 static struct entry *sys_dirs;
87
88 /* This is the sealed list.  It is just a combination of other lists.  */
89 static struct entry *sealed;
90
91 /* We keep track of the longest path we've seen.  */
92 static int longest_path = 0;
93
94 \f
95
96 static void
97 free_entry (entp)
98      struct entry **entp;
99 {
100   struct entry *e, *n;
101
102   for (e = *entp; e; e = n)
103     {
104       n = e->next;
105       free (e->name);
106       free (e);
107     }
108   *entp = NULL;
109 }
110
111 static void
112 append_entry (entp, ent)
113      struct entry **entp;
114      struct entry *ent;
115 {
116   /* It doesn't matter if this is slow, since it is run only at
117      startup, and then infrequently.  */
118   struct entry *e;
119
120   /* Find end of list.  */
121   for (e = *entp; e && e->next; e = e->next)
122     ;
123
124   if (e)
125     e->next = ent;
126   else
127     *entp = ent;
128 }
129
130 static void
131 add_entry (entp, filename, is_system)
132      struct entry **entp;
133      const char *filename;
134      int is_system;
135 {
136   int len;
137   struct entry *n;
138
139   n = (struct entry *) ALLOC (sizeof (struct entry));
140   n->flags = is_system ? FLAG_SYSTEM : 0;
141   n->next = NULL;
142
143   len = strlen (filename);
144   if (len > 4 && (strcmp (filename + len - 4, ".zip") == 0
145                   || strcmp (filename + len - 4, ".jar") == 0))
146     {
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;
154     }
155
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)
160     {
161       char *f2 = (char *) alloca (len + 2);
162       strcpy (f2, filename);
163       f2[len] = DIR_SEPARATOR;
164       f2[len + 1] = '\0';
165       n->name = xstrdup (f2);
166       ++len;
167     }
168   else
169     n->name = xstrdup (filename);
170
171   if (len > longest_path)
172     longest_path = len;
173
174   append_entry (entp, n);
175 }
176
177 static void
178 add_path (entp, cp, is_system)
179      struct entry **entp;
180      const char *cp;
181      int is_system;
182 {
183   const char *startp, *endp;
184
185   if (cp)
186     {
187       char *buf = (char *) alloca (strlen (cp) + 3);
188       startp = endp = cp;
189       while (1)
190         {
191           if (! *endp || *endp == PATH_SEPARATOR)
192             {
193               if (endp == startp)
194                 {
195                   buf[0] = '.';
196                   buf[1] = DIR_SEPARATOR;
197                   buf[2] = '\0';
198                 }
199               else
200                 {
201                   strncpy (buf, startp, endp - startp);
202                   buf[endp - startp] = '\0';
203                 }
204               add_entry (entp, buf, is_system);
205               if (! *endp)
206                 break;
207               ++endp;
208               startp = endp;
209             }
210           else
211             ++endp;
212         }
213     }
214 }
215
216 /* Initialize the path module.  */
217 void
218 jcf_path_init ()
219 {
220   char *cp;
221   char *try, sep[2];
222   struct stat stat_b;
223   int found = 0, len;
224
225   add_entry (&sys_dirs, ".", 0);
226
227   sep[0] = DIR_SEPARATOR;
228   sep[1] = '\0';
229
230   GET_ENV_PATH_LIST (cp, "GCC_EXEC_PREFIX");
231   if (cp)
232     {
233       try = alloca (strlen (cp) + 50);
234       /* The exec prefix can be something like
235          /usr/local/bin/../lib/gcc-lib/.  We want to change this
236          into a pointer to the share directory.  We support two
237          configurations: one where prefix and exec-prefix are the
238          same, and one where exec-prefix is `prefix/SOMETHING'.  */
239       strcpy (try, cp);
240       strcat (try, DIR_UP);
241       strcat (try, sep);
242       strcat (try, DIR_UP);
243       strcat (try, sep);
244       len = strlen (try);
245
246       strcpy (try + len, "share");
247       strcat (try, sep);
248       strcat (try, "libgcj.zip");
249       if (! stat (try, &stat_b))
250         {
251           add_entry (&sys_dirs, try, 1);
252           found = 1;
253         }
254       else
255         {
256           strcpy (try + len, DIR_UP);
257           strcat (try, sep);
258           strcat (try, "share");
259           strcat (try, sep);
260           strcat (try, "libgcj.zip");
261           if (! stat (try, &stat_b))
262             {
263               add_entry (&sys_dirs, try, 1);
264               found = 1;
265             }
266         }
267     }
268   if (! found)
269     {
270       /* Desperation: use the installed one.  */
271       add_entry (&sys_dirs, LIBGCJ_ZIP_FILE, 1);
272     }
273
274   GET_ENV_PATH_LIST (cp, "CLASSPATH");
275   add_path (&classpath_env, cp, 0);
276 }
277
278 /* Call this when -classpath is seen on the command line.  */
279 void
280 jcf_path_classpath_arg (path)
281      const char *path;
282 {
283   free_entry (&classpath_l);
284   add_path (&classpath_l, path, 0);
285 }
286
287 /* Call this when -CLASSPATH is seen on the command line.  */
288 void
289 jcf_path_CLASSPATH_arg (path)
290      const char *path;
291 {
292   free_entry (&classpath_u);
293   add_path (&classpath_u, path, 0);
294 }
295
296 /* Call this when -I is seen on the command line.  */
297 void
298 jcf_path_include_arg (path)
299      const char *path;
300 {
301   add_entry (&include_dirs, path, 0);
302 }
303
304 /* We `seal' the path by linking everything into one big list.  Then
305    we provide a way to iterate through the sealed list.  */
306 void
307 jcf_path_seal ()
308 {
309   int do_system = 1;
310   struct entry *secondary;
311
312   sealed = include_dirs;
313   include_dirs = NULL;
314
315   if (classpath_l)
316     {
317       secondary = classpath_l;
318       classpath_l = NULL;
319       do_system = 0;
320     }
321   else if (classpath_u)
322     {
323       secondary = classpath_u;
324       classpath_u = NULL;
325     }
326   else
327     {
328       secondary = classpath_env;
329       classpath_env = NULL;
330     }
331
332   free_entry (&classpath_l);
333   free_entry (&classpath_u);
334   free_entry (&classpath_env);
335
336   append_entry (&sealed, secondary);
337
338   if (do_system)
339     {
340       append_entry (&sealed, sys_dirs);
341       sys_dirs = NULL;
342     }
343   else
344     free_entry (&sys_dirs);
345 }
346
347 void *
348 jcf_path_start ()
349 {
350   return (void *) sealed;
351 }
352
353 void *
354 jcf_path_next (x)
355      void *x;
356 {
357   struct entry *ent = (struct entry *) x;
358   return (void *) ent->next;
359 }
360
361 /* We guarantee that the return path will either be a zip file, or it
362    will end with a directory separator.  */
363 char *
364 jcf_path_name (x)
365      void *x;
366 {
367   struct entry *ent = (struct entry *) x;
368   return ent->name;
369 }
370
371 int
372 jcf_path_is_zipfile (x)
373      void *x;
374 {
375   struct entry *ent = (struct entry *) x;
376   return (ent->flags & FLAG_ZIP);
377 }
378
379 int
380 jcf_path_is_system (x)
381      void *x;
382 {
383   struct entry *ent = (struct entry *) x;
384   return (ent->flags & FLAG_SYSTEM);
385 }
386
387 int
388 jcf_path_max_len ()
389 {
390   return longest_path;
391 }