Beispiel #1
0
/**
   @param argument the string to check for. If the argument is found,
   then it is removed from the argument list

   @param returnFirst true if we should just take the first matching argument,
   or false if we should iterate through the list and only use the last one
   (default is false, use true if you want to use the same parameter
   multiple times)

   @return NULL if the argument wasn't found; the value given after the
   found argument; or at empty string (with a NULL first character) if
   the argument was found but no value followed the argument flag.
**/
AREXPORT char * ArArgumentParser::checkParameterArgument(const char *argument,
							 bool returnFirst)
{
  char *ret;
  char *retRecursive;
  size_t i;
  std::string extraHyphen;

  extraHyphen = "-";
  extraHyphen += argument;

  for (i = 0; i < getArgc(); i++)
  {
    if (strcasecmp(argument, getArgv()[i]) == 0 ||
	strcasecmp(extraHyphen.c_str(), getArgv()[i]) == 0)
    {
      // see if we have a ret, we don't if the ret would be beyond argc
      if (getArgc() > i+1)
      {
	ret = getArgv()[i+1];
      }
      else
      {
	ret = myEmptyArg;
      }
      // remove our argument
      removeArg(i);
      // if we have a return remove that one too
      if (ret != NULL && ret != myEmptyArg)
	removeArg(i);
      // now see if there are any more, if so return that
      if (returnFirst)
      {
	return ret;
      }
      else if ((retRecursive = checkParameterArgument(argument)) != NULL)
      {
	return retRecursive;
      }
      // otherwise return what we found
      else
      {
	return ret;
      }
    }
  }
  return NULL;
}
Beispiel #2
0
/**
   @param argument the string to check for, if the argument is found
   its pulled from the list of arguments

   @param ... the extra string to feed into the argument for parsing
   (like printf)

   @return true if the argument was found, false otherwise
**/
AREXPORT bool ArArgumentParser::checkArgument(const char *argument)
{
  size_t i;
  std::string extraHyphen;
  extraHyphen = "-";
  extraHyphen += argument;
  for (i = 0; i < getArgc(); i++)
  {
    if (strcasecmp(argument, getArgv()[i]) == 0 ||
	strcasecmp(extraHyphen.c_str(), getArgv()[i]) == 0)
    {
      removeArg(i);
      // MPL took this out so you could add the same arg multiple times
      //checkArgument(argument);
      return true;
    }
  }
  return false;
}
AREXPORT void ArArgumentBuilder::compressQuoted(bool stripQuotationMarks)
{
  size_t argLen;
  size_t i;
  std::string myNewArg;

  for (i = 0; i < myArgc; i++)
  {
    argLen = strlen(myArgv[i]);
    if (stripQuotationMarks && argLen >= 2 && 
	myArgv[i][0] == '"' && myArgv[i][argLen - 1] == '"')
    {
      myNewArg = &myArgv[i][1];
      myNewArg[myNewArg.size() - 1] = '\0';
      delete[] myArgv[i];
      // but replacing ourself with the new arg
      myArgv[i] = cppstrdup(myNewArg.c_str());
      //myArgv[i] = strdup(myNewArg.c_str());
      continue;
    }
    // if this arg begins with a quote but doesn't end with one
    if (argLen >= 2 && myArgv[i][0] == '"' && myArgv[i][argLen - 1] != '"')
    {
      // start the new value for this arg, if stripping quotations
      // then start after the quote
      if (stripQuotationMarks)
	myNewArg = &myArgv[i][1];
      else
	myNewArg = myArgv[i];

      bool isEndQuoteFound = false;

      // now while the end char of the next args isn't the end of our
      // start quote we toss things into this arg
      while ((i + 1 < myArgc) && !isEndQuoteFound) {
          
        int nextArgLen = strlen(myArgv[i+1]);

        // Check whether the next arg contains the ending quote...
        if ((nextArgLen > 0) &&
	    (myArgv[i+1][nextArgLen - 1] == '"')) 
	{
	      isEndQuoteFound = true;
        }
    
        // Concatenate the next arg to this one...
        myNewArg += " ";
        myNewArg += myArgv[i+1];
	// if we are striping quotes off then replace the quote
	if (stripQuotationMarks && myNewArg.size() > 0 && isEndQuoteFound)
	  myNewArg[myNewArg.size() - 1] = '\0';
        // removing those next args
        removeArg(i+1);
        // and ourself
        delete[] myArgv[i];

        // but replacing ourself with the new arg
        myArgv[i] = cppstrdup(myNewArg.c_str());
	//myArgv[i] = strdup(myNewArg.c_str());
      }
    }
  }
}
Beispiel #4
0
MulticastPipeMultiplexer* clusterize(int& argc,char**& argv)
	{
	MulticastPipeMultiplexer* result=0;
	
	/* Determine whether this instance is on the master or a slave node: */
	if(argc==8&&strcmp(argv[1],"-clusterizeSlaveInstance")==0)
		{
		/********************
		This is a slave node:
		********************/
		
		/* Read multipipe settings from the command line: */
		int numSlaves=atoi(argv[2]);
		int nodeIndex=atoi(argv[3]);
		char* master=argv[4];
		int masterPort=atoi(argv[5]);
		char* multicastGroup=argv[6];
		int multicastPort=atoi(argv[7]);
		
		/* Connect back to the master: */
		try
			{
			/* Create the multicast multiplexer: */
			result=new Comm::MulticastPipeMultiplexer(numSlaves,nodeIndex,master,masterPort,multicastGroup,multicastPort);
			
			/* Wait until the entire cluster is connected: */
			result->waitForConnection();
			
			{
			/* Read the application's command line via a multicast pipe: */
			MulticastPipe argPipe(result);
			slaveArgc=argPipe.read<int>();
			slaveArgv=new char*[slaveArgc+1];
			for(int i=0;i<=slaveArgc;++i)
				slaveArgv[i]=0;
			for(int i=0;i<slaveArgc;++i)
				slaveArgv[i]=Misc::readCString(argPipe);
			}
			
			/* Override the actual command line provided by the caller: */
			argc=slaveArgc;
			argv=slaveArgv;
			}
		catch(std::runtime_error error)
			{
			std::cerr<<"Node "<<nodeIndex<<": Caught exception "<<error.what()<<" while initializing cluster communication"<<std::endl;
			delete result;
			result=0;
			}
		}
	else
		{
		/* Read and remove clusterization arguments from the command line: */
		const char* hostname=getenv("HOSTNAME");
		if(hostname==0)
			hostname=getenv("HOST");
		std::string master=hostname;
		int masterPort=26000;
		std::vector<std::string> slaves;
		std::string multicastGroup;
		int multicastPort=26000;
		std::string remoteCommand="ssh";
		for(int i=1;i<argc;++i)
			{
			if(argv[i][0]=='-')
				{
				if(strcasecmp(argv[i],"-master")==0)
					{
					removeArg(argc,argv,i);
					if(i<argc)
						{
						master=argv[i];
						removeArg(argc,argv,i);
						}
					--i;
					}
				else if(strcasecmp(argv[i],"-masterPort")==0)
					{
					removeArg(argc,argv,i);
					if(i<argc)
						{
						masterPort=atoi(argv[i]);
						removeArg(argc,argv,i);
						}
					--i;
					}
				else if(strcasecmp(argv[i],"-slaves")==0)
					{
					removeArg(argc,argv,i);
					if(i<argc)
						{
						int numSlaves=atoi(argv[i]);
						removeArg(argc,argv,i);
						for(int j=0;j<numSlaves&&i<argc;++j)
							{
							slaves.push_back(argv[i]);
							removeArg(argc,argv,i);
							}
						}
					--i;
					}
				else if(strcasecmp(argv[i],"-multicastGroup")==0)
					{
					removeArg(argc,argv,i);
					if(i<argc)
						{
						multicastGroup=argv[i];
						removeArg(argc,argv,i);
						}
					--i;
					}
				else if(strcasecmp(argv[i],"-multicastPort")==0)
					{
					removeArg(argc,argv,i);
					if(i<argc)
						{
						multicastPort=atoi(argv[i]);
						removeArg(argc,argv,i);
						}
					--i;
					}
				else if(strcasecmp(argv[i],"-remoteCommand")==0)
					{
					removeArg(argc,argv,i);
					if(i<argc)
						{
						remoteCommand=argv[i];
						removeArg(argc,argv,i);
						}
					--i;
					}
				}
			}
		
		if(!slaves.empty()&&!multicastGroup.empty())
			{
			try
				{
				/* Create the multicast multiplexer: */
				result=new Comm::MulticastPipeMultiplexer(slaves.size(),0,master.c_str(),masterPort,multicastGroup.c_str(),multicastPort);
				masterPort=result->getLocalPortNumber();
				
				/* Start the multipipe slaves on all slave nodes: */
				numSlaves=int(slaves.size());
				slavePids=new pid_t[numSlaves];
				std::string cwd=Misc::getCurrentDirectory();
				size_t rcLen=cwd.length()+strlen(argv[0])+master.length()+multicastGroup.length()+512;
				char* rc=new char[rcLen];
				for(int i=0;i<numSlaves;++i)
					{
					pid_t childPid=fork();
					if(childPid==0)
						{
						/* Delete the multicast pipe multiplexer in the child instance: */
						delete result;
						result=0;
						
						/* Create a command line to run the program from the current working directory: */
						int ai=0;
						ai+=snprintf(rc+ai,rcLen-ai,"cd %s ;",cwd.c_str());
						ai+=snprintf(rc+ai,rcLen-ai," %s",argv[0]);
						ai+=snprintf(rc+ai,rcLen-ai," -clusterizeSlaveInstance");
						ai+=snprintf(rc+ai,rcLen-ai," %d %d",numSlaves,i+1);
						ai+=snprintf(rc+ai,rcLen-ai," %s %d",master.c_str(),masterPort);
						ai+=snprintf(rc+ai,rcLen-ai," %s %d",multicastGroup.c_str(),multicastPort);
						
						/* Create command line for the ssh (or other remote login) program: */
						char* sshArgv[20];
						int sshArgc=0;
						sshArgv[sshArgc++]=const_cast<char*>(remoteCommand.c_str());
						sshArgv[sshArgc++]=const_cast<char*>(slaves[i].c_str());
						sshArgv[sshArgc++]=rc;
						sshArgv[sshArgc]=0;
						
						/* Run the remote login program: */
						execvp(sshArgv[0],sshArgv);
						}
					else
						{
						/* Store PID of ssh process for later: */
						slavePids[i]=childPid;
						}
					}
				
				/* Clean up: */
				delete[] rc;
				
				/* Wait until the entire cluster is connected: */
				result->waitForConnection();
				
				{
				/* Write the application's command line to a multicast pipe: */
				MulticastPipe argPipe(result);
				argPipe.write<int>(argc);
				for(int i=0;i<argc;++i)
					Misc::writeCString(argv[i],argPipe);
				argPipe.finishMessage();
				}
				}
			catch(std::runtime_error error)
				{
				std::cerr<<"Master node: Caught exception "<<error.what()<<" while initializing cluster communication"<<std::endl;
				delete result;
				result=0;
				}
			}
		}
	
	return result;
	}
