retag
[mcs.git] / tools / mkbundle / template_main.c
blob9a7e241d9c919413132f68f35b250d64aa0da98b
1 int mono_main (int argc, char* argv[]);
3 #include <stdlib.h>
4 #include <string.h>
5 #ifdef _WIN32
6 #include <windows.h>
7 #endif
9 static char **mono_options = NULL;
11 static int count_mono_options_args (void)
13 const char *e = getenv ("MONO_BUNDLED_OPTIONS");
14 const char *p, *q;
15 int i, n;
17 if (e == NULL)
18 return 0;
20 /* Don't bother with any quoting here. It is unlikely one would
21 * want to pass options containing spaces anyway.
24 p = e;
25 n = 1;
26 while ((q = strchr (p, ' ')) != NULL) {
27 n++;
28 p = q + 1;
31 mono_options = malloc (sizeof (char *) * (n + 1));
33 p = e;
34 i = 0;
35 while ((q = strchr (p, ' ')) != NULL) {
36 mono_options[i] = malloc ((q - p) + 1);
37 memcpy (mono_options[i], p, q - p);
38 mono_options[i][q - p] = '\0';
39 i++;
40 p = q + 1;
42 mono_options[i++] = strdup (p);
43 mono_options[i] = NULL;
45 return n;
49 int main (int argc, char* argv[])
51 char **newargs;
52 int i, k = 0;
54 #ifdef _WIN32
55 /* CommandLineToArgvW() might return a different argc than the
56 * one passed to main(), so let it overwrite that, as we won't
57 * use argv[] on Windows anyway.
59 wchar_t **wargv = CommandLineToArgvW (GetCommandLineW (), &argc);
60 #endif
62 newargs = (char **) malloc (sizeof (char *) * (argc + 2) + count_mono_options_args ());
64 #ifdef _WIN32
65 newargs [k++] = g_utf16_to_utf8 (wargv [0], -1, NULL, NULL, NULL);
66 #else
67 newargs [k++] = argv [0];
68 #endif
70 if (mono_options != NULL) {
71 i = 0;
72 while (mono_options[i] != NULL)
73 newargs[k++] = mono_options[i++];
76 newargs [k++] = image_name;
78 for (i = 1; i < argc; i++) {
79 #ifdef _WIN32
80 newargs [k++] = g_utf16_to_utf8 (wargv [i], -1, NULL, NULL, NULL);
81 #else
82 newargs [k++] = argv [i];
83 #endif
85 #ifdef _WIN32
86 LocalFree (wargv);
87 #endif
88 newargs [k] = NULL;
90 if (config_dir != NULL && getenv ("MONO_CFG_DIR") == NULL)
91 mono_set_dirs (getenv ("MONO_PATH"), config_dir);
93 mono_mkbundle_init();
95 return mono_main (k, newargs);