[m-rev.] for review: unicode command line arguments on windows

Peter Wang novalazy at gmail.com
Wed Dec 14 12:25:45 AEDT 2011


This can be applied to a later 11.07 release if you like.
--

Branches: main, 11.07

Support Unicode characters in command line arguments on Windows.

util/mkinit.c:
	Retrieve the array of wide-character command-line argument
	strings with GetCommandLineW and CommandLineToArgvW.

	Convert the strings to UTF-8.

	Pass the array of UTF-8 argument strings to mercury_main as usual.

diff --git a/util/mkinit.c b/util/mkinit.c
index 8df56b8..73d825e 100644
--- a/util/mkinit.c
+++ b/util/mkinit.c
@@ -327,6 +327,10 @@ static const char header2[] =
     "#endif\n"
     "#include \"mercury_init.h\"\n"
     "#include \"mercury_grade.h\"\n"
+    "#ifdef MR_WIN32\n"
+    "  #include \"mercury_windows.h\"\n"
+    "  #include <shellapi.h>\n"
+    "#endif\n"
     "\n"
     "#define MR_TRACE_ENABLED %d\n"
     "#if MR_TRACE_ENABLED\n"
@@ -531,6 +535,32 @@ static const char mercury_main_func[] =
     "   return mercury_terminate();\n"
     "}\n"
     "\n"
+    /*
+    ** Convert wide-character representation of the command line
+    ** arguments to UTF-8.
+    */
+    "#ifdef MR_WIN32\n"
+    "static char **\n"
+    "mercury_win32_args(int *argc_ptr)\n"
+    "{\n"
+    "   LPWSTR  *wargv;\n"
+    "   char    **argv;\n"
+    "   int     i;\n"
+    "\n"
+    "   wargv = CommandLineToArgvW(GetCommandLineW(), argc_ptr);\n"
+    "   argv = MR_GC_NEW_ARRAY(char *, *argc_ptr);\n"
+    "   for (i = 0; i < *argc_ptr; i++) {\n"
+    "       int bytes = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1,\n"
+    "           NULL, 0, NULL, NULL);\n"
+    "       argv[i] = MR_GC_malloc(bytes);\n"
+    "       WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1,\n"
+    "           argv[i], bytes, NULL, NULL);\n"
+    "   }\n"
+    "   LocalFree(wargv);\n"
+    "   return argv;\n"
+    "}\n"
+    "#endif\n"
+    "\n"
     ;
 
 static const char mercury_grade_var[] =
@@ -552,6 +582,9 @@ static const char main_func[] =
     "#ifdef MR_PROFILE_SBRK\n"
     "   old_break = sbrk(0);\n"
     "#endif\n"
+    "#ifdef MR_WIN32\n"
+    "   argv = mercury_win32_args(&argc);\n"
+    "#endif\n"
     "   result = mercury_main(argc, argv);\n"
     "#ifdef MR_PROFILE_SBRK\n"
     "   new_break = sbrk(0);\n"

--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list