/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is Mozilla Communicator client code. * * The Initial Developer of the Original Code is * Netscape Communications Corporation. * Portions created by the Initial Developer are Copyright (C) 1998 * the Initial Developer. All Rights Reserved. * * Contributor(s): * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */ #include "nsICmdLineService.h" #include "nsCommandLineService.h" #include "nsIComponentManager.h" #include "nsILocalFile.h" #include "nsString.h" #include "plstr.h" #include "nsCRT.h" #include "nsNetUtil.h" #ifdef XP_MACOSX #include "nsCommandLineServiceMac.h" #endif nsCmdLineService::nsCmdLineService() : mArgCount(0), mArgc(0), mArgv(0) { } /* * Implement the nsISupports methods... */ NS_IMPL_ISUPPORTS1(nsCmdLineService, nsICmdLineService) static void* ProcessURLArg(char* str) { // Problem: since the arg parsing code doesn't know which flags // take arguments, it always calls this method for the last // non-flag argument. But sometimes that argument is actually // the arg for the last switch, e.g. -width 500 or -Profile default. // nsLocalFile will only work on absolute pathnames, so return // if str doesn't start with '/' or '\'. if (str && (*str == '\\' || *str == '/')) { nsCOMPtr uri; nsresult rv = NS_NewURI(getter_AddRefs(uri), str); if (NS_FAILED(rv)) { nsCOMPtr file(do_CreateInstance("@mozilla.org/file/local;1")); if (file) { rv = file->InitWithNativePath(nsDependentCString(str)); if (NS_SUCCEEDED(rv)) { nsCAutoString fileurl; rv = NS_GetURLSpecFromFile(file, fileurl); if (NS_SUCCEEDED(rv)) return NS_REINTERPRET_CAST(void*, ToNewCString(fileurl)); } } } } return NS_REINTERPRET_CAST(void*, nsCRT::strdup(str)); } NS_IMETHODIMP nsCmdLineService::Initialize(int aArgc, char ** aArgv) { PRInt32 i=0; nsresult rv = nsnull; #ifdef XP_MACOSX rv = InitializeMacCommandLine(aArgc, aArgv); NS_ASSERTION(NS_SUCCEEDED(rv), "Initializing AppleEvents failed"); #endif // Save aArgc and argv mArgc = aArgc; mArgv = new char*[ aArgc ]; for(i=0; i 0 && aArgv[0]) { mArgList.AppendElement(NS_REINTERPRET_CAST(void*, nsCRT::strdup("-progname"))); mArgValueList.AppendElement(NS_REINTERPRET_CAST(void*, nsCRT::strdup(aArgv[0]))); mArgCount++; i++; } for(i=1; iCount(); i++) { const char *param = (const char*)paramList->ElementAt(i); // skip past leading / and - if (*param == '-' || *param == '/') { ++param; if (*param == *(param-1)) // skip "--" or "//" ++param; } nsCAutoString contractID("@mozilla.org/commandlinehandler/general-startup;1?type="); contractID += param; nsCOMPtr handler = do_GetService(contractID.get(), &rv); if (NS_FAILED(rv)) continue; *aResult = handler; NS_ADDREF(*aResult); return NS_OK; } // went through all the parameters, didn't find one return NS_ERROR_FAILURE; } #if 0 NS_IMETHODIMP nsCmdLineService::PrintCmdArgs() { if (mArgCount == 0) { printf("No command line options provided\n"); return; } for (int i=0; i