1 /* src/vm/properties.c - handling commandline properties
3 Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 $Id: properties.c 8382 2007-08-21 13:05:42Z twisti $
36 #include <sys/utsname.h>
40 #include "mm/memory.h"
42 #include "native/jni.h"
44 #include "vm/global.h" /* required by java_lang_String.h */
45 #include "native/include/java_lang_String.h"
47 #include "toolbox/list.h"
48 #include "toolbox/util.h"
50 #include "vm/properties.h"
51 #include "vm/stringlocal.h"
54 #include "vm/jit/asmpart.h"
56 #include "vmcore/method.h"
57 #include "vmcore/options.h"
60 /* internal property structure ************************************************/
62 typedef struct list_properties_entry list_properties_entry;
64 struct list_properties_entry {
71 /* global variables ***********************************************************/
73 static list_t *list_properties = NULL;
76 /* properties_init *************************************************************
78 Initialize the properties list and fill the list with default
81 *******************************************************************************/
83 bool properties_init(void)
85 #if defined(ENABLE_JAVASE)
90 # if defined(WITH_CLASSPATH_GNU)
98 struct utsname *utsnamebuf;
102 /* create the properties list */
104 list_properties = list_create(OFFSET(list_properties_entry, linkage));
106 #if defined(ENABLE_JAVASE)
108 /* get properties from system */
110 env_java_home = getenv("JAVA_HOME");
112 /* set JAVA_HOME to default prefix if not defined */
114 if (env_java_home == NULL)
115 env_java_home = cacao_prefix;
117 /* fill in system properties */
119 /* add /jre to java.home property */
121 len = strlen(env_java_home) + strlen("/jre") + strlen("0");
123 java_home = MNEW(char, len);
125 strcpy(java_home, env_java_home);
126 strcat(java_home, "/jre");
128 properties_add("java.home", java_home);
130 properties_add("java.vm.specification.version", "1.0");
131 properties_add("java.vm.specification.vendor", "Sun Microsystems Inc.");
132 properties_add("java.vm.specification.name", "Java Virtual Machine Specification");
133 properties_add("java.vm.version", VERSION);
134 properties_add("java.vm.vendor", "CACAO Team");
135 properties_add("java.vm.name", "CACAO");
137 # if defined(ENABLE_INTRP)
139 /* XXX We don't support java.lang.Compiler */
140 /* properties_add("java.compiler", "cacao.intrp"); */
141 properties_add("java.vm.info", "interpreted mode");
146 /* XXX We don't support java.lang.Compiler */
147 /* properties_add("java.compiler", "cacao.jit"); */
148 properties_add("java.vm.info", "JIT mode");
151 # if defined(WITH_CLASSPATH_GNU)
153 /* get properties from system */
157 env_user = getenv("USER");
158 env_home = getenv("HOME");
159 env_lang = getenv("LANG");
161 utsnamebuf = NEW(struct utsname);
165 properties_add("java.runtime.version", VERSION);
166 properties_add("java.runtime.name", "CACAO");
168 properties_add("java.specification.version", "1.5");
169 properties_add("java.specification.vendor", "Sun Microsystems Inc.");
170 properties_add("java.specification.name", "Java Platform API Specification");
172 properties_add("java.version", JAVA_VERSION);
173 properties_add("java.vendor", "GNU Classpath");
174 properties_add("java.vendor.url", "http://www.gnu.org/software/classpath/");
176 properties_add("java.class.path", _Jv_classpath);
177 properties_add("java.class.version", CLASS_VERSION);
179 /* Set bootclasspath properties. One for GNU classpath and the
180 other for compatibility with Sun (required by most
183 properties_add("java.boot.class.path", _Jv_bootclasspath);
184 properties_add("sun.boot.class.path", _Jv_bootclasspath);
186 # if defined(WITH_STATIC_CLASSPATH)
187 properties_add("gnu.classpath.boot.library.path", ".");
188 properties_add("java.library.path" , ".");
190 /* fill gnu.classpath.boot.library.path with GNU Classpath library
193 properties_add("gnu.classpath.boot.library.path", classpath_libdir);
194 properties_add("java.library.path", _Jv_java_library_path);
197 properties_add("java.io.tmpdir", "/tmp");
199 # if defined(ENABLE_INTRP)
201 properties_add("gnu.java.compiler.name", "cacao.intrp");
206 properties_add("gnu.java.compiler.name", "cacao.jit");
209 /* set the java.ext.dirs property */
211 len = strlen(env_java_home) + strlen("/jre/lib/ext") + strlen("0");
213 extdirs = MNEW(char, len);
215 strcpy(extdirs, env_java_home);
216 strcat(extdirs, "/jre/lib/ext");
218 properties_add("java.ext.dirs", extdirs);
220 properties_add("java.endorsed.dirs", ""CACAO_PREFIX"/jre/lib/endorsed");
222 # if defined(DISABLE_GC)
223 /* When we disable the GC, we mmap the whole heap to a specific
224 address, so we can compare call traces. For this reason we have
225 to add the same properties on different machines, otherwise
226 more memory may be allocated (e.g. strlen("i386")
227 vs. strlen("alpha"). */
229 properties_add("os.arch", "unknown");
230 properties_add("os.name", "unknown");
231 properties_add("os.version", "unknown");
233 properties_add("os.arch", JAVA_ARCH);
234 properties_add("os.name", utsnamebuf->sysname);
235 properties_add("os.version", utsnamebuf->release);
238 # if defined(WITH_STATIC_CLASSPATH)
239 /* This is just for debugging purposes and can cause troubles in
242 properties_add("gnu.cpu.endian", "unknown");
244 # if WORDS_BIGENDIAN == 1
245 properties_add("gnu.cpu.endian", "big");
247 properties_add("gnu.cpu.endian", "little");
251 properties_add("file.separator", "/");
252 properties_add("path.separator", ":");
253 properties_add("line.separator", "\n");
255 properties_add("user.name", env_user ? env_user : "null");
256 properties_add("user.home", env_home ? env_home : "null");
257 properties_add("user.dir", cwd ? cwd : "null");
261 if (env_lang != NULL) {
262 /* get the local stuff from the environment */
264 if (strlen(env_lang) <= 2) {
265 properties_add("user.language", env_lang);
268 if ((env_lang[2] == '_') && (strlen(env_lang) >= 5)) {
269 lang = MNEW(char, 3);
270 strncpy(lang, (char *) &env_lang[0], 2);
273 country = MNEW(char, 3);
274 strncpy(country, (char *) &env_lang[3], 2);
277 properties_add("user.language", lang);
278 properties_add("user.country", country);
283 /* if no default locale was specified, use `en_US' */
285 properties_add("user.language", "en");
286 properties_add("user.country", "US");
289 # elif defined(WITH_CLASSPATH_SUN)
291 /* Find correct java.home. We check if there is a JRE
294 /* NOTE: We use the server VM here as it should be available on
295 all architectures. */
298 strlen(env_java_home) +
299 strlen("/jre/lib/"JAVA_ARCH"/server/libjvm.so") +
302 java_home = MNEW(char, len);
304 strcpy(java_home, env_java_home);
305 strcat(java_home, "/jre/lib/"JAVA_ARCH"/server/libjvm.so");
307 /* Check if that libjvm.so exists. */
309 if (access(java_home, F_OK) == 0) {
310 /* Yes, we add /jre to java.home. */
312 strcpy(java_home, env_java_home);
313 strcat(java_home, "/jre");
316 /* No, java.home is at it is. */
318 strcpy(java_home, env_java_home);
321 properties_add("java.home", java_home);
322 properties_add("sun.boot.library.path", classpath_libdir);
326 # error unknown classpath configuration
330 #elif defined(ENABLE_JAVAME_CLDC1_1)
332 properties_add("microedition.configuration", "CLDC-1.1");
333 properties_add("microedition.platform", "generic");
334 properties_add("microedition.encoding", "ISO8859_1");
335 properties_add("microedition.profiles", "");
339 # error unknown Java configuration
343 /* everything's ok */
349 /* properties_add **************************************************************
351 Adds a property entry to the internal property list. If there's
352 already an entry with the same key, replace it.
354 *******************************************************************************/
356 void properties_add(char *key, char *value)
358 list_properties_entry *pe;
360 /* search for the entry */
362 for (pe = list_first_unsynced(list_properties); pe != NULL;
363 pe = list_next_unsynced(list_properties, pe)) {
364 if (strcmp(pe->key, key) == 0) {
365 /* entry was found, replace the value */
373 /* entry was not found, insert a new one */
375 pe = NEW(list_properties_entry);
380 list_add_last_unsynced(list_properties, pe);
384 /* properties_get **************************************************************
386 Get a property entry from the internal property list.
388 *******************************************************************************/
390 char *properties_get(char *key)
392 list_properties_entry *pe;
394 for (pe = list_first_unsynced(list_properties); pe != NULL;
395 pe = list_next_unsynced(list_properties, pe)) {
396 if (strcmp(pe->key, key) == 0)
404 /* properties_system_add *******************************************************
406 Adds a given property to the Java system properties.
408 *******************************************************************************/
410 void properties_system_add(java_handle_t *p, char *key, char *value)
416 /* search for method to add properties */
418 m = class_resolveclassmethod(p->vftbl->class,
420 utf_new_char("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"),
427 /* add to the Java system properties */
429 k = javastring_new_from_utf_string(key);
430 v = javastring_new_from_utf_string(value);
432 (void) vm_call_method(m, p, k, v);
436 /* properties_system_add_all ***************************************************
438 Adds all properties from the properties list to the Java system
442 p.... is actually a java_util_Properties structure
444 *******************************************************************************/
446 #if defined(ENABLE_JAVASE)
447 void properties_system_add_all(java_handle_t *p)
449 list_properties_entry *pe;
452 java_handle_t *value;
454 /* search for method to add properties */
456 m = class_resolveclassmethod(p->vftbl->class,
458 utf_new_char("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"),
465 /* process all properties stored in the internal table */
467 for (pe = list_first(list_properties); pe != NULL;
468 pe = list_next(list_properties, pe)) {
469 /* add to the Java system properties */
471 key = javastring_new_from_utf_string(pe->key);
472 value = javastring_new_from_utf_string(pe->value);
474 (void) vm_call_method(m, (java_handle_t *) p, key, value);
477 #endif /* defined(ENABLE_JAVASE) */
481 * These are local overrides for various environment variables in Emacs.
482 * Please do not remove this and leave it at the end of the file, where
483 * Emacs will automagically detect them.
484 * ---------------------------------------------------------------------
487 * indent-tabs-mode: t