nsBrowserApp.cpp
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is mozilla.org code.
16  *
17  * The Initial Developer of the Original Code is
18  * Netscape Communications Corporation.
19  * Portions created by the Initial Developer are Copyright (C) 2002
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  * Brian Ryner <bryner@brianryner.com>
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either the GNU General Public License Version 2 or later (the "GPL"), or
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38 
39 #include "nsXULAppAPI.h"
40 #ifdef XP_WIN
41 #include <windows.h>
42 #include <stdlib.h>
43 #endif
44 
45 #include <stdio.h>
46 #include <stdarg.h>
47 
48 #include "plstr.h"
49 #include "prprf.h"
50 #include "prenv.h"
51 
52 #include "nsCOMPtr.h"
53 #include "nsILocalFile.h"
54 #include "nsStringGlue.h"
55 
56 #ifdef XP_WIN
57 // we want to use the DLL blocklist if possible
58 #define XRE_WANT_DLL_BLOCKLIST
59 // we want a wmain entry point
60 #include "nsWindowsWMain.cpp"
61 #endif
62 
63 static void Output(const char *fmt, ... )
64 {
65  va_list ap;
66  va_start(ap, fmt);
67 
68 #if defined(XP_WIN) && !MOZ_WINCONSOLE
69  PRUnichar msg[2048];
70  _vsnwprintf(msg, sizeof(msg)/sizeof(msg[0]), NS_ConvertUTF8toUTF16(fmt).get(), ap);
71  MessageBoxW(NULL, msg, L"XULRunner", MB_OK | MB_ICONERROR);
72 #else
73  vfprintf(stderr, fmt, ap);
74 #endif
75 
76  va_end(ap);
77 }
78 
82 static PRBool IsArg(const char* arg, const char* s)
83 {
84  if (*arg == '-')
85  {
86  if (*++arg == '-')
87  ++arg;
88  return !PL_strcasecmp(arg, s);
89  }
90 
91 #if defined(XP_WIN) || defined(XP_OS2)
92  if (*arg == '/')
93  return !PL_strcasecmp(++arg, s);
94 #endif
95 
96  return PR_FALSE;
97 }
98 
103 {
104 public:
105  ScopedLogging() { NS_LogInit(); }
106  ~ScopedLogging() { NS_LogTerm(); }
107 };
108 
109 int main(int argc, char* argv[])
110 {
112 
113  nsCOMPtr<nsILocalFile> appini;
114  nsresult rv = XRE_GetBinaryPath(argv[0], getter_AddRefs(appini));
115  if (NS_FAILED(rv)) {
116  Output("Couldn't calculate the application directory.");
117  return 255;
118  }
119  appini->SetNativeLeafName(NS_LITERAL_CSTRING("application.ini"));
120 
121  // Allow firefox.exe to launch XULRunner apps via -app <application.ini>
122  // Note that -app must be the *first* argument.
123  char *appEnv = nsnull;
124  const char *appDataFile = PR_GetEnv("XUL_APP_FILE");
125  if (appDataFile && *appDataFile) {
126  rv = XRE_GetFileFromPath(appDataFile, getter_AddRefs(appini));
127  if (NS_FAILED(rv)) {
128  Output("Invalid path found: '%s'", appDataFile);
129  return 255;
130  }
131  }
132  else if (argc > 1 && IsArg(argv[1], "app")) {
133  if (argc == 2) {
134  Output("Incorrect number of arguments passed to -app");
135  return 255;
136  }
137 
138  rv = XRE_GetFileFromPath(argv[2], getter_AddRefs(appini));
139  if (NS_FAILED(rv)) {
140  Output("application.ini path not recognized: '%s'", argv[2]);
141  return 255;
142  }
143 
144  appEnv = PR_smprintf("XUL_APP_FILE=%s", argv[2]);
145  PR_SetEnv(appEnv);
146  argv[2] = argv[0];
147  argv += 2;
148  argc -= 2;
149  }
150 
151  nsXREAppData *appData;
152  rv = XRE_CreateAppData(appini, &appData);
153  if (NS_FAILED(rv)) {
154  Output("Couldn't read application.ini");
155  return 255;
156  }
157 
158  int result = XRE_main(argc, argv, appData);
159  XRE_FreeAppData(appData);
160  if (appEnv)
161  PR_smprintf_free(appEnv);
162  return result;
163 }
static PRBool IsArg(const char *arg, const char *s)
function log(s)
int main(int argc, char *argv[])
static void Output(const char *fmt,...)
function msg