void CommandBuilder::BuildCommand(const wxString &cmdName, const wxString &cmdParamsArg) { // Stage 1: create a Command object of the right type auto scriptOutput = ScriptCommandRelay::GetResponseTarget(); auto output = std::make_unique<CommandOutputTargets>(std::make_unique<NullProgressTarget>(), scriptOutput, scriptOutput); #ifdef OLD_BATCH_SYSTEM OldStyleCommandType *factory = CommandDirectory::Get()->LookUp(cmdName); if (factory == NULL) { // Fall back to hoping the Batch Command system can handle it #endif OldStyleCommandType *type = CommandDirectory::Get()->LookUp(wxT("BatchCommand")); wxASSERT(type != NULL); mCommand = type->Create(nullptr); mCommand->SetParameter(wxT("CommandName"), cmdName); mCommand->SetParameter(wxT("ParamString"), cmdParamsArg); auto aCommand = std::make_shared<ApplyAndSendResponse>(mCommand, output); Success(aCommand); return; #ifdef OLD_BATCH_SYSTEM } CommandSignature &signature = factory->GetSignature(); mCommand = factory->Create(nullptr); //mCommand->SetOutput( std::move(output) ); // Stage 2: set the parameters ShuttleCli shuttle; shuttle.mParams = cmdParamsArg; shuttle.mbStoreInClient = true; ParamValueMap::const_iterator iter; ParamValueMap params = signature.GetDefaults(); // Iterate through the parameters defined by the command for (iter = params.begin(); iter != params.end(); ++iter) { wxString paramString; // IF there is a match in the args actually used if (shuttle.TransferString(iter->first, paramString, wxT(""))) { // Then set that parameter. if (!mCommand->SetParameter(iter->first, paramString)) { Failure(); return; } } } // Check for unrecognised parameters wxString cmdParams(cmdParamsArg); while (!cmdParams.empty()) { cmdParams.Trim(true); cmdParams.Trim(false); int splitAt = cmdParams.Find(wxT('=')); if (splitAt < 0 && !cmdParams.empty()) { Failure(wxT("Parameter string is missing '='")); return; } wxString paramName = cmdParams.Left(splitAt); if (params.find(paramName) == params.end()) { Failure(wxT("Unrecognized parameter: '") + paramName + wxT("'")); return; } // Handling of quoted strings is quite limitted. // You start and end with a " or a '. // There is no escaping in the string. cmdParams = cmdParams.Mid(splitAt+1); if( cmdParams.empty() ) splitAt =-1; else if( cmdParams[0] == '\"' ){ cmdParams = cmdParams.Mid(1); splitAt = cmdParams.Find(wxT('\"'))+1; } else if( cmdParams[0] == '\'' ){ cmdParams = cmdParams.Mid(1); splitAt = cmdParams.Find(wxT('\''))+1; } else splitAt = cmdParams.Find(wxT(' '))+1; if (splitAt < 1) { splitAt = cmdParams.length(); } cmdParams = cmdParams.Mid(splitAt); } auto aCommand = std::make_shared<ApplyAndSendResponse>(mCommand, output); Success(aCommand); #endif }
void CommandBuilder::BuildCommand(const wxString &cmdName, wxString cmdParams) { // Stage 1: create a Command object of the right type CommandMessageTarget *scriptOutput = ScriptCommandRelay::GetResponseTarget(); CommandOutputTarget *output = new CommandOutputTarget(new NullProgressTarget(), scriptOutput, scriptOutput); CommandType *factory = CommandDirectory::Get()->LookUp(cmdName); if (factory == NULL) { // Fall back to hoping the Batch Command system can handle it CommandType *type = CommandDirectory::Get()->LookUp(wxT("BatchCommand")); wxASSERT(type != NULL); mCommand = type->Create(output); mCommand->SetParameter(wxT("CommandName"), cmdName); mCommand->SetParameter(wxT("ParamString"), cmdParams); Success(new ApplyAndSendResponse(mCommand)); return; } CommandSignature &signature = factory->GetSignature(); mCommand = factory->Create(output); // Stage 2: set the parameters ShuttleCli shuttle; shuttle.mParams = cmdParams; shuttle.mbStoreInClient = true; ParamValueMap::const_iterator iter; ParamValueMap params = signature.GetDefaults(); for (iter = params.begin(); iter != params.end(); ++iter) { wxString paramString; if (shuttle.TransferString(iter->first, paramString, wxT(""))) { if (!mCommand->SetParameter(iter->first, paramString)) { Failure(); return; } } } // Check for unrecognised parameters while (cmdParams != wxEmptyString) { cmdParams.Trim(true); cmdParams.Trim(false); int splitAt = cmdParams.Find(wxT('=')); if (splitAt < 0 && cmdParams != wxEmptyString) { Failure(wxT("Parameter string is missing '='")); return; } wxString paramName = cmdParams.Left(splitAt); if (params.find(paramName) == params.end()) { Failure(wxT("Unrecognized parameter: '") + paramName + wxT("'")); return; } cmdParams = cmdParams.Mid(splitAt+1); splitAt = cmdParams.Find(wxT(' ')); if (splitAt < 0) { splitAt = cmdParams.Len(); } cmdParams = cmdParams.Mid(splitAt); } Success(new ApplyAndSendResponse(mCommand)); }