[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" |
---|
[5639] | 19 | #include "shell_command_class.h" |
---|
[1853] | 20 | |
---|
[5072] | 21 | #include "list.h" |
---|
[5129] | 22 | #include "debug.h" |
---|
[5113] | 23 | #include "class_list.h" |
---|
| 24 | |
---|
| 25 | #include "key_names.h" |
---|
[5075] | 26 | #include <stdarg.h> |
---|
| 27 | #include <stdio.h> |
---|
[5174] | 28 | #include <string.h> |
---|
[5075] | 29 | |
---|
[1856] | 30 | using namespace std; |
---|
[1853] | 31 | |
---|
[5166] | 32 | /** |
---|
| 33 | * constructs and registers a new Command |
---|
| 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 | */ |
---|
[5641] | 38 | ShellCommand::ShellCommand(const char* commandName, const char* className, const Executor& executor) |
---|
[3365] | 39 | { |
---|
[5141] | 40 | this->setClassID(CL_SHELL_COMMAND, "ShellCommand"); |
---|
| 41 | this->setName(commandName); |
---|
[5164] | 42 | this->description = NULL; |
---|
[5196] | 43 | this->alias = NULL; |
---|
[5642] | 44 | this->executor = executor.clone(); |
---|
[5141] | 45 | |
---|
[5161] | 46 | // this->classID = classID; |
---|
[5198] | 47 | this->shellClass = ShellCommandClass::getCommandClass(className); //ClassList::IDToString(classID); |
---|
| 48 | if (this->shellClass != NULL) |
---|
| 49 | this->shellClass->commandList->add(this); |
---|
[5068] | 50 | } |
---|
[4320] | 51 | |
---|
[5166] | 52 | /** |
---|
| 53 | * deconstructs a ShellCommand |
---|
| 54 | */ |
---|
[5636] | 55 | ShellCommand::~ShellCommand() |
---|
[5130] | 56 | { |
---|
[5196] | 57 | if (this->alias != NULL && ShellCommandClass::aliasList != NULL) |
---|
| 58 | { |
---|
| 59 | ShellCommandClass::aliasList->remove(this->alias); |
---|
| 60 | delete this->alias; |
---|
| 61 | } |
---|
[5641] | 62 | delete this->executor; |
---|
[5130] | 63 | } |
---|
[1853] | 64 | |
---|
[5166] | 65 | /** |
---|
[5636] | 66 | * registers a new ShellCommand |
---|
| 67 | */ |
---|
[5641] | 68 | ShellCommand* ShellCommand::registerCommand(const char* commandName, const char* className, const Executor& executor) |
---|
[5636] | 69 | { |
---|
[5637] | 70 | if (ShellCommand::isRegistered(commandName, className, executor)) |
---|
| 71 | return NULL; |
---|
| 72 | else |
---|
| 73 | return new ShellCommand(commandName, className, executor); |
---|
[5636] | 74 | |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | /** |
---|
[5166] | 81 | * unregister an existing commandName |
---|
| 82 | * @param className the name of the Class the command belongs to. |
---|
| 83 | * @param commandName the name of the command itself |
---|
| 84 | */ |
---|
[5636] | 85 | void ShellCommand::unregisterCommand(const char* commandName, const char* className) |
---|
[5165] | 86 | { |
---|
[5171] | 87 | if (ShellCommandClass::commandClassList == NULL) |
---|
| 88 | ShellCommandClass::initCommandClassList(); |
---|
| 89 | |
---|
[5172] | 90 | const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className); |
---|
[5171] | 91 | |
---|
[5172] | 92 | if (checkClass != NULL) |
---|
[5171] | 93 | { |
---|
[5636] | 94 | tIterator<ShellCommand>* iterator = checkClass->commandList->getIterator(); |
---|
| 95 | ShellCommand* elem = iterator->firstElement(); |
---|
[5171] | 96 | while(elem != NULL) |
---|
| 97 | { |
---|
| 98 | if (!strcmp(commandName, elem->getName())) |
---|
| 99 | { |
---|
| 100 | checkClass->commandList->remove(elem); |
---|
| 101 | delete elem; |
---|
| 102 | break; |
---|
| 103 | } |
---|
| 104 | elem = iterator->nextElement(); |
---|
| 105 | } |
---|
| 106 | delete iterator; |
---|
| 107 | |
---|
| 108 | if (checkClass->commandList->getSize() == 0) |
---|
| 109 | { |
---|
| 110 | ShellCommandClass::commandClassList->remove(checkClass); |
---|
| 111 | delete checkClass; |
---|
| 112 | } |
---|
| 113 | } |
---|
[5165] | 114 | } |
---|
| 115 | |
---|
[5166] | 116 | /** |
---|
| 117 | * checks if a command has already been registered. |
---|
| 118 | * @param commandName the name of the Command |
---|
| 119 | * @param className the name of the Class the command should apply to. |
---|
| 120 | * @param paramCount how many arguments the Command takes |
---|
| 121 | * @returns true, if the command is registered/false otherwise |
---|
| 122 | * |
---|
| 123 | * This is used internally, to see, if we have multiple command subscriptions. |
---|
| 124 | * This is checked in the registerCommand-function. |
---|
| 125 | */ |
---|
[5641] | 126 | bool ShellCommand::isRegistered(const char* commandName, const char* className, const Executor& executor) |
---|
[5113] | 127 | { |
---|
[5170] | 128 | if (ShellCommandClass::commandClassList == NULL) |
---|
[5072] | 129 | { |
---|
[5170] | 130 | ShellCommandClass::initCommandClassList(); |
---|
[5113] | 131 | return false; |
---|
| 132 | } |
---|
[5105] | 133 | |
---|
[5170] | 134 | const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className); |
---|
| 135 | if (checkClass != NULL) |
---|
[5113] | 136 | { |
---|
[5636] | 137 | tIterator<ShellCommand>* iterator = checkClass->commandList->getIterator(); |
---|
| 138 | ShellCommand* elem = iterator->firstElement(); |
---|
[5170] | 139 | while(elem != NULL) |
---|
| 140 | { |
---|
| 141 | if (!strcmp(commandName, elem->getName())) |
---|
| 142 | { |
---|
| 143 | PRINTF(2)("Command already registered\n"); |
---|
| 144 | delete iterator; |
---|
| 145 | return true; |
---|
| 146 | } |
---|
| 147 | elem = iterator->nextElement(); |
---|
| 148 | } |
---|
| 149 | delete iterator; |
---|
| 150 | return false; |
---|
[5113] | 151 | } |
---|
[5170] | 152 | else |
---|
| 153 | return false; |
---|
[5113] | 154 | } |
---|
| 155 | |
---|
[5140] | 156 | |
---|
[5145] | 157 | /** |
---|
| 158 | * executes commands |
---|
| 159 | * @param executionString the string containing the following input |
---|
[5148] | 160 | * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]] |
---|
[5145] | 161 | * @return true on success, false otherwise. |
---|
| 162 | */ |
---|
[5636] | 163 | bool ShellCommand::execute(const char* executionString) |
---|
[5135] | 164 | { |
---|
[5198] | 165 | if (ShellCommandClass::commandClassList == NULL) |
---|
| 166 | return false; |
---|
| 167 | |
---|
[5203] | 168 | long classID = CL_NULL; //< the classID retrieved from the Class. |
---|
| 169 | ShellCommandClass* commandClass = NULL; //< the command class this command applies to. |
---|
| 170 | tList<BaseObject>* objectList = NULL; //< the list of Objects stored in classID |
---|
| 171 | BaseObject* objectPointer = NULL; //< a pointer to th Object to Execute the command on |
---|
| 172 | bool emptyComplete = false; //< if the completion input is empty string. e.g "" |
---|
| 173 | unsigned int fktPos = 1; //< the position of the function (needed for finding it) |
---|
| 174 | // long completeType = SHELLC_NONE; //< the Type we'd like to complete. |
---|
[5198] | 175 | SubString inputSplits(executionString, true); |
---|
| 176 | |
---|
| 177 | if (inputSplits.getCount() == 0) |
---|
| 178 | return false; |
---|
| 179 | if (inputSplits.getCount() >= 1) |
---|
| 180 | { |
---|
[5200] | 181 | // CHECK FOR ALIAS |
---|
[5198] | 182 | if (ShellCommandClass::aliasList != NULL) |
---|
| 183 | { |
---|
| 184 | tIterator<ShellCommandAlias>* itAL = ShellCommandClass::aliasList->getIterator(); |
---|
| 185 | ShellCommandAlias* elemAL = itAL->firstElement(); |
---|
| 186 | while(elemAL != NULL) |
---|
| 187 | { |
---|
[5199] | 188 | if (elemAL->getName() != NULL && !strcmp(elemAL->getName(), inputSplits.getString(0)) && elemAL->getCommand() != NULL && |
---|
| 189 | elemAL->getCommand()->shellClass != NULL ) |
---|
[5198] | 190 | { |
---|
[5199] | 191 | objectList = ClassList::getList(elemAL->getCommand()->shellClass->getName()); |
---|
| 192 | if (objectList != NULL) |
---|
| 193 | { |
---|
[5204] | 194 | if (inputSplits.getCount() > 1) |
---|
[5637] | 195 | elemAL->getCommand()->executor->execute(objectList->firstElement(), executionString+inputSplits.getOffset(1)); |
---|
[5204] | 196 | else |
---|
[5637] | 197 | elemAL->getCommand()->executor->execute(objectList->firstElement(), ""); |
---|
[5200] | 198 | delete itAL; |
---|
[5199] | 199 | return true; |
---|
| 200 | } |
---|
[5198] | 201 | } |
---|
| 202 | elemAL = itAL->nextElement(); |
---|
| 203 | } |
---|
| 204 | delete itAL; |
---|
| 205 | } |
---|
[5203] | 206 | // looking for a Matching Class |
---|
| 207 | if (likely(ShellCommandClass::commandClassList != NULL)) |
---|
| 208 | { |
---|
| 209 | tIterator<ShellCommandClass>* itCL = ShellCommandClass::commandClassList->getIterator(); |
---|
| 210 | ShellCommandClass* elemCL = itCL->firstElement(); |
---|
| 211 | while(elemCL != NULL) |
---|
| 212 | { |
---|
| 213 | if (elemCL->getName() && !strcasecmp(inputSplits.getString(0), elemCL->getName())) |
---|
| 214 | { |
---|
| 215 | //elemCL->getName(); |
---|
| 216 | classID = ClassList::StringToID(elemCL->getName()); |
---|
| 217 | commandClass = elemCL; |
---|
| 218 | objectList = ClassList::getList(classID); |
---|
| 219 | break; |
---|
| 220 | } |
---|
| 221 | elemCL = itCL->nextElement(); |
---|
| 222 | } |
---|
| 223 | delete itCL; |
---|
| 224 | } |
---|
[5200] | 225 | |
---|
[5329] | 226 | if (commandClass != NULL && inputSplits.getCount() >= 2) |
---|
[5203] | 227 | { |
---|
[5329] | 228 | if (objectList != NULL) |
---|
[5203] | 229 | { |
---|
[5329] | 230 | // Checking for a Match in the Objects of classID (else take the first) |
---|
| 231 | tIterator<BaseObject>* itBO = objectList->getIterator(); |
---|
| 232 | BaseObject* enumBO = itBO->firstElement(); |
---|
| 233 | while(enumBO) |
---|
[5203] | 234 | { |
---|
[5329] | 235 | if (enumBO->getName() != NULL && !strcasecmp(enumBO->getName(), inputSplits.getString(1))) |
---|
| 236 | { |
---|
| 237 | objectPointer = enumBO; |
---|
| 238 | fktPos = 2; |
---|
| 239 | break; |
---|
| 240 | } |
---|
| 241 | enumBO = itBO->nextElement(); |
---|
| 242 | } |
---|
| 243 | delete itBO; |
---|
[5203] | 244 | |
---|
| 245 | // |
---|
[5329] | 246 | if (objectPointer == NULL) |
---|
| 247 | objectPointer = objectList->firstElement(); |
---|
| 248 | } |
---|
[5203] | 249 | // match a function. |
---|
[5329] | 250 | if (commandClass != NULL && (fktPos == 1 || (fktPos == 2 && inputSplits.getCount() >= 3))) |
---|
[5203] | 251 | { |
---|
[5636] | 252 | tIterator<ShellCommand>* itCMD = commandClass->commandList->getIterator(); |
---|
| 253 | ShellCommand* enumCMD = itCMD->firstElement(); |
---|
[5203] | 254 | while (enumCMD != NULL) |
---|
| 255 | { |
---|
| 256 | if (!strcmp(enumCMD->getName(), inputSplits.getString(fktPos))) |
---|
| 257 | { |
---|
[5640] | 258 | if (objectPointer == NULL && enumCMD->executor->getType() == Executor_Objective) |
---|
[5329] | 259 | { |
---|
| 260 | delete itCMD; |
---|
| 261 | return false; |
---|
| 262 | } |
---|
[5203] | 263 | if (inputSplits.getCount() > fktPos+1) |
---|
[5637] | 264 | enumCMD->executor->execute(objectPointer, executionString+inputSplits.getOffset(fktPos +1)); |
---|
[5203] | 265 | else |
---|
[5637] | 266 | enumCMD->executor->execute(objectPointer, ""); |
---|
[5203] | 267 | delete itCMD; |
---|
| 268 | return true; |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | enumCMD = itCMD->nextElement(); |
---|
| 272 | } |
---|
| 273 | delete itCMD; |
---|
| 274 | } |
---|
| 275 | } |
---|
[5198] | 276 | } |
---|
[5135] | 277 | } |
---|
[5148] | 278 | |
---|
[5166] | 279 | /** |
---|
| 280 | * lets a command be described |
---|
| 281 | * @param description the description of the Given command |
---|
| 282 | */ |
---|
[5636] | 283 | ShellCommand* ShellCommand::describe(const char* description) |
---|
[5164] | 284 | { |
---|
| 285 | if (this == NULL) |
---|
| 286 | return NULL; |
---|
[5165] | 287 | else |
---|
| 288 | { |
---|
| 289 | this->description = description; |
---|
| 290 | return this; |
---|
| 291 | } |
---|
[5164] | 292 | } |
---|
| 293 | |
---|
[5197] | 294 | /** |
---|
| 295 | * adds an Alias to this Command |
---|
| 296 | * @param alias the name of the Alias to set |
---|
| 297 | * @returns itself |
---|
| 298 | */ |
---|
[5636] | 299 | ShellCommand* ShellCommand::setAlias(const char* alias) |
---|
[5195] | 300 | { |
---|
[5196] | 301 | if (this == NULL) |
---|
| 302 | return NULL; |
---|
| 303 | |
---|
| 304 | if (this->alias != NULL) |
---|
| 305 | { |
---|
| 306 | PRINTF(2)("not more than one Alias allowed for functions (%s::%s)\n", this->getName(), this->shellClass->getName()); |
---|
| 307 | } |
---|
| 308 | else |
---|
| 309 | { |
---|
| 310 | if (ShellCommandClass::aliasList == NULL) |
---|
| 311 | ShellCommandClass::aliasList = new tList<ShellCommandAlias>; |
---|
| 312 | |
---|
| 313 | ShellCommandAlias* aliasCMD = new ShellCommandAlias(alias, this); |
---|
| 314 | ShellCommandClass::aliasList->add(aliasCMD); |
---|
| 315 | this->alias = aliasCMD; |
---|
| 316 | } |
---|
| 317 | return this; |
---|
[5195] | 318 | } |
---|
| 319 | |
---|
[5166] | 320 | /** |
---|
[5207] | 321 | * sets default Values of the Commands |
---|
| 322 | * @param count how many default Values to set. |
---|
| 323 | * @param ... the default Values in order. They will be cast to the right type |
---|
| 324 | * @returns itself |
---|
| 325 | * |
---|
| 326 | * Be aware, that when you use this Function, you !!MUST!! match the input as |
---|
| 327 | * count, [EXACTLY THE SAME AS IF YOU WOULD CALL THE FUNCTION UP TO count ARGUMENTS] |
---|
| 328 | */ |
---|
[5636] | 329 | ShellCommand* ShellCommand::defaultValues(unsigned int count, ...) |
---|
[5207] | 330 | { |
---|
| 331 | if (this == NULL) |
---|
| 332 | return NULL; |
---|
| 333 | |
---|
[5640] | 334 | va_list values; |
---|
| 335 | va_start(values, count); |
---|
[5207] | 336 | |
---|
[5640] | 337 | this->executor->defaultValues(count, values); |
---|
[5552] | 338 | |
---|
[5207] | 339 | return this; |
---|
| 340 | } |
---|
| 341 | |
---|
| 342 | /** |
---|
[5166] | 343 | * prints out nice information about the Shells Commands |
---|
| 344 | */ |
---|
[5636] | 345 | void ShellCommand::debug() |
---|
[5148] | 346 | { |
---|
[5170] | 347 | if (ShellCommandClass::commandClassList == NULL) |
---|
[5148] | 348 | { |
---|
[5171] | 349 | PRINT(0)("No Command registered.\n"); |
---|
[5148] | 350 | return; |
---|
| 351 | } |
---|
| 352 | |
---|
[5170] | 353 | tIterator<ShellCommandClass>* iteratorCL = ShellCommandClass::commandClassList->getIterator(); |
---|
| 354 | ShellCommandClass* elemCL = iteratorCL->firstElement(); |
---|
| 355 | while(elemCL != NULL) |
---|
[5148] | 356 | { |
---|
[5171] | 357 | PRINT(0)("Class:'%s' registered %d commands: \n", elemCL->className, elemCL->commandList->getSize()); |
---|
[5636] | 358 | tIterator<ShellCommand>* iterator = elemCL->commandList->getIterator(); |
---|
| 359 | const ShellCommand* elem = iterator->firstElement(); |
---|
[5172] | 360 | while(elem != NULL) |
---|
[5170] | 361 | { |
---|
[5642] | 362 | PRINT(0)(" command:'%s' : params:%d: ", elem->getName(), elem->executor->getParamCount()); |
---|
| 363 | /// FIXME |
---|
| 364 | /* for (unsigned int i = 0; i< elem->paramCount; i++) |
---|
| 365 | printf("%s ", ShellCommand::paramToString(elem->parameters[i]));*/ |
---|
[5170] | 366 | if (elem->description != NULL) |
---|
| 367 | printf("- %s", elem->description); |
---|
| 368 | printf("\n"); |
---|
[5148] | 369 | |
---|
[5170] | 370 | elem = iterator->nextElement(); |
---|
| 371 | } |
---|
| 372 | delete iterator; |
---|
| 373 | elemCL = iteratorCL->nextElement(); |
---|
[5148] | 374 | } |
---|
[5170] | 375 | delete iteratorCL; |
---|
[5148] | 376 | } |
---|
| 377 | |
---|
[5166] | 378 | /** |
---|
| 379 | * converts a Parameter to a String |
---|
| 380 | * @param parameter the Parameter we have. |
---|
| 381 | * @returns the Name of the Parameter at Hand |
---|
| 382 | */ |
---|
[5636] | 383 | const char* ShellCommand::paramToString(long parameter) |
---|
[5148] | 384 | { |
---|
[5634] | 385 | return MultiType::MultiTypeToString((MT_Type)parameter); |
---|
| 386 | // FIXME |
---|
| 387 | /* switch (parameter) |
---|
[5148] | 388 | { |
---|
| 389 | case ParameterBool: |
---|
| 390 | return "BOOL"; |
---|
| 391 | break; |
---|
| 392 | case ParameterChar: |
---|
| 393 | return "CHAR"; |
---|
| 394 | break; |
---|
| 395 | case ParameterString: |
---|
| 396 | return "STRING"; |
---|
| 397 | break; |
---|
| 398 | case ParameterInt: |
---|
| 399 | return "INT"; |
---|
| 400 | break; |
---|
| 401 | case ParameterUInt: |
---|
| 402 | return "UINT"; |
---|
| 403 | break; |
---|
| 404 | case ParameterFloat: |
---|
| 405 | return "FLOAT"; |
---|
| 406 | break; |
---|
| 407 | case ParameterLong: |
---|
| 408 | return "LONG"; |
---|
| 409 | break; |
---|
| 410 | default: |
---|
| 411 | return "NULL"; |
---|
| 412 | break; |
---|
[5634] | 413 | }*/ |
---|
[5148] | 414 | } |
---|