[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 | /** |
---|
[5170] | 32 | * creates a new ShellCommandClass |
---|
| 33 | * @param className the Name of the command-class to create |
---|
| 34 | */ |
---|
| 35 | ShellCommandClass::ShellCommandClass(const char* className) |
---|
| 36 | { |
---|
[5188] | 37 | this->setClassID(CL_SHELL_COMMAND_CLASS, "ShellCommandClass"); |
---|
| 38 | this->setName(className); |
---|
| 39 | |
---|
[5170] | 40 | this->className = className; |
---|
| 41 | this->classID = CL_NULL; |
---|
| 42 | this->commandList = new tList<ShellCommandBase>; |
---|
| 43 | |
---|
| 44 | ShellCommandClass::commandClassList->add(this); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | * destructs the shellCommandClass again |
---|
| 49 | */ |
---|
| 50 | ShellCommandClass::~ShellCommandClass() |
---|
| 51 | { |
---|
| 52 | tIterator<ShellCommandBase>* iterator = this->commandList->getIterator(); |
---|
| 53 | ShellCommandBase* elem = iterator->firstElement(); |
---|
| 54 | while(elem != NULL) |
---|
| 55 | { |
---|
| 56 | delete elem; |
---|
| 57 | elem = iterator->nextElement(); |
---|
| 58 | } |
---|
| 59 | delete iterator; |
---|
| 60 | delete this->commandList; |
---|
| 61 | } |
---|
| 62 | |
---|
[5171] | 63 | /** |
---|
| 64 | * unregisters all Commands that exist |
---|
| 65 | */ |
---|
| 66 | void ShellCommandClass::unregisterAllCommands() |
---|
| 67 | { |
---|
| 68 | tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator(); |
---|
| 69 | ShellCommandClass* elem = iterator->firstElement(); |
---|
| 70 | while(elem != NULL) |
---|
| 71 | { |
---|
| 72 | delete elem; |
---|
| 73 | |
---|
| 74 | elem = iterator->nextElement(); |
---|
| 75 | } |
---|
| 76 | delete iterator; |
---|
| 77 | |
---|
| 78 | delete ShellCommandClass::commandClassList; |
---|
| 79 | ShellCommandClass::commandClassList = NULL; |
---|
| 80 | } |
---|
| 81 | |
---|
[5170] | 82 | const ShellCommandClass* ShellCommandClass::isRegistered(const char* className) |
---|
| 83 | { |
---|
| 84 | if (ShellCommandClass::commandClassList == NULL) |
---|
| 85 | initCommandClassList(); |
---|
| 86 | |
---|
| 87 | tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator(); |
---|
| 88 | ShellCommandClass* elem = iterator->firstElement(); |
---|
| 89 | while(elem != NULL) |
---|
| 90 | { |
---|
| 91 | if (!strcmp(className, elem->className)) |
---|
| 92 | { |
---|
[5171] | 93 | if (elem->classID == CL_NULL) |
---|
| 94 | elem->classID = ClassList::StringToID(className); |
---|
| 95 | |
---|
[5170] | 96 | delete iterator; |
---|
| 97 | return elem; |
---|
| 98 | } |
---|
| 99 | elem = iterator->nextElement(); |
---|
| 100 | } |
---|
| 101 | delete iterator; |
---|
| 102 | return NULL; |
---|
| 103 | } |
---|
| 104 | |
---|
[5172] | 105 | /** |
---|
| 106 | * searches for a CommandClass |
---|
| 107 | * @param className the name of the CommandClass |
---|
| 108 | * @returns the CommandClass if found, or a new CommandClass if not |
---|
| 109 | */ |
---|
[5170] | 110 | ShellCommandClass* ShellCommandClass::getCommandClass(const char* className) |
---|
| 111 | { |
---|
| 112 | if (ShellCommandClass::commandClassList == NULL) |
---|
| 113 | initCommandClassList(); |
---|
| 114 | |
---|
| 115 | tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator(); |
---|
| 116 | ShellCommandClass* elem = iterator->firstElement(); |
---|
| 117 | while(elem != NULL) |
---|
| 118 | { |
---|
| 119 | if (!strcmp(className, elem->className)) |
---|
| 120 | { |
---|
| 121 | delete iterator; |
---|
| 122 | return elem; |
---|
| 123 | } |
---|
| 124 | elem = iterator->nextElement(); |
---|
| 125 | } |
---|
| 126 | delete iterator; |
---|
| 127 | return new ShellCommandClass(className); |
---|
| 128 | } |
---|
| 129 | |
---|
[5172] | 130 | /** |
---|
| 131 | * initializes the CommandList (if it is NULL) |
---|
| 132 | */ |
---|
[5170] | 133 | void ShellCommandClass::initCommandClassList() |
---|
| 134 | { |
---|
| 135 | if (ShellCommandClass::commandClassList == NULL) |
---|
| 136 | { |
---|
| 137 | ShellCommandClass::commandClassList = new tList<ShellCommandClass>; |
---|
| 138 | ShellCommand<ShellCommandBase>::registerCommand("debug", "ShellCommand", &ShellCommandBase::debugDyn); |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | tList<ShellCommandClass>* ShellCommandClass::commandClassList = NULL; |
---|
| 143 | |
---|
| 144 | /** |
---|
[5166] | 145 | * constructs and registers a new Command |
---|
| 146 | * @param commandName the name of the Command |
---|
| 147 | * @param className the name of the class to apply this command to |
---|
| 148 | * @param paramCount the count of parameters this command takes |
---|
| 149 | * @return self |
---|
| 150 | */ |
---|
[5161] | 151 | ShellCommandBase::ShellCommandBase(const char* commandName, const char* className, unsigned int paramCount, ...) |
---|
[3365] | 152 | { |
---|
[5141] | 153 | this->setClassID(CL_SHELL_COMMAND, "ShellCommand"); |
---|
| 154 | this->setName(commandName); |
---|
[5164] | 155 | this->description = NULL; |
---|
[5141] | 156 | |
---|
[5161] | 157 | // this->classID = classID; |
---|
[5170] | 158 | ShellCommandClass::getCommandClass(className)->commandList->add(this); //ClassList::IDToString(classID); |
---|
[5141] | 159 | |
---|
[5130] | 160 | // handling parameters, and storing them: |
---|
[5142] | 161 | if (paramCount > FUNCTOR_MAX_ARGUMENTS) |
---|
| 162 | paramCount = FUNCTOR_MAX_ARGUMENTS; |
---|
[5130] | 163 | this->paramCount = paramCount; |
---|
[5148] | 164 | this->parameters = new unsigned int[paramCount]; |
---|
[5130] | 165 | |
---|
[5148] | 166 | va_list parameterList; |
---|
| 167 | va_start(parameterList, paramCount); |
---|
| 168 | |
---|
[5130] | 169 | for (unsigned int i = 0; i < paramCount; i++) |
---|
[5142] | 170 | { |
---|
[5148] | 171 | this->parameters[i] = va_arg(parameterList, int); |
---|
[5130] | 172 | |
---|
[5146] | 173 | switch (this->parameters[i]) |
---|
[5142] | 174 | { |
---|
| 175 | case ParameterBool: |
---|
[5148] | 176 | this->defaultBools[i] = va_arg(parameterList, int); |
---|
[5142] | 177 | break; |
---|
| 178 | case ParameterChar: |
---|
| 179 | this->defaultStrings[i] = new char[2]; |
---|
[5148] | 180 | sprintf(this->defaultStrings[0], "%c", va_arg(parameterList, int)); |
---|
[5142] | 181 | break; |
---|
| 182 | case ParameterString: |
---|
[5148] | 183 | this->defaultStrings[i] = va_arg(parameterList, char*); |
---|
[5142] | 184 | break; |
---|
| 185 | case ParameterInt: |
---|
[5148] | 186 | this->defaultInts[i] = va_arg(parameterList, int); |
---|
[5142] | 187 | break; |
---|
| 188 | case ParameterUInt: |
---|
[5148] | 189 | this->defaultInts[i] = va_arg(parameterList, unsigned int); |
---|
[5142] | 190 | break; |
---|
| 191 | case ParameterFloat: |
---|
[5148] | 192 | this->defaultFloats[i] = va_arg(parameterList, double); |
---|
[5142] | 193 | break; |
---|
| 194 | case ParameterLong: |
---|
[5148] | 195 | this->defaultInts[i] = va_arg(parameterList, long); |
---|
[5142] | 196 | break; |
---|
| 197 | default: |
---|
| 198 | break; |
---|
| 199 | } |
---|
| 200 | } |
---|
[5068] | 201 | } |
---|
[4320] | 202 | |
---|
[5166] | 203 | /** |
---|
| 204 | * deconstructs a ShellCommand |
---|
| 205 | * @return |
---|
| 206 | */ |
---|
[5130] | 207 | ShellCommandBase::~ShellCommandBase() |
---|
| 208 | { |
---|
| 209 | delete[] this->parameters; |
---|
| 210 | } |
---|
[1853] | 211 | |
---|
[5166] | 212 | /** |
---|
| 213 | * unregister an existing commandName |
---|
| 214 | * @param className the name of the Class the command belongs to. |
---|
| 215 | * @param commandName the name of the command itself |
---|
| 216 | * |
---|
| 217 | * @todo implement |
---|
| 218 | */ |
---|
| 219 | void ShellCommandBase::unregisterCommand(const char* commandName, const char* className) |
---|
[5165] | 220 | { |
---|
[5171] | 221 | if (ShellCommandClass::commandClassList == NULL) |
---|
| 222 | ShellCommandClass::initCommandClassList(); |
---|
| 223 | |
---|
[5172] | 224 | const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className); |
---|
[5171] | 225 | |
---|
[5172] | 226 | if (checkClass != NULL) |
---|
[5171] | 227 | { |
---|
| 228 | tIterator<ShellCommandBase>* iterator = checkClass->commandList->getIterator(); |
---|
| 229 | ShellCommandBase* elem = iterator->firstElement(); |
---|
| 230 | while(elem != NULL) |
---|
| 231 | { |
---|
| 232 | if (!strcmp(commandName, elem->getName())) |
---|
| 233 | { |
---|
| 234 | checkClass->commandList->remove(elem); |
---|
| 235 | delete elem; |
---|
| 236 | break; |
---|
| 237 | } |
---|
| 238 | elem = iterator->nextElement(); |
---|
| 239 | } |
---|
| 240 | delete iterator; |
---|
| 241 | |
---|
| 242 | if (checkClass->commandList->getSize() == 0) |
---|
| 243 | { |
---|
| 244 | ShellCommandClass::commandClassList->remove(checkClass); |
---|
| 245 | delete checkClass; |
---|
| 246 | } |
---|
| 247 | } |
---|
[5165] | 248 | } |
---|
| 249 | |
---|
[5166] | 250 | /** |
---|
| 251 | * checks if a command has already been registered. |
---|
| 252 | * @param commandName the name of the Command |
---|
| 253 | * @param className the name of the Class the command should apply to. |
---|
| 254 | * @param paramCount how many arguments the Command takes |
---|
| 255 | * @returns true, if the command is registered/false otherwise |
---|
| 256 | * |
---|
| 257 | * This is used internally, to see, if we have multiple command subscriptions. |
---|
| 258 | * This is checked in the registerCommand-function. |
---|
| 259 | */ |
---|
[5161] | 260 | bool ShellCommandBase::isRegistered(const char* commandName, const char* className, unsigned int paramCount, ...) |
---|
[5113] | 261 | { |
---|
[5170] | 262 | if (ShellCommandClass::commandClassList == NULL) |
---|
[5072] | 263 | { |
---|
[5170] | 264 | ShellCommandClass::initCommandClassList(); |
---|
[5113] | 265 | return false; |
---|
| 266 | } |
---|
[5105] | 267 | |
---|
[5170] | 268 | const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className); |
---|
| 269 | if (checkClass != NULL) |
---|
[5113] | 270 | { |
---|
[5170] | 271 | tIterator<ShellCommandBase>* iterator = checkClass->commandList->getIterator(); |
---|
| 272 | ShellCommandBase* elem = iterator->firstElement(); |
---|
| 273 | while(elem != NULL) |
---|
| 274 | { |
---|
| 275 | if (!strcmp(commandName, elem->getName())) |
---|
| 276 | { |
---|
| 277 | PRINTF(2)("Command already registered\n"); |
---|
| 278 | delete iterator; |
---|
| 279 | return true; |
---|
| 280 | } |
---|
| 281 | elem = iterator->nextElement(); |
---|
| 282 | } |
---|
| 283 | delete iterator; |
---|
| 284 | return false; |
---|
[5113] | 285 | } |
---|
[5170] | 286 | else |
---|
| 287 | return false; |
---|
[5113] | 288 | } |
---|
| 289 | |
---|
[5140] | 290 | |
---|
[5145] | 291 | /** |
---|
| 292 | * executes commands |
---|
| 293 | * @param executionString the string containing the following input |
---|
[5148] | 294 | * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]] |
---|
[5145] | 295 | * @return true on success, false otherwise. |
---|
| 296 | */ |
---|
[5135] | 297 | bool ShellCommandBase::execute(const char* executionString) |
---|
| 298 | { |
---|
[5170] | 299 | // if (ShellCommandBase::commandList == NULL) |
---|
| 300 | // return false; |
---|
| 301 | // |
---|
| 302 | // tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator(); |
---|
| 303 | // ShellCommandBase* elem = iterator->firstElement(); |
---|
| 304 | // while(elem != NULL) |
---|
| 305 | // { |
---|
| 306 | // printf("%s::%s\n", elem->className, elem->getName()); |
---|
| 307 | // if (!strncasecmp (executionString, elem->className, strlen(elem->className)) && |
---|
| 308 | // (*(executionString+strlen(elem->className)) == ' ' || |
---|
| 309 | // *(executionString+strlen(elem->className)) == ':' )) |
---|
| 310 | // { |
---|
| 311 | // const char* commandBegin = executionString + strlen(elem->className); |
---|
| 312 | // |
---|
| 313 | // PRINTF(4)("Class %s matches\n", elem->className); |
---|
| 314 | // BaseObject* objectPointer = NULL; |
---|
| 315 | // if (ClassList::StringToID(elem->className) & CL_MASK_SINGLETON == CL_MASK_SINGLETON) |
---|
| 316 | // { |
---|
| 317 | // while(*commandBegin == ' ') |
---|
| 318 | // commandBegin++; |
---|
| 319 | // if (strncmp (commandBegin, elem->getName(), strlen(elem->getName())) || |
---|
| 320 | // *(commandBegin + strlen(elem->getName())) != ' ' && |
---|
| 321 | // *(commandBegin + strlen(elem->getName())) != '\0') |
---|
| 322 | // { |
---|
| 323 | // elem = iterator->nextElement(); |
---|
| 324 | // continue; |
---|
| 325 | // } |
---|
| 326 | // PRINTF(4)("Command %s matches\n", elem->getName()); |
---|
| 327 | // // getting singleton-reference |
---|
| 328 | // tList<BaseObject>* list = ClassList::getList(elem->className); |
---|
| 329 | // if (list != NULL) |
---|
| 330 | // objectPointer = list->firstElement(); |
---|
| 331 | // } |
---|
| 332 | // else |
---|
| 333 | // { |
---|
| 334 | // // checking for the Object |
---|
| 335 | // while(*commandBegin == ' ') |
---|
| 336 | // commandBegin++; |
---|
| 337 | // tList<BaseObject>* list = ClassList::getList(elem->className); |
---|
| 338 | // if (list == NULL) |
---|
| 339 | // break; |
---|
| 340 | // tIterator<BaseObject>* iterBO = list->getIterator(); |
---|
| 341 | // BaseObject* enumBO = iterBO->firstElement(); |
---|
| 342 | // while(enumBO != NULL) |
---|
| 343 | // { |
---|
| 344 | // if(!strncmp(commandBegin, enumBO->getName(), strlen(enumBO->getName()))) |
---|
| 345 | // { |
---|
| 346 | // PRINTF(4)("Object %s matches\n", enumBO->getName()); |
---|
| 347 | // objectPointer = enumBO; |
---|
| 348 | // break; |
---|
| 349 | // } |
---|
| 350 | // enumBO = iterBO->nextElement(); |
---|
| 351 | // } |
---|
| 352 | // delete iterBO; |
---|
| 353 | // |
---|
| 354 | // // break on no object Found. We cannot operate on Classes, but on Objects |
---|
| 355 | // if (objectPointer == NULL) |
---|
| 356 | // break; |
---|
| 357 | // commandBegin = commandBegin + strlen(objectPointer->getName()); |
---|
| 358 | // while(*commandBegin == ' ') |
---|
| 359 | // commandBegin++; |
---|
| 360 | // // checking for the requested function. |
---|
| 361 | // if (strncmp (commandBegin, elem->getName(), strlen(elem->getName()))) |
---|
| 362 | // { |
---|
| 363 | // elem = iterator->nextElement(); |
---|
| 364 | // continue; |
---|
| 365 | // } |
---|
| 366 | // PRINTF(4)("Function '%s' found\n", commandBegin); |
---|
| 367 | // } |
---|
| 368 | // const char* paramBegin = strchr(commandBegin, ' '); |
---|
| 369 | // if (paramBegin == NULL) |
---|
| 370 | // paramBegin = commandBegin + strlen(elem->getName()); |
---|
| 371 | // while (*paramBegin == ' ') |
---|
| 372 | // paramBegin++; |
---|
| 373 | // |
---|
| 374 | // PRINTF(3)("Parameters to Pass: %s\n", paramBegin); |
---|
| 375 | // if (objectPointer != NULL && paramBegin != NULL) |
---|
| 376 | // { |
---|
| 377 | // elem->executeCommand(objectPointer, paramBegin); |
---|
| 378 | // delete iterator; |
---|
| 379 | // return true; |
---|
| 380 | // } |
---|
| 381 | // } |
---|
| 382 | // elem = iterator->nextElement(); |
---|
| 383 | // } |
---|
| 384 | // delete iterator; |
---|
| 385 | // return true; |
---|
[5135] | 386 | } |
---|
[5148] | 387 | |
---|
[5166] | 388 | /** |
---|
| 389 | * lets a command be described |
---|
| 390 | * @param description the description of the Given command |
---|
| 391 | */ |
---|
[5164] | 392 | ShellCommandBase* ShellCommandBase::describe(const char* description) |
---|
| 393 | { |
---|
| 394 | if (this == NULL) |
---|
| 395 | return NULL; |
---|
[5165] | 396 | else |
---|
| 397 | { |
---|
| 398 | this->description = description; |
---|
| 399 | return this; |
---|
| 400 | } |
---|
[5164] | 401 | } |
---|
| 402 | |
---|
[5166] | 403 | /** |
---|
| 404 | * see ShellCommandBase::debug() |
---|
| 405 | */ |
---|
[5161] | 406 | void ShellCommandBase::debugDyn() |
---|
| 407 | { |
---|
| 408 | this->debug(); |
---|
| 409 | } |
---|
[5148] | 410 | |
---|
[5166] | 411 | /** |
---|
| 412 | * prints out nice information about the Shells Commands |
---|
| 413 | */ |
---|
[5148] | 414 | void ShellCommandBase::debug() |
---|
| 415 | { |
---|
[5170] | 416 | if (ShellCommandClass::commandClassList == NULL) |
---|
[5148] | 417 | { |
---|
[5171] | 418 | PRINT(0)("No Command registered.\n"); |
---|
[5148] | 419 | return; |
---|
| 420 | } |
---|
| 421 | |
---|
[5170] | 422 | tIterator<ShellCommandClass>* iteratorCL = ShellCommandClass::commandClassList->getIterator(); |
---|
| 423 | ShellCommandClass* elemCL = iteratorCL->firstElement(); |
---|
| 424 | while(elemCL != NULL) |
---|
[5148] | 425 | { |
---|
[5171] | 426 | PRINT(0)("Class:'%s' registered %d commands: \n", elemCL->className, elemCL->commandList->getSize()); |
---|
[5170] | 427 | tIterator<ShellCommandBase>* iterator = elemCL->commandList->getIterator(); |
---|
| 428 | const ShellCommandBase* elem = iterator->firstElement(); |
---|
[5172] | 429 | while(elem != NULL) |
---|
[5170] | 430 | { |
---|
[5171] | 431 | PRINT(0)(" command:'%s' : params:%d: ", elem->getName(), elem->paramCount); |
---|
[5170] | 432 | for (unsigned int i = 0; i< elem->paramCount; i++) |
---|
| 433 | printf("%s ", ShellCommandBase::paramToString(elem->parameters[i])); |
---|
| 434 | if (elem->description != NULL) |
---|
| 435 | printf("- %s", elem->description); |
---|
| 436 | printf("\n"); |
---|
[5148] | 437 | |
---|
[5170] | 438 | elem = iterator->nextElement(); |
---|
| 439 | } |
---|
| 440 | delete iterator; |
---|
| 441 | elemCL = iteratorCL->nextElement(); |
---|
[5148] | 442 | } |
---|
[5170] | 443 | delete iteratorCL; |
---|
[5148] | 444 | } |
---|
| 445 | |
---|
[5166] | 446 | /** |
---|
| 447 | * converts a Parameter to a String |
---|
| 448 | * @param parameter the Parameter we have. |
---|
| 449 | * @returns the Name of the Parameter at Hand |
---|
| 450 | */ |
---|
[5148] | 451 | const char* ShellCommandBase::paramToString(long parameter) |
---|
| 452 | { |
---|
| 453 | switch (parameter) |
---|
| 454 | { |
---|
| 455 | case ParameterBool: |
---|
| 456 | return "BOOL"; |
---|
| 457 | break; |
---|
| 458 | case ParameterChar: |
---|
| 459 | return "CHAR"; |
---|
| 460 | break; |
---|
| 461 | case ParameterString: |
---|
| 462 | return "STRING"; |
---|
| 463 | break; |
---|
| 464 | case ParameterInt: |
---|
| 465 | return "INT"; |
---|
| 466 | break; |
---|
| 467 | case ParameterUInt: |
---|
| 468 | return "UINT"; |
---|
| 469 | break; |
---|
| 470 | case ParameterFloat: |
---|
| 471 | return "FLOAT"; |
---|
| 472 | break; |
---|
| 473 | case ParameterLong: |
---|
| 474 | return "LONG"; |
---|
| 475 | break; |
---|
| 476 | default: |
---|
| 477 | return "NULL"; |
---|
| 478 | break; |
---|
| 479 | } |
---|
| 480 | } |
---|