[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> |
---|
| 27 | |
---|
[1856] | 28 | using namespace std; |
---|
[1853] | 29 | |
---|
[5161] | 30 | ShellCommandBase::ShellCommandBase(const char* commandName, const char* className, unsigned int paramCount, ...) |
---|
[3365] | 31 | { |
---|
[5141] | 32 | this->setClassID(CL_SHELL_COMMAND, "ShellCommand"); |
---|
| 33 | this->setName(commandName); |
---|
[5164] | 34 | this->description = NULL; |
---|
[5141] | 35 | |
---|
[5161] | 36 | // this->classID = classID; |
---|
| 37 | this->className = className; //ClassList::IDToString(classID); |
---|
[5141] | 38 | |
---|
[5130] | 39 | // handling parameters, and storing them: |
---|
[5142] | 40 | if (paramCount > FUNCTOR_MAX_ARGUMENTS) |
---|
| 41 | paramCount = FUNCTOR_MAX_ARGUMENTS; |
---|
[5130] | 42 | this->paramCount = paramCount; |
---|
[5148] | 43 | this->parameters = new unsigned int[paramCount]; |
---|
[5130] | 44 | |
---|
[5148] | 45 | va_list parameterList; |
---|
| 46 | va_start(parameterList, paramCount); |
---|
| 47 | |
---|
[5130] | 48 | for (unsigned int i = 0; i < paramCount; i++) |
---|
[5142] | 49 | { |
---|
[5148] | 50 | this->parameters[i] = va_arg(parameterList, int); |
---|
[5130] | 51 | |
---|
[5146] | 52 | switch (this->parameters[i]) |
---|
[5142] | 53 | { |
---|
| 54 | case ParameterBool: |
---|
[5148] | 55 | this->defaultBools[i] = va_arg(parameterList, int); |
---|
[5142] | 56 | break; |
---|
| 57 | case ParameterChar: |
---|
| 58 | this->defaultStrings[i] = new char[2]; |
---|
[5148] | 59 | sprintf(this->defaultStrings[0], "%c", va_arg(parameterList, int)); |
---|
[5142] | 60 | break; |
---|
| 61 | case ParameterString: |
---|
[5148] | 62 | this->defaultStrings[i] = va_arg(parameterList, char*); |
---|
[5142] | 63 | break; |
---|
| 64 | case ParameterInt: |
---|
[5148] | 65 | this->defaultInts[i] = va_arg(parameterList, int); |
---|
[5142] | 66 | break; |
---|
| 67 | case ParameterUInt: |
---|
[5148] | 68 | this->defaultInts[i] = va_arg(parameterList, unsigned int); |
---|
[5142] | 69 | break; |
---|
| 70 | case ParameterFloat: |
---|
[5148] | 71 | this->defaultFloats[i] = va_arg(parameterList, double); |
---|
[5142] | 72 | break; |
---|
| 73 | case ParameterLong: |
---|
[5148] | 74 | this->defaultInts[i] = va_arg(parameterList, long); |
---|
[5142] | 75 | break; |
---|
| 76 | default: |
---|
| 77 | break; |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | |
---|
[5130] | 81 | // adding this ShellCommand to the list of known Commands |
---|
[5129] | 82 | ShellCommandBase::commandList->add(this); |
---|
[5068] | 83 | } |
---|
[4320] | 84 | |
---|
[5130] | 85 | ShellCommandBase::~ShellCommandBase() |
---|
| 86 | { |
---|
| 87 | delete[] this->parameters; |
---|
| 88 | } |
---|
[1853] | 89 | |
---|
[5129] | 90 | tList<ShellCommandBase>* ShellCommandBase::commandList = NULL; |
---|
[5079] | 91 | |
---|
[5161] | 92 | |
---|
| 93 | |
---|
| 94 | bool ShellCommandBase::isRegistered(const char* commandName, const char* className, unsigned int paramCount, ...) |
---|
[5113] | 95 | { |
---|
[5148] | 96 | va_list parameterList; |
---|
| 97 | va_start(parameterList, paramCount); |
---|
[5135] | 98 | |
---|
[5129] | 99 | if (ShellCommandBase::commandList == NULL) |
---|
[5072] | 100 | { |
---|
[5129] | 101 | ShellCommandBase::commandList = new tList<ShellCommandBase>; |
---|
[5161] | 102 | ShellCommand<ShellCommandBase>::registerCommand("debug", "ShellCommand", &ShellCommandBase::debugDyn); |
---|
[5113] | 103 | return false; |
---|
| 104 | } |
---|
[5105] | 105 | |
---|
[5129] | 106 | tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator(); |
---|
| 107 | ShellCommandBase* elem = iterator->firstElement(); |
---|
| 108 | while(elem != NULL) |
---|
[5113] | 109 | { |
---|
[5161] | 110 | if (!strcmp(className, elem->className) && !strcmp(commandName, elem->getName())) |
---|
[5113] | 111 | { |
---|
[5140] | 112 | PRINTF(2)("Command already registered\n"); |
---|
[5129] | 113 | delete iterator; |
---|
| 114 | return true; |
---|
[5113] | 115 | } |
---|
[5129] | 116 | elem = iterator->nextElement(); |
---|
[5113] | 117 | } |
---|
[5129] | 118 | delete iterator; |
---|
[5140] | 119 | return false; |
---|
[5113] | 120 | } |
---|
| 121 | |
---|
[5140] | 122 | |
---|
[5145] | 123 | /** |
---|
| 124 | * executes commands |
---|
| 125 | * @param executionString the string containing the following input |
---|
[5148] | 126 | * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]] |
---|
[5145] | 127 | * @return true on success, false otherwise. |
---|
| 128 | */ |
---|
[5135] | 129 | bool ShellCommandBase::execute(const char* executionString) |
---|
| 130 | { |
---|
| 131 | if (ShellCommandBase::commandList == NULL) |
---|
| 132 | return false; |
---|
| 133 | |
---|
| 134 | tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator(); |
---|
| 135 | ShellCommandBase* elem = iterator->firstElement(); |
---|
| 136 | while(elem != NULL) |
---|
| 137 | { |
---|
[5155] | 138 | printf("%s::%s\n", elem->className, elem->getName()); |
---|
[5142] | 139 | if (!strncasecmp (executionString, elem->className, strlen(elem->className)) && |
---|
| 140 | (*(executionString+strlen(elem->className)) == ' ' || |
---|
| 141 | *(executionString+strlen(elem->className)) == ':' )) |
---|
[5135] | 142 | { |
---|
[5142] | 143 | const char* commandBegin = executionString + strlen(elem->className); |
---|
| 144 | |
---|
[5146] | 145 | PRINTF(4)("Class %s matches\n", elem->className); |
---|
[5142] | 146 | BaseObject* objectPointer = NULL; |
---|
[5163] | 147 | if (ClassList::StringToID(elem->className) & CL_MASK_SINGLETON == CL_MASK_SINGLETON) |
---|
[5142] | 148 | { |
---|
| 149 | while(*commandBegin == ' ') |
---|
| 150 | commandBegin++; |
---|
[5154] | 151 | if (strncmp (commandBegin, elem->getName(), strlen(elem->getName())) || |
---|
| 152 | *(commandBegin + strlen(elem->getName())) != ' ' && |
---|
| 153 | *(commandBegin + strlen(elem->getName())) != '\0') |
---|
[5144] | 154 | { |
---|
| 155 | elem = iterator->nextElement(); |
---|
[5143] | 156 | continue; |
---|
[5144] | 157 | } |
---|
[5148] | 158 | PRINTF(4)("Command %s matches\n", elem->getName()); |
---|
[5144] | 159 | // getting singleton-reference |
---|
[5161] | 160 | tList<BaseObject>* list = ClassList::getList(elem->className); |
---|
[5148] | 161 | if (list != NULL) |
---|
[5145] | 162 | objectPointer = list->firstElement(); |
---|
[5142] | 163 | } |
---|
| 164 | else |
---|
| 165 | { |
---|
[5144] | 166 | // checking for the Object |
---|
[5143] | 167 | while(*commandBegin == ' ') |
---|
| 168 | commandBegin++; |
---|
[5161] | 169 | tList<BaseObject>* list = ClassList::getList(elem->className); |
---|
[5145] | 170 | if (list == NULL) |
---|
| 171 | break; |
---|
| 172 | tIterator<BaseObject>* iterBO = list->getIterator(); |
---|
[5143] | 173 | BaseObject* enumBO = iterBO->firstElement(); |
---|
[5144] | 174 | while(enumBO != NULL) |
---|
[5143] | 175 | { |
---|
[5144] | 176 | if(!strncmp(commandBegin, enumBO->getName(), strlen(enumBO->getName()))) |
---|
[5143] | 177 | { |
---|
[5146] | 178 | PRINTF(4)("Object %s matches\n", enumBO->getName()); |
---|
[5143] | 179 | objectPointer = enumBO; |
---|
| 180 | break; |
---|
| 181 | } |
---|
| 182 | enumBO = iterBO->nextElement(); |
---|
| 183 | } |
---|
| 184 | delete iterBO; |
---|
[5142] | 185 | |
---|
[5144] | 186 | // break on no object Found. We cannot operate on Classes, but on Objects |
---|
[5143] | 187 | if (objectPointer == NULL) |
---|
| 188 | break; |
---|
| 189 | commandBegin = commandBegin + strlen(objectPointer->getName()); |
---|
| 190 | while(*commandBegin == ' ') |
---|
| 191 | commandBegin++; |
---|
[5144] | 192 | // checking for the requested function. |
---|
| 193 | if (strncmp (commandBegin, elem->getName(), strlen(elem->getName()))) |
---|
| 194 | { |
---|
| 195 | elem = iterator->nextElement(); |
---|
| 196 | continue; |
---|
| 197 | } |
---|
[5148] | 198 | PRINTF(4)("Function '%s' found\n", commandBegin); |
---|
[5142] | 199 | } |
---|
[5144] | 200 | const char* paramBegin = strchr(commandBegin, ' '); |
---|
| 201 | if (paramBegin == NULL) |
---|
| 202 | paramBegin = commandBegin + strlen(elem->getName()); |
---|
[5147] | 203 | while (*paramBegin == ' ') |
---|
| 204 | paramBegin++; |
---|
[5142] | 205 | |
---|
[5148] | 206 | PRINTF(3)("Parameters to Pass: %s\n", paramBegin); |
---|
[5146] | 207 | if (objectPointer != NULL && paramBegin != NULL) |
---|
[5143] | 208 | { |
---|
[5144] | 209 | elem->executeCommand(objectPointer, paramBegin); |
---|
[5143] | 210 | delete iterator; |
---|
| 211 | return true; |
---|
| 212 | } |
---|
[5135] | 213 | } |
---|
| 214 | elem = iterator->nextElement(); |
---|
| 215 | } |
---|
| 216 | delete iterator; |
---|
| 217 | return true; |
---|
| 218 | } |
---|
[5148] | 219 | |
---|
[5164] | 220 | |
---|
| 221 | ShellCommandBase* ShellCommandBase::describe(const char* description) |
---|
| 222 | { |
---|
| 223 | if (this == NULL) |
---|
| 224 | return NULL; |
---|
| 225 | |
---|
| 226 | this->description = description; |
---|
| 227 | return this; |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | |
---|
[5161] | 231 | void ShellCommandBase::debugDyn() |
---|
| 232 | { |
---|
| 233 | this->debug(); |
---|
| 234 | } |
---|
[5148] | 235 | |
---|
| 236 | void ShellCommandBase::debug() |
---|
| 237 | { |
---|
| 238 | if (ShellCommandBase::commandList == NULL) |
---|
| 239 | { |
---|
| 240 | PRINT(0)("No Command registered so far\n"); |
---|
| 241 | return; |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator(); |
---|
| 245 | ShellCommandBase* elem = iterator->firstElement(); |
---|
| 246 | while(elem != NULL) |
---|
| 247 | { |
---|
| 248 | PRINT(0)("Class %s registered command %s with %d parameters: ", elem->className, elem->getName(), elem->paramCount); |
---|
| 249 | for (unsigned int i = 0; i< elem->paramCount; i++) |
---|
[5164] | 250 | printf("%s ", ShellCommandBase::paramToString(elem->parameters[i])); |
---|
| 251 | if (elem->description != NULL) |
---|
| 252 | printf("- %s", elem->description); |
---|
[5148] | 253 | printf("\n"); |
---|
| 254 | |
---|
| 255 | elem = iterator->nextElement(); |
---|
| 256 | } |
---|
| 257 | delete iterator; |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | |
---|
| 261 | |
---|
| 262 | const char* ShellCommandBase::paramToString(long parameter) |
---|
| 263 | { |
---|
| 264 | switch (parameter) |
---|
| 265 | { |
---|
| 266 | case ParameterBool: |
---|
| 267 | return "BOOL"; |
---|
| 268 | break; |
---|
| 269 | case ParameterChar: |
---|
| 270 | return "CHAR"; |
---|
| 271 | break; |
---|
| 272 | case ParameterString: |
---|
| 273 | return "STRING"; |
---|
| 274 | break; |
---|
| 275 | case ParameterInt: |
---|
| 276 | return "INT"; |
---|
| 277 | break; |
---|
| 278 | case ParameterUInt: |
---|
| 279 | return "UINT"; |
---|
| 280 | break; |
---|
| 281 | case ParameterFloat: |
---|
| 282 | return "FLOAT"; |
---|
| 283 | break; |
---|
| 284 | case ParameterLong: |
---|
| 285 | return "LONG"; |
---|
| 286 | break; |
---|
| 287 | default: |
---|
| 288 | return "NULL"; |
---|
| 289 | break; |
---|
| 290 | } |
---|
| 291 | } |
---|