- support external classpath
[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 2057 2005-03-23 10:57:52Z twisti $
32
33 */
34
35
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/utsname.h>
39 #include <time.h>
40 #include <unistd.h>
41
42 #include "config.h"
43 #include "cacao/cacao.h"
44 #include "mm/memory.h"
45 #include "native/jni.h"
46 #include "native/native.h"
47 #include "native/include/java_util_Properties.h"
48 #include "toolbox/logging.h"
49 #include "toolbox/util.h"
50 #include "vm/exceptions.h"
51 #include "vm/builtin.h"
52 #include "vm/loader.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         asm_calljavafunction(m,
100                                                  p,
101                                                  javastring_new_char(key),
102                                                  javastring_new_char(value),
103                                                  NULL);
104 }
105
106
107 /*
108  * Class:     gnu/classpath/VMSystemProperties
109  * Method:    preInit
110  * Signature: (Ljava/util/Properties;)V
111  */
112 JNIEXPORT void JNICALL Java_gnu_classpath_VMSystemProperties_preInit(JNIEnv *env, jclass clazz, java_util_Properties *p)
113 {
114         methodinfo *m;
115         char       *cwd;
116         char       *java_home;
117         char       *user;
118         char       *home;
119         char       *locale;
120         char       *lang;
121         char       *region;
122         struct utsname utsnamebuf;
123
124         /* endianess union */
125         union {
126                 u4 i;
127                 u1 c[4];
128         } u;
129
130 #if !defined(STATIC_CLASSPATH)
131         char *libpath;
132         s4    libpathlen;
133 #endif
134
135         if (!p) {
136                 *exceptionptr = new_nullpointerexception();
137                 return;
138         }
139
140         /* get properties from system */
141
142         cwd = _Jv_getcwd();
143         java_home = getenv("JAVA_HOME");
144         user = getenv("USER");
145         home = getenv("HOME");
146         uname(&utsnamebuf);
147
148         /* search for method to add properties */
149
150         m = class_resolveclassmethod(p->header.vftbl->class,
151                                                                  utf_new_char("put"),
152                                                                  utf_new_char("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"),
153                                                                  clazz,
154                                                                  true);
155
156         if (!m)
157                 return;
158
159         insert_property(m, p, "java.version", "1.4");
160         insert_property(m, p, "java.vendor", "CACAO Team");
161         insert_property(m, p, "java.vendor.url", "http://www.cacaojvm.org/");
162         insert_property(m, p, "java.home", java_home ? java_home : CACAO_INSTALL_PREFIX""CACAO_JRE_DIR);
163         insert_property(m, p, "java.vm.specification.version", "1.0");
164         insert_property(m, p, "java.vm.specification.vendor", "Sun Microsystems Inc.");
165         insert_property(m, p, "java.vm.specification.name", "Java Virtual Machine Specification");
166         insert_property(m, p, "java.vm.version", VERSION);
167         insert_property(m, p, "java.vm.vendor", "CACAO Team");
168         insert_property(m, p, "java.vm.name", "CACAO");
169         insert_property(m, p, "java.specification.version", "1.4");
170         insert_property(m, p, "java.specification.vendor", "Sun Microsystems Inc.");
171         insert_property(m, p, "java.specification.name", "Java Platform API Specification");
172         insert_property(m, p, "java.class.version", "48.0");
173         insert_property(m, p, "java.class.path", classpath);
174
175         /* Set bootclasspath properties. One for GNU classpath and the other for  */
176         /* compatibility with Sun (required by most applications).                */
177         insert_property(m, p, "java.boot.class.path", bootclasspath);
178         insert_property(m, p, "sun.boot.class.path", bootclasspath);
179
180 #if defined(STATIC_CLASSPATH)
181         insert_property(m, p, "gnu.classpath.boot.library.path", ".");
182         insert_property(m, p, "java.library.path" , ".");
183 #else
184         /* fill gnu.classpath.boot.library.path with GNU classpath library path */
185
186 #if !defined(WITH_EXTERNAL_CLASSPATH)
187         libpathlen = strlen(CACAO_INSTALL_PREFIX) + strlen(CACAO_LIBRARY_PATH) + 1;
188 #else
189         libpathlen = strlen(EXTERNAL_CLASSPATH_PREFIX) +
190                 strlen(CLASSPATH_LIBRARY_PATH) + 1;
191 #endif
192
193         libpath = MNEW(char, libpathlen);
194
195 #if !defined(WITH_EXTERNAL_CLASSPATH)
196         strcat(libpath, CACAO_INSTALL_PREFIX);
197         strcat(libpath, CACAO_LIBRARY_PATH);
198 #else
199         strcat(libpath, EXTERNAL_CLASSPATH_PREFIX);
200         strcat(libpath, CLASSPATH_LIBRARY_PATH);
201 #endif
202         insert_property(m, p, "gnu.classpath.boot.library.path", libpath);
203
204         /* XXX compatibility till new classpath release */
205 /*      MFREE(libpath, char, libpathlen); */
206
207
208         /* now fill java.library.path */
209
210         if (getenv("LD_LIBRARY_PATH"))
211                 libpathlen += strlen(getenv("LD_LIBRARY_PATH")) + 1;
212
213 /*              libpath = MNEW(char, libpathlen); */
214         libpath = MREALLOC(libpath, char, libpathlen, libpathlen);
215
216         strcat(libpath, ":");
217         strcat(libpath, getenv("LD_LIBRARY_PATH"));
218
219         insert_property(m, p, "java.library.path", libpath);
220
221         MFREE(libpath, char, libpathlen);
222 /*      } */
223 #endif
224
225         insert_property(m, p, "java.io.tmpdir", "/tmp");
226
227         /* XXX We don't support java.lang.Compiler */
228 /*      insert_property(m, p, "java.compiler", "cacao.jit"); */
229
230         insert_property(m, p, "java.ext.dirs", CACAO_INSTALL_PREFIX""CACAO_EXT_DIR);
231         insert_property(m, p, "os.name", utsnamebuf.sysname);
232         insert_property(m, p, "os.arch", utsnamebuf.machine);
233         insert_property(m, p, "os.version", utsnamebuf.release);
234         insert_property(m, p, "file.separator", "/");
235         /* insert_property(m, p, "file.encoding", "null"); -- this must be set properly */
236         insert_property(m, p, "path.separator", ":");
237         insert_property(m, p, "line.separator", "\n");
238         insert_property(m, p, "user.name", user ? user : "null");
239         insert_property(m, p, "user.home", home ? home : "null");
240         insert_property(m, p, "user.dir", cwd ? cwd : "null");
241
242         /* Are we little or big endian? */
243         u.i = 1;
244         insert_property(m, p, "gnu.cpu.endian", u.c[0] ? "little" : "big");
245
246 #ifdef USE_GTK
247         /* disable gthread-jni's portable native sync due to yet unresolved 
248            threading issues */
249         insert_property(m, p, "gnu.classpath.awt.gtk.portable.native.sync", "false");
250 #endif
251
252
253         /* get locales */
254         locale = getenv("LANG");
255         if (locale != NULL) { /* gnu classpath is going to set en as language */
256                 if (strlen(locale) <= 2) {
257                         insert_property(m, p, "user.language", locale);
258                 } else {
259                         if ((locale[2]=='_')&&(strlen(locale)>=5)) {
260                                 lang = MNEW(char, 3);
261                                 strncpy(lang, (char*)&locale[0], 2);
262                                 lang[2]='\0';
263                                 region = MNEW(char, 3);
264                                 strncpy(region, (char*)&locale[3], 2);
265                                 region[2]='\0';
266                                 insert_property(m, p, "user.language", lang);
267                                 insert_property(m, p, "user.region", region);
268                         }
269                 }
270         }
271         
272         
273 #if 0
274         /* how do we get them? */
275         { "user.country", "US" },
276         { "user.timezone", "Europe/Vienna" },
277
278         /* XXX do we need this one? */
279         { "java.protocol.handler.pkgs", "gnu.java.net.protocol"}
280 #endif
281         insert_property(m, p, "java.protocol.handler.pkgs", "gnu.java.net.protocol");
282
283         /* insert properties defined on commandline */
284
285         while (properties) {
286                 property *tp;
287
288                 insert_property(m, p, properties->key, properties->value);
289
290                 tp = properties;
291                 properties = properties->next;
292                 FREE(tp, property);
293         }
294
295         /* clean up */
296
297         MFREE(cwd, char, 0);
298
299         return;
300 }
301
302
303 /*
304  * These are local overrides for various environment variables in Emacs.
305  * Please do not remove this and leave it at the end of the file, where
306  * Emacs will automagically detect them.
307  * ---------------------------------------------------------------------
308  * Local variables:
309  * mode: c
310  * indent-tabs-mode: t
311  * c-basic-offset: 4
312  * tab-width: 4
313  * End:
314  */