nsWindowsWMain.cpp 2.6 KB

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