|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 // This file is a .cpp file meant to be included in nsBrowserApp.cpp and other |
|
6 // similar bootstrap code. It converts wide-character windows wmain into UTF-8 |
|
7 // narrow-character strings. |
|
8 |
|
9 #ifndef XP_WIN |
|
10 #error This file only makes sense on Windows. |
|
11 #endif |
|
12 |
|
13 #include "nsUTF8Utils.h" |
|
14 |
|
15 #ifndef XRE_DONT_PROTECT_DLL_LOAD |
|
16 #include "nsSetDllDirectory.h" |
|
17 #endif |
|
18 |
|
19 #ifdef __MINGW32__ |
|
20 |
|
21 /* MingW currently does not implement a wide version of the |
|
22 startup routines. Workaround is to implement something like |
|
23 it ourselves. See bug 411826 */ |
|
24 |
|
25 #include <shellapi.h> |
|
26 |
|
27 int wmain(int argc, WCHAR **argv); |
|
28 |
|
29 int main(int argc, char **argv) |
|
30 { |
|
31 LPWSTR commandLine = GetCommandLineW(); |
|
32 int argcw = 0; |
|
33 LPWSTR *argvw = CommandLineToArgvW(commandLine, &argcw); |
|
34 if (!argvw) |
|
35 return 127; |
|
36 |
|
37 int result = wmain(argcw, argvw); |
|
38 LocalFree(argvw); |
|
39 return result; |
|
40 } |
|
41 #endif /* __MINGW32__ */ |
|
42 |
|
43 #define main NS_internal_main |
|
44 |
|
45 #ifndef XRE_WANT_ENVIRON |
|
46 int main(int argc, char **argv); |
|
47 #else |
|
48 int main(int argc, char **argv, char **envp); |
|
49 #endif |
|
50 |
|
51 static char* |
|
52 AllocConvertUTF16toUTF8(char16ptr_t arg) |
|
53 { |
|
54 // be generous... UTF16 units can expand up to 3 UTF8 units |
|
55 int len = wcslen(arg); |
|
56 char *s = new char[len * 3 + 1]; |
|
57 if (!s) |
|
58 return nullptr; |
|
59 |
|
60 ConvertUTF16toUTF8 convert(s); |
|
61 convert.write(arg, len); |
|
62 convert.write_terminator(); |
|
63 return s; |
|
64 } |
|
65 |
|
66 static void |
|
67 FreeAllocStrings(int argc, char **argv) |
|
68 { |
|
69 while (argc) { |
|
70 --argc; |
|
71 delete [] argv[argc]; |
|
72 } |
|
73 |
|
74 delete [] argv; |
|
75 } |
|
76 |
|
77 int wmain(int argc, WCHAR **argv) |
|
78 { |
|
79 #ifndef XRE_DONT_PROTECT_DLL_LOAD |
|
80 mozilla::SanitizeEnvironmentVariables(); |
|
81 SetDllDirectoryW(L""); |
|
82 #endif |
|
83 |
|
84 char **argvConverted = new char*[argc + 1]; |
|
85 if (!argvConverted) |
|
86 return 127; |
|
87 |
|
88 for (int i = 0; i < argc; ++i) { |
|
89 argvConverted[i] = AllocConvertUTF16toUTF8(argv[i]); |
|
90 if (!argvConverted[i]) { |
|
91 return 127; |
|
92 } |
|
93 } |
|
94 argvConverted[argc] = nullptr; |
|
95 |
|
96 // need to save argvConverted copy for later deletion. |
|
97 char **deleteUs = new char*[argc+1]; |
|
98 if (!deleteUs) { |
|
99 FreeAllocStrings(argc, argvConverted); |
|
100 return 127; |
|
101 } |
|
102 for (int i = 0; i < argc; i++) |
|
103 deleteUs[i] = argvConverted[i]; |
|
104 #ifndef XRE_WANT_ENVIRON |
|
105 int result = main(argc, argvConverted); |
|
106 #else |
|
107 // Force creation of the multibyte _environ variable. |
|
108 getenv("PATH"); |
|
109 int result = main(argc, argvConverted, _environ); |
|
110 #endif |
|
111 |
|
112 delete[] argvConverted; |
|
113 FreeAllocStrings(argc, deleteUs); |
|
114 |
|
115 return result; |
|
116 } |