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