* Added #include "vm/options.h".
[cacao.git] / src / native / vm / VMSystemProperties.c
1 /* src/native/vm/VMSystemProperties.c - gnu/classpath/VMSystemProperties
2
3    Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
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.
14
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.
19
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., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Christian Thalinger
28
29    Changes:
30
31    $Id: VMSystemProperties.c 3166 2005-09-10 16:34:12Z twisti $
32
33 */
34
35
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/utsname.h>
39 #include <time.h>
40
41 #include "config.h"
42 #include "cacao/cacao.h"
43 #include "mm/memory.h"
44 #include "native/jni.h"
45 #include "native/native.h"
46 #include "native/include/java_util_Properties.h"
47 #include "toolbox/logging.h"
48 #include "toolbox/util.h"
49 #include "vm/exceptions.h"
50 #include "vm/builtin.h"
51 #include "vm/loader.h"
52 #include "vm/options.h"
53 #include "vm/stringlocal.h"
54 #include "vm/tables.h"
55 #include "vm/jit/asmpart.h"
56
57
58 /* temporary property structure */
59
60 typedef struct property property;
61
62 struct property {
63         char     *key;
64         char     *value;
65         property *next;
66 };
67
68 static property *properties = NULL;
69
70
71 /* create_property *************************************************************
72
73    Create a property entry for a command line property definition.
74
75 *******************************************************************************/
76
77 void create_property(char *key, char *value)
78 {
79         property *p;
80
81         p = NEW(property);
82         p->key = key;
83         p->value = value;
84         p->next = properties;
85         properties = p;
86 }
87
88
89 /* insert_property *************************************************************
90
91    Used for inserting a property into the system's properties table. Method m
92    (usually put) and the properties table must be given.
93
94 *******************************************************************************/
95
96 static void insert_property(methodinfo *m, java_util_Properties *p, char *key,
97                                                         char *value)
98 {
99         java_lang_String *k;
100         java_lang_String *v;
101
102         k = javastring_new_char(key);
103         v = javastring_new_char(value);
104
105         asm_calljavafunction(m, p, k, v, NULL);
106 }
107
108
109 /*
110  * Class:     gnu/classpath/VMSystemProperties
111  * Method:    preInit
112  * Signature: (Ljava/util/Properties;)V
113  */
114 JNIEXPORT void JNICALL Java_gnu_classpath_VMSystemProperties_preInit(JNIEnv *env, jclass clazz, java_util_Properties *p)
115 {
116         methodinfo *m;
117         char       *cwd;
118         char       *java_home;
119         char       *user;
120         char       *home;
121         char       *locale;
122         char       *lang;
123         char       *region;
124         struct utsname utsnamebuf;
125
126         /* endianess union */
127         union {
128                 u4 i;
129                 u1 c[4];
130         } u;
131
132 #if !defined(ENABLE_STATICVM)
133         char *ld_library_path;
134         char *libpath;
135         s4    libpathlen;
136 #endif
137
138         if (!p) {
139                 *exceptionptr = new_nullpointerexception();
140                 return;
141         }
142
143         /* get properties from system */
144
145         cwd = _Jv_getcwd();
146         java_home = getenv("JAVA_HOME");
147         user = getenv("USER");
148         home = getenv("HOME");
149         uname(&utsnamebuf);
150
151         /* search for method to add properties */
152
153         m = class_resolveclassmethod(p->header.vftbl->class,
154                                                                  utf_new_char("put"),
155                                                                  utf_new_char("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"),
156                                                                  clazz,
157                                                                  true);
158
159         if (!m)
160                 return;
161
162         insert_property(m, p, "java.version", JAVA_VERSION);
163         insert_property(m, p, "java.vendor", "CACAO Team");
164         insert_property(m, p, "java.vendor.url", "http://www.cacaojvm.org/");
165         insert_property(m, p, "java.home", java_home ? java_home : CACAO_INSTALL_PREFIX""CACAO_JRE_DIR);
166         insert_property(m, p, "java.vm.specification.version", "1.0");
167         insert_property(m, p, "java.vm.specification.vendor", "Sun Microsystems Inc.");
168         insert_property(m, p, "java.vm.specification.name", "Java Virtual Machine Specification");
169         insert_property(m, p, "java.vm.version", VERSION);
170         insert_property(m, p, "java.vm.vendor", "CACAO Team");
171         insert_property(m, p, "java.vm.name", "CACAO");
172         insert_property(m, p, "java.specification.version", "1.4");
173         insert_property(m, p, "java.specification.vendor", "Sun Microsystems Inc.");
174         insert_property(m, p, "java.specification.name", "Java Platform API Specification");
175         insert_property(m, p, "java.class.version", "48.0");
176         insert_property(m, p, "java.class.path", classpath);
177
178         insert_property(m, p, "java.runtime.version", VERSION);
179         insert_property(m, p, "java.runtime.name", "CACAO");
180
181         /* Set bootclasspath properties. One for GNU classpath and the other for  */
182         /* compatibility with Sun (required by most applications).                */
183         insert_property(m, p, "java.boot.class.path", bootclasspath);
184         insert_property(m, p, "sun.boot.class.path", bootclasspath);
185
186 #if defined(ENABLE_STATICVM)
187         insert_property(m, p, "gnu.classpath.boot.library.path", ".");
188         insert_property(m, p, "java.library.path" , ".");
189 #else
190         /* fill gnu.classpath.boot.library.path with GNU classpath library path */
191
192 #if !defined(WITH_EXTERNAL_CLASSPATH)
193         libpathlen = strlen(CACAO_INSTALL_PREFIX) + strlen(CACAO_LIBRARY_PATH) +
194                 strlen("0");
195 #else
196         libpathlen = strlen(EXTERNAL_CLASSPATH_PREFIX) +
197                 strlen(CLASSPATH_LIBRARY_PATH) + strlen("0");
198 #endif
199
200         libpath = MNEW(char, libpathlen);
201
202 #if !defined(WITH_EXTERNAL_CLASSPATH)
203         strcat(libpath, CACAO_INSTALL_PREFIX);
204         strcat(libpath, CACAO_LIBRARY_PATH);
205 #else
206         strcat(libpath, EXTERNAL_CLASSPATH_PREFIX);
207         strcat(libpath, CLASSPATH_LIBRARY_PATH);
208 #endif
209         insert_property(m, p, "gnu.classpath.boot.library.path", libpath);
210
211         MFREE(libpath, char, libpathlen);
212
213
214         /* now fill java.library.path */
215
216         ld_library_path = getenv("LD_LIBRARY_PATH");
217
218         if (ld_library_path) {
219                 libpathlen = strlen(".:") + strlen(ld_library_path) + strlen("0");
220
221                 libpath = MNEW(char, libpathlen);
222
223                 strcpy(libpath, ".:");
224                 strcat(libpath, ld_library_path);
225
226                 insert_property(m, p, "java.library.path", libpath);
227
228                 MFREE(libpath, char, libpathlen);
229
230         } else {
231                 insert_property(m, p, "java.library.path", ".");
232         }
233 #endif
234
235         insert_property(m, p, "java.io.tmpdir", "/tmp");
236
237 #if defined(ENABLE_INTRP)
238         if (opt_intrp) {
239                 /* XXX We don't support java.lang.Compiler */
240 /*              insert_property(m, p, "java.compiler", "cacao.intrp"); */
241                 insert_property(m, p, "java.vm.info", "interpreted mode");
242         } else
243 #endif
244         {
245                 /* XXX We don't support java.lang.Compiler */
246 /*              insert_property(m, p, "java.compiler", "cacao.jit"); */
247                 insert_property(m, p, "java.vm.info", "JIT mode");
248         }
249
250         insert_property(m, p, "java.ext.dirs", CACAO_INSTALL_PREFIX""CACAO_EXT_DIR);
251         insert_property(m, p, "os.name", utsnamebuf.sysname);
252         insert_property(m, p, "os.arch", utsnamebuf.machine);
253         insert_property(m, p, "os.version", utsnamebuf.release);
254         insert_property(m, p, "file.separator", "/");
255         /* insert_property(m, p, "file.encoding", "null"); -- this must be set properly */
256         insert_property(m, p, "path.separator", ":");
257         insert_property(m, p, "line.separator", "\n");
258         insert_property(m, p, "user.name", user ? user : "null");
259         insert_property(m, p, "user.home", home ? home : "null");
260         insert_property(m, p, "user.dir", cwd ? cwd : "null");
261
262         /* Are we little or big endian? */
263         u.i = 1;
264         insert_property(m, p, "gnu.cpu.endian", u.c[0] ? "little" : "big");
265
266 #ifdef USE_GTK
267         /* disable gthread-jni's portable native sync due to yet unresolved 
268            threading issues */
269         insert_property(m, p, "gnu.classpath.awt.gtk.portable.native.sync", "false");
270 #endif
271
272
273         /* get locales */
274         locale = getenv("LANG");
275         if (locale != NULL) { /* gnu classpath is going to set en as language */
276                 if (strlen(locale) <= 2) {
277                         insert_property(m, p, "user.language", locale);
278                 } else {
279                         if ((locale[2]=='_')&&(strlen(locale)>=5)) {
280                                 lang = MNEW(char, 3);
281                                 strncpy(lang, (char*)&locale[0], 2);
282                                 lang[2]='\0';
283                                 region = MNEW(char, 3);
284                                 strncpy(region, (char*)&locale[3], 2);
285                                 region[2]='\0';
286                                 insert_property(m, p, "user.language", lang);
287                                 insert_property(m, p, "user.region", region);
288                         }
289                 }
290         }
291         
292         
293 #if 0
294         /* how do we get them? */
295         { "user.country", "US" },
296         { "user.timezone", "Europe/Vienna" },
297
298         /* XXX do we need this one? */
299         { "java.protocol.handler.pkgs", "gnu.java.net.protocol"}
300 #endif
301         insert_property(m, p, "java.protocol.handler.pkgs", "gnu.java.net.protocol");
302
303         /* insert properties defined on commandline */
304
305         while (properties) {
306                 property *tp;
307
308                 insert_property(m, p, properties->key, properties->value);
309
310                 tp = properties;
311                 properties = properties->next;
312                 FREE(tp, property);
313         }
314
315         /* clean up */
316
317         MFREE(cwd, char, 0);
318
319         return;
320 }
321
322
323 /*
324  * These are local overrides for various environment variables in Emacs.
325  * Please do not remove this and leave it at the end of the file, where
326  * Emacs will automagically detect them.
327  * ---------------------------------------------------------------------
328  * Local variables:
329  * mode: c
330  * indent-tabs-mode: t
331  * c-basic-offset: 4
332  * tab-width: 4
333  * End:
334  */