From 599a35c60cdbff60be1effff6dfcc3860dc6ce62 Mon Sep 17 00:00:00 2001 From: Lucas Meijer Date: Wed, 9 Mar 2011 16:33:17 +0100 Subject: [PATCH] split out mono_set_commandline_arguments(), so embedders can control what System.Environment.CommandLine returns. --- mono/metadata/object.c | 79 ++++++++++++++++++++++++++---------------- mono/metadata/object.h | 3 ++ 2 files changed, 52 insertions(+), 30 deletions(-) diff --git a/mono/metadata/object.c b/mono/metadata/object.c index 40999a618ba..b732305350f 100644 --- a/mono/metadata/object.c +++ b/mono/metadata/object.c @@ -3454,41 +3454,29 @@ free_main_args (void) } /** - * mono_runtime_run_main: - * @method: the method to start the application with (usually Main) + * mono_set_commandline_arguments: * @argc: number of arguments from the command line * @argv: array of strings from the command line - * @exc: excetption results + * @basedir: optional base path of assembly with entrypoint * - * Execute a standard Main() method (argc/argv contains the - * executable name). This method also sets the command line argument value - * needed by System.Environment. + * This method sets the command line argument value needed by System.Environment. * - * */ -int -mono_runtime_run_main (MonoMethod *method, int argc, char* argv[], - MonoObject **exc) + +void +mono_set_commandline_arguments(int argc, char* argv[], const char* basedir) { int i; - MonoArray *args = NULL; - MonoDomain *domain = mono_domain_get (); gchar *utf8_fullpath; - MonoMethodSignature *sig; - - g_assert (method != NULL); - mono_thread_set_main (mono_thread_current ()); - + g_assert (main_args == NULL); //this function should only be called once. main_args = g_new0 (char*, argc); num_main_args = argc; - - if (!g_path_is_absolute (argv [0])) { + + if (!g_path_is_absolute (argv [0]) && basedir != NULL) { gchar *basename = g_path_get_basename (argv [0]); - gchar *fullpath = g_build_filename (method->klass->image->assembly->basedir, - basename, - NULL); - + gchar *fullpath = g_build_filename (basedir, basename, NULL); + utf8_fullpath = mono_utf8_from_external (fullpath); if(utf8_fullpath == NULL) { /* Printing the arg text will cause glib to @@ -3500,7 +3488,7 @@ mono_runtime_run_main (MonoMethod *method, int argc, char* argv[], g_print ("Please add the correct encoding to MONO_EXTERNAL_ENCODINGS and try again.\n"); exit (-1); } - + g_free (fullpath); g_free (basename); } else { @@ -3511,12 +3499,12 @@ mono_runtime_run_main (MonoMethod *method, int argc, char* argv[], exit (-1); } } - + main_args [0] = utf8_fullpath; - + for (i = 1; i < argc; ++i) { gchar *utf8_arg; - + utf8_arg=mono_utf8_from_external (argv[i]); if(utf8_arg==NULL) { /* Ditto the comment about Invalid UTF-8 here */ @@ -3524,11 +3512,37 @@ mono_runtime_run_main (MonoMethod *method, int argc, char* argv[], g_print ("Please add the correct encoding to MONO_EXTERNAL_ENCODINGS and try again.\n"); exit (-1); } - + main_args [i] = utf8_arg; } - argc--; - argv++; +} + +/** + * mono_runtime_run_main: + * @method: the method to start the application with (usually Main) + * @argc: number of arguments from the command line + * @argv: array of strings from the command line + * @exc: excetption results + * + * Execute a standard Main() method (argc/argv contains the + * executable name). This method also sets the command line argument value + * needed by System.Environment. + * + * + */ +int +mono_runtime_run_main (MonoMethod *method, int argc, char* argv[], + MonoObject **exc) +{ + int i; + MonoArray *args = NULL; + MonoDomain *domain = mono_domain_get (); + + MonoMethodSignature *sig; + + g_assert (method != NULL); + + mono_thread_set_main (mono_thread_current ()); sig = mono_method_signature (method); if (!sig) { @@ -3536,6 +3550,11 @@ mono_runtime_run_main (MonoMethod *method, int argc, char* argv[], exit (-1); } + mono_set_commandline_arguments(argc, argv, method->klass->image->assembly->basedir); + + argc--; + argv++; + if (sig->param_count) { args = (MonoArray*)mono_array_new (domain, mono_defaults.string_class, argc); for (i = 0; i < argc; ++i) { diff --git a/mono/metadata/object.h b/mono/metadata/object.h index ee367d0ad74..ed113ebfc06 100644 --- a/mono/metadata/object.h +++ b/mono/metadata/object.h @@ -223,6 +223,9 @@ mono_runtime_invoke_array (MonoMethod *method, void *obj, MonoArray *params, void* mono_method_get_unmanaged_thunk (MonoMethod *method); +void +mono_set_commandline_arguments (int argc, char* argv[], const char* basedir); + MonoArray* mono_runtime_get_main_args (void); -- GitLab