Bug 15574. XML deserialization recursion: add array type to known_types?
[mono.git] / mono / mini / main.c
1 #include <config.h>
2 #include "mini.h"
3 #ifndef HOST_WIN32
4 #ifndef BUILDVER_INCLUDED
5 #include "buildver-boehm.h"
6 #endif
7 #endif
8
9 /*
10  * If the MONO_ENV_OPTIONS environment variable is set, it uses this as a
11  * source of command line arguments that are passed to Mono before the
12  * command line arguments specified in the command line.
13  */
14 static int
15 mono_main_with_options (int argc, char *argv [])
16 {
17         const char *env_options = g_getenv ("MONO_ENV_OPTIONS");
18         if (env_options != NULL){
19                 GPtrArray *array = g_ptr_array_new ();
20                 GString *buffer = g_string_new ("");
21                 const char *p;
22                 int i;
23                 gboolean in_quotes = FALSE;
24                 char quote_char = '\0';
25
26                 for (p = env_options; *p; p++){
27                         switch (*p){
28                         case ' ': case '\t':
29                                 if (!in_quotes) {
30                                         if (buffer->len != 0){
31                                                 g_ptr_array_add (array, g_strdup (buffer->str));
32                                                 g_string_truncate (buffer, 0);
33                                         }
34                                 } else {
35                                         g_string_append_c (buffer, *p);
36                                 }
37                                 break;
38                         case '\\':
39                                 if (p [1]){
40                                         g_string_append_c (buffer, p [1]);
41                                         p++;
42                                 }
43                                 break;
44                         case '\'':
45                         case '"':
46                                 if (in_quotes) {
47                                         if (quote_char == *p)
48                                                 in_quotes = FALSE;
49                                         else
50                                                 g_string_append_c (buffer, *p);
51                                 } else {
52                                         in_quotes = TRUE;
53                                         quote_char = *p;
54                                 }
55                                 break;
56                         default:
57                                 g_string_append_c (buffer, *p);
58                                 break;
59                         }
60                 }
61                 if (in_quotes) {
62                         fprintf (stderr, "Unmatched quotes in value of MONO_ENV_OPTIONS: [%s]\n", env_options);
63                         exit (1);
64                 }
65                         
66                 if (buffer->len != 0)
67                         g_ptr_array_add (array, g_strdup (buffer->str));
68                 g_string_free (buffer, TRUE);
69
70                 if (array->len > 0){
71                         int new_argc = array->len + argc;
72                         char **new_argv = g_new (char *, new_argc + 1);
73                         int j;
74
75                         new_argv [0] = argv [0];
76                         
77                         /* First the environment variable settings, to allow the command line options to override */
78                         for (i = 0; i < array->len; i++)
79                                 new_argv [i+1] = g_ptr_array_index (array, i);
80                         i++;
81                         for (j = 1; j < argc; j++)
82                                 new_argv [i++] = argv [j];
83                         new_argv [i] = NULL;
84
85                         argc = new_argc;
86                         argv = new_argv;
87                 }
88                 g_ptr_array_free (array, TRUE);
89         }
90
91         return mono_main (argc, argv);
92 }
93
94 #ifdef HOST_WIN32
95
96 int
97 main (void)
98 {
99         int argc;
100         gunichar2** argvw;
101         gchar** argv;
102         int i;
103
104         argvw = CommandLineToArgvW (GetCommandLine (), &argc);
105         argv = g_new0 (gchar*, argc + 1);
106         for (i = 0; i < argc; i++)
107                 argv [i] = g_utf16_to_utf8 (argvw [i], -1, NULL, NULL, NULL);
108         argv [argc] = NULL;
109
110         LocalFree (argvw);
111
112         return mono_main_with_options  (argc, argv);
113 }
114
115 #else
116
117 int
118 main (int argc, char* argv[])
119 {
120         mono_build_date = build_date;
121         
122         return mono_main_with_options (argc, argv);
123 }
124
125 #endif