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