2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2018-04-17 22:48:21 +03:00
|
|
|
#ifndef nsWindowsWMain_cpp
|
|
|
|
#define nsWindowsWMain_cpp
|
|
|
|
|
2007-12-31 18:15:43 +03:00
|
|
|
// This file is a .cpp file meant to be included in nsBrowserApp.cpp and other
|
|
|
|
// similar bootstrap code. It converts wide-character windows wmain into UTF-8
|
|
|
|
// narrow-character strings.
|
|
|
|
|
|
|
|
#ifndef XP_WIN
|
|
|
|
# error This file only makes sense on Windows.
|
|
|
|
#endif
|
|
|
|
|
2017-12-22 18:53:12 +03:00
|
|
|
#include "mozilla/Char16.h"
|
2007-12-31 18:15:43 +03:00
|
|
|
#include "nsUTF8Utils.h"
|
2018-02-03 09:53:34 +03:00
|
|
|
|
2018-02-13 11:52:44 +03:00
|
|
|
#include <windows.h>
|
2010-11-15 18:19:57 +03:00
|
|
|
|
2008-02-10 11:46:13 +03:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
|
|
|
|
/* MingW currently does not implement a wide version of the
|
|
|
|
startup routines. Workaround is to implement something like
|
|
|
|
it ourselves. See bug 411826 */
|
|
|
|
|
|
|
|
# include <shellapi.h>
|
|
|
|
|
|
|
|
int wmain(int argc, WCHAR** argv);
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
LPWSTR commandLine = GetCommandLineW();
|
|
|
|
int argcw = 0;
|
|
|
|
LPWSTR* argvw = CommandLineToArgvW(commandLine, &argcw);
|
|
|
|
if (!argvw) return 127;
|
|
|
|
|
|
|
|
int result = wmain(argcw, argvw);
|
|
|
|
LocalFree(argvw);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif /* __MINGW32__ */
|
|
|
|
|
2007-12-31 18:15:43 +03:00
|
|
|
#define main NS_internal_main
|
|
|
|
|
2012-09-30 20:45:05 +04:00
|
|
|
#ifndef XRE_WANT_ENVIRON
|
2007-12-31 18:15:43 +03:00
|
|
|
int main(int argc, char** argv);
|
2012-09-30 20:45:05 +04:00
|
|
|
#else
|
|
|
|
int main(int argc, char** argv, char** envp);
|
|
|
|
#endif
|
2007-12-31 18:15:43 +03:00
|
|
|
|
2018-02-13 11:52:44 +03:00
|
|
|
static void SanitizeEnvironmentVariables() {
|
|
|
|
DWORD bufferSize = GetEnvironmentVariableW(L"PATH", nullptr, 0);
|
|
|
|
if (bufferSize) {
|
|
|
|
wchar_t* originalPath = new wchar_t[bufferSize];
|
|
|
|
if (bufferSize - 1 ==
|
|
|
|
GetEnvironmentVariableW(L"PATH", originalPath, bufferSize)) {
|
|
|
|
bufferSize = ExpandEnvironmentStringsW(originalPath, nullptr, 0);
|
|
|
|
if (bufferSize) {
|
|
|
|
wchar_t* newPath = new wchar_t[bufferSize];
|
|
|
|
if (ExpandEnvironmentStringsW(originalPath, newPath, bufferSize)) {
|
|
|
|
SetEnvironmentVariableW(L"PATH", newPath);
|
|
|
|
}
|
|
|
|
delete[] newPath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete[] originalPath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-03 19:07:22 +04:00
|
|
|
static char* AllocConvertUTF16toUTF8(char16ptr_t arg) {
|
2007-12-31 18:15:43 +03:00
|
|
|
// be generous... UTF16 units can expand up to 3 UTF8 units
|
2018-07-06 10:44:43 +03:00
|
|
|
size_t len = wcslen(arg);
|
|
|
|
// ConvertUTF16toUTF8 requires +1. Let's do that here, too, lacking
|
|
|
|
// knowledge of Windows internals.
|
|
|
|
size_t dstLen = len * 3 + 1;
|
|
|
|
char* s = new char[dstLen + 1]; // Another +1 for zero terminator
|
2013-10-11 00:36:42 +04:00
|
|
|
if (!s) return nullptr;
|
2007-12-31 18:15:43 +03:00
|
|
|
|
2018-07-06 10:44:43 +03:00
|
|
|
int written =
|
|
|
|
::WideCharToMultiByte(CP_UTF8, 0, arg, len, s, dstLen, nullptr, nullptr);
|
|
|
|
s[written] = 0;
|
2007-12-31 18:15:43 +03:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void FreeAllocStrings(int argc, char** argv) {
|
|
|
|
while (argc) {
|
|
|
|
--argc;
|
|
|
|
delete[] argv[argc];
|
|
|
|
}
|
|
|
|
|
|
|
|
delete[] argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
int wmain(int argc, WCHAR** argv) {
|
2018-02-13 11:52:44 +03:00
|
|
|
SanitizeEnvironmentVariables();
|
2012-02-23 18:53:55 +04:00
|
|
|
SetDllDirectoryW(L"");
|
2010-09-29 04:33:43 +04:00
|
|
|
|
2018-04-17 22:48:21 +03:00
|
|
|
// Only run this code if LauncherProcessWin.h was included beforehand, thus
|
|
|
|
// signalling that the hosting process should support launcher mode.
|
|
|
|
#if defined(mozilla_LauncherProcessWin_h)
|
2019-02-26 00:14:07 +03:00
|
|
|
mozilla::Maybe<int> launcherResult =
|
|
|
|
mozilla::LauncherMain(argc, argv, sAppData);
|
|
|
|
if (launcherResult) {
|
|
|
|
return launcherResult.value();
|
2018-04-17 22:48:21 +03:00
|
|
|
}
|
|
|
|
#endif // defined(mozilla_LauncherProcessWin_h)
|
|
|
|
|
2007-12-31 18:15:43 +03:00
|
|
|
char** argvConverted = new char*[argc + 1];
|
|
|
|
if (!argvConverted) return 127;
|
|
|
|
|
|
|
|
for (int i = 0; i < argc; ++i) {
|
|
|
|
argvConverted[i] = AllocConvertUTF16toUTF8(argv[i]);
|
|
|
|
if (!argvConverted[i]) {
|
|
|
|
return 127;
|
|
|
|
}
|
|
|
|
}
|
2013-10-11 00:36:42 +04:00
|
|
|
argvConverted[argc] = nullptr;
|
2008-02-10 11:47:09 +03:00
|
|
|
|
2008-02-07 00:53:43 +03:00
|
|
|
// need to save argvConverted copy for later deletion.
|
|
|
|
char** deleteUs = new char*[argc + 1];
|
|
|
|
if (!deleteUs) {
|
|
|
|
FreeAllocStrings(argc, argvConverted);
|
|
|
|
return 127;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < argc; i++) deleteUs[i] = argvConverted[i];
|
2012-09-30 20:45:05 +04:00
|
|
|
#ifndef XRE_WANT_ENVIRON
|
2007-12-31 18:15:43 +03:00
|
|
|
int result = main(argc, argvConverted);
|
2012-09-30 20:45:05 +04:00
|
|
|
#else
|
|
|
|
// Force creation of the multibyte _environ variable.
|
|
|
|
getenv("PATH");
|
|
|
|
int result = main(argc, argvConverted, _environ);
|
|
|
|
#endif
|
2008-02-10 11:47:09 +03:00
|
|
|
|
2008-02-07 00:53:43 +03:00
|
|
|
delete[] argvConverted;
|
|
|
|
FreeAllocStrings(argc, deleteUs);
|
2008-02-10 11:47:09 +03:00
|
|
|
|
2007-12-31 18:15:43 +03:00
|
|
|
return result;
|
|
|
|
}
|
2018-04-17 22:48:21 +03:00
|
|
|
|
|
|
|
#endif // nsWindowsWMain_cpp
|