aboutsummaryrefslogtreecommitdiff
path: root/gcc/java/jcf-path.c
blob: bdd951506f47ce164ad92cf07e9713a954fbe6c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* Handle CLASSPATH, -classpath, and path searching.

   Copyright (C) 1998, 1999  Free Software Foundation, Inc.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU CC; see the file COPYING.  If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  

Java and all Java-based marks are trademarks or registered trademarks
of Sun Microsystems, Inc. in the United States and other countries.
The Free Software Foundation is independent of Sun Microsystems, Inc.  */

/* Written by Tom Tromey <tromey@cygnus.com>, October 1998.  */

#include <config.h>
#include "system.h"

#include "jcf.h"

/* Some boilerplate that really belongs in a header.  */

#ifndef GET_ENV_PATH_LIST
#define GET_ENV_PATH_LIST(VAR,NAME)	do { (VAR) = getenv (NAME); } while (0)
#endif

/* By default, colon separates directories in a path.  */
#ifndef PATH_SEPARATOR
#define PATH_SEPARATOR ':'
#endif

#ifndef DIR_SEPARATOR
#define DIR_SEPARATOR '/'
#endif



/* Possible flag values.  */
#define FLAG_SYSTEM 1
#define FLAG_ZIP    2

/* We keep linked lists of directory names.  A ``directory'' can be
   either an ordinary directory or a .zip file.  */
struct entry
{
  char *name;
  int flags;
  struct entry *next;
};

/* We support several different ways to set the class path.

   built-in system directory (only libgcj.zip)
   CLASSPATH environment variable
   -CLASSPATH overrides CLASSPATH
   -classpath option - overrides CLASSPATH, -CLASSPATH, and built-in
   -I prepends path to list

   We implement this by keeping several path lists, and then simply
   ignoring the ones which are not relevant.  */

/* This holds all the -I directories.  */
static struct entry *include_dirs;

/* This holds the CLASSPATH environment variable.  */
static struct entry *classpath_env;

/* This holds the -CLASSPATH command-line option.  */
static struct entry *classpath_u;

/* This holds the -classpath command-line option.  */
static struct entry *classpath_l;

/* This holds the default directories.  Some of these will have the
   "system" flag set.  */
static struct entry *sys_dirs;

/* This is the sealed list.  It is just a combination of other lists.  */
static struct entry *sealed;

/* We keep track of the longest path we've seen.  */
static int longest_path = 0;



static void
free_entry (entp)
     struct entry **entp;
{
  struct entry *e, *n;

  for (e = *entp; e; e = n)
    {
      n = e->next;
      free (e->name);
      free (e);
    }
  *entp = NULL;
}

static void
append_entry (entp, ent)
     struct entry **entp;
     struct entry *ent;
{
  /* It doesn't matter if this is slow, since it is run only at
     startup, and then infrequently.  */
  struct entry *e;

  /* Find end of list.  */
  for (e = *entp; e && e->next; e = e->next)
    ;

  if (e)
    e->next = ent;
  else
    *entp = ent;
}

static void
add_entry (entp, filename, is_system)
     struct entry **entp;
     char *filename;
     int is_system;
{
  int len;
  struct entry *n;

  n = (struct entry *) ALLOC (sizeof (struct entry));
  n->flags = is_system ? FLAG_SYSTEM : 0;
  n->next = NULL;

  len = strlen (filename);
  if (len > 4 && (strcmp (filename + len - 4, ".zip") == 0
		  || strcmp (filename + len - 4, ".jar") == 0))
    {
      n->flags |= FLAG_ZIP;
      /* If the user uses -classpath then he'll have to include
	 libgcj.zip in the value.  We check for this in a simplistic
	 way.  Symlinks will fool this test.  This is only used for
	 -MM and -MMD, so it probably isn't terribly important.  */
      if (! strcmp (filename, LIBGCJ_ZIP_FILE))
	n->flags |= FLAG_SYSTEM;
    }

  /* Note that we add a trailing separator to `.zip' names as well.
     This is a little hack that lets the searching code in jcf-io.c
     work more easily.  Eww.  */
  if (filename[len - 1] != '/' && filename[len - 1] != DIR_SEPARATOR)
    {
      char *f2 = (char *) alloca (len + 2);
      strcpy (f2, filename);
      f2[len] = DIR_SEPARATOR;
      f2[len + 1] = '\0';
      n->name = strdup (f2);
      ++len;
    }
  else
    n->name = strdup (filename);

  if (len > longest_path)
    longest_path = len;

  append_entry (entp, n);
}

