[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 | { |
---|
[5130] | 54 | parameters[i] = va_arg(parameters, long); |
---|
| 55 | |
---|
[5142] | 56 | switch (parameters[i]) |
---|
| 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 | |
---|
[5135] | 125 | bool ShellCommandBase::execute(const char* executionString) |
---|
| 126 | { |
---|
| 127 | if (ShellCommandBase::commandList == NULL) |
---|
| 128 | return false; |
---|
| 129 | |
---|
| 130 | tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator(); |
---|
| 131 | ShellCommandBase* elem = iterator->firstElement(); |
---|
| 132 | while(elem != NULL) |
---|
| 133 | { |
---|
[5141] | 134 | printf("%s\n", elem->getName()); |
---|
[5142] | 135 | if (!strncasecmp (executionString, elem->className, strlen(elem->className)) && |
---|
| 136 | (*(executionString+strlen(elem->className)) == ' ' || |
---|
| 137 | *(executionString+strlen(elem->className)) == ':' )) |
---|
[5135] | 138 | { |
---|
[5142] | 139 | const char* commandBegin = executionString + strlen(elem->className); |
---|
| 140 | |
---|
| 141 | PRINTF(4)("Class %s matches\n", elem->className); |
---|
| 142 | BaseObject* objectPointer = NULL; |
---|
| 143 | if (elem->isSingleton) |
---|
| 144 | { |
---|
| 145 | while(*commandBegin == ' ') |
---|
| 146 | commandBegin++; |
---|
| 147 | if (strncmp (commandBegin, elem->getName(), strlen(elem->getName()))) |
---|
[5143] | 148 | continue; |
---|
[5142] | 149 | objectPointer = ClassList::getList(elem->classID)->firstElement(); |
---|
| 150 | } |
---|
| 151 | else |
---|
| 152 | { |
---|
[5143] | 153 | while(*commandBegin == ' ') |
---|
| 154 | commandBegin++; |
---|
| 155 | tIterator<BaseObject>* iterBO = ClassList::getList(elem->classID)->getIterator(); |
---|
| 156 | BaseObject* enumBO = iterBO->firstElement(); |
---|
| 157 | { |
---|
| 158 | if( !strncmp(commandBegin, enumBO->getName(), strlen(enumBO->getName()))) |
---|
| 159 | { |
---|
| 160 | objectPointer = enumBO; |
---|
| 161 | break; |
---|
| 162 | } |
---|
| 163 | enumBO = iterBO->nextElement(); |
---|
| 164 | } |
---|
| 165 | delete iterBO; |
---|
[5142] | 166 | |
---|
[5143] | 167 | if (objectPointer == NULL) |
---|
| 168 | break; |
---|
| 169 | commandBegin = commandBegin + strlen(objectPointer->getName()); |
---|
| 170 | while(*commandBegin == ' ') |
---|
| 171 | commandBegin++; |
---|
[5142] | 172 | } |
---|
| 173 | const char* commandEnd = strchr(commandBegin, ' '); |
---|
| 174 | if (commandEnd == NULL) |
---|
[5143] | 175 | commandEnd = commandBegin + strlen(commandBegin); |
---|
[5142] | 176 | |
---|
| 177 | if (objectPointer != NULL) |
---|
[5143] | 178 | { |
---|
[5142] | 179 | elem->executeCommand(objectPointer, commandEnd); |
---|
[5143] | 180 | delete iterator; |
---|
| 181 | return true; |
---|
| 182 | } |
---|
[5135] | 183 | } |
---|
| 184 | elem = iterator->nextElement(); |
---|
| 185 | } |
---|
| 186 | delete iterator; |
---|
| 187 | return true; |
---|
| 188 | } |
---|