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