OSDN Git Service

Added Java 1.1 language features.
[pf3gnuchains/gcc-fork.git] / gcc / java / jcf-path.c
index 6e88496..3e63bc5 100644 (file)
@@ -1,6 +1,6 @@
 /* Handle CLASSPATH, -classpath, and path searching.
 
-   Copyright (C) 1998  Free Software Foundation, Inc.
+   Copyright (C) 1998, 1999, 2000  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
@@ -23,7 +23,7 @@ The Free Software Foundation is independent of Sun Microsystems, Inc.  */
 
 /* Written by Tom Tromey <tromey@cygnus.com>, October 1998.  */
 
-#include <config.h>
+#include "config.h"
 #include "system.h"
 
 #include "jcf.h"
@@ -43,6 +43,10 @@ The Free Software Foundation is independent of Sun Microsystems, Inc.  */
 #define DIR_SEPARATOR '/'
 #endif
 
+#ifndef DIR_UP
+#define DIR_UP ".."
+#endif
+
 \f
 
 /* Possible flag values.  */
@@ -58,9 +62,14 @@ struct entry
   struct entry *next;
 };
 
+static void free_entry PARAMS ((struct entry **));
+static void append_entry PARAMS ((struct entry **, struct entry *));
+static void add_entry PARAMS ((struct entry **, const char *, int));
+static void add_path PARAMS ((struct entry **, const char *, int));
+
 /* We support several different ways to set the class path.
 
-   built-in system directory (only libjava.zip)
+   built-in system directory (only libgcj.zip)
    CLASSPATH environment variable
    -CLASSPATH overrides CLASSPATH
    -classpath option - overrides CLASSPATH, -CLASSPATH, and built-in
@@ -130,7 +139,7 @@ append_entry (entp, ent)
 static void
 add_entry (entp, filename, is_system)
      struct entry **entp;
-     char *filename;
+     const char *filename;
      int is_system;
 {
   int len;
@@ -141,28 +150,32 @@ add_entry (entp, filename, is_system)
   n->next = NULL;
 
   len = strlen (filename);
-  if (len > 4 && ! strcmp (filename - 4, ".zip"))
+  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
-        libjava.zip in the value.  We check for this in a simplistic
+        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, LIBJAVA_ZIP_FILE))
+      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 + 1);
+      char *f2 = (char *) alloca (len + 2);
       strcpy (f2, filename);
       f2[len] = DIR_SEPARATOR;
       f2[len + 1] = '\0';
-      n->name = strdup (f2);
+      n->name = xstrdup (f2);
       ++len;
     }
   else
-    n->name = strdup (filename);
+    n->name = xstrdup (filename);
 
   if (len > longest_path)
     longest_path = len;
@@ -173,10 +186,10 @@ add_entry (entp, filename, is_system)
 static void
 add_path (entp, cp, is_system)
      struct entry **entp;
-     char *cp;
+     const char *cp;
      int is_system;
 {
-  char *startp, *endp;
+  const char *startp, *endp;
 
   if (cp)
     {
@@ -186,20 +199,17 @@ add_path (entp, cp, is_system)
        {
          if (! *endp || *endp == PATH_SEPARATOR)
            {
-             strncpy (buf, startp, endp - startp);
              if (endp == startp)
                {
                  buf[0] = '.';
                  buf[1] = DIR_SEPARATOR;
                  buf[2] = '\0';
                }
-             else if (endp[-1] != '/' && endp[1] != DIR_SEPARATOR)
+             else
                {
-                 buf[endp - startp] = DIR_SEPARATOR;
-                 buf[endp - startp + 1] = '\0';
+                 strncpy (buf, startp, endp - startp);
+                 buf[endp - startp] = '\0';
                }
-             else
-               buf[endp - startp] = '\0';
              add_entry (entp, buf, is_system);
              if (! *endp)
                break;
@@ -217,9 +227,58 @@ void
 jcf_path_init ()
 {
   char *cp;
+  char *try, sep[2];
+  struct stat stat_b;
+  int found = 0, len;
 
   add_entry (&sys_dirs, ".", 0);
-  add_entry (&sys_dirs, LIBJAVA_ZIP_FILE, 1);
+
+  sep[0] = DIR_SEPARATOR;
+  sep[1] = '\0';
+
+  GET_ENV_PATH_LIST (cp, "GCC_EXEC_PREFIX");
+  if (cp)
+    {
+      try = alloca (strlen (cp) + 50);
+      /* The exec prefix can be something like
+        /usr/local/bin/../lib/gcc-lib/.  We want to change this
+        into a pointer to the share directory.  We support two
+        configurations: one where prefix and exec-prefix are the
+        same, and one where exec-prefix is `prefix/SOMETHING'.  */
+      strcpy (try, cp);
+      strcat (try, DIR_UP);
+      strcat (try, sep);
+      strcat (try, DIR_UP);
+      strcat (try, sep);
+      len = strlen (try);
+
+      strcpy (try + len, "share");
+      strcat (try, sep);
+      strcat (try, "libgcj.zip");
+      if (! stat (try, &stat_b))
+       {
+         add_entry (&sys_dirs, try, 1);
+         found = 1;
+       }
+      else
+       {
+         strcpy (try + len, DIR_UP);
+         strcat (try, sep);
+         strcat (try, "share");
+         strcat (try, sep);
+         strcat (try, "libgcj.zip");
+         if (! stat (try, &stat_b))
+           {
+             add_entry (&sys_dirs, try, 1);
+             found = 1;
+           }
+       }
+    }
+  if (! found)
+    {
+      /* Desperation: use the installed one.  */
+      add_entry (&sys_dirs, LIBGCJ_ZIP_FILE, 1);
+    }
 
   GET_ENV_PATH_LIST (cp, "CLASSPATH");
   add_path (&classpath_env, cp, 0);
@@ -228,7 +287,7 @@ jcf_path_init ()
 /* Call this when -classpath is seen on the command line.  */
 void
 jcf_path_classpath_arg (path)
-     char *path;
+     const char *path;
 {
   free_entry (&classpath_l);
   add_path (&classpath_l, path, 0);
@@ -237,7 +296,7 @@ jcf_path_classpath_arg (path)
 /* Call this when -CLASSPATH is seen on the command line.  */
 void
 jcf_path_CLASSPATH_arg (path)
-     char *path;
+     const char *path;
 {
   free_entry (&classpath_u);
   add_path (&classpath_u, path, 0);
@@ -246,7 +305,7 @@ jcf_path_CLASSPATH_arg (path)
 /* Call this when -I is seen on the command line.  */
 void
 jcf_path_include_arg (path)
-     char *path;
+     const char *path;
 {
   add_entry (&include_dirs, path, 0);
 }