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