2012-05-21 04:12:37 -07: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/. */
|
|
|
|
|
2007-12-31 07:15:43 -08: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
|
|
|
|
|
|
|
|
#include "nsUTF8Utils.h"
|
2010-11-15 07:19:57 -08:00
|
|
|
|
|
|
|
#ifndef XRE_DONT_PROTECT_DLL_LOAD
|
2010-11-04 11:45:51 -07:00
|
|
|
#include "nsSetDllDirectory.h"
|
2010-11-15 07:19:57 -08:00
|
|
|
#endif
|
2007-12-31 07:15:43 -08:00
|
|
|
|
2008-02-10 00:46:13 -08: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 07:15:43 -08:00
|
|
|
#define main NS_internal_main
|
|
|
|
|
2012-09-30 09:45:05 -07:00
|
|
|
#ifndef XRE_WANT_ENVIRON
|
2007-12-31 07:15:43 -08:00
|
|
|
int main(int argc, char **argv);
|
2012-09-30 09:45:05 -07:00
|
|
|
#else
|
|
|
|
int main(int argc, char **argv, char **envp);
|
|
|
|
#endif
|
2007-12-31 07:15:43 -08:00
|
|
|
|
|
|
|
static char*
|
2013-12-03 07:07:22 -08:00
|
|
|
AllocConvertUTF16toUTF8(char16ptr_t arg)
|
2007-12-31 07:15:43 -08:00
|
|
|
{
|
|
|
|
// be generous... UTF16 units can expand up to 3 UTF8 units
|
|
|
|
int len = wcslen(arg);
|
|
|
|
char *s = new char[len * 3 + 1];
|
|
|
|
if (!s)
|
2013-10-10 13:36:42 -07:00
|
|
|
return nullptr;
|
2007-12-31 07:15:43 -08:00
|
|
|
|
|
|
|
ConvertUTF16toUTF8 convert(s);
|
2008-01-03 16:07:06 -08:00
|
|
|
convert.write(arg, len);
|
2008-01-07 08:38:12 -08:00
|
|
|
convert.write_terminator();
|
2007-12-31 07:15:43 -08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
FreeAllocStrings(int argc, char **argv)
|
|
|
|
{
|
|
|
|
while (argc) {
|
|
|
|
--argc;
|
|
|
|
delete [] argv[argc];
|
|
|
|
}
|
|
|
|
|
|
|
|
delete [] argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
int wmain(int argc, WCHAR **argv)
|
|
|
|
{
|
2010-11-04 11:45:51 -07:00
|
|
|
#ifndef XRE_DONT_PROTECT_DLL_LOAD
|
2011-05-21 23:22:27 -07:00
|
|
|
mozilla::SanitizeEnvironmentVariables();
|
2012-02-23 06:53:55 -08:00
|
|
|
SetDllDirectoryW(L"");
|
2010-11-04 11:45:51 -07:00
|
|
|
#endif
|
2010-09-28 17:33:43 -07:00
|
|
|
|
2007-12-31 07:15:43 -08: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-10 13:36:42 -07:00
|
|
|
argvConverted[argc] = nullptr;
|
2008-02-10 00:47:09 -08:00
|
|
|
|
2008-02-06 13:53:43 -08:00
|
|
|
// need to save argvConverted copy for later deletion.
|
|
|
|
char **deleteUs = new char*[argc+1];
|
|
|
|
if (!deleteUs) {
|
|
|
|
FreeAllocStrings(argc, argvConverted);
|
|
|
|
return 127;
|
|
|
|
}
|
2009-04-13 08:18:43 -07:00
|
|
|
for (int i = 0; i < argc; i++)
|
2008-02-06 13:53:43 -08:00
|
|
|
deleteUs[i] = argvConverted[i];
|
2012-09-30 09:45:05 -07:00
|
|
|
#ifndef XRE_WANT_ENVIRON
|
2007-12-31 07:15:43 -08:00
|
|
|
int result = main(argc, argvConverted);
|
2012-09-30 09:45:05 -07:00
|
|
|
#else
|
|
|
|
// Force creation of the multibyte _environ variable.
|
|
|
|
getenv("PATH");
|
|
|
|
int result = main(argc, argvConverted, _environ);
|
|
|
|
#endif
|
2008-02-10 00:47:09 -08:00
|
|
|
|
2008-02-06 13:53:43 -08:00
|
|
|
delete[] argvConverted;
|
|
|
|
FreeAllocStrings(argc, deleteUs);
|
2008-02-10 00:47:09 -08:00
|
|
|
|
2007-12-31 07:15:43 -08:00
|
|
|
return result;
|
|
|
|
}
|