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