Merge pull request #4621 from alexanderkyte/strdup_env
[mono.git] / mono / metadata / mono-config.c
1 /**
2  * \file
3  *
4  * Runtime and assembly configuration file support routines.
5  *
6  * Author: Paolo Molaro (lupus@ximian.com)
7  *
8  * Copyright 2002-2003 Ximian, Inc (http://www.ximian.com)
9  * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
10  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
11  */
12 #include "config.h"
13 #include <glib.h>
14 #include <string.h>
15
16 #include "mono/metadata/assembly.h"
17 #include "mono/metadata/loader.h"
18 #include "mono/metadata/mono-config.h"
19 #include "mono/metadata/metadata-internals.h"
20 #include "mono/metadata/object-internals.h"
21 #include "mono/utils/mono-logger-internals.h"
22
23 #if defined(TARGET_PS3)
24 #define CONFIG_OS "CellOS"
25 #elif defined(__linux__)
26 #define CONFIG_OS "linux"
27 #elif defined(__APPLE__)
28 #define CONFIG_OS "osx"
29 #elif defined(sun)
30 #define CONFIG_OS "solaris"
31 #elif defined(__FreeBSD__)
32 #define CONFIG_OS "freebsd"
33 #elif defined(__NetBSD__)
34 #define CONFIG_OS "netbsd"
35 #elif defined(__OpenBSD__)
36 #define CONFIG_OS "openbsd"
37 #elif defined(__WIN32__) || defined(TARGET_WIN32)
38 #define CONFIG_OS "windows"
39 #elif defined(_IBMR2)
40 #define CONFIG_OS "aix"
41 #elif defined(__hpux)
42 #define CONFIG_OS "hpux"
43 #elif defined(__HAIKU__)
44 #define CONFIG_OS "haiku"
45 #else
46 #warning Unknown operating system
47 #define CONFIG_OS "unknownOS"
48 #endif
49
50 #ifndef CONFIG_CPU
51 #if defined(__i386__) || defined(TARGET_X86)
52 #define CONFIG_CPU "x86"
53 #define CONFIG_WORDSIZE "32"
54 #elif defined(__x86_64__) || defined(TARGET_AMD64)
55 #define CONFIG_CPU "x86-64"
56 #define CONFIG_WORDSIZE "64"
57 #elif defined(sparc) || defined(__sparc__)
58 #define CONFIG_CPU "sparc"
59 #define CONFIG_WORDSIZE "32"
60 #elif defined(__ppc64__) || defined(__powerpc64__) || defined(TARGET_POWERPC)
61 #define CONFIG_WORDSIZE "64"
62 #ifdef __mono_ppc_ilp32__ 
63 #   define CONFIG_CPU "ppc64ilp32"
64 #else
65 #   define CONFIG_CPU "ppc64"
66 #endif
67 #elif defined(__ppc__) || defined(__powerpc__)
68 #define CONFIG_CPU "ppc"
69 #define CONFIG_WORDSIZE "32"
70 #elif defined(__s390x__)
71 #define CONFIG_CPU "s390x"
72 #define CONFIG_WORDSIZE "64"
73 #elif defined(__s390__)
74 #define CONFIG_CPU "s390"
75 #define CONFIG_WORDSIZE "32"
76 #elif defined(__arm__)
77 #define CONFIG_CPU "arm"
78 #define CONFIG_WORDSIZE "32"
79 #elif defined(__aarch64__)
80 #define CONFIG_CPU "armv8"
81 #define CONFIG_WORDSIZE "64"
82 #elif defined(__ia64__)
83 #define CONFIG_CPU "ia64"
84 #define CONFIG_WORDSIZE "64"
85 #elif defined(mips) || defined(__mips) || defined(_mips)
86 #define CONFIG_CPU "mips"
87 #define CONFIG_WORDSIZE "32"
88 #else
89 #error Unknown CPU
90 #define CONFIG_CPU "unknownCPU"
91 #endif
92 #endif
93
94 /**
95  * mono_config_get_os:
96  *
97  * Returns the operating system that Mono is running on, as used for dllmap entries.
98  */
99 const char *
100 mono_config_get_os (void)
101 {
102         return CONFIG_OS;
103 }
104
105 /**
106  * mono_config_get_cpu:
107  *
108  * Returns the architecture that Mono is running on, as used for dllmap entries.
109  */
110 const char *
111 mono_config_get_cpu (void)
112 {
113         return CONFIG_CPU;
114 }
115
116 /**
117  * mono_config_get_wordsize:
118  *
119  * Returns the word size that Mono is running on, as used for dllmap entries.
120  */
121 const char *
122 mono_config_get_wordsize (void)
123 {
124         return CONFIG_WORDSIZE;
125 }
126
127 static void start_element (GMarkupParseContext *context, 
128                            const gchar         *element_name,
129                            const gchar        **attribute_names,
130                            const gchar        **attribute_values,
131                            gpointer             user_data,
132                            GError             **error);
133
134 static void end_element   (GMarkupParseContext *context,
135                            const gchar         *element_name,
136                            gpointer             user_data,
137                            GError             **error);
138
139 static void parse_text    (GMarkupParseContext *context,
140                            const gchar         *text,
141                            gsize                text_len,
142                            gpointer             user_data,
143                            GError             **error);
144
145 static void passthrough   (GMarkupParseContext *context,
146                            const gchar         *text,
147                            gsize                text_len,
148                            gpointer             user_data,
149                            GError             **error);
150
151 static void parse_error   (GMarkupParseContext *context,
152                            GError              *error,
153                            gpointer             user_data);
154
155 static const GMarkupParser 
156 mono_parser = {
157         start_element,
158         end_element,
159         parse_text,
160         passthrough,
161         parse_error
162 };
163
164 static GHashTable *config_handlers;
165
166 static char *mono_cfg_dir = NULL;
167
168 /* when this interface is stable, export it. */
169 typedef struct MonoParseHandler MonoParseHandler;
170
171 struct MonoParseHandler {
172         const char *element_name;
173         void*(*init)   (MonoImage *assembly);
174         void (*start)  (gpointer user_data, const gchar *name,
175                         const gchar **attributes,
176                         const gchar **values);
177         void (*text)   (gpointer user_data, const char *text, gsize test_len);
178         void (*end)    (gpointer user_data, const char *name);
179         void (*finish) (gpointer user_data);
180 };
181
182 typedef struct {
183         MonoAssemblyBindingInfo *info;
184         void (*info_parsed)(MonoAssemblyBindingInfo *info, void *user_data);
185         void *user_data;
186 } ParserUserData;
187
188 typedef struct {
189         MonoParseHandler *current;
190         void *user_data;
191         MonoImage *assembly;
192         int inited;
193 } ParseState;
194
195 static void start_element (GMarkupParseContext *context, 
196                            const gchar         *element_name,
197                            const gchar        **attribute_names,
198                            const gchar        **attribute_values,
199                            gpointer             user_data,
200                            GError             **error)
201 {
202         ParseState *state = (ParseState *)user_data;
203         if (!state->current) {
204                 state->current = (MonoParseHandler *)g_hash_table_lookup (config_handlers, element_name);
205                 if (state->current && state->current->init)
206                         state->user_data = state->current->init (state->assembly);
207         }
208         if (state->current && state->current->start)
209                 state->current->start (state->user_data, element_name, attribute_names, attribute_values);
210 }
211
212 static void end_element   (GMarkupParseContext *context,
213                            const gchar         *element_name,
214                            gpointer             user_data,
215                            GError             **error)
216 {
217         ParseState *state = (ParseState *)user_data;
218         if (state->current) {
219                 if (state->current->end)
220                         state->current->end (state->user_data, element_name);
221                 if (strcmp (state->current->element_name, element_name) == 0) {
222                         if (state->current->finish)
223                                 state->current->finish (state->user_data);
224                         state->current = NULL;
225                         state->user_data = NULL;
226                 }
227         }
228 }
229
230 static void parse_text    (GMarkupParseContext *context,
231                            const gchar         *text,
232                            gsize                text_len,
233                            gpointer             user_data,
234                            GError             **error)
235 {
236         ParseState *state = (ParseState *)user_data;
237         if (state->current && state->current->text)
238                 state->current->text (state->user_data, text, text_len);
239 }
240
241 static void passthrough   (GMarkupParseContext *context,
242                            const gchar         *text,
243                            gsize                text_len,
244                            gpointer             user_data,
245                            GError             **error)
246 {
247         /* do nothing */
248 }
249
250 static void parse_error   (GMarkupParseContext *context,
251                            GError              *error,
252                            gpointer             user_data)
253 {
254         ParseState *state = (ParseState *)user_data;
255         const gchar *msg;
256         const gchar *filename;
257
258         filename = state && state->user_data ? (gchar *) state->user_data : "<unknown>";
259         msg = error && error->message ? error->message : "";
260         g_warning ("Error parsing %s: %s", filename, msg);
261 }
262
263 static int
264 arch_matches (const char* arch, const char *value)
265 {
266         char **splitted, **p;
267         int found = FALSE;
268         if (value [0] == '!')
269                 return !arch_matches (arch, value + 1);
270         p = splitted = g_strsplit (value, ",", 0);
271         while (*p) {
272                 if (strcmp (arch, *p) == 0) {
273                         found = TRUE;
274                         break;
275                 }
276                 p++;
277         }
278         g_strfreev (splitted);
279         return found;
280 }
281
282 typedef struct {
283         char *dll;
284         char *target;
285         int ignore;
286         MonoImage *assembly;
287 } DllInfo;
288
289 static void*
290 dllmap_init (MonoImage *assembly) {
291         DllInfo *info = g_new0 (DllInfo, 1);
292         info->assembly = assembly;
293         return info;
294 }
295
296 static void
297 dllmap_start (gpointer user_data, 
298               const gchar         *element_name,
299               const gchar        **attribute_names,
300               const gchar        **attribute_values)
301 {
302         int i;
303         DllInfo *info = (DllInfo *)user_data;
304         
305         if (strcmp (element_name, "dllmap") == 0) {
306                 g_free (info->dll);
307                 g_free (info->target);
308                 info->dll = info->target = NULL;
309                 info->ignore = FALSE;
310                 for (i = 0; attribute_names [i]; ++i) {
311                         if (strcmp (attribute_names [i], "dll") == 0)
312                                 info->dll = g_strdup (attribute_values [i]);
313                         else if (strcmp (attribute_names [i], "target") == 0){
314                                 char *p = strstr (attribute_values [i], "$mono_libdir");
315                                 if (p != NULL){
316                                         char *libdir = mono_native_getrootdir ();
317                                         size_t libdir_len = strlen (libdir);
318                                         char *result;
319                                         
320                                         result = (char *)g_malloc (libdir_len-strlen("$mono_libdir")+strlen(attribute_values[i])+1);
321                                         strncpy (result, attribute_values[i], p-attribute_values[i]);
322                                         strcpy (result+(p-attribute_values[i]), libdir);
323                                         g_free (libdir);
324                                         strcat (result, p+strlen("$mono_libdir"));
325                                         info->target = result;
326                                 } else 
327                                         info->target = g_strdup (attribute_values [i]);
328                         } else if (strcmp (attribute_names [i], "os") == 0 && !arch_matches (CONFIG_OS, attribute_values [i]))
329                                 info->ignore = TRUE;
330                         else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
331                                 info->ignore = TRUE;
332                         else if (strcmp (attribute_names [i], "wordsize") == 0 && !arch_matches (CONFIG_WORDSIZE, attribute_values [i]))
333                                 info->ignore = TRUE;
334                 }
335                 if (!info->ignore)
336                         mono_dllmap_insert (info->assembly, info->dll, NULL, info->target, NULL);
337         } else if (strcmp (element_name, "dllentry") == 0) {
338                 const char *name = NULL, *target = NULL, *dll = NULL;
339                 int ignore = FALSE;
340                 for (i = 0; attribute_names [i]; ++i) {
341                         if (strcmp (attribute_names [i], "dll") == 0)
342                                 dll = attribute_values [i];
343                         else if (strcmp (attribute_names [i], "target") == 0)
344                                 target = attribute_values [i];
345                         else if (strcmp (attribute_names [i], "name") == 0)
346                                 name = attribute_values [i];
347                         else if (strcmp (attribute_names [i], "os") == 0 && !arch_matches (CONFIG_OS, attribute_values [i]))
348                                 ignore = TRUE;
349                         else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
350                                 ignore = TRUE;
351                         else if (strcmp (attribute_names [i], "wordsize") == 0 && !arch_matches (CONFIG_WORDSIZE, attribute_values [i]))
352                                 ignore = TRUE;
353                 }
354                 if (!dll)
355                         dll = info->dll;
356                 if (!info->ignore && !ignore)
357                         mono_dllmap_insert (info->assembly, info->dll, name, dll, target);
358         }
359 }
360
361 static void
362 dllmap_finish (gpointer user_data)
363 {
364         DllInfo *info = (DllInfo *)user_data;
365
366         g_free (info->dll);
367         g_free (info->target);
368         g_free (info);
369 }
370
371 static const MonoParseHandler
372 dllmap_handler = {
373         "dllmap",
374         dllmap_init,
375         dllmap_start,
376         NULL, /* text */
377         NULL, /* end */
378         dllmap_finish
379 };
380
381 static void
382 legacyUEP_start (gpointer user_data, 
383               const gchar         *element_name,
384               const gchar        **attribute_names,
385               const gchar        **attribute_values) {
386         if ((strcmp (element_name, "legacyUnhandledExceptionPolicy") == 0) &&
387                         (attribute_names [0] != NULL) &&
388                         (strcmp (attribute_names [0], "enabled") == 0)) {
389                 if ((strcmp (attribute_values [0], "1") == 0) ||
390                                 (g_ascii_strcasecmp (attribute_values [0], "true") == 0)) {
391                         mono_runtime_unhandled_exception_policy_set (MONO_UNHANDLED_POLICY_LEGACY);
392                 }
393         }
394 }
395
396 static const MonoParseHandler
397 legacyUEP_handler = {
398         "legacyUnhandledExceptionPolicy",
399         NULL, /* init */
400         legacyUEP_start,
401         NULL, /* text */
402         NULL, /* end */
403         NULL, /* finish */
404 };
405
406 static void
407 aot_cache_start (gpointer user_data,
408                                  const gchar         *element_name,
409                                  const gchar        **attribute_names,
410                                  const gchar        **attribute_values)
411 {
412         int i;
413         MonoAotCacheConfig *config;
414
415         if (strcmp (element_name, "aotcache") != 0)
416                 return;
417
418         config = mono_get_aot_cache_config ();
419
420         /* Per-app configuration */
421         for (i = 0; attribute_names [i]; ++i) {
422                 if (!strcmp (attribute_names [i], "app")) {
423                         config->apps = g_slist_prepend (config->apps, g_strdup (attribute_values [i]));
424                 }
425         }
426
427         /* Global configuration */
428         for (i = 0; attribute_names [i]; ++i) {
429                 if (!strcmp (attribute_names [i], "assemblies")) {
430                         char **parts, **ptr;
431                         char *part;
432
433                         parts = g_strsplit (attribute_values [i], " ", -1);
434                         for (ptr = parts; ptr && *ptr; ptr ++) {
435                                 part = *ptr;
436                                 config->assemblies = g_slist_prepend (config->assemblies, g_strdup (part));
437                         }
438                         g_strfreev (parts);
439                 } else if (!strcmp (attribute_names [i], "options")) {
440                         config->aot_options = g_strdup (attribute_values [i]);
441                 }
442         }
443 }
444
445 static const MonoParseHandler
446 aot_cache_handler = {
447         "aotcache",
448         NULL, /* init */
449         aot_cache_start,
450         NULL, /* text */
451         NULL, /* end */
452         NULL, /* finish */
453 };
454
455 static int inited = 0;
456
457 static void
458 mono_config_init (void)
459 {
460         inited = 1;
461         config_handlers = g_hash_table_new (g_str_hash, g_str_equal);
462         g_hash_table_insert (config_handlers, (gpointer) dllmap_handler.element_name, (gpointer) &dllmap_handler);
463         g_hash_table_insert (config_handlers, (gpointer) legacyUEP_handler.element_name, (gpointer) &legacyUEP_handler);
464         g_hash_table_insert (config_handlers, (gpointer) aot_cache_handler.element_name, (gpointer) &aot_cache_handler);
465 }
466
467 /**
468  * mono_config_cleanup:
469  */
470 void
471 mono_config_cleanup (void)
472 {
473         if (config_handlers)
474                 g_hash_table_destroy (config_handlers);
475         g_free (mono_cfg_dir);
476 }
477
478 /* FIXME: error handling */
479
480 static void
481 mono_config_parse_xml_with_context (ParseState *state, const char *text, gsize len)
482 {
483         GMarkupParseContext *context;
484
485         if (!inited)
486                 mono_config_init ();
487
488         context = g_markup_parse_context_new (&mono_parser, (GMarkupParseFlags)0, state, NULL);
489         if (g_markup_parse_context_parse (context, text, len, NULL)) {
490                 g_markup_parse_context_end_parse (context, NULL);
491         }
492         g_markup_parse_context_free (context);
493 }
494
495 /* If assembly is NULL, parse in the global context */
496 static int
497 mono_config_parse_file_with_context (ParseState *state, const char *filename)
498 {
499         gchar *text;
500         gsize len;
501         gint offset;
502
503         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_CONFIG,
504                         "Config attempting to parse: '%s'.", filename);
505
506         if (!g_file_get_contents (filename, &text, &len, NULL))
507                 return 0;
508
509         offset = 0;
510         if (len > 3 && text [0] == '\xef' && text [1] == (gchar) '\xbb' && text [2] == '\xbf')
511                 offset = 3; /* Skip UTF-8 BOM */
512         if (state->user_data == NULL)
513                 state->user_data = (gpointer) filename;
514         mono_config_parse_xml_with_context (state, text + offset, len - offset);
515         g_free (text);
516         return 1;
517 }
518
519 /**
520  * mono_config_parse_memory:
521  * \param buffer a pointer to an string XML representation of the configuration
522  * Parses the configuration from a buffer
523  */
524 void
525 mono_config_parse_memory (const char *buffer)
526 {
527         ParseState state = {NULL};
528
529         state.user_data = (gpointer) "<buffer>";
530         mono_config_parse_xml_with_context (&state, buffer, strlen (buffer));
531 }
532
533 static void
534 mono_config_parse_file (const char *filename)
535 {
536         ParseState state = {NULL};
537         state.user_data = (gpointer) filename;
538         mono_config_parse_file_with_context (&state, filename);
539 }
540
541 /* 
542  * use the equivalent lookup code from the GAC when available.
543  * Depending on state, this should give something like:
544  *      aname/version-pubtoken/
545  *      aname/version/
546  *      aname
547  */
548 static char*
549 get_assembly_filename (MonoImage *image, int state)
550 {
551         switch (state) {
552         case 0:
553                 return g_strdup (mono_image_get_name (image));
554         default:
555                 return NULL;
556         }
557 }
558
559 typedef struct _BundledConfig BundledConfig;
560
561 struct _BundledConfig {
562         BundledConfig *next;
563         const char* aname;
564         const char* config_xml;
565 };
566
567 static BundledConfig *bundled_configs = NULL;
568
569 static const char *bundled_machine_config = NULL;
570
571 /**
572  * mono_register_config_for_assembly:
573  */
574 void
575 mono_register_config_for_assembly (const char* assembly_name, const char* config_xml)
576 {
577         BundledConfig *bconfig;
578
579         bconfig = g_new0 (BundledConfig, 1);
580         bconfig->aname = assembly_name;
581         bconfig->config_xml = config_xml;
582         bconfig->next = bundled_configs;
583         bundled_configs = bconfig;
584 }
585
586 /**
587  * mono_config_string_for_assembly_file:
588  */
589 const char *
590 mono_config_string_for_assembly_file (const char *filename)
591 {
592         BundledConfig *bconfig;
593         
594         for (bconfig = bundled_configs; bconfig; bconfig = bconfig->next) {
595                 if (bconfig->aname && strcmp (bconfig->aname, filename) == 0)
596                         return bconfig->config_xml;
597         }
598         return NULL;
599 }
600
601 /**
602  * mono_config_for_assembly:
603  */
604 void 
605 mono_config_for_assembly (MonoImage *assembly)
606 {
607         ParseState state = {NULL};
608         int got_it = 0, i;
609         char *aname, *cfg, *cfg_name;
610         const char *bundled_config;
611         
612         state.assembly = assembly;
613
614         bundled_config = mono_config_string_for_assembly_file (assembly->module_name);
615         if (bundled_config) {
616                 state.user_data = (gpointer) "<bundled>";
617                 mono_config_parse_xml_with_context (&state, bundled_config, strlen (bundled_config));
618         }
619
620         cfg_name = g_strdup_printf ("%s.config", mono_image_get_filename (assembly));
621         mono_config_parse_file_with_context (&state, cfg_name);
622         g_free (cfg_name);
623
624         cfg_name = g_strdup_printf ("%s.config", mono_image_get_name (assembly));
625
626         for (i = 0; (aname = get_assembly_filename (assembly, i)) != NULL; ++i) {
627                 cfg = g_build_filename (mono_get_config_dir (), "mono", "assemblies", aname, cfg_name, NULL);
628                 got_it += mono_config_parse_file_with_context (&state, cfg);
629                 g_free (cfg);
630
631 #ifdef TARGET_WIN32
632                 const char *home = g_get_home_dir ();
633                 cfg = g_build_filename (home, ".mono", "assemblies", aname, cfg_name, NULL);
634                 got_it += mono_config_parse_file_with_context (&state, cfg);
635                 g_free (cfg);
636 #endif
637                 g_free (aname);
638                 if (got_it)
639                         break;
640         }
641         g_free (cfg_name);
642 }
643
644 /**
645  * mono_config_parse:
646  * \param filename the filename to load the configuration variables from.
647  * Pass a NULL filename to parse the default config files
648  * (or the file in the \c MONO_CONFIG env var).
649  */
650 void
651 mono_config_parse (const char *filename) {
652         const char *home;
653         char *mono_cfg;
654 #ifndef TARGET_WIN32
655         char *user_cfg;
656 #endif
657
658         if (filename) {
659                 mono_config_parse_file (filename);
660                 return;
661         }
662
663         // FIXME: leak, do we store any references to home
664         char *env_home = g_getenv ("MONO_CONFIG");
665         if (env_home) {
666                 mono_config_parse_file (env_home);
667                 return;
668         }
669
670         mono_cfg = g_build_filename (mono_get_config_dir (), "mono", "config", NULL);
671         mono_config_parse_file (mono_cfg);
672         g_free (mono_cfg);
673
674 #if !defined(TARGET_WIN32) && !defined(__native_client__)
675         home = g_get_home_dir ();
676         user_cfg = g_strconcat (home, G_DIR_SEPARATOR_S, ".mono/config", NULL);
677         mono_config_parse_file (user_cfg);
678         g_free (user_cfg);
679 #endif
680 }
681
682 /**
683  * mono_set_config_dir:
684  * Invoked during startup
685  */
686 void
687 mono_set_config_dir (const char *dir)
688 {
689         /* If this environment variable is set, overrides the directory computed */
690         char *env_mono_cfg_dir = g_getenv ("MONO_CFG_DIR");
691         if (env_mono_cfg_dir == NULL && dir != NULL)
692                 env_mono_cfg_dir = strdup (dir);
693
694         mono_cfg_dir = env_mono_cfg_dir;
695 }
696
697 /**
698  * mono_get_config_dir:
699  */
700 const char* 
701 mono_get_config_dir (void)
702 {
703         if (mono_cfg_dir == NULL)
704                 mono_set_dirs (NULL, NULL);
705
706         return mono_cfg_dir;
707 }
708
709 /**
710  * mono_register_machine_config:
711  */
712 void
713 mono_register_machine_config (const char *config_xml)
714 {
715         bundled_machine_config = config_xml;
716 }
717
718 /**
719  * mono_get_machine_config:
720  */
721 const char *
722 mono_get_machine_config (void)
723 {
724         return bundled_machine_config;
725 }
726
727 static void
728 assembly_binding_end (gpointer user_data, const char *element_name)
729 {
730         ParserUserData *pud = (ParserUserData *)user_data;
731
732         if (!strcmp (element_name, "dependentAssembly")) {
733                 if (pud->info_parsed && pud->info) {
734                         pud->info_parsed (pud->info, pud->user_data);
735                         g_free (pud->info->name);
736                         g_free (pud->info->culture);
737                 }
738         }
739 }
740
741 static void
742 publisher_policy_start (gpointer user_data,
743                 const gchar *element_name,
744                 const gchar **attribute_names,
745                 const gchar **attribute_values)
746 {
747         ParserUserData *pud;
748         MonoAssemblyBindingInfo *info;
749         int n;
750
751         pud = (ParserUserData *)user_data;
752         info = pud->info;
753         if (!strcmp (element_name, "dependentAssembly")) {
754                 info->name = NULL;
755                 info->culture = NULL;
756                 info->has_old_version_bottom = FALSE;
757                 info->has_old_version_top = FALSE;
758                 info->has_new_version = FALSE;
759                 info->is_valid = FALSE;
760                 memset (&info->old_version_bottom, 0, sizeof (info->old_version_bottom));
761                 memset (&info->old_version_top, 0, sizeof (info->old_version_top));
762                 memset (&info->new_version, 0, sizeof (info->new_version));
763         } else if (!strcmp (element_name, "assemblyIdentity")) {
764                 for (n = 0; attribute_names [n]; n++) {
765                         const gchar *attribute_name = attribute_names [n];
766                         
767                         if (!strcmp (attribute_name, "name"))
768                                 info->name = g_strdup (attribute_values [n]);
769                         else if (!strcmp (attribute_name, "publicKeyToken")) {
770                                 if (strlen (attribute_values [n]) == MONO_PUBLIC_KEY_TOKEN_LENGTH - 1)
771                                         g_strlcpy ((char *) info->public_key_token, attribute_values [n], MONO_PUBLIC_KEY_TOKEN_LENGTH);
772                         } else if (!strcmp (attribute_name, "culture")) {
773                                 if (!strcmp (attribute_values [n], "neutral"))
774                                         info->culture = g_strdup ("");
775                                 else
776                                         info->culture = g_strdup (attribute_values [n]);
777                         }
778                 }
779         } else if (!strcmp (element_name, "bindingRedirect")) {
780                 for (n = 0; attribute_names [n]; n++) {
781                         const gchar *attribute_name = attribute_names [n];
782
783                         if (!strcmp (attribute_name, "oldVersion")) {
784                                 gchar **numbers, **version, **versions;
785                                 gint major, minor, build, revision;
786
787                                 /* Invalid value */
788                                 if (!strcmp (attribute_values [n], ""))
789                                         return;
790                                 
791                                 versions = g_strsplit (attribute_values [n], "-", 2);
792                                 version = g_strsplit (*versions, ".", 4);
793
794                                 /* We assign the values to gint vars to do the checks */
795                                 numbers = version;
796                                 major = *numbers ? atoi (*numbers++) : -1;
797                                 minor = *numbers ? atoi (*numbers++) : -1;
798                                 build = *numbers ? atoi (*numbers++) : -1;
799                                 revision = *numbers ? atoi (*numbers) : -1;
800                                 g_strfreev (version);
801                                 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
802                                         g_strfreev (versions);
803                                         return;
804                                 }
805
806                                 info->old_version_bottom.major = major;
807                                 info->old_version_bottom.minor = minor;
808                                 info->old_version_bottom.build = build;
809                                 info->old_version_bottom.revision = revision;
810                                 info->has_old_version_bottom = TRUE;
811
812                                 if (!*(versions + 1)) {
813                                         g_strfreev (versions);
814                                         continue;
815                                 }
816                                 
817                                 numbers = version = g_strsplit (*(versions + 1), ".", 4);
818                                 major = *numbers ? atoi (*numbers++) : -1;
819                                 minor = *numbers ? atoi (*numbers++) : -1;
820                                 build = *numbers ? atoi (*numbers++) : -1;
821                                 revision = *numbers ? atoi (*numbers) : 1;
822                                 g_strfreev (version);
823                                 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
824                                         g_strfreev (versions);
825                                         return;
826                                 }
827
828                                 info->old_version_top.major = major;
829                                 info->old_version_top.minor = minor;
830                                 info->old_version_top.build = build;
831                                 info->old_version_top.revision = revision;
832                                 info->has_old_version_top = TRUE;
833
834                                 g_strfreev (versions);
835                         } else if (!strcmp (attribute_name, "newVersion")) {
836                                 gchar **numbers, **version;
837
838                                 /* Invalid value */
839                                 if (!strcmp (attribute_values [n], ""))
840                                         return;
841
842                                 numbers = version = g_strsplit (attribute_values [n], ".", 4);
843                                 info->new_version.major = *numbers ? atoi (*numbers++) : -1;
844                                 info->new_version.minor = *numbers ? atoi (*numbers++) : -1;
845                                 info->new_version.build = *numbers ? atoi (*numbers++) : -1;
846                                 info->new_version.revision = *numbers ? atoi (*numbers) : -1;
847                                 info->has_new_version = TRUE;
848                                 g_strfreev (version);
849                         }
850                 }
851         }
852 }
853
854 static MonoParseHandler
855 publisher_policy_parser = {
856         "", /* We don't need to use declare an xml element */
857         NULL,
858         publisher_policy_start,
859         NULL,
860         NULL,
861         NULL
862 };
863
864 void
865 mono_config_parse_publisher_policy (const gchar *filename, MonoAssemblyBindingInfo *info)
866 {
867         ParserUserData user_data = {
868                 info,
869                 NULL,
870                 NULL
871         };
872         ParseState state = {
873                 &publisher_policy_parser, /* MonoParseHandler */
874                 &user_data, /* user_data */
875                 NULL, /* MonoImage (we don't need it right now)*/
876                 TRUE /* We are already inited */
877         };
878         
879         mono_config_parse_file_with_context (&state, filename);
880 }
881
882 static MonoParseHandler
883 config_assemblybinding_parser = {
884         "", /* We don't need to use declare an xml element */
885         NULL,
886         publisher_policy_start,
887         NULL,
888         assembly_binding_end,
889         NULL
890 };
891
892 void
893 mono_config_parse_assembly_bindings (const char *filename, int amajor, int aminor, void *user_data, void (*infocb)(MonoAssemblyBindingInfo *info, void *user_data))
894 {
895         MonoAssemblyBindingInfo info;
896         ParserUserData pud;
897         ParseState state;
898
899         info.major = amajor;
900         info.minor = aminor;
901
902         pud.info = &info;
903         pud.info_parsed = infocb;
904         pud.user_data = user_data;
905
906         state.current = &config_assemblybinding_parser;  /* MonoParseHandler */
907         state.user_data = &pud;
908         state.assembly = NULL; /* MonoImage (we don't need it right now)*/
909         state.inited = TRUE; /* We are already inited */
910
911         mono_config_parse_file_with_context (&state, filename);
912 }
913
914 static mono_bool mono_server_mode = FALSE;
915
916 /**
917  * mono_config_set_server_mode:
918  */
919 void
920 mono_config_set_server_mode (mono_bool server_mode)
921 {
922         mono_server_mode = server_mode;
923 }
924
925 /**
926  * mono_config_is_server_mode:
927  */
928 mono_bool
929 mono_config_is_server_mode (void)
930 {
931         return mono_server_mode;
932 }
933