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);
358 * @filename: the filename to load the configuration variables from.
360 * Pass a NULL filename to parse the default config files
361 * (or the file in the MONO_CONFIG env var).
364 mono_config_parse (const char *filename) {
367 #ifndef PLATFORM_WIN32
372 mono_config_parse_file (filename);
376 home = g_getenv ("MONO_CONFIG");
378 mono_config_parse_file (home);
382 mono_cfg = g_build_filename (mono_get_config_dir (), "mono", "config", NULL);
383 mono_config_parse_file (mono_cfg);
386 #ifndef PLATFORM_WIN32
387 home = g_get_home_dir ();
388 user_cfg = g_strconcat (home, G_DIR_SEPARATOR_S, ".mono/config", NULL);
389 mono_config_parse_file (user_cfg);
394 static const char *mono_cfg_dir = NULL;
396 /* Invoked during startup */
398 mono_set_config_dir (const char *dir)
400 /* If this variable is set, overrides the directory computed */
401 mono_cfg_dir = g_getenv ("MONO_CFG_DIR");
402 if (mono_cfg_dir == NULL)
403 mono_cfg_dir = g_strdup (dir);
407 mono_get_config_dir (void)
409 if (mono_cfg_dir == NULL)
410 mono_set_dirs (NULL, NULL);
416 publisher_policy_start (gpointer user_data,
417 const gchar *element_name,
418 const gchar **attribute_names,
419 const gchar **attribute_values)
421 MonoAssemblyBindingInfo *info;
425 if (!strcmp (element_name, "assemblyIdentity")) {
426 for (n = 0; attribute_names [n]; n++) {
427 const gchar *attribute_name = attribute_names [n];
429 if (!strcmp (attribute_name, "name"))
430 info->name = g_strdup (attribute_values [n]);
431 else if (!strcmp (attribute_name, "publicKeyToken")) {
432 if (strlen (attribute_values [n]) == MONO_PUBLIC_KEY_TOKEN_LENGTH - 1)
433 g_strlcpy ((char *) info->public_key_token, attribute_values [n], MONO_PUBLIC_KEY_TOKEN_LENGTH);
434 } else if (!strcmp (attribute_name, "culture")) {
435 if (!strcmp (attribute_values [n], "neutral"))
436 info->culture = g_strdup ("");
438 info->culture = g_strdup (attribute_values [n]);
441 } else if (!strcmp (element_name, "bindingRedirect")) {
442 for (n = 0; attribute_names [n]; n++) {
443 const gchar *attribute_name = attribute_names [n];
445 if (!strcmp (attribute_name, "oldVersion")) {
446 gchar **numbers, **version, **versions;
447 gint major, minor, build, revision;
450 if (!strcmp (attribute_values [n], ""))
453 versions = g_strsplit (attribute_values [n], "-", 2);
454 version = g_strsplit (*versions, ".", 4);
456 /* We assign the values to gint vars to do the checks */
458 major = *numbers ? atoi (*numbers++) : -1;
459 minor = *numbers ? atoi (*numbers++) : -1;
460 build = *numbers ? atoi (*numbers++) : -1;
461 revision = *numbers ? atoi (*numbers) : -1;
462 g_strfreev (version);
463 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
464 g_strfreev (versions);
468 info->old_version_bottom.major = major;
469 info->old_version_bottom.minor = minor;
470 info->old_version_bottom.build = build;
471 info->old_version_bottom.revision = revision;
472 info->has_old_version_bottom = TRUE;
474 if (!*(versions + 1)) {
475 g_strfreev (versions);
479 numbers = version = g_strsplit (*(versions + 1), ".", 4);
480 major = *numbers ? atoi (*numbers++) : -1;
481 minor = *numbers ? atoi (*numbers++) : -1;
482 build = *numbers ? atoi (*numbers++) : -1;
483 revision = *numbers ? atoi (*numbers) : 1;
484 g_strfreev (version);
485 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
486 g_strfreev (versions);
490 info->old_version_top.major = major;
491 info->old_version_top.minor = minor;
492 info->old_version_top.build = build;
493 info->old_version_top.revision = revision;
494 info->has_old_version_top = TRUE;
496 g_strfreev (versions);
497 } else if (!strcmp (attribute_name, "newVersion")) {
498 gchar **numbers, **version;
501 if (!strcmp (attribute_values [n], ""))
504 numbers = version = g_strsplit (attribute_values [n], ".", 4);
505 info->new_version.major = *numbers ? atoi (*numbers++) : -1;
506 info->new_version.minor = *numbers ? atoi (*numbers++) : -1;
507 info->new_version.build = *numbers ? atoi (*numbers++) : -1;
508 info->new_version.revision = *numbers ? atoi (*numbers) : -1;
509 info->has_new_version = TRUE;
510 g_strfreev (version);
516 static MonoParseHandler
517 publisher_policy_parser = {
518 "", /* We don't need to use declare an xml element */
520 publisher_policy_start,
527 mono_config_parse_publisher_policy (const gchar *filename, MonoAssemblyBindingInfo *info)
530 &publisher_policy_parser, /* MonoParseHandler */
531 info, /* user_data */
532 NULL, /* MonoImage (we don't need it right now)*/
533 TRUE /* We are already inited */
536 mono_config_parse_file_with_context (&state, filename);