[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 | |
---|
[5135] | 30 | ShellCommandBase::ShellCommandBase(const char* commandName, ClassID classID, unsigned int paramCount, ...) |
---|
[3365] | 31 | { |
---|
[5141] | 32 | this->setClassID(CL_SHELL_COMMAND, "ShellCommand"); |
---|
| 33 | this->setName(commandName); |
---|
| 34 | |
---|
[5135] | 35 | va_list parameters; |
---|
| 36 | va_start(parameters, paramCount); |
---|
| 37 | |
---|
[5129] | 38 | this->classID = classID; |
---|
[5141] | 39 | this->className = ClassList::IDToString(classID); |
---|
| 40 | |
---|
[5140] | 41 | if (classID & CL_MASK_SINGLETON == CL_MASK_SINGLETON) |
---|
| 42 | this->isSingleton = true; |
---|
| 43 | else |
---|
| 44 | this->isSingleton = false; |
---|
| 45 | |
---|
[5130] | 46 | // handling parameters, and storing them: |
---|
[5142] | 47 | if (paramCount > FUNCTOR_MAX_ARGUMENTS) |
---|
| 48 | paramCount = FUNCTOR_MAX_ARGUMENTS; |
---|
[5130] | 49 | this->paramCount = paramCount; |
---|
[5143] | 50 | this->parameters = new long[paramCount]; |
---|
[5130] | 51 | |
---|
| 52 | for (unsigned int i = 0; i < paramCount; i++) |
---|
[5142] | 53 | { |
---|
[5146] | 54 | this->parameters[i] = va_arg(parameters, long); |
---|
[5130] | 55 | |
---|
[5146] | 56 | switch (this->parameters[i]) |
---|
[5142] | 57 | { |
---|
| 58 | case ParameterBool: |
---|
| 59 | this->defaultBools[i] = va_arg(parameters, int); |
---|
| 60 | break; |
---|
| 61 | case ParameterChar: |
---|
| 62 | this->defaultStrings[i] = new char[2]; |
---|
| 63 | sprintf(this->defaultStrings[0], "%c", va_arg(parameters, int)); |
---|
| 64 | break; |
---|
| 65 | case ParameterString: |
---|
| 66 | this->defaultStrings[i] = va_arg(parameters, char*); |
---|
| 67 | break; |
---|
| 68 | case ParameterInt: |
---|
| 69 | this->defaultInts[i] = va_arg(parameters, int); |
---|
| 70 | break; |
---|
| 71 | case ParameterUInt: |
---|
| 72 | this->defaultInts[i] = va_arg(parameters, unsigned int); |
---|
| 73 | break; |
---|
| 74 | case ParameterFloat: |
---|
| 75 | this->defaultFloats[i] = va_arg(parameters, double); |
---|
| 76 | break; |
---|
| 77 | case ParameterLong: |
---|
| 78 | this->defaultInts[i] = va_arg(parameters, long); |
---|
| 79 | break; |
---|
| 80 | default: |
---|
| 81 | break; |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | |
---|
[5130] | 85 | // adding this ShellCommand to the list of known Commands |
---|
[5129] | 86 | ShellCommandBase::commandList->add(this); |
---|
[5068] | 87 | } |
---|
[4320] | 88 | |
---|
[5130] | 89 | ShellCommandBase::~ShellCommandBase() |
---|
| 90 | { |
---|
| 91 | delete[] this->parameters; |
---|
| 92 | } |
---|
[1853] | 93 | |
---|
[5093] | 94 | |
---|
[5129] | 95 | tList<ShellCommandBase>* ShellCommandBase::commandList = NULL; |
---|
[5079] | 96 | |
---|
[5135] | 97 | bool ShellCommandBase::isRegistered(const char* commandName, ClassID classID, unsigned int paramCount, ...) |
---|
[5113] | 98 | { |
---|
[5135] | 99 | va_list parameters; |
---|
| 100 | va_start(parameters, paramCount); |
---|
| 101 | |
---|
[5129] | 102 | if (ShellCommandBase::commandList == NULL) |
---|
[5072] | 103 | { |
---|
[5129] | 104 | ShellCommandBase::commandList = new tList<ShellCommandBase>; |
---|
[5113] | 105 | return false; |
---|
| 106 | } |
---|
[5105] | 107 | |
---|
[5129] | 108 | tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator(); |
---|
| 109 | ShellCommandBase* elem = iterator->firstElement(); |
---|
| 110 | while(elem != NULL) |
---|
[5113] | 111 | { |
---|
[5141] | 112 | if (classID == elem->classID && !strcmp(commandName, elem->getName())) |
---|
[5113] | 113 | { |
---|
[5140] | 114 | PRINTF(2)("Command already registered\n"); |
---|
[5129] | 115 | delete iterator; |
---|
| 116 | return true; |
---|
[5113] | 117 | } |
---|
[5129] | 118 | elem = iterator->nextElement(); |
---|
[5113] | 119 | } |
---|
[5129] | 120 | delete iterator; |
---|
[5140] | 121 | return false; |
---|
[5113] | 122 | } |
---|
| 123 | |
---|
[5140] | 124 | |
---|
[5145] | 125 | /** |
---|
| 126 | * executes commands |
---|
| 127 | * @param executionString the string containing the following input |
---|
| 128 | * <ClassName> [<ObjectName>] <functionName> [parameter1[,parameter2[,...]]] |
---|
| 129 | * @return true on success, false otherwise. |
---|
| 130 | */ |
---|
[5135] | 131 | bool ShellCommandBase::execute(const char* executionString) |
---|
| 132 | { |
---|
| 133 | if (ShellCommandBase::commandList == NULL) |
---|
| 134 | return false; |
---|
| 135 | |
---|
| 136 | tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator(); |
---|
| 137 | ShellCommandBase* elem = iterator->firstElement(); |
---|
| 138 | while(elem != NULL) |
---|
| 139 | { |
---|
[5141] | 140 | printf("%s\n", elem->getName()); |
---|
[5142] | 141 | if (!strncasecmp (executionString, elem->className, strlen(elem->className)) && |
---|
| 142 | (*(executionString+strlen(elem->className)) == ' ' || |
---|
| 143 | *(executionString+strlen(elem->className)) == ':' )) |
---|
[5135] | 144 | { |
---|
[5142] | 145 | const char* commandBegin = executionString + strlen(elem->className); |
---|
| 146 | |
---|
[5146] | 147 | PRINTF(4)("Class %s matches\n", elem->className); |
---|
[5142] | 148 | BaseObject* objectPointer = NULL; |
---|
| 149 | if (elem->isSingleton) |
---|
| 150 | { |
---|
| 151 | while(*commandBegin == ' ') |
---|
| 152 | commandBegin++; |
---|
| 153 | if (strncmp (commandBegin, elem->getName(), strlen(elem->getName()))) |
---|
[5144] | 154 | { |
---|
| 155 | elem = iterator->nextElement(); |
---|
[5143] | 156 | continue; |
---|
[5144] | 157 | } |
---|
| 158 | // getting singleton-reference |
---|
[5145] | 159 | tList<BaseObject>* list = ClassList::getList(elem->classID); |
---|
| 160 | if (list) |
---|
| 161 | objectPointer = list->firstElement(); |
---|
[5142] | 162 | } |
---|
| 163 | else |
---|
| 164 | { |
---|
[5144] | 165 | // checking for the Object |
---|
[5143] | 166 | while(*commandBegin == ' ') |
---|
| 167 | commandBegin++; |
---|
[5145] | 168 | tList<BaseObject>* list = ClassList::getList(elem->classID); |
---|
| 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 | } |
---|
[5145] | 197 | PRINTF(5)("Function '%s' found\n", commandBegin); |
---|
[5142] | 198 | } |
---|
[5144] | 199 | const char* paramBegin = strchr(commandBegin, ' '); |
---|
| 200 | if (paramBegin == NULL) |
---|
| 201 | paramBegin = commandBegin + strlen(elem->getName()); |
---|
[5142] | 202 | |
---|
[5146] | 203 | if (objectPointer != NULL && paramBegin != NULL) |
---|
[5143] | 204 | { |
---|
[5144] | 205 | elem->executeCommand(objectPointer, paramBegin); |
---|
[5143] | 206 | delete iterator; |
---|
| 207 | return true; |
---|
| 208 | } |
---|
[5135] | 209 | } |
---|
| 210 | elem = iterator->nextElement(); |
---|
| 211 | } |
---|
| 212 | delete iterator; |
---|
| 213 | return true; |
---|
| 214 | } |
---|