Fix LDC, LDC_W, and INSTANCEOF opcodes, more debugging
[jamvm-avr32-jem.git] / src / properties.c
blob4b3e097fd83a8423821838ac375d155b1f69974f
1 /*
2 * Copyright (C) 2003, 2004, 2005, 2006, 2007
3 * Robert Lougher <rob@lougher.org.uk>.
5 * This file is part of JamVM.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2,
10 * or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <string.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <sys/utsname.h>
27 #include "jam.h"
29 /* If we have endian.h include it. Otherwise, include sys/param.h
30 if we have it. If the BYTE_ORDER macro is still undefined, we
31 fall-back, and work out the endianness ourselves at runtime --
32 this always works.
34 #ifdef HAVE_ENDIAN_H
35 #include <endian.h>
36 #elif HAVE_SYS_PARAM_H
37 #include <sys/param.h>
38 #endif
40 #ifdef HAVE_LOCALE_H
41 #include <locale.h>
42 #endif
44 static Property *commandline_props;
45 static int commandline_props_count;
47 void initialiseProperties(InitArgs *args) {
48 commandline_props = args->commandline_props;
49 commandline_props_count = args->props_count;
52 char *getCommandLineProperty(char *key) {
53 int i;
55 for(i = 0; i < commandline_props_count; i++)
56 if(strcmp(commandline_props[i].key, key) == 0)
57 return commandline_props[i].value;
59 return NULL;
62 void setProperty(Object *properties, char *key, char *value) {
63 Object *k = Cstr2String(key);
64 Object *v = Cstr2String(value ? value : "?");
66 MethodBlock *mb = lookupMethod(properties->class, "put",
67 "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
68 executeMethod(properties, mb, k, v);
71 void addCommandLineProperties(Object *properties) {
72 if(commandline_props_count) {
73 int i;
75 for(i = 0; i < commandline_props_count; i++) {
76 setProperty(properties, commandline_props[i].key, commandline_props[i].value);
77 sysFree(commandline_props[i].key);
80 commandline_props_count = 0;
81 sysFree(commandline_props);
85 void setLocaleProperties(Object *properties) {
86 #if defined(HAVE_SETLOCALE) && defined(HAVE_LC_MESSAGES)
87 char *locale;
89 setlocale(LC_ALL, "");
90 if((locale = setlocale(LC_MESSAGES, "")) != NULL) {
91 int len = strlen(locale);
93 /* Check if the returned string is in the expected format,
94 e.g. de, or en_GB */
95 if(len == 2 || (len > 4 && locale[2] == '_')) {
96 char code[3];
98 code[0] = locale[0];
99 code[1] = locale[1];
100 code[2] = '\0';
101 setProperty(properties, "user.language", code);
103 /* Set the region -- the bit after the "_" */
104 if(len > 4) {
105 code[0] = locale[3];
106 code[1] = locale[4];
107 setProperty(properties, "user.region", code);
111 #endif
114 void setEndianProperty(Object *properties) {
115 #ifdef BYTE_ORDER
116 #if BYTE_ORDER == BIG_ENDIAN
117 setProperty(properties, "gnu.cpu.endian", "big");
118 #else
119 setProperty(properties, "gnu.cpu.endian", "little");
120 #endif
121 #else
122 /* No byte-order macro -- work it out ourselves at runtime */
123 union {
124 int i;
125 char c[sizeof(int)];
126 } u;
128 u.i = 1;
129 setProperty(properties, "gnu.cpu.endian",
130 u.c[sizeof(int)-1] == 1 ? "big" : "little");
131 #endif
134 void setUserDirProperty(Object *properties) {
135 char *cwd = getcwd(NULL, 0);
137 setProperty(properties, "user.dir", cwd);
139 if(cwd != NULL)
140 sysFree(cwd);
143 void setOSProperties(Object *properties) {
144 struct utsname info;
146 uname(&info);
147 setProperty(properties, "os.arch", OS_ARCH);
148 setProperty(properties, "os.name", info.sysname);
149 setProperty(properties, "os.version", info.release);
152 char *getJavaHome() {
153 char *env = getenv("JAVA_HOME");
154 return env ? env : INSTALL_DIR;
157 void addDefaultProperties(Object *properties) {
158 setProperty(properties, "java.vm.name", "JamVM");
159 setProperty(properties, "java.vm.version", VERSION);
160 setProperty(properties, "java.runtime.version", VERSION);
161 setProperty(properties, "java.vm.vendor", "Robert Lougher");
162 setProperty(properties, "java.vm.vendor.url", "http://jamvm.sourceforge.net");
163 setProperty(properties, "java.version", JAVA_COMPAT_VERSION);
164 setProperty(properties, "java.vendor", "GNU Classpath");
165 setProperty(properties, "java.vendor.url", "http://www.classpath.org");
166 setProperty(properties, "java.home", getJavaHome());
167 setProperty(properties, "java.specification.version", "1.4");
168 setProperty(properties, "java.specification.vendor", "Sun Microsystems, Inc.");
169 setProperty(properties, "java.specification.name", "Java Platform API Specification");
170 setProperty(properties, "java.vm.specification.version", "1.0");
171 setProperty(properties, "java.vm.specification.vendor", "Sun Microsystems, Inc.");
172 setProperty(properties, "java.vm.specification.name", "Java Virtual Machine Specification");
173 setProperty(properties, "java.class.version", "48.0");
174 setProperty(properties, "java.class.path", getClassPath());
175 setProperty(properties, "sun.boot.class.path", getBootClassPath());
176 setProperty(properties, "java.boot.class.path", getBootClassPath());
177 setProperty(properties, "gnu.classpath.boot.library.path", getBootDllPath());
178 setProperty(properties, "java.library.path", getDllPath());
179 setProperty(properties, "java.io.tmpdir", "/tmp");
180 setProperty(properties, "java.compiler", "");
181 setProperty(properties, "java.ext.dirs", "");
182 setProperty(properties, "file.separator", "/");
183 setProperty(properties, "path.separator", ":");
184 setProperty(properties, "line.separator", "\n");
185 setProperty(properties, "user.name", getenv("USER"));
186 setProperty(properties, "user.home", getenv("HOME"));
188 setOSProperties(properties);
189 setEndianProperty(properties);
190 setUserDirProperty(properties);
191 setLocaleProperties(properties);