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/. */
5 #include "nsXULAppAPI.h"
6 #include "nsXPCOMGlue.h"
11 #define snprintf _snprintf
12 #define strcasecmp _stricmp
15 #include "nsAppRunner.h"
19 #include "nsCRTGlue.h"
20 #include "nsStringAPI.h"
21 #include "nsServiceManagerUtils.h"
25 #include "nsINIParser.h"
28 #define XRE_DONT_SUPPORT_XPSP2 // See https://bugzil.la/1023941#c32
29 #include "nsWindowsWMain.cpp"
32 #include "BinaryPath.h"
34 #include "nsXPCOMPrivate.h" // for MAXPATHLEN and XPCOM_DLL
36 using namespace mozilla
;
39 * Output a string to the user. This method is really only meant to be used to
40 * output last-ditch error messages designed for developers NOT END USERS.
43 * Pass true to indicate severe errors.
45 * printf-style format string followed by arguments.
47 static void Output(bool isError
, const char *fmt
, ... )
52 #if (defined(XP_WIN) && !MOZ_WINCONSOLE)
54 _vsnwprintf(msg
, sizeof(msg
)/sizeof(msg
[0]), NS_ConvertUTF8toUTF16(fmt
).get(), ap
);
58 flags
|= MB_ICONERROR
;
60 flags
|= MB_ICONINFORMATION
;
62 MessageBoxW(nullptr, msg
, L
"XULRunner", flags
);
64 vfprintf(stderr
, fmt
, ap
);
71 * Return true if |arg| matches the given argument name.
73 static bool IsArg(const char* arg
, const char* s
)
79 return !strcasecmp(arg
, s
);
84 return !strcasecmp(++arg
, s
);
91 GetGREVersion(const char *argv0
,
92 nsACString
*aMilestone
,
96 aMilestone
->AssignLiteral("<Error>");
98 aVersion
->AssignLiteral("<Error>");
100 nsCOMPtr
<nsIFile
> iniFile
;
101 nsresult rv
= BinaryPath::GetFile(argv0
, getter_AddRefs(iniFile
));
105 iniFile
->SetNativeLeafName(NS_LITERAL_CSTRING("platform.ini"));
108 rv
= parser
.Init(iniFile
);
113 rv
= parser
.GetString("Build", "Milestone", *aMilestone
);
118 rv
= parser
.GetString("Build", "BuildID", *aVersion
);
126 * A helper class which calls NS_LogInit/NS_LogTerm in its scope.
131 ScopedLogging() { NS_LogInit(); }
132 ~ScopedLogging() { NS_LogTerm(); }
135 static void Usage(const char *argv0
)
137 nsAutoCString milestone
;
138 GetGREVersion(argv0
, &milestone
, nullptr);
140 // display additional information (XXX make localizable?)
142 "Mozilla XULRunner %s\n\n"
143 "Usage: " XULRUNNER_PROGNAME
" [OPTIONS]\n"
144 " " XULRUNNER_PROGNAME
" APP-FILE [APP-OPTIONS...]\n"
147 " --app specify APP-FILE (optional)\n"
148 " -h, --help show this message\n"
149 " -v, --version show version\n"
150 " --gre-version print the GRE version string on stdout\n"
153 " Application initialization file.\n"
156 " Application specific options.\n",
160 XRE_GetFileFromPathType XRE_GetFileFromPath
;
161 XRE_CreateAppDataType XRE_CreateAppData
;
162 XRE_FreeAppDataType XRE_FreeAppData
;
163 XRE_mainType XRE_main
;
165 static const nsDynamicFunctionLoad kXULFuncs
[] = {
166 { "XRE_GetFileFromPath", (NSFuncPtr
*) &XRE_GetFileFromPath
},
167 { "XRE_CreateAppData", (NSFuncPtr
*) &XRE_CreateAppData
},
168 { "XRE_FreeAppData", (NSFuncPtr
*) &XRE_FreeAppData
},
169 { "XRE_main", (NSFuncPtr
*) &XRE_main
},
176 AutoAppData(nsIFile
* aINIFile
) : mAppData(nullptr) {
177 nsresult rv
= XRE_CreateAppData(aINIFile
, &mAppData
);
183 XRE_FreeAppData(mAppData
);
186 operator nsXREAppData
*() const { return mAppData
; }
187 nsXREAppData
* operator -> () const { return mAppData
; }
190 nsXREAppData
* mAppData
;
193 int main(int argc
, char* argv
[])
195 char exePath
[MAXPATHLEN
];
196 nsresult rv
= mozilla::BinaryPath::Get(argv
[0], exePath
);
198 Output(true, "Couldn't calculate the application directory.\n");
202 char *lastSlash
= strrchr(exePath
, XPCOM_FILE_PATH_SEPARATOR
[0]);
203 if (!lastSlash
|| (size_t(lastSlash
- exePath
) > MAXPATHLEN
- sizeof(XPCOM_DLL
) - 1))
206 strcpy(++lastSlash
, XPCOM_DLL
);
208 rv
= XPCOMGlueStartup(exePath
);
210 Output(true, "Couldn't load XPCOM.\n");
216 if (argc
> 1 && (IsArg(argv
[1], "h") ||
217 IsArg(argv
[1], "help") ||
218 IsArg(argv
[1], "?")))
224 if (argc
== 2 && (IsArg(argv
[1], "v") || IsArg(argv
[1], "version")))
226 nsAutoCString milestone
;
227 nsAutoCString version
;
228 GetGREVersion(argv
[0], &milestone
, &version
);
229 Output(false, "Mozilla XULRunner %s - %s\n",
230 milestone
.get(), version
.get());
234 rv
= XPCOMGlueLoadXULFunctions(kXULFuncs
);
236 Output(true, "Couldn't load XRE functions.\n");
241 nsAutoCString milestone
;
242 rv
= GetGREVersion(argv
[0], &milestone
, nullptr);
246 if (IsArg(argv
[1], "gre-version")) {
252 printf("%s\n", milestone
.get());
256 if (IsArg(argv
[1], "install-app")) {
257 Output(true, "--install-app support has been removed. Use 'python install-app.py' instead.\n");
262 const char *appDataFile
= getenv("XUL_APP_FILE");
264 if (!(appDataFile
&& *appDataFile
)) {
270 if (IsArg(argv
[1], "app")) {
280 appDataFile
= argv
[1];
285 static char kAppEnv
[MAXPATHLEN
];
286 snprintf(kAppEnv
, MAXPATHLEN
, "XUL_APP_FILE=%s", appDataFile
);
290 nsCOMPtr
<nsIFile
> appDataLF
;
291 rv
= XRE_GetFileFromPath(appDataFile
, getter_AddRefs(appDataLF
));
293 Output(true, "Error: unrecognized application.ini path.\n");
297 AutoAppData
appData(appDataLF
);
299 Output(true, "Error: couldn't parse application.ini.\n");
303 return XRE_main(argc
, argv
, appData
, 0);