4 * Runtime and assembly configuration file support routines.
6 * Author: Paolo Molaro (lupus@ximian.com)
8 * (C) 2002 Ximian, Inc.
14 #include "mono/metadata/assembly.h"
15 #include "mono/metadata/loader.h"
16 #include "mono/metadata/mono-config.h"
17 #include "mono/metadata/metadata-internals.h"
18 #include "mono/utils/mono-logger.h"
20 static void start_element (GMarkupParseContext *context,
21 const gchar *element_name,
22 const gchar **attribute_names,
23 const gchar **attribute_values,
27 static void end_element (GMarkupParseContext *context,
28 const gchar *element_name,
32 static void parse_text (GMarkupParseContext *context,
38 static void passthrough (GMarkupParseContext *context,
44 static void parse_error (GMarkupParseContext *context,
48 static const GMarkupParser
57 static GHashTable *config_handlers;
59 /* when this interface is stable, export it. */
60 typedef struct MonoParseHandler MonoParseHandler;
62 struct MonoParseHandler {
63 const char *element_name;
64 void*(*init) (MonoImage *assembly);
65 void (*start) (gpointer user_data, const gchar *name,
66 const gchar **attributes,
67 const gchar **values);
68 void (*text) (gpointer user_data, const char *text, gsize test_len);
69 void (*end) (gpointer user_data, const char *name);
70 void (*finish) (gpointer user_data);
74 MonoParseHandler *current;
80 static void start_element (GMarkupParseContext *context,
81 const gchar *element_name,
82 const gchar **attribute_names,
83 const gchar **attribute_values,
87 ParseState *state = user_data;
88 if (!state->current) {
89 state->current = g_hash_table_lookup (config_handlers, element_name);
90 if (state->current && state->current->init)
91 state->user_data = state->current->init (state->assembly);
93 if (state->current && state->current->start)
94 state->current->start (state->user_data, element_name, attribute_names, attribute_values);
97 static void end_element (GMarkupParseContext *context,
98 const gchar *element_name,
102 ParseState *state = user_data;
103 if (state->current) {
104 if (state->current->end)
105 state->current->end (state->user_data, element_name);
106 if (strcmp (state->current->element_name, element_name) == 0) {
107 if (state->current->finish)
108 state->current->finish (state->user_data);
109 state->current = NULL;
110 state->user_data = NULL;
115 static void parse_text (GMarkupParseContext *context,
121 ParseState *state = user_data;
122 if (state->current && state->current->text)
123 state->current->text (state->user_data, text, text_len);
126 static void passthrough (GMarkupParseContext *context,
135 static void parse_error (GMarkupParseContext *context,
148 dllmap_init (MonoImage *assembly) {
149 DllInfo *info = g_new0 (DllInfo, 1);
150 info->assembly = assembly;
155 dllmap_start (gpointer user_data,
156 const gchar *element_name,
157 const gchar **attribute_names,
158 const gchar **attribute_values)
161 DllInfo *info = user_data;
163 if (strcmp (element_name, "dllmap") == 0) {
165 g_free (info->target);
166 info->dll = info->target = NULL;
167 for (i = 0; attribute_names [i]; ++i) {
168 if (strcmp (attribute_names [i], "dll") == 0)
169 info->dll = g_strdup (attribute_values [i]);
170 else if (strcmp (attribute_names [i], "target") == 0)
171 info->target = g_strdup (attribute_values [i]);
173 mono_dllmap_insert (info->assembly, info->dll, NULL, info->target, NULL);
174 } else if (strcmp (element_name, "dllentry") == 0) {
175 const char *name = NULL, *target = NULL, *dll = NULL;
176 for (i = 0; attribute_names [i]; ++i) {
177 if (strcmp (attribute_names [i], "dll") == 0)
178 dll = attribute_values [i];
179 else if (strcmp (attribute_names [i], "target") == 0)
180 target = attribute_values [i];
181 else if (strcmp (attribute_names [i], "name") == 0)
182 name = attribute_values [i];
186 mono_dllmap_insert (info->assembly, info->dll, name, dll, target);
191 dllmap_finish (gpointer user_data)
193 DllInfo *info = user_data;
196 g_free (info->target);
200 static const MonoParseHandler
210 static int inited = 0;
213 mono_config_init (void)
216 config_handlers = g_hash_table_new (g_str_hash, g_str_equal);
217 g_hash_table_insert (config_handlers, (gpointer) dllmap_handler.element_name, (gpointer) &dllmap_handler);
220 /* FIXME: error handling */
223 mono_config_parse_xml_with_context (ParseState *state, const char *text, gsize len)
225 GMarkupParseContext *context;
230 context = g_markup_parse_context_new (&mono_parser, 0, state, NULL);
231 if (g_markup_parse_context_parse (context, text, len, NULL)) {
232 g_markup_parse_context_end_parse (context, NULL);
234 g_markup_parse_context_free (context);
237 /* If assembly is NULL, parse in the global context */
239 mono_config_parse_file_with_context (ParseState *state, const char *filename)
244 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_CONFIG,
245 "Config attempting to parse: '%s'.", filename);
247 if (!g_file_get_contents (filename, &text, &len, NULL))
249 mono_config_parse_xml_with_context (state, text, len);
255 * mono_config_parse_memory:
256 * @buffer: a pointer to an string XML representation of the configuration
258 * Parses the configuration from a buffer
261 mono_config_parse_memory (const char *buffer)
263 ParseState state = {NULL};
265 mono_config_parse_xml_with_context (&state, buffer, strlen (buffer));
269 mono_config_parse_file (const char *filename)
271 ParseState state = {NULL};
272 mono_config_parse_file_with_context (&state, filename);
276 * use the equivalent lookup code from the GAC when available.
277 * Depending on state, this should give something like:
278 * aname/version-pubtoken/
283 get_assembly_filename (MonoImage *image, int state)
287 return g_strdup (mono_image_get_name (image));
293 typedef struct _BundledConfig BundledConfig;
295 struct _BundledConfig {
298 const char* config_xml;
301 static BundledConfig *bundled_configs = NULL;
304 mono_register_config_for_assembly (const char* assembly_name, const char* config_xml)
306 BundledConfig *bconfig;
308 bconfig = g_new0 (BundledConfig, 1);
309 bconfig->aname = assembly_name;
310 bconfig->config_xml = config_xml;
311 bconfig->next = bundled_configs;
312 bundled_configs = bconfig;
316 mono_config_for_assembly (MonoImage *assembly)
318 ParseState state = {NULL};
320 char *aname, *cfg, *cfg_name;
322 BundledConfig *bconfig;
324 state.assembly = assembly;
326 for (bconfig = bundled_configs; bconfig; bconfig = bconfig->next) {
327 if (bconfig->aname && strcmp (bconfig->aname, assembly->module_name) == 0)
328 mono_config_parse_xml_with_context (&state, bconfig->config_xml, strlen (bconfig->config_xml));
331 cfg_name = g_strdup_printf ("%s.config", mono_image_get_filename (assembly));
332 mono_config_parse_file_with_context (&state, cfg_name);
335 cfg_name = g_strdup_printf ("%s.config", mono_image_get_name (assembly));
337 home = g_get_home_dir ();
339 for (i = 0; (aname = get_assembly_filename (assembly, i)) != NULL; ++i) {
340 cfg = g_build_filename (mono_get_config_dir (), "mono", "assemblies", aname, cfg_name, NULL);
341 got_it += mono_config_parse_file_with_context (&state, cfg);
344 #ifndef PLATFORM_WIN32
345 cfg = g_build_filename (home, ".mono", "assemblies", aname, cfg_name, NULL);
346 got_it += mono_config_parse_file_with_context (&state, cfg);
357 * Pass a NULL filename to parse the default config files
358 * (or the file in the MONO_CONFIG env var).
361 mono_config_parse (const char *filename) {
364 #ifndef PLATFORM_WIN32
369 mono_config_parse_file (filename);
373 home = g_getenv ("MONO_CONFIG");
375 mono_config_parse_file (home);
379 mono_cfg = g_build_filename (mono_get_config_dir (), "mono", "config", NULL);
380 mono_config_parse_file (mono_cfg);
383 #ifndef PLATFORM_WIN32
384 home = g_get_home_dir ();
385 user_cfg = g_strconcat (home, G_DIR_SEPARATOR_S, ".mono/config", NULL);
386 mono_config_parse_file (user_cfg);
391 static const char *mono_cfg_dir = NULL;
394 mono_install_get_config_dir (void)
396 #ifdef PLATFORM_WIN32
400 mono_cfg_dir = g_getenv ("MONO_CFG_DIR");
403 #ifndef PLATFORM_WIN32
404 mono_cfg_dir = MONO_CFG_DIR;
406 prefix = g_path_get_dirname (mono_assembly_getrootdir ());
407 mono_cfg_dir = g_build_filename (prefix, "etc", NULL);
414 mono_get_config_dir (void)
417 mono_install_get_config_dir ();