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"
15 #include "mono/metadata/metadata-internals.h"
16 #include "mono/utils/mono-logger.h"
18 static void start_element (GMarkupParseContext
*context
,
19 const gchar
*element_name
,
20 const gchar
**attribute_names
,
21 const gchar
**attribute_values
,
25 static void end_element (GMarkupParseContext
*context
,
26 const gchar
*element_name
,
30 static void parse_text (GMarkupParseContext
*context
,
36 static void passthrough (GMarkupParseContext
*context
,
42 static void parse_error (GMarkupParseContext
*context
,
46 static const GMarkupParser
55 static GHashTable
*config_handlers
;
57 /* when this interface is stable, export it. */
58 typedef struct MonoParseHandler MonoParseHandler
;
60 struct MonoParseHandler
{
61 const char *element_name
;
62 void*(*init
) (MonoImage
*assembly
);
63 void (*start
) (gpointer user_data
, const gchar
*name
,
64 const gchar
**attributes
,
65 const gchar
**values
);
66 void (*text
) (gpointer user_data
, const char *text
, gsize test_len
);
67 void (*end
) (gpointer user_data
, const char *name
);
68 void (*finish
) (gpointer user_data
);
72 MonoParseHandler
*current
;
78 static void start_element (GMarkupParseContext
*context
,
79 const gchar
*element_name
,
80 const gchar
**attribute_names
,
81 const gchar
**attribute_values
,
85 ParseState
*state
= user_data
;
86 if (!state
->current
) {
87 state
->current
= g_hash_table_lookup (config_handlers
, element_name
);
88 if (state
->current
&& state
->current
->init
)
89 state
->user_data
= state
->current
->init (state
->assembly
);
91 if (state
->current
&& state
->current
->start
)
92 state
->current
->start (state
->user_data
, element_name
, attribute_names
, attribute_values
);
95 static void end_element (GMarkupParseContext
*context
,
96 const gchar
*element_name
,
100 ParseState
*state
= user_data
;
101 if (state
->current
) {
102 if (state
->current
->end
)
103 state
->current
->end (state
->user_data
, element_name
);
104 if (strcmp (state
->current
->element_name
, element_name
) == 0) {
105 if (state
->current
->finish
)
106 state
->current
->finish (state
->user_data
);
107 state
->current
= NULL
;
108 state
->user_data
= NULL
;
113 static void parse_text (GMarkupParseContext
*context
,
119 ParseState
*state
= user_data
;
120 if (state
->current
&& state
->current
->text
)
121 state
->current
->text (state
->user_data
, text
, text_len
);
124 static void passthrough (GMarkupParseContext
*context
,
133 static void parse_error (GMarkupParseContext
*context
,
146 dllmap_init (MonoImage
*assembly
) {
147 DllInfo
*info
= g_new0 (DllInfo
, 1);
148 info
->assembly
= assembly
;
153 dllmap_start (gpointer user_data
,
154 const gchar
*element_name
,
155 const gchar
**attribute_names
,
156 const gchar
**attribute_values
)
159 DllInfo
*info
= user_data
;
161 if (strcmp (element_name
, "dllmap") == 0) {
163 g_free (info
->target
);
164 info
->dll
= info
->target
= NULL
;
165 for (i
= 0; attribute_names
[i
]; ++i
) {
166 if (strcmp (attribute_names
[i
], "dll") == 0)
167 info
->dll
= g_strdup (attribute_values
[i
]);
168 else if (strcmp (attribute_names
[i
], "target") == 0)
169 info
->target
= g_strdup (attribute_values
[i
]);
171 mono_dllmap_insert (info
->assembly
, info
->dll
, NULL
, info
->target
, NULL
);
172 } else if (strcmp (element_name
, "dllentry") == 0) {
173 const char *name
= NULL
, *target
= NULL
, *dll
= NULL
;
174 for (i
= 0; attribute_names
[i
]; ++i
) {
175 if (strcmp (attribute_names
[i
], "dll") == 0)
176 dll
= attribute_values
[i
];
177 else if (strcmp (attribute_names
[i
], "target") == 0)
178 target
= attribute_values
[i
];
179 else if (strcmp (attribute_names
[i
], "name") == 0)
180 name
= attribute_values
[i
];
184 mono_dllmap_insert (info
->assembly
, info
->dll
, name
, dll
, target
);
189 dllmap_finish (gpointer user_data
)
191 DllInfo
*info
= user_data
;
194 g_free (info
->target
);
198 static const MonoParseHandler
208 static int inited
= 0;
211 mono_config_init (void)
214 config_handlers
= g_hash_table_new (g_str_hash
, g_str_equal
);
215 g_hash_table_insert (config_handlers
, (gpointer
) dllmap_handler
.element_name
, (gpointer
) &dllmap_handler
);
218 /* FIXME: error handling */
220 /* If assembly is NULL, parse in the global context */
222 mono_config_parse_file_with_context (ParseState
*state
, const char *filename
)
224 GMarkupParseContext
*context
;
228 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_CONFIG
,
229 "Config attempting to parse: '%s'.", filename
);
234 if (!g_file_get_contents (filename
, &text
, &len
, NULL
))
236 context
= g_markup_parse_context_new (&mono_parser
, 0, state
, NULL
);
237 if (g_markup_parse_context_parse (context
, text
, len
, NULL
)) {
238 g_markup_parse_context_end_parse (context
, NULL
);
240 g_markup_parse_context_free (context
);
246 mono_config_parse_file (const char *filename
)
248 ParseState state
= {NULL
};
249 mono_config_parse_file_with_context (&state
, filename
);
253 * use the equivalent lookup code from the GAC when available.
254 * Depending on state, this should give something like:
255 * aname/version-pubtoken/
260 get_assembly_filename (MonoImage
*image
, int state
)
264 return g_strdup (mono_image_get_name (image
));
271 mono_config_for_assembly (MonoImage
*assembly
)
273 ParseState state
= {NULL
};
275 char *aname
, *cfg
, *cfg_name
;
278 state
.assembly
= assembly
;
279 cfg_name
= g_strdup_printf ("%s.config", mono_image_get_filename (assembly
));
280 mono_config_parse_file_with_context (&state
, cfg_name
);
283 cfg_name
= g_strdup_printf ("%s.config", mono_image_get_name (assembly
));
285 home
= g_get_home_dir ();
287 for (i
= 0; (aname
= get_assembly_filename (assembly
, i
)) != NULL
; ++i
) {
288 cfg
= g_build_filename (mono_get_config_dir (), "mono", "assemblies", aname
, cfg_name
, NULL
);
289 got_it
+= mono_config_parse_file_with_context (&state
, cfg
);
292 #ifndef PLATFORM_WIN32
293 cfg
= g_build_filename (home
, ".mono", "assemblies", aname
, cfg_name
, NULL
);
294 got_it
+= mono_config_parse_file_with_context (&state
, cfg
);
305 * Pass a NULL filename to parse the default config files
306 * (or the file in the MONO_CONFIG env var).
309 mono_config_parse (const char *filename
) {
315 mono_config_parse_file (filename
);
319 home
= g_getenv ("MONO_CONFIG");
321 mono_config_parse_file (home
);
325 mono_cfg
= g_build_filename (mono_get_config_dir (), "mono", "config", NULL
);
326 mono_config_parse_file (mono_cfg
);
329 #ifndef PLATFORM_WIN32
330 home
= g_get_home_dir ();
331 user_cfg
= g_strconcat (home
, G_DIR_SEPARATOR_S
, ".mono/config", NULL
);
332 mono_config_parse_file (user_cfg
);
337 static const char *mono_cfg_dir
= NULL
;
340 mono_install_get_config_dir (void)
342 #ifdef PLATFORM_WIN32
346 mono_cfg_dir
= g_getenv ("MONO_CFG_DIR");
349 #ifndef PLATFORM_WIN32
350 mono_cfg_dir
= MONO_CFG_DIR
;
352 prefix
= g_path_get_dirname (mono_assembly_getrootdir ());
353 mono_cfg_dir
= g_build_filename (prefix
, "etc", NULL
);
360 mono_get_config_dir (void)
363 mono_install_get_config_dir ();