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