[947] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
[1056] | 3 | * > www.orxonox.net < |
---|
[947] | 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "CommandExecutor.h" |
---|
| 30 | #include "ConsoleCommand.h" |
---|
| 31 | #include "util/String.h" |
---|
[1030] | 32 | #include "util/Convert.h" |
---|
[947] | 33 | #include "Identifier.h" |
---|
| 34 | #include "Language.h" |
---|
| 35 | #include "Debug.h" |
---|
[1214] | 36 | #include "TclBind.h" |
---|
[947] | 37 | |
---|
| 38 | namespace orxonox |
---|
| 39 | { |
---|
[949] | 40 | CommandExecutor& CommandExecutor::getInstance() |
---|
| 41 | { |
---|
| 42 | static CommandExecutor instance; |
---|
| 43 | return instance; |
---|
| 44 | } |
---|
[947] | 45 | |
---|
[952] | 46 | CommandEvaluation& CommandExecutor::getEvaluation() |
---|
| 47 | { |
---|
| 48 | return CommandExecutor::getInstance().evaluation_; |
---|
| 49 | } |
---|
| 50 | |
---|
[1214] | 51 | const CommandEvaluation& CommandExecutor::getLastEvaluation() |
---|
| 52 | { |
---|
| 53 | return CommandExecutor::getInstance().evaluation_; |
---|
| 54 | } |
---|
| 55 | |
---|
[1502] | 56 | ConsoleCommand& CommandExecutor::addConsoleCommandShortcut(ConsoleCommand* command) |
---|
[947] | 57 | { |
---|
[1502] | 58 | std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getInstance().consoleCommandShortcuts_.find(command->getName()); |
---|
| 59 | if (it != CommandExecutor::getInstance().consoleCommandShortcuts_.end()) |
---|
| 60 | { |
---|
| 61 | COUT(2) << "Warning: Overwriting console-command shortcut with name " << command->getName() << "." << std::endl; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | CommandExecutor::getInstance().consoleCommandShortcuts_[command->getName()] = command; |
---|
| 66 | CommandExecutor::getInstance().consoleCommandShortcuts_LC_[getLowercase(command->getName())] = command; |
---|
| 67 | return (*command); |
---|
[947] | 68 | } |
---|
| 69 | |
---|
| 70 | /** |
---|
| 71 | @brief Returns the executor of a console command shortcut with given name. |
---|
| 72 | @brief name The name of the requested console command shortcut |
---|
| 73 | @return The executor of the requested console command shortcut |
---|
| 74 | */ |
---|
[1502] | 75 | ConsoleCommand* CommandExecutor::getConsoleCommandShortcut(const std::string& name) |
---|
[947] | 76 | { |
---|
[1502] | 77 | std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getInstance().consoleCommandShortcuts_.find(name); |
---|
[951] | 78 | if (it != CommandExecutor::getInstance().consoleCommandShortcuts_.end()) |
---|
[947] | 79 | return (*it).second; |
---|
| 80 | else |
---|
| 81 | return 0; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | /** |
---|
| 85 | @brief Returns the executor of a console command shortcut with given name in lowercase. |
---|
| 86 | @brief name The name of the requested console command shortcut in lowercase |
---|
| 87 | @return The executor of the requested console command shortcut |
---|
| 88 | */ |
---|
[1502] | 89 | ConsoleCommand* CommandExecutor::getLowercaseConsoleCommandShortcut(const std::string& name) |
---|
[947] | 90 | { |
---|
[1502] | 91 | std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getInstance().consoleCommandShortcuts_LC_.find(name); |
---|
[951] | 92 | if (it != CommandExecutor::getInstance().consoleCommandShortcuts_LC_.end()) |
---|
[947] | 93 | return (*it).second; |
---|
| 94 | else |
---|
| 95 | return 0; |
---|
| 96 | } |
---|
| 97 | |
---|
[1214] | 98 | bool CommandExecutor::execute(const std::string& command, bool useTcl) |
---|
[947] | 99 | { |
---|
[1214] | 100 | if (useTcl) |
---|
| 101 | return TclBind::eval(command); |
---|
[994] | 102 | |
---|
[1502] | 103 | CommandExecutor::parseIfNeeded(command); |
---|
| 104 | return CommandExecutor::getEvaluation().execute(); |
---|
[952] | 105 | } |
---|
[948] | 106 | |
---|
[947] | 107 | std::string CommandExecutor::complete(const std::string& command) |
---|
| 108 | { |
---|
[1502] | 109 | CommandExecutor::parseIfNeeded(command); |
---|
| 110 | return CommandExecutor::getEvaluation().complete(); |
---|
[952] | 111 | } |
---|
[948] | 112 | |
---|
[1502] | 113 | std::string CommandExecutor::hint(const std::string& command) |
---|
[952] | 114 | { |
---|
[1502] | 115 | CommandExecutor::parseIfNeeded(command); |
---|
| 116 | return CommandExecutor::getEvaluation().hint(); |
---|
[947] | 117 | } |
---|
| 118 | |
---|
[1502] | 119 | CommandEvaluation CommandExecutor::evaluate(const std::string& command) |
---|
[947] | 120 | { |
---|
[1502] | 121 | CommandExecutor::parse(command); |
---|
| 122 | CommandExecutor::getEvaluation().evaluateParams(); |
---|
| 123 | return CommandExecutor::getEvaluation(); |
---|
[952] | 124 | } |
---|
[948] | 125 | |
---|
[1502] | 126 | void CommandExecutor::parseIfNeeded(const std::string& command) |
---|
[952] | 127 | { |
---|
[1502] | 128 | if (CommandExecutor::getEvaluation().state_ == CS_Uninitialized) |
---|
[947] | 129 | { |
---|
[1502] | 130 | CommandExecutor::parse(command); |
---|
[947] | 131 | } |
---|
[1502] | 132 | else if (CommandExecutor::getEvaluation().originalCommand_ != command) |
---|
[1214] | 133 | { |
---|
[1502] | 134 | if (CommandExecutor::getEvaluation().command_ == command) |
---|
| 135 | { |
---|
| 136 | CommandExecutor::parse(command); |
---|
| 137 | CommandExecutor::getEvaluation().bNewCommand_ = false; |
---|
| 138 | } |
---|
| 139 | else |
---|
| 140 | { |
---|
| 141 | CommandExecutor::parse(command); |
---|
| 142 | } |
---|
[1214] | 143 | } |
---|
[952] | 144 | } |
---|
| 145 | |
---|
[947] | 146 | void CommandExecutor::parse(const std::string& command, bool bInitialize) |
---|
| 147 | { |
---|
| 148 | if (bInitialize) |
---|
[1502] | 149 | CommandExecutor::getEvaluation().initialize(command); |
---|
[947] | 150 | |
---|
[1502] | 151 | CommandExecutor::getEvaluation().commandTokens_.split(command, " ", SubString::WhiteSpaces, false, '\\', false, '"', false, '(', ')', false, '\0'); |
---|
| 152 | CommandExecutor::getEvaluation().command_ = command; |
---|
| 153 | |
---|
[952] | 154 | switch (CommandExecutor::getEvaluation().state_) |
---|
[947] | 155 | { |
---|
[955] | 156 | case CS_Uninitialized: |
---|
[1502] | 157 | { |
---|
[955] | 158 | // Impossible |
---|
| 159 | break; |
---|
[1502] | 160 | } |
---|
[947] | 161 | case CS_Empty: |
---|
[1502] | 162 | { |
---|
[947] | 163 | if (CommandExecutor::argumentsGiven() == 0) |
---|
| 164 | { |
---|
[1502] | 165 | CommandExecutor::createListOfPossibleFunctions(""); |
---|
| 166 | CommandExecutor::createListOfPossibleIdentifiers(""); |
---|
| 167 | break; |
---|
[947] | 168 | } |
---|
| 169 | else |
---|
| 170 | { |
---|
[1502] | 171 | CommandExecutor::getEvaluation().state_ = CS_ShortcutOrIdentifier; |
---|
| 172 | // Move on to next case |
---|
[947] | 173 | } |
---|
[1502] | 174 | } |
---|
| 175 | case CS_ShortcutOrIdentifier: |
---|
| 176 | { |
---|
| 177 | if (CommandExecutor::argumentsGiven() > 1) |
---|
[947] | 178 | { |
---|
[1502] | 179 | // There's a finished first argument - check if it's a shortcut or a classname |
---|
| 180 | CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(CommandExecutor::getArgument(0)); |
---|
| 181 | CommandExecutor::getEvaluation().functionclass_ = CommandExecutor::getPossibleIdentifier(CommandExecutor::getArgument(0)); |
---|
[947] | 182 | |
---|
[1502] | 183 | if (CommandExecutor::getEvaluation().function_) |
---|
[947] | 184 | { |
---|
[1502] | 185 | // It's a shortcut |
---|
| 186 | CommandExecutor::getEvaluation().state_ = CS_ParamPreparation; |
---|
| 187 | CommandExecutor::getEvaluation().functionclass_ = 0; |
---|
| 188 | // Move on to next case |
---|
[947] | 189 | } |
---|
[1502] | 190 | else if (CommandExecutor::getEvaluation().functionclass_) |
---|
[947] | 191 | { |
---|
[1502] | 192 | // It's a functionname |
---|
| 193 | CommandExecutor::getEvaluation().state_ = CS_Function; |
---|
| 194 | CommandExecutor::getEvaluation().function_ = 0; |
---|
| 195 | // Move on to next case |
---|
[947] | 196 | } |
---|
| 197 | else |
---|
| 198 | { |
---|
[1502] | 199 | // The first argument is bad |
---|
| 200 | CommandExecutor::getEvaluation().state_ = CS_Error; |
---|
| 201 | AddLanguageEntry("commandexecutorunknownfirstargument", "is not a shortcut nor a classname"); |
---|
| 202 | CommandExecutor::getEvaluation().errorMessage_ = "Error: " + CommandExecutor::getArgument(0) + " " + GetLocalisation("commandexecutorunknownfirstargument") + "."; |
---|
[947] | 203 | return; |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | else |
---|
| 207 | { |
---|
[1502] | 208 | // There's no finished first argument - search possible shortcuts or classnames |
---|
| 209 | CommandExecutor::createListOfPossibleFunctions(CommandExecutor::getArgument(0)); |
---|
| 210 | CommandExecutor::createListOfPossibleIdentifiers(CommandExecutor::getArgument(0)); |
---|
| 211 | |
---|
| 212 | unsigned int num_functions = CommandExecutor::getEvaluation().listOfPossibleFunctions_.size(); |
---|
| 213 | unsigned int num_identifiers = CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.size(); |
---|
| 214 | |
---|
| 215 | if (num_functions == 1 && num_identifiers == 0) |
---|
[947] | 216 | { |
---|
[1502] | 217 | // It's a shortcut |
---|
| 218 | std::string functionname = *(*CommandExecutor::getEvaluation().listOfPossibleFunctions_.begin()).first; |
---|
| 219 | CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(functionname); |
---|
| 220 | if (getLowercase(functionname) != getLowercase(CommandExecutor::getArgument(0))) |
---|
[947] | 221 | { |
---|
[1502] | 222 | // Unfinished shortcut |
---|
| 223 | CommandExecutor::getEvaluation().bCommandChanged_ = true; |
---|
[947] | 224 | } |
---|
[1502] | 225 | CommandExecutor::getEvaluation().state_ = CS_ParamPreparation; |
---|
| 226 | CommandExecutor::getEvaluation().functionclass_ = 0; |
---|
| 227 | CommandExecutor::getEvaluation().command_ = CommandExecutor::getEvaluation().function_->getName(); |
---|
| 228 | if (CommandExecutor::getEvaluation().function_->getParamCount() > 0) |
---|
[947] | 229 | { |
---|
[1502] | 230 | CommandExecutor::getEvaluation().command_ += " "; |
---|
| 231 | CommandExecutor::getEvaluation().bCommandChanged_ = true; |
---|
[947] | 232 | } |
---|
[1502] | 233 | // Move on to next case |
---|
[947] | 234 | } |
---|
[1502] | 235 | else if (num_identifiers == 1 && num_functions == 0) |
---|
[947] | 236 | { |
---|
[1502] | 237 | // It's a classname |
---|
| 238 | std::string classname = *(*CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.begin()).first; |
---|
| 239 | CommandExecutor::getEvaluation().functionclass_ = CommandExecutor::getPossibleIdentifier(classname); |
---|
| 240 | if (getLowercase(classname) != getLowercase(CommandExecutor::getArgument(0))) |
---|
[947] | 241 | { |
---|
[1502] | 242 | // Unfinished classname |
---|
| 243 | CommandExecutor::getEvaluation().bCommandChanged_ = true; |
---|
[947] | 244 | } |
---|
[1502] | 245 | CommandExecutor::getEvaluation().state_ = CS_Function; |
---|
| 246 | CommandExecutor::getEvaluation().function_ = 0; |
---|
| 247 | CommandExecutor::getEvaluation().command_ = CommandExecutor::getEvaluation().functionclass_->getName() + " "; |
---|
| 248 | // Move on to next case |
---|
| 249 | } |
---|
| 250 | else if (num_identifiers == 0 && num_functions == 0) |
---|
| 251 | { |
---|
| 252 | // No possibilities |
---|
| 253 | CommandExecutor::getEvaluation().state_ = CS_Error; |
---|
| 254 | AddLanguageEntry("commandexecutorunknownfirstargumentstart", "There is no command or classname starting with"); |
---|
| 255 | CommandExecutor::getEvaluation().errorMessage_ = "Error: " + GetLocalisation("commandexecutorunknownfirstargumentstart") + " " + CommandExecutor::getArgument(0) + "."; |
---|
[947] | 256 | return; |
---|
| 257 | } |
---|
[1502] | 258 | else |
---|
[947] | 259 | { |
---|
[1502] | 260 | // There are several possiblilities |
---|
| 261 | std::list<std::pair<const std::string*, const std::string*> > temp; |
---|
| 262 | temp.insert(temp.end(), CommandExecutor::getEvaluation().listOfPossibleFunctions_.begin(), CommandExecutor::getEvaluation().listOfPossibleFunctions_.end()); |
---|
| 263 | temp.insert(temp.end(), CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.begin(), CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.end()); |
---|
| 264 | CommandExecutor::getEvaluation().command_ = CommandExecutor::getCommonBegin(temp); |
---|
| 265 | CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(CommandExecutor::getArgument(0)); |
---|
| 266 | CommandExecutor::getEvaluation().functionclass_ = CommandExecutor::getPossibleIdentifier(CommandExecutor::getArgument(0)); |
---|
| 267 | CommandExecutor::getEvaluation().bCommandChanged_ = true; |
---|
[947] | 268 | return; |
---|
| 269 | } |
---|
| 270 | } |
---|
[1502] | 271 | } |
---|
| 272 | case CS_Function: |
---|
| 273 | { |
---|
| 274 | if (CommandExecutor::getEvaluation().functionclass_) |
---|
[947] | 275 | { |
---|
[1502] | 276 | // There is a classname - search for the commandname |
---|
| 277 | if (CommandExecutor::argumentsGiven() > 2) |
---|
[947] | 278 | { |
---|
[1502] | 279 | // There is a finished second argument - check if it's a commandname |
---|
| 280 | CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(CommandExecutor::getArgument(1), CommandExecutor::getEvaluation().functionclass_); |
---|
| 281 | |
---|
| 282 | if (CommandExecutor::getEvaluation().function_) |
---|
[947] | 283 | { |
---|
[1502] | 284 | // It's a function |
---|
| 285 | CommandExecutor::getEvaluation().state_ = CS_ParamPreparation; |
---|
| 286 | // Move on to next case |
---|
[947] | 287 | } |
---|
| 288 | else |
---|
| 289 | { |
---|
[1502] | 290 | // The second argument is bad |
---|
| 291 | CommandExecutor::getEvaluation().state_ = CS_Error; |
---|
| 292 | AddLanguageEntry("commandexecutorunknownsecondargument", "is not a function of"); |
---|
| 293 | CommandExecutor::getEvaluation().errorMessage_ = "Error: " + CommandExecutor::getArgument(1) + " " + GetLocalisation("commandexecutorunknownsecondargument") + " " + CommandExecutor::getEvaluation().functionclass_->getName() + "."; |
---|
[947] | 294 | return; |
---|
| 295 | } |
---|
| 296 | } |
---|
| 297 | else |
---|
| 298 | { |
---|
[1502] | 299 | // There is no finished second argument - search for possibilities |
---|
| 300 | CommandExecutor::createListOfPossibleFunctions(CommandExecutor::getArgument(1), CommandExecutor::getEvaluation().functionclass_); |
---|
| 301 | unsigned int num_functions = CommandExecutor::getEvaluation().listOfPossibleFunctions_.size(); |
---|
| 302 | |
---|
| 303 | if (num_functions == 1) |
---|
[947] | 304 | { |
---|
[1502] | 305 | // It's a function |
---|
| 306 | std::string functionname = *(*CommandExecutor::getEvaluation().listOfPossibleFunctions_.begin()).first; |
---|
| 307 | CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(functionname, CommandExecutor::getEvaluation().functionclass_); |
---|
| 308 | if (getLowercase(functionname) != getLowercase(CommandExecutor::getArgument(1))) |
---|
[947] | 309 | { |
---|
[1502] | 310 | // Unfinished function |
---|
| 311 | CommandExecutor::getEvaluation().bCommandChanged_ = true; |
---|
[947] | 312 | } |
---|
[1502] | 313 | CommandExecutor::getEvaluation().state_ = CS_ParamPreparation; |
---|
| 314 | CommandExecutor::getEvaluation().command_ = CommandExecutor::getEvaluation().functionclass_->getName() + " " + CommandExecutor::getEvaluation().function_->getName(); |
---|
| 315 | if (CommandExecutor::getEvaluation().function_->getParamCount() > 0) |
---|
| 316 | { |
---|
| 317 | CommandExecutor::getEvaluation().command_ += " "; |
---|
| 318 | CommandExecutor::getEvaluation().bCommandChanged_ = true; |
---|
| 319 | } |
---|
| 320 | // Move on to next case |
---|
[947] | 321 | } |
---|
[1502] | 322 | else if (num_functions == 0) |
---|
[965] | 323 | { |
---|
[1502] | 324 | // No possibilities |
---|
[952] | 325 | CommandExecutor::getEvaluation().state_ = CS_Error; |
---|
[1502] | 326 | AddLanguageEntry("commandexecutorunknownsecondargumentstart", "has no function starting with"); |
---|
| 327 | CommandExecutor::getEvaluation().errorMessage_ = "Error: " + CommandExecutor::getEvaluation().functionclass_->getName() + " " + GetLocalisation("commandexecutorunknownsecondargumentstart") + " " + CommandExecutor::getArgument(1) + "."; |
---|
[947] | 328 | return; |
---|
| 329 | } |
---|
| 330 | else |
---|
| 331 | { |
---|
[1502] | 332 | // There are several possibilities |
---|
| 333 | CommandExecutor::getEvaluation().command_ = CommandExecutor::getEvaluation().functionclass_->getName() + " " + CommandExecutor::getCommonBegin(CommandExecutor::getEvaluation().listOfPossibleFunctions_); |
---|
| 334 | CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(CommandExecutor::getArgument(1), CommandExecutor::getEvaluation().functionclass_); |
---|
| 335 | CommandExecutor::getEvaluation().bCommandChanged_ = true; |
---|
[947] | 336 | return; |
---|
| 337 | } |
---|
| 338 | } |
---|
| 339 | } |
---|
| 340 | else |
---|
| 341 | { |
---|
[1502] | 342 | // There is no classname - move on to CS_ParamPreparation |
---|
| 343 | } |
---|
| 344 | } |
---|
| 345 | case CS_ParamPreparation: |
---|
| 346 | { |
---|
| 347 | if (CommandExecutor::getEvaluation().function_->getParamCount() == 0 || CommandExecutor::enoughArgumentsGiven(CommandExecutor::getEvaluation().function_)) |
---|
| 348 | { |
---|
| 349 | CommandExecutor::getEvaluation().state_ = CS_Finished; |
---|
[947] | 350 | return; |
---|
| 351 | } |
---|
[1502] | 352 | else |
---|
[947] | 353 | { |
---|
[1502] | 354 | unsigned int argumentNumber = CommandExecutor::argumentsGiven() - 2; |
---|
| 355 | if (CommandExecutor::getEvaluation().functionclass_) |
---|
| 356 | argumentNumber -= 1; |
---|
| 357 | |
---|
| 358 | CommandExecutor::createListOfPossibleArguments(CommandExecutor::getLastArgument(), CommandExecutor::getEvaluation().function_, argumentNumber); |
---|
| 359 | CommandExecutor::getEvaluation().state_ = CS_Params; |
---|
| 360 | |
---|
| 361 | if (CommandExecutor::getEvaluation().bCommandChanged_) |
---|
[947] | 362 | { |
---|
[1502] | 363 | // Don't do more than one change |
---|
[947] | 364 | return; |
---|
| 365 | } |
---|
| 366 | } |
---|
[1502] | 367 | } |
---|
| 368 | case CS_Params: |
---|
| 369 | { |
---|
| 370 | if (CommandExecutor::getEvaluation().listOfPossibleArguments_.size() == 1) |
---|
[947] | 371 | { |
---|
[1502] | 372 | // There is exactly one possible argument |
---|
| 373 | CommandExecutor::getEvaluation().argument_ = (*CommandExecutor::getEvaluation().listOfPossibleArguments_.begin()).getString(); |
---|
| 374 | CommandExecutor::getEvaluation().possibleArgument_ = (*CommandExecutor::getEvaluation().listOfPossibleArguments_.begin()).getString(); |
---|
| 375 | CommandExecutor::getEvaluation().state_ = CS_ParamPreparation; |
---|
[947] | 376 | return; |
---|
| 377 | } |
---|
[1502] | 378 | else if (CommandExecutor::getEvaluation().listOfPossibleArguments_.size() == 0) |
---|
[947] | 379 | { |
---|
[1502] | 380 | // The user tries something new - we let him do |
---|
| 381 | CommandExecutor::getEvaluation().state_ = CS_ParamPreparation; |
---|
| 382 | CommandExecutor::getEvaluation().argument_ = CommandExecutor::getLastArgument(); |
---|
| 383 | return; |
---|
[947] | 384 | } |
---|
| 385 | else |
---|
| 386 | { |
---|
[1502] | 387 | // There are several possibilities |
---|
| 388 | unsigned int argumentNumber = CommandExecutor::argumentsGiven(); |
---|
| 389 | if (argumentNumber > 0) |
---|
| 390 | --argumentNumber; |
---|
| 391 | if (CommandExecutor::getEvaluation().functionclass_ && argumentNumber > 0) |
---|
| 392 | --argumentNumber; |
---|
[947] | 393 | |
---|
[1502] | 394 | CommandExecutor::getEvaluation().argument_ = CommandExecutor::getCommonBegin(CommandExecutor::getEvaluation().listOfPossibleArguments_); |
---|
| 395 | CommandExecutor::getEvaluation().possibleArgument_ = CommandExecutor::getPossibleArgument(CommandExecutor::getLastArgument(), CommandExecutor::getEvaluation().function_, argumentNumber); |
---|
| 396 | CommandExecutor::getEvaluation().state_ = CS_ParamPreparation; |
---|
[947] | 397 | return; |
---|
| 398 | } |
---|
[1502] | 399 | } |
---|
| 400 | case CS_Finished: |
---|
| 401 | { |
---|
| 402 | // Nothing more to do |
---|
[947] | 403 | break; |
---|
[1502] | 404 | } |
---|
[947] | 405 | case CS_Error: |
---|
[1502] | 406 | { |
---|
| 407 | // Bad, very bad |
---|
[947] | 408 | break; |
---|
[1502] | 409 | } |
---|
[947] | 410 | } |
---|
| 411 | } |
---|
| 412 | |
---|
[1502] | 413 | unsigned int CommandExecutor::argumentsFinished() |
---|
[947] | 414 | { |
---|
[1502] | 415 | unsigned int argumentsGiven = CommandExecutor::argumentsGiven(); |
---|
| 416 | if (argumentsGiven > 0) |
---|
| 417 | return argumentsGiven - 1; |
---|
| 418 | else |
---|
| 419 | return 0; |
---|
[947] | 420 | } |
---|
| 421 | |
---|
[1502] | 422 | unsigned int CommandExecutor::argumentsGiven() |
---|
[947] | 423 | { |
---|
[1502] | 424 | if (CommandExecutor::getEvaluation().command_.size() > 0 && CommandExecutor::getEvaluation().command_[CommandExecutor::getEvaluation().command_.size() - 1] == ' ') |
---|
| 425 | return CommandExecutor::getEvaluation().commandTokens_.size() + 1; |
---|
| 426 | else |
---|
| 427 | return CommandExecutor::getEvaluation().commandTokens_.size(); |
---|
[947] | 428 | } |
---|
| 429 | |
---|
[1502] | 430 | bool CommandExecutor::enoughArgumentsGiven(ConsoleCommand* command) |
---|
[947] | 431 | { |
---|
[1502] | 432 | if (CommandExecutor::getEvaluation().functionclass_) |
---|
| 433 | return (CommandExecutor::argumentsGiven() > (2 + command->getParamCount())); |
---|
[947] | 434 | else |
---|
[1502] | 435 | return (CommandExecutor::argumentsGiven() > (1 + command->getParamCount())); |
---|
[947] | 436 | } |
---|
| 437 | |
---|
[1502] | 438 | std::string CommandExecutor::getArgument(unsigned int index) |
---|
[947] | 439 | { |
---|
[1502] | 440 | if (index < (CommandExecutor::getEvaluation().commandTokens_.size())) |
---|
| 441 | return CommandExecutor::getEvaluation().commandTokens_[index]; |
---|
[947] | 442 | else |
---|
| 443 | return ""; |
---|
| 444 | } |
---|
| 445 | |
---|
[1502] | 446 | std::string CommandExecutor::getLastArgument() |
---|
[947] | 447 | { |
---|
[1502] | 448 | return CommandExecutor::getArgument(CommandExecutor::argumentsGiven() - 1); |
---|
[947] | 449 | } |
---|
| 450 | |
---|
[1502] | 451 | void CommandExecutor::createListOfPossibleIdentifiers(const std::string& fragment) |
---|
[947] | 452 | { |
---|
[1502] | 453 | CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.clear(); |
---|
| 454 | std::string lowercase = getLowercase(fragment); |
---|
[947] | 455 | for (std::map<std::string, Identifier*>::const_iterator it = Identifier::getLowercaseIdentifierMapBegin(); it != Identifier::getLowercaseIdentifierMapEnd(); ++it) |
---|
| 456 | if ((*it).second->hasConsoleCommands()) |
---|
[1502] | 457 | if ((*it).first.find(lowercase) == 0 || fragment == "") |
---|
| 458 | CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.push_back(std::pair<const std::string*, const std::string*>(&(*it).first, &(*it).second->getName())); |
---|
[947] | 459 | } |
---|
| 460 | |
---|
[1502] | 461 | void CommandExecutor::createListOfPossibleFunctions(const std::string& fragment, Identifier* identifier) |
---|
[947] | 462 | { |
---|
[1502] | 463 | CommandExecutor::getEvaluation().listOfPossibleFunctions_.clear(); |
---|
| 464 | std::string lowercase = getLowercase(fragment); |
---|
| 465 | if (!identifier) |
---|
[947] | 466 | { |
---|
[1502] | 467 | for (std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getLowercaseConsoleCommandShortcutMapBegin(); it != CommandExecutor::getLowercaseConsoleCommandShortcutMapEnd(); ++it) |
---|
| 468 | if ((*it).first.find(lowercase) == 0 || fragment == "") |
---|
| 469 | CommandExecutor::getEvaluation().listOfPossibleFunctions_.push_back(std::pair<const std::string*, const std::string*>(&(*it).first, &(*it).second->getName())); |
---|
[947] | 470 | } |
---|
[1502] | 471 | else |
---|
[947] | 472 | { |
---|
[1502] | 473 | for (std::map<std::string, ConsoleCommand*>::const_iterator it = identifier->getLowercaseConsoleCommandMapBegin(); it != identifier->getLowercaseConsoleCommandMapEnd(); ++it) |
---|
| 474 | if ((*it).first.find(lowercase) == 0 || fragment == "") |
---|
| 475 | CommandExecutor::getEvaluation().listOfPossibleFunctions_.push_back(std::pair<const std::string*, const std::string*>(&(*it).first, &(*it).second->getName())); |
---|
[947] | 476 | } |
---|
| 477 | } |
---|
| 478 | |
---|
[1502] | 479 | void CommandExecutor::createListOfPossibleArguments(const std::string& fragment, ConsoleCommand* command, unsigned int param) |
---|
[947] | 480 | { |
---|
[1502] | 481 | CommandExecutor::createArgumentCompletionList(command, param); |
---|
| 482 | |
---|
| 483 | CommandExecutor::getEvaluation().listOfPossibleArguments_.clear(); |
---|
| 484 | std::string lowercase = getLowercase(fragment); |
---|
| 485 | for (ArgumentCompletionList::const_iterator it = command->getArgumentCompletionListBegin(); it != command->getArgumentCompletionListEnd(); ++it) |
---|
[947] | 486 | { |
---|
[1502] | 487 | if ((*it).lowercaseComparison()) |
---|
[947] | 488 | { |
---|
[1502] | 489 | if ((*it).getComparable().find(lowercase) == 0 || fragment == "") |
---|
| 490 | CommandExecutor::getEvaluation().listOfPossibleArguments_.push_back(*it); |
---|
[947] | 491 | } |
---|
[1502] | 492 | else |
---|
[947] | 493 | { |
---|
[1502] | 494 | if ((*it).getComparable().find(fragment) == 0 || fragment == "") |
---|
| 495 | CommandExecutor::getEvaluation().listOfPossibleArguments_.push_back(*it); |
---|
[947] | 496 | } |
---|
| 497 | } |
---|
| 498 | } |
---|
| 499 | |
---|
[1502] | 500 | Identifier* CommandExecutor::getPossibleIdentifier(const std::string& name) |
---|
[947] | 501 | { |
---|
[1502] | 502 | std::string lowercase = getLowercase(name); |
---|
| 503 | std::map<std::string, Identifier*>::const_iterator it = Identifier::getLowercaseIdentifierMap().find(lowercase); |
---|
[947] | 504 | if ((it != Identifier::getLowercaseIdentifierMapEnd()) && (*it).second->hasConsoleCommands()) |
---|
| 505 | return (*it).second; |
---|
| 506 | |
---|
| 507 | return 0; |
---|
| 508 | } |
---|
| 509 | |
---|
[1502] | 510 | ConsoleCommand* CommandExecutor::getPossibleCommand(const std::string& name, Identifier* identifier) |
---|
[947] | 511 | { |
---|
[1502] | 512 | std::string lowercase = getLowercase(name); |
---|
| 513 | if (!identifier) |
---|
| 514 | { |
---|
| 515 | std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getLowercaseConsoleCommandShortcutMap().find(lowercase); |
---|
| 516 | if (it != CommandExecutor::getLowercaseConsoleCommandShortcutMapEnd()) |
---|
| 517 | return (*it).second; |
---|
| 518 | } |
---|
| 519 | else |
---|
| 520 | { |
---|
| 521 | std::map<std::string, ConsoleCommand*>::const_iterator it = identifier->getLowercaseConsoleCommandMap().find(lowercase); |
---|
| 522 | if (it != identifier->getLowercaseConsoleCommandMapEnd()) |
---|
| 523 | return (*it).second; |
---|
| 524 | } |
---|
[947] | 525 | return 0; |
---|
| 526 | } |
---|
| 527 | |
---|
[1502] | 528 | std::string CommandExecutor::getPossibleArgument(const std::string& name, ConsoleCommand* command, unsigned int param) |
---|
[947] | 529 | { |
---|
[1502] | 530 | CommandExecutor::createArgumentCompletionList(command, param); |
---|
[947] | 531 | |
---|
[1502] | 532 | std::string lowercase = getLowercase(name); |
---|
| 533 | for (ArgumentCompletionList::const_iterator it = command->getArgumentCompletionListBegin(); it != command->getArgumentCompletionListEnd(); ++it) |
---|
[955] | 534 | { |
---|
[1502] | 535 | if ((*it).lowercaseComparison()) |
---|
| 536 | { |
---|
| 537 | if ((*it).getComparable() == lowercase) |
---|
| 538 | return (*it).getString(); |
---|
| 539 | } |
---|
| 540 | else |
---|
| 541 | { |
---|
| 542 | if ((*it).getComparable() == name) |
---|
| 543 | return (*it).getString(); |
---|
| 544 | } |
---|
[955] | 545 | } |
---|
[947] | 546 | |
---|
[1502] | 547 | return ""; |
---|
[947] | 548 | } |
---|
| 549 | |
---|
[1502] | 550 | void CommandExecutor::createArgumentCompletionList(ConsoleCommand* command, unsigned int param) |
---|
[947] | 551 | { |
---|
[1502] | 552 | std::string params[5]; |
---|
[947] | 553 | |
---|
[1502] | 554 | unsigned int index = 0; |
---|
| 555 | unsigned int lowestIndex = 1 + (CommandExecutor::getEvaluation().functionclass_ != 0); |
---|
[947] | 556 | |
---|
[1502] | 557 | for (unsigned int i = CommandExecutor::argumentsGiven() - 1; i >= lowestIndex; --i) |
---|
[947] | 558 | { |
---|
[1502] | 559 | params[index] = CommandExecutor::getArgument(i); |
---|
| 560 | ++index; |
---|
| 561 | if (index >= 5) |
---|
| 562 | break; |
---|
[947] | 563 | } |
---|
| 564 | |
---|
[1502] | 565 | command->createArgumentCompletionList(param, params[0], params[1], params[2], params[3], params[4]); |
---|
[947] | 566 | } |
---|
| 567 | |
---|
[972] | 568 | std::string CommandExecutor::getCommonBegin(const std::list<std::pair<const std::string*, const std::string*> >& list) |
---|
[948] | 569 | { |
---|
| 570 | if (list.size() == 0) |
---|
| 571 | { |
---|
| 572 | return ""; |
---|
| 573 | } |
---|
| 574 | else if (list.size() == 1) |
---|
| 575 | { |
---|
[972] | 576 | return ((*(*list.begin()).first) + " "); |
---|
[948] | 577 | } |
---|
| 578 | else |
---|
| 579 | { |
---|
| 580 | std::string output = ""; |
---|
| 581 | for (unsigned int i = 0; true; i++) |
---|
| 582 | { |
---|
| 583 | char temp = 0; |
---|
[972] | 584 | for (std::list<std::pair<const std::string*, const std::string*> >::const_iterator it = list.begin(); it != list.end(); ++it) |
---|
[948] | 585 | { |
---|
[972] | 586 | if ((*(*it).first).size() > i) |
---|
[948] | 587 | { |
---|
| 588 | if (it == list.begin()) |
---|
| 589 | { |
---|
[972] | 590 | temp = (*(*it).first)[i]; |
---|
[948] | 591 | } |
---|
| 592 | else |
---|
| 593 | { |
---|
[972] | 594 | if (temp != (*(*it).first)[i]) |
---|
[948] | 595 | return output; |
---|
| 596 | } |
---|
| 597 | } |
---|
| 598 | else |
---|
| 599 | { |
---|
| 600 | return output; |
---|
| 601 | } |
---|
| 602 | } |
---|
| 603 | output += temp; |
---|
| 604 | } |
---|
| 605 | return output; |
---|
| 606 | } |
---|
| 607 | } |
---|
[1502] | 608 | |
---|
| 609 | std::string CommandExecutor::getCommonBegin(const ArgumentCompletionList& list) |
---|
| 610 | { |
---|
| 611 | if (list.size() == 0) |
---|
| 612 | { |
---|
| 613 | return ""; |
---|
| 614 | } |
---|
| 615 | else if (list.size() == 1) |
---|
| 616 | { |
---|
| 617 | return ((*list.begin()).getComparable() + " "); |
---|
| 618 | } |
---|
| 619 | else |
---|
| 620 | { |
---|
| 621 | std::string output = ""; |
---|
| 622 | for (unsigned int i = 0; true; i++) |
---|
| 623 | { |
---|
| 624 | char temp = 0; |
---|
| 625 | for (ArgumentCompletionList::const_iterator it = list.begin(); it != list.end(); ++it) |
---|
| 626 | { |
---|
| 627 | std::string argument = (*it).getComparable(); |
---|
| 628 | if (argument.size() > i) |
---|
| 629 | { |
---|
| 630 | if (it == list.begin()) |
---|
| 631 | { |
---|
| 632 | temp = argument[i]; |
---|
| 633 | } |
---|
| 634 | else |
---|
| 635 | { |
---|
| 636 | if (temp != argument[i]) |
---|
| 637 | return output; |
---|
| 638 | } |
---|
| 639 | } |
---|
| 640 | else |
---|
| 641 | { |
---|
| 642 | return output; |
---|
| 643 | } |
---|
| 644 | } |
---|
| 645 | output += temp; |
---|
| 646 | } |
---|
| 647 | return output; |
---|
| 648 | } |
---|
| 649 | } |
---|
[947] | 650 | } |
---|