Reland bug 121341.
[wine-gecko.git] / xpcom / tools / registry / regxpcom.cpp
blob98627ba6aa8e00f28a10676a3c7f96360f91b593
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
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/
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.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Pierre Phaneuf <pp@ludusdesign.com>
24 * Mike Shaver <shaver@mozilla.org>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #include "stdlib.h"
41 #include "prenv.h"
42 #include "nspr.h"
44 #include "nsXPCOMPrivate.h" // for XPCOM_DLL defines.
46 #include "nsXPCOMGlue.h"
47 #include "nsIComponentManager.h"
48 #include "nsIComponentRegistrar.h"
49 #include "nsIServiceManager.h"
50 #include "nsCOMPtr.h"
51 #include "nsILocalFile.h"
52 #include "nsEmbedString.h"
53 #include "nsIDirectoryService.h"
54 #include "nsDirectoryServiceDefs.h"
57 static PRBool gUnreg = PR_FALSE, gQuiet = PR_FALSE;
59 static const char* gXPCOMLocation = nsnull;
60 static const char* gCompRegLocation = nsnull;
61 static const char* gXPTIDatLocation = nsnull;
63 class DirectoryServiceProvider : public nsIDirectoryServiceProvider
65 public:
66 DirectoryServiceProvider() {}
68 NS_DECL_ISUPPORTS
69 NS_DECL_NSIDIRECTORYSERVICEPROVIDER
71 private:
72 ~DirectoryServiceProvider() {}
75 NS_IMPL_ISUPPORTS1(DirectoryServiceProvider, nsIDirectoryServiceProvider)
77 NS_IMETHODIMP
78 DirectoryServiceProvider::GetFile(const char *prop, PRBool *persistent, nsIFile **_retval)
80 nsCOMPtr<nsILocalFile> localFile;
81 nsresult rv = NS_ERROR_FAILURE;
83 *_retval = nsnull;
84 *persistent = PR_TRUE;
86 const char* fileLocation = nsnull;
88 if(strcmp(prop, NS_XPCOM_CURRENT_PROCESS_DIR) == 0 && gXPCOMLocation)
90 fileLocation = gXPCOMLocation;
92 else if(strcmp(prop, NS_XPCOM_COMPONENT_REGISTRY_FILE) == 0 && gCompRegLocation)
94 fileLocation = gCompRegLocation;
96 else if(strcmp(prop, NS_XPCOM_XPTI_REGISTRY_FILE) == 0 && gXPTIDatLocation)
98 fileLocation = gXPTIDatLocation;
100 else
101 return NS_ERROR_FAILURE;
103 rv = NS_NewNativeLocalFile(nsEmbedCString(fileLocation), PR_TRUE, getter_AddRefs(localFile));
104 if (NS_FAILED(rv)) return rv;
106 return localFile->QueryInterface(NS_GET_IID(nsIFile), (void**)_retval);
109 int startup_xpcom()
111 nsresult rv;
113 if (gXPCOMLocation) {
114 int len = strlen(gXPCOMLocation);
115 char* xpcomPath = (char*) malloc(len + sizeof(XPCOM_DLL) + sizeof(XPCOM_FILE_PATH_SEPARATOR) + 1);
116 sprintf(xpcomPath, "%s" XPCOM_FILE_PATH_SEPARATOR XPCOM_DLL, gXPCOMLocation);
118 rv = XPCOMGlueStartup(xpcomPath);
120 free(xpcomPath);
122 const char* path = getenv(XPCOM_SEARCH_KEY);
123 if (!path) {
124 path = "";
127 else
129 rv = XPCOMGlueStartup(nsnull);
132 if (NS_FAILED(rv))
134 printf("Can not initialize XPCOM Glue\n");
135 return -1;
138 DirectoryServiceProvider *provider = new DirectoryServiceProvider();
139 if ( !provider )
141 NS_WARNING("GRE_Startup failed");
142 XPCOMGlueShutdown();
143 return -1;
146 nsCOMPtr<nsILocalFile> file;
147 if (gXPCOMLocation)
149 rv = NS_NewNativeLocalFile(nsEmbedCString(gXPCOMLocation),
150 PR_TRUE,
151 getter_AddRefs(file));
154 NS_ADDREF(provider);
155 rv = NS_InitXPCOM2(nsnull, file, provider);
156 NS_RELEASE(provider);
158 if (NS_FAILED(rv)) {
159 printf("Can not initialize XPCOM\n");
160 XPCOMGlueShutdown();
161 return -1;
164 return 0;
167 void shutdown_xpcom()
169 nsresult rv;
171 rv = NS_ShutdownXPCOM(nsnull);
173 if (NS_FAILED(rv)) {
174 printf("Can not shutdown XPCOM cleanly\n");
177 rv = XPCOMGlueShutdown();
179 if (NS_FAILED(rv)) {
180 printf("Can not shutdown XPCOM Glue cleanly\n");
185 nsresult Register(const char *path)
187 startup_xpcom();
189 nsresult rv;
190 nsCOMPtr<nsILocalFile> spec;
192 if (path) {
193 rv = NS_NewNativeLocalFile(nsEmbedCString(path),
194 PR_TRUE,
195 getter_AddRefs(spec));
198 nsCOMPtr<nsIComponentRegistrar> registrar;
199 rv = NS_GetComponentRegistrar(getter_AddRefs(registrar));
200 if (NS_FAILED(rv)) {
201 printf("Can not aquire component registrar\n");
202 return rv;
205 if (gUnreg)
206 rv = registrar->AutoUnregister(spec);
207 else
208 rv = registrar->AutoRegister(spec);
210 spec = 0;
211 registrar = 0;
213 shutdown_xpcom();
214 return rv;
218 void ReportSuccess(const char *file)
220 if (gQuiet)
221 return;
223 if (gUnreg)
224 printf("Unregistration successful for %s\n", file);
225 else
226 printf("Registration successful for %s\n", file);
229 void ReportError(nsresult err, const char *file)
231 if (gUnreg)
232 printf("Unregistration failed: (");
233 else
234 printf("Registration failed: (");
236 switch (err)
238 case NS_ERROR_FACTORY_NOT_LOADED:
239 printf("Factory not loaded");
240 break;
241 case NS_NOINTERFACE:
242 printf("No Interface");
243 break;
244 case NS_ERROR_NULL_POINTER:
245 printf("Null pointer");
246 break;
247 case NS_ERROR_OUT_OF_MEMORY:
248 printf("Out of memory");
249 break;
250 default:
251 printf("%x", (unsigned)err);
254 printf(") %s\n", file);
257 void printHelp()
259 printf(
260 "Mozilla regxpcom - a registration tool for xpcom components \n"
261 " \n"
262 "Usage: regxpcom [options] [file-or-directory] \n"
263 " \n"
264 "Options: \n"
265 " -x path Specifies the location of a directory containing the \n"
266 " xpcom library which will be used when registering new \n"
267 " component libraries. This path will also be added to \n"
268 " the \"load library\" path. If not specified, the \n"
269 " current working directory will be used. \n"
270 " -c path Specifies the location of the compreg.dat file. If \n"
271 " not specified, the compreg.dat file will be in its \n"
272 " default location. \n"
273 " -d path Specifies the location of the xpti.dat file. If not \n"
274 " specified, the xpti.dat file will be in its default \n"
275 " location. \n"
276 " -a Option to register all files in the default component \n"
277 " directories. This is the default behavior if regxpcom \n"
278 " is called without any arguments. \n"
279 " -h Displays this help screen. Must be the only option \n"
280 " specified. \n"
281 " -u Option to uninstall the files-or-directory instead of \n"
282 " registering them. \n"
283 " -q Quiets some of the output of regxpcom. \n\n");
286 int ProcessArgs(int argc, char *argv[])
288 int i = 1, result = 0;
289 nsresult res;
291 while (i < argc)
293 if (argv[i][0] == '-')
295 int j;
296 for (j = 1; argv[i][j] != '\0'; j++)
298 switch (argv[i][j])
300 case 'h':
301 printHelp();
302 return 0; // we are all done!
304 case 'u':
305 gUnreg = PR_TRUE;
306 break;
308 case 'q':
309 gQuiet = PR_TRUE;
310 break;
312 case 'a':
314 res = Register(nsnull);
315 if (NS_FAILED(res))
317 ReportError(res, "component directory");
318 result = -1;
320 else
322 ReportSuccess("component directory");
325 break;
327 case 'x':
328 gXPCOMLocation = argv[++i];
329 j = strlen(gXPCOMLocation) - 1;
330 break;
332 case 'c':
333 gCompRegLocation = argv[++i];
334 j = strlen(gCompRegLocation) - 1;
335 break;
337 case 'd':
338 gXPTIDatLocation = argv[++i];
339 j = strlen(gXPTIDatLocation) - 1;
340 break;
342 default:
343 printf("Unknown option '%c'\n", argv[i][j]);
347 else
349 res = Register(argv[i]);
351 if (NS_FAILED(res))
353 ReportError(res, argv[i]);
354 result = -1;
356 else
358 ReportSuccess(argv[i]);
361 i++;
363 return result;
367 int main(int argc, char *argv[])
369 int ret;
370 nsresult rv;
372 /* With no arguments, regxpcom will autoregister */
373 if (argc <= 1)
375 startup_xpcom();
376 nsCOMPtr<nsIComponentRegistrar> registrar;
377 rv = NS_GetComponentRegistrar(getter_AddRefs(registrar));
378 if (NS_FAILED(rv)) {
379 printf("Can not aquire component registrar\n");
380 return -1;
382 rv = registrar->AutoRegister(nsnull);
383 ret = (NS_FAILED(rv)) ? -1 : 0;
384 registrar = 0;
385 shutdown_xpcom();
386 } else
387 ret = ProcessArgs(argc, argv);
389 return ret;