set user.language/user.region system property according to LANG enviroment variable
[cacao.git] / src / native / vm / VMRuntime.c
index 2d5e67fffabfee506f79bf414954658ffab8f878..983c1337872b46534ef8286a2ff441a02386e924 100644 (file)
@@ -1,9 +1,9 @@
-/* nat/Runtime.c - java/lang/Runtime
+/* native/vm/VMRuntime.c - java/lang/VMRuntime
 
-   Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
-   R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
-   M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
-   P. Tomsich, J. Wenninger
+   Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
+   R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
+   C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
+   Institut f. Computersprachen - TU Wien
 
    This file is part of CACAO.
 
    Changes: Joseph Wenninger
             Christian Thalinger
 
-   $Id: VMRuntime.c 1506 2004-11-14 14:48:49Z jowenn $
+   $Id: VMRuntime.c 1863 2005-01-05 20:14:08Z motse $
 
 */
 
 
+#include "config.h"
+
 #include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <sys/utsname.h>
-#include "exceptions.h"
-#include "main.h"
-#include "jni.h"
-#include "builtin.h"
-#include "exceptions.h"
-#include "loader.h"
-#include "native.h"
-#include "tables.h"
-#include "asmpart.h"
+
+#if defined(__DARWIN__)
+# include <mach/mach.h>
+#endif
+
+#if !defined(STATIC_CLASSPATH)
+# include <dlfcn.h>
+#endif
+
+#include "cacao/cacao.h"
 #include "mm/boehm.h"
+#include "mm/memory.h"
+#include "native/jni.h"
+#include "native/native.h"
+#include "native/include/java_io_File.h"
+#include "native/include/java_lang_String.h"
+#include "native/include/java_lang_Process.h"
+#include "native/include/java_util_Properties.h"     /* java_lang_VMRuntime.h */
+#include "native/include/java_lang_VMRuntime.h"
 #include "toolbox/logging.h"
-#include "toolbox/memory.h"
-#include "nat/java_io_File.h"
-#include "nat/java_lang_String.h"
-#include "nat/java_lang_Process.h"
-#include "nat/java_util_Properties.h"    /* needed for java_lang_VMRuntime.h */
-#include "nat/java_lang_VMRuntime.h"
+#include "vm/builtin.h"
+#include "vm/exceptions.h"
+#include "vm/loader.h"
+#include "vm/tables.h"
+#include "vm/jit/asmpart.h"
 
-#include "config.h"
-#ifndef STATIC_CLASSPATH
-#include <dlfcn.h>
-#endif
 
 /* this should work on BSD */
 /*
@@ -78,8 +84,8 @@ static bool finalizeOnExit = false;
 typedef struct property property;
 
 struct property {
-       char *key;
-       char *value;
+       char     *key;
+       char     *value;
        property *next;
 };
 
@@ -302,8 +308,10 @@ JNIEXPORT s4 JNICALL Java_java_lang_VMRuntime_nativeLoad(JNIEnv *env, jclass cla
 {
        int retVal=0;
 
+#ifdef JOWENN_DEBUG
        char *buffer;
        int buffer_len;
+#endif
        utf *data;
 
 #ifdef JOWENN_DEBUG
@@ -332,7 +340,7 @@ JNIEXPORT s4 JNICALL Java_java_lang_VMRuntime_nativeLoad(JNIEnv *env, jclass cla
 #ifndef STATIC_CLASSPATH
        /*here it could be interesting to store the references in a list eg for nicely cleaning up or for certain platforms*/
         if (dlopen(data->text,RTLD_NOW | RTLD_GLOBAL)) {
-               log_text("LIBLOADED");
+               /*log_text("LIBLOADED");*/
                 retVal=1;
         }
 #else
