1 | /* |
---|
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. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ |
---|
17 | |
---|
18 | #include "shell_command.h" |
---|
19 | |
---|
20 | #include "list.h" |
---|
21 | #include "debug.h" |
---|
22 | #include "class_list.h" |
---|
23 | |
---|
24 | #include "key_names.h" |
---|
25 | #include <stdarg.h> |
---|
26 | #include <stdio.h> |
---|
27 | |
---|
28 | using namespace std; |
---|
29 | |
---|
30 | /** |
---|
31 | * constructs and registers a new Command |
---|
32 | * @param commandName the name of the Command |
---|
33 | * @param className the name of the class to apply this command to |
---|
34 | * @param paramCount the count of parameters this command takes |
---|
35 | * @return self |
---|
36 | */ |
---|
37 | ShellCommandBase::ShellCommandBase(const char* commandName, const char* className, unsigned int paramCount, ...) |
---|
38 | { |
---|
39 | this->setClassID(CL_SHELL_COMMAND, "ShellCommand"); |
---|
40 | this->setName(commandName); |
---|
41 | this->description = NULL; |
---|
42 | |
---|
43 | // this->classID = classID; |
---|
44 | this->className = className; //ClassList::IDToString(classID); |
---|
45 | |
---|
46 | // handling parameters, and storing them: |
---|
47 | if (paramCount > FUNCTOR_MAX_ARGUMENTS) |
---|
48 | paramCount = FUNCTOR_MAX_ARGUMENTS; |
---|
49 | this->paramCount = paramCount; |
---|
50 | this->parameters = new unsigned int[paramCount]; |
---|
51 | |
---|
52 | va_list parameterList; |
---|
53 | va_start(parameterList, paramCount); |
---|
54 | |
---|
55 | for (unsigned int i = 0; i < paramCount; i++) |
---|
56 | { |
---|
57 | this->parameters[i] = va_arg(parameterList, int); |
---|
58 | |
---|
59 | switch (this->parameters[i]) |
---|
60 | { |
---|
61 | case ParameterBool: |
---|
62 | this->defaultBools[i] = va_arg(parameterList, int); |
---|
63 | break; |
---|
64 | case ParameterChar: |
---|
65 | this->defaultStrings[i] = new char[2]; |
---|
66 | sprintf(this->defaultStrings[0], "%c", va_arg(parameterList, int)); |
---|
67 | break; |
---|
68 | case ParameterString: |
---|
69 | this->defaultStrings[i] = va_arg(parameterList, char*); |
---|
70 | break; |
---|
71 | case ParameterInt: |
---|
72 | this->defaultInts[i] = va_arg(parameterList, int); |
---|
73 | break; |
---|
74 | case ParameterUInt: |
---|
75 | this->defaultInts[i] = va_arg(parameterList, unsigned int); |
---|
76 | break; |
---|
77 | case ParameterFloat: |
---|
78 | this->defaultFloats[i] = va_arg(parameterList, double); |
---|
79 | break; |
---|
80 | case ParameterLong: |
---|
81 | this->defaultInts[i] = va_arg(parameterList, long); |
---|
82 | break; |
---|
83 | default: |
---|
84 | break; |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | // adding this ShellCommand to the list of known Commands |
---|
89 | ShellCommandBase::commandList->add(this); |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * deconstructs a ShellCommand |
---|
94 | * @return |
---|
95 | */ |
---|
96 | ShellCommandBase::~ShellCommandBase() |
---|
97 | { |
---|
98 | delete[] this->parameters; |
---|
99 | } |
---|
100 | |
---|
101 | /** |
---|
102 | * unregisters all Commands that exist |
---|
103 | */ |
---|
104 | void ShellCommandBase::unregisterAllCommands() |
---|
105 | { |
---|
106 | tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator(); |
---|
107 | ShellCommandBase* elem = iterator->firstElement(); |
---|
108 | while(elem != NULL) |
---|
109 | { |
---|
110 | delete elem; |
---|
111 | |
---|
112 | elem = iterator->nextElement(); |
---|
113 | } |
---|
114 | delete iterator; |
---|
115 | |
---|
116 | delete ShellCommandBase::commandList; |
---|
117 | ShellCommandBase::commandList = NULL; |
---|
118 | } |
---|
119 | |
---|
120 | /** |
---|
121 | * unregister an existing commandName |
---|
122 | * @param className the name of the Class the command belongs to. |
---|
123 | * @param commandName the name of the command itself |
---|
124 | * |
---|
125 | * @todo implement |
---|
126 | */ |
---|
127 | void ShellCommandBase::unregisterCommand(const char* commandName, const char* className) |
---|
128 | { |
---|
129 | PRINTF(1)("IMPLEMENT THIS\n"); |
---|
130 | } |
---|
131 | |
---|
132 | tList<ShellCommandBase>* ShellCommandBase::commandList = NULL; |
---|
133 | |
---|
134 | |
---|
135 | /** |
---|
136 | * checks if a command has already been registered. |
---|
137 | * @param commandName the name of the Command |
---|
138 | * @param className the name of the Class the command should apply to. |
---|
139 | * @param paramCount how many arguments the Command takes |
---|
140 | * @returns true, if the command is registered/false otherwise |
---|
141 | * |
---|
142 | * This is used internally, to see, if we have multiple command subscriptions. |
---|
143 | * This is checked in the registerCommand-function. |
---|
144 | */ |
---|
145 | bool ShellCommandBase::isRegistered(const char* commandName, const char* className, unsigned int paramCount, ...) |
---|
146 | { |
---|
147 | va_list parameterList; |
---|
148 | va_start(parameterList, paramCount); |
---|
149 | |
---|
150 | if (ShellCommandBase::commandList == NULL) |
---|
151 | { |
---|
152 | ShellCommandBase::commandList = new tList<ShellCommandBase>; |
---|
153 | ShellCommand<ShellCommandBase>::registerCommand("debug", "ShellCommand", &ShellCommandBase::debugDyn); |
---|
154 | return false; |
---|
155 | } |
---|
156 | |
---|
157 | tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator(); |
---|
158 | ShellCommandBase* elem = iterator->firstElement(); |
---|
159 | while(elem != NULL) |
---|
160 | { |
---|
161 | if (!strcmp(className, elem->className) && !strcmp(commandName, elem->getName())) |
---|
162 | { |
---|
163 | PRINTF(2)("Command already registered\n"); |
---|
164 | delete iterator; |
---|
165 | return true; |
---|
166 | } |
---|
167 | elem = iterator->nextElement(); |
---|
168 | } |
---|
169 | delete iterator; |
---|
170 | return false; |
---|
171 | } |
---|
172 | |
---|
173 | |
---|
174 | /** |
---|
175 | * executes commands |
---|
176 | * @param executionString the string containing the following input |
---|
177 | * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]] |
---|
178 | * @return true on success, false otherwise. |
---|
179 | */ |
---|
180 | bool ShellCommandBase::execute(const char* executionString) |
---|
181 | { |
---|
182 | if (ShellCommandBase::commandList == NULL) |
---|
183 | return false; |
---|
184 | |
---|
185 | tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator(); |
---|
186 | ShellCommandBase* elem = iterator->firstElement(); |
---|
187 | while(elem != NULL) |
---|
188 | { |
---|
189 | printf("%s::%s\n", elem->className, elem->getName()); |
---|
190 | if (!strncasecmp (executionString, elem->className, strlen(elem->className)) && |
---|
191 | (*(executionString+strlen(elem->className)) == ' ' || |
---|
192 | *(executionString+strlen(elem->className)) == ':' )) |
---|
193 | { |
---|
194 | const char* commandBegin = executionString + strlen(elem->className); |
---|
195 | |
---|
196 | PRINTF(4)("Class %s matches\n", elem->className); |
---|
197 | BaseObject* objectPointer = NULL; |
---|
198 | if (ClassList::StringToID(elem->className) & CL_MASK_SINGLETON == CL_MASK_SINGLETON) |
---|
199 | { |
---|
200 | while(*commandBegin == ' ') |
---|
201 | commandBegin++; |
---|
202 | if (strncmp (commandBegin, elem->getName(), strlen(elem->getName())) || |
---|
203 | *(commandBegin + strlen(elem->getName())) != ' ' && |
---|
204 | *(commandBegin + strlen(elem->getName())) != '\0') |
---|
205 | { |
---|
206 | elem = iterator->nextElement(); |
---|
207 | continue; |
---|
208 | } |
---|
209 | PRINTF(4)("Command %s matches\n", elem->getName()); |
---|
210 | // getting singleton-reference |
---|
211 | tList<BaseObject>* list = ClassList::getList(elem->className); |
---|
212 | if (list != NULL) |
---|
213 | objectPointer = list->firstElement(); |
---|
214 | } |
---|
215 | else |
---|
216 | { |
---|
217 | // checking for the Object |
---|
218 | while(*commandBegin == ' ') |
---|
219 | commandBegin++; |
---|
220 | tList<BaseObject>* list = ClassList::getList(elem->className); |
---|
221 | if (list == NULL) |
---|
222 | break; |
---|
223 | tIterator<BaseObject>* iterBO = list->getIterator(); |
---|
224 | BaseObject* enumBO = iterBO->firstElement(); |
---|
225 | while(enumBO != NULL) |
---|
226 | { |
---|
227 | if(!strncmp(commandBegin, enumBO->getName(), strlen(enumBO->getName()))) |
---|
228 | { |
---|
229 | PRINTF(4)("Object %s matches\n", enumBO->getName()); |
---|
230 | objectPointer = enumBO; |
---|
231 | break; |
---|
232 | } |
---|
233 | enumBO = iterBO->nextElement(); |
---|
234 | } |
---|
235 | delete iterBO; |
---|
236 | |
---|
237 | // break on no object Found. We cannot operate on Classes, but on Objects |
---|
238 | if (objectPointer == NULL) |
---|
239 | break; |
---|
240 | commandBegin = commandBegin + strlen(objectPointer->getName()); |
---|
241 | while(*commandBegin == ' ') |
---|
242 | commandBegin++; |
---|
243 | // checking for the requested function. |
---|
244 | if (strncmp (commandBegin, elem->getName(), strlen(elem->getName()))) |
---|
245 | { |
---|
246 | elem = iterator->nextElement(); |
---|
247 | continue; |
---|
248 | } |
---|
249 | PRINTF(4)("Function '%s' found\n", commandBegin); |
---|
250 | } |
---|
251 | const char* paramBegin = strchr(commandBegin, ' '); |
---|
252 | if (paramBegin == NULL) |
---|
253 | paramBegin = commandBegin + strlen(elem->getName()); |
---|
254 | while (*paramBegin == ' ') |
---|
255 | paramBegin++; |
---|
256 | |
---|
257 | PRINTF(3)("Parameters to Pass: %s\n", paramBegin); |
---|
258 | if (objectPointer != NULL && paramBegin != NULL) |
---|
259 | { |
---|
260 | elem->executeCommand(objectPointer, paramBegin); |
---|
261 | delete iterator; |
---|
262 | return true; |
---|
263 | } |
---|
264 | } |
---|
265 | elem = iterator->nextElement(); |
---|
266 | } |
---|
267 | delete iterator; |
---|
268 | return true; |
---|
269 | } |
---|
270 | |
---|
271 | /** |
---|
272 | * lets a command be described |
---|
273 | * @param description the description of the Given command |
---|
274 | */ |
---|
275 | ShellCommandBase* ShellCommandBase::describe(const char* description) |
---|
276 | { |
---|
277 | if (this == NULL) |
---|
278 | return NULL; |
---|
279 | else |
---|
280 | { |
---|
281 | this->description = description; |
---|
282 | return this; |
---|
283 | } |
---|
284 | } |
---|
285 | |
---|
286 | /** |
---|
287 | * see ShellCommandBase::debug() |
---|
288 | */ |
---|
289 | void ShellCommandBase::debugDyn() |
---|
290 | { |
---|
291 | this->debug(); |
---|
292 | } |
---|
293 | |
---|
294 | /** |
---|
295 | * prints out nice information about the Shells Commands |
---|
296 | */ |
---|
297 | void ShellCommandBase::debug() |
---|
298 | { |
---|
299 | if (ShellCommandBase::commandList == NULL) |
---|
300 | { |
---|
301 | PRINT(0)("No Command registered so far\n"); |
---|
302 | return; |
---|
303 | } |
---|
304 | |
---|
305 | tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator(); |
---|
306 | ShellCommandBase* elem = iterator->firstElement(); |
---|
307 | while(elem != NULL) |
---|
308 | { |
---|
309 | PRINT(0)("Class:'%s':command:'%s':params:%d: ", elem->className, elem->getName(), elem->paramCount); |
---|
310 | for (unsigned int i = 0; i< elem->paramCount; i++) |
---|
311 | printf("%s ", ShellCommandBase::paramToString(elem->parameters[i])); |
---|
312 | if (elem->description != NULL) |
---|
313 | printf("- %s", elem->description); |
---|
314 | printf("\n"); |
---|
315 | |
---|
316 | elem = iterator->nextElement(); |
---|
317 | } |
---|
318 | delete iterator; |
---|
319 | } |
---|
320 | |
---|
321 | /** |
---|
322 | * converts a Parameter to a String |
---|
323 | * @param parameter the Parameter we have. |
---|
324 | * @returns the Name of the Parameter at Hand |
---|
325 | */ |
---|
326 | const char* ShellCommandBase::paramToString(long parameter) |
---|
327 | { |
---|
328 | switch (parameter) |
---|
329 | { |
---|
330 | case ParameterBool: |
---|
331 | return "BOOL"; |
---|
332 | break; |
---|
333 | case ParameterChar: |
---|
334 | return "CHAR"; |
---|
335 | break; |
---|
336 | case ParameterString: |
---|
337 | return "STRING"; |
---|
338 | break; |
---|
339 | case ParameterInt: |
---|
340 | return "INT"; |
---|
341 | break; |
---|
342 | case ParameterUInt: |
---|
343 | return "UINT"; |
---|
344 | break; |
---|
345 | case ParameterFloat: |
---|
346 | return "FLOAT"; |
---|
347 | break; |
---|
348 | case ParameterLong: |
---|
349 | return "LONG"; |
---|
350 | break; |
---|
351 | default: |
---|
352 | return "NULL"; |
---|
353 | break; |
---|
354 | } |
---|
355 | } |
---|