* Updated header: Added 2006. Changed address of FSF. Changed email
[cacao.git] / src / vm / properties.c
1 /* src/vm/properties.c - handling commandline properties
2
3    Copyright (C) 1996-2005, 2006 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
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., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    Contact: cacao@cacaojvm.org
26
27    Authors: Christian Thalinger
28
29    Changes:
30
31    $Id: properties.c 4357 2006-01-22 23:33:38Z twisti $
32
33 */
34
35
36 #include "config.h"
37
38 #include <stdlib.h>
39
40 #include "vm/types.h"
41
42 #include "vm/global.h"
43 #include "native/include/java_lang_String.h"
44 #include "native/include/java_util_Properties.h"
45 #include "toolbox/list.h"
46 #include "vm/method.h"
47 #include "vm/options.h"
48 #include "vm/stringlocal.h"
49 #include "vm/jit/asmpart.h"
50
51
52 /* temporary property structure ***********************************************/
53
54 typedef struct list_properties_entry list_properties_entry;
55
56 struct list_properties_entry {
57         char     *key;
58         char     *value;
59         listnode linkage;
60 };
61
62
63 /* global variables ***********************************************************/
64
65 static list *list_properties = NULL;
66
67 static java_util_Properties *psystem;
68 static methodinfo *mput;
69
70
71 /* properties_init *************************************************************
72
73    Initialize the properties list.
74
75 *******************************************************************************/
76
77 bool properties_init(void)
78 {
79         list_properties = NEW(list);
80
81         list_init(list_properties, OFFSET(list_properties_entry, linkage));
82
83         /* everything's ok */
84
85         return true;
86 }
87
88
89 /* properties_postinit *********************************************************
90
91    Post-initialize the properties.  The passed properties table is the
92    Java system properties table.
93
94 *******************************************************************************/
95
96 bool properties_postinit(java_util_Properties *p)
97 {
98         /* set global Java system properties pointer */
99
100         psystem = p;
101
102         /* search for method to add properties */
103
104         mput = class_resolveclassmethod(p->header.vftbl->class,
105                                                                         utf_new_char("put"),
106                                                                         utf_new_char("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"),
107                                                                         NULL,
108                                                                         true);
109
110         if (mput == NULL)
111                 return false;
112
113         return true;
114 }
115
116
117 /* properties_add **************************************************************
118
119    Adds a property entry for a command line property definition.
120
121 *******************************************************************************/
122
123 void properties_add(char *key, char *value)
124 {
125         list_properties_entry *p;
126
127         p = NEW(list_properties_entry);
128         p->key   = key;
129         p->value = value;
130
131         list_addlast(list_properties, p);
132 }
133
134
135 /* get_property ****************************************************************
136
137    Get a property entry from a command line property definition.
138
139 *******************************************************************************/
140
141 char *properties_get(char *key)
142 {
143         list_properties_entry *p;
144
145         for (p = list_first(list_properties); p != NULL;
146                  p = list_next(list_properties, p)) {
147                 if (strcmp(p->key, key) == 0)
148                         return p->value;
149         }
150
151         return NULL;
152 }
153
154
155 /* properties_system_add *******************************************************
156
157    Adds a property to the Java system properties.
158
159 *******************************************************************************/
160
161 void properties_system_add(char *key, char *value)
162 {
163         java_lang_String *k;
164         java_lang_String *v;
165
166         k = javastring_new_char(key);
167         v = javastring_new_char(value);
168
169         ASM_CALLJAVAFUNCTION(mput, psystem, k, v, NULL);
170 }
171
172
173 /* properties_system_add_all ***************************************************
174
175    Adds a all properties from the properties list to the Java system
176    properties.
177
178 *******************************************************************************/
179
180 void properties_system_add_all(void)
181 {
182         list_properties_entry *p;
183
184         for (p = list_first(list_properties); p != NULL;
185                  p = list_first(list_properties)) {
186                 /* add to the Java system properties */
187
188                 properties_system_add(p->key, p->value);
189
190                 /* remove the entry from the list */
191
192                 list_remove(list_properties, p);
193
194                 /* and free the memory */
195
196                 FREE(p, list_properties_entry);
197         }
198 }
199
200
201 /*
202  * These are local overrides for various environment variables in Emacs.
203  * Please do not remove this and leave it at the end of the file, where
204  * Emacs will automagically detect them.
205  * ---------------------------------------------------------------------
206  * Local variables:
207  * mode: c
208  * indent-tabs-mode: t
209  * c-basic-offset: 4
210  * tab-width: 4
211  * End:
212  */