[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 | |
---|
[7388] | 48 | this->alias = NULL; |
---|
[4320] | 49 | |
---|
[7374] | 50 | // this->classID = classID; |
---|
[7388] | 51 | this->shellClass = ShellCommandClass::getCommandClass(className); |
---|
[7398] | 52 | assert (this->shellClass != NULL); |
---|
[7399] | 53 | this->shellClass->registerCommand(this); |
---|
[7374] | 54 | } |
---|
| 55 | |
---|
| 56 | /** |
---|
[7394] | 57 | * @brief deconstructs a ShellCommand |
---|
[7374] | 58 | */ |
---|
| 59 | ShellCommand::~ShellCommand() |
---|
[5196] | 60 | { |
---|
[7399] | 61 | this->shellClass->unregisterCommand(this); |
---|
[7389] | 62 | if (this->alias != NULL) |
---|
[7374] | 63 | delete this->alias; |
---|
| 64 | delete this->executor; |
---|
[5196] | 65 | } |
---|
[1853] | 66 | |
---|
[7374] | 67 | /** |
---|
[7398] | 68 | * @brief registers a new ShellCommand |
---|
[7374] | 69 | */ |
---|
| 70 | ShellCommand* ShellCommand::registerCommand(const std::string& commandName, const std::string& className, const Executor& executor) |
---|
| 71 | { |
---|
| 72 | if (ShellCommand::isRegistered(commandName, className)) |
---|
| 73 | return NULL; |
---|
| 74 | else |
---|
| 75 | return new ShellCommand(commandName, className, executor); |
---|
[5636] | 76 | |
---|
[7374] | 77 | } |
---|
[5636] | 78 | |
---|
[7374] | 79 | /** |
---|
[7398] | 80 | * @brief unregister an existing commandName |
---|
[7374] | 81 | * @param className the name of the Class the command belongs to. |
---|
| 82 | * @param commandName the name of the command itself |
---|
| 83 | */ |
---|
| 84 | void ShellCommand::unregisterCommand(const std::string& commandName, const std::string& className) |
---|
| 85 | { |
---|
[5171] | 86 | |
---|
[7399] | 87 | ShellCommandClass* cmdClass = ShellCommandClass::getCommandClass(className); |
---|
| 88 | if (cmdClass != NULL) |
---|
| 89 | { |
---|
| 90 | std::vector<ShellCommand*>::iterator cmd; |
---|
| 91 | for (cmd = cmdClass->commandList.begin(); cmd < cmdClass->commandList.end(); cmd++) |
---|
[5171] | 92 | { |
---|
[7399] | 93 | if (commandName == (*cmd)->getName()) |
---|
[7340] | 94 | { |
---|
[7399] | 95 | delete (*cmd); |
---|
[7340] | 96 | } |
---|
[7399] | 97 | } |
---|
| 98 | } |
---|
[5113] | 99 | } |
---|
[5105] | 100 | |
---|
[7374] | 101 | /** |
---|
[7394] | 102 | * @brief checks if a command has already been registered. |
---|
[7374] | 103 | * @param commandName the name of the Command |
---|
| 104 | * @param className the name of the Class the command should apply to. |
---|
| 105 | * @returns true, if the command is registered/false otherwise |
---|
| 106 | * |
---|
| 107 | * This is used internally, to see, if we have multiple command subscriptions. |
---|
| 108 | * This is checked in the registerCommand-function. |
---|
| 109 | */ |
---|
| 110 | bool ShellCommand::isRegistered(const std::string& commandName, const std::string& className) |
---|
[5113] | 111 | { |
---|
[7374] | 112 | const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className); |
---|
| 113 | if (checkClass != NULL) |
---|
| 114 | { |
---|
[7388] | 115 | std::vector<ShellCommand*>::const_iterator elem; |
---|
[7374] | 116 | for (elem = checkClass->commandList.begin(); elem != checkClass->commandList.end(); elem++) |
---|
[5779] | 117 | { |
---|
[7374] | 118 | if (commandName == (*elem)->getName()) |
---|
| 119 | { |
---|
| 120 | PRINTF(2)("Command '%s::%s' already registered\n", className.c_str(), commandName.c_str()); |
---|
| 121 | return true; |
---|
| 122 | } |
---|
[5170] | 123 | } |
---|
[7374] | 124 | return false; |
---|
[5779] | 125 | } |
---|
[7374] | 126 | else |
---|
| 127 | return false; |
---|
[5113] | 128 | } |
---|
| 129 | |
---|
[5140] | 130 | |
---|
[7374] | 131 | /** |
---|
[7394] | 132 | * @brief executes commands |
---|
[7374] | 133 | * @param executionString the string containing the following input |
---|
| 134 | * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]] |
---|
| 135 | * @return true on success, false otherwise. |
---|
| 136 | */ |
---|
| 137 | bool ShellCommand::execute(const std::string& executionString) |
---|
| 138 | { |
---|
| 139 | long classID = CL_NULL; //< the classID retrieved from the Class. |
---|
| 140 | ShellCommandClass* commandClass = NULL; //< the command class this command applies to. |
---|
| 141 | const std::list<BaseObject*>* objectList = NULL; //< the list of Objects stored in classID |
---|
| 142 | BaseObject* objectPointer = NULL; //< a pointer to th Object to Execute the command on |
---|
| 143 | bool emptyComplete = false; //< if the completion input is empty string. e.g "" |
---|
| 144 | // long completeType = SHELLC_NONE; //< the Type we'd like to complete. |
---|
| 145 | SubString inputSplits(executionString, SubString::WhiteSpacesWithComma); |
---|
[5198] | 146 | |
---|
[7340] | 147 | |
---|
[7374] | 148 | // if we do not have any input return |
---|
| 149 | if (inputSplits.empty()) |
---|
| 150 | return false; |
---|
[7340] | 151 | |
---|
[7374] | 152 | // if we only have one input (!MUST BE AN ALIAS) |
---|
| 153 | if (inputSplits.size() >= 1) |
---|
[5198] | 154 | { |
---|
[7374] | 155 | // CHECK FOR ALIAS |
---|
[7397] | 156 | std::vector<ShellCommandAlias*>::const_iterator alias; |
---|
| 157 | for (alias = ShellCommandAlias::getAliases().begin(); alias != ShellCommandAlias::getAliases().end(); alias++ ) |
---|
[5198] | 158 | { |
---|
[7389] | 159 | if (inputSplits.getString(0) == (*alias)->getName() && (*alias)->getCommand() != NULL && |
---|
| 160 | (*alias)->getCommand()->shellClass != NULL ) |
---|
[5198] | 161 | { |
---|
[7389] | 162 | objectList = ClassList::getList((*alias)->getCommand()->shellClass->getName()); |
---|
| 163 | if (objectList != NULL) |
---|
[5199] | 164 | { |
---|
[7389] | 165 | (*(*alias)->getCommand()->executor)(objectList->front(), inputSplits.getSubSet(1).join()); |
---|
| 166 | return true; |
---|
[5199] | 167 | } |
---|
[7389] | 168 | /// TODO CHECK FOR STATIC functions. |
---|
[5198] | 169 | } |
---|
| 170 | } |
---|
[7340] | 171 | |
---|
[7374] | 172 | // looking for a Matching Class |
---|
[7394] | 173 | std::vector<ShellCommandClass*>::iterator commandClassIT; |
---|
| 174 | for (commandClassIT = ShellCommandClass::commandClassList.begin(); commandClassIT != ShellCommandClass::commandClassList.end(); commandClassIT++) |
---|
[5203] | 175 | { |
---|
[7394] | 176 | if ((*commandClassIT)->getName() && inputSplits[0] == (*commandClassIT)->getName()) |
---|
[5203] | 177 | { |
---|
[7394] | 178 | //elemCL->getName(); |
---|
| 179 | classID = ClassList::StringToID((*commandClassIT)->getName()); |
---|
| 180 | commandClass = (*commandClassIT); |
---|
| 181 | objectList = ClassList::getList((ClassID)classID); |
---|
| 182 | break; |
---|
[5203] | 183 | } |
---|
| 184 | } |
---|
[5200] | 185 | |
---|
[7374] | 186 | // Second Agument. (either Object, or Function) |
---|
| 187 | if (commandClass != NULL && inputSplits.size() >= 2) |
---|
[5203] | 188 | { |
---|
[7374] | 189 | int fktPos = 1; // The position of the Function (either at pos 1, or 2) |
---|
| 190 | // If we have an ObjectList. |
---|
| 191 | if (objectList != NULL) |
---|
[5203] | 192 | { |
---|
[7374] | 193 | // Checking for a Match in the Objects of classID (else take the first) |
---|
| 194 | std::list<BaseObject*>::const_iterator object; |
---|
| 195 | for (object = objectList->begin(); object != objectList->end(); object++) |
---|
[5329] | 196 | { |
---|
[7374] | 197 | if ((*object)->getName() != NULL && inputSplits[1] == (*object)->getName()) |
---|
| 198 | { |
---|
| 199 | objectPointer = (*object); |
---|
| 200 | fktPos = 2; |
---|
| 201 | break; |
---|
| 202 | } |
---|
[5329] | 203 | } |
---|
[7374] | 204 | |
---|
| 205 | // if we did not find an Object with matching name, take the first. |
---|
| 206 | if (objectPointer == NULL) |
---|
| 207 | objectPointer = objectList->front(); |
---|
[7340] | 208 | } |
---|
[5203] | 209 | |
---|
[7374] | 210 | // match a function. |
---|
| 211 | if (commandClass != NULL && (fktPos == 1 || (fktPos == 2 && inputSplits.size() >= 3))) |
---|
[5203] | 212 | { |
---|
[7388] | 213 | std::vector<ShellCommand*>::iterator cmdIT; |
---|
[7374] | 214 | for (cmdIT = commandClass->commandList.begin(); cmdIT != commandClass->commandList.end(); cmdIT++) |
---|
[5203] | 215 | { |
---|
[7374] | 216 | if (inputSplits[fktPos] == (*cmdIT)->getName()) |
---|
[7340] | 217 | { |
---|
[7374] | 218 | if (objectPointer == NULL && (*cmdIT)->executor->getType() & Executor_Objective) |
---|
| 219 | return false; |
---|
| 220 | else |
---|
| 221 | { |
---|
| 222 | (*(*cmdIT)->executor)(objectPointer, inputSplits.getSubSet(fktPos+1).join()); /// TODO CHECK IF OK |
---|
| 223 | return true; |
---|
| 224 | } |
---|
[7340] | 225 | } |
---|
[5203] | 226 | } |
---|
| 227 | } |
---|
| 228 | } |
---|
| 229 | } |
---|
[7374] | 230 | return false; |
---|
[5198] | 231 | } |
---|
[5148] | 232 | |
---|
[7374] | 233 | /** |
---|
| 234 | * lets a command be described |
---|
| 235 | * @param description the description of the Given command |
---|
| 236 | */ |
---|
| 237 | ShellCommand* ShellCommand::describe(const std::string& description) |
---|
[7340] | 238 | { |
---|
[7374] | 239 | if (this == NULL) |
---|
| 240 | return NULL; |
---|
| 241 | else |
---|
| 242 | { |
---|
| 243 | this->description = description; |
---|
| 244 | return this; |
---|
| 245 | } |
---|
[7340] | 246 | } |
---|
[5164] | 247 | |
---|
[7374] | 248 | /** |
---|
[7389] | 249 | * @brief adds an Alias to this Command |
---|
[7374] | 250 | * @param alias the name of the Alias to set |
---|
| 251 | * @returns itself |
---|
| 252 | */ |
---|
| 253 | ShellCommand* ShellCommand::setAlias(const std::string& alias) |
---|
[5196] | 254 | { |
---|
[7374] | 255 | if (this == NULL) |
---|
| 256 | return NULL; |
---|
[5196] | 257 | |
---|
[7374] | 258 | if (this->alias != NULL) |
---|
| 259 | { |
---|
| 260 | PRINTF(2)("not more than one Alias allowed for functions (%s::%s)\n", this->getName(), this->shellClass->getName()); |
---|
| 261 | } |
---|
| 262 | else |
---|
| 263 | { |
---|
| 264 | ShellCommandAlias* aliasCMD = new ShellCommandAlias(alias, this); |
---|
| 265 | this->alias = aliasCMD; |
---|
| 266 | } |
---|
| 267 | return this; |
---|
[5196] | 268 | } |
---|
[5195] | 269 | |
---|
[7374] | 270 | /** |
---|
| 271 | * @brief set the default values of the executor |
---|
| 272 | * @param value0 the first default value |
---|
| 273 | * @param value1 the second default value |
---|
| 274 | * @param value2 the third default value |
---|
| 275 | * @param value3 the fourth default value |
---|
| 276 | * @param value4 the fifth default value |
---|
| 277 | */ |
---|
| 278 | ShellCommand* ShellCommand::defaultValues(const MultiType& value0, const MultiType& value1, |
---|
| 279 | const MultiType& value2, const MultiType& value3, |
---|
| 280 | const MultiType& value4) |
---|
| 281 | { |
---|
| 282 | if (this == NULL || this->executor == NULL) |
---|
| 283 | return NULL; |
---|
[5207] | 284 | |
---|
[7374] | 285 | this->executor->defaultValues(value0, value1, value2, value3, value4); |
---|
[5207] | 286 | |
---|
[7374] | 287 | return this; |
---|
[5148] | 288 | } |
---|
| 289 | |
---|
[7374] | 290 | /** |
---|
| 291 | * prints out nice information about the Shells Commands |
---|
| 292 | */ |
---|
| 293 | void ShellCommand::debug() |
---|
[5148] | 294 | { |
---|
[7394] | 295 | std::vector<ShellCommandClass*>::iterator classIT; |
---|
| 296 | for (classIT = ShellCommandClass::commandClassList.begin(); classIT != ShellCommandClass::commandClassList.end(); classIT++) |
---|
[7374] | 297 | { |
---|
| 298 | PRINT(0)("Class:'%s' registered %d commands: \n", (*classIT)->className.c_str(), (*classIT)->commandList.size()); |
---|
[5148] | 299 | |
---|
[7388] | 300 | std::vector<ShellCommand*>::iterator cmdIT; |
---|
[7374] | 301 | for (cmdIT = (*classIT)->commandList.begin(); cmdIT != (*classIT)->commandList.end(); cmdIT++) |
---|
| 302 | { |
---|
| 303 | PRINT(0)(" command:'%s' : params:%d: ", (*cmdIT)->getName(), (*cmdIT)->executor->getParamCount()); |
---|
| 304 | /// FIXME |
---|
| 305 | /* for (unsigned int i = 0; i< elem->paramCount; i++) |
---|
| 306 | printf("%s ", ShellCommand::paramToString(elem->parameters[i]));*/ |
---|
| 307 | if (!(*cmdIT)->description.empty()) |
---|
| 308 | printf("- %s", (*cmdIT)->description.c_str()); |
---|
| 309 | printf("\n"); |
---|
| 310 | |
---|
| 311 | } |
---|
[5170] | 312 | } |
---|
[5148] | 313 | } |
---|
| 314 | |
---|
[7374] | 315 | /** |
---|
| 316 | * converts a Parameter to a String |
---|
| 317 | * @param parameter the Parameter we have. |
---|
| 318 | * @returns the Name of the Parameter at Hand |
---|
| 319 | */ |
---|
| 320 | const char* ShellCommand::paramToString(long parameter) |
---|
| 321 | { |
---|
| 322 | return MultiType::MultiTypeToString((MT_Type)parameter); |
---|
| 323 | } |
---|
| 324 | |
---|
[7389] | 325 | |
---|
[7397] | 326 | /** |
---|
| 327 | * @param aliasName the name of the Alias |
---|
| 328 | * @param command the Command, to associate this alias with |
---|
| 329 | */ |
---|
[7389] | 330 | ShellCommandAlias::ShellCommandAlias(const std::string& aliasName, ShellCommand* command) |
---|
| 331 | { |
---|
| 332 | this->aliasName = aliasName; |
---|
| 333 | this->command = command; |
---|
| 334 | ShellCommandAlias::aliasList.push_back(this); |
---|
| 335 | }; |
---|
| 336 | |
---|
| 337 | ShellCommandAlias::~ShellCommandAlias() |
---|
| 338 | { |
---|
| 339 | std::vector<ShellCommandAlias*>::iterator delA = std::find(aliasList.begin(), aliasList.end(), this); |
---|
| 340 | if (delA != aliasList.end()) |
---|
| 341 | ShellCommandAlias::aliasList.push_back(this); |
---|
| 342 | |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | std::vector<ShellCommandAlias*> ShellCommandAlias::aliasList; |
---|
| 346 | /** |
---|
| 347 | * @brief collects the Aliases registered to the ShellCommands |
---|
| 348 | * @param stringList a List to paste the Aliases into. |
---|
| 349 | * @returns true on success, false otherwise |
---|
| 350 | */ |
---|
| 351 | bool ShellCommandAlias::getCommandListOfAlias(std::list<std::string>& stringList) |
---|
| 352 | { |
---|
| 353 | std::vector<ShellCommandAlias*>::iterator alias; |
---|
| 354 | for (alias = ShellCommandAlias::aliasList.begin(); alias != ShellCommandAlias::aliasList.end(); alias++) |
---|
| 355 | stringList.push_back((*alias)->getName()); |
---|
| 356 | return true; |
---|
| 357 | } |
---|
| 358 | |
---|
| 359 | |
---|
[5148] | 360 | } |
---|