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