Beispiel #5
0
int main(int argc, char **argv)
{
#ifdef WIN32
    PRBool bLoadMySQL = PR_FALSE;

#ifdef DEBUG
    // the pref setting for debug is too late.  Let's use a cmdline arg of -debug
    for(int i=1; i<argc; i++)
    {
        if (stricmp(argv[i], "-debug") == 0)
        {        
            char szMessage [256];
            wsprintf (szMessage, "Please attach a debugger to the process 0x%X"
                                 " [%d] and click OK",
                      GetCurrentProcessId(), GetCurrentProcessId() );
            MessageBox(NULL, szMessage, "Jaxer Debug Time!",
                       MB_OK | MB_SERVICE_NOTIFICATION);

            removeArg(&argc, &argv[i]);
            break;
        }
    }
#endif
#endif

    nsresult rv;

	nsIServiceManager* serviceManager;
    AppDirectoryProvider appDirectoryProvider;
    nsCOMPtr<nsIDirectoryServiceProvider> dirProvider(&appDirectoryProvider);


	/* Preprocess specific command line args */
	for (int i = 1; i < argc; /*NOP*/ ) {
		if (!strcmp(argv[i], "-tempdir") || !strcmp(argv[i], "--tempdir")) {
			removeArg(&argc, &argv[i]);
			if (i < argc) {
				appDirectoryProvider.SetTempPath(nsDependentCString(argv[i]));
				removeArg(&argc, &argv[i]);
			}
		} else if (!strcmp(argv[i], "-extensions") || !strcmp(argv[i], "--extensions")) {
			removeArg(&argc, &argv[i]);
			if (i < argc) {
				appDirectoryProvider.SetExtensionsPath(nsDependentCString(argv[i]));
				removeArg(&argc, &argv[i]);
			}
		} else if (!strcmp(argv[i], "-handlewinmysqlclientbug") || !strcmp(argv[i], "--handlewinmysqlclientbug")) {
			removeArg(&argc, &argv[i]);
			if (i < argc) {
#ifdef _WIN32
				if(!stricmp(argv[i], "true") || !strcmp(argv[i], "1"))
                    bLoadMySQL = PR_TRUE;
#endif
				removeArg(&argc, &argv[i]);
			}
		} else if (!PL_strcasecmp(argv[i], "-webserverconfig")) {
			removeArg(&argc, &argv[i]);
			if (i < argc) {
				gWebServerConfig = strdup(argv[i]);
				removeArg(&argc, &argv[i]);
			}
		} else {
			++i;
		}
	}

#ifdef _WIN32
    HINSTANCE hMySQL = NULL;
    BOOL bSuccess = FALSE;
    if (bLoadMySQL)
    {
        //printf("Enabled LoadMySQL\n");
        hMySQL = LoadLibrary("libmysql.dll");
        if (hMySQL)
        {
            bSuccess = DisableThreadLibraryCalls(hMySQL);
            if (!bSuccess)
                fprintf(stderr, "Failed to disable thread library call: err=%d\n", GetLastError());
        }else
        {
            fprintf(stderr, "Failed to load libmysql.dll: err=%d\n", GetLastError());
        }
    }
#endif

#ifdef _BUILD_STATIC_BIN
    // Combine the static components and the app components.
    PRUint32 combinedCount = kStaticModuleCount + kModulesCount;
    static nsStaticModuleInfo *sCombined = new nsStaticModuleInfo[combinedCount];
    NS_ENSURE_TRUE(sCombined, NS_ERROR_OUT_OF_MEMORY);

    memcpy(sCombined, kPStaticModules,
           sizeof(nsStaticModuleInfo) * kStaticModuleCount);
    memcpy(sCombined + kStaticModuleCount, &kStaticModules,
           sizeof(nsStaticModuleInfo) * kModulesCount);

    // Spin up the XPCOM infrastructure.
    NS_InitXPCOM3(&serviceManager, nsnull, dirProvider, sCombined, combinedCount);
#else
    // Spin up the XPCOM infrastructure.
    NS_InitXPCOM3(&serviceManager, nsnull, dirProvider, &kStaticModules, kModulesCount); 
#endif
	
    // Exit immediately if we're only interested in XPCOM auto-registration.
    int exitCode = 1;
    if (argc > 1 && !strcmp(argv[1], "-reg")) {
        exitCode = 0;
        gJaxerLog.Log(eDEBUG, "Jaxer -reg call.");
    } else {
        // Give FCGX a chance to adopt stdin/stdout.
	do {
		/* Initialize appData */
		nsCOMPtr<nsIFile> processDir;
		rv = NS_GetSpecialDirectory(NS_XPCOM_CURRENT_PROCESS_DIR, getter_AddRefs(processDir));
		if (NS_FAILED(rv)) {
			fprintf(stderr, "Get current process directory failed: rv=0x%x", rv);
			break;
		}

		nsCOMPtr<nsILocalFile> xreDirectory(do_QueryInterface(processDir));
		appData.xreDirectory = xreDirectory;
		NS_IF_ADDREF(appData.xreDirectory);

#ifdef MOZ_CRASHREPORTER
		rv = CrashReporter::SetExceptionHandler(appData.xreDirectory, appData.crashReporterURL);
		if (NS_SUCCEEDED(rv)) {
			// pass some basic info from the app data
			if (appData.vendor)
				CrashReporter::AnnotateCrashReport(NS_LITERAL_CSTRING("Vendor"),
											 nsDependentCString(appData.vendor));
			if (appData.name)
				CrashReporter::AnnotateCrashReport(NS_LITERAL_CSTRING("ProductName"),
											 nsDependentCString(appData.name));
			if (appData.version)
				CrashReporter::AnnotateCrashReport(NS_LITERAL_CSTRING("Version"),
											 nsDependentCString(appData.version));
			if (appData.buildID)
				CrashReporter::AnnotateCrashReport(NS_LITERAL_CSTRING("BuildID"),
											 nsDependentCString(appData.buildID));
		}

		nsCOMPtr<nsIFile> dumpD;
		rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(dumpD));
		if (NS_FAILED(rv)) {
			fprintf(stderr, "Jaxer: Cannot get OS tmp dir. rv=0x%x", rv);
			break;
		}

		nsAutoString pathStr;
		rv = dumpD->GetPath(pathStr);
		if(NS_SUCCEEDED(rv)) {
		  CrashReporter::SetMinidumpPath(pathStr);
		}
#endif

		// call Jaxer main
		exitCode = jaxerMain(argc, argv);
	} while(PR_FALSE);
    }


