1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is Mozilla.
16 * The Initial Developer of the Original Code is IBM Corporation.
17 * Portions created by IBM Corporation are Copyright (C) 2003
18 * IBM Corporation. All Rights Reserved.
21 * Darin Fisher <darin@meer.net>
22 * Benjamin Smedberg <benjamin@smedbergs.us>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
44 #include "nsXULAppAPI.h"
45 #include "nsXPCOMGlue.h"
46 #include "nsRegisterGRE.h"
47 #include "nsAppRunner.h"
48 #include "nsILocalFile.h"
49 #include "nsIXULAppInstall.h"
52 #include "nsCRTGlue.h"
53 #include "nsStringAPI.h"
54 #include "nsServiceManagerUtils.h"
58 #include "nsINIParser.h"
61 #include "nsWindowsWMain.cpp"
65 * Output a string to the user. This method is really only meant to be used to
66 * output last-ditch error messages designed for developers NOT END USERS.
69 * Pass true to indicate severe errors.
71 * printf-style format string followed by arguments.
73 static void Output(PRBool isError
, const char *fmt
, ... )
78 #if (defined(XP_WIN) && !MOZ_WINCONSOLE) || defined(WINCE)
79 char *msg
= PR_vsmprintf(fmt
, ap
);
84 flags
|= MB_ICONERROR
;
86 flags
|= MB_ICONINFORMATION
;
88 wchar_t wide_msg
[1024];
89 MultiByteToWideChar(CP_ACP
,
94 sizeof(wide_msg
) / sizeof(wchar_t));
96 MessageBoxW(NULL
, wide_msg
, L
"XULRunner", flags
);
98 PR_smprintf_free(msg
);
101 vfprintf(stderr
, fmt
, ap
);
108 * Return true if |arg| matches the given argument name.
110 static PRBool
IsArg(const char* arg
, const char* s
)
116 return !PL_strcasecmp(arg
, s
);
119 #if defined(XP_WIN) || defined(XP_OS2)
121 return !PL_strcasecmp(++arg
, s
);
128 GetGREVersion(const char *argv0
,
129 nsACString
*aMilestone
,
130 nsACString
*aVersion
)
133 aMilestone
->Assign("<Error>");
135 aVersion
->Assign("<Error>");
137 nsCOMPtr
<nsILocalFile
> iniFile
;
138 nsresult rv
= XRE_GetBinaryPath(argv0
, getter_AddRefs(iniFile
));
142 iniFile
->SetNativeLeafName(NS_LITERAL_CSTRING("platform.ini"));
145 rv
= parser
.Init(iniFile
);
150 rv
= parser
.GetString("Build", "Milestone", *aMilestone
);
155 rv
= parser
.GetString("Build", "BuildID", *aVersion
);
162 static void Usage(const char *argv0
)
164 nsCAutoString milestone
;
165 GetGREVersion(argv0
, &milestone
, nsnull
);
167 // display additional information (XXX make localizable?)
169 "Mozilla XULRunner %s\n\n"
170 "Usage: " XULRUNNER_PROGNAME
" [OPTIONS]\n"
171 " " XULRUNNER_PROGNAME
" APP-FILE [APP-OPTIONS...]\n"
174 " --app specify APP-FILE (optional)\n"
175 " -h, --help show this message\n"
176 " -v, --version show version\n"
177 " --gre-version print the GRE version string on stdout\n"
178 " --register-global register this GRE in the machine registry\n"
179 " --register-user register this GRE in the user registry\n"
180 " --unregister-global unregister this GRE formerly registered with\n"
181 " --register-global\n"
182 " --unregister-user unregister this GRE formely registered with\n"
184 " --find-gre <version> Find a GRE with version <version> and print\n"
185 " the path on stdout\n"
186 " --install-app <application> [<destination> [<directoryname>]]\n"
187 " Install a XUL application.\n"
190 " Application initialization file.\n"
193 " Application specific options.\n",
198 GetXULRunnerDir(const char *argv0
, nsIFile
* *aResult
)
202 nsCOMPtr
<nsILocalFile
> appFile
;
203 rv
= XRE_GetBinaryPath(argv0
, getter_AddRefs(appFile
));
205 Output(PR_TRUE
, "Could not find XULRunner application path.\n");
209 rv
= appFile
->GetParent(aResult
);
211 Output(PR_TRUE
, "Could not find XULRunner installation dir.\n");
217 InstallXULApp(nsIFile
* aXULRunnerDir
,
218 const char *aAppLocation
,
219 const char *aInstallTo
,
220 const char *aLeafName
)
222 nsCOMPtr
<nsILocalFile
> appLocation
;
223 nsCOMPtr
<nsILocalFile
> installTo
;
226 nsresult rv
= XRE_GetFileFromPath(aAppLocation
, getter_AddRefs(appLocation
));
231 rv
= XRE_GetFileFromPath(aInstallTo
, getter_AddRefs(installTo
));
237 NS_CStringToUTF16(nsDependentCString(aLeafName
),
238 NS_CSTRING_ENCODING_NATIVE_FILESYSTEM
, leafName
);
240 rv
= NS_InitXPCOM2(nsnull
, aXULRunnerDir
, nsnull
);
245 // Scope our COMPtr to avoid holding XPCOM refs beyond xpcom shutdown
246 nsCOMPtr
<nsIXULAppInstall
> install
247 (do_GetService("@mozilla.org/xulrunner/app-install-service;1"));
249 rv
= NS_ERROR_FAILURE
;
252 rv
= install
->InstallApplication(appLocation
, installTo
, leafName
);
256 NS_ShutdownXPCOM(nsnull
);
264 static const GREProperty kGREProperties
[] = {
265 { "xulrunner", "true" }
266 #ifdef TARGET_XPCOM_ABI
267 , { "abi", TARGET_XPCOM_ABI
}
270 , { "javaxpcom", "1" }
277 AutoAppData(nsILocalFile
* aINIFile
) : mAppData(nsnull
) {
278 nsresult rv
= XRE_CreateAppData(aINIFile
, &mAppData
);
284 XRE_FreeAppData(mAppData
);
287 operator nsXREAppData
*() const { return mAppData
; }
288 nsXREAppData
* operator -> () const { return mAppData
; }
291 nsXREAppData
* mAppData
;
294 int main(int argc
, char* argv
[])
296 if (argc
> 1 && (IsArg(argv
[1], "h") ||
297 IsArg(argv
[1], "help") ||
298 IsArg(argv
[1], "?")))
304 if (argc
== 2 && (IsArg(argv
[1], "v") || IsArg(argv
[1], "version")))
306 nsCAutoString milestone
;
307 nsCAutoString version
;
308 GetGREVersion(argv
[0], &milestone
, &version
);
309 Output(PR_FALSE
, "Mozilla XULRunner %s - %s\n",
310 milestone
.get(), version
.get());
315 nsCAutoString milestone
;
316 nsresult rv
= GetGREVersion(argv
[0], &milestone
, nsnull
);
320 PRBool registerGlobal
= IsArg(argv
[1], "register-global");
321 PRBool registerUser
= IsArg(argv
[1], "register-user");
322 if (registerGlobal
|| registerUser
) {
328 nsCOMPtr
<nsIFile
> regDir
;
329 rv
= GetXULRunnerDir(argv
[0], getter_AddRefs(regDir
));
333 return RegisterXULRunner(registerGlobal
, regDir
,
335 NS_ARRAY_LENGTH(kGREProperties
),
336 milestone
.get()) ? 0 : 2;
339 registerGlobal
= IsArg(argv
[1], "unregister-global");
340 registerUser
= IsArg(argv
[1], "unregister-user");
341 if (registerGlobal
|| registerUser
) {
347 nsCOMPtr
<nsIFile
> regDir
;
348 rv
= GetXULRunnerDir(argv
[0], getter_AddRefs(regDir
));
352 UnregisterXULRunner(registerGlobal
, regDir
, milestone
.get());
356 if (IsArg(argv
[1], "find-gre")) {
362 char path
[MAXPATHLEN
];
363 static const GREVersionRange vr
= {
367 static const GREProperty kProperties
[] = {
368 { "xulrunner", "true" }
371 rv
= GRE_GetGREPathWithProperties(&vr
, 1, kProperties
,
372 NS_ARRAY_LENGTH(kProperties
),
377 printf("%s\n", path
);
381 if (IsArg(argv
[1], "gre-version")) {
387 printf("%s\n", milestone
.get());
391 if (IsArg(argv
[1], "install-app")) {
392 if (argc
< 3 || argc
> 5) {
397 char *appLocation
= argv
[2];
399 char *installTo
= nsnull
;
402 if (!*installTo
) // left blank?
406 char *leafName
= nsnull
;
413 nsCOMPtr
<nsIFile
> regDir
;
414 rv
= GetXULRunnerDir(argv
[0], getter_AddRefs(regDir
));
418 return InstallXULApp(regDir
, appLocation
, installTo
, leafName
);
422 const char *appDataFile
= PR_GetEnv("XUL_APP_FILE");
424 if (!(appDataFile
&& *appDataFile
)) {
430 if (IsArg(argv
[1], "app")) {
440 appDataFile
= argv
[1];
445 static char kAppEnv
[MAXPATHLEN
];
446 PR_snprintf(kAppEnv
, MAXPATHLEN
, "XUL_APP_FILE=%s", appDataFile
);
450 nsCOMPtr
<nsILocalFile
> appDataLF
;
451 nsresult rv
= XRE_GetFileFromPath(appDataFile
, getter_AddRefs(appDataLF
));
453 Output(PR_TRUE
, "Error: unrecognized application.ini path.\n");
457 AutoAppData
appData(appDataLF
);
459 Output(PR_TRUE
, "Error: couldn't parse application.ini.\n");
463 return XRE_main(argc
, argv
, appData
);