4 * Runtime and assembly configuration file support routines.
6 * Author: Paolo Molaro (lupus@ximian.com)
8 * (C) 2002 Ximian, Inc.
13 #include "mono/metadata/loader.h"
14 #include "mono/metadata/mono-config.h"
16 static void start_element (GMarkupParseContext *context,
17 const gchar *element_name,
18 const gchar **attribute_names,
19 const gchar **attribute_values,
23 static void end_element (GMarkupParseContext *context,
24 const gchar *element_name,
28 static void parse_text (GMarkupParseContext *context,
34 static void passthrough (GMarkupParseContext *context,
40 static void parse_error (GMarkupParseContext *context,
44 static const GMarkupParser
53 static GHashTable *config_handlers;
55 /* when this interface is stable, export it. */
56 typedef struct MonoParseHandler MonoParseHandler;
58 struct MonoParseHandler {
59 const char *element_name;
61 void (*start) (gpointer user_data, const gchar *name,
62 const gchar **attributes,
63 const gchar **values);
64 void (*text) (gpointer user_data, const char *text, gsize test_len);
65 void (*end) (gpointer user_data, const char *name);
66 void (*finish) (gpointer user_data);
70 MonoParseHandler *current;
75 static void start_element (GMarkupParseContext *context,
76 const gchar *element_name,
77 const gchar **attribute_names,
78 const gchar **attribute_values,
82 ParseState *state = user_data;
83 if (!state->current) {
84 state->current = g_hash_table_lookup (config_handlers, element_name);
85 if (state->current && state->current->init)
86 state->user_data = state->current->init ();
88 if (state->current && state->current->start)
89 state->current->start (state->user_data, element_name, attribute_names, attribute_values);
92 static void end_element (GMarkupParseContext *context,
93 const gchar *element_name,
97 ParseState *state = user_data;
99 if (state->current->end)
100 state->current->end (state->user_data, element_name);
101 if (strcmp (state->current->element_name, element_name) == 0) {
102 if (state->current->finish)
103 state->current->finish (state->user_data);
104 state->current = NULL;
105 state->user_data = NULL;
110 static void parse_text (GMarkupParseContext *context,
116 ParseState *state = user_data;
117 if (state->current && state->current->text)
118 state->current->text (state->user_data, text, text_len);
121 static void passthrough (GMarkupParseContext *context,
130 static void parse_error (GMarkupParseContext *context,
143 DllInfo *info = g_new0 (DllInfo, 1);
148 dllmap_start (gpointer user_data,
149 const gchar *element_name,
150 const gchar **attribute_names,
151 const gchar **attribute_values)
154 DllInfo *info = user_data;
156 if (strcmp (element_name, "dllmap") == 0) {
158 g_free (info->target);
159 info->dll = info->target = NULL;
160 for (i = 0; attribute_names [i]; ++i) {
161 if (strcmp (attribute_names [i], "dll") == 0)
162 info->dll = g_strdup (attribute_values [i]);
163 else if (strcmp (attribute_names [i], "target") == 0)
164 info->target = g_strdup (attribute_values [i]);
166 mono_dllmap_insert (info->dll, NULL, info->target, NULL);
167 } else if (strcmp (element_name, "dllentry") == 0) {
168 const char *name = NULL, *target = NULL, *dll = NULL;
169 for (i = 0; attribute_names [i]; ++i) {
170 if (strcmp (attribute_names [i], "dll") == 0)
171 dll = attribute_values [i];
172 else if (strcmp (attribute_names [i], "target") == 0)
173 target = attribute_values [i];
174 else if (strcmp (attribute_names [i], "name") == 0)
175 name = attribute_values [i];
179 mono_dllmap_insert (info->dll, name, dll, target);
184 dllmap_finish (gpointer user_data)
186 DllInfo *info = user_data;
189 g_free (info->target);
193 static const MonoParseHandler
204 static int inited = 0;
207 mono_config_init (void)
210 config_handlers = g_hash_table_new (g_str_hash, g_str_equal);
211 g_hash_table_insert (config_handlers, dllmap_handler.element_name, &dllmap_handler);
214 /* FIXME: error handling */
217 mono_config_parse_file (const char *filename)
219 GMarkupParseContext *context;
220 ParseState state = {NULL};
227 if (!g_file_get_contents (filename, &text, &len, NULL))
229 context = g_markup_parse_context_new (&mono_parser, 0, &state, NULL);
230 if (g_markup_parse_context_parse (context, text, len, NULL)) {
231 g_markup_parse_context_end_parse (context, NULL);
233 g_markup_parse_context_free (context);
237 * Pass a NULL filename to parse the default config files
238 * (or the file in the MONO_CONFIG env var).
241 mono_config_parse (const char *filename) {
246 mono_config_parse_file (filename);
250 home = g_getenv ("MONO_CONFIG");
252 mono_config_parse_file (home);
255 #if defined (PLATFORM_WIN32)
256 /* maybe it's better to use a registry key or the install root from the binary */
257 mono_config_parse_file (MONO_CFG_DIR "\\mono\\config");
259 mono_config_parse_file (MONO_CFG_DIR "/mono/config");
260 home = g_get_home_dir ();
261 user_cfg = g_strconcat (home, G_DIR_SEPARATOR_S, ".mono/config", NULL);
262 mono_config_parse_file (user_cfg);