Bumping manifests a=b2g-bump
[gecko.git] / xulrunner / app / nsXULRunnerApp.cpp
bloba539ede77f9b4fa22d39daa28b60054f610020ff
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"
7 #include <stdio.h>
8 #include <stdlib.h>
9 #ifdef XP_WIN
10 #include <windows.h>
11 #define snprintf _snprintf
12 #define strcasecmp _stricmp
13 #endif
15 #include "nsAppRunner.h"
16 #include "nsIFile.h"
17 #include "nsCOMPtr.h"
18 #include "nsMemory.h"
19 #include "nsCRTGlue.h"
20 #include "nsStringAPI.h"
21 #include "nsServiceManagerUtils.h"
22 #include "plstr.h"
23 #include "prprf.h"
24 #include "prenv.h"
25 #include "nsINIParser.h"
27 #ifdef XP_WIN
28 #define XRE_DONT_SUPPORT_XPSP2 // See https://bugzil.la/1023941#c32
29 #include "nsWindowsWMain.cpp"
30 #endif
32 #include "BinaryPath.h"
34 #include "nsXPCOMPrivate.h" // for MAXPATHLEN and XPCOM_DLL
36 using namespace mozilla;
38 /**
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.
42 * @param isError
43 * Pass true to indicate severe errors.
44 * @param fmt
45 * printf-style format string followed by arguments.
47 static void Output(bool isError, const char *fmt, ... )
49 va_list ap;
50 va_start(ap, fmt);
52 #if (defined(XP_WIN) && !MOZ_WINCONSOLE)
53 wchar_t msg[2048];
54 _vsnwprintf(msg, sizeof(msg)/sizeof(msg[0]), NS_ConvertUTF8toUTF16(fmt).get(), ap);
56 UINT flags = MB_OK;
57 if (isError)
58 flags |= MB_ICONERROR;
59 else
60 flags |= MB_ICONINFORMATION;
62 MessageBoxW(nullptr, msg, L"XULRunner", flags);
63 #else
64 vfprintf(stderr, fmt, ap);
65 #endif
67 va_end(ap);
70 /**
71 * Return true if |arg| matches the given argument name.
73 static bool IsArg(const char* arg, const char* s)
75 if (*arg == '-')
77 if (*++arg == '-')
78 ++arg;
79 return !strcasecmp(arg, s);
82 #if defined(XP_WIN)
83 if (*arg == '/')
84 return !strcasecmp(++arg, s);
85 #endif
87 return false;
90 static nsresult
91 GetGREVersion(const char *argv0,
92 nsACString *aMilestone,
93 nsACString *aVersion)
95 if (aMilestone)
96 aMilestone->AssignLiteral("<Error>");
97 if (aVersion)
98 aVersion->AssignLiteral("<Error>");
100 nsCOMPtr<nsIFile> iniFile;
101 nsresult rv = BinaryPath::GetFile(argv0, getter_AddRefs(iniFile));
102 if (NS_FAILED(rv))
103 return rv;
105 iniFile->SetNativeLeafName(NS_LITERAL_CSTRING("platform.ini"));
107 nsINIParser parser;
108 rv = parser.Init(iniFile);
109 if (NS_FAILED(rv))
110 return rv;
112 if (aMilestone) {
113 rv = parser.GetString("Build", "Milestone", *aMilestone);
114 if (NS_FAILED(rv))
115 return rv;
117 if (aVersion) {
118 rv = parser.GetString("Build", "BuildID", *aVersion);
119 if (NS_FAILED(rv))
120 return rv;
122 return NS_OK;
126 * A helper class which calls NS_LogInit/NS_LogTerm in its scope.
128 class ScopedLogging
130 public:
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?)
141 Output(false,
142 "Mozilla XULRunner %s\n\n"
143 "Usage: " XULRUNNER_PROGNAME " [OPTIONS]\n"
144 " " XULRUNNER_PROGNAME " APP-FILE [APP-OPTIONS...]\n"
145 "\n"
146 "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"
151 "\n"
152 "APP-FILE\n"
153 " Application initialization file.\n"
154 "\n"
155 "APP-OPTIONS\n"
156 " Application specific options.\n",
157 milestone.get());
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 },
170 { nullptr, nullptr }
173 class AutoAppData
175 public:
176 AutoAppData(nsIFile* aINIFile) : mAppData(nullptr) {
177 nsresult rv = XRE_CreateAppData(aINIFile, &mAppData);
178 if (NS_FAILED(rv))
179 mAppData = nullptr;
181 ~AutoAppData() {
182 if (mAppData)
183 XRE_FreeAppData(mAppData);
186 operator nsXREAppData*() const { return mAppData; }
187 nsXREAppData* operator -> () const { return mAppData; }
189 private:
190 nsXREAppData* mAppData;
193 int main(int argc, char* argv[])
195 char exePath[MAXPATHLEN];
196 nsresult rv = mozilla::BinaryPath::Get(argv[0], exePath);
197 if (NS_FAILED(rv)) {
198 Output(true, "Couldn't calculate the application directory.\n");
199 return 255;
202 char *lastSlash = strrchr(exePath, XPCOM_FILE_PATH_SEPARATOR[0]);
203 if (!lastSlash || (size_t(lastSlash - exePath) > MAXPATHLEN - sizeof(XPCOM_DLL) - 1))
204 return 255;
206 strcpy(++lastSlash, XPCOM_DLL);
208 rv = XPCOMGlueStartup(exePath);
209 if (NS_FAILED(rv)) {
210 Output(true, "Couldn't load XPCOM.\n");
211 return 255;
214 ScopedLogging log;
216 if (argc > 1 && (IsArg(argv[1], "h") ||
217 IsArg(argv[1], "help") ||
218 IsArg(argv[1], "?")))
220 Usage(argv[0]);
221 return 0;
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());
231 return 0;
234 rv = XPCOMGlueLoadXULFunctions(kXULFuncs);
235 if (NS_FAILED(rv)) {
236 Output(true, "Couldn't load XRE functions.\n");
237 return 255;
240 if (argc > 1) {
241 nsAutoCString milestone;
242 rv = GetGREVersion(argv[0], &milestone, nullptr);
243 if (NS_FAILED(rv))
244 return 2;
246 if (IsArg(argv[1], "gre-version")) {
247 if (argc != 2) {
248 Usage(argv[0]);
249 return 1;
252 printf("%s\n", milestone.get());
253 return 0;
256 if (IsArg(argv[1], "install-app")) {
257 Output(true, "--install-app support has been removed. Use 'python install-app.py' instead.\n");
258 return 1;
262 const char *appDataFile = getenv("XUL_APP_FILE");
264 if (!(appDataFile && *appDataFile)) {
265 if (argc < 2) {
266 Usage(argv[0]);
267 return 1;
270 if (IsArg(argv[1], "app")) {
271 if (argc == 2) {
272 Usage(argv[0]);
273 return 1;
275 argv[1] = argv[0];
276 ++argv;
277 --argc;
280 appDataFile = argv[1];
281 argv[1] = argv[0];
282 ++argv;
283 --argc;
285 static char kAppEnv[MAXPATHLEN];
286 snprintf(kAppEnv, MAXPATHLEN, "XUL_APP_FILE=%s", appDataFile);
287 putenv(kAppEnv);
290 nsCOMPtr<nsIFile> appDataLF;
291 rv = XRE_GetFileFromPath(appDataFile, getter_AddRefs(appDataLF));
292 if (NS_FAILED(rv)) {
293 Output(true, "Error: unrecognized application.ini path.\n");
294 return 2;
297 AutoAppData appData(appDataLF);
298 if (!appData) {
299 Output(true, "Error: couldn't parse application.ini.\n");
300 return 2;
303 return XRE_main(argc, argv, appData, 0);