[4744] | 1 | /* |
---|
[1853] | 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
[1855] | 10 | |
---|
| 11 | ### File Specific: |
---|
[5068] | 12 | main-programmer: Benjamin Grauer |
---|
[1855] | 13 | co-programmer: ... |
---|
[1853] | 14 | */ |
---|
| 15 | |
---|
[7374] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SHELL |
---|
[1853] | 17 | |
---|
[5129] | 18 | #include "shell_command.h" |
---|
[5639] | 19 | #include "shell_command_class.h" |
---|
[1853] | 20 | |
---|
[6222] | 21 | #include "compiler.h" |
---|
[5129] | 22 | #include "debug.h" |
---|
[5113] | 23 | #include "class_list.h" |
---|
| 24 | |
---|
| 25 | #include "key_names.h" |
---|
[5075] | 26 | |
---|
[7374] | 27 | namespace OrxShell |
---|
[3365] | 28 | { |
---|
[7394] | 29 | SHELL_COMMAND_STATIC(debug, ShellCommand, ShellCommand::debug); |
---|
[5141] | 30 | |
---|
[7394] | 31 | |
---|
[7374] | 32 | /** |
---|
[7394] | 33 | * @brief constructs and registers a new Command |
---|
[7374] | 34 | * @param commandName the name of the Command |
---|
| 35 | * @param className the name of the class to apply this command to |
---|
| 36 | * @param paramCount the count of parameters this command takes |
---|
| 37 | */ |
---|
| 38 | ShellCommand::ShellCommand(const std::string& commandName, const std::string& className, const Executor& executor) |
---|
| 39 | { |
---|
| 40 | this->setClassID(CL_SHELL_COMMAND, "ShellCommand"); |
---|
| 41 | PRINTF(5)("create shellcommand %s %s\n", commandName, className); |
---|
| 42 | this->setName(commandName); |
---|
[7398] | 43 | |
---|
| 44 | // copy the executor: |
---|
[7374] | 45 | this->executor = executor.clone(); |
---|
| 46 | this->executor->setName(commandName); |
---|
[7398] | 47 | |
---|
[7407] | 48 | for (unsigned int i = 0; i < this->executor->getParamCount(); i++) |
---|
| 49 | this->completors.push_back(new CompletorDefault(&this->executor->getDefaultValue(i))); |
---|
[7388] | 50 | this->alias = NULL; |
---|
[4320] | 51 | |
---|
[7374] | 52 | // this->classID = classID; |
---|
[7408] | 53 | this->shellClass = ShellCommandClass::acquireCommandClass(className); |
---|
[7398] | 54 | assert (this->shellClass != NULL); |
---|
[7399] | 55 | this->shellClass->registerCommand(this); |
---|
[7374] | 56 | } |
---|
| 57 | |
---|
| 58 | /** |
---|
[7394] | 59 | * @brief deconstructs a ShellCommand |
---|
[7374] | 60 | */ |
---|
| 61 | ShellCommand::~ShellCommand() |
---|
[5196] | 62 | { |
---|
[7399] | 63 | this->shellClass->unregisterCommand(this); |
---|
[7389] | 64 | if (this->alias != NULL) |
---|
[7374] | 65 | delete this->alias; |
---|
[7407] | 66 | while (!this->completors.empty()) |
---|
| 67 | { |
---|
| 68 | delete this->completors.back(); |
---|
| 69 | this->completors.pop_back(); |
---|
| 70 | } |
---|
[7374] | 71 | delete this->executor; |
---|
[5196] | 72 | } |
---|
[1853] | 73 | |
---|
[7374] | 74 | /** |
---|
[7398] | 75 | * @brief registers a new ShellCommand |
---|
[7374] | 76 | */ |
---|
| 77 | ShellCommand* ShellCommand::registerCommand(const std::string& commandName, const std::string& className, const Executor& executor) |
---|
| 78 | { |
---|
[7403] | 79 | if (ShellCommand::exists(commandName, className)) |
---|
[7374] | 80 | return NULL; |
---|
| 81 | else |
---|
| 82 | return new ShellCommand(commandName, className, executor); |
---|
[5636] | 83 | |
---|
[7374] | 84 | } |
---|
[5636] | 85 | |
---|
[7374] | 86 | /** |
---|
[7398] | 87 | * @brief unregister an existing commandName |
---|
[7374] | 88 | * @param className the name of the Class the command belongs to. |
---|
| 89 | * @param commandName the name of the command itself |
---|
| 90 | */ |
---|
| 91 | void ShellCommand::unregisterCommand(const std::string& commandName, const std::string& className) |
---|
| 92 | { |
---|
[5171] | 93 | |
---|
[7408] | 94 | ShellCommandClass* cmdClass = ShellCommandClass::acquireCommandClass(className); |
---|
[7399] | 95 | if (cmdClass != NULL) |
---|
| 96 | { |
---|
| 97 | std::vector<ShellCommand*>::iterator cmd; |
---|
| 98 | for (cmd = cmdClass->commandList.begin(); cmd < cmdClass->commandList.end(); cmd++) |
---|
| 99 | if (commandName == (*cmd)->getName()) |
---|
| 100 | delete (*cmd); |
---|
| 101 | } |
---|
[5113] | 102 | } |
---|
[5105] | 103 | |
---|
[7413] | 104 | /** |
---|
| 105 | * @brief gets a command if it has already been registered. |
---|
| 106 | * @param commandName the name of the Command |
---|
| 107 | * @param cmdClass the CommandClass of the Class the command is in. |
---|
| 108 | * @returns The Registered Command, or NULL if it does not exist. |
---|
| 109 | */ |
---|
| 110 | const ShellCommand* const ShellCommand::getCommand(const std::string& commandName, const ShellCommandClass* cmdClass) |
---|
| 111 | { |
---|
| 112 | assert(cmdClass != NULL); |
---|
[7408] | 113 | |
---|
[7413] | 114 | std::vector<ShellCommand*>::const_iterator elem; |
---|
| 115 | for (elem = cmdClass->commandList.begin(); elem != cmdClass->commandList.end(); elem++) |
---|
| 116 | if (commandName == (*elem)->getName()) |
---|
| 117 | return (*elem); |
---|
| 118 | return NULL; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | |
---|
[7374] | 122 | /** |
---|
[7408] | 123 | * @brief gets a command if it has already been registered. |
---|
[7374] | 124 | * @param commandName the name of the Command |
---|
[7413] | 125 | * @param className the name of the Class the command is in. |
---|
[7408] | 126 | * @returns The Registered Command, or NULL if it does not exist. |
---|
[7374] | 127 | */ |
---|
[7408] | 128 | const ShellCommand* const ShellCommand::getCommand(const std::string& commandName, const std::string& className) |
---|
[5113] | 129 | { |
---|
[7408] | 130 | const ShellCommandClass* checkClass = ShellCommandClass::getCommandClass(className); |
---|
[7403] | 131 | if (likely(checkClass != NULL)) |
---|
[7413] | 132 | return ShellCommand::getCommand(commandName, checkClass); |
---|
| 133 | return NULL; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | /** |
---|
| 137 | * @brief takes out an InputLine, searching for a Command. |
---|
| 138 | * @param inputLine: the Input to analyse. |
---|
| 139 | * @returns: The ShellCommand if found. |
---|
| 140 | */ |
---|
| 141 | const ShellCommand* const ShellCommand::getCommandFromInput(const std::string& inputLine) |
---|
| 142 | { |
---|
| 143 | SubString strings(inputLine, SubString::WhiteSpaces); |
---|
| 144 | // no input, no Command. |
---|
| 145 | if (strings.size() == 0) |
---|
| 146 | return NULL; |
---|
| 147 | |
---|
| 148 | // CHECK FOR ALIAS |
---|
| 149 | std::vector<ShellCommandAlias*>::const_iterator alias; |
---|
| 150 | for (alias = ShellCommandAlias::getAliases().begin(); alias != ShellCommandAlias::getAliases().end(); alias++ ) |
---|
[7374] | 151 | { |
---|
[7413] | 152 | if (strings[0] == (*alias)->getName()) |
---|
| 153 | { |
---|
| 154 | assert ((*alias)->getCommand() != NULL && (*alias)->getCommand()->shellClass != NULL); |
---|
| 155 | return (*alias)->getCommand(); |
---|
| 156 | } |
---|
[5779] | 157 | } |
---|
[7413] | 158 | |
---|
| 159 | const ShellCommandClass* cmdClass = ShellCommandClass::getCommandClass(strings[0]); |
---|
| 160 | if (cmdClass != NULL) |
---|
| 161 | { |
---|
| 162 | const ShellCommand* retCmd; |
---|
| 163 | if (strings.size() >= 1) |
---|
| 164 | retCmd = ShellCommand::getCommand(strings[1], cmdClass); |
---|
| 165 | if (retCmd == NULL && strings.size() >= 2) |
---|
| 166 | retCmd = ShellCommand::getCommand(strings[2], cmdClass); |
---|
| 167 | return retCmd; |
---|
| 168 | |
---|
| 169 | } |
---|
[5113] | 170 | } |
---|
| 171 | |
---|
[7413] | 172 | |
---|
[7408] | 173 | /** |
---|
| 174 | * @brief checks if a command has already been registered. |
---|
| 175 | * @param commandName the name of the Command |
---|
| 176 | * @param className the name of the Class the command should apply to. |
---|
| 177 | * @returns true, if the command is registered/false otherwise |
---|
| 178 | * |
---|
| 179 | * This is used internally, to see, if we have multiple command subscriptions. |
---|
| 180 | * This is checked in the registerCommand-function. |
---|
| 181 | */ |
---|
| 182 | bool ShellCommand::exists(const std::string& commandName, const std::string& className) |
---|
| 183 | { |
---|
| 184 | return (ShellCommand::getCommand(commandName, className) != NULL); |
---|
| 185 | } |
---|
[5140] | 186 | |
---|
[7408] | 187 | |
---|
[7374] | 188 | /** |
---|
[7394] | 189 | * @brief executes commands |
---|
[7374] | 190 | * @param executionString the string containing the following input |
---|
| 191 | * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]] |
---|
| 192 | * @return true on success, false otherwise. |
---|
| 193 | */ |
---|
| 194 | bool ShellCommand::execute(const std::string& executionString) |
---|
| 195 | { |
---|
| 196 | long classID = CL_NULL; //< the classID retrieved from the Class. |
---|
| 197 | ShellCommandClass* commandClass = NULL; //< the command class this command applies to. |
---|
| 198 | const std::list<BaseObject*>* objectList = NULL; //< the list of Objects stored in classID |
---|
| 199 | BaseObject* objectPointer = NULL; //< a pointer to th Object to Execute the command on |
---|
[7411] | 200 | // bool emptyComplete = false; //< if the completion input is empty string. e.g "" |
---|
[7374] | 201 | // long completeType = SHELLC_NONE; //< the Type we'd like to complete. |
---|
[7403] | 202 | |
---|
[7374] | 203 | SubString inputSplits(executionString, SubString::WhiteSpacesWithComma); |
---|
[5198] | 204 | |
---|
[7340] | 205 | |
---|
[7374] | 206 | // if we do not have any input return |
---|
| 207 | if (inputSplits.empty()) |
---|
| 208 | return false; |
---|
[7340] | 209 | |
---|
[7374] | 210 | // if we only have one input (!MUST BE AN ALIAS) |
---|
[7403] | 211 | // CHECK FOR ALIAS |
---|
| 212 | std::vector<ShellCommandAlias*>::const_iterator alias; |
---|
| 213 | for (alias = ShellCommandAlias::getAliases().begin(); alias != ShellCommandAlias::getAliases().end(); alias++ ) |
---|
[5198] | 214 | { |
---|
[7403] | 215 | if (inputSplits.getString(0) == (*alias)->getName() && (*alias)->getCommand() != NULL && |
---|
| 216 | (*alias)->getCommand()->shellClass != NULL ) |
---|
[5198] | 217 | { |
---|
[7403] | 218 | objectList = ClassList::getList((*alias)->getCommand()->shellClass->getName()); |
---|
| 219 | if (objectList != NULL) |
---|
[5198] | 220 | { |
---|
[7403] | 221 | (*(*alias)->getCommand()->executor)(objectList->front(), inputSplits.getSubSet(1).join()); |
---|
| 222 | return true; |
---|
[5198] | 223 | } |
---|
| 224 | } |
---|
[7403] | 225 | } |
---|
[7340] | 226 | |
---|
[7403] | 227 | // looking for a Matching Class (in the First Argument) |
---|
| 228 | std::vector<ShellCommandClass*>::iterator commandClassIT; |
---|
| 229 | for (commandClassIT = ShellCommandClass::commandClassList.begin(); commandClassIT != ShellCommandClass::commandClassList.end(); commandClassIT++) |
---|
| 230 | { |
---|
| 231 | if (inputSplits[0] == (*commandClassIT)->getName()) |
---|
[5203] | 232 | { |
---|
[7403] | 233 | //elemCL->getName(); |
---|
| 234 | classID = ClassList::StringToID((*commandClassIT)->getName()); |
---|
| 235 | commandClass = (*commandClassIT); |
---|
| 236 | objectList = ClassList::getList((ClassID)classID); |
---|
| 237 | break; |
---|
[5203] | 238 | } |
---|
[7403] | 239 | } |
---|
[5200] | 240 | |
---|
[7403] | 241 | // Second Agument. (either Object, or Function) |
---|
| 242 | if (commandClass != NULL && inputSplits.size() >= 2) |
---|
| 243 | { |
---|
| 244 | int fktPos = 1; // The position of the Function (either at pos 1(if object given), or 2) |
---|
| 245 | // If we have an ObjectList. |
---|
| 246 | if (objectList != NULL) |
---|
[5203] | 247 | { |
---|
[7403] | 248 | // Checking for a Match in the Objects of classID (else take the first) |
---|
| 249 | std::list<BaseObject*>::const_iterator object; |
---|
| 250 | for (object = objectList->begin(); object != objectList->end(); object++) |
---|
[5203] | 251 | { |
---|
[7403] | 252 | if ((*object)->getName() != NULL && inputSplits[1] == (*object)->getName()) |
---|
[5329] | 253 | { |
---|
[7403] | 254 | /// TODO make this work for multiple Objects at once. |
---|
| 255 | objectPointer = (*object); |
---|
| 256 | fktPos = 2; |
---|
| 257 | break; |
---|
[5329] | 258 | } |
---|
[7340] | 259 | } |
---|
[5203] | 260 | |
---|
[7403] | 261 | // if we did not find an Object with matching name, take the first. |
---|
| 262 | if (objectPointer == NULL) |
---|
| 263 | objectPointer = objectList->front(); |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | // match a function. |
---|
| 267 | if (commandClass != NULL && (fktPos == 1 || (fktPos == 2 && inputSplits.size() >= 3))) |
---|
| 268 | { |
---|
| 269 | std::vector<ShellCommand*>::iterator cmdIT; |
---|
| 270 | for (cmdIT = commandClass->commandList.begin(); cmdIT != commandClass->commandList.end(); cmdIT++) |
---|
[5203] | 271 | { |
---|
[7403] | 272 | if (inputSplits[fktPos] == (*cmdIT)->getName()) |
---|
[5203] | 273 | { |
---|
[7403] | 274 | if (objectPointer == NULL && (*cmdIT)->executor->getType() & Executor_Objective) |
---|
| 275 | return false; |
---|
| 276 | else |
---|
[7340] | 277 | { |
---|
[7403] | 278 | (*(*cmdIT)->executor)(objectPointer, inputSplits.getSubSet(fktPos+1).join()); /// TODO CHECK IF OK |
---|
| 279 | return true; |
---|
[7340] | 280 | } |
---|
[5203] | 281 | } |
---|
| 282 | } |
---|
| 283 | } |
---|
| 284 | } |
---|
[7374] | 285 | return false; |
---|
[5198] | 286 | } |
---|
[5148] | 287 | |
---|
[7411] | 288 | |
---|
[7374] | 289 | /** |
---|
[7401] | 290 | * @brief lets a command be described |
---|
[7374] | 291 | * @param description the description of the Given command |
---|
| 292 | */ |
---|
| 293 | ShellCommand* ShellCommand::describe(const std::string& description) |
---|
[7340] | 294 | { |
---|
[7374] | 295 | if (this == NULL) |
---|
| 296 | return NULL; |
---|
[7403] | 297 | this->description = description; |
---|
| 298 | return this; |
---|
[7340] | 299 | } |
---|
[5164] | 300 | |
---|
[7374] | 301 | /** |
---|
[7389] | 302 | * @brief adds an Alias to this Command |
---|
[7374] | 303 | * @param alias the name of the Alias to set |
---|
| 304 | * @returns itself |
---|
| 305 | */ |
---|
| 306 | ShellCommand* ShellCommand::setAlias(const std::string& alias) |
---|
[5196] | 307 | { |
---|
[7374] | 308 | if (this == NULL) |
---|
| 309 | return NULL; |
---|
[5196] | 310 | |
---|
[7374] | 311 | if (this->alias != NULL) |
---|
| 312 | { |
---|
| 313 | PRINTF(2)("not more than one Alias allowed for functions (%s::%s)\n", this->getName(), this->shellClass->getName()); |
---|
| 314 | } |
---|
| 315 | else |
---|
| 316 | { |
---|
| 317 | ShellCommandAlias* aliasCMD = new ShellCommandAlias(alias, this); |
---|
| 318 | this->alias = aliasCMD; |
---|
| 319 | } |
---|
| 320 | return this; |
---|
[5196] | 321 | } |
---|
[5195] | 322 | |
---|
[7374] | 323 | /** |
---|
| 324 | * @brief set the default values of the executor |
---|
| 325 | * @param value0 the first default value |
---|
| 326 | * @param value1 the second default value |
---|
| 327 | * @param value2 the third default value |
---|
| 328 | * @param value3 the fourth default value |
---|
| 329 | * @param value4 the fifth default value |
---|
| 330 | */ |
---|
| 331 | ShellCommand* ShellCommand::defaultValues(const MultiType& value0, const MultiType& value1, |
---|
| 332 | const MultiType& value2, const MultiType& value3, |
---|
| 333 | const MultiType& value4) |
---|
| 334 | { |
---|
| 335 | if (this == NULL || this->executor == NULL) |
---|
| 336 | return NULL; |
---|
[5207] | 337 | |
---|
[7374] | 338 | this->executor->defaultValues(value0, value1, value2, value3, value4); |
---|
[5207] | 339 | |
---|
[7374] | 340 | return this; |
---|
[5148] | 341 | } |
---|
| 342 | |
---|
[7412] | 343 | ShellCommand* ShellCommand::completionPlugin(unsigned int parameter, const CompletorPlugin& completorPlugin) |
---|
| 344 | { |
---|
| 345 | if (this == NULL || this->executor == NULL) |
---|
| 346 | return NULL; |
---|
| 347 | |
---|
| 348 | if (parameter >= this->executor->getParamCount()) |
---|
| 349 | { |
---|
| 350 | PRINTF(1)("Parameter %d not inside of valid ParameterCount %d of Command %s::%s\n", |
---|
[7413] | 351 | parameter, this->executor->getParamCount(), this->getName(), this->shellClass->getName()); |
---|
[7412] | 352 | } |
---|
| 353 | else |
---|
| 354 | { |
---|
| 355 | delete this->completors[parameter]; |
---|
| 356 | this->completors[parameter] = completorPlugin.clone(); |
---|
| 357 | } |
---|
| 358 | return this; |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | |
---|
[7374] | 362 | /** |
---|
[7401] | 363 | * @brief prints out nice information about the Shells Commands |
---|
[7374] | 364 | */ |
---|
| 365 | void ShellCommand::debug() |
---|
[5148] | 366 | { |
---|
[7394] | 367 | std::vector<ShellCommandClass*>::iterator classIT; |
---|
| 368 | for (classIT = ShellCommandClass::commandClassList.begin(); classIT != ShellCommandClass::commandClassList.end(); classIT++) |
---|
[7374] | 369 | { |
---|
| 370 | PRINT(0)("Class:'%s' registered %d commands: \n", (*classIT)->className.c_str(), (*classIT)->commandList.size()); |
---|
[5148] | 371 | |
---|
[7388] | 372 | std::vector<ShellCommand*>::iterator cmdIT; |
---|
[7374] | 373 | for (cmdIT = (*classIT)->commandList.begin(); cmdIT != (*classIT)->commandList.end(); cmdIT++) |
---|
| 374 | { |
---|
| 375 | PRINT(0)(" command:'%s' : params:%d: ", (*cmdIT)->getName(), (*cmdIT)->executor->getParamCount()); |
---|
| 376 | /// FIXME |
---|
| 377 | /* for (unsigned int i = 0; i< elem->paramCount; i++) |
---|
| 378 | printf("%s ", ShellCommand::paramToString(elem->parameters[i]));*/ |
---|
| 379 | if (!(*cmdIT)->description.empty()) |
---|
| 380 | printf("- %s", (*cmdIT)->description.c_str()); |
---|
| 381 | printf("\n"); |
---|
| 382 | |
---|
| 383 | } |
---|
[5170] | 384 | } |
---|
[5148] | 385 | } |
---|
| 386 | |
---|
[7374] | 387 | /** |
---|
[7401] | 388 | * @brief converts a Parameter to a String |
---|
[7374] | 389 | * @param parameter the Parameter we have. |
---|
| 390 | * @returns the Name of the Parameter at Hand |
---|
| 391 | */ |
---|
[7401] | 392 | const std::string& ShellCommand::paramToString(long parameter) |
---|
[7374] | 393 | { |
---|
| 394 | return MultiType::MultiTypeToString((MT_Type)parameter); |
---|
| 395 | } |
---|
| 396 | |
---|
[7389] | 397 | |
---|
[7412] | 398 | |
---|
| 399 | /////////// |
---|
| 400 | // ALIAS // |
---|
| 401 | /////////// |
---|
| 402 | |
---|
[7397] | 403 | /** |
---|
| 404 | * @param aliasName the name of the Alias |
---|
| 405 | * @param command the Command, to associate this alias with |
---|
| 406 | */ |
---|
[7389] | 407 | ShellCommandAlias::ShellCommandAlias(const std::string& aliasName, ShellCommand* command) |
---|
| 408 | { |
---|
| 409 | this->aliasName = aliasName; |
---|
| 410 | this->command = command; |
---|
| 411 | ShellCommandAlias::aliasList.push_back(this); |
---|
| 412 | }; |
---|
| 413 | |
---|
| 414 | ShellCommandAlias::~ShellCommandAlias() |
---|
| 415 | { |
---|
| 416 | std::vector<ShellCommandAlias*>::iterator delA = std::find(aliasList.begin(), aliasList.end(), this); |
---|
| 417 | if (delA != aliasList.end()) |
---|
| 418 | ShellCommandAlias::aliasList.push_back(this); |
---|
| 419 | |
---|
| 420 | } |
---|
| 421 | |
---|
| 422 | std::vector<ShellCommandAlias*> ShellCommandAlias::aliasList; |
---|
| 423 | /** |
---|
| 424 | * @brief collects the Aliases registered to the ShellCommands |
---|
| 425 | * @param stringList a List to paste the Aliases into. |
---|
| 426 | * @returns true on success, false otherwise |
---|
| 427 | */ |
---|
[7403] | 428 | |
---|
[7389] | 429 | bool ShellCommandAlias::getCommandListOfAlias(std::list<std::string>& stringList) |
---|
| 430 | { |
---|
| 431 | std::vector<ShellCommandAlias*>::iterator alias; |
---|
| 432 | for (alias = ShellCommandAlias::aliasList.begin(); alias != ShellCommandAlias::aliasList.end(); alias++) |
---|
| 433 | stringList.push_back((*alias)->getName()); |
---|
| 434 | return true; |
---|
| 435 | } |
---|
| 436 | |
---|
| 437 | |
---|
[5148] | 438 | } |
---|