[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 | |
---|
[3955] | 16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ |
---|
[1853] | 17 | |
---|
[5129] | 18 | #include "shell_command.h" |
---|
[1853] | 19 | |
---|
[5072] | 20 | #include "list.h" |
---|
[5129] | 21 | #include "debug.h" |
---|
[5113] | 22 | #include "class_list.h" |
---|
| 23 | |
---|
| 24 | #include "key_names.h" |
---|
[5075] | 25 | #include <stdarg.h> |
---|
| 26 | #include <stdio.h> |
---|
[5174] | 27 | #include <string.h> |
---|
[5075] | 28 | |
---|
[1856] | 29 | using namespace std; |
---|
[1853] | 30 | |
---|
[5166] | 31 | /** |
---|
[5461] | 32 | * creates a new CommandChainClass |
---|
[5170] | 33 | * @param className the Name of the command-class to create |
---|
| 34 | */ |
---|
[5461] | 35 | CommandChainClass::CommandChainClass(const char* className) |
---|
[5170] | 36 | { |
---|
[5461] | 37 | this->setClassID(CL_COMMAND_CHAIN_CLASS, "CommandChainClass"); |
---|
[5188] | 38 | this->setName(className); |
---|
| 39 | |
---|
[5170] | 40 | this->className = className; |
---|
| 41 | this->classID = CL_NULL; |
---|
[5461] | 42 | this->commandList = new tList<CommandChainBase>; |
---|
[5170] | 43 | |
---|
[5461] | 44 | CommandChainClass::commandClassList->add(this); |
---|
[5170] | 45 | } |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | * destructs the shellCommandClass again |
---|
| 49 | */ |
---|
[5461] | 50 | CommandChainClass::~CommandChainClass() |
---|
[5170] | 51 | { |
---|
[5461] | 52 | tIterator<CommandChainBase>* iterator = this->commandList->getIterator(); |
---|
| 53 | CommandChainBase* elem = iterator->firstElement(); |
---|
[5170] | 54 | while(elem != NULL) |
---|
| 55 | { |
---|
| 56 | delete elem; |
---|
| 57 | elem = iterator->nextElement(); |
---|
| 58 | } |
---|
| 59 | delete iterator; |
---|
| 60 | delete this->commandList; |
---|
| 61 | } |
---|
| 62 | |
---|
[5197] | 63 | /** |
---|
| 64 | * collects the Commands registered to some class. |
---|
| 65 | * @param className the name of the Class to collect the Commands from. |
---|
| 66 | * @param stringList a List to paste the Commands into. |
---|
| 67 | * @returns true on success, false otherwise |
---|
| 68 | */ |
---|
[5461] | 69 | bool CommandChainClass::getCommandListOfClass(const char* className, tList<const char>* stringList) |
---|
[5189] | 70 | { |
---|
[5192] | 71 | if (stringList == NULL || className == NULL) |
---|
[5190] | 72 | return false; |
---|
| 73 | |
---|
[5461] | 74 | tIterator<CommandChainClass>* iterator = CommandChainClass::commandClassList->getIterator(); |
---|
| 75 | CommandChainClass* elem = iterator->firstElement(); |
---|
[5189] | 76 | while(elem != NULL) |
---|
| 77 | { |
---|
| 78 | if (!strcmp (elem->getName(), className)) |
---|
| 79 | { |
---|
[5461] | 80 | tIterator<CommandChainBase>* itFkt = elem->commandList->getIterator(); |
---|
| 81 | CommandChainBase* command = itFkt->firstElement(); |
---|
[5189] | 82 | while (command != NULL) |
---|
| 83 | { |
---|
[5190] | 84 | stringList->add(command->getName()); |
---|
[5189] | 85 | command = itFkt->nextElement(); |
---|
| 86 | } |
---|
| 87 | delete itFkt; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | elem = iterator->nextElement(); |
---|
| 91 | } |
---|
| 92 | delete iterator; |
---|
[5190] | 93 | return true; |
---|
[5189] | 94 | } |
---|
| 95 | |
---|
[5197] | 96 | /** |
---|
[5461] | 97 | * collects the Aliases registered to the CommandChains |
---|
[5197] | 98 | * @param stringList a List to paste the Aliases into. |
---|
| 99 | * @returns true on success, false otherwise |
---|
| 100 | */ |
---|
[5461] | 101 | bool CommandChainClass::getCommandListOfAlias(tList<const char>* stringList) |
---|
[5195] | 102 | { |
---|
[5461] | 103 | if (stringList == NULL || CommandChainClass::aliasList == NULL) |
---|
[5195] | 104 | return false; |
---|
| 105 | |
---|
[5461] | 106 | tIterator<CommandChainAlias>* iterator = CommandChainClass::aliasList->getIterator(); |
---|
| 107 | CommandChainAlias* elem = iterator->firstElement(); |
---|
[5196] | 108 | while(elem != NULL) |
---|
| 109 | { |
---|
| 110 | stringList->add(elem->getName()); |
---|
| 111 | elem = iterator->nextElement(); |
---|
| 112 | } |
---|
| 113 | delete iterator; |
---|
| 114 | return true; |
---|
[5195] | 115 | } |
---|
| 116 | |
---|
[5171] | 117 | /** |
---|
| 118 | * unregisters all Commands that exist |
---|
| 119 | */ |
---|
[5461] | 120 | void CommandChainClass::unregisterAllCommands() |
---|
[5171] | 121 | { |
---|
[5195] | 122 | // unregister all commands |
---|
[5461] | 123 | tIterator<CommandChainClass>* iterator = CommandChainClass::commandClassList->getIterator(); |
---|
| 124 | CommandChainClass* elem = iterator->firstElement(); |
---|
[5171] | 125 | while(elem != NULL) |
---|
| 126 | { |
---|
| 127 | delete elem; |
---|
| 128 | |
---|
| 129 | elem = iterator->nextElement(); |
---|
| 130 | } |
---|
| 131 | delete iterator; |
---|
| 132 | |
---|
[5461] | 133 | delete CommandChainClass::commandClassList; |
---|
| 134 | CommandChainClass::commandClassList = NULL; |
---|
[5195] | 135 | |
---|
| 136 | // unregister all aliases (there should be nothing to do here :)) |
---|
[5461] | 137 | if (CommandChainClass::aliasList != NULL) |
---|
[5195] | 138 | { |
---|
[5461] | 139 | tIterator<CommandChainAlias>* itAL = CommandChainClass::aliasList->getIterator(); |
---|
| 140 | CommandChainAlias* elemAL = itAL->firstElement(); |
---|
[5197] | 141 | while(elemAL != NULL) |
---|
| 142 | { |
---|
| 143 | delete elemAL; |
---|
| 144 | elemAL = itAL->nextElement(); |
---|
| 145 | } |
---|
| 146 | delete itAL; |
---|
[5461] | 147 | delete CommandChainClass::aliasList; |
---|
| 148 | CommandChainClass::aliasList = NULL; |
---|
[5195] | 149 | } |
---|
[5171] | 150 | } |
---|
| 151 | |
---|
[5197] | 152 | /** |
---|
| 153 | * checks if a Class is already registered to the Commands' class-stack |
---|
| 154 | * @param className the Name of the Class to check for |
---|
| 155 | * @returns the CommandClass if found, NULL otherwise |
---|
| 156 | */ |
---|
[5461] | 157 | const CommandChainClass* CommandChainClass::isRegistered(const char* className) |
---|
[5170] | 158 | { |
---|
[5461] | 159 | if (CommandChainClass::commandClassList == NULL) |
---|
[5170] | 160 | initCommandClassList(); |
---|
| 161 | |
---|
[5461] | 162 | tIterator<CommandChainClass>* iterator = CommandChainClass::commandClassList->getIterator(); |
---|
| 163 | CommandChainClass* elem = iterator->firstElement(); |
---|
[5170] | 164 | while(elem != NULL) |
---|
| 165 | { |
---|
| 166 | if (!strcmp(className, elem->className)) |
---|
| 167 | { |
---|
[5171] | 168 | if (elem->classID == CL_NULL) |
---|
| 169 | elem->classID = ClassList::StringToID(className); |
---|
| 170 | |
---|
[5170] | 171 | delete iterator; |
---|
| 172 | return elem; |
---|
| 173 | } |
---|
| 174 | elem = iterator->nextElement(); |
---|
| 175 | } |
---|
| 176 | delete iterator; |
---|
| 177 | return NULL; |
---|
| 178 | } |
---|
| 179 | |
---|
[5172] | 180 | /** |
---|
| 181 | * searches for a CommandClass |
---|
| 182 | * @param className the name of the CommandClass |
---|
| 183 | * @returns the CommandClass if found, or a new CommandClass if not |
---|
| 184 | */ |
---|
[5461] | 185 | CommandChainClass* CommandChainClass::getCommandClass(const char* className) |
---|
[5170] | 186 | { |
---|
[5461] | 187 | if (CommandChainClass::commandClassList == NULL) |
---|
[5170] | 188 | initCommandClassList(); |
---|
| 189 | |
---|
[5461] | 190 | tIterator<CommandChainClass>* iterator = CommandChainClass::commandClassList->getIterator(); |
---|
| 191 | CommandChainClass* elem = iterator->firstElement(); |
---|
[5170] | 192 | while(elem != NULL) |
---|
| 193 | { |
---|
| 194 | if (!strcmp(className, elem->className)) |
---|
| 195 | { |
---|
| 196 | delete iterator; |
---|
| 197 | return elem; |
---|
| 198 | } |
---|
| 199 | elem = iterator->nextElement(); |
---|
| 200 | } |
---|
| 201 | delete iterator; |
---|
[5461] | 202 | return new CommandChainClass(className); |
---|
[5170] | 203 | } |
---|
| 204 | |
---|
[5172] | 205 | /** |
---|
| 206 | * initializes the CommandList (if it is NULL) |
---|
| 207 | */ |
---|
[5461] | 208 | void CommandChainClass::initCommandClassList() |
---|
[5170] | 209 | { |
---|
[5461] | 210 | if (CommandChainClass::commandClassList == NULL) |
---|
[5170] | 211 | { |
---|
[5461] | 212 | CommandChainClass::commandClassList = new tList<CommandChainClass>; |
---|
| 213 | CommandChainStatic<CommandChainBase>::registerCommand("debug", "CommandChain", CommandChainBase::debug); |
---|
[5170] | 214 | } |
---|
| 215 | } |
---|
| 216 | |
---|
[5461] | 217 | void CommandChainClass::help(const char* className) |
---|
[5204] | 218 | { |
---|
| 219 | if (className == NULL) |
---|
| 220 | return; |
---|
[5461] | 221 | if (likely(CommandChainClass::commandClassList != NULL)) |
---|
[5204] | 222 | { |
---|
[5461] | 223 | tIterator<CommandChainClass>* itCL = CommandChainClass::commandClassList->getIterator(); |
---|
| 224 | CommandChainClass* elemCL = itCL->firstElement(); |
---|
[5204] | 225 | while(elemCL != NULL) |
---|
| 226 | { |
---|
| 227 | if (elemCL->className && !strcasecmp(className, elemCL->className)) |
---|
| 228 | { |
---|
| 229 | PRINT(0)("Class:'%s' registered %d commands: \n", elemCL->className, elemCL->commandList->getSize()); |
---|
[5461] | 230 | tIterator<CommandChainBase>* iterator = elemCL->commandList->getIterator(); |
---|
| 231 | const CommandChainBase* elem = iterator->firstElement(); |
---|
[5204] | 232 | while(elem != NULL) |
---|
| 233 | { |
---|
| 234 | PRINT(0)(" command:'%s' : params:%d: ", elem->getName(), elem->paramCount); |
---|
| 235 | for (unsigned int i = 0; i< elem->paramCount; i++) |
---|
[5461] | 236 | PRINT(0)("%s ", CommandChainBase::paramToString(elem->parameters[i])); |
---|
[5204] | 237 | if (elem->description != NULL) |
---|
| 238 | PRINT(0)("- %s", elem->description); |
---|
| 239 | PRINT(0)("\n"); |
---|
| 240 | elem = iterator->nextElement(); |
---|
| 241 | } |
---|
| 242 | delete iterator; |
---|
| 243 | |
---|
| 244 | delete itCL; |
---|
| 245 | return; |
---|
| 246 | } |
---|
| 247 | elemCL = itCL->nextElement(); |
---|
| 248 | } |
---|
| 249 | delete itCL; |
---|
| 250 | PRINTF(3)("Class %s not found in Command's classes\n", className); |
---|
| 251 | } |
---|
| 252 | else |
---|
| 253 | { |
---|
| 254 | PRINTF(1)("List of commandClasses does not exist"); |
---|
| 255 | } |
---|
| 256 | } |
---|
| 257 | |
---|
[5461] | 258 | tList<CommandChainClass>* CommandChainClass::commandClassList = NULL; |
---|
| 259 | tList<CommandChainAlias>* CommandChainClass::aliasList = NULL; |
---|
[5170] | 260 | |
---|
| 261 | /** |
---|
[5166] | 262 | * constructs and registers a new Command |
---|
| 263 | * @param commandName the name of the Command |
---|
| 264 | * @param className the name of the class to apply this command to |
---|
| 265 | * @param paramCount the count of parameters this command takes |
---|
| 266 | * @return self |
---|
| 267 | */ |
---|
[5461] | 268 | CommandChainBase::CommandChainBase(const char* commandName, const char* className, unsigned int paramCount, ...) |
---|
[3365] | 269 | { |
---|
[5461] | 270 | this->setClassID(CL_COMMAND_CHAIN, "CommandChain"); |
---|
[5141] | 271 | this->setName(commandName); |
---|
[5164] | 272 | this->description = NULL; |
---|
[5196] | 273 | this->alias = NULL; |
---|
[5141] | 274 | |
---|
[5161] | 275 | // this->classID = classID; |
---|
[5461] | 276 | this->shellClass = CommandChainClass::getCommandClass(className); //ClassList::IDToString(classID); |
---|
[5198] | 277 | if (this->shellClass != NULL) |
---|
| 278 | this->shellClass->commandList->add(this); |
---|
[5130] | 279 | // handling parameters, and storing them: |
---|
[5142] | 280 | if (paramCount > FUNCTOR_MAX_ARGUMENTS) |
---|
| 281 | paramCount = FUNCTOR_MAX_ARGUMENTS; |
---|
[5130] | 282 | this->paramCount = paramCount; |
---|
[5148] | 283 | this->parameters = new unsigned int[paramCount]; |
---|
[5130] | 284 | |
---|
[5148] | 285 | va_list parameterList; |
---|
| 286 | va_start(parameterList, paramCount); |
---|
| 287 | |
---|
[5130] | 288 | for (unsigned int i = 0; i < paramCount; i++) |
---|
[5142] | 289 | { |
---|
[5148] | 290 | this->parameters[i] = va_arg(parameterList, int); |
---|
[5130] | 291 | |
---|
[5146] | 292 | switch (this->parameters[i]) |
---|
[5142] | 293 | { |
---|
| 294 | case ParameterBool: |
---|
[5148] | 295 | this->defaultBools[i] = va_arg(parameterList, int); |
---|
[5142] | 296 | break; |
---|
| 297 | case ParameterChar: |
---|
| 298 | this->defaultStrings[i] = new char[2]; |
---|
[5148] | 299 | sprintf(this->defaultStrings[0], "%c", va_arg(parameterList, int)); |
---|
[5142] | 300 | break; |
---|
| 301 | case ParameterString: |
---|
[5148] | 302 | this->defaultStrings[i] = va_arg(parameterList, char*); |
---|
[5142] | 303 | break; |
---|
| 304 | case ParameterInt: |
---|
[5148] | 305 | this->defaultInts[i] = va_arg(parameterList, int); |
---|
[5142] | 306 | break; |
---|
| 307 | case ParameterUInt: |
---|
[5148] | 308 | this->defaultInts[i] = va_arg(parameterList, unsigned int); |
---|
[5142] | 309 | break; |
---|
| 310 | case ParameterFloat: |
---|
[5148] | 311 | this->defaultFloats[i] = va_arg(parameterList, double); |
---|
[5142] | 312 | break; |
---|
| 313 | case ParameterLong: |
---|
[5148] | 314 | this->defaultInts[i] = va_arg(parameterList, long); |
---|
[5142] | 315 | break; |
---|
| 316 | default: |
---|
| 317 | break; |
---|
| 318 | } |
---|
| 319 | } |
---|
[5068] | 320 | } |
---|
[4320] | 321 | |
---|
[5166] | 322 | /** |
---|
[5461] | 323 | * deconstructs a CommandChain |
---|
[5166] | 324 | * @return |
---|
| 325 | */ |
---|
[5461] | 326 | CommandChainBase::~CommandChainBase() |
---|
[5130] | 327 | { |
---|
| 328 | delete[] this->parameters; |
---|
[5461] | 329 | if (this->alias != NULL && CommandChainClass::aliasList != NULL) |
---|
[5196] | 330 | { |
---|
[5461] | 331 | CommandChainClass::aliasList->remove(this->alias); |
---|
[5196] | 332 | delete this->alias; |
---|
| 333 | } |
---|
[5130] | 334 | } |
---|
[1853] | 335 | |
---|
[5166] | 336 | /** |
---|
| 337 | * unregister an existing commandName |
---|
| 338 | * @param className the name of the Class the command belongs to. |
---|
| 339 | * @param commandName the name of the command itself |
---|
| 340 | */ |
---|
[5461] | 341 | void CommandChainBase::unregisterCommand(const char* commandName, const char* className) |
---|
[5165] | 342 | { |
---|
[5461] | 343 | if (CommandChainClass::commandClassList == NULL) |
---|
| 344 | CommandChainClass::initCommandClassList(); |
---|
[5171] | 345 | |
---|
[5461] | 346 | const CommandChainClass* checkClass = CommandChainClass::isRegistered(className); |
---|
[5171] | 347 | |
---|
[5172] | 348 | if (checkClass != NULL) |
---|
[5171] | 349 | { |
---|
[5461] | 350 | tIterator<CommandChainBase>* iterator = checkClass->commandList->getIterator(); |
---|
| 351 | CommandChainBase* elem = iterator->firstElement(); |
---|
[5171] | 352 | while(elem != NULL) |
---|
| 353 | { |
---|
| 354 | if (!strcmp(commandName, elem->getName())) |
---|
| 355 | { |
---|
| 356 | checkClass->commandList->remove(elem); |
---|
| 357 | delete elem; |
---|
| 358 | break; |
---|
| 359 | } |
---|
| 360 | elem = iterator->nextElement(); |
---|
| 361 | } |
---|
| 362 | delete iterator; |
---|
| 363 | |
---|
| 364 | if (checkClass->commandList->getSize() == 0) |
---|
| 365 | { |
---|
[5461] | 366 | CommandChainClass::commandClassList->remove(checkClass); |
---|
[5171] | 367 | delete checkClass; |
---|
| 368 | } |
---|
| 369 | } |
---|
[5165] | 370 | } |
---|
| 371 | |
---|
[5166] | 372 | /** |
---|
| 373 | * checks if a command has already been registered. |
---|
| 374 | * @param commandName the name of the Command |
---|
| 375 | * @param className the name of the Class the command should apply to. |
---|
| 376 | * @param paramCount how many arguments the Command takes |
---|
| 377 | * @returns true, if the command is registered/false otherwise |
---|
| 378 | * |
---|
| 379 | * This is used internally, to see, if we have multiple command subscriptions. |
---|
| 380 | * This is checked in the registerCommand-function. |
---|
| 381 | */ |
---|
[5461] | 382 | bool CommandChainBase::isRegistered(const char* commandName, const char* className, unsigned int paramCount, ...) |
---|
[5113] | 383 | { |
---|
[5461] | 384 | if (CommandChainClass::commandClassList == NULL) |
---|
[5072] | 385 | { |
---|
[5461] | 386 | CommandChainClass::initCommandClassList(); |
---|
[5113] | 387 | return false; |
---|
| 388 | } |
---|
[5105] | 389 | |
---|
[5461] | 390 | const CommandChainClass* checkClass = CommandChainClass::isRegistered(className); |
---|
[5170] | 391 | if (checkClass != NULL) |
---|
[5113] | 392 | { |
---|
[5461] | 393 | tIterator<CommandChainBase>* iterator = checkClass->commandList->getIterator(); |
---|
| 394 | CommandChainBase* elem = iterator->firstElement(); |
---|
[5170] | 395 | while(elem != NULL) |
---|
| 396 | { |
---|
| 397 | if (!strcmp(commandName, elem->getName())) |
---|
| 398 | { |
---|
| 399 | PRINTF(2)("Command already registered\n"); |
---|
| 400 | delete iterator; |
---|
| 401 | return true; |
---|
| 402 | } |
---|
| 403 | elem = iterator->nextElement(); |
---|
| 404 | } |
---|
| 405 | delete iterator; |
---|
| 406 | return false; |
---|
[5113] | 407 | } |
---|
[5170] | 408 | else |
---|
| 409 | return false; |
---|
[5113] | 410 | } |
---|
| 411 | |
---|
[5140] | 412 | |
---|
[5145] | 413 | /** |
---|
| 414 | * executes commands |
---|
| 415 | * @param executionString the string containing the following input |
---|
[5148] | 416 | * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]] |
---|
[5145] | 417 | * @return true on success, false otherwise. |
---|
| 418 | */ |
---|
[5200] | 419 | #include "stdlibincl.h" |
---|
[5461] | 420 | bool CommandChainBase::execute(const char* executionString) |
---|
[5135] | 421 | { |
---|
[5461] | 422 | if (CommandChainClass::commandClassList == NULL) |
---|
[5198] | 423 | return false; |
---|
| 424 | |
---|
[5203] | 425 | long classID = CL_NULL; //< the classID retrieved from the Class. |
---|
[5461] | 426 | CommandChainClass* commandClass = NULL; //< the command class this command applies to. |
---|
[5203] | 427 | tList<BaseObject>* objectList = NULL; //< the list of Objects stored in classID |
---|
| 428 | BaseObject* objectPointer = NULL; //< a pointer to th Object to Execute the command on |
---|
| 429 | bool emptyComplete = false; //< if the completion input is empty string. e.g "" |
---|
| 430 | unsigned int fktPos = 1; //< the position of the function (needed for finding it) |
---|
| 431 | // long completeType = SHELLC_NONE; //< the Type we'd like to complete. |
---|
[5198] | 432 | SubString inputSplits(executionString, true); |
---|
| 433 | |
---|
| 434 | if (inputSplits.getCount() == 0) |
---|
| 435 | return false; |
---|
| 436 | if (inputSplits.getCount() >= 1) |
---|
| 437 | { |
---|
[5200] | 438 | // CHECK FOR ALIAS |
---|
[5461] | 439 | if (CommandChainClass::aliasList != NULL) |
---|
[5198] | 440 | { |
---|
[5461] | 441 | tIterator<CommandChainAlias>* itAL = CommandChainClass::aliasList->getIterator(); |
---|
| 442 | CommandChainAlias* elemAL = itAL->firstElement(); |
---|
[5198] | 443 | while(elemAL != NULL) |
---|
| 444 | { |
---|
[5199] | 445 | if (elemAL->getName() != NULL && !strcmp(elemAL->getName(), inputSplits.getString(0)) && elemAL->getCommand() != NULL && |
---|
| 446 | elemAL->getCommand()->shellClass != NULL ) |
---|
[5198] | 447 | { |
---|
[5199] | 448 | objectList = ClassList::getList(elemAL->getCommand()->shellClass->getName()); |
---|
| 449 | if (objectList != NULL) |
---|
| 450 | { |
---|
[5204] | 451 | if (inputSplits.getCount() > 1) |
---|
| 452 | elemAL->getCommand()->executeCommand(objectList->firstElement(), executionString+inputSplits.getOffset(1)); |
---|
| 453 | else |
---|
| 454 | elemAL->getCommand()->executeCommand(objectList->firstElement(), ""); |
---|
[5200] | 455 | delete itAL; |
---|
[5199] | 456 | return true; |
---|
| 457 | } |
---|
[5198] | 458 | } |
---|
| 459 | elemAL = itAL->nextElement(); |
---|
| 460 | } |
---|
| 461 | delete itAL; |
---|
| 462 | } |
---|
[5203] | 463 | // looking for a Matching Class |
---|
[5461] | 464 | if (likely(CommandChainClass::commandClassList != NULL)) |
---|
[5203] | 465 | { |
---|
[5461] | 466 | tIterator<CommandChainClass>* itCL = CommandChainClass::commandClassList->getIterator(); |
---|
| 467 | CommandChainClass* elemCL = itCL->firstElement(); |
---|
[5203] | 468 | while(elemCL != NULL) |
---|
| 469 | { |
---|
| 470 | if (elemCL->getName() && !strcasecmp(inputSplits.getString(0), elemCL->getName())) |
---|
| 471 | { |
---|
| 472 | //elemCL->getName(); |
---|
| 473 | classID = ClassList::StringToID(elemCL->getName()); |
---|
| 474 | commandClass = elemCL; |
---|
| 475 | objectList = ClassList::getList(classID); |
---|
| 476 | break; |
---|
| 477 | } |
---|
| 478 | elemCL = itCL->nextElement(); |
---|
| 479 | } |
---|
| 480 | delete itCL; |
---|
| 481 | } |
---|
[5200] | 482 | |
---|
[5329] | 483 | if (commandClass != NULL && inputSplits.getCount() >= 2) |
---|
[5203] | 484 | { |
---|
[5329] | 485 | if (objectList != NULL) |
---|
[5203] | 486 | { |
---|
[5329] | 487 | // Checking for a Match in the Objects of classID (else take the first) |
---|
| 488 | tIterator<BaseObject>* itBO = objectList->getIterator(); |
---|
| 489 | BaseObject* enumBO = itBO->firstElement(); |
---|
| 490 | while(enumBO) |
---|
[5203] | 491 | { |
---|
[5329] | 492 | if (enumBO->getName() != NULL && !strcasecmp(enumBO->getName(), inputSplits.getString(1))) |
---|
| 493 | { |
---|
| 494 | objectPointer = enumBO; |
---|
| 495 | fktPos = 2; |
---|
| 496 | break; |
---|
| 497 | } |
---|
| 498 | enumBO = itBO->nextElement(); |
---|
| 499 | } |
---|
| 500 | delete itBO; |
---|
[5203] | 501 | |
---|
| 502 | // |
---|
[5329] | 503 | if (objectPointer == NULL) |
---|
| 504 | objectPointer = objectList->firstElement(); |
---|
| 505 | } |
---|
[5203] | 506 | // match a function. |
---|
[5329] | 507 | if (commandClass != NULL && (fktPos == 1 || (fktPos == 2 && inputSplits.getCount() >= 3))) |
---|
[5203] | 508 | { |
---|
[5461] | 509 | tIterator<CommandChainBase>* itCMD = commandClass->commandList->getIterator(); |
---|
| 510 | CommandChainBase* enumCMD = itCMD->firstElement(); |
---|
[5203] | 511 | while (enumCMD != NULL) |
---|
| 512 | { |
---|
| 513 | if (!strcmp(enumCMD->getName(), inputSplits.getString(fktPos))) |
---|
| 514 | { |
---|
[5461] | 515 | if (objectPointer == NULL && enumCMD->functorType == CommandChain_Objective) |
---|
[5329] | 516 | { |
---|
| 517 | delete itCMD; |
---|
| 518 | return false; |
---|
| 519 | } |
---|
[5203] | 520 | if (inputSplits.getCount() > fktPos+1) |
---|
| 521 | enumCMD->executeCommand(objectPointer, executionString+inputSplits.getOffset(fktPos +1)); |
---|
| 522 | else |
---|
| 523 | enumCMD->executeCommand(objectPointer, ""); |
---|
| 524 | delete itCMD; |
---|
| 525 | return true; |
---|
| 526 | } |
---|
| 527 | |
---|
| 528 | enumCMD = itCMD->nextElement(); |
---|
| 529 | } |
---|
| 530 | delete itCMD; |
---|
| 531 | } |
---|
| 532 | } |
---|
[5198] | 533 | } |
---|
[5135] | 534 | } |
---|
[5148] | 535 | |
---|
[5166] | 536 | /** |
---|
| 537 | * lets a command be described |
---|
| 538 | * @param description the description of the Given command |
---|
| 539 | */ |
---|
[5461] | 540 | CommandChainBase* CommandChainBase::describe(const char* description) |
---|
[5164] | 541 | { |
---|
| 542 | if (this == NULL) |
---|
| 543 | return NULL; |
---|
[5165] | 544 | else |
---|
| 545 | { |
---|
| 546 | this->description = description; |
---|
| 547 | return this; |
---|
| 548 | } |
---|
[5164] | 549 | } |
---|
| 550 | |
---|
[5197] | 551 | /** |
---|
| 552 | * adds an Alias to this Command |
---|
| 553 | * @param alias the name of the Alias to set |
---|
| 554 | * @returns itself |
---|
| 555 | */ |
---|
[5461] | 556 | CommandChainBase* CommandChainBase::setAlias(const char* alias) |
---|
[5195] | 557 | { |
---|
[5196] | 558 | if (this == NULL) |
---|
| 559 | return NULL; |
---|
| 560 | |
---|
| 561 | if (this->alias != NULL) |
---|
| 562 | { |
---|
| 563 | PRINTF(2)("not more than one Alias allowed for functions (%s::%s)\n", this->getName(), this->shellClass->getName()); |
---|
| 564 | } |
---|
| 565 | else |
---|
| 566 | { |
---|
[5461] | 567 | if (CommandChainClass::aliasList == NULL) |
---|
| 568 | CommandChainClass::aliasList = new tList<CommandChainAlias>; |
---|
[5196] | 569 | |
---|
[5461] | 570 | CommandChainAlias* aliasCMD = new CommandChainAlias(alias, this); |
---|
| 571 | CommandChainClass::aliasList->add(aliasCMD); |
---|
[5196] | 572 | this->alias = aliasCMD; |
---|
| 573 | } |
---|
| 574 | return this; |
---|
[5195] | 575 | } |
---|
| 576 | |
---|
[5166] | 577 | /** |
---|
[5207] | 578 | * sets default Values of the Commands |
---|
| 579 | * @param count how many default Values to set. |
---|
| 580 | * @param ... the default Values in order. They will be cast to the right type |
---|
| 581 | * @returns itself |
---|
| 582 | * |
---|
| 583 | * Be aware, that when you use this Function, you !!MUST!! match the input as |
---|
| 584 | * count, [EXACTLY THE SAME AS IF YOU WOULD CALL THE FUNCTION UP TO count ARGUMENTS] |
---|
| 585 | */ |
---|
[5461] | 586 | CommandChainBase* CommandChainBase::defaultValues(unsigned int count, ...) |
---|
[5207] | 587 | { |
---|
| 588 | if (this == NULL) |
---|
| 589 | return NULL; |
---|
| 590 | if (count == 0) |
---|
| 591 | return this; |
---|
| 592 | if (count > this->paramCount) |
---|
| 593 | count = this->paramCount; |
---|
| 594 | |
---|
| 595 | va_list defaultList; |
---|
| 596 | va_start(defaultList, count); |
---|
| 597 | |
---|
| 598 | for (unsigned int i = 0; i < count; i++) |
---|
| 599 | { |
---|
| 600 | switch (this->parameters[i]) |
---|
| 601 | { |
---|
| 602 | case ParameterBool: |
---|
| 603 | this->defaultBools[i] = va_arg(defaultList, int); |
---|
| 604 | break; |
---|
| 605 | case ParameterChar: |
---|
| 606 | this->defaultStrings[i] = new char[2]; |
---|
| 607 | sprintf(this->defaultStrings[0], "%c", va_arg(defaultList, int)); |
---|
| 608 | break; |
---|
| 609 | case ParameterString: |
---|
| 610 | this->defaultStrings[i] = va_arg(defaultList, char*); |
---|
| 611 | break; |
---|
| 612 | case ParameterInt: |
---|
| 613 | this->defaultInts[i] = va_arg(defaultList, int); |
---|
| 614 | break; |
---|
| 615 | case ParameterUInt: |
---|
| 616 | this->defaultInts[i] = va_arg(defaultList, unsigned int); |
---|
| 617 | break; |
---|
| 618 | case ParameterFloat: |
---|
| 619 | this->defaultFloats[i] = va_arg(defaultList, double); |
---|
| 620 | break; |
---|
| 621 | case ParameterLong: |
---|
| 622 | this->defaultInts[i] = va_arg(defaultList, long); |
---|
| 623 | break; |
---|
| 624 | default: |
---|
| 625 | break; |
---|
| 626 | } |
---|
| 627 | } |
---|
| 628 | |
---|
| 629 | return this; |
---|
| 630 | } |
---|
| 631 | |
---|
| 632 | /** |
---|
[5166] | 633 | * prints out nice information about the Shells Commands |
---|
| 634 | */ |
---|
[5461] | 635 | void CommandChainBase::debug() |
---|
[5148] | 636 | { |
---|
[5461] | 637 | if (CommandChainClass::commandClassList == NULL) |
---|
[5148] | 638 | { |
---|
[5171] | 639 | PRINT(0)("No Command registered.\n"); |
---|
[5148] | 640 | return; |
---|
| 641 | } |
---|
| 642 | |
---|
[5461] | 643 | tIterator<CommandChainClass>* iteratorCL = CommandChainClass::commandClassList->getIterator(); |
---|
| 644 | CommandChainClass* elemCL = iteratorCL->firstElement(); |
---|
[5170] | 645 | while(elemCL != NULL) |
---|
[5148] | 646 | { |
---|
[5171] | 647 | PRINT(0)("Class:'%s' registered %d commands: \n", elemCL->className, elemCL->commandList->getSize()); |
---|
[5461] | 648 | tIterator<CommandChainBase>* iterator = elemCL->commandList->getIterator(); |
---|
| 649 | const CommandChainBase* elem = iterator->firstElement(); |
---|
[5172] | 650 | while(elem != NULL) |
---|
[5170] | 651 | { |
---|
[5171] | 652 | PRINT(0)(" command:'%s' : params:%d: ", elem->getName(), elem->paramCount); |
---|
[5170] | 653 | for (unsigned int i = 0; i< elem->paramCount; i++) |
---|
[5461] | 654 | printf("%s ", CommandChainBase::paramToString(elem->parameters[i])); |
---|
[5170] | 655 | if (elem->description != NULL) |
---|
| 656 | printf("- %s", elem->description); |
---|
| 657 | printf("\n"); |
---|
[5148] | 658 | |
---|
[5170] | 659 | elem = iterator->nextElement(); |
---|
| 660 | } |
---|
| 661 | delete iterator; |
---|
| 662 | elemCL = iteratorCL->nextElement(); |
---|
[5148] | 663 | } |
---|
[5170] | 664 | delete iteratorCL; |
---|
[5148] | 665 | } |
---|
| 666 | |
---|
[5166] | 667 | /** |
---|
| 668 | * converts a Parameter to a String |
---|
| 669 | * @param parameter the Parameter we have. |
---|
| 670 | * @returns the Name of the Parameter at Hand |
---|
| 671 | */ |
---|
[5461] | 672 | const char* CommandChainBase::paramToString(long parameter) |
---|
[5148] | 673 | { |
---|
| 674 | switch (parameter) |
---|
| 675 | { |
---|
| 676 | case ParameterBool: |
---|
| 677 | return "BOOL"; |
---|
| 678 | break; |
---|
| 679 | case ParameterChar: |
---|
| 680 | return "CHAR"; |
---|
| 681 | break; |
---|
| 682 | case ParameterString: |
---|
| 683 | return "STRING"; |
---|
| 684 | break; |
---|
| 685 | case ParameterInt: |
---|
| 686 | return "INT"; |
---|
| 687 | break; |
---|
| 688 | case ParameterUInt: |
---|
| 689 | return "UINT"; |
---|
| 690 | break; |
---|
| 691 | case ParameterFloat: |
---|
| 692 | return "FLOAT"; |
---|
| 693 | break; |
---|
| 694 | case ParameterLong: |
---|
| 695 | return "LONG"; |
---|
| 696 | break; |
---|
| 697 | default: |
---|
| 698 | return "NULL"; |
---|
| 699 | break; |
---|
| 700 | } |
---|
| 701 | } |
---|