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