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
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.
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 ***** */
44 #include "nsXPCOMPrivate.h" // for XPCOM_DLL defines.
46 #include "nsXPCOMGlue.h"
47 #include "nsIComponentManager.h"
48 #include "nsIComponentRegistrar.h"
49 #include "nsIServiceManager.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
66 DirectoryServiceProvider() {}
69 NS_DECL_NSIDIRECTORYSERVICEPROVIDER
72 ~DirectoryServiceProvider() {}
75 NS_IMPL_ISUPPORTS1(DirectoryServiceProvider
, nsIDirectoryServiceProvider
)
78 DirectoryServiceProvider::GetFile(const char *prop
, PRBool
*persistent
, nsIFile
**_retval
)
80 nsCOMPtr
<nsILocalFile
> localFile
;
81 nsresult rv
= NS_ERROR_FAILURE
;
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
;
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
);
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
);
122 const char* path
= getenv(XPCOM_SEARCH_KEY
);
129 rv
= XPCOMGlueStartup(nsnull
);
134 printf("Can not initialize XPCOM Glue\n");
138 DirectoryServiceProvider
*provider
= new DirectoryServiceProvider();
141 NS_WARNING("GRE_Startup failed");
146 nsCOMPtr
<nsILocalFile
> file
;
149 rv
= NS_NewNativeLocalFile(nsEmbedCString(gXPCOMLocation
),
151 getter_AddRefs(file
));
155 rv
= NS_InitXPCOM2(nsnull
, file
, provider
);
156 NS_RELEASE(provider
);
159 printf("Can not initialize XPCOM\n");
167 void shutdown_xpcom()
171 rv
= NS_ShutdownXPCOM(nsnull
);
174 printf("Can not shutdown XPCOM cleanly\n");
177 rv
= XPCOMGlueShutdown();
180 printf("Can not shutdown XPCOM Glue cleanly\n");
185 nsresult
Register(const char *path
)
190 nsCOMPtr
<nsILocalFile
> spec
;
193 rv
= NS_NewNativeLocalFile(nsEmbedCString(path
),
195 getter_AddRefs(spec
));
198 nsCOMPtr
<nsIComponentRegistrar
> registrar
;
199 rv
= NS_GetComponentRegistrar(getter_AddRefs(registrar
));
201 printf("Can not aquire component registrar\n");
206 rv
= registrar
->AutoUnregister(spec
);
208 rv
= registrar
->AutoRegister(spec
);
218 void ReportSuccess(const char *file
)
224 printf("Unregistration successful for %s\n", file
);
226 printf("Registration successful for %s\n", file
);
229 void ReportError(nsresult err
, const char *file
)
232 printf("Unregistration failed: (");
234 printf("Registration failed: (");
238 case NS_ERROR_FACTORY_NOT_LOADED
:
239 printf("Factory not loaded");
242 printf("No Interface");
244 case NS_ERROR_NULL_POINTER
:
245 printf("Null pointer");
247 case NS_ERROR_OUT_OF_MEMORY
:
248 printf("Out of memory");
251 printf("%x", (unsigned)err
);
254 printf(") %s\n", file
);
260 "Mozilla regxpcom - a registration tool for xpcom components \n"
262 "Usage: regxpcom [options] [file-or-directory] \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"
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"
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;
293 if (argv
[i
][0] == '-')
296 for (j
= 1; argv
[i
][j
] != '\0'; j
++)
302 return 0; // we are all done!
314 res
= Register(nsnull
);
317 ReportError(res
, "component directory");
322 ReportSuccess("component directory");
328 gXPCOMLocation
= argv
[++i
];
329 j
= strlen(gXPCOMLocation
) - 1;
333 gCompRegLocation
= argv
[++i
];
334 j
= strlen(gCompRegLocation
) - 1;
338 gXPTIDatLocation
= argv
[++i
];
339 j
= strlen(gXPTIDatLocation
) - 1;
343 printf("Unknown option '%c'\n", argv
[i
][j
]);
349 res
= Register(argv
[i
]);
353 ReportError(res
, argv
[i
]);
358 ReportSuccess(argv
[i
]);
367 int main(int argc
, char *argv
[])
372 /* With no arguments, regxpcom will autoregister */
376 nsCOMPtr
<nsIComponentRegistrar
> registrar
;
377 rv
= NS_GetComponentRegistrar(getter_AddRefs(registrar
));
379 printf("Can not aquire component registrar\n");
382 rv
= registrar
->AutoRegister(nsnull
);
383 ret
= (NS_FAILED(rv
)) ? -1 : 0;
387 ret
= ProcessArgs(argc
, argv
);