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