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