@@ -344,37 +352,46 @@ JNIEXPORT s4 JNICALL Java_java_lang_VMRuntime_nativeLoad(JNIEnv *env, jclass cla
 
 
 /*
- * Class:     java_lang_VMRuntime
+ * Class:     java/lang/VMRuntime
  * Method:    nativeGetLibname
  * Signature: (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
  */
-JNIEXPORT java_lang_String* JNICALL Java_java_lang_VMRuntime_nativeGetLibname(JNIEnv *env, jclass clazz, java_lang_String *par1, java_lang_String *par2)
+JNIEXPORT java_lang_String* JNICALL Java_java_lang_VMRuntime_nativeGetLibname(JNIEnv *env, jclass clazz, java_lang_String *pathname, java_lang_String *libname)
 {
        char *buffer;
        int buffer_len;
-       utf *data;
-       java_lang_String *resultString; 
-       data = javastring_toutf(par2, 0);
+       utf *u;
+       java_lang_String *s;
+
+       if (!libname) {
+               *exceptionptr = new_nullpointerexception();
+               return NULL;
+       }
+
+       u = javastring_toutf(libname, 0);
        
-       if (!data) {
+       if (!u) {
                log_text("nativeGetLibName: Error: empty string");
                return 0;;
        }
        
-       buffer_len = utf_strlen(data) + 6 /*lib .so */ +1 /*0*/;
+       buffer_len = utf_strlen(u) + 6 /*lib .so */ +1 /*0*/;
        buffer = MNEW(char, buffer_len);
-       sprintf(buffer,"lib");
-       utf_sprint(buffer+3,data);
-       strcat(buffer,".so");
+
+       sprintf(buffer, "lib");
+       utf_sprint(buffer + 3, u);
+       strcat(buffer, ".so");
+
 #ifdef JOWENN_DEBUG
-        log_text("nativeGetLibName:");
+       log_text("nativeGetLibName:");
        log_text(buffer);
 #endif
        
-       resultString=javastring_new_char(buffer);       
+       s = javastring_new_char(buffer);        
 
        MFREE(buffer, char, buffer_len);
-       return resultString;
+
+       return s;
 }
 
 
@@ -392,10 +409,18 @@ JNIEXPORT void JNICALL Java_java_lang_VMRuntime_insertSystemProperties(JNIEnv *e
        char *java_home;
        char *user;
        char *home;
+       char *locale;
+       char *lang;
+       char *region;
+
        struct utsname utsnamebuf;
+#if !defined(STATIC_CLASSPATH)
+       char *libpath;
+       s4    libpathlen;
+#endif
 
        if (!p) {
-               *exceptionptr = new_exception(string_java_lang_NullPointerException);
+               *exceptionptr = new_nullpointerexception();
                return;
        }
 
@@ -420,8 +445,8 @@ JNIEXPORT void JNICALL Java_java_lang_VMRuntime_insertSystemProperties(JNIEnv *e
 
        insert_property(m, p, "java.version", VERSION);
        insert_property(m, p, "java.vendor", "CACAO Team");
-       insert_property(m, p, "java.vendor.url", "http://www.complang.tuwien.ac.at/java/cacao/");
-       insert_property(m, p, "java.home", java_home ? java_home : "null");
+       insert_property(m, p, "java.vendor.url", "http://www.cacaojvm.org/");
+       insert_property(m, p, "java.home", java_home ? java_home : CACAO_INSTALL_PREFIX);
        insert_property(m, p, "java.vm.specification.version", "1.0");
        insert_property(m, p, "java.vm.specification.vendor", "Sun Microsystems Inc.");
        insert_property(m, p, "java.vm.specification.name", "Java Virtual Machine Specification");
@@ -433,14 +458,48 @@ JNIEXPORT void JNICALL Java_java_lang_VMRuntime_insertSystemProperties(JNIEnv *e
        insert_property(m, p, "java.specification.name", "Java Platform API Specification");
        insert_property(m, p, "java.class.version", "48.0");
        insert_property(m, p, "java.class.path", classpath);
+
+       /* Set bootclasspath properties. One for GNU classpath and the other for  */
+       /* compatibility with Sun (required by most applications).                */
+       insert_property(m, p, "java.boot.class.path", bootclasspath);
+       insert_property(m, p, "sun.boot.class.path", bootclasspath);
+
 #if defined(STATIC_CLASSPATH)
        insert_property(m, p, "java.library.path" , ".");
 #else
-       insert_property(m, p, "java.library.path" , getenv("LD_LIBRARY_PATH"));
+       libpathlen = strlen(CACAO_INSTALL_PREFIX) + strlen(CACAO_LIBRARY_PATH) + 1;
+
+       if (getenv("CACAO_LIB_OVERRIDE"))
+               libpathlen += strlen(getenv("CACAO_LIB_OVERRIDE")) + 1;
+
+       if (getenv("LD_LIBRARY_PATH"))
+               libpathlen += strlen(getenv("LD_LIBRARY_PATH")) + 1;
+
+       libpath = MNEW(char, libpathlen);
+
+       if (getenv("CACAO_LIB_OVERRIDE")) {
+               strcat(libpath, getenv("CACAO_LIB_OVERRIDE"));
+               strcat(libpath, ":");
+       }
+
+       strcat(libpath, CACAO_INSTALL_PREFIX);
+       strcat(libpath, CACAO_LIBRARY_PATH);
+
+       if (getenv("LD_LIBRARY_PATH")) {
+               strcat(libpath, ":");
+               strcat(libpath, getenv("LD_LIBRARY_PATH"));
+       }
+       insert_property(m, p, "java.library.path", libpath);
+
+       MFREE(libpath, char, libpathlen);
 #endif
+
        insert_property(m, p, "java.io.tmpdir", "/tmp");
-       insert_property(m, p, "java.compiler", "cacao.jit");
-       insert_property(m, p, "java.ext.dirs", "null");
+
+       /* XXX We don't support java.lang.Compiler */
+/*     insert_property(m, p, "java.compiler", "cacao.jit"); */
+
+       insert_property(m, p, "java.ext.dirs", CACAO_INSTALL_PREFIX""CACAO_EXT_DIR);
        insert_property(m, p, "os.name", utsnamebuf.sysname);
        insert_property(m, p, "os.arch", utsnamebuf.machine);
        insert_property(m, p, "os.version", utsnamebuf.release);
@@ -452,17 +511,42 @@ JNIEXPORT void JNICALL Java_java_lang_VMRuntime_insertSystemProperties(JNIEnv *e
        insert_property(m, p, "user.home", home ? home : "null");
        insert_property(m, p, "user.dir", cwd ? cwd : "null");
 
+#ifdef USE_GTK
+       /* disable gthread-jni's portable native sync due to yet unresolved 
+          threading issues */
+       insert_property(m, p, "gnu.classpath.awt.gtk.portable.native.sync", "false");
+#endif
+
+
+       /* get locales */
+       locale = getenv("LANG");
+       if (locale != NULL) { /* gnu classpath is going to set en as language */
+               if (strlen(locale) <= 2) {
+                       insert_property(m, p, "user.language", locale);
+               } else {
+                       if ((locale[2]=='_')&&(strlen(locale)>=5)) {
+                               lang = MNEW(char, 3);
+                               strncpy(lang, (char*)&locale[0], 2);
+                               lang[2]='\0';
+                               region = MNEW(char, 3);
+                               strncpy(region, (char*)&locale[3], 2);
+                               region[2]='\0';
+                               insert_property(m, p, "user.language", lang);
+                               insert_property(m, p, "user.region", region);
+                       }
+               }
+       }
+       
+       
 #if 0
        /* how do we get them? */
-       { "user.language", "en" },
-       { "user.region", "US" },
        { "user.country", "US" },
        { "user.timezone", "Europe/Vienna" },
 
        /* XXX do we need this one? */
        { "java.protocol.handler.pkgs", "gnu.java.net.protocol"}
 #endif
-       insert_property(m,p,"java.protocol.handler.pkgs","gnu.java.net.protocol");
+       insert_property(m, p, "java.protocol.handler.pkgs", "gnu.java.net.protocol");
 
        /* insert properties defined on commandline */