- added gnu header
[cacao.git] / src / native / vm / VMRuntime.c
1 /* nat/Runtime.c - java/lang/Runtime
2
3    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4    R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5    M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6    P. Tomsich, J. Wenninger
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: Roman Obermaiser
28
29    Changes: Joseph Wenninger
30
31    $Id: VMRuntime.c 873 2004-01-11 20:59:29Z twisti $
32
33 */
34
35
36 #include <string.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <sys/utsname.h>
40 #include "jni.h"
41 #include "builtin.h"
42 #include "loader.h"
43 #include "native.h"
44 #include "tables.h"
45 #include "asmpart.h"
46 #include "mm/boehm.h"
47 #include "toolbox/loging.h"
48 #include "toolbox/memory.h"
49 #include "java_io_File.h"
50 #include "java_lang_String.h"
51 #include "java_lang_Process.h"
52 #include "java_util_Properties.h"    /* needed for java_lang_Runtime.h */
53 #include "java_lang_Runtime.h"
54
55
56 #define JOWENN_DEBUG
57
58
59 #define MAXPROPS 100
60 static int activeprops = 19;  
61    
62 static char *proplist[MAXPROPS][2] = {
63         { "java.class.path", NULL },
64         { "java.home", NULL },
65         { "user.home", NULL },  
66         { "user.name", NULL },
67         { "user.dir",  NULL },
68                                 
69         { "os.arch", NULL },
70         { "os.name", NULL },
71         { "os.version", NULL },
72                                          
73         { "java.class.version", "45.3" },
74         { "java.version", PACKAGE":"VERSION },
75         { "java.vendor", "CACAO Team" },
76         { "java.vendor.url", "http://www.complang.tuwien.ac.at/java/cacao/" },
77         { "java.vm.name", "CACAO"}, 
78         { "java.tmpdir", "/tmp/"},
79         { "java.io.tmpdir", "/tmp/"},
80
81         { "path.separator", ":" },
82         { "file.separator", "/" },
83         { "line.separator", "\n" },
84         { "java.protocol.handler.pkgs", "gnu.java.net.protocol"}
85 };
86
87
88 /*
89  * Class:     java_lang_Runtime
90  * Method:    execInternal
91  * Signature: ([Ljava/lang/String;[Ljava/lang/String;Ljava/io/File;)Ljava/lang/Process;
92  */
93 JNIEXPORT java_lang_Process* JNICALL Java_java_lang_Runtime_execInternal(JNIEnv *env, java_lang_Runtime *this, java_objectarray *cmd, java_objectarray *shellenv, java_io_File *workingdir)
94 {
95         log_text("Java_java_lang_Runtime_execInternal called");
96
97         return NULL;
98 }
99
100
101 /*
102  * Class:     java/lang/Runtime
103  * Method:    exitInternal
104  * Signature: (I)V
105  */
106 JNIEXPORT void JNICALL Java_java_lang_Runtime_exitInternal(JNIEnv *env, java_lang_Runtime *this, s4 par1)
107 {
108         cacao_shutdown(par1);
109 }
110
111
112 /*
113  * Class:     java/lang/Runtime
114  * Method:    freeMemory
115  * Signature: ()J
116  */
117 JNIEXPORT s8 JNICALL Java_java_lang_Runtime_freeMemory(JNIEnv *env, java_lang_Runtime *this)
118 {
119         log_text ("java_lang_Runtime_freeMemory called");
120
121         return builtin_i2l(0);
122 }
123
124
125 /*
126  * Class:     java/lang/Runtime
127  * Method:    gc
128  * Signature: ()V
129  */
130 JNIEXPORT void JNICALL Java_java_lang_Runtime_gc(JNIEnv *env, java_lang_Runtime *this)
131 {
132         gc_call();
133 }
134
135
136 /*
137  * Class:     java/lang/Runtime
138  * Method:    runFinalization
139  * Signature: ()V
140  */
141 JNIEXPORT void JNICALL Java_java_lang_Runtime_runFinalization(JNIEnv *env, java_lang_Runtime *this)
142 {
143         /* empty */
144 }
145
146
147 /*
148  * Class:     java/lang/Runtime
149  * Method:    runFinalizersOnExit
150  * Signature: (Z)V
151  */
152 JNIEXPORT void JNICALL Java_java_lang_Runtime_runFinalizersOnExitInternal(JNIEnv *env, jclass clazz, s4 par1)
153 {
154         log_text("Java_java_lang_Runtime_runFinalizersOnExit0 called");
155 }
156
157
158 /*
159  * Class:     java/lang/Runtime
160  * Method:    totalMemory
161  * Signature: ()J
162  */
163 JNIEXPORT s8 JNICALL Java_java_lang_Runtime_totalMemory(JNIEnv *env, java_lang_Runtime *this)
164 {
165         log_text ("java_lang_Runtime_totalMemory called");
166
167         return builtin_i2l(0);
168 }
169
170
171 /*
172  * Class:     java/lang/Runtime
173  * Method:    traceInstructions
174  * Signature: (Z)V
175  */
176 JNIEXPORT void JNICALL Java_java_lang_Runtime_traceInstructions(JNIEnv *env, java_lang_Runtime *this, s4 par1)
177 {
178         log_text("Java_java_lang_Runtime_traceInstructions called");
179 }
180
181
182 /*
183  * Class:     java/lang/Runtime
184  * Method:    traceMethodCalls
185  * Signature: (Z)V
186  */
187 JNIEXPORT void JNICALL Java_java_lang_Runtime_traceMethodCalls(JNIEnv *env, java_lang_Runtime *this, s4 par1)
188 {
189         log_text("Java_java_lang_Runtime_traceMethodCalls called");
190 }
191
192
193 /*
194  * Class:     java_lang_Runtime
195  * Method:    availableProcessors
196  * Signature: ()I
197  */
198 JNIEXPORT s4 JNICALL Java_java_lang_Runtime_availableProcessors(JNIEnv *env, java_lang_Runtime *this)
199 {
200         log_text("Java_java_lang_Runtime_availableProcessors called, returning hardcoded 1");
201
202         return 1;
203 }
204
205
206 /*
207  * Class:     java_lang_Runtime
208  * Method:    nativeLoad
209  * Signature: (Ljava/lang/String;)I
210  */
211 JNIEXPORT s4 JNICALL Java_java_lang_Runtime_nativeLoad(JNIEnv *env, java_lang_Runtime *this, java_lang_String *par1)
212 {
213 #ifdef JOWENN_DEBUG     
214         char *buffer;
215         int buffer_len;
216         utf *data;
217         
218         data = javastring_toutf(par1, 0);
219         
220         if (!data) {
221                 log_text("nativeLoad: Error: empty string");
222                 return 1;
223         }
224         
225         buffer_len = utf_strlen(data) + 40;
226
227                 
228         buffer = MNEW(char, buffer_len);
229
230         strcpy(buffer, "Java_java_lang_Runtime_nativeLoad:");
231         utf_sprint(buffer + strlen((char *) data), data);
232         log_text(buffer);       
233
234         MFREE(buffer, char, buffer_len);
235 #endif
236         log_text("Java_java_lang_Runtime_nativeLoad");
237
238         return 1;
239 }
240
241
242 /*
243  * Class:     java_lang_Runtime
244  * Method:    nativeGetLibname
245  * Signature: (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
246  */
247 JNIEXPORT java_lang_String* JNICALL Java_java_lang_Runtime_nativeGetLibname(JNIEnv *env, jclass clazz, java_lang_String *par1, java_lang_String *par2)
248 {
249 #ifdef JOWENN_DEBUG     
250         char *buffer;
251         int buffer_len;
252         utf *data;
253         
254         data = javastring_toutf(par2, 0);
255         
256         if (!data) {
257                 log_text("nativeGetLibName: Error: empty string");
258                 return 0;;
259         }
260         
261         buffer_len = utf_strlen(data) + 40;
262         
263         buffer = MNEW(char, buffer_len);
264
265         strcpy(buffer, "Java_java_lang_Runtime_nativeGetLibname:");
266         utf_sprint(buffer + strlen((char *) data), data);
267         log_text(buffer);       
268
269         MFREE(buffer, char, buffer_len);
270 #endif
271         log_text("Java_java_lang_Runtime_nativeGetLibname");
272
273         return 0;
274 }
275
276
277 /*
278  * Class:     java_lang_Runtime
279  * Method:    insertSystemProperties
280  * Signature: (Ljava/util/Properties;)V
281  */
282 JNIEXPORT void JNICALL Java_java_lang_Runtime_insertSystemProperties(JNIEnv *env, jclass clazz, java_util_Properties *p)
283 {
284
285 #define BUFFERSIZE 200
286         u4 i;
287         methodinfo *m;
288         char buffer[BUFFERSIZE];
289         struct utsname utsnamebuf;
290
291         proplist[0][1] = classpath;
292         proplist[1][1] = getenv("JAVA_HOME");
293         proplist[2][1] = getenv("HOME");
294         proplist[3][1] = getenv("USER");
295         proplist[4][1] = getcwd(buffer, BUFFERSIZE);
296
297         /* get properties from system */
298         uname(&utsnamebuf);
299         proplist[5][1] = utsnamebuf.machine;
300         proplist[6][1] = utsnamebuf.sysname;
301         proplist[7][1] = utsnamebuf.release;
302
303         if (!p)
304                 panic("Java_java_lang_Runtime_insertSystemProperties called with NULL-Argument");
305
306         /* search for method to add properties */
307         m = class_resolvemethod(p->header.vftbl->class,
308                                                         utf_new_char("put"),
309                                                         utf_new_char("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;")
310                                                         );
311
312         if (!m)
313                 panic("Can not find method 'put' for class Properties");
314
315         /* add the properties */
316         for (i = 0; i < activeprops; i++) {
317
318                 if (proplist[i][1] == NULL) proplist[i][1] = "";
319
320                 asm_calljavafunction(m,
321                                                          p,
322                                                          javastring_new_char(proplist[i][0]),
323                                                          javastring_new_char(proplist[i][1]),
324                                                          NULL
325                                                          );
326         }
327
328         return;
329 }
330
331
332 /*
333  * Class:     java_lang_Runtime
334  * Method:    maxMemory
335  * Signature: ()J
336  */
337 JNIEXPORT s8 JNICALL Java_java_lang_Runtime_maxMemory(JNIEnv *env, java_lang_Runtime *this)
338 {
339         log_text("Java_java_lang_Runtime_maxMemory");
340
341         return 0;
342 }
343
344
345 /*
346  * These are local overrides for various environment variables in Emacs.
347  * Please do not remove this and leave it at the end of the file, where
348  * Emacs will automagically detect them.
349  * ---------------------------------------------------------------------
350  * Local variables:
351  * mode: c
352  * indent-tabs-mode: t
353  * c-basic-offset: 4
354  * tab-width: 4
355  * End:
356  */