#ifdef MOZ_CRASHREPORTER
	CrashReporter::UnsetExceptionHandler();
#endif
	NS_IF_RELEASE(appData.xreDirectory);

	appDirectoryProvider.DoShutdown();
	dirProvider = nsnull;

	NS_ShutdownXPCOM(serviceManager);

#ifdef _BUILD_STATIC_BIN
    delete[] sCombined;
#endif

#ifdef _WIN32
    if (bLoadMySQL && hMySQL)
        FreeLibrary(hMySQL);
#endif

    return exitCode;
}
Beispiel #6
0
void 
ArgumentParser::checkArgs(int &argc, char *argv[], bool caxoe) {
   // complain_and_exit_on_error defaults to true...

   int i,j;
  
  // This loop cycles through the arguments.
   for (i = 1; i < argc ; i++) {
      // This loop compares the arguments passed in with those we're
      // checking them against
      bool matched_one = false;
      for (j = 0; j < numArgs; j++) {
         // the first check is necessary because argc can change during
         // execution...
         switch (argRecordArray[j].type) {
         case BOOLEAN:{
	
	   if (i < argc && argRecordArray[j].argText == argv[i] ){
	     // They matched - let's read in the data
	     matched_one = true;
	     // Argc is passed by reference!
	     removeArg(i, argc, argv);
	     *(bool*)(argRecordArray[j].data) = true;
	   }
	   break;
         }
         case INTEGER:{
	   if( i < argc && argRecordArray[j].argText == argv[i] ){
               matched_one = true;
               removeArg( i, argc, argv );
               *(int*)(argRecordArray[j].data) = atoi(argv[i]);
               removeArg( i, argc, argv );
            }
            break;
         }
         case STRING:{
            // Argc is passed by reference!	      
            if ( i < argc && argRecordArray[j].argText == argv[i] ){
	      // They matched - let's read in the data
	      matched_one = true;
	      removeArg(i, argc, argv);
	      *(char**)(argRecordArray[j].data) = argv[i];
	      removeArg(i, argc, argv);
            }
            break;
         }
         case STRING_LIST:{
            char *substring = NULL;
            if (i < argc && argRecordArray[j].argText == argv[i] ){
               removeArg(i, argc, argv);
               substring = argv[i];
            }
            else{
               int character;
               bool one_did_not_match = false;
               for( character = 0; 
                    character < (int)argRecordArray[j].argText.length(); 
                    character++ ){
                  if( argRecordArray[j].argText[character] !=
                      argv[i][character] ){
                     one_did_not_match = true;
                  }
               }	  
               if( one_did_not_match == false ){
		 substring = argv[i] +
		   argRecordArray[j].argText.length()*sizeof(char);
               }
            }
            if( substring != NULL ){
               matched_one = true;
               removeArg(i, argc, argv);
            }
            break;
         }
         default:
            cerr << "Invalid arg type in arg array!\n";
            exit(-1);
         }
      
         if( matched_one == true ){
            // we need to reprocess all of the args, to be safe...
            j = 0;
            i = 0;
            matched_one = false;
         }
      } // for j
   } // for i
  
   checkRemaining( argc, argv, caxoe );
}