static void
add_path (entp, cp, is_system)
     struct entry **entp;
     char *cp;
     int is_system;
{
  char *startp, *endp;

  if (cp)
    {
      char *buf = (char *) alloca (strlen (cp) + 3);
      startp = endp = cp;
      while (1)
	{
	  if (! *endp || *endp == PATH_SEPARATOR)
	    {
	      if (endp == startp)
		{
		  buf[0] = '.';
		  buf[1] = DIR_SEPARATOR;
		  buf[2] = '\0';
		}
	      else
		{
		  strncpy (buf, startp, endp - startp);
		  buf[endp - startp] = '\0';
		}
	      add_entry (entp, buf, is_system);
	      if (! *endp)
		break;
	      ++endp;
	      startp = endp;
	    }
	  else
	    ++endp;
	}
    }
}

/* Initialize the path module.  */
void
jcf_path_init ()
{
  char *cp;

  add_entry (&sys_dirs, ".", 0);
  add_entry (&sys_dirs, LIBGCJ_ZIP_FILE, 1);

  GET_ENV_PATH_LIST (cp, "CLASSPATH");
  add_path (&classpath_env, cp, 0);
}

/* Call this when -classpath is seen on the command line.  */
void
jcf_path_classpath_arg (path)
     char *path;
{
  free_entry (&classpath_l);
  add_path (&classpath_l, path, 0);
}

/* Call this when -CLASSPATH is seen on the command line.  */
void
jcf_path_CLASSPATH_arg (path)
     char *path;
{
  free_entry (&classpath_u);
  add_path (&classpath_u, path, 0);
}

/* Call this when -I is seen on the command line.  */
void
jcf_path_include_arg (path)
     char *path;
{
  add_entry (&include_dirs, path, 0);
}

/* We `seal' the path by linking everything into one big list.  Then
   we provide a way to iterate through the sealed list.  */
void
jcf_path_seal ()
{
  int do_system = 1;
  struct entry *secondary;

  sealed = include_dirs;
  include_dirs = NULL;

  if (classpath_l)
    {
      secondary = classpath_l;
      classpath_l = NULL;
      do_system = 0;
    }
  else if (classpath_u)
    {
      secondary = classpath_u;
      classpath_u = NULL;
    }
  else
    {
      secondary = classpath_env;
      classpath_env = NULL;
    }

  free_entry (&classpath_l);
  free_entry (&classpath_u);
  free_entry (&classpath_env);

  append_entry (&sealed, secondary);

  if (do_system)
    {
      append_entry (&sealed, sys_dirs);
      sys_dirs = NULL;
    }
  else
    free_entry (&sys_dirs);
}

void *
jcf_path_start ()
{
  return (void *) sealed;
}

void *
jcf_path_next (x)
     void *x;
{
  struct entry *ent = (struct entry *) x;
  return (void *) ent->next;
}

/* We guarantee that the return path will either be a zip file, or it
   will end with a directory separator.  */
char *
jcf_path_name (x)
     void *x;
{
  struct entry *ent = (struct entry *) x;
  return ent->name;
}

int
jcf_path_is_zipfile (x)
     void *x;
{
  struct entry *ent = (struct entry *) x;
  return (ent->flags & FLAG_ZIP);
}

int
jcf_path_is_system (x)
     void *x;
{
  struct entry *ent = (struct entry *) x;
  return (ent->flags & FLAG_SYSTEM);
}

int
jcf_path_max_len ()
{
  return longest_